1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/crc-ccitt.h> 3 #include <linux/delay.h> 4 #include <linux/gpio/consumer.h> 5 #include <linux/i2c.h> 6 #include <linux/ihex.h> 7 #include <linux/input.h> 8 #include <linux/input/mt.h> 9 #include <linux/input/touchscreen.h> 10 #include <linux/interrupt.h> 11 #include <linux/mod_devicetable.h> 12 #include <linux/module.h> 13 #include <linux/sizes.h> 14 #include <linux/slab.h> 15 #include <linux/unaligned.h> 16 17 #define ILI2XXX_POLL_PERIOD 15 18 19 #define ILI210X_DATA_SIZE 64 20 #define ILI211X_DATA_SIZE 43 21 #define ILI251X_DATA_SIZE1 31 22 #define ILI251X_DATA_SIZE2 20 23 24 /* Touchscreen commands */ 25 #define REG_TOUCHDATA 0x10 26 #define REG_PANEL_INFO 0x20 27 #define REG_FIRMWARE_VERSION 0x40 28 #define REG_PROTOCOL_VERSION 0x42 29 #define REG_KERNEL_VERSION 0x61 30 #define REG_IC_BUSY 0x80 31 #define REG_IC_BUSY_NOT_BUSY 0x50 32 #define REG_GET_MODE 0xc0 33 #define REG_GET_MODE_AP 0x5a 34 #define REG_GET_MODE_BL 0x55 35 #define REG_SET_MODE_AP 0xc1 36 #define REG_SET_MODE_BL 0xc2 37 #define REG_WRITE_DATA 0xc3 38 #define REG_WRITE_ENABLE 0xc4 39 #define REG_READ_DATA_CRC 0xc7 40 #define REG_CALIBRATE 0xcc 41 42 #define ILI251X_FW_FILENAME "ilitek/ili251x.bin" 43 44 struct ili2xxx_chip { 45 int (*read_reg)(struct i2c_client *client, u8 reg, 46 void *buf, size_t len); 47 int (*get_touch_data)(struct i2c_client *client, u8 *data); 48 bool (*parse_touch_data)(const u8 *data, unsigned int finger, 49 unsigned int *x, unsigned int *y, 50 unsigned int *z); 51 bool (*continue_polling)(const u8 *data, bool touch); 52 unsigned int max_touches; 53 unsigned int resolution; 54 bool has_calibrate_reg; 55 bool has_firmware_proto; 56 bool has_pressure_reg; 57 }; 58 59 struct ili210x { 60 struct i2c_client *client; 61 struct input_dev *input; 62 struct gpio_desc *reset_gpio; 63 struct touchscreen_properties prop; 64 const struct ili2xxx_chip *chip; 65 u8 version_firmware[8]; 66 u8 version_kernel[5]; 67 u8 version_proto[2]; 68 u8 ic_mode[2]; 69 bool stop; 70 }; 71 72 static int ili210x_read_reg(struct i2c_client *client, 73 u8 reg, void *buf, size_t len) 74 { 75 struct i2c_msg msg[] = { 76 { 77 .addr = client->addr, 78 .flags = 0, 79 .len = 1, 80 .buf = ®, 81 }, 82 { 83 .addr = client->addr, 84 .flags = I2C_M_RD, 85 .len = len, 86 .buf = buf, 87 } 88 }; 89 int error, ret; 90 91 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 92 if (ret != ARRAY_SIZE(msg)) { 93 error = ret < 0 ? ret : -EIO; 94 dev_err(&client->dev, "%s failed: %d\n", __func__, error); 95 return error; 96 } 97 98 return 0; 99 } 100 101 static int ili210x_read_touch_data(struct i2c_client *client, u8 *data) 102 { 103 return ili210x_read_reg(client, REG_TOUCHDATA, 104 data, ILI210X_DATA_SIZE); 105 } 106 107 static bool ili210x_touchdata_to_coords(const u8 *touchdata, 108 unsigned int finger, 109 unsigned int *x, unsigned int *y, 110 unsigned int *z) 111 { 112 if (!(touchdata[0] & BIT(finger))) 113 return false; 114 115 *x = get_unaligned_be16(touchdata + 1 + (finger * 4) + 0); 116 *y = get_unaligned_be16(touchdata + 1 + (finger * 4) + 2); 117 118 return true; 119 } 120 121 static bool ili210x_check_continue_polling(const u8 *data, bool touch) 122 { 123 return data[0] & 0xf3; 124 } 125 126 static const struct ili2xxx_chip ili210x_chip = { 127 .read_reg = ili210x_read_reg, 128 .get_touch_data = ili210x_read_touch_data, 129 .parse_touch_data = ili210x_touchdata_to_coords, 130 .continue_polling = ili210x_check_continue_polling, 131 .max_touches = 2, 132 .has_calibrate_reg = true, 133 }; 134 135 static int ili211x_read_touch_data(struct i2c_client *client, u8 *data) 136 { 137 s16 sum = 0; 138 int error; 139 int ret; 140 int i; 141 142 ret = i2c_master_recv(client, data, ILI211X_DATA_SIZE); 143 if (ret != ILI211X_DATA_SIZE) { 144 error = ret < 0 ? ret : -EIO; 145 dev_err(&client->dev, "%s failed: %d\n", __func__, error); 146 return error; 147 } 148 149 /* This chip uses custom checksum at the end of data */ 150 for (i = 0; i < ILI211X_DATA_SIZE - 1; i++) 151 sum = (sum + data[i]) & 0xff; 152 153 if ((-sum & 0xff) != data[ILI211X_DATA_SIZE - 1]) { 154 dev_err(&client->dev, 155 "CRC error (crc=0x%02x expected=0x%02x)\n", 156 sum, data[ILI211X_DATA_SIZE - 1]); 157 return -EIO; 158 } 159 160 return 0; 161 } 162 163 static bool ili211x_touchdata_to_coords(const u8 *touchdata, 164 unsigned int finger, 165 unsigned int *x, unsigned int *y, 166 unsigned int *z) 167 { 168 u32 data; 169 170 data = get_unaligned_be32(touchdata + 1 + (finger * 4) + 0); 171 if (data == 0xffffffff) /* Finger up */ 172 return false; 173 174 *x = ((touchdata[1 + (finger * 4) + 0] & 0xf0) << 4) | 175 touchdata[1 + (finger * 4) + 1]; 176 *y = ((touchdata[1 + (finger * 4) + 0] & 0x0f) << 8) | 177 touchdata[1 + (finger * 4) + 2]; 178 179 return true; 180 } 181 182 static bool ili211x_decline_polling(const u8 *data, bool touch) 183 { 184 return false; 185 } 186 187 static const struct ili2xxx_chip ili211x_chip = { 188 .read_reg = ili210x_read_reg, 189 .get_touch_data = ili211x_read_touch_data, 190 .parse_touch_data = ili211x_touchdata_to_coords, 191 .continue_polling = ili211x_decline_polling, 192 .max_touches = 10, 193 .resolution = 2048, 194 }; 195 196 static bool ili212x_touchdata_to_coords(const u8 *touchdata, 197 unsigned int finger, 198 unsigned int *x, unsigned int *y, 199 unsigned int *z) 200 { 201 u16 val; 202 203 val = get_unaligned_be16(touchdata + 3 + (finger * 5) + 0); 204 if (!(val & BIT(15))) /* Touch indication */ 205 return false; 206 207 *x = val & 0x3fff; 208 *y = get_unaligned_be16(touchdata + 3 + (finger * 5) + 2); 209 210 return true; 211 } 212 213 static bool ili212x_check_continue_polling(const u8 *data, bool touch) 214 { 215 return touch; 216 } 217 218 static const struct ili2xxx_chip ili212x_chip = { 219 .read_reg = ili210x_read_reg, 220 .get_touch_data = ili210x_read_touch_data, 221 .parse_touch_data = ili212x_touchdata_to_coords, 222 .continue_polling = ili212x_check_continue_polling, 223 .max_touches = 10, 224 .has_calibrate_reg = true, 225 }; 226 227 static int ili251x_read_reg_common(struct i2c_client *client, 228 u8 reg, void *buf, size_t len, 229 unsigned int delay) 230 { 231 int error; 232 int ret; 233 234 ret = i2c_master_send(client, ®, 1); 235 if (ret == 1) { 236 if (delay) 237 usleep_range(delay, delay + 500); 238 239 ret = i2c_master_recv(client, buf, len); 240 if (ret == len) 241 return 0; 242 } 243 244 error = ret < 0 ? ret : -EIO; 245 dev_err(&client->dev, "%s failed: %d\n", __func__, error); 246 return ret; 247 } 248 249 static int ili251x_read_reg(struct i2c_client *client, 250 u8 reg, void *buf, size_t len) 251 { 252 return ili251x_read_reg_common(client, reg, buf, len, 5000); 253 } 254 255 static int ili251x_read_touch_data(struct i2c_client *client, u8 *data) 256 { 257 int error; 258 259 error = ili251x_read_reg_common(client, REG_TOUCHDATA, 260 data, ILI251X_DATA_SIZE1, 0); 261 if (!error && data[0] == 2) { 262 error = i2c_master_recv(client, data + ILI251X_DATA_SIZE1, 263 ILI251X_DATA_SIZE2); 264 if (error >= 0) 265 error = error == ILI251X_DATA_SIZE2 ? 0 : -EIO; 266 } 267 268 return error; 269 } 270 271 static bool ili251x_touchdata_to_coords(const u8 *touchdata, 272 unsigned int finger, 273 unsigned int *x, unsigned int *y, 274 unsigned int *z) 275 { 276 u16 val; 277 278 val = get_unaligned_be16(touchdata + 1 + (finger * 5) + 0); 279 if (!(val & BIT(15))) /* Touch indication */ 280 return false; 281 282 *x = val & 0x3fff; 283 *y = get_unaligned_be16(touchdata + 1 + (finger * 5) + 2); 284 *z = touchdata[1 + (finger * 5) + 4]; 285 286 return true; 287 } 288 289 static bool ili251x_check_continue_polling(const u8 *data, bool touch) 290 { 291 return touch; 292 } 293 294 static const struct ili2xxx_chip ili251x_chip = { 295 .read_reg = ili251x_read_reg, 296 .get_touch_data = ili251x_read_touch_data, 297 .parse_touch_data = ili251x_touchdata_to_coords, 298 .continue_polling = ili251x_check_continue_polling, 299 .max_touches = 10, 300 .has_calibrate_reg = true, 301 .has_firmware_proto = true, 302 .has_pressure_reg = true, 303 }; 304 305 static bool ili210x_report_events(struct ili210x *priv, u8 *touchdata) 306 { 307 struct input_dev *input = priv->input; 308 int i; 309 bool contact = false, touch; 310 unsigned int x = 0, y = 0, z = 0; 311 312 for (i = 0; i < priv->chip->max_touches; i++) { 313 touch = priv->chip->parse_touch_data(touchdata, i, &x, &y, &z); 314 315 input_mt_slot(input, i); 316 if (input_mt_report_slot_state(input, MT_TOOL_FINGER, touch)) { 317 touchscreen_report_pos(input, &priv->prop, x, y, true); 318 if (priv->chip->has_pressure_reg) 319 input_report_abs(input, ABS_MT_PRESSURE, z); 320 contact = true; 321 } 322 } 323 324 input_mt_report_pointer_emulation(input, false); 325 input_sync(input); 326 327 return contact; 328 } 329 330 static void ili210x_process_events(struct ili210x *priv) 331 { 332 struct i2c_client *client = priv->client; 333 const struct ili2xxx_chip *chip = priv->chip; 334 u8 touchdata[ILI210X_DATA_SIZE] = { 0 }; 335 bool keep_polling; 336 ktime_t time_next; 337 s64 time_delta; 338 bool touch; 339 int error; 340 341 do { 342 time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD); 343 error = chip->get_touch_data(client, touchdata); 344 if (error) { 345 dev_err(&client->dev, 346 "Unable to get touch data: %d\n", error); 347 break; 348 } 349 350 touch = ili210x_report_events(priv, touchdata); 351 keep_polling = chip->continue_polling(touchdata, touch); 352 if (keep_polling) { 353 time_delta = ktime_us_delta(time_next, ktime_get()); 354 if (time_delta > 0) 355 usleep_range(time_delta, time_delta + 1000); 356 } 357 } while (!priv->stop && keep_polling); 358 } 359 360 static irqreturn_t ili210x_irq(int irq, void *irq_data) 361 { 362 struct ili210x *priv = irq_data; 363 364 ili210x_process_events(priv); 365 366 return IRQ_HANDLED; 367 }; 368 369 static void ili210x_work_i2c_poll(struct input_dev *input) 370 { 371 struct ili210x *priv = input_get_drvdata(input); 372 373 ili210x_process_events(priv); 374 } 375 376 static int ili251x_firmware_update_resolution(struct device *dev) 377 { 378 struct i2c_client *client = to_i2c_client(dev); 379 struct ili210x *priv = i2c_get_clientdata(client); 380 u16 resx, resy; 381 u8 rs[10]; 382 int error; 383 384 /* The firmware update blob might have changed the resolution. */ 385 error = priv->chip->read_reg(client, REG_PANEL_INFO, &rs, sizeof(rs)); 386 if (!error) { 387 resx = le16_to_cpup((__le16 *)rs); 388 resy = le16_to_cpup((__le16 *)(rs + 2)); 389 390 /* The value reported by the firmware is invalid. */ 391 if (!resx || resx == 0xffff || !resy || resy == 0xffff) 392 error = -EINVAL; 393 } 394 395 /* 396 * In case of error, the firmware might be stuck in bootloader mode, 397 * e.g. after a failed firmware update. Set maximum resolution, but 398 * do not fail to probe, so the user can re-trigger the firmware 399 * update and recover the touch controller. 400 */ 401 if (error) { 402 dev_warn(dev, "Invalid resolution reported by controller.\n"); 403 resx = 16384; 404 resy = 16384; 405 } 406 407 input_abs_set_max(priv->input, ABS_X, resx - 1); 408 input_abs_set_max(priv->input, ABS_Y, resy - 1); 409 input_abs_set_max(priv->input, ABS_MT_POSITION_X, resx - 1); 410 input_abs_set_max(priv->input, ABS_MT_POSITION_Y, resy - 1); 411 412 return error; 413 } 414 415 static ssize_t ili251x_firmware_update_firmware_version(struct device *dev) 416 { 417 struct i2c_client *client = to_i2c_client(dev); 418 struct ili210x *priv = i2c_get_clientdata(client); 419 int error; 420 u8 fw[8]; 421 422 /* Get firmware version */ 423 error = priv->chip->read_reg(client, REG_FIRMWARE_VERSION, 424 &fw, sizeof(fw)); 425 if (!error) 426 memcpy(priv->version_firmware, fw, sizeof(fw)); 427 428 return error; 429 } 430 431 static ssize_t ili251x_firmware_update_kernel_version(struct device *dev) 432 { 433 struct i2c_client *client = to_i2c_client(dev); 434 struct ili210x *priv = i2c_get_clientdata(client); 435 int error; 436 u8 kv[5]; 437 438 /* Get kernel version */ 439 error = priv->chip->read_reg(client, REG_KERNEL_VERSION, 440 &kv, sizeof(kv)); 441 if (!error) 442 memcpy(priv->version_kernel, kv, sizeof(kv)); 443 444 return error; 445 } 446 447 static ssize_t ili251x_firmware_update_protocol_version(struct device *dev) 448 { 449 struct i2c_client *client = to_i2c_client(dev); 450 struct ili210x *priv = i2c_get_clientdata(client); 451 int error; 452 u8 pv[2]; 453 454 /* Get protocol version */ 455 error = priv->chip->read_reg(client, REG_PROTOCOL_VERSION, 456 &pv, sizeof(pv)); 457 if (!error) 458 memcpy(priv->version_proto, pv, sizeof(pv)); 459 460 return error; 461 } 462 463 static ssize_t ili251x_firmware_update_ic_mode(struct device *dev) 464 { 465 struct i2c_client *client = to_i2c_client(dev); 466 struct ili210x *priv = i2c_get_clientdata(client); 467 int error; 468 u8 md[2]; 469 470 /* Get chip boot mode */ 471 error = priv->chip->read_reg(client, REG_GET_MODE, &md, sizeof(md)); 472 if (!error) 473 memcpy(priv->ic_mode, md, sizeof(md)); 474 475 return error; 476 } 477 478 static int ili251x_firmware_update_cached_state(struct device *dev) 479 { 480 struct i2c_client *client = to_i2c_client(dev); 481 struct ili210x *priv = i2c_get_clientdata(client); 482 int error; 483 484 if (!priv->chip->has_firmware_proto) 485 return 0; 486 487 /* Wait for firmware to boot and stabilize itself. */ 488 msleep(200); 489 490 /* Firmware does report valid information. */ 491 error = ili251x_firmware_update_resolution(dev); 492 if (error) 493 return error; 494 495 error = ili251x_firmware_update_firmware_version(dev); 496 if (error) 497 return error; 498 499 error = ili251x_firmware_update_kernel_version(dev); 500 if (error) 501 return error; 502 503 error = ili251x_firmware_update_protocol_version(dev); 504 if (error) 505 return error; 506 507 error = ili251x_firmware_update_ic_mode(dev); 508 if (error) 509 return error; 510 511 return 0; 512 } 513 514 static ssize_t ili251x_firmware_version_show(struct device *dev, 515 struct device_attribute *attr, 516 char *buf) 517 { 518 struct i2c_client *client = to_i2c_client(dev); 519 struct ili210x *priv = i2c_get_clientdata(client); 520 u8 *fw = priv->version_firmware; 521 522 return sysfs_emit(buf, "%02x%02x.%02x%02x.%02x%02x.%02x%02x\n", 523 fw[0], fw[1], fw[2], fw[3], 524 fw[4], fw[5], fw[6], fw[7]); 525 } 526 static DEVICE_ATTR(firmware_version, 0444, ili251x_firmware_version_show, NULL); 527 528 static ssize_t ili251x_kernel_version_show(struct device *dev, 529 struct device_attribute *attr, 530 char *buf) 531 { 532 struct i2c_client *client = to_i2c_client(dev); 533 struct ili210x *priv = i2c_get_clientdata(client); 534 u8 *kv = priv->version_kernel; 535 536 return sysfs_emit(buf, "%02x.%02x.%02x.%02x.%02x\n", 537 kv[0], kv[1], kv[2], kv[3], kv[4]); 538 } 539 static DEVICE_ATTR(kernel_version, 0444, ili251x_kernel_version_show, NULL); 540 541 static ssize_t ili251x_protocol_version_show(struct device *dev, 542 struct device_attribute *attr, 543 char *buf) 544 { 545 struct i2c_client *client = to_i2c_client(dev); 546 struct ili210x *priv = i2c_get_clientdata(client); 547 u8 *pv = priv->version_proto; 548 549 return sysfs_emit(buf, "%02x.%02x\n", pv[0], pv[1]); 550 } 551 static DEVICE_ATTR(protocol_version, 0444, ili251x_protocol_version_show, NULL); 552 553 static ssize_t ili251x_mode_show(struct device *dev, 554 struct device_attribute *attr, char *buf) 555 { 556 struct i2c_client *client = to_i2c_client(dev); 557 struct ili210x *priv = i2c_get_clientdata(client); 558 u8 *md = priv->ic_mode; 559 char *mode = "AP"; 560 561 if (md[0] == REG_GET_MODE_AP) /* Application Mode */ 562 mode = "AP"; 563 else if (md[0] == REG_GET_MODE_BL) /* BootLoader Mode */ 564 mode = "BL"; 565 else /* Unknown Mode */ 566 mode = "??"; 567 568 return sysfs_emit(buf, "%02x.%02x:%s\n", md[0], md[1], mode); 569 } 570 static DEVICE_ATTR(mode, 0444, ili251x_mode_show, NULL); 571 572 static ssize_t ili210x_calibrate(struct device *dev, 573 struct device_attribute *attr, 574 const char *buf, size_t count) 575 { 576 struct i2c_client *client = to_i2c_client(dev); 577 struct ili210x *priv = i2c_get_clientdata(client); 578 unsigned long calibrate; 579 int rc; 580 u8 cmd = REG_CALIBRATE; 581 582 if (kstrtoul(buf, 10, &calibrate)) 583 return -EINVAL; 584 585 if (calibrate > 1) 586 return -EINVAL; 587 588 if (calibrate) { 589 rc = i2c_master_send(priv->client, &cmd, sizeof(cmd)); 590 if (rc != sizeof(cmd)) 591 return -EIO; 592 } 593 594 return count; 595 } 596 static DEVICE_ATTR(calibrate, S_IWUSR, NULL, ili210x_calibrate); 597 598 static const u8 *ili251x_firmware_to_buffer(const struct firmware *fw, 599 u16 *ac_end, u16 *df_end) 600 { 601 const struct ihex_binrec *rec; 602 u32 fw_addr, fw_last_addr = 0; 603 u16 fw_len; 604 605 /* 606 * The firmware ihex blob can never be bigger than 64 kiB, so make this 607 * simple -- allocate a 64 kiB buffer, iterate over the ihex blob records 608 * once, copy them all into this buffer at the right locations, and then 609 * do all operations on this linear buffer. 610 */ 611 u8* fw_buf __free(kvfree) = kvmalloc(SZ_64K, GFP_KERNEL); 612 if (!fw_buf) 613 return ERR_PTR(-ENOMEM); 614 615 rec = (const struct ihex_binrec *)fw->data; 616 while (rec) { 617 fw_addr = be32_to_cpu(rec->addr); 618 fw_len = be16_to_cpu(rec->len); 619 620 /* The last 32 Byte firmware block can be 0xffe0 */ 621 if (fw_addr + fw_len > SZ_64K || fw_addr > SZ_64K - 32) 622 return ERR_PTR(-EFBIG); 623 624 /* Find the last address before DF start address, that is AC end */ 625 if (fw_addr == 0xf000) 626 *ac_end = fw_last_addr; 627 fw_last_addr = fw_addr + fw_len; 628 629 memcpy(fw_buf + fw_addr, rec->data, fw_len); 630 rec = ihex_next_binrec(rec); 631 } 632 633 /* DF end address is the last address in the firmware blob */ 634 *df_end = fw_addr + fw_len; 635 636 return_ptr(fw_buf); 637 } 638 639 /* Switch mode between Application and BootLoader */ 640 static int ili251x_switch_ic_mode(struct i2c_client *client, u8 cmd_mode) 641 { 642 struct ili210x *priv = i2c_get_clientdata(client); 643 u8 cmd_wren[3] = { REG_WRITE_ENABLE, 0x5a, 0xa5 }; 644 u8 md[2]; 645 int error; 646 647 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md)); 648 if (error) 649 return error; 650 /* Mode already set */ 651 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) || 652 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL)) 653 return 0; 654 655 /* Unlock writes */ 656 error = i2c_master_send(client, cmd_wren, sizeof(cmd_wren)); 657 if (error != sizeof(cmd_wren)) 658 return -EINVAL; 659 660 mdelay(20); 661 662 /* Select mode (BootLoader or Application) */ 663 error = i2c_master_send(client, &cmd_mode, 1); 664 if (error != 1) 665 return -EINVAL; 666 667 mdelay(200); /* Reboot into bootloader takes a lot of time ... */ 668 669 /* Read back mode */ 670 error = priv->chip->read_reg(client, REG_GET_MODE, md, sizeof(md)); 671 if (error) 672 return error; 673 /* Check if mode is correct now. */ 674 if ((cmd_mode == REG_SET_MODE_AP && md[0] == REG_GET_MODE_AP) || 675 (cmd_mode == REG_SET_MODE_BL && md[0] == REG_GET_MODE_BL)) 676 return 0; 677 678 return -EINVAL; 679 } 680 681 static int ili251x_firmware_busy(struct i2c_client *client) 682 { 683 struct ili210x *priv = i2c_get_clientdata(client); 684 int error, i = 0; 685 u8 data; 686 687 do { 688 /* The read_reg already contains suitable delay */ 689 error = priv->chip->read_reg(client, REG_IC_BUSY, &data, 1); 690 if (error) 691 return error; 692 if (i++ == 100000) 693 return -ETIMEDOUT; 694 } while (data != REG_IC_BUSY_NOT_BUSY); 695 696 return 0; 697 } 698 699 static int ili251x_firmware_write_to_ic(struct device *dev, const u8 *fwbuf, 700 u16 start, u16 end, u8 dataflash) 701 { 702 struct i2c_client *client = to_i2c_client(dev); 703 struct ili210x *priv = i2c_get_clientdata(client); 704 u8 cmd_crc = REG_READ_DATA_CRC; 705 u8 crcrb[4] = { 0 }; 706 u8 fw_data[33]; 707 u16 fw_addr; 708 int error; 709 710 /* 711 * The DF (dataflash) needs 2 bytes offset for unknown reasons, 712 * the AC (application) has 2 bytes CRC16-CCITT at the end. 713 */ 714 u16 crc = crc_ccitt(0, fwbuf + start + (dataflash ? 2 : 0), 715 end - start - 2); 716 717 /* Unlock write to either AC (application) or DF (dataflash) area */ 718 u8 cmd_wr[10] = { 719 REG_WRITE_ENABLE, 0x5a, 0xa5, dataflash, 720 (end >> 16) & 0xff, (end >> 8) & 0xff, end & 0xff, 721 (crc >> 16) & 0xff, (crc >> 8) & 0xff, crc & 0xff 722 }; 723 724 error = i2c_master_send(client, cmd_wr, sizeof(cmd_wr)); 725 if (error != sizeof(cmd_wr)) 726 return -EINVAL; 727 728 error = ili251x_firmware_busy(client); 729 if (error) 730 return error; 731 732 for (fw_addr = start; fw_addr < end; fw_addr += 32) { 733 fw_data[0] = REG_WRITE_DATA; 734 memcpy(&(fw_data[1]), fwbuf + fw_addr, 32); 735 error = i2c_master_send(client, fw_data, 33); 736 if (error != sizeof(fw_data)) 737 return error; 738 error = ili251x_firmware_busy(client); 739 if (error) 740 return error; 741 } 742 743 error = i2c_master_send(client, &cmd_crc, 1); 744 if (error != 1) 745 return -EINVAL; 746 747 error = ili251x_firmware_busy(client); 748 if (error) 749 return error; 750 751 error = priv->chip->read_reg(client, REG_READ_DATA_CRC, 752 &crcrb, sizeof(crcrb)); 753 if (error) 754 return error; 755 756 /* Check CRC readback */ 757 if ((crcrb[0] != (crc & 0xff)) || crcrb[1] != ((crc >> 8) & 0xff)) 758 return -EINVAL; 759 760 return 0; 761 } 762 763 static int ili251x_firmware_reset(struct i2c_client *client) 764 { 765 u8 cmd_reset[2] = { 0xf2, 0x01 }; 766 int error; 767 768 error = i2c_master_send(client, cmd_reset, sizeof(cmd_reset)); 769 if (error != sizeof(cmd_reset)) 770 return -EINVAL; 771 772 return ili251x_firmware_busy(client); 773 } 774 775 static void ili210x_hardware_reset(struct gpio_desc *reset_gpio) 776 { 777 /* Reset the controller */ 778 gpiod_set_value_cansleep(reset_gpio, 1); 779 usleep_range(12000, 15000); 780 gpiod_set_value_cansleep(reset_gpio, 0); 781 msleep(300); 782 } 783 784 static int ili210x_do_firmware_update(struct ili210x *priv, 785 const u8 *fwbuf, u16 ac_end, u16 df_end) 786 { 787 struct i2c_client *client = priv->client; 788 struct device *dev = &client->dev; 789 int error; 790 int i; 791 792 error = ili251x_firmware_reset(client); 793 if (error) 794 return error; 795 796 /* This may not succeed on first try, so re-try a few times. */ 797 for (i = 0; i < 5; i++) { 798 error = ili251x_switch_ic_mode(client, REG_SET_MODE_BL); 799 if (!error) 800 break; 801 } 802 803 if (error) 804 return error; 805 806 dev_dbg(dev, "IC is now in BootLoader mode\n"); 807 808 msleep(200); /* The bootloader seems to need some time too. */ 809 810 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0xf000, df_end, 1); 811 if (error) { 812 dev_err(dev, "DF firmware update failed, error=%d\n", error); 813 return error; 814 } 815 816 dev_dbg(dev, "DataFlash firmware written\n"); 817 818 error = ili251x_firmware_write_to_ic(dev, fwbuf, 0x2000, ac_end, 0); 819 if (error) { 820 dev_err(dev, "AC firmware update failed, error=%d\n", error); 821 return error; 822 } 823 824 dev_dbg(dev, "Application firmware written\n"); 825 826 /* This may not succeed on first try, so re-try a few times. */ 827 for (i = 0; i < 5; i++) { 828 error = ili251x_switch_ic_mode(client, REG_SET_MODE_AP); 829 if (!error) 830 break; 831 } 832 833 if (error) 834 return error; 835 836 dev_dbg(dev, "IC is now in Application mode\n"); 837 838 error = ili251x_firmware_update_cached_state(dev); 839 if (error) 840 return error; 841 842 return 0; 843 } 844 845 static ssize_t ili210x_firmware_update(struct device *dev, const u8 *fwbuf, 846 u16 ac_end, u16 df_end) 847 { 848 struct i2c_client *client = to_i2c_client(dev); 849 struct ili210x *priv = i2c_get_clientdata(client); 850 const char *fwname = ILI251X_FW_FILENAME; 851 int error; 852 853 dev_dbg(dev, "Firmware update started, firmware=%s\n", fwname); 854 855 ili210x_hardware_reset(priv->reset_gpio); 856 857 error = ili210x_do_firmware_update(priv, fwbuf, ac_end, df_end); 858 859 ili210x_hardware_reset(priv->reset_gpio); 860 861 dev_dbg(dev, "Firmware update ended, error=%i\n", error); 862 863 return error; 864 } 865 866 static ssize_t ili210x_firmware_update_store(struct device *dev, 867 struct device_attribute *attr, 868 const char *buf, size_t count) 869 { 870 struct i2c_client *client = to_i2c_client(dev); 871 const char *fwname = ILI251X_FW_FILENAME; 872 u16 ac_end, df_end; 873 int error; 874 875 const struct firmware *fw __free(firmware) = NULL; 876 error = request_ihex_firmware(&fw, fwname, dev); 877 if (error) { 878 dev_err(dev, "Failed to request firmware %s, error=%d\n", 879 fwname, error); 880 return error; 881 } 882 883 const u8* fwbuf __free(kvfree) = 884 ili251x_firmware_to_buffer(fw, &ac_end, &df_end); 885 error = PTR_ERR_OR_ZERO(fwbuf); 886 if (error) 887 return error; 888 889 /* 890 * Disable touchscreen IRQ, so that we would not get spurious touch 891 * interrupt during firmware update, and so that the IRQ handler won't 892 * trigger and interfere with the firmware update. There is no bit in 893 * the touch controller to disable the IRQs during update, so we have 894 * to do it this way here. 895 */ 896 if (client->irq > 0) { 897 guard(disable_irq)(&client->irq); 898 error = ili210x_firmware_update(dev, fwbuf, ac_end, df_end); 899 } else { 900 error = ili210x_firmware_update(dev, fwbuf, ac_end, df_end); 901 } 902 903 return error ?: count; 904 } 905 906 static DEVICE_ATTR(firmware_update, 0200, NULL, ili210x_firmware_update_store); 907 908 static struct attribute *ili210x_attrs[] = { 909 &dev_attr_calibrate.attr, 910 &dev_attr_firmware_update.attr, 911 &dev_attr_firmware_version.attr, 912 &dev_attr_kernel_version.attr, 913 &dev_attr_protocol_version.attr, 914 &dev_attr_mode.attr, 915 NULL, 916 }; 917 918 static umode_t ili210x_attributes_visible(struct kobject *kobj, 919 struct attribute *attr, int index) 920 { 921 struct device *dev = kobj_to_dev(kobj); 922 struct i2c_client *client = to_i2c_client(dev); 923 struct ili210x *priv = i2c_get_clientdata(client); 924 925 /* Calibrate is present on all ILI2xxx which have calibrate register */ 926 if (attr == &dev_attr_calibrate.attr) 927 return priv->chip->has_calibrate_reg ? attr->mode : 0; 928 929 /* Firmware/Kernel/Protocol/BootMode is implemented only for ILI251x */ 930 if (!priv->chip->has_firmware_proto) 931 return 0; 932 933 return attr->mode; 934 } 935 936 static const struct attribute_group ili210x_group = { 937 .attrs = ili210x_attrs, 938 .is_visible = ili210x_attributes_visible, 939 }; 940 __ATTRIBUTE_GROUPS(ili210x); 941 942 static void ili210x_power_down(void *data) 943 { 944 struct gpio_desc *reset_gpio = data; 945 946 gpiod_set_value_cansleep(reset_gpio, 1); 947 } 948 949 static void ili210x_stop(void *data) 950 { 951 struct ili210x *priv = data; 952 953 /* Tell ISR to quit even if there is a contact. */ 954 priv->stop = true; 955 } 956 957 static int ili210x_i2c_probe(struct i2c_client *client) 958 { 959 const struct i2c_device_id *id = i2c_client_get_device_id(client); 960 struct device *dev = &client->dev; 961 const struct ili2xxx_chip *chip; 962 struct ili210x *priv; 963 struct gpio_desc *reset_gpio; 964 struct input_dev *input; 965 int error; 966 unsigned int max_xy; 967 968 dev_dbg(dev, "Probing for ILI210X I2C Touschreen driver"); 969 970 chip = device_get_match_data(dev); 971 if (!chip && id) 972 chip = (const struct ili2xxx_chip *)id->driver_data; 973 if (!chip) 974 return dev_err_probe(&client->dev, -ENODEV, "unknown device model\n"); 975 976 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 977 if (IS_ERR(reset_gpio)) 978 return PTR_ERR(reset_gpio); 979 980 if (reset_gpio) { 981 error = devm_add_action_or_reset(dev, ili210x_power_down, 982 reset_gpio); 983 if (error) 984 return error; 985 986 ili210x_hardware_reset(reset_gpio); 987 } 988 989 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 990 if (!priv) 991 return -ENOMEM; 992 993 input = devm_input_allocate_device(dev); 994 if (!input) 995 return -ENOMEM; 996 997 priv->client = client; 998 priv->input = input; 999 priv->reset_gpio = reset_gpio; 1000 priv->chip = chip; 1001 i2c_set_clientdata(client, priv); 1002 1003 /* Setup input device */ 1004 input->name = "ILI210x Touchscreen"; 1005 input->id.bustype = BUS_I2C; 1006 1007 /* Multi touch */ 1008 max_xy = (chip->resolution ?: SZ_64K) - 1; 1009 input_set_abs_params(input, ABS_MT_POSITION_X, 0, max_xy, 0, 0); 1010 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, max_xy, 0, 0); 1011 if (priv->chip->has_pressure_reg) 1012 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 0xa, 0, 0); 1013 error = ili251x_firmware_update_cached_state(dev); 1014 if (error) 1015 dev_warn(dev, "Unable to cache firmware information, err: %d\n", 1016 error); 1017 1018 touchscreen_parse_properties(input, true, &priv->prop); 1019 1020 error = input_mt_init_slots(input, priv->chip->max_touches, 1021 INPUT_MT_DIRECT); 1022 if (error) 1023 return dev_err_probe(dev, error, "Unable to set up slots\n"); 1024 1025 input_set_drvdata(input, priv); 1026 1027 if (client->irq > 0) { 1028 error = devm_request_threaded_irq(dev, client->irq, NULL, ili210x_irq, 1029 IRQF_ONESHOT, client->name, priv); 1030 if (error) 1031 return dev_err_probe(dev, error, "Unable to request touchscreen IRQ\n"); 1032 } else { 1033 error = input_setup_polling(input, ili210x_work_i2c_poll); 1034 if (error) 1035 return dev_err_probe(dev, error, "Could not set up polling mode\n"); 1036 1037 input_set_poll_interval(input, ILI2XXX_POLL_PERIOD); 1038 } 1039 1040 error = devm_add_action_or_reset(dev, ili210x_stop, priv); 1041 if (error) 1042 return error; 1043 1044 error = input_register_device(priv->input); 1045 if (error) 1046 return dev_err_probe(dev, error, "Cannot register input device\n"); 1047 1048 return 0; 1049 } 1050 1051 static const struct i2c_device_id ili210x_i2c_id[] = { 1052 { "ili210x", (long)&ili210x_chip }, 1053 { "ili2117", (long)&ili211x_chip }, 1054 { "ili2120", (long)&ili212x_chip }, 1055 { "ili251x", (long)&ili251x_chip }, 1056 { } 1057 }; 1058 MODULE_DEVICE_TABLE(i2c, ili210x_i2c_id); 1059 1060 static const struct of_device_id ili210x_dt_ids[] = { 1061 { .compatible = "ilitek,ili210x", .data = &ili210x_chip }, 1062 { .compatible = "ilitek,ili2117", .data = &ili211x_chip }, 1063 { .compatible = "ilitek,ili2120", .data = &ili212x_chip }, 1064 { .compatible = "ilitek,ili251x", .data = &ili251x_chip }, 1065 { } 1066 }; 1067 MODULE_DEVICE_TABLE(of, ili210x_dt_ids); 1068 1069 static struct i2c_driver ili210x_ts_driver = { 1070 .driver = { 1071 .name = "ili210x_i2c", 1072 .dev_groups = ili210x_groups, 1073 .of_match_table = ili210x_dt_ids, 1074 }, 1075 .id_table = ili210x_i2c_id, 1076 .probe = ili210x_i2c_probe, 1077 }; 1078 1079 module_i2c_driver(ili210x_ts_driver); 1080 1081 MODULE_AUTHOR("Olivier Sobrie <olivier@sobrie.be>"); 1082 MODULE_DESCRIPTION("ILI210X I2C Touchscreen Driver"); 1083 MODULE_LICENSE("GPL"); 1084