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