aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHeikki Krogerus <heikki.krogerus@linux.intel.com>2018-04-25 10:22:09 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-05-03 13:55:31 -0400
commit1a2f474d328f292ee706414824ec4ca690cdf5ba (patch)
tree785580246bf5876d87b0715364df50c313baec46
parent5671a4608c326b8ec16dec2f0f32e64a33cdd317 (diff)
usb: typec: tps6598x: handle block reads separately with plain-I2C adapters
If the I2C adapter that the PD controller is attached to does not support SMBus protocol, the driver needs to handle block reads separately. The first byte returned in block read protocol will show the total number of bytes. It needs to be stripped away. This is handled separately in the driver only because right now we have no way of requesting the used protocol with regmap-i2c. This is in practice a workaround for what is really a problem in regmap-i2c. The other option would have been to register custom regmap, or not use regmap at all, however, since the solution is very simple, I choose to use it in this case for convenience. It is easy to remove once we figure out how to handle this kind of cases in regmap-i2c. Fixes: 0a4c005bd171 ("usb: typec: driver for TI TPS6598x USB Power Delivery controllers") Reviewed-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/usb/typec/tps6598x.c47
1 files changed, 39 insertions, 8 deletions
diff --git a/drivers/usb/typec/tps6598x.c b/drivers/usb/typec/tps6598x.c
index 8b8406867c02..4b4c8d271b27 100644
--- a/drivers/usb/typec/tps6598x.c
+++ b/drivers/usb/typec/tps6598x.c
@@ -73,6 +73,7 @@ struct tps6598x {
73 struct device *dev; 73 struct device *dev;
74 struct regmap *regmap; 74 struct regmap *regmap;
75 struct mutex lock; /* device lock */ 75 struct mutex lock; /* device lock */
76 u8 i2c_protocol:1;
76 77
77 struct typec_port *port; 78 struct typec_port *port;
78 struct typec_partner *partner; 79 struct typec_partner *partner;
@@ -80,19 +81,39 @@ struct tps6598x {
80 struct typec_capability typec_cap; 81 struct typec_capability typec_cap;
81}; 82};
82 83
84static int
85tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
86{
87 u8 data[len + 1];
88 int ret;
89
90 if (!tps->i2c_protocol)
91 return regmap_raw_read(tps->regmap, reg, val, len);
92
93 ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
94 if (ret)
95 return ret;
96
97 if (data[0] < len)
98 return -EIO;
99
100 memcpy(val, &data[1], len);
101 return 0;
102}
103
83static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val) 104static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
84{ 105{
85 return regmap_raw_read(tps->regmap, reg, val, sizeof(u16)); 106 return tps6598x_block_read(tps, reg, val, sizeof(u16));
86} 107}
87 108
88static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val) 109static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
89{ 110{
90 return regmap_raw_read(tps->regmap, reg, val, sizeof(u32)); 111 return tps6598x_block_read(tps, reg, val, sizeof(u32));
91} 112}
92 113
93static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val) 114static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
94{ 115{
95 return regmap_raw_read(tps->regmap, reg, val, sizeof(u64)); 116 return tps6598x_block_read(tps, reg, val, sizeof(u64));
96} 117}
97 118
98static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val) 119static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
@@ -121,8 +142,8 @@ static int tps6598x_read_partner_identity(struct tps6598x *tps)
121 struct tps6598x_rx_identity_reg id; 142 struct tps6598x_rx_identity_reg id;
122 int ret; 143 int ret;
123 144
124 ret = regmap_raw_read(tps->regmap, TPS_REG_RX_IDENTITY_SOP, 145 ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
125 &id, sizeof(id)); 146 &id, sizeof(id));
126 if (ret) 147 if (ret)
127 return ret; 148 return ret;
128 149
@@ -224,13 +245,13 @@ static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
224 } while (val); 245 } while (val);
225 246
226 if (out_len) { 247 if (out_len) {
227 ret = regmap_raw_read(tps->regmap, TPS_REG_DATA1, 248 ret = tps6598x_block_read(tps, TPS_REG_DATA1,
228 out_data, out_len); 249 out_data, out_len);
229 if (ret) 250 if (ret)
230 return ret; 251 return ret;
231 val = out_data[0]; 252 val = out_data[0];
232 } else { 253 } else {
233 ret = regmap_read(tps->regmap, TPS_REG_DATA1, &val); 254 ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
234 if (ret) 255 if (ret)
235 return ret; 256 return ret;
236 } 257 }
@@ -385,6 +406,16 @@ static int tps6598x_probe(struct i2c_client *client)
385 if (!vid) 406 if (!vid)
386 return -ENODEV; 407 return -ENODEV;
387 408
409 /*
410 * Checking can the adapter handle SMBus protocol. If it can not, the
411 * driver needs to take care of block reads separately.
412 *
413 * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
414 * unconditionally if the adapter has I2C_FUNC_I2C set.
415 */
416 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
417 tps->i2c_protocol = true;
418
388 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status); 419 ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
389 if (ret < 0) 420 if (ret < 0)
390 return ret; 421 return ret;