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/bitfield.h>
14 #include <linux/completion.h>
15 #include <linux/delay.h>
16 #include <linux/hid.h>
17 #include <linux/hidraw.h>
18 #include <linux/i2c.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/iio/iio.h>
23 /* Commands codes in a raw output report */
25 MCP2221_I2C_WR_DATA = 0x90,
26 MCP2221_I2C_WR_NO_STOP = 0x94,
27 MCP2221_I2C_RD_DATA = 0x91,
28 MCP2221_I2C_RD_RPT_START = 0x93,
29 MCP2221_I2C_GET_DATA = 0x40,
30 MCP2221_I2C_PARAM_OR_STATUS = 0x10,
31 MCP2221_I2C_SET_SPEED = 0x20,
32 MCP2221_I2C_CANCEL = 0x10,
33 MCP2221_GPIO_SET = 0x50,
34 MCP2221_GPIO_GET = 0x51,
35 MCP2221_SET_SRAM_SETTINGS = 0x60,
36 MCP2221_GET_SRAM_SETTINGS = 0x61,
37 MCP2221_READ_FLASH_DATA = 0xb0,
40 /* Response codes in a raw input report */
42 MCP2221_SUCCESS = 0x00,
43 MCP2221_I2C_ENG_BUSY = 0x01,
44 MCP2221_I2C_START_TOUT = 0x12,
45 MCP2221_I2C_STOP_TOUT = 0x62,
46 MCP2221_I2C_WRADDRL_TOUT = 0x23,
47 MCP2221_I2C_WRDATA_TOUT = 0x44,
48 MCP2221_I2C_WRADDRL_NACK = 0x25,
49 MCP2221_I2C_MASK_ADDR_NACK = 0x40,
50 MCP2221_I2C_WRADDRL_SEND = 0x21,
51 MCP2221_I2C_ADDR_NACK = 0x25,
52 MCP2221_I2C_READ_PARTIAL = 0x54,
53 MCP2221_I2C_READ_COMPL = 0x55,
54 MCP2221_ALT_F_NOT_GPIOV = 0xEE,
55 MCP2221_ALT_F_NOT_GPIOD = 0xEF,
58 /* MCP GPIO direction encoding */
60 MCP2221_DIR_OUT = 0x00,
61 MCP2221_DIR_IN = 0x01,
66 /* MCP GPIO set command layout */
78 /* MCP GPIO get command layout */
89 * There is no way to distinguish responses. Therefore next command
90 * is sent only after response to previous has been received. Mutex
91 * lock is used for this purpose mainly.
94 struct hid_device *hdev;
95 struct i2c_adapter adapter;
97 struct completion wait_in_report;
98 struct delayed_work init_work;
104 struct gpio_chip *gc;
108 #if IS_REACHABLE(CONFIG_IIO)
109 struct iio_chan_spec iio_channels[3];
122 * Default i2c bus clock frequency 400 kHz. Modify this if you
123 * want to set some other frequency (min 50 kHz - max 400 kHz).
125 static uint i2c_clk_freq = 400;
127 /* Synchronously send output report to the device */
128 static int mcp_send_report(struct mcp2221 *mcp,
129 u8 *out_report, size_t len)
134 buf = kmemdup(out_report, len, GFP_KERNEL);
138 /* mcp2221 uses interrupt endpoint for out reports */
139 ret = hid_hw_output_report(mcp->hdev, buf, len);
148 * Send o/p report to the device and wait for i/p report to be
149 * received from the device. If the device does not respond,
152 static int mcp_send_data_req_status(struct mcp2221 *mcp,
153 u8 *out_report, int len)
158 reinit_completion(&mcp->wait_in_report);
160 ret = mcp_send_report(mcp, out_report, len);
164 t = wait_for_completion_timeout(&mcp->wait_in_report,
165 msecs_to_jiffies(4000));
172 /* Check pass/fail for actual communication with i2c slave */
173 static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
175 memset(mcp->txbuf, 0, 8);
176 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
178 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
181 /* Cancels last command releasing i2c bus just in case occupied */
182 static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
184 memset(mcp->txbuf, 0, 8);
185 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
186 mcp->txbuf[2] = MCP2221_I2C_CANCEL;
188 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
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.
194 static int mcp_chk_last_cmd_status_free_bus(struct mcp2221 *mcp)
198 ret = mcp_chk_last_cmd_status(mcp);
200 /* The last command was a failure.
201 * Send a cancel which will also free the bus.
203 usleep_range(980, 1000);
204 mcp_cancel_last_cmd(mcp);
210 static int mcp_set_i2c_speed(struct mcp2221 *mcp)
214 memset(mcp->txbuf, 0, 8);
215 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
216 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
217 mcp->txbuf[4] = mcp->cur_i2c_clk_div;
219 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
221 /* Small delay is needed here */
222 usleep_range(980, 1000);
223 mcp_cancel_last_cmd(mcp);
230 * An output report can contain minimum 1 and maximum 60 user data
231 * bytes. If the number of data bytes is more then 60, we send it
232 * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
233 * bytes. Total number of bytes is informed in very first report to
234 * mcp2221, from that point onwards it first collect all the data
235 * from host and then send to i2c slave device.
237 static int mcp_i2c_write(struct mcp2221 *mcp,
238 struct i2c_msg *msg, int type, u8 last_status)
240 int ret, len, idx, sent;
250 mcp->txbuf[0] = type;
251 mcp->txbuf[1] = msg->len & 0xff;
252 mcp->txbuf[2] = msg->len >> 8;
253 mcp->txbuf[3] = (u8)(msg->addr << 1);
255 memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
257 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
261 usleep_range(980, 1000);
264 ret = mcp_chk_last_cmd_status_free_bus(mcp);
270 if (sent >= msg->len)
274 if ((msg->len - sent) < 60)
275 len = msg->len - sent;
280 * Testing shows delay is needed between successive writes
281 * otherwise next write fails on first-try from i2c core.
282 * This value is obtained through automated stress testing.
284 usleep_range(980, 1000);
291 * Device reads all data (0 - 65535 bytes) from i2c slave device and
292 * stores it in device itself. This data is read back from device to
293 * host in multiples of 60 bytes using input reports.
295 static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
296 struct i2c_msg *msg, int type, u16 smbus_addr,
297 u8 smbus_len, u8 *smbus_buf)
303 mcp->txbuf[0] = type;
305 mcp->txbuf[1] = msg->len & 0xff;
306 mcp->txbuf[2] = msg->len >> 8;
307 mcp->txbuf[3] = (u8)(msg->addr << 1);
308 total_len = msg->len;
309 mcp->rxbuf = msg->buf;
311 mcp->txbuf[1] = smbus_len;
313 mcp->txbuf[3] = (u8)(smbus_addr << 1);
314 total_len = smbus_len;
315 mcp->rxbuf = smbus_buf;
318 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
325 /* Wait for the data to be read by the device */
326 usleep_range(980, 1000);
328 memset(mcp->txbuf, 0, 4);
329 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
331 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
334 /* The data wasn't ready to read.
335 * Wait a bit longer and try again.
337 usleep_range(90, 100);
345 } while (mcp->rxbuf_idx < total_len);
347 usleep_range(980, 1000);
348 ret = mcp_chk_last_cmd_status_free_bus(mcp);
353 static int mcp_i2c_xfer(struct i2c_adapter *adapter,
354 struct i2c_msg msgs[], int num)
357 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
359 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
361 mutex_lock(&mcp->lock);
364 if (msgs->flags & I2C_M_RD) {
365 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
368 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
373 } else if (num == 2) {
374 /* Ex transaction; send reg address and read its contents */
375 if (msgs[0].addr == msgs[1].addr &&
376 !(msgs[0].flags & I2C_M_RD) &&
377 (msgs[1].flags & I2C_M_RD)) {
379 ret = mcp_i2c_write(mcp, &msgs[0],
380 MCP2221_I2C_WR_NO_STOP, 0);
384 ret = mcp_i2c_smbus_read(mcp, &msgs[1],
385 MCP2221_I2C_RD_RPT_START,
391 dev_err(&adapter->dev,
392 "unsupported multi-msg i2c transaction\n");
396 dev_err(&adapter->dev,
397 "unsupported multi-msg i2c transaction\n");
402 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
403 mutex_unlock(&mcp->lock);
407 static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
408 u8 command, u8 *buf, u8 len, int type,
413 mcp->txbuf[0] = type;
414 mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
416 mcp->txbuf[3] = (u8)(addr << 1);
417 mcp->txbuf[4] = command;
424 mcp->txbuf[5] = buf[0];
428 mcp->txbuf[5] = buf[0];
429 mcp->txbuf[6] = buf[1];
433 if (len > I2C_SMBUS_BLOCK_MAX)
436 memcpy(&mcp->txbuf[5], buf, len);
440 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
445 usleep_range(980, 1000);
447 ret = mcp_chk_last_cmd_status_free_bus(mcp);
453 static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
454 unsigned short flags, char read_write,
455 u8 command, int size,
456 union i2c_smbus_data *data)
459 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
461 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
463 mutex_lock(&mcp->lock);
467 case I2C_SMBUS_QUICK:
468 if (read_write == I2C_SMBUS_READ)
469 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
470 addr, 0, &data->byte);
472 ret = mcp_smbus_write(mcp, addr, command, NULL,
473 0, MCP2221_I2C_WR_DATA, 1);
476 if (read_write == I2C_SMBUS_READ)
477 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
478 addr, 1, &data->byte);
480 ret = mcp_smbus_write(mcp, addr, command, NULL,
481 0, MCP2221_I2C_WR_DATA, 1);
483 case I2C_SMBUS_BYTE_DATA:
484 if (read_write == I2C_SMBUS_READ) {
485 ret = mcp_smbus_write(mcp, addr, command, NULL,
486 0, MCP2221_I2C_WR_NO_STOP, 0);
490 ret = mcp_i2c_smbus_read(mcp, NULL,
491 MCP2221_I2C_RD_RPT_START,
492 addr, 1, &data->byte);
494 ret = mcp_smbus_write(mcp, addr, command, &data->byte,
495 1, MCP2221_I2C_WR_DATA, 1);
498 case I2C_SMBUS_WORD_DATA:
499 if (read_write == I2C_SMBUS_READ) {
500 ret = mcp_smbus_write(mcp, addr, command, NULL,
501 0, MCP2221_I2C_WR_NO_STOP, 0);
505 ret = mcp_i2c_smbus_read(mcp, NULL,
506 MCP2221_I2C_RD_RPT_START,
507 addr, 2, (u8 *)&data->word);
509 ret = mcp_smbus_write(mcp, addr, command,
510 (u8 *)&data->word, 2,
511 MCP2221_I2C_WR_DATA, 1);
514 case I2C_SMBUS_BLOCK_DATA:
515 if (read_write == I2C_SMBUS_READ) {
516 ret = mcp_smbus_write(mcp, addr, command, NULL,
517 0, MCP2221_I2C_WR_NO_STOP, 1);
522 mcp->rxbuf = data->block;
523 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
524 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
528 if (!data->block[0]) {
532 ret = mcp_smbus_write(mcp, addr, command, data->block,
534 MCP2221_I2C_WR_DATA, 1);
537 case I2C_SMBUS_I2C_BLOCK_DATA:
538 if (read_write == I2C_SMBUS_READ) {
539 ret = mcp_smbus_write(mcp, addr, command, NULL,
540 0, MCP2221_I2C_WR_NO_STOP, 1);
545 mcp->rxbuf = data->block;
546 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
547 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
551 if (!data->block[0]) {
555 ret = mcp_smbus_write(mcp, addr, command,
556 &data->block[1], data->block[0],
557 MCP2221_I2C_WR_DATA, 1);
560 case I2C_SMBUS_PROC_CALL:
561 ret = mcp_smbus_write(mcp, addr, command,
563 2, MCP2221_I2C_WR_NO_STOP, 0);
567 ret = mcp_i2c_smbus_read(mcp, NULL,
568 MCP2221_I2C_RD_RPT_START,
569 addr, 2, (u8 *)&data->word);
571 case I2C_SMBUS_BLOCK_PROC_CALL:
572 ret = mcp_smbus_write(mcp, addr, command, data->block,
574 MCP2221_I2C_WR_NO_STOP, 0);
578 ret = mcp_i2c_smbus_read(mcp, NULL,
579 MCP2221_I2C_RD_RPT_START,
580 addr, I2C_SMBUS_BLOCK_MAX,
584 dev_err(&mcp->adapter.dev,
585 "unsupported smbus transaction size:%d\n", size);
590 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
591 mutex_unlock(&mcp->lock);
595 static u32 mcp_i2c_func(struct i2c_adapter *adapter)
597 return I2C_FUNC_I2C |
598 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
599 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
600 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
603 static const struct i2c_algorithm mcp_i2c_algo = {
604 .master_xfer = mcp_i2c_xfer,
605 .smbus_xfer = mcp_smbus_xfer,
606 .functionality = mcp_i2c_func,
609 #if IS_REACHABLE(CONFIG_GPIOLIB)
610 static int mcp_gpio_get(struct gpio_chip *gc,
614 struct mcp2221 *mcp = gpiochip_get_data(gc);
616 mcp->txbuf[0] = MCP2221_GPIO_GET;
618 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);
620 mutex_lock(&mcp->lock);
621 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
622 mutex_unlock(&mcp->lock);
627 static void mcp_gpio_set(struct gpio_chip *gc,
628 unsigned int offset, int value)
630 struct mcp2221 *mcp = gpiochip_get_data(gc);
632 memset(mcp->txbuf, 0, 18);
633 mcp->txbuf[0] = MCP2221_GPIO_SET;
635 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
637 mcp->txbuf[mcp->gp_idx - 1] = 1;
638 mcp->txbuf[mcp->gp_idx] = !!value;
640 mutex_lock(&mcp->lock);
641 mcp_send_data_req_status(mcp, mcp->txbuf, 18);
642 mutex_unlock(&mcp->lock);
645 static int mcp_gpio_dir_set(struct mcp2221 *mcp,
646 unsigned int offset, u8 val)
648 memset(mcp->txbuf, 0, 18);
649 mcp->txbuf[0] = MCP2221_GPIO_SET;
651 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
653 mcp->txbuf[mcp->gp_idx - 1] = 1;
654 mcp->txbuf[mcp->gp_idx] = val;
656 return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
659 static int mcp_gpio_direction_input(struct gpio_chip *gc,
663 struct mcp2221 *mcp = gpiochip_get_data(gc);
665 mutex_lock(&mcp->lock);
666 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
667 mutex_unlock(&mcp->lock);
672 static int mcp_gpio_direction_output(struct gpio_chip *gc,
673 unsigned int offset, int value)
676 struct mcp2221 *mcp = gpiochip_get_data(gc);
678 mutex_lock(&mcp->lock);
679 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
680 mutex_unlock(&mcp->lock);
682 /* Can't configure as output, bailout early */
686 mcp_gpio_set(gc, offset, value);
691 static int mcp_gpio_get_direction(struct gpio_chip *gc,
695 struct mcp2221 *mcp = gpiochip_get_data(gc);
697 mcp->txbuf[0] = MCP2221_GPIO_GET;
699 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);
701 mutex_lock(&mcp->lock);
702 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
703 mutex_unlock(&mcp->lock);
708 if (mcp->gpio_dir == MCP2221_DIR_IN)
709 return GPIO_LINE_DIRECTION_IN;
711 return GPIO_LINE_DIRECTION_OUT;
715 /* Gives current state of i2c engine inside mcp2221 */
716 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
722 case MCP2221_I2C_WRADDRL_NACK:
723 case MCP2221_I2C_WRADDRL_SEND:
726 case MCP2221_I2C_START_TOUT:
727 case MCP2221_I2C_STOP_TOUT:
728 case MCP2221_I2C_WRADDRL_TOUT:
729 case MCP2221_I2C_WRDATA_TOUT:
732 case MCP2221_I2C_ENG_BUSY:
735 case MCP2221_SUCCESS:
746 * MCP2221 uses interrupt endpoint for input reports. This function
747 * is called by HID layer when it receives i/p report from mcp2221,
748 * which is actually a response to the previously sent command.
750 * MCP2221A firmware specific return codes are parsed and 0 or
751 * appropriate negative error code is returned. Delayed response
752 * results in timeout error and stray reponses results in -EIO.
754 static int mcp2221_raw_event(struct hid_device *hdev,
755 struct hid_report *report, u8 *data, int size)
758 struct mcp2221 *mcp = hid_get_drvdata(hdev);
762 case MCP2221_I2C_WR_DATA:
763 case MCP2221_I2C_WR_NO_STOP:
764 case MCP2221_I2C_RD_DATA:
765 case MCP2221_I2C_RD_RPT_START:
767 case MCP2221_SUCCESS:
771 mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
773 complete(&mcp->wait_in_report);
776 case MCP2221_I2C_PARAM_OR_STATUS:
778 case MCP2221_SUCCESS:
779 if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
780 (data[3] != MCP2221_I2C_SET_SPEED)) {
781 mcp->status = -EAGAIN;
784 if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
785 mcp->status = -ENXIO;
788 mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
789 #if IS_REACHABLE(CONFIG_IIO)
790 memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values));
796 complete(&mcp->wait_in_report);
799 case MCP2221_I2C_GET_DATA:
801 case MCP2221_SUCCESS:
802 if (data[2] == MCP2221_I2C_ADDR_NACK) {
803 mcp->status = -ENXIO;
806 if (!mcp_get_i2c_eng_state(mcp, data, 2)
811 if (data[3] == 127) {
815 if (data[2] == MCP2221_I2C_READ_COMPL ||
816 data[2] == MCP2221_I2C_READ_PARTIAL) {
818 memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
819 mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
828 complete(&mcp->wait_in_report);
831 case MCP2221_GPIO_GET:
833 case MCP2221_SUCCESS:
834 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
835 (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
836 mcp->status = -ENOENT;
838 mcp->status = !!data[mcp->gp_idx];
839 mcp->gpio_dir = data[mcp->gp_idx + 1];
843 mcp->status = -EAGAIN;
845 complete(&mcp->wait_in_report);
848 case MCP2221_GPIO_SET:
850 case MCP2221_SUCCESS:
851 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
852 (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
853 mcp->status = -ENOENT;
859 mcp->status = -EAGAIN;
861 complete(&mcp->wait_in_report);
864 case MCP2221_SET_SRAM_SETTINGS:
866 case MCP2221_SUCCESS:
870 mcp->status = -EAGAIN;
872 complete(&mcp->wait_in_report);
875 case MCP2221_GET_SRAM_SETTINGS:
877 case MCP2221_SUCCESS:
878 memcpy(&mcp->mode, &data[22], 4);
879 #if IS_REACHABLE(CONFIG_IIO)
880 mcp->dac_value = data[6] & GENMASK(4, 0);
885 mcp->status = -EAGAIN;
887 complete(&mcp->wait_in_report);
890 case MCP2221_READ_FLASH_DATA:
892 case MCP2221_SUCCESS:
895 /* Only handles CHIP SETTINGS subpage currently */
896 if (mcp->txbuf[1] != 0) {
901 #if IS_REACHABLE(CONFIG_IIO)
904 /* DAC scale value */
905 tmp = FIELD_GET(GENMASK(7, 6), data[6]);
906 if ((data[6] & BIT(5)) && tmp)
907 mcp->dac_scale = tmp + 4;
911 /* ADC scale value */
912 tmp = FIELD_GET(GENMASK(4, 3), data[7]);
913 if ((data[7] & BIT(2)) && tmp)
914 mcp->adc_scale = tmp - 1;
922 mcp->status = -EAGAIN;
924 complete(&mcp->wait_in_report);
929 complete(&mcp->wait_in_report);
935 /* Device resource managed function for HID unregistration */
936 static void mcp2221_hid_unregister(void *ptr)
938 struct hid_device *hdev = ptr;
944 /* This is needed to be sure hid_hw_stop() isn't called twice by the subsystem */
945 static void mcp2221_remove(struct hid_device *hdev)
947 struct mcp2221 *mcp = hid_get_drvdata(hdev);
949 cancel_delayed_work_sync(&mcp->init_work);
952 #if IS_REACHABLE(CONFIG_IIO)
953 static int mcp2221_read_raw(struct iio_dev *indio_dev,
954 struct iio_chan_spec const *channel, int *val,
955 int *val2, long mask)
957 struct mcp2221_iio *priv = iio_priv(indio_dev);
958 struct mcp2221 *mcp = priv->mcp;
961 if (mask == IIO_CHAN_INFO_SCALE) {
963 *val = 1 << mcp->dac_scale;
965 *val = 1 << mcp->adc_scale;
970 mutex_lock(&mcp->lock);
972 if (channel->output) {
973 *val = mcp->dac_value;
976 /* Read ADC values */
977 ret = mcp_chk_last_cmd_status(mcp);
980 *val = le16_to_cpu((__force __le16) mcp->adc_values[channel->address]);
988 mutex_unlock(&mcp->lock);
993 static int mcp2221_write_raw(struct iio_dev *indio_dev,
994 struct iio_chan_spec const *chan,
995 int val, int val2, long mask)
997 struct mcp2221_iio *priv = iio_priv(indio_dev);
998 struct mcp2221 *mcp = priv->mcp;
1001 if (val < 0 || val >= BIT(5))
1004 mutex_lock(&mcp->lock);
1006 memset(mcp->txbuf, 0, 12);
1007 mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS;
1008 mcp->txbuf[4] = BIT(7) | val;
1010 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12);
1012 mcp->dac_value = val;
1014 mutex_unlock(&mcp->lock);
1019 static const struct iio_info mcp2221_info = {
1020 .read_raw = &mcp2221_read_raw,
1021 .write_raw = &mcp2221_write_raw,
1024 static int mcp_iio_channels(struct mcp2221 *mcp)
1027 bool dac_created = false;
1029 /* GP0 doesn't have ADC/DAC alternative function */
1030 for (idx = 1; idx < MCP_NGPIO; idx++) {
1031 struct iio_chan_spec *chan = &mcp->iio_channels[cnt];
1033 switch (mcp->mode[idx]) {
1035 chan->address = idx - 1;
1036 chan->channel = cnt++;
1039 /* GP1 doesn't have DAC alternative function */
1040 if (idx == 1 || dac_created)
1042 /* DAC1 and DAC2 outputs are connected to the same DAC */
1051 chan->type = IIO_VOLTAGE;
1053 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
1054 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
1055 chan->scan_index = -1;
1061 static void mcp_init_work(struct work_struct *work)
1063 struct iio_dev *indio_dev;
1064 struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work);
1065 struct mcp2221_iio *data;
1066 static int retries = 5;
1067 int ret, num_channels;
1069 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
1070 mutex_lock(&mcp->lock);
1072 mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS;
1073 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
1076 goto reschedule_task;
1078 num_channels = mcp_iio_channels(mcp);
1082 mcp->txbuf[0] = MCP2221_READ_FLASH_DATA;
1084 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2);
1087 goto reschedule_task;
1089 indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data));
1093 data = iio_priv(indio_dev);
1096 indio_dev->name = "mcp2221";
1097 indio_dev->modes = INDIO_DIRECT_MODE;
1098 indio_dev->info = &mcp2221_info;
1099 indio_dev->channels = mcp->iio_channels;
1100 indio_dev->num_channels = num_channels;
1102 devm_iio_device_register(&mcp->hdev->dev, indio_dev);
1105 mutex_unlock(&mcp->lock);
1106 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1111 mutex_unlock(&mcp->lock);
1112 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1117 /* Device is not ready to read SRAM or FLASH data, try again */
1118 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1122 static int mcp2221_probe(struct hid_device *hdev,
1123 const struct hid_device_id *id)
1126 struct mcp2221 *mcp;
1128 mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
1132 ret = hid_parse(hdev);
1134 hid_err(hdev, "can't parse reports\n");
1139 * This driver uses the .raw_event callback and therefore does not need any
1140 * HID_CONNECT_xxx flags.
1142 ret = hid_hw_start(hdev, 0);
1144 hid_err(hdev, "can't start hardware\n");
1148 hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8,
1149 hdev->version & 0xff, hdev->name, hdev->phys);
1151 ret = hid_hw_open(hdev);
1153 hid_err(hdev, "can't open device\n");
1158 mutex_init(&mcp->lock);
1159 init_completion(&mcp->wait_in_report);
1160 hid_set_drvdata(hdev, mcp);
1163 ret = devm_add_action_or_reset(&hdev->dev, mcp2221_hid_unregister, hdev);
1167 hid_device_io_start(hdev);
1169 /* Set I2C bus clock diviser */
1170 if (i2c_clk_freq > 400)
1172 if (i2c_clk_freq < 50)
1174 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
1175 ret = mcp_set_i2c_speed(mcp);
1177 hid_err(hdev, "can't set i2c speed: %d\n", ret);
1181 mcp->adapter.owner = THIS_MODULE;
1182 mcp->adapter.class = I2C_CLASS_HWMON;
1183 mcp->adapter.algo = &mcp_i2c_algo;
1184 mcp->adapter.retries = 1;
1185 mcp->adapter.dev.parent = &hdev->dev;
1186 ACPI_COMPANION_SET(&mcp->adapter.dev, ACPI_COMPANION(hdev->dev.parent));
1187 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
1188 "MCP2221 usb-i2c bridge");
1190 i2c_set_adapdata(&mcp->adapter, mcp);
1191 ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter);
1193 hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
1197 #if IS_REACHABLE(CONFIG_GPIOLIB)
1198 /* Setup GPIO chip */
1199 mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
1203 mcp->gc->label = "mcp2221_gpio";
1204 mcp->gc->direction_input = mcp_gpio_direction_input;
1205 mcp->gc->direction_output = mcp_gpio_direction_output;
1206 mcp->gc->get_direction = mcp_gpio_get_direction;
1207 mcp->gc->set = mcp_gpio_set;
1208 mcp->gc->get = mcp_gpio_get;
1209 mcp->gc->ngpio = MCP_NGPIO;
1211 mcp->gc->can_sleep = 1;
1212 mcp->gc->parent = &hdev->dev;
1214 ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
1219 #if IS_REACHABLE(CONFIG_IIO)
1220 INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work);
1221 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1227 static const struct hid_device_id mcp2221_devices[] = {
1228 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
1231 MODULE_DEVICE_TABLE(hid, mcp2221_devices);
1233 static struct hid_driver mcp2221_driver = {
1235 .id_table = mcp2221_devices,
1236 .probe = mcp2221_probe,
1237 .remove = mcp2221_remove,
1238 .raw_event = mcp2221_raw_event,
1241 /* Register with HID core */
1242 module_hid_driver(mcp2221_driver);
1244 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
1245 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
1246 MODULE_LICENSE("GPL v2");