xref: /linux/drivers/hid/hid-mcp2221.c (revision 02a46753601a24e1673d9c28173121055e8e6cc9)
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 
190*02a46753SHamish Martin /* Check if the last command succeeded or failed and return the result.
191*02a46753SHamish Martin  * If the command did fail, cancel that command which will free the i2c bus.
192*02a46753SHamish Martin  */
193*02a46753SHamish Martin static int mcp_chk_last_cmd_status_free_bus(struct mcp2221 *mcp)
194*02a46753SHamish Martin {
195*02a46753SHamish Martin 	int ret;
196*02a46753SHamish Martin 
197*02a46753SHamish Martin 	ret = mcp_chk_last_cmd_status(mcp);
198*02a46753SHamish Martin 	if (ret) {
199*02a46753SHamish Martin 		/* The last command was a failure.
200*02a46753SHamish Martin 		 * Send a cancel which will also free the bus.
201*02a46753SHamish Martin 		 */
202*02a46753SHamish Martin 		usleep_range(980, 1000);
203*02a46753SHamish Martin 		mcp_cancel_last_cmd(mcp);
204*02a46753SHamish Martin 	}
205*02a46753SHamish Martin 
206*02a46753SHamish Martin 	return ret;
207*02a46753SHamish Martin }
208*02a46753SHamish Martin 
20967a95c21SRishi Gupta static int mcp_set_i2c_speed(struct mcp2221 *mcp)
21067a95c21SRishi Gupta {
21167a95c21SRishi Gupta 	int ret;
21267a95c21SRishi Gupta 
21367a95c21SRishi Gupta 	memset(mcp->txbuf, 0, 8);
21467a95c21SRishi Gupta 	mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
21567a95c21SRishi Gupta 	mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
21667a95c21SRishi Gupta 	mcp->txbuf[4] = mcp->cur_i2c_clk_div;
21767a95c21SRishi Gupta 
21867a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
21967a95c21SRishi Gupta 	if (ret) {
22067a95c21SRishi Gupta 		/* Small delay is needed here */
22167a95c21SRishi Gupta 		usleep_range(980, 1000);
22267a95c21SRishi Gupta 		mcp_cancel_last_cmd(mcp);
22367a95c21SRishi Gupta 	}
22467a95c21SRishi Gupta 
22567a95c21SRishi Gupta 	return 0;
22667a95c21SRishi Gupta }
22767a95c21SRishi Gupta 
22867a95c21SRishi Gupta /*
22967a95c21SRishi Gupta  * An output report can contain minimum 1 and maximum 60 user data
23067a95c21SRishi Gupta  * bytes. If the number of data bytes is more then 60, we send it
23167a95c21SRishi Gupta  * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
23267a95c21SRishi Gupta  * bytes. Total number of bytes is informed in very first report to
23367a95c21SRishi Gupta  * mcp2221, from that point onwards it first collect all the data
23467a95c21SRishi Gupta  * from host and then send to i2c slave device.
23567a95c21SRishi Gupta  */
23667a95c21SRishi Gupta static int mcp_i2c_write(struct mcp2221 *mcp,
23767a95c21SRishi Gupta 				struct i2c_msg *msg, int type, u8 last_status)
23867a95c21SRishi Gupta {
23967a95c21SRishi Gupta 	int ret, len, idx, sent;
24067a95c21SRishi Gupta 
24167a95c21SRishi Gupta 	idx = 0;
24267a95c21SRishi Gupta 	sent  = 0;
24367a95c21SRishi Gupta 	if (msg->len < 60)
24467a95c21SRishi Gupta 		len = msg->len;
24567a95c21SRishi Gupta 	else
24667a95c21SRishi Gupta 		len = 60;
24767a95c21SRishi Gupta 
24867a95c21SRishi Gupta 	do {
24967a95c21SRishi Gupta 		mcp->txbuf[0] = type;
25067a95c21SRishi Gupta 		mcp->txbuf[1] = msg->len & 0xff;
25167a95c21SRishi Gupta 		mcp->txbuf[2] = msg->len >> 8;
25267a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(msg->addr << 1);
25367a95c21SRishi Gupta 
25467a95c21SRishi Gupta 		memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
25567a95c21SRishi Gupta 
25667a95c21SRishi Gupta 		ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
25767a95c21SRishi Gupta 		if (ret)
25867a95c21SRishi Gupta 			return ret;
25967a95c21SRishi Gupta 
26067a95c21SRishi Gupta 		usleep_range(980, 1000);
26167a95c21SRishi Gupta 
26267a95c21SRishi Gupta 		if (last_status) {
263*02a46753SHamish Martin 			ret = mcp_chk_last_cmd_status_free_bus(mcp);
26467a95c21SRishi Gupta 			if (ret)
26567a95c21SRishi Gupta 				return ret;
26667a95c21SRishi Gupta 		}
26767a95c21SRishi Gupta 
26867a95c21SRishi Gupta 		sent = sent + len;
26967a95c21SRishi Gupta 		if (sent >= msg->len)
27067a95c21SRishi Gupta 			break;
27167a95c21SRishi Gupta 
27267a95c21SRishi Gupta 		idx = idx + len;
27367a95c21SRishi Gupta 		if ((msg->len - sent) < 60)
27467a95c21SRishi Gupta 			len = msg->len - sent;
27567a95c21SRishi Gupta 		else
27667a95c21SRishi Gupta 			len = 60;
27767a95c21SRishi Gupta 
27867a95c21SRishi Gupta 		/*
27967a95c21SRishi Gupta 		 * Testing shows delay is needed between successive writes
28067a95c21SRishi Gupta 		 * otherwise next write fails on first-try from i2c core.
28167a95c21SRishi Gupta 		 * This value is obtained through automated stress testing.
28267a95c21SRishi Gupta 		 */
28367a95c21SRishi Gupta 		usleep_range(980, 1000);
28467a95c21SRishi Gupta 	} while (len > 0);
28567a95c21SRishi Gupta 
28667a95c21SRishi Gupta 	return ret;
28767a95c21SRishi Gupta }
28867a95c21SRishi Gupta 
28967a95c21SRishi Gupta /*
29067a95c21SRishi Gupta  * Device reads all data (0 - 65535 bytes) from i2c slave device and
29167a95c21SRishi Gupta  * stores it in device itself. This data is read back from device to
29267a95c21SRishi Gupta  * host in multiples of 60 bytes using input reports.
29367a95c21SRishi Gupta  */
29467a95c21SRishi Gupta static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
29567a95c21SRishi Gupta 				struct i2c_msg *msg, int type, u16 smbus_addr,
29667a95c21SRishi Gupta 				u8 smbus_len, u8 *smbus_buf)
29767a95c21SRishi Gupta {
29867a95c21SRishi Gupta 	int ret;
29967a95c21SRishi Gupta 	u16 total_len;
30067a95c21SRishi Gupta 
30167a95c21SRishi Gupta 	mcp->txbuf[0] = type;
30267a95c21SRishi Gupta 	if (msg) {
30367a95c21SRishi Gupta 		mcp->txbuf[1] = msg->len & 0xff;
30467a95c21SRishi Gupta 		mcp->txbuf[2] = msg->len >> 8;
30567a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(msg->addr << 1);
30667a95c21SRishi Gupta 		total_len = msg->len;
30767a95c21SRishi Gupta 		mcp->rxbuf = msg->buf;
30867a95c21SRishi Gupta 	} else {
30967a95c21SRishi Gupta 		mcp->txbuf[1] = smbus_len;
31067a95c21SRishi Gupta 		mcp->txbuf[2] = 0;
31167a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(smbus_addr << 1);
31267a95c21SRishi Gupta 		total_len = smbus_len;
31367a95c21SRishi Gupta 		mcp->rxbuf = smbus_buf;
31467a95c21SRishi Gupta 	}
31567a95c21SRishi Gupta 
31667a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
31767a95c21SRishi Gupta 	if (ret)
31867a95c21SRishi Gupta 		return ret;
31967a95c21SRishi Gupta 
32067a95c21SRishi Gupta 	mcp->rxbuf_idx = 0;
32167a95c21SRishi Gupta 
32267a95c21SRishi Gupta 	do {
32367a95c21SRishi Gupta 		memset(mcp->txbuf, 0, 4);
32467a95c21SRishi Gupta 		mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
32567a95c21SRishi Gupta 
32667a95c21SRishi Gupta 		ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
32767a95c21SRishi Gupta 		if (ret)
32867a95c21SRishi Gupta 			return ret;
32967a95c21SRishi Gupta 
330*02a46753SHamish Martin 		ret = mcp_chk_last_cmd_status_free_bus(mcp);
33167a95c21SRishi Gupta 		if (ret)
33267a95c21SRishi Gupta 			return ret;
33367a95c21SRishi Gupta 
33467a95c21SRishi Gupta 		usleep_range(980, 1000);
33567a95c21SRishi Gupta 	} while (mcp->rxbuf_idx < total_len);
33667a95c21SRishi Gupta 
33767a95c21SRishi Gupta 	return ret;
33867a95c21SRishi Gupta }
33967a95c21SRishi Gupta 
34067a95c21SRishi Gupta static int mcp_i2c_xfer(struct i2c_adapter *adapter,
34167a95c21SRishi Gupta 				struct i2c_msg msgs[], int num)
34267a95c21SRishi Gupta {
34367a95c21SRishi Gupta 	int ret;
34467a95c21SRishi Gupta 	struct mcp2221 *mcp = i2c_get_adapdata(adapter);
34567a95c21SRishi Gupta 
34667a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
34767a95c21SRishi Gupta 
34867a95c21SRishi Gupta 	mutex_lock(&mcp->lock);
34967a95c21SRishi Gupta 
35067a95c21SRishi Gupta 	if (num == 1) {
35167a95c21SRishi Gupta 		if (msgs->flags & I2C_M_RD) {
35267a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
35367a95c21SRishi Gupta 							0, 0, NULL);
35467a95c21SRishi Gupta 		} else {
35567a95c21SRishi Gupta 			ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
35667a95c21SRishi Gupta 		}
35767a95c21SRishi Gupta 		if (ret)
35867a95c21SRishi Gupta 			goto exit;
35967a95c21SRishi Gupta 		ret = num;
36067a95c21SRishi Gupta 	} else if (num == 2) {
36167a95c21SRishi Gupta 		/* Ex transaction; send reg address and read its contents */
36267a95c21SRishi Gupta 		if (msgs[0].addr == msgs[1].addr &&
36367a95c21SRishi Gupta 			!(msgs[0].flags & I2C_M_RD) &&
36467a95c21SRishi Gupta 			 (msgs[1].flags & I2C_M_RD)) {
36567a95c21SRishi Gupta 
36667a95c21SRishi Gupta 			ret = mcp_i2c_write(mcp, &msgs[0],
36767a95c21SRishi Gupta 						MCP2221_I2C_WR_NO_STOP, 0);
36867a95c21SRishi Gupta 			if (ret)
36967a95c21SRishi Gupta 				goto exit;
37067a95c21SRishi Gupta 
37167a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, &msgs[1],
37267a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
37367a95c21SRishi Gupta 						0, 0, NULL);
37467a95c21SRishi Gupta 			if (ret)
37567a95c21SRishi Gupta 				goto exit;
37667a95c21SRishi Gupta 			ret = num;
37767a95c21SRishi Gupta 		} else {
37867a95c21SRishi Gupta 			dev_err(&adapter->dev,
37967a95c21SRishi Gupta 				"unsupported multi-msg i2c transaction\n");
38067a95c21SRishi Gupta 			ret = -EOPNOTSUPP;
38167a95c21SRishi Gupta 		}
38267a95c21SRishi Gupta 	} else {
38367a95c21SRishi Gupta 		dev_err(&adapter->dev,
38467a95c21SRishi Gupta 			"unsupported multi-msg i2c transaction\n");
38567a95c21SRishi Gupta 		ret = -EOPNOTSUPP;
38667a95c21SRishi Gupta 	}
38767a95c21SRishi Gupta 
38867a95c21SRishi Gupta exit:
38967a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
39067a95c21SRishi Gupta 	mutex_unlock(&mcp->lock);
39167a95c21SRishi Gupta 	return ret;
39267a95c21SRishi Gupta }
39367a95c21SRishi Gupta 
39467a95c21SRishi Gupta static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
39567a95c21SRishi Gupta 				u8 command, u8 *buf, u8 len, int type,
39667a95c21SRishi Gupta 				u8 last_status)
39767a95c21SRishi Gupta {
39867a95c21SRishi Gupta 	int data_len, ret;
39967a95c21SRishi Gupta 
40067a95c21SRishi Gupta 	mcp->txbuf[0] = type;
40167a95c21SRishi Gupta 	mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
40267a95c21SRishi Gupta 	mcp->txbuf[2] = 0;
40367a95c21SRishi Gupta 	mcp->txbuf[3] = (u8)(addr << 1);
40467a95c21SRishi Gupta 	mcp->txbuf[4] = command;
40567a95c21SRishi Gupta 
40667a95c21SRishi Gupta 	switch (len) {
40767a95c21SRishi Gupta 	case 0:
40867a95c21SRishi Gupta 		data_len = 5;
40967a95c21SRishi Gupta 		break;
41067a95c21SRishi Gupta 	case 1:
41167a95c21SRishi Gupta 		mcp->txbuf[5] = buf[0];
41267a95c21SRishi Gupta 		data_len = 6;
41367a95c21SRishi Gupta 		break;
41467a95c21SRishi Gupta 	case 2:
41567a95c21SRishi Gupta 		mcp->txbuf[5] = buf[0];
41667a95c21SRishi Gupta 		mcp->txbuf[6] = buf[1];
41767a95c21SRishi Gupta 		data_len = 7;
41867a95c21SRishi Gupta 		break;
41967a95c21SRishi Gupta 	default:
42062ac2473SHarshit Mogalapalli 		if (len > I2C_SMBUS_BLOCK_MAX)
42162ac2473SHarshit Mogalapalli 			return -EINVAL;
42262ac2473SHarshit Mogalapalli 
42367a95c21SRishi Gupta 		memcpy(&mcp->txbuf[5], buf, len);
42467a95c21SRishi Gupta 		data_len = len + 5;
42567a95c21SRishi Gupta 	}
42667a95c21SRishi Gupta 
42767a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
42867a95c21SRishi Gupta 	if (ret)
42967a95c21SRishi Gupta 		return ret;
43067a95c21SRishi Gupta 
43167a95c21SRishi Gupta 	if (last_status) {
43267a95c21SRishi Gupta 		usleep_range(980, 1000);
43367a95c21SRishi Gupta 
434*02a46753SHamish Martin 		ret = mcp_chk_last_cmd_status_free_bus(mcp);
43567a95c21SRishi Gupta 	}
43667a95c21SRishi Gupta 
43767a95c21SRishi Gupta 	return ret;
43867a95c21SRishi Gupta }
43967a95c21SRishi Gupta 
44067a95c21SRishi Gupta static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
44167a95c21SRishi Gupta 				unsigned short flags, char read_write,
44267a95c21SRishi Gupta 				u8 command, int size,
44367a95c21SRishi Gupta 				union i2c_smbus_data *data)
44467a95c21SRishi Gupta {
44567a95c21SRishi Gupta 	int ret;
44667a95c21SRishi Gupta 	struct mcp2221 *mcp = i2c_get_adapdata(adapter);
44767a95c21SRishi Gupta 
44867a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
44967a95c21SRishi Gupta 
45067a95c21SRishi Gupta 	mutex_lock(&mcp->lock);
45167a95c21SRishi Gupta 
45267a95c21SRishi Gupta 	switch (size) {
45367a95c21SRishi Gupta 
45467a95c21SRishi Gupta 	case I2C_SMBUS_QUICK:
45567a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ)
45667a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
45767a95c21SRishi Gupta 						addr, 0, &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:
46367a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ)
46467a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
46567a95c21SRishi Gupta 						addr, 1, &data->byte);
46667a95c21SRishi Gupta 		else
46767a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
46867a95c21SRishi Gupta 						0, MCP2221_I2C_WR_DATA, 1);
46967a95c21SRishi Gupta 		break;
47067a95c21SRishi Gupta 	case I2C_SMBUS_BYTE_DATA:
47167a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
47267a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
47367a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 0);
47467a95c21SRishi Gupta 			if (ret)
47567a95c21SRishi Gupta 				goto exit;
47667a95c21SRishi Gupta 
47767a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL,
47867a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
47967a95c21SRishi Gupta 						addr, 1, &data->byte);
48067a95c21SRishi Gupta 		} else {
48167a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, &data->byte,
48267a95c21SRishi Gupta 						1, MCP2221_I2C_WR_DATA, 1);
48367a95c21SRishi Gupta 		}
48467a95c21SRishi Gupta 		break;
48567a95c21SRishi Gupta 	case I2C_SMBUS_WORD_DATA:
48667a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
48767a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
48867a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 0);
48967a95c21SRishi Gupta 			if (ret)
49067a95c21SRishi Gupta 				goto exit;
49167a95c21SRishi Gupta 
49267a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL,
49367a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
49467a95c21SRishi Gupta 						addr, 2, (u8 *)&data->word);
49567a95c21SRishi Gupta 		} else {
49667a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command,
49767a95c21SRishi Gupta 						(u8 *)&data->word, 2,
49867a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
49967a95c21SRishi Gupta 		}
50067a95c21SRishi Gupta 		break;
50167a95c21SRishi Gupta 	case I2C_SMBUS_BLOCK_DATA:
50267a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
50367a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
50467a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 1);
50567a95c21SRishi Gupta 			if (ret)
50667a95c21SRishi Gupta 				goto exit;
50767a95c21SRishi Gupta 
50867a95c21SRishi Gupta 			mcp->rxbuf_idx = 0;
50967a95c21SRishi Gupta 			mcp->rxbuf = data->block;
51067a95c21SRishi Gupta 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
51167a95c21SRishi Gupta 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
51267a95c21SRishi Gupta 			if (ret)
51367a95c21SRishi Gupta 				goto exit;
51467a95c21SRishi Gupta 		} else {
51567a95c21SRishi Gupta 			if (!data->block[0]) {
51667a95c21SRishi Gupta 				ret = -EINVAL;
51767a95c21SRishi Gupta 				goto exit;
51867a95c21SRishi Gupta 			}
51967a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, data->block,
52067a95c21SRishi Gupta 						data->block[0] + 1,
52167a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
52267a95c21SRishi Gupta 		}
52367a95c21SRishi Gupta 		break;
52467a95c21SRishi Gupta 	case I2C_SMBUS_I2C_BLOCK_DATA:
52567a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
52667a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
52767a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 1);
52867a95c21SRishi Gupta 			if (ret)
52967a95c21SRishi Gupta 				goto exit;
53067a95c21SRishi Gupta 
53167a95c21SRishi Gupta 			mcp->rxbuf_idx = 0;
53267a95c21SRishi Gupta 			mcp->rxbuf = data->block;
53367a95c21SRishi Gupta 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
53467a95c21SRishi Gupta 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
53567a95c21SRishi Gupta 			if (ret)
53667a95c21SRishi Gupta 				goto exit;
53767a95c21SRishi Gupta 		} else {
53867a95c21SRishi Gupta 			if (!data->block[0]) {
53967a95c21SRishi Gupta 				ret = -EINVAL;
54067a95c21SRishi Gupta 				goto exit;
54167a95c21SRishi Gupta 			}
54267a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command,
54367a95c21SRishi Gupta 						&data->block[1], data->block[0],
54467a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
54567a95c21SRishi Gupta 		}
54667a95c21SRishi Gupta 		break;
54767a95c21SRishi Gupta 	case I2C_SMBUS_PROC_CALL:
54867a95c21SRishi Gupta 		ret = mcp_smbus_write(mcp, addr, command,
54967a95c21SRishi Gupta 						(u8 *)&data->word,
55067a95c21SRishi Gupta 						2, MCP2221_I2C_WR_NO_STOP, 0);
55167a95c21SRishi Gupta 		if (ret)
55267a95c21SRishi Gupta 			goto exit;
55367a95c21SRishi Gupta 
55467a95c21SRishi Gupta 		ret = mcp_i2c_smbus_read(mcp, NULL,
55567a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
55667a95c21SRishi Gupta 						addr, 2, (u8 *)&data->word);
55767a95c21SRishi Gupta 		break;
55867a95c21SRishi Gupta 	case I2C_SMBUS_BLOCK_PROC_CALL:
55967a95c21SRishi Gupta 		ret = mcp_smbus_write(mcp, addr, command, data->block,
56067a95c21SRishi Gupta 						data->block[0] + 1,
56167a95c21SRishi Gupta 						MCP2221_I2C_WR_NO_STOP, 0);
56267a95c21SRishi Gupta 		if (ret)
56367a95c21SRishi Gupta 			goto exit;
56467a95c21SRishi Gupta 
56567a95c21SRishi Gupta 		ret = mcp_i2c_smbus_read(mcp, NULL,
56667a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
56767a95c21SRishi Gupta 						addr, I2C_SMBUS_BLOCK_MAX,
56867a95c21SRishi Gupta 						data->block);
56967a95c21SRishi Gupta 		break;
57067a95c21SRishi Gupta 	default:
57167a95c21SRishi Gupta 		dev_err(&mcp->adapter.dev,
57267a95c21SRishi Gupta 			"unsupported smbus transaction size:%d\n", size);
57367a95c21SRishi Gupta 		ret = -EOPNOTSUPP;
57467a95c21SRishi Gupta 	}
57567a95c21SRishi Gupta 
57667a95c21SRishi Gupta exit:
57767a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
57867a95c21SRishi Gupta 	mutex_unlock(&mcp->lock);
57967a95c21SRishi Gupta 	return ret;
58067a95c21SRishi Gupta }
58167a95c21SRishi Gupta 
58267a95c21SRishi Gupta static u32 mcp_i2c_func(struct i2c_adapter *adapter)
58367a95c21SRishi Gupta {
58467a95c21SRishi Gupta 	return I2C_FUNC_I2C |
58567a95c21SRishi Gupta 			I2C_FUNC_SMBUS_READ_BLOCK_DATA |
58667a95c21SRishi Gupta 			I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
58767a95c21SRishi Gupta 			(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
58867a95c21SRishi Gupta }
58967a95c21SRishi Gupta 
59067a95c21SRishi Gupta static const struct i2c_algorithm mcp_i2c_algo = {
59167a95c21SRishi Gupta 	.master_xfer = mcp_i2c_xfer,
59267a95c21SRishi Gupta 	.smbus_xfer = mcp_smbus_xfer,
59367a95c21SRishi Gupta 	.functionality = mcp_i2c_func,
59467a95c21SRishi Gupta };
59567a95c21SRishi Gupta 
5963d74c9ecSMatt Ranostay #if IS_REACHABLE(CONFIG_GPIOLIB)
597328de1c5SRishi Gupta static int mcp_gpio_get(struct gpio_chip *gc,
598328de1c5SRishi Gupta 				unsigned int offset)
599328de1c5SRishi Gupta {
600328de1c5SRishi Gupta 	int ret;
601328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
602328de1c5SRishi Gupta 
603328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_GET;
604328de1c5SRishi Gupta 
605ca6961d8SLouis Morhet 	mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);
606328de1c5SRishi Gupta 
607328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
608328de1c5SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
609328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
610328de1c5SRishi Gupta 
611328de1c5SRishi Gupta 	return ret;
612328de1c5SRishi Gupta }
613328de1c5SRishi Gupta 
614328de1c5SRishi Gupta static void mcp_gpio_set(struct gpio_chip *gc,
615328de1c5SRishi Gupta 				unsigned int offset, int value)
616328de1c5SRishi Gupta {
617328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
618328de1c5SRishi Gupta 
619328de1c5SRishi Gupta 	memset(mcp->txbuf, 0, 18);
620328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_SET;
621328de1c5SRishi Gupta 
622567b8e9fSLars Povlsen 	mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
623328de1c5SRishi Gupta 
624328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx - 1] = 1;
625328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx] = !!value;
626328de1c5SRishi Gupta 
627328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
628328de1c5SRishi Gupta 	mcp_send_data_req_status(mcp, mcp->txbuf, 18);
629328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
630328de1c5SRishi Gupta }
631328de1c5SRishi Gupta 
632328de1c5SRishi Gupta static int mcp_gpio_dir_set(struct mcp2221 *mcp,
633328de1c5SRishi Gupta 				unsigned int offset, u8 val)
634328de1c5SRishi Gupta {
635328de1c5SRishi Gupta 	memset(mcp->txbuf, 0, 18);
636328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_SET;
637328de1c5SRishi Gupta 
638567b8e9fSLars Povlsen 	mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
639328de1c5SRishi Gupta 
640328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx - 1] = 1;
641328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx] = val;
642328de1c5SRishi Gupta 
643328de1c5SRishi Gupta 	return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
644328de1c5SRishi Gupta }
645328de1c5SRishi Gupta 
646328de1c5SRishi Gupta static int mcp_gpio_direction_input(struct gpio_chip *gc,
647328de1c5SRishi Gupta 				unsigned int offset)
648328de1c5SRishi Gupta {
649328de1c5SRishi Gupta 	int ret;
650328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
651328de1c5SRishi Gupta 
652328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
653567b8e9fSLars Povlsen 	ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
654328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
655328de1c5SRishi Gupta 
656328de1c5SRishi Gupta 	return ret;
657328de1c5SRishi Gupta }
658328de1c5SRishi Gupta 
659328de1c5SRishi Gupta static int mcp_gpio_direction_output(struct gpio_chip *gc,
660328de1c5SRishi Gupta 				unsigned int offset, int value)
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_OUT);
667328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
668328de1c5SRishi Gupta 
669328de1c5SRishi Gupta 	/* Can't configure as output, bailout early */
670328de1c5SRishi Gupta 	if (ret)
671328de1c5SRishi Gupta 		return ret;
672328de1c5SRishi Gupta 
673328de1c5SRishi Gupta 	mcp_gpio_set(gc, offset, value);
674328de1c5SRishi Gupta 
675328de1c5SRishi Gupta 	return 0;
676328de1c5SRishi Gupta }
677328de1c5SRishi Gupta 
678328de1c5SRishi Gupta static int mcp_gpio_get_direction(struct gpio_chip *gc,
679328de1c5SRishi Gupta 				unsigned int offset)
680328de1c5SRishi Gupta {
681328de1c5SRishi Gupta 	int ret;
682328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
683328de1c5SRishi Gupta 
684328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_GET;
685328de1c5SRishi Gupta 
686ca6961d8SLouis Morhet 	mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);
687328de1c5SRishi Gupta 
688328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
689328de1c5SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
690328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
691328de1c5SRishi Gupta 
692328de1c5SRishi Gupta 	if (ret)
693328de1c5SRishi Gupta 		return ret;
694328de1c5SRishi Gupta 
695567b8e9fSLars Povlsen 	if (mcp->gpio_dir == MCP2221_DIR_IN)
696328de1c5SRishi Gupta 		return GPIO_LINE_DIRECTION_IN;
697328de1c5SRishi Gupta 
698328de1c5SRishi Gupta 	return GPIO_LINE_DIRECTION_OUT;
699328de1c5SRishi Gupta }
7003d74c9ecSMatt Ranostay #endif
701328de1c5SRishi Gupta 
70267a95c21SRishi Gupta /* Gives current state of i2c engine inside mcp2221 */
70367a95c21SRishi Gupta static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
70467a95c21SRishi Gupta 				u8 *data, u8 idx)
70567a95c21SRishi Gupta {
70667a95c21SRishi Gupta 	int ret;
70767a95c21SRishi Gupta 
70867a95c21SRishi Gupta 	switch (data[idx]) {
70967a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_NACK:
71067a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_SEND:
71167a95c21SRishi Gupta 		ret = -ENXIO;
71267a95c21SRishi Gupta 		break;
71367a95c21SRishi Gupta 	case MCP2221_I2C_START_TOUT:
71467a95c21SRishi Gupta 	case MCP2221_I2C_STOP_TOUT:
71567a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_TOUT:
71667a95c21SRishi Gupta 	case MCP2221_I2C_WRDATA_TOUT:
71767a95c21SRishi Gupta 		ret = -ETIMEDOUT;
71867a95c21SRishi Gupta 		break;
71967a95c21SRishi Gupta 	case MCP2221_I2C_ENG_BUSY:
72067a95c21SRishi Gupta 		ret = -EAGAIN;
72167a95c21SRishi Gupta 		break;
72267a95c21SRishi Gupta 	case MCP2221_SUCCESS:
72367a95c21SRishi Gupta 		ret = 0x00;
72467a95c21SRishi Gupta 		break;
72567a95c21SRishi Gupta 	default:
72667a95c21SRishi Gupta 		ret = -EIO;
72767a95c21SRishi Gupta 	}
72867a95c21SRishi Gupta 
72967a95c21SRishi Gupta 	return ret;
73067a95c21SRishi Gupta }
73167a95c21SRishi Gupta 
73267a95c21SRishi Gupta /*
73367a95c21SRishi Gupta  * MCP2221 uses interrupt endpoint for input reports. This function
73467a95c21SRishi Gupta  * is called by HID layer when it receives i/p report from mcp2221,
73567a95c21SRishi Gupta  * which is actually a response to the previously sent command.
73667a95c21SRishi Gupta  *
73767a95c21SRishi Gupta  * MCP2221A firmware specific return codes are parsed and 0 or
73867a95c21SRishi Gupta  * appropriate negative error code is returned. Delayed response
73967a95c21SRishi Gupta  * results in timeout error and stray reponses results in -EIO.
74067a95c21SRishi Gupta  */
74167a95c21SRishi Gupta static int mcp2221_raw_event(struct hid_device *hdev,
74267a95c21SRishi Gupta 				struct hid_report *report, u8 *data, int size)
74367a95c21SRishi Gupta {
744daf405c8SJiri Kosina 	u8 *buf;
74567a95c21SRishi Gupta 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
74667a95c21SRishi Gupta 
74767a95c21SRishi Gupta 	switch (data[0]) {
74867a95c21SRishi Gupta 
74967a95c21SRishi Gupta 	case MCP2221_I2C_WR_DATA:
75067a95c21SRishi Gupta 	case MCP2221_I2C_WR_NO_STOP:
75167a95c21SRishi Gupta 	case MCP2221_I2C_RD_DATA:
75267a95c21SRishi Gupta 	case MCP2221_I2C_RD_RPT_START:
75367a95c21SRishi Gupta 		switch (data[1]) {
75467a95c21SRishi Gupta 		case MCP2221_SUCCESS:
75567a95c21SRishi Gupta 			mcp->status = 0;
75667a95c21SRishi Gupta 			break;
75767a95c21SRishi Gupta 		default:
75867a95c21SRishi Gupta 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
75967a95c21SRishi Gupta 		}
76067a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
76167a95c21SRishi Gupta 		break;
76267a95c21SRishi Gupta 
76367a95c21SRishi Gupta 	case MCP2221_I2C_PARAM_OR_STATUS:
76467a95c21SRishi Gupta 		switch (data[1]) {
76567a95c21SRishi Gupta 		case MCP2221_SUCCESS:
76667a95c21SRishi Gupta 			if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
76767a95c21SRishi Gupta 				(data[3] != MCP2221_I2C_SET_SPEED)) {
76867a95c21SRishi Gupta 				mcp->status = -EAGAIN;
76967a95c21SRishi Gupta 				break;
77067a95c21SRishi Gupta 			}
77167a95c21SRishi Gupta 			if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
77267a95c21SRishi Gupta 				mcp->status = -ENXIO;
77367a95c21SRishi Gupta 				break;
77467a95c21SRishi Gupta 			}
77567a95c21SRishi Gupta 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
776960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
777960f9df7SMatt Ranostay 			memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values));
778960f9df7SMatt Ranostay #endif
77967a95c21SRishi Gupta 			break;
78067a95c21SRishi Gupta 		default:
78167a95c21SRishi Gupta 			mcp->status = -EIO;
78267a95c21SRishi Gupta 		}
78367a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
78467a95c21SRishi Gupta 		break;
78567a95c21SRishi Gupta 
78667a95c21SRishi Gupta 	case MCP2221_I2C_GET_DATA:
78767a95c21SRishi Gupta 		switch (data[1]) {
78867a95c21SRishi Gupta 		case MCP2221_SUCCESS:
78967a95c21SRishi Gupta 			if (data[2] == MCP2221_I2C_ADDR_NACK) {
79067a95c21SRishi Gupta 				mcp->status = -ENXIO;
79167a95c21SRishi Gupta 				break;
79267a95c21SRishi Gupta 			}
79367a95c21SRishi Gupta 			if (!mcp_get_i2c_eng_state(mcp, data, 2)
79467a95c21SRishi Gupta 				&& (data[3] == 0)) {
79567a95c21SRishi Gupta 				mcp->status = 0;
79667a95c21SRishi Gupta 				break;
79767a95c21SRishi Gupta 			}
79867a95c21SRishi Gupta 			if (data[3] == 127) {
79967a95c21SRishi Gupta 				mcp->status = -EIO;
80067a95c21SRishi Gupta 				break;
80167a95c21SRishi Gupta 			}
80267a95c21SRishi Gupta 			if (data[2] == MCP2221_I2C_READ_COMPL) {
80367a95c21SRishi Gupta 				buf = mcp->rxbuf;
80467a95c21SRishi Gupta 				memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
80567a95c21SRishi Gupta 				mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
80667a95c21SRishi Gupta 				mcp->status = 0;
80767a95c21SRishi Gupta 				break;
80867a95c21SRishi Gupta 			}
80967a95c21SRishi Gupta 			mcp->status = -EIO;
81067a95c21SRishi Gupta 			break;
81167a95c21SRishi Gupta 		default:
81267a95c21SRishi Gupta 			mcp->status = -EIO;
81367a95c21SRishi Gupta 		}
81467a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
81567a95c21SRishi Gupta 		break;
81667a95c21SRishi Gupta 
817328de1c5SRishi Gupta 	case MCP2221_GPIO_GET:
818328de1c5SRishi Gupta 		switch (data[1]) {
819328de1c5SRishi Gupta 		case MCP2221_SUCCESS:
820328de1c5SRishi Gupta 			if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
821328de1c5SRishi Gupta 				(data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
822328de1c5SRishi Gupta 				mcp->status = -ENOENT;
823328de1c5SRishi Gupta 			} else {
824328de1c5SRishi Gupta 				mcp->status = !!data[mcp->gp_idx];
825567b8e9fSLars Povlsen 				mcp->gpio_dir = data[mcp->gp_idx + 1];
826328de1c5SRishi Gupta 			}
827328de1c5SRishi Gupta 			break;
828328de1c5SRishi Gupta 		default:
829328de1c5SRishi Gupta 			mcp->status = -EAGAIN;
830328de1c5SRishi Gupta 		}
831328de1c5SRishi Gupta 		complete(&mcp->wait_in_report);
832328de1c5SRishi Gupta 		break;
833328de1c5SRishi Gupta 
834328de1c5SRishi Gupta 	case MCP2221_GPIO_SET:
835328de1c5SRishi Gupta 		switch (data[1]) {
836328de1c5SRishi Gupta 		case MCP2221_SUCCESS:
837328de1c5SRishi Gupta 			if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
838328de1c5SRishi Gupta 				(data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
839328de1c5SRishi Gupta 				mcp->status = -ENOENT;
840328de1c5SRishi Gupta 			} else {
841328de1c5SRishi Gupta 				mcp->status = 0;
842328de1c5SRishi Gupta 			}
843328de1c5SRishi Gupta 			break;
844328de1c5SRishi Gupta 		default:
845328de1c5SRishi Gupta 			mcp->status = -EAGAIN;
846328de1c5SRishi Gupta 		}
847328de1c5SRishi Gupta 		complete(&mcp->wait_in_report);
848328de1c5SRishi Gupta 		break;
849328de1c5SRishi Gupta 
850960f9df7SMatt Ranostay 	case MCP2221_SET_SRAM_SETTINGS:
851960f9df7SMatt Ranostay 		switch (data[1]) {
852960f9df7SMatt Ranostay 		case MCP2221_SUCCESS:
853960f9df7SMatt Ranostay 			mcp->status = 0;
854960f9df7SMatt Ranostay 			break;
855960f9df7SMatt Ranostay 		default:
856960f9df7SMatt Ranostay 			mcp->status = -EAGAIN;
857960f9df7SMatt Ranostay 		}
858960f9df7SMatt Ranostay 		complete(&mcp->wait_in_report);
859960f9df7SMatt Ranostay 		break;
860960f9df7SMatt Ranostay 
861960f9df7SMatt Ranostay 	case MCP2221_GET_SRAM_SETTINGS:
862960f9df7SMatt Ranostay 		switch (data[1]) {
863960f9df7SMatt Ranostay 		case MCP2221_SUCCESS:
864960f9df7SMatt Ranostay 			memcpy(&mcp->mode, &data[22], 4);
865960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
866960f9df7SMatt Ranostay 			mcp->dac_value = data[6] & GENMASK(4, 0);
867960f9df7SMatt Ranostay #endif
868960f9df7SMatt Ranostay 			mcp->status = 0;
869960f9df7SMatt Ranostay 			break;
870960f9df7SMatt Ranostay 		default:
871960f9df7SMatt Ranostay 			mcp->status = -EAGAIN;
872960f9df7SMatt Ranostay 		}
873960f9df7SMatt Ranostay 		complete(&mcp->wait_in_report);
874960f9df7SMatt Ranostay 		break;
875960f9df7SMatt Ranostay 
876960f9df7SMatt Ranostay 	case MCP2221_READ_FLASH_DATA:
877960f9df7SMatt Ranostay 		switch (data[1]) {
878960f9df7SMatt Ranostay 		case MCP2221_SUCCESS:
879960f9df7SMatt Ranostay 			mcp->status = 0;
880960f9df7SMatt Ranostay 
881960f9df7SMatt Ranostay 			/* Only handles CHIP SETTINGS subpage currently */
882960f9df7SMatt Ranostay 			if (mcp->txbuf[1] != 0) {
883960f9df7SMatt Ranostay 				mcp->status = -EIO;
884960f9df7SMatt Ranostay 				break;
885960f9df7SMatt Ranostay 			}
886960f9df7SMatt Ranostay 
887960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
888daf405c8SJiri Kosina 			{
889daf405c8SJiri Kosina 				u8 tmp;
890960f9df7SMatt Ranostay 				/* DAC scale value */
891960f9df7SMatt Ranostay 				tmp = FIELD_GET(GENMASK(7, 6), data[6]);
892960f9df7SMatt Ranostay 				if ((data[6] & BIT(5)) && tmp)
893960f9df7SMatt Ranostay 					mcp->dac_scale = tmp + 4;
894960f9df7SMatt Ranostay 				else
895960f9df7SMatt Ranostay 					mcp->dac_scale = 5;
896960f9df7SMatt Ranostay 
897960f9df7SMatt Ranostay 				/* ADC scale value */
898960f9df7SMatt Ranostay 				tmp = FIELD_GET(GENMASK(4, 3), data[7]);
899960f9df7SMatt Ranostay 				if ((data[7] & BIT(2)) && tmp)
900960f9df7SMatt Ranostay 					mcp->adc_scale = tmp - 1;
901960f9df7SMatt Ranostay 				else
902960f9df7SMatt Ranostay 					mcp->adc_scale = 0;
903daf405c8SJiri Kosina 			}
904960f9df7SMatt Ranostay #endif
905960f9df7SMatt Ranostay 
906960f9df7SMatt Ranostay 			break;
907960f9df7SMatt Ranostay 		default:
908960f9df7SMatt Ranostay 			mcp->status = -EAGAIN;
909960f9df7SMatt Ranostay 		}
910960f9df7SMatt Ranostay 		complete(&mcp->wait_in_report);
911960f9df7SMatt Ranostay 		break;
912960f9df7SMatt Ranostay 
91367a95c21SRishi Gupta 	default:
91467a95c21SRishi Gupta 		mcp->status = -EIO;
91567a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
91667a95c21SRishi Gupta 	}
91767a95c21SRishi Gupta 
91867a95c21SRishi Gupta 	return 1;
91967a95c21SRishi Gupta }
92067a95c21SRishi Gupta 
921deb3b88bSMatt Ranostay /* Device resource managed function for HID unregistration */
922deb3b88bSMatt Ranostay static void mcp2221_hid_unregister(void *ptr)
923deb3b88bSMatt Ranostay {
924deb3b88bSMatt Ranostay 	struct hid_device *hdev = ptr;
925deb3b88bSMatt Ranostay 
926deb3b88bSMatt Ranostay 	hid_hw_close(hdev);
927deb3b88bSMatt Ranostay 	hid_hw_stop(hdev);
928deb3b88bSMatt Ranostay }
929deb3b88bSMatt Ranostay 
930deb3b88bSMatt Ranostay /* This is needed to be sure hid_hw_stop() isn't called twice by the subsystem */
931deb3b88bSMatt Ranostay static void mcp2221_remove(struct hid_device *hdev)
932deb3b88bSMatt Ranostay {
93347e91fdfSBenjamin Tissoires 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
93447e91fdfSBenjamin Tissoires 
93547e91fdfSBenjamin Tissoires 	cancel_delayed_work_sync(&mcp->init_work);
936deb3b88bSMatt Ranostay }
937deb3b88bSMatt Ranostay 
938960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
939960f9df7SMatt Ranostay static int mcp2221_read_raw(struct iio_dev *indio_dev,
940960f9df7SMatt Ranostay 			    struct iio_chan_spec const *channel, int *val,
941960f9df7SMatt Ranostay 			    int *val2, long mask)
942960f9df7SMatt Ranostay {
943960f9df7SMatt Ranostay 	struct mcp2221_iio *priv = iio_priv(indio_dev);
944960f9df7SMatt Ranostay 	struct mcp2221 *mcp = priv->mcp;
945960f9df7SMatt Ranostay 	int ret;
946960f9df7SMatt Ranostay 
947960f9df7SMatt Ranostay 	if (mask == IIO_CHAN_INFO_SCALE) {
948960f9df7SMatt Ranostay 		if (channel->output)
949960f9df7SMatt Ranostay 			*val = 1 << mcp->dac_scale;
950960f9df7SMatt Ranostay 		else
951960f9df7SMatt Ranostay 			*val = 1 << mcp->adc_scale;
952960f9df7SMatt Ranostay 
953960f9df7SMatt Ranostay 		return IIO_VAL_INT;
954960f9df7SMatt Ranostay 	}
955960f9df7SMatt Ranostay 
956960f9df7SMatt Ranostay 	mutex_lock(&mcp->lock);
957960f9df7SMatt Ranostay 
958960f9df7SMatt Ranostay 	if (channel->output) {
959960f9df7SMatt Ranostay 		*val = mcp->dac_value;
960960f9df7SMatt Ranostay 		ret = IIO_VAL_INT;
961960f9df7SMatt Ranostay 	} else {
962960f9df7SMatt Ranostay 		/* Read ADC values */
963960f9df7SMatt Ranostay 		ret = mcp_chk_last_cmd_status(mcp);
964960f9df7SMatt Ranostay 
965960f9df7SMatt Ranostay 		if (!ret) {
966e91fc483SMatt Ranostay 			*val = le16_to_cpu((__force __le16) mcp->adc_values[channel->address]);
967960f9df7SMatt Ranostay 			if (*val >= BIT(10))
968960f9df7SMatt Ranostay 				ret =  -EINVAL;
969960f9df7SMatt Ranostay 			else
970960f9df7SMatt Ranostay 				ret = IIO_VAL_INT;
971960f9df7SMatt Ranostay 		}
972960f9df7SMatt Ranostay 	}
973960f9df7SMatt Ranostay 
974960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
975960f9df7SMatt Ranostay 
976960f9df7SMatt Ranostay 	return ret;
977960f9df7SMatt Ranostay }
978960f9df7SMatt Ranostay 
979960f9df7SMatt Ranostay static int mcp2221_write_raw(struct iio_dev *indio_dev,
980960f9df7SMatt Ranostay 			     struct iio_chan_spec const *chan,
981960f9df7SMatt Ranostay 			     int val, int val2, long mask)
982960f9df7SMatt Ranostay {
983960f9df7SMatt Ranostay 	struct mcp2221_iio *priv = iio_priv(indio_dev);
984960f9df7SMatt Ranostay 	struct mcp2221 *mcp = priv->mcp;
985960f9df7SMatt Ranostay 	int ret;
986960f9df7SMatt Ranostay 
987960f9df7SMatt Ranostay 	if (val < 0 || val >= BIT(5))
988960f9df7SMatt Ranostay 		return -EINVAL;
989960f9df7SMatt Ranostay 
990960f9df7SMatt Ranostay 	mutex_lock(&mcp->lock);
991960f9df7SMatt Ranostay 
992960f9df7SMatt Ranostay 	memset(mcp->txbuf, 0, 12);
993960f9df7SMatt Ranostay 	mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS;
994960f9df7SMatt Ranostay 	mcp->txbuf[4] = BIT(7) | val;
995960f9df7SMatt Ranostay 
996960f9df7SMatt Ranostay 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12);
997960f9df7SMatt Ranostay 	if (!ret)
998960f9df7SMatt Ranostay 		mcp->dac_value = val;
999960f9df7SMatt Ranostay 
1000960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
1001960f9df7SMatt Ranostay 
1002960f9df7SMatt Ranostay 	return ret;
1003960f9df7SMatt Ranostay }
1004960f9df7SMatt Ranostay 
1005960f9df7SMatt Ranostay static const struct iio_info mcp2221_info = {
1006960f9df7SMatt Ranostay 	.read_raw = &mcp2221_read_raw,
1007960f9df7SMatt Ranostay 	.write_raw = &mcp2221_write_raw,
1008960f9df7SMatt Ranostay };
1009960f9df7SMatt Ranostay 
1010960f9df7SMatt Ranostay static int mcp_iio_channels(struct mcp2221 *mcp)
1011960f9df7SMatt Ranostay {
1012960f9df7SMatt Ranostay 	int idx, cnt = 0;
1013960f9df7SMatt Ranostay 	bool dac_created = false;
1014960f9df7SMatt Ranostay 
1015960f9df7SMatt Ranostay 	/* GP0 doesn't have ADC/DAC alternative function */
1016960f9df7SMatt Ranostay 	for (idx = 1; idx < MCP_NGPIO; idx++) {
1017960f9df7SMatt Ranostay 		struct iio_chan_spec *chan = &mcp->iio_channels[cnt];
1018960f9df7SMatt Ranostay 
1019960f9df7SMatt Ranostay 		switch (mcp->mode[idx]) {
1020960f9df7SMatt Ranostay 		case 2:
1021960f9df7SMatt Ranostay 			chan->address = idx - 1;
1022960f9df7SMatt Ranostay 			chan->channel = cnt++;
1023960f9df7SMatt Ranostay 			break;
1024960f9df7SMatt Ranostay 		case 3:
1025960f9df7SMatt Ranostay 			/* GP1 doesn't have DAC alternative function */
1026960f9df7SMatt Ranostay 			if (idx == 1 || dac_created)
1027960f9df7SMatt Ranostay 				continue;
1028960f9df7SMatt Ranostay 			/* DAC1 and DAC2 outputs are connected to the same DAC */
1029960f9df7SMatt Ranostay 			dac_created = true;
1030960f9df7SMatt Ranostay 			chan->output = 1;
1031960f9df7SMatt Ranostay 			cnt++;
1032960f9df7SMatt Ranostay 			break;
1033960f9df7SMatt Ranostay 		default:
1034960f9df7SMatt Ranostay 			continue;
1035960f9df7SMatt Ranostay 		};
1036960f9df7SMatt Ranostay 
1037960f9df7SMatt Ranostay 		chan->type = IIO_VOLTAGE;
1038960f9df7SMatt Ranostay 		chan->indexed = 1;
1039960f9df7SMatt Ranostay 		chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
1040960f9df7SMatt Ranostay 		chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
1041960f9df7SMatt Ranostay 		chan->scan_index = -1;
1042960f9df7SMatt Ranostay 	}
1043960f9df7SMatt Ranostay 
1044960f9df7SMatt Ranostay 	return cnt;
1045960f9df7SMatt Ranostay }
1046960f9df7SMatt Ranostay 
1047960f9df7SMatt Ranostay static void mcp_init_work(struct work_struct *work)
1048960f9df7SMatt Ranostay {
1049960f9df7SMatt Ranostay 	struct iio_dev *indio_dev;
1050960f9df7SMatt Ranostay 	struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work);
1051960f9df7SMatt Ranostay 	struct mcp2221_iio *data;
1052960f9df7SMatt Ranostay 	static int retries = 5;
1053960f9df7SMatt Ranostay 	int ret, num_channels;
1054960f9df7SMatt Ranostay 
1055960f9df7SMatt Ranostay 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
1056960f9df7SMatt Ranostay 	mutex_lock(&mcp->lock);
1057960f9df7SMatt Ranostay 
1058960f9df7SMatt Ranostay 	mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS;
1059960f9df7SMatt Ranostay 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
1060960f9df7SMatt Ranostay 
1061960f9df7SMatt Ranostay 	if (ret == -EAGAIN)
1062960f9df7SMatt Ranostay 		goto reschedule_task;
1063960f9df7SMatt Ranostay 
1064960f9df7SMatt Ranostay 	num_channels = mcp_iio_channels(mcp);
1065960f9df7SMatt Ranostay 	if (!num_channels)
1066960f9df7SMatt Ranostay 		goto unlock;
1067960f9df7SMatt Ranostay 
1068960f9df7SMatt Ranostay 	mcp->txbuf[0] = MCP2221_READ_FLASH_DATA;
1069960f9df7SMatt Ranostay 	mcp->txbuf[1] = 0;
1070960f9df7SMatt Ranostay 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2);
1071960f9df7SMatt Ranostay 
1072960f9df7SMatt Ranostay 	if (ret == -EAGAIN)
1073960f9df7SMatt Ranostay 		goto reschedule_task;
1074960f9df7SMatt Ranostay 
1075960f9df7SMatt Ranostay 	indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data));
1076960f9df7SMatt Ranostay 	if (!indio_dev)
1077960f9df7SMatt Ranostay 		goto unlock;
1078960f9df7SMatt Ranostay 
1079960f9df7SMatt Ranostay 	data = iio_priv(indio_dev);
1080960f9df7SMatt Ranostay 	data->mcp = mcp;
1081960f9df7SMatt Ranostay 
1082960f9df7SMatt Ranostay 	indio_dev->name = "mcp2221";
1083960f9df7SMatt Ranostay 	indio_dev->modes = INDIO_DIRECT_MODE;
1084960f9df7SMatt Ranostay 	indio_dev->info = &mcp2221_info;
1085960f9df7SMatt Ranostay 	indio_dev->channels = mcp->iio_channels;
1086960f9df7SMatt Ranostay 	indio_dev->num_channels = num_channels;
1087960f9df7SMatt Ranostay 
1088960f9df7SMatt Ranostay 	devm_iio_device_register(&mcp->hdev->dev, indio_dev);
1089960f9df7SMatt Ranostay 
1090960f9df7SMatt Ranostay unlock:
1091960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
1092960f9df7SMatt Ranostay 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1093960f9df7SMatt Ranostay 
1094960f9df7SMatt Ranostay 	return;
1095960f9df7SMatt Ranostay 
1096960f9df7SMatt Ranostay reschedule_task:
1097960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
1098960f9df7SMatt Ranostay 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1099960f9df7SMatt Ranostay 
1100960f9df7SMatt Ranostay 	if (!retries--)
1101960f9df7SMatt Ranostay 		return;
1102960f9df7SMatt Ranostay 
1103960f9df7SMatt Ranostay 	/* Device is not ready to read SRAM or FLASH data, try again */
1104960f9df7SMatt Ranostay 	schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1105960f9df7SMatt Ranostay }
1106960f9df7SMatt Ranostay #endif
1107960f9df7SMatt Ranostay 
110867a95c21SRishi Gupta static int mcp2221_probe(struct hid_device *hdev,
110967a95c21SRishi Gupta 					const struct hid_device_id *id)
111067a95c21SRishi Gupta {
111167a95c21SRishi Gupta 	int ret;
111267a95c21SRishi Gupta 	struct mcp2221 *mcp;
111367a95c21SRishi Gupta 
111467a95c21SRishi Gupta 	mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
111567a95c21SRishi Gupta 	if (!mcp)
111667a95c21SRishi Gupta 		return -ENOMEM;
111767a95c21SRishi Gupta 
111867a95c21SRishi Gupta 	ret = hid_parse(hdev);
111967a95c21SRishi Gupta 	if (ret) {
112067a95c21SRishi Gupta 		hid_err(hdev, "can't parse reports\n");
112167a95c21SRishi Gupta 		return ret;
112267a95c21SRishi Gupta 	}
112367a95c21SRishi Gupta 
112467c90d14SEnrik Berkhan 	/*
112567c90d14SEnrik Berkhan 	 * This driver uses the .raw_event callback and therefore does not need any
112667c90d14SEnrik Berkhan 	 * HID_CONNECT_xxx flags.
112767c90d14SEnrik Berkhan 	 */
112867c90d14SEnrik Berkhan 	ret = hid_hw_start(hdev, 0);
112967a95c21SRishi Gupta 	if (ret) {
113067a95c21SRishi Gupta 		hid_err(hdev, "can't start hardware\n");
113167a95c21SRishi Gupta 		return ret;
113267a95c21SRishi Gupta 	}
113367a95c21SRishi Gupta 
113467c90d14SEnrik Berkhan 	hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8,
113567c90d14SEnrik Berkhan 			hdev->version & 0xff, hdev->name, hdev->phys);
113667c90d14SEnrik Berkhan 
113767a95c21SRishi Gupta 	ret = hid_hw_open(hdev);
113867a95c21SRishi Gupta 	if (ret) {
113967a95c21SRishi Gupta 		hid_err(hdev, "can't open device\n");
1140deb3b88bSMatt Ranostay 		hid_hw_stop(hdev);
1141deb3b88bSMatt Ranostay 		return ret;
114267a95c21SRishi Gupta 	}
114367a95c21SRishi Gupta 
114467a95c21SRishi Gupta 	mutex_init(&mcp->lock);
114567a95c21SRishi Gupta 	init_completion(&mcp->wait_in_report);
114667a95c21SRishi Gupta 	hid_set_drvdata(hdev, mcp);
114767a95c21SRishi Gupta 	mcp->hdev = hdev;
114867a95c21SRishi Gupta 
1149deb3b88bSMatt Ranostay 	ret = devm_add_action_or_reset(&hdev->dev, mcp2221_hid_unregister, hdev);
1150deb3b88bSMatt Ranostay 	if (ret)
1151deb3b88bSMatt Ranostay 		return ret;
1152deb3b88bSMatt Ranostay 
115367a95c21SRishi Gupta 	/* Set I2C bus clock diviser */
115467a95c21SRishi Gupta 	if (i2c_clk_freq > 400)
115567a95c21SRishi Gupta 		i2c_clk_freq = 400;
115667a95c21SRishi Gupta 	if (i2c_clk_freq < 50)
115767a95c21SRishi Gupta 		i2c_clk_freq = 50;
115867a95c21SRishi Gupta 	mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
1159*02a46753SHamish Martin 	ret = mcp_set_i2c_speed(mcp);
1160*02a46753SHamish Martin 	if (ret) {
1161*02a46753SHamish Martin 		hid_err(hdev, "can't set i2c speed: %d\n", ret);
1162*02a46753SHamish Martin 		return ret;
1163*02a46753SHamish Martin 	}
116467a95c21SRishi Gupta 
116567a95c21SRishi Gupta 	mcp->adapter.owner = THIS_MODULE;
116667a95c21SRishi Gupta 	mcp->adapter.class = I2C_CLASS_HWMON;
116767a95c21SRishi Gupta 	mcp->adapter.algo = &mcp_i2c_algo;
116867a95c21SRishi Gupta 	mcp->adapter.retries = 1;
116967a95c21SRishi Gupta 	mcp->adapter.dev.parent = &hdev->dev;
1170d9786159SHamish Martin 	ACPI_COMPANION_SET(&mcp->adapter.dev, ACPI_COMPANION(hdev->dev.parent));
117167a95c21SRishi Gupta 	snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
117267c90d14SEnrik Berkhan 			"MCP2221 usb-i2c bridge");
117367a95c21SRishi Gupta 
1174deb3b88bSMatt Ranostay 	ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter);
117567a95c21SRishi Gupta 	if (ret) {
117667a95c21SRishi Gupta 		hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
1177deb3b88bSMatt Ranostay 		return ret;
117867a95c21SRishi Gupta 	}
117967a95c21SRishi Gupta 	i2c_set_adapdata(&mcp->adapter, mcp);
118067a95c21SRishi Gupta 
11813d74c9ecSMatt Ranostay #if IS_REACHABLE(CONFIG_GPIOLIB)
1182328de1c5SRishi Gupta 	/* Setup GPIO chip */
1183328de1c5SRishi Gupta 	mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
1184deb3b88bSMatt Ranostay 	if (!mcp->gc)
1185deb3b88bSMatt Ranostay 		return -ENOMEM;
1186328de1c5SRishi Gupta 
1187328de1c5SRishi Gupta 	mcp->gc->label = "mcp2221_gpio";
1188328de1c5SRishi Gupta 	mcp->gc->direction_input = mcp_gpio_direction_input;
1189328de1c5SRishi Gupta 	mcp->gc->direction_output = mcp_gpio_direction_output;
1190328de1c5SRishi Gupta 	mcp->gc->get_direction = mcp_gpio_get_direction;
1191328de1c5SRishi Gupta 	mcp->gc->set = mcp_gpio_set;
1192328de1c5SRishi Gupta 	mcp->gc->get = mcp_gpio_get;
1193567b8e9fSLars Povlsen 	mcp->gc->ngpio = MCP_NGPIO;
1194328de1c5SRishi Gupta 	mcp->gc->base = -1;
1195328de1c5SRishi Gupta 	mcp->gc->can_sleep = 1;
1196328de1c5SRishi Gupta 	mcp->gc->parent = &hdev->dev;
1197328de1c5SRishi Gupta 
1198328de1c5SRishi Gupta 	ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
1199328de1c5SRishi Gupta 	if (ret)
1200deb3b88bSMatt Ranostay 		return ret;
1201ea418b35SMatt Ranostay #endif
1202328de1c5SRishi Gupta 
1203960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
1204960f9df7SMatt Ranostay 	INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work);
1205960f9df7SMatt Ranostay 	schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1206960f9df7SMatt Ranostay #endif
1207960f9df7SMatt Ranostay 
120867a95c21SRishi Gupta 	return 0;
120967a95c21SRishi Gupta }
121067a95c21SRishi Gupta 
121167a95c21SRishi Gupta static const struct hid_device_id mcp2221_devices[] = {
121267a95c21SRishi Gupta 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
121367a95c21SRishi Gupta 	{ }
121467a95c21SRishi Gupta };
121567a95c21SRishi Gupta MODULE_DEVICE_TABLE(hid, mcp2221_devices);
121667a95c21SRishi Gupta 
121767a95c21SRishi Gupta static struct hid_driver mcp2221_driver = {
121867a95c21SRishi Gupta 	.name		= "mcp2221",
121967a95c21SRishi Gupta 	.id_table	= mcp2221_devices,
122067a95c21SRishi Gupta 	.probe		= mcp2221_probe,
122167a95c21SRishi Gupta 	.remove		= mcp2221_remove,
122267a95c21SRishi Gupta 	.raw_event	= mcp2221_raw_event,
122367a95c21SRishi Gupta };
122467a95c21SRishi Gupta 
122567a95c21SRishi Gupta /* Register with HID core */
122667a95c21SRishi Gupta module_hid_driver(mcp2221_driver);
122767a95c21SRishi Gupta 
122867a95c21SRishi Gupta MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
122967a95c21SRishi Gupta MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
123067a95c21SRishi Gupta MODULE_LICENSE("GPL v2");
1231