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, 5226824686SHamish Martin MCP2221_I2C_READ_PARTIAL = 0x54, 5367a95c21SRishi Gupta MCP2221_I2C_READ_COMPL = 0x55, 54328de1c5SRishi Gupta MCP2221_ALT_F_NOT_GPIOV = 0xEE, 55328de1c5SRishi Gupta MCP2221_ALT_F_NOT_GPIOD = 0xEF, 5667a95c21SRishi Gupta }; 5767a95c21SRishi Gupta 58567b8e9fSLars Povlsen /* MCP GPIO direction encoding */ 59567b8e9fSLars Povlsen enum { 60567b8e9fSLars Povlsen MCP2221_DIR_OUT = 0x00, 61567b8e9fSLars Povlsen MCP2221_DIR_IN = 0x01, 62567b8e9fSLars Povlsen }; 63567b8e9fSLars Povlsen 64567b8e9fSLars Povlsen #define MCP_NGPIO 4 65567b8e9fSLars Povlsen 66567b8e9fSLars Povlsen /* MCP GPIO set command layout */ 67567b8e9fSLars Povlsen struct mcp_set_gpio { 68567b8e9fSLars Povlsen u8 cmd; 69567b8e9fSLars Povlsen u8 dummy; 70567b8e9fSLars Povlsen struct { 71567b8e9fSLars Povlsen u8 change_value; 72567b8e9fSLars Povlsen u8 value; 73567b8e9fSLars Povlsen u8 change_direction; 74567b8e9fSLars Povlsen u8 direction; 75567b8e9fSLars Povlsen } gpio[MCP_NGPIO]; 76567b8e9fSLars Povlsen } __packed; 77567b8e9fSLars Povlsen 78567b8e9fSLars Povlsen /* MCP GPIO get command layout */ 79567b8e9fSLars Povlsen struct mcp_get_gpio { 80567b8e9fSLars Povlsen u8 cmd; 81567b8e9fSLars Povlsen u8 dummy; 82567b8e9fSLars Povlsen struct { 83567b8e9fSLars Povlsen u8 value; 84e36c31f8SLouis Morhet u8 direction; 85567b8e9fSLars Povlsen } gpio[MCP_NGPIO]; 86567b8e9fSLars Povlsen } __packed; 87567b8e9fSLars Povlsen 8867a95c21SRishi Gupta /* 8967a95c21SRishi Gupta * There is no way to distinguish responses. Therefore next command 9067a95c21SRishi Gupta * is sent only after response to previous has been received. Mutex 9167a95c21SRishi Gupta * lock is used for this purpose mainly. 9267a95c21SRishi Gupta */ 9367a95c21SRishi Gupta struct mcp2221 { 9467a95c21SRishi Gupta struct hid_device *hdev; 9567a95c21SRishi Gupta struct i2c_adapter adapter; 9667a95c21SRishi Gupta struct mutex lock; 9767a95c21SRishi Gupta struct completion wait_in_report; 98960f9df7SMatt Ranostay struct delayed_work init_work; 9967a95c21SRishi Gupta u8 *rxbuf; 10067a95c21SRishi Gupta u8 txbuf[64]; 10167a95c21SRishi Gupta int rxbuf_idx; 10267a95c21SRishi Gupta int status; 10367a95c21SRishi Gupta u8 cur_i2c_clk_div; 104328de1c5SRishi Gupta struct gpio_chip *gc; 105328de1c5SRishi Gupta u8 gp_idx; 106328de1c5SRishi Gupta u8 gpio_dir; 107960f9df7SMatt Ranostay u8 mode[4]; 108960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 109960f9df7SMatt Ranostay struct iio_chan_spec iio_channels[3]; 110960f9df7SMatt Ranostay u16 adc_values[3]; 111960f9df7SMatt Ranostay u8 adc_scale; 112960f9df7SMatt Ranostay u8 dac_value; 113960f9df7SMatt Ranostay u16 dac_scale; 114960f9df7SMatt Ranostay #endif 115960f9df7SMatt Ranostay }; 116960f9df7SMatt Ranostay 117960f9df7SMatt Ranostay struct mcp2221_iio { 118960f9df7SMatt Ranostay struct mcp2221 *mcp; 11967a95c21SRishi Gupta }; 12067a95c21SRishi Gupta 12167a95c21SRishi Gupta /* 12267a95c21SRishi Gupta * Default i2c bus clock frequency 400 kHz. Modify this if you 12367a95c21SRishi Gupta * want to set some other frequency (min 50 kHz - max 400 kHz). 12467a95c21SRishi Gupta */ 12567a95c21SRishi Gupta static uint i2c_clk_freq = 400; 12667a95c21SRishi Gupta 12767a95c21SRishi Gupta /* Synchronously send output report to the device */ 12867a95c21SRishi Gupta static int mcp_send_report(struct mcp2221 *mcp, 12967a95c21SRishi Gupta u8 *out_report, size_t len) 13067a95c21SRishi Gupta { 13167a95c21SRishi Gupta u8 *buf; 13267a95c21SRishi Gupta int ret; 13367a95c21SRishi Gupta 13467a95c21SRishi Gupta buf = kmemdup(out_report, len, GFP_KERNEL); 13567a95c21SRishi Gupta if (!buf) 13667a95c21SRishi Gupta return -ENOMEM; 13767a95c21SRishi Gupta 13867a95c21SRishi Gupta /* mcp2221 uses interrupt endpoint for out reports */ 13967a95c21SRishi Gupta ret = hid_hw_output_report(mcp->hdev, buf, len); 14067a95c21SRishi Gupta kfree(buf); 14167a95c21SRishi Gupta 14267a95c21SRishi Gupta if (ret < 0) 14367a95c21SRishi Gupta return ret; 14467a95c21SRishi Gupta return 0; 14567a95c21SRishi Gupta } 14667a95c21SRishi Gupta 14767a95c21SRishi Gupta /* 14867a95c21SRishi Gupta * Send o/p report to the device and wait for i/p report to be 14967a95c21SRishi Gupta * received from the device. If the device does not respond, 15067a95c21SRishi Gupta * we timeout. 15167a95c21SRishi Gupta */ 15267a95c21SRishi Gupta static int mcp_send_data_req_status(struct mcp2221 *mcp, 15367a95c21SRishi Gupta u8 *out_report, int len) 15467a95c21SRishi Gupta { 15567a95c21SRishi Gupta int ret; 15667a95c21SRishi Gupta unsigned long t; 15767a95c21SRishi Gupta 15867a95c21SRishi Gupta reinit_completion(&mcp->wait_in_report); 15967a95c21SRishi Gupta 16067a95c21SRishi Gupta ret = mcp_send_report(mcp, out_report, len); 16167a95c21SRishi Gupta if (ret) 16267a95c21SRishi Gupta return ret; 16367a95c21SRishi Gupta 16467a95c21SRishi Gupta t = wait_for_completion_timeout(&mcp->wait_in_report, 16567a95c21SRishi Gupta msecs_to_jiffies(4000)); 16667a95c21SRishi Gupta if (!t) 16767a95c21SRishi Gupta return -ETIMEDOUT; 16867a95c21SRishi Gupta 16967a95c21SRishi Gupta return mcp->status; 17067a95c21SRishi Gupta } 17167a95c21SRishi Gupta 17267a95c21SRishi Gupta /* Check pass/fail for actual communication with i2c slave */ 17367a95c21SRishi Gupta static int mcp_chk_last_cmd_status(struct mcp2221 *mcp) 17467a95c21SRishi Gupta { 17567a95c21SRishi Gupta memset(mcp->txbuf, 0, 8); 17667a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 17767a95c21SRishi Gupta 17867a95c21SRishi Gupta return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 17967a95c21SRishi Gupta } 18067a95c21SRishi Gupta 18167a95c21SRishi Gupta /* Cancels last command releasing i2c bus just in case occupied */ 18267a95c21SRishi Gupta static int mcp_cancel_last_cmd(struct mcp2221 *mcp) 18367a95c21SRishi Gupta { 18467a95c21SRishi Gupta memset(mcp->txbuf, 0, 8); 18567a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 18667a95c21SRishi Gupta mcp->txbuf[2] = MCP2221_I2C_CANCEL; 18767a95c21SRishi Gupta 18867a95c21SRishi Gupta return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 18967a95c21SRishi Gupta } 19067a95c21SRishi Gupta 19102a46753SHamish Martin /* Check if the last command succeeded or failed and return the result. 19202a46753SHamish Martin * If the command did fail, cancel that command which will free the i2c bus. 19302a46753SHamish Martin */ 19402a46753SHamish Martin static int mcp_chk_last_cmd_status_free_bus(struct mcp2221 *mcp) 19502a46753SHamish Martin { 19602a46753SHamish Martin int ret; 19702a46753SHamish Martin 19802a46753SHamish Martin ret = mcp_chk_last_cmd_status(mcp); 19902a46753SHamish Martin if (ret) { 20002a46753SHamish Martin /* The last command was a failure. 20102a46753SHamish Martin * Send a cancel which will also free the bus. 20202a46753SHamish Martin */ 20302a46753SHamish Martin usleep_range(980, 1000); 20402a46753SHamish Martin mcp_cancel_last_cmd(mcp); 20502a46753SHamish Martin } 20602a46753SHamish Martin 20702a46753SHamish Martin return ret; 20802a46753SHamish Martin } 20902a46753SHamish Martin 21067a95c21SRishi Gupta static int mcp_set_i2c_speed(struct mcp2221 *mcp) 21167a95c21SRishi Gupta { 21267a95c21SRishi Gupta int ret; 21367a95c21SRishi Gupta 21467a95c21SRishi Gupta memset(mcp->txbuf, 0, 8); 21567a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 21667a95c21SRishi Gupta mcp->txbuf[3] = MCP2221_I2C_SET_SPEED; 21767a95c21SRishi Gupta mcp->txbuf[4] = mcp->cur_i2c_clk_div; 21867a95c21SRishi Gupta 21967a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8); 22067a95c21SRishi Gupta if (ret) { 22167a95c21SRishi Gupta /* Small delay is needed here */ 22267a95c21SRishi Gupta usleep_range(980, 1000); 22367a95c21SRishi Gupta mcp_cancel_last_cmd(mcp); 22467a95c21SRishi Gupta } 22567a95c21SRishi Gupta 22667a95c21SRishi Gupta return 0; 22767a95c21SRishi Gupta } 22867a95c21SRishi Gupta 22967a95c21SRishi Gupta /* 23067a95c21SRishi Gupta * An output report can contain minimum 1 and maximum 60 user data 23167a95c21SRishi Gupta * bytes. If the number of data bytes is more then 60, we send it 23267a95c21SRishi Gupta * in chunks of 60 bytes. Last chunk may contain exactly 60 or less 23367a95c21SRishi Gupta * bytes. Total number of bytes is informed in very first report to 23467a95c21SRishi Gupta * mcp2221, from that point onwards it first collect all the data 23567a95c21SRishi Gupta * from host and then send to i2c slave device. 23667a95c21SRishi Gupta */ 23767a95c21SRishi Gupta static int mcp_i2c_write(struct mcp2221 *mcp, 23867a95c21SRishi Gupta struct i2c_msg *msg, int type, u8 last_status) 23967a95c21SRishi Gupta { 24067a95c21SRishi Gupta int ret, len, idx, sent; 24167a95c21SRishi Gupta 24267a95c21SRishi Gupta idx = 0; 24367a95c21SRishi Gupta sent = 0; 24467a95c21SRishi Gupta if (msg->len < 60) 24567a95c21SRishi Gupta len = msg->len; 24667a95c21SRishi Gupta else 24767a95c21SRishi Gupta len = 60; 24867a95c21SRishi Gupta 24967a95c21SRishi Gupta do { 25067a95c21SRishi Gupta mcp->txbuf[0] = type; 25167a95c21SRishi Gupta mcp->txbuf[1] = msg->len & 0xff; 25267a95c21SRishi Gupta mcp->txbuf[2] = msg->len >> 8; 25367a95c21SRishi Gupta mcp->txbuf[3] = (u8)(msg->addr << 1); 25467a95c21SRishi Gupta 25567a95c21SRishi Gupta memcpy(&mcp->txbuf[4], &msg->buf[idx], len); 25667a95c21SRishi Gupta 25767a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4); 25867a95c21SRishi Gupta if (ret) 25967a95c21SRishi Gupta return ret; 26067a95c21SRishi Gupta 26167a95c21SRishi Gupta usleep_range(980, 1000); 26267a95c21SRishi Gupta 26367a95c21SRishi Gupta if (last_status) { 26402a46753SHamish Martin ret = mcp_chk_last_cmd_status_free_bus(mcp); 26567a95c21SRishi Gupta if (ret) 26667a95c21SRishi Gupta return ret; 26767a95c21SRishi Gupta } 26867a95c21SRishi Gupta 26967a95c21SRishi Gupta sent = sent + len; 27067a95c21SRishi Gupta if (sent >= msg->len) 27167a95c21SRishi Gupta break; 27267a95c21SRishi Gupta 27367a95c21SRishi Gupta idx = idx + len; 27467a95c21SRishi Gupta if ((msg->len - sent) < 60) 27567a95c21SRishi Gupta len = msg->len - sent; 27667a95c21SRishi Gupta else 27767a95c21SRishi Gupta len = 60; 27867a95c21SRishi Gupta 27967a95c21SRishi Gupta /* 28067a95c21SRishi Gupta * Testing shows delay is needed between successive writes 28167a95c21SRishi Gupta * otherwise next write fails on first-try from i2c core. 28267a95c21SRishi Gupta * This value is obtained through automated stress testing. 28367a95c21SRishi Gupta */ 28467a95c21SRishi Gupta usleep_range(980, 1000); 28567a95c21SRishi Gupta } while (len > 0); 28667a95c21SRishi Gupta 28767a95c21SRishi Gupta return ret; 28867a95c21SRishi Gupta } 28967a95c21SRishi Gupta 29067a95c21SRishi Gupta /* 29167a95c21SRishi Gupta * Device reads all data (0 - 65535 bytes) from i2c slave device and 29267a95c21SRishi Gupta * stores it in device itself. This data is read back from device to 29367a95c21SRishi Gupta * host in multiples of 60 bytes using input reports. 29467a95c21SRishi Gupta */ 29567a95c21SRishi Gupta static int mcp_i2c_smbus_read(struct mcp2221 *mcp, 29667a95c21SRishi Gupta struct i2c_msg *msg, int type, u16 smbus_addr, 29767a95c21SRishi Gupta u8 smbus_len, u8 *smbus_buf) 29867a95c21SRishi Gupta { 29967a95c21SRishi Gupta int ret; 30067a95c21SRishi Gupta u16 total_len; 30126824686SHamish Martin int retries = 0; 30267a95c21SRishi Gupta 30367a95c21SRishi Gupta mcp->txbuf[0] = type; 30467a95c21SRishi Gupta if (msg) { 30567a95c21SRishi Gupta mcp->txbuf[1] = msg->len & 0xff; 30667a95c21SRishi Gupta mcp->txbuf[2] = msg->len >> 8; 30767a95c21SRishi Gupta mcp->txbuf[3] = (u8)(msg->addr << 1); 30867a95c21SRishi Gupta total_len = msg->len; 30967a95c21SRishi Gupta mcp->rxbuf = msg->buf; 31067a95c21SRishi Gupta } else { 31167a95c21SRishi Gupta mcp->txbuf[1] = smbus_len; 31267a95c21SRishi Gupta mcp->txbuf[2] = 0; 31367a95c21SRishi Gupta mcp->txbuf[3] = (u8)(smbus_addr << 1); 31467a95c21SRishi Gupta total_len = smbus_len; 31567a95c21SRishi Gupta mcp->rxbuf = smbus_buf; 31667a95c21SRishi Gupta } 31767a95c21SRishi Gupta 31867a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4); 31967a95c21SRishi Gupta if (ret) 32067a95c21SRishi Gupta return ret; 32167a95c21SRishi Gupta 32267a95c21SRishi Gupta mcp->rxbuf_idx = 0; 32367a95c21SRishi Gupta 32467a95c21SRishi Gupta do { 32526824686SHamish Martin /* Wait for the data to be read by the device */ 32626824686SHamish Martin usleep_range(980, 1000); 32726824686SHamish Martin 32867a95c21SRishi Gupta memset(mcp->txbuf, 0, 4); 32967a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 33067a95c21SRishi Gupta 33167a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 33226824686SHamish Martin if (ret) { 33326824686SHamish Martin if (retries < 5) { 33426824686SHamish Martin /* The data wasn't ready to read. 33526824686SHamish Martin * Wait a bit longer and try again. 33626824686SHamish Martin */ 33726824686SHamish Martin usleep_range(90, 100); 33826824686SHamish Martin retries++; 33926824686SHamish Martin } else { 34067a95c21SRishi Gupta return ret; 34126824686SHamish Martin } 34226824686SHamish Martin } else { 34326824686SHamish Martin retries = 0; 34426824686SHamish Martin } 34526824686SHamish Martin } while (mcp->rxbuf_idx < total_len); 34667a95c21SRishi Gupta 34767a95c21SRishi Gupta usleep_range(980, 1000); 34826824686SHamish Martin ret = mcp_chk_last_cmd_status_free_bus(mcp); 34967a95c21SRishi Gupta 35067a95c21SRishi Gupta return ret; 35167a95c21SRishi Gupta } 35267a95c21SRishi Gupta 35367a95c21SRishi Gupta static int mcp_i2c_xfer(struct i2c_adapter *adapter, 35467a95c21SRishi Gupta struct i2c_msg msgs[], int num) 35567a95c21SRishi Gupta { 35667a95c21SRishi Gupta int ret; 35767a95c21SRishi Gupta struct mcp2221 *mcp = i2c_get_adapdata(adapter); 35867a95c21SRishi Gupta 35967a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_FULLON); 36067a95c21SRishi Gupta 36167a95c21SRishi Gupta mutex_lock(&mcp->lock); 36267a95c21SRishi Gupta 36367a95c21SRishi Gupta if (num == 1) { 36467a95c21SRishi Gupta if (msgs->flags & I2C_M_RD) { 36567a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA, 36667a95c21SRishi Gupta 0, 0, NULL); 36767a95c21SRishi Gupta } else { 36867a95c21SRishi Gupta ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1); 36967a95c21SRishi Gupta } 37067a95c21SRishi Gupta if (ret) 37167a95c21SRishi Gupta goto exit; 37267a95c21SRishi Gupta ret = num; 37367a95c21SRishi Gupta } else if (num == 2) { 37467a95c21SRishi Gupta /* Ex transaction; send reg address and read its contents */ 37567a95c21SRishi Gupta if (msgs[0].addr == msgs[1].addr && 37667a95c21SRishi Gupta !(msgs[0].flags & I2C_M_RD) && 37767a95c21SRishi Gupta (msgs[1].flags & I2C_M_RD)) { 37867a95c21SRishi Gupta 37967a95c21SRishi Gupta ret = mcp_i2c_write(mcp, &msgs[0], 38067a95c21SRishi Gupta MCP2221_I2C_WR_NO_STOP, 0); 38167a95c21SRishi Gupta if (ret) 38267a95c21SRishi Gupta goto exit; 38367a95c21SRishi Gupta 38467a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, &msgs[1], 38567a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 38667a95c21SRishi Gupta 0, 0, NULL); 38767a95c21SRishi Gupta if (ret) 38867a95c21SRishi Gupta goto exit; 38967a95c21SRishi Gupta ret = num; 39067a95c21SRishi Gupta } else { 39167a95c21SRishi Gupta dev_err(&adapter->dev, 39267a95c21SRishi Gupta "unsupported multi-msg i2c transaction\n"); 39367a95c21SRishi Gupta ret = -EOPNOTSUPP; 39467a95c21SRishi Gupta } 39567a95c21SRishi Gupta } else { 39667a95c21SRishi Gupta dev_err(&adapter->dev, 39767a95c21SRishi Gupta "unsupported multi-msg i2c transaction\n"); 39867a95c21SRishi Gupta ret = -EOPNOTSUPP; 39967a95c21SRishi Gupta } 40067a95c21SRishi Gupta 40167a95c21SRishi Gupta exit: 40267a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 40367a95c21SRishi Gupta mutex_unlock(&mcp->lock); 40467a95c21SRishi Gupta return ret; 40567a95c21SRishi Gupta } 40667a95c21SRishi Gupta 40767a95c21SRishi Gupta static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr, 40867a95c21SRishi Gupta u8 command, u8 *buf, u8 len, int type, 40967a95c21SRishi Gupta u8 last_status) 41067a95c21SRishi Gupta { 41167a95c21SRishi Gupta int data_len, ret; 41267a95c21SRishi Gupta 41367a95c21SRishi Gupta mcp->txbuf[0] = type; 41467a95c21SRishi Gupta mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */ 41567a95c21SRishi Gupta mcp->txbuf[2] = 0; 41667a95c21SRishi Gupta mcp->txbuf[3] = (u8)(addr << 1); 41767a95c21SRishi Gupta mcp->txbuf[4] = command; 41867a95c21SRishi Gupta 41967a95c21SRishi Gupta switch (len) { 42067a95c21SRishi Gupta case 0: 42167a95c21SRishi Gupta data_len = 5; 42267a95c21SRishi Gupta break; 42367a95c21SRishi Gupta case 1: 42467a95c21SRishi Gupta mcp->txbuf[5] = buf[0]; 42567a95c21SRishi Gupta data_len = 6; 42667a95c21SRishi Gupta break; 42767a95c21SRishi Gupta case 2: 42867a95c21SRishi Gupta mcp->txbuf[5] = buf[0]; 42967a95c21SRishi Gupta mcp->txbuf[6] = buf[1]; 43067a95c21SRishi Gupta data_len = 7; 43167a95c21SRishi Gupta break; 43267a95c21SRishi Gupta default: 43362ac2473SHarshit Mogalapalli if (len > I2C_SMBUS_BLOCK_MAX) 43462ac2473SHarshit Mogalapalli return -EINVAL; 43562ac2473SHarshit Mogalapalli 43667a95c21SRishi Gupta memcpy(&mcp->txbuf[5], buf, len); 43767a95c21SRishi Gupta data_len = len + 5; 43867a95c21SRishi Gupta } 43967a95c21SRishi Gupta 44067a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len); 44167a95c21SRishi Gupta if (ret) 44267a95c21SRishi Gupta return ret; 44367a95c21SRishi Gupta 44467a95c21SRishi Gupta if (last_status) { 44567a95c21SRishi Gupta usleep_range(980, 1000); 44667a95c21SRishi Gupta 44702a46753SHamish Martin ret = mcp_chk_last_cmd_status_free_bus(mcp); 44867a95c21SRishi Gupta } 44967a95c21SRishi Gupta 45067a95c21SRishi Gupta return ret; 45167a95c21SRishi Gupta } 45267a95c21SRishi Gupta 45367a95c21SRishi Gupta static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 45467a95c21SRishi Gupta unsigned short flags, char read_write, 45567a95c21SRishi Gupta u8 command, int size, 45667a95c21SRishi Gupta union i2c_smbus_data *data) 45767a95c21SRishi Gupta { 45867a95c21SRishi Gupta int ret; 45967a95c21SRishi Gupta struct mcp2221 *mcp = i2c_get_adapdata(adapter); 46067a95c21SRishi Gupta 46167a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_FULLON); 46267a95c21SRishi Gupta 46367a95c21SRishi Gupta mutex_lock(&mcp->lock); 46467a95c21SRishi Gupta 46567a95c21SRishi Gupta switch (size) { 46667a95c21SRishi Gupta 46767a95c21SRishi Gupta case I2C_SMBUS_QUICK: 46867a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) 46967a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 47067a95c21SRishi Gupta addr, 0, &data->byte); 47167a95c21SRishi Gupta else 47267a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 47367a95c21SRishi Gupta 0, MCP2221_I2C_WR_DATA, 1); 47467a95c21SRishi Gupta break; 47567a95c21SRishi Gupta case I2C_SMBUS_BYTE: 47667a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) 47767a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 47867a95c21SRishi Gupta addr, 1, &data->byte); 47967a95c21SRishi Gupta else 48067a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 48167a95c21SRishi Gupta 0, MCP2221_I2C_WR_DATA, 1); 48267a95c21SRishi Gupta break; 48367a95c21SRishi Gupta case I2C_SMBUS_BYTE_DATA: 48467a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 48567a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 48667a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 0); 48767a95c21SRishi Gupta if (ret) 48867a95c21SRishi Gupta goto exit; 48967a95c21SRishi Gupta 49067a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 49167a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 49267a95c21SRishi Gupta addr, 1, &data->byte); 49367a95c21SRishi Gupta } else { 49467a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, &data->byte, 49567a95c21SRishi Gupta 1, MCP2221_I2C_WR_DATA, 1); 49667a95c21SRishi Gupta } 49767a95c21SRishi Gupta break; 49867a95c21SRishi Gupta case I2C_SMBUS_WORD_DATA: 49967a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 50067a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 50167a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 0); 50267a95c21SRishi Gupta if (ret) 50367a95c21SRishi Gupta goto exit; 50467a95c21SRishi Gupta 50567a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 50667a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 50767a95c21SRishi Gupta addr, 2, (u8 *)&data->word); 50867a95c21SRishi Gupta } else { 50967a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, 51067a95c21SRishi Gupta (u8 *)&data->word, 2, 51167a95c21SRishi Gupta MCP2221_I2C_WR_DATA, 1); 51267a95c21SRishi Gupta } 51367a95c21SRishi Gupta break; 51467a95c21SRishi Gupta case I2C_SMBUS_BLOCK_DATA: 51567a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 51667a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 51767a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 1); 51867a95c21SRishi Gupta if (ret) 51967a95c21SRishi Gupta goto exit; 52067a95c21SRishi Gupta 52167a95c21SRishi Gupta mcp->rxbuf_idx = 0; 52267a95c21SRishi Gupta mcp->rxbuf = data->block; 52367a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 52467a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 52567a95c21SRishi Gupta if (ret) 52667a95c21SRishi Gupta goto exit; 52767a95c21SRishi Gupta } else { 52867a95c21SRishi Gupta if (!data->block[0]) { 52967a95c21SRishi Gupta ret = -EINVAL; 53067a95c21SRishi Gupta goto exit; 53167a95c21SRishi Gupta } 53267a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, data->block, 53367a95c21SRishi Gupta data->block[0] + 1, 53467a95c21SRishi Gupta MCP2221_I2C_WR_DATA, 1); 53567a95c21SRishi Gupta } 53667a95c21SRishi Gupta break; 53767a95c21SRishi Gupta case I2C_SMBUS_I2C_BLOCK_DATA: 53867a95c21SRishi Gupta if (read_write == I2C_SMBUS_READ) { 53967a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, NULL, 54067a95c21SRishi Gupta 0, MCP2221_I2C_WR_NO_STOP, 1); 54167a95c21SRishi Gupta if (ret) 54267a95c21SRishi Gupta goto exit; 54367a95c21SRishi Gupta 54467a95c21SRishi Gupta mcp->rxbuf_idx = 0; 54567a95c21SRishi Gupta mcp->rxbuf = data->block; 54667a95c21SRishi Gupta mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 54767a95c21SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 54867a95c21SRishi Gupta if (ret) 54967a95c21SRishi Gupta goto exit; 55067a95c21SRishi Gupta } else { 55167a95c21SRishi Gupta if (!data->block[0]) { 55267a95c21SRishi Gupta ret = -EINVAL; 55367a95c21SRishi Gupta goto exit; 55467a95c21SRishi Gupta } 55567a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, 55667a95c21SRishi Gupta &data->block[1], data->block[0], 55767a95c21SRishi Gupta MCP2221_I2C_WR_DATA, 1); 55867a95c21SRishi Gupta } 55967a95c21SRishi Gupta break; 56067a95c21SRishi Gupta case I2C_SMBUS_PROC_CALL: 56167a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, 56267a95c21SRishi Gupta (u8 *)&data->word, 56367a95c21SRishi Gupta 2, MCP2221_I2C_WR_NO_STOP, 0); 56467a95c21SRishi Gupta if (ret) 56567a95c21SRishi Gupta goto exit; 56667a95c21SRishi Gupta 56767a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 56867a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 56967a95c21SRishi Gupta addr, 2, (u8 *)&data->word); 57067a95c21SRishi Gupta break; 57167a95c21SRishi Gupta case I2C_SMBUS_BLOCK_PROC_CALL: 57267a95c21SRishi Gupta ret = mcp_smbus_write(mcp, addr, command, data->block, 57367a95c21SRishi Gupta data->block[0] + 1, 57467a95c21SRishi Gupta MCP2221_I2C_WR_NO_STOP, 0); 57567a95c21SRishi Gupta if (ret) 57667a95c21SRishi Gupta goto exit; 57767a95c21SRishi Gupta 57867a95c21SRishi Gupta ret = mcp_i2c_smbus_read(mcp, NULL, 57967a95c21SRishi Gupta MCP2221_I2C_RD_RPT_START, 58067a95c21SRishi Gupta addr, I2C_SMBUS_BLOCK_MAX, 58167a95c21SRishi Gupta data->block); 58267a95c21SRishi Gupta break; 58367a95c21SRishi Gupta default: 58467a95c21SRishi Gupta dev_err(&mcp->adapter.dev, 58567a95c21SRishi Gupta "unsupported smbus transaction size:%d\n", size); 58667a95c21SRishi Gupta ret = -EOPNOTSUPP; 58767a95c21SRishi Gupta } 58867a95c21SRishi Gupta 58967a95c21SRishi Gupta exit: 59067a95c21SRishi Gupta hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 59167a95c21SRishi Gupta mutex_unlock(&mcp->lock); 59267a95c21SRishi Gupta return ret; 59367a95c21SRishi Gupta } 59467a95c21SRishi Gupta 59567a95c21SRishi Gupta static u32 mcp_i2c_func(struct i2c_adapter *adapter) 59667a95c21SRishi Gupta { 59767a95c21SRishi Gupta return I2C_FUNC_I2C | 59867a95c21SRishi Gupta I2C_FUNC_SMBUS_READ_BLOCK_DATA | 59967a95c21SRishi Gupta I2C_FUNC_SMBUS_BLOCK_PROC_CALL | 60067a95c21SRishi Gupta (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC); 60167a95c21SRishi Gupta } 60267a95c21SRishi Gupta 60367a95c21SRishi Gupta static const struct i2c_algorithm mcp_i2c_algo = { 60467a95c21SRishi Gupta .master_xfer = mcp_i2c_xfer, 60567a95c21SRishi Gupta .smbus_xfer = mcp_smbus_xfer, 60667a95c21SRishi Gupta .functionality = mcp_i2c_func, 60767a95c21SRishi Gupta }; 60867a95c21SRishi Gupta 6093d74c9ecSMatt Ranostay #if IS_REACHABLE(CONFIG_GPIOLIB) 610328de1c5SRishi Gupta static int mcp_gpio_get(struct gpio_chip *gc, 611328de1c5SRishi Gupta unsigned int offset) 612328de1c5SRishi Gupta { 613328de1c5SRishi Gupta int ret; 614328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 615328de1c5SRishi Gupta 616328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_GET; 617328de1c5SRishi Gupta 618ca6961d8SLouis Morhet mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]); 619328de1c5SRishi Gupta 620328de1c5SRishi Gupta mutex_lock(&mcp->lock); 621328de1c5SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 622328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 623328de1c5SRishi Gupta 624328de1c5SRishi Gupta return ret; 625328de1c5SRishi Gupta } 626328de1c5SRishi Gupta 627328de1c5SRishi Gupta static void mcp_gpio_set(struct gpio_chip *gc, 628328de1c5SRishi Gupta unsigned int offset, int value) 629328de1c5SRishi Gupta { 630328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 631328de1c5SRishi Gupta 632328de1c5SRishi Gupta memset(mcp->txbuf, 0, 18); 633328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_SET; 634328de1c5SRishi Gupta 635567b8e9fSLars Povlsen mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value); 636328de1c5SRishi Gupta 637328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx - 1] = 1; 638328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx] = !!value; 639328de1c5SRishi Gupta 640328de1c5SRishi Gupta mutex_lock(&mcp->lock); 641328de1c5SRishi Gupta mcp_send_data_req_status(mcp, mcp->txbuf, 18); 642328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 643328de1c5SRishi Gupta } 644328de1c5SRishi Gupta 645328de1c5SRishi Gupta static int mcp_gpio_dir_set(struct mcp2221 *mcp, 646328de1c5SRishi Gupta unsigned int offset, u8 val) 647328de1c5SRishi Gupta { 648328de1c5SRishi Gupta memset(mcp->txbuf, 0, 18); 649328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_SET; 650328de1c5SRishi Gupta 651567b8e9fSLars Povlsen mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction); 652328de1c5SRishi Gupta 653328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx - 1] = 1; 654328de1c5SRishi Gupta mcp->txbuf[mcp->gp_idx] = val; 655328de1c5SRishi Gupta 656328de1c5SRishi Gupta return mcp_send_data_req_status(mcp, mcp->txbuf, 18); 657328de1c5SRishi Gupta } 658328de1c5SRishi Gupta 659328de1c5SRishi Gupta static int mcp_gpio_direction_input(struct gpio_chip *gc, 660328de1c5SRishi Gupta unsigned int offset) 661328de1c5SRishi Gupta { 662328de1c5SRishi Gupta int ret; 663328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 664328de1c5SRishi Gupta 665328de1c5SRishi Gupta mutex_lock(&mcp->lock); 666567b8e9fSLars Povlsen ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN); 667328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 668328de1c5SRishi Gupta 669328de1c5SRishi Gupta return ret; 670328de1c5SRishi Gupta } 671328de1c5SRishi Gupta 672328de1c5SRishi Gupta static int mcp_gpio_direction_output(struct gpio_chip *gc, 673328de1c5SRishi Gupta unsigned int offset, int value) 674328de1c5SRishi Gupta { 675328de1c5SRishi Gupta int ret; 676328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 677328de1c5SRishi Gupta 678328de1c5SRishi Gupta mutex_lock(&mcp->lock); 679567b8e9fSLars Povlsen ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT); 680328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 681328de1c5SRishi Gupta 682328de1c5SRishi Gupta /* Can't configure as output, bailout early */ 683328de1c5SRishi Gupta if (ret) 684328de1c5SRishi Gupta return ret; 685328de1c5SRishi Gupta 686328de1c5SRishi Gupta mcp_gpio_set(gc, offset, value); 687328de1c5SRishi Gupta 688328de1c5SRishi Gupta return 0; 689328de1c5SRishi Gupta } 690328de1c5SRishi Gupta 691328de1c5SRishi Gupta static int mcp_gpio_get_direction(struct gpio_chip *gc, 692328de1c5SRishi Gupta unsigned int offset) 693328de1c5SRishi Gupta { 694328de1c5SRishi Gupta int ret; 695328de1c5SRishi Gupta struct mcp2221 *mcp = gpiochip_get_data(gc); 696328de1c5SRishi Gupta 697328de1c5SRishi Gupta mcp->txbuf[0] = MCP2221_GPIO_GET; 698328de1c5SRishi Gupta 699ca6961d8SLouis Morhet mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]); 700328de1c5SRishi Gupta 701328de1c5SRishi Gupta mutex_lock(&mcp->lock); 702328de1c5SRishi Gupta ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 703328de1c5SRishi Gupta mutex_unlock(&mcp->lock); 704328de1c5SRishi Gupta 705328de1c5SRishi Gupta if (ret) 706328de1c5SRishi Gupta return ret; 707328de1c5SRishi Gupta 708567b8e9fSLars Povlsen if (mcp->gpio_dir == MCP2221_DIR_IN) 709328de1c5SRishi Gupta return GPIO_LINE_DIRECTION_IN; 710328de1c5SRishi Gupta 711328de1c5SRishi Gupta return GPIO_LINE_DIRECTION_OUT; 712328de1c5SRishi Gupta } 7133d74c9ecSMatt Ranostay #endif 714328de1c5SRishi Gupta 71567a95c21SRishi Gupta /* Gives current state of i2c engine inside mcp2221 */ 71667a95c21SRishi Gupta static int mcp_get_i2c_eng_state(struct mcp2221 *mcp, 71767a95c21SRishi Gupta u8 *data, u8 idx) 71867a95c21SRishi Gupta { 71967a95c21SRishi Gupta int ret; 72067a95c21SRishi Gupta 72167a95c21SRishi Gupta switch (data[idx]) { 72267a95c21SRishi Gupta case MCP2221_I2C_WRADDRL_NACK: 72367a95c21SRishi Gupta case MCP2221_I2C_WRADDRL_SEND: 72467a95c21SRishi Gupta ret = -ENXIO; 72567a95c21SRishi Gupta break; 72667a95c21SRishi Gupta case MCP2221_I2C_START_TOUT: 72767a95c21SRishi Gupta case MCP2221_I2C_STOP_TOUT: 72867a95c21SRishi Gupta case MCP2221_I2C_WRADDRL_TOUT: 72967a95c21SRishi Gupta case MCP2221_I2C_WRDATA_TOUT: 73067a95c21SRishi Gupta ret = -ETIMEDOUT; 73167a95c21SRishi Gupta break; 73267a95c21SRishi Gupta case MCP2221_I2C_ENG_BUSY: 73367a95c21SRishi Gupta ret = -EAGAIN; 73467a95c21SRishi Gupta break; 73567a95c21SRishi Gupta case MCP2221_SUCCESS: 73667a95c21SRishi Gupta ret = 0x00; 73767a95c21SRishi Gupta break; 73867a95c21SRishi Gupta default: 73967a95c21SRishi Gupta ret = -EIO; 74067a95c21SRishi Gupta } 74167a95c21SRishi Gupta 74267a95c21SRishi Gupta return ret; 74367a95c21SRishi Gupta } 74467a95c21SRishi Gupta 74567a95c21SRishi Gupta /* 74667a95c21SRishi Gupta * MCP2221 uses interrupt endpoint for input reports. This function 74767a95c21SRishi Gupta * is called by HID layer when it receives i/p report from mcp2221, 74867a95c21SRishi Gupta * which is actually a response to the previously sent command. 74967a95c21SRishi Gupta * 75067a95c21SRishi Gupta * MCP2221A firmware specific return codes are parsed and 0 or 75167a95c21SRishi Gupta * appropriate negative error code is returned. Delayed response 75267a95c21SRishi Gupta * results in timeout error and stray reponses results in -EIO. 75367a95c21SRishi Gupta */ 75467a95c21SRishi Gupta static int mcp2221_raw_event(struct hid_device *hdev, 75567a95c21SRishi Gupta struct hid_report *report, u8 *data, int size) 75667a95c21SRishi Gupta { 757daf405c8SJiri Kosina u8 *buf; 75867a95c21SRishi Gupta struct mcp2221 *mcp = hid_get_drvdata(hdev); 75967a95c21SRishi Gupta 76067a95c21SRishi Gupta switch (data[0]) { 76167a95c21SRishi Gupta 76267a95c21SRishi Gupta case MCP2221_I2C_WR_DATA: 76367a95c21SRishi Gupta case MCP2221_I2C_WR_NO_STOP: 76467a95c21SRishi Gupta case MCP2221_I2C_RD_DATA: 76567a95c21SRishi Gupta case MCP2221_I2C_RD_RPT_START: 76667a95c21SRishi Gupta switch (data[1]) { 76767a95c21SRishi Gupta case MCP2221_SUCCESS: 76867a95c21SRishi Gupta mcp->status = 0; 76967a95c21SRishi Gupta break; 77067a95c21SRishi Gupta default: 77167a95c21SRishi Gupta mcp->status = mcp_get_i2c_eng_state(mcp, data, 2); 77267a95c21SRishi Gupta } 77367a95c21SRishi Gupta complete(&mcp->wait_in_report); 77467a95c21SRishi Gupta break; 77567a95c21SRishi Gupta 77667a95c21SRishi Gupta case MCP2221_I2C_PARAM_OR_STATUS: 77767a95c21SRishi Gupta switch (data[1]) { 77867a95c21SRishi Gupta case MCP2221_SUCCESS: 77967a95c21SRishi Gupta if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) && 78067a95c21SRishi Gupta (data[3] != MCP2221_I2C_SET_SPEED)) { 78167a95c21SRishi Gupta mcp->status = -EAGAIN; 78267a95c21SRishi Gupta break; 78367a95c21SRishi Gupta } 78467a95c21SRishi Gupta if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) { 78567a95c21SRishi Gupta mcp->status = -ENXIO; 78667a95c21SRishi Gupta break; 78767a95c21SRishi Gupta } 78867a95c21SRishi Gupta mcp->status = mcp_get_i2c_eng_state(mcp, data, 8); 789960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 790960f9df7SMatt Ranostay memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values)); 791960f9df7SMatt Ranostay #endif 79267a95c21SRishi Gupta break; 79367a95c21SRishi Gupta default: 79467a95c21SRishi Gupta mcp->status = -EIO; 79567a95c21SRishi Gupta } 79667a95c21SRishi Gupta complete(&mcp->wait_in_report); 79767a95c21SRishi Gupta break; 79867a95c21SRishi Gupta 79967a95c21SRishi Gupta case MCP2221_I2C_GET_DATA: 80067a95c21SRishi Gupta switch (data[1]) { 80167a95c21SRishi Gupta case MCP2221_SUCCESS: 80267a95c21SRishi Gupta if (data[2] == MCP2221_I2C_ADDR_NACK) { 80367a95c21SRishi Gupta mcp->status = -ENXIO; 80467a95c21SRishi Gupta break; 80567a95c21SRishi Gupta } 80667a95c21SRishi Gupta if (!mcp_get_i2c_eng_state(mcp, data, 2) 80767a95c21SRishi Gupta && (data[3] == 0)) { 80867a95c21SRishi Gupta mcp->status = 0; 80967a95c21SRishi Gupta break; 81067a95c21SRishi Gupta } 81167a95c21SRishi Gupta if (data[3] == 127) { 81267a95c21SRishi Gupta mcp->status = -EIO; 81367a95c21SRishi Gupta break; 81467a95c21SRishi Gupta } 81526824686SHamish Martin if (data[2] == MCP2221_I2C_READ_COMPL || 81626824686SHamish Martin data[2] == MCP2221_I2C_READ_PARTIAL) { 81767a95c21SRishi Gupta buf = mcp->rxbuf; 81867a95c21SRishi Gupta memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]); 81967a95c21SRishi Gupta mcp->rxbuf_idx = mcp->rxbuf_idx + data[3]; 82067a95c21SRishi Gupta mcp->status = 0; 82167a95c21SRishi Gupta break; 82267a95c21SRishi Gupta } 82367a95c21SRishi Gupta mcp->status = -EIO; 82467a95c21SRishi Gupta break; 82567a95c21SRishi Gupta default: 82667a95c21SRishi Gupta mcp->status = -EIO; 82767a95c21SRishi Gupta } 82867a95c21SRishi Gupta complete(&mcp->wait_in_report); 82967a95c21SRishi Gupta break; 83067a95c21SRishi Gupta 831328de1c5SRishi Gupta case MCP2221_GPIO_GET: 832328de1c5SRishi Gupta switch (data[1]) { 833328de1c5SRishi Gupta case MCP2221_SUCCESS: 834328de1c5SRishi Gupta if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) || 835328de1c5SRishi Gupta (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) { 836328de1c5SRishi Gupta mcp->status = -ENOENT; 837328de1c5SRishi Gupta } else { 838328de1c5SRishi Gupta mcp->status = !!data[mcp->gp_idx]; 839567b8e9fSLars Povlsen mcp->gpio_dir = data[mcp->gp_idx + 1]; 840328de1c5SRishi Gupta } 841328de1c5SRishi Gupta break; 842328de1c5SRishi Gupta default: 843328de1c5SRishi Gupta mcp->status = -EAGAIN; 844328de1c5SRishi Gupta } 845328de1c5SRishi Gupta complete(&mcp->wait_in_report); 846328de1c5SRishi Gupta break; 847328de1c5SRishi Gupta 848328de1c5SRishi Gupta case MCP2221_GPIO_SET: 849328de1c5SRishi Gupta switch (data[1]) { 850328de1c5SRishi Gupta case MCP2221_SUCCESS: 851328de1c5SRishi Gupta if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) || 852328de1c5SRishi Gupta (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) { 853328de1c5SRishi Gupta mcp->status = -ENOENT; 854328de1c5SRishi Gupta } else { 855328de1c5SRishi Gupta mcp->status = 0; 856328de1c5SRishi Gupta } 857328de1c5SRishi Gupta break; 858328de1c5SRishi Gupta default: 859328de1c5SRishi Gupta mcp->status = -EAGAIN; 860328de1c5SRishi Gupta } 861328de1c5SRishi Gupta complete(&mcp->wait_in_report); 862328de1c5SRishi Gupta break; 863328de1c5SRishi Gupta 864960f9df7SMatt Ranostay case MCP2221_SET_SRAM_SETTINGS: 865960f9df7SMatt Ranostay switch (data[1]) { 866960f9df7SMatt Ranostay case MCP2221_SUCCESS: 867960f9df7SMatt Ranostay mcp->status = 0; 868960f9df7SMatt Ranostay break; 869960f9df7SMatt Ranostay default: 870960f9df7SMatt Ranostay mcp->status = -EAGAIN; 871960f9df7SMatt Ranostay } 872960f9df7SMatt Ranostay complete(&mcp->wait_in_report); 873960f9df7SMatt Ranostay break; 874960f9df7SMatt Ranostay 875960f9df7SMatt Ranostay case MCP2221_GET_SRAM_SETTINGS: 876960f9df7SMatt Ranostay switch (data[1]) { 877960f9df7SMatt Ranostay case MCP2221_SUCCESS: 878960f9df7SMatt Ranostay memcpy(&mcp->mode, &data[22], 4); 879960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 880960f9df7SMatt Ranostay mcp->dac_value = data[6] & GENMASK(4, 0); 881960f9df7SMatt Ranostay #endif 882960f9df7SMatt Ranostay mcp->status = 0; 883960f9df7SMatt Ranostay break; 884960f9df7SMatt Ranostay default: 885960f9df7SMatt Ranostay mcp->status = -EAGAIN; 886960f9df7SMatt Ranostay } 887960f9df7SMatt Ranostay complete(&mcp->wait_in_report); 888960f9df7SMatt Ranostay break; 889960f9df7SMatt Ranostay 890960f9df7SMatt Ranostay case MCP2221_READ_FLASH_DATA: 891960f9df7SMatt Ranostay switch (data[1]) { 892960f9df7SMatt Ranostay case MCP2221_SUCCESS: 893960f9df7SMatt Ranostay mcp->status = 0; 894960f9df7SMatt Ranostay 895960f9df7SMatt Ranostay /* Only handles CHIP SETTINGS subpage currently */ 896960f9df7SMatt Ranostay if (mcp->txbuf[1] != 0) { 897960f9df7SMatt Ranostay mcp->status = -EIO; 898960f9df7SMatt Ranostay break; 899960f9df7SMatt Ranostay } 900960f9df7SMatt Ranostay 901960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 902daf405c8SJiri Kosina { 903daf405c8SJiri Kosina u8 tmp; 904960f9df7SMatt Ranostay /* DAC scale value */ 905960f9df7SMatt Ranostay tmp = FIELD_GET(GENMASK(7, 6), data[6]); 906960f9df7SMatt Ranostay if ((data[6] & BIT(5)) && tmp) 907960f9df7SMatt Ranostay mcp->dac_scale = tmp + 4; 908960f9df7SMatt Ranostay else 909960f9df7SMatt Ranostay mcp->dac_scale = 5; 910960f9df7SMatt Ranostay 911960f9df7SMatt Ranostay /* ADC scale value */ 912960f9df7SMatt Ranostay tmp = FIELD_GET(GENMASK(4, 3), data[7]); 913960f9df7SMatt Ranostay if ((data[7] & BIT(2)) && tmp) 914960f9df7SMatt Ranostay mcp->adc_scale = tmp - 1; 915960f9df7SMatt Ranostay else 916960f9df7SMatt Ranostay mcp->adc_scale = 0; 917daf405c8SJiri Kosina } 918960f9df7SMatt Ranostay #endif 919960f9df7SMatt Ranostay 920960f9df7SMatt Ranostay break; 921960f9df7SMatt Ranostay default: 922960f9df7SMatt Ranostay mcp->status = -EAGAIN; 923960f9df7SMatt Ranostay } 924960f9df7SMatt Ranostay complete(&mcp->wait_in_report); 925960f9df7SMatt Ranostay break; 926960f9df7SMatt Ranostay 92767a95c21SRishi Gupta default: 92867a95c21SRishi Gupta mcp->status = -EIO; 92967a95c21SRishi Gupta complete(&mcp->wait_in_report); 93067a95c21SRishi Gupta } 93167a95c21SRishi Gupta 93267a95c21SRishi Gupta return 1; 93367a95c21SRishi Gupta } 93467a95c21SRishi Gupta 935deb3b88bSMatt Ranostay /* Device resource managed function for HID unregistration */ 936deb3b88bSMatt Ranostay static void mcp2221_hid_unregister(void *ptr) 937deb3b88bSMatt Ranostay { 938deb3b88bSMatt Ranostay struct hid_device *hdev = ptr; 939deb3b88bSMatt Ranostay 940deb3b88bSMatt Ranostay hid_hw_close(hdev); 941deb3b88bSMatt Ranostay hid_hw_stop(hdev); 942deb3b88bSMatt Ranostay } 943deb3b88bSMatt Ranostay 944deb3b88bSMatt Ranostay /* This is needed to be sure hid_hw_stop() isn't called twice by the subsystem */ 945deb3b88bSMatt Ranostay static void mcp2221_remove(struct hid_device *hdev) 946deb3b88bSMatt Ranostay { 9473cba9cfcSAbdelrahman Morsy #if IS_REACHABLE(CONFIG_IIO) 94847e91fdfSBenjamin Tissoires struct mcp2221 *mcp = hid_get_drvdata(hdev); 94947e91fdfSBenjamin Tissoires 95047e91fdfSBenjamin Tissoires cancel_delayed_work_sync(&mcp->init_work); 9513cba9cfcSAbdelrahman Morsy #endif 952deb3b88bSMatt Ranostay } 953deb3b88bSMatt Ranostay 954960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 955960f9df7SMatt Ranostay static int mcp2221_read_raw(struct iio_dev *indio_dev, 956960f9df7SMatt Ranostay struct iio_chan_spec const *channel, int *val, 957960f9df7SMatt Ranostay int *val2, long mask) 958960f9df7SMatt Ranostay { 959960f9df7SMatt Ranostay struct mcp2221_iio *priv = iio_priv(indio_dev); 960960f9df7SMatt Ranostay struct mcp2221 *mcp = priv->mcp; 961960f9df7SMatt Ranostay int ret; 962960f9df7SMatt Ranostay 963960f9df7SMatt Ranostay if (mask == IIO_CHAN_INFO_SCALE) { 964960f9df7SMatt Ranostay if (channel->output) 965960f9df7SMatt Ranostay *val = 1 << mcp->dac_scale; 966960f9df7SMatt Ranostay else 967960f9df7SMatt Ranostay *val = 1 << mcp->adc_scale; 968960f9df7SMatt Ranostay 969960f9df7SMatt Ranostay return IIO_VAL_INT; 970960f9df7SMatt Ranostay } 971960f9df7SMatt Ranostay 972960f9df7SMatt Ranostay mutex_lock(&mcp->lock); 973960f9df7SMatt Ranostay 974960f9df7SMatt Ranostay if (channel->output) { 975960f9df7SMatt Ranostay *val = mcp->dac_value; 976960f9df7SMatt Ranostay ret = IIO_VAL_INT; 977960f9df7SMatt Ranostay } else { 978960f9df7SMatt Ranostay /* Read ADC values */ 979960f9df7SMatt Ranostay ret = mcp_chk_last_cmd_status(mcp); 980960f9df7SMatt Ranostay 981960f9df7SMatt Ranostay if (!ret) { 982e91fc483SMatt Ranostay *val = le16_to_cpu((__force __le16) mcp->adc_values[channel->address]); 983960f9df7SMatt Ranostay if (*val >= BIT(10)) 984960f9df7SMatt Ranostay ret = -EINVAL; 985960f9df7SMatt Ranostay else 986960f9df7SMatt Ranostay ret = IIO_VAL_INT; 987960f9df7SMatt Ranostay } 988960f9df7SMatt Ranostay } 989960f9df7SMatt Ranostay 990960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 991960f9df7SMatt Ranostay 992960f9df7SMatt Ranostay return ret; 993960f9df7SMatt Ranostay } 994960f9df7SMatt Ranostay 995960f9df7SMatt Ranostay static int mcp2221_write_raw(struct iio_dev *indio_dev, 996960f9df7SMatt Ranostay struct iio_chan_spec const *chan, 997960f9df7SMatt Ranostay int val, int val2, long mask) 998960f9df7SMatt Ranostay { 999960f9df7SMatt Ranostay struct mcp2221_iio *priv = iio_priv(indio_dev); 1000960f9df7SMatt Ranostay struct mcp2221 *mcp = priv->mcp; 1001960f9df7SMatt Ranostay int ret; 1002960f9df7SMatt Ranostay 1003960f9df7SMatt Ranostay if (val < 0 || val >= BIT(5)) 1004960f9df7SMatt Ranostay return -EINVAL; 1005960f9df7SMatt Ranostay 1006960f9df7SMatt Ranostay mutex_lock(&mcp->lock); 1007960f9df7SMatt Ranostay 1008960f9df7SMatt Ranostay memset(mcp->txbuf, 0, 12); 1009960f9df7SMatt Ranostay mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS; 1010960f9df7SMatt Ranostay mcp->txbuf[4] = BIT(7) | val; 1011960f9df7SMatt Ranostay 1012960f9df7SMatt Ranostay ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12); 1013960f9df7SMatt Ranostay if (!ret) 1014960f9df7SMatt Ranostay mcp->dac_value = val; 1015960f9df7SMatt Ranostay 1016960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 1017960f9df7SMatt Ranostay 1018960f9df7SMatt Ranostay return ret; 1019960f9df7SMatt Ranostay } 1020960f9df7SMatt Ranostay 1021960f9df7SMatt Ranostay static const struct iio_info mcp2221_info = { 1022960f9df7SMatt Ranostay .read_raw = &mcp2221_read_raw, 1023960f9df7SMatt Ranostay .write_raw = &mcp2221_write_raw, 1024960f9df7SMatt Ranostay }; 1025960f9df7SMatt Ranostay 1026960f9df7SMatt Ranostay static int mcp_iio_channels(struct mcp2221 *mcp) 1027960f9df7SMatt Ranostay { 1028960f9df7SMatt Ranostay int idx, cnt = 0; 1029960f9df7SMatt Ranostay bool dac_created = false; 1030960f9df7SMatt Ranostay 1031960f9df7SMatt Ranostay /* GP0 doesn't have ADC/DAC alternative function */ 1032960f9df7SMatt Ranostay for (idx = 1; idx < MCP_NGPIO; idx++) { 1033960f9df7SMatt Ranostay struct iio_chan_spec *chan = &mcp->iio_channels[cnt]; 1034960f9df7SMatt Ranostay 1035960f9df7SMatt Ranostay switch (mcp->mode[idx]) { 1036960f9df7SMatt Ranostay case 2: 1037960f9df7SMatt Ranostay chan->address = idx - 1; 1038960f9df7SMatt Ranostay chan->channel = cnt++; 1039960f9df7SMatt Ranostay break; 1040960f9df7SMatt Ranostay case 3: 1041960f9df7SMatt Ranostay /* GP1 doesn't have DAC alternative function */ 1042960f9df7SMatt Ranostay if (idx == 1 || dac_created) 1043960f9df7SMatt Ranostay continue; 1044960f9df7SMatt Ranostay /* DAC1 and DAC2 outputs are connected to the same DAC */ 1045960f9df7SMatt Ranostay dac_created = true; 1046960f9df7SMatt Ranostay chan->output = 1; 1047960f9df7SMatt Ranostay cnt++; 1048960f9df7SMatt Ranostay break; 1049960f9df7SMatt Ranostay default: 1050960f9df7SMatt Ranostay continue; 1051*ad1ff1f2SChen Ni } 1052960f9df7SMatt Ranostay 1053960f9df7SMatt Ranostay chan->type = IIO_VOLTAGE; 1054960f9df7SMatt Ranostay chan->indexed = 1; 1055960f9df7SMatt Ranostay chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 1056960f9df7SMatt Ranostay chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); 1057960f9df7SMatt Ranostay chan->scan_index = -1; 1058960f9df7SMatt Ranostay } 1059960f9df7SMatt Ranostay 1060960f9df7SMatt Ranostay return cnt; 1061960f9df7SMatt Ranostay } 1062960f9df7SMatt Ranostay 1063960f9df7SMatt Ranostay static void mcp_init_work(struct work_struct *work) 1064960f9df7SMatt Ranostay { 1065960f9df7SMatt Ranostay struct iio_dev *indio_dev; 1066960f9df7SMatt Ranostay struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work); 1067960f9df7SMatt Ranostay struct mcp2221_iio *data; 1068960f9df7SMatt Ranostay static int retries = 5; 1069960f9df7SMatt Ranostay int ret, num_channels; 1070960f9df7SMatt Ranostay 1071960f9df7SMatt Ranostay hid_hw_power(mcp->hdev, PM_HINT_FULLON); 1072960f9df7SMatt Ranostay mutex_lock(&mcp->lock); 1073960f9df7SMatt Ranostay 1074960f9df7SMatt Ranostay mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS; 1075960f9df7SMatt Ranostay ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 1076960f9df7SMatt Ranostay 1077960f9df7SMatt Ranostay if (ret == -EAGAIN) 1078960f9df7SMatt Ranostay goto reschedule_task; 1079960f9df7SMatt Ranostay 1080960f9df7SMatt Ranostay num_channels = mcp_iio_channels(mcp); 1081960f9df7SMatt Ranostay if (!num_channels) 1082960f9df7SMatt Ranostay goto unlock; 1083960f9df7SMatt Ranostay 1084960f9df7SMatt Ranostay mcp->txbuf[0] = MCP2221_READ_FLASH_DATA; 1085960f9df7SMatt Ranostay mcp->txbuf[1] = 0; 1086960f9df7SMatt Ranostay ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2); 1087960f9df7SMatt Ranostay 1088960f9df7SMatt Ranostay if (ret == -EAGAIN) 1089960f9df7SMatt Ranostay goto reschedule_task; 1090960f9df7SMatt Ranostay 1091960f9df7SMatt Ranostay indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data)); 1092960f9df7SMatt Ranostay if (!indio_dev) 1093960f9df7SMatt Ranostay goto unlock; 1094960f9df7SMatt Ranostay 1095960f9df7SMatt Ranostay data = iio_priv(indio_dev); 1096960f9df7SMatt Ranostay data->mcp = mcp; 1097960f9df7SMatt Ranostay 1098960f9df7SMatt Ranostay indio_dev->name = "mcp2221"; 1099960f9df7SMatt Ranostay indio_dev->modes = INDIO_DIRECT_MODE; 1100960f9df7SMatt Ranostay indio_dev->info = &mcp2221_info; 1101960f9df7SMatt Ranostay indio_dev->channels = mcp->iio_channels; 1102960f9df7SMatt Ranostay indio_dev->num_channels = num_channels; 1103960f9df7SMatt Ranostay 1104960f9df7SMatt Ranostay devm_iio_device_register(&mcp->hdev->dev, indio_dev); 1105960f9df7SMatt Ranostay 1106960f9df7SMatt Ranostay unlock: 1107960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 1108960f9df7SMatt Ranostay hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 1109960f9df7SMatt Ranostay 1110960f9df7SMatt Ranostay return; 1111960f9df7SMatt Ranostay 1112960f9df7SMatt Ranostay reschedule_task: 1113960f9df7SMatt Ranostay mutex_unlock(&mcp->lock); 1114960f9df7SMatt Ranostay hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 1115960f9df7SMatt Ranostay 1116960f9df7SMatt Ranostay if (!retries--) 1117960f9df7SMatt Ranostay return; 1118960f9df7SMatt Ranostay 1119960f9df7SMatt Ranostay /* Device is not ready to read SRAM or FLASH data, try again */ 1120960f9df7SMatt Ranostay schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100)); 1121960f9df7SMatt Ranostay } 1122960f9df7SMatt Ranostay #endif 1123960f9df7SMatt Ranostay 112467a95c21SRishi Gupta static int mcp2221_probe(struct hid_device *hdev, 112567a95c21SRishi Gupta const struct hid_device_id *id) 112667a95c21SRishi Gupta { 112767a95c21SRishi Gupta int ret; 112867a95c21SRishi Gupta struct mcp2221 *mcp; 112967a95c21SRishi Gupta 113067a95c21SRishi Gupta mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL); 113167a95c21SRishi Gupta if (!mcp) 113267a95c21SRishi Gupta return -ENOMEM; 113367a95c21SRishi Gupta 113467a95c21SRishi Gupta ret = hid_parse(hdev); 113567a95c21SRishi Gupta if (ret) { 113667a95c21SRishi Gupta hid_err(hdev, "can't parse reports\n"); 113767a95c21SRishi Gupta return ret; 113867a95c21SRishi Gupta } 113967a95c21SRishi Gupta 114067c90d14SEnrik Berkhan /* 114167c90d14SEnrik Berkhan * This driver uses the .raw_event callback and therefore does not need any 114267c90d14SEnrik Berkhan * HID_CONNECT_xxx flags. 114367c90d14SEnrik Berkhan */ 114467c90d14SEnrik Berkhan ret = hid_hw_start(hdev, 0); 114567a95c21SRishi Gupta if (ret) { 114667a95c21SRishi Gupta hid_err(hdev, "can't start hardware\n"); 114767a95c21SRishi Gupta return ret; 114867a95c21SRishi Gupta } 114967a95c21SRishi Gupta 115067c90d14SEnrik Berkhan hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8, 115167c90d14SEnrik Berkhan hdev->version & 0xff, hdev->name, hdev->phys); 115267c90d14SEnrik Berkhan 115367a95c21SRishi Gupta ret = hid_hw_open(hdev); 115467a95c21SRishi Gupta if (ret) { 115567a95c21SRishi Gupta hid_err(hdev, "can't open device\n"); 1156deb3b88bSMatt Ranostay hid_hw_stop(hdev); 1157deb3b88bSMatt Ranostay return ret; 115867a95c21SRishi Gupta } 115967a95c21SRishi Gupta 116067a95c21SRishi Gupta mutex_init(&mcp->lock); 116167a95c21SRishi Gupta init_completion(&mcp->wait_in_report); 116267a95c21SRishi Gupta hid_set_drvdata(hdev, mcp); 116367a95c21SRishi Gupta mcp->hdev = hdev; 116467a95c21SRishi Gupta 1165deb3b88bSMatt Ranostay ret = devm_add_action_or_reset(&hdev->dev, mcp2221_hid_unregister, hdev); 1166deb3b88bSMatt Ranostay if (ret) 1167deb3b88bSMatt Ranostay return ret; 1168deb3b88bSMatt Ranostay 116973ce9f1fSHamish Martin hid_device_io_start(hdev); 117073ce9f1fSHamish Martin 117167a95c21SRishi Gupta /* Set I2C bus clock diviser */ 117267a95c21SRishi Gupta if (i2c_clk_freq > 400) 117367a95c21SRishi Gupta i2c_clk_freq = 400; 117467a95c21SRishi Gupta if (i2c_clk_freq < 50) 117567a95c21SRishi Gupta i2c_clk_freq = 50; 117667a95c21SRishi Gupta mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3; 117702a46753SHamish Martin ret = mcp_set_i2c_speed(mcp); 117802a46753SHamish Martin if (ret) { 117902a46753SHamish Martin hid_err(hdev, "can't set i2c speed: %d\n", ret); 118002a46753SHamish Martin return ret; 118102a46753SHamish Martin } 118267a95c21SRishi Gupta 118367a95c21SRishi Gupta mcp->adapter.owner = THIS_MODULE; 118467a95c21SRishi Gupta mcp->adapter.class = I2C_CLASS_HWMON; 118567a95c21SRishi Gupta mcp->adapter.algo = &mcp_i2c_algo; 118667a95c21SRishi Gupta mcp->adapter.retries = 1; 118767a95c21SRishi Gupta mcp->adapter.dev.parent = &hdev->dev; 1188d9786159SHamish Martin ACPI_COMPANION_SET(&mcp->adapter.dev, ACPI_COMPANION(hdev->dev.parent)); 118967a95c21SRishi Gupta snprintf(mcp->adapter.name, sizeof(mcp->adapter.name), 119067c90d14SEnrik Berkhan "MCP2221 usb-i2c bridge"); 119167a95c21SRishi Gupta 1192f2d4a583SHamish Martin i2c_set_adapdata(&mcp->adapter, mcp); 1193deb3b88bSMatt Ranostay ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter); 119467a95c21SRishi Gupta if (ret) { 119567a95c21SRishi Gupta hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret); 1196deb3b88bSMatt Ranostay return ret; 119767a95c21SRishi Gupta } 119867a95c21SRishi Gupta 11993d74c9ecSMatt Ranostay #if IS_REACHABLE(CONFIG_GPIOLIB) 1200328de1c5SRishi Gupta /* Setup GPIO chip */ 1201328de1c5SRishi Gupta mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL); 1202deb3b88bSMatt Ranostay if (!mcp->gc) 1203deb3b88bSMatt Ranostay return -ENOMEM; 1204328de1c5SRishi Gupta 1205328de1c5SRishi Gupta mcp->gc->label = "mcp2221_gpio"; 1206328de1c5SRishi Gupta mcp->gc->direction_input = mcp_gpio_direction_input; 1207328de1c5SRishi Gupta mcp->gc->direction_output = mcp_gpio_direction_output; 1208328de1c5SRishi Gupta mcp->gc->get_direction = mcp_gpio_get_direction; 1209328de1c5SRishi Gupta mcp->gc->set = mcp_gpio_set; 1210328de1c5SRishi Gupta mcp->gc->get = mcp_gpio_get; 1211567b8e9fSLars Povlsen mcp->gc->ngpio = MCP_NGPIO; 1212328de1c5SRishi Gupta mcp->gc->base = -1; 1213328de1c5SRishi Gupta mcp->gc->can_sleep = 1; 1214328de1c5SRishi Gupta mcp->gc->parent = &hdev->dev; 1215328de1c5SRishi Gupta 1216328de1c5SRishi Gupta ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp); 1217328de1c5SRishi Gupta if (ret) 1218deb3b88bSMatt Ranostay return ret; 1219ea418b35SMatt Ranostay #endif 1220328de1c5SRishi Gupta 1221960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO) 1222960f9df7SMatt Ranostay INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work); 1223960f9df7SMatt Ranostay schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100)); 1224960f9df7SMatt Ranostay #endif 1225960f9df7SMatt Ranostay 122667a95c21SRishi Gupta return 0; 122767a95c21SRishi Gupta } 122867a95c21SRishi Gupta 122967a95c21SRishi Gupta static const struct hid_device_id mcp2221_devices[] = { 123067a95c21SRishi Gupta { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) }, 123167a95c21SRishi Gupta { } 123267a95c21SRishi Gupta }; 123367a95c21SRishi Gupta MODULE_DEVICE_TABLE(hid, mcp2221_devices); 123467a95c21SRishi Gupta 123567a95c21SRishi Gupta static struct hid_driver mcp2221_driver = { 123667a95c21SRishi Gupta .name = "mcp2221", 123767a95c21SRishi Gupta .id_table = mcp2221_devices, 123867a95c21SRishi Gupta .probe = mcp2221_probe, 123967a95c21SRishi Gupta .remove = mcp2221_remove, 124067a95c21SRishi Gupta .raw_event = mcp2221_raw_event, 124167a95c21SRishi Gupta }; 124267a95c21SRishi Gupta 124367a95c21SRishi Gupta /* Register with HID core */ 124467a95c21SRishi Gupta module_hid_driver(mcp2221_driver); 124567a95c21SRishi Gupta 124667a95c21SRishi Gupta MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>"); 124767a95c21SRishi Gupta MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge"); 124867a95c21SRishi Gupta MODULE_LICENSE("GPL v2"); 1249