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/module.h>
30 #include <linux/slab.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/input.h>
34 #include <linux/uaccess.h>
35 #include <linux/jiffies.h>
36 #include <linux/completion.h>
38 #include <linux/regulator/consumer.h>
39 #include <asm/unaligned.h>
43 #define DRIVER_NAME "elan_i2c"
44 #define ELAN_DRIVER_VERSION "1.6.3"
45 #define ELAN_VENDOR_ID 0x04f3
46 #define ETP_MAX_PRESSURE 255
47 #define ETP_FWIDTH_REDUCE 90
48 #define ETP_FINGER_WIDTH 15
49 #define ETP_RETRY_COUNT 3
51 #define ETP_MAX_FINGERS 5
52 #define ETP_FINGER_DATA_LEN 5
53 #define ETP_REPORT_ID 0x5D
54 #define ETP_REPORT_ID_OFFSET 2
55 #define ETP_TOUCH_INFO_OFFSET 3
56 #define ETP_FINGER_DATA_OFFSET 4
57 #define ETP_HOVER_INFO_OFFSET 30
58 #define ETP_MAX_REPORT_LEN 34
60 /* The main device structure */
62 struct i2c_client *client;
63 struct input_dev *input;
64 struct regulator *vcc;
66 const struct elan_transport_ops *ops;
69 struct completion fw_completion;
72 struct mutex sysfs_mutex;
87 int pressure_adjustment;
90 u16 fw_validpage_count;
91 u16 fw_signature_address;
101 static int elan_get_fwinfo(u16 ic_type, u16 *validpage_count,
102 u16 *signature_address)
108 *validpage_count = 512;
116 *validpage_count = 768;
119 *validpage_count = 896;
122 *validpage_count = 640;
125 *validpage_count = 1024;
128 /* unknown ic type clear value */
129 *validpage_count = 0;
130 *signature_address = 0;
135 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
140 static int elan_set_power(struct elan_tp_data *data, bool on)
142 int repeat = ETP_RETRY_COUNT;
146 error = data->ops->power_control(data->client, on);
151 } while (--repeat > 0);
153 dev_err(&data->client->dev, "failed to set power %s: %d\n",
154 on ? "on" : "off", error);
158 static int elan_sleep(struct elan_tp_data *data)
160 int repeat = ETP_RETRY_COUNT;
164 error = data->ops->sleep_control(data->client, true);
169 } while (--repeat > 0);
174 static int elan_query_product(struct elan_tp_data *data)
178 error = data->ops->get_product_id(data->client, &data->product_id);
182 error = data->ops->get_sm_version(data->client, &data->ic_type,
183 &data->sm_version, &data->clickpad);
190 static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
192 if (data->ic_type == 0x0E) {
193 switch (data->product_id) {
199 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
200 /* ASUS EeeBook X205TA */
207 static int __elan_initialize(struct elan_tp_data *data)
209 struct i2c_client *client = data->client;
210 bool woken_up = false;
213 error = data->ops->initialize(client);
215 dev_err(&client->dev, "device initialize failed: %d\n", error);
219 error = elan_query_product(data);
224 * Some ASUS devices were shipped with firmware that requires
225 * touchpads to be woken up first, before attempting to switch
226 * them into absolute reporting mode.
228 if (elan_check_ASUS_special_fw(data)) {
229 error = data->ops->sleep_control(client, false);
231 dev_err(&client->dev,
232 "failed to wake device up: %d\n", error);
240 data->mode |= ETP_ENABLE_ABS;
241 error = data->ops->set_mode(client, data->mode);
243 dev_err(&client->dev,
244 "failed to switch to absolute mode: %d\n", error);
249 error = data->ops->sleep_control(client, false);
251 dev_err(&client->dev,
252 "failed to wake device up: %d\n", error);
260 static int elan_initialize(struct elan_tp_data *data)
262 int repeat = ETP_RETRY_COUNT;
266 error = __elan_initialize(data);
271 } while (--repeat > 0);
276 static int elan_query_device_info(struct elan_tp_data *data)
281 error = data->ops->get_version(data->client, false, &data->fw_version);
285 error = data->ops->get_checksum(data->client, false,
290 error = data->ops->get_version(data->client, true, &data->iap_version);
294 error = data->ops->get_pressure_adjustment(data->client,
295 &data->pressure_adjustment);
299 error = data->ops->get_pattern(data->client, &data->pattern);
303 if (data->pattern == 0x01)
304 ic_type = data->ic_type;
306 ic_type = data->iap_version;
308 error = elan_get_fwinfo(ic_type, &data->fw_validpage_count,
309 &data->fw_signature_address);
311 dev_warn(&data->client->dev,
312 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
313 data->iap_version, data->ic_type);
318 static unsigned int elan_convert_resolution(u8 val)
321 * (value from firmware) * 10 + 790 = dpi
323 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
327 return ((int)(char)val * 10 + 790) * 10 / 254;
330 static int elan_query_device_parameters(struct elan_tp_data *data)
332 unsigned int x_traces, y_traces;
333 u8 hw_x_res, hw_y_res;
336 error = data->ops->get_max(data->client, &data->max_x, &data->max_y);
340 error = data->ops->get_num_traces(data->client, &x_traces, &y_traces);
344 data->width_x = data->max_x / x_traces;
345 data->width_y = data->max_y / y_traces;
347 error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res);
351 data->x_res = elan_convert_resolution(hw_x_res);
352 data->y_res = elan_convert_resolution(hw_y_res);
358 **********************************************************
359 * IAP firmware updater related routines
360 **********************************************************
362 static int elan_write_fw_block(struct elan_tp_data *data,
363 const u8 *page, u16 checksum, int idx)
365 int retry = ETP_RETRY_COUNT;
369 error = data->ops->write_fw_block(data->client,
370 page, checksum, idx);
374 dev_dbg(&data->client->dev,
375 "IAP retrying page %d (error: %d)\n", idx, error);
376 } while (--retry > 0);
381 static int __elan_update_firmware(struct elan_tp_data *data,
382 const struct firmware *fw)
384 struct i2c_client *client = data->client;
385 struct device *dev = &client->dev;
390 u16 sw_checksum = 0, fw_checksum = 0;
392 error = data->ops->prepare_fw_update(client);
396 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
398 boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE;
399 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
401 const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE];
403 for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2)
404 checksum += ((page[j + 1] << 8) | page[j]);
406 error = elan_write_fw_block(data, page, checksum, i);
408 dev_err(dev, "write page %d fail: %d\n", i, error);
412 sw_checksum += checksum;
415 /* Wait WDT reset and power on reset */
418 error = data->ops->finish_fw_update(client, &data->fw_completion);
422 error = data->ops->get_checksum(client, true, &fw_checksum);
426 if (sw_checksum != fw_checksum) {
427 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
428 sw_checksum, fw_checksum);
435 static int elan_update_firmware(struct elan_tp_data *data,
436 const struct firmware *fw)
438 struct i2c_client *client = data->client;
441 dev_dbg(&client->dev, "Starting firmware update....\n");
443 disable_irq(client->irq);
444 data->in_fw_update = true;
446 retval = __elan_update_firmware(data, fw);
448 dev_err(&client->dev, "firmware update failed: %d\n", retval);
449 data->ops->iap_reset(client);
451 /* Reinitialize TP after fw is updated */
452 elan_initialize(data);
453 elan_query_device_info(data);
456 data->in_fw_update = false;
457 enable_irq(client->irq);
463 *******************************************************************
465 *******************************************************************
467 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
468 struct device_attribute *attr,
471 struct i2c_client *client = to_i2c_client(dev);
472 struct elan_tp_data *data = i2c_get_clientdata(client);
474 return sprintf(buf, "0x%04x\n", data->fw_checksum);
477 static ssize_t elan_sysfs_read_product_id(struct device *dev,
478 struct device_attribute *attr,
481 struct i2c_client *client = to_i2c_client(dev);
482 struct elan_tp_data *data = i2c_get_clientdata(client);
484 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
488 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
489 struct device_attribute *attr,
492 struct i2c_client *client = to_i2c_client(dev);
493 struct elan_tp_data *data = i2c_get_clientdata(client);
495 return sprintf(buf, "%d.0\n", data->fw_version);
498 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
499 struct device_attribute *attr,
502 struct i2c_client *client = to_i2c_client(dev);
503 struct elan_tp_data *data = i2c_get_clientdata(client);
505 return sprintf(buf, "%d.0\n", data->sm_version);
508 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
509 struct device_attribute *attr,
512 struct i2c_client *client = to_i2c_client(dev);
513 struct elan_tp_data *data = i2c_get_clientdata(client);
515 return sprintf(buf, "%d.0\n", data->iap_version);
518 static ssize_t elan_sysfs_update_fw(struct device *dev,
519 struct device_attribute *attr,
520 const char *buf, size_t count)
522 struct elan_tp_data *data = dev_get_drvdata(dev);
523 const struct firmware *fw;
526 const u8 *fw_signature;
527 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
529 if (data->fw_validpage_count == 0)
532 /* Look for a firmware with the product id appended. */
533 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
535 dev_err(dev, "failed to allocate memory for firmware name\n");
539 dev_info(dev, "requesting fw '%s'\n", fw_name);
540 error = reject_firmware(&fw, fw_name, dev);
543 dev_err(dev, "failed to request firmware: %d\n", error);
547 /* Firmware file must match signature data */
548 fw_signature = &fw->data[data->fw_signature_address];
549 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
550 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
551 (int)sizeof(signature), signature,
552 (int)sizeof(signature), fw_signature);
557 error = mutex_lock_interruptible(&data->sysfs_mutex);
561 error = elan_update_firmware(data, fw);
563 mutex_unlock(&data->sysfs_mutex);
566 release_firmware(fw);
567 return error ?: count;
570 static ssize_t calibrate_store(struct device *dev,
571 struct device_attribute *attr,
572 const char *buf, size_t count)
574 struct i2c_client *client = to_i2c_client(dev);
575 struct elan_tp_data *data = i2c_get_clientdata(client);
579 u8 val[ETP_CALIBRATE_MAX_LEN];
581 retval = mutex_lock_interruptible(&data->sysfs_mutex);
585 disable_irq(client->irq);
587 data->mode |= ETP_ENABLE_CALIBRATE;
588 retval = data->ops->set_mode(client, data->mode);
590 dev_err(dev, "failed to enable calibration mode: %d\n",
595 retval = data->ops->calibrate(client);
597 dev_err(dev, "failed to start calibration: %d\n",
599 goto out_disable_calibrate;
604 /* Wait 250ms before checking if calibration has completed. */
607 retval = data->ops->calibrate_result(client, val);
609 dev_err(dev, "failed to check calibration result: %d\n",
611 else if (val[0] == 0)
612 break; /* calibration done */
617 dev_err(dev, "failed to calibrate. Timeout.\n");
621 out_disable_calibrate:
622 data->mode &= ~ETP_ENABLE_CALIBRATE;
623 error = data->ops->set_mode(data->client, data->mode);
625 dev_err(dev, "failed to disable calibration mode: %d\n",
631 enable_irq(client->irq);
632 mutex_unlock(&data->sysfs_mutex);
633 return retval ?: count;
636 static ssize_t elan_sysfs_read_mode(struct device *dev,
637 struct device_attribute *attr,
640 struct i2c_client *client = to_i2c_client(dev);
641 struct elan_tp_data *data = i2c_get_clientdata(client);
645 error = mutex_lock_interruptible(&data->sysfs_mutex);
649 error = data->ops->iap_get_mode(data->client, &mode);
651 mutex_unlock(&data->sysfs_mutex);
656 return sprintf(buf, "%d\n", (int)mode);
659 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
660 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
661 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
662 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
663 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
664 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
665 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
667 static DEVICE_ATTR_WO(calibrate);
669 static struct attribute *elan_sysfs_entries[] = {
670 &dev_attr_product_id.attr,
671 &dev_attr_firmware_version.attr,
672 &dev_attr_sample_version.attr,
673 &dev_attr_iap_version.attr,
674 &dev_attr_fw_checksum.attr,
675 &dev_attr_calibrate.attr,
677 &dev_attr_update_fw.attr,
681 static const struct attribute_group elan_sysfs_group = {
682 .attrs = elan_sysfs_entries,
685 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
686 const char *buf, size_t count)
688 struct i2c_client *client = to_i2c_client(dev);
689 struct elan_tp_data *data = i2c_get_clientdata(client);
693 retval = mutex_lock_interruptible(&data->sysfs_mutex);
697 disable_irq(client->irq);
699 data->baseline_ready = false;
701 data->mode |= ETP_ENABLE_CALIBRATE;
702 retval = data->ops->set_mode(data->client, data->mode);
704 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
711 retval = data->ops->get_baseline_data(data->client, true,
712 &data->max_baseline);
714 dev_err(dev, "Failed to read max baseline form device: %d\n",
716 goto out_disable_calibrate;
719 retval = data->ops->get_baseline_data(data->client, false,
720 &data->min_baseline);
722 dev_err(dev, "Failed to read min baseline form device: %d\n",
724 goto out_disable_calibrate;
727 data->baseline_ready = true;
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 after acquiring baseline: %d\n",
739 enable_irq(client->irq);
740 mutex_unlock(&data->sysfs_mutex);
741 return retval ?: count;
744 static ssize_t min_show(struct device *dev,
745 struct device_attribute *attr, char *buf)
747 struct i2c_client *client = to_i2c_client(dev);
748 struct elan_tp_data *data = i2c_get_clientdata(client);
751 retval = mutex_lock_interruptible(&data->sysfs_mutex);
755 if (!data->baseline_ready) {
760 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
763 mutex_unlock(&data->sysfs_mutex);
767 static ssize_t max_show(struct device *dev,
768 struct device_attribute *attr, char *buf)
770 struct i2c_client *client = to_i2c_client(dev);
771 struct elan_tp_data *data = i2c_get_clientdata(client);
774 retval = mutex_lock_interruptible(&data->sysfs_mutex);
778 if (!data->baseline_ready) {
783 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
786 mutex_unlock(&data->sysfs_mutex);
791 static DEVICE_ATTR_WO(acquire);
792 static DEVICE_ATTR_RO(min);
793 static DEVICE_ATTR_RO(max);
795 static struct attribute *elan_baseline_sysfs_entries[] = {
796 &dev_attr_acquire.attr,
802 static const struct attribute_group elan_baseline_sysfs_group = {
804 .attrs = elan_baseline_sysfs_entries,
807 static const struct attribute_group *elan_sysfs_groups[] = {
809 &elan_baseline_sysfs_group,
814 ******************************************************************
816 ******************************************************************
818 static void elan_report_contact(struct elan_tp_data *data,
819 int contact_num, bool contact_valid,
822 struct input_dev *input = data->input;
823 unsigned int pos_x, pos_y;
824 unsigned int pressure, mk_x, mk_y;
825 unsigned int area_x, area_y, major, minor;
826 unsigned int scaled_pressure;
829 pos_x = ((finger_data[0] & 0xf0) << 4) |
831 pos_y = ((finger_data[0] & 0x0f) << 8) |
833 mk_x = (finger_data[3] & 0x0f);
834 mk_y = (finger_data[3] >> 4);
835 pressure = finger_data[4];
837 if (pos_x > data->max_x || pos_y > data->max_y) {
838 dev_dbg(input->dev.parent,
839 "[%d] x=%d y=%d over max (%d, %d)",
840 contact_num, pos_x, pos_y,
841 data->max_x, data->max_y);
846 * To avoid treating large finger as palm, let's reduce the
847 * width x and y per trace.
849 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
850 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
852 major = max(area_x, area_y);
853 minor = min(area_x, area_y);
855 scaled_pressure = pressure + data->pressure_adjustment;
857 if (scaled_pressure > ETP_MAX_PRESSURE)
858 scaled_pressure = ETP_MAX_PRESSURE;
860 input_mt_slot(input, contact_num);
861 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
862 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
863 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
864 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
865 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
866 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
867 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
869 input_mt_slot(input, contact_num);
870 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
874 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet)
876 struct input_dev *input = data->input;
877 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
879 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
880 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
881 bool contact_valid, hover_event;
883 hover_event = hover_info & 0x40;
884 for (i = 0; i < ETP_MAX_FINGERS; i++) {
885 contact_valid = tp_info & (1U << (3 + i));
886 elan_report_contact(data, i, contact_valid, finger_data);
889 finger_data += ETP_FINGER_DATA_LEN;
892 input_report_key(input, BTN_LEFT, tp_info & 0x01);
893 input_report_key(input, BTN_RIGHT, tp_info & 0x02);
894 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
895 input_mt_report_pointer_emulation(input, true);
899 static irqreturn_t elan_isr(int irq, void *dev_id)
901 struct elan_tp_data *data = dev_id;
902 struct device *dev = &data->client->dev;
904 u8 report[ETP_MAX_REPORT_LEN];
907 * When device is connected to i2c bus, when all IAP page writes
908 * complete, the driver will receive interrupt and must read
909 * 0000 to confirm that IAP is finished.
911 if (data->in_fw_update) {
912 complete(&data->fw_completion);
916 error = data->ops->get_report(data->client, report);
920 if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID)
921 dev_err(dev, "invalid report id data (%x)\n",
922 report[ETP_REPORT_ID_OFFSET]);
924 elan_report_absolute(data, report);
931 ******************************************************************
932 * Elan initialization functions
933 ******************************************************************
935 static int elan_setup_input_device(struct elan_tp_data *data)
937 struct device *dev = &data->client->dev;
938 struct input_dev *input;
939 unsigned int max_width = max(data->width_x, data->width_y);
940 unsigned int min_width = min(data->width_x, data->width_y);
943 input = devm_input_allocate_device(dev);
947 input->name = "Elan Touchpad";
948 input->id.bustype = BUS_I2C;
949 input->id.vendor = ELAN_VENDOR_ID;
950 input->id.product = data->product_id;
951 input_set_drvdata(input, data);
953 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
954 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
956 dev_err(dev, "failed to initialize MT slots: %d\n", error);
960 __set_bit(EV_ABS, input->evbit);
961 __set_bit(INPUT_PROP_POINTER, input->propbit);
963 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
965 __set_bit(BTN_RIGHT, input->keybit);
966 __set_bit(BTN_LEFT, input->keybit);
968 /* Set up ST parameters */
969 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
970 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
971 input_abs_set_res(input, ABS_X, data->x_res);
972 input_abs_set_res(input, ABS_Y, data->y_res);
973 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
974 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0);
975 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
977 /* And MT parameters */
978 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
979 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
980 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
981 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
982 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
983 ETP_MAX_PRESSURE, 0, 0);
984 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
985 ETP_FINGER_WIDTH * max_width, 0, 0);
986 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0,
987 ETP_FINGER_WIDTH * min_width, 0, 0);
994 static void elan_disable_regulator(void *_data)
996 struct elan_tp_data *data = _data;
998 regulator_disable(data->vcc);
1001 static void elan_remove_sysfs_groups(void *_data)
1003 struct elan_tp_data *data = _data;
1005 sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups);
1008 static int elan_probe(struct i2c_client *client,
1009 const struct i2c_device_id *dev_id)
1011 const struct elan_transport_ops *transport_ops;
1012 struct device *dev = &client->dev;
1013 struct elan_tp_data *data;
1014 unsigned long irqflags;
1017 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1018 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1019 transport_ops = &elan_i2c_ops;
1020 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1021 i2c_check_functionality(client->adapter,
1022 I2C_FUNC_SMBUS_BYTE_DATA |
1023 I2C_FUNC_SMBUS_BLOCK_DATA |
1024 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1025 transport_ops = &elan_smbus_ops;
1027 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1031 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1035 i2c_set_clientdata(client, data);
1037 data->ops = transport_ops;
1038 data->client = client;
1039 init_completion(&data->fw_completion);
1040 mutex_init(&data->sysfs_mutex);
1042 data->vcc = devm_regulator_get(dev, "vcc");
1043 if (IS_ERR(data->vcc)) {
1044 error = PTR_ERR(data->vcc);
1045 if (error != -EPROBE_DEFER)
1046 dev_err(dev, "Failed to get 'vcc' regulator: %d\n",
1051 error = regulator_enable(data->vcc);
1053 dev_err(dev, "Failed to enable regulator: %d\n", error);
1057 error = devm_add_action(dev, elan_disable_regulator, data);
1059 regulator_disable(data->vcc);
1060 dev_err(dev, "Failed to add disable regulator action: %d\n",
1065 /* Make sure there is something at this address */
1066 error = i2c_smbus_read_byte(client);
1068 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1072 /* Initialize the touchpad. */
1073 error = elan_initialize(data);
1077 error = elan_query_device_info(data);
1081 error = elan_query_device_parameters(data);
1086 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1093 "Elan Touchpad Extra Information:\n"
1094 " Max ABS X,Y: %d,%d\n"
1095 " Width X,Y: %d,%d\n"
1096 " Resolution X,Y: %d,%d (dots/mm)\n"
1098 " info pattern: 0x%x\n",
1099 data->max_x, data->max_y,
1100 data->width_x, data->width_y,
1101 data->x_res, data->y_res,
1102 data->ic_type, data->pattern);
1104 /* Set up input device properties based on queried parameters. */
1105 error = elan_setup_input_device(data);
1110 * Systems using device tree should set up interrupt via DTS,
1111 * the rest will use the default falling edge interrupts.
1113 irqflags = dev->of_node ? 0 : IRQF_TRIGGER_FALLING;
1115 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1116 irqflags | IRQF_ONESHOT,
1117 client->name, data);
1119 dev_err(dev, "cannot register irq=%d\n", client->irq);
1123 error = sysfs_create_groups(&dev->kobj, elan_sysfs_groups);
1125 dev_err(dev, "failed to create sysfs attributes: %d\n", error);
1129 error = devm_add_action(dev, elan_remove_sysfs_groups, data);
1131 elan_remove_sysfs_groups(data);
1132 dev_err(dev, "Failed to add sysfs cleanup action: %d\n",
1137 error = input_register_device(data->input);
1139 dev_err(dev, "failed to register input device: %d\n", error);
1144 * Systems using device tree should set up wakeup via DTS,
1145 * the rest will configure device as wakeup source by default.
1148 device_init_wakeup(dev, true);
1153 static int __maybe_unused elan_suspend(struct device *dev)
1155 struct i2c_client *client = to_i2c_client(dev);
1156 struct elan_tp_data *data = i2c_get_clientdata(client);
1160 * We are taking the mutex to make sure sysfs operations are
1161 * complete before we attempt to bring the device into low[er]
1164 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1168 disable_irq(client->irq);
1170 if (device_may_wakeup(dev)) {
1171 ret = elan_sleep(data);
1172 /* Enable wake from IRQ */
1173 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1175 ret = elan_set_power(data, false);
1179 ret = regulator_disable(data->vcc);
1181 dev_err(dev, "error %d disabling regulator\n", ret);
1182 /* Attempt to power the chip back up */
1183 elan_set_power(data, true);
1188 mutex_unlock(&data->sysfs_mutex);
1192 static int __maybe_unused elan_resume(struct device *dev)
1194 struct i2c_client *client = to_i2c_client(dev);
1195 struct elan_tp_data *data = i2c_get_clientdata(client);
1198 if (!device_may_wakeup(dev)) {
1199 error = regulator_enable(data->vcc);
1201 dev_err(dev, "error %d enabling regulator\n", error);
1204 } else if (data->irq_wake) {
1205 disable_irq_wake(client->irq);
1206 data->irq_wake = false;
1209 error = elan_set_power(data, true);
1211 dev_err(dev, "power up when resuming failed: %d\n", error);
1215 error = elan_initialize(data);
1217 dev_err(dev, "initialize when resuming failed: %d\n", error);
1220 enable_irq(data->client->irq);
1224 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1226 static const struct i2c_device_id elan_id[] = {
1230 MODULE_DEVICE_TABLE(i2c, elan_id);
1233 static const struct acpi_device_id elan_acpi_id[] = {
1259 /* { "ELAN061B", 0 }, not working on the Lenovo Legion Y7000 */
1283 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1287 static const struct of_device_id elan_of_match[] = {
1288 { .compatible = "elan,ekth3000" },
1291 MODULE_DEVICE_TABLE(of, elan_of_match);
1294 static struct i2c_driver elan_driver = {
1296 .name = DRIVER_NAME,
1298 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1299 .of_match_table = of_match_ptr(elan_of_match),
1300 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1302 .probe = elan_probe,
1303 .id_table = elan_id,
1306 module_i2c_driver(elan_driver);
1308 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1309 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1310 MODULE_LICENSE("GPL");
1311 MODULE_VERSION(ELAN_DRIVER_VERSION);