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