xref: /linux/drivers/hid/hid-mcp2221.c (revision 67a95c21463d066060b0f66d65a75d45bb386ffb)
1*67a95c21SRishi Gupta // SPDX-License-Identifier: GPL-2.0-only
2*67a95c21SRishi Gupta /*
3*67a95c21SRishi Gupta  * MCP2221A - Microchip USB to I2C Host Protocol Bridge
4*67a95c21SRishi Gupta  *
5*67a95c21SRishi Gupta  * Copyright (c) 2020, Rishi Gupta <gupt21@gmail.com>
6*67a95c21SRishi Gupta  *
7*67a95c21SRishi Gupta  * Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/20005565B.pdf
8*67a95c21SRishi Gupta  */
9*67a95c21SRishi Gupta 
10*67a95c21SRishi Gupta #include <linux/module.h>
11*67a95c21SRishi Gupta #include <linux/err.h>
12*67a95c21SRishi Gupta #include <linux/mutex.h>
13*67a95c21SRishi Gupta #include <linux/completion.h>
14*67a95c21SRishi Gupta #include <linux/delay.h>
15*67a95c21SRishi Gupta #include <linux/hid.h>
16*67a95c21SRishi Gupta #include <linux/hidraw.h>
17*67a95c21SRishi Gupta #include <linux/i2c.h>
18*67a95c21SRishi Gupta #include "hid-ids.h"
19*67a95c21SRishi Gupta 
20*67a95c21SRishi Gupta /* Commands codes in a raw output report */
21*67a95c21SRishi Gupta enum {
22*67a95c21SRishi Gupta 	MCP2221_I2C_WR_DATA = 0x90,
23*67a95c21SRishi Gupta 	MCP2221_I2C_WR_NO_STOP = 0x94,
24*67a95c21SRishi Gupta 	MCP2221_I2C_RD_DATA = 0x91,
25*67a95c21SRishi Gupta 	MCP2221_I2C_RD_RPT_START = 0x93,
26*67a95c21SRishi Gupta 	MCP2221_I2C_GET_DATA = 0x40,
27*67a95c21SRishi Gupta 	MCP2221_I2C_PARAM_OR_STATUS	= 0x10,
28*67a95c21SRishi Gupta 	MCP2221_I2C_SET_SPEED = 0x20,
29*67a95c21SRishi Gupta 	MCP2221_I2C_CANCEL = 0x10,
30*67a95c21SRishi Gupta };
31*67a95c21SRishi Gupta 
32*67a95c21SRishi Gupta /* Response codes in a raw input report */
33*67a95c21SRishi Gupta enum {
34*67a95c21SRishi Gupta 	MCP2221_SUCCESS = 0x00,
35*67a95c21SRishi Gupta 	MCP2221_I2C_ENG_BUSY = 0x01,
36*67a95c21SRishi Gupta 	MCP2221_I2C_START_TOUT = 0x12,
37*67a95c21SRishi Gupta 	MCP2221_I2C_STOP_TOUT = 0x62,
38*67a95c21SRishi Gupta 	MCP2221_I2C_WRADDRL_TOUT = 0x23,
39*67a95c21SRishi Gupta 	MCP2221_I2C_WRDATA_TOUT = 0x44,
40*67a95c21SRishi Gupta 	MCP2221_I2C_WRADDRL_NACK = 0x25,
41*67a95c21SRishi Gupta 	MCP2221_I2C_MASK_ADDR_NACK = 0x40,
42*67a95c21SRishi Gupta 	MCP2221_I2C_WRADDRL_SEND = 0x21,
43*67a95c21SRishi Gupta 	MCP2221_I2C_ADDR_NACK = 0x25,
44*67a95c21SRishi Gupta 	MCP2221_I2C_READ_COMPL = 0x55,
45*67a95c21SRishi Gupta };
46*67a95c21SRishi Gupta 
47*67a95c21SRishi Gupta /*
48*67a95c21SRishi Gupta  * There is no way to distinguish responses. Therefore next command
49*67a95c21SRishi Gupta  * is sent only after response to previous has been received. Mutex
50*67a95c21SRishi Gupta  * lock is used for this purpose mainly.
51*67a95c21SRishi Gupta  */
52*67a95c21SRishi Gupta struct mcp2221 {
53*67a95c21SRishi Gupta 	struct hid_device *hdev;
54*67a95c21SRishi Gupta 	struct i2c_adapter adapter;
55*67a95c21SRishi Gupta 	struct mutex lock;
56*67a95c21SRishi Gupta 	struct completion wait_in_report;
57*67a95c21SRishi Gupta 	u8 *rxbuf;
58*67a95c21SRishi Gupta 	u8 txbuf[64];
59*67a95c21SRishi Gupta 	int rxbuf_idx;
60*67a95c21SRishi Gupta 	int status;
61*67a95c21SRishi Gupta 	u8 cur_i2c_clk_div;
62*67a95c21SRishi Gupta };
63*67a95c21SRishi Gupta 
64*67a95c21SRishi Gupta /*
65*67a95c21SRishi Gupta  * Default i2c bus clock frequency 400 kHz. Modify this if you
66*67a95c21SRishi Gupta  * want to set some other frequency (min 50 kHz - max 400 kHz).
67*67a95c21SRishi Gupta  */
68*67a95c21SRishi Gupta static uint i2c_clk_freq = 400;
69*67a95c21SRishi Gupta 
70*67a95c21SRishi Gupta /* Synchronously send output report to the device */
71*67a95c21SRishi Gupta static int mcp_send_report(struct mcp2221 *mcp,
72*67a95c21SRishi Gupta 					u8 *out_report, size_t len)
73*67a95c21SRishi Gupta {
74*67a95c21SRishi Gupta 	u8 *buf;
75*67a95c21SRishi Gupta 	int ret;
76*67a95c21SRishi Gupta 
77*67a95c21SRishi Gupta 	buf = kmemdup(out_report, len, GFP_KERNEL);
78*67a95c21SRishi Gupta 	if (!buf)
79*67a95c21SRishi Gupta 		return -ENOMEM;
80*67a95c21SRishi Gupta 
81*67a95c21SRishi Gupta 	/* mcp2221 uses interrupt endpoint for out reports */
82*67a95c21SRishi Gupta 	ret = hid_hw_output_report(mcp->hdev, buf, len);
83*67a95c21SRishi Gupta 	kfree(buf);
84*67a95c21SRishi Gupta 
85*67a95c21SRishi Gupta 	if (ret < 0)
86*67a95c21SRishi Gupta 		return ret;
87*67a95c21SRishi Gupta 	return 0;
88*67a95c21SRishi Gupta }
89*67a95c21SRishi Gupta 
90*67a95c21SRishi Gupta /*
91*67a95c21SRishi Gupta  * Send o/p report to the device and wait for i/p report to be
92*67a95c21SRishi Gupta  * received from the device. If the device does not respond,
93*67a95c21SRishi Gupta  * we timeout.
94*67a95c21SRishi Gupta  */
95*67a95c21SRishi Gupta static int mcp_send_data_req_status(struct mcp2221 *mcp,
96*67a95c21SRishi Gupta 			u8 *out_report, int len)
97*67a95c21SRishi Gupta {
98*67a95c21SRishi Gupta 	int ret;
99*67a95c21SRishi Gupta 	unsigned long t;
100*67a95c21SRishi Gupta 
101*67a95c21SRishi Gupta 	reinit_completion(&mcp->wait_in_report);
102*67a95c21SRishi Gupta 
103*67a95c21SRishi Gupta 	ret = mcp_send_report(mcp, out_report, len);
104*67a95c21SRishi Gupta 	if (ret)
105*67a95c21SRishi Gupta 		return ret;
106*67a95c21SRishi Gupta 
107*67a95c21SRishi Gupta 	t = wait_for_completion_timeout(&mcp->wait_in_report,
108*67a95c21SRishi Gupta 							msecs_to_jiffies(4000));
109*67a95c21SRishi Gupta 	if (!t)
110*67a95c21SRishi Gupta 		return -ETIMEDOUT;
111*67a95c21SRishi Gupta 
112*67a95c21SRishi Gupta 	return mcp->status;
113*67a95c21SRishi Gupta }
114*67a95c21SRishi Gupta 
115*67a95c21SRishi Gupta /* Check pass/fail for actual communication with i2c slave */
116*67a95c21SRishi Gupta static int mcp_chk_last_cmd_status(struct mcp2221 *mcp)
117*67a95c21SRishi Gupta {
118*67a95c21SRishi Gupta 	memset(mcp->txbuf, 0, 8);
119*67a95c21SRishi Gupta 	mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
120*67a95c21SRishi Gupta 
121*67a95c21SRishi Gupta 	return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
122*67a95c21SRishi Gupta }
123*67a95c21SRishi Gupta 
124*67a95c21SRishi Gupta /* Cancels last command releasing i2c bus just in case occupied */
125*67a95c21SRishi Gupta static int mcp_cancel_last_cmd(struct mcp2221 *mcp)
126*67a95c21SRishi Gupta {
127*67a95c21SRishi Gupta 	memset(mcp->txbuf, 0, 8);
128*67a95c21SRishi Gupta 	mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
129*67a95c21SRishi Gupta 	mcp->txbuf[2] = MCP2221_I2C_CANCEL;
130*67a95c21SRishi Gupta 
131*67a95c21SRishi Gupta 	return mcp_send_data_req_status(mcp, mcp->txbuf, 8);
132*67a95c21SRishi Gupta }
133*67a95c21SRishi Gupta 
134*67a95c21SRishi Gupta static int mcp_set_i2c_speed(struct mcp2221 *mcp)
135*67a95c21SRishi Gupta {
136*67a95c21SRishi Gupta 	int ret;
137*67a95c21SRishi Gupta 
138*67a95c21SRishi Gupta 	memset(mcp->txbuf, 0, 8);
139*67a95c21SRishi Gupta 	mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS;
140*67a95c21SRishi Gupta 	mcp->txbuf[3] = MCP2221_I2C_SET_SPEED;
141*67a95c21SRishi Gupta 	mcp->txbuf[4] = mcp->cur_i2c_clk_div;
142*67a95c21SRishi Gupta 
143*67a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8);
144*67a95c21SRishi Gupta 	if (ret) {
145*67a95c21SRishi Gupta 		/* Small delay is needed here */
146*67a95c21SRishi Gupta 		usleep_range(980, 1000);
147*67a95c21SRishi Gupta 		mcp_cancel_last_cmd(mcp);
148*67a95c21SRishi Gupta 	}
149*67a95c21SRishi Gupta 
150*67a95c21SRishi Gupta 	return 0;
151*67a95c21SRishi Gupta }
152*67a95c21SRishi Gupta 
153*67a95c21SRishi Gupta /*
154*67a95c21SRishi Gupta  * An output report can contain minimum 1 and maximum 60 user data
155*67a95c21SRishi Gupta  * bytes. If the number of data bytes is more then 60, we send it
156*67a95c21SRishi Gupta  * in chunks of 60 bytes. Last chunk may contain exactly 60 or less
157*67a95c21SRishi Gupta  * bytes. Total number of bytes is informed in very first report to
158*67a95c21SRishi Gupta  * mcp2221, from that point onwards it first collect all the data
159*67a95c21SRishi Gupta  * from host and then send to i2c slave device.
160*67a95c21SRishi Gupta  */
161*67a95c21SRishi Gupta static int mcp_i2c_write(struct mcp2221 *mcp,
162*67a95c21SRishi Gupta 				struct i2c_msg *msg, int type, u8 last_status)
163*67a95c21SRishi Gupta {
164*67a95c21SRishi Gupta 	int ret, len, idx, sent;
165*67a95c21SRishi Gupta 
166*67a95c21SRishi Gupta 	idx = 0;
167*67a95c21SRishi Gupta 	sent  = 0;
168*67a95c21SRishi Gupta 	if (msg->len < 60)
169*67a95c21SRishi Gupta 		len = msg->len;
170*67a95c21SRishi Gupta 	else
171*67a95c21SRishi Gupta 		len = 60;
172*67a95c21SRishi Gupta 
173*67a95c21SRishi Gupta 	do {
174*67a95c21SRishi Gupta 		mcp->txbuf[0] = type;
175*67a95c21SRishi Gupta 		mcp->txbuf[1] = msg->len & 0xff;
176*67a95c21SRishi Gupta 		mcp->txbuf[2] = msg->len >> 8;
177*67a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(msg->addr << 1);
178*67a95c21SRishi Gupta 
179*67a95c21SRishi Gupta 		memcpy(&mcp->txbuf[4], &msg->buf[idx], len);
180*67a95c21SRishi Gupta 
181*67a95c21SRishi Gupta 		ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4);
182*67a95c21SRishi Gupta 		if (ret)
183*67a95c21SRishi Gupta 			return ret;
184*67a95c21SRishi Gupta 
185*67a95c21SRishi Gupta 		usleep_range(980, 1000);
186*67a95c21SRishi Gupta 
187*67a95c21SRishi Gupta 		if (last_status) {
188*67a95c21SRishi Gupta 			ret = mcp_chk_last_cmd_status(mcp);
189*67a95c21SRishi Gupta 			if (ret)
190*67a95c21SRishi Gupta 				return ret;
191*67a95c21SRishi Gupta 		}
192*67a95c21SRishi Gupta 
193*67a95c21SRishi Gupta 		sent = sent + len;
194*67a95c21SRishi Gupta 		if (sent >= msg->len)
195*67a95c21SRishi Gupta 			break;
196*67a95c21SRishi Gupta 
197*67a95c21SRishi Gupta 		idx = idx + len;
198*67a95c21SRishi Gupta 		if ((msg->len - sent) < 60)
199*67a95c21SRishi Gupta 			len = msg->len - sent;
200*67a95c21SRishi Gupta 		else
201*67a95c21SRishi Gupta 			len = 60;
202*67a95c21SRishi Gupta 
203*67a95c21SRishi Gupta 		/*
204*67a95c21SRishi Gupta 		 * Testing shows delay is needed between successive writes
205*67a95c21SRishi Gupta 		 * otherwise next write fails on first-try from i2c core.
206*67a95c21SRishi Gupta 		 * This value is obtained through automated stress testing.
207*67a95c21SRishi Gupta 		 */
208*67a95c21SRishi Gupta 		usleep_range(980, 1000);
209*67a95c21SRishi Gupta 	} while (len > 0);
210*67a95c21SRishi Gupta 
211*67a95c21SRishi Gupta 	return ret;
212*67a95c21SRishi Gupta }
213*67a95c21SRishi Gupta 
214*67a95c21SRishi Gupta /*
215*67a95c21SRishi Gupta  * Device reads all data (0 - 65535 bytes) from i2c slave device and
216*67a95c21SRishi Gupta  * stores it in device itself. This data is read back from device to
217*67a95c21SRishi Gupta  * host in multiples of 60 bytes using input reports.
218*67a95c21SRishi Gupta  */
219*67a95c21SRishi Gupta static int mcp_i2c_smbus_read(struct mcp2221 *mcp,
220*67a95c21SRishi Gupta 				struct i2c_msg *msg, int type, u16 smbus_addr,
221*67a95c21SRishi Gupta 				u8 smbus_len, u8 *smbus_buf)
222*67a95c21SRishi Gupta {
223*67a95c21SRishi Gupta 	int ret;
224*67a95c21SRishi Gupta 	u16 total_len;
225*67a95c21SRishi Gupta 
226*67a95c21SRishi Gupta 	mcp->txbuf[0] = type;
227*67a95c21SRishi Gupta 	if (msg) {
228*67a95c21SRishi Gupta 		mcp->txbuf[1] = msg->len & 0xff;
229*67a95c21SRishi Gupta 		mcp->txbuf[2] = msg->len >> 8;
230*67a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(msg->addr << 1);
231*67a95c21SRishi Gupta 		total_len = msg->len;
232*67a95c21SRishi Gupta 		mcp->rxbuf = msg->buf;
233*67a95c21SRishi Gupta 	} else {
234*67a95c21SRishi Gupta 		mcp->txbuf[1] = smbus_len;
235*67a95c21SRishi Gupta 		mcp->txbuf[2] = 0;
236*67a95c21SRishi Gupta 		mcp->txbuf[3] = (u8)(smbus_addr << 1);
237*67a95c21SRishi Gupta 		total_len = smbus_len;
238*67a95c21SRishi Gupta 		mcp->rxbuf = smbus_buf;
239*67a95c21SRishi Gupta 	}
240*67a95c21SRishi Gupta 
241*67a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4);
242*67a95c21SRishi Gupta 	if (ret)
243*67a95c21SRishi Gupta 		return ret;
244*67a95c21SRishi Gupta 
245*67a95c21SRishi Gupta 	mcp->rxbuf_idx = 0;
246*67a95c21SRishi Gupta 
247*67a95c21SRishi Gupta 	do {
248*67a95c21SRishi Gupta 		memset(mcp->txbuf, 0, 4);
249*67a95c21SRishi Gupta 		mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
250*67a95c21SRishi Gupta 
251*67a95c21SRishi Gupta 		ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
252*67a95c21SRishi Gupta 		if (ret)
253*67a95c21SRishi Gupta 			return ret;
254*67a95c21SRishi Gupta 
255*67a95c21SRishi Gupta 		ret = mcp_chk_last_cmd_status(mcp);
256*67a95c21SRishi Gupta 		if (ret)
257*67a95c21SRishi Gupta 			return ret;
258*67a95c21SRishi Gupta 
259*67a95c21SRishi Gupta 		usleep_range(980, 1000);
260*67a95c21SRishi Gupta 	} while (mcp->rxbuf_idx < total_len);
261*67a95c21SRishi Gupta 
262*67a95c21SRishi Gupta 	return ret;
263*67a95c21SRishi Gupta }
264*67a95c21SRishi Gupta 
265*67a95c21SRishi Gupta static int mcp_i2c_xfer(struct i2c_adapter *adapter,
266*67a95c21SRishi Gupta 				struct i2c_msg msgs[], int num)
267*67a95c21SRishi Gupta {
268*67a95c21SRishi Gupta 	int ret;
269*67a95c21SRishi Gupta 	struct mcp2221 *mcp = i2c_get_adapdata(adapter);
270*67a95c21SRishi Gupta 
271*67a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
272*67a95c21SRishi Gupta 
273*67a95c21SRishi Gupta 	mutex_lock(&mcp->lock);
274*67a95c21SRishi Gupta 
275*67a95c21SRishi Gupta 	/* Setting speed before every transaction is required for mcp2221 */
276*67a95c21SRishi Gupta 	ret = mcp_set_i2c_speed(mcp);
277*67a95c21SRishi Gupta 	if (ret)
278*67a95c21SRishi Gupta 		goto exit;
279*67a95c21SRishi Gupta 
280*67a95c21SRishi Gupta 	if (num == 1) {
281*67a95c21SRishi Gupta 		if (msgs->flags & I2C_M_RD) {
282*67a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA,
283*67a95c21SRishi Gupta 							0, 0, NULL);
284*67a95c21SRishi Gupta 		} else {
285*67a95c21SRishi Gupta 			ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1);
286*67a95c21SRishi Gupta 		}
287*67a95c21SRishi Gupta 		if (ret)
288*67a95c21SRishi Gupta 			goto exit;
289*67a95c21SRishi Gupta 		ret = num;
290*67a95c21SRishi Gupta 	} else if (num == 2) {
291*67a95c21SRishi Gupta 		/* Ex transaction; send reg address and read its contents */
292*67a95c21SRishi Gupta 		if (msgs[0].addr == msgs[1].addr &&
293*67a95c21SRishi Gupta 			!(msgs[0].flags & I2C_M_RD) &&
294*67a95c21SRishi Gupta 			 (msgs[1].flags & I2C_M_RD)) {
295*67a95c21SRishi Gupta 
296*67a95c21SRishi Gupta 			ret = mcp_i2c_write(mcp, &msgs[0],
297*67a95c21SRishi Gupta 						MCP2221_I2C_WR_NO_STOP, 0);
298*67a95c21SRishi Gupta 			if (ret)
299*67a95c21SRishi Gupta 				goto exit;
300*67a95c21SRishi Gupta 
301*67a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, &msgs[1],
302*67a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
303*67a95c21SRishi Gupta 						0, 0, NULL);
304*67a95c21SRishi Gupta 			if (ret)
305*67a95c21SRishi Gupta 				goto exit;
306*67a95c21SRishi Gupta 			ret = num;
307*67a95c21SRishi Gupta 		} else {
308*67a95c21SRishi Gupta 			dev_err(&adapter->dev,
309*67a95c21SRishi Gupta 				"unsupported multi-msg i2c transaction\n");
310*67a95c21SRishi Gupta 			ret = -EOPNOTSUPP;
311*67a95c21SRishi Gupta 		}
312*67a95c21SRishi Gupta 	} else {
313*67a95c21SRishi Gupta 		dev_err(&adapter->dev,
314*67a95c21SRishi Gupta 			"unsupported multi-msg i2c transaction\n");
315*67a95c21SRishi Gupta 		ret = -EOPNOTSUPP;
316*67a95c21SRishi Gupta 	}
317*67a95c21SRishi Gupta 
318*67a95c21SRishi Gupta exit:
319*67a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
320*67a95c21SRishi Gupta 	mutex_unlock(&mcp->lock);
321*67a95c21SRishi Gupta 	return ret;
322*67a95c21SRishi Gupta }
323*67a95c21SRishi Gupta 
324*67a95c21SRishi Gupta static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr,
325*67a95c21SRishi Gupta 				u8 command, u8 *buf, u8 len, int type,
326*67a95c21SRishi Gupta 				u8 last_status)
327*67a95c21SRishi Gupta {
328*67a95c21SRishi Gupta 	int data_len, ret;
329*67a95c21SRishi Gupta 
330*67a95c21SRishi Gupta 	mcp->txbuf[0] = type;
331*67a95c21SRishi Gupta 	mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */
332*67a95c21SRishi Gupta 	mcp->txbuf[2] = 0;
333*67a95c21SRishi Gupta 	mcp->txbuf[3] = (u8)(addr << 1);
334*67a95c21SRishi Gupta 	mcp->txbuf[4] = command;
335*67a95c21SRishi Gupta 
336*67a95c21SRishi Gupta 	switch (len) {
337*67a95c21SRishi Gupta 	case 0:
338*67a95c21SRishi Gupta 		data_len = 5;
339*67a95c21SRishi Gupta 		break;
340*67a95c21SRishi Gupta 	case 1:
341*67a95c21SRishi Gupta 		mcp->txbuf[5] = buf[0];
342*67a95c21SRishi Gupta 		data_len = 6;
343*67a95c21SRishi Gupta 		break;
344*67a95c21SRishi Gupta 	case 2:
345*67a95c21SRishi Gupta 		mcp->txbuf[5] = buf[0];
346*67a95c21SRishi Gupta 		mcp->txbuf[6] = buf[1];
347*67a95c21SRishi Gupta 		data_len = 7;
348*67a95c21SRishi Gupta 		break;
349*67a95c21SRishi Gupta 	default:
350*67a95c21SRishi Gupta 		memcpy(&mcp->txbuf[5], buf, len);
351*67a95c21SRishi Gupta 		data_len = len + 5;
352*67a95c21SRishi Gupta 	}
353*67a95c21SRishi Gupta 
354*67a95c21SRishi Gupta 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
355*67a95c21SRishi Gupta 	if (ret)
356*67a95c21SRishi Gupta 		return ret;
357*67a95c21SRishi Gupta 
358*67a95c21SRishi Gupta 	if (last_status) {
359*67a95c21SRishi Gupta 		usleep_range(980, 1000);
360*67a95c21SRishi Gupta 
361*67a95c21SRishi Gupta 		ret = mcp_chk_last_cmd_status(mcp);
362*67a95c21SRishi Gupta 		if (ret)
363*67a95c21SRishi Gupta 			return ret;
364*67a95c21SRishi Gupta 	}
365*67a95c21SRishi Gupta 
366*67a95c21SRishi Gupta 	return ret;
367*67a95c21SRishi Gupta }
368*67a95c21SRishi Gupta 
369*67a95c21SRishi Gupta static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
370*67a95c21SRishi Gupta 				unsigned short flags, char read_write,
371*67a95c21SRishi Gupta 				u8 command, int size,
372*67a95c21SRishi Gupta 				union i2c_smbus_data *data)
373*67a95c21SRishi Gupta {
374*67a95c21SRishi Gupta 	int ret;
375*67a95c21SRishi Gupta 	struct mcp2221 *mcp = i2c_get_adapdata(adapter);
376*67a95c21SRishi Gupta 
377*67a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
378*67a95c21SRishi Gupta 
379*67a95c21SRishi Gupta 	mutex_lock(&mcp->lock);
380*67a95c21SRishi Gupta 
381*67a95c21SRishi Gupta 	ret = mcp_set_i2c_speed(mcp);
382*67a95c21SRishi Gupta 	if (ret)
383*67a95c21SRishi Gupta 		goto exit;
384*67a95c21SRishi Gupta 
385*67a95c21SRishi Gupta 	switch (size) {
386*67a95c21SRishi Gupta 
387*67a95c21SRishi Gupta 	case I2C_SMBUS_QUICK:
388*67a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ)
389*67a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
390*67a95c21SRishi Gupta 						addr, 0, &data->byte);
391*67a95c21SRishi Gupta 		else
392*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
393*67a95c21SRishi Gupta 						0, MCP2221_I2C_WR_DATA, 1);
394*67a95c21SRishi Gupta 		break;
395*67a95c21SRishi Gupta 	case I2C_SMBUS_BYTE:
396*67a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ)
397*67a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
398*67a95c21SRishi Gupta 						addr, 1, &data->byte);
399*67a95c21SRishi Gupta 		else
400*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
401*67a95c21SRishi Gupta 						0, MCP2221_I2C_WR_DATA, 1);
402*67a95c21SRishi Gupta 		break;
403*67a95c21SRishi Gupta 	case I2C_SMBUS_BYTE_DATA:
404*67a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
405*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
406*67a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 0);
407*67a95c21SRishi Gupta 			if (ret)
408*67a95c21SRishi Gupta 				goto exit;
409*67a95c21SRishi Gupta 
410*67a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL,
411*67a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
412*67a95c21SRishi Gupta 						addr, 1, &data->byte);
413*67a95c21SRishi Gupta 		} else {
414*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, &data->byte,
415*67a95c21SRishi Gupta 						1, MCP2221_I2C_WR_DATA, 1);
416*67a95c21SRishi Gupta 		}
417*67a95c21SRishi Gupta 		break;
418*67a95c21SRishi Gupta 	case I2C_SMBUS_WORD_DATA:
419*67a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
420*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
421*67a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 0);
422*67a95c21SRishi Gupta 			if (ret)
423*67a95c21SRishi Gupta 				goto exit;
424*67a95c21SRishi Gupta 
425*67a95c21SRishi Gupta 			ret = mcp_i2c_smbus_read(mcp, NULL,
426*67a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
427*67a95c21SRishi Gupta 						addr, 2, (u8 *)&data->word);
428*67a95c21SRishi Gupta 		} else {
429*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command,
430*67a95c21SRishi Gupta 						(u8 *)&data->word, 2,
431*67a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
432*67a95c21SRishi Gupta 		}
433*67a95c21SRishi Gupta 		break;
434*67a95c21SRishi Gupta 	case I2C_SMBUS_BLOCK_DATA:
435*67a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
436*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
437*67a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 1);
438*67a95c21SRishi Gupta 			if (ret)
439*67a95c21SRishi Gupta 				goto exit;
440*67a95c21SRishi Gupta 
441*67a95c21SRishi Gupta 			mcp->rxbuf_idx = 0;
442*67a95c21SRishi Gupta 			mcp->rxbuf = data->block;
443*67a95c21SRishi Gupta 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
444*67a95c21SRishi Gupta 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
445*67a95c21SRishi Gupta 			if (ret)
446*67a95c21SRishi Gupta 				goto exit;
447*67a95c21SRishi Gupta 		} else {
448*67a95c21SRishi Gupta 			if (!data->block[0]) {
449*67a95c21SRishi Gupta 				ret = -EINVAL;
450*67a95c21SRishi Gupta 				goto exit;
451*67a95c21SRishi Gupta 			}
452*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, data->block,
453*67a95c21SRishi Gupta 						data->block[0] + 1,
454*67a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
455*67a95c21SRishi Gupta 		}
456*67a95c21SRishi Gupta 		break;
457*67a95c21SRishi Gupta 	case I2C_SMBUS_I2C_BLOCK_DATA:
458*67a95c21SRishi Gupta 		if (read_write == I2C_SMBUS_READ) {
459*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command, NULL,
460*67a95c21SRishi Gupta 						0, MCP2221_I2C_WR_NO_STOP, 1);
461*67a95c21SRishi Gupta 			if (ret)
462*67a95c21SRishi Gupta 				goto exit;
463*67a95c21SRishi Gupta 
464*67a95c21SRishi Gupta 			mcp->rxbuf_idx = 0;
465*67a95c21SRishi Gupta 			mcp->rxbuf = data->block;
466*67a95c21SRishi Gupta 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
467*67a95c21SRishi Gupta 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
468*67a95c21SRishi Gupta 			if (ret)
469*67a95c21SRishi Gupta 				goto exit;
470*67a95c21SRishi Gupta 		} else {
471*67a95c21SRishi Gupta 			if (!data->block[0]) {
472*67a95c21SRishi Gupta 				ret = -EINVAL;
473*67a95c21SRishi Gupta 				goto exit;
474*67a95c21SRishi Gupta 			}
475*67a95c21SRishi Gupta 			ret = mcp_smbus_write(mcp, addr, command,
476*67a95c21SRishi Gupta 						&data->block[1], data->block[0],
477*67a95c21SRishi Gupta 						MCP2221_I2C_WR_DATA, 1);
478*67a95c21SRishi Gupta 		}
479*67a95c21SRishi Gupta 		break;
480*67a95c21SRishi Gupta 	case I2C_SMBUS_PROC_CALL:
481*67a95c21SRishi Gupta 		ret = mcp_smbus_write(mcp, addr, command,
482*67a95c21SRishi Gupta 						(u8 *)&data->word,
483*67a95c21SRishi Gupta 						2, MCP2221_I2C_WR_NO_STOP, 0);
484*67a95c21SRishi Gupta 		if (ret)
485*67a95c21SRishi Gupta 			goto exit;
486*67a95c21SRishi Gupta 
487*67a95c21SRishi Gupta 		ret = mcp_i2c_smbus_read(mcp, NULL,
488*67a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
489*67a95c21SRishi Gupta 						addr, 2, (u8 *)&data->word);
490*67a95c21SRishi Gupta 		break;
491*67a95c21SRishi Gupta 	case I2C_SMBUS_BLOCK_PROC_CALL:
492*67a95c21SRishi Gupta 		ret = mcp_smbus_write(mcp, addr, command, data->block,
493*67a95c21SRishi Gupta 						data->block[0] + 1,
494*67a95c21SRishi Gupta 						MCP2221_I2C_WR_NO_STOP, 0);
495*67a95c21SRishi Gupta 		if (ret)
496*67a95c21SRishi Gupta 			goto exit;
497*67a95c21SRishi Gupta 
498*67a95c21SRishi Gupta 		ret = mcp_i2c_smbus_read(mcp, NULL,
499*67a95c21SRishi Gupta 						MCP2221_I2C_RD_RPT_START,
500*67a95c21SRishi Gupta 						addr, I2C_SMBUS_BLOCK_MAX,
501*67a95c21SRishi Gupta 						data->block);
502*67a95c21SRishi Gupta 		break;
503*67a95c21SRishi Gupta 	default:
504*67a95c21SRishi Gupta 		dev_err(&mcp->adapter.dev,
505*67a95c21SRishi Gupta 			"unsupported smbus transaction size:%d\n", size);
506*67a95c21SRishi Gupta 		ret = -EOPNOTSUPP;
507*67a95c21SRishi Gupta 	}
508*67a95c21SRishi Gupta 
509*67a95c21SRishi Gupta exit:
510*67a95c21SRishi Gupta 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
511*67a95c21SRishi Gupta 	mutex_unlock(&mcp->lock);
512*67a95c21SRishi Gupta 	return ret;
513*67a95c21SRishi Gupta }
514*67a95c21SRishi Gupta 
515*67a95c21SRishi Gupta static u32 mcp_i2c_func(struct i2c_adapter *adapter)
516*67a95c21SRishi Gupta {
517*67a95c21SRishi Gupta 	return I2C_FUNC_I2C |
518*67a95c21SRishi Gupta 			I2C_FUNC_SMBUS_READ_BLOCK_DATA |
519*67a95c21SRishi Gupta 			I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
520*67a95c21SRishi Gupta 			(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
521*67a95c21SRishi Gupta }
522*67a95c21SRishi Gupta 
523*67a95c21SRishi Gupta static const struct i2c_algorithm mcp_i2c_algo = {
524*67a95c21SRishi Gupta 	.master_xfer = mcp_i2c_xfer,
525*67a95c21SRishi Gupta 	.smbus_xfer = mcp_smbus_xfer,
526*67a95c21SRishi Gupta 	.functionality = mcp_i2c_func,
527*67a95c21SRishi Gupta };
528*67a95c21SRishi Gupta 
529*67a95c21SRishi Gupta /* Gives current state of i2c engine inside mcp2221 */
530*67a95c21SRishi Gupta static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
531*67a95c21SRishi Gupta 				u8 *data, u8 idx)
532*67a95c21SRishi Gupta {
533*67a95c21SRishi Gupta 	int ret;
534*67a95c21SRishi Gupta 
535*67a95c21SRishi Gupta 	switch (data[idx]) {
536*67a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_NACK:
537*67a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_SEND:
538*67a95c21SRishi Gupta 		ret = -ENXIO;
539*67a95c21SRishi Gupta 		break;
540*67a95c21SRishi Gupta 	case MCP2221_I2C_START_TOUT:
541*67a95c21SRishi Gupta 	case MCP2221_I2C_STOP_TOUT:
542*67a95c21SRishi Gupta 	case MCP2221_I2C_WRADDRL_TOUT:
543*67a95c21SRishi Gupta 	case MCP2221_I2C_WRDATA_TOUT:
544*67a95c21SRishi Gupta 		ret = -ETIMEDOUT;
545*67a95c21SRishi Gupta 		break;
546*67a95c21SRishi Gupta 	case MCP2221_I2C_ENG_BUSY:
547*67a95c21SRishi Gupta 		ret = -EAGAIN;
548*67a95c21SRishi Gupta 		break;
549*67a95c21SRishi Gupta 	case MCP2221_SUCCESS:
550*67a95c21SRishi Gupta 		ret = 0x00;
551*67a95c21SRishi Gupta 		break;
552*67a95c21SRishi Gupta 	default:
553*67a95c21SRishi Gupta 		ret = -EIO;
554*67a95c21SRishi Gupta 	}
555*67a95c21SRishi Gupta 
556*67a95c21SRishi Gupta 	return ret;
557*67a95c21SRishi Gupta }
558*67a95c21SRishi Gupta 
559*67a95c21SRishi Gupta /*
560*67a95c21SRishi Gupta  * MCP2221 uses interrupt endpoint for input reports. This function
561*67a95c21SRishi Gupta  * is called by HID layer when it receives i/p report from mcp2221,
562*67a95c21SRishi Gupta  * which is actually a response to the previously sent command.
563*67a95c21SRishi Gupta  *
564*67a95c21SRishi Gupta  * MCP2221A firmware specific return codes are parsed and 0 or
565*67a95c21SRishi Gupta  * appropriate negative error code is returned. Delayed response
566*67a95c21SRishi Gupta  * results in timeout error and stray reponses results in -EIO.
567*67a95c21SRishi Gupta  */
568*67a95c21SRishi Gupta static int mcp2221_raw_event(struct hid_device *hdev,
569*67a95c21SRishi Gupta 				struct hid_report *report, u8 *data, int size)
570*67a95c21SRishi Gupta {
571*67a95c21SRishi Gupta 	u8 *buf;
572*67a95c21SRishi Gupta 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
573*67a95c21SRishi Gupta 
574*67a95c21SRishi Gupta 	switch (data[0]) {
575*67a95c21SRishi Gupta 
576*67a95c21SRishi Gupta 	case MCP2221_I2C_WR_DATA:
577*67a95c21SRishi Gupta 	case MCP2221_I2C_WR_NO_STOP:
578*67a95c21SRishi Gupta 	case MCP2221_I2C_RD_DATA:
579*67a95c21SRishi Gupta 	case MCP2221_I2C_RD_RPT_START:
580*67a95c21SRishi Gupta 		switch (data[1]) {
581*67a95c21SRishi Gupta 		case MCP2221_SUCCESS:
582*67a95c21SRishi Gupta 			mcp->status = 0;
583*67a95c21SRishi Gupta 			break;
584*67a95c21SRishi Gupta 		default:
585*67a95c21SRishi Gupta 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
586*67a95c21SRishi Gupta 		}
587*67a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
588*67a95c21SRishi Gupta 		break;
589*67a95c21SRishi Gupta 
590*67a95c21SRishi Gupta 	case MCP2221_I2C_PARAM_OR_STATUS:
591*67a95c21SRishi Gupta 		switch (data[1]) {
592*67a95c21SRishi Gupta 		case MCP2221_SUCCESS:
593*67a95c21SRishi Gupta 			if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
594*67a95c21SRishi Gupta 				(data[3] != MCP2221_I2C_SET_SPEED)) {
595*67a95c21SRishi Gupta 				mcp->status = -EAGAIN;
596*67a95c21SRishi Gupta 				break;
597*67a95c21SRishi Gupta 			}
598*67a95c21SRishi Gupta 			if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
599*67a95c21SRishi Gupta 				mcp->status = -ENXIO;
600*67a95c21SRishi Gupta 				break;
601*67a95c21SRishi Gupta 			}
602*67a95c21SRishi Gupta 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
603*67a95c21SRishi Gupta 			break;
604*67a95c21SRishi Gupta 		default:
605*67a95c21SRishi Gupta 			mcp->status = -EIO;
606*67a95c21SRishi Gupta 		}
607*67a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
608*67a95c21SRishi Gupta 		break;
609*67a95c21SRishi Gupta 
610*67a95c21SRishi Gupta 	case MCP2221_I2C_GET_DATA:
611*67a95c21SRishi Gupta 		switch (data[1]) {
612*67a95c21SRishi Gupta 		case MCP2221_SUCCESS:
613*67a95c21SRishi Gupta 			if (data[2] == MCP2221_I2C_ADDR_NACK) {
614*67a95c21SRishi Gupta 				mcp->status = -ENXIO;
615*67a95c21SRishi Gupta 				break;
616*67a95c21SRishi Gupta 			}
617*67a95c21SRishi Gupta 			if (!mcp_get_i2c_eng_state(mcp, data, 2)
618*67a95c21SRishi Gupta 				&& (data[3] == 0)) {
619*67a95c21SRishi Gupta 				mcp->status = 0;
620*67a95c21SRishi Gupta 				break;
621*67a95c21SRishi Gupta 			}
622*67a95c21SRishi Gupta 			if (data[3] == 127) {
623*67a95c21SRishi Gupta 				mcp->status = -EIO;
624*67a95c21SRishi Gupta 				break;
625*67a95c21SRishi Gupta 			}
626*67a95c21SRishi Gupta 			if (data[2] == MCP2221_I2C_READ_COMPL) {
627*67a95c21SRishi Gupta 				buf = mcp->rxbuf;
628*67a95c21SRishi Gupta 				memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
629*67a95c21SRishi Gupta 				mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
630*67a95c21SRishi Gupta 				mcp->status = 0;
631*67a95c21SRishi Gupta 				break;
632*67a95c21SRishi Gupta 			}
633*67a95c21SRishi Gupta 			mcp->status = -EIO;
634*67a95c21SRishi Gupta 			break;
635*67a95c21SRishi Gupta 		default:
636*67a95c21SRishi Gupta 			mcp->status = -EIO;
637*67a95c21SRishi Gupta 		}
638*67a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
639*67a95c21SRishi Gupta 		break;
640*67a95c21SRishi Gupta 
641*67a95c21SRishi Gupta 	default:
642*67a95c21SRishi Gupta 		mcp->status = -EIO;
643*67a95c21SRishi Gupta 		complete(&mcp->wait_in_report);
644*67a95c21SRishi Gupta 	}
645*67a95c21SRishi Gupta 
646*67a95c21SRishi Gupta 	return 1;
647*67a95c21SRishi Gupta }
648*67a95c21SRishi Gupta 
649*67a95c21SRishi Gupta static int mcp2221_probe(struct hid_device *hdev,
650*67a95c21SRishi Gupta 					const struct hid_device_id *id)
651*67a95c21SRishi Gupta {
652*67a95c21SRishi Gupta 	int ret;
653*67a95c21SRishi Gupta 	struct mcp2221 *mcp;
654*67a95c21SRishi Gupta 
655*67a95c21SRishi Gupta 	mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
656*67a95c21SRishi Gupta 	if (!mcp)
657*67a95c21SRishi Gupta 		return -ENOMEM;
658*67a95c21SRishi Gupta 
659*67a95c21SRishi Gupta 	ret = hid_parse(hdev);
660*67a95c21SRishi Gupta 	if (ret) {
661*67a95c21SRishi Gupta 		hid_err(hdev, "can't parse reports\n");
662*67a95c21SRishi Gupta 		return ret;
663*67a95c21SRishi Gupta 	}
664*67a95c21SRishi Gupta 
665*67a95c21SRishi Gupta 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
666*67a95c21SRishi Gupta 	if (ret) {
667*67a95c21SRishi Gupta 		hid_err(hdev, "can't start hardware\n");
668*67a95c21SRishi Gupta 		return ret;
669*67a95c21SRishi Gupta 	}
670*67a95c21SRishi Gupta 
671*67a95c21SRishi Gupta 	ret = hid_hw_open(hdev);
672*67a95c21SRishi Gupta 	if (ret) {
673*67a95c21SRishi Gupta 		hid_err(hdev, "can't open device\n");
674*67a95c21SRishi Gupta 		goto err_hstop;
675*67a95c21SRishi Gupta 	}
676*67a95c21SRishi Gupta 
677*67a95c21SRishi Gupta 	mutex_init(&mcp->lock);
678*67a95c21SRishi Gupta 	init_completion(&mcp->wait_in_report);
679*67a95c21SRishi Gupta 	hid_set_drvdata(hdev, mcp);
680*67a95c21SRishi Gupta 	mcp->hdev = hdev;
681*67a95c21SRishi Gupta 
682*67a95c21SRishi Gupta 	/* Set I2C bus clock diviser */
683*67a95c21SRishi Gupta 	if (i2c_clk_freq > 400)
684*67a95c21SRishi Gupta 		i2c_clk_freq = 400;
685*67a95c21SRishi Gupta 	if (i2c_clk_freq < 50)
686*67a95c21SRishi Gupta 		i2c_clk_freq = 50;
687*67a95c21SRishi Gupta 	mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
688*67a95c21SRishi Gupta 
689*67a95c21SRishi Gupta 	mcp->adapter.owner = THIS_MODULE;
690*67a95c21SRishi Gupta 	mcp->adapter.class = I2C_CLASS_HWMON;
691*67a95c21SRishi Gupta 	mcp->adapter.algo = &mcp_i2c_algo;
692*67a95c21SRishi Gupta 	mcp->adapter.retries = 1;
693*67a95c21SRishi Gupta 	mcp->adapter.dev.parent = &hdev->dev;
694*67a95c21SRishi Gupta 	snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
695*67a95c21SRishi Gupta 			"MCP2221 usb-i2c bridge on hidraw%d",
696*67a95c21SRishi Gupta 			((struct hidraw *)hdev->hidraw)->minor);
697*67a95c21SRishi Gupta 
698*67a95c21SRishi Gupta 	ret = i2c_add_adapter(&mcp->adapter);
699*67a95c21SRishi Gupta 	if (ret) {
700*67a95c21SRishi Gupta 		hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
701*67a95c21SRishi Gupta 		goto err_i2c;
702*67a95c21SRishi Gupta 	}
703*67a95c21SRishi Gupta 	i2c_set_adapdata(&mcp->adapter, mcp);
704*67a95c21SRishi Gupta 
705*67a95c21SRishi Gupta 	return 0;
706*67a95c21SRishi Gupta 
707*67a95c21SRishi Gupta err_i2c:
708*67a95c21SRishi Gupta 	hid_hw_close(mcp->hdev);
709*67a95c21SRishi Gupta err_hstop:
710*67a95c21SRishi Gupta 	hid_hw_stop(mcp->hdev);
711*67a95c21SRishi Gupta 	return ret;
712*67a95c21SRishi Gupta }
713*67a95c21SRishi Gupta 
714*67a95c21SRishi Gupta static void mcp2221_remove(struct hid_device *hdev)
715*67a95c21SRishi Gupta {
716*67a95c21SRishi Gupta 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
717*67a95c21SRishi Gupta 
718*67a95c21SRishi Gupta 	i2c_del_adapter(&mcp->adapter);
719*67a95c21SRishi Gupta 	hid_hw_close(mcp->hdev);
720*67a95c21SRishi Gupta 	hid_hw_stop(mcp->hdev);
721*67a95c21SRishi Gupta }
722*67a95c21SRishi Gupta 
723*67a95c21SRishi Gupta static const struct hid_device_id mcp2221_devices[] = {
724*67a95c21SRishi Gupta 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
725*67a95c21SRishi Gupta 	{ }
726*67a95c21SRishi Gupta };
727*67a95c21SRishi Gupta MODULE_DEVICE_TABLE(hid, mcp2221_devices);
728*67a95c21SRishi Gupta 
729*67a95c21SRishi Gupta static struct hid_driver mcp2221_driver = {
730*67a95c21SRishi Gupta 	.name		= "mcp2221",
731*67a95c21SRishi Gupta 	.id_table	= mcp2221_devices,
732*67a95c21SRishi Gupta 	.probe		= mcp2221_probe,
733*67a95c21SRishi Gupta 	.remove		= mcp2221_remove,
734*67a95c21SRishi Gupta 	.raw_event	= mcp2221_raw_event,
735*67a95c21SRishi Gupta };
736*67a95c21SRishi Gupta 
737*67a95c21SRishi Gupta /* Register with HID core */
738*67a95c21SRishi Gupta module_hid_driver(mcp2221_driver);
739*67a95c21SRishi Gupta 
740*67a95c21SRishi Gupta MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
741*67a95c21SRishi Gupta MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
742*67a95c21SRishi Gupta MODULE_LICENSE("GPL v2");
743