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