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_COMPL = 0x55,
53 MCP2221_ALT_F_NOT_GPIOV = 0xEE,
54 MCP2221_ALT_F_NOT_GPIOD = 0xEF,
57 /* MCP GPIO direction encoding */
59 MCP2221_DIR_OUT = 0x00,
60 MCP2221_DIR_IN = 0x01,
65 /* MCP GPIO set command layout */
77 /* MCP GPIO get command layout */
88 * There is no way to distinguish responses. Therefore next command
89 * is sent only after response to previous has been received. Mutex
90 * lock is used for this purpose mainly.
93 struct hid_device *hdev;
94 struct i2c_adapter adapter;
96 struct completion wait_in_report;
97 struct delayed_work init_work;
103 struct gpio_chip *gc;
107 #if IS_REACHABLE(CONFIG_IIO)
108 struct iio_chan_spec iio_channels[3];
121 * Default i2c bus clock frequency 400 kHz. Modify this if you
122 * want to set some other frequency (min 50 kHz - max 400 kHz).
124 static uint i2c_clk_freq = 400;
126 /* Synchronously send output report to the device */
127 static int mcp_send_report(struct mcp2221 *mcp,
128 u8 *out_report, size_t len)
133 buf = kmemdup(out_report, len, GFP_KERNEL);
137 /* mcp2221 uses interrupt endpoint for out reports */
138 ret = hid_hw_output_report(mcp->hdev, buf, len);
147 * Send o/p report to the device and wait for i/p report to be
148 * received from the device. If the device does not respond,
151 static int mcp_send_data_req_status(struct mcp2221 *mcp,
152 u8 *out_report, int len)
157 reinit_completion(&mcp->wait_in_report);
159 ret = mcp_send_report(mcp, out_report, len);
163 t = wait_for_completion_timeout(&mcp->wait_in_report,
164 msecs_to_jiffies(4000));
171 /* Check pass/fail for actual communication with i2c slave */
172 static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
174 memset(mcp->txbuf, 0, 8);
175 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
177 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
180 /* Cancels last command releasing i2c bus just in case occupied */
181 static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
183 memset(mcp->txbuf, 0, 8);
184 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
185 mcp->txbuf[2] = MCP2221_I2C_CANCEL;
187 return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
190 static int mcp_set_i2c_speed(struct mcp2221 *mcp)
194 memset(mcp->txbuf, 0, 8);
195 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
196 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
197 mcp->txbuf[4] = mcp->cur_i2c_clk_div;
199 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
201 /* Small delay is needed here */
202 usleep_range(980, 1000);
203 mcp_cancel_last_cmd(mcp);
210 * An output report can contain minimum 1 and maximum 60 user data
211 * bytes. If the number of data bytes is more then 60, we send it
212 * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
213 * bytes. Total number of bytes is informed in very first report to
214 * mcp2221, from that point onwards it first collect all the data
215 * from host and then send to i2c slave device.
217 static int mcp_i2c_write(struct mcp2221 *mcp,
218 struct i2c_msg *msg, int type, u8 last_status)
220 int ret, len, idx, sent;
230 mcp->txbuf[0] = type;
231 mcp->txbuf[1] = msg->len & 0xff;
232 mcp->txbuf[2] = msg->len >> 8;
233 mcp->txbuf[3] = (u8)(msg->addr << 1);
235 memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
237 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
241 usleep_range(980, 1000);
244 ret = mcp_chk_last_cmd_status(mcp);
250 if (sent >= msg->len)
254 if ((msg->len - sent) < 60)
255 len = msg->len - sent;
260 * Testing shows delay is needed between successive writes
261 * otherwise next write fails on first-try from i2c core.
262 * This value is obtained through automated stress testing.
264 usleep_range(980, 1000);
271 * Device reads all data (0 - 65535 bytes) from i2c slave device and
272 * stores it in device itself. This data is read back from device to
273 * host in multiples of 60 bytes using input reports.
275 static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
276 struct i2c_msg *msg, int type, u16 smbus_addr,
277 u8 smbus_len, u8 *smbus_buf)
282 mcp->txbuf[0] = type;
284 mcp->txbuf[1] = msg->len & 0xff;
285 mcp->txbuf[2] = msg->len >> 8;
286 mcp->txbuf[3] = (u8)(msg->addr << 1);
287 total_len = msg->len;
288 mcp->rxbuf = msg->buf;
290 mcp->txbuf[1] = smbus_len;
292 mcp->txbuf[3] = (u8)(smbus_addr << 1);
293 total_len = smbus_len;
294 mcp->rxbuf = smbus_buf;
297 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
304 memset(mcp->txbuf, 0, 4);
305 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
307 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
311 ret = mcp_chk_last_cmd_status(mcp);
315 usleep_range(980, 1000);
316 } while (mcp->rxbuf_idx < total_len);
321 static int mcp_i2c_xfer(struct i2c_adapter *adapter,
322 struct i2c_msg msgs[], int num)
325 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
327 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
329 mutex_lock(&mcp->lock);
331 /* Setting speed before every transaction is required for mcp2221 */
332 ret = mcp_set_i2c_speed(mcp);
337 if (msgs->flags & I2C_M_RD) {
338 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
341 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
346 } else if (num == 2) {
347 /* Ex transaction; send reg address and read its contents */
348 if (msgs[0].addr == msgs[1].addr &&
349 !(msgs[0].flags & I2C_M_RD) &&
350 (msgs[1].flags & I2C_M_RD)) {
352 ret = mcp_i2c_write(mcp, &msgs[0],
353 MCP2221_I2C_WR_NO_STOP, 0);
357 ret = mcp_i2c_smbus_read(mcp, &msgs[1],
358 MCP2221_I2C_RD_RPT_START,
364 dev_err(&adapter->dev,
365 "unsupported multi-msg i2c transaction\n");
369 dev_err(&adapter->dev,
370 "unsupported multi-msg i2c transaction\n");
375 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
376 mutex_unlock(&mcp->lock);
380 static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
381 u8 command, u8 *buf, u8 len, int type,
386 mcp->txbuf[0] = type;
387 mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
389 mcp->txbuf[3] = (u8)(addr << 1);
390 mcp->txbuf[4] = command;
397 mcp->txbuf[5] = buf[0];
401 mcp->txbuf[5] = buf[0];
402 mcp->txbuf[6] = buf[1];
406 if (len > I2C_SMBUS_BLOCK_MAX)
409 memcpy(&mcp->txbuf[5], buf, len);
413 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
418 usleep_range(980, 1000);
420 ret = mcp_chk_last_cmd_status(mcp);
428 static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
429 unsigned short flags, char read_write,
430 u8 command, int size,
431 union i2c_smbus_data *data)
434 struct mcp2221 *mcp = i2c_get_adapdata(adapter);
436 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
438 mutex_lock(&mcp->lock);
440 ret = mcp_set_i2c_speed(mcp);
446 case I2C_SMBUS_QUICK:
447 if (read_write == I2C_SMBUS_READ)
448 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
449 addr, 0, &data->byte);
451 ret = mcp_smbus_write(mcp, addr, command, NULL,
452 0, MCP2221_I2C_WR_DATA, 1);
455 if (read_write == I2C_SMBUS_READ)
456 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
457 addr, 1, &data->byte);
459 ret = mcp_smbus_write(mcp, addr, command, NULL,
460 0, MCP2221_I2C_WR_DATA, 1);
462 case I2C_SMBUS_BYTE_DATA:
463 if (read_write == I2C_SMBUS_READ) {
464 ret = mcp_smbus_write(mcp, addr, command, NULL,
465 0, MCP2221_I2C_WR_NO_STOP, 0);
469 ret = mcp_i2c_smbus_read(mcp, NULL,
470 MCP2221_I2C_RD_RPT_START,
471 addr, 1, &data->byte);
473 ret = mcp_smbus_write(mcp, addr, command, &data->byte,
474 1, MCP2221_I2C_WR_DATA, 1);
477 case I2C_SMBUS_WORD_DATA:
478 if (read_write == I2C_SMBUS_READ) {
479 ret = mcp_smbus_write(mcp, addr, command, NULL,
480 0, MCP2221_I2C_WR_NO_STOP, 0);
484 ret = mcp_i2c_smbus_read(mcp, NULL,
485 MCP2221_I2C_RD_RPT_START,
486 addr, 2, (u8 *)&data->word);
488 ret = mcp_smbus_write(mcp, addr, command,
489 (u8 *)&data->word, 2,
490 MCP2221_I2C_WR_DATA, 1);
493 case I2C_SMBUS_BLOCK_DATA:
494 if (read_write == I2C_SMBUS_READ) {
495 ret = mcp_smbus_write(mcp, addr, command, NULL,
496 0, MCP2221_I2C_WR_NO_STOP, 1);
501 mcp->rxbuf = data->block;
502 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
503 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
507 if (!data->block[0]) {
511 ret = mcp_smbus_write(mcp, addr, command, data->block,
513 MCP2221_I2C_WR_DATA, 1);
516 case I2C_SMBUS_I2C_BLOCK_DATA:
517 if (read_write == I2C_SMBUS_READ) {
518 ret = mcp_smbus_write(mcp, addr, command, NULL,
519 0, MCP2221_I2C_WR_NO_STOP, 1);
524 mcp->rxbuf = data->block;
525 mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
526 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
530 if (!data->block[0]) {
534 ret = mcp_smbus_write(mcp, addr, command,
535 &data->block[1], data->block[0],
536 MCP2221_I2C_WR_DATA, 1);
539 case I2C_SMBUS_PROC_CALL:
540 ret = mcp_smbus_write(mcp, addr, command,
542 2, MCP2221_I2C_WR_NO_STOP, 0);
546 ret = mcp_i2c_smbus_read(mcp, NULL,
547 MCP2221_I2C_RD_RPT_START,
548 addr, 2, (u8 *)&data->word);
550 case I2C_SMBUS_BLOCK_PROC_CALL:
551 ret = mcp_smbus_write(mcp, addr, command, data->block,
553 MCP2221_I2C_WR_NO_STOP, 0);
557 ret = mcp_i2c_smbus_read(mcp, NULL,
558 MCP2221_I2C_RD_RPT_START,
559 addr, I2C_SMBUS_BLOCK_MAX,
563 dev_err(&mcp->adapter.dev,
564 "unsupported smbus transaction size:%d\n", size);
569 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
570 mutex_unlock(&mcp->lock);
574 static u32 mcp_i2c_func(struct i2c_adapter *adapter)
576 return I2C_FUNC_I2C |
577 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
578 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
579 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
582 static const struct i2c_algorithm mcp_i2c_algo = {
583 .master_xfer = mcp_i2c_xfer,
584 .smbus_xfer = mcp_smbus_xfer,
585 .functionality = mcp_i2c_func,
588 #if IS_REACHABLE(CONFIG_GPIOLIB)
589 static int mcp_gpio_get(struct gpio_chip *gc,
593 struct mcp2221 *mcp = gpiochip_get_data(gc);
595 mcp->txbuf[0] = MCP2221_GPIO_GET;
597 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].value);
599 mutex_lock(&mcp->lock);
600 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
601 mutex_unlock(&mcp->lock);
606 static void mcp_gpio_set(struct gpio_chip *gc,
607 unsigned int offset, int value)
609 struct mcp2221 *mcp = gpiochip_get_data(gc);
611 memset(mcp->txbuf, 0, 18);
612 mcp->txbuf[0] = MCP2221_GPIO_SET;
614 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
616 mcp->txbuf[mcp->gp_idx - 1] = 1;
617 mcp->txbuf[mcp->gp_idx] = !!value;
619 mutex_lock(&mcp->lock);
620 mcp_send_data_req_status(mcp, mcp->txbuf, 18);
621 mutex_unlock(&mcp->lock);
624 static int mcp_gpio_dir_set(struct mcp2221 *mcp,
625 unsigned int offset, u8 val)
627 memset(mcp->txbuf, 0, 18);
628 mcp->txbuf[0] = MCP2221_GPIO_SET;
630 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
632 mcp->txbuf[mcp->gp_idx - 1] = 1;
633 mcp->txbuf[mcp->gp_idx] = val;
635 return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
638 static int mcp_gpio_direction_input(struct gpio_chip *gc,
642 struct mcp2221 *mcp = gpiochip_get_data(gc);
644 mutex_lock(&mcp->lock);
645 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
646 mutex_unlock(&mcp->lock);
651 static int mcp_gpio_direction_output(struct gpio_chip *gc,
652 unsigned int offset, int value)
655 struct mcp2221 *mcp = gpiochip_get_data(gc);
657 mutex_lock(&mcp->lock);
658 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
659 mutex_unlock(&mcp->lock);
661 /* Can't configure as output, bailout early */
665 mcp_gpio_set(gc, offset, value);
670 static int mcp_gpio_get_direction(struct gpio_chip *gc,
674 struct mcp2221 *mcp = gpiochip_get_data(gc);
676 mcp->txbuf[0] = MCP2221_GPIO_GET;
678 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].direction);
680 mutex_lock(&mcp->lock);
681 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
682 mutex_unlock(&mcp->lock);
687 if (mcp->gpio_dir == MCP2221_DIR_IN)
688 return GPIO_LINE_DIRECTION_IN;
690 return GPIO_LINE_DIRECTION_OUT;
694 /* Gives current state of i2c engine inside mcp2221 */
695 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
701 case MCP2221_I2C_WRADDRL_NACK:
702 case MCP2221_I2C_WRADDRL_SEND:
705 case MCP2221_I2C_START_TOUT:
706 case MCP2221_I2C_STOP_TOUT:
707 case MCP2221_I2C_WRADDRL_TOUT:
708 case MCP2221_I2C_WRDATA_TOUT:
711 case MCP2221_I2C_ENG_BUSY:
714 case MCP2221_SUCCESS:
725 * MCP2221 uses interrupt endpoint for input reports. This function
726 * is called by HID layer when it receives i/p report from mcp2221,
727 * which is actually a response to the previously sent command.
729 * MCP2221A firmware specific return codes are parsed and 0 or
730 * appropriate negative error code is returned. Delayed response
731 * results in timeout error and stray reponses results in -EIO.
733 static int mcp2221_raw_event(struct hid_device *hdev,
734 struct hid_report *report, u8 *data, int size)
737 struct mcp2221 *mcp = hid_get_drvdata(hdev);
741 case MCP2221_I2C_WR_DATA:
742 case MCP2221_I2C_WR_NO_STOP:
743 case MCP2221_I2C_RD_DATA:
744 case MCP2221_I2C_RD_RPT_START:
746 case MCP2221_SUCCESS:
750 mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
752 complete(&mcp->wait_in_report);
755 case MCP2221_I2C_PARAM_OR_STATUS:
757 case MCP2221_SUCCESS:
758 if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
759 (data[3] != MCP2221_I2C_SET_SPEED)) {
760 mcp->status = -EAGAIN;
763 if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
764 mcp->status = -ENXIO;
767 mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
768 #if IS_REACHABLE(CONFIG_IIO)
769 memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values));
775 complete(&mcp->wait_in_report);
778 case MCP2221_I2C_GET_DATA:
780 case MCP2221_SUCCESS:
781 if (data[2] == MCP2221_I2C_ADDR_NACK) {
782 mcp->status = -ENXIO;
785 if (!mcp_get_i2c_eng_state(mcp, data, 2)
790 if (data[3] == 127) {
794 if (data[2] == MCP2221_I2C_READ_COMPL) {
796 memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
797 mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
806 complete(&mcp->wait_in_report);
809 case MCP2221_GPIO_GET:
811 case MCP2221_SUCCESS:
812 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
813 (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
814 mcp->status = -ENOENT;
816 mcp->status = !!data[mcp->gp_idx];
817 mcp->gpio_dir = data[mcp->gp_idx + 1];
821 mcp->status = -EAGAIN;
823 complete(&mcp->wait_in_report);
826 case MCP2221_GPIO_SET:
828 case MCP2221_SUCCESS:
829 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
830 (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
831 mcp->status = -ENOENT;
837 mcp->status = -EAGAIN;
839 complete(&mcp->wait_in_report);
842 case MCP2221_SET_SRAM_SETTINGS:
844 case MCP2221_SUCCESS:
848 mcp->status = -EAGAIN;
850 complete(&mcp->wait_in_report);
853 case MCP2221_GET_SRAM_SETTINGS:
855 case MCP2221_SUCCESS:
856 memcpy(&mcp->mode, &data[22], 4);
857 #if IS_REACHABLE(CONFIG_IIO)
858 mcp->dac_value = data[6] & GENMASK(4, 0);
863 mcp->status = -EAGAIN;
865 complete(&mcp->wait_in_report);
868 case MCP2221_READ_FLASH_DATA:
870 case MCP2221_SUCCESS:
873 /* Only handles CHIP SETTINGS subpage currently */
874 if (mcp->txbuf[1] != 0) {
879 #if IS_REACHABLE(CONFIG_IIO)
882 /* DAC scale value */
883 tmp = FIELD_GET(GENMASK(7, 6), data[6]);
884 if ((data[6] & BIT(5)) && tmp)
885 mcp->dac_scale = tmp + 4;
889 /* ADC scale value */
890 tmp = FIELD_GET(GENMASK(4, 3), data[7]);
891 if ((data[7] & BIT(2)) && tmp)
892 mcp->adc_scale = tmp - 1;
900 mcp->status = -EAGAIN;
902 complete(&mcp->wait_in_report);
907 complete(&mcp->wait_in_report);
913 /* Device resource managed function for HID unregistration */
914 static void mcp2221_hid_unregister(void *ptr)
916 struct hid_device *hdev = ptr;
922 /* This is needed to be sure hid_hw_stop() isn't called twice by the subsystem */
923 static void mcp2221_remove(struct hid_device *hdev)
925 struct mcp2221 *mcp = hid_get_drvdata(hdev);
927 cancel_delayed_work_sync(&mcp->init_work);
930 #if IS_REACHABLE(CONFIG_IIO)
931 static int mcp2221_read_raw(struct iio_dev *indio_dev,
932 struct iio_chan_spec const *channel, int *val,
933 int *val2, long mask)
935 struct mcp2221_iio *priv = iio_priv(indio_dev);
936 struct mcp2221 *mcp = priv->mcp;
939 if (mask == IIO_CHAN_INFO_SCALE) {
941 *val = 1 << mcp->dac_scale;
943 *val = 1 << mcp->adc_scale;
948 mutex_lock(&mcp->lock);
950 if (channel->output) {
951 *val = mcp->dac_value;
954 /* Read ADC values */
955 ret = mcp_chk_last_cmd_status(mcp);
958 *val = le16_to_cpu((__force __le16) mcp->adc_values[channel->address]);
966 mutex_unlock(&mcp->lock);
971 static int mcp2221_write_raw(struct iio_dev *indio_dev,
972 struct iio_chan_spec const *chan,
973 int val, int val2, long mask)
975 struct mcp2221_iio *priv = iio_priv(indio_dev);
976 struct mcp2221 *mcp = priv->mcp;
979 if (val < 0 || val >= BIT(5))
982 mutex_lock(&mcp->lock);
984 memset(mcp->txbuf, 0, 12);
985 mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS;
986 mcp->txbuf[4] = BIT(7) | val;
988 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12);
990 mcp->dac_value = val;
992 mutex_unlock(&mcp->lock);
997 static const struct iio_info mcp2221_info = {
998 .read_raw = &mcp2221_read_raw,
999 .write_raw = &mcp2221_write_raw,
1002 static int mcp_iio_channels(struct mcp2221 *mcp)
1005 bool dac_created = false;
1007 /* GP0 doesn't have ADC/DAC alternative function */
1008 for (idx = 1; idx < MCP_NGPIO; idx++) {
1009 struct iio_chan_spec *chan = &mcp->iio_channels[cnt];
1011 switch (mcp->mode[idx]) {
1013 chan->address = idx - 1;
1014 chan->channel = cnt++;
1017 /* GP1 doesn't have DAC alternative function */
1018 if (idx == 1 || dac_created)
1020 /* DAC1 and DAC2 outputs are connected to the same DAC */
1029 chan->type = IIO_VOLTAGE;
1031 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
1032 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
1033 chan->scan_index = -1;
1039 static void mcp_init_work(struct work_struct *work)
1041 struct iio_dev *indio_dev;
1042 struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work);
1043 struct mcp2221_iio *data;
1044 static int retries = 5;
1045 int ret, num_channels;
1047 hid_hw_power(mcp->hdev, PM_HINT_FULLON);
1048 mutex_lock(&mcp->lock);
1050 mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS;
1051 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
1054 goto reschedule_task;
1056 num_channels = mcp_iio_channels(mcp);
1060 mcp->txbuf[0] = MCP2221_READ_FLASH_DATA;
1062 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2);
1065 goto reschedule_task;
1067 indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data));
1071 data = iio_priv(indio_dev);
1074 indio_dev->name = "mcp2221";
1075 indio_dev->modes = INDIO_DIRECT_MODE;
1076 indio_dev->info = &mcp2221_info;
1077 indio_dev->channels = mcp->iio_channels;
1078 indio_dev->num_channels = num_channels;
1080 devm_iio_device_register(&mcp->hdev->dev, indio_dev);
1083 mutex_unlock(&mcp->lock);
1084 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1089 mutex_unlock(&mcp->lock);
1090 hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1095 /* Device is not ready to read SRAM or FLASH data, try again */
1096 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1100 static int mcp2221_probe(struct hid_device *hdev,
1101 const struct hid_device_id *id)
1104 struct mcp2221 *mcp;
1106 mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
1110 ret = hid_parse(hdev);
1112 hid_err(hdev, "can't parse reports\n");
1117 * This driver uses the .raw_event callback and therefore does not need any
1118 * HID_CONNECT_xxx flags.
1120 ret = hid_hw_start(hdev, 0);
1122 hid_err(hdev, "can't start hardware\n");
1126 hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8,
1127 hdev->version & 0xff, hdev->name, hdev->phys);
1129 ret = hid_hw_open(hdev);
1131 hid_err(hdev, "can't open device\n");
1136 mutex_init(&mcp->lock);
1137 init_completion(&mcp->wait_in_report);
1138 hid_set_drvdata(hdev, mcp);
1141 ret = devm_add_action_or_reset(&hdev->dev, mcp2221_hid_unregister, hdev);
1145 /* Set I2C bus clock diviser */
1146 if (i2c_clk_freq > 400)
1148 if (i2c_clk_freq < 50)
1150 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
1152 mcp->adapter.owner = THIS_MODULE;
1153 mcp->adapter.class = I2C_CLASS_HWMON;
1154 mcp->adapter.algo = &mcp_i2c_algo;
1155 mcp->adapter.retries = 1;
1156 mcp->adapter.dev.parent = &hdev->dev;
1157 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
1158 "MCP2221 usb-i2c bridge");
1160 ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter);
1162 hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
1165 i2c_set_adapdata(&mcp->adapter, mcp);
1167 #if IS_REACHABLE(CONFIG_GPIOLIB)
1168 /* Setup GPIO chip */
1169 mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
1173 mcp->gc->label = "mcp2221_gpio";
1174 mcp->gc->direction_input = mcp_gpio_direction_input;
1175 mcp->gc->direction_output = mcp_gpio_direction_output;
1176 mcp->gc->get_direction = mcp_gpio_get_direction;
1177 mcp->gc->set = mcp_gpio_set;
1178 mcp->gc->get = mcp_gpio_get;
1179 mcp->gc->ngpio = MCP_NGPIO;
1181 mcp->gc->can_sleep = 1;
1182 mcp->gc->parent = &hdev->dev;
1184 ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
1189 #if IS_REACHABLE(CONFIG_IIO)
1190 INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work);
1191 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1197 static const struct hid_device_id mcp2221_devices[] = {
1198 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
1201 MODULE_DEVICE_TABLE(hid, mcp2221_devices);
1203 static struct hid_driver mcp2221_driver = {
1205 .id_table = mcp2221_devices,
1206 .probe = mcp2221_probe,
1207 .remove = mcp2221_remove,
1208 .raw_event = mcp2221_raw_event,
1211 /* Register with HID core */
1212 module_hid_driver(mcp2221_driver);
1214 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
1215 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
1216 MODULE_LICENSE("GPL v2");