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