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: https://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>
18 #include <linux/gpio/driver.h>
21 /* Commands codes in a raw output report */
23 MCP2221_I2C_WR_DATA
= 0x90,
24 MCP2221_I2C_WR_NO_STOP
= 0x94,
25 MCP2221_I2C_RD_DATA
= 0x91,
26 MCP2221_I2C_RD_RPT_START
= 0x93,
27 MCP2221_I2C_GET_DATA
= 0x40,
28 MCP2221_I2C_PARAM_OR_STATUS
= 0x10,
29 MCP2221_I2C_SET_SPEED
= 0x20,
30 MCP2221_I2C_CANCEL
= 0x10,
31 MCP2221_GPIO_SET
= 0x50,
32 MCP2221_GPIO_GET
= 0x51,
35 /* Response codes in a raw input report */
37 MCP2221_SUCCESS
= 0x00,
38 MCP2221_I2C_ENG_BUSY
= 0x01,
39 MCP2221_I2C_START_TOUT
= 0x12,
40 MCP2221_I2C_STOP_TOUT
= 0x62,
41 MCP2221_I2C_WRADDRL_TOUT
= 0x23,
42 MCP2221_I2C_WRDATA_TOUT
= 0x44,
43 MCP2221_I2C_WRADDRL_NACK
= 0x25,
44 MCP2221_I2C_MASK_ADDR_NACK
= 0x40,
45 MCP2221_I2C_WRADDRL_SEND
= 0x21,
46 MCP2221_I2C_ADDR_NACK
= 0x25,
47 MCP2221_I2C_READ_COMPL
= 0x55,
48 MCP2221_ALT_F_NOT_GPIOV
= 0xEE,
49 MCP2221_ALT_F_NOT_GPIOD
= 0xEF,
53 * There is no way to distinguish responses. Therefore next command
54 * is sent only after response to previous has been received. Mutex
55 * lock is used for this purpose mainly.
58 struct hid_device
*hdev
;
59 struct i2c_adapter adapter
;
61 struct completion wait_in_report
;
73 * Default i2c bus clock frequency 400 kHz. Modify this if you
74 * want to set some other frequency (min 50 kHz - max 400 kHz).
76 static uint i2c_clk_freq
= 400;
78 /* Synchronously send output report to the device */
79 static int mcp_send_report(struct mcp2221
*mcp
,
80 u8
*out_report
, size_t len
)
85 buf
= kmemdup(out_report
, len
, GFP_KERNEL
);
89 /* mcp2221 uses interrupt endpoint for out reports */
90 ret
= hid_hw_output_report(mcp
->hdev
, buf
, len
);
99 * Send o/p report to the device and wait for i/p report to be
100 * received from the device. If the device does not respond,
103 static int mcp_send_data_req_status(struct mcp2221
*mcp
,
104 u8
*out_report
, int len
)
109 reinit_completion(&mcp
->wait_in_report
);
111 ret
= mcp_send_report(mcp
, out_report
, len
);
115 t
= wait_for_completion_timeout(&mcp
->wait_in_report
,
116 msecs_to_jiffies(4000));
123 /* Check pass/fail for actual communication with i2c slave */
124 static int mcp_chk_last_cmd_status(struct mcp2221
*mcp
)
126 memset(mcp
->txbuf
, 0, 8);
127 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
129 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
132 /* Cancels last command releasing i2c bus just in case occupied */
133 static int mcp_cancel_last_cmd(struct mcp2221
*mcp
)
135 memset(mcp
->txbuf
, 0, 8);
136 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
137 mcp
->txbuf
[2] = MCP2221_I2C_CANCEL
;
139 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
142 static int mcp_set_i2c_speed(struct mcp2221
*mcp
)
146 memset(mcp
->txbuf
, 0, 8);
147 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
148 mcp
->txbuf
[3] = MCP2221_I2C_SET_SPEED
;
149 mcp
->txbuf
[4] = mcp
->cur_i2c_clk_div
;
151 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
153 /* Small delay is needed here */
154 usleep_range(980, 1000);
155 mcp_cancel_last_cmd(mcp
);
162 * An output report can contain minimum 1 and maximum 60 user data
163 * bytes. If the number of data bytes is more then 60, we send it
164 * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
165 * bytes. Total number of bytes is informed in very first report to
166 * mcp2221, from that point onwards it first collect all the data
167 * from host and then send to i2c slave device.
169 static int mcp_i2c_write(struct mcp2221
*mcp
,
170 struct i2c_msg
*msg
, int type
, u8 last_status
)
172 int ret
, len
, idx
, sent
;
182 mcp
->txbuf
[0] = type
;
183 mcp
->txbuf
[1] = msg
->len
& 0xff;
184 mcp
->txbuf
[2] = msg
->len
>> 8;
185 mcp
->txbuf
[3] = (u8
)(msg
->addr
<< 1);
187 memcpy(&mcp
->txbuf
[4], &msg
->buf
[idx
], len
);
189 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, len
+ 4);
193 usleep_range(980, 1000);
196 ret
= mcp_chk_last_cmd_status(mcp
);
202 if (sent
>= msg
->len
)
206 if ((msg
->len
- sent
) < 60)
207 len
= msg
->len
- sent
;
212 * Testing shows delay is needed between successive writes
213 * otherwise next write fails on first-try from i2c core.
214 * This value is obtained through automated stress testing.
216 usleep_range(980, 1000);
223 * Device reads all data (0 - 65535 bytes) from i2c slave device and
224 * stores it in device itself. This data is read back from device to
225 * host in multiples of 60 bytes using input reports.
227 static int mcp_i2c_smbus_read(struct mcp2221
*mcp
,
228 struct i2c_msg
*msg
, int type
, u16 smbus_addr
,
229 u8 smbus_len
, u8
*smbus_buf
)
234 mcp
->txbuf
[0] = type
;
236 mcp
->txbuf
[1] = msg
->len
& 0xff;
237 mcp
->txbuf
[2] = msg
->len
>> 8;
238 mcp
->txbuf
[3] = (u8
)(msg
->addr
<< 1);
239 total_len
= msg
->len
;
240 mcp
->rxbuf
= msg
->buf
;
242 mcp
->txbuf
[1] = smbus_len
;
244 mcp
->txbuf
[3] = (u8
)(smbus_addr
<< 1);
245 total_len
= smbus_len
;
246 mcp
->rxbuf
= smbus_buf
;
249 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 4);
256 memset(mcp
->txbuf
, 0, 4);
257 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
259 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
263 ret
= mcp_chk_last_cmd_status(mcp
);
267 usleep_range(980, 1000);
268 } while (mcp
->rxbuf_idx
< total_len
);
273 static int mcp_i2c_xfer(struct i2c_adapter
*adapter
,
274 struct i2c_msg msgs
[], int num
)
277 struct mcp2221
*mcp
= i2c_get_adapdata(adapter
);
279 hid_hw_power(mcp
->hdev
, PM_HINT_FULLON
);
281 mutex_lock(&mcp
->lock
);
283 /* Setting speed before every transaction is required for mcp2221 */
284 ret
= mcp_set_i2c_speed(mcp
);
289 if (msgs
->flags
& I2C_M_RD
) {
290 ret
= mcp_i2c_smbus_read(mcp
, msgs
, MCP2221_I2C_RD_DATA
,
293 ret
= mcp_i2c_write(mcp
, msgs
, MCP2221_I2C_WR_DATA
, 1);
298 } else if (num
== 2) {
299 /* Ex transaction; send reg address and read its contents */
300 if (msgs
[0].addr
== msgs
[1].addr
&&
301 !(msgs
[0].flags
& I2C_M_RD
) &&
302 (msgs
[1].flags
& I2C_M_RD
)) {
304 ret
= mcp_i2c_write(mcp
, &msgs
[0],
305 MCP2221_I2C_WR_NO_STOP
, 0);
309 ret
= mcp_i2c_smbus_read(mcp
, &msgs
[1],
310 MCP2221_I2C_RD_RPT_START
,
316 dev_err(&adapter
->dev
,
317 "unsupported multi-msg i2c transaction\n");
321 dev_err(&adapter
->dev
,
322 "unsupported multi-msg i2c transaction\n");
327 hid_hw_power(mcp
->hdev
, PM_HINT_NORMAL
);
328 mutex_unlock(&mcp
->lock
);
332 static int mcp_smbus_write(struct mcp2221
*mcp
, u16 addr
,
333 u8 command
, u8
*buf
, u8 len
, int type
,
338 mcp
->txbuf
[0] = type
;
339 mcp
->txbuf
[1] = len
+ 1; /* 1 is due to command byte itself */
341 mcp
->txbuf
[3] = (u8
)(addr
<< 1);
342 mcp
->txbuf
[4] = command
;
349 mcp
->txbuf
[5] = buf
[0];
353 mcp
->txbuf
[5] = buf
[0];
354 mcp
->txbuf
[6] = buf
[1];
358 memcpy(&mcp
->txbuf
[5], buf
, len
);
362 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, data_len
);
367 usleep_range(980, 1000);
369 ret
= mcp_chk_last_cmd_status(mcp
);
377 static int mcp_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
378 unsigned short flags
, char read_write
,
379 u8 command
, int size
,
380 union i2c_smbus_data
*data
)
383 struct mcp2221
*mcp
= i2c_get_adapdata(adapter
);
385 hid_hw_power(mcp
->hdev
, PM_HINT_FULLON
);
387 mutex_lock(&mcp
->lock
);
389 ret
= mcp_set_i2c_speed(mcp
);
395 case I2C_SMBUS_QUICK
:
396 if (read_write
== I2C_SMBUS_READ
)
397 ret
= mcp_i2c_smbus_read(mcp
, NULL
, MCP2221_I2C_RD_DATA
,
398 addr
, 0, &data
->byte
);
400 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
401 0, MCP2221_I2C_WR_DATA
, 1);
404 if (read_write
== I2C_SMBUS_READ
)
405 ret
= mcp_i2c_smbus_read(mcp
, NULL
, MCP2221_I2C_RD_DATA
,
406 addr
, 1, &data
->byte
);
408 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
409 0, MCP2221_I2C_WR_DATA
, 1);
411 case I2C_SMBUS_BYTE_DATA
:
412 if (read_write
== I2C_SMBUS_READ
) {
413 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
414 0, MCP2221_I2C_WR_NO_STOP
, 0);
418 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
419 MCP2221_I2C_RD_RPT_START
,
420 addr
, 1, &data
->byte
);
422 ret
= mcp_smbus_write(mcp
, addr
, command
, &data
->byte
,
423 1, MCP2221_I2C_WR_DATA
, 1);
426 case I2C_SMBUS_WORD_DATA
:
427 if (read_write
== I2C_SMBUS_READ
) {
428 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
429 0, MCP2221_I2C_WR_NO_STOP
, 0);
433 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
434 MCP2221_I2C_RD_RPT_START
,
435 addr
, 2, (u8
*)&data
->word
);
437 ret
= mcp_smbus_write(mcp
, addr
, command
,
438 (u8
*)&data
->word
, 2,
439 MCP2221_I2C_WR_DATA
, 1);
442 case I2C_SMBUS_BLOCK_DATA
:
443 if (read_write
== I2C_SMBUS_READ
) {
444 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
445 0, MCP2221_I2C_WR_NO_STOP
, 1);
450 mcp
->rxbuf
= data
->block
;
451 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
452 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
456 if (!data
->block
[0]) {
460 ret
= mcp_smbus_write(mcp
, addr
, command
, data
->block
,
462 MCP2221_I2C_WR_DATA
, 1);
465 case I2C_SMBUS_I2C_BLOCK_DATA
:
466 if (read_write
== I2C_SMBUS_READ
) {
467 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
468 0, MCP2221_I2C_WR_NO_STOP
, 1);
473 mcp
->rxbuf
= data
->block
;
474 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
475 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
479 if (!data
->block
[0]) {
483 ret
= mcp_smbus_write(mcp
, addr
, command
,
484 &data
->block
[1], data
->block
[0],
485 MCP2221_I2C_WR_DATA
, 1);
488 case I2C_SMBUS_PROC_CALL
:
489 ret
= mcp_smbus_write(mcp
, addr
, command
,
491 2, MCP2221_I2C_WR_NO_STOP
, 0);
495 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
496 MCP2221_I2C_RD_RPT_START
,
497 addr
, 2, (u8
*)&data
->word
);
499 case I2C_SMBUS_BLOCK_PROC_CALL
:
500 ret
= mcp_smbus_write(mcp
, addr
, command
, data
->block
,
502 MCP2221_I2C_WR_NO_STOP
, 0);
506 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
507 MCP2221_I2C_RD_RPT_START
,
508 addr
, I2C_SMBUS_BLOCK_MAX
,
512 dev_err(&mcp
->adapter
.dev
,
513 "unsupported smbus transaction size:%d\n", size
);
518 hid_hw_power(mcp
->hdev
, PM_HINT_NORMAL
);
519 mutex_unlock(&mcp
->lock
);
523 static u32
mcp_i2c_func(struct i2c_adapter
*adapter
)
525 return I2C_FUNC_I2C
|
526 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
527 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
528 (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_PEC
);
531 static const struct i2c_algorithm mcp_i2c_algo
= {
532 .master_xfer
= mcp_i2c_xfer
,
533 .smbus_xfer
= mcp_smbus_xfer
,
534 .functionality
= mcp_i2c_func
,
537 static int mcp_gpio_get(struct gpio_chip
*gc
,
541 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
543 mcp
->txbuf
[0] = MCP2221_GPIO_GET
;
545 mcp
->gp_idx
= (offset
+ 1) * 2;
547 mutex_lock(&mcp
->lock
);
548 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
549 mutex_unlock(&mcp
->lock
);
554 static void mcp_gpio_set(struct gpio_chip
*gc
,
555 unsigned int offset
, int value
)
557 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
559 memset(mcp
->txbuf
, 0, 18);
560 mcp
->txbuf
[0] = MCP2221_GPIO_SET
;
562 mcp
->gp_idx
= ((offset
+ 1) * 4) - 1;
564 mcp
->txbuf
[mcp
->gp_idx
- 1] = 1;
565 mcp
->txbuf
[mcp
->gp_idx
] = !!value
;
567 mutex_lock(&mcp
->lock
);
568 mcp_send_data_req_status(mcp
, mcp
->txbuf
, 18);
569 mutex_unlock(&mcp
->lock
);
572 static int mcp_gpio_dir_set(struct mcp2221
*mcp
,
573 unsigned int offset
, u8 val
)
575 memset(mcp
->txbuf
, 0, 18);
576 mcp
->txbuf
[0] = MCP2221_GPIO_SET
;
578 mcp
->gp_idx
= (offset
+ 1) * 5;
580 mcp
->txbuf
[mcp
->gp_idx
- 1] = 1;
581 mcp
->txbuf
[mcp
->gp_idx
] = val
;
583 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 18);
586 static int mcp_gpio_direction_input(struct gpio_chip
*gc
,
590 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
592 mutex_lock(&mcp
->lock
);
593 ret
= mcp_gpio_dir_set(mcp
, offset
, 0);
594 mutex_unlock(&mcp
->lock
);
599 static int mcp_gpio_direction_output(struct gpio_chip
*gc
,
600 unsigned int offset
, int value
)
603 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
605 mutex_lock(&mcp
->lock
);
606 ret
= mcp_gpio_dir_set(mcp
, offset
, 1);
607 mutex_unlock(&mcp
->lock
);
609 /* Can't configure as output, bailout early */
613 mcp_gpio_set(gc
, offset
, value
);
618 static int mcp_gpio_get_direction(struct gpio_chip
*gc
,
622 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
624 mcp
->txbuf
[0] = MCP2221_GPIO_GET
;
626 mcp
->gp_idx
= (offset
+ 1) * 2;
628 mutex_lock(&mcp
->lock
);
629 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
630 mutex_unlock(&mcp
->lock
);
636 return GPIO_LINE_DIRECTION_IN
;
638 return GPIO_LINE_DIRECTION_OUT
;
641 /* Gives current state of i2c engine inside mcp2221 */
642 static int mcp_get_i2c_eng_state(struct mcp2221
*mcp
,
648 case MCP2221_I2C_WRADDRL_NACK
:
649 case MCP2221_I2C_WRADDRL_SEND
:
652 case MCP2221_I2C_START_TOUT
:
653 case MCP2221_I2C_STOP_TOUT
:
654 case MCP2221_I2C_WRADDRL_TOUT
:
655 case MCP2221_I2C_WRDATA_TOUT
:
658 case MCP2221_I2C_ENG_BUSY
:
661 case MCP2221_SUCCESS
:
672 * MCP2221 uses interrupt endpoint for input reports. This function
673 * is called by HID layer when it receives i/p report from mcp2221,
674 * which is actually a response to the previously sent command.
676 * MCP2221A firmware specific return codes are parsed and 0 or
677 * appropriate negative error code is returned. Delayed response
678 * results in timeout error and stray reponses results in -EIO.
680 static int mcp2221_raw_event(struct hid_device
*hdev
,
681 struct hid_report
*report
, u8
*data
, int size
)
684 struct mcp2221
*mcp
= hid_get_drvdata(hdev
);
688 case MCP2221_I2C_WR_DATA
:
689 case MCP2221_I2C_WR_NO_STOP
:
690 case MCP2221_I2C_RD_DATA
:
691 case MCP2221_I2C_RD_RPT_START
:
693 case MCP2221_SUCCESS
:
697 mcp
->status
= mcp_get_i2c_eng_state(mcp
, data
, 2);
699 complete(&mcp
->wait_in_report
);
702 case MCP2221_I2C_PARAM_OR_STATUS
:
704 case MCP2221_SUCCESS
:
705 if ((mcp
->txbuf
[3] == MCP2221_I2C_SET_SPEED
) &&
706 (data
[3] != MCP2221_I2C_SET_SPEED
)) {
707 mcp
->status
= -EAGAIN
;
710 if (data
[20] & MCP2221_I2C_MASK_ADDR_NACK
) {
711 mcp
->status
= -ENXIO
;
714 mcp
->status
= mcp_get_i2c_eng_state(mcp
, data
, 8);
719 complete(&mcp
->wait_in_report
);
722 case MCP2221_I2C_GET_DATA
:
724 case MCP2221_SUCCESS
:
725 if (data
[2] == MCP2221_I2C_ADDR_NACK
) {
726 mcp
->status
= -ENXIO
;
729 if (!mcp_get_i2c_eng_state(mcp
, data
, 2)
734 if (data
[3] == 127) {
738 if (data
[2] == MCP2221_I2C_READ_COMPL
) {
740 memcpy(&buf
[mcp
->rxbuf_idx
], &data
[4], data
[3]);
741 mcp
->rxbuf_idx
= mcp
->rxbuf_idx
+ data
[3];
750 complete(&mcp
->wait_in_report
);
753 case MCP2221_GPIO_GET
:
755 case MCP2221_SUCCESS
:
756 if ((data
[mcp
->gp_idx
] == MCP2221_ALT_F_NOT_GPIOV
) ||
757 (data
[mcp
->gp_idx
+ 1] == MCP2221_ALT_F_NOT_GPIOD
)) {
758 mcp
->status
= -ENOENT
;
760 mcp
->status
= !!data
[mcp
->gp_idx
];
761 mcp
->gpio_dir
= !!data
[mcp
->gp_idx
+ 1];
765 mcp
->status
= -EAGAIN
;
767 complete(&mcp
->wait_in_report
);
770 case MCP2221_GPIO_SET
:
772 case MCP2221_SUCCESS
:
773 if ((data
[mcp
->gp_idx
] == MCP2221_ALT_F_NOT_GPIOV
) ||
774 (data
[mcp
->gp_idx
- 1] == MCP2221_ALT_F_NOT_GPIOV
)) {
775 mcp
->status
= -ENOENT
;
781 mcp
->status
= -EAGAIN
;
783 complete(&mcp
->wait_in_report
);
788 complete(&mcp
->wait_in_report
);
794 static int mcp2221_probe(struct hid_device
*hdev
,
795 const struct hid_device_id
*id
)
800 mcp
= devm_kzalloc(&hdev
->dev
, sizeof(*mcp
), GFP_KERNEL
);
804 ret
= hid_parse(hdev
);
806 hid_err(hdev
, "can't parse reports\n");
810 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
812 hid_err(hdev
, "can't start hardware\n");
816 ret
= hid_hw_open(hdev
);
818 hid_err(hdev
, "can't open device\n");
822 mutex_init(&mcp
->lock
);
823 init_completion(&mcp
->wait_in_report
);
824 hid_set_drvdata(hdev
, mcp
);
827 /* Set I2C bus clock diviser */
828 if (i2c_clk_freq
> 400)
830 if (i2c_clk_freq
< 50)
832 mcp
->cur_i2c_clk_div
= (12000000 / (i2c_clk_freq
* 1000)) - 3;
834 mcp
->adapter
.owner
= THIS_MODULE
;
835 mcp
->adapter
.class = I2C_CLASS_HWMON
;
836 mcp
->adapter
.algo
= &mcp_i2c_algo
;
837 mcp
->adapter
.retries
= 1;
838 mcp
->adapter
.dev
.parent
= &hdev
->dev
;
839 snprintf(mcp
->adapter
.name
, sizeof(mcp
->adapter
.name
),
840 "MCP2221 usb-i2c bridge on hidraw%d",
841 ((struct hidraw
*)hdev
->hidraw
)->minor
);
843 ret
= i2c_add_adapter(&mcp
->adapter
);
845 hid_err(hdev
, "can't add usb-i2c adapter: %d\n", ret
);
848 i2c_set_adapdata(&mcp
->adapter
, mcp
);
850 /* Setup GPIO chip */
851 mcp
->gc
= devm_kzalloc(&hdev
->dev
, sizeof(*mcp
->gc
), GFP_KERNEL
);
857 mcp
->gc
->label
= "mcp2221_gpio";
858 mcp
->gc
->direction_input
= mcp_gpio_direction_input
;
859 mcp
->gc
->direction_output
= mcp_gpio_direction_output
;
860 mcp
->gc
->get_direction
= mcp_gpio_get_direction
;
861 mcp
->gc
->set
= mcp_gpio_set
;
862 mcp
->gc
->get
= mcp_gpio_get
;
865 mcp
->gc
->can_sleep
= 1;
866 mcp
->gc
->parent
= &hdev
->dev
;
868 ret
= devm_gpiochip_add_data(&hdev
->dev
, mcp
->gc
, mcp
);
875 i2c_del_adapter(&mcp
->adapter
);
877 hid_hw_close(mcp
->hdev
);
879 hid_hw_stop(mcp
->hdev
);
883 static void mcp2221_remove(struct hid_device
*hdev
)
885 struct mcp2221
*mcp
= hid_get_drvdata(hdev
);
887 i2c_del_adapter(&mcp
->adapter
);
888 hid_hw_close(mcp
->hdev
);
889 hid_hw_stop(mcp
->hdev
);
892 static const struct hid_device_id mcp2221_devices
[] = {
893 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_MCP2221
) },
896 MODULE_DEVICE_TABLE(hid
, mcp2221_devices
);
898 static struct hid_driver mcp2221_driver
= {
900 .id_table
= mcp2221_devices
,
901 .probe
= mcp2221_probe
,
902 .remove
= mcp2221_remove
,
903 .raw_event
= mcp2221_raw_event
,
906 /* Register with HID core */
907 module_hid_driver(mcp2221_driver
);
909 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
910 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
911 MODULE_LICENSE("GPL v2");