167a95c21SRishi Gupta // SPDX-License-Identifier: GPL-2.0-only 267a95c21SRishi Gupta /* 367a95c21SRishi Gupta * MCP2221A - Microchip USB to I2C Host Protocol Bridge 467a95c21SRishi Gupta * 567a95c21SRishi Gupta * Copyright (c) 2020, Rishi Gupta <gupt21@gmail.com> 667a95c21SRishi Gupta * 7b266eacfSAlexander A. Klimov * Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf 867a95c21SRishi Gupta */ 967a95c21SRishi Gupta 1067a95c21SRishi Gupta #include <linux/module.h> 1167a95c21SRishi Gupta #include <linux/err.h> 1267a95c21SRishi Gupta #include <linux/mutex.h> 13960f9df7SMatt Ranostay #include <linux/bitfield.h> 1467a95c21SRishi Gupta #include <linux/completion.h> 1567a95c21SRishi Gupta #include <linux/delay.h> 1667a95c21SRishi Gupta #include <linux/hid.h> 1767a95c21SRishi Gupta #include <linux/hidraw.h> 1867a95c21SRishi Gupta #include <linux/i2c.h> 19328de1c5SRishi Gupta #include <linux/gpio/driver.h> 20960f9df7SMatt Ranostay #include <linux/iio/iio.h> 2167a95c21SRishi Gupta #include "hid-ids.h" 2267a95c21SRishi Gupta 2367a95c21SRishi Gupta /* Commands codes in a raw output report */ 2467a95c21SRishi Gupta enum { 2567a95c21SRishi Gupta MCP2221_I2C_WR_DATA = 0x90, 2667a95c21SRishi Gupta MCP2221_I2C_WR_NO_STOP = 0x94, 2767a95c21SRishi Gupta MCP2221_I2C_RD_DATA = 0x91, 2867a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START = 0x93, 2967a95c21SRishi Gupta MCP2221_I2C_GET_DATA = 0x40, 3067a95c21SRishi Gupta MCP2221_I2C_PARAM_OR_STATUS = 0x10, 3167a95c21SRishi Gupta MCP2221_I2C_SET_SPEED = 0x20, 3267a95c21SRishi Gupta MCP2221_I2C_CANCEL = 0x10, 33328de1c5SRishi Gupta MCP2221_GPIO_SET = 0x50, 34328de1c5SRishi Gupta MCP2221_GPIO_GET = 0x51, 35960f9df7SMatt Ranostay MCP2221_SET_SRAM_SETTINGS = 0x60, 36960f9df7SMatt Ranostay MCP2221_GET_SRAM_SETTINGS = 0x61, 37960f9df7SMatt Ranostay MCP2221_READ_FLASH_DATA = 0xb0, 3867a95c21SRishi Gupta }; 3967a95c21SRishi Gupta 4067a95c21SRishi Gupta /* Response codes in a raw input report */ 4167a95c21SRishi Gupta enum { 4267a95c21SRishi Gupta MCP2221_SUCCESS = 0x00, 4367a95c21SRishi Gupta MCP2221_I2C_ENG_BUSY = 0x01, 4467a95c21SRishi Gupta MCP2221_I2C_START_TOUT = 0x12, 4567a95c21SRishi Gupta MCP2221_I2C_STOP_TOUT = 0x62, 4667a95c21SRishi Gupta MCP2221_I2C_WRADDRL_TOUT = 0x23, 4767a95c21SRishi Gupta MCP2221_I2C_WRDATA_TOUT = 0x44, 4867a95c21SRishi Gupta MCP2221_I2C_WRADDRL_NACK = 0x25, 4967a95c21SRishi Gupta MCP2221_I2C_MASK_ADDR_NACK = 0x40, 5067a95c21SRishi Gupta MCP2221_I2C_WRADDRL_SEND = 0x21, 5167a95c21SRishi Gupta MCP2221_I2C_ADDR_NACK = 0x25, 5267a95c21SRishi Gupta MCP2221_I2C_READ_COMPL = 0x55, 53328de1c5SRishi Gupta MCP2221_ALT_F_NOT_GPIOV = 0xEE, 54328de1c5SRishi Gupta MCP2221_ALT_F_NOT_GPIOD = 0xEF, 5567a95c21SRishi Gupta }; 5667a95c21SRishi Gupta 57567b8e9fSLars Povlsen /* MCP GPIO direction encoding */ 58567b8e9fSLars Povlsen enum { 59567b8e9fSLars Povlsen MCP2221_DIR_OUT = 0x00, 60567b8e9fSLars Povlsen MCP2221_DIR_IN = 0x01, 61567b8e9fSLars Povlsen }; 62567b8e9fSLars Povlsen 63567b8e9fSLars Povlsen #define MCP_NGPIO 4 64567b8e9fSLars Povlsen 65567b8e9fSLars Povlsen /* MCP GPIO set command layout */ 66567b8e9fSLars Povlsen struct mcp_set_gpio { 67567b8e9fSLars Povlsen u8 cmd; 68567b8e9fSLars Povlsen u8 dummy; 69567b8e9fSLars Povlsen struct { 70567b8e9fSLars Povlsen u8 change_value; 71567b8e9fSLars Povlsen u8 value; 72567b8e9fSLars Povlsen u8 change_direction; 73567b8e9fSLars Povlsen u8 direction; 74567b8e9fSLars Povlsen } gpio[MCP_NGPIO]; 75567b8e9fSLars Povlsen } __packed; 76567b8e9fSLars Povlsen 77567b8e9fSLars Povlsen /* MCP GPIO get command layout */ 78567b8e9fSLars Povlsen struct mcp_get_gpio { 79567b8e9fSLars Povlsen u8 cmd; 80567b8e9fSLars Povlsen u8 dummy; 81567b8e9fSLars Povlsen struct { 82567b8e9fSLars Povlsen u8 value; 83e36c31f8SLouis Morhet u8 direction; 84567b8e9fSLars Povlsen } gpio[MCP_NGPIO]; 85567b8e9fSLars Povlsen } __packed; 86567b8e9fSLars Povlsen 8767a95c21SRishi Gupta /* 8867a95c21SRishi Gupta * There is no way to distinguish responses. Therefore next command 8967a95c21SRishi Gupta * is sent only after response to previous has been received. Mutex 9067a95c21SRishi Gupta * lock is used for this purpose mainly. 9167a95c21SRishi Gupta */ 9267a95c21SRishi Gupta struct mcp2221 { 9367a95c21SRishi Gupta struct hid_device *hdev; 9467a95c21SRishi Gupta struct i2c_adapter adapter; 9567a95c21SRishi Gupta struct mutex lock; 9667a95c21SRishi Gupta struct completion wait_in_report; 97960f9df7SMatt Ranostay struct delayed_work init_work; 9867a95c21SRishi Gupta u8 *rxbuf; 9967a95c21SRishi Gupta u8 txbuf[64]; 10067a95c21SRishi Gupta int rxbuf_idx; 10167a95c21SRishi Gupta int status; 10267a95c21SRishi Gupta u8 cur_i2c_clk_div; 103328de1c5SRishi Gupta struct gpio_chip *gc; 104328de1c5SRishi Gupta u8 gp_idx; 105328de1c5SRishi Gupta u8 gpio_dir; 106960f9df7SMatt Ranostay u8 mode[4]; 107960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 108960f9df7SMatt Ranostay struct iio_chan_spec iio_channels[3]; 109960f9df7SMatt Ranostay u16 adc_values[3]; 110960f9df7SMatt Ranostay u8 adc_scale; 111960f9df7SMatt Ranostay u8 dac_value; 112960f9df7SMatt Ranostay u16 dac_scale; 113960f9df7SMatt Ranostay #endif 114960f9df7SMatt Ranostay }; 115960f9df7SMatt Ranostay 116960f9df7SMatt Ranostay struct mcp2221_iio { 117960f9df7SMatt Ranostay struct mcp2221 *mcp; 11867a95c21SRishi Gupta }; 11967a95c21SRishi Gupta 12067a95c21SRishi Gupta /* 12167a95c21SRishi Gupta * Default i2c bus clock frequency 400 kHz. Modify this if you 12267a95c21SRishi Gupta * want to set some other frequency (min 50 kHz - max 400 kHz). 12367a95c21SRishi Gupta */ 12467a95c21SRishi Gupta static uint i2c_clk_freq = 400; 12567a95c21SRishi Gupta 12667a95c21SRishi Gupta /* Synchronously send output report to the device */ 12767a95c21SRishi Gupta static int mcp_send_report(struct mcp2221 *mcp, 12867a95c21SRishi Gupta u8 *out_report, size_t len) 12967a95c21SRishi Gupta { 13067a95c21SRishi Gupta u8 *buf; 13167a95c21SRishi Gupta int ret; 13267a95c21SRishi Gupta 13367a95c21SRishi Gupta buf = kmemdup(out_report, len, GFP_KERNEL); 13467a95c21SRishi Gupta if (!buf) 13567a95c21SRishi Gupta return -ENOMEM; 13667a95c21SRishi Gupta 13767a95c21SRishi Gupta /* mcp2221 uses interrupt endpoint for out reports */ 13867a95c21SRishi Gupta ret = hid_hw_output_report(mcp->hdev, buf, len); 13967a95c21SRishi Gupta kfree(buf); 14067a95c21SRishi Gupta 14167a95c21SRishi Gupta if (ret < 0) 14267a95c21SRishi Gupta return ret; 14367a95c21SRishi Gupta return 0; 14467a95c21SRishi Gupta } 14567a95c21SRishi Gupta 14667a95c21SRishi Gupta /* 14767a95c21SRishi Gupta * Send o/p report to the device and wait for i/p report to be 14867a95c21SRishi Gupta * received from the device. If the device does not respond, 14967a95c21SRishi Gupta * we timeout. 15067a95c21SRishi Gupta */ 15167a95c21SRishi Gupta static int mcp_send_data_req_status(struct mcp2221 *mcp, 15267a95c21SRishi Gupta u8 *out_report, int len) 15367a95c21SRishi Gupta { 15467a95c21SRishi Gupta int ret; 15567a95c21SRishi Gupta unsigned long t; 15667a95c21SRishi Gupta 15767a95c21SRishi Gupta reinit_completion(&mcp->wait_in_report); 15867a95c21SRishi Gupta 15967a95c21SRishi Gupta ret = mcp_send_report(mcp, out_report, len); 16067a95c21SRishi Gupta if (ret) 16167a95c21SRishi Gupta return ret; 16267a95c21SRishi Gupta 16367a95c21SRishi Gupta t = wait_for_completion_timeout(&mcp->wait_in_report, 16467a95c21SRishi Gupta msecs_to_jiffies(4000)); 16567a95c21SRishi Gupta if (!t) 16667a95c21SRishi Gupta return -ETIMEDOUT; 16767a95c21SRishi Gupta 16867a95c21SRishi Gupta return mcp->status; 16967a95c21SRishi Gupta } 17067a95c21SRishi Gupta 17167a95c21SRishi Gupta /* Check pass/fail for actual communication with i2c slave */ 17267a95c21SRishi Gupta static int mcp_chk_last_cmd_status(struct mcp2221 *mcp) 17367a95c21SRishi Gupta { 17467a95c21SRishi Gupta memset(mcp->txbuf, 0, 8); 17567a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 17667a95c21SRishi Gupta 17767a95c21SRishi Gupta return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 17867a95c21SRishi Gupta } 17967a95c21SRishi Gupta 18067a95c21SRishi Gupta /* Cancels last command releasing i2c bus just in case occupied */ 18167a95c21SRishi Gupta static int mcp_cancel_last_cmd(struct mcp2221 *mcp) 18267a95c21SRishi Gupta { 18367a95c21SRishi Gupta memset(mcp->txbuf, 0, 8); 18467a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 18567a95c21SRishi Gupta mcp->txbuf[2] = MCP2221_I2C_CANCEL; 18667a95c21SRishi Gupta 18767a95c21SRishi Gupta return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 18867a95c21SRishi Gupta } 18967a95c21SRishi Gupta 19067a95c21SRishi Gupta static int mcp_set_i2c_speed(struct mcp2221 *mcp) 19167a95c21SRishi Gupta { 19267a95c21SRishi Gupta int ret; 19367a95c21SRishi Gupta 19467a95c21SRishi Gupta memset(mcp->txbuf, 0, 8); 19567a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 19667a95c21SRishi Gupta mcp->txbuf[3] = MCP2221_I2C_SET_SPEED; 19767a95c21SRishi Gupta mcp->txbuf[4] = mcp->cur_i2c_clk_div; 19867a95c21SRishi Gupta 19967a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8); 20067a95c21SRishi Gupta if (ret) { 20167a95c21SRishi Gupta /* Small delay is needed here */ 20267a95c21SRishi Gupta usleep_range(980, 1000); 20367a95c21SRishi Gupta mcp_cancel_last_cmd(mcp); 20467a95c21SRishi Gupta } 20567a95c21SRishi Gupta 20667a95c21SRishi Gupta return 0; 20767a95c21SRishi Gupta } 20867a95c21SRishi Gupta 20967a95c21SRishi Gupta /* 21067a95c21SRishi Gupta * An output report can contain minimum 1 and maximum 60 user data 21167a95c21SRishi Gupta * bytes. If the number of data bytes is more then 60, we send it 21267a95c21SRishi Gupta * in chunks of 60 bytes. Last chunk may contain exactly 60 or less 21367a95c21SRishi Gupta * bytes. Total number of bytes is informed in very first report to 21467a95c21SRishi Gupta * mcp2221, from that point onwards it first collect all the data 21567a95c21SRishi Gupta * from host and then send to i2c slave device. 21667a95c21SRishi Gupta */ 21767a95c21SRishi Gupta static int mcp_i2c_write(struct mcp2221 *mcp, 21867a95c21SRishi Gupta struct i2c_msg *msg, int type, u8 last_status) 21967a95c21SRishi Gupta { 22067a95c21SRishi Gupta int ret, len, idx, sent; 22167a95c21SRishi Gupta 22267a95c21SRishi Gupta idx = 0; 22367a95c21SRishi Gupta sent = 0; 22467a95c21SRishi Gupta if (msg->len < 60) 22567a95c21SRishi Gupta len = msg->len; 22667a95c21SRishi Gupta else 22767a95c21SRishi Gupta len = 60; 22867a95c21SRishi Gupta 22967a95c21SRishi Gupta do { 23067a95c21SRishi Gupta mcp->txbuf[0] = type; 23167a95c21SRishi Gupta mcp->txbuf[1] = msg->len & 0xff; 23267a95c21SRishi Gupta mcp->txbuf[2] = msg->len >> 8; 23367a95c21SRishi Gupta mcp->txbuf[3] = (u8)(msg->addr << 1); 23467a95c21SRishi Gupta 23567a95c21SRishi Gupta memcpy(&mcp->txbuf[4], &msg->buf[idx], len); 23667a95c21SRishi Gupta 23767a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4); 23867a95c21SRishi Gupta if (ret) 23967a95c21SRishi Gupta return ret; 24067a95c21SRishi Gupta 24167a95c21SRishi Gupta usleep_range(980, 1000); 24267a95c21SRishi Gupta 24367a95c21SRishi Gupta if (last_status) { 24467a95c21SRishi Gupta ret = mcp_chk_last_cmd_status(mcp); 24567a95c21SRishi Gupta if (ret) 24667a95c21SRishi Gupta return ret; 24767a95c21SRishi Gupta } 24867a95c21SRishi Gupta 24967a95c21SRishi Gupta sent = sent + len; 25067a95c21SRishi Gupta if (sent >= msg->len) 25167a95c21SRishi Gupta break; 25267a95c21SRishi Gupta 25367a95c21SRishi Gupta idx = idx + len; 25467a95c21SRishi Gupta if ((msg->len - sent) < 60) 25567a95c21SRishi Gupta len = msg->len - sent; 25667a95c21SRishi Gupta else 25767a95c21SRishi Gupta len = 60; 25867a95c21SRishi Gupta 25967a95c21SRishi Gupta /* 26067a95c21SRishi Gupta * Testing shows delay is needed between successive writes 26167a95c21SRishi Gupta * otherwise next write fails on first-try from i2c core. 26267a95c21SRishi Gupta * This value is obtained through automated stress testing. 26367a95c21SRishi Gupta */ 26467a95c21SRishi Gupta usleep_range(980, 1000); 26567a95c21SRishi Gupta } while (len > 0); 26667a95c21SRishi Gupta 26767a95c21SRishi Gupta return ret; 26867a95c21SRishi Gupta } 26967a95c21SRishi Gupta 27067a95c21SRishi Gupta /* 27167a95c21SRishi Gupta * Device reads all data (0 - 65535 bytes) from i2c slave device and 27267a95c21SRishi Gupta * stores it in device itself. This data is read back from device to 27367a95c21SRishi Gupta * host in multiples of 60 bytes using input reports. 27467a95c21SRishi Gupta */ 27567a95c21SRishi Gupta static int mcp_i2c_smbus_read(struct mcp2221 *mcp, 27667a95c21SRishi Gupta struct i2c_msg *msg, int type, u16 smbus_addr, 27767a95c21SRishi Gupta u8 smbus_len, u8 *smbus_buf) 27867a95c21SRishi Gupta { 27967a95c21SRishi Gupta int ret; 28067a95c21SRishi Gupta u16 total_len; 28167a95c21SRishi Gupta 28267a95c21SRishi Gupta mcp->txbuf[0] = type; 28367a95c21SRishi Gupta if (msg) { 28467a95c21SRishi Gupta mcp->txbuf[1] = msg->len & 0xff; 28567a95c21SRishi Gupta mcp->txbuf[2] = msg->len >> 8; 28667a95c21SRishi Gupta mcp->txbuf[3] = (u8)(msg->addr << 1); 28767a95c21SRishi Gupta total_len = msg->len; 28867a95c21SRishi Gupta mcp->rxbuf = msg->buf; 28967a95c21SRishi Gupta } else { 29067a95c21SRishi Gupta mcp->txbuf[1] = smbus_len; 29167a95c21SRishi Gupta mcp->txbuf[2] = 0; 29267a95c21SRishi Gupta mcp->txbuf[3] = (u8)(smbus_addr << 1); 29367a95c21SRishi Gupta total_len = smbus_len; 29467a95c21SRishi Gupta mcp->rxbuf = smbus_buf; 29567a95c21SRishi Gupta } 29667a95c21SRishi Gupta 29767a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4); 29867a95c21SRishi Gupta if (ret) 29967a95c21SRishi Gupta return ret; 30067a95c21SRishi Gupta 30167a95c21SRishi Gupta mcp->rxbuf_idx = 0; 30267a95c21SRishi Gupta 30367a95c21SRishi Gupta do { 30467a95c21SRishi Gupta memset(mcp->txbuf, 0, 4); 30567a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 30667a95c21SRishi Gupta 30767a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 30867a95c21SRishi Gupta if (ret) 30967a95c21SRishi Gupta return ret; 31067a95c21SRishi Gupta 31167a95c21SRishi Gupta ret = mcp_chk_last_cmd_status(mcp); 31267a95c21SRishi Gupta if (ret) 31367a95c21SRishi Gupta return ret; 31467a95c21SRishi Gupta 31567a95c21SRishi Gupta usleep_range(980, 1000); 31667a95c21SRishi Gupta } while (mcp->rxbuf_idx < total_len); 31767a95c21SRishi Gupta 31867a95c21SRishi Gupta return ret; 31967a95c21SRishi Gupta } 32067a95c21SRishi Gupta 32167a95c21SRishi Gupta static int mcp_i2c_xfer(struct i2c_adapter *adapter, 32267a95c21SRishi Gupta struct i2c_msg msgs[], int num) 32367a95c21SRishi Gupta { 32467a95c21SRishi Gupta int ret; 32567a95c21SRishi Gupta struct mcp2221 *mcp = i2c_get_adapdata(adapter); 32667a95c21SRishi Gupta 32767a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_FULLON); 32867a95c21SRishi Gupta 32967a95c21SRishi Gupta mutex_lock(&mcp->lock); 33067a95c21SRishi Gupta 33167a95c21SRishi Gupta /* Setting speed before every transaction is required for mcp2221 */ 33267a95c21SRishi Gupta ret = mcp_set_i2c_speed(mcp); 33367a95c21SRishi Gupta if (ret) 33467a95c21SRishi Gupta goto exit; 33567a95c21SRishi Gupta 33667a95c21SRishi Gupta if (num == 1) { 33767a95c21SRishi Gupta if (msgs->flags & I2C_M_RD) { 33867a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA, 33967a95c21SRishi Gupta 0, 0, NULL); 34067a95c21SRishi Gupta } else { 34167a95c21SRishi Gupta ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1); 34267a95c21SRishi Gupta } 34367a95c21SRishi Gupta if (ret) 34467a95c21SRishi Gupta goto exit; 34567a95c21SRishi Gupta ret = num; 34667a95c21SRishi Gupta } else if (num == 2) { 34767a95c21SRishi Gupta /* Ex transaction; send reg address and read its contents */ 34867a95c21SRishi Gupta if (msgs[0].addr == msgs[1].addr && 34967a95c21SRishi Gupta !(msgs[0].flags & I2C_M_RD) && 35067a95c21SRishi Gupta (msgs[1].flags & I2C_M_RD)) { 35167a95c21SRishi Gupta 35267a95c21SRishi Gupta ret = mcp_i2c_write(mcp, &msgs[0], 35367a95c21SRishi Gupta MCP2221_I2C_WR_NO_STOP, 0); 35467a95c21SRishi Gupta if (ret) 35567a95c21SRishi Gupta goto exit; 35667a95c21SRishi Gupta 35767a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, &msgs[1], 35867a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 35967a95c21SRishi Gupta 0, 0, NULL); 36067a95c21SRishi Gupta if (ret) 36167a95c21SRishi Gupta goto exit; 36267a95c21SRishi Gupta ret = num; 36367a95c21SRishi Gupta } else { 36467a95c21SRishi Gupta dev_err(&adapter->dev, 36567a95c21SRishi Gupta "unsupported multi-msg i2c transaction\n"); 36667a95c21SRishi Gupta ret = -EOPNOTSUPP; 36767a95c21SRishi Gupta } 36867a95c21SRishi Gupta } else { 36967a95c21SRishi Gupta dev_err(&adapter->dev, 37067a95c21SRishi Gupta "unsupported multi-msg i2c transaction\n"); 37167a95c21SRishi Gupta ret = -EOPNOTSUPP; 37267a95c21SRishi Gupta } 37367a95c21SRishi Gupta 37467a95c21SRishi Gupta exit: 37567a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 37667a95c21SRishi Gupta mutex_unlock(&mcp->lock); 37767a95c21SRishi Gupta return ret; 37867a95c21SRishi Gupta } 37967a95c21SRishi Gupta 38067a95c21SRishi Gupta static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr, 38167a95c21SRishi Gupta u8 command, u8 *buf, u8 len, int type, 38267a95c21SRishi Gupta u8 last_status) 38367a95c21SRishi Gupta { 38467a95c21SRishi Gupta int data_len, ret; 38567a95c21SRishi Gupta 38667a95c21SRishi Gupta mcp->txbuf[0] = type; 38767a95c21SRishi Gupta mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */ 38867a95c21SRishi Gupta mcp->txbuf[2] = 0; 38967a95c21SRishi Gupta mcp->txbuf[3] = (u8)(addr << 1); 39067a95c21SRishi Gupta mcp->txbuf[4] = command; 39167a95c21SRishi Gupta 39267a95c21SRishi Gupta switch (len) { 39367a95c21SRishi Gupta case 0: 39467a95c21SRishi Gupta data_len = 5; 39567a95c21SRishi Gupta break; 39667a95c21SRishi Gupta case 1: 39767a95c21SRishi Gupta mcp->txbuf[5] = buf[0]; 39867a95c21SRishi Gupta data_len = 6; 39967a95c21SRishi Gupta break; 40067a95c21SRishi Gupta case 2: 40167a95c21SRishi Gupta mcp->txbuf[5] = buf[0]; 40267a95c21SRishi Gupta mcp->txbuf[6] = buf[1]; 40367a95c21SRishi Gupta data_len = 7; 40467a95c21SRishi Gupta break; 40567a95c21SRishi Gupta default: 40662ac2473SHarshit Mogalapalli if (len > I2C_SMBUS_BLOCK_MAX) 40762ac2473SHarshit Mogalapalli return -EINVAL; 40862ac2473SHarshit Mogalapalli 40967a95c21SRishi Gupta memcpy(&mcp->txbuf[5], buf, len); 41067a95c21SRishi Gupta data_len = len + 5; 41167a95c21SRishi Gupta } 41267a95c21SRishi Gupta 41367a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len); 41467a95c21SRishi Gupta if (ret) 41567a95c21SRishi Gupta return ret; 41667a95c21SRishi Gupta 41767a95c21SRishi Gupta if (last_status) { 41867a95c21SRishi Gupta usleep_range(980, 1000); 41967a95c21SRishi Gupta 42067a95c21SRishi Gupta ret = mcp_chk_last_cmd_status(mcp); 42167a95c21SRishi Gupta if (ret) 42267a95c21SRishi Gupta return ret; 42367a95c21SRishi Gupta } 42467a95c21SRishi Gupta 42567a95c21SRishi Gupta return ret; 42667a95c21SRishi Gupta } 42767a95c21SRishi Gupta 42867a95c21SRishi Gupta static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 42967a95c21SRishi Gupta unsigned short flags, char read_write, 43067a95c21SRishi Gupta u8 command, int size, 43167a95c21SRishi Gupta union i2c_smbus_data *data) 43267a95c21SRishi Gupta { 43367a95c21SRishi Gupta int ret; 43467a95c21SRishi Gupta struct mcp2221 *mcp = i2c_get_adapdata(adapter); 43567a95c21SRishi Gupta 43667a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_FULLON); 43767a95c21SRishi Gupta 43867a95c21SRishi Gupta mutex_lock(&mcp->lock); 43967a95c21SRishi Gupta 44067a95c21SRishi Gupta ret = mcp_set_i2c_speed(mcp); 44167a95c21SRishi Gupta if (ret) 44267a95c21SRishi Gupta goto exit; 44367a95c21SRishi Gupta 44467a95c21SRishi Gupta switch (size) { 44567a95c21SRishi Gupta 44667a95c21SRishi Gupta case I2C_SMBUS_QUICK: 44767a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) 44867a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 44967a95c21SRishi Gupta addr, 0, &data->byte); 45067a95c21SRishi Gupta else 45167a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 45267a95c21SRishi Gupta 0, MCP2221_I2C_WR_DATA, 1); 45367a95c21SRishi Gupta break; 45467a95c21SRishi Gupta case I2C_SMBUS_BYTE: 45567a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) 45667a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 45767a95c21SRishi Gupta addr, 1, &data->byte); 45867a95c21SRishi Gupta else 45967a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 46067a95c21SRishi Gupta 0, MCP2221_I2C_WR_DATA, 1); 46167a95c21SRishi Gupta break; 46267a95c21SRishi Gupta case I2C_SMBUS_BYTE_DATA: 46367a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 46467a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 46567a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 0); 46667a95c21SRishi Gupta if (ret) 46767a95c21SRishi Gupta goto exit; 46867a95c21SRishi Gupta 46967a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 47067a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 47167a95c21SRishi Gupta addr, 1, &data->byte); 47267a95c21SRishi Gupta } else { 47367a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, &data->byte, 47467a95c21SRishi Gupta 1, MCP2221_I2C_WR_DATA, 1); 47567a95c21SRishi Gupta } 47667a95c21SRishi Gupta break; 47767a95c21SRishi Gupta case I2C_SMBUS_WORD_DATA: 47867a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 47967a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 48067a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 0); 48167a95c21SRishi Gupta if (ret) 48267a95c21SRishi Gupta goto exit; 48367a95c21SRishi Gupta 48467a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 48567a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 48667a95c21SRishi Gupta addr, 2, (u8 *)&data->word); 48767a95c21SRishi Gupta } else { 48867a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, 48967a95c21SRishi Gupta (u8 *)&data->word, 2, 49067a95c21SRishi Gupta MCP2221_I2C_WR_DATA, 1); 49167a95c21SRishi Gupta } 49267a95c21SRishi Gupta break; 49367a95c21SRishi Gupta case I2C_SMBUS_BLOCK_DATA: 49467a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 49567a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 49667a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 1); 49767a95c21SRishi Gupta if (ret) 49867a95c21SRishi Gupta goto exit; 49967a95c21SRishi Gupta 50067a95c21SRishi Gupta mcp->rxbuf_idx = 0; 50167a95c21SRishi Gupta mcp->rxbuf = data->block; 50267a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 50367a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 50467a95c21SRishi Gupta if (ret) 50567a95c21SRishi Gupta goto exit; 50667a95c21SRishi Gupta } else { 50767a95c21SRishi Gupta if (!data->block[0]) { 50867a95c21SRishi Gupta ret = -EINVAL; 50967a95c21SRishi Gupta goto exit; 51067a95c21SRishi Gupta } 51167a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, data->block, 51267a95c21SRishi Gupta data->block[0] + 1, 51367a95c21SRishi Gupta MCP2221_I2C_WR_DATA, 1); 51467a95c21SRishi Gupta } 51567a95c21SRishi Gupta break; 51667a95c21SRishi Gupta case I2C_SMBUS_I2C_BLOCK_DATA: 51767a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 51867a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 51967a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 1); 52067a95c21SRishi Gupta if (ret) 52167a95c21SRishi Gupta goto exit; 52267a95c21SRishi Gupta 52367a95c21SRishi Gupta mcp->rxbuf_idx = 0; 52467a95c21SRishi Gupta mcp->rxbuf = data->block; 52567a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 52667a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 52767a95c21SRishi Gupta if (ret) 52867a95c21SRishi Gupta goto exit; 52967a95c21SRishi Gupta } else { 53067a95c21SRishi Gupta if (!data->block[0]) { 53167a95c21SRishi Gupta ret = -EINVAL; 53267a95c21SRishi Gupta goto exit; 53367a95c21SRishi Gupta } 53467a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, 53567a95c21SRishi Gupta &data->block[1], data->block[0], 53667a95c21SRishi Gupta MCP2221_I2C_WR_DATA, 1); 53767a95c21SRishi Gupta } 53867a95c21SRishi Gupta break; 53967a95c21SRishi Gupta case I2C_SMBUS_PROC_CALL: 54067a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, 54167a95c21SRishi Gupta (u8 *)&data->word, 54267a95c21SRishi Gupta 2, MCP2221_I2C_WR_NO_STOP, 0); 54367a95c21SRishi Gupta if (ret) 54467a95c21SRishi Gupta goto exit; 54567a95c21SRishi Gupta 54667a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 54767a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 54867a95c21SRishi Gupta addr, 2, (u8 *)&data->word); 54967a95c21SRishi Gupta break; 55067a95c21SRishi Gupta case I2C_SMBUS_BLOCK_PROC_CALL: 55167a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, data->block, 55267a95c21SRishi Gupta data->block[0] + 1, 55367a95c21SRishi Gupta MCP2221_I2C_WR_NO_STOP, 0); 55467a95c21SRishi Gupta if (ret) 55567a95c21SRishi Gupta goto exit; 55667a95c21SRishi Gupta 55767a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 55867a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 55967a95c21SRishi Gupta addr, I2C_SMBUS_BLOCK_MAX, 56067a95c21SRishi Gupta data->block); 56167a95c21SRishi Gupta break; 56267a95c21SRishi Gupta default: 56367a95c21SRishi Gupta dev_err(&mcp->adapter.dev, 56467a95c21SRishi Gupta "unsupported smbus transaction size:%d\n", size); 56567a95c21SRishi Gupta ret = -EOPNOTSUPP; 56667a95c21SRishi Gupta } 56767a95c21SRishi Gupta 56867a95c21SRishi Gupta exit: 56967a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 57067a95c21SRishi Gupta mutex_unlock(&mcp->lock); 57167a95c21SRishi Gupta return ret; 57267a95c21SRishi Gupta } 57367a95c21SRishi Gupta 57467a95c21SRishi Gupta static u32 mcp_i2c_func(struct i2c_adapter *adapter) 57567a95c21SRishi Gupta { 57667a95c21SRishi Gupta return I2C_FUNC_I2C | 57767a95c21SRishi Gupta I2C_FUNC_SMBUS_READ_BLOCK_DATA | 57867a95c21SRishi Gupta I2C_FUNC_SMBUS_BLOCK_PROC_CALL | 57967a95c21SRishi Gupta (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC); 58067a95c21SRishi Gupta } 58167a95c21SRishi Gupta 58267a95c21SRishi Gupta static const struct i2c_algorithm mcp_i2c_algo = { 58367a95c21SRishi Gupta .master_xfer = mcp_i2c_xfer, 58467a95c21SRishi Gupta .smbus_xfer = mcp_smbus_xfer, 58567a95c21SRishi Gupta .functionality = mcp_i2c_func, 58667a95c21SRishi Gupta }; 58767a95c21SRishi Gupta 5883d74c9ecSMatt Ranostay #if IS_REACHABLE(CONFIG_GPIOLIB) 589328de1c5SRishi Gupta static int mcp_gpio_get(struct gpio_chip *gc, 590328de1c5SRishi Gupta unsigned int offset) 591328de1c5SRishi Gupta { 592328de1c5SRishi Gupta int ret; 593328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 594328de1c5SRishi Gupta 595328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_GET; 596328de1c5SRishi Gupta 597*ca6961d8SLouis Morhet mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]); 598328de1c5SRishi Gupta 599328de1c5SRishi Gupta mutex_lock(&mcp->lock); 600328de1c5SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 601328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 602328de1c5SRishi Gupta 603328de1c5SRishi Gupta return ret; 604328de1c5SRishi Gupta } 605328de1c5SRishi Gupta 606328de1c5SRishi Gupta static void mcp_gpio_set(struct gpio_chip *gc, 607328de1c5SRishi Gupta unsigned int offset, int value) 608328de1c5SRishi Gupta { 609328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 610328de1c5SRishi Gupta 611328de1c5SRishi Gupta memset(mcp->txbuf, 0, 18); 612328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_SET; 613328de1c5SRishi Gupta 614567b8e9fSLars Povlsen mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value); 615328de1c5SRishi Gupta 616328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx - 1] = 1; 617328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx] = !!value; 618328de1c5SRishi Gupta 619328de1c5SRishi Gupta mutex_lock(&mcp->lock); 620328de1c5SRishi Gupta mcp_send_data_req_status(mcp, mcp->txbuf, 18); 621328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 622328de1c5SRishi Gupta } 623328de1c5SRishi Gupta 624328de1c5SRishi Gupta static int mcp_gpio_dir_set(struct mcp2221 *mcp, 625328de1c5SRishi Gupta unsigned int offset, u8 val) 626328de1c5SRishi Gupta { 627328de1c5SRishi Gupta memset(mcp->txbuf, 0, 18); 628328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_SET; 629328de1c5SRishi Gupta 630567b8e9fSLars Povlsen mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction); 631328de1c5SRishi Gupta 632328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx - 1] = 1; 633328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx] = val; 634328de1c5SRishi Gupta 635328de1c5SRishi Gupta return mcp_send_data_req_status(mcp, mcp->txbuf, 18); 636328de1c5SRishi Gupta } 637328de1c5SRishi Gupta 638328de1c5SRishi Gupta static int mcp_gpio_direction_input(struct gpio_chip *gc, 639328de1c5SRishi Gupta unsigned int offset) 640328de1c5SRishi Gupta { 641328de1c5SRishi Gupta int ret; 642328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 643328de1c5SRishi Gupta 644328de1c5SRishi Gupta mutex_lock(&mcp->lock); 645567b8e9fSLars Povlsen ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN); 646328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 647328de1c5SRishi Gupta 648328de1c5SRishi Gupta return ret; 649328de1c5SRishi Gupta } 650328de1c5SRishi Gupta 651328de1c5SRishi Gupta static int mcp_gpio_direction_output(struct gpio_chip *gc, 652328de1c5SRishi Gupta unsigned int offset, int value) 653328de1c5SRishi Gupta { 654328de1c5SRishi Gupta int ret; 655328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 656328de1c5SRishi Gupta 657328de1c5SRishi Gupta mutex_lock(&mcp->lock); 658567b8e9fSLars Povlsen ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT); 659328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 660328de1c5SRishi Gupta 661328de1c5SRishi Gupta /* Can't configure as output, bailout early */ 662328de1c5SRishi Gupta if (ret) 663328de1c5SRishi Gupta return ret; 664328de1c5SRishi Gupta 665328de1c5SRishi Gupta mcp_gpio_set(gc, offset, value); 666328de1c5SRishi Gupta 667328de1c5SRishi Gupta return 0; 668328de1c5SRishi Gupta } 669328de1c5SRishi Gupta 670328de1c5SRishi Gupta static int mcp_gpio_get_direction(struct gpio_chip *gc, 671328de1c5SRishi Gupta unsigned int offset) 672328de1c5SRishi Gupta { 673328de1c5SRishi Gupta int ret; 674328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 675328de1c5SRishi Gupta 676328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_GET; 677328de1c5SRishi Gupta 678*ca6961d8SLouis Morhet mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]); 679328de1c5SRishi Gupta 680328de1c5SRishi Gupta mutex_lock(&mcp->lock); 681328de1c5SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 682328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 683328de1c5SRishi Gupta 684328de1c5SRishi Gupta if (ret) 685328de1c5SRishi Gupta return ret; 686328de1c5SRishi Gupta 687567b8e9fSLars Povlsen if (mcp->gpio_dir == MCP2221_DIR_IN) 688328de1c5SRishi Gupta return GPIO_LINE_DIRECTION_IN; 689328de1c5SRishi Gupta 690328de1c5SRishi Gupta return GPIO_LINE_DIRECTION_OUT; 691328de1c5SRishi Gupta } 6923d74c9ecSMatt Ranostay #endif 693328de1c5SRishi Gupta 69467a95c21SRishi Gupta /* Gives current state of i2c engine inside mcp2221 */ 69567a95c21SRishi Gupta static int mcp_get_i2c_eng_state(struct mcp2221 *mcp, 69667a95c21SRishi Gupta u8 *data, u8 idx) 69767a95c21SRishi Gupta { 69867a95c21SRishi Gupta int ret; 69967a95c21SRishi Gupta 70067a95c21SRishi Gupta switch (data[idx]) { 70167a95c21SRishi Gupta case MCP2221_I2C_WRADDRL_NACK: 70267a95c21SRishi Gupta case MCP2221_I2C_WRADDRL_SEND: 70367a95c21SRishi Gupta ret = -ENXIO; 70467a95c21SRishi Gupta break; 70567a95c21SRishi Gupta case MCP2221_I2C_START_TOUT: 70667a95c21SRishi Gupta case MCP2221_I2C_STOP_TOUT: 70767a95c21SRishi Gupta case MCP2221_I2C_WRADDRL_TOUT: 70867a95c21SRishi Gupta case MCP2221_I2C_WRDATA_TOUT: 70967a95c21SRishi Gupta ret = -ETIMEDOUT; 71067a95c21SRishi Gupta break; 71167a95c21SRishi Gupta case MCP2221_I2C_ENG_BUSY: 71267a95c21SRishi Gupta ret = -EAGAIN; 71367a95c21SRishi Gupta break; 71467a95c21SRishi Gupta case MCP2221_SUCCESS: 71567a95c21SRishi Gupta ret = 0x00; 71667a95c21SRishi Gupta break; 71767a95c21SRishi Gupta default: 71867a95c21SRishi Gupta ret = -EIO; 71967a95c21SRishi Gupta } 72067a95c21SRishi Gupta 72167a95c21SRishi Gupta return ret; 72267a95c21SRishi Gupta } 72367a95c21SRishi Gupta 72467a95c21SRishi Gupta /* 72567a95c21SRishi Gupta * MCP2221 uses interrupt endpoint for input reports. This function 72667a95c21SRishi Gupta * is called by HID layer when it receives i/p report from mcp2221, 72767a95c21SRishi Gupta * which is actually a response to the previously sent command. 72867a95c21SRishi Gupta * 72967a95c21SRishi Gupta * MCP2221A firmware specific return codes are parsed and 0 or 73067a95c21SRishi Gupta * appropriate negative error code is returned. Delayed response 73167a95c21SRishi Gupta * results in timeout error and stray reponses results in -EIO. 73267a95c21SRishi Gupta */ 73367a95c21SRishi Gupta static int mcp2221_raw_event(struct hid_device *hdev, 73467a95c21SRishi Gupta struct hid_report *report, u8 *data, int size) 73567a95c21SRishi Gupta { 736daf405c8SJiri Kosina u8 *buf; 73767a95c21SRishi Gupta struct mcp2221 *mcp = hid_get_drvdata(hdev); 73867a95c21SRishi Gupta 73967a95c21SRishi Gupta switch (data[0]) { 74067a95c21SRishi Gupta 74167a95c21SRishi Gupta case MCP2221_I2C_WR_DATA: 74267a95c21SRishi Gupta case MCP2221_I2C_WR_NO_STOP: 74367a95c21SRishi Gupta case MCP2221_I2C_RD_DATA: 74467a95c21SRishi Gupta case MCP2221_I2C_RD_RPT_START: 74567a95c21SRishi Gupta switch (data[1]) { 74667a95c21SRishi Gupta case MCP2221_SUCCESS: 74767a95c21SRishi Gupta mcp->status = 0; 74867a95c21SRishi Gupta break; 74967a95c21SRishi Gupta default: 75067a95c21SRishi Gupta mcp->status = mcp_get_i2c_eng_state(mcp, data, 2); 75167a95c21SRishi Gupta } 75267a95c21SRishi Gupta complete(&mcp->wait_in_report); 75367a95c21SRishi Gupta break; 75467a95c21SRishi Gupta 75567a95c21SRishi Gupta case MCP2221_I2C_PARAM_OR_STATUS: 75667a95c21SRishi Gupta switch (data[1]) { 75767a95c21SRishi Gupta case MCP2221_SUCCESS: 75867a95c21SRishi Gupta if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) && 75967a95c21SRishi Gupta (data[3] != MCP2221_I2C_SET_SPEED)) { 76067a95c21SRishi Gupta mcp->status = -EAGAIN; 76167a95c21SRishi Gupta break; 76267a95c21SRishi Gupta } 76367a95c21SRishi Gupta if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) { 76467a95c21SRishi Gupta mcp->status = -ENXIO; 76567a95c21SRishi Gupta break; 76667a95c21SRishi Gupta } 76767a95c21SRishi Gupta mcp->status = mcp_get_i2c_eng_state(mcp, data, 8); 768960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 769960f9df7SMatt Ranostay memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values)); 770960f9df7SMatt Ranostay #endif 77167a95c21SRishi Gupta break; 77267a95c21SRishi Gupta default: 77367a95c21SRishi Gupta mcp->status = -EIO; 77467a95c21SRishi Gupta } 77567a95c21SRishi Gupta complete(&mcp->wait_in_report); 77667a95c21SRishi Gupta break; 77767a95c21SRishi Gupta 77867a95c21SRishi Gupta case MCP2221_I2C_GET_DATA: 77967a95c21SRishi Gupta switch (data[1]) { 78067a95c21SRishi Gupta case MCP2221_SUCCESS: 78167a95c21SRishi Gupta if (data[2] == MCP2221_I2C_ADDR_NACK) { 78267a95c21SRishi Gupta mcp->status = -ENXIO; 78367a95c21SRishi Gupta break; 78467a95c21SRishi Gupta } 78567a95c21SRishi Gupta if (!mcp_get_i2c_eng_state(mcp, data, 2) 78667a95c21SRishi Gupta && (data[3] == 0)) { 78767a95c21SRishi Gupta mcp->status = 0; 78867a95c21SRishi Gupta break; 78967a95c21SRishi Gupta } 79067a95c21SRishi Gupta if (data[3] == 127) { 79167a95c21SRishi Gupta mcp->status = -EIO; 79267a95c21SRishi Gupta break; 79367a95c21SRishi Gupta } 79467a95c21SRishi Gupta if (data[2] == MCP2221_I2C_READ_COMPL) { 79567a95c21SRishi Gupta buf = mcp->rxbuf; 79667a95c21SRishi Gupta memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]); 79767a95c21SRishi Gupta mcp->rxbuf_idx = mcp->rxbuf_idx + data[3]; 79867a95c21SRishi Gupta mcp->status = 0; 79967a95c21SRishi Gupta break; 80067a95c21SRishi Gupta } 80167a95c21SRishi Gupta mcp->status = -EIO; 80267a95c21SRishi Gupta break; 80367a95c21SRishi Gupta default: 80467a95c21SRishi Gupta mcp->status = -EIO; 80567a95c21SRishi Gupta } 80667a95c21SRishi Gupta complete(&mcp->wait_in_report); 80767a95c21SRishi Gupta break; 80867a95c21SRishi Gupta 809328de1c5SRishi Gupta case MCP2221_GPIO_GET: 810328de1c5SRishi Gupta switch (data[1]) { 811328de1c5SRishi Gupta case MCP2221_SUCCESS: 812328de1c5SRishi Gupta if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) || 813328de1c5SRishi Gupta (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) { 814328de1c5SRishi Gupta mcp->status = -ENOENT; 815328de1c5SRishi Gupta } else { 816328de1c5SRishi Gupta mcp->status = !!data[mcp->gp_idx]; 817567b8e9fSLars Povlsen mcp->gpio_dir = data[mcp->gp_idx + 1]; 818328de1c5SRishi Gupta } 819328de1c5SRishi Gupta break; 820328de1c5SRishi Gupta default: 821328de1c5SRishi Gupta mcp->status = -EAGAIN; 822328de1c5SRishi Gupta } 823328de1c5SRishi Gupta complete(&mcp->wait_in_report); 824328de1c5SRishi Gupta break; 825328de1c5SRishi Gupta 826328de1c5SRishi Gupta case MCP2221_GPIO_SET: 827328de1c5SRishi Gupta switch (data[1]) { 828328de1c5SRishi Gupta case MCP2221_SUCCESS: 829328de1c5SRishi Gupta if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) || 830328de1c5SRishi Gupta (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) { 831328de1c5SRishi Gupta mcp->status = -ENOENT; 832328de1c5SRishi Gupta } else { 833328de1c5SRishi Gupta mcp->status = 0; 834328de1c5SRishi Gupta } 835328de1c5SRishi Gupta break; 836328de1c5SRishi Gupta default: 837328de1c5SRishi Gupta mcp->status = -EAGAIN; 838328de1c5SRishi Gupta } 839328de1c5SRishi Gupta complete(&mcp->wait_in_report); 840328de1c5SRishi Gupta break; 841328de1c5SRishi Gupta 842960f9df7SMatt Ranostay case MCP2221_SET_SRAM_SETTINGS: 843960f9df7SMatt Ranostay switch (data[1]) { 844960f9df7SMatt Ranostay case MCP2221_SUCCESS: 845960f9df7SMatt Ranostay mcp->status = 0; 846960f9df7SMatt Ranostay break; 847960f9df7SMatt Ranostay default: 848960f9df7SMatt Ranostay mcp->status = -EAGAIN; 849960f9df7SMatt Ranostay } 850960f9df7SMatt Ranostay complete(&mcp->wait_in_report); 851960f9df7SMatt Ranostay break; 852960f9df7SMatt Ranostay 853960f9df7SMatt Ranostay case MCP2221_GET_SRAM_SETTINGS: 854960f9df7SMatt Ranostay switch (data[1]) { 855960f9df7SMatt Ranostay case MCP2221_SUCCESS: 856960f9df7SMatt Ranostay memcpy(&mcp->mode, &data[22], 4); 857960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 858960f9df7SMatt Ranostay mcp->dac_value = data[6] & GENMASK(4, 0); 859960f9df7SMatt Ranostay #endif 860960f9df7SMatt Ranostay mcp->status = 0; 861960f9df7SMatt Ranostay break; 862960f9df7SMatt Ranostay default: 863960f9df7SMatt Ranostay mcp->status = -EAGAIN; 864960f9df7SMatt Ranostay } 865960f9df7SMatt Ranostay complete(&mcp->wait_in_report); 866960f9df7SMatt Ranostay break; 867960f9df7SMatt Ranostay 868960f9df7SMatt Ranostay case MCP2221_READ_FLASH_DATA: 869960f9df7SMatt Ranostay switch (data[1]) { 870960f9df7SMatt Ranostay case MCP2221_SUCCESS: 871960f9df7SMatt Ranostay mcp->status = 0; 872960f9df7SMatt Ranostay 873960f9df7SMatt Ranostay /* Only handles CHIP SETTINGS subpage currently */ 874960f9df7SMatt Ranostay if (mcp->txbuf[1] != 0) { 875960f9df7SMatt Ranostay mcp->status = -EIO; 876960f9df7SMatt Ranostay break; 877960f9df7SMatt Ranostay } 878960f9df7SMatt Ranostay 879960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 880daf405c8SJiri Kosina { 881daf405c8SJiri Kosina u8 tmp; 882960f9df7SMatt Ranostay /* DAC scale value */ 883960f9df7SMatt Ranostay tmp = FIELD_GET(GENMASK(7, 6), data[6]); 884960f9df7SMatt Ranostay if ((data[6] & BIT(5)) && tmp) 885960f9df7SMatt Ranostay mcp->dac_scale = tmp + 4; 886960f9df7SMatt Ranostay else 887960f9df7SMatt Ranostay mcp->dac_scale = 5; 888960f9df7SMatt Ranostay 889960f9df7SMatt Ranostay /* ADC scale value */ 890960f9df7SMatt Ranostay tmp = FIELD_GET(GENMASK(4, 3), data[7]); 891960f9df7SMatt Ranostay if ((data[7] & BIT(2)) && tmp) 892960f9df7SMatt Ranostay mcp->adc_scale = tmp - 1; 893960f9df7SMatt Ranostay else 894960f9df7SMatt Ranostay mcp->adc_scale = 0; 895daf405c8SJiri Kosina } 896960f9df7SMatt Ranostay #endif 897960f9df7SMatt Ranostay 898960f9df7SMatt Ranostay break; 899960f9df7SMatt Ranostay default: 900960f9df7SMatt Ranostay mcp->status = -EAGAIN; 901960f9df7SMatt Ranostay } 902960f9df7SMatt Ranostay complete(&mcp->wait_in_report); 903960f9df7SMatt Ranostay break; 904960f9df7SMatt Ranostay 90567a95c21SRishi Gupta default: 90667a95c21SRishi Gupta mcp->status = -EIO; 90767a95c21SRishi Gupta complete(&mcp->wait_in_report); 90867a95c21SRishi Gupta } 90967a95c21SRishi Gupta 91067a95c21SRishi Gupta return 1; 91167a95c21SRishi Gupta } 91267a95c21SRishi Gupta 913deb3b88bSMatt Ranostay /* Device resource managed function for HID unregistration */ 914deb3b88bSMatt Ranostay static void mcp2221_hid_unregister(void *ptr) 915deb3b88bSMatt Ranostay { 916deb3b88bSMatt Ranostay struct hid_device *hdev = ptr; 917deb3b88bSMatt Ranostay 918deb3b88bSMatt Ranostay hid_hw_close(hdev); 919deb3b88bSMatt Ranostay hid_hw_stop(hdev); 920deb3b88bSMatt Ranostay } 921deb3b88bSMatt Ranostay 922deb3b88bSMatt Ranostay /* This is needed to be sure hid_hw_stop() isn't called twice by the subsystem */ 923deb3b88bSMatt Ranostay static void mcp2221_remove(struct hid_device *hdev) 924deb3b88bSMatt Ranostay { 92547e91fdfSBenjamin Tissoires struct mcp2221 *mcp = hid_get_drvdata(hdev); 92647e91fdfSBenjamin Tissoires 92747e91fdfSBenjamin Tissoires cancel_delayed_work_sync(&mcp->init_work); 928deb3b88bSMatt Ranostay } 929deb3b88bSMatt Ranostay 930960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 931960f9df7SMatt Ranostay static int mcp2221_read_raw(struct iio_dev *indio_dev, 932960f9df7SMatt Ranostay struct iio_chan_spec const *channel, int *val, 933960f9df7SMatt Ranostay int *val2, long mask) 934960f9df7SMatt Ranostay { 935960f9df7SMatt Ranostay struct mcp2221_iio *priv = iio_priv(indio_dev); 936960f9df7SMatt Ranostay struct mcp2221 *mcp = priv->mcp; 937960f9df7SMatt Ranostay int ret; 938960f9df7SMatt Ranostay 939960f9df7SMatt Ranostay if (mask == IIO_CHAN_INFO_SCALE) { 940960f9df7SMatt Ranostay if (channel->output) 941960f9df7SMatt Ranostay *val = 1 << mcp->dac_scale; 942960f9df7SMatt Ranostay else 943960f9df7SMatt Ranostay *val = 1 << mcp->adc_scale; 944960f9df7SMatt Ranostay 945960f9df7SMatt Ranostay return IIO_VAL_INT; 946960f9df7SMatt Ranostay } 947960f9df7SMatt Ranostay 948960f9df7SMatt Ranostay mutex_lock(&mcp->lock); 949960f9df7SMatt Ranostay 950960f9df7SMatt Ranostay if (channel->output) { 951960f9df7SMatt Ranostay *val = mcp->dac_value; 952960f9df7SMatt Ranostay ret = IIO_VAL_INT; 953960f9df7SMatt Ranostay } else { 954960f9df7SMatt Ranostay /* Read ADC values */ 955960f9df7SMatt Ranostay ret = mcp_chk_last_cmd_status(mcp); 956960f9df7SMatt Ranostay 957960f9df7SMatt Ranostay if (!ret) { 958e91fc483SMatt Ranostay *val = le16_to_cpu((__force __le16) mcp->adc_values[channel->address]); 959960f9df7SMatt Ranostay if (*val >= BIT(10)) 960960f9df7SMatt Ranostay ret = -EINVAL; 961960f9df7SMatt Ranostay else 962960f9df7SMatt Ranostay ret = IIO_VAL_INT; 963960f9df7SMatt Ranostay } 964960f9df7SMatt Ranostay } 965960f9df7SMatt Ranostay 966960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 967960f9df7SMatt Ranostay 968960f9df7SMatt Ranostay return ret; 969960f9df7SMatt Ranostay } 970960f9df7SMatt Ranostay 971960f9df7SMatt Ranostay static int mcp2221_write_raw(struct iio_dev *indio_dev, 972960f9df7SMatt Ranostay struct iio_chan_spec const *chan, 973960f9df7SMatt Ranostay int val, int val2, long mask) 974960f9df7SMatt Ranostay { 975960f9df7SMatt Ranostay struct mcp2221_iio *priv = iio_priv(indio_dev); 976960f9df7SMatt Ranostay struct mcp2221 *mcp = priv->mcp; 977960f9df7SMatt Ranostay int ret; 978960f9df7SMatt Ranostay 979960f9df7SMatt Ranostay if (val < 0 || val >= BIT(5)) 980960f9df7SMatt Ranostay return -EINVAL; 981960f9df7SMatt Ranostay 982960f9df7SMatt Ranostay mutex_lock(&mcp->lock); 983960f9df7SMatt Ranostay 984960f9df7SMatt Ranostay memset(mcp->txbuf, 0, 12); 985960f9df7SMatt Ranostay mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS; 986960f9df7SMatt Ranostay mcp->txbuf[4] = BIT(7) | val; 987960f9df7SMatt Ranostay 988960f9df7SMatt Ranostay ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12); 989960f9df7SMatt Ranostay if (!ret) 990960f9df7SMatt Ranostay mcp->dac_value = val; 991960f9df7SMatt Ranostay 992960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 993960f9df7SMatt Ranostay 994960f9df7SMatt Ranostay return ret; 995960f9df7SMatt Ranostay } 996960f9df7SMatt Ranostay 997960f9df7SMatt Ranostay static const struct iio_info mcp2221_info = { 998960f9df7SMatt Ranostay .read_raw = &mcp2221_read_raw, 999960f9df7SMatt Ranostay .write_raw = &mcp2221_write_raw, 1000960f9df7SMatt Ranostay }; 1001960f9df7SMatt Ranostay 1002960f9df7SMatt Ranostay static int mcp_iio_channels(struct mcp2221 *mcp) 1003960f9df7SMatt Ranostay { 1004960f9df7SMatt Ranostay int idx, cnt = 0; 1005960f9df7SMatt Ranostay bool dac_created = false; 1006960f9df7SMatt Ranostay 1007960f9df7SMatt Ranostay /* GP0 doesn't have ADC/DAC alternative function */ 1008960f9df7SMatt Ranostay for (idx = 1; idx < MCP_NGPIO; idx++) { 1009960f9df7SMatt Ranostay struct iio_chan_spec *chan = &mcp->iio_channels[cnt]; 1010960f9df7SMatt Ranostay 1011960f9df7SMatt Ranostay switch (mcp->mode[idx]) { 1012960f9df7SMatt Ranostay case 2: 1013960f9df7SMatt Ranostay chan->address = idx - 1; 1014960f9df7SMatt Ranostay chan->channel = cnt++; 1015960f9df7SMatt Ranostay break; 1016960f9df7SMatt Ranostay case 3: 1017960f9df7SMatt Ranostay /* GP1 doesn't have DAC alternative function */ 1018960f9df7SMatt Ranostay if (idx == 1 || dac_created) 1019960f9df7SMatt Ranostay continue; 1020960f9df7SMatt Ranostay /* DAC1 and DAC2 outputs are connected to the same DAC */ 1021960f9df7SMatt Ranostay dac_created = true; 1022960f9df7SMatt Ranostay chan->output = 1; 1023960f9df7SMatt Ranostay cnt++; 1024960f9df7SMatt Ranostay break; 1025960f9df7SMatt Ranostay default: 1026960f9df7SMatt Ranostay continue; 1027960f9df7SMatt Ranostay }; 1028960f9df7SMatt Ranostay 1029960f9df7SMatt Ranostay chan->type = IIO_VOLTAGE; 1030960f9df7SMatt Ranostay chan->indexed = 1; 1031960f9df7SMatt Ranostay chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 1032960f9df7SMatt Ranostay chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); 1033960f9df7SMatt Ranostay chan->scan_index = -1; 1034960f9df7SMatt Ranostay } 1035960f9df7SMatt Ranostay 1036960f9df7SMatt Ranostay return cnt; 1037960f9df7SMatt Ranostay } 1038960f9df7SMatt Ranostay 1039960f9df7SMatt Ranostay static void mcp_init_work(struct work_struct *work) 1040960f9df7SMatt Ranostay { 1041960f9df7SMatt Ranostay struct iio_dev *indio_dev; 1042960f9df7SMatt Ranostay struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work); 1043960f9df7SMatt Ranostay struct mcp2221_iio *data; 1044960f9df7SMatt Ranostay static int retries = 5; 1045960f9df7SMatt Ranostay int ret, num_channels; 1046960f9df7SMatt Ranostay 1047960f9df7SMatt Ranostay hid_hw_power(mcp->hdev, PM_HINT_FULLON); 1048960f9df7SMatt Ranostay mutex_lock(&mcp->lock); 1049960f9df7SMatt Ranostay 1050960f9df7SMatt Ranostay mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS; 1051960f9df7SMatt Ranostay ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 1052960f9df7SMatt Ranostay 1053960f9df7SMatt Ranostay if (ret == -EAGAIN) 1054960f9df7SMatt Ranostay goto reschedule_task; 1055960f9df7SMatt Ranostay 1056960f9df7SMatt Ranostay num_channels = mcp_iio_channels(mcp); 1057960f9df7SMatt Ranostay if (!num_channels) 1058960f9df7SMatt Ranostay goto unlock; 1059960f9df7SMatt Ranostay 1060960f9df7SMatt Ranostay mcp->txbuf[0] = MCP2221_READ_FLASH_DATA; 1061960f9df7SMatt Ranostay mcp->txbuf[1] = 0; 1062960f9df7SMatt Ranostay ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2); 1063960f9df7SMatt Ranostay 1064960f9df7SMatt Ranostay if (ret == -EAGAIN) 1065960f9df7SMatt Ranostay goto reschedule_task; 1066960f9df7SMatt Ranostay 1067960f9df7SMatt Ranostay indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data)); 1068960f9df7SMatt Ranostay if (!indio_dev) 1069960f9df7SMatt Ranostay goto unlock; 1070960f9df7SMatt Ranostay 1071960f9df7SMatt Ranostay data = iio_priv(indio_dev); 1072960f9df7SMatt Ranostay data->mcp = mcp; 1073960f9df7SMatt Ranostay 1074960f9df7SMatt Ranostay indio_dev->name = "mcp2221"; 1075960f9df7SMatt Ranostay indio_dev->modes = INDIO_DIRECT_MODE; 1076960f9df7SMatt Ranostay indio_dev->info = &mcp2221_info; 1077960f9df7SMatt Ranostay indio_dev->channels = mcp->iio_channels; 1078960f9df7SMatt Ranostay indio_dev->num_channels = num_channels; 1079960f9df7SMatt Ranostay 1080960f9df7SMatt Ranostay devm_iio_device_register(&mcp->hdev->dev, indio_dev); 1081960f9df7SMatt Ranostay 1082960f9df7SMatt Ranostay unlock: 1083960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 1084960f9df7SMatt Ranostay hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 1085960f9df7SMatt Ranostay 1086960f9df7SMatt Ranostay return; 1087960f9df7SMatt Ranostay 1088960f9df7SMatt Ranostay reschedule_task: 1089960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 1090960f9df7SMatt Ranostay hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 1091960f9df7SMatt Ranostay 1092960f9df7SMatt Ranostay if (!retries--) 1093960f9df7SMatt Ranostay return; 1094960f9df7SMatt Ranostay 1095960f9df7SMatt Ranostay /* Device is not ready to read SRAM or FLASH data, try again */ 1096960f9df7SMatt Ranostay schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100)); 1097960f9df7SMatt Ranostay } 1098960f9df7SMatt Ranostay #endif 1099960f9df7SMatt Ranostay 110067a95c21SRishi Gupta static int mcp2221_probe(struct hid_device *hdev, 110167a95c21SRishi Gupta const struct hid_device_id *id) 110267a95c21SRishi Gupta { 110367a95c21SRishi Gupta int ret; 110467a95c21SRishi Gupta struct mcp2221 *mcp; 110567a95c21SRishi Gupta 110667a95c21SRishi Gupta mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL); 110767a95c21SRishi Gupta if (!mcp) 110867a95c21SRishi Gupta return -ENOMEM; 110967a95c21SRishi Gupta 111067a95c21SRishi Gupta ret = hid_parse(hdev); 111167a95c21SRishi Gupta if (ret) { 111267a95c21SRishi Gupta hid_err(hdev, "can't parse reports\n"); 111367a95c21SRishi Gupta return ret; 111467a95c21SRishi Gupta } 111567a95c21SRishi Gupta 111667c90d14SEnrik Berkhan /* 111767c90d14SEnrik Berkhan * This driver uses the .raw_event callback and therefore does not need any 111867c90d14SEnrik Berkhan * HID_CONNECT_xxx flags. 111967c90d14SEnrik Berkhan */ 112067c90d14SEnrik Berkhan ret = hid_hw_start(hdev, 0); 112167a95c21SRishi Gupta if (ret) { 112267a95c21SRishi Gupta hid_err(hdev, "can't start hardware\n"); 112367a95c21SRishi Gupta return ret; 112467a95c21SRishi Gupta } 112567a95c21SRishi Gupta 112667c90d14SEnrik Berkhan hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8, 112767c90d14SEnrik Berkhan hdev->version & 0xff, hdev->name, hdev->phys); 112867c90d14SEnrik Berkhan 112967a95c21SRishi Gupta ret = hid_hw_open(hdev); 113067a95c21SRishi Gupta if (ret) { 113167a95c21SRishi Gupta hid_err(hdev, "can't open device\n"); 1132deb3b88bSMatt Ranostay hid_hw_stop(hdev); 1133deb3b88bSMatt Ranostay return ret; 113467a95c21SRishi Gupta } 113567a95c21SRishi Gupta 113667a95c21SRishi Gupta mutex_init(&mcp->lock); 113767a95c21SRishi Gupta init_completion(&mcp->wait_in_report); 113867a95c21SRishi Gupta hid_set_drvdata(hdev, mcp); 113967a95c21SRishi Gupta mcp->hdev = hdev; 114067a95c21SRishi Gupta 1141deb3b88bSMatt Ranostay ret = devm_add_action_or_reset(&hdev->dev, mcp2221_hid_unregister, hdev); 1142deb3b88bSMatt Ranostay if (ret) 1143deb3b88bSMatt Ranostay return ret; 1144deb3b88bSMatt Ranostay 114567a95c21SRishi Gupta /* Set I2C bus clock diviser */ 114667a95c21SRishi Gupta if (i2c_clk_freq > 400) 114767a95c21SRishi Gupta i2c_clk_freq = 400; 114867a95c21SRishi Gupta if (i2c_clk_freq < 50) 114967a95c21SRishi Gupta i2c_clk_freq = 50; 115067a95c21SRishi Gupta mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3; 115167a95c21SRishi Gupta 115267a95c21SRishi Gupta mcp->adapter.owner = THIS_MODULE; 115367a95c21SRishi Gupta mcp->adapter.class = I2C_CLASS_HWMON; 115467a95c21SRishi Gupta mcp->adapter.algo = &mcp_i2c_algo; 115567a95c21SRishi Gupta mcp->adapter.retries = 1; 115667a95c21SRishi Gupta mcp->adapter.dev.parent = &hdev->dev; 115767a95c21SRishi Gupta snprintf(mcp->adapter.name, sizeof(mcp->adapter.name), 115867c90d14SEnrik Berkhan "MCP2221 usb-i2c bridge"); 115967a95c21SRishi Gupta 1160deb3b88bSMatt Ranostay ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter); 116167a95c21SRishi Gupta if (ret) { 116267a95c21SRishi Gupta hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret); 1163deb3b88bSMatt Ranostay return ret; 116467a95c21SRishi Gupta } 116567a95c21SRishi Gupta i2c_set_adapdata(&mcp->adapter, mcp); 116667a95c21SRishi Gupta 11673d74c9ecSMatt Ranostay #if IS_REACHABLE(CONFIG_GPIOLIB) 1168328de1c5SRishi Gupta /* Setup GPIO chip */ 1169328de1c5SRishi Gupta mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL); 1170deb3b88bSMatt Ranostay if (!mcp->gc) 1171deb3b88bSMatt Ranostay return -ENOMEM; 1172328de1c5SRishi Gupta 1173328de1c5SRishi Gupta mcp->gc->label = "mcp2221_gpio"; 1174328de1c5SRishi Gupta mcp->gc->direction_input = mcp_gpio_direction_input; 1175328de1c5SRishi Gupta mcp->gc->direction_output = mcp_gpio_direction_output; 1176328de1c5SRishi Gupta mcp->gc->get_direction = mcp_gpio_get_direction; 1177328de1c5SRishi Gupta mcp->gc->set = mcp_gpio_set; 1178328de1c5SRishi Gupta mcp->gc->get = mcp_gpio_get; 1179567b8e9fSLars Povlsen mcp->gc->ngpio = MCP_NGPIO; 1180328de1c5SRishi Gupta mcp->gc->base = -1; 1181328de1c5SRishi Gupta mcp->gc->can_sleep = 1; 1182328de1c5SRishi Gupta mcp->gc->parent = &hdev->dev; 1183328de1c5SRishi Gupta 1184328de1c5SRishi Gupta ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp); 1185328de1c5SRishi Gupta if (ret) 1186deb3b88bSMatt Ranostay return ret; 1187ea418b35SMatt Ranostay #endif 1188328de1c5SRishi Gupta 1189960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 1190960f9df7SMatt Ranostay INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work); 1191960f9df7SMatt Ranostay schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100)); 1192960f9df7SMatt Ranostay #endif 1193960f9df7SMatt Ranostay 119467a95c21SRishi Gupta return 0; 119567a95c21SRishi Gupta } 119667a95c21SRishi Gupta 119767a95c21SRishi Gupta static const struct hid_device_id mcp2221_devices[] = { 119867a95c21SRishi Gupta { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) }, 119967a95c21SRishi Gupta { } 120067a95c21SRishi Gupta }; 120167a95c21SRishi Gupta MODULE_DEVICE_TABLE(hid, mcp2221_devices); 120267a95c21SRishi Gupta 120367a95c21SRishi Gupta static struct hid_driver mcp2221_driver = { 120467a95c21SRishi Gupta .name = "mcp2221", 120567a95c21SRishi Gupta .id_table = mcp2221_devices, 120667a95c21SRishi Gupta .probe = mcp2221_probe, 120767a95c21SRishi Gupta .remove = mcp2221_remove, 120867a95c21SRishi Gupta .raw_event = mcp2221_raw_event, 120967a95c21SRishi Gupta }; 121067a95c21SRishi Gupta 121167a95c21SRishi Gupta /* Register with HID core */ 121267a95c21SRishi Gupta module_hid_driver(mcp2221_driver); 121367a95c21SRishi Gupta 121467a95c21SRishi Gupta MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>"); 121567a95c21SRishi Gupta MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge"); 121667a95c21SRishi Gupta MODULE_LICENSE("GPL v2"); 1217