4444 MCP2221_I2C_MASK_ADDR_NACK = 0x40 ,
4545 MCP2221_I2C_WRADDRL_SEND = 0x21 ,
4646 MCP2221_I2C_ADDR_NACK = 0x25 ,
47+ MCP2221_I2C_READ_PARTIAL = 0x54 ,
4748 MCP2221_I2C_READ_COMPL = 0x55 ,
4849 MCP2221_ALT_F_NOT_GPIOV = 0xEE ,
4950 MCP2221_ALT_F_NOT_GPIOD = 0xEF ,
@@ -169,6 +170,25 @@ static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
169170 return mcp_send_data_req_status (mcp , mcp -> txbuf , 8 );
170171}
171172
173+ /* Check if the last command succeeded or failed and return the result.
174+ * If the command did fail, cancel that command which will free the i2c bus.
175+ */
176+ static int mcp_chk_last_cmd_status_free_bus (struct mcp2221 * mcp )
177+ {
178+ int ret ;
179+
180+ ret = mcp_chk_last_cmd_status (mcp );
181+ if (ret ) {
182+ /* The last command was a failure.
183+ * Send a cancel which will also free the bus.
184+ */
185+ usleep_range (980 , 1000 );
186+ mcp_cancel_last_cmd (mcp );
187+ }
188+
189+ return ret ;
190+ }
191+
172192static int mcp_set_i2c_speed (struct mcp2221 * mcp )
173193{
174194 int ret ;
@@ -223,7 +243,7 @@ static int mcp_i2c_write(struct mcp2221 *mcp,
223243 usleep_range (980 , 1000 );
224244
225245 if (last_status ) {
226- ret = mcp_chk_last_cmd_status (mcp );
246+ ret = mcp_chk_last_cmd_status_free_bus (mcp );
227247 if (ret )
228248 return ret ;
229249 }
@@ -260,6 +280,7 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
260280{
261281 int ret ;
262282 u16 total_len ;
283+ int retries = 0 ;
263284
264285 mcp -> txbuf [0 ] = type ;
265286 if (msg ) {
@@ -283,20 +304,31 @@ static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
283304 mcp -> rxbuf_idx = 0 ;
284305
285306 do {
307+ /* Wait for the data to be read by the device */
308+ usleep_range (980 , 1000 );
309+
286310 memset (mcp -> txbuf , 0 , 4 );
287311 mcp -> txbuf [0 ] = MCP2221_I2C_GET_DATA ;
288312
289313 ret = mcp_send_data_req_status (mcp , mcp -> txbuf , 1 );
290- if (ret )
291- return ret ;
292-
293- ret = mcp_chk_last_cmd_status (mcp );
294- if (ret )
295- return ret ;
296-
297- usleep_range (980 , 1000 );
314+ if (ret ) {
315+ if (retries < 5 ) {
316+ /* The data wasn't ready to read.
317+ * Wait a bit longer and try again.
318+ */
319+ usleep_range (90 , 100 );
320+ retries ++ ;
321+ } else {
322+ return ret ;
323+ }
324+ } else {
325+ retries = 0 ;
326+ }
298327 } while (mcp -> rxbuf_idx < total_len );
299328
329+ usleep_range (980 , 1000 );
330+ ret = mcp_chk_last_cmd_status_free_bus (mcp );
331+
300332 return ret ;
301333}
302334
@@ -310,11 +342,6 @@ static int mcp_i2c_xfer(struct i2c_adapter *adapter,
310342
311343 mutex_lock (& mcp -> lock );
312344
313- /* Setting speed before every transaction is required for mcp2221 */
314- ret = mcp_set_i2c_speed (mcp );
315- if (ret )
316- goto exit ;
317-
318345 if (num == 1 ) {
319346 if (msgs -> flags & I2C_M_RD ) {
320347 ret = mcp_i2c_smbus_read (mcp , msgs , MCP2221_I2C_RD_DATA ,
@@ -399,9 +426,7 @@ static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
399426 if (last_status ) {
400427 usleep_range (980 , 1000 );
401428
402- ret = mcp_chk_last_cmd_status (mcp );
403- if (ret )
404- return ret ;
429+ ret = mcp_chk_last_cmd_status_free_bus (mcp );
405430 }
406431
407432 return ret ;
@@ -419,10 +444,6 @@ static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
419444
420445 mutex_lock (& mcp -> lock );
421446
422- ret = mcp_set_i2c_speed (mcp );
423- if (ret )
424- goto exit ;
425-
426447 switch (size ) {
427448
428449 case I2C_SMBUS_QUICK :
@@ -768,7 +789,8 @@ static int mcp2221_raw_event(struct hid_device *hdev,
768789 mcp -> status = - EIO ;
769790 break ;
770791 }
771- if (data [2 ] == MCP2221_I2C_READ_COMPL ) {
792+ if (data [2 ] == MCP2221_I2C_READ_COMPL ||
793+ data [2 ] == MCP2221_I2C_READ_PARTIAL ) {
772794 buf = mcp -> rxbuf ;
773795 memcpy (& buf [mcp -> rxbuf_idx ], & data [4 ], data [3 ]);
774796 mcp -> rxbuf_idx = mcp -> rxbuf_idx + data [3 ];
@@ -870,6 +892,11 @@ static int mcp2221_probe(struct hid_device *hdev,
870892 if (i2c_clk_freq < 50 )
871893 i2c_clk_freq = 50 ;
872894 mcp -> cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000 )) - 3 ;
895+ ret = mcp_set_i2c_speed (mcp );
896+ if (ret ) {
897+ hid_err (hdev , "can't set i2c speed: %d\n" , ret );
898+ return ret ;
899+ }
873900
874901 mcp -> adapter .owner = THIS_MODULE ;
875902 mcp -> adapter .class = I2C_CLASS_HWMON ;
0 commit comments