1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * ILITEK Touch IC driver for 23XX, 25XX and Lego series 4 * 5 * Copyright (C) 2011 ILI Technology Corporation. 6 * Copyright (C) 2020 Luca Hsu <luca_hsu@ilitek.com> 7 * Copyright (C) 2021 Joe Hung <joe_hung@ilitek.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/input.h> 13 #include <linux/input/mt.h> 14 #include <linux/i2c.h> 15 #include <linux/slab.h> 16 #include <linux/delay.h> 17 #include <linux/interrupt.h> 18 #include <linux/gpio.h> 19 #include <linux/gpio/consumer.h> 20 #include <linux/errno.h> 21 #include <linux/acpi.h> 22 #include <linux/input/touchscreen.h> 23 #include <asm/unaligned.h> 24 25 26 #define ILITEK_TS_NAME "ilitek_ts" 27 #define BL_V1_8 0x108 28 #define BL_V1_7 0x107 29 #define BL_V1_6 0x106 30 31 #define ILITEK_TP_CMD_GET_TP_RES 0x20 32 #define ILITEK_TP_CMD_GET_SCRN_RES 0x21 33 #define ILITEK_TP_CMD_SET_IC_SLEEP 0x30 34 #define ILITEK_TP_CMD_SET_IC_WAKE 0x31 35 #define ILITEK_TP_CMD_GET_FW_VER 0x40 36 #define ILITEK_TP_CMD_GET_PRL_VER 0x42 37 #define ILITEK_TP_CMD_GET_MCU_VER 0x61 38 #define ILITEK_TP_CMD_GET_IC_MODE 0xC0 39 40 #define ILITEK_TP_I2C_REPORT_ID 0x48 41 42 #define REPORT_COUNT_ADDRESS 61 43 #define ILITEK_SUPPORT_MAX_POINT 40 44 45 struct ilitek_protocol_info { 46 u16 ver; 47 u8 ver_major; 48 }; 49 50 struct ilitek_ts_data { 51 struct i2c_client *client; 52 struct gpio_desc *reset_gpio; 53 struct input_dev *input_dev; 54 struct touchscreen_properties prop; 55 56 const struct ilitek_protocol_map *ptl_cb_func; 57 struct ilitek_protocol_info ptl; 58 59 char product_id[30]; 60 u16 mcu_ver; 61 u8 ic_mode; 62 u8 firmware_ver[8]; 63 64 s32 reset_time; 65 s32 screen_max_x; 66 s32 screen_max_y; 67 s32 screen_min_x; 68 s32 screen_min_y; 69 s32 max_tp; 70 }; 71 72 struct ilitek_protocol_map { 73 u16 cmd; 74 const char *name; 75 int (*func)(struct ilitek_ts_data *ts, u16 cmd, u8 *inbuf, u8 *outbuf); 76 }; 77 78 enum ilitek_cmds { 79 /* common cmds */ 80 GET_PTL_VER = 0, 81 GET_FW_VER, 82 GET_SCRN_RES, 83 GET_TP_RES, 84 GET_IC_MODE, 85 GET_MCU_VER, 86 SET_IC_SLEEP, 87 SET_IC_WAKE, 88 89 /* ALWAYS keep at the end */ 90 MAX_CMD_CNT 91 }; 92 93 /* ILITEK I2C R/W APIs */ 94 static int ilitek_i2c_write_and_read(struct ilitek_ts_data *ts, 95 u8 *cmd, int write_len, int delay, 96 u8 *data, int read_len) 97 { 98 int error; 99 struct i2c_client *client = ts->client; 100 struct i2c_msg msgs[] = { 101 { 102 .addr = client->addr, 103 .flags = 0, 104 .len = write_len, 105 .buf = cmd, 106 }, 107 { 108 .addr = client->addr, 109 .flags = I2C_M_RD, 110 .len = read_len, 111 .buf = data, 112 }, 113 }; 114 115 if (delay == 0 && write_len > 0 && read_len > 0) { 116 error = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 117 if (error < 0) 118 return error; 119 } else { 120 if (write_len > 0) { 121 error = i2c_transfer(client->adapter, msgs, 1); 122 if (error < 0) 123 return error; 124 } 125 if (delay > 0) 126 mdelay(delay); 127 128 if (read_len > 0) { 129 error = i2c_transfer(client->adapter, msgs + 1, 1); 130 if (error < 0) 131 return error; 132 } 133 } 134 135 return 0; 136 } 137 138 /* ILITEK ISR APIs */ 139 static void ilitek_touch_down(struct ilitek_ts_data *ts, unsigned int id, 140 unsigned int x, unsigned int y) 141 { 142 struct input_dev *input = ts->input_dev; 143 144 input_mt_slot(input, id); 145 input_mt_report_slot_state(input, MT_TOOL_FINGER, true); 146 147 touchscreen_report_pos(input, &ts->prop, x, y, true); 148 } 149 150 static int ilitek_process_and_report_v6(struct ilitek_ts_data *ts) 151 { 152 int error = 0; 153 u8 buf[512]; 154 int packet_len = 5; 155 int packet_max_point = 10; 156 int report_max_point; 157 int i, count; 158 struct input_dev *input = ts->input_dev; 159 struct device *dev = &ts->client->dev; 160 unsigned int x, y, status, id; 161 162 error = ilitek_i2c_write_and_read(ts, NULL, 0, 0, buf, 64); 163 if (error) { 164 dev_err(dev, "get touch info failed, err:%d\n", error); 165 return error; 166 } 167 168 if (buf[0] != ILITEK_TP_I2C_REPORT_ID) { 169 dev_err(dev, "get touch info failed. Wrong id: 0x%02X\n", buf[0]); 170 return -EINVAL; 171 } 172 173 report_max_point = buf[REPORT_COUNT_ADDRESS]; 174 if (report_max_point > ts->max_tp) { 175 dev_err(dev, "FW report max point:%d > panel info. max:%d\n", 176 report_max_point, ts->max_tp); 177 return -EINVAL; 178 } 179 180 count = DIV_ROUND_UP(report_max_point, packet_max_point); 181 for (i = 1; i < count; i++) { 182 error = ilitek_i2c_write_and_read(ts, NULL, 0, 0, 183 buf + i * 64, 64); 184 if (error) { 185 dev_err(dev, "get touch info. failed, cnt:%d, err:%d\n", 186 count, error); 187 return error; 188 } 189 } 190 191 for (i = 0; i < report_max_point; i++) { 192 status = buf[i * packet_len + 1] & 0x40; 193 if (!status) 194 continue; 195 196 id = buf[i * packet_len + 1] & 0x3F; 197 198 x = get_unaligned_le16(buf + i * packet_len + 2); 199 y = get_unaligned_le16(buf + i * packet_len + 4); 200 201 if (x > ts->screen_max_x || x < ts->screen_min_x || 202 y > ts->screen_max_y || y < ts->screen_min_y) { 203 dev_warn(dev, "invalid position, X[%d,%u,%d], Y[%d,%u,%d]\n", 204 ts->screen_min_x, x, ts->screen_max_x, 205 ts->screen_min_y, y, ts->screen_max_y); 206 continue; 207 } 208 209 ilitek_touch_down(ts, id, x, y); 210 } 211 212 input_mt_sync_frame(input); 213 input_sync(input); 214 215 return 0; 216 } 217 218 /* APIs of cmds for ILITEK Touch IC */ 219 static int api_protocol_set_cmd(struct ilitek_ts_data *ts, 220 u16 idx, u8 *inbuf, u8 *outbuf) 221 { 222 u16 cmd; 223 int error; 224 225 if (idx >= MAX_CMD_CNT) 226 return -EINVAL; 227 228 cmd = ts->ptl_cb_func[idx].cmd; 229 error = ts->ptl_cb_func[idx].func(ts, cmd, inbuf, outbuf); 230 if (error) 231 return error; 232 233 return 0; 234 } 235 236 static int api_protocol_get_ptl_ver(struct ilitek_ts_data *ts, 237 u16 cmd, u8 *inbuf, u8 *outbuf) 238 { 239 int error; 240 u8 buf[64]; 241 242 buf[0] = cmd; 243 error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 3); 244 if (error) 245 return error; 246 247 ts->ptl.ver = get_unaligned_be16(outbuf); 248 ts->ptl.ver_major = outbuf[0]; 249 250 return 0; 251 } 252 253 static int api_protocol_get_mcu_ver(struct ilitek_ts_data *ts, 254 u16 cmd, u8 *inbuf, u8 *outbuf) 255 { 256 int error; 257 u8 buf[64]; 258 259 buf[0] = cmd; 260 error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 32); 261 if (error) 262 return error; 263 264 ts->mcu_ver = get_unaligned_le16(outbuf); 265 memset(ts->product_id, 0, sizeof(ts->product_id)); 266 memcpy(ts->product_id, outbuf + 6, 26); 267 268 return 0; 269 } 270 271 static int api_protocol_get_fw_ver(struct ilitek_ts_data *ts, 272 u16 cmd, u8 *inbuf, u8 *outbuf) 273 { 274 int error; 275 u8 buf[64]; 276 277 buf[0] = cmd; 278 error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 8); 279 if (error) 280 return error; 281 282 memcpy(ts->firmware_ver, outbuf, 8); 283 284 return 0; 285 } 286 287 static int api_protocol_get_scrn_res(struct ilitek_ts_data *ts, 288 u16 cmd, u8 *inbuf, u8 *outbuf) 289 { 290 int error; 291 u8 buf[64]; 292 293 buf[0] = cmd; 294 error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 8); 295 if (error) 296 return error; 297 298 ts->screen_min_x = get_unaligned_le16(outbuf); 299 ts->screen_min_y = get_unaligned_le16(outbuf + 2); 300 ts->screen_max_x = get_unaligned_le16(outbuf + 4); 301 ts->screen_max_y = get_unaligned_le16(outbuf + 6); 302 303 return 0; 304 } 305 306 static int api_protocol_get_tp_res(struct ilitek_ts_data *ts, 307 u16 cmd, u8 *inbuf, u8 *outbuf) 308 { 309 int error; 310 u8 buf[64]; 311 312 buf[0] = cmd; 313 error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 15); 314 if (error) 315 return error; 316 317 ts->max_tp = outbuf[8]; 318 if (ts->max_tp > ILITEK_SUPPORT_MAX_POINT) { 319 dev_err(&ts->client->dev, "Invalid MAX_TP:%d from FW\n", 320 ts->max_tp); 321 return -EINVAL; 322 } 323 324 return 0; 325 } 326 327 static int api_protocol_get_ic_mode(struct ilitek_ts_data *ts, 328 u16 cmd, u8 *inbuf, u8 *outbuf) 329 { 330 int error; 331 u8 buf[64]; 332 333 buf[0] = cmd; 334 error = ilitek_i2c_write_and_read(ts, buf, 1, 5, outbuf, 2); 335 if (error) 336 return error; 337 338 ts->ic_mode = outbuf[0]; 339 return 0; 340 } 341 342 static int api_protocol_set_ic_sleep(struct ilitek_ts_data *ts, 343 u16 cmd, u8 *inbuf, u8 *outbuf) 344 { 345 u8 buf[64]; 346 347 buf[0] = cmd; 348 return ilitek_i2c_write_and_read(ts, buf, 1, 0, NULL, 0); 349 } 350 351 static int api_protocol_set_ic_wake(struct ilitek_ts_data *ts, 352 u16 cmd, u8 *inbuf, u8 *outbuf) 353 { 354 u8 buf[64]; 355 356 buf[0] = cmd; 357 return ilitek_i2c_write_and_read(ts, buf, 1, 0, NULL, 0); 358 } 359 360 static const struct ilitek_protocol_map ptl_func_map[] = { 361 /* common cmds */ 362 [GET_PTL_VER] = { 363 ILITEK_TP_CMD_GET_PRL_VER, "GET_PTL_VER", 364 api_protocol_get_ptl_ver 365 }, 366 [GET_FW_VER] = { 367 ILITEK_TP_CMD_GET_FW_VER, "GET_FW_VER", 368 api_protocol_get_fw_ver 369 }, 370 [GET_SCRN_RES] = { 371 ILITEK_TP_CMD_GET_SCRN_RES, "GET_SCRN_RES", 372 api_protocol_get_scrn_res 373 }, 374 [GET_TP_RES] = { 375 ILITEK_TP_CMD_GET_TP_RES, "GET_TP_RES", 376 api_protocol_get_tp_res 377 }, 378 [GET_IC_MODE] = { 379 ILITEK_TP_CMD_GET_IC_MODE, "GET_IC_MODE", 380 api_protocol_get_ic_mode 381 }, 382 [GET_MCU_VER] = { 383 ILITEK_TP_CMD_GET_MCU_VER, "GET_MOD_VER", 384 api_protocol_get_mcu_ver 385 }, 386 [SET_IC_SLEEP] = { 387 ILITEK_TP_CMD_SET_IC_SLEEP, "SET_IC_SLEEP", 388 api_protocol_set_ic_sleep 389 }, 390 [SET_IC_WAKE] = { 391 ILITEK_TP_CMD_SET_IC_WAKE, "SET_IC_WAKE", 392 api_protocol_set_ic_wake 393 }, 394 }; 395 396 /* Probe APIs */ 397 static void ilitek_reset(struct ilitek_ts_data *ts, int delay) 398 { 399 if (ts->reset_gpio) { 400 gpiod_set_value(ts->reset_gpio, 1); 401 mdelay(10); 402 gpiod_set_value(ts->reset_gpio, 0); 403 mdelay(delay); 404 } 405 } 406 407 static int ilitek_protocol_init(struct ilitek_ts_data *ts) 408 { 409 int error; 410 u8 outbuf[64]; 411 412 ts->ptl_cb_func = ptl_func_map; 413 ts->reset_time = 600; 414 415 error = api_protocol_set_cmd(ts, GET_PTL_VER, NULL, outbuf); 416 if (error) 417 return error; 418 419 /* Protocol v3 is not support currently */ 420 if (ts->ptl.ver_major == 0x3 || 421 ts->ptl.ver == BL_V1_6 || 422 ts->ptl.ver == BL_V1_7) 423 return -EINVAL; 424 425 return 0; 426 } 427 428 static int ilitek_read_tp_info(struct ilitek_ts_data *ts, bool boot) 429 { 430 u8 outbuf[256]; 431 int error; 432 433 error = api_protocol_set_cmd(ts, GET_PTL_VER, NULL, outbuf); 434 if (error) 435 return error; 436 437 error = api_protocol_set_cmd(ts, GET_MCU_VER, NULL, outbuf); 438 if (error) 439 return error; 440 441 error = api_protocol_set_cmd(ts, GET_FW_VER, NULL, outbuf); 442 if (error) 443 return error; 444 445 if (boot) { 446 error = api_protocol_set_cmd(ts, GET_SCRN_RES, NULL, 447 outbuf); 448 if (error) 449 return error; 450 } 451 452 error = api_protocol_set_cmd(ts, GET_TP_RES, NULL, outbuf); 453 if (error) 454 return error; 455 456 error = api_protocol_set_cmd(ts, GET_IC_MODE, NULL, outbuf); 457 if (error) 458 return error; 459 460 return 0; 461 } 462 463 static int ilitek_input_dev_init(struct device *dev, struct ilitek_ts_data *ts) 464 { 465 int error; 466 struct input_dev *input; 467 468 input = devm_input_allocate_device(dev); 469 if (!input) 470 return -ENOMEM; 471 472 ts->input_dev = input; 473 input->name = ILITEK_TS_NAME; 474 input->id.bustype = BUS_I2C; 475 476 __set_bit(INPUT_PROP_DIRECT, input->propbit); 477 478 input_set_abs_params(input, ABS_MT_POSITION_X, 479 ts->screen_min_x, ts->screen_max_x, 0, 0); 480 input_set_abs_params(input, ABS_MT_POSITION_Y, 481 ts->screen_min_y, ts->screen_max_y, 0, 0); 482 483 touchscreen_parse_properties(input, true, &ts->prop); 484 485 error = input_mt_init_slots(input, ts->max_tp, 486 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 487 if (error) { 488 dev_err(dev, "initialize MT slots failed, err:%d\n", error); 489 return error; 490 } 491 492 error = input_register_device(input); 493 if (error) { 494 dev_err(dev, "register input device failed, err:%d\n", error); 495 return error; 496 } 497 498 return 0; 499 } 500 501 static irqreturn_t ilitek_i2c_isr(int irq, void *dev_id) 502 { 503 struct ilitek_ts_data *ts = dev_id; 504 int error; 505 506 error = ilitek_process_and_report_v6(ts); 507 if (error < 0) { 508 dev_err(&ts->client->dev, "[%s] err:%d\n", __func__, error); 509 return IRQ_NONE; 510 } 511 512 return IRQ_HANDLED; 513 } 514 515 static ssize_t firmware_version_show(struct device *dev, 516 struct device_attribute *attr, char *buf) 517 { 518 struct i2c_client *client = to_i2c_client(dev); 519 struct ilitek_ts_data *ts = i2c_get_clientdata(client); 520 521 return scnprintf(buf, PAGE_SIZE, 522 "fw version: [%02X%02X.%02X%02X.%02X%02X.%02X%02X]\n", 523 ts->firmware_ver[0], ts->firmware_ver[1], 524 ts->firmware_ver[2], ts->firmware_ver[3], 525 ts->firmware_ver[4], ts->firmware_ver[5], 526 ts->firmware_ver[6], ts->firmware_ver[7]); 527 } 528 static DEVICE_ATTR_RO(firmware_version); 529 530 static ssize_t product_id_show(struct device *dev, 531 struct device_attribute *attr, char *buf) 532 { 533 struct i2c_client *client = to_i2c_client(dev); 534 struct ilitek_ts_data *ts = i2c_get_clientdata(client); 535 536 return scnprintf(buf, PAGE_SIZE, "product id: [%04X], module: [%s]\n", 537 ts->mcu_ver, ts->product_id); 538 } 539 static DEVICE_ATTR_RO(product_id); 540 541 static struct attribute *ilitek_sysfs_attrs[] = { 542 &dev_attr_firmware_version.attr, 543 &dev_attr_product_id.attr, 544 NULL 545 }; 546 547 static struct attribute_group ilitek_attrs_group = { 548 .attrs = ilitek_sysfs_attrs, 549 }; 550 551 static int ilitek_ts_i2c_probe(struct i2c_client *client) 552 { 553 struct ilitek_ts_data *ts; 554 struct device *dev = &client->dev; 555 int error; 556 557 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 558 dev_err(dev, "i2c check functionality failed\n"); 559 return -ENXIO; 560 } 561 562 ts = devm_kzalloc(dev, sizeof(*ts), GFP_KERNEL); 563 if (!ts) 564 return -ENOMEM; 565 566 ts->client = client; 567 i2c_set_clientdata(client, ts); 568 569 ts->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 570 if (IS_ERR(ts->reset_gpio)) { 571 error = PTR_ERR(ts->reset_gpio); 572 dev_err(dev, "request gpiod failed: %d", error); 573 return error; 574 } 575 576 ilitek_reset(ts, 1000); 577 578 error = ilitek_protocol_init(ts); 579 if (error) { 580 dev_err(dev, "protocol init failed: %d", error); 581 return error; 582 } 583 584 error = ilitek_read_tp_info(ts, true); 585 if (error) { 586 dev_err(dev, "read tp info failed: %d", error); 587 return error; 588 } 589 590 error = ilitek_input_dev_init(dev, ts); 591 if (error) { 592 dev_err(dev, "input dev init failed: %d", error); 593 return error; 594 } 595 596 error = devm_request_threaded_irq(dev, ts->client->irq, 597 NULL, ilitek_i2c_isr, IRQF_ONESHOT, 598 "ilitek_touch_irq", ts); 599 if (error) { 600 dev_err(dev, "request threaded irq failed: %d\n", error); 601 return error; 602 } 603 604 error = devm_device_add_group(dev, &ilitek_attrs_group); 605 if (error) { 606 dev_err(dev, "sysfs create group failed: %d\n", error); 607 return error; 608 } 609 610 return 0; 611 } 612 613 static int ilitek_suspend(struct device *dev) 614 { 615 struct i2c_client *client = to_i2c_client(dev); 616 struct ilitek_ts_data *ts = i2c_get_clientdata(client); 617 int error; 618 619 disable_irq(client->irq); 620 621 if (!device_may_wakeup(dev)) { 622 error = api_protocol_set_cmd(ts, SET_IC_SLEEP, NULL, NULL); 623 if (error) 624 return error; 625 } 626 627 return 0; 628 } 629 630 static int ilitek_resume(struct device *dev) 631 { 632 struct i2c_client *client = to_i2c_client(dev); 633 struct ilitek_ts_data *ts = i2c_get_clientdata(client); 634 int error; 635 636 if (!device_may_wakeup(dev)) { 637 error = api_protocol_set_cmd(ts, SET_IC_WAKE, NULL, NULL); 638 if (error) 639 return error; 640 641 ilitek_reset(ts, ts->reset_time); 642 } 643 644 enable_irq(client->irq); 645 646 return 0; 647 } 648 649 static DEFINE_SIMPLE_DEV_PM_OPS(ilitek_pm_ops, ilitek_suspend, ilitek_resume); 650 651 static const struct i2c_device_id ilitek_ts_i2c_id[] = { 652 { ILITEK_TS_NAME, 0 }, 653 { }, 654 }; 655 MODULE_DEVICE_TABLE(i2c, ilitek_ts_i2c_id); 656 657 #ifdef CONFIG_ACPI 658 static const struct acpi_device_id ilitekts_acpi_id[] = { 659 { "ILTK0001", 0 }, 660 { }, 661 }; 662 MODULE_DEVICE_TABLE(acpi, ilitekts_acpi_id); 663 #endif 664 665 #ifdef CONFIG_OF 666 static const struct of_device_id ilitek_ts_i2c_match[] = { 667 {.compatible = "ilitek,ili2130",}, 668 {.compatible = "ilitek,ili2131",}, 669 {.compatible = "ilitek,ili2132",}, 670 {.compatible = "ilitek,ili2316",}, 671 {.compatible = "ilitek,ili2322",}, 672 {.compatible = "ilitek,ili2323",}, 673 {.compatible = "ilitek,ili2326",}, 674 {.compatible = "ilitek,ili2520",}, 675 {.compatible = "ilitek,ili2521",}, 676 { }, 677 }; 678 MODULE_DEVICE_TABLE(of, ilitek_ts_i2c_match); 679 #endif 680 681 static struct i2c_driver ilitek_ts_i2c_driver = { 682 .driver = { 683 .name = ILITEK_TS_NAME, 684 .pm = pm_sleep_ptr(&ilitek_pm_ops), 685 .of_match_table = of_match_ptr(ilitek_ts_i2c_match), 686 .acpi_match_table = ACPI_PTR(ilitekts_acpi_id), 687 }, 688 .probe = ilitek_ts_i2c_probe, 689 .id_table = ilitek_ts_i2c_id, 690 }; 691 module_i2c_driver(ilitek_ts_i2c_driver); 692 693 MODULE_AUTHOR("ILITEK"); 694 MODULE_DESCRIPTION("ILITEK I2C Touchscreen Driver"); 695 MODULE_LICENSE("GPL"); 696