2 * Elan I2C/SMBus Touchpad driver
4 * Copyright (c) 2013 ELAN Microelectronics Corp.
6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
7 * Author: KT Liao <kt.liao@emc.com.tw>
10 * Based on cyapa driver:
11 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
12 * copyright (c) 2011-2012 Google, Inc.
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as published
16 * by the Free Software Foundation.
18 * Trademarks are the property of their respective owners.
21 #include <linux/acpi.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/firmware.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/input/mt.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/input.h>
35 #include <linux/uaccess.h>
36 #include <linux/jiffies.h>
37 #include <linux/completion.h>
39 #include <linux/property.h>
40 #include <linux/regulator/consumer.h>
41 #include <asm/unaligned.h>
45 #define DRIVER_NAME "elan_i2c"
46 #define ELAN_VENDOR_ID 0x04f3
47 #define ETP_MAX_PRESSURE 255
48 #define ETP_FWIDTH_REDUCE 90
49 #define ETP_FINGER_WIDTH 15
50 #define ETP_RETRY_COUNT 3
52 #define ETP_MAX_FINGERS 5
53 #define ETP_FINGER_DATA_LEN 5
54 #define ETP_REPORT_ID 0x5D
55 #define ETP_TP_REPORT_ID 0x5E
56 #define ETP_REPORT_ID_OFFSET 2
57 #define ETP_TOUCH_INFO_OFFSET 3
58 #define ETP_FINGER_DATA_OFFSET 4
59 #define ETP_HOVER_INFO_OFFSET 30
60 #define ETP_MAX_REPORT_LEN 34
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 int pressure_adjustment;
93 u16 fw_validpage_count;
94 u16 fw_signature_address;
104 static int elan_get_fwinfo(u16 ic_type, u16 *validpage_count,
105 u16 *signature_address)
111 *validpage_count = 512;
119 *validpage_count = 768;
122 *validpage_count = 896;
125 *validpage_count = 640;
128 *validpage_count = 1024;
131 /* unknown ic type clear value */
132 *validpage_count = 0;
133 *signature_address = 0;
138 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
143 static int elan_set_power(struct elan_tp_data *data, bool on)
145 int repeat = ETP_RETRY_COUNT;
149 error = data->ops->power_control(data->client, on);
154 } while (--repeat > 0);
156 dev_err(&data->client->dev, "failed to set power %s: %d\n",
157 on ? "on" : "off", error);
161 static int elan_sleep(struct elan_tp_data *data)
163 int repeat = ETP_RETRY_COUNT;
167 error = data->ops->sleep_control(data->client, true);
172 } while (--repeat > 0);
177 static int elan_query_product(struct elan_tp_data *data)
181 error = data->ops->get_product_id(data->client, &data->product_id);
185 error = data->ops->get_sm_version(data->client, &data->ic_type,
186 &data->sm_version, &data->clickpad);
193 static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
195 if (data->ic_type == 0x0E) {
196 switch (data->product_id) {
202 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
203 /* ASUS EeeBook X205TA */
210 static int __elan_initialize(struct elan_tp_data *data)
212 struct i2c_client *client = data->client;
213 bool woken_up = false;
216 error = data->ops->initialize(client);
218 dev_err(&client->dev, "device initialize failed: %d\n", error);
222 error = elan_query_product(data);
227 * Some ASUS devices were shipped with firmware that requires
228 * touchpads to be woken up first, before attempting to switch
229 * them into absolute reporting mode.
231 if (elan_check_ASUS_special_fw(data)) {
232 error = data->ops->sleep_control(client, false);
234 dev_err(&client->dev,
235 "failed to wake device up: %d\n", error);
243 data->mode |= ETP_ENABLE_ABS;
244 error = data->ops->set_mode(client, data->mode);
246 dev_err(&client->dev,
247 "failed to switch to absolute mode: %d\n", error);
252 error = data->ops->sleep_control(client, false);
254 dev_err(&client->dev,
255 "failed to wake device up: %d\n", error);
263 static int elan_initialize(struct elan_tp_data *data)
265 int repeat = ETP_RETRY_COUNT;
269 error = __elan_initialize(data);
274 } while (--repeat > 0);
279 static int elan_query_device_info(struct elan_tp_data *data)
284 error = data->ops->get_version(data->client, false, &data->fw_version);
288 error = data->ops->get_checksum(data->client, false,
293 error = data->ops->get_version(data->client, true, &data->iap_version);
297 error = data->ops->get_pressure_adjustment(data->client,
298 &data->pressure_adjustment);
302 error = data->ops->get_pattern(data->client, &data->pattern);
306 if (data->pattern == 0x01)
307 ic_type = data->ic_type;
309 ic_type = data->iap_version;
311 error = elan_get_fwinfo(ic_type, &data->fw_validpage_count,
312 &data->fw_signature_address);
314 dev_warn(&data->client->dev,
315 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
316 data->iap_version, data->ic_type);
321 static unsigned int elan_convert_resolution(u8 val)
324 * (value from firmware) * 10 + 790 = dpi
326 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
330 return ((int)(char)val * 10 + 790) * 10 / 254;
333 static int elan_query_device_parameters(struct elan_tp_data *data)
335 unsigned int x_traces, y_traces;
336 u8 hw_x_res, hw_y_res;
339 error = data->ops->get_max(data->client, &data->max_x, &data->max_y);
343 error = data->ops->get_num_traces(data->client, &x_traces, &y_traces);
347 data->width_x = data->max_x / x_traces;
348 data->width_y = data->max_y / y_traces;
350 error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res);
354 data->x_res = elan_convert_resolution(hw_x_res);
355 data->y_res = elan_convert_resolution(hw_y_res);
361 **********************************************************
362 * IAP firmware updater related routines
363 **********************************************************
365 static int elan_write_fw_block(struct elan_tp_data *data,
366 const u8 *page, u16 checksum, int idx)
368 int retry = ETP_RETRY_COUNT;
372 error = data->ops->write_fw_block(data->client,
373 page, checksum, idx);
377 dev_dbg(&data->client->dev,
378 "IAP retrying page %d (error: %d)\n", idx, error);
379 } while (--retry > 0);
384 static int __elan_update_firmware(struct elan_tp_data *data,
385 const struct firmware *fw)
387 struct i2c_client *client = data->client;
388 struct device *dev = &client->dev;
393 u16 sw_checksum = 0, fw_checksum = 0;
395 error = data->ops->prepare_fw_update(client);
399 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
401 boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE;
402 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
404 const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE];
406 for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2)
407 checksum += ((page[j + 1] << 8) | page[j]);
409 error = elan_write_fw_block(data, page, checksum, i);
411 dev_err(dev, "write page %d fail: %d\n", i, error);
415 sw_checksum += checksum;
418 /* Wait WDT reset and power on reset */
421 error = data->ops->finish_fw_update(client, &data->fw_completion);
425 error = data->ops->get_checksum(client, true, &fw_checksum);
429 if (sw_checksum != fw_checksum) {
430 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
431 sw_checksum, fw_checksum);
438 static int elan_update_firmware(struct elan_tp_data *data,
439 const struct firmware *fw)
441 struct i2c_client *client = data->client;
444 dev_dbg(&client->dev, "Starting firmware update....\n");
446 disable_irq(client->irq);
447 data->in_fw_update = true;
449 retval = __elan_update_firmware(data, fw);
451 dev_err(&client->dev, "firmware update failed: %d\n", retval);
452 data->ops->iap_reset(client);
454 /* Reinitialize TP after fw is updated */
455 elan_initialize(data);
456 elan_query_device_info(data);
459 data->in_fw_update = false;
460 enable_irq(client->irq);
466 *******************************************************************
468 *******************************************************************
470 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
471 struct device_attribute *attr,
474 struct i2c_client *client = to_i2c_client(dev);
475 struct elan_tp_data *data = i2c_get_clientdata(client);
477 return sprintf(buf, "0x%04x\n", data->fw_checksum);
480 static ssize_t elan_sysfs_read_product_id(struct device *dev,
481 struct device_attribute *attr,
484 struct i2c_client *client = to_i2c_client(dev);
485 struct elan_tp_data *data = i2c_get_clientdata(client);
487 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
491 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
492 struct device_attribute *attr,
495 struct i2c_client *client = to_i2c_client(dev);
496 struct elan_tp_data *data = i2c_get_clientdata(client);
498 return sprintf(buf, "%d.0\n", data->fw_version);
501 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
502 struct device_attribute *attr,
505 struct i2c_client *client = to_i2c_client(dev);
506 struct elan_tp_data *data = i2c_get_clientdata(client);
508 return sprintf(buf, "%d.0\n", data->sm_version);
511 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
512 struct device_attribute *attr,
515 struct i2c_client *client = to_i2c_client(dev);
516 struct elan_tp_data *data = i2c_get_clientdata(client);
518 return sprintf(buf, "%d.0\n", data->iap_version);
521 static ssize_t elan_sysfs_update_fw(struct device *dev,
522 struct device_attribute *attr,
523 const char *buf, size_t count)
525 struct elan_tp_data *data = dev_get_drvdata(dev);
526 const struct firmware *fw;
529 const u8 *fw_signature;
530 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
532 if (data->fw_validpage_count == 0)
535 /* Look for a firmware with the product id appended. */
536 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
538 dev_err(dev, "failed to allocate memory for firmware name\n");
542 dev_info(dev, "requesting fw '%s'\n", fw_name);
543 error = reject_firmware(&fw, fw_name, dev);
546 dev_err(dev, "failed to request firmware: %d\n", error);
550 /* Firmware file must match signature data */
551 fw_signature = &fw->data[data->fw_signature_address];
552 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
553 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
554 (int)sizeof(signature), signature,
555 (int)sizeof(signature), fw_signature);
560 error = mutex_lock_interruptible(&data->sysfs_mutex);
564 error = elan_update_firmware(data, fw);
566 mutex_unlock(&data->sysfs_mutex);
569 release_firmware(fw);
570 return error ?: count;
573 static ssize_t calibrate_store(struct device *dev,
574 struct device_attribute *attr,
575 const char *buf, size_t count)
577 struct i2c_client *client = to_i2c_client(dev);
578 struct elan_tp_data *data = i2c_get_clientdata(client);
582 u8 val[ETP_CALIBRATE_MAX_LEN];
584 retval = mutex_lock_interruptible(&data->sysfs_mutex);
588 disable_irq(client->irq);
590 data->mode |= ETP_ENABLE_CALIBRATE;
591 retval = data->ops->set_mode(client, data->mode);
593 dev_err(dev, "failed to enable calibration mode: %d\n",
598 retval = data->ops->calibrate(client);
600 dev_err(dev, "failed to start calibration: %d\n",
602 goto out_disable_calibrate;
607 /* Wait 250ms before checking if calibration has completed. */
610 retval = data->ops->calibrate_result(client, val);
612 dev_err(dev, "failed to check calibration result: %d\n",
614 else if (val[0] == 0)
615 break; /* calibration done */
620 dev_err(dev, "failed to calibrate. Timeout.\n");
624 out_disable_calibrate:
625 data->mode &= ~ETP_ENABLE_CALIBRATE;
626 error = data->ops->set_mode(data->client, data->mode);
628 dev_err(dev, "failed to disable calibration mode: %d\n",
634 enable_irq(client->irq);
635 mutex_unlock(&data->sysfs_mutex);
636 return retval ?: count;
639 static ssize_t elan_sysfs_read_mode(struct device *dev,
640 struct device_attribute *attr,
643 struct i2c_client *client = to_i2c_client(dev);
644 struct elan_tp_data *data = i2c_get_clientdata(client);
648 error = mutex_lock_interruptible(&data->sysfs_mutex);
652 error = data->ops->iap_get_mode(data->client, &mode);
654 mutex_unlock(&data->sysfs_mutex);
659 return sprintf(buf, "%d\n", (int)mode);
662 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
663 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
664 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
665 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
666 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
667 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
668 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
670 static DEVICE_ATTR_WO(calibrate);
672 static struct attribute *elan_sysfs_entries[] = {
673 &dev_attr_product_id.attr,
674 &dev_attr_firmware_version.attr,
675 &dev_attr_sample_version.attr,
676 &dev_attr_iap_version.attr,
677 &dev_attr_fw_checksum.attr,
678 &dev_attr_calibrate.attr,
680 &dev_attr_update_fw.attr,
684 static const struct attribute_group elan_sysfs_group = {
685 .attrs = elan_sysfs_entries,
688 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
689 const char *buf, size_t count)
691 struct i2c_client *client = to_i2c_client(dev);
692 struct elan_tp_data *data = i2c_get_clientdata(client);
696 retval = mutex_lock_interruptible(&data->sysfs_mutex);
700 disable_irq(client->irq);
702 data->baseline_ready = false;
704 data->mode |= ETP_ENABLE_CALIBRATE;
705 retval = data->ops->set_mode(data->client, data->mode);
707 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
714 retval = data->ops->get_baseline_data(data->client, true,
715 &data->max_baseline);
717 dev_err(dev, "Failed to read max baseline form device: %d\n",
719 goto out_disable_calibrate;
722 retval = data->ops->get_baseline_data(data->client, false,
723 &data->min_baseline);
725 dev_err(dev, "Failed to read min baseline form device: %d\n",
727 goto out_disable_calibrate;
730 data->baseline_ready = true;
732 out_disable_calibrate:
733 data->mode &= ~ETP_ENABLE_CALIBRATE;
734 error = data->ops->set_mode(data->client, data->mode);
736 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
742 enable_irq(client->irq);
743 mutex_unlock(&data->sysfs_mutex);
744 return retval ?: count;
747 static ssize_t min_show(struct device *dev,
748 struct device_attribute *attr, char *buf)
750 struct i2c_client *client = to_i2c_client(dev);
751 struct elan_tp_data *data = i2c_get_clientdata(client);
754 retval = mutex_lock_interruptible(&data->sysfs_mutex);
758 if (!data->baseline_ready) {
763 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
766 mutex_unlock(&data->sysfs_mutex);
770 static ssize_t max_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
773 struct i2c_client *client = to_i2c_client(dev);
774 struct elan_tp_data *data = i2c_get_clientdata(client);
777 retval = mutex_lock_interruptible(&data->sysfs_mutex);
781 if (!data->baseline_ready) {
786 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
789 mutex_unlock(&data->sysfs_mutex);
794 static DEVICE_ATTR_WO(acquire);
795 static DEVICE_ATTR_RO(min);
796 static DEVICE_ATTR_RO(max);
798 static struct attribute *elan_baseline_sysfs_entries[] = {
799 &dev_attr_acquire.attr,
805 static const struct attribute_group elan_baseline_sysfs_group = {
807 .attrs = elan_baseline_sysfs_entries,
810 static const struct attribute_group *elan_sysfs_groups[] = {
812 &elan_baseline_sysfs_group,
817 ******************************************************************
819 ******************************************************************
821 static void elan_report_contact(struct elan_tp_data *data,
822 int contact_num, bool contact_valid,
825 struct input_dev *input = data->input;
826 unsigned int pos_x, pos_y;
827 unsigned int pressure, mk_x, mk_y;
828 unsigned int area_x, area_y, major, minor;
829 unsigned int scaled_pressure;
832 pos_x = ((finger_data[0] & 0xf0) << 4) |
834 pos_y = ((finger_data[0] & 0x0f) << 8) |
836 mk_x = (finger_data[3] & 0x0f);
837 mk_y = (finger_data[3] >> 4);
838 pressure = finger_data[4];
840 if (pos_x > data->max_x || pos_y > data->max_y) {
841 dev_dbg(input->dev.parent,
842 "[%d] x=%d y=%d over max (%d, %d)",
843 contact_num, pos_x, pos_y,
844 data->max_x, data->max_y);
849 * To avoid treating large finger as palm, let's reduce the
850 * width x and y per trace.
852 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
853 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
855 major = max(area_x, area_y);
856 minor = min(area_x, area_y);
858 scaled_pressure = pressure + data->pressure_adjustment;
860 if (scaled_pressure > ETP_MAX_PRESSURE)
861 scaled_pressure = ETP_MAX_PRESSURE;
863 input_mt_slot(input, contact_num);
864 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
865 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
866 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
867 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
868 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
869 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
870 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
872 input_mt_slot(input, contact_num);
873 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
877 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet)
879 struct input_dev *input = data->input;
880 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
882 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
883 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
884 bool contact_valid, hover_event;
886 hover_event = hover_info & 0x40;
887 for (i = 0; i < ETP_MAX_FINGERS; i++) {
888 contact_valid = tp_info & (1U << (3 + i));
889 elan_report_contact(data, i, contact_valid, finger_data);
892 finger_data += ETP_FINGER_DATA_LEN;
895 input_report_key(input, BTN_LEFT, tp_info & 0x01);
896 input_report_key(input, BTN_RIGHT, tp_info & 0x02);
897 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
898 input_mt_report_pointer_emulation(input, true);
902 static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report)
904 struct input_dev *input = data->tp_input;
905 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1];
908 if (!data->tp_input) {
909 dev_warn_once(&data->client->dev,
910 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
914 input_report_key(input, BTN_LEFT, packet[0] & 0x01);
915 input_report_key(input, BTN_RIGHT, packet[0] & 0x02);
916 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04);
918 if ((packet[3] & 0x0F) == 0x06) {
919 x = packet[4] - (int)((packet[1] ^ 0x80) << 1);
920 y = (int)((packet[2] ^ 0x80) << 1) - packet[5];
922 input_report_rel(input, REL_X, x);
923 input_report_rel(input, REL_Y, y);
929 static irqreturn_t elan_isr(int irq, void *dev_id)
931 struct elan_tp_data *data = dev_id;
932 struct device *dev = &data->client->dev;
934 u8 report[ETP_MAX_REPORT_LEN];
937 * When device is connected to i2c bus, when all IAP page writes
938 * complete, the driver will receive interrupt and must read
939 * 0000 to confirm that IAP is finished.
941 if (data->in_fw_update) {
942 complete(&data->fw_completion);
946 error = data->ops->get_report(data->client, report);
950 switch (report[ETP_REPORT_ID_OFFSET]) {
952 elan_report_absolute(data, report);
954 case ETP_TP_REPORT_ID:
955 elan_report_trackpoint(data, report);
958 dev_err(dev, "invalid report id data (%x)\n",
959 report[ETP_REPORT_ID_OFFSET]);
967 ******************************************************************
968 * Elan initialization functions
969 ******************************************************************
972 static int elan_setup_trackpoint_input_device(struct elan_tp_data *data)
974 struct device *dev = &data->client->dev;
975 struct input_dev *input;
977 input = devm_input_allocate_device(dev);
981 input->name = "Elan TrackPoint";
982 input->id.bustype = BUS_I2C;
983 input->id.vendor = ELAN_VENDOR_ID;
984 input->id.product = data->product_id;
985 input_set_drvdata(input, data);
987 input_set_capability(input, EV_REL, REL_X);
988 input_set_capability(input, EV_REL, REL_Y);
989 input_set_capability(input, EV_KEY, BTN_LEFT);
990 input_set_capability(input, EV_KEY, BTN_RIGHT);
991 input_set_capability(input, EV_KEY, BTN_MIDDLE);
993 __set_bit(INPUT_PROP_POINTER, input->propbit);
994 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit);
996 data->tp_input = input;
1001 static int elan_setup_input_device(struct elan_tp_data *data)
1003 struct device *dev = &data->client->dev;
1004 struct input_dev *input;
1005 unsigned int max_width = max(data->width_x, data->width_y);
1006 unsigned int min_width = min(data->width_x, data->width_y);
1009 input = devm_input_allocate_device(dev);
1013 input->name = "Elan Touchpad";
1014 input->id.bustype = BUS_I2C;
1015 input->id.vendor = ELAN_VENDOR_ID;
1016 input->id.product = data->product_id;
1017 input_set_drvdata(input, data);
1019 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
1020 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
1022 dev_err(dev, "failed to initialize MT slots: %d\n", error);
1026 __set_bit(EV_ABS, input->evbit);
1027 __set_bit(INPUT_PROP_POINTER, input->propbit);
1029 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1031 __set_bit(BTN_RIGHT, input->keybit);
1032 __set_bit(BTN_LEFT, input->keybit);
1034 /* Set up ST parameters */
1035 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
1036 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
1037 input_abs_set_res(input, ABS_X, data->x_res);
1038 input_abs_set_res(input, ABS_Y, data->y_res);
1039 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
1040 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0);
1041 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
1043 /* And MT parameters */
1044 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
1045 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
1046 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
1047 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
1048 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
1049 ETP_MAX_PRESSURE, 0, 0);
1050 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
1051 ETP_FINGER_WIDTH * max_width, 0, 0);
1052 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0,
1053 ETP_FINGER_WIDTH * min_width, 0, 0);
1055 data->input = input;
1060 static void elan_disable_regulator(void *_data)
1062 struct elan_tp_data *data = _data;
1064 regulator_disable(data->vcc);
1067 static void elan_remove_sysfs_groups(void *_data)
1069 struct elan_tp_data *data = _data;
1071 sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups);
1074 static int elan_probe(struct i2c_client *client,
1075 const struct i2c_device_id *dev_id)
1077 const struct elan_transport_ops *transport_ops;
1078 struct device *dev = &client->dev;
1079 struct elan_tp_data *data;
1080 unsigned long irqflags;
1083 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1084 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1085 transport_ops = &elan_i2c_ops;
1086 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1087 i2c_check_functionality(client->adapter,
1088 I2C_FUNC_SMBUS_BYTE_DATA |
1089 I2C_FUNC_SMBUS_BLOCK_DATA |
1090 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1091 transport_ops = &elan_smbus_ops;
1093 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1097 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1101 i2c_set_clientdata(client, data);
1103 data->ops = transport_ops;
1104 data->client = client;
1105 init_completion(&data->fw_completion);
1106 mutex_init(&data->sysfs_mutex);
1108 data->vcc = devm_regulator_get(dev, "vcc");
1109 if (IS_ERR(data->vcc)) {
1110 error = PTR_ERR(data->vcc);
1111 if (error != -EPROBE_DEFER)
1112 dev_err(dev, "Failed to get 'vcc' regulator: %d\n",
1117 error = regulator_enable(data->vcc);
1119 dev_err(dev, "Failed to enable regulator: %d\n", error);
1123 error = devm_add_action(dev, elan_disable_regulator, data);
1125 regulator_disable(data->vcc);
1126 dev_err(dev, "Failed to add disable regulator action: %d\n",
1131 /* Make sure there is something at this address */
1132 error = i2c_smbus_read_byte(client);
1134 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1138 /* Initialize the touchpad. */
1139 error = elan_initialize(data);
1143 error = elan_query_device_info(data);
1147 error = elan_query_device_parameters(data);
1152 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1159 "Elan Touchpad Extra Information:\n"
1160 " Max ABS X,Y: %d,%d\n"
1161 " Width X,Y: %d,%d\n"
1162 " Resolution X,Y: %d,%d (dots/mm)\n"
1164 " info pattern: 0x%x\n",
1165 data->max_x, data->max_y,
1166 data->width_x, data->width_y,
1167 data->x_res, data->y_res,
1168 data->ic_type, data->pattern);
1170 /* Set up input device properties based on queried parameters. */
1171 error = elan_setup_input_device(data);
1175 if (device_property_read_bool(&client->dev, "elan,trackpoint")) {
1176 error = elan_setup_trackpoint_input_device(data);
1182 * Platform code (ACPI, DTS) should normally set up interrupt
1183 * for us, but in case it did not let's fall back to using falling
1184 * edge to be compatible with older Chromebooks.
1186 irqflags = irq_get_trigger_type(client->irq);
1188 irqflags = IRQF_TRIGGER_FALLING;
1190 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1191 irqflags | IRQF_ONESHOT,
1192 client->name, data);
1194 dev_err(dev, "cannot register irq=%d\n", client->irq);
1198 error = sysfs_create_groups(&dev->kobj, elan_sysfs_groups);
1200 dev_err(dev, "failed to create sysfs attributes: %d\n", error);
1204 error = devm_add_action(dev, elan_remove_sysfs_groups, data);
1206 elan_remove_sysfs_groups(data);
1207 dev_err(dev, "Failed to add sysfs cleanup action: %d\n",
1212 error = input_register_device(data->input);
1214 dev_err(dev, "failed to register input device: %d\n", error);
1218 if (data->tp_input) {
1219 error = input_register_device(data->tp_input);
1221 dev_err(&client->dev,
1222 "failed to register TrackPoint input device: %d\n",
1229 * Systems using device tree should set up wakeup via DTS,
1230 * the rest will configure device as wakeup source by default.
1233 device_init_wakeup(dev, true);
1238 static int __maybe_unused elan_suspend(struct device *dev)
1240 struct i2c_client *client = to_i2c_client(dev);
1241 struct elan_tp_data *data = i2c_get_clientdata(client);
1245 * We are taking the mutex to make sure sysfs operations are
1246 * complete before we attempt to bring the device into low[er]
1249 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1253 disable_irq(client->irq);
1255 if (device_may_wakeup(dev)) {
1256 ret = elan_sleep(data);
1257 /* Enable wake from IRQ */
1258 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1260 ret = elan_set_power(data, false);
1264 ret = regulator_disable(data->vcc);
1266 dev_err(dev, "error %d disabling regulator\n", ret);
1267 /* Attempt to power the chip back up */
1268 elan_set_power(data, true);
1273 mutex_unlock(&data->sysfs_mutex);
1277 static int __maybe_unused elan_resume(struct device *dev)
1279 struct i2c_client *client = to_i2c_client(dev);
1280 struct elan_tp_data *data = i2c_get_clientdata(client);
1283 if (!device_may_wakeup(dev)) {
1284 error = regulator_enable(data->vcc);
1286 dev_err(dev, "error %d enabling regulator\n", error);
1289 } else if (data->irq_wake) {
1290 disable_irq_wake(client->irq);
1291 data->irq_wake = false;
1294 error = elan_set_power(data, true);
1296 dev_err(dev, "power up when resuming failed: %d\n", error);
1300 error = elan_initialize(data);
1302 dev_err(dev, "initialize when resuming failed: %d\n", error);
1305 enable_irq(data->client->irq);
1309 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1311 static const struct i2c_device_id elan_id[] = {
1315 MODULE_DEVICE_TABLE(i2c, elan_id);
1318 static const struct acpi_device_id elan_acpi_id[] = {
1343 /* { "ELAN061B", 0 }, not working on the Lenovo Legion Y7000 */
1367 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1371 static const struct of_device_id elan_of_match[] = {
1372 { .compatible = "elan,ekth3000" },
1375 MODULE_DEVICE_TABLE(of, elan_of_match);
1378 static struct i2c_driver elan_driver = {
1380 .name = DRIVER_NAME,
1382 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1383 .of_match_table = of_match_ptr(elan_of_match),
1384 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1386 .probe = elan_probe,
1387 .id_table = elan_id,
1390 module_i2c_driver(elan_driver);
1392 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1393 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1394 MODULE_LICENSE("GPL");