581738642f
Do not use the global platform_i2c_transfer() function that can only be implemented by a single driver. Instead, make a `struct device` aware transfer() function the only interface function for I2C controller dri- vers to implement. To not force the slave device drivers to be implemented either above generic I2C or specialized SMBus operations, we support SMBus control- lers in the slave device interface too. We start with four simple slave functions: i2c_readb(), i2c_writeb(), i2c_readb_at() and i2c_writeb_at(). They are all compatible to respec- tive SMBus functions. But we keep aliases because it would be weird to force e.g. an I2C EEPROM driver to call smbus_read_byte(). Change-Id: I98386f91bf4799ba3df84ec8bc0f64edd4142818 Signed-off-by: Nico Huber <nico.huber@secunet.com> Reviewed-on: https://review.coreboot.org/20846 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Aaron Durbin <adurbin@chromium.org> Reviewed-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
/*
|
|
* 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 <device/i2c_simple.h>
|
|
#include <stdint.h>
|
|
|
|
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;
|
|
}
|