xref: /linux/drivers/hid/hid-mcp2221.c (revision 960f9df7c620ecb6030aff1d9a6c3d67598b8290)
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>
13*960f9df7SMatt 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>
20*960f9df7SMatt 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,
35*960f9df7SMatt Ranostay 	MCP2221_SET_SRAM_SETTINGS = 0x60,
36*960f9df7SMatt Ranostay 	MCP2221_GET_SRAM_SETTINGS = 0x61,
37*960f9df7SMatt 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 direction;
83567b8e9fSLars Povlsen 		u8 value;
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;
97*960f9df7SMatt 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;
106*960f9df7SMatt Ranostay 	u8 mode[4];
107*960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
108*960f9df7SMatt Ranostay 	struct iio_chan_spec iio_channels[3];
109*960f9df7SMatt Ranostay 	u16 adc_values[3];
110*960f9df7SMatt Ranostay 	u8 adc_scale;
111*960f9df7SMatt Ranostay 	u8 dac_value;
112*960f9df7SMatt Ranostay 	u16 dac_scale;
113*960f9df7SMatt Ranostay #endif
114*960f9df7SMatt Ranostay };
115*960f9df7SMatt Ranostay 
116*960f9df7SMatt Ranostay struct mcp2221_iio {
117*960f9df7SMatt Ranostay 	struct mcp2221 *mcp;
11867a95c21SRishi Gupta };
11967a95c21SRishi Gupta 
12067a95c21SRishi Gupta /*
12167a95c21SRishi Gupta  * Default i2c bus clock frequency 400 kHz. Modify this if you
12267a95c21SRishi Gupta  * want to set some other frequency (min 50 kHz - max 400 kHz).
12367a95c21SRishi Gupta  */
12467a95c21SRishi Gupta static uint i2c_clk_freq = 400;
12567a95c21SRishi Gupta 
12667a95c21SRishi Gupta /* Synchronously send output report to the device */
12767a95c21SRishi Gupta static int mcp_send_report(struct mcp2221 *mcp,
12867a95c21SRishi Gupta 					u8 *out_report, size_t len)
12967a95c21SRishi Gupta {
13067a95c21SRishi Gupta 	u8 *buf;
13167a95c21SRishi Gupta 	int ret;
13267a95c21SRishi Gupta 
13367a95c21SRishi Gupta 	buf = kmemdup(out_report, len, GFP_KERNEL);
13467a95c21SRishi Gupta 	if (!buf)
13567a95c21SRishi Gupta 		return -ENOMEM;
13667a95c21SRishi Gupta 
13767a95c21SRishi Gupta 	/* mcp2221 uses interrupt endpoint for out reports */
13867a95c21SRishi Gupta 	ret = hid_hw_output_report(mcp->hdev, buf, len);
13967a95c21SRishi Gupta 	kfree(buf);
14067a95c21SRishi Gupta 
14167a95c21SRishi Gupta 	if (ret < 0)
14267a95c21SRishi Gupta 		return ret;
14367a95c21SRishi Gupta 	return 0;
14467a95c21SRishi Gupta }
14567a95c21SRishi Gupta 
14667a95c21SRishi Gupta /*
14767a95c21SRishi Gupta  * Send o/p report to the device and wait for i/p report to be
14867a95c21SRishi Gupta  * received from the device. If the device does not respond,
14967a95c21SRishi Gupta  * we timeout.
15067a95c21SRishi Gupta  */
15167a95c21SRishi Gupta static int mcp_send_data_req_status(struct mcp2221 *mcp,
15267a95c21SRishi Gupta 			u8 *out_report, int len)
15367a95c21SRishi Gupta {
15467a95c21SRishi Gupta 	int ret;
15567a95c21SRishi Gupta 	unsigned long t;
15667a95c21SRishi Gupta 
15767a95c21SRishi Gupta 	reinit_completion(&mcp->wait_in_report);
15867a95c21SRishi Gupta 
15967a95c21SRishi Gupta 	ret = mcp_send_report(mcp, out_report, len);
16067a95c21SRishi Gupta 	if (ret)
16167a95c21SRishi Gupta 		return ret;
16267a95c21SRishi Gupta 
16367a95c21SRishi Gupta 	t = wait_for_completion_timeout(&mcp->wait_in_report,
16467a95c21SRishi Gupta 							msecs_to_jiffies(4000));
16567a95c21SRishi Gupta 	if (!t)
16667a95c21SRishi Gupta 		return -ETIMEDOUT;
16767a95c21SRishi Gupta 
16867a95c21SRishi Gupta 	return mcp->status;
16967a95c21SRishi Gupta }
17067a95c21SRishi Gupta 
17167a95c21SRishi Gupta /* Check pass/fail for actual communication with i2c slave */
17267a95c21SRishi Gupta static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
17367a95c21SRishi Gupta {
17467a95c21SRishi Gupta 	memset(mcp->txbuf, 0, 8);
17567a95c21SRishi Gupta 	mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
17667a95c21SRishi Gupta 
17767a95c21SRishi Gupta 	return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
17867a95c21SRishi Gupta }
17967a95c21SRishi Gupta 
18067a95c21SRishi Gupta /* Cancels last command releasing i2c bus just in case occupied */
18167a95c21SRishi Gupta static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
18267a95c21SRishi Gupta {
18367a95c21SRishi Gupta 	memset(mcp->txbuf, 0, 8);
18467a95c21SRishi Gupta 	mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
18567a95c21SRishi Gupta 	mcp->txbuf[2] = MCP2221_I2C_CANCEL;
18667a95c21SRishi Gupta 
18767a95c21SRishi Gupta 	return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
18867a95c21SRishi Gupta }
18967a95c21SRishi Gupta 
19067a95c21SRishi Gupta static int mcp_set_i2c_speed(struct mcp2221 *mcp)
19167a95c21SRishi Gupta {
19267a95c21SRishi Gupta 	int ret;
19367a95c21SRishi Gupta 
19467a95c21SRishi Gupta 	memset(mcp->txbuf, 0, 8);
19567a95c21SRishi Gupta 	mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
19667a95c21SRishi Gupta 	mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
19767a95c21SRishi Gupta 	mcp->txbuf[4] = mcp->cur_i2c_clk_div;
19867a95c21SRishi Gupta 
19967a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
20067a95c21SRishi Gupta 	if (ret) {
20167a95c21SRishi Gupta 		/* Small delay is needed here */
20267a95c21SRishi Gupta 		usleep_range(980, 1000);
20367a95c21SRishi Gupta 		mcp_cancel_last_cmd(mcp);
20467a95c21SRishi Gupta 	}
20567a95c21SRishi Gupta 
20667a95c21SRishi Gupta 	return 0;
20767a95c21SRishi Gupta }
20867a95c21SRishi Gupta 
20967a95c21SRishi Gupta /*
21067a95c21SRishi Gupta  * An output report can contain minimum 1 and maximum 60 user data
21167a95c21SRishi Gupta  * bytes. If the number of data bytes is more then 60, we send it
21267a95c21SRishi Gupta  * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
21367a95c21SRishi Gupta  * bytes. Total number of bytes is informed in very first report to
21467a95c21SRishi Gupta  * mcp2221, from that point onwards it first collect all the data
21567a95c21SRishi Gupta  * from host and then send to i2c slave device.
21667a95c21SRishi Gupta  */
21767a95c21SRishi Gupta static int mcp_i2c_write(struct mcp2221 *mcp,
21867a95c21SRishi Gupta 				struct i2c_msg *msg, int type, u8 last_status)
21967a95c21SRishi Gupta {
22067a95c21SRishi Gupta 	int ret, len, idx, sent;
22167a95c21SRishi Gupta 
22267a95c21SRishi Gupta 	idx = 0;
22367a95c21SRishi Gupta 	sent  = 0;
22467a95c21SRishi Gupta 	if (msg->len < 60)
22567a95c21SRishi Gupta 		len = msg->len;
22667a95c21SRishi Gupta 	else
22767a95c21SRishi Gupta 		len = 60;
22867a95c21SRishi Gupta 
22967a95c21SRishi Gupta 	do {
23067a95c21SRishi Gupta 		mcp->txbuf[0] = type;
23167a95c21SRishi Gupta 		mcp->txbuf[1] = msg->len & 0xff;
23267a95c21SRishi Gupta 		mcp->txbuf[2] = msg->len >> 8;
23367a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(msg->addr << 1);
23467a95c21SRishi Gupta 
23567a95c21SRishi Gupta 		memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
23667a95c21SRishi Gupta 
23767a95c21SRishi Gupta 		ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
23867a95c21SRishi Gupta 		if (ret)
23967a95c21SRishi Gupta 			return ret;
24067a95c21SRishi Gupta 
24167a95c21SRishi Gupta 		usleep_range(980, 1000);
24267a95c21SRishi Gupta 
24367a95c21SRishi Gupta 		if (last_status) {
24467a95c21SRishi Gupta 			ret = mcp_chk_last_cmd_status(mcp);
24567a95c21SRishi Gupta 			if (ret)
24667a95c21SRishi Gupta 				return ret;
24767a95c21SRishi Gupta 		}
24867a95c21SRishi Gupta 
24967a95c21SRishi Gupta 		sent = sent + len;
25067a95c21SRishi Gupta 		if (sent >= msg->len)
25167a95c21SRishi Gupta 			break;
25267a95c21SRishi Gupta 
25367a95c21SRishi Gupta 		idx = idx + len;
25467a95c21SRishi Gupta 		if ((msg->len - sent) < 60)
25567a95c21SRishi Gupta 			len = msg->len - sent;
25667a95c21SRishi Gupta 		else
25767a95c21SRishi Gupta 			len = 60;
25867a95c21SRishi Gupta 
25967a95c21SRishi Gupta 		/*
26067a95c21SRishi Gupta 		 * Testing shows delay is needed between successive writes
26167a95c21SRishi Gupta 		 * otherwise next write fails on first-try from i2c core.
26267a95c21SRishi Gupta 		 * This value is obtained through automated stress testing.
26367a95c21SRishi Gupta 		 */
26467a95c21SRishi Gupta 		usleep_range(980, 1000);
26567a95c21SRishi Gupta 	} while (len > 0);
26667a95c21SRishi Gupta 
26767a95c21SRishi Gupta 	return ret;
26867a95c21SRishi Gupta }
26967a95c21SRishi Gupta 
27067a95c21SRishi Gupta /*
27167a95c21SRishi Gupta  * Device reads all data (0 - 65535 bytes) from i2c slave device and
27267a95c21SRishi Gupta  * stores it in device itself. This data is read back from device to
27367a95c21SRishi Gupta  * host in multiples of 60 bytes using input reports.
27467a95c21SRishi Gupta  */
27567a95c21SRishi Gupta static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
27667a95c21SRishi Gupta 				struct i2c_msg *msg, int type, u16 smbus_addr,
27767a95c21SRishi Gupta 				u8 smbus_len, u8 *smbus_buf)
27867a95c21SRishi Gupta {
27967a95c21SRishi Gupta 	int ret;
28067a95c21SRishi Gupta 	u16 total_len;
28167a95c21SRishi Gupta 
28267a95c21SRishi Gupta 	mcp->txbuf[0] = type;
28367a95c21SRishi Gupta 	if (msg) {
28467a95c21SRishi Gupta 		mcp->txbuf[1] = msg->len & 0xff;
28567a95c21SRishi Gupta 		mcp->txbuf[2] = msg->len >> 8;
28667a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(msg->addr << 1);
28767a95c21SRishi Gupta 		total_len = msg->len;
28867a95c21SRishi Gupta 		mcp->rxbuf = msg->buf;
28967a95c21SRishi Gupta 	} else {
29067a95c21SRishi Gupta 		mcp->txbuf[1] = smbus_len;
29167a95c21SRishi Gupta 		mcp->txbuf[2] = 0;
29267a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(smbus_addr << 1);
29367a95c21SRishi Gupta 		total_len = smbus_len;
29467a95c21SRishi Gupta 		mcp->rxbuf = smbus_buf;
29567a95c21SRishi Gupta 	}
29667a95c21SRishi Gupta 
29767a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
29867a95c21SRishi Gupta 	if (ret)
29967a95c21SRishi Gupta 		return ret;
30067a95c21SRishi Gupta 
30167a95c21SRishi Gupta 	mcp->rxbuf_idx = 0;
30267a95c21SRishi Gupta 
30367a95c21SRishi Gupta 	do {
30467a95c21SRishi Gupta 		memset(mcp->txbuf, 0, 4);
30567a95c21SRishi Gupta 		mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
30667a95c21SRishi Gupta 
30767a95c21SRishi Gupta 		ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
30867a95c21SRishi Gupta 		if (ret)
30967a95c21SRishi Gupta 			return ret;
31067a95c21SRishi Gupta 
31167a95c21SRishi Gupta 		ret = mcp_chk_last_cmd_status(mcp);
31267a95c21SRishi Gupta 		if (ret)
31367a95c21SRishi Gupta 			return ret;
31467a95c21SRishi Gupta 
31567a95c21SRishi Gupta 		usleep_range(980, 1000);
31667a95c21SRishi Gupta 	} while (mcp->rxbuf_idx < total_len);
31767a95c21SRishi Gupta 
31867a95c21SRishi Gupta 	return ret;
31967a95c21SRishi Gupta }
32067a95c21SRishi Gupta 
32167a95c21SRishi Gupta static int mcp_i2c_xfer(struct i2c_adapter *adapter,
32267a95c21SRishi Gupta 				struct i2c_msg msgs[], int num)
32367a95c21SRishi Gupta {
32467a95c21SRishi Gupta 	int ret;
32567a95c21SRishi Gupta 	struct mcp2221 *mcp = i2c_get_adapdata(adapter);
32667a95c21SRishi Gupta 
32767a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
32867a95c21SRishi Gupta 
32967a95c21SRishi Gupta 	mutex_lock(&mcp->lock);
33067a95c21SRishi Gupta 
33167a95c21SRishi Gupta 	/* Setting speed before every transaction is required for mcp2221 */
33267a95c21SRishi Gupta 	ret = mcp_set_i2c_speed(mcp);
33367a95c21SRishi Gupta 	if (ret)
33467a95c21SRishi Gupta 		goto exit;
33567a95c21SRishi Gupta 
33667a95c21SRishi Gupta 	if (num == 1) {
33767a95c21SRishi Gupta 		if (msgs->flags & I2C_M_RD) {
33867a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
33967a95c21SRishi Gupta 							0, 0, NULL);
34067a95c21SRishi Gupta 		} else {
34167a95c21SRishi Gupta 			ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
34267a95c21SRishi Gupta 		}
34367a95c21SRishi Gupta 		if (ret)
34467a95c21SRishi Gupta 			goto exit;
34567a95c21SRishi Gupta 		ret = num;
34667a95c21SRishi Gupta 	} else if (num == 2) {
34767a95c21SRishi Gupta 		/* Ex transaction; send reg address and read its contents */
34867a95c21SRishi Gupta 		if (msgs[0].addr == msgs[1].addr &&
34967a95c21SRishi Gupta 			!(msgs[0].flags & I2C_M_RD) &&
35067a95c21SRishi Gupta 			 (msgs[1].flags & I2C_M_RD)) {
35167a95c21SRishi Gupta 
35267a95c21SRishi Gupta 			ret = mcp_i2c_write(mcp, &msgs[0],
35367a95c21SRishi Gupta 						MCP2221_I2C_WR_NO_STOP, 0);
35467a95c21SRishi Gupta 			if (ret)
35567a95c21SRishi Gupta 				goto exit;
35667a95c21SRishi Gupta 
35767a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, &msgs[1],
35867a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
35967a95c21SRishi Gupta 						0, 0, NULL);
36067a95c21SRishi Gupta 			if (ret)
36167a95c21SRishi Gupta 				goto exit;
36267a95c21SRishi Gupta 			ret = num;
36367a95c21SRishi Gupta 		} else {
36467a95c21SRishi Gupta 			dev_err(&adapter->dev,
36567a95c21SRishi Gupta 				"unsupported multi-msg i2c transaction\n");
36667a95c21SRishi Gupta 			ret = -EOPNOTSUPP;
36767a95c21SRishi Gupta 		}
36867a95c21SRishi Gupta 	} else {
36967a95c21SRishi Gupta 		dev_err(&adapter->dev,
37067a95c21SRishi Gupta 			"unsupported multi-msg i2c transaction\n");
37167a95c21SRishi Gupta 		ret = -EOPNOTSUPP;
37267a95c21SRishi Gupta 	}
37367a95c21SRishi Gupta 
37467a95c21SRishi Gupta exit:
37567a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
37667a95c21SRishi Gupta 	mutex_unlock(&mcp->lock);
37767a95c21SRishi Gupta 	return ret;
37867a95c21SRishi Gupta }
37967a95c21SRishi Gupta 
38067a95c21SRishi Gupta static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
38167a95c21SRishi Gupta 				u8 command, u8 *buf, u8 len, int type,
38267a95c21SRishi Gupta 				u8 last_status)
38367a95c21SRishi Gupta {
38467a95c21SRishi Gupta 	int data_len, ret;
38567a95c21SRishi Gupta 
38667a95c21SRishi Gupta 	mcp->txbuf[0] = type;
38767a95c21SRishi Gupta 	mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
38867a95c21SRishi Gupta 	mcp->txbuf[2] = 0;
38967a95c21SRishi Gupta 	mcp->txbuf[3] = (u8)(addr << 1);
39067a95c21SRishi Gupta 	mcp->txbuf[4] = command;
39167a95c21SRishi Gupta 
39267a95c21SRishi Gupta 	switch (len) {
39367a95c21SRishi Gupta 	case 0:
39467a95c21SRishi Gupta 		data_len = 5;
39567a95c21SRishi Gupta 		break;
39667a95c21SRishi Gupta 	case 1:
39767a95c21SRishi Gupta 		mcp->txbuf[5] = buf[0];
39867a95c21SRishi Gupta 		data_len = 6;
39967a95c21SRishi Gupta 		break;
40067a95c21SRishi Gupta 	case 2:
40167a95c21SRishi Gupta 		mcp->txbuf[5] = buf[0];
40267a95c21SRishi Gupta 		mcp->txbuf[6] = buf[1];
40367a95c21SRishi Gupta 		data_len = 7;
40467a95c21SRishi Gupta 		break;
40567a95c21SRishi Gupta 	default:
40662ac2473SHarshit Mogalapalli 		if (len > I2C_SMBUS_BLOCK_MAX)
40762ac2473SHarshit Mogalapalli 			return -EINVAL;
40862ac2473SHarshit Mogalapalli 
40967a95c21SRishi Gupta 		memcpy(&mcp->txbuf[5], buf, len);
41067a95c21SRishi Gupta 		data_len = len + 5;
41167a95c21SRishi Gupta 	}
41267a95c21SRishi Gupta 
41367a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
41467a95c21SRishi Gupta 	if (ret)
41567a95c21SRishi Gupta 		return ret;
41667a95c21SRishi Gupta 
41767a95c21SRishi Gupta 	if (last_status) {
41867a95c21SRishi Gupta 		usleep_range(980, 1000);
41967a95c21SRishi Gupta 
42067a95c21SRishi Gupta 		ret = mcp_chk_last_cmd_status(mcp);
42167a95c21SRishi Gupta 		if (ret)
42267a95c21SRishi Gupta 			return ret;
42367a95c21SRishi Gupta 	}
42467a95c21SRishi Gupta 
42567a95c21SRishi Gupta 	return ret;
42667a95c21SRishi Gupta }
42767a95c21SRishi Gupta 
42867a95c21SRishi Gupta static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
42967a95c21SRishi Gupta 				unsigned short flags, char read_write,
43067a95c21SRishi Gupta 				u8 command, int size,
43167a95c21SRishi Gupta 				union i2c_smbus_data *data)
43267a95c21SRishi Gupta {
43367a95c21SRishi Gupta 	int ret;
43467a95c21SRishi Gupta 	struct mcp2221 *mcp = i2c_get_adapdata(adapter);
43567a95c21SRishi Gupta 
43667a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
43767a95c21SRishi Gupta 
43867a95c21SRishi Gupta 	mutex_lock(&mcp->lock);
43967a95c21SRishi Gupta 
44067a95c21SRishi Gupta 	ret = mcp_set_i2c_speed(mcp);
44167a95c21SRishi Gupta 	if (ret)
44267a95c21SRishi Gupta 		goto exit;
44367a95c21SRishi Gupta 
44467a95c21SRishi Gupta 	switch (size) {
44567a95c21SRishi Gupta 
44667a95c21SRishi Gupta 	case I2C_SMBUS_QUICK:
44767a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ)
44867a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
44967a95c21SRishi Gupta 						addr, 0, &data->byte);
45067a95c21SRishi Gupta 		else
45167a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
45267a95c21SRishi Gupta 						0, MCP2221_I2C_WR_DATA, 1);
45367a95c21SRishi Gupta 		break;
45467a95c21SRishi Gupta 	case I2C_SMBUS_BYTE:
45567a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ)
45667a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
45767a95c21SRishi Gupta 						addr, 1, &data->byte);
45867a95c21SRishi Gupta 		else
45967a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
46067a95c21SRishi Gupta 						0, MCP2221_I2C_WR_DATA, 1);
46167a95c21SRishi Gupta 		break;
46267a95c21SRishi Gupta 	case I2C_SMBUS_BYTE_DATA:
46367a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
46467a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
46567a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 0);
46667a95c21SRishi Gupta 			if (ret)
46767a95c21SRishi Gupta 				goto exit;
46867a95c21SRishi Gupta 
46967a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL,
47067a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
47167a95c21SRishi Gupta 						addr, 1, &data->byte);
47267a95c21SRishi Gupta 		} else {
47367a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, &data->byte,
47467a95c21SRishi Gupta 						1, MCP2221_I2C_WR_DATA, 1);
47567a95c21SRishi Gupta 		}
47667a95c21SRishi Gupta 		break;
47767a95c21SRishi Gupta 	case I2C_SMBUS_WORD_DATA:
47867a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
47967a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
48067a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 0);
48167a95c21SRishi Gupta 			if (ret)
48267a95c21SRishi Gupta 				goto exit;
48367a95c21SRishi Gupta 
48467a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL,
48567a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
48667a95c21SRishi Gupta 						addr, 2, (u8 *)&data->word);
48767a95c21SRishi Gupta 		} else {
48867a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command,
48967a95c21SRishi Gupta 						(u8 *)&data->word, 2,
49067a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
49167a95c21SRishi Gupta 		}
49267a95c21SRishi Gupta 		break;
49367a95c21SRishi Gupta 	case I2C_SMBUS_BLOCK_DATA:
49467a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
49567a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
49667a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 1);
49767a95c21SRishi Gupta 			if (ret)
49867a95c21SRishi Gupta 				goto exit;
49967a95c21SRishi Gupta 
50067a95c21SRishi Gupta 			mcp->rxbuf_idx = 0;
50167a95c21SRishi Gupta 			mcp->rxbuf = data->block;
50267a95c21SRishi Gupta 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
50367a95c21SRishi Gupta 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
50467a95c21SRishi Gupta 			if (ret)
50567a95c21SRishi Gupta 				goto exit;
50667a95c21SRishi Gupta 		} else {
50767a95c21SRishi Gupta 			if (!data->block[0]) {
50867a95c21SRishi Gupta 				ret = -EINVAL;
50967a95c21SRishi Gupta 				goto exit;
51067a95c21SRishi Gupta 			}
51167a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, data->block,
51267a95c21SRishi Gupta 						data->block[0] + 1,
51367a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
51467a95c21SRishi Gupta 		}
51567a95c21SRishi Gupta 		break;
51667a95c21SRishi Gupta 	case I2C_SMBUS_I2C_BLOCK_DATA:
51767a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
51867a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
51967a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 1);
52067a95c21SRishi Gupta 			if (ret)
52167a95c21SRishi Gupta 				goto exit;
52267a95c21SRishi Gupta 
52367a95c21SRishi Gupta 			mcp->rxbuf_idx = 0;
52467a95c21SRishi Gupta 			mcp->rxbuf = data->block;
52567a95c21SRishi Gupta 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
52667a95c21SRishi Gupta 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
52767a95c21SRishi Gupta 			if (ret)
52867a95c21SRishi Gupta 				goto exit;
52967a95c21SRishi Gupta 		} else {
53067a95c21SRishi Gupta 			if (!data->block[0]) {
53167a95c21SRishi Gupta 				ret = -EINVAL;
53267a95c21SRishi Gupta 				goto exit;
53367a95c21SRishi Gupta 			}
53467a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command,
53567a95c21SRishi Gupta 						&data->block[1], data->block[0],
53667a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
53767a95c21SRishi Gupta 		}
53867a95c21SRishi Gupta 		break;
53967a95c21SRishi Gupta 	case I2C_SMBUS_PROC_CALL:
54067a95c21SRishi Gupta 		ret = mcp_smbus_write(mcp, addr, command,
54167a95c21SRishi Gupta 						(u8 *)&data->word,
54267a95c21SRishi Gupta 						2, MCP2221_I2C_WR_NO_STOP, 0);
54367a95c21SRishi Gupta 		if (ret)
54467a95c21SRishi Gupta 			goto exit;
54567a95c21SRishi Gupta 
54667a95c21SRishi Gupta 		ret = mcp_i2c_smbus_read(mcp, NULL,
54767a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
54867a95c21SRishi Gupta 						addr, 2, (u8 *)&data->word);
54967a95c21SRishi Gupta 		break;
55067a95c21SRishi Gupta 	case I2C_SMBUS_BLOCK_PROC_CALL:
55167a95c21SRishi Gupta 		ret = mcp_smbus_write(mcp, addr, command, data->block,
55267a95c21SRishi Gupta 						data->block[0] + 1,
55367a95c21SRishi Gupta 						MCP2221_I2C_WR_NO_STOP, 0);
55467a95c21SRishi Gupta 		if (ret)
55567a95c21SRishi Gupta 			goto exit;
55667a95c21SRishi Gupta 
55767a95c21SRishi Gupta 		ret = mcp_i2c_smbus_read(mcp, NULL,
55867a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
55967a95c21SRishi Gupta 						addr, I2C_SMBUS_BLOCK_MAX,
56067a95c21SRishi Gupta 						data->block);
56167a95c21SRishi Gupta 		break;
56267a95c21SRishi Gupta 	default:
56367a95c21SRishi Gupta 		dev_err(&mcp->adapter.dev,
56467a95c21SRishi Gupta 			"unsupported smbus transaction size:%d\n", size);
56567a95c21SRishi Gupta 		ret = -EOPNOTSUPP;
56667a95c21SRishi Gupta 	}
56767a95c21SRishi Gupta 
56867a95c21SRishi Gupta exit:
56967a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
57067a95c21SRishi Gupta 	mutex_unlock(&mcp->lock);
57167a95c21SRishi Gupta 	return ret;
57267a95c21SRishi Gupta }
57367a95c21SRishi Gupta 
57467a95c21SRishi Gupta static u32 mcp_i2c_func(struct i2c_adapter *adapter)
57567a95c21SRishi Gupta {
57667a95c21SRishi Gupta 	return I2C_FUNC_I2C |
57767a95c21SRishi Gupta 			I2C_FUNC_SMBUS_READ_BLOCK_DATA |
57867a95c21SRishi Gupta 			I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
57967a95c21SRishi Gupta 			(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
58067a95c21SRishi Gupta }
58167a95c21SRishi Gupta 
58267a95c21SRishi Gupta static const struct i2c_algorithm mcp_i2c_algo = {
58367a95c21SRishi Gupta 	.master_xfer = mcp_i2c_xfer,
58467a95c21SRishi Gupta 	.smbus_xfer = mcp_smbus_xfer,
58567a95c21SRishi Gupta 	.functionality = mcp_i2c_func,
58667a95c21SRishi Gupta };
58767a95c21SRishi Gupta 
588328de1c5SRishi Gupta static int mcp_gpio_get(struct gpio_chip *gc,
589328de1c5SRishi Gupta 				unsigned int offset)
590328de1c5SRishi Gupta {
591328de1c5SRishi Gupta 	int ret;
592328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
593328de1c5SRishi Gupta 
594328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_GET;
595328de1c5SRishi Gupta 
596567b8e9fSLars Povlsen 	mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].value);
597328de1c5SRishi Gupta 
598328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
599328de1c5SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
600328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
601328de1c5SRishi Gupta 
602328de1c5SRishi Gupta 	return ret;
603328de1c5SRishi Gupta }
604328de1c5SRishi Gupta 
605328de1c5SRishi Gupta static void mcp_gpio_set(struct gpio_chip *gc,
606328de1c5SRishi Gupta 				unsigned int offset, int value)
607328de1c5SRishi Gupta {
608328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
609328de1c5SRishi Gupta 
610328de1c5SRishi Gupta 	memset(mcp->txbuf, 0, 18);
611328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_SET;
612328de1c5SRishi Gupta 
613567b8e9fSLars Povlsen 	mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
614328de1c5SRishi Gupta 
615328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx - 1] = 1;
616328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx] = !!value;
617328de1c5SRishi Gupta 
618328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
619328de1c5SRishi Gupta 	mcp_send_data_req_status(mcp, mcp->txbuf, 18);
620328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
621328de1c5SRishi Gupta }
622328de1c5SRishi Gupta 
623328de1c5SRishi Gupta static int mcp_gpio_dir_set(struct mcp2221 *mcp,
624328de1c5SRishi Gupta 				unsigned int offset, u8 val)
625328de1c5SRishi Gupta {
626328de1c5SRishi Gupta 	memset(mcp->txbuf, 0, 18);
627328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_SET;
628328de1c5SRishi Gupta 
629567b8e9fSLars Povlsen 	mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
630328de1c5SRishi Gupta 
631328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx - 1] = 1;
632328de1c5SRishi Gupta 	mcp->txbuf[mcp->gp_idx] = val;
633328de1c5SRishi Gupta 
634328de1c5SRishi Gupta 	return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
635328de1c5SRishi Gupta }
636328de1c5SRishi Gupta 
637328de1c5SRishi Gupta static int mcp_gpio_direction_input(struct gpio_chip *gc,
638328de1c5SRishi Gupta 				unsigned int offset)
639328de1c5SRishi Gupta {
640328de1c5SRishi Gupta 	int ret;
641328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
642328de1c5SRishi Gupta 
643328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
644567b8e9fSLars Povlsen 	ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
645328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
646328de1c5SRishi Gupta 
647328de1c5SRishi Gupta 	return ret;
648328de1c5SRishi Gupta }
649328de1c5SRishi Gupta 
650328de1c5SRishi Gupta static int mcp_gpio_direction_output(struct gpio_chip *gc,
651328de1c5SRishi Gupta 				unsigned int offset, int value)
652328de1c5SRishi Gupta {
653328de1c5SRishi Gupta 	int ret;
654328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
655328de1c5SRishi Gupta 
656328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
657567b8e9fSLars Povlsen 	ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
658328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
659328de1c5SRishi Gupta 
660328de1c5SRishi Gupta 	/* Can't configure as output, bailout early */
661328de1c5SRishi Gupta 	if (ret)
662328de1c5SRishi Gupta 		return ret;
663328de1c5SRishi Gupta 
664328de1c5SRishi Gupta 	mcp_gpio_set(gc, offset, value);
665328de1c5SRishi Gupta 
666328de1c5SRishi Gupta 	return 0;
667328de1c5SRishi Gupta }
668328de1c5SRishi Gupta 
669328de1c5SRishi Gupta static int mcp_gpio_get_direction(struct gpio_chip *gc,
670328de1c5SRishi Gupta 				unsigned int offset)
671328de1c5SRishi Gupta {
672328de1c5SRishi Gupta 	int ret;
673328de1c5SRishi Gupta 	struct mcp2221 *mcp = gpiochip_get_data(gc);
674328de1c5SRishi Gupta 
675328de1c5SRishi Gupta 	mcp->txbuf[0] = MCP2221_GPIO_GET;
676328de1c5SRishi Gupta 
677567b8e9fSLars Povlsen 	mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].direction);
678328de1c5SRishi Gupta 
679328de1c5SRishi Gupta 	mutex_lock(&mcp->lock);
680328de1c5SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
681328de1c5SRishi Gupta 	mutex_unlock(&mcp->lock);
682328de1c5SRishi Gupta 
683328de1c5SRishi Gupta 	if (ret)
684328de1c5SRishi Gupta 		return ret;
685328de1c5SRishi Gupta 
686567b8e9fSLars Povlsen 	if (mcp->gpio_dir == MCP2221_DIR_IN)
687328de1c5SRishi Gupta 		return GPIO_LINE_DIRECTION_IN;
688328de1c5SRishi Gupta 
689328de1c5SRishi Gupta 	return GPIO_LINE_DIRECTION_OUT;
690328de1c5SRishi Gupta }
691328de1c5SRishi Gupta 
69267a95c21SRishi Gupta /* Gives current state of i2c engine inside mcp2221 */
69367a95c21SRishi Gupta static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
69467a95c21SRishi Gupta 				u8 *data, u8 idx)
69567a95c21SRishi Gupta {
69667a95c21SRishi Gupta 	int ret;
69767a95c21SRishi Gupta 
69867a95c21SRishi Gupta 	switch (data[idx]) {
69967a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_NACK:
70067a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_SEND:
70167a95c21SRishi Gupta 		ret = -ENXIO;
70267a95c21SRishi Gupta 		break;
70367a95c21SRishi Gupta 	case MCP2221_I2C_START_TOUT:
70467a95c21SRishi Gupta 	case MCP2221_I2C_STOP_TOUT:
70567a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_TOUT:
70667a95c21SRishi Gupta 	case MCP2221_I2C_WRDATA_TOUT:
70767a95c21SRishi Gupta 		ret = -ETIMEDOUT;
70867a95c21SRishi Gupta 		break;
70967a95c21SRishi Gupta 	case MCP2221_I2C_ENG_BUSY:
71067a95c21SRishi Gupta 		ret = -EAGAIN;
71167a95c21SRishi Gupta 		break;
71267a95c21SRishi Gupta 	case MCP2221_SUCCESS:
71367a95c21SRishi Gupta 		ret = 0x00;
71467a95c21SRishi Gupta 		break;
71567a95c21SRishi Gupta 	default:
71667a95c21SRishi Gupta 		ret = -EIO;
71767a95c21SRishi Gupta 	}
71867a95c21SRishi Gupta 
71967a95c21SRishi Gupta 	return ret;
72067a95c21SRishi Gupta }
72167a95c21SRishi Gupta 
72267a95c21SRishi Gupta /*
72367a95c21SRishi Gupta  * MCP2221 uses interrupt endpoint for input reports. This function
72467a95c21SRishi Gupta  * is called by HID layer when it receives i/p report from mcp2221,
72567a95c21SRishi Gupta  * which is actually a response to the previously sent command.
72667a95c21SRishi Gupta  *
72767a95c21SRishi Gupta  * MCP2221A firmware specific return codes are parsed and 0 or
72867a95c21SRishi Gupta  * appropriate negative error code is returned. Delayed response
72967a95c21SRishi Gupta  * results in timeout error and stray reponses results in -EIO.
73067a95c21SRishi Gupta  */
73167a95c21SRishi Gupta static int mcp2221_raw_event(struct hid_device *hdev,
73267a95c21SRishi Gupta 				struct hid_report *report, u8 *data, int size)
73367a95c21SRishi Gupta {
734*960f9df7SMatt Ranostay 	u8 *buf, tmp;
73567a95c21SRishi Gupta 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
73667a95c21SRishi Gupta 
73767a95c21SRishi Gupta 	switch (data[0]) {
73867a95c21SRishi Gupta 
73967a95c21SRishi Gupta 	case MCP2221_I2C_WR_DATA:
74067a95c21SRishi Gupta 	case MCP2221_I2C_WR_NO_STOP:
74167a95c21SRishi Gupta 	case MCP2221_I2C_RD_DATA:
74267a95c21SRishi Gupta 	case MCP2221_I2C_RD_RPT_START:
74367a95c21SRishi Gupta 		switch (data[1]) {
74467a95c21SRishi Gupta 		case MCP2221_SUCCESS:
74567a95c21SRishi Gupta 			mcp->status = 0;
74667a95c21SRishi Gupta 			break;
74767a95c21SRishi Gupta 		default:
74867a95c21SRishi Gupta 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
74967a95c21SRishi Gupta 		}
75067a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
75167a95c21SRishi Gupta 		break;
75267a95c21SRishi Gupta 
75367a95c21SRishi Gupta 	case MCP2221_I2C_PARAM_OR_STATUS:
75467a95c21SRishi Gupta 		switch (data[1]) {
75567a95c21SRishi Gupta 		case MCP2221_SUCCESS:
75667a95c21SRishi Gupta 			if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
75767a95c21SRishi Gupta 				(data[3] != MCP2221_I2C_SET_SPEED)) {
75867a95c21SRishi Gupta 				mcp->status = -EAGAIN;
75967a95c21SRishi Gupta 				break;
76067a95c21SRishi Gupta 			}
76167a95c21SRishi Gupta 			if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
76267a95c21SRishi Gupta 				mcp->status = -ENXIO;
76367a95c21SRishi Gupta 				break;
76467a95c21SRishi Gupta 			}
76567a95c21SRishi Gupta 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
766*960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
767*960f9df7SMatt Ranostay 			memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values));
768*960f9df7SMatt Ranostay #endif
76967a95c21SRishi Gupta 			break;
77067a95c21SRishi Gupta 		default:
77167a95c21SRishi Gupta 			mcp->status = -EIO;
77267a95c21SRishi Gupta 		}
77367a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
77467a95c21SRishi Gupta 		break;
77567a95c21SRishi Gupta 
77667a95c21SRishi Gupta 	case MCP2221_I2C_GET_DATA:
77767a95c21SRishi Gupta 		switch (data[1]) {
77867a95c21SRishi Gupta 		case MCP2221_SUCCESS:
77967a95c21SRishi Gupta 			if (data[2] == MCP2221_I2C_ADDR_NACK) {
78067a95c21SRishi Gupta 				mcp->status = -ENXIO;
78167a95c21SRishi Gupta 				break;
78267a95c21SRishi Gupta 			}
78367a95c21SRishi Gupta 			if (!mcp_get_i2c_eng_state(mcp, data, 2)
78467a95c21SRishi Gupta 				&& (data[3] == 0)) {
78567a95c21SRishi Gupta 				mcp->status = 0;
78667a95c21SRishi Gupta 				break;
78767a95c21SRishi Gupta 			}
78867a95c21SRishi Gupta 			if (data[3] == 127) {
78967a95c21SRishi Gupta 				mcp->status = -EIO;
79067a95c21SRishi Gupta 				break;
79167a95c21SRishi Gupta 			}
79267a95c21SRishi Gupta 			if (data[2] == MCP2221_I2C_READ_COMPL) {
79367a95c21SRishi Gupta 				buf = mcp->rxbuf;
79467a95c21SRishi Gupta 				memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
79567a95c21SRishi Gupta 				mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
79667a95c21SRishi Gupta 				mcp->status = 0;
79767a95c21SRishi Gupta 				break;
79867a95c21SRishi Gupta 			}
79967a95c21SRishi Gupta 			mcp->status = -EIO;
80067a95c21SRishi Gupta 			break;
80167a95c21SRishi Gupta 		default:
80267a95c21SRishi Gupta 			mcp->status = -EIO;
80367a95c21SRishi Gupta 		}
80467a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
80567a95c21SRishi Gupta 		break;
80667a95c21SRishi Gupta 
807328de1c5SRishi Gupta 	case MCP2221_GPIO_GET:
808328de1c5SRishi Gupta 		switch (data[1]) {
809328de1c5SRishi Gupta 		case MCP2221_SUCCESS:
810328de1c5SRishi Gupta 			if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
811328de1c5SRishi Gupta 				(data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
812328de1c5SRishi Gupta 				mcp->status = -ENOENT;
813328de1c5SRishi Gupta 			} else {
814328de1c5SRishi Gupta 				mcp->status = !!data[mcp->gp_idx];
815567b8e9fSLars Povlsen 				mcp->gpio_dir = data[mcp->gp_idx + 1];
816328de1c5SRishi Gupta 			}
817328de1c5SRishi Gupta 			break;
818328de1c5SRishi Gupta 		default:
819328de1c5SRishi Gupta 			mcp->status = -EAGAIN;
820328de1c5SRishi Gupta 		}
821328de1c5SRishi Gupta 		complete(&mcp->wait_in_report);
822328de1c5SRishi Gupta 		break;
823328de1c5SRishi Gupta 
824328de1c5SRishi Gupta 	case MCP2221_GPIO_SET:
825328de1c5SRishi Gupta 		switch (data[1]) {
826328de1c5SRishi Gupta 		case MCP2221_SUCCESS:
827328de1c5SRishi Gupta 			if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
828328de1c5SRishi Gupta 				(data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
829328de1c5SRishi Gupta 				mcp->status = -ENOENT;
830328de1c5SRishi Gupta 			} else {
831328de1c5SRishi Gupta 				mcp->status = 0;
832328de1c5SRishi Gupta 			}
833328de1c5SRishi Gupta 			break;
834328de1c5SRishi Gupta 		default:
835328de1c5SRishi Gupta 			mcp->status = -EAGAIN;
836328de1c5SRishi Gupta 		}
837328de1c5SRishi Gupta 		complete(&mcp->wait_in_report);
838328de1c5SRishi Gupta 		break;
839328de1c5SRishi Gupta 
840*960f9df7SMatt Ranostay 	case MCP2221_SET_SRAM_SETTINGS:
841*960f9df7SMatt Ranostay 		switch (data[1]) {
842*960f9df7SMatt Ranostay 		case MCP2221_SUCCESS:
843*960f9df7SMatt Ranostay 			mcp->status = 0;
844*960f9df7SMatt Ranostay 			break;
845*960f9df7SMatt Ranostay 		default:
846*960f9df7SMatt Ranostay 			mcp->status = -EAGAIN;
847*960f9df7SMatt Ranostay 		}
848*960f9df7SMatt Ranostay 		complete(&mcp->wait_in_report);
849*960f9df7SMatt Ranostay 		break;
850*960f9df7SMatt Ranostay 
851*960f9df7SMatt Ranostay 	case MCP2221_GET_SRAM_SETTINGS:
852*960f9df7SMatt Ranostay 		switch (data[1]) {
853*960f9df7SMatt Ranostay 		case MCP2221_SUCCESS:
854*960f9df7SMatt Ranostay 			memcpy(&mcp->mode, &data[22], 4);
855*960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
856*960f9df7SMatt Ranostay 			mcp->dac_value = data[6] & GENMASK(4, 0);
857*960f9df7SMatt Ranostay #endif
858*960f9df7SMatt Ranostay 			mcp->status = 0;
859*960f9df7SMatt Ranostay 			break;
860*960f9df7SMatt Ranostay 		default:
861*960f9df7SMatt Ranostay 			mcp->status = -EAGAIN;
862*960f9df7SMatt Ranostay 		}
863*960f9df7SMatt Ranostay 		complete(&mcp->wait_in_report);
864*960f9df7SMatt Ranostay 		break;
865*960f9df7SMatt Ranostay 
866*960f9df7SMatt Ranostay 	case MCP2221_READ_FLASH_DATA:
867*960f9df7SMatt Ranostay 		switch (data[1]) {
868*960f9df7SMatt Ranostay 		case MCP2221_SUCCESS:
869*960f9df7SMatt Ranostay 			mcp->status = 0;
870*960f9df7SMatt Ranostay 
871*960f9df7SMatt Ranostay 			/* Only handles CHIP SETTINGS subpage currently */
872*960f9df7SMatt Ranostay 			if (mcp->txbuf[1] != 0) {
873*960f9df7SMatt Ranostay 				mcp->status = -EIO;
874*960f9df7SMatt Ranostay 				break;
875*960f9df7SMatt Ranostay 			}
876*960f9df7SMatt Ranostay 
877*960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
878*960f9df7SMatt Ranostay 			/* DAC scale value */
879*960f9df7SMatt Ranostay 			tmp = FIELD_GET(GENMASK(7, 6), data[6]);
880*960f9df7SMatt Ranostay 			if ((data[6] & BIT(5)) && tmp)
881*960f9df7SMatt Ranostay 				mcp->dac_scale = tmp + 4;
882*960f9df7SMatt Ranostay 			else
883*960f9df7SMatt Ranostay 				mcp->dac_scale = 5;
884*960f9df7SMatt Ranostay 
885*960f9df7SMatt Ranostay 			/* ADC scale value */
886*960f9df7SMatt Ranostay 			tmp = FIELD_GET(GENMASK(4, 3), data[7]);
887*960f9df7SMatt Ranostay 			if ((data[7] & BIT(2)) && tmp)
888*960f9df7SMatt Ranostay 				mcp->adc_scale = tmp - 1;
889*960f9df7SMatt Ranostay 			else
890*960f9df7SMatt Ranostay 				mcp->adc_scale = 0;
891*960f9df7SMatt Ranostay #endif
892*960f9df7SMatt Ranostay 
893*960f9df7SMatt Ranostay 			break;
894*960f9df7SMatt Ranostay 		default:
895*960f9df7SMatt Ranostay 			mcp->status = -EAGAIN;
896*960f9df7SMatt Ranostay 		}
897*960f9df7SMatt Ranostay 		complete(&mcp->wait_in_report);
898*960f9df7SMatt Ranostay 		break;
899*960f9df7SMatt Ranostay 
90067a95c21SRishi Gupta 	default:
90167a95c21SRishi Gupta 		mcp->status = -EIO;
90267a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
90367a95c21SRishi Gupta 	}
90467a95c21SRishi Gupta 
90567a95c21SRishi Gupta 	return 1;
90667a95c21SRishi Gupta }
90767a95c21SRishi Gupta 
908deb3b88bSMatt Ranostay /* Device resource managed function for HID unregistration */
909deb3b88bSMatt Ranostay static void mcp2221_hid_unregister(void *ptr)
910deb3b88bSMatt Ranostay {
911deb3b88bSMatt Ranostay 	struct hid_device *hdev = ptr;
912deb3b88bSMatt Ranostay 
913deb3b88bSMatt Ranostay 	hid_hw_close(hdev);
914deb3b88bSMatt Ranostay 	hid_hw_stop(hdev);
915deb3b88bSMatt Ranostay }
916deb3b88bSMatt Ranostay 
917deb3b88bSMatt Ranostay /* This is needed to be sure hid_hw_stop() isn't called twice by the subsystem */
918deb3b88bSMatt Ranostay static void mcp2221_remove(struct hid_device *hdev)
919deb3b88bSMatt Ranostay {
920deb3b88bSMatt Ranostay }
921deb3b88bSMatt Ranostay 
922*960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
923*960f9df7SMatt Ranostay static int mcp2221_read_raw(struct iio_dev *indio_dev,
924*960f9df7SMatt Ranostay 			    struct iio_chan_spec const *channel, int *val,
925*960f9df7SMatt Ranostay 			    int *val2, long mask)
926*960f9df7SMatt Ranostay {
927*960f9df7SMatt Ranostay 	struct mcp2221_iio *priv = iio_priv(indio_dev);
928*960f9df7SMatt Ranostay 	struct mcp2221 *mcp = priv->mcp;
929*960f9df7SMatt Ranostay 	int ret;
930*960f9df7SMatt Ranostay 
931*960f9df7SMatt Ranostay 	if (mask == IIO_CHAN_INFO_SCALE) {
932*960f9df7SMatt Ranostay 		if (channel->output)
933*960f9df7SMatt Ranostay 			*val = 1 << mcp->dac_scale;
934*960f9df7SMatt Ranostay 		else
935*960f9df7SMatt Ranostay 			*val = 1 << mcp->adc_scale;
936*960f9df7SMatt Ranostay 
937*960f9df7SMatt Ranostay 		return IIO_VAL_INT;
938*960f9df7SMatt Ranostay 	}
939*960f9df7SMatt Ranostay 
940*960f9df7SMatt Ranostay 	mutex_lock(&mcp->lock);
941*960f9df7SMatt Ranostay 
942*960f9df7SMatt Ranostay 	if (channel->output) {
943*960f9df7SMatt Ranostay 		*val = mcp->dac_value;
944*960f9df7SMatt Ranostay 		ret = IIO_VAL_INT;
945*960f9df7SMatt Ranostay 	} else {
946*960f9df7SMatt Ranostay 		/* Read ADC values */
947*960f9df7SMatt Ranostay 		ret = mcp_chk_last_cmd_status(mcp);
948*960f9df7SMatt Ranostay 
949*960f9df7SMatt Ranostay 		if (!ret) {
950*960f9df7SMatt Ranostay 			*val = le16_to_cpu(mcp->adc_values[channel->address]);
951*960f9df7SMatt Ranostay 			if (*val >= BIT(10))
952*960f9df7SMatt Ranostay 				ret =  -EINVAL;
953*960f9df7SMatt Ranostay 			else
954*960f9df7SMatt Ranostay 				ret = IIO_VAL_INT;
955*960f9df7SMatt Ranostay 		}
956*960f9df7SMatt Ranostay 	}
957*960f9df7SMatt Ranostay 
958*960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
959*960f9df7SMatt Ranostay 
960*960f9df7SMatt Ranostay 	return ret;
961*960f9df7SMatt Ranostay }
962*960f9df7SMatt Ranostay 
963*960f9df7SMatt Ranostay static int mcp2221_write_raw(struct iio_dev *indio_dev,
964*960f9df7SMatt Ranostay 			     struct iio_chan_spec const *chan,
965*960f9df7SMatt Ranostay 			     int val, int val2, long mask)
966*960f9df7SMatt Ranostay {
967*960f9df7SMatt Ranostay 	struct mcp2221_iio *priv = iio_priv(indio_dev);
968*960f9df7SMatt Ranostay 	struct mcp2221 *mcp = priv->mcp;
969*960f9df7SMatt Ranostay 	int ret;
970*960f9df7SMatt Ranostay 
971*960f9df7SMatt Ranostay 	if (val < 0 || val >= BIT(5))
972*960f9df7SMatt Ranostay 		return -EINVAL;
973*960f9df7SMatt Ranostay 
974*960f9df7SMatt Ranostay 	mutex_lock(&mcp->lock);
975*960f9df7SMatt Ranostay 
976*960f9df7SMatt Ranostay 	memset(mcp->txbuf, 0, 12);
977*960f9df7SMatt Ranostay 	mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS;
978*960f9df7SMatt Ranostay 	mcp->txbuf[4] = BIT(7) | val;
979*960f9df7SMatt Ranostay 
980*960f9df7SMatt Ranostay 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12);
981*960f9df7SMatt Ranostay 	if (!ret)
982*960f9df7SMatt Ranostay 		mcp->dac_value = val;
983*960f9df7SMatt Ranostay 
984*960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
985*960f9df7SMatt Ranostay 
986*960f9df7SMatt Ranostay 	return ret;
987*960f9df7SMatt Ranostay }
988*960f9df7SMatt Ranostay 
989*960f9df7SMatt Ranostay static const struct iio_info mcp2221_info = {
990*960f9df7SMatt Ranostay 	.read_raw = &mcp2221_read_raw,
991*960f9df7SMatt Ranostay 	.write_raw = &mcp2221_write_raw,
992*960f9df7SMatt Ranostay };
993*960f9df7SMatt Ranostay 
994*960f9df7SMatt Ranostay static int mcp_iio_channels(struct mcp2221 *mcp)
995*960f9df7SMatt Ranostay {
996*960f9df7SMatt Ranostay 	int idx, cnt = 0;
997*960f9df7SMatt Ranostay 	bool dac_created = false;
998*960f9df7SMatt Ranostay 
999*960f9df7SMatt Ranostay 	/* GP0 doesn't have ADC/DAC alternative function */
1000*960f9df7SMatt Ranostay 	for (idx = 1; idx < MCP_NGPIO; idx++) {
1001*960f9df7SMatt Ranostay 		struct iio_chan_spec *chan = &mcp->iio_channels[cnt];
1002*960f9df7SMatt Ranostay 
1003*960f9df7SMatt Ranostay 		switch (mcp->mode[idx]) {
1004*960f9df7SMatt Ranostay 		case 2:
1005*960f9df7SMatt Ranostay 			chan->address = idx - 1;
1006*960f9df7SMatt Ranostay 			chan->channel = cnt++;
1007*960f9df7SMatt Ranostay 			break;
1008*960f9df7SMatt Ranostay 		case 3:
1009*960f9df7SMatt Ranostay 			/* GP1 doesn't have DAC alternative function */
1010*960f9df7SMatt Ranostay 			if (idx == 1 || dac_created)
1011*960f9df7SMatt Ranostay 				continue;
1012*960f9df7SMatt Ranostay 			/* DAC1 and DAC2 outputs are connected to the same DAC */
1013*960f9df7SMatt Ranostay 			dac_created = true;
1014*960f9df7SMatt Ranostay 			chan->output = 1;
1015*960f9df7SMatt Ranostay 			cnt++;
1016*960f9df7SMatt Ranostay 			break;
1017*960f9df7SMatt Ranostay 		default:
1018*960f9df7SMatt Ranostay 			continue;
1019*960f9df7SMatt Ranostay 		};
1020*960f9df7SMatt Ranostay 
1021*960f9df7SMatt Ranostay 		chan->type = IIO_VOLTAGE;
1022*960f9df7SMatt Ranostay 		chan->indexed = 1;
1023*960f9df7SMatt Ranostay 		chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
1024*960f9df7SMatt Ranostay 		chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
1025*960f9df7SMatt Ranostay 		chan->scan_index = -1;
1026*960f9df7SMatt Ranostay 	}
1027*960f9df7SMatt Ranostay 
1028*960f9df7SMatt Ranostay 	return cnt;
1029*960f9df7SMatt Ranostay }
1030*960f9df7SMatt Ranostay 
1031*960f9df7SMatt Ranostay static void mcp_init_work(struct work_struct *work)
1032*960f9df7SMatt Ranostay {
1033*960f9df7SMatt Ranostay 	struct iio_dev *indio_dev;
1034*960f9df7SMatt Ranostay 	struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work);
1035*960f9df7SMatt Ranostay 	struct mcp2221_iio *data;
1036*960f9df7SMatt Ranostay 	static int retries = 5;
1037*960f9df7SMatt Ranostay 	int ret, num_channels;
1038*960f9df7SMatt Ranostay 
1039*960f9df7SMatt Ranostay 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
1040*960f9df7SMatt Ranostay 	mutex_lock(&mcp->lock);
1041*960f9df7SMatt Ranostay 
1042*960f9df7SMatt Ranostay 	mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS;
1043*960f9df7SMatt Ranostay 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
1044*960f9df7SMatt Ranostay 
1045*960f9df7SMatt Ranostay 	if (ret == -EAGAIN)
1046*960f9df7SMatt Ranostay 		goto reschedule_task;
1047*960f9df7SMatt Ranostay 
1048*960f9df7SMatt Ranostay 	num_channels = mcp_iio_channels(mcp);
1049*960f9df7SMatt Ranostay 	if (!num_channels)
1050*960f9df7SMatt Ranostay 		goto unlock;
1051*960f9df7SMatt Ranostay 
1052*960f9df7SMatt Ranostay 	mcp->txbuf[0] = MCP2221_READ_FLASH_DATA;
1053*960f9df7SMatt Ranostay 	mcp->txbuf[1] = 0;
1054*960f9df7SMatt Ranostay 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2);
1055*960f9df7SMatt Ranostay 
1056*960f9df7SMatt Ranostay 	if (ret == -EAGAIN)
1057*960f9df7SMatt Ranostay 		goto reschedule_task;
1058*960f9df7SMatt Ranostay 
1059*960f9df7SMatt Ranostay 	indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data));
1060*960f9df7SMatt Ranostay 	if (!indio_dev)
1061*960f9df7SMatt Ranostay 		goto unlock;
1062*960f9df7SMatt Ranostay 
1063*960f9df7SMatt Ranostay 	data = iio_priv(indio_dev);
1064*960f9df7SMatt Ranostay 	data->mcp = mcp;
1065*960f9df7SMatt Ranostay 
1066*960f9df7SMatt Ranostay 	indio_dev->name = "mcp2221";
1067*960f9df7SMatt Ranostay 	indio_dev->modes = INDIO_DIRECT_MODE;
1068*960f9df7SMatt Ranostay 	indio_dev->info = &mcp2221_info;
1069*960f9df7SMatt Ranostay 	indio_dev->channels = mcp->iio_channels;
1070*960f9df7SMatt Ranostay 	indio_dev->num_channels = num_channels;
1071*960f9df7SMatt Ranostay 
1072*960f9df7SMatt Ranostay 	devm_iio_device_register(&mcp->hdev->dev, indio_dev);
1073*960f9df7SMatt Ranostay 
1074*960f9df7SMatt Ranostay unlock:
1075*960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
1076*960f9df7SMatt Ranostay 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1077*960f9df7SMatt Ranostay 
1078*960f9df7SMatt Ranostay 	return;
1079*960f9df7SMatt Ranostay 
1080*960f9df7SMatt Ranostay reschedule_task:
1081*960f9df7SMatt Ranostay 	mutex_unlock(&mcp->lock);
1082*960f9df7SMatt Ranostay 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
1083*960f9df7SMatt Ranostay 
1084*960f9df7SMatt Ranostay 	if (!retries--)
1085*960f9df7SMatt Ranostay 		return;
1086*960f9df7SMatt Ranostay 
1087*960f9df7SMatt Ranostay 	/* Device is not ready to read SRAM or FLASH data, try again */
1088*960f9df7SMatt Ranostay 	schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1089*960f9df7SMatt Ranostay }
1090*960f9df7SMatt Ranostay #endif
1091*960f9df7SMatt Ranostay 
109267a95c21SRishi Gupta static int mcp2221_probe(struct hid_device *hdev,
109367a95c21SRishi Gupta 					const struct hid_device_id *id)
109467a95c21SRishi Gupta {
109567a95c21SRishi Gupta 	int ret;
109667a95c21SRishi Gupta 	struct mcp2221 *mcp;
109767a95c21SRishi Gupta 
109867a95c21SRishi Gupta 	mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
109967a95c21SRishi Gupta 	if (!mcp)
110067a95c21SRishi Gupta 		return -ENOMEM;
110167a95c21SRishi Gupta 
110267a95c21SRishi Gupta 	ret = hid_parse(hdev);
110367a95c21SRishi Gupta 	if (ret) {
110467a95c21SRishi Gupta 		hid_err(hdev, "can't parse reports\n");
110567a95c21SRishi Gupta 		return ret;
110667a95c21SRishi Gupta 	}
110767a95c21SRishi Gupta 
110867a95c21SRishi Gupta 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
110967a95c21SRishi Gupta 	if (ret) {
111067a95c21SRishi Gupta 		hid_err(hdev, "can't start hardware\n");
111167a95c21SRishi Gupta 		return ret;
111267a95c21SRishi Gupta 	}
111367a95c21SRishi Gupta 
111467a95c21SRishi Gupta 	ret = hid_hw_open(hdev);
111567a95c21SRishi Gupta 	if (ret) {
111667a95c21SRishi Gupta 		hid_err(hdev, "can't open device\n");
1117deb3b88bSMatt Ranostay 		hid_hw_stop(hdev);
1118deb3b88bSMatt Ranostay 		return ret;
111967a95c21SRishi Gupta 	}
112067a95c21SRishi Gupta 
112167a95c21SRishi Gupta 	mutex_init(&mcp->lock);
112267a95c21SRishi Gupta 	init_completion(&mcp->wait_in_report);
112367a95c21SRishi Gupta 	hid_set_drvdata(hdev, mcp);
112467a95c21SRishi Gupta 	mcp->hdev = hdev;
112567a95c21SRishi Gupta 
1126deb3b88bSMatt Ranostay 	ret = devm_add_action_or_reset(&hdev->dev, mcp2221_hid_unregister, hdev);
1127deb3b88bSMatt Ranostay 	if (ret)
1128deb3b88bSMatt Ranostay 		return ret;
1129deb3b88bSMatt Ranostay 
113067a95c21SRishi Gupta 	/* Set I2C bus clock diviser */
113167a95c21SRishi Gupta 	if (i2c_clk_freq > 400)
113267a95c21SRishi Gupta 		i2c_clk_freq = 400;
113367a95c21SRishi Gupta 	if (i2c_clk_freq < 50)
113467a95c21SRishi Gupta 		i2c_clk_freq = 50;
113567a95c21SRishi Gupta 	mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
113667a95c21SRishi Gupta 
113767a95c21SRishi Gupta 	mcp->adapter.owner = THIS_MODULE;
113867a95c21SRishi Gupta 	mcp->adapter.class = I2C_CLASS_HWMON;
113967a95c21SRishi Gupta 	mcp->adapter.algo = &mcp_i2c_algo;
114067a95c21SRishi Gupta 	mcp->adapter.retries = 1;
114167a95c21SRishi Gupta 	mcp->adapter.dev.parent = &hdev->dev;
114267a95c21SRishi Gupta 	snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
114367a95c21SRishi Gupta 			"MCP2221 usb-i2c bridge on hidraw%d",
114467a95c21SRishi Gupta 			((struct hidraw *)hdev->hidraw)->minor);
114567a95c21SRishi Gupta 
1146deb3b88bSMatt Ranostay 	ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter);
114767a95c21SRishi Gupta 	if (ret) {
114867a95c21SRishi Gupta 		hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
1149deb3b88bSMatt Ranostay 		return ret;
115067a95c21SRishi Gupta 	}
115167a95c21SRishi Gupta 	i2c_set_adapdata(&mcp->adapter, mcp);
115267a95c21SRishi Gupta 
1153328de1c5SRishi Gupta 	/* Setup GPIO chip */
1154328de1c5SRishi Gupta 	mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
1155deb3b88bSMatt Ranostay 	if (!mcp->gc)
1156deb3b88bSMatt Ranostay 		return -ENOMEM;
1157328de1c5SRishi Gupta 
1158328de1c5SRishi Gupta 	mcp->gc->label = "mcp2221_gpio";
1159328de1c5SRishi Gupta 	mcp->gc->direction_input = mcp_gpio_direction_input;
1160328de1c5SRishi Gupta 	mcp->gc->direction_output = mcp_gpio_direction_output;
1161328de1c5SRishi Gupta 	mcp->gc->get_direction = mcp_gpio_get_direction;
1162328de1c5SRishi Gupta 	mcp->gc->set = mcp_gpio_set;
1163328de1c5SRishi Gupta 	mcp->gc->get = mcp_gpio_get;
1164567b8e9fSLars Povlsen 	mcp->gc->ngpio = MCP_NGPIO;
1165328de1c5SRishi Gupta 	mcp->gc->base = -1;
1166328de1c5SRishi Gupta 	mcp->gc->can_sleep = 1;
1167328de1c5SRishi Gupta 	mcp->gc->parent = &hdev->dev;
1168328de1c5SRishi Gupta 
1169ea418b35SMatt Ranostay #if IS_REACHABLE(CONFIG_GPIOLIB)
1170328de1c5SRishi Gupta 	ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
1171328de1c5SRishi Gupta 	if (ret)
1172deb3b88bSMatt Ranostay 		return ret;
1173ea418b35SMatt Ranostay #endif
1174328de1c5SRishi Gupta 
1175*960f9df7SMatt Ranostay #if IS_REACHABLE(CONFIG_IIO)
1176*960f9df7SMatt Ranostay 	INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work);
1177*960f9df7SMatt Ranostay 	schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100));
1178*960f9df7SMatt Ranostay #endif
1179*960f9df7SMatt Ranostay 
118067a95c21SRishi Gupta 	return 0;
118167a95c21SRishi Gupta }
118267a95c21SRishi Gupta 
118367a95c21SRishi Gupta static const struct hid_device_id mcp2221_devices[] = {
118467a95c21SRishi Gupta 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
118567a95c21SRishi Gupta 	{ }
118667a95c21SRishi Gupta };
118767a95c21SRishi Gupta MODULE_DEVICE_TABLE(hid, mcp2221_devices);
118867a95c21SRishi Gupta 
118967a95c21SRishi Gupta static struct hid_driver mcp2221_driver = {
119067a95c21SRishi Gupta 	.name		= "mcp2221",
119167a95c21SRishi Gupta 	.id_table	= mcp2221_devices,
119267a95c21SRishi Gupta 	.probe		= mcp2221_probe,
119367a95c21SRishi Gupta 	.remove		= mcp2221_remove,
119467a95c21SRishi Gupta 	.raw_event	= mcp2221_raw_event,
119567a95c21SRishi Gupta };
119667a95c21SRishi Gupta 
119767a95c21SRishi Gupta /* Register with HID core */
119867a95c21SRishi Gupta module_hid_driver(mcp2221_driver);
119967a95c21SRishi Gupta 
120067a95c21SRishi Gupta MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
120167a95c21SRishi Gupta MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
120267a95c21SRishi Gupta MODULE_LICENSE("GPL v2");
1203