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/bitfield.h> 14 #include <linux/completion.h> 15 #include <linux/delay.h> 16 #include <linux/hid.h> 17 #include <linux/hidraw.h> 18 #include <linux/i2c.h> 19 #include <linux/gpio/driver.h> 20 #include <linux/iio/iio.h> 21 #include "hid-ids.h" 22 23 /* Commands codes in a raw output report */ 24 enum { 25 MCP2221_I2C_WR_DATA = 0x90, 26 MCP2221_I2C_WR_NO_STOP = 0x94, 27 MCP2221_I2C_RD_DATA = 0x91, 28 MCP2221_I2C_RD_RPT_START = 0x93, 29 MCP2221_I2C_GET_DATA = 0x40, 30 MCP2221_I2C_PARAM_OR_STATUS = 0x10, 31 MCP2221_I2C_SET_SPEED = 0x20, 32 MCP2221_I2C_CANCEL = 0x10, 33 MCP2221_GPIO_SET = 0x50, 34 MCP2221_GPIO_GET = 0x51, 35 MCP2221_SET_SRAM_SETTINGS = 0x60, 36 MCP2221_GET_SRAM_SETTINGS = 0x61, 37 MCP2221_READ_FLASH_DATA = 0xb0, 38 }; 39 40 /* Response codes in a raw input report */ 41 enum { 42 MCP2221_SUCCESS = 0x00, 43 MCP2221_I2C_ENG_BUSY = 0x01, 44 MCP2221_I2C_START_TOUT = 0x12, 45 MCP2221_I2C_STOP_TOUT = 0x62, 46 MCP2221_I2C_WRADDRL_TOUT = 0x23, 47 MCP2221_I2C_WRDATA_TOUT = 0x44, 48 MCP2221_I2C_WRADDRL_NACK = 0x25, 49 MCP2221_I2C_MASK_ADDR_NACK = 0x40, 50 MCP2221_I2C_WRADDRL_SEND = 0x21, 51 MCP2221_I2C_ADDR_NACK = 0x25, 52 MCP2221_I2C_READ_PARTIAL = 0x54, 53 MCP2221_I2C_READ_COMPL = 0x55, 54 MCP2221_ALT_F_NOT_GPIOV = 0xEE, 55 MCP2221_ALT_F_NOT_GPIOD = 0xEF, 56 }; 57 58 /* MCP GPIO direction encoding */ 59 enum { 60 MCP2221_DIR_OUT = 0x00, 61 MCP2221_DIR_IN = 0x01, 62 }; 63 64 #define MCP_NGPIO 4 65 66 /* MCP GPIO set command layout */ 67 struct mcp_set_gpio { 68 u8 cmd; 69 u8 dummy; 70 struct { 71 u8 change_value; 72 u8 value; 73 u8 change_direction; 74 u8 direction; 75 } gpio[MCP_NGPIO]; 76 } __packed; 77 78 /* MCP GPIO get command layout */ 79 struct mcp_get_gpio { 80 u8 cmd; 81 u8 dummy; 82 struct { 83 u8 value; 84 u8 direction; 85 } gpio[MCP_NGPIO]; 86 } __packed; 87 88 /* 89 * There is no way to distinguish responses. Therefore next command 90 * is sent only after response to previous has been received. Mutex 91 * lock is used for this purpose mainly. 92 */ 93 struct mcp2221 { 94 struct hid_device *hdev; 95 struct i2c_adapter adapter; 96 struct mutex lock; 97 struct completion wait_in_report; 98 struct delayed_work init_work; 99 u8 *rxbuf; 100 u8 txbuf[64]; 101 int rxbuf_idx; 102 int status; 103 u8 cur_i2c_clk_div; 104 struct gpio_chip *gc; 105 u8 gp_idx; 106 u8 gpio_dir; 107 u8 mode[4]; 108 #if IS_REACHABLE(CONFIG_IIO) 109 struct iio_chan_spec iio_channels[3]; 110 u16 adc_values[3]; 111 u8 adc_scale; 112 u8 dac_value; 113 u16 dac_scale; 114 #endif 115 }; 116 117 struct mcp2221_iio { 118 struct mcp2221 *mcp; 119 }; 120 121 /* 122 * Default i2c bus clock frequency 400 kHz. Modify this if you 123 * want to set some other frequency (min 50 kHz - max 400 kHz). 124 */ 125 static uint i2c_clk_freq = 400; 126 127 /* Synchronously send output report to the device */ 128 static int mcp_send_report(struct mcp2221 *mcp, 129 u8 *out_report, size_t len) 130 { 131 u8 *buf; 132 int ret; 133 134 buf = kmemdup(out_report, len, GFP_KERNEL); 135 if (!buf) 136 return -ENOMEM; 137 138 /* mcp2221 uses interrupt endpoint for out reports */ 139 ret = hid_hw_output_report(mcp->hdev, buf, len); 140 kfree(buf); 141 142 if (ret < 0) 143 return ret; 144 return 0; 145 } 146 147 /* 148 * Send o/p report to the device and wait for i/p report to be 149 * received from the device. If the device does not respond, 150 * we timeout. 151 */ 152 static int mcp_send_data_req_status(struct mcp2221 *mcp, 153 u8 *out_report, int len) 154 { 155 int ret; 156 unsigned long t; 157 158 reinit_completion(&mcp->wait_in_report); 159 160 ret = mcp_send_report(mcp, out_report, len); 161 if (ret) 162 return ret; 163 164 t = wait_for_completion_timeout(&mcp->wait_in_report, 165 msecs_to_jiffies(4000)); 166 if (!t) 167 return -ETIMEDOUT; 168 169 return mcp->status; 170 } 171 172 /* Check pass/fail for actual communication with i2c slave */ 173 static int mcp_chk_last_cmd_status(struct mcp2221 *mcp) 174 { 175 memset(mcp->txbuf, 0, 8); 176 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 177 178 return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 179 } 180 181 /* Cancels last command releasing i2c bus just in case occupied */ 182 static int mcp_cancel_last_cmd(struct mcp2221 *mcp) 183 { 184 memset(mcp->txbuf, 0, 8); 185 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 186 mcp->txbuf[2] = MCP2221_I2C_CANCEL; 187 188 return mcp_send_data_req_status(mcp, mcp->txbuf, 8); 189 } 190 191 /* Check if the last command succeeded or failed and return the result. 192 * If the command did fail, cancel that command which will free the i2c bus. 193 */ 194 static int mcp_chk_last_cmd_status_free_bus(struct mcp2221 *mcp) 195 { 196 int ret; 197 198 ret = mcp_chk_last_cmd_status(mcp); 199 if (ret) { 200 /* The last command was a failure. 201 * Send a cancel which will also free the bus. 202 */ 203 usleep_range(980, 1000); 204 mcp_cancel_last_cmd(mcp); 205 } 206 207 return ret; 208 } 209 210 static int mcp_set_i2c_speed(struct mcp2221 *mcp) 211 { 212 int ret; 213 214 memset(mcp->txbuf, 0, 8); 215 mcp->txbuf[0] = MCP2221_I2C_PARAM_OR_STATUS; 216 mcp->txbuf[3] = MCP2221_I2C_SET_SPEED; 217 mcp->txbuf[4] = mcp->cur_i2c_clk_div; 218 219 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 8); 220 if (ret) { 221 /* Small delay is needed here */ 222 usleep_range(980, 1000); 223 mcp_cancel_last_cmd(mcp); 224 } 225 226 return 0; 227 } 228 229 /* 230 * An output report can contain minimum 1 and maximum 60 user data 231 * bytes. If the number of data bytes is more then 60, we send it 232 * in chunks of 60 bytes. Last chunk may contain exactly 60 or less 233 * bytes. Total number of bytes is informed in very first report to 234 * mcp2221, from that point onwards it first collect all the data 235 * from host and then send to i2c slave device. 236 */ 237 static int mcp_i2c_write(struct mcp2221 *mcp, 238 struct i2c_msg *msg, int type, u8 last_status) 239 { 240 int ret, len, idx, sent; 241 242 idx = 0; 243 sent = 0; 244 if (msg->len < 60) 245 len = msg->len; 246 else 247 len = 60; 248 249 do { 250 mcp->txbuf[0] = type; 251 mcp->txbuf[1] = msg->len & 0xff; 252 mcp->txbuf[2] = msg->len >> 8; 253 mcp->txbuf[3] = (u8)(msg->addr << 1); 254 255 memcpy(&mcp->txbuf[4], &msg->buf[idx], len); 256 257 ret = mcp_send_data_req_status(mcp, mcp->txbuf, len + 4); 258 if (ret) 259 return ret; 260 261 usleep_range(980, 1000); 262 263 if (last_status) { 264 ret = mcp_chk_last_cmd_status_free_bus(mcp); 265 if (ret) 266 return ret; 267 } 268 269 sent = sent + len; 270 if (sent >= msg->len) 271 break; 272 273 idx = idx + len; 274 if ((msg->len - sent) < 60) 275 len = msg->len - sent; 276 else 277 len = 60; 278 279 /* 280 * Testing shows delay is needed between successive writes 281 * otherwise next write fails on first-try from i2c core. 282 * This value is obtained through automated stress testing. 283 */ 284 usleep_range(980, 1000); 285 } while (len > 0); 286 287 return ret; 288 } 289 290 /* 291 * Device reads all data (0 - 65535 bytes) from i2c slave device and 292 * stores it in device itself. This data is read back from device to 293 * host in multiples of 60 bytes using input reports. 294 */ 295 static int mcp_i2c_smbus_read(struct mcp2221 *mcp, 296 struct i2c_msg *msg, int type, u16 smbus_addr, 297 u8 smbus_len, u8 *smbus_buf) 298 { 299 int ret; 300 u16 total_len; 301 int retries = 0; 302 303 mcp->txbuf[0] = type; 304 if (msg) { 305 mcp->txbuf[1] = msg->len & 0xff; 306 mcp->txbuf[2] = msg->len >> 8; 307 mcp->txbuf[3] = (u8)(msg->addr << 1); 308 total_len = msg->len; 309 mcp->rxbuf = msg->buf; 310 } else { 311 mcp->txbuf[1] = smbus_len; 312 mcp->txbuf[2] = 0; 313 mcp->txbuf[3] = (u8)(smbus_addr << 1); 314 total_len = smbus_len; 315 mcp->rxbuf = smbus_buf; 316 } 317 318 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 4); 319 if (ret) 320 return ret; 321 322 mcp->rxbuf_idx = 0; 323 324 do { 325 /* Wait for the data to be read by the device */ 326 usleep_range(980, 1000); 327 328 memset(mcp->txbuf, 0, 4); 329 mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 330 331 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 332 if (ret) { 333 if (retries < 5) { 334 /* The data wasn't ready to read. 335 * Wait a bit longer and try again. 336 */ 337 usleep_range(90, 100); 338 retries++; 339 } else { 340 return ret; 341 } 342 } else { 343 retries = 0; 344 } 345 } while (mcp->rxbuf_idx < total_len); 346 347 usleep_range(980, 1000); 348 ret = mcp_chk_last_cmd_status_free_bus(mcp); 349 350 return ret; 351 } 352 353 static int mcp_i2c_xfer(struct i2c_adapter *adapter, 354 struct i2c_msg msgs[], int num) 355 { 356 int ret; 357 struct mcp2221 *mcp = i2c_get_adapdata(adapter); 358 359 hid_hw_power(mcp->hdev, PM_HINT_FULLON); 360 361 mutex_lock(&mcp->lock); 362 363 if (num == 1) { 364 if (msgs->flags & I2C_M_RD) { 365 ret = mcp_i2c_smbus_read(mcp, msgs, MCP2221_I2C_RD_DATA, 366 0, 0, NULL); 367 } else { 368 ret = mcp_i2c_write(mcp, msgs, MCP2221_I2C_WR_DATA, 1); 369 } 370 if (ret) 371 goto exit; 372 ret = num; 373 } else if (num == 2) { 374 /* Ex transaction; send reg address and read its contents */ 375 if (msgs[0].addr == msgs[1].addr && 376 !(msgs[0].flags & I2C_M_RD) && 377 (msgs[1].flags & I2C_M_RD)) { 378 379 ret = mcp_i2c_write(mcp, &msgs[0], 380 MCP2221_I2C_WR_NO_STOP, 0); 381 if (ret) 382 goto exit; 383 384 ret = mcp_i2c_smbus_read(mcp, &msgs[1], 385 MCP2221_I2C_RD_RPT_START, 386 0, 0, NULL); 387 if (ret) 388 goto exit; 389 ret = num; 390 } else { 391 dev_err(&adapter->dev, 392 "unsupported multi-msg i2c transaction\n"); 393 ret = -EOPNOTSUPP; 394 } 395 } else { 396 dev_err(&adapter->dev, 397 "unsupported multi-msg i2c transaction\n"); 398 ret = -EOPNOTSUPP; 399 } 400 401 exit: 402 hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 403 mutex_unlock(&mcp->lock); 404 return ret; 405 } 406 407 static int mcp_smbus_write(struct mcp2221 *mcp, u16 addr, 408 u8 command, u8 *buf, u8 len, int type, 409 u8 last_status) 410 { 411 int data_len, ret; 412 413 mcp->txbuf[0] = type; 414 mcp->txbuf[1] = len + 1; /* 1 is due to command byte itself */ 415 mcp->txbuf[2] = 0; 416 mcp->txbuf[3] = (u8)(addr << 1); 417 mcp->txbuf[4] = command; 418 419 switch (len) { 420 case 0: 421 data_len = 5; 422 break; 423 case 1: 424 mcp->txbuf[5] = buf[0]; 425 data_len = 6; 426 break; 427 case 2: 428 mcp->txbuf[5] = buf[0]; 429 mcp->txbuf[6] = buf[1]; 430 data_len = 7; 431 break; 432 default: 433 if (len > I2C_SMBUS_BLOCK_MAX) 434 return -EINVAL; 435 436 memcpy(&mcp->txbuf[5], buf, len); 437 data_len = len + 5; 438 } 439 440 ret = mcp_send_data_req_status(mcp, mcp->txbuf, data_len); 441 if (ret) 442 return ret; 443 444 if (last_status) { 445 usleep_range(980, 1000); 446 447 ret = mcp_chk_last_cmd_status_free_bus(mcp); 448 } 449 450 return ret; 451 } 452 453 static int mcp_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 454 unsigned short flags, char read_write, 455 u8 command, int size, 456 union i2c_smbus_data *data) 457 { 458 int ret; 459 struct mcp2221 *mcp = i2c_get_adapdata(adapter); 460 461 hid_hw_power(mcp->hdev, PM_HINT_FULLON); 462 463 mutex_lock(&mcp->lock); 464 465 switch (size) { 466 467 case I2C_SMBUS_QUICK: 468 if (read_write == I2C_SMBUS_READ) 469 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 470 addr, 0, &data->byte); 471 else 472 ret = mcp_smbus_write(mcp, addr, command, NULL, 473 0, MCP2221_I2C_WR_DATA, 1); 474 break; 475 case I2C_SMBUS_BYTE: 476 if (read_write == I2C_SMBUS_READ) 477 ret = mcp_i2c_smbus_read(mcp, NULL, MCP2221_I2C_RD_DATA, 478 addr, 1, &data->byte); 479 else 480 ret = mcp_smbus_write(mcp, addr, command, NULL, 481 0, MCP2221_I2C_WR_DATA, 1); 482 break; 483 case I2C_SMBUS_BYTE_DATA: 484 if (read_write == I2C_SMBUS_READ) { 485 ret = mcp_smbus_write(mcp, addr, command, NULL, 486 0, MCP2221_I2C_WR_NO_STOP, 0); 487 if (ret) 488 goto exit; 489 490 ret = mcp_i2c_smbus_read(mcp, NULL, 491 MCP2221_I2C_RD_RPT_START, 492 addr, 1, &data->byte); 493 } else { 494 ret = mcp_smbus_write(mcp, addr, command, &data->byte, 495 1, MCP2221_I2C_WR_DATA, 1); 496 } 497 break; 498 case I2C_SMBUS_WORD_DATA: 499 if (read_write == I2C_SMBUS_READ) { 500 ret = mcp_smbus_write(mcp, addr, command, NULL, 501 0, MCP2221_I2C_WR_NO_STOP, 0); 502 if (ret) 503 goto exit; 504 505 ret = mcp_i2c_smbus_read(mcp, NULL, 506 MCP2221_I2C_RD_RPT_START, 507 addr, 2, (u8 *)&data->word); 508 } else { 509 ret = mcp_smbus_write(mcp, addr, command, 510 (u8 *)&data->word, 2, 511 MCP2221_I2C_WR_DATA, 1); 512 } 513 break; 514 case I2C_SMBUS_BLOCK_DATA: 515 if (read_write == I2C_SMBUS_READ) { 516 ret = mcp_smbus_write(mcp, addr, command, NULL, 517 0, MCP2221_I2C_WR_NO_STOP, 1); 518 if (ret) 519 goto exit; 520 521 mcp->rxbuf_idx = 0; 522 mcp->rxbuf = data->block; 523 mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 524 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 525 if (ret) 526 goto exit; 527 } else { 528 if (!data->block[0]) { 529 ret = -EINVAL; 530 goto exit; 531 } 532 ret = mcp_smbus_write(mcp, addr, command, data->block, 533 data->block[0] + 1, 534 MCP2221_I2C_WR_DATA, 1); 535 } 536 break; 537 case I2C_SMBUS_I2C_BLOCK_DATA: 538 if (read_write == I2C_SMBUS_READ) { 539 ret = mcp_smbus_write(mcp, addr, command, NULL, 540 0, MCP2221_I2C_WR_NO_STOP, 1); 541 if (ret) 542 goto exit; 543 544 mcp->rxbuf_idx = 0; 545 mcp->rxbuf = data->block; 546 mcp->txbuf[0] = MCP2221_I2C_GET_DATA; 547 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 548 if (ret) 549 goto exit; 550 } else { 551 if (!data->block[0]) { 552 ret = -EINVAL; 553 goto exit; 554 } 555 ret = mcp_smbus_write(mcp, addr, command, 556 &data->block[1], data->block[0], 557 MCP2221_I2C_WR_DATA, 1); 558 } 559 break; 560 case I2C_SMBUS_PROC_CALL: 561 ret = mcp_smbus_write(mcp, addr, command, 562 (u8 *)&data->word, 563 2, MCP2221_I2C_WR_NO_STOP, 0); 564 if (ret) 565 goto exit; 566 567 ret = mcp_i2c_smbus_read(mcp, NULL, 568 MCP2221_I2C_RD_RPT_START, 569 addr, 2, (u8 *)&data->word); 570 break; 571 case I2C_SMBUS_BLOCK_PROC_CALL: 572 ret = mcp_smbus_write(mcp, addr, command, data->block, 573 data->block[0] + 1, 574 MCP2221_I2C_WR_NO_STOP, 0); 575 if (ret) 576 goto exit; 577 578 ret = mcp_i2c_smbus_read(mcp, NULL, 579 MCP2221_I2C_RD_RPT_START, 580 addr, I2C_SMBUS_BLOCK_MAX, 581 data->block); 582 break; 583 default: 584 dev_err(&mcp->adapter.dev, 585 "unsupported smbus transaction size:%d\n", size); 586 ret = -EOPNOTSUPP; 587 } 588 589 exit: 590 hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 591 mutex_unlock(&mcp->lock); 592 return ret; 593 } 594 595 static u32 mcp_i2c_func(struct i2c_adapter *adapter) 596 { 597 return I2C_FUNC_I2C | 598 I2C_FUNC_SMBUS_READ_BLOCK_DATA | 599 I2C_FUNC_SMBUS_BLOCK_PROC_CALL | 600 (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_PEC); 601 } 602 603 static const struct i2c_algorithm mcp_i2c_algo = { 604 .master_xfer = mcp_i2c_xfer, 605 .smbus_xfer = mcp_smbus_xfer, 606 .functionality = mcp_i2c_func, 607 }; 608 609 #if IS_REACHABLE(CONFIG_GPIOLIB) 610 static int mcp_gpio_get(struct gpio_chip *gc, 611 unsigned int offset) 612 { 613 int ret; 614 struct mcp2221 *mcp = gpiochip_get_data(gc); 615 616 mcp->txbuf[0] = MCP2221_GPIO_GET; 617 618 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]); 619 620 mutex_lock(&mcp->lock); 621 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 622 mutex_unlock(&mcp->lock); 623 624 return ret; 625 } 626 627 static int mcp_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) 628 { 629 struct mcp2221 *mcp = gpiochip_get_data(gc); 630 int ret; 631 632 memset(mcp->txbuf, 0, 18); 633 mcp->txbuf[0] = MCP2221_GPIO_SET; 634 635 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].value); 636 637 mcp->txbuf[mcp->gp_idx - 1] = 1; 638 mcp->txbuf[mcp->gp_idx] = !!value; 639 640 mutex_lock(&mcp->lock); 641 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 18); 642 mutex_unlock(&mcp->lock); 643 644 return ret; 645 } 646 647 static int mcp_gpio_dir_set(struct mcp2221 *mcp, 648 unsigned int offset, u8 val) 649 { 650 memset(mcp->txbuf, 0, 18); 651 mcp->txbuf[0] = MCP2221_GPIO_SET; 652 653 mcp->gp_idx = offsetof(struct mcp_set_gpio, gpio[offset].direction); 654 655 mcp->txbuf[mcp->gp_idx - 1] = 1; 656 mcp->txbuf[mcp->gp_idx] = val; 657 658 return mcp_send_data_req_status(mcp, mcp->txbuf, 18); 659 } 660 661 static int mcp_gpio_direction_input(struct gpio_chip *gc, 662 unsigned int offset) 663 { 664 int ret; 665 struct mcp2221 *mcp = gpiochip_get_data(gc); 666 667 mutex_lock(&mcp->lock); 668 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_IN); 669 mutex_unlock(&mcp->lock); 670 671 return ret; 672 } 673 674 static int mcp_gpio_direction_output(struct gpio_chip *gc, 675 unsigned int offset, int value) 676 { 677 int ret; 678 struct mcp2221 *mcp = gpiochip_get_data(gc); 679 680 mutex_lock(&mcp->lock); 681 ret = mcp_gpio_dir_set(mcp, offset, MCP2221_DIR_OUT); 682 mutex_unlock(&mcp->lock); 683 684 /* Can't configure as output, bailout early */ 685 if (ret) 686 return ret; 687 688 mcp_gpio_set(gc, offset, value); 689 690 return 0; 691 } 692 693 static int mcp_gpio_get_direction(struct gpio_chip *gc, 694 unsigned int offset) 695 { 696 int ret; 697 struct mcp2221 *mcp = gpiochip_get_data(gc); 698 699 mcp->txbuf[0] = MCP2221_GPIO_GET; 700 701 mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]); 702 703 mutex_lock(&mcp->lock); 704 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 705 mutex_unlock(&mcp->lock); 706 707 if (ret) 708 return ret; 709 710 if (mcp->gpio_dir == MCP2221_DIR_IN) 711 return GPIO_LINE_DIRECTION_IN; 712 713 return GPIO_LINE_DIRECTION_OUT; 714 } 715 #endif 716 717 /* Gives current state of i2c engine inside mcp2221 */ 718 static int mcp_get_i2c_eng_state(struct mcp2221 *mcp, 719 u8 *data, u8 idx) 720 { 721 int ret; 722 723 switch (data[idx]) { 724 case MCP2221_I2C_WRADDRL_NACK: 725 case MCP2221_I2C_WRADDRL_SEND: 726 ret = -ENXIO; 727 break; 728 case MCP2221_I2C_START_TOUT: 729 case MCP2221_I2C_STOP_TOUT: 730 case MCP2221_I2C_WRADDRL_TOUT: 731 case MCP2221_I2C_WRDATA_TOUT: 732 ret = -ETIMEDOUT; 733 break; 734 case MCP2221_I2C_ENG_BUSY: 735 ret = -EAGAIN; 736 break; 737 case MCP2221_SUCCESS: 738 ret = 0x00; 739 break; 740 default: 741 ret = -EIO; 742 } 743 744 return ret; 745 } 746 747 /* 748 * MCP2221 uses interrupt endpoint for input reports. This function 749 * is called by HID layer when it receives i/p report from mcp2221, 750 * which is actually a response to the previously sent command. 751 * 752 * MCP2221A firmware specific return codes are parsed and 0 or 753 * appropriate negative error code is returned. Delayed response 754 * results in timeout error and stray reponses results in -EIO. 755 */ 756 static int mcp2221_raw_event(struct hid_device *hdev, 757 struct hid_report *report, u8 *data, int size) 758 { 759 u8 *buf; 760 struct mcp2221 *mcp = hid_get_drvdata(hdev); 761 762 switch (data[0]) { 763 764 case MCP2221_I2C_WR_DATA: 765 case MCP2221_I2C_WR_NO_STOP: 766 case MCP2221_I2C_RD_DATA: 767 case MCP2221_I2C_RD_RPT_START: 768 switch (data[1]) { 769 case MCP2221_SUCCESS: 770 mcp->status = 0; 771 break; 772 default: 773 mcp->status = mcp_get_i2c_eng_state(mcp, data, 2); 774 } 775 complete(&mcp->wait_in_report); 776 break; 777 778 case MCP2221_I2C_PARAM_OR_STATUS: 779 switch (data[1]) { 780 case MCP2221_SUCCESS: 781 if ((mcp->txbuf[3] == MCP2221_I2C_SET_SPEED) && 782 (data[3] != MCP2221_I2C_SET_SPEED)) { 783 mcp->status = -EAGAIN; 784 break; 785 } 786 if (data[20] & MCP2221_I2C_MASK_ADDR_NACK) { 787 mcp->status = -ENXIO; 788 break; 789 } 790 mcp->status = mcp_get_i2c_eng_state(mcp, data, 8); 791 #if IS_REACHABLE(CONFIG_IIO) 792 memcpy(&mcp->adc_values, &data[50], sizeof(mcp->adc_values)); 793 #endif 794 break; 795 default: 796 mcp->status = -EIO; 797 } 798 complete(&mcp->wait_in_report); 799 break; 800 801 case MCP2221_I2C_GET_DATA: 802 switch (data[1]) { 803 case MCP2221_SUCCESS: 804 if (data[2] == MCP2221_I2C_ADDR_NACK) { 805 mcp->status = -ENXIO; 806 break; 807 } 808 if (!mcp_get_i2c_eng_state(mcp, data, 2) 809 && (data[3] == 0)) { 810 mcp->status = 0; 811 break; 812 } 813 if (data[3] == 127) { 814 mcp->status = -EIO; 815 break; 816 } 817 if (data[2] == MCP2221_I2C_READ_COMPL || 818 data[2] == MCP2221_I2C_READ_PARTIAL) { 819 buf = mcp->rxbuf; 820 memcpy(&buf[mcp->rxbuf_idx], &data[4], data[3]); 821 mcp->rxbuf_idx = mcp->rxbuf_idx + data[3]; 822 mcp->status = 0; 823 break; 824 } 825 mcp->status = -EIO; 826 break; 827 default: 828 mcp->status = -EIO; 829 } 830 complete(&mcp->wait_in_report); 831 break; 832 833 case MCP2221_GPIO_GET: 834 switch (data[1]) { 835 case MCP2221_SUCCESS: 836 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) || 837 (data[mcp->gp_idx + 1] == MCP2221_ALT_F_NOT_GPIOD)) { 838 mcp->status = -ENOENT; 839 } else { 840 mcp->status = !!data[mcp->gp_idx]; 841 mcp->gpio_dir = data[mcp->gp_idx + 1]; 842 } 843 break; 844 default: 845 mcp->status = -EAGAIN; 846 } 847 complete(&mcp->wait_in_report); 848 break; 849 850 case MCP2221_GPIO_SET: 851 switch (data[1]) { 852 case MCP2221_SUCCESS: 853 if ((data[mcp->gp_idx] == MCP2221_ALT_F_NOT_GPIOV) || 854 (data[mcp->gp_idx - 1] == MCP2221_ALT_F_NOT_GPIOV)) { 855 mcp->status = -ENOENT; 856 } else { 857 mcp->status = 0; 858 } 859 break; 860 default: 861 mcp->status = -EAGAIN; 862 } 863 complete(&mcp->wait_in_report); 864 break; 865 866 case MCP2221_SET_SRAM_SETTINGS: 867 switch (data[1]) { 868 case MCP2221_SUCCESS: 869 mcp->status = 0; 870 break; 871 default: 872 mcp->status = -EAGAIN; 873 } 874 complete(&mcp->wait_in_report); 875 break; 876 877 case MCP2221_GET_SRAM_SETTINGS: 878 switch (data[1]) { 879 case MCP2221_SUCCESS: 880 memcpy(&mcp->mode, &data[22], 4); 881 #if IS_REACHABLE(CONFIG_IIO) 882 mcp->dac_value = data[6] & GENMASK(4, 0); 883 #endif 884 mcp->status = 0; 885 break; 886 default: 887 mcp->status = -EAGAIN; 888 } 889 complete(&mcp->wait_in_report); 890 break; 891 892 case MCP2221_READ_FLASH_DATA: 893 switch (data[1]) { 894 case MCP2221_SUCCESS: 895 mcp->status = 0; 896 897 /* Only handles CHIP SETTINGS subpage currently */ 898 if (mcp->txbuf[1] != 0) { 899 mcp->status = -EIO; 900 break; 901 } 902 903 #if IS_REACHABLE(CONFIG_IIO) 904 { 905 u8 tmp; 906 /* DAC scale value */ 907 tmp = FIELD_GET(GENMASK(7, 6), data[6]); 908 if ((data[6] & BIT(5)) && tmp) 909 mcp->dac_scale = tmp + 4; 910 else 911 mcp->dac_scale = 5; 912 913 /* ADC scale value */ 914 tmp = FIELD_GET(GENMASK(4, 3), data[7]); 915 if ((data[7] & BIT(2)) && tmp) 916 mcp->adc_scale = tmp - 1; 917 else 918 mcp->adc_scale = 0; 919 } 920 #endif 921 922 break; 923 default: 924 mcp->status = -EAGAIN; 925 } 926 complete(&mcp->wait_in_report); 927 break; 928 929 default: 930 mcp->status = -EIO; 931 complete(&mcp->wait_in_report); 932 } 933 934 return 1; 935 } 936 937 /* Device resource managed function for HID unregistration */ 938 static void mcp2221_hid_unregister(void *ptr) 939 { 940 struct hid_device *hdev = ptr; 941 942 hid_hw_close(hdev); 943 hid_hw_stop(hdev); 944 } 945 946 /* This is needed to be sure hid_hw_stop() isn't called twice by the subsystem */ 947 static void mcp2221_remove(struct hid_device *hdev) 948 { 949 #if IS_REACHABLE(CONFIG_IIO) 950 struct mcp2221 *mcp = hid_get_drvdata(hdev); 951 952 cancel_delayed_work_sync(&mcp->init_work); 953 #endif 954 } 955 956 #if IS_REACHABLE(CONFIG_IIO) 957 static int mcp2221_read_raw(struct iio_dev *indio_dev, 958 struct iio_chan_spec const *channel, int *val, 959 int *val2, long mask) 960 { 961 struct mcp2221_iio *priv = iio_priv(indio_dev); 962 struct mcp2221 *mcp = priv->mcp; 963 int ret; 964 965 if (mask == IIO_CHAN_INFO_SCALE) { 966 if (channel->output) 967 *val = 1 << mcp->dac_scale; 968 else 969 *val = 1 << mcp->adc_scale; 970 971 return IIO_VAL_INT; 972 } 973 974 mutex_lock(&mcp->lock); 975 976 if (channel->output) { 977 *val = mcp->dac_value; 978 ret = IIO_VAL_INT; 979 } else { 980 /* Read ADC values */ 981 ret = mcp_chk_last_cmd_status(mcp); 982 983 if (!ret) { 984 *val = le16_to_cpu((__force __le16) mcp->adc_values[channel->address]); 985 if (*val >= BIT(10)) 986 ret = -EINVAL; 987 else 988 ret = IIO_VAL_INT; 989 } 990 } 991 992 mutex_unlock(&mcp->lock); 993 994 return ret; 995 } 996 997 static int mcp2221_write_raw(struct iio_dev *indio_dev, 998 struct iio_chan_spec const *chan, 999 int val, int val2, long mask) 1000 { 1001 struct mcp2221_iio *priv = iio_priv(indio_dev); 1002 struct mcp2221 *mcp = priv->mcp; 1003 int ret; 1004 1005 if (val < 0 || val >= BIT(5)) 1006 return -EINVAL; 1007 1008 mutex_lock(&mcp->lock); 1009 1010 memset(mcp->txbuf, 0, 12); 1011 mcp->txbuf[0] = MCP2221_SET_SRAM_SETTINGS; 1012 mcp->txbuf[4] = BIT(7) | val; 1013 1014 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 12); 1015 if (!ret) 1016 mcp->dac_value = val; 1017 1018 mutex_unlock(&mcp->lock); 1019 1020 return ret; 1021 } 1022 1023 static const struct iio_info mcp2221_info = { 1024 .read_raw = &mcp2221_read_raw, 1025 .write_raw = &mcp2221_write_raw, 1026 }; 1027 1028 static int mcp_iio_channels(struct mcp2221 *mcp) 1029 { 1030 int idx, cnt = 0; 1031 bool dac_created = false; 1032 1033 /* GP0 doesn't have ADC/DAC alternative function */ 1034 for (idx = 1; idx < MCP_NGPIO; idx++) { 1035 struct iio_chan_spec *chan = &mcp->iio_channels[cnt]; 1036 1037 switch (mcp->mode[idx]) { 1038 case 2: 1039 chan->address = idx - 1; 1040 chan->channel = cnt++; 1041 break; 1042 case 3: 1043 /* GP1 doesn't have DAC alternative function */ 1044 if (idx == 1 || dac_created) 1045 continue; 1046 /* DAC1 and DAC2 outputs are connected to the same DAC */ 1047 dac_created = true; 1048 chan->output = 1; 1049 cnt++; 1050 break; 1051 default: 1052 continue; 1053 } 1054 1055 chan->type = IIO_VOLTAGE; 1056 chan->indexed = 1; 1057 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 1058 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); 1059 chan->scan_index = -1; 1060 } 1061 1062 return cnt; 1063 } 1064 1065 static void mcp_init_work(struct work_struct *work) 1066 { 1067 struct iio_dev *indio_dev; 1068 struct mcp2221 *mcp = container_of(work, struct mcp2221, init_work.work); 1069 struct mcp2221_iio *data; 1070 static int retries = 5; 1071 int ret, num_channels; 1072 1073 hid_hw_power(mcp->hdev, PM_HINT_FULLON); 1074 mutex_lock(&mcp->lock); 1075 1076 mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS; 1077 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1); 1078 1079 if (ret == -EAGAIN) 1080 goto reschedule_task; 1081 1082 num_channels = mcp_iio_channels(mcp); 1083 if (!num_channels) 1084 goto unlock; 1085 1086 mcp->txbuf[0] = MCP2221_READ_FLASH_DATA; 1087 mcp->txbuf[1] = 0; 1088 ret = mcp_send_data_req_status(mcp, mcp->txbuf, 2); 1089 1090 if (ret == -EAGAIN) 1091 goto reschedule_task; 1092 1093 indio_dev = devm_iio_device_alloc(&mcp->hdev->dev, sizeof(*data)); 1094 if (!indio_dev) 1095 goto unlock; 1096 1097 data = iio_priv(indio_dev); 1098 data->mcp = mcp; 1099 1100 indio_dev->name = "mcp2221"; 1101 indio_dev->modes = INDIO_DIRECT_MODE; 1102 indio_dev->info = &mcp2221_info; 1103 indio_dev->channels = mcp->iio_channels; 1104 indio_dev->num_channels = num_channels; 1105 1106 devm_iio_device_register(&mcp->hdev->dev, indio_dev); 1107 1108 unlock: 1109 mutex_unlock(&mcp->lock); 1110 hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 1111 1112 return; 1113 1114 reschedule_task: 1115 mutex_unlock(&mcp->lock); 1116 hid_hw_power(mcp->hdev, PM_HINT_NORMAL); 1117 1118 if (!retries--) 1119 return; 1120 1121 /* Device is not ready to read SRAM or FLASH data, try again */ 1122 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100)); 1123 } 1124 #endif 1125 1126 static int mcp2221_probe(struct hid_device *hdev, 1127 const struct hid_device_id *id) 1128 { 1129 int ret; 1130 struct mcp2221 *mcp; 1131 1132 mcp = devm_kzalloc(&hdev->dev, sizeof(*mcp), GFP_KERNEL); 1133 if (!mcp) 1134 return -ENOMEM; 1135 1136 ret = hid_parse(hdev); 1137 if (ret) { 1138 hid_err(hdev, "can't parse reports\n"); 1139 return ret; 1140 } 1141 1142 /* 1143 * This driver uses the .raw_event callback and therefore does not need any 1144 * HID_CONNECT_xxx flags. 1145 */ 1146 ret = hid_hw_start(hdev, 0); 1147 if (ret) { 1148 hid_err(hdev, "can't start hardware\n"); 1149 return ret; 1150 } 1151 1152 hid_info(hdev, "USB HID v%x.%02x Device [%s] on %s\n", hdev->version >> 8, 1153 hdev->version & 0xff, hdev->name, hdev->phys); 1154 1155 ret = hid_hw_open(hdev); 1156 if (ret) { 1157 hid_err(hdev, "can't open device\n"); 1158 hid_hw_stop(hdev); 1159 return ret; 1160 } 1161 1162 mutex_init(&mcp->lock); 1163 init_completion(&mcp->wait_in_report); 1164 hid_set_drvdata(hdev, mcp); 1165 mcp->hdev = hdev; 1166 1167 ret = devm_add_action_or_reset(&hdev->dev, mcp2221_hid_unregister, hdev); 1168 if (ret) 1169 return ret; 1170 1171 hid_device_io_start(hdev); 1172 1173 /* Set I2C bus clock diviser */ 1174 if (i2c_clk_freq > 400) 1175 i2c_clk_freq = 400; 1176 if (i2c_clk_freq < 50) 1177 i2c_clk_freq = 50; 1178 mcp->cur_i2c_clk_div = (12000000 / (i2c_clk_freq * 1000)) - 3; 1179 ret = mcp_set_i2c_speed(mcp); 1180 if (ret) { 1181 hid_err(hdev, "can't set i2c speed: %d\n", ret); 1182 return ret; 1183 } 1184 1185 mcp->adapter.owner = THIS_MODULE; 1186 mcp->adapter.class = I2C_CLASS_HWMON; 1187 mcp->adapter.algo = &mcp_i2c_algo; 1188 mcp->adapter.retries = 1; 1189 mcp->adapter.dev.parent = &hdev->dev; 1190 ACPI_COMPANION_SET(&mcp->adapter.dev, ACPI_COMPANION(hdev->dev.parent)); 1191 snprintf(mcp->adapter.name, sizeof(mcp->adapter.name), 1192 "MCP2221 usb-i2c bridge"); 1193 1194 i2c_set_adapdata(&mcp->adapter, mcp); 1195 ret = devm_i2c_add_adapter(&hdev->dev, &mcp->adapter); 1196 if (ret) { 1197 hid_err(hdev, "can't add usb-i2c adapter: %d\n", ret); 1198 return ret; 1199 } 1200 1201 #if IS_REACHABLE(CONFIG_GPIOLIB) 1202 /* Setup GPIO chip */ 1203 mcp->gc = devm_kzalloc(&hdev->dev, sizeof(*mcp->gc), GFP_KERNEL); 1204 if (!mcp->gc) 1205 return -ENOMEM; 1206 1207 mcp->gc->label = "mcp2221_gpio"; 1208 mcp->gc->direction_input = mcp_gpio_direction_input; 1209 mcp->gc->direction_output = mcp_gpio_direction_output; 1210 mcp->gc->get_direction = mcp_gpio_get_direction; 1211 mcp->gc->set_rv = mcp_gpio_set; 1212 mcp->gc->get = mcp_gpio_get; 1213 mcp->gc->ngpio = MCP_NGPIO; 1214 mcp->gc->base = -1; 1215 mcp->gc->can_sleep = 1; 1216 mcp->gc->parent = &hdev->dev; 1217 1218 ret = devm_gpiochip_add_data(&hdev->dev, mcp->gc, mcp); 1219 if (ret) 1220 return ret; 1221 #endif 1222 1223 #if IS_REACHABLE(CONFIG_IIO) 1224 INIT_DELAYED_WORK(&mcp->init_work, mcp_init_work); 1225 schedule_delayed_work(&mcp->init_work, msecs_to_jiffies(100)); 1226 #endif 1227 1228 return 0; 1229 } 1230 1231 static const struct hid_device_id mcp2221_devices[] = { 1232 { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_MCP2221) }, 1233 { } 1234 }; 1235 MODULE_DEVICE_TABLE(hid, mcp2221_devices); 1236 1237 static struct hid_driver mcp2221_driver = { 1238 .name = "mcp2221", 1239 .id_table = mcp2221_devices, 1240 .probe = mcp2221_probe, 1241 .remove = mcp2221_remove, 1242 .raw_event = mcp2221_raw_event, 1243 }; 1244 1245 /* Register with HID core */ 1246 module_hid_driver(mcp2221_driver); 1247 1248 MODULE_AUTHOR("Rishi Gupta <gupt21@gmail.com>"); 1249 MODULE_DESCRIPTION("MCP2221 Microchip HID USB to I2C master bridge"); 1250 MODULE_LICENSE("GPL v2"); 1251