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>
This commit is contained in:
Nico Huber
2017-07-12 17:59:16 +02:00
committed by Martin Roth
parent 673e014fab
commit 029dfff30c
17 changed files with 192 additions and 144 deletions

View File

@@ -144,9 +144,10 @@ static int iic_tpm_read(uint8_t addr, uint8_t *buffer, size_t len)
* retries should usually not be needed, but are kept just to
* be safe on the safe side.
*/
struct i2c_seg aseg = { .read = 0, .chip = tpm_dev->addr,
struct i2c_msg aseg = { .flags = 0, .slave = tpm_dev->addr,
.buf = &addr, .len = 1 };
struct i2c_seg dseg = { .read = 1, .chip = tpm_dev->addr,
struct i2c_msg dseg = { .flags = I2C_M_RD,
.slave = tpm_dev->addr,
.buf = buffer, .len = len };
for (count = 0; count < MAX_COUNT; count++) {
rc = i2c_transfer(tpm_dev->bus, &aseg, 1) ||

View File

@@ -111,7 +111,7 @@ static TiLp55231 lp55231s[WW_RING_NUM_LED_CONTROLLERS];
* the transfer function ignores errors when accessing the reset register.
*/
static int ledc_transfer(TiLp55231 *ledc, struct i2c_seg *segs,
static int ledc_transfer(TiLp55231 *ledc, struct i2c_msg *segs,
int seg_count, int reset)
{
int rv, max_attempts = 2;
@@ -128,7 +128,7 @@ static int ledc_transfer(TiLp55231 *ledc, struct i2c_seg *segs,
if (!reset)
printk(BIOS_WARNING,
"%s: dev %#x, reg %#x, %s transaction error.\n",
__func__, segs->chip, segs->buf[0],
__func__, segs->slave, segs->buf[0],
seg_count == 1 ? "write" : "read");
else
rv = 0;
@@ -144,7 +144,7 @@ static int ledc_transfer(TiLp55231 *ledc, struct i2c_seg *segs,
static int ledc_write(TiLp55231 *ledc, uint8_t start_addr,
const uint8_t *data, unsigned count)
{
struct i2c_seg seg;
struct i2c_msg seg;
if (count > (sizeof(ledc->data_buffer) - 1)) {
printk(BIOS_WARNING, "%s: transfer size too large (%d bytes)\n",
@@ -155,8 +155,8 @@ static int ledc_write(TiLp55231 *ledc, uint8_t start_addr,
memcpy(ledc->data_buffer + 1, data, count);
ledc->data_buffer[0] = start_addr;
seg.read = 0;
seg.chip = ledc->dev_addr;
seg.flags = 0;
seg.slave = ledc->dev_addr;
seg.buf = ledc->data_buffer;
seg.len = count + 1;
@@ -166,15 +166,15 @@ static int ledc_write(TiLp55231 *ledc, uint8_t start_addr,
/* To keep things simple, read is limited to one byte at a time. */
static int ledc_read(TiLp55231 *ledc, uint8_t addr, uint8_t *data)
{
struct i2c_seg seg[2];
struct i2c_msg seg[2];
seg[0].read = 0;
seg[0].chip = ledc->dev_addr;
seg[0].flags = 0;
seg[0].slave = ledc->dev_addr;
seg[0].buf = &addr;
seg[0].len = 1;
seg[1].read = 1;
seg[1].chip = ledc->dev_addr;
seg[1].flags = I2C_M_RD;
seg[1].slave = ledc->dev_addr;
seg[1].buf = data;
seg[1].len = 1;