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