1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Elan Microelectronics touch panels with I2C interface 4 * 5 * Copyright (C) 2014 Elan Microelectronics Corporation. 6 * Scott Liu <scott.liu@emc.com.tw> 7 * 8 * This code is partly based on hid-multitouch.c: 9 * 10 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr> 11 * Copyright (c) 2010-2012 Benjamin Tissoires <benjamin.tissoires@gmail.com> 12 * Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France 13 * 14 * This code is partly based on i2c-hid.c: 15 * 16 * Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com> 17 * Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France 18 * Copyright (c) 2012 Red Hat, Inc 19 */ 20 21 22 #include <linux/bits.h> 23 #include <linux/module.h> 24 #include <linux/input.h> 25 #include <linux/interrupt.h> 26 #include <linux/irq.h> 27 #include <linux/platform_device.h> 28 #include <linux/async.h> 29 #include <linux/i2c.h> 30 #include <linux/delay.h> 31 #include <linux/uaccess.h> 32 #include <linux/buffer_head.h> 33 #include <linux/slab.h> 34 #include <linux/firmware.h> 35 #include <linux/input/mt.h> 36 #include <linux/input/touchscreen.h> 37 #include <linux/acpi.h> 38 #include <linux/of.h> 39 #include <linux/gpio/consumer.h> 40 #include <linux/regulator/consumer.h> 41 #include <asm/unaligned.h> 42 43 /* Device, Driver information */ 44 #define DEVICE_NAME "elants_i2c" 45 46 /* Convert from rows or columns into resolution */ 47 #define ELAN_TS_RESOLUTION(n, m) (((n) - 1) * (m)) 48 49 /* FW header data */ 50 #define HEADER_SIZE 4 51 #define FW_HDR_TYPE 0 52 #define FW_HDR_COUNT 1 53 #define FW_HDR_LENGTH 2 54 55 /* Buffer mode Queue Header information */ 56 #define QUEUE_HEADER_SINGLE 0x62 57 #define QUEUE_HEADER_NORMAL 0X63 58 #define QUEUE_HEADER_WAIT 0x64 59 #define QUEUE_HEADER_NORMAL2 0x66 60 61 /* Command header definition */ 62 #define CMD_HEADER_WRITE 0x54 63 #define CMD_HEADER_READ 0x53 64 #define CMD_HEADER_6B_READ 0x5B 65 #define CMD_HEADER_ROM_READ 0x96 66 #define CMD_HEADER_RESP 0x52 67 #define CMD_HEADER_6B_RESP 0x9B 68 #define CMD_HEADER_ROM_RESP 0x95 69 #define CMD_HEADER_HELLO 0x55 70 #define CMD_HEADER_REK 0x66 71 72 /* FW position data */ 73 #define PACKET_SIZE_OLD 40 74 #define PACKET_SIZE 55 75 #define MAX_CONTACT_NUM 10 76 #define FW_POS_HEADER 0 77 #define FW_POS_STATE 1 78 #define FW_POS_TOTAL 2 79 #define FW_POS_XY 3 80 #define FW_POS_TOOL_TYPE 33 81 #define FW_POS_CHECKSUM 34 82 #define FW_POS_WIDTH 35 83 #define FW_POS_PRESSURE 45 84 85 #define HEADER_REPORT_10_FINGER 0x62 86 87 /* Header (4 bytes) plus 3 full 10-finger packets */ 88 #define MAX_PACKET_SIZE 169 89 90 #define BOOT_TIME_DELAY_MS 50 91 92 /* FW read command, 0x53 0x?? 0x0, 0x01 */ 93 #define E_ELAN_INFO_FW_VER 0x00 94 #define E_ELAN_INFO_BC_VER 0x10 95 #define E_ELAN_INFO_X_RES 0x60 96 #define E_ELAN_INFO_Y_RES 0x63 97 #define E_ELAN_INFO_REK 0xD0 98 #define E_ELAN_INFO_TEST_VER 0xE0 99 #define E_ELAN_INFO_FW_ID 0xF0 100 #define E_INFO_OSR 0xD6 101 #define E_INFO_PHY_SCAN 0xD7 102 #define E_INFO_PHY_DRIVER 0xD8 103 104 /* FW write command, 0x54 0x?? 0x0, 0x01 */ 105 #define E_POWER_STATE_SLEEP 0x50 106 #define E_POWER_STATE_RESUME 0x58 107 108 #define MAX_RETRIES 3 109 #define MAX_FW_UPDATE_RETRIES 30 110 111 #define ELAN_FW_PAGESIZE 132 112 113 /* calibration timeout definition */ 114 #define ELAN_CALI_TIMEOUT_MSEC 12000 115 116 #define ELAN_POWERON_DELAY_USEC 500 117 #define ELAN_RESET_DELAY_MSEC 20 118 119 enum elants_chip_id { 120 EKTH3500, 121 EKTF3624, 122 }; 123 124 enum elants_state { 125 ELAN_STATE_NORMAL, 126 ELAN_WAIT_QUEUE_HEADER, 127 ELAN_WAIT_RECALIBRATION, 128 }; 129 130 enum elants_iap_mode { 131 ELAN_IAP_OPERATIONAL, 132 ELAN_IAP_RECOVERY, 133 }; 134 135 /* struct elants_data - represents state of Elan touchscreen device */ 136 struct elants_data { 137 struct i2c_client *client; 138 struct input_dev *input; 139 140 struct regulator *vcc33; 141 struct regulator *vccio; 142 struct gpio_desc *reset_gpio; 143 144 u16 fw_version; 145 u8 test_version; 146 u8 solution_version; 147 u8 bc_version; 148 u8 iap_version; 149 u16 hw_version; 150 u8 major_res; 151 unsigned int x_res; /* resolution in units/mm */ 152 unsigned int y_res; 153 unsigned int x_max; 154 unsigned int y_max; 155 unsigned int phy_x; 156 unsigned int phy_y; 157 struct touchscreen_properties prop; 158 159 enum elants_state state; 160 enum elants_chip_id chip_id; 161 enum elants_iap_mode iap_mode; 162 163 /* Guards against concurrent access to the device via sysfs */ 164 struct mutex sysfs_mutex; 165 166 u8 cmd_resp[HEADER_SIZE]; 167 struct completion cmd_done; 168 169 bool wake_irq_enabled; 170 bool keep_power_in_suspend; 171 172 /* Must be last to be used for DMA operations */ 173 u8 buf[MAX_PACKET_SIZE] ____cacheline_aligned; 174 }; 175 176 static int elants_i2c_send(struct i2c_client *client, 177 const void *data, size_t size) 178 { 179 int ret; 180 181 ret = i2c_master_send(client, data, size); 182 if (ret == size) 183 return 0; 184 185 if (ret >= 0) 186 ret = -EIO; 187 188 dev_err(&client->dev, "%s failed (%*ph): %d\n", 189 __func__, (int)size, data, ret); 190 191 return ret; 192 } 193 194 static int elants_i2c_read(struct i2c_client *client, void *data, size_t size) 195 { 196 int ret; 197 198 ret = i2c_master_recv(client, data, size); 199 if (ret == size) 200 return 0; 201 202 if (ret >= 0) 203 ret = -EIO; 204 205 dev_err(&client->dev, "%s failed: %d\n", __func__, ret); 206 207 return ret; 208 } 209 210 static int elants_i2c_execute_command(struct i2c_client *client, 211 const u8 *cmd, size_t cmd_size, 212 u8 *resp, size_t resp_size, 213 int retries, const char *cmd_name) 214 { 215 struct i2c_msg msgs[2]; 216 int ret; 217 u8 expected_response; 218 219 switch (cmd[0]) { 220 case CMD_HEADER_READ: 221 expected_response = CMD_HEADER_RESP; 222 break; 223 224 case CMD_HEADER_6B_READ: 225 expected_response = CMD_HEADER_6B_RESP; 226 break; 227 228 case CMD_HEADER_ROM_READ: 229 expected_response = CMD_HEADER_ROM_RESP; 230 break; 231 232 default: 233 dev_err(&client->dev, "(%s): invalid command: %*ph\n", 234 cmd_name, (int)cmd_size, cmd); 235 return -EINVAL; 236 } 237 238 for (;;) { 239 msgs[0].addr = client->addr; 240 msgs[0].flags = client->flags & I2C_M_TEN; 241 msgs[0].len = cmd_size; 242 msgs[0].buf = (u8 *)cmd; 243 244 msgs[1].addr = client->addr; 245 msgs[1].flags = (client->flags & I2C_M_TEN) | I2C_M_RD; 246 msgs[1].flags |= I2C_M_RD; 247 msgs[1].len = resp_size; 248 msgs[1].buf = resp; 249 250 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 251 if (ret < 0) { 252 if (--retries > 0) { 253 dev_dbg(&client->dev, 254 "(%s) I2C transfer failed: %pe (retrying)\n", 255 cmd_name, ERR_PTR(ret)); 256 continue; 257 } 258 259 dev_err(&client->dev, 260 "(%s) I2C transfer failed: %pe\n", 261 cmd_name, ERR_PTR(ret)); 262 return ret; 263 } 264 265 if (ret != ARRAY_SIZE(msgs) || 266 resp[FW_HDR_TYPE] != expected_response) { 267 if (--retries > 0) { 268 dev_dbg(&client->dev, 269 "(%s) unexpected response: %*ph (retrying)\n", 270 cmd_name, ret, resp); 271 continue; 272 } 273 274 dev_err(&client->dev, 275 "(%s) unexpected response: %*ph\n", 276 cmd_name, ret, resp); 277 return -EIO; 278 } 279 280 return 0; 281 } 282 } 283 284 static int elants_i2c_calibrate(struct elants_data *ts) 285 { 286 struct i2c_client *client = ts->client; 287 int ret, error; 288 static const u8 w_flashkey[] = { CMD_HEADER_WRITE, 0xC0, 0xE1, 0x5A }; 289 static const u8 rek[] = { CMD_HEADER_WRITE, 0x29, 0x00, 0x01 }; 290 static const u8 rek_resp[] = { CMD_HEADER_REK, 0x66, 0x66, 0x66 }; 291 292 disable_irq(client->irq); 293 294 ts->state = ELAN_WAIT_RECALIBRATION; 295 reinit_completion(&ts->cmd_done); 296 297 elants_i2c_send(client, w_flashkey, sizeof(w_flashkey)); 298 elants_i2c_send(client, rek, sizeof(rek)); 299 300 enable_irq(client->irq); 301 302 ret = wait_for_completion_interruptible_timeout(&ts->cmd_done, 303 msecs_to_jiffies(ELAN_CALI_TIMEOUT_MSEC)); 304 305 ts->state = ELAN_STATE_NORMAL; 306 307 if (ret <= 0) { 308 error = ret < 0 ? ret : -ETIMEDOUT; 309 dev_err(&client->dev, 310 "error while waiting for calibration to complete: %d\n", 311 error); 312 return error; 313 } 314 315 if (memcmp(rek_resp, ts->cmd_resp, sizeof(rek_resp))) { 316 dev_err(&client->dev, 317 "unexpected calibration response: %*ph\n", 318 (int)sizeof(ts->cmd_resp), ts->cmd_resp); 319 return -EINVAL; 320 } 321 322 return 0; 323 } 324 325 static int elants_i2c_sw_reset(struct i2c_client *client) 326 { 327 const u8 soft_rst_cmd[] = { 0x77, 0x77, 0x77, 0x77 }; 328 int error; 329 330 error = elants_i2c_send(client, soft_rst_cmd, 331 sizeof(soft_rst_cmd)); 332 if (error) { 333 dev_err(&client->dev, "software reset failed: %d\n", error); 334 return error; 335 } 336 337 /* 338 * We should wait at least 10 msec (but no more than 40) before 339 * sending fastboot or IAP command to the device. 340 */ 341 msleep(30); 342 343 return 0; 344 } 345 346 static u16 elants_i2c_parse_version(u8 *buf) 347 { 348 return get_unaligned_be32(buf) >> 4; 349 } 350 351 static int elants_i2c_query_hw_version(struct elants_data *ts) 352 { 353 struct i2c_client *client = ts->client; 354 int retry_cnt = MAX_RETRIES; 355 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_ID, 0x00, 0x01 }; 356 u8 resp[HEADER_SIZE]; 357 int error; 358 359 while (retry_cnt--) { 360 error = elants_i2c_execute_command(client, cmd, sizeof(cmd), 361 resp, sizeof(resp), 1, 362 "read fw id"); 363 if (error) 364 return error; 365 366 ts->hw_version = elants_i2c_parse_version(resp); 367 if (ts->hw_version != 0xffff) 368 return 0; 369 } 370 371 dev_err(&client->dev, "Invalid fw id: %#04x\n", ts->hw_version); 372 373 return -EINVAL; 374 } 375 376 static int elants_i2c_query_fw_version(struct elants_data *ts) 377 { 378 struct i2c_client *client = ts->client; 379 int retry_cnt = MAX_RETRIES; 380 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_FW_VER, 0x00, 0x01 }; 381 u8 resp[HEADER_SIZE]; 382 int error; 383 384 while (retry_cnt--) { 385 error = elants_i2c_execute_command(client, cmd, sizeof(cmd), 386 resp, sizeof(resp), 1, 387 "read fw version"); 388 if (error) 389 return error; 390 391 ts->fw_version = elants_i2c_parse_version(resp); 392 if (ts->fw_version != 0x0000 && ts->fw_version != 0xffff) 393 return 0; 394 395 dev_dbg(&client->dev, "(read fw version) resp %*phC\n", 396 (int)sizeof(resp), resp); 397 } 398 399 dev_err(&client->dev, "Invalid fw ver: %#04x\n", ts->fw_version); 400 401 return -EINVAL; 402 } 403 404 static int elants_i2c_query_test_version(struct elants_data *ts) 405 { 406 struct i2c_client *client = ts->client; 407 int error; 408 u16 version; 409 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_TEST_VER, 0x00, 0x01 }; 410 u8 resp[HEADER_SIZE]; 411 412 error = elants_i2c_execute_command(client, cmd, sizeof(cmd), 413 resp, sizeof(resp), MAX_RETRIES, 414 "read test version"); 415 if (error) { 416 dev_err(&client->dev, "Failed to read test version\n"); 417 return error; 418 } 419 420 version = elants_i2c_parse_version(resp); 421 ts->test_version = version >> 8; 422 ts->solution_version = version & 0xff; 423 424 return 0; 425 } 426 427 static int elants_i2c_query_bc_version(struct elants_data *ts) 428 { 429 struct i2c_client *client = ts->client; 430 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_BC_VER, 0x00, 0x01 }; 431 u8 resp[HEADER_SIZE]; 432 u16 version; 433 int error; 434 435 error = elants_i2c_execute_command(client, cmd, sizeof(cmd), 436 resp, sizeof(resp), 1, 437 "read BC version"); 438 if (error) 439 return error; 440 441 version = elants_i2c_parse_version(resp); 442 ts->bc_version = version >> 8; 443 ts->iap_version = version & 0xff; 444 445 return 0; 446 } 447 448 static int elants_i2c_query_ts_info_ektf(struct elants_data *ts) 449 { 450 struct i2c_client *client = ts->client; 451 int error; 452 u8 resp[4]; 453 u16 phy_x, phy_y; 454 const u8 get_xres_cmd[] = { 455 CMD_HEADER_READ, E_ELAN_INFO_X_RES, 0x00, 0x00 456 }; 457 const u8 get_yres_cmd[] = { 458 CMD_HEADER_READ, E_ELAN_INFO_Y_RES, 0x00, 0x00 459 }; 460 461 /* Get X/Y size in mm */ 462 error = elants_i2c_execute_command(client, get_xres_cmd, 463 sizeof(get_xres_cmd), 464 resp, sizeof(resp), 1, 465 "get X size"); 466 if (error) 467 return error; 468 469 phy_x = resp[2] | ((resp[3] & 0xF0) << 4); 470 471 error = elants_i2c_execute_command(client, get_yres_cmd, 472 sizeof(get_yres_cmd), 473 resp, sizeof(resp), 1, 474 "get Y size"); 475 if (error) 476 return error; 477 478 phy_y = resp[2] | ((resp[3] & 0xF0) << 4); 479 480 dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y); 481 482 ts->phy_x = phy_x; 483 ts->phy_y = phy_y; 484 485 /* eKTF doesn't report max size, set it to default values */ 486 ts->x_max = 2240 - 1; 487 ts->y_max = 1408 - 1; 488 489 return 0; 490 } 491 492 static int elants_i2c_query_ts_info_ekth(struct elants_data *ts) 493 { 494 struct i2c_client *client = ts->client; 495 int error; 496 u8 resp[17]; 497 u16 phy_x, phy_y, rows, cols, osr; 498 const u8 get_resolution_cmd[] = { 499 CMD_HEADER_6B_READ, 0x00, 0x00, 0x00, 0x00, 0x00 500 }; 501 const u8 get_osr_cmd[] = { 502 CMD_HEADER_READ, E_INFO_OSR, 0x00, 0x01 503 }; 504 const u8 get_physical_scan_cmd[] = { 505 CMD_HEADER_READ, E_INFO_PHY_SCAN, 0x00, 0x01 506 }; 507 const u8 get_physical_drive_cmd[] = { 508 CMD_HEADER_READ, E_INFO_PHY_DRIVER, 0x00, 0x01 509 }; 510 511 /* Get trace number */ 512 error = elants_i2c_execute_command(client, 513 get_resolution_cmd, 514 sizeof(get_resolution_cmd), 515 resp, sizeof(resp), 1, 516 "get resolution"); 517 if (error) 518 return error; 519 520 rows = resp[2] + resp[6] + resp[10]; 521 cols = resp[3] + resp[7] + resp[11]; 522 523 /* Get report resolution value of ABS_MT_TOUCH_MAJOR */ 524 ts->major_res = resp[16]; 525 526 /* Process mm_to_pixel information */ 527 error = elants_i2c_execute_command(client, 528 get_osr_cmd, sizeof(get_osr_cmd), 529 resp, sizeof(resp), 1, "get osr"); 530 if (error) 531 return error; 532 533 osr = resp[3]; 534 535 error = elants_i2c_execute_command(client, 536 get_physical_scan_cmd, 537 sizeof(get_physical_scan_cmd), 538 resp, sizeof(resp), 1, 539 "get physical scan"); 540 if (error) 541 return error; 542 543 phy_x = get_unaligned_be16(&resp[2]); 544 545 error = elants_i2c_execute_command(client, 546 get_physical_drive_cmd, 547 sizeof(get_physical_drive_cmd), 548 resp, sizeof(resp), 1, 549 "get physical drive"); 550 if (error) 551 return error; 552 553 phy_y = get_unaligned_be16(&resp[2]); 554 555 dev_dbg(&client->dev, "phy_x=%d, phy_y=%d\n", phy_x, phy_y); 556 557 if (rows == 0 || cols == 0 || osr == 0) { 558 dev_warn(&client->dev, 559 "invalid trace number data: %d, %d, %d\n", 560 rows, cols, osr); 561 } else { 562 /* translate trace number to TS resolution */ 563 ts->x_max = ELAN_TS_RESOLUTION(rows, osr); 564 ts->x_res = DIV_ROUND_CLOSEST(ts->x_max, phy_x); 565 ts->y_max = ELAN_TS_RESOLUTION(cols, osr); 566 ts->y_res = DIV_ROUND_CLOSEST(ts->y_max, phy_y); 567 ts->phy_x = phy_x; 568 ts->phy_y = phy_y; 569 } 570 571 return 0; 572 } 573 574 static int elants_i2c_fastboot(struct i2c_client *client) 575 { 576 const u8 boot_cmd[] = { 0x4D, 0x61, 0x69, 0x6E }; 577 int error; 578 579 error = elants_i2c_send(client, boot_cmd, sizeof(boot_cmd)); 580 if (error) { 581 dev_err(&client->dev, "boot failed: %d\n", error); 582 return error; 583 } 584 585 dev_dbg(&client->dev, "boot success -- 0x%x\n", client->addr); 586 return 0; 587 } 588 589 static int elants_i2c_initialize(struct elants_data *ts) 590 { 591 struct i2c_client *client = ts->client; 592 int error, error2, retry_cnt; 593 const u8 hello_packet[] = { 0x55, 0x55, 0x55, 0x55 }; 594 const u8 recov_packet[] = { 0x55, 0x55, 0x80, 0x80 }; 595 u8 buf[HEADER_SIZE]; 596 597 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { 598 error = elants_i2c_sw_reset(client); 599 if (error) { 600 /* Continue initializing if it's the last try */ 601 if (retry_cnt < MAX_RETRIES - 1) 602 continue; 603 } 604 605 error = elants_i2c_fastboot(client); 606 if (error) { 607 /* Continue initializing if it's the last try */ 608 if (retry_cnt < MAX_RETRIES - 1) 609 continue; 610 } 611 612 /* Wait for Hello packet */ 613 msleep(BOOT_TIME_DELAY_MS); 614 615 error = elants_i2c_read(client, buf, sizeof(buf)); 616 if (error) { 617 dev_err(&client->dev, 618 "failed to read 'hello' packet: %d\n", error); 619 } else if (!memcmp(buf, hello_packet, sizeof(hello_packet))) { 620 ts->iap_mode = ELAN_IAP_OPERATIONAL; 621 break; 622 } else if (!memcmp(buf, recov_packet, sizeof(recov_packet))) { 623 /* 624 * Setting error code will mark device 625 * in recovery mode below. 626 */ 627 error = -EIO; 628 break; 629 } else { 630 error = -EINVAL; 631 dev_err(&client->dev, 632 "invalid 'hello' packet: %*ph\n", 633 (int)sizeof(buf), buf); 634 } 635 } 636 637 /* hw version is available even if device in recovery state */ 638 error2 = elants_i2c_query_hw_version(ts); 639 if (!error2) 640 error2 = elants_i2c_query_bc_version(ts); 641 if (!error) 642 error = error2; 643 644 if (!error) 645 error = elants_i2c_query_fw_version(ts); 646 if (!error) 647 error = elants_i2c_query_test_version(ts); 648 649 switch (ts->chip_id) { 650 case EKTH3500: 651 if (!error) 652 error = elants_i2c_query_ts_info_ekth(ts); 653 break; 654 case EKTF3624: 655 if (!error) 656 error = elants_i2c_query_ts_info_ektf(ts); 657 break; 658 default: 659 BUG(); 660 } 661 662 if (error) 663 ts->iap_mode = ELAN_IAP_RECOVERY; 664 665 return 0; 666 } 667 668 /* 669 * Firmware update interface. 670 */ 671 672 static int elants_i2c_fw_write_page(struct i2c_client *client, 673 const void *page) 674 { 675 const u8 ack_ok[] = { 0xaa, 0xaa }; 676 u8 buf[2]; 677 int retry; 678 int error; 679 680 for (retry = 0; retry < MAX_FW_UPDATE_RETRIES; retry++) { 681 error = elants_i2c_send(client, page, ELAN_FW_PAGESIZE); 682 if (error) { 683 dev_err(&client->dev, 684 "IAP Write Page failed: %d\n", error); 685 continue; 686 } 687 688 error = elants_i2c_read(client, buf, 2); 689 if (error) { 690 dev_err(&client->dev, 691 "IAP Ack read failed: %d\n", error); 692 return error; 693 } 694 695 if (!memcmp(buf, ack_ok, sizeof(ack_ok))) 696 return 0; 697 698 error = -EIO; 699 dev_err(&client->dev, 700 "IAP Get Ack Error [%02x:%02x]\n", 701 buf[0], buf[1]); 702 } 703 704 return error; 705 } 706 707 static int elants_i2c_validate_remark_id(struct elants_data *ts, 708 const struct firmware *fw) 709 { 710 struct i2c_client *client = ts->client; 711 int error; 712 const u8 cmd[] = { CMD_HEADER_ROM_READ, 0x80, 0x1F, 0x00, 0x00, 0x21 }; 713 u8 resp[6] = { 0 }; 714 u16 ts_remark_id = 0; 715 u16 fw_remark_id = 0; 716 717 /* Compare TS Remark ID and FW Remark ID */ 718 error = elants_i2c_execute_command(client, cmd, sizeof(cmd), 719 resp, sizeof(resp), 720 1, "read Remark ID"); 721 if (error) 722 return error; 723 724 ts_remark_id = get_unaligned_be16(&resp[3]); 725 726 fw_remark_id = get_unaligned_le16(&fw->data[fw->size - 4]); 727 728 if (fw_remark_id != ts_remark_id) { 729 dev_err(&client->dev, 730 "Remark ID Mismatched: ts_remark_id=0x%04x, fw_remark_id=0x%04x.\n", 731 ts_remark_id, fw_remark_id); 732 return -EINVAL; 733 } 734 735 return 0; 736 } 737 738 static int elants_i2c_do_update_firmware(struct i2c_client *client, 739 const struct firmware *fw, 740 bool force) 741 { 742 struct elants_data *ts = i2c_get_clientdata(client); 743 const u8 enter_iap[] = { 0x45, 0x49, 0x41, 0x50 }; 744 const u8 enter_iap2[] = { 0x54, 0x00, 0x12, 0x34 }; 745 const u8 iap_ack[] = { 0x55, 0xaa, 0x33, 0xcc }; 746 const u8 close_idle[] = { 0x54, 0x2c, 0x01, 0x01 }; 747 u8 buf[HEADER_SIZE]; 748 u16 send_id; 749 int page, n_fw_pages; 750 int error; 751 bool check_remark_id = ts->iap_version >= 0x60; 752 753 /* Recovery mode detection! */ 754 if (force) { 755 dev_dbg(&client->dev, "Recovery mode procedure\n"); 756 757 if (check_remark_id) { 758 error = elants_i2c_validate_remark_id(ts, fw); 759 if (error) 760 return error; 761 } 762 763 error = elants_i2c_send(client, enter_iap2, sizeof(enter_iap2)); 764 if (error) { 765 dev_err(&client->dev, "failed to enter IAP mode: %d\n", 766 error); 767 return error; 768 } 769 } else { 770 /* Start IAP Procedure */ 771 dev_dbg(&client->dev, "Normal IAP procedure\n"); 772 773 /* Close idle mode */ 774 error = elants_i2c_send(client, close_idle, sizeof(close_idle)); 775 if (error) 776 dev_err(&client->dev, "Failed close idle: %d\n", error); 777 msleep(60); 778 779 elants_i2c_sw_reset(client); 780 msleep(20); 781 782 if (check_remark_id) { 783 error = elants_i2c_validate_remark_id(ts, fw); 784 if (error) 785 return error; 786 } 787 788 error = elants_i2c_send(client, enter_iap, sizeof(enter_iap)); 789 if (error) { 790 dev_err(&client->dev, "failed to enter IAP mode: %d\n", 791 error); 792 return error; 793 } 794 } 795 796 msleep(20); 797 798 /* check IAP state */ 799 error = elants_i2c_read(client, buf, 4); 800 if (error) { 801 dev_err(&client->dev, 802 "failed to read IAP acknowledgement: %d\n", 803 error); 804 return error; 805 } 806 807 if (memcmp(buf, iap_ack, sizeof(iap_ack))) { 808 dev_err(&client->dev, 809 "failed to enter IAP: %*ph (expected %*ph)\n", 810 (int)sizeof(buf), buf, (int)sizeof(iap_ack), iap_ack); 811 return -EIO; 812 } 813 814 dev_info(&client->dev, "successfully entered IAP mode"); 815 816 send_id = client->addr; 817 error = elants_i2c_send(client, &send_id, 1); 818 if (error) { 819 dev_err(&client->dev, "sending dummy byte failed: %d\n", 820 error); 821 return error; 822 } 823 824 /* Clear the last page of Master */ 825 error = elants_i2c_send(client, fw->data, ELAN_FW_PAGESIZE); 826 if (error) { 827 dev_err(&client->dev, "clearing of the last page failed: %d\n", 828 error); 829 return error; 830 } 831 832 error = elants_i2c_read(client, buf, 2); 833 if (error) { 834 dev_err(&client->dev, 835 "failed to read ACK for clearing the last page: %d\n", 836 error); 837 return error; 838 } 839 840 n_fw_pages = fw->size / ELAN_FW_PAGESIZE; 841 dev_dbg(&client->dev, "IAP Pages = %d\n", n_fw_pages); 842 843 for (page = 0; page < n_fw_pages; page++) { 844 error = elants_i2c_fw_write_page(client, 845 fw->data + page * ELAN_FW_PAGESIZE); 846 if (error) { 847 dev_err(&client->dev, 848 "failed to write FW page %d: %d\n", 849 page, error); 850 return error; 851 } 852 } 853 854 /* Old iap needs to wait 200ms for WDT and rest is for hello packets */ 855 msleep(300); 856 857 dev_info(&client->dev, "firmware update completed\n"); 858 return 0; 859 } 860 861 static int elants_i2c_fw_update(struct elants_data *ts) 862 { 863 struct i2c_client *client = ts->client; 864 const struct firmware *fw; 865 char *fw_name; 866 int error; 867 868 fw_name = kasprintf(GFP_KERNEL, "elants_i2c_%04x.bin", ts->hw_version); 869 if (!fw_name) 870 return -ENOMEM; 871 872 dev_info(&client->dev, "requesting fw name = %s\n", fw_name); 873 error = request_firmware(&fw, fw_name, &client->dev); 874 kfree(fw_name); 875 if (error) { 876 dev_err(&client->dev, "failed to request firmware: %d\n", 877 error); 878 return error; 879 } 880 881 if (fw->size % ELAN_FW_PAGESIZE) { 882 dev_err(&client->dev, "invalid firmware length: %zu\n", 883 fw->size); 884 error = -EINVAL; 885 goto out; 886 } 887 888 disable_irq(client->irq); 889 890 error = elants_i2c_do_update_firmware(client, fw, 891 ts->iap_mode == ELAN_IAP_RECOVERY); 892 if (error) { 893 dev_err(&client->dev, "firmware update failed: %d\n", error); 894 ts->iap_mode = ELAN_IAP_RECOVERY; 895 goto out_enable_irq; 896 } 897 898 error = elants_i2c_initialize(ts); 899 if (error) { 900 dev_err(&client->dev, 901 "failed to initialize device after firmware update: %d\n", 902 error); 903 ts->iap_mode = ELAN_IAP_RECOVERY; 904 goto out_enable_irq; 905 } 906 907 ts->iap_mode = ELAN_IAP_OPERATIONAL; 908 909 out_enable_irq: 910 ts->state = ELAN_STATE_NORMAL; 911 enable_irq(client->irq); 912 msleep(100); 913 914 if (!error) 915 elants_i2c_calibrate(ts); 916 out: 917 release_firmware(fw); 918 return error; 919 } 920 921 /* 922 * Event reporting. 923 */ 924 925 static void elants_i2c_mt_event(struct elants_data *ts, u8 *buf, 926 size_t packet_size) 927 { 928 struct input_dev *input = ts->input; 929 unsigned int n_fingers; 930 unsigned int tool_type; 931 u16 finger_state; 932 int i; 933 934 n_fingers = buf[FW_POS_STATE + 1] & 0x0f; 935 finger_state = ((buf[FW_POS_STATE + 1] & 0x30) << 4) | 936 buf[FW_POS_STATE]; 937 938 dev_dbg(&ts->client->dev, 939 "n_fingers: %u, state: %04x\n", n_fingers, finger_state); 940 941 /* Note: all fingers have the same tool type */ 942 tool_type = buf[FW_POS_TOOL_TYPE] & BIT(0) ? 943 MT_TOOL_FINGER : MT_TOOL_PALM; 944 945 for (i = 0; i < MAX_CONTACT_NUM && n_fingers; i++) { 946 if (finger_state & 1) { 947 unsigned int x, y, p, w; 948 u8 *pos; 949 950 pos = &buf[FW_POS_XY + i * 3]; 951 x = (((u16)pos[0] & 0xf0) << 4) | pos[1]; 952 y = (((u16)pos[0] & 0x0f) << 8) | pos[2]; 953 954 /* 955 * eKTF3624 may have use "old" touch-report format, 956 * depending on a device and TS firmware version. 957 * For example, ASUS Transformer devices use the "old" 958 * format, while ASUS Nexus 7 uses the "new" formant. 959 */ 960 if (packet_size == PACKET_SIZE_OLD && 961 ts->chip_id == EKTF3624) { 962 w = buf[FW_POS_WIDTH + i / 2]; 963 w >>= 4 * (~i & 1); 964 w |= w << 4; 965 w |= !w; 966 p = w; 967 } else { 968 p = buf[FW_POS_PRESSURE + i]; 969 w = buf[FW_POS_WIDTH + i]; 970 } 971 972 dev_dbg(&ts->client->dev, "i=%d x=%d y=%d p=%d w=%d\n", 973 i, x, y, p, w); 974 975 input_mt_slot(input, i); 976 input_mt_report_slot_state(input, tool_type, true); 977 touchscreen_report_pos(input, &ts->prop, x, y, true); 978 input_event(input, EV_ABS, ABS_MT_PRESSURE, p); 979 input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, w); 980 981 n_fingers--; 982 } 983 984 finger_state >>= 1; 985 } 986 987 input_mt_sync_frame(input); 988 input_sync(input); 989 } 990 991 static u8 elants_i2c_calculate_checksum(u8 *buf) 992 { 993 u8 checksum = 0; 994 u8 i; 995 996 for (i = 0; i < FW_POS_CHECKSUM; i++) 997 checksum += buf[i]; 998 999 return checksum; 1000 } 1001 1002 static void elants_i2c_event(struct elants_data *ts, u8 *buf, 1003 size_t packet_size) 1004 { 1005 u8 checksum = elants_i2c_calculate_checksum(buf); 1006 1007 if (unlikely(buf[FW_POS_CHECKSUM] != checksum)) 1008 dev_warn(&ts->client->dev, 1009 "%s: invalid checksum for packet %02x: %02x vs. %02x\n", 1010 __func__, buf[FW_POS_HEADER], 1011 checksum, buf[FW_POS_CHECKSUM]); 1012 else if (unlikely(buf[FW_POS_HEADER] != HEADER_REPORT_10_FINGER)) 1013 dev_warn(&ts->client->dev, 1014 "%s: unknown packet type: %02x\n", 1015 __func__, buf[FW_POS_HEADER]); 1016 else 1017 elants_i2c_mt_event(ts, buf, packet_size); 1018 } 1019 1020 static irqreturn_t elants_i2c_irq(int irq, void *_dev) 1021 { 1022 const u8 wait_packet[] = { 0x64, 0x64, 0x64, 0x64 }; 1023 struct elants_data *ts = _dev; 1024 struct i2c_client *client = ts->client; 1025 int report_count, report_len; 1026 int i; 1027 int len; 1028 1029 len = i2c_master_recv_dmasafe(client, ts->buf, sizeof(ts->buf)); 1030 if (len < 0) { 1031 dev_err(&client->dev, "%s: failed to read data: %d\n", 1032 __func__, len); 1033 goto out; 1034 } 1035 1036 dev_dbg(&client->dev, "%s: packet %*ph\n", 1037 __func__, HEADER_SIZE, ts->buf); 1038 1039 switch (ts->state) { 1040 case ELAN_WAIT_RECALIBRATION: 1041 if (ts->buf[FW_HDR_TYPE] == CMD_HEADER_REK) { 1042 memcpy(ts->cmd_resp, ts->buf, sizeof(ts->cmd_resp)); 1043 complete(&ts->cmd_done); 1044 ts->state = ELAN_STATE_NORMAL; 1045 } 1046 break; 1047 1048 case ELAN_WAIT_QUEUE_HEADER: 1049 if (ts->buf[FW_HDR_TYPE] != QUEUE_HEADER_NORMAL) 1050 break; 1051 1052 ts->state = ELAN_STATE_NORMAL; 1053 fallthrough; 1054 1055 case ELAN_STATE_NORMAL: 1056 1057 switch (ts->buf[FW_HDR_TYPE]) { 1058 case CMD_HEADER_HELLO: 1059 case CMD_HEADER_RESP: 1060 break; 1061 1062 case QUEUE_HEADER_WAIT: 1063 if (memcmp(ts->buf, wait_packet, sizeof(wait_packet))) { 1064 dev_err(&client->dev, 1065 "invalid wait packet %*ph\n", 1066 HEADER_SIZE, ts->buf); 1067 } else { 1068 ts->state = ELAN_WAIT_QUEUE_HEADER; 1069 udelay(30); 1070 } 1071 break; 1072 1073 case QUEUE_HEADER_SINGLE: 1074 elants_i2c_event(ts, &ts->buf[HEADER_SIZE], 1075 ts->buf[FW_HDR_LENGTH]); 1076 break; 1077 1078 case QUEUE_HEADER_NORMAL2: /* CMD_HEADER_REK */ 1079 /* 1080 * Depending on firmware version, eKTF3624 touchscreens 1081 * may utilize one of these opcodes for the touch events: 1082 * 0x63 (NORMAL) and 0x66 (NORMAL2). The 0x63 is used by 1083 * older firmware version and differs from 0x66 such that 1084 * touch pressure value needs to be adjusted. The 0x66 1085 * opcode of newer firmware is equal to 0x63 of eKTH3500. 1086 */ 1087 if (ts->chip_id != EKTF3624) 1088 break; 1089 1090 fallthrough; 1091 1092 case QUEUE_HEADER_NORMAL: 1093 report_count = ts->buf[FW_HDR_COUNT]; 1094 if (report_count == 0 || report_count > 3) { 1095 dev_err(&client->dev, 1096 "bad report count: %*ph\n", 1097 HEADER_SIZE, ts->buf); 1098 break; 1099 } 1100 1101 report_len = ts->buf[FW_HDR_LENGTH] / report_count; 1102 1103 if (report_len == PACKET_SIZE_OLD && 1104 ts->chip_id == EKTF3624) { 1105 dev_dbg_once(&client->dev, 1106 "using old report format\n"); 1107 } else if (report_len != PACKET_SIZE) { 1108 dev_err(&client->dev, 1109 "mismatching report length: %*ph\n", 1110 HEADER_SIZE, ts->buf); 1111 break; 1112 } 1113 1114 for (i = 0; i < report_count; i++) { 1115 u8 *buf = ts->buf + HEADER_SIZE + 1116 i * report_len; 1117 elants_i2c_event(ts, buf, report_len); 1118 } 1119 break; 1120 1121 default: 1122 dev_err(&client->dev, "unknown packet %*ph\n", 1123 HEADER_SIZE, ts->buf); 1124 break; 1125 } 1126 break; 1127 } 1128 1129 out: 1130 return IRQ_HANDLED; 1131 } 1132 1133 /* 1134 * sysfs interface 1135 */ 1136 static ssize_t calibrate_store(struct device *dev, 1137 struct device_attribute *attr, 1138 const char *buf, size_t count) 1139 { 1140 struct i2c_client *client = to_i2c_client(dev); 1141 struct elants_data *ts = i2c_get_clientdata(client); 1142 int error; 1143 1144 error = mutex_lock_interruptible(&ts->sysfs_mutex); 1145 if (error) 1146 return error; 1147 1148 error = elants_i2c_calibrate(ts); 1149 1150 mutex_unlock(&ts->sysfs_mutex); 1151 return error ?: count; 1152 } 1153 1154 static ssize_t write_update_fw(struct device *dev, 1155 struct device_attribute *attr, 1156 const char *buf, size_t count) 1157 { 1158 struct i2c_client *client = to_i2c_client(dev); 1159 struct elants_data *ts = i2c_get_clientdata(client); 1160 int error; 1161 1162 error = mutex_lock_interruptible(&ts->sysfs_mutex); 1163 if (error) 1164 return error; 1165 1166 error = elants_i2c_fw_update(ts); 1167 dev_dbg(dev, "firmware update result: %d\n", error); 1168 1169 mutex_unlock(&ts->sysfs_mutex); 1170 return error ?: count; 1171 } 1172 1173 static ssize_t show_iap_mode(struct device *dev, 1174 struct device_attribute *attr, char *buf) 1175 { 1176 struct i2c_client *client = to_i2c_client(dev); 1177 struct elants_data *ts = i2c_get_clientdata(client); 1178 1179 return sprintf(buf, "%s\n", 1180 ts->iap_mode == ELAN_IAP_OPERATIONAL ? 1181 "Normal" : "Recovery"); 1182 } 1183 1184 static ssize_t show_calibration_count(struct device *dev, 1185 struct device_attribute *attr, char *buf) 1186 { 1187 struct i2c_client *client = to_i2c_client(dev); 1188 const u8 cmd[] = { CMD_HEADER_READ, E_ELAN_INFO_REK, 0x00, 0x01 }; 1189 u8 resp[HEADER_SIZE]; 1190 u16 rek_count; 1191 int error; 1192 1193 error = elants_i2c_execute_command(client, cmd, sizeof(cmd), 1194 resp, sizeof(resp), 1, 1195 "read ReK status"); 1196 if (error) 1197 return sprintf(buf, "%d\n", error); 1198 1199 rek_count = get_unaligned_be16(&resp[2]); 1200 return sprintf(buf, "0x%04x\n", rek_count); 1201 } 1202 1203 static DEVICE_ATTR_WO(calibrate); 1204 static DEVICE_ATTR(iap_mode, S_IRUGO, show_iap_mode, NULL); 1205 static DEVICE_ATTR(calibration_count, S_IRUGO, show_calibration_count, NULL); 1206 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, write_update_fw); 1207 1208 struct elants_version_attribute { 1209 struct device_attribute dattr; 1210 size_t field_offset; 1211 size_t field_size; 1212 }; 1213 1214 #define __ELANTS_FIELD_SIZE(_field) \ 1215 sizeof(((struct elants_data *)NULL)->_field) 1216 #define __ELANTS_VERIFY_SIZE(_field) \ 1217 (BUILD_BUG_ON_ZERO(__ELANTS_FIELD_SIZE(_field) > 2) + \ 1218 __ELANTS_FIELD_SIZE(_field)) 1219 #define ELANTS_VERSION_ATTR(_field) \ 1220 struct elants_version_attribute elants_ver_attr_##_field = { \ 1221 .dattr = __ATTR(_field, S_IRUGO, \ 1222 elants_version_attribute_show, NULL), \ 1223 .field_offset = offsetof(struct elants_data, _field), \ 1224 .field_size = __ELANTS_VERIFY_SIZE(_field), \ 1225 } 1226 1227 static ssize_t elants_version_attribute_show(struct device *dev, 1228 struct device_attribute *dattr, 1229 char *buf) 1230 { 1231 struct i2c_client *client = to_i2c_client(dev); 1232 struct elants_data *ts = i2c_get_clientdata(client); 1233 struct elants_version_attribute *attr = 1234 container_of(dattr, struct elants_version_attribute, dattr); 1235 u8 *field = (u8 *)((char *)ts + attr->field_offset); 1236 unsigned int fmt_size; 1237 unsigned int val; 1238 1239 if (attr->field_size == 1) { 1240 val = *field; 1241 fmt_size = 2; /* 2 HEX digits */ 1242 } else { 1243 val = *(u16 *)field; 1244 fmt_size = 4; /* 4 HEX digits */ 1245 } 1246 1247 return sprintf(buf, "%0*x\n", fmt_size, val); 1248 } 1249 1250 static ELANTS_VERSION_ATTR(fw_version); 1251 static ELANTS_VERSION_ATTR(hw_version); 1252 static ELANTS_VERSION_ATTR(test_version); 1253 static ELANTS_VERSION_ATTR(solution_version); 1254 static ELANTS_VERSION_ATTR(bc_version); 1255 static ELANTS_VERSION_ATTR(iap_version); 1256 1257 static struct attribute *elants_attributes[] = { 1258 &dev_attr_calibrate.attr, 1259 &dev_attr_update_fw.attr, 1260 &dev_attr_iap_mode.attr, 1261 &dev_attr_calibration_count.attr, 1262 1263 &elants_ver_attr_fw_version.dattr.attr, 1264 &elants_ver_attr_hw_version.dattr.attr, 1265 &elants_ver_attr_test_version.dattr.attr, 1266 &elants_ver_attr_solution_version.dattr.attr, 1267 &elants_ver_attr_bc_version.dattr.attr, 1268 &elants_ver_attr_iap_version.dattr.attr, 1269 NULL 1270 }; 1271 1272 static const struct attribute_group elants_attribute_group = { 1273 .attrs = elants_attributes, 1274 }; 1275 1276 static int elants_i2c_power_on(struct elants_data *ts) 1277 { 1278 int error; 1279 1280 /* 1281 * If we do not have reset gpio assume platform firmware 1282 * controls regulators and does power them on for us. 1283 */ 1284 if (IS_ERR_OR_NULL(ts->reset_gpio)) 1285 return 0; 1286 1287 gpiod_set_value_cansleep(ts->reset_gpio, 1); 1288 1289 error = regulator_enable(ts->vcc33); 1290 if (error) { 1291 dev_err(&ts->client->dev, 1292 "failed to enable vcc33 regulator: %d\n", 1293 error); 1294 goto release_reset_gpio; 1295 } 1296 1297 error = regulator_enable(ts->vccio); 1298 if (error) { 1299 dev_err(&ts->client->dev, 1300 "failed to enable vccio regulator: %d\n", 1301 error); 1302 regulator_disable(ts->vcc33); 1303 goto release_reset_gpio; 1304 } 1305 1306 /* 1307 * We need to wait a bit after powering on controller before 1308 * we are allowed to release reset GPIO. 1309 */ 1310 udelay(ELAN_POWERON_DELAY_USEC); 1311 1312 release_reset_gpio: 1313 gpiod_set_value_cansleep(ts->reset_gpio, 0); 1314 if (error) 1315 return error; 1316 1317 msleep(ELAN_RESET_DELAY_MSEC); 1318 1319 return 0; 1320 } 1321 1322 static void elants_i2c_power_off(void *_data) 1323 { 1324 struct elants_data *ts = _data; 1325 1326 if (!IS_ERR_OR_NULL(ts->reset_gpio)) { 1327 /* 1328 * Activate reset gpio to prevent leakage through the 1329 * pin once we shut off power to the controller. 1330 */ 1331 gpiod_set_value_cansleep(ts->reset_gpio, 1); 1332 regulator_disable(ts->vccio); 1333 regulator_disable(ts->vcc33); 1334 } 1335 } 1336 1337 static int elants_i2c_probe(struct i2c_client *client, 1338 const struct i2c_device_id *id) 1339 { 1340 union i2c_smbus_data dummy; 1341 struct elants_data *ts; 1342 unsigned long irqflags; 1343 int error; 1344 1345 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1346 dev_err(&client->dev, 1347 "%s: i2c check functionality error\n", DEVICE_NAME); 1348 return -ENXIO; 1349 } 1350 1351 ts = devm_kzalloc(&client->dev, sizeof(struct elants_data), GFP_KERNEL); 1352 if (!ts) 1353 return -ENOMEM; 1354 1355 mutex_init(&ts->sysfs_mutex); 1356 init_completion(&ts->cmd_done); 1357 1358 ts->client = client; 1359 ts->chip_id = (enum elants_chip_id)id->driver_data; 1360 i2c_set_clientdata(client, ts); 1361 1362 ts->vcc33 = devm_regulator_get(&client->dev, "vcc33"); 1363 if (IS_ERR(ts->vcc33)) { 1364 error = PTR_ERR(ts->vcc33); 1365 if (error != -EPROBE_DEFER) 1366 dev_err(&client->dev, 1367 "Failed to get 'vcc33' regulator: %d\n", 1368 error); 1369 return error; 1370 } 1371 1372 ts->vccio = devm_regulator_get(&client->dev, "vccio"); 1373 if (IS_ERR(ts->vccio)) { 1374 error = PTR_ERR(ts->vccio); 1375 if (error != -EPROBE_DEFER) 1376 dev_err(&client->dev, 1377 "Failed to get 'vccio' regulator: %d\n", 1378 error); 1379 return error; 1380 } 1381 1382 ts->reset_gpio = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_LOW); 1383 if (IS_ERR(ts->reset_gpio)) { 1384 error = PTR_ERR(ts->reset_gpio); 1385 1386 if (error == -EPROBE_DEFER) 1387 return error; 1388 1389 if (error != -ENOENT && error != -ENOSYS) { 1390 dev_err(&client->dev, 1391 "failed to get reset gpio: %d\n", 1392 error); 1393 return error; 1394 } 1395 1396 ts->keep_power_in_suspend = true; 1397 } 1398 1399 error = elants_i2c_power_on(ts); 1400 if (error) 1401 return error; 1402 1403 error = devm_add_action(&client->dev, elants_i2c_power_off, ts); 1404 if (error) { 1405 dev_err(&client->dev, 1406 "failed to install power off action: %d\n", error); 1407 elants_i2c_power_off(ts); 1408 return error; 1409 } 1410 1411 /* Make sure there is something at this address */ 1412 if (i2c_smbus_xfer(client->adapter, client->addr, 0, 1413 I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &dummy) < 0) { 1414 dev_err(&client->dev, "nothing at this address\n"); 1415 return -ENXIO; 1416 } 1417 1418 error = elants_i2c_initialize(ts); 1419 if (error) { 1420 dev_err(&client->dev, "failed to initialize: %d\n", error); 1421 return error; 1422 } 1423 1424 ts->input = devm_input_allocate_device(&client->dev); 1425 if (!ts->input) { 1426 dev_err(&client->dev, "Failed to allocate input device\n"); 1427 return -ENOMEM; 1428 } 1429 1430 ts->input->name = "Elan Touchscreen"; 1431 ts->input->id.bustype = BUS_I2C; 1432 1433 /* Multitouch input params setup */ 1434 1435 input_set_abs_params(ts->input, ABS_MT_POSITION_X, 0, ts->x_max, 0, 0); 1436 input_set_abs_params(ts->input, ABS_MT_POSITION_Y, 0, ts->y_max, 0, 0); 1437 input_set_abs_params(ts->input, ABS_MT_TOUCH_MAJOR, 0, 255, 0, 0); 1438 input_set_abs_params(ts->input, ABS_MT_PRESSURE, 0, 255, 0, 0); 1439 input_set_abs_params(ts->input, ABS_MT_TOOL_TYPE, 1440 0, MT_TOOL_PALM, 0, 0); 1441 1442 touchscreen_parse_properties(ts->input, true, &ts->prop); 1443 1444 if (ts->chip_id == EKTF3624) { 1445 /* calculate resolution from size */ 1446 ts->x_res = DIV_ROUND_CLOSEST(ts->prop.max_x, ts->phy_x); 1447 ts->y_res = DIV_ROUND_CLOSEST(ts->prop.max_y, ts->phy_y); 1448 } 1449 1450 input_abs_set_res(ts->input, ABS_MT_POSITION_X, ts->x_res); 1451 input_abs_set_res(ts->input, ABS_MT_POSITION_Y, ts->y_res); 1452 if (ts->major_res > 0) 1453 input_abs_set_res(ts->input, ABS_MT_TOUCH_MAJOR, ts->major_res); 1454 1455 error = input_mt_init_slots(ts->input, MAX_CONTACT_NUM, 1456 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED); 1457 if (error) { 1458 dev_err(&client->dev, 1459 "failed to initialize MT slots: %d\n", error); 1460 return error; 1461 } 1462 1463 error = input_register_device(ts->input); 1464 if (error) { 1465 dev_err(&client->dev, 1466 "unable to register input device: %d\n", error); 1467 return error; 1468 } 1469 1470 /* 1471 * Platform code (ACPI, DTS) should normally set up interrupt 1472 * for us, but in case it did not let's fall back to using falling 1473 * edge to be compatible with older Chromebooks. 1474 */ 1475 irqflags = irq_get_trigger_type(client->irq); 1476 if (!irqflags) 1477 irqflags = IRQF_TRIGGER_FALLING; 1478 1479 error = devm_request_threaded_irq(&client->dev, client->irq, 1480 NULL, elants_i2c_irq, 1481 irqflags | IRQF_ONESHOT, 1482 client->name, ts); 1483 if (error) { 1484 dev_err(&client->dev, "Failed to register interrupt\n"); 1485 return error; 1486 } 1487 1488 /* 1489 * Systems using device tree should set up wakeup via DTS, 1490 * the rest will configure device as wakeup source by default. 1491 */ 1492 if (!client->dev.of_node) 1493 device_init_wakeup(&client->dev, true); 1494 1495 error = devm_device_add_group(&client->dev, &elants_attribute_group); 1496 if (error) { 1497 dev_err(&client->dev, "failed to create sysfs attributes: %d\n", 1498 error); 1499 return error; 1500 } 1501 1502 return 0; 1503 } 1504 1505 static int __maybe_unused elants_i2c_suspend(struct device *dev) 1506 { 1507 struct i2c_client *client = to_i2c_client(dev); 1508 struct elants_data *ts = i2c_get_clientdata(client); 1509 const u8 set_sleep_cmd[] = { 1510 CMD_HEADER_WRITE, E_POWER_STATE_SLEEP, 0x00, 0x01 1511 }; 1512 int retry_cnt; 1513 int error; 1514 1515 /* Command not support in IAP recovery mode */ 1516 if (ts->iap_mode != ELAN_IAP_OPERATIONAL) 1517 return -EBUSY; 1518 1519 disable_irq(client->irq); 1520 1521 if (device_may_wakeup(dev)) { 1522 /* 1523 * The device will automatically enter idle mode 1524 * that has reduced power consumption. 1525 */ 1526 ts->wake_irq_enabled = (enable_irq_wake(client->irq) == 0); 1527 } else if (ts->keep_power_in_suspend) { 1528 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { 1529 error = elants_i2c_send(client, set_sleep_cmd, 1530 sizeof(set_sleep_cmd)); 1531 if (!error) 1532 break; 1533 1534 dev_err(&client->dev, 1535 "suspend command failed: %d\n", error); 1536 } 1537 } else { 1538 elants_i2c_power_off(ts); 1539 } 1540 1541 return 0; 1542 } 1543 1544 static int __maybe_unused elants_i2c_resume(struct device *dev) 1545 { 1546 struct i2c_client *client = to_i2c_client(dev); 1547 struct elants_data *ts = i2c_get_clientdata(client); 1548 const u8 set_active_cmd[] = { 1549 CMD_HEADER_WRITE, E_POWER_STATE_RESUME, 0x00, 0x01 1550 }; 1551 int retry_cnt; 1552 int error; 1553 1554 if (device_may_wakeup(dev)) { 1555 if (ts->wake_irq_enabled) 1556 disable_irq_wake(client->irq); 1557 elants_i2c_sw_reset(client); 1558 } else if (ts->keep_power_in_suspend) { 1559 for (retry_cnt = 0; retry_cnt < MAX_RETRIES; retry_cnt++) { 1560 error = elants_i2c_send(client, set_active_cmd, 1561 sizeof(set_active_cmd)); 1562 if (!error) 1563 break; 1564 1565 dev_err(&client->dev, 1566 "resume command failed: %d\n", error); 1567 } 1568 } else { 1569 elants_i2c_power_on(ts); 1570 elants_i2c_initialize(ts); 1571 } 1572 1573 ts->state = ELAN_STATE_NORMAL; 1574 enable_irq(client->irq); 1575 1576 return 0; 1577 } 1578 1579 static SIMPLE_DEV_PM_OPS(elants_i2c_pm_ops, 1580 elants_i2c_suspend, elants_i2c_resume); 1581 1582 static const struct i2c_device_id elants_i2c_id[] = { 1583 { DEVICE_NAME, EKTH3500 }, 1584 { "ekth3500", EKTH3500 }, 1585 { "ektf3624", EKTF3624 }, 1586 { } 1587 }; 1588 MODULE_DEVICE_TABLE(i2c, elants_i2c_id); 1589 1590 #ifdef CONFIG_ACPI 1591 static const struct acpi_device_id elants_acpi_id[] = { 1592 { "ELAN0001", EKTH3500 }, 1593 { } 1594 }; 1595 MODULE_DEVICE_TABLE(acpi, elants_acpi_id); 1596 #endif 1597 1598 #ifdef CONFIG_OF 1599 static const struct of_device_id elants_of_match[] = { 1600 { .compatible = "elan,ekth3500" }, 1601 { .compatible = "elan,ektf3624" }, 1602 { /* sentinel */ } 1603 }; 1604 MODULE_DEVICE_TABLE(of, elants_of_match); 1605 #endif 1606 1607 static struct i2c_driver elants_i2c_driver = { 1608 .probe = elants_i2c_probe, 1609 .id_table = elants_i2c_id, 1610 .driver = { 1611 .name = DEVICE_NAME, 1612 .pm = &elants_i2c_pm_ops, 1613 .acpi_match_table = ACPI_PTR(elants_acpi_id), 1614 .of_match_table = of_match_ptr(elants_of_match), 1615 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1616 }, 1617 }; 1618 module_i2c_driver(elants_i2c_driver); 1619 1620 MODULE_AUTHOR("Scott Liu <scott.liu@emc.com.tw>"); 1621 MODULE_DESCRIPTION("Elan I2c Touchscreen driver"); 1622 MODULE_LICENSE("GPL"); 1623