1 /* 2 * Elan I2C/SMBus Touchpad driver - I2C interface 3 * 4 * Copyright (c) 2013 ELAN Microelectronics Corp. 5 * 6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> 7 * 8 * Based on cyapa driver: 9 * copyright (c) 2011-2012 Cypress Semiconductor, Inc. 10 * copyright (c) 2011-2012 Google, Inc. 11 * 12 * This program is free software; you can redistribute it and/or modify it 13 * under the terms of the GNU General Public License version 2 as published 14 * by the Free Software Foundation. 15 * 16 * Trademarks are the property of their respective owners. 17 */ 18 19 #include <linux/completion.h> 20 #include <linux/delay.h> 21 #include <linux/i2c.h> 22 #include <linux/interrupt.h> 23 #include <linux/jiffies.h> 24 #include <linux/kernel.h> 25 #include <linux/sched.h> 26 #include <asm/unaligned.h> 27 28 #include "elan_i2c.h" 29 30 /* Elan i2c commands */ 31 #define ETP_I2C_RESET 0x0100 32 #define ETP_I2C_WAKE_UP 0x0800 33 #define ETP_I2C_SLEEP 0x0801 34 #define ETP_I2C_DESC_CMD 0x0001 35 #define ETP_I2C_REPORT_DESC_CMD 0x0002 36 #define ETP_I2C_STAND_CMD 0x0005 37 #define ETP_I2C_PATTERN_CMD 0x0100 38 #define ETP_I2C_UNIQUEID_CMD 0x0101 39 #define ETP_I2C_FW_VERSION_CMD 0x0102 40 #define ETP_I2C_IC_TYPE_CMD 0x0103 41 #define ETP_I2C_OSM_VERSION_CMD 0x0103 42 #define ETP_I2C_NSM_VERSION_CMD 0x0104 43 #define ETP_I2C_XY_TRACENUM_CMD 0x0105 44 #define ETP_I2C_MAX_X_AXIS_CMD 0x0106 45 #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107 46 #define ETP_I2C_RESOLUTION_CMD 0x0108 47 #define ETP_I2C_PRESSURE_CMD 0x010A 48 #define ETP_I2C_IAP_VERSION_CMD 0x0110 49 #define ETP_I2C_SET_CMD 0x0300 50 #define ETP_I2C_POWER_CMD 0x0307 51 #define ETP_I2C_FW_CHECKSUM_CMD 0x030F 52 #define ETP_I2C_IAP_CTRL_CMD 0x0310 53 #define ETP_I2C_IAP_CMD 0x0311 54 #define ETP_I2C_IAP_RESET_CMD 0x0314 55 #define ETP_I2C_IAP_CHECKSUM_CMD 0x0315 56 #define ETP_I2C_CALIBRATE_CMD 0x0316 57 #define ETP_I2C_MAX_BASELINE_CMD 0x0317 58 #define ETP_I2C_MIN_BASELINE_CMD 0x0318 59 60 #define ETP_I2C_REPORT_LEN 34 61 #define ETP_I2C_DESC_LENGTH 30 62 #define ETP_I2C_REPORT_DESC_LENGTH 158 63 #define ETP_I2C_INF_LENGTH 2 64 #define ETP_I2C_IAP_PASSWORD 0x1EA5 65 #define ETP_I2C_IAP_RESET 0xF0F0 66 #define ETP_I2C_MAIN_MODE_ON (1 << 9) 67 #define ETP_I2C_IAP_REG_L 0x01 68 #define ETP_I2C_IAP_REG_H 0x06 69 70 static int elan_i2c_read_block(struct i2c_client *client, 71 u16 reg, u8 *val, u16 len) 72 { 73 __le16 buf[] = { 74 cpu_to_le16(reg), 75 }; 76 struct i2c_msg msgs[] = { 77 { 78 .addr = client->addr, 79 .flags = client->flags & I2C_M_TEN, 80 .len = sizeof(buf), 81 .buf = (u8 *)buf, 82 }, 83 { 84 .addr = client->addr, 85 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD, 86 .len = len, 87 .buf = val, 88 } 89 }; 90 int ret; 91 92 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 93 return ret == ARRAY_SIZE(msgs) ? 0 : (ret < 0 ? ret : -EIO); 94 } 95 96 static int elan_i2c_read_cmd(struct i2c_client *client, u16 reg, u8 *val) 97 { 98 int retval; 99 100 retval = elan_i2c_read_block(client, reg, val, ETP_I2C_INF_LENGTH); 101 if (retval < 0) { 102 dev_err(&client->dev, "reading cmd (0x%04x) fail.\n", reg); 103 return retval; 104 } 105 106 return 0; 107 } 108 109 static int elan_i2c_write_cmd(struct i2c_client *client, u16 reg, u16 cmd) 110 { 111 __le16 buf[] = { 112 cpu_to_le16(reg), 113 cpu_to_le16(cmd), 114 }; 115 struct i2c_msg msg = { 116 .addr = client->addr, 117 .flags = client->flags & I2C_M_TEN, 118 .len = sizeof(buf), 119 .buf = (u8 *)buf, 120 }; 121 int ret; 122 123 ret = i2c_transfer(client->adapter, &msg, 1); 124 if (ret != 1) { 125 if (ret >= 0) 126 ret = -EIO; 127 dev_err(&client->dev, "writing cmd (0x%04x) failed: %d\n", 128 reg, ret); 129 return ret; 130 } 131 132 return 0; 133 } 134 135 static int elan_i2c_initialize(struct i2c_client *client) 136 { 137 struct device *dev = &client->dev; 138 int error; 139 u8 val[256]; 140 141 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET); 142 if (error) { 143 dev_err(dev, "device reset failed: %d\n", error); 144 return error; 145 } 146 147 /* Wait for the device to reset */ 148 msleep(100); 149 150 /* get reset acknowledgement 0000 */ 151 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH); 152 if (error < 0) { 153 dev_err(dev, "failed to read reset response: %d\n", error); 154 return error; 155 } 156 157 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD, 158 val, ETP_I2C_DESC_LENGTH); 159 if (error) { 160 dev_err(dev, "cannot get device descriptor: %d\n", error); 161 return error; 162 } 163 164 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD, 165 val, ETP_I2C_REPORT_DESC_LENGTH); 166 if (error) { 167 dev_err(dev, "fetching report descriptor failed.: %d\n", error); 168 return error; 169 } 170 171 return 0; 172 } 173 174 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep) 175 { 176 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, 177 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP); 178 } 179 180 static int elan_i2c_power_control(struct i2c_client *client, bool enable) 181 { 182 u8 val[2]; 183 u16 reg; 184 int error; 185 186 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val); 187 if (error) { 188 dev_err(&client->dev, 189 "failed to read current power state: %d\n", 190 error); 191 return error; 192 } 193 194 reg = le16_to_cpup((__le16 *)val); 195 if (enable) 196 reg &= ~ETP_DISABLE_POWER; 197 else 198 reg |= ETP_DISABLE_POWER; 199 200 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg); 201 if (error) { 202 dev_err(&client->dev, 203 "failed to write current power state: %d\n", 204 error); 205 return error; 206 } 207 208 return 0; 209 } 210 211 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode) 212 { 213 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode); 214 } 215 216 217 static int elan_i2c_calibrate(struct i2c_client *client) 218 { 219 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1); 220 } 221 222 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val) 223 { 224 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1); 225 } 226 227 static int elan_i2c_get_baseline_data(struct i2c_client *client, 228 bool max_baseline, u8 *value) 229 { 230 int error; 231 u8 val[3]; 232 233 error = elan_i2c_read_cmd(client, 234 max_baseline ? ETP_I2C_MAX_BASELINE_CMD : 235 ETP_I2C_MIN_BASELINE_CMD, 236 val); 237 if (error) 238 return error; 239 240 *value = le16_to_cpup((__le16 *)val); 241 242 return 0; 243 } 244 245 static int elan_i2c_get_pattern(struct i2c_client *client, u8 *pattern) 246 { 247 int error; 248 u8 val[3]; 249 250 error = elan_i2c_read_cmd(client, ETP_I2C_PATTERN_CMD, val); 251 if (error) { 252 dev_err(&client->dev, "failed to get pattern: %d\n", error); 253 return error; 254 } 255 *pattern = val[1]; 256 257 return 0; 258 } 259 260 static int elan_i2c_get_version(struct i2c_client *client, 261 bool iap, u8 *version) 262 { 263 int error; 264 u8 pattern_ver; 265 u8 val[3]; 266 267 error = elan_i2c_get_pattern(client, &pattern_ver); 268 if (error) { 269 dev_err(&client->dev, "failed to get pattern version\n"); 270 return error; 271 } 272 273 error = elan_i2c_read_cmd(client, 274 iap ? ETP_I2C_IAP_VERSION_CMD : 275 ETP_I2C_FW_VERSION_CMD, 276 val); 277 if (error) { 278 dev_err(&client->dev, "failed to get %s version: %d\n", 279 iap ? "IAP" : "FW", error); 280 return error; 281 } 282 283 if (pattern_ver == 0x01) 284 *version = iap ? val[1] : val[0]; 285 else 286 *version = val[0]; 287 return 0; 288 } 289 290 static int elan_i2c_get_sm_version(struct i2c_client *client, 291 u16 *ic_type, u8 *version, 292 u8 *clickpad) 293 { 294 int error; 295 u8 pattern_ver; 296 u8 val[3]; 297 298 error = elan_i2c_get_pattern(client, &pattern_ver); 299 if (error) { 300 dev_err(&client->dev, "failed to get pattern version\n"); 301 return error; 302 } 303 304 if (pattern_ver == 0x01) { 305 error = elan_i2c_read_cmd(client, ETP_I2C_IC_TYPE_CMD, val); 306 if (error) { 307 dev_err(&client->dev, "failed to get ic type: %d\n", 308 error); 309 return error; 310 } 311 *ic_type = be16_to_cpup((__be16 *)val); 312 313 error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD, 314 val); 315 if (error) { 316 dev_err(&client->dev, "failed to get SM version: %d\n", 317 error); 318 return error; 319 } 320 *version = val[1]; 321 *clickpad = val[0] & 0x10; 322 } else { 323 error = elan_i2c_read_cmd(client, ETP_I2C_OSM_VERSION_CMD, val); 324 if (error) { 325 dev_err(&client->dev, "failed to get SM version: %d\n", 326 error); 327 return error; 328 } 329 *version = val[0]; 330 *ic_type = val[1]; 331 332 error = elan_i2c_read_cmd(client, ETP_I2C_NSM_VERSION_CMD, 333 val); 334 if (error) { 335 dev_err(&client->dev, "failed to get SM version: %d\n", 336 error); 337 return error; 338 } 339 *clickpad = val[0] & 0x10; 340 } 341 342 return 0; 343 } 344 345 static int elan_i2c_get_product_id(struct i2c_client *client, u16 *id) 346 { 347 int error; 348 u8 val[3]; 349 350 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val); 351 if (error) { 352 dev_err(&client->dev, "failed to get product ID: %d\n", error); 353 return error; 354 } 355 356 *id = le16_to_cpup((__le16 *)val); 357 return 0; 358 } 359 360 static int elan_i2c_get_checksum(struct i2c_client *client, 361 bool iap, u16 *csum) 362 { 363 int error; 364 u8 val[3]; 365 366 error = elan_i2c_read_cmd(client, 367 iap ? ETP_I2C_IAP_CHECKSUM_CMD : 368 ETP_I2C_FW_CHECKSUM_CMD, 369 val); 370 if (error) { 371 dev_err(&client->dev, "failed to get %s checksum: %d\n", 372 iap ? "IAP" : "FW", error); 373 return error; 374 } 375 376 *csum = le16_to_cpup((__le16 *)val); 377 return 0; 378 } 379 380 static int elan_i2c_get_max(struct i2c_client *client, 381 unsigned int *max_x, unsigned int *max_y) 382 { 383 int error; 384 u8 val[3]; 385 386 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val); 387 if (error) { 388 dev_err(&client->dev, "failed to get X dimension: %d\n", error); 389 return error; 390 } 391 392 *max_x = le16_to_cpup((__le16 *)val) & 0x0fff; 393 394 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val); 395 if (error) { 396 dev_err(&client->dev, "failed to get Y dimension: %d\n", error); 397 return error; 398 } 399 400 *max_y = le16_to_cpup((__le16 *)val) & 0x0fff; 401 402 return 0; 403 } 404 405 static int elan_i2c_get_resolution(struct i2c_client *client, 406 u8 *hw_res_x, u8 *hw_res_y) 407 { 408 int error; 409 u8 val[3]; 410 411 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val); 412 if (error) { 413 dev_err(&client->dev, "failed to get resolution: %d\n", error); 414 return error; 415 } 416 417 *hw_res_x = val[0]; 418 *hw_res_y = val[1]; 419 420 return 0; 421 } 422 423 static int elan_i2c_get_num_traces(struct i2c_client *client, 424 unsigned int *x_traces, 425 unsigned int *y_traces) 426 { 427 int error; 428 u8 val[3]; 429 430 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val); 431 if (error) { 432 dev_err(&client->dev, "failed to get trace info: %d\n", error); 433 return error; 434 } 435 436 *x_traces = val[0]; 437 *y_traces = val[1]; 438 439 return 0; 440 } 441 442 static int elan_i2c_get_pressure_adjustment(struct i2c_client *client, 443 int *adjustment) 444 { 445 int error; 446 u8 val[3]; 447 448 error = elan_i2c_read_cmd(client, ETP_I2C_PRESSURE_CMD, val); 449 if (error) { 450 dev_err(&client->dev, "failed to get pressure format: %d\n", 451 error); 452 return error; 453 } 454 455 if ((val[0] >> 4) & 0x1) 456 *adjustment = 0; 457 else 458 *adjustment = ETP_PRESSURE_OFFSET; 459 460 return 0; 461 } 462 463 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode) 464 { 465 int error; 466 u16 constant; 467 u8 val[3]; 468 469 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val); 470 if (error) { 471 dev_err(&client->dev, 472 "failed to read iap control register: %d\n", 473 error); 474 return error; 475 } 476 477 constant = le16_to_cpup((__le16 *)val); 478 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant); 479 480 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE; 481 482 return 0; 483 } 484 485 static int elan_i2c_iap_reset(struct i2c_client *client) 486 { 487 int error; 488 489 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD, 490 ETP_I2C_IAP_RESET); 491 if (error) { 492 dev_err(&client->dev, "cannot reset IC: %d\n", error); 493 return error; 494 } 495 496 return 0; 497 } 498 499 static int elan_i2c_set_flash_key(struct i2c_client *client) 500 { 501 int error; 502 503 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD, 504 ETP_I2C_IAP_PASSWORD); 505 if (error) { 506 dev_err(&client->dev, "cannot set flash key: %d\n", error); 507 return error; 508 } 509 510 return 0; 511 } 512 513 static int elan_i2c_prepare_fw_update(struct i2c_client *client) 514 { 515 struct device *dev = &client->dev; 516 int error; 517 enum tp_mode mode; 518 u8 val[3]; 519 u16 password; 520 521 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */ 522 error = elan_i2c_iap_get_mode(client, &mode); 523 if (error) 524 return error; 525 526 if (mode == IAP_MODE) { 527 /* Reset IC */ 528 error = elan_i2c_iap_reset(client); 529 if (error) 530 return error; 531 532 msleep(30); 533 } 534 535 /* Set flash key*/ 536 error = elan_i2c_set_flash_key(client); 537 if (error) 538 return error; 539 540 /* Wait for F/W IAP initialization */ 541 msleep(mode == MAIN_MODE ? 100 : 30); 542 543 /* Check if we are in IAP mode or not */ 544 error = elan_i2c_iap_get_mode(client, &mode); 545 if (error) 546 return error; 547 548 if (mode == MAIN_MODE) { 549 dev_err(dev, "wrong mode: %d\n", mode); 550 return -EIO; 551 } 552 553 /* Set flash key again */ 554 error = elan_i2c_set_flash_key(client); 555 if (error) 556 return error; 557 558 /* Wait for F/W IAP initialization */ 559 msleep(30); 560 561 /* read back to check we actually enabled successfully. */ 562 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val); 563 if (error) { 564 dev_err(dev, "cannot read iap password: %d\n", 565 error); 566 return error; 567 } 568 569 password = le16_to_cpup((__le16 *)val); 570 if (password != ETP_I2C_IAP_PASSWORD) { 571 dev_err(dev, "wrong iap password: 0x%X\n", password); 572 return -EIO; 573 } 574 575 return 0; 576 } 577 578 static int elan_i2c_write_fw_block(struct i2c_client *client, 579 const u8 *page, u16 checksum, int idx) 580 { 581 struct device *dev = &client->dev; 582 u8 page_store[ETP_FW_PAGE_SIZE + 4]; 583 u8 val[3]; 584 u16 result; 585 int ret, error; 586 587 page_store[0] = ETP_I2C_IAP_REG_L; 588 page_store[1] = ETP_I2C_IAP_REG_H; 589 memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE); 590 /* recode checksum at last two bytes */ 591 put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]); 592 593 ret = i2c_master_send(client, page_store, sizeof(page_store)); 594 if (ret != sizeof(page_store)) { 595 error = ret < 0 ? ret : -EIO; 596 dev_err(dev, "Failed to write page %d: %d\n", idx, error); 597 return error; 598 } 599 600 /* Wait for F/W to update one page ROM data. */ 601 msleep(35); 602 603 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val); 604 if (error) { 605 dev_err(dev, "Failed to read IAP write result: %d\n", error); 606 return error; 607 } 608 609 result = le16_to_cpup((__le16 *)val); 610 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) { 611 dev_err(dev, "IAP reports failed write: %04hx\n", 612 result); 613 return -EIO; 614 } 615 616 return 0; 617 } 618 619 static int elan_i2c_finish_fw_update(struct i2c_client *client, 620 struct completion *completion) 621 { 622 struct device *dev = &client->dev; 623 int error; 624 int len; 625 u8 buffer[ETP_I2C_REPORT_LEN]; 626 627 len = i2c_master_recv(client, buffer, ETP_I2C_REPORT_LEN); 628 if (len != ETP_I2C_REPORT_LEN) { 629 error = len < 0 ? len : -EIO; 630 dev_warn(dev, "failed to read I2C data after FW WDT reset: %d (%d)\n", 631 error, len); 632 } 633 634 reinit_completion(completion); 635 enable_irq(client->irq); 636 637 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET); 638 if (error) { 639 dev_err(dev, "device reset failed: %d\n", error); 640 } else if (!wait_for_completion_timeout(completion, 641 msecs_to_jiffies(300))) { 642 dev_err(dev, "timeout waiting for device reset\n"); 643 error = -ETIMEDOUT; 644 } 645 646 disable_irq(client->irq); 647 648 if (error) 649 return error; 650 651 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH); 652 if (len != ETP_I2C_INF_LENGTH) { 653 error = len < 0 ? len : -EIO; 654 dev_err(dev, "failed to read INT signal: %d (%d)\n", 655 error, len); 656 return error; 657 } 658 659 return 0; 660 } 661 662 static int elan_i2c_get_report(struct i2c_client *client, u8 *report) 663 { 664 int len; 665 666 len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN); 667 if (len < 0) { 668 dev_err(&client->dev, "failed to read report data: %d\n", len); 669 return len; 670 } 671 672 if (len != ETP_I2C_REPORT_LEN) { 673 dev_err(&client->dev, 674 "wrong report length (%d vs %d expected)\n", 675 len, ETP_I2C_REPORT_LEN); 676 return -EIO; 677 } 678 679 return 0; 680 } 681 682 const struct elan_transport_ops elan_i2c_ops = { 683 .initialize = elan_i2c_initialize, 684 .sleep_control = elan_i2c_sleep_control, 685 .power_control = elan_i2c_power_control, 686 .set_mode = elan_i2c_set_mode, 687 688 .calibrate = elan_i2c_calibrate, 689 .calibrate_result = elan_i2c_calibrate_result, 690 691 .get_baseline_data = elan_i2c_get_baseline_data, 692 693 .get_version = elan_i2c_get_version, 694 .get_sm_version = elan_i2c_get_sm_version, 695 .get_product_id = elan_i2c_get_product_id, 696 .get_checksum = elan_i2c_get_checksum, 697 .get_pressure_adjustment = elan_i2c_get_pressure_adjustment, 698 699 .get_max = elan_i2c_get_max, 700 .get_resolution = elan_i2c_get_resolution, 701 .get_num_traces = elan_i2c_get_num_traces, 702 703 .iap_get_mode = elan_i2c_iap_get_mode, 704 .iap_reset = elan_i2c_iap_reset, 705 706 .prepare_fw_update = elan_i2c_prepare_fw_update, 707 .write_fw_block = elan_i2c_write_fw_block, 708 .finish_fw_update = elan_i2c_finish_fw_update, 709 710 .get_pattern = elan_i2c_get_pattern, 711 712 .get_report = elan_i2c_get_report, 713 }; 714