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