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