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 * Version: 1.5.5 8 * 9 * Based on cyapa driver: 10 * copyright (c) 2011-2012 Cypress Semiconductor, Inc. 11 * copyright (c) 2011-2012 Google, Inc. 12 * 13 * This program is free software; you can redistribute it and/or modify it 14 * under the terms of the GNU General Public License version 2 as published 15 * by the Free Software Foundation. 16 * 17 * Trademarks are the property of their respective owners. 18 */ 19 20 #include <linux/completion.h> 21 #include <linux/delay.h> 22 #include <linux/i2c.h> 23 #include <linux/interrupt.h> 24 #include <linux/jiffies.h> 25 #include <linux/kernel.h> 26 #include <linux/sched.h> 27 #include <asm/unaligned.h> 28 29 #include "elan_i2c.h" 30 31 /* Elan i2c commands */ 32 #define ETP_I2C_RESET 0x0100 33 #define ETP_I2C_WAKE_UP 0x0800 34 #define ETP_I2C_SLEEP 0x0801 35 #define ETP_I2C_DESC_CMD 0x0001 36 #define ETP_I2C_REPORT_DESC_CMD 0x0002 37 #define ETP_I2C_STAND_CMD 0x0005 38 #define ETP_I2C_UNIQUEID_CMD 0x0101 39 #define ETP_I2C_FW_VERSION_CMD 0x0102 40 #define ETP_I2C_SM_VERSION_CMD 0x0103 41 #define ETP_I2C_XY_TRACENUM_CMD 0x0105 42 #define ETP_I2C_MAX_X_AXIS_CMD 0x0106 43 #define ETP_I2C_MAX_Y_AXIS_CMD 0x0107 44 #define ETP_I2C_RESOLUTION_CMD 0x0108 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 return ret == 1 ? 0 : (ret < 0 ? ret : -EIO); 122 } 123 124 static int elan_i2c_initialize(struct i2c_client *client) 125 { 126 struct device *dev = &client->dev; 127 int error; 128 u8 val[256]; 129 130 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET); 131 if (error) { 132 dev_err(dev, "device reset failed: %d\n", error); 133 return error; 134 } 135 136 /* Wait for the device to reset */ 137 msleep(100); 138 139 /* get reset acknowledgement 0000 */ 140 error = i2c_master_recv(client, val, ETP_I2C_INF_LENGTH); 141 if (error < 0) { 142 dev_err(dev, "failed to read reset response: %d\n", error); 143 return error; 144 } 145 146 error = elan_i2c_read_block(client, ETP_I2C_DESC_CMD, 147 val, ETP_I2C_DESC_LENGTH); 148 if (error) { 149 dev_err(dev, "cannot get device descriptor: %d\n", error); 150 return error; 151 } 152 153 error = elan_i2c_read_block(client, ETP_I2C_REPORT_DESC_CMD, 154 val, ETP_I2C_REPORT_DESC_LENGTH); 155 if (error) { 156 dev_err(dev, "fetching report descriptor failed.: %d\n", error); 157 return error; 158 } 159 160 return 0; 161 } 162 163 static int elan_i2c_sleep_control(struct i2c_client *client, bool sleep) 164 { 165 return elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, 166 sleep ? ETP_I2C_SLEEP : ETP_I2C_WAKE_UP); 167 } 168 169 static int elan_i2c_power_control(struct i2c_client *client, bool enable) 170 { 171 u8 val[2]; 172 u16 reg; 173 int error; 174 175 error = elan_i2c_read_cmd(client, ETP_I2C_POWER_CMD, val); 176 if (error) { 177 dev_err(&client->dev, 178 "failed to read current power state: %d\n", 179 error); 180 return error; 181 } 182 183 reg = le16_to_cpup((__le16 *)val); 184 if (enable) 185 reg &= ~ETP_DISABLE_POWER; 186 else 187 reg |= ETP_DISABLE_POWER; 188 189 error = elan_i2c_write_cmd(client, ETP_I2C_POWER_CMD, reg); 190 if (error) { 191 dev_err(&client->dev, 192 "failed to write current power state: %d\n", 193 error); 194 return error; 195 } 196 197 return 0; 198 } 199 200 static int elan_i2c_set_mode(struct i2c_client *client, u8 mode) 201 { 202 return elan_i2c_write_cmd(client, ETP_I2C_SET_CMD, mode); 203 } 204 205 206 static int elan_i2c_calibrate(struct i2c_client *client) 207 { 208 return elan_i2c_write_cmd(client, ETP_I2C_CALIBRATE_CMD, 1); 209 } 210 211 static int elan_i2c_calibrate_result(struct i2c_client *client, u8 *val) 212 { 213 return elan_i2c_read_block(client, ETP_I2C_CALIBRATE_CMD, val, 1); 214 } 215 216 static int elan_i2c_get_baseline_data(struct i2c_client *client, 217 bool max_baseline, u8 *value) 218 { 219 int error; 220 u8 val[3]; 221 222 error = elan_i2c_read_cmd(client, 223 max_baseline ? ETP_I2C_MAX_BASELINE_CMD : 224 ETP_I2C_MIN_BASELINE_CMD, 225 val); 226 if (error) 227 return error; 228 229 *value = le16_to_cpup((__le16 *)val); 230 231 return 0; 232 } 233 234 static int elan_i2c_get_version(struct i2c_client *client, 235 bool iap, u8 *version) 236 { 237 int error; 238 u8 val[3]; 239 240 error = elan_i2c_read_cmd(client, 241 iap ? ETP_I2C_IAP_VERSION_CMD : 242 ETP_I2C_FW_VERSION_CMD, 243 val); 244 if (error) { 245 dev_err(&client->dev, "failed to get %s version: %d\n", 246 iap ? "IAP" : "FW", error); 247 return error; 248 } 249 250 *version = val[0]; 251 return 0; 252 } 253 254 static int elan_i2c_get_sm_version(struct i2c_client *client, u8 *version) 255 { 256 int error; 257 u8 val[3]; 258 259 error = elan_i2c_read_cmd(client, ETP_I2C_SM_VERSION_CMD, val); 260 if (error) { 261 dev_err(&client->dev, "failed to get SM version: %d\n", error); 262 return error; 263 } 264 265 *version = val[0]; 266 return 0; 267 } 268 269 static int elan_i2c_get_product_id(struct i2c_client *client, u8 *id) 270 { 271 int error; 272 u8 val[3]; 273 274 error = elan_i2c_read_cmd(client, ETP_I2C_UNIQUEID_CMD, val); 275 if (error) { 276 dev_err(&client->dev, "failed to get product ID: %d\n", error); 277 return error; 278 } 279 280 *id = val[0]; 281 return 0; 282 } 283 284 static int elan_i2c_get_checksum(struct i2c_client *client, 285 bool iap, u16 *csum) 286 { 287 int error; 288 u8 val[3]; 289 290 error = elan_i2c_read_cmd(client, 291 iap ? ETP_I2C_IAP_CHECKSUM_CMD : 292 ETP_I2C_FW_CHECKSUM_CMD, 293 val); 294 if (error) { 295 dev_err(&client->dev, "failed to get %s checksum: %d\n", 296 iap ? "IAP" : "FW", error); 297 return error; 298 } 299 300 *csum = le16_to_cpup((__le16 *)val); 301 return 0; 302 } 303 304 static int elan_i2c_get_max(struct i2c_client *client, 305 unsigned int *max_x, unsigned int *max_y) 306 { 307 int error; 308 u8 val[3]; 309 310 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_X_AXIS_CMD, val); 311 if (error) { 312 dev_err(&client->dev, "failed to get X dimension: %d\n", error); 313 return error; 314 } 315 316 *max_x = le16_to_cpup((__le16 *)val) & 0x0fff; 317 318 error = elan_i2c_read_cmd(client, ETP_I2C_MAX_Y_AXIS_CMD, val); 319 if (error) { 320 dev_err(&client->dev, "failed to get Y dimension: %d\n", error); 321 return error; 322 } 323 324 *max_y = le16_to_cpup((__le16 *)val) & 0x0fff; 325 326 return 0; 327 } 328 329 static int elan_i2c_get_resolution(struct i2c_client *client, 330 u8 *hw_res_x, u8 *hw_res_y) 331 { 332 int error; 333 u8 val[3]; 334 335 error = elan_i2c_read_cmd(client, ETP_I2C_RESOLUTION_CMD, val); 336 if (error) { 337 dev_err(&client->dev, "failed to get resolution: %d\n", error); 338 return error; 339 } 340 341 *hw_res_x = val[0]; 342 *hw_res_y = val[1]; 343 344 return 0; 345 } 346 347 static int elan_i2c_get_num_traces(struct i2c_client *client, 348 unsigned int *x_traces, 349 unsigned int *y_traces) 350 { 351 int error; 352 u8 val[3]; 353 354 error = elan_i2c_read_cmd(client, ETP_I2C_XY_TRACENUM_CMD, val); 355 if (error) { 356 dev_err(&client->dev, "failed to get trace info: %d\n", error); 357 return error; 358 } 359 360 *x_traces = val[0] - 1; 361 *y_traces = val[1] - 1; 362 363 return 0; 364 } 365 366 static int elan_i2c_iap_get_mode(struct i2c_client *client, enum tp_mode *mode) 367 { 368 int error; 369 u16 constant; 370 u8 val[3]; 371 372 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val); 373 if (error) { 374 dev_err(&client->dev, 375 "failed to read iap control register: %d\n", 376 error); 377 return error; 378 } 379 380 constant = le16_to_cpup((__le16 *)val); 381 dev_dbg(&client->dev, "iap control reg: 0x%04x.\n", constant); 382 383 *mode = (constant & ETP_I2C_MAIN_MODE_ON) ? MAIN_MODE : IAP_MODE; 384 385 return 0; 386 } 387 388 static int elan_i2c_iap_reset(struct i2c_client *client) 389 { 390 int error; 391 392 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_RESET_CMD, 393 ETP_I2C_IAP_RESET); 394 if (error) { 395 dev_err(&client->dev, "cannot reset IC: %d\n", error); 396 return error; 397 } 398 399 return 0; 400 } 401 402 static int elan_i2c_set_flash_key(struct i2c_client *client) 403 { 404 int error; 405 406 error = elan_i2c_write_cmd(client, ETP_I2C_IAP_CMD, 407 ETP_I2C_IAP_PASSWORD); 408 if (error) { 409 dev_err(&client->dev, "cannot set flash key: %d\n", error); 410 return error; 411 } 412 413 return 0; 414 } 415 416 static int elan_i2c_prepare_fw_update(struct i2c_client *client) 417 { 418 struct device *dev = &client->dev; 419 int error; 420 enum tp_mode mode; 421 u8 val[3]; 422 u16 password; 423 424 /* Get FW in which mode (IAP_MODE/MAIN_MODE) */ 425 error = elan_i2c_iap_get_mode(client, &mode); 426 if (error) 427 return error; 428 429 if (mode == IAP_MODE) { 430 /* Reset IC */ 431 error = elan_i2c_iap_reset(client); 432 if (error) 433 return error; 434 435 msleep(30); 436 } 437 438 /* Set flash key*/ 439 error = elan_i2c_set_flash_key(client); 440 if (error) 441 return error; 442 443 /* Wait for F/W IAP initialization */ 444 msleep(mode == MAIN_MODE ? 100 : 30); 445 446 /* Check if we are in IAP mode or not */ 447 error = elan_i2c_iap_get_mode(client, &mode); 448 if (error) 449 return error; 450 451 if (mode == MAIN_MODE) { 452 dev_err(dev, "wrong mode: %d\n", mode); 453 return -EIO; 454 } 455 456 /* Set flash key again */ 457 error = elan_i2c_set_flash_key(client); 458 if (error) 459 return error; 460 461 /* Wait for F/W IAP initialization */ 462 msleep(30); 463 464 /* read back to check we actually enabled successfully. */ 465 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CMD, val); 466 if (error) { 467 dev_err(dev, "cannot read iap password: %d\n", 468 error); 469 return error; 470 } 471 472 password = le16_to_cpup((__le16 *)val); 473 if (password != ETP_I2C_IAP_PASSWORD) { 474 dev_err(dev, "wrong iap password: 0x%X\n", password); 475 return -EIO; 476 } 477 478 return 0; 479 } 480 481 static int elan_i2c_write_fw_block(struct i2c_client *client, 482 const u8 *page, u16 checksum, int idx) 483 { 484 struct device *dev = &client->dev; 485 u8 page_store[ETP_FW_PAGE_SIZE + 4]; 486 u8 val[3]; 487 u16 result; 488 int ret, error; 489 490 page_store[0] = ETP_I2C_IAP_REG_L; 491 page_store[1] = ETP_I2C_IAP_REG_H; 492 memcpy(&page_store[2], page, ETP_FW_PAGE_SIZE); 493 /* recode checksum at last two bytes */ 494 put_unaligned_le16(checksum, &page_store[ETP_FW_PAGE_SIZE + 2]); 495 496 ret = i2c_master_send(client, page_store, sizeof(page_store)); 497 if (ret != sizeof(page_store)) { 498 error = ret < 0 ? ret : -EIO; 499 dev_err(dev, "Failed to write page %d: %d\n", idx, error); 500 return error; 501 } 502 503 /* Wait for F/W to update one page ROM data. */ 504 msleep(20); 505 506 error = elan_i2c_read_cmd(client, ETP_I2C_IAP_CTRL_CMD, val); 507 if (error) { 508 dev_err(dev, "Failed to read IAP write result: %d\n", error); 509 return error; 510 } 511 512 result = le16_to_cpup((__le16 *)val); 513 if (result & (ETP_FW_IAP_PAGE_ERR | ETP_FW_IAP_INTF_ERR)) { 514 dev_err(dev, "IAP reports failed write: %04hx\n", 515 result); 516 return -EIO; 517 } 518 519 return 0; 520 } 521 522 static int elan_i2c_finish_fw_update(struct i2c_client *client, 523 struct completion *completion) 524 { 525 struct device *dev = &client->dev; 526 long ret; 527 int error; 528 int len; 529 u8 buffer[ETP_I2C_INF_LENGTH]; 530 531 reinit_completion(completion); 532 enable_irq(client->irq); 533 534 error = elan_i2c_write_cmd(client, ETP_I2C_STAND_CMD, ETP_I2C_RESET); 535 if (!error) 536 ret = wait_for_completion_interruptible_timeout(completion, 537 msecs_to_jiffies(300)); 538 disable_irq(client->irq); 539 540 if (error) { 541 dev_err(dev, "device reset failed: %d\n", error); 542 return error; 543 } else if (ret == 0) { 544 dev_err(dev, "timeout waiting for device reset\n"); 545 return -ETIMEDOUT; 546 } else if (ret < 0) { 547 error = ret; 548 dev_err(dev, "error waiting for device reset: %d\n", error); 549 return error; 550 } 551 552 len = i2c_master_recv(client, buffer, ETP_I2C_INF_LENGTH); 553 if (len != ETP_I2C_INF_LENGTH) { 554 error = len < 0 ? len : -EIO; 555 dev_err(dev, "failed to read INT signal: %d (%d)\n", 556 error, len); 557 return error; 558 } 559 560 return 0; 561 } 562 563 static int elan_i2c_get_report(struct i2c_client *client, u8 *report) 564 { 565 int len; 566 567 len = i2c_master_recv(client, report, ETP_I2C_REPORT_LEN); 568 if (len < 0) { 569 dev_err(&client->dev, "failed to read report data: %d\n", len); 570 return len; 571 } 572 573 if (len != ETP_I2C_REPORT_LEN) { 574 dev_err(&client->dev, 575 "wrong report length (%d vs %d expected)\n", 576 len, ETP_I2C_REPORT_LEN); 577 return -EIO; 578 } 579 580 return 0; 581 } 582 583 const struct elan_transport_ops elan_i2c_ops = { 584 .initialize = elan_i2c_initialize, 585 .sleep_control = elan_i2c_sleep_control, 586 .power_control = elan_i2c_power_control, 587 .set_mode = elan_i2c_set_mode, 588 589 .calibrate = elan_i2c_calibrate, 590 .calibrate_result = elan_i2c_calibrate_result, 591 592 .get_baseline_data = elan_i2c_get_baseline_data, 593 594 .get_version = elan_i2c_get_version, 595 .get_sm_version = elan_i2c_get_sm_version, 596 .get_product_id = elan_i2c_get_product_id, 597 .get_checksum = elan_i2c_get_checksum, 598 599 .get_max = elan_i2c_get_max, 600 .get_resolution = elan_i2c_get_resolution, 601 .get_num_traces = elan_i2c_get_num_traces, 602 603 .iap_get_mode = elan_i2c_iap_get_mode, 604 .iap_reset = elan_i2c_iap_reset, 605 606 .prepare_fw_update = elan_i2c_prepare_fw_update, 607 .write_fw_block = elan_i2c_write_fw_block, 608 .finish_fw_update = elan_i2c_finish_fw_update, 609 610 .get_report = elan_i2c_get_report, 611 }; 612