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