From d33ebd13741b7473e93a1a6780edbb1e2dca8258 Mon Sep 17 00:00:00 2001 From: Yidi Lin Date: Tue, 15 Mar 2016 14:38:44 +0800 Subject: device: Add i2c read/write register field API i2c_read_field() - read the value from the specific register field i2c_write_field() - write the value to the specific register field BRANCH=none BUG=none TEST=none Change-Id: I2098715b4583c1936c93b3ff45ec330910964304 Signed-off-by: Patrick Georgi Original-Commit-Id: 0817fc76d07491b39c066f1393a6435f0831b50c Original-Change-Id: I92c187a89d10cfcecf3dfd9291e0bc015459c393 Original-Signed-off-by: Yidi Lin Original-Reviewed-on: https://chromium-review.googlesource.com/332712 Original-Reviewed-by: Julius Werner Reviewed-on: https://review.coreboot.org/14105 Tested-by: build bot (Jenkins) Reviewed-by: Stefan Reinauer Reviewed-by: Martin Roth --- src/device/i2c.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/device/i2c.c (limited to 'src/device/i2c.c') diff --git a/src/device/i2c.c b/src/device/i2c.c new file mode 100644 index 0000000000..53bfdf0c94 --- /dev/null +++ b/src/device/i2c.c @@ -0,0 +1,46 @@ +/* + * This file is part of the coreboot project. + * + * Copyright 2016 MediaTek Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include + +int i2c_read_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t *data, + uint8_t mask, uint8_t shift) +{ + int ret; + uint8_t buf = 0; + + ret = i2c_readb(bus, chip, reg, &buf); + + buf &= (mask << shift); + *data = (buf >> shift); + + return ret; +} + +int i2c_write_field(unsigned bus, uint8_t chip, uint8_t reg, uint8_t data, + uint8_t mask, uint8_t shift) +{ + int ret; + uint8_t buf = 0; + + ret = i2c_readb(bus, chip, reg, &buf); + + buf &= ~(mask << shift); + buf |= (data << shift); + + ret |= i2c_writeb(bus, chip, reg, buf); + + return ret; +} -- cgit v1.2.3