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,
52 /* MCP GPIO direction encoding */
54 MCP2221_DIR_OUT
= 0x00,
55 MCP2221_DIR_IN
= 0x01,
60 /* MCP GPIO set command layout */
72 /* MCP GPIO get command layout */
83 * There is no way to distinguish responses. Therefore next command
84 * is sent only after response to previous has been received. Mutex
85 * lock is used for this purpose mainly.
88 struct hid_device
*hdev
;
89 struct i2c_adapter adapter
;
91 struct completion wait_in_report
;
103 * Default i2c bus clock frequency 400 kHz. Modify this if you
104 * want to set some other frequency (min 50 kHz - max 400 kHz).
106 static uint i2c_clk_freq
= 400;
108 /* Synchronously send output report to the device */
109 static int mcp_send_report(struct mcp2221
*mcp
,
110 u8
*out_report
, size_t len
)
115 buf
= kmemdup(out_report
, len
, GFP_KERNEL
);
119 /* mcp2221 uses interrupt endpoint for out reports */
120 ret
= hid_hw_output_report(mcp
->hdev
, buf
, len
);
129 * Send o/p report to the device and wait for i/p report to be
130 * received from the device. If the device does not respond,
133 static int mcp_send_data_req_status(struct mcp2221
*mcp
,
134 u8
*out_report
, int len
)
139 reinit_completion(&mcp
->wait_in_report
);
141 ret
= mcp_send_report(mcp
, out_report
, len
);
145 t
= wait_for_completion_timeout(&mcp
->wait_in_report
,
146 msecs_to_jiffies(4000));
153 /* Check pass/fail for actual communication with i2c slave */
154 static int mcp_chk_last_cmd_status(struct mcp2221
*mcp
)
156 memset(mcp
->txbuf
, 0, 8);
157 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
159 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
162 /* Cancels last command releasing i2c bus just in case occupied */
163 static int mcp_cancel_last_cmd(struct mcp2221
*mcp
)
165 memset(mcp
->txbuf
, 0, 8);
166 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
167 mcp
->txbuf
[2] = MCP2221_I2C_CANCEL
;
169 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
172 static int mcp_set_i2c_speed(struct mcp2221
*mcp
)
176 memset(mcp
->txbuf
, 0, 8);
177 mcp
->txbuf
[0] = MCP2221_I2C_PARAM_OR_STATUS
;
178 mcp
->txbuf
[3] = MCP2221_I2C_SET_SPEED
;
179 mcp
->txbuf
[4] = mcp
->cur_i2c_clk_div
;
181 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 8);
183 /* Small delay is needed here */
184 usleep_range(980, 1000);
185 mcp_cancel_last_cmd(mcp
);
192 * An output report can contain minimum 1 and maximum 60 user data
193 * bytes. If the number of data bytes is more then 60, we send it
194 * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
195 * bytes. Total number of bytes is informed in very first report to
196 * mcp2221, from that point onwards it first collect all the data
197 * from host and then send to i2c slave device.
199 static int mcp_i2c_write(struct mcp2221
*mcp
,
200 struct i2c_msg
*msg
, int type
, u8 last_status
)
202 int ret
, len
, idx
, sent
;
212 mcp
->txbuf
[0] = type
;
213 mcp
->txbuf
[1] = msg
->len
& 0xff;
214 mcp
->txbuf
[2] = msg
->len
>> 8;
215 mcp
->txbuf
[3] = (u8
)(msg
->addr
<< 1);
217 memcpy(&mcp
->txbuf
[4], &msg
->buf
[idx
], len
);
219 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, len
+ 4);
223 usleep_range(980, 1000);
226 ret
= mcp_chk_last_cmd_status(mcp
);
232 if (sent
>= msg
->len
)
236 if ((msg
->len
- sent
) < 60)
237 len
= msg
->len
- sent
;
242 * Testing shows delay is needed between successive writes
243 * otherwise next write fails on first-try from i2c core.
244 * This value is obtained through automated stress testing.
246 usleep_range(980, 1000);
253 * Device reads all data (0 - 65535 bytes) from i2c slave device and
254 * stores it in device itself. This data is read back from device to
255 * host in multiples of 60 bytes using input reports.
257 static int mcp_i2c_smbus_read(struct mcp2221
*mcp
,
258 struct i2c_msg
*msg
, int type
, u16 smbus_addr
,
259 u8 smbus_len
, u8
*smbus_buf
)
264 mcp
->txbuf
[0] = type
;
266 mcp
->txbuf
[1] = msg
->len
& 0xff;
267 mcp
->txbuf
[2] = msg
->len
>> 8;
268 mcp
->txbuf
[3] = (u8
)(msg
->addr
<< 1);
269 total_len
= msg
->len
;
270 mcp
->rxbuf
= msg
->buf
;
272 mcp
->txbuf
[1] = smbus_len
;
274 mcp
->txbuf
[3] = (u8
)(smbus_addr
<< 1);
275 total_len
= smbus_len
;
276 mcp
->rxbuf
= smbus_buf
;
279 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 4);
286 memset(mcp
->txbuf
, 0, 4);
287 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
289 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
293 ret
= mcp_chk_last_cmd_status(mcp
);
297 usleep_range(980, 1000);
298 } while (mcp
->rxbuf_idx
< total_len
);
303 static int mcp_i2c_xfer(struct i2c_adapter
*adapter
,
304 struct i2c_msg msgs
[], int num
)
307 struct mcp2221
*mcp
= i2c_get_adapdata(adapter
);
309 hid_hw_power(mcp
->hdev
, PM_HINT_FULLON
);
311 mutex_lock(&mcp
->lock
);
313 /* Setting speed before every transaction is required for mcp2221 */
314 ret
= mcp_set_i2c_speed(mcp
);
319 if (msgs
->flags
& I2C_M_RD
) {
320 ret
= mcp_i2c_smbus_read(mcp
, msgs
, MCP2221_I2C_RD_DATA
,
323 ret
= mcp_i2c_write(mcp
, msgs
, MCP2221_I2C_WR_DATA
, 1);
328 } else if (num
== 2) {
329 /* Ex transaction; send reg address and read its contents */
330 if (msgs
[0].addr
== msgs
[1].addr
&&
331 !(msgs
[0].flags
& I2C_M_RD
) &&
332 (msgs
[1].flags
& I2C_M_RD
)) {
334 ret
= mcp_i2c_write(mcp
, &msgs
[0],
335 MCP2221_I2C_WR_NO_STOP
, 0);
339 ret
= mcp_i2c_smbus_read(mcp
, &msgs
[1],
340 MCP2221_I2C_RD_RPT_START
,
346 dev_err(&adapter
->dev
,
347 "unsupported multi-msg i2c transaction\n");
351 dev_err(&adapter
->dev
,
352 "unsupported multi-msg i2c transaction\n");
357 hid_hw_power(mcp
->hdev
, PM_HINT_NORMAL
);
358 mutex_unlock(&mcp
->lock
);
362 static int mcp_smbus_write(struct mcp2221
*mcp
, u16 addr
,
363 u8 command
, u8
*buf
, u8 len
, int type
,
368 mcp
->txbuf
[0] = type
;
369 mcp
->txbuf
[1] = len
+ 1; /* 1 is due to command byte itself */
371 mcp
->txbuf
[3] = (u8
)(addr
<< 1);
372 mcp
->txbuf
[4] = command
;
379 mcp
->txbuf
[5] = buf
[0];
383 mcp
->txbuf
[5] = buf
[0];
384 mcp
->txbuf
[6] = buf
[1];
388 memcpy(&mcp
->txbuf
[5], buf
, len
);
392 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, data_len
);
397 usleep_range(980, 1000);
399 ret
= mcp_chk_last_cmd_status(mcp
);
407 static int mcp_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
,
408 unsigned short flags
, char read_write
,
409 u8 command
, int size
,
410 union i2c_smbus_data
*data
)
413 struct mcp2221
*mcp
= i2c_get_adapdata(adapter
);
415 hid_hw_power(mcp
->hdev
, PM_HINT_FULLON
);
417 mutex_lock(&mcp
->lock
);
419 ret
= mcp_set_i2c_speed(mcp
);
425 case I2C_SMBUS_QUICK
:
426 if (read_write
== I2C_SMBUS_READ
)
427 ret
= mcp_i2c_smbus_read(mcp
, NULL
, MCP2221_I2C_RD_DATA
,
428 addr
, 0, &data
->byte
);
430 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
431 0, MCP2221_I2C_WR_DATA
, 1);
434 if (read_write
== I2C_SMBUS_READ
)
435 ret
= mcp_i2c_smbus_read(mcp
, NULL
, MCP2221_I2C_RD_DATA
,
436 addr
, 1, &data
->byte
);
438 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
439 0, MCP2221_I2C_WR_DATA
, 1);
441 case I2C_SMBUS_BYTE_DATA
:
442 if (read_write
== I2C_SMBUS_READ
) {
443 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
444 0, MCP2221_I2C_WR_NO_STOP
, 0);
448 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
449 MCP2221_I2C_RD_RPT_START
,
450 addr
, 1, &data
->byte
);
452 ret
= mcp_smbus_write(mcp
, addr
, command
, &data
->byte
,
453 1, MCP2221_I2C_WR_DATA
, 1);
456 case I2C_SMBUS_WORD_DATA
:
457 if (read_write
== I2C_SMBUS_READ
) {
458 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
459 0, MCP2221_I2C_WR_NO_STOP
, 0);
463 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
464 MCP2221_I2C_RD_RPT_START
,
465 addr
, 2, (u8
*)&data
->word
);
467 ret
= mcp_smbus_write(mcp
, addr
, command
,
468 (u8
*)&data
->word
, 2,
469 MCP2221_I2C_WR_DATA
, 1);
472 case I2C_SMBUS_BLOCK_DATA
:
473 if (read_write
== I2C_SMBUS_READ
) {
474 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
475 0, MCP2221_I2C_WR_NO_STOP
, 1);
480 mcp
->rxbuf
= data
->block
;
481 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
482 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
486 if (!data
->block
[0]) {
490 ret
= mcp_smbus_write(mcp
, addr
, command
, data
->block
,
492 MCP2221_I2C_WR_DATA
, 1);
495 case I2C_SMBUS_I2C_BLOCK_DATA
:
496 if (read_write
== I2C_SMBUS_READ
) {
497 ret
= mcp_smbus_write(mcp
, addr
, command
, NULL
,
498 0, MCP2221_I2C_WR_NO_STOP
, 1);
503 mcp
->rxbuf
= data
->block
;
504 mcp
->txbuf
[0] = MCP2221_I2C_GET_DATA
;
505 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
509 if (!data
->block
[0]) {
513 ret
= mcp_smbus_write(mcp
, addr
, command
,
514 &data
->block
[1], data
->block
[0],
515 MCP2221_I2C_WR_DATA
, 1);
518 case I2C_SMBUS_PROC_CALL
:
519 ret
= mcp_smbus_write(mcp
, addr
, command
,
521 2, MCP2221_I2C_WR_NO_STOP
, 0);
525 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
526 MCP2221_I2C_RD_RPT_START
,
527 addr
, 2, (u8
*)&data
->word
);
529 case I2C_SMBUS_BLOCK_PROC_CALL
:
530 ret
= mcp_smbus_write(mcp
, addr
, command
, data
->block
,
532 MCP2221_I2C_WR_NO_STOP
, 0);
536 ret
= mcp_i2c_smbus_read(mcp
, NULL
,
537 MCP2221_I2C_RD_RPT_START
,
538 addr
, I2C_SMBUS_BLOCK_MAX
,
542 dev_err(&mcp
->adapter
.dev
,
543 "unsupported smbus transaction size:%d\n", size
);
548 hid_hw_power(mcp
->hdev
, PM_HINT_NORMAL
);
549 mutex_unlock(&mcp
->lock
);
553 static u32
mcp_i2c_func(struct i2c_adapter
*adapter
)
555 return I2C_FUNC_I2C
|
556 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
557 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
558 (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_PEC
);
561 static const struct i2c_algorithm mcp_i2c_algo
= {
562 .master_xfer
= mcp_i2c_xfer
,
563 .smbus_xfer
= mcp_smbus_xfer
,
564 .functionality
= mcp_i2c_func
,
567 static int mcp_gpio_get(struct gpio_chip
*gc
,
571 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
573 mcp
->txbuf
[0] = MCP2221_GPIO_GET
;
575 mcp
->gp_idx
= offsetof(struct mcp_get_gpio
, gpio
[offset
].value
);
577 mutex_lock(&mcp
->lock
);
578 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
579 mutex_unlock(&mcp
->lock
);
584 static void mcp_gpio_set(struct gpio_chip
*gc
,
585 unsigned int offset
, int value
)
587 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
589 memset(mcp
->txbuf
, 0, 18);
590 mcp
->txbuf
[0] = MCP2221_GPIO_SET
;
592 mcp
->gp_idx
= offsetof(struct mcp_set_gpio
, gpio
[offset
].value
);
594 mcp
->txbuf
[mcp
->gp_idx
- 1] = 1;
595 mcp
->txbuf
[mcp
->gp_idx
] = !!value
;
597 mutex_lock(&mcp
->lock
);
598 mcp_send_data_req_status(mcp
, mcp
->txbuf
, 18);
599 mutex_unlock(&mcp
->lock
);
602 static int mcp_gpio_dir_set(struct mcp2221
*mcp
,
603 unsigned int offset
, u8 val
)
605 memset(mcp
->txbuf
, 0, 18);
606 mcp
->txbuf
[0] = MCP2221_GPIO_SET
;
608 mcp
->gp_idx
= offsetof(struct mcp_set_gpio
, gpio
[offset
].direction
);
610 mcp
->txbuf
[mcp
->gp_idx
- 1] = 1;
611 mcp
->txbuf
[mcp
->gp_idx
] = val
;
613 return mcp_send_data_req_status(mcp
, mcp
->txbuf
, 18);
616 static int mcp_gpio_direction_input(struct gpio_chip
*gc
,
620 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
622 mutex_lock(&mcp
->lock
);
623 ret
= mcp_gpio_dir_set(mcp
, offset
, MCP2221_DIR_IN
);
624 mutex_unlock(&mcp
->lock
);
629 static int mcp_gpio_direction_output(struct gpio_chip
*gc
,
630 unsigned int offset
, int value
)
633 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
635 mutex_lock(&mcp
->lock
);
636 ret
= mcp_gpio_dir_set(mcp
, offset
, MCP2221_DIR_OUT
);
637 mutex_unlock(&mcp
->lock
);
639 /* Can't configure as output, bailout early */
643 mcp_gpio_set(gc
, offset
, value
);
648 static int mcp_gpio_get_direction(struct gpio_chip
*gc
,
652 struct mcp2221
*mcp
= gpiochip_get_data(gc
);
654 mcp
->txbuf
[0] = MCP2221_GPIO_GET
;
656 mcp
->gp_idx
= offsetof(struct mcp_get_gpio
, gpio
[offset
].direction
);
658 mutex_lock(&mcp
->lock
);
659 ret
= mcp_send_data_req_status(mcp
, mcp
->txbuf
, 1);
660 mutex_unlock(&mcp
->lock
);
665 if (mcp
->gpio_dir
== MCP2221_DIR_IN
)
666 return GPIO_LINE_DIRECTION_IN
;
668 return GPIO_LINE_DIRECTION_OUT
;
671 /* Gives current state of i2c engine inside mcp2221 */
672 static int mcp_get_i2c_eng_state(struct mcp2221
*mcp
,
678 case MCP2221_I2C_WRADDRL_NACK
:
679 case MCP2221_I2C_WRADDRL_SEND
:
682 case MCP2221_I2C_START_TOUT
:
683 case MCP2221_I2C_STOP_TOUT
:
684 case MCP2221_I2C_WRADDRL_TOUT
:
685 case MCP2221_I2C_WRDATA_TOUT
:
688 case MCP2221_I2C_ENG_BUSY
:
691 case MCP2221_SUCCESS
:
702 * MCP2221 uses interrupt endpoint for input reports. This function
703 * is called by HID layer when it receives i/p report from mcp2221,
704 * which is actually a response to the previously sent command.
706 * MCP2221A firmware specific return codes are parsed and 0 or
707 * appropriate negative error code is returned. Delayed response
708 * results in timeout error and stray reponses results in -EIO.
710 static int mcp2221_raw_event(struct hid_device
*hdev
,
711 struct hid_report
*report
, u8
*data
, int size
)
714 struct mcp2221
*mcp
= hid_get_drvdata(hdev
);
718 case MCP2221_I2C_WR_DATA
:
719 case MCP2221_I2C_WR_NO_STOP
:
720 case MCP2221_I2C_RD_DATA
:
721 case MCP2221_I2C_RD_RPT_START
:
723 case MCP2221_SUCCESS
:
727 mcp
->status
= mcp_get_i2c_eng_state(mcp
, data
, 2);
729 complete(&mcp
->wait_in_report
);
732 case MCP2221_I2C_PARAM_OR_STATUS
:
734 case MCP2221_SUCCESS
:
735 if ((mcp
->txbuf
[3] == MCP2221_I2C_SET_SPEED
) &&
736 (data
[3] != MCP2221_I2C_SET_SPEED
)) {
737 mcp
->status
= -EAGAIN
;
740 if (data
[20] & MCP2221_I2C_MASK_ADDR_NACK
) {
741 mcp
->status
= -ENXIO
;
744 mcp
->status
= mcp_get_i2c_eng_state(mcp
, data
, 8);
749 complete(&mcp
->wait_in_report
);
752 case MCP2221_I2C_GET_DATA
:
754 case MCP2221_SUCCESS
:
755 if (data
[2] == MCP2221_I2C_ADDR_NACK
) {
756 mcp
->status
= -ENXIO
;
759 if (!mcp_get_i2c_eng_state(mcp
, data
, 2)
764 if (data
[3] == 127) {
768 if (data
[2] == MCP2221_I2C_READ_COMPL
) {
770 memcpy(&buf
[mcp
->rxbuf_idx
], &data
[4], data
[3]);
771 mcp
->rxbuf_idx
= mcp
->rxbuf_idx
+ data
[3];
780 complete(&mcp
->wait_in_report
);
783 case MCP2221_GPIO_GET
:
785 case MCP2221_SUCCESS
:
786 if ((data
[mcp
->gp_idx
] == MCP2221_ALT_F_NOT_GPIOV
) ||
787 (data
[mcp
->gp_idx
+ 1] == MCP2221_ALT_F_NOT_GPIOD
)) {
788 mcp
->status
= -ENOENT
;
790 mcp
->status
= !!data
[mcp
->gp_idx
];
791 mcp
->gpio_dir
= data
[mcp
->gp_idx
+ 1];
795 mcp
->status
= -EAGAIN
;
797 complete(&mcp
->wait_in_report
);
800 case MCP2221_GPIO_SET
:
802 case MCP2221_SUCCESS
:
803 if ((data
[mcp
->gp_idx
] == MCP2221_ALT_F_NOT_GPIOV
) ||
804 (data
[mcp
->gp_idx
- 1] == MCP2221_ALT_F_NOT_GPIOV
)) {
805 mcp
->status
= -ENOENT
;
811 mcp
->status
= -EAGAIN
;
813 complete(&mcp
->wait_in_report
);
818 complete(&mcp
->wait_in_report
);
824 static int mcp2221_probe(struct hid_device
*hdev
,
825 const struct hid_device_id
*id
)
830 mcp
= devm_kzalloc(&hdev
->dev
, sizeof(*mcp
), GFP_KERNEL
);
834 ret
= hid_parse(hdev
);
836 hid_err(hdev
, "can't parse reports\n");
840 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
842 hid_err(hdev
, "can't start hardware\n");
846 ret
= hid_hw_open(hdev
);
848 hid_err(hdev
, "can't open device\n");
852 mutex_init(&mcp
->lock
);
853 init_completion(&mcp
->wait_in_report
);
854 hid_set_drvdata(hdev
, mcp
);
857 /* Set I2C bus clock diviser */
858 if (i2c_clk_freq
> 400)
860 if (i2c_clk_freq
< 50)
862 mcp
->cur_i2c_clk_div
= (12000000 / (i2c_clk_freq
* 1000)) - 3;
864 mcp
->adapter
.owner
= THIS_MODULE
;
865 mcp
->adapter
.class = I2C_CLASS_HWMON
;
866 mcp
->adapter
.algo
= &mcp_i2c_algo
;
867 mcp
->adapter
.retries
= 1;
868 mcp
->adapter
.dev
.parent
= &hdev
->dev
;
869 snprintf(mcp
->adapter
.name
, sizeof(mcp
->adapter
.name
),
870 "MCP2221 usb-i2c bridge on hidraw%d",
871 ((struct hidraw
*)hdev
->hidraw
)->minor
);
873 ret
= i2c_add_adapter(&mcp
->adapter
);
875 hid_err(hdev
, "can't add usb-i2c adapter: %d\n", ret
);
878 i2c_set_adapdata(&mcp
->adapter
, mcp
);
880 /* Setup GPIO chip */
881 mcp
->gc
= devm_kzalloc(&hdev
->dev
, sizeof(*mcp
->gc
), GFP_KERNEL
);
887 mcp
->gc
->label
= "mcp2221_gpio";
888 mcp
->gc
->direction_input
= mcp_gpio_direction_input
;
889 mcp
->gc
->direction_output
= mcp_gpio_direction_output
;
890 mcp
->gc
->get_direction
= mcp_gpio_get_direction
;
891 mcp
->gc
->set
= mcp_gpio_set
;
892 mcp
->gc
->get
= mcp_gpio_get
;
893 mcp
->gc
->ngpio
= MCP_NGPIO
;
895 mcp
->gc
->can_sleep
= 1;
896 mcp
->gc
->parent
= &hdev
->dev
;
898 ret
= devm_gpiochip_add_data(&hdev
->dev
, mcp
->gc
, mcp
);
905 i2c_del_adapter(&mcp
->adapter
);
907 hid_hw_close(mcp
->hdev
);
909 hid_hw_stop(mcp
->hdev
);
913 static void mcp2221_remove(struct hid_device
*hdev
)
915 struct mcp2221
*mcp
= hid_get_drvdata(hdev
);
917 i2c_del_adapter(&mcp
->adapter
);
918 hid_hw_close(mcp
->hdev
);
919 hid_hw_stop(mcp
->hdev
);
922 static const struct hid_device_id mcp2221_devices
[] = {
923 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP
, USB_DEVICE_ID_MCP2221
) },
926 MODULE_DEVICE_TABLE(hid
, mcp2221_devices
);
928 static struct hid_driver mcp2221_driver
= {
930 .id_table
= mcp2221_devices
,
931 .probe
= mcp2221_probe
,
932 .remove
= mcp2221_remove
,
933 .raw_event
= mcp2221_raw_event
,
936 /* Register with HID core */
937 module_hid_driver(mcp2221_driver
);
939 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
940 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
941 MODULE_LICENSE("GPL v2");