2016-03-15 07:38:44 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-06-08 00:38:14 +02:00
|
|
|
#include <device/device.h>
|
2016-03-15 07:38:44 +01:00
|
|
|
#include <device/i2c.h>
|
2016-06-08 00:38:14 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#if ENV_RAMSTAGE
|
|
|
|
/* Return I2C operations for a bus */
|
|
|
|
static inline const struct i2c_bus_operations *ops_i2c_bus(struct bus *bus)
|
|
|
|
{
|
|
|
|
if (bus && bus->dev && bus->dev->ops)
|
|
|
|
return bus->dev->ops->ops_i2c_bus;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_dev_find_bus(struct device *dev)
|
|
|
|
{
|
|
|
|
const struct i2c_bus_operations *ops;
|
|
|
|
struct bus *pbus;
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Locate parent bus with I2C controller ops */
|
|
|
|
pbus = dev->bus;
|
|
|
|
while (pbus && pbus->dev && !ops_i2c_bus(pbus))
|
|
|
|
if (pbus->dev->bus != pbus)
|
|
|
|
pbus = pbus->dev->bus;
|
|
|
|
|
|
|
|
/* Check if this I2C controller ops implements dev_to_bus() */
|
|
|
|
ops = ops_i2c_bus(pbus);
|
|
|
|
if (!ops || !ops->dev_to_bus)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Use controller ops to determine the bus number */
|
|
|
|
return ops->dev_to_bus(pbus->dev);
|
|
|
|
}
|
|
|
|
|
i2c: Move to Linux like `struct i2c_msg`
Our current struct for I2C segments `i2c_seg` was close to being compa-
tible to the Linux version `i2c_msg`, close to being compatible to SMBus
and close to being readable (e.g. what was `chip` supposed to mean?) but
turned out to be hard to fix.
Instead of extending it in a backwards compatible way (and not touching
current controller drivers), replace it with a Linux source compatible
`struct i2c_msg` and patch all the drivers and users with Coccinelle.
The new `struct i2c_msg` should ease porting drivers from Linux and help
to write SMBus compatible controller drivers.
Beside integer type changes, the field `read` is replaced with a generic
field `flags` and `chip` is renamed to `slave`.
Patched with Coccinelle using the clumsy spatch below and some manual
changes:
* Nested struct initializers and one field access skipped by Coccinelle.
* Removed assumption in the code that I2C_M_RD is 1.
* In `i2c.h`, changed all occurences of `chip` to `slave`.
@@ @@
-struct i2c_seg
+struct i2c_msg
@@ identifier msg; expression e; @@
(
struct i2c_msg msg = {
- .read = 0,
+ .flags = 0,
};
|
struct i2c_msg msg = {
- .read = 1,
+ .flags = I2C_M_RD,
};
|
struct i2c_msg msg = {
- .chip = e,
+ .slave = e,
};
)
@@ struct i2c_msg msg; statement S1, S2; @@
(
-if (msg.read)
+if (msg.flags & I2C_M_RD)
S1 else S2
|
-if (msg.read)
+if (msg.flags & I2C_M_RD)
S1
)
@@ struct i2c_msg *msg; statement S1, S2; @@
(
-if (msg->read)
+if (msg->flags & I2C_M_RD)
S1 else S2
|
-if (msg->read)
+if (msg->flags & I2C_M_RD)
S1
)
@@ struct i2c_msg msg; expression e; @@
(
-msg.read = 0;
+msg.flags = 0;
|
-msg.read = 1;
+msg.flags = I2C_M_RD;
|
-msg.read = e;
+msg.flags = e ? I2C_M_RD : 0;
|
-!!(msg.read)
+(msg.flags & I2C_M_RD)
|
-(msg.read)
+(msg.flags & I2C_M_RD)
)
@@ struct i2c_msg *msg; expression e; @@
(
-msg->read = 0;
+msg->flags = 0;
|
-msg->read = 1;
+msg->flags = I2C_M_RD;
|
-msg->read = e;
+msg->flags = e ? I2C_M_RD : 0;
|
-!!(msg->read)
+(msg->flags & I2C_M_RD)
|
-(msg->read)
+(msg->flags & I2C_M_RD)
)
@@ struct i2c_msg msg; @@
-msg.chip
+msg.slave
@@ struct i2c_msg *msg; expression e; @@
-msg[e].chip
+msg[e].slave
@ slave disable ptr_to_array @ struct i2c_msg *msg; @@
-msg->chip
+msg->slave
Change-Id: Ifd7cabf0a18ffd7a1def25d1d7059b713d0b7ea9
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: https://review.coreboot.org/20542
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-by: Werner Zeh <werner.zeh@siemens.com>
Reviewed-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
2017-07-12 17:59:16 +02:00
|
|
|
int i2c_dev_transfer(struct device *dev, struct i2c_msg *segments, int count)
|
2016-06-08 00:38:14 +02:00
|
|
|
{
|
|
|
|
int bus = i2c_dev_find_bus(dev);
|
|
|
|
if (bus < 0)
|
|
|
|
return -1;
|
|
|
|
return i2c_transfer(bus, segments, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_dev_readb(struct device *dev, uint8_t reg, uint8_t *data)
|
|
|
|
{
|
|
|
|
int bus = i2c_dev_find_bus(dev);
|
|
|
|
if (bus < 0)
|
|
|
|
return -1;
|
|
|
|
return i2c_readb(bus, dev->path.i2c.device, reg, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_dev_writeb(struct device *dev, uint8_t reg, uint8_t data)
|
|
|
|
{
|
|
|
|
int bus = i2c_dev_find_bus(dev);
|
|
|
|
if (bus < 0)
|
|
|
|
return -1;
|
|
|
|
return i2c_writeb(bus, dev->path.i2c.device, reg, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_dev_read_bytes(struct device *dev, uint8_t reg, uint8_t *data, int len)
|
|
|
|
{
|
|
|
|
int bus = i2c_dev_find_bus(dev);
|
|
|
|
if (bus < 0)
|
|
|
|
return -1;
|
|
|
|
return i2c_read_bytes(bus, dev->path.i2c.device, reg, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_dev_read_raw(struct device *dev, uint8_t *data, int len)
|
|
|
|
{
|
|
|
|
int bus = i2c_dev_find_bus(dev);
|
|
|
|
if (bus < 0)
|
|
|
|
return -1;
|
|
|
|
return i2c_read_raw(bus, dev->path.i2c.device, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2c_dev_write_raw(struct device *dev, uint8_t *data, int len)
|
|
|
|
{
|
|
|
|
int bus = i2c_dev_find_bus(dev);
|
|
|
|
if (bus < 0)
|
|
|
|
return -1;
|
|
|
|
return i2c_write_raw(bus, dev->path.i2c.device, data, len);
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-15 07:38:44 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|