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