xref: /openbmc/linux/drivers/hid/hid-mcp2221.c (revision e6e8c6c2)
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 		if (len > I2C_SMBUS_BLOCK_MAX)
389 			return -EINVAL;
390 
391 		memcpy(&mcp->txbuf[5], buf, len);
392 		data_len = len + 5;
393 	}
394 
395 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len);
396 	if (ret)
397 		return ret;
398 
399 	if (last_status) {
400 		usleep_range(980, 1000);
401 
402 		ret = mcp_chk_last_cmd_status(mcp);
403 		if (ret)
404 			return ret;
405 	}
406 
407 	return ret;
408 }
409 
410 static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
411 				unsigned short flags, char read_write,
412 				u8 command, int size,
413 				union i2c_smbus_data *data)
414 {
415 	int ret;
416 	struct mcp2221 *mcp = i2c_get_adapdata(adapter);
417 
418 	hid_hw_power(mcp->hdev, PM_HINT_FULLON);
419 
420 	mutex_lock(&mcp->lock);
421 
422 	ret = mcp_set_i2c_speed(mcp);
423 	if (ret)
424 		goto exit;
425 
426 	switch (size) {
427 
428 	case I2C_SMBUS_QUICK:
429 		if (read_write == I2C_SMBUS_READ)
430 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
431 						addr, 0, &data->byte);
432 		else
433 			ret = mcp_smbus_write(mcp, addr, command, NULL,
434 						0, MCP2221_I2C_WR_DATA, 1);
435 		break;
436 	case I2C_SMBUS_BYTE:
437 		if (read_write == I2C_SMBUS_READ)
438 			ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA,
439 						addr, 1, &data->byte);
440 		else
441 			ret = mcp_smbus_write(mcp, addr, command, NULL,
442 						0, MCP2221_I2C_WR_DATA, 1);
443 		break;
444 	case I2C_SMBUS_BYTE_DATA:
445 		if (read_write == I2C_SMBUS_READ) {
446 			ret = mcp_smbus_write(mcp, addr, command, NULL,
447 						0, MCP2221_I2C_WR_NO_STOP, 0);
448 			if (ret)
449 				goto exit;
450 
451 			ret = mcp_i2c_smbus_read(mcp, NULL,
452 						MCP2221_I2C_RD_RPT_START,
453 						addr, 1, &data->byte);
454 		} else {
455 			ret = mcp_smbus_write(mcp, addr, command, &data->byte,
456 						1, MCP2221_I2C_WR_DATA, 1);
457 		}
458 		break;
459 	case I2C_SMBUS_WORD_DATA:
460 		if (read_write == I2C_SMBUS_READ) {
461 			ret = mcp_smbus_write(mcp, addr, command, NULL,
462 						0, MCP2221_I2C_WR_NO_STOP, 0);
463 			if (ret)
464 				goto exit;
465 
466 			ret = mcp_i2c_smbus_read(mcp, NULL,
467 						MCP2221_I2C_RD_RPT_START,
468 						addr, 2, (u8 *)&data->word);
469 		} else {
470 			ret = mcp_smbus_write(mcp, addr, command,
471 						(u8 *)&data->word, 2,
472 						MCP2221_I2C_WR_DATA, 1);
473 		}
474 		break;
475 	case I2C_SMBUS_BLOCK_DATA:
476 		if (read_write == I2C_SMBUS_READ) {
477 			ret = mcp_smbus_write(mcp, addr, command, NULL,
478 						0, MCP2221_I2C_WR_NO_STOP, 1);
479 			if (ret)
480 				goto exit;
481 
482 			mcp->rxbuf_idx = 0;
483 			mcp->rxbuf = data->block;
484 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
485 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
486 			if (ret)
487 				goto exit;
488 		} else {
489 			if (!data->block[0]) {
490 				ret = -EINVAL;
491 				goto exit;
492 			}
493 			ret = mcp_smbus_write(mcp, addr, command, data->block,
494 						data->block[0] + 1,
495 						MCP2221_I2C_WR_DATA, 1);
496 		}
497 		break;
498 	case I2C_SMBUS_I2C_BLOCK_DATA:
499 		if (read_write == I2C_SMBUS_READ) {
500 			ret = mcp_smbus_write(mcp, addr, command, NULL,
501 						0, MCP2221_I2C_WR_NO_STOP, 1);
502 			if (ret)
503 				goto exit;
504 
505 			mcp->rxbuf_idx = 0;
506 			mcp->rxbuf = data->block;
507 			mcp->txbuf[0] = MCP2221_I2C_GET_DATA;
508 			ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
509 			if (ret)
510 				goto exit;
511 		} else {
512 			if (!data->block[0]) {
513 				ret = -EINVAL;
514 				goto exit;
515 			}
516 			ret = mcp_smbus_write(mcp, addr, command,
517 						&data->block[1], data->block[0],
518 						MCP2221_I2C_WR_DATA, 1);
519 		}
520 		break;
521 	case I2C_SMBUS_PROC_CALL:
522 		ret = mcp_smbus_write(mcp, addr, command,
523 						(u8 *)&data->word,
524 						2, MCP2221_I2C_WR_NO_STOP, 0);
525 		if (ret)
526 			goto exit;
527 
528 		ret = mcp_i2c_smbus_read(mcp, NULL,
529 						MCP2221_I2C_RD_RPT_START,
530 						addr, 2, (u8 *)&data->word);
531 		break;
532 	case I2C_SMBUS_BLOCK_PROC_CALL:
533 		ret = mcp_smbus_write(mcp, addr, command, data->block,
534 						data->block[0] + 1,
535 						MCP2221_I2C_WR_NO_STOP, 0);
536 		if (ret)
537 			goto exit;
538 
539 		ret = mcp_i2c_smbus_read(mcp, NULL,
540 						MCP2221_I2C_RD_RPT_START,
541 						addr, I2C_SMBUS_BLOCK_MAX,
542 						data->block);
543 		break;
544 	default:
545 		dev_err(&mcp->adapter.dev,
546 			"unsupported smbus transaction size:%d\n", size);
547 		ret = -EOPNOTSUPP;
548 	}
549 
550 exit:
551 	hid_hw_power(mcp->hdev, PM_HINT_NORMAL);
552 	mutex_unlock(&mcp->lock);
553 	return ret;
554 }
555 
556 static u32 mcp_i2c_func(struct i2c_adapter *adapter)
557 {
558 	return I2C_FUNC_I2C |
559 			I2C_FUNC_SMBUS_READ_BLOCK_DATA |
560 			I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
561 			(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC);
562 }
563 
564 static const struct i2c_algorithm mcp_i2c_algo = {
565 	.master_xfer = mcp_i2c_xfer,
566 	.smbus_xfer = mcp_smbus_xfer,
567 	.functionality = mcp_i2c_func,
568 };
569 
570 static int mcp_gpio_get(struct gpio_chip *gc,
571 				unsigned int offset)
572 {
573 	int ret;
574 	struct mcp2221 *mcp = gpiochip_get_data(gc);
575 
576 	mcp->txbuf[0] = MCP2221_GPIO_GET;
577 
578 	mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].value);
579 
580 	mutex_lock(&mcp->lock);
581 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
582 	mutex_unlock(&mcp->lock);
583 
584 	return ret;
585 }
586 
587 static void mcp_gpio_set(struct gpio_chip *gc,
588 				unsigned int offset, int value)
589 {
590 	struct mcp2221 *mcp = gpiochip_get_data(gc);
591 
592 	memset(mcp->txbuf, 0, 18);
593 	mcp->txbuf[0] = MCP2221_GPIO_SET;
594 
595 	mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value);
596 
597 	mcp->txbuf[mcp->gp_idx - 1] = 1;
598 	mcp->txbuf[mcp->gp_idx] = !!value;
599 
600 	mutex_lock(&mcp->lock);
601 	mcp_send_data_req_status(mcp, mcp->txbuf, 18);
602 	mutex_unlock(&mcp->lock);
603 }
604 
605 static int mcp_gpio_dir_set(struct mcp2221 *mcp,
606 				unsigned int offset, u8 val)
607 {
608 	memset(mcp->txbuf, 0, 18);
609 	mcp->txbuf[0] = MCP2221_GPIO_SET;
610 
611 	mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction);
612 
613 	mcp->txbuf[mcp->gp_idx - 1] = 1;
614 	mcp->txbuf[mcp->gp_idx] = val;
615 
616 	return mcp_send_data_req_status(mcp, mcp->txbuf, 18);
617 }
618 
619 static int mcp_gpio_direction_input(struct gpio_chip *gc,
620 				unsigned int offset)
621 {
622 	int ret;
623 	struct mcp2221 *mcp = gpiochip_get_data(gc);
624 
625 	mutex_lock(&mcp->lock);
626 	ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN);
627 	mutex_unlock(&mcp->lock);
628 
629 	return ret;
630 }
631 
632 static int mcp_gpio_direction_output(struct gpio_chip *gc,
633 				unsigned int offset, int value)
634 {
635 	int ret;
636 	struct mcp2221 *mcp = gpiochip_get_data(gc);
637 
638 	mutex_lock(&mcp->lock);
639 	ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT);
640 	mutex_unlock(&mcp->lock);
641 
642 	/* Can't configure as output, bailout early */
643 	if (ret)
644 		return ret;
645 
646 	mcp_gpio_set(gc, offset, value);
647 
648 	return 0;
649 }
650 
651 static int mcp_gpio_get_direction(struct gpio_chip *gc,
652 				unsigned int offset)
653 {
654 	int ret;
655 	struct mcp2221 *mcp = gpiochip_get_data(gc);
656 
657 	mcp->txbuf[0] = MCP2221_GPIO_GET;
658 
659 	mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset].direction);
660 
661 	mutex_lock(&mcp->lock);
662 	ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
663 	mutex_unlock(&mcp->lock);
664 
665 	if (ret)
666 		return ret;
667 
668 	if (mcp->gpio_dir == MCP2221_DIR_IN)
669 		return GPIO_LINE_DIRECTION_IN;
670 
671 	return GPIO_LINE_DIRECTION_OUT;
672 }
673 
674 /* Gives current state of i2c engine inside mcp2221 */
675 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
676 				u8 *data, u8 idx)
677 {
678 	int ret;
679 
680 	switch (data[idx]) {
681 	case MCP2221_I2C_WRADDRL_NACK:
682 	case MCP2221_I2C_WRADDRL_SEND:
683 		ret = -ENXIO;
684 		break;
685 	case MCP2221_I2C_START_TOUT:
686 	case MCP2221_I2C_STOP_TOUT:
687 	case MCP2221_I2C_WRADDRL_TOUT:
688 	case MCP2221_I2C_WRDATA_TOUT:
689 		ret = -ETIMEDOUT;
690 		break;
691 	case MCP2221_I2C_ENG_BUSY:
692 		ret = -EAGAIN;
693 		break;
694 	case MCP2221_SUCCESS:
695 		ret = 0x00;
696 		break;
697 	default:
698 		ret = -EIO;
699 	}
700 
701 	return ret;
702 }
703 
704 /*
705  * MCP2221 uses interrupt endpoint for input reports. This function
706  * is called by HID layer when it receives i/p report from mcp2221,
707  * which is actually a response to the previously sent command.
708  *
709  * MCP2221A firmware specific return codes are parsed and 0 or
710  * appropriate negative error code is returned. Delayed response
711  * results in timeout error and stray reponses results in -EIO.
712  */
713 static int mcp2221_raw_event(struct hid_device *hdev,
714 				struct hid_report *report, u8 *data, int size)
715 {
716 	u8 *buf;
717 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
718 
719 	switch (data[0]) {
720 
721 	case MCP2221_I2C_WR_DATA:
722 	case MCP2221_I2C_WR_NO_STOP:
723 	case MCP2221_I2C_RD_DATA:
724 	case MCP2221_I2C_RD_RPT_START:
725 		switch (data[1]) {
726 		case MCP2221_SUCCESS:
727 			mcp->status = 0;
728 			break;
729 		default:
730 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 2);
731 		}
732 		complete(&mcp->wait_in_report);
733 		break;
734 
735 	case MCP2221_I2C_PARAM_OR_STATUS:
736 		switch (data[1]) {
737 		case MCP2221_SUCCESS:
738 			if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) &&
739 				(data[3] != MCP2221_I2C_SET_SPEED)) {
740 				mcp->status = -EAGAIN;
741 				break;
742 			}
743 			if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) {
744 				mcp->status = -ENXIO;
745 				break;
746 			}
747 			mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
748 			break;
749 		default:
750 			mcp->status = -EIO;
751 		}
752 		complete(&mcp->wait_in_report);
753 		break;
754 
755 	case MCP2221_I2C_GET_DATA:
756 		switch (data[1]) {
757 		case MCP2221_SUCCESS:
758 			if (data[2] == MCP2221_I2C_ADDR_NACK) {
759 				mcp->status = -ENXIO;
760 				break;
761 			}
762 			if (!mcp_get_i2c_eng_state(mcp, data, 2)
763 				&& (data[3] == 0)) {
764 				mcp->status = 0;
765 				break;
766 			}
767 			if (data[3] == 127) {
768 				mcp->status = -EIO;
769 				break;
770 			}
771 			if (data[2] == MCP2221_I2C_READ_COMPL) {
772 				buf = mcp->rxbuf;
773 				memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]);
774 				mcp->rxbuf_idx = mcp->rxbuf_idx + data[3];
775 				mcp->status = 0;
776 				break;
777 			}
778 			mcp->status = -EIO;
779 			break;
780 		default:
781 			mcp->status = -EIO;
782 		}
783 		complete(&mcp->wait_in_report);
784 		break;
785 
786 	case MCP2221_GPIO_GET:
787 		switch (data[1]) {
788 		case MCP2221_SUCCESS:
789 			if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
790 				(data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) {
791 				mcp->status = -ENOENT;
792 			} else {
793 				mcp->status = !!data[mcp->gp_idx];
794 				mcp->gpio_dir = data[mcp->gp_idx + 1];
795 			}
796 			break;
797 		default:
798 			mcp->status = -EAGAIN;
799 		}
800 		complete(&mcp->wait_in_report);
801 		break;
802 
803 	case MCP2221_GPIO_SET:
804 		switch (data[1]) {
805 		case MCP2221_SUCCESS:
806 			if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) ||
807 				(data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) {
808 				mcp->status = -ENOENT;
809 			} else {
810 				mcp->status = 0;
811 			}
812 			break;
813 		default:
814 			mcp->status = -EAGAIN;
815 		}
816 		complete(&mcp->wait_in_report);
817 		break;
818 
819 	default:
820 		mcp->status = -EIO;
821 		complete(&mcp->wait_in_report);
822 	}
823 
824 	return 1;
825 }
826 
827 static int mcp2221_probe(struct hid_device *hdev,
828 					const struct hid_device_id *id)
829 {
830 	int ret;
831 	struct mcp2221 *mcp;
832 
833 	mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL);
834 	if (!mcp)
835 		return -ENOMEM;
836 
837 	ret = hid_parse(hdev);
838 	if (ret) {
839 		hid_err(hdev, "can't parse reports\n");
840 		return ret;
841 	}
842 
843 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
844 	if (ret) {
845 		hid_err(hdev, "can't start hardware\n");
846 		return ret;
847 	}
848 
849 	ret = hid_hw_open(hdev);
850 	if (ret) {
851 		hid_err(hdev, "can't open device\n");
852 		goto err_hstop;
853 	}
854 
855 	mutex_init(&mcp->lock);
856 	init_completion(&mcp->wait_in_report);
857 	hid_set_drvdata(hdev, mcp);
858 	mcp->hdev = hdev;
859 
860 	/* Set I2C bus clock diviser */
861 	if (i2c_clk_freq > 400)
862 		i2c_clk_freq = 400;
863 	if (i2c_clk_freq < 50)
864 		i2c_clk_freq = 50;
865 	mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3;
866 
867 	mcp->adapter.owner = THIS_MODULE;
868 	mcp->adapter.class = I2C_CLASS_HWMON;
869 	mcp->adapter.algo = &mcp_i2c_algo;
870 	mcp->adapter.retries = 1;
871 	mcp->adapter.dev.parent = &hdev->dev;
872 	snprintf(mcp->adapter.name, sizeof(mcp->adapter.name),
873 			"MCP2221 usb-i2c bridge on hidraw%d",
874 			((struct hidraw *)hdev->hidraw)->minor);
875 
876 	ret = i2c_add_adapter(&mcp->adapter);
877 	if (ret) {
878 		hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret);
879 		goto err_i2c;
880 	}
881 	i2c_set_adapdata(&mcp->adapter, mcp);
882 
883 	/* Setup GPIO chip */
884 	mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL);
885 	if (!mcp->gc) {
886 		ret = -ENOMEM;
887 		goto err_gc;
888 	}
889 
890 	mcp->gc->label = "mcp2221_gpio";
891 	mcp->gc->direction_input = mcp_gpio_direction_input;
892 	mcp->gc->direction_output = mcp_gpio_direction_output;
893 	mcp->gc->get_direction = mcp_gpio_get_direction;
894 	mcp->gc->set = mcp_gpio_set;
895 	mcp->gc->get = mcp_gpio_get;
896 	mcp->gc->ngpio = MCP_NGPIO;
897 	mcp->gc->base = -1;
898 	mcp->gc->can_sleep = 1;
899 	mcp->gc->parent = &hdev->dev;
900 
901 	ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp);
902 	if (ret)
903 		goto err_gc;
904 
905 	return 0;
906 
907 err_gc:
908 	i2c_del_adapter(&mcp->adapter);
909 err_i2c:
910 	hid_hw_close(mcp->hdev);
911 err_hstop:
912 	hid_hw_stop(mcp->hdev);
913 	return ret;
914 }
915 
916 static void mcp2221_remove(struct hid_device *hdev)
917 {
918 	struct mcp2221 *mcp = hid_get_drvdata(hdev);
919 
920 	i2c_del_adapter(&mcp->adapter);
921 	hid_hw_close(mcp->hdev);
922 	hid_hw_stop(mcp->hdev);
923 }
924 
925 static const struct hid_device_id mcp2221_devices[] = {
926 	{ HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) },
927 	{ }
928 };
929 MODULE_DEVICE_TABLE(hid, mcp2221_devices);
930 
931 static struct hid_driver mcp2221_driver = {
932 	.name		= "mcp2221",
933 	.id_table	= mcp2221_devices,
934 	.probe		= mcp2221_probe,
935 	.remove		= mcp2221_remove,
936 	.raw_event	= mcp2221_raw_event,
937 };
938 
939 /* Register with HID core */
940 module_hid_driver(mcp2221_driver);
941 
942 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>");
943 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge");
944 MODULE_LICENSE("GPL v2");
945