From 029dfff30cc468b3e21c2004a135d3380a8c20e6 Mon Sep 17 00:00:00 2001 From: Nico Huber Date: Wed, 12 Jul 2017 17:59:16 +0200 Subject: i2c: Move to Linux like `struct i2c_msg` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-on: https://review.coreboot.org/20542 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin Reviewed-by: Werner Zeh Reviewed-by: Kyösti Mälkki Reviewed-by: Paul Menzel --- src/soc/rockchip/common/i2c.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/soc/rockchip/common') diff --git a/src/soc/rockchip/common/i2c.c b/src/soc/rockchip/common/i2c.c index 032efda9b2..5847f1658f 100644 --- a/src/soc/rockchip/common/i2c.c +++ b/src/soc/rockchip/common/i2c.c @@ -124,7 +124,7 @@ static int i2c_send_stop(struct rk_i2c_regs *reg_addr) return res; } -static int i2c_read(struct rk_i2c_regs *reg_addr, struct i2c_seg segment) +static int i2c_read(struct rk_i2c_regs *reg_addr, struct i2c_msg segment) { int res = 0; uint8_t *data = segment.buf; @@ -136,7 +136,7 @@ static int i2c_read(struct rk_i2c_regs *reg_addr, struct i2c_seg segment) unsigned int con = 0; unsigned int i, j; - write32(®_addr->i2c_mrxaddr, I2C_8BIT | segment.chip << 1 | 1); + write32(®_addr->i2c_mrxaddr, I2C_8BIT | segment.slave << 1 | 1); write32(®_addr->i2c_mrxraddr, 0); con = I2C_MODE_TRX | I2C_EN | I2C_ACT2NAK; while (bytes_remaining) { @@ -182,7 +182,7 @@ static int i2c_read(struct rk_i2c_regs *reg_addr, struct i2c_seg segment) return res; } -static int i2c_write(struct rk_i2c_regs *reg_addr, struct i2c_seg segment) +static int i2c_write(struct rk_i2c_regs *reg_addr, struct i2c_msg segment) { int res = 0; uint8_t *data = segment.buf; @@ -194,7 +194,7 @@ static int i2c_write(struct rk_i2c_regs *reg_addr, struct i2c_seg segment) unsigned int j = 1; u32 txdata = 0; - txdata |= (segment.chip << 1); + txdata |= (segment.slave << 1); while (bytes_remaining) { bytes_transferred = MIN(bytes_remaining, 32); words_transferred = ALIGN_UP(bytes_transferred, 4) / 4; @@ -239,25 +239,26 @@ static int i2c_write(struct rk_i2c_regs *reg_addr, struct i2c_seg segment) return res; } -static int i2c_do_xfer(void *reg_addr, struct i2c_seg segment) +static int i2c_do_xfer(void *reg_addr, struct i2c_msg segment) { int res = 0; if (i2c_send_start(reg_addr)) return I2C_TIMEOUT; - if (segment.read) + if (segment.flags & I2C_M_RD) res = i2c_read(reg_addr, segment); else res = i2c_write(reg_addr, segment); return i2c_send_stop(reg_addr) || res; } -int platform_i2c_transfer(unsigned bus, struct i2c_seg *segments, int seg_count) +int platform_i2c_transfer(unsigned bus, struct i2c_msg *segments, + int seg_count) { int i; int res = 0; struct rk_i2c_regs *regs = (struct rk_i2c_regs *)(i2c_bus[bus]); - struct i2c_seg *seg = segments; + struct i2c_msg *seg = segments; for (i = 0; i < seg_count; i++, seg++) { res = i2c_do_xfer(regs, *seg); -- cgit v1.2.3