49
49
MCP2221_I2C_MASK_ADDR_NACK = 0x40 ,
50
50
MCP2221_I2C_WRADDRL_SEND = 0x21 ,
51
51
MCP2221_I2C_ADDR_NACK = 0x25 ,
52
+ MCP2221_I2C_READ_PARTIAL = 0x54 ,
52
53
MCP2221_I2C_READ_COMPL = 0x55 ,
53
54
MCP2221_ALT_F_NOT_GPIOV = 0xEE ,
54
55
MCP2221_ALT_F_NOT_GPIOD = 0xEF ,
@@ -187,6 +188,25 @@ static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
187
188
return mcp_send_data_req_status (mcp , mcp -> txbuf , 8 );
188
189
}
189
190
191
+ /* Check if the last command succeeded or failed and return the result.
192
+ * If the command did fail, cancel that command which will free the i2c bus.
193
+ */
194
+ static int mcp_chk_last_cmd_status_free_bus (struct mcp2221 * mcp )
195
+ {
196
+ int ret ;
197
+
198
+ ret = mcp_chk_last_cmd_status (mcp );
199
+ if (ret ) {
200
+ /* The last command was a failure.
201
+ * Send a cancel which will also free the bus.
202
+ */
203
+ usleep_range (980 , 1000 );
204
+ mcp_cancel_last_cmd (mcp );
205
+ }
206
+
207
+ return ret ;
208
+ }
209
+
190
210
static int mcp_set_i2c_speed (struct mcp2221 * mcp )
191
211
{
192
212
int ret ;
@@ -241,7 +261,7 @@ static int mcp_i2c_write(struct mcp2221 *mcp,
241
261
usleep_range (980 , 1000 );
242
262
243
263
if (last_status ) {
244
- ret = mcp_chk_last_cmd_status (mcp );
264
+ ret = mcp_chk_last_cmd_status_free_bus (mcp );
245
265
if (ret )
246
266
return ret ;
247
267
}
@@ -278,6 +298,7 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
278
298
{
279
299
int ret ;
280
300
u16 total_len ;
301
+ int retries = 0 ;
281
302
282
303
mcp -> txbuf [0 ] = type ;
283
304
if (msg ) {
@@ -301,20 +322,31 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
301
322
mcp -> rxbuf_idx = 0 ;
302
323
303
324
do {
325
+ /* Wait for the data to be read by the device */
326
+ usleep_range (980 , 1000 );
327
+
304
328
memset (mcp -> txbuf , 0 , 4 );
305
329
mcp -> txbuf [0 ] = MCP2221_I2C_GET_DATA ;
306
330
307
331
ret = mcp_send_data_req_status (mcp , mcp -> txbuf , 1 );
308
- if (ret )
309
- return ret ;
310
-
311
- ret = mcp_chk_last_cmd_status (mcp );
312
- if (ret )
313
- return ret ;
314
-
315
- usleep_range (980 , 1000 );
332
+ if (ret ) {
333
+ if (retries < 5 ) {
334
+ /* The data wasn't ready to read.
335
+ * Wait a bit longer and try again.
336
+ */
337
+ usleep_range (90 , 100 );
338
+ retries ++ ;
339
+ } else {
340
+ return ret ;
341
+ }
342
+ } else {
343
+ retries = 0 ;
344
+ }
316
345
} while (mcp -> rxbuf_idx < total_len );
317
346
347
+ usleep_range (980 , 1000 );
348
+ ret = mcp_chk_last_cmd_status_free_bus (mcp );
349
+
318
350
return ret ;
319
351
}
320
352
@@ -328,11 +360,6 @@ static int mcp_i2c_xfer(struct i2c_adapter *adapter,
328
360
329
361
mutex_lock (& mcp -> lock );
330
362
331
- /* Setting speed before every transaction is required for mcp2221 */
332
- ret = mcp_set_i2c_speed (mcp );
333
- if (ret )
334
- goto exit ;
335
-
336
363
if (num == 1 ) {
337
364
if (msgs -> flags & I2C_M_RD ) {
338
365
ret = mcp_i2c_smbus_read (mcp , msgs , MCP2221_I2C_RD_DATA ,
@@ -417,9 +444,7 @@ static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
417
444
if (last_status ) {
418
445
usleep_range (980 , 1000 );
419
446
420
- ret = mcp_chk_last_cmd_status (mcp );
421
- if (ret )
422
- return ret ;
447
+ ret = mcp_chk_last_cmd_status_free_bus (mcp );
423
448
}
424
449
425
450
return ret ;
@@ -437,10 +462,6 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
437
462
438
463
mutex_lock (& mcp -> lock );
439
464
440
- ret = mcp_set_i2c_speed (mcp );
441
- if (ret )
442
- goto exit ;
443
-
444
465
switch (size ) {
445
466
446
467
case I2C_SMBUS_QUICK :
@@ -791,7 +812,8 @@ static int mcp2221_raw_event(struct hid_device *hdev,
791
812
mcp -> status = - EIO ;
792
813
break ;
793
814
}
794
- if (data [2 ] == MCP2221_I2C_READ_COMPL ) {
815
+ if (data [2 ] == MCP2221_I2C_READ_COMPL ||
816
+ data [2 ] == MCP2221_I2C_READ_PARTIAL ) {
795
817
buf = mcp -> rxbuf ;
796
818
memcpy (& buf [mcp -> rxbuf_idx ], & data [4 ], data [3 ]);
797
819
mcp -> rxbuf_idx = mcp -> rxbuf_idx + data [3 ];
@@ -1150,12 +1172,18 @@ static int mcp2221_probe(struct hid_device *hdev,
1150
1172
if (i2c_clk_freq < 50 )
1151
1173
i2c_clk_freq = 50 ;
1152
1174
mcp -> cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000 )) - 3 ;
1175
+ ret = mcp_set_i2c_speed (mcp );
1176
+ if (ret ) {
1177
+ hid_err (hdev , "can't set i2c speed: %d\n" , ret );
1178
+ return ret ;
1179
+ }
1153
1180
1154
1181
mcp -> adapter .owner = THIS_MODULE ;
1155
1182
mcp -> adapter .class = I2C_CLASS_HWMON ;
1156
1183
mcp -> adapter .algo = & mcp_i2c_algo ;
1157
1184
mcp -> adapter .retries = 1 ;
1158
1185
mcp -> adapter .dev .parent = & hdev -> dev ;
1186
+ ACPI_COMPANION_SET (& mcp -> adapter .dev , ACPI_COMPANION (hdev -> dev .parent ));
1159
1187
snprintf (mcp -> adapter .name , sizeof (mcp -> adapter .name ),
1160
1188
"MCP2221 usb-i2c bridge" );
1161
1189
0 commit comments