1 // SPDX-License-Identifier: GPL-2.0-only
3 * Elan I2C/SMBus Touchpad driver
5 * Copyright (c) 2013 ELAN Microelectronics Corp.
7 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
8 * Author: KT Liao <kt.liao@emc.com.tw>
11 * Based on cyapa driver:
12 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
13 * copyright (c) 2011-2012 Google, Inc.
15 * Trademarks are the property of their respective owners.
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>
36 #include <linux/property.h>
37 #include <linux/input/elan-i2c-ids.h>
38 #include <linux/regulator/consumer.h>
39 #include <asm/unaligned.h>
43 #define DRIVER_NAME "elan_i2c"
44 #define ELAN_VENDOR_ID 0x04f3
45 #define ETP_MAX_PRESSURE 255
46 #define ETP_FWIDTH_REDUCE 90
47 #define ETP_FINGER_WIDTH 15
48 #define ETP_RETRY_COUNT 3
50 #define ETP_MAX_FINGERS 5
51 #define ETP_FINGER_DATA_LEN 5
52 #define ETP_REPORT_ID 0x5D
53 #define ETP_REPORT_ID2 0x60 /* High precision report */
54 #define ETP_TP_REPORT_ID 0x5E
55 #define ETP_REPORT_ID_OFFSET 2
56 #define ETP_TOUCH_INFO_OFFSET 3
57 #define ETP_FINGER_DATA_OFFSET 4
58 #define ETP_HOVER_INFO_OFFSET 30
59 #define ETP_MK_DATA_OFFSET 33 /* For high precision reports */
60 #define ETP_MAX_REPORT_LEN 39
62 /* The main device structure */
64 struct i2c_client *client;
65 struct input_dev *input;
66 struct input_dev *tp_input; /* trackpoint input node */
67 struct regulator *vcc;
69 const struct elan_transport_ops *ops;
72 struct completion fw_completion;
75 struct mutex sysfs_mutex;
90 unsigned int report_features;
91 unsigned int report_len;
92 int pressure_adjustment;
95 u16 fw_validpage_count;
97 u32 fw_signature_address;
108 static int elan_get_fwinfo(u16 ic_type, u8 iap_version, u16 *validpage_count,
109 u32 *signature_address, u16 *page_size)
115 *validpage_count = 512;
123 *validpage_count = 768;
126 *validpage_count = 896;
129 *validpage_count = 640;
132 *validpage_count = 1024;
135 *validpage_count = 1280;
138 *validpage_count = 2048;
141 *validpage_count = 1024;
144 /* unknown ic type clear value */
145 *validpage_count = 0;
146 *signature_address = 0;
152 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
154 if (ic_type == 0x14 && iap_version >= 2) {
155 *validpage_count /= 8;
156 *page_size = ETP_FW_PAGE_SIZE_512;
157 } else if (ic_type >= 0x0D && iap_version >= 1) {
158 *validpage_count /= 2;
159 *page_size = ETP_FW_PAGE_SIZE_128;
161 *page_size = ETP_FW_PAGE_SIZE;
167 static int elan_enable_power(struct elan_tp_data *data)
169 int repeat = ETP_RETRY_COUNT;
172 error = regulator_enable(data->vcc);
174 dev_err(&data->client->dev,
175 "failed to enable regulator: %d\n", error);
180 error = data->ops->power_control(data->client, true);
185 } while (--repeat > 0);
187 dev_err(&data->client->dev, "failed to enable power: %d\n", error);
191 static int elan_disable_power(struct elan_tp_data *data)
193 int repeat = ETP_RETRY_COUNT;
197 error = data->ops->power_control(data->client, false);
199 error = regulator_disable(data->vcc);
201 dev_err(&data->client->dev,
202 "failed to disable regulator: %d\n",
204 /* Attempt to power the chip back up */
205 data->ops->power_control(data->client, true);
213 } while (--repeat > 0);
215 dev_err(&data->client->dev, "failed to disable power: %d\n", error);
219 static int elan_sleep(struct elan_tp_data *data)
221 int repeat = ETP_RETRY_COUNT;
225 error = data->ops->sleep_control(data->client, true);
230 } while (--repeat > 0);
235 static int elan_query_product(struct elan_tp_data *data)
239 error = data->ops->get_product_id(data->client, &data->product_id);
243 error = data->ops->get_pattern(data->client, &data->pattern);
247 error = data->ops->get_sm_version(data->client, data->pattern,
248 &data->ic_type, &data->sm_version,
256 static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
258 if (data->ic_type == 0x0E) {
259 switch (data->product_id) {
265 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
266 /* ASUS EeeBook X205TA */
273 static int __elan_initialize(struct elan_tp_data *data)
275 struct i2c_client *client = data->client;
276 bool woken_up = false;
279 error = data->ops->initialize(client);
281 dev_err(&client->dev, "device initialize failed: %d\n", error);
285 error = elan_query_product(data);
290 * Some ASUS devices were shipped with firmware that requires
291 * touchpads to be woken up first, before attempting to switch
292 * them into absolute reporting mode.
294 if (elan_check_ASUS_special_fw(data)) {
295 error = data->ops->sleep_control(client, false);
297 dev_err(&client->dev,
298 "failed to wake device up: %d\n", error);
306 data->mode |= ETP_ENABLE_ABS;
307 error = data->ops->set_mode(client, data->mode);
309 dev_err(&client->dev,
310 "failed to switch to absolute mode: %d\n", error);
315 error = data->ops->sleep_control(client, false);
317 dev_err(&client->dev,
318 "failed to wake device up: %d\n", error);
326 static int elan_initialize(struct elan_tp_data *data)
328 int repeat = ETP_RETRY_COUNT;
332 error = __elan_initialize(data);
337 } while (--repeat > 0);
342 static int elan_query_device_info(struct elan_tp_data *data)
346 error = data->ops->get_version(data->client, data->pattern, false,
351 error = data->ops->get_checksum(data->client, false,
356 error = data->ops->get_version(data->client, data->pattern,
357 true, &data->iap_version);
361 error = data->ops->get_pressure_adjustment(data->client,
362 &data->pressure_adjustment);
366 error = data->ops->get_report_features(data->client, data->pattern,
367 &data->report_features,
372 error = elan_get_fwinfo(data->ic_type, data->iap_version,
373 &data->fw_validpage_count,
374 &data->fw_signature_address,
375 &data->fw_page_size);
377 dev_warn(&data->client->dev,
378 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
379 data->iap_version, data->ic_type);
384 static unsigned int elan_convert_resolution(u8 val, u8 pattern)
388 * (value from firmware) * 10 + 790 = dpi
390 * ((value from firmware) + 3) * 100 = dpi
392 int res = pattern <= 0x01 ?
393 (int)(char)val * 10 + 790 : ((int)(char)val + 3) * 100;
395 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
398 return res * 10 / 254;
401 static int elan_query_device_parameters(struct elan_tp_data *data)
403 struct i2c_client *client = data->client;
404 unsigned int x_traces, y_traces;
406 u8 hw_x_res, hw_y_res;
409 if (device_property_read_u32(&client->dev,
410 "touchscreen-size-x", &data->max_x) ||
411 device_property_read_u32(&client->dev,
412 "touchscreen-size-y", &data->max_y)) {
413 error = data->ops->get_max(data->client,
419 /* size is the maximum + 1 */
424 if (device_property_read_u32(&client->dev,
427 device_property_read_u32(&client->dev,
430 error = data->ops->get_num_traces(data->client,
431 &x_traces, &y_traces);
435 data->width_x = data->max_x / x_traces;
436 data->width_y = data->max_y / y_traces;
438 if (device_property_read_u32(&client->dev,
439 "touchscreen-x-mm", &x_mm) ||
440 device_property_read_u32(&client->dev,
441 "touchscreen-y-mm", &y_mm)) {
442 error = data->ops->get_resolution(data->client,
443 &hw_x_res, &hw_y_res);
447 data->x_res = elan_convert_resolution(hw_x_res, data->pattern);
448 data->y_res = elan_convert_resolution(hw_y_res, data->pattern);
450 data->x_res = (data->max_x + 1) / x_mm;
451 data->y_res = (data->max_y + 1) / y_mm;
454 if (device_property_read_bool(&client->dev, "elan,clickpad"))
457 if (device_property_read_bool(&client->dev, "elan,middle-button"))
458 data->middle_button = true;
464 **********************************************************
465 * IAP firmware updater related routines
466 **********************************************************
468 static int elan_write_fw_block(struct elan_tp_data *data, u16 page_size,
469 const u8 *page, u16 checksum, int idx)
471 int retry = ETP_RETRY_COUNT;
475 error = data->ops->write_fw_block(data->client, page_size,
476 page, checksum, idx);
480 dev_dbg(&data->client->dev,
481 "IAP retrying page %d (error: %d)\n", idx, error);
482 } while (--retry > 0);
487 static int __elan_update_firmware(struct elan_tp_data *data,
488 const struct firmware *fw)
490 struct i2c_client *client = data->client;
491 struct device *dev = &client->dev;
496 u16 sw_checksum = 0, fw_checksum = 0;
498 error = data->ops->prepare_fw_update(client, data->ic_type,
503 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
505 boot_page_count = (iap_start_addr * 2) / data->fw_page_size;
506 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
508 const u8 *page = &fw->data[i * data->fw_page_size];
510 for (j = 0; j < data->fw_page_size; j += 2)
511 checksum += ((page[j + 1] << 8) | page[j]);
513 error = elan_write_fw_block(data, data->fw_page_size,
516 dev_err(dev, "write page %d fail: %d\n", i, error);
520 sw_checksum += checksum;
523 /* Wait WDT reset and power on reset */
526 error = data->ops->finish_fw_update(client, &data->fw_completion);
530 error = data->ops->get_checksum(client, true, &fw_checksum);
534 if (sw_checksum != fw_checksum) {
535 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
536 sw_checksum, fw_checksum);
543 static int elan_update_firmware(struct elan_tp_data *data,
544 const struct firmware *fw)
546 struct i2c_client *client = data->client;
549 dev_dbg(&client->dev, "Starting firmware update....\n");
551 disable_irq(client->irq);
552 data->in_fw_update = true;
554 retval = __elan_update_firmware(data, fw);
556 dev_err(&client->dev, "firmware update failed: %d\n", retval);
557 data->ops->iap_reset(client);
559 /* Reinitialize TP after fw is updated */
560 elan_initialize(data);
561 elan_query_device_info(data);
564 data->in_fw_update = false;
565 enable_irq(client->irq);
571 *******************************************************************
573 *******************************************************************
575 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
576 struct device_attribute *attr,
579 struct i2c_client *client = to_i2c_client(dev);
580 struct elan_tp_data *data = i2c_get_clientdata(client);
582 return sprintf(buf, "0x%04x\n", data->fw_checksum);
585 static ssize_t elan_sysfs_read_product_id(struct device *dev,
586 struct device_attribute *attr,
589 struct i2c_client *client = to_i2c_client(dev);
590 struct elan_tp_data *data = i2c_get_clientdata(client);
592 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
596 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
597 struct device_attribute *attr,
600 struct i2c_client *client = to_i2c_client(dev);
601 struct elan_tp_data *data = i2c_get_clientdata(client);
603 return sprintf(buf, "%d.0\n", data->fw_version);
606 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
607 struct device_attribute *attr,
610 struct i2c_client *client = to_i2c_client(dev);
611 struct elan_tp_data *data = i2c_get_clientdata(client);
613 return sprintf(buf, "%d.0\n", data->sm_version);
616 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
617 struct device_attribute *attr,
620 struct i2c_client *client = to_i2c_client(dev);
621 struct elan_tp_data *data = i2c_get_clientdata(client);
623 return sprintf(buf, "%d.0\n", data->iap_version);
626 static ssize_t elan_sysfs_update_fw(struct device *dev,
627 struct device_attribute *attr,
628 const char *buf, size_t count)
630 struct elan_tp_data *data = dev_get_drvdata(dev);
631 const struct firmware *fw;
634 const u8 *fw_signature;
635 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
637 if (data->fw_validpage_count == 0)
640 /* Look for a firmware with the product id appended. */
641 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
643 dev_err(dev, "failed to allocate memory for firmware name\n");
647 dev_info(dev, "requesting fw '%s'\n", fw_name);
648 error = request_firmware(&fw, fw_name, dev);
651 dev_err(dev, "failed to request firmware: %d\n", error);
655 /* Firmware file must match signature data */
656 fw_signature = &fw->data[data->fw_signature_address];
657 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
658 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
659 (int)sizeof(signature), signature,
660 (int)sizeof(signature), fw_signature);
665 error = mutex_lock_interruptible(&data->sysfs_mutex);
669 error = elan_update_firmware(data, fw);
671 mutex_unlock(&data->sysfs_mutex);
674 release_firmware(fw);
675 return error ?: count;
678 static ssize_t calibrate_store(struct device *dev,
679 struct device_attribute *attr,
680 const char *buf, size_t count)
682 struct i2c_client *client = to_i2c_client(dev);
683 struct elan_tp_data *data = i2c_get_clientdata(client);
687 u8 val[ETP_CALIBRATE_MAX_LEN];
689 retval = mutex_lock_interruptible(&data->sysfs_mutex);
693 disable_irq(client->irq);
695 data->mode |= ETP_ENABLE_CALIBRATE;
696 retval = data->ops->set_mode(client, data->mode);
698 dev_err(dev, "failed to enable calibration mode: %d\n",
703 retval = data->ops->calibrate(client);
705 dev_err(dev, "failed to start calibration: %d\n",
707 goto out_disable_calibrate;
712 /* Wait 250ms before checking if calibration has completed. */
715 retval = data->ops->calibrate_result(client, val);
717 dev_err(dev, "failed to check calibration result: %d\n",
719 else if (val[0] == 0)
720 break; /* calibration done */
725 dev_err(dev, "failed to calibrate. Timeout.\n");
729 out_disable_calibrate:
730 data->mode &= ~ETP_ENABLE_CALIBRATE;
731 error = data->ops->set_mode(data->client, data->mode);
733 dev_err(dev, "failed to disable calibration mode: %d\n",
739 enable_irq(client->irq);
740 mutex_unlock(&data->sysfs_mutex);
741 return retval ?: count;
744 static ssize_t elan_sysfs_read_mode(struct device *dev,
745 struct device_attribute *attr,
748 struct i2c_client *client = to_i2c_client(dev);
749 struct elan_tp_data *data = i2c_get_clientdata(client);
753 error = mutex_lock_interruptible(&data->sysfs_mutex);
757 error = data->ops->iap_get_mode(data->client, &mode);
759 mutex_unlock(&data->sysfs_mutex);
764 return sprintf(buf, "%d\n", (int)mode);
767 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
768 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
769 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
770 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
771 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
772 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
773 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
775 static DEVICE_ATTR_WO(calibrate);
777 static struct attribute *elan_sysfs_entries[] = {
778 &dev_attr_product_id.attr,
779 &dev_attr_firmware_version.attr,
780 &dev_attr_sample_version.attr,
781 &dev_attr_iap_version.attr,
782 &dev_attr_fw_checksum.attr,
783 &dev_attr_calibrate.attr,
785 &dev_attr_update_fw.attr,
789 static const struct attribute_group elan_sysfs_group = {
790 .attrs = elan_sysfs_entries,
793 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
796 struct i2c_client *client = to_i2c_client(dev);
797 struct elan_tp_data *data = i2c_get_clientdata(client);
801 retval = mutex_lock_interruptible(&data->sysfs_mutex);
805 disable_irq(client->irq);
807 data->baseline_ready = false;
809 data->mode |= ETP_ENABLE_CALIBRATE;
810 retval = data->ops->set_mode(data->client, data->mode);
812 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
819 retval = data->ops->get_baseline_data(data->client, true,
820 &data->max_baseline);
822 dev_err(dev, "Failed to read max baseline form device: %d\n",
824 goto out_disable_calibrate;
827 retval = data->ops->get_baseline_data(data->client, false,
828 &data->min_baseline);
830 dev_err(dev, "Failed to read min baseline form device: %d\n",
832 goto out_disable_calibrate;
835 data->baseline_ready = true;
837 out_disable_calibrate:
838 data->mode &= ~ETP_ENABLE_CALIBRATE;
839 error = data->ops->set_mode(data->client, data->mode);
841 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
847 enable_irq(client->irq);
848 mutex_unlock(&data->sysfs_mutex);
849 return retval ?: count;
852 static ssize_t min_show(struct device *dev,
853 struct device_attribute *attr, char *buf)
855 struct i2c_client *client = to_i2c_client(dev);
856 struct elan_tp_data *data = i2c_get_clientdata(client);
859 retval = mutex_lock_interruptible(&data->sysfs_mutex);
863 if (!data->baseline_ready) {
868 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
871 mutex_unlock(&data->sysfs_mutex);
875 static ssize_t max_show(struct device *dev,
876 struct device_attribute *attr, char *buf)
878 struct i2c_client *client = to_i2c_client(dev);
879 struct elan_tp_data *data = i2c_get_clientdata(client);
882 retval = mutex_lock_interruptible(&data->sysfs_mutex);
886 if (!data->baseline_ready) {
891 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
894 mutex_unlock(&data->sysfs_mutex);
899 static DEVICE_ATTR_WO(acquire);
900 static DEVICE_ATTR_RO(min);
901 static DEVICE_ATTR_RO(max);
903 static struct attribute *elan_baseline_sysfs_entries[] = {
904 &dev_attr_acquire.attr,
910 static const struct attribute_group elan_baseline_sysfs_group = {
912 .attrs = elan_baseline_sysfs_entries,
915 static const struct attribute_group *elan_sysfs_groups[] = {
917 &elan_baseline_sysfs_group,
922 ******************************************************************
924 ******************************************************************
926 static void elan_report_contact(struct elan_tp_data *data, int contact_num,
927 bool contact_valid, bool high_precision,
928 u8 *packet, u8 *finger_data)
930 struct input_dev *input = data->input;
931 unsigned int pos_x, pos_y;
932 unsigned int pressure, scaled_pressure;
935 if (high_precision) {
936 pos_x = get_unaligned_be16(&finger_data[0]);
937 pos_y = get_unaligned_be16(&finger_data[2]);
939 pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1];
940 pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2];
943 if (pos_x > data->max_x || pos_y > data->max_y) {
944 dev_dbg(input->dev.parent,
945 "[%d] x=%d y=%d over max (%d, %d)",
946 contact_num, pos_x, pos_y,
947 data->max_x, data->max_y);
951 pressure = finger_data[4];
952 scaled_pressure = pressure + data->pressure_adjustment;
953 if (scaled_pressure > ETP_MAX_PRESSURE)
954 scaled_pressure = ETP_MAX_PRESSURE;
956 input_mt_slot(input, contact_num);
957 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
958 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
959 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
960 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
962 if (data->report_features & ETP_FEATURE_REPORT_MK) {
963 unsigned int mk_x, mk_y, area_x, area_y;
964 u8 mk_data = high_precision ?
965 packet[ETP_MK_DATA_OFFSET + contact_num] :
968 mk_x = mk_data & 0x0f;
972 * To avoid treating large finger as palm, let's reduce
973 * the width x and y per trace.
975 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
976 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
978 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
979 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
980 max(area_x, area_y));
981 input_report_abs(input, ABS_MT_TOUCH_MINOR,
982 min(area_x, area_y));
985 input_mt_slot(input, contact_num);
986 input_mt_report_slot_inactive(input);
990 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet,
993 struct input_dev *input = data->input;
994 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
996 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
997 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
998 bool contact_valid, hover_event;
1000 pm_wakeup_event(&data->client->dev, 0);
1002 hover_event = hover_info & BIT(6);
1004 for (i = 0; i < ETP_MAX_FINGERS; i++) {
1005 contact_valid = tp_info & BIT(3 + i);
1006 elan_report_contact(data, i, contact_valid, high_precision,
1007 packet, finger_data);
1009 finger_data += ETP_FINGER_DATA_LEN;
1012 input_report_key(input, BTN_LEFT, tp_info & BIT(0));
1013 input_report_key(input, BTN_MIDDLE, tp_info & BIT(2));
1014 input_report_key(input, BTN_RIGHT, tp_info & BIT(1));
1015 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
1016 input_mt_report_pointer_emulation(input, true);
1020 static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report)
1022 struct input_dev *input = data->tp_input;
1023 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1];
1026 pm_wakeup_event(&data->client->dev, 0);
1028 if (!data->tp_input) {
1029 dev_warn_once(&data->client->dev,
1030 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
1034 input_report_key(input, BTN_LEFT, packet[0] & 0x01);
1035 input_report_key(input, BTN_RIGHT, packet[0] & 0x02);
1036 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04);
1038 if ((packet[3] & 0x0F) == 0x06) {
1039 x = packet[4] - (int)((packet[1] ^ 0x80) << 1);
1040 y = (int)((packet[2] ^ 0x80) << 1) - packet[5];
1042 input_report_rel(input, REL_X, x);
1043 input_report_rel(input, REL_Y, y);
1049 static irqreturn_t elan_isr(int irq, void *dev_id)
1051 struct elan_tp_data *data = dev_id;
1053 u8 report[ETP_MAX_REPORT_LEN];
1056 * When device is connected to i2c bus, when all IAP page writes
1057 * complete, the driver will receive interrupt and must read
1058 * 0000 to confirm that IAP is finished.
1060 if (data->in_fw_update) {
1061 complete(&data->fw_completion);
1065 error = data->ops->get_report(data->client, report, data->report_len);
1069 switch (report[ETP_REPORT_ID_OFFSET]) {
1071 elan_report_absolute(data, report, false);
1073 case ETP_REPORT_ID2:
1074 elan_report_absolute(data, report, true);
1076 case ETP_TP_REPORT_ID:
1077 elan_report_trackpoint(data, report);
1080 dev_err(&data->client->dev, "invalid report id data (%x)\n",
1081 report[ETP_REPORT_ID_OFFSET]);
1089 ******************************************************************
1090 * Elan initialization functions
1091 ******************************************************************
1094 static int elan_setup_trackpoint_input_device(struct elan_tp_data *data)
1096 struct device *dev = &data->client->dev;
1097 struct input_dev *input;
1099 input = devm_input_allocate_device(dev);
1103 input->name = "Elan TrackPoint";
1104 input->id.bustype = BUS_I2C;
1105 input->id.vendor = ELAN_VENDOR_ID;
1106 input->id.product = data->product_id;
1107 input_set_drvdata(input, data);
1109 input_set_capability(input, EV_REL, REL_X);
1110 input_set_capability(input, EV_REL, REL_Y);
1111 input_set_capability(input, EV_KEY, BTN_LEFT);
1112 input_set_capability(input, EV_KEY, BTN_RIGHT);
1113 input_set_capability(input, EV_KEY, BTN_MIDDLE);
1115 __set_bit(INPUT_PROP_POINTER, input->propbit);
1116 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit);
1118 data->tp_input = input;
1123 static int elan_setup_input_device(struct elan_tp_data *data)
1125 struct device *dev = &data->client->dev;
1126 struct input_dev *input;
1127 unsigned int max_width = max(data->width_x, data->width_y);
1128 unsigned int min_width = min(data->width_x, data->width_y);
1131 input = devm_input_allocate_device(dev);
1135 input->name = "Elan Touchpad";
1136 input->id.bustype = BUS_I2C;
1137 input->id.vendor = ELAN_VENDOR_ID;
1138 input->id.product = data->product_id;
1139 input_set_drvdata(input, data);
1141 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
1142 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
1144 dev_err(dev, "failed to initialize MT slots: %d\n", error);
1148 __set_bit(EV_ABS, input->evbit);
1149 __set_bit(INPUT_PROP_POINTER, input->propbit);
1150 if (data->clickpad) {
1151 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1153 __set_bit(BTN_RIGHT, input->keybit);
1154 if (data->middle_button)
1155 __set_bit(BTN_MIDDLE, input->keybit);
1157 __set_bit(BTN_LEFT, input->keybit);
1159 /* Set up ST parameters */
1160 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
1161 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
1162 input_abs_set_res(input, ABS_X, data->x_res);
1163 input_abs_set_res(input, ABS_Y, data->y_res);
1164 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
1165 if (data->report_features & ETP_FEATURE_REPORT_MK)
1166 input_set_abs_params(input, ABS_TOOL_WIDTH,
1167 0, ETP_FINGER_WIDTH, 0, 0);
1168 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
1170 /* And MT parameters */
1171 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
1172 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
1173 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
1174 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
1175 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
1176 ETP_MAX_PRESSURE, 0, 0);
1177 if (data->report_features & ETP_FEATURE_REPORT_MK) {
1178 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1179 0, ETP_FINGER_WIDTH * max_width, 0, 0);
1180 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1181 0, ETP_FINGER_WIDTH * min_width, 0, 0);
1184 data->input = input;
1189 static void elan_disable_regulator(void *_data)
1191 struct elan_tp_data *data = _data;
1193 regulator_disable(data->vcc);
1196 static int elan_probe(struct i2c_client *client,
1197 const struct i2c_device_id *dev_id)
1199 const struct elan_transport_ops *transport_ops;
1200 struct device *dev = &client->dev;
1201 struct elan_tp_data *data;
1202 unsigned long irqflags;
1205 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1206 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1207 transport_ops = &elan_i2c_ops;
1208 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1209 i2c_check_functionality(client->adapter,
1210 I2C_FUNC_SMBUS_BYTE_DATA |
1211 I2C_FUNC_SMBUS_BLOCK_DATA |
1212 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1213 transport_ops = &elan_smbus_ops;
1215 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1219 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1223 i2c_set_clientdata(client, data);
1225 data->ops = transport_ops;
1226 data->client = client;
1227 init_completion(&data->fw_completion);
1228 mutex_init(&data->sysfs_mutex);
1230 data->vcc = devm_regulator_get(dev, "vcc");
1231 if (IS_ERR(data->vcc)) {
1232 error = PTR_ERR(data->vcc);
1233 if (error != -EPROBE_DEFER)
1234 dev_err(dev, "Failed to get 'vcc' regulator: %d\n",
1239 error = regulator_enable(data->vcc);
1241 dev_err(dev, "Failed to enable regulator: %d\n", error);
1245 error = devm_add_action_or_reset(dev, elan_disable_regulator, data);
1247 dev_err(dev, "Failed to add disable regulator action: %d\n",
1252 /* Make sure there is something at this address */
1253 error = i2c_smbus_read_byte(client);
1255 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1259 /* Initialize the touchpad. */
1260 error = elan_initialize(data);
1264 error = elan_query_device_info(data);
1268 error = elan_query_device_parameters(data);
1273 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1280 "Elan Touchpad Extra Information:\n"
1281 " Max ABS X,Y: %d,%d\n"
1282 " Width X,Y: %d,%d\n"
1283 " Resolution X,Y: %d,%d (dots/mm)\n"
1285 " info pattern: 0x%x\n",
1286 data->max_x, data->max_y,
1287 data->width_x, data->width_y,
1288 data->x_res, data->y_res,
1289 data->ic_type, data->pattern);
1291 /* Set up input device properties based on queried parameters. */
1292 error = elan_setup_input_device(data);
1296 if (device_property_read_bool(&client->dev, "elan,trackpoint")) {
1297 error = elan_setup_trackpoint_input_device(data);
1303 * Platform code (ACPI, DTS) should normally set up interrupt
1304 * for us, but in case it did not let's fall back to using falling
1305 * edge to be compatible with older Chromebooks.
1307 irqflags = irq_get_trigger_type(client->irq);
1309 irqflags = IRQF_TRIGGER_FALLING;
1311 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1312 irqflags | IRQF_ONESHOT,
1313 client->name, data);
1315 dev_err(dev, "cannot register irq=%d\n", client->irq);
1319 error = devm_device_add_groups(dev, elan_sysfs_groups);
1321 dev_err(dev, "failed to create sysfs attributes: %d\n", error);
1325 error = input_register_device(data->input);
1327 dev_err(dev, "failed to register input device: %d\n", error);
1331 if (data->tp_input) {
1332 error = input_register_device(data->tp_input);
1334 dev_err(&client->dev,
1335 "failed to register TrackPoint input device: %d\n",
1342 * Systems using device tree should set up wakeup via DTS,
1343 * the rest will configure device as wakeup source by default.
1346 device_init_wakeup(dev, true);
1351 static int __maybe_unused elan_suspend(struct device *dev)
1353 struct i2c_client *client = to_i2c_client(dev);
1354 struct elan_tp_data *data = i2c_get_clientdata(client);
1358 * We are taking the mutex to make sure sysfs operations are
1359 * complete before we attempt to bring the device into low[er]
1362 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1366 disable_irq(client->irq);
1368 if (device_may_wakeup(dev)) {
1369 ret = elan_sleep(data);
1370 /* Enable wake from IRQ */
1371 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1373 ret = elan_disable_power(data);
1376 mutex_unlock(&data->sysfs_mutex);
1380 static int __maybe_unused elan_resume(struct device *dev)
1382 struct i2c_client *client = to_i2c_client(dev);
1383 struct elan_tp_data *data = i2c_get_clientdata(client);
1386 if (device_may_wakeup(dev) && data->irq_wake) {
1387 disable_irq_wake(client->irq);
1388 data->irq_wake = false;
1391 error = elan_enable_power(data);
1393 dev_err(dev, "power up when resuming failed: %d\n", error);
1397 error = elan_initialize(data);
1399 dev_err(dev, "initialize when resuming failed: %d\n", error);
1402 enable_irq(data->client->irq);
1406 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1408 static const struct i2c_device_id elan_id[] = {
1412 MODULE_DEVICE_TABLE(i2c, elan_id);
1415 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1419 static const struct of_device_id elan_of_match[] = {
1420 { .compatible = "elan,ekth3000" },
1423 MODULE_DEVICE_TABLE(of, elan_of_match);
1426 static struct i2c_driver elan_driver = {
1428 .name = DRIVER_NAME,
1430 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1431 .of_match_table = of_match_ptr(elan_of_match),
1432 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1434 .probe = elan_probe,
1435 .id_table = elan_id,
1438 module_i2c_driver(elan_driver);
1440 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1441 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1442 MODULE_LICENSE("GPL");