1 // SPDX-License-Identifier: GPL-2.0-only
3 * MCP2221A - Microchip USB to I2C Host Protocol Bridge
5 * Copyright (c) 2020, Rishi Gupta <gupt21@gmail.com>
7 * Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/mutex.h>
13 #include <linux/completion.h>
14 #include <linux/delay.h>
15 #include <linux/hid.h>
16 #include <linux/hidraw.h>
17 #include <linux/i2c.h>
20 /* Commands codes in a raw output report */
22 MCP2221_I2C_WR_DATA
= 0x90,
23 MCP2221_I2C_WR_NO_STOP
= 0x94,
24 MCP2221_I2C_RD_DATA
= 0x91,
25 MCP2221_I2C_RD_RPT_START
= 0x93,
26 MCP2221_I2C_GET_DATA
= 0x40,
27 MCP2221_I2C_PARAM_OR_STATUS
= 0x10,
28 MCP2221_I2C_SET_SPEED
= 0x20,
29 MCP2221_I2C_CANCEL
= 0x10,
32 /* Response codes in a raw input report */
34 MCP2221_SUCCESS
= 0x00,
35 MCP2221_I2C_ENG_BUSY
= 0x01,
36 MCP2221_I2C_START_TOUT
= 0x12,
37 MCP2221_I2C_STOP_TOUT
= 0x62,
38 MCP2221_I2C_WRADDRL_TOUT
= 0x23,
39 MCP2221_I2C_WRDATA_TOUT
= 0x44,
40 MCP2221_I2C_WRADDRL_NACK
= 0x25,
41 MCP2221_I2C_MASK_ADDR_NACK
= 0x40,
42 MCP2221_I2C_WRADDRL_SEND
= 0x21,
43 MCP2221_I2C_ADDR_NACK
= 0x25,
44 MCP2221_I2C_READ_COMPL
= 0x55,
48 * There is no way to distinguish responses. Therefore next command
49 * is sent only after response to previous has been received. Mutex
50 * lock is used for this purpose mainly.
53 struct hid_device
*hdev
;
54 struct i2c_adapter adapter
;
56 struct completion wait_in_report
;
65 * Default i2c bus clock frequency 400 kHz. Modify this if you
66 * want to set some other frequency (min 50 kHz - max 400 kHz).
68 static uint i2c_clk_freq
= 400;
70 /* Synchronously send output report to the device */
71 static int mcp_send_report(struct mcp2221
*mcp
,
72 u8
*out_report
, size_t len
)
77 buf
= kmemdup(out_report
, len
, GFP_KERNEL
);
81 /* mcp2221 uses interrupt endpoint for out reports */
82 ret
= hid_hw_output_report(mcp
->hdev
, buf
, len
);
91 * Send o/p report to the device and wait for i/p report to be
92 * received from the device. If the device does not respond,
95 static int mcp_send_data_req_status(struct mcp2221
*mcp
,
96 u8
*out_report
, int len
)
101 reinit_completion(&mcp
->wait_in_report
);
103 ret
= mcp_send_report(mcp
, out_report
, len
);
107 t
= wait_for_completion_timeout(&mcp
->wait_in_report
,
108 msecs_to_jiffies(4000));
115 /* Check pass/fail for actual communication with i2c slave */
116 static int mcp_chk_last_cmd_status(struct mcp2221
*mcp
)
118 memset(mcp
->txbuf
, 0, 8);
119 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
121 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
124 /* Cancels last command releasing i2c bus just in case occupied */
125 static int mcp_cancel_last_cmd(struct mcp2221
*mcp
)
127 memset(mcp
->txbuf
, 0, 8);
128 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
129 mcp
->txbuf
[2] = MCP2221_I2C_CANCEL
;
131 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
134 static int mcp_set_i2c_speed(struct mcp2221
*mcp
)
138 memset(mcp
->txbuf
, 0, 8);
139 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
140 mcp
->txbuf
[3] = MCP2221_I2C_SET_SPEED
;
141 mcp
->txbuf
[4] = mcp
->cur_i2c_clk_div
;
143 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
145 /* Small delay is needed here */
146 usleep_range(980, 1000);
147 mcp_cancel_last_cmd(mcp
);
154 * An output report can contain minimum 1 and maximum 60 user data
155 * bytes. If the number of data bytes is more then 60, we send it
156 * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
157 * bytes. Total number of bytes is informed in very first report to
158 * mcp2221, from that point onwards it first collect all the data
159 * from host and then send to i2c slave device.
161 static int mcp_i2c_write(struct mcp2221
*mcp
,
162 struct i2c_msg
*msg
, int type
, u8 last_status
)
164 int ret
, len
, idx
, sent
;
174 mcp
->txbuf
[0] = type
;
175 mcp
->txbuf
[1] = msg
->len
& 0xff;
176 mcp
->txbuf
[2] = msg
->len
>> 8;
177 mcp
->txbuf
[3] = (u8
)(msg
->addr
<< 1);
179 memcpy(&mcp
->txbuf
[4], &msg
->buf
[idx
], len
);
181 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, len
+ 4);
185 usleep_range(980, 1000);
188 ret
= mcp_chk_last_cmd_status(mcp
);
194 if (sent
>= msg
->len
)
198 if ((msg
->len
- sent
) < 60)
199 len
= msg
->len
- sent
;
204 * Testing shows delay is needed between successive writes
205 * otherwise next write fails on first-try from i2c core.
206 * This value is obtained through automated stress testing.
208 usleep_range(980, 1000);
215 * Device reads all data (0 - 65535 bytes) from i2c slave device and
216 * stores it in device itself. This data is read back from device to
217 * host in multiples of 60 bytes using input reports.
219 static int mcp_i2c_smbus_read(struct mcp2221
*mcp
,
220 struct i2c_msg
*msg
, int type
, u16 smbus_addr
,
221 u8 smbus_len
, u8
*smbus_buf
)
226 mcp
->txbuf
[0] = type
;
228 mcp
->txbuf
[1] = msg
->len
& 0xff;
229 mcp
->txbuf
[2] = msg
->len
>> 8;
230 mcp
->txbuf
[3] = (u8
)(msg
->addr
<< 1);
231 total_len
= msg
->len
;
232 mcp
->rxbuf
= msg
->buf
;
234 mcp
->txbuf
[1] = smbus_len
;
236 mcp
->txbuf
[3] = (u8
)(smbus_addr
<< 1);
237 total_len
= smbus_len
;
238 mcp
->rxbuf
= smbus_buf
;
241 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 4);
248 memset(mcp
->txbuf
, 0, 4);
249 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
251 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
255 ret
= mcp_chk_last_cmd_status(mcp
);
259 usleep_range(980, 1000);
260 } while (mcp
->rxbuf_idx
< total_len
);
265 static int mcp_i2c_xfer(struct i2c_adapter
*adapter
,
266 struct i2c_msg msgs
[], int num
)
269 struct mcp2221
*mcp
= i2c_get_adapdata(adapter
);
271 hid_hw_power(mcp
->hdev
, PM_HINT_FULLON
);
273 mutex_lock(&mcp
->lock
);
275 /* Setting speed before every transaction is required for mcp2221 */
276 ret
= mcp_set_i2c_speed(mcp
);
281 if (msgs
->flags
& I2C_M_RD
) {
282 ret
= mcp_i2c_smbus_read(mcp
, msgs
, MCP2221_I2C_RD_DATA
,
285 ret
= mcp_i2c_write(mcp
, msgs
, MCP2221_I2C_WR_DATA
, 1);
290 } else if (num
== 2) {
291 /* Ex transaction; send reg address and read its contents */
292 if (msgs
[0].addr
== msgs
[1].addr
&&
293 !(msgs
[0].flags
& I2C_M_RD
) &&
294 (msgs
[1].flags
& I2C_M_RD
)) {
296 ret
= mcp_i2c_write(mcp
, &msgs
[0],
297 MCP2221_I2C_WR_NO_STOP
, 0);
301 ret
= mcp_i2c_smbus_read(mcp
, &msgs
[1],
302 MCP2221_I2C_RD_RPT_START
,
308 dev_err(&adapter
->dev
,
309 "unsupported multi-msg i2c transaction\n");
313 dev_err(&adapter
->dev
,
314 "unsupported multi-msg i2c transaction\n");
319 hid_hw_power(mcp
->hdev
, PM_HINT_NORMAL
);
320 mutex_unlock(&mcp
->lock
);
324 static int mcp_smbus_write(struct mcp2221
*mcp
, u16 addr
,
325 u8 command
, u8
*buf
, u8 len
, int type
,
330 mcp
->txbuf
[0] = type
;
331 mcp
->txbuf
[1] = len
+ 1; /* 1 is due to command byte itself */
333 mcp
->txbuf
[3] = (u8
)(addr
<< 1);
334 mcp
->txbuf
[4] = command
;
341 mcp
->txbuf
[5] = buf
[0];
345 mcp
->txbuf
[5] = buf
[0];
346 mcp
->txbuf
[6] = buf
[1];
350 memcpy(&mcp
->txbuf
[5], buf
, len
);
354 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, data_len
);
359 usleep_range(980, 1000);
361 ret
= mcp_chk_last_cmd_status(mcp
);
369 static int mcp_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
370 unsigned short flags
, char read_write
,
371 u8 command
, int size
,
372 union i2c_smbus_data
*data
)
375 struct mcp2221
*mcp
= i2c_get_adapdata(adapter
);
377 hid_hw_power(mcp
->hdev
, PM_HINT_FULLON
);
379 mutex_lock(&mcp
->lock
);
381 ret
= mcp_set_i2c_speed(mcp
);
387 case I2C_SMBUS_QUICK
:
388 if (read_write
== I2C_SMBUS_READ
)
389 ret
= mcp_i2c_smbus_read(mcp
, NULL
, MCP2221_I2C_RD_DATA
,
390 addr
, 0, &data
->byte
);
392 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
393 0, MCP2221_I2C_WR_DATA
, 1);
396 if (read_write
== I2C_SMBUS_READ
)
397 ret
= mcp_i2c_smbus_read(mcp
, NULL
, MCP2221_I2C_RD_DATA
,
398 addr
, 1, &data
->byte
);
400 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
401 0, MCP2221_I2C_WR_DATA
, 1);
403 case I2C_SMBUS_BYTE_DATA
:
404 if (read_write
== I2C_SMBUS_READ
) {
405 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
406 0, MCP2221_I2C_WR_NO_STOP
, 0);
410 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
411 MCP2221_I2C_RD_RPT_START
,
412 addr
, 1, &data
->byte
);
414 ret
= mcp_smbus_write(mcp
, addr
, command
, &data
->byte
,
415 1, MCP2221_I2C_WR_DATA
, 1);
418 case I2C_SMBUS_WORD_DATA
:
419 if (read_write
== I2C_SMBUS_READ
) {
420 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
421 0, MCP2221_I2C_WR_NO_STOP
, 0);
425 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
426 MCP2221_I2C_RD_RPT_START
,
427 addr
, 2, (u8
*)&data
->word
);
429 ret
= mcp_smbus_write(mcp
, addr
, command
,
430 (u8
*)&data
->word
, 2,
431 MCP2221_I2C_WR_DATA
, 1);
434 case I2C_SMBUS_BLOCK_DATA
:
435 if (read_write
== I2C_SMBUS_READ
) {
436 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
437 0, MCP2221_I2C_WR_NO_STOP
, 1);
442 mcp
->rxbuf
= data
->block
;
443 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
444 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
448 if (!data
->block
[0]) {
452 ret
= mcp_smbus_write(mcp
, addr
, command
, data
->block
,
454 MCP2221_I2C_WR_DATA
, 1);
457 case I2C_SMBUS_I2C_BLOCK_DATA
:
458 if (read_write
== I2C_SMBUS_READ
) {
459 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
460 0, MCP2221_I2C_WR_NO_STOP
, 1);
465 mcp
->rxbuf
= data
->block
;
466 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
467 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
471 if (!data
->block
[0]) {
475 ret
= mcp_smbus_write(mcp
, addr
, command
,
476 &data
->block
[1], data
->block
[0],
477 MCP2221_I2C_WR_DATA
, 1);
480 case I2C_SMBUS_PROC_CALL
:
481 ret
= mcp_smbus_write(mcp
, addr
, command
,
483 2, MCP2221_I2C_WR_NO_STOP
, 0);
487 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
488 MCP2221_I2C_RD_RPT_START
,
489 addr
, 2, (u8
*)&data
->word
);
491 case I2C_SMBUS_BLOCK_PROC_CALL
:
492 ret
= mcp_smbus_write(mcp
, addr
, command
, data
->block
,
494 MCP2221_I2C_WR_NO_STOP
, 0);
498 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
499 MCP2221_I2C_RD_RPT_START
,
500 addr
, I2C_SMBUS_BLOCK_MAX
,
504 dev_err(&mcp
->adapter
.dev
,
505 "unsupported smbus transaction size:%d\n", size
);
510 hid_hw_power(mcp
->hdev
, PM_HINT_NORMAL
);
511 mutex_unlock(&mcp
->lock
);
515 static u32
mcp_i2c_func(struct i2c_adapter
*adapter
)
517 return I2C_FUNC_I2C
|
518 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
519 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
520 (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_PEC
);
523 static const struct i2c_algorithm mcp_i2c_algo
= {
524 .master_xfer
= mcp_i2c_xfer
,
525 .smbus_xfer
= mcp_smbus_xfer
,
526 .functionality
= mcp_i2c_func
,
529 /* Gives current state of i2c engine inside mcp2221 */
530 static int mcp_get_i2c_eng_state(struct mcp2221
*mcp
,
536 case MCP2221_I2C_WRADDRL_NACK
:
537 case MCP2221_I2C_WRADDRL_SEND
:
540 case MCP2221_I2C_START_TOUT
:
541 case MCP2221_I2C_STOP_TOUT
:
542 case MCP2221_I2C_WRADDRL_TOUT
:
543 case MCP2221_I2C_WRDATA_TOUT
:
546 case MCP2221_I2C_ENG_BUSY
:
549 case MCP2221_SUCCESS
:
560 * MCP2221 uses interrupt endpoint for input reports. This function
561 * is called by HID layer when it receives i/p report from mcp2221,
562 * which is actually a response to the previously sent command.
564 * MCP2221A firmware specific return codes are parsed and 0 or
565 * appropriate negative error code is returned. Delayed response
566 * results in timeout error and stray reponses results in -EIO.
568 static int mcp2221_raw_event(struct hid_device
*hdev
,
569 struct hid_report
*report
, u8
*data
, int size
)
572 struct mcp2221
*mcp
= hid_get_drvdata(hdev
);
576 case MCP2221_I2C_WR_DATA
:
577 case MCP2221_I2C_WR_NO_STOP
:
578 case MCP2221_I2C_RD_DATA
:
579 case MCP2221_I2C_RD_RPT_START
:
581 case MCP2221_SUCCESS
:
585 mcp
->status
= mcp_get_i2c_eng_state(mcp
, data
, 2);
587 complete(&mcp
->wait_in_report
);
590 case MCP2221_I2C_PARAM_OR_STATUS
:
592 case MCP2221_SUCCESS
:
593 if ((mcp
->txbuf
[3] == MCP2221_I2C_SET_SPEED
) &&
594 (data
[3] != MCP2221_I2C_SET_SPEED
)) {
595 mcp
->status
= -EAGAIN
;
598 if (data
[20] & MCP2221_I2C_MASK_ADDR_NACK
) {
599 mcp
->status
= -ENXIO
;
602 mcp
->status
= mcp_get_i2c_eng_state(mcp
, data
, 8);
607 complete(&mcp
->wait_in_report
);
610 case MCP2221_I2C_GET_DATA
:
612 case MCP2221_SUCCESS
:
613 if (data
[2] == MCP2221_I2C_ADDR_NACK
) {
614 mcp
->status
= -ENXIO
;
617 if (!mcp_get_i2c_eng_state(mcp
, data
, 2)
622 if (data
[3] == 127) {
626 if (data
[2] == MCP2221_I2C_READ_COMPL
) {
628 memcpy(&buf
[mcp
->rxbuf_idx
], &data
[4], data
[3]);
629 mcp
->rxbuf_idx
= mcp
->rxbuf_idx
+ data
[3];
638 complete(&mcp
->wait_in_report
);
643 complete(&mcp
->wait_in_report
);
649 static int mcp2221_probe(struct hid_device
*hdev
,
650 const struct hid_device_id
*id
)
655 mcp
= devm_kzalloc(&hdev
->dev
, sizeof(*mcp
), GFP_KERNEL
);
659 ret
= hid_parse(hdev
);
661 hid_err(hdev
, "can't parse reports\n");
665 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
667 hid_err(hdev
, "can't start hardware\n");
671 ret
= hid_hw_open(hdev
);
673 hid_err(hdev
, "can't open device\n");
677 mutex_init(&mcp
->lock
);
678 init_completion(&mcp
->wait_in_report
);
679 hid_set_drvdata(hdev
, mcp
);
682 /* Set I2C bus clock diviser */
683 if (i2c_clk_freq
> 400)
685 if (i2c_clk_freq
< 50)
687 mcp
->cur_i2c_clk_div
= (12000000 / (i2c_clk_freq
* 1000)) - 3;
689 mcp
->adapter
.owner
= THIS_MODULE
;
690 mcp
->adapter
.class = I2C_CLASS_HWMON
;
691 mcp
->adapter
.algo
= &mcp_i2c_algo
;
692 mcp
->adapter
.retries
= 1;
693 mcp
->adapter
.dev
.parent
= &hdev
->dev
;
694 snprintf(mcp
->adapter
.name
, sizeof(mcp
->adapter
.name
),
695 "MCP2221 usb-i2c bridge on hidraw%d",
696 ((struct hidraw
*)hdev
->hidraw
)->minor
);
698 ret
= i2c_add_adapter(&mcp
->adapter
);
700 hid_err(hdev
, "can't add usb-i2c adapter: %d\n", ret
);
703 i2c_set_adapdata(&mcp
->adapter
, mcp
);
708 hid_hw_close(mcp
->hdev
);
710 hid_hw_stop(mcp
->hdev
);
714 static void mcp2221_remove(struct hid_device
*hdev
)
716 struct mcp2221
*mcp
= hid_get_drvdata(hdev
);
718 i2c_del_adapter(&mcp
->adapter
);
719 hid_hw_close(mcp
->hdev
);
720 hid_hw_stop(mcp
->hdev
);
723 static const struct hid_device_id mcp2221_devices
[] = {
724 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_MCP2221
) },
727 MODULE_DEVICE_TABLE(hid
, mcp2221_devices
);
729 static struct hid_driver mcp2221_driver
= {
731 .id_table
= mcp2221_devices
,
732 .probe
= mcp2221_probe
,
733 .remove
= mcp2221_remove
,
734 .raw_event
= mcp2221_raw_event
,
737 /* Register with HID core */
738 module_hid_driver(mcp2221_driver
);
740 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
741 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
742 MODULE_LICENSE("GPL v2");