Re: [PATCHv3] usb: typec: tps6598x: handle block writes separately with plain-I2C adapters

From: Guenter Roeck
Date: Wed Feb 20 2019 - 10:36:28 EST


On 2/20/19 7:35 AM, Guenter Roeck wrote:
On 2/20/19 7:11 AM, Nikolaus Voss wrote:
From: Nikolaus Voss <nikolaus.voss@xxxxxxxxxxxxxxxxxxxxx>

Commit 1a2f474d328f handles block _reads_ separately with plain-I2C
adapters, but the problem described with regmap-i2c not handling
SMBus block transfers (i.e. read and writes) correctly also exists
with writes.

As workaround, this patch adds a block write function the same way
1a2f474d328f adds a block read function.

Fixes: 1a2f474d328f ("usb: typec: tps6598x: handle block reads separately with plain-I2C adapters")
Fixes: 0a4c005bd171 ("usb: typec: driver for TI TPS6598x USB Power Delivery controllers")
Signed-off-by: Nikolaus Voss <nikolaus.voss@xxxxxxxxxxxxxxxxxxxxx>

Reviewed-by: Guenter Roeck <linux@xxxxxxxxxxxx>

Note that tps6598x_exec_cmd() is only called with in_len == out_len == 0
and NULL data pointers. It might make sense to simplify the function and
drop the unused parameters as well as the associated code.

Clarification: That would be a separate patch.

Guenter

Guenter

---
v2: fix tps6598x_exec_cmd also
v3: use fixed length for stack buffer
---
 drivers/usb/typec/tps6598x.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c
index c84c8c189e90..eb8046f87a54 100644
--- a/drivers/usb/typec/tps6598x.c
+++ b/drivers/usb/typec/tps6598x.c
@@ -110,6 +110,20 @@ tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
ÂÂÂÂÂ return 0;
 }
+static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ void *val, size_t len)
+{
+ÂÂÂ u8 data[TPS_MAX_LEN + 1];
+
+ÂÂÂ if (!tps->i2c_protocol)
+ÂÂÂÂÂÂÂ return regmap_raw_write(tps->regmap, reg, val, len);
+
+ÂÂÂ data[0] = len;
+ÂÂÂ memcpy(&data[1], val, len);
+
+ÂÂÂ return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
+}
+
 static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
 {
ÂÂÂÂÂ return tps6598x_block_read(tps, reg, val, sizeof(u16));
@@ -127,23 +141,23 @@ static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
 static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
 {
-ÂÂÂ return regmap_raw_write(tps->regmap, reg, &val, sizeof(u16));
+ÂÂÂ return tps6598x_block_write(tps, reg, &val, sizeof(u16));
 }
 static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
 {
-ÂÂÂ return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
+ÂÂÂ return tps6598x_block_write(tps, reg, &val, sizeof(u32));
 }
 static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
 {
-ÂÂÂ return regmap_raw_write(tps->regmap, reg, &val, sizeof(u64));
+ÂÂÂ return tps6598x_block_write(tps, reg, &val, sizeof(u64));
 }
 static inline int
 tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
 {
-ÂÂÂ return regmap_raw_write(tps->regmap, reg, &val, sizeof(u32));
+ÂÂÂ return tps6598x_block_write(tps, reg, &val, sizeof(u32));
 }
 static int tps6598x_read_partner_identity(struct tps6598x *tps)
@@ -229,8 +243,8 @@ static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
ÂÂÂÂÂÂÂÂÂ return -EBUSY;
ÂÂÂÂÂ if (in_len) {
-ÂÂÂÂÂÂÂ ret = regmap_raw_write(tps->regmap, TPS_REG_DATA1,
-ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ in_data, in_len);
+ÂÂÂÂÂÂÂ ret = tps6598x_block_write(tps, TPS_REG_DATA1,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ in_data, in_len);
ÂÂÂÂÂÂÂÂÂ if (ret)
ÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
ÂÂÂÂÂ }