1 /* 2 * Elan I2C/SMBus Touchpad driver 3 * 4 * Copyright (c) 2013 ELAN Microelectronics Corp. 5 * 6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw> 7 * Author: KT Liao <kt.liao@emc.com.tw> 8 * Version: 1.6.3 9 * 10 * Based on cyapa driver: 11 * copyright (c) 2011-2012 Cypress Semiconductor, Inc. 12 * copyright (c) 2011-2012 Google, Inc. 13 * 14 * This program is free software; you can redistribute it and/or modify it 15 * under the terms of the GNU General Public License version 2 as published 16 * by the Free Software Foundation. 17 * 18 * Trademarks are the property of their respective owners. 19 */ 20 21 #include <linux/acpi.h> 22 #include <linux/delay.h> 23 #include <linux/device.h> 24 #include <linux/firmware.h> 25 #include <linux/i2c.h> 26 #include <linux/init.h> 27 #include <linux/input/mt.h> 28 #include <linux/interrupt.h> 29 #include <linux/irq.h> 30 #include <linux/module.h> 31 #include <linux/slab.h> 32 #include <linux/kernel.h> 33 #include <linux/sched.h> 34 #include <linux/input.h> 35 #include <linux/uaccess.h> 36 #include <linux/jiffies.h> 37 #include <linux/completion.h> 38 #include <linux/of.h> 39 #include <linux/regulator/consumer.h> 40 #include <asm/unaligned.h> 41 42 #include "elan_i2c.h" 43 44 #define DRIVER_NAME "elan_i2c" 45 #define ELAN_VENDOR_ID 0x04f3 46 #define ETP_MAX_PRESSURE 255 47 #define ETP_FWIDTH_REDUCE 90 48 #define ETP_FINGER_WIDTH 15 49 #define ETP_RETRY_COUNT 3 50 51 #define ETP_MAX_FINGERS 5 52 #define ETP_FINGER_DATA_LEN 5 53 #define ETP_REPORT_ID 0x5D 54 #define ETP_REPORT_ID_OFFSET 2 55 #define ETP_TOUCH_INFO_OFFSET 3 56 #define ETP_FINGER_DATA_OFFSET 4 57 #define ETP_HOVER_INFO_OFFSET 30 58 #define ETP_MAX_REPORT_LEN 34 59 60 /* The main device structure */ 61 struct elan_tp_data { 62 struct i2c_client *client; 63 struct input_dev *input; 64 struct regulator *vcc; 65 66 const struct elan_transport_ops *ops; 67 68 /* for fw update */ 69 struct completion fw_completion; 70 bool in_fw_update; 71 72 struct mutex sysfs_mutex; 73 74 unsigned int max_x; 75 unsigned int max_y; 76 unsigned int width_x; 77 unsigned int width_y; 78 unsigned int x_res; 79 unsigned int y_res; 80 81 u8 pattern; 82 u16 product_id; 83 u8 fw_version; 84 u8 sm_version; 85 u8 iap_version; 86 u16 fw_checksum; 87 int pressure_adjustment; 88 u8 mode; 89 u16 ic_type; 90 u16 fw_validpage_count; 91 u16 fw_signature_address; 92 93 bool irq_wake; 94 95 u8 min_baseline; 96 u8 max_baseline; 97 bool baseline_ready; 98 u8 clickpad; 99 }; 100 101 static int elan_get_fwinfo(u16 ic_type, u16 *validpage_count, 102 u16 *signature_address) 103 { 104 switch (ic_type) { 105 case 0x00: 106 case 0x06: 107 case 0x08: 108 *validpage_count = 512; 109 break; 110 case 0x03: 111 case 0x07: 112 case 0x09: 113 case 0x0A: 114 case 0x0B: 115 case 0x0C: 116 *validpage_count = 768; 117 break; 118 case 0x0D: 119 *validpage_count = 896; 120 break; 121 case 0x0E: 122 *validpage_count = 640; 123 break; 124 case 0x10: 125 *validpage_count = 1024; 126 break; 127 default: 128 /* unknown ic type clear value */ 129 *validpage_count = 0; 130 *signature_address = 0; 131 return -ENXIO; 132 } 133 134 *signature_address = 135 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE; 136 137 return 0; 138 } 139 140 static int elan_enable_power(struct elan_tp_data *data) 141 { 142 int repeat = ETP_RETRY_COUNT; 143 int error; 144 145 error = regulator_enable(data->vcc); 146 if (error) { 147 dev_err(&data->client->dev, 148 "failed to enable regulator: %d\n", error); 149 return error; 150 } 151 152 do { 153 error = data->ops->power_control(data->client, true); 154 if (error >= 0) 155 return 0; 156 157 msleep(30); 158 } while (--repeat > 0); 159 160 dev_err(&data->client->dev, "failed to enable power: %d\n", error); 161 return error; 162 } 163 164 static int elan_disable_power(struct elan_tp_data *data) 165 { 166 int repeat = ETP_RETRY_COUNT; 167 int error; 168 169 do { 170 error = data->ops->power_control(data->client, false); 171 if (!error) { 172 error = regulator_disable(data->vcc); 173 if (error) { 174 dev_err(&data->client->dev, 175 "failed to disable regulator: %d\n", 176 error); 177 /* Attempt to power the chip back up */ 178 data->ops->power_control(data->client, true); 179 break; 180 } 181 182 return 0; 183 } 184 185 msleep(30); 186 } while (--repeat > 0); 187 188 dev_err(&data->client->dev, "failed to disable power: %d\n", error); 189 return error; 190 } 191 192 static int elan_sleep(struct elan_tp_data *data) 193 { 194 int repeat = ETP_RETRY_COUNT; 195 int error; 196 197 do { 198 error = data->ops->sleep_control(data->client, true); 199 if (!error) 200 return 0; 201 202 msleep(30); 203 } while (--repeat > 0); 204 205 return error; 206 } 207 208 static int elan_query_product(struct elan_tp_data *data) 209 { 210 int error; 211 212 error = data->ops->get_product_id(data->client, &data->product_id); 213 if (error) 214 return error; 215 216 error = data->ops->get_sm_version(data->client, &data->ic_type, 217 &data->sm_version, &data->clickpad); 218 if (error) 219 return error; 220 221 return 0; 222 } 223 224 static int elan_check_ASUS_special_fw(struct elan_tp_data *data) 225 { 226 if (data->ic_type == 0x0E) { 227 switch (data->product_id) { 228 case 0x05 ... 0x07: 229 case 0x09: 230 case 0x13: 231 return true; 232 } 233 } else if (data->ic_type == 0x08 && data->product_id == 0x26) { 234 /* ASUS EeeBook X205TA */ 235 return true; 236 } 237 238 return false; 239 } 240 241 static int __elan_initialize(struct elan_tp_data *data) 242 { 243 struct i2c_client *client = data->client; 244 bool woken_up = false; 245 int error; 246 247 error = data->ops->initialize(client); 248 if (error) { 249 dev_err(&client->dev, "device initialize failed: %d\n", error); 250 return error; 251 } 252 253 error = elan_query_product(data); 254 if (error) 255 return error; 256 257 /* 258 * Some ASUS devices were shipped with firmware that requires 259 * touchpads to be woken up first, before attempting to switch 260 * them into absolute reporting mode. 261 */ 262 if (elan_check_ASUS_special_fw(data)) { 263 error = data->ops->sleep_control(client, false); 264 if (error) { 265 dev_err(&client->dev, 266 "failed to wake device up: %d\n", error); 267 return error; 268 } 269 270 msleep(200); 271 woken_up = true; 272 } 273 274 data->mode |= ETP_ENABLE_ABS; 275 error = data->ops->set_mode(client, data->mode); 276 if (error) { 277 dev_err(&client->dev, 278 "failed to switch to absolute mode: %d\n", error); 279 return error; 280 } 281 282 if (!woken_up) { 283 error = data->ops->sleep_control(client, false); 284 if (error) { 285 dev_err(&client->dev, 286 "failed to wake device up: %d\n", error); 287 return error; 288 } 289 } 290 291 return 0; 292 } 293 294 static int elan_initialize(struct elan_tp_data *data) 295 { 296 int repeat = ETP_RETRY_COUNT; 297 int error; 298 299 do { 300 error = __elan_initialize(data); 301 if (!error) 302 return 0; 303 304 msleep(30); 305 } while (--repeat > 0); 306 307 return error; 308 } 309 310 static int elan_query_device_info(struct elan_tp_data *data) 311 { 312 int error; 313 u16 ic_type; 314 315 error = data->ops->get_version(data->client, false, &data->fw_version); 316 if (error) 317 return error; 318 319 error = data->ops->get_checksum(data->client, false, 320 &data->fw_checksum); 321 if (error) 322 return error; 323 324 error = data->ops->get_version(data->client, true, &data->iap_version); 325 if (error) 326 return error; 327 328 error = data->ops->get_pressure_adjustment(data->client, 329 &data->pressure_adjustment); 330 if (error) 331 return error; 332 333 error = data->ops->get_pattern(data->client, &data->pattern); 334 if (error) 335 return error; 336 337 if (data->pattern == 0x01) 338 ic_type = data->ic_type; 339 else 340 ic_type = data->iap_version; 341 342 error = elan_get_fwinfo(ic_type, &data->fw_validpage_count, 343 &data->fw_signature_address); 344 if (error) 345 dev_warn(&data->client->dev, 346 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n", 347 data->iap_version, data->ic_type); 348 349 return 0; 350 } 351 352 static unsigned int elan_convert_resolution(u8 val) 353 { 354 /* 355 * (value from firmware) * 10 + 790 = dpi 356 * 357 * We also have to convert dpi to dots/mm (*10/254 to avoid floating 358 * point). 359 */ 360 361 return ((int)(char)val * 10 + 790) * 10 / 254; 362 } 363 364 static int elan_query_device_parameters(struct elan_tp_data *data) 365 { 366 unsigned int x_traces, y_traces; 367 u8 hw_x_res, hw_y_res; 368 int error; 369 370 error = data->ops->get_max(data->client, &data->max_x, &data->max_y); 371 if (error) 372 return error; 373 374 error = data->ops->get_num_traces(data->client, &x_traces, &y_traces); 375 if (error) 376 return error; 377 378 data->width_x = data->max_x / x_traces; 379 data->width_y = data->max_y / y_traces; 380 381 error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res); 382 if (error) 383 return error; 384 385 data->x_res = elan_convert_resolution(hw_x_res); 386 data->y_res = elan_convert_resolution(hw_y_res); 387 388 return 0; 389 } 390 391 /* 392 ********************************************************** 393 * IAP firmware updater related routines 394 ********************************************************** 395 */ 396 static int elan_write_fw_block(struct elan_tp_data *data, 397 const u8 *page, u16 checksum, int idx) 398 { 399 int retry = ETP_RETRY_COUNT; 400 int error; 401 402 do { 403 error = data->ops->write_fw_block(data->client, 404 page, checksum, idx); 405 if (!error) 406 return 0; 407 408 dev_dbg(&data->client->dev, 409 "IAP retrying page %d (error: %d)\n", idx, error); 410 } while (--retry > 0); 411 412 return error; 413 } 414 415 static int __elan_update_firmware(struct elan_tp_data *data, 416 const struct firmware *fw) 417 { 418 struct i2c_client *client = data->client; 419 struct device *dev = &client->dev; 420 int i, j; 421 int error; 422 u16 iap_start_addr; 423 u16 boot_page_count; 424 u16 sw_checksum = 0, fw_checksum = 0; 425 426 error = data->ops->prepare_fw_update(client); 427 if (error) 428 return error; 429 430 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]); 431 432 boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE; 433 for (i = boot_page_count; i < data->fw_validpage_count; i++) { 434 u16 checksum = 0; 435 const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE]; 436 437 for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2) 438 checksum += ((page[j + 1] << 8) | page[j]); 439 440 error = elan_write_fw_block(data, page, checksum, i); 441 if (error) { 442 dev_err(dev, "write page %d fail: %d\n", i, error); 443 return error; 444 } 445 446 sw_checksum += checksum; 447 } 448 449 /* Wait WDT reset and power on reset */ 450 msleep(600); 451 452 error = data->ops->finish_fw_update(client, &data->fw_completion); 453 if (error) 454 return error; 455 456 error = data->ops->get_checksum(client, true, &fw_checksum); 457 if (error) 458 return error; 459 460 if (sw_checksum != fw_checksum) { 461 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n", 462 sw_checksum, fw_checksum); 463 return -EIO; 464 } 465 466 return 0; 467 } 468 469 static int elan_update_firmware(struct elan_tp_data *data, 470 const struct firmware *fw) 471 { 472 struct i2c_client *client = data->client; 473 int retval; 474 475 dev_dbg(&client->dev, "Starting firmware update....\n"); 476 477 disable_irq(client->irq); 478 data->in_fw_update = true; 479 480 retval = __elan_update_firmware(data, fw); 481 if (retval) { 482 dev_err(&client->dev, "firmware update failed: %d\n", retval); 483 data->ops->iap_reset(client); 484 } else { 485 /* Reinitialize TP after fw is updated */ 486 elan_initialize(data); 487 elan_query_device_info(data); 488 } 489 490 data->in_fw_update = false; 491 enable_irq(client->irq); 492 493 return retval; 494 } 495 496 /* 497 ******************************************************************* 498 * SYSFS attributes 499 ******************************************************************* 500 */ 501 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev, 502 struct device_attribute *attr, 503 char *buf) 504 { 505 struct i2c_client *client = to_i2c_client(dev); 506 struct elan_tp_data *data = i2c_get_clientdata(client); 507 508 return sprintf(buf, "0x%04x\n", data->fw_checksum); 509 } 510 511 static ssize_t elan_sysfs_read_product_id(struct device *dev, 512 struct device_attribute *attr, 513 char *buf) 514 { 515 struct i2c_client *client = to_i2c_client(dev); 516 struct elan_tp_data *data = i2c_get_clientdata(client); 517 518 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n", 519 data->product_id); 520 } 521 522 static ssize_t elan_sysfs_read_fw_ver(struct device *dev, 523 struct device_attribute *attr, 524 char *buf) 525 { 526 struct i2c_client *client = to_i2c_client(dev); 527 struct elan_tp_data *data = i2c_get_clientdata(client); 528 529 return sprintf(buf, "%d.0\n", data->fw_version); 530 } 531 532 static ssize_t elan_sysfs_read_sm_ver(struct device *dev, 533 struct device_attribute *attr, 534 char *buf) 535 { 536 struct i2c_client *client = to_i2c_client(dev); 537 struct elan_tp_data *data = i2c_get_clientdata(client); 538 539 return sprintf(buf, "%d.0\n", data->sm_version); 540 } 541 542 static ssize_t elan_sysfs_read_iap_ver(struct device *dev, 543 struct device_attribute *attr, 544 char *buf) 545 { 546 struct i2c_client *client = to_i2c_client(dev); 547 struct elan_tp_data *data = i2c_get_clientdata(client); 548 549 return sprintf(buf, "%d.0\n", data->iap_version); 550 } 551 552 static ssize_t elan_sysfs_update_fw(struct device *dev, 553 struct device_attribute *attr, 554 const char *buf, size_t count) 555 { 556 struct elan_tp_data *data = dev_get_drvdata(dev); 557 const struct firmware *fw; 558 char *fw_name; 559 int error; 560 const u8 *fw_signature; 561 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF}; 562 563 if (data->fw_validpage_count == 0) 564 return -EINVAL; 565 566 /* Look for a firmware with the product id appended. */ 567 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id); 568 if (!fw_name) { 569 dev_err(dev, "failed to allocate memory for firmware name\n"); 570 return -ENOMEM; 571 } 572 573 dev_info(dev, "requesting fw '%s'\n", fw_name); 574 error = request_firmware(&fw, fw_name, dev); 575 kfree(fw_name); 576 if (error) { 577 dev_err(dev, "failed to request firmware: %d\n", error); 578 return error; 579 } 580 581 /* Firmware file must match signature data */ 582 fw_signature = &fw->data[data->fw_signature_address]; 583 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) { 584 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n", 585 (int)sizeof(signature), signature, 586 (int)sizeof(signature), fw_signature); 587 error = -EBADF; 588 goto out_release_fw; 589 } 590 591 error = mutex_lock_interruptible(&data->sysfs_mutex); 592 if (error) 593 goto out_release_fw; 594 595 error = elan_update_firmware(data, fw); 596 597 mutex_unlock(&data->sysfs_mutex); 598 599 out_release_fw: 600 release_firmware(fw); 601 return error ?: count; 602 } 603 604 static ssize_t calibrate_store(struct device *dev, 605 struct device_attribute *attr, 606 const char *buf, size_t count) 607 { 608 struct i2c_client *client = to_i2c_client(dev); 609 struct elan_tp_data *data = i2c_get_clientdata(client); 610 int tries = 20; 611 int retval; 612 int error; 613 u8 val[3]; 614 615 retval = mutex_lock_interruptible(&data->sysfs_mutex); 616 if (retval) 617 return retval; 618 619 disable_irq(client->irq); 620 621 data->mode |= ETP_ENABLE_CALIBRATE; 622 retval = data->ops->set_mode(client, data->mode); 623 if (retval) { 624 dev_err(dev, "failed to enable calibration mode: %d\n", 625 retval); 626 goto out; 627 } 628 629 retval = data->ops->calibrate(client); 630 if (retval) { 631 dev_err(dev, "failed to start calibration: %d\n", 632 retval); 633 goto out_disable_calibrate; 634 } 635 636 val[0] = 0xff; 637 do { 638 /* Wait 250ms before checking if calibration has completed. */ 639 msleep(250); 640 641 retval = data->ops->calibrate_result(client, val); 642 if (retval) 643 dev_err(dev, "failed to check calibration result: %d\n", 644 retval); 645 else if (val[0] == 0) 646 break; /* calibration done */ 647 648 } while (--tries); 649 650 if (tries == 0) { 651 dev_err(dev, "failed to calibrate. Timeout.\n"); 652 retval = -ETIMEDOUT; 653 } 654 655 out_disable_calibrate: 656 data->mode &= ~ETP_ENABLE_CALIBRATE; 657 error = data->ops->set_mode(data->client, data->mode); 658 if (error) { 659 dev_err(dev, "failed to disable calibration mode: %d\n", 660 error); 661 if (!retval) 662 retval = error; 663 } 664 out: 665 enable_irq(client->irq); 666 mutex_unlock(&data->sysfs_mutex); 667 return retval ?: count; 668 } 669 670 static ssize_t elan_sysfs_read_mode(struct device *dev, 671 struct device_attribute *attr, 672 char *buf) 673 { 674 struct i2c_client *client = to_i2c_client(dev); 675 struct elan_tp_data *data = i2c_get_clientdata(client); 676 int error; 677 enum tp_mode mode; 678 679 error = mutex_lock_interruptible(&data->sysfs_mutex); 680 if (error) 681 return error; 682 683 error = data->ops->iap_get_mode(data->client, &mode); 684 685 mutex_unlock(&data->sysfs_mutex); 686 687 if (error) 688 return error; 689 690 return sprintf(buf, "%d\n", (int)mode); 691 } 692 693 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL); 694 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL); 695 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL); 696 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL); 697 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL); 698 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL); 699 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw); 700 701 static DEVICE_ATTR_WO(calibrate); 702 703 static struct attribute *elan_sysfs_entries[] = { 704 &dev_attr_product_id.attr, 705 &dev_attr_firmware_version.attr, 706 &dev_attr_sample_version.attr, 707 &dev_attr_iap_version.attr, 708 &dev_attr_fw_checksum.attr, 709 &dev_attr_calibrate.attr, 710 &dev_attr_mode.attr, 711 &dev_attr_update_fw.attr, 712 NULL, 713 }; 714 715 static const struct attribute_group elan_sysfs_group = { 716 .attrs = elan_sysfs_entries, 717 }; 718 719 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr, 720 const char *buf, size_t count) 721 { 722 struct i2c_client *client = to_i2c_client(dev); 723 struct elan_tp_data *data = i2c_get_clientdata(client); 724 int error; 725 int retval; 726 727 retval = mutex_lock_interruptible(&data->sysfs_mutex); 728 if (retval) 729 return retval; 730 731 disable_irq(client->irq); 732 733 data->baseline_ready = false; 734 735 data->mode |= ETP_ENABLE_CALIBRATE; 736 retval = data->ops->set_mode(data->client, data->mode); 737 if (retval) { 738 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n", 739 retval); 740 goto out; 741 } 742 743 msleep(250); 744 745 retval = data->ops->get_baseline_data(data->client, true, 746 &data->max_baseline); 747 if (retval) { 748 dev_err(dev, "Failed to read max baseline form device: %d\n", 749 retval); 750 goto out_disable_calibrate; 751 } 752 753 retval = data->ops->get_baseline_data(data->client, false, 754 &data->min_baseline); 755 if (retval) { 756 dev_err(dev, "Failed to read min baseline form device: %d\n", 757 retval); 758 goto out_disable_calibrate; 759 } 760 761 data->baseline_ready = true; 762 763 out_disable_calibrate: 764 data->mode &= ~ETP_ENABLE_CALIBRATE; 765 error = data->ops->set_mode(data->client, data->mode); 766 if (error) { 767 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n", 768 error); 769 if (!retval) 770 retval = error; 771 } 772 out: 773 enable_irq(client->irq); 774 mutex_unlock(&data->sysfs_mutex); 775 return retval ?: count; 776 } 777 778 static ssize_t min_show(struct device *dev, 779 struct device_attribute *attr, char *buf) 780 { 781 struct i2c_client *client = to_i2c_client(dev); 782 struct elan_tp_data *data = i2c_get_clientdata(client); 783 int retval; 784 785 retval = mutex_lock_interruptible(&data->sysfs_mutex); 786 if (retval) 787 return retval; 788 789 if (!data->baseline_ready) { 790 retval = -ENODATA; 791 goto out; 792 } 793 794 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline); 795 796 out: 797 mutex_unlock(&data->sysfs_mutex); 798 return retval; 799 } 800 801 static ssize_t max_show(struct device *dev, 802 struct device_attribute *attr, char *buf) 803 { 804 struct i2c_client *client = to_i2c_client(dev); 805 struct elan_tp_data *data = i2c_get_clientdata(client); 806 int retval; 807 808 retval = mutex_lock_interruptible(&data->sysfs_mutex); 809 if (retval) 810 return retval; 811 812 if (!data->baseline_ready) { 813 retval = -ENODATA; 814 goto out; 815 } 816 817 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline); 818 819 out: 820 mutex_unlock(&data->sysfs_mutex); 821 return retval; 822 } 823 824 825 static DEVICE_ATTR_WO(acquire); 826 static DEVICE_ATTR_RO(min); 827 static DEVICE_ATTR_RO(max); 828 829 static struct attribute *elan_baseline_sysfs_entries[] = { 830 &dev_attr_acquire.attr, 831 &dev_attr_min.attr, 832 &dev_attr_max.attr, 833 NULL, 834 }; 835 836 static const struct attribute_group elan_baseline_sysfs_group = { 837 .name = "baseline", 838 .attrs = elan_baseline_sysfs_entries, 839 }; 840 841 static const struct attribute_group *elan_sysfs_groups[] = { 842 &elan_sysfs_group, 843 &elan_baseline_sysfs_group, 844 NULL 845 }; 846 847 /* 848 ****************************************************************** 849 * Elan isr functions 850 ****************************************************************** 851 */ 852 static void elan_report_contact(struct elan_tp_data *data, 853 int contact_num, bool contact_valid, 854 u8 *finger_data) 855 { 856 struct input_dev *input = data->input; 857 unsigned int pos_x, pos_y; 858 unsigned int pressure, mk_x, mk_y; 859 unsigned int area_x, area_y, major, minor; 860 unsigned int scaled_pressure; 861 862 if (contact_valid) { 863 pos_x = ((finger_data[0] & 0xf0) << 4) | 864 finger_data[1]; 865 pos_y = ((finger_data[0] & 0x0f) << 8) | 866 finger_data[2]; 867 mk_x = (finger_data[3] & 0x0f); 868 mk_y = (finger_data[3] >> 4); 869 pressure = finger_data[4]; 870 871 if (pos_x > data->max_x || pos_y > data->max_y) { 872 dev_dbg(input->dev.parent, 873 "[%d] x=%d y=%d over max (%d, %d)", 874 contact_num, pos_x, pos_y, 875 data->max_x, data->max_y); 876 return; 877 } 878 879 /* 880 * To avoid treating large finger as palm, let's reduce the 881 * width x and y per trace. 882 */ 883 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE); 884 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE); 885 886 major = max(area_x, area_y); 887 minor = min(area_x, area_y); 888 889 scaled_pressure = pressure + data->pressure_adjustment; 890 891 if (scaled_pressure > ETP_MAX_PRESSURE) 892 scaled_pressure = ETP_MAX_PRESSURE; 893 894 input_mt_slot(input, contact_num); 895 input_mt_report_slot_state(input, MT_TOOL_FINGER, true); 896 input_report_abs(input, ABS_MT_POSITION_X, pos_x); 897 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y); 898 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure); 899 input_report_abs(input, ABS_TOOL_WIDTH, mk_x); 900 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major); 901 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor); 902 } else { 903 input_mt_slot(input, contact_num); 904 input_mt_report_slot_state(input, MT_TOOL_FINGER, false); 905 } 906 } 907 908 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet) 909 { 910 struct input_dev *input = data->input; 911 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET]; 912 int i; 913 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET]; 914 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET]; 915 bool contact_valid, hover_event; 916 917 hover_event = hover_info & 0x40; 918 for (i = 0; i < ETP_MAX_FINGERS; i++) { 919 contact_valid = tp_info & (1U << (3 + i)); 920 elan_report_contact(data, i, contact_valid, finger_data); 921 922 if (contact_valid) 923 finger_data += ETP_FINGER_DATA_LEN; 924 } 925 926 input_report_key(input, BTN_LEFT, tp_info & 0x01); 927 input_report_key(input, BTN_RIGHT, tp_info & 0x02); 928 input_report_abs(input, ABS_DISTANCE, hover_event != 0); 929 input_mt_report_pointer_emulation(input, true); 930 input_sync(input); 931 } 932 933 static irqreturn_t elan_isr(int irq, void *dev_id) 934 { 935 struct elan_tp_data *data = dev_id; 936 struct device *dev = &data->client->dev; 937 int error; 938 u8 report[ETP_MAX_REPORT_LEN]; 939 940 /* 941 * When device is connected to i2c bus, when all IAP page writes 942 * complete, the driver will receive interrupt and must read 943 * 0000 to confirm that IAP is finished. 944 */ 945 if (data->in_fw_update) { 946 complete(&data->fw_completion); 947 goto out; 948 } 949 950 error = data->ops->get_report(data->client, report); 951 if (error) 952 goto out; 953 954 if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID) 955 dev_err(dev, "invalid report id data (%x)\n", 956 report[ETP_REPORT_ID_OFFSET]); 957 else 958 elan_report_absolute(data, report); 959 960 out: 961 return IRQ_HANDLED; 962 } 963 964 /* 965 ****************************************************************** 966 * Elan initialization functions 967 ****************************************************************** 968 */ 969 static int elan_setup_input_device(struct elan_tp_data *data) 970 { 971 struct device *dev = &data->client->dev; 972 struct input_dev *input; 973 unsigned int max_width = max(data->width_x, data->width_y); 974 unsigned int min_width = min(data->width_x, data->width_y); 975 int error; 976 977 input = devm_input_allocate_device(dev); 978 if (!input) 979 return -ENOMEM; 980 981 input->name = "Elan Touchpad"; 982 input->id.bustype = BUS_I2C; 983 input->id.vendor = ELAN_VENDOR_ID; 984 input->id.product = data->product_id; 985 input_set_drvdata(input, data); 986 987 error = input_mt_init_slots(input, ETP_MAX_FINGERS, 988 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED); 989 if (error) { 990 dev_err(dev, "failed to initialize MT slots: %d\n", error); 991 return error; 992 } 993 994 __set_bit(EV_ABS, input->evbit); 995 __set_bit(INPUT_PROP_POINTER, input->propbit); 996 if (data->clickpad) 997 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit); 998 else 999 __set_bit(BTN_RIGHT, input->keybit); 1000 __set_bit(BTN_LEFT, input->keybit); 1001 1002 /* Set up ST parameters */ 1003 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0); 1004 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0); 1005 input_abs_set_res(input, ABS_X, data->x_res); 1006 input_abs_set_res(input, ABS_Y, data->y_res); 1007 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0); 1008 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0); 1009 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0); 1010 1011 /* And MT parameters */ 1012 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0); 1013 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0); 1014 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res); 1015 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res); 1016 input_set_abs_params(input, ABS_MT_PRESSURE, 0, 1017 ETP_MAX_PRESSURE, 0, 0); 1018 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 1019 ETP_FINGER_WIDTH * max_width, 0, 0); 1020 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 1021 ETP_FINGER_WIDTH * min_width, 0, 0); 1022 1023 data->input = input; 1024 1025 return 0; 1026 } 1027 1028 static void elan_disable_regulator(void *_data) 1029 { 1030 struct elan_tp_data *data = _data; 1031 1032 regulator_disable(data->vcc); 1033 } 1034 1035 static void elan_remove_sysfs_groups(void *_data) 1036 { 1037 struct elan_tp_data *data = _data; 1038 1039 sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups); 1040 } 1041 1042 static int elan_probe(struct i2c_client *client, 1043 const struct i2c_device_id *dev_id) 1044 { 1045 const struct elan_transport_ops *transport_ops; 1046 struct device *dev = &client->dev; 1047 struct elan_tp_data *data; 1048 unsigned long irqflags; 1049 int error; 1050 1051 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) && 1052 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 1053 transport_ops = &elan_i2c_ops; 1054 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) && 1055 i2c_check_functionality(client->adapter, 1056 I2C_FUNC_SMBUS_BYTE_DATA | 1057 I2C_FUNC_SMBUS_BLOCK_DATA | 1058 I2C_FUNC_SMBUS_I2C_BLOCK)) { 1059 transport_ops = &elan_smbus_ops; 1060 } else { 1061 dev_err(dev, "not a supported I2C/SMBus adapter\n"); 1062 return -EIO; 1063 } 1064 1065 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL); 1066 if (!data) 1067 return -ENOMEM; 1068 1069 i2c_set_clientdata(client, data); 1070 1071 data->ops = transport_ops; 1072 data->client = client; 1073 init_completion(&data->fw_completion); 1074 mutex_init(&data->sysfs_mutex); 1075 1076 data->vcc = devm_regulator_get(dev, "vcc"); 1077 if (IS_ERR(data->vcc)) { 1078 error = PTR_ERR(data->vcc); 1079 if (error != -EPROBE_DEFER) 1080 dev_err(dev, "Failed to get 'vcc' regulator: %d\n", 1081 error); 1082 return error; 1083 } 1084 1085 error = regulator_enable(data->vcc); 1086 if (error) { 1087 dev_err(dev, "Failed to enable regulator: %d\n", error); 1088 return error; 1089 } 1090 1091 error = devm_add_action(dev, elan_disable_regulator, data); 1092 if (error) { 1093 regulator_disable(data->vcc); 1094 dev_err(dev, "Failed to add disable regulator action: %d\n", 1095 error); 1096 return error; 1097 } 1098 1099 /* Make sure there is something at this address */ 1100 error = i2c_smbus_read_byte(client); 1101 if (error < 0) { 1102 dev_dbg(&client->dev, "nothing at this address: %d\n", error); 1103 return -ENXIO; 1104 } 1105 1106 /* Initialize the touchpad. */ 1107 error = elan_initialize(data); 1108 if (error) 1109 return error; 1110 1111 error = elan_query_device_info(data); 1112 if (error) 1113 return error; 1114 1115 error = elan_query_device_parameters(data); 1116 if (error) 1117 return error; 1118 1119 dev_info(dev, 1120 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n", 1121 data->product_id, 1122 data->fw_version, 1123 data->sm_version, 1124 data->iap_version); 1125 1126 dev_dbg(dev, 1127 "Elan Touchpad Extra Information:\n" 1128 " Max ABS X,Y: %d,%d\n" 1129 " Width X,Y: %d,%d\n" 1130 " Resolution X,Y: %d,%d (dots/mm)\n" 1131 " ic type: 0x%x\n" 1132 " info pattern: 0x%x\n", 1133 data->max_x, data->max_y, 1134 data->width_x, data->width_y, 1135 data->x_res, data->y_res, 1136 data->ic_type, data->pattern); 1137 1138 /* Set up input device properties based on queried parameters. */ 1139 error = elan_setup_input_device(data); 1140 if (error) 1141 return error; 1142 1143 /* 1144 * Platform code (ACPI, DTS) should normally set up interrupt 1145 * for us, but in case it did not let's fall back to using falling 1146 * edge to be compatible with older Chromebooks. 1147 */ 1148 irqflags = irq_get_trigger_type(client->irq); 1149 if (!irqflags) 1150 irqflags = IRQF_TRIGGER_FALLING; 1151 1152 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr, 1153 irqflags | IRQF_ONESHOT, 1154 client->name, data); 1155 if (error) { 1156 dev_err(dev, "cannot register irq=%d\n", client->irq); 1157 return error; 1158 } 1159 1160 error = sysfs_create_groups(&dev->kobj, elan_sysfs_groups); 1161 if (error) { 1162 dev_err(dev, "failed to create sysfs attributes: %d\n", error); 1163 return error; 1164 } 1165 1166 error = devm_add_action(dev, elan_remove_sysfs_groups, data); 1167 if (error) { 1168 elan_remove_sysfs_groups(data); 1169 dev_err(dev, "Failed to add sysfs cleanup action: %d\n", 1170 error); 1171 return error; 1172 } 1173 1174 error = input_register_device(data->input); 1175 if (error) { 1176 dev_err(dev, "failed to register input device: %d\n", error); 1177 return error; 1178 } 1179 1180 /* 1181 * Systems using device tree should set up wakeup via DTS, 1182 * the rest will configure device as wakeup source by default. 1183 */ 1184 if (!dev->of_node) 1185 device_init_wakeup(dev, true); 1186 1187 return 0; 1188 } 1189 1190 static int __maybe_unused elan_suspend(struct device *dev) 1191 { 1192 struct i2c_client *client = to_i2c_client(dev); 1193 struct elan_tp_data *data = i2c_get_clientdata(client); 1194 int ret; 1195 1196 /* 1197 * We are taking the mutex to make sure sysfs operations are 1198 * complete before we attempt to bring the device into low[er] 1199 * power mode. 1200 */ 1201 ret = mutex_lock_interruptible(&data->sysfs_mutex); 1202 if (ret) 1203 return ret; 1204 1205 disable_irq(client->irq); 1206 1207 if (device_may_wakeup(dev)) { 1208 ret = elan_sleep(data); 1209 /* Enable wake from IRQ */ 1210 data->irq_wake = (enable_irq_wake(client->irq) == 0); 1211 } else { 1212 ret = elan_disable_power(data); 1213 } 1214 1215 mutex_unlock(&data->sysfs_mutex); 1216 return ret; 1217 } 1218 1219 static int __maybe_unused elan_resume(struct device *dev) 1220 { 1221 struct i2c_client *client = to_i2c_client(dev); 1222 struct elan_tp_data *data = i2c_get_clientdata(client); 1223 int error; 1224 1225 if (device_may_wakeup(dev) && data->irq_wake) { 1226 disable_irq_wake(client->irq); 1227 data->irq_wake = false; 1228 } 1229 1230 error = elan_enable_power(data); 1231 if (error) { 1232 dev_err(dev, "power up when resuming failed: %d\n", error); 1233 goto err; 1234 } 1235 1236 error = elan_initialize(data); 1237 if (error) 1238 dev_err(dev, "initialize when resuming failed: %d\n", error); 1239 1240 err: 1241 enable_irq(data->client->irq); 1242 return error; 1243 } 1244 1245 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume); 1246 1247 static const struct i2c_device_id elan_id[] = { 1248 { DRIVER_NAME, 0 }, 1249 { }, 1250 }; 1251 MODULE_DEVICE_TABLE(i2c, elan_id); 1252 1253 #ifdef CONFIG_ACPI 1254 static const struct acpi_device_id elan_acpi_id[] = { 1255 { "ELAN0000", 0 }, 1256 { "ELAN0100", 0 }, 1257 { "ELAN0600", 0 }, 1258 { "ELAN0602", 0 }, 1259 { "ELAN0605", 0 }, 1260 { "ELAN0608", 0 }, 1261 { "ELAN0609", 0 }, 1262 { "ELAN060B", 0 }, 1263 { "ELAN060C", 0 }, 1264 { "ELAN0611", 0 }, 1265 { "ELAN1000", 0 }, 1266 { } 1267 }; 1268 MODULE_DEVICE_TABLE(acpi, elan_acpi_id); 1269 #endif 1270 1271 #ifdef CONFIG_OF 1272 static const struct of_device_id elan_of_match[] = { 1273 { .compatible = "elan,ekth3000" }, 1274 { /* sentinel */ } 1275 }; 1276 MODULE_DEVICE_TABLE(of, elan_of_match); 1277 #endif 1278 1279 static struct i2c_driver elan_driver = { 1280 .driver = { 1281 .name = DRIVER_NAME, 1282 .pm = &elan_pm_ops, 1283 .acpi_match_table = ACPI_PTR(elan_acpi_id), 1284 .of_match_table = of_match_ptr(elan_of_match), 1285 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1286 }, 1287 .probe = elan_probe, 1288 .id_table = elan_id, 1289 }; 1290 1291 module_i2c_driver(elan_driver); 1292 1293 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>"); 1294 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver"); 1295 MODULE_LICENSE("GPL"); 1296