Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * MCP2221A - Microchip USB to I2C Host Protocol Bridge
0004  *
0005  * Copyright (c) 2020, Rishi Gupta <gupt21@gmail.com>
0006  *
0007  * Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/err.h>
0012 #include <linux/mutex.h>
0013 #include <linux/completion.h>
0014 #include <linux/delay.h>
0015 #include <linux/hid.h>
0016 #include <linux/hidraw.h>
0017 #include <linux/i2c.h>
0018 #include <linux/gpio/driver.h>
0019 #include "hid-ids.h"
0020 
0021 /* Commands codes in a raw output report */
0022 enum {
0023     MCP2221_I2C_WR_DATA = 0x90,
0024     MCP2221_I2C_WR_NO_STOP = 0x94,
0025     MCP2221_I2C_RD_DATA = 0x91,
0026     MCP2221_I2C_RD_RPT_START = 0x93,
0027     MCP2221_I2C_GET_DATA = 0x40,
0028     MCP2221_I2C_PARAM_OR_STATUS = 0x10,
0029     MCP2221_I2C_SET_SPEED = 0x20,
0030     MCP2221_I2C_CANCEL = 0x10,
0031     MCP2221_GPIO_SET = 0x50,
0032     MCP2221_GPIO_GET = 0x51,
0033 };
0034 
0035 /* Response codes in a raw input report */
0036 enum {
0037     MCP2221_SUCCESS = 0x00,
0038     MCP2221_I2C_ENG_BUSY = 0x01,
0039     MCP2221_I2C_START_TOUT = 0x12,
0040     MCP2221_I2C_STOP_TOUT = 0x62,
0041     MCP2221_I2C_WRADDRL_TOUT = 0x23,
0042     MCP2221_I2C_WRDATA_TOUT = 0x44,
0043     MCP2221_I2C_WRADDRL_NACK = 0x25,
0044     MCP2221_I2C_MASK_ADDR_NACK = 0x40,
0045     MCP2221_I2C_WRADDRL_SEND = 0x21,
0046     MCP2221_I2C_ADDR_NACK = 0x25,
0047     MCP2221_I2C_READ_COMPL = 0x55,
0048     MCP2221_ALT_F_NOT_GPIOV = 0xEE,
0049     MCP2221_ALT_F_NOT_GPIOD = 0xEF,
0050 };
0051 
0052 /* MCP GPIO direction encoding */
0053 enum {
0054     MCP2221_DIR_OUT = 0x00,
0055     MCP2221_DIR_IN = 0x01,
0056 };
0057 
0058 #define MCP_NGPIO   4
0059 
0060 /* MCP GPIO set command layout */
0061 struct mcp_set_gpio {
0062     u8 cmd;
0063     u8 dummy;
0064     struct {
0065         u8 change_value;
0066         u8 value;
0067         u8 change_direction;
0068         u8 direction;
0069     } gpio[MCP_NGPIO];
0070 } __packed;
0071 
0072 /* MCP GPIO get command layout */
0073 struct mcp_get_gpio {
0074     u8 cmd;
0075     u8 dummy;
0076     struct {
0077         u8 direction;
0078         u8 value;
0079     } gpio[MCP_NGPIO];
0080 } __packed;
0081 
0082 /*
0083  * There is no way to distinguish responses. Therefore next command
0084  * is sent only after response to previous has been received. Mutex
0085  * lock is used for this purpose mainly.
0086  */
0087 struct mcp2221 {
0088     struct hid_device *hdev;
0089     struct i2c_adapter adapter;
0090     struct mutex lock;
0091     struct completion wait_in_report;
0092     u8 *rxbuf;
0093     u8 txbuf[64];
0094     int rxbuf_idx;
0095     int status;
0096     u8 cur_i2c_clk_div;
0097     struct gpio_chip *gc;
0098     u8 gp_idx;
0099     u8 gpio_dir;
0100 };
0101 
0102 /*
0103  * Default i2c bus clock frequency 400 kHz. Modify this if you
0104  * want to set some other frequency (min 50 kHz - max 400 kHz).
0105  */
0106 static uint i2c_clk_freq = 400;
0107 
0108 /* Synchronously send output report to the device */
0109 static int mcp_send_report(struct mcp2221 *mcp,
0110                     u8 *out_report, size_t len)
0111 {
0112     u8 *buf;
0113     int ret;
0114 
0115     buf = kmemdup(out_report, len, GFP_KERNEL);
0116     if (!buf)
0117         return -ENOMEM;
0118 
0119     /* mcp2221 uses interrupt endpoint for out reports */
0120     ret = hid_hw_output_report(mcp->hdev, buf, len);
0121     kfree(buf);
0122 
0123     if (ret < 0)
0124         return ret;
0125     return 0;
0126 }
0127 
0128 /*
0129  * Send o/p report to the device and wait for i/p report to be
0130  * received from the device. If the device does not respond,
0131  * we timeout.
0132  */
0133 static int mcp_send_data_req_status(struct mcp2221 *mcp,
0134             u8 *out_report, int len)
0135 {
0136     int ret;
0137     unsigned long t;
0138 
0139     reinit_completion(&mcp->wait_in_report);
0140 
0141     ret = mcp_send_report(mcp, out_report, len);
0142     if (ret)
0143         return ret;
0144 
0145     t = wait_for_completion_timeout(&mcp->wait_in_report,
0146                             msecs_to_jiffies(4000));
0147     if (!t)
0148         return -ETIMEDOUT;
0149 
0150     return mcp->status;
0151 }
0152 
0153 /* Check pass/fail for actual communication with i2c slave */
0154 static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
0155 {
0156     memset(mcp->txbuf, 0, 8);
0157     mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
0158 
0159     return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
0160 }
0161 
0162 /* Cancels last command releasing i2c bus just in case occupied */
0163 static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
0164 {
0165     memset(mcp->txbuf, 0, 8);
0166     mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
0167     mcp->txbuf[2] = MCP2221_I2C_CANCEL;
0168 
0169     return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
0170 }
0171 
0172 static int mcp_set_i2c_speed(struct mcp2221 *mcp)
0173 {
0174     int ret;
0175 
0176     memset(mcp->txbuf, 0, 8);
0177     mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
0178     mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
0179     mcp->txbuf[4] = mcp->cur_i2c_clk_div;
0180 
0181     ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
0182     if (ret) {
0183         /* Small delay is needed here */
0184         usleep_range(980, 1000);
0185         mcp_cancel_last_cmd(mcp);
0186     }
0187 
0188     return 0;
0189 }
0190 
0191 /*
0192  * An output report can contain minimum 1 and maximum 60 user data
0193  * bytes. If the number of data bytes is more then 60, we send it
0194  * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
0195  * bytes. Total number of bytes is informed in very first report to
0196  * mcp2221, from that point onwards it first collect all the data
0197  * from host and then send to i2c slave device.
0198  */
0199 static int mcp_i2c_write(struct mcp2221 *mcp,
0200                 struct i2c_msg *msg, int type, u8 last_status)
0201 {
0202     int ret, len, idx, sent;
0203 
0204     idx = 0;
0205     sent  = 0;
0206     if (msg->len < 60)
0207         len = msg->len;
0208     else
0209         len = 60;
0210 
0211     do {
0212         mcp->txbuf[0] = type;
0213         mcp->txbuf[1] = msg->len & 0xff;
0214         mcp->txbuf[2] = msg->len >> 8;
0215         mcp->txbuf[3] = (u8)(msg->addr << 1);
0216 
0217         memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
0218 
0219         ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
0220         if (ret)
0221             return ret;
0222 
0223         usleep_range(980, 1000);
0224 
0225         if (last_status) {
0226             ret = mcp_chk_last_cmd_status(mcp);
0227             if (ret)
0228                 return ret;
0229         }
0230 
0231         sent = sent + len;
0232         if (sent >= msg->len)
0233             break;
0234 
0235         idx = idx + len;
0236         if ((msg->len - sent) < 60)
0237             len = msg->len - sent;
0238         else
0239             len = 60;
0240 
0241         /*
0242          * Testing shows delay is needed between successive writes
0243          * otherwise next write fails on first-try from i2c core.
0244          * This value is obtained through automated stress testing.
0245          */
0246         usleep_range(980, 1000);
0247     } while (len > 0);
0248 
0249     return ret;
0250 }
0251 
0252 /*
0253  * Device reads all data (0 - 65535 bytes) from i2c slave device and
0254  * stores it in device itself. This data is read back from device to
0255  * host in multiples of 60 bytes using input reports.
0256  */
0257 static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
0258                 struct i2c_msg *msg, int type, u16 smbus_addr,
0259                 u8 smbus_len, u8 *smbus_buf)
0260 {
0261     int ret;
0262     u16 total_len;
0263 
0264     mcp->txbuf[0] = type;
0265     if (msg) {
0266         mcp->txbuf[1] = msg->len & 0xff;
0267         mcp->txbuf[2] = msg->len >> 8;
0268         mcp->txbuf[3] = (u8)(msg->addr << 1);
0269         total_len = msg->len;
0270         mcp->rxbuf = msg->buf;
0271     } else {
0272         mcp->txbuf[1] = smbus_len;
0273         mcp->txbuf[2] = 0;
0274         mcp->txbuf[3] = (u8)(smbus_addr << 1);
0275         total_len = smbus_len;
0276         mcp->rxbuf = smbus_buf;
0277     }
0278 
0279     ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
0280     if (ret)
0281         return ret;
0282 
0283     mcp->rxbuf_idx = 0;
0284 
0285     do {
0286         memset(mcp->txbuf, 0, 4);
0287         mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
0288 
0289         ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
0290         if (ret)
0291             return ret;
0292 
0293         ret = mcp_chk_last_cmd_status(mcp);
0294         if (ret)
0295             return ret;
0296 
0297         usleep_range(980, 1000);
0298     } while (mcp->rxbuf_idx < total_len);
0299 
0300     return ret;
0301 }
0302 
0303 static int mcp_i2c_xfer(struct i2c_adapter *adapter,
0304                 struct i2c_msg msgs[], int num)
0305 {
0306     int ret;
0307     struct mcp2221 *mcp = i2c_get_adapdata(adapter);
0308 
0309     hid_hw_power(mcp->hdev, PM_HINT_FULLON);
0310 
0311     mutex_lock(&mcp->lock);
0312 
0313     /* Setting speed before every transaction is required for mcp2221 */
0314     ret = mcp_set_i2c_speed(mcp);
0315     if (ret)
0316         goto exit;
0317 
0318     if (num == 1) {
0319         if (msgs->flags & I2C_M_RD) {
0320             ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
0321                             0, 0, NULL);
0322         } else {
0323             ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
0324         }
0325         if (ret)
0326             goto exit;
0327         ret = num;
0328     } else if (num == 2) {
0329         /* Ex transaction; send reg address and read its contents */
0330         if (msgs[0].addr == msgs[1].addr &&
0331             !(msgs[0].flags & I2C_M_RD) &&
0332              (msgs[1].flags & I2C_M_RD)) {
0333 
0334             ret = mcp_i2c_write(mcp, &msgs[0],
0335                         MCP2221_I2C_WR_NO_STOP, 0);
0336             if (ret)
0337                 goto exit;
0338 
0339             ret = mcp_i2c_smbus_read(mcp, &msgs[1],
0340                         MCP2221_I2C_RD_RPT_START,
0341                         0, 0, NULL);
0342             if (ret)
0343                 goto exit;
0344             ret = num;
0345         } else {
0346             dev_err(&adapter->dev,
0347                 "unsupported multi-msg i2c transaction\n");
0348             ret = -EOPNOTSUPP;
0349         }
0350     } else {
0351         dev_err(&adapter->dev,
0352             "unsupported multi-msg i2c transaction\n");
0353         ret = -EOPNOTSUPP;
0354     }
0355 
0356 exit:
0357     hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
0358     mutex_unlock(&mcp->lock);
0359     return ret;
0360 }
0361 
0362 static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
0363                 u8 command, u8 *buf, u8 len, int type,
0364                 u8 last_status)
0365 {
0366     int data_len, ret;
0367 
0368     mcp->txbuf[0] = type;
0369     mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
0370     mcp->txbuf[2] = 0;
0371     mcp->txbuf[3] = (u8)(addr << 1);
0372     mcp->txbuf[4] = command;
0373 
0374     switch (len) {
0375     case 0:
0376         data_len = 5;
0377         break;
0378     case 1:
0379         mcp->txbuf[5] = buf[0];
0380         data_len = 6;
0381         break;
0382     case 2:
0383         mcp->txbuf[5] = buf[0];
0384         mcp->txbuf[6] = buf[1];
0385         data_len = 7;
0386         break;
0387     default:
0388         if (len > I2C_SMBUS_BLOCK_MAX)
0389             return -EINVAL;
0390 
0391         memcpy(&mcp->txbuf[5], buf, len);
0392         data_len = len + 5;
0393     }
0394 
0395     ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
0396     if (ret)
0397         return ret;
0398 
0399     if (last_status) {
0400         usleep_range(980, 1000);
0401 
0402         ret = mcp_chk_last_cmd_status(mcp);
0403         if (ret)
0404             return ret;
0405     }
0406 
0407     return ret;
0408 }
0409 
0410 static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
0411                 unsigned short flags, char read_write,
0412                 u8 command, int size,
0413                 union i2c_smbus_data *data)
0414 {
0415     int ret;
0416     struct mcp2221 *mcp = i2c_get_adapdata(adapter);
0417 
0418     hid_hw_power(mcp->hdev, PM_HINT_FULLON);
0419 
0420     mutex_lock(&mcp->lock);
0421 
0422     ret = mcp_set_i2c_speed(mcp);
0423     if (ret)
0424         goto exit;
0425 
0426     switch (size) {
0427 
0428     case I2C_SMBUS_QUICK:
0429         if (read_write == I2C_SMBUS_READ)
0430             ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
0431                         addr, 0, &data->byte);
0432         else
0433             ret = mcp_smbus_write(mcp, addr, command, NULL,
0434                         0, MCP2221_I2C_WR_DATA, 1);
0435         break;
0436     case I2C_SMBUS_BYTE:
0437         if (read_write == I2C_SMBUS_READ)
0438             ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
0439                         addr, 1, &data->byte);
0440         else
0441             ret = mcp_smbus_write(mcp, addr, command, NULL,
0442                         0, MCP2221_I2C_WR_DATA, 1);
0443         break;
0444     case I2C_SMBUS_BYTE_DATA:
0445         if (read_write == I2C_SMBUS_READ) {
0446             ret = mcp_smbus_write(mcp, addr, command, NULL,
0447                         0, MCP2221_I2C_WR_NO_STOP, 0);
0448             if (ret)
0449                 goto exit;
0450 
0451             ret = mcp_i2c_smbus_read(mcp, NULL,
0452                         MCP2221_I2C_RD_RPT_START,
0453                         addr, 1, &data->byte);
0454         } else {
0455             ret = mcp_smbus_write(mcp, addr, command, &data->byte,
0456                         1, MCP2221_I2C_WR_DATA, 1);
0457         }
0458         break;
0459     case I2C_SMBUS_WORD_DATA:
0460         if (read_write == I2C_SMBUS_READ) {
0461             ret = mcp_smbus_write(mcp, addr, command, NULL,
0462                         0, MCP2221_I2C_WR_NO_STOP, 0);
0463             if (ret)
0464                 goto exit;
0465 
0466             ret = mcp_i2c_smbus_read(mcp, NULL,
0467                         MCP2221_I2C_RD_RPT_START,
0468                         addr, 2, (u8 *)&data->word);
0469         } else {
0470             ret = mcp_smbus_write(mcp, addr, command,
0471                         (u8 *)&data->word, 2,
0472                         MCP2221_I2C_WR_DATA, 1);
0473         }
0474         break;
0475     case I2C_SMBUS_BLOCK_DATA:
0476         if (read_write == I2C_SMBUS_READ) {
0477             ret = mcp_smbus_write(mcp, addr, command, NULL,
0478                         0, MCP2221_I2C_WR_NO_STOP, 1);
0479             if (ret)
0480                 goto exit;
0481 
0482             mcp->rxbuf_idx = 0;
0483             mcp->rxbuf = data->block;
0484             mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
0485             ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
0486             if (ret)
0487                 goto exit;
0488         } else {
0489             if (!data->block[0]) {
0490                 ret = -EINVAL;
0491                 goto exit;
0492             }
0493             ret = mcp_smbus_write(mcp, addr, command, data->block,
0494                         data->block[0] + 1,
0495                         MCP2221_I2C_WR_DATA, 1);
0496         }
0497         break;
0498     case I2C_SMBUS_I2C_BLOCK_DATA:
0499         if (read_write == I2C_SMBUS_READ) {
0500             ret = mcp_smbus_write(mcp, addr, command, NULL,
0501                         0, MCP2221_I2C_WR_NO_STOP, 1);
0502             if (ret)
0503                 goto exit;
0504 
0505             mcp->rxbuf_idx = 0;
0506             mcp->rxbuf = data->block;
0507             mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
0508             ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
0509             if (ret)
0510                 goto exit;
0511         } else {
0512             if (!data->block[0]) {
0513                 ret = -EINVAL;
0514                 goto exit;
0515             }
0516             ret = mcp_smbus_write(mcp, addr, command,
0517                         &data->block[1], data->block[0],
0518                         MCP2221_I2C_WR_DATA, 1);
0519         }
0520         break;
0521     case I2C_SMBUS_PROC_CALL:
0522         ret = mcp_smbus_write(mcp, addr, command,
0523                         (u8 *)&data->word,
0524                         2, MCP2221_I2C_WR_NO_STOP, 0);
0525         if (ret)
0526             goto exit;
0527 
0528         ret = mcp_i2c_smbus_read(mcp, NULL,
0529                         MCP2221_I2C_RD_RPT_START,
0530                         addr, 2, (u8 *)&data->word);
0531         break;
0532     case I2C_SMBUS_BLOCK_PROC_CALL:
0533         ret = mcp_smbus_write(mcp, addr, command, data->block,
0534                         data->block[0] + 1,
0535                         MCP2221_I2C_WR_NO_STOP, 0);
0536         if (ret)
0537             goto exit;
0538 
0539         ret = mcp_i2c_smbus_read(mcp, NULL,
0540                         MCP2221_I2C_RD_RPT_START,
0541                         addr, I2C_SMBUS_BLOCK_MAX,
0542                         data->block);
0543         break;
0544     default:
0545         dev_err(&mcp->adapter.dev,
0546             "unsupported smbus transaction size:%d\n", size);
0547         ret = -EOPNOTSUPP;
0548     }
0549 
0550 exit:
0551     hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
0552     mutex_unlock(&mcp->lock);
0553     return ret;
0554 }
0555 
0556 static u32 mcp_i2c_func(struct i2c_adapter *adapter)
0557 {
0558     return I2C_FUNC_I2C |
0559             I2C_FUNC_SMBUS_READ_BLOCK_DATA |
0560             I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
0561             (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
0562 }
0563 
0564 static const struct i2c_algorithm mcp_i2c_algo = {
0565     .master_xfer = mcp_i2c_xfer,
0566     .smbus_xfer = mcp_smbus_xfer,
0567     .functionality = mcp_i2c_func,
0568 };
0569 
0570 static int mcp_gpio_get(struct gpio_chip *gc,
0571                 unsigned int offset)
0572 {
0573     int ret;
0574     struct mcp2221 *mcp = gpiochip_get_data(gc);
0575 
0576     mcp->txbuf[0] = MCP2221_GPIO_GET;
0577 
0578     mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].value);
0579 
0580     mutex_lock(&mcp->lock);
0581     ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
0582     mutex_unlock(&mcp->lock);
0583 
0584     return ret;
0585 }
0586 
0587 static void mcp_gpio_set(struct gpio_chip *gc,
0588                 unsigned int offset, int value)
0589 {
0590     struct mcp2221 *mcp = gpiochip_get_data(gc);
0591 
0592     memset(mcp->txbuf, 0, 18);
0593     mcp->txbuf[0] = MCP2221_GPIO_SET;
0594 
0595     mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
0596 
0597     mcp->txbuf[mcp->gp_idx - 1] = 1;
0598     mcp->txbuf[mcp->gp_idx] = !!value;
0599 
0600     mutex_lock(&mcp->lock);
0601     mcp_send_data_req_status(mcp, mcp->txbuf, 18);
0602     mutex_unlock(&mcp->lock);
0603 }
0604 
0605 static int mcp_gpio_dir_set(struct mcp2221 *mcp,
0606                 unsigned int offset, u8 val)
0607 {
0608     memset(mcp->txbuf, 0, 18);
0609     mcp->txbuf[0] = MCP2221_GPIO_SET;
0610 
0611     mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
0612 
0613     mcp->txbuf[mcp->gp_idx - 1] = 1;
0614     mcp->txbuf[mcp->gp_idx] = val;
0615 
0616     return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
0617 }
0618 
0619 static int mcp_gpio_direction_input(struct gpio_chip *gc,
0620                 unsigned int offset)
0621 {
0622     int ret;
0623     struct mcp2221 *mcp = gpiochip_get_data(gc);
0624 
0625     mutex_lock(&mcp->lock);
0626     ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
0627     mutex_unlock(&mcp->lock);
0628 
0629     return ret;
0630 }
0631 
0632 static int mcp_gpio_direction_output(struct gpio_chip *gc,
0633                 unsigned int offset, int value)
0634 {
0635     int ret;
0636     struct mcp2221 *mcp = gpiochip_get_data(gc);
0637 
0638     mutex_lock(&mcp->lock);
0639     ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
0640     mutex_unlock(&mcp->lock);
0641 
0642     /* Can't configure as output, bailout early */
0643     if (ret)
0644         return ret;
0645 
0646     mcp_gpio_set(gc, offset, value);
0647 
0648     return 0;
0649 }
0650 
0651 static int mcp_gpio_get_direction(struct gpio_chip *gc,
0652                 unsigned int offset)
0653 {
0654     int ret;
0655     struct mcp2221 *mcp = gpiochip_get_data(gc);
0656 
0657     mcp->txbuf[0] = MCP2221_GPIO_GET;
0658 
0659     mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].direction);
0660 
0661     mutex_lock(&mcp->lock);
0662     ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
0663     mutex_unlock(&mcp->lock);
0664 
0665     if (ret)
0666         return ret;
0667 
0668     if (mcp->gpio_dir == MCP2221_DIR_IN)
0669         return GPIO_LINE_DIRECTION_IN;
0670 
0671     return GPIO_LINE_DIRECTION_OUT;
0672 }
0673 
0674 /* Gives current state of i2c engine inside mcp2221 */
0675 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
0676                 u8 *data, u8 idx)
0677 {
0678     int ret;
0679 
0680     switch (data[idx]) {
0681     case MCP2221_I2C_WRADDRL_NACK:
0682     case MCP2221_I2C_WRADDRL_SEND:
0683         ret = -ENXIO;
0684         break;
0685     case MCP2221_I2C_START_TOUT:
0686     case MCP2221_I2C_STOP_TOUT:
0687     case MCP2221_I2C_WRADDRL_TOUT:
0688     case MCP2221_I2C_WRDATA_TOUT:
0689         ret = -ETIMEDOUT;
0690         break;
0691     case MCP2221_I2C_ENG_BUSY:
0692         ret = -EAGAIN;
0693         break;
0694     case MCP2221_SUCCESS:
0695         ret = 0x00;
0696         break;
0697     default:
0698         ret = -EIO;
0699     }
0700 
0701     return ret;
0702 }
0703 
0704 /*
0705  * MCP2221 uses interrupt endpoint for input reports. This function
0706  * is called by HID layer when it receives i/p report from mcp2221,
0707  * which is actually a response to the previously sent command.
0708  *
0709  * MCP2221A firmware specific return codes are parsed and 0 or
0710  * appropriate negative error code is returned. Delayed response
0711  * results in timeout error and stray reponses results in -EIO.
0712  */
0713 static int mcp2221_raw_event(struct hid_device *hdev,
0714                 struct hid_report *report, u8 *data, int size)
0715 {
0716     u8 *buf;
0717     struct mcp2221 *mcp = hid_get_drvdata(hdev);
0718 
0719     switch (data[0]) {
0720 
0721     case MCP2221_I2C_WR_DATA:
0722     case MCP2221_I2C_WR_NO_STOP:
0723     case MCP2221_I2C_RD_DATA:
0724     case MCP2221_I2C_RD_RPT_START:
0725         switch (data[1]) {
0726         case MCP2221_SUCCESS:
0727             mcp->status = 0;
0728             break;
0729         default:
0730             mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
0731         }
0732         complete(&mcp->wait_in_report);
0733         break;
0734 
0735     case MCP2221_I2C_PARAM_OR_STATUS:
0736         switch (data[1]) {
0737         case MCP2221_SUCCESS:
0738             if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
0739                 (data[3] != MCP2221_I2C_SET_SPEED)) {
0740                 mcp->status = -EAGAIN;
0741                 break;
0742             }
0743             if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
0744                 mcp->status = -ENXIO;
0745                 break;
0746             }
0747             mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
0748             break;
0749         default:
0750             mcp->status = -EIO;
0751         }
0752         complete(&mcp->wait_in_report);
0753         break;
0754 
0755     case MCP2221_I2C_GET_DATA:
0756         switch (data[1]) {
0757         case MCP2221_SUCCESS:
0758             if (data[2] == MCP2221_I2C_ADDR_NACK) {
0759                 mcp->status = -ENXIO;
0760                 break;
0761             }
0762             if (!mcp_get_i2c_eng_state(mcp, data, 2)
0763                 && (data[3] == 0)) {
0764                 mcp->status = 0;
0765                 break;
0766             }
0767             if (data[3] == 127) {
0768                 mcp->status = -EIO;
0769                 break;
0770             }
0771             if (data[2] == MCP2221_I2C_READ_COMPL) {
0772                 buf = mcp->rxbuf;
0773                 memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
0774                 mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
0775                 mcp->status = 0;
0776                 break;
0777             }
0778             mcp->status = -EIO;
0779             break;
0780         default:
0781             mcp->status = -EIO;
0782         }
0783         complete(&mcp->wait_in_report);
0784         break;
0785 
0786     case MCP2221_GPIO_GET:
0787         switch (data[1]) {
0788         case MCP2221_SUCCESS:
0789             if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
0790                 (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
0791                 mcp->status = -ENOENT;
0792             } else {
0793                 mcp->status = !!data[mcp->gp_idx];
0794                 mcp->gpio_dir = data[mcp->gp_idx + 1];
0795             }
0796             break;
0797         default:
0798             mcp->status = -EAGAIN;
0799         }
0800         complete(&mcp->wait_in_report);
0801         break;
0802 
0803     case MCP2221_GPIO_SET:
0804         switch (data[1]) {
0805         case MCP2221_SUCCESS:
0806             if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
0807                 (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
0808                 mcp->status = -ENOENT;
0809             } else {
0810                 mcp->status = 0;
0811             }
0812             break;
0813         default:
0814             mcp->status = -EAGAIN;
0815         }
0816         complete(&mcp->wait_in_report);
0817         break;
0818 
0819     default:
0820         mcp->status = -EIO;
0821         complete(&mcp->wait_in_report);
0822     }
0823 
0824     return 1;
0825 }
0826 
0827 static int mcp2221_probe(struct hid_device *hdev,
0828                     const struct hid_device_id *id)
0829 {
0830     int ret;
0831     struct mcp2221 *mcp;
0832 
0833     mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
0834     if (!mcp)
0835         return -ENOMEM;
0836 
0837     ret = hid_parse(hdev);
0838     if (ret) {
0839         hid_err(hdev, "can't parse reports\n");
0840         return ret;
0841     }
0842 
0843     ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
0844     if (ret) {
0845         hid_err(hdev, "can't start hardware\n");
0846         return ret;
0847     }
0848 
0849     ret = hid_hw_open(hdev);
0850     if (ret) {
0851         hid_err(hdev, "can't open device\n");
0852         goto err_hstop;
0853     }
0854 
0855     mutex_init(&mcp->lock);
0856     init_completion(&mcp->wait_in_report);
0857     hid_set_drvdata(hdev, mcp);
0858     mcp->hdev = hdev;
0859 
0860     /* Set I2C bus clock diviser */
0861     if (i2c_clk_freq > 400)
0862         i2c_clk_freq = 400;
0863     if (i2c_clk_freq < 50)
0864         i2c_clk_freq = 50;
0865     mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
0866 
0867     mcp->adapter.owner = THIS_MODULE;
0868     mcp->adapter.class = I2C_CLASS_HWMON;
0869     mcp->adapter.algo = &mcp_i2c_algo;
0870     mcp->adapter.retries = 1;
0871     mcp->adapter.dev.parent = &hdev->dev;
0872     snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
0873             "MCP2221 usb-i2c bridge on hidraw%d",
0874             ((struct hidraw *)hdev->hidraw)->minor);
0875 
0876     ret = i2c_add_adapter(&mcp->adapter);
0877     if (ret) {
0878         hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
0879         goto err_i2c;
0880     }
0881     i2c_set_adapdata(&mcp->adapter, mcp);
0882 
0883     /* Setup GPIO chip */
0884     mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
0885     if (!mcp->gc) {
0886         ret = -ENOMEM;
0887         goto err_gc;
0888     }
0889 
0890     mcp->gc->label = "mcp2221_gpio";
0891     mcp->gc->direction_input = mcp_gpio_direction_input;
0892     mcp->gc->direction_output = mcp_gpio_direction_output;
0893     mcp->gc->get_direction = mcp_gpio_get_direction;
0894     mcp->gc->set = mcp_gpio_set;
0895     mcp->gc->get = mcp_gpio_get;
0896     mcp->gc->ngpio = MCP_NGPIO;
0897     mcp->gc->base = -1;
0898     mcp->gc->can_sleep = 1;
0899     mcp->gc->parent = &hdev->dev;
0900 
0901     ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
0902     if (ret)
0903         goto err_gc;
0904 
0905     return 0;
0906 
0907 err_gc:
0908     i2c_del_adapter(&mcp->adapter);
0909 err_i2c:
0910     hid_hw_close(mcp->hdev);
0911 err_hstop:
0912     hid_hw_stop(mcp->hdev);
0913     return ret;
0914 }
0915 
0916 static void mcp2221_remove(struct hid_device *hdev)
0917 {
0918     struct mcp2221 *mcp = hid_get_drvdata(hdev);
0919 
0920     i2c_del_adapter(&mcp->adapter);
0921     hid_hw_close(mcp->hdev);
0922     hid_hw_stop(mcp->hdev);
0923 }
0924 
0925 static const struct hid_device_id mcp2221_devices[] = {
0926     { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
0927     { }
0928 };
0929 MODULE_DEVICE_TABLE(hid, mcp2221_devices);
0930 
0931 static struct hid_driver mcp2221_driver = {
0932     .name       = "mcp2221",
0933     .id_table   = mcp2221_devices,
0934     .probe      = mcp2221_probe,
0935     .remove     = mcp2221_remove,
0936     .raw_event  = mcp2221_raw_event,
0937 };
0938 
0939 /* Register with HID core */
0940 module_hid_driver(mcp2221_driver);
0941 
0942 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
0943 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
0944 MODULE_LICENSE("GPL v2");