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