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.2"
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;
86 int pressure_adjustment;
89 u16 fw_validpage_count;
90 u16 fw_signature_address;
99 static int elan_get_fwinfo(u8 iap_version, u16 *validpage_count,
100 u16 *signature_address)
102 switch (iap_version) {
106 *validpage_count = 512;
114 *validpage_count = 768;
117 *validpage_count = 896;
120 *validpage_count = 640;
123 /* unknown ic type clear value */
124 *validpage_count = 0;
125 *signature_address = 0;
130 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
135 static int elan_set_power(struct elan_tp_data *data, bool on)
137 int repeat = ETP_RETRY_COUNT;
141 error = data->ops->power_control(data->client, on);
146 } while (--repeat > 0);
148 dev_err(&data->client->dev, "failed to set power %s: %d\n",
149 on ? "on" : "off", error);
153 static int elan_sleep(struct elan_tp_data *data)
155 int repeat = ETP_RETRY_COUNT;
159 error = data->ops->sleep_control(data->client, true);
164 } while (--repeat > 0);
169 static int elan_query_product(struct elan_tp_data *data)
173 error = data->ops->get_product_id(data->client, &data->product_id);
177 error = data->ops->get_sm_version(data->client, &data->ic_type,
185 static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
187 if (data->ic_type == 0x0E) {
188 switch (data->product_id) {
194 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
195 /* ASUS EeeBook X205TA */
202 static int __elan_initialize(struct elan_tp_data *data)
204 struct i2c_client *client = data->client;
205 bool woken_up = false;
208 error = data->ops->initialize(client);
210 dev_err(&client->dev, "device initialize failed: %d\n", error);
214 error = elan_query_product(data);
219 * Some ASUS devices were shipped with firmware that requires
220 * touchpads to be woken up first, before attempting to switch
221 * them into absolute reporting mode.
223 if (elan_check_ASUS_special_fw(data)) {
224 error = data->ops->sleep_control(client, false);
226 dev_err(&client->dev,
227 "failed to wake device up: %d\n", error);
235 data->mode |= ETP_ENABLE_ABS;
236 error = data->ops->set_mode(client, data->mode);
238 dev_err(&client->dev,
239 "failed to switch to absolute mode: %d\n", error);
244 error = data->ops->sleep_control(client, false);
246 dev_err(&client->dev,
247 "failed to wake device up: %d\n", error);
255 static int elan_initialize(struct elan_tp_data *data)
257 int repeat = ETP_RETRY_COUNT;
261 error = __elan_initialize(data);
266 } while (--repeat > 0);
271 static int elan_query_device_info(struct elan_tp_data *data)
275 error = data->ops->get_version(data->client, false, &data->fw_version);
279 error = data->ops->get_checksum(data->client, false,
284 error = data->ops->get_version(data->client, true, &data->iap_version);
288 error = data->ops->get_pressure_adjustment(data->client,
289 &data->pressure_adjustment);
293 error = elan_get_fwinfo(data->iap_version, &data->fw_validpage_count,
294 &data->fw_signature_address);
296 dev_warn(&data->client->dev,
297 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
298 data->iap_version, data->ic_type);
303 static unsigned int elan_convert_resolution(u8 val)
306 * (value from firmware) * 10 + 790 = dpi
308 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
312 return ((int)(char)val * 10 + 790) * 10 / 254;
315 static int elan_query_device_parameters(struct elan_tp_data *data)
317 unsigned int x_traces, y_traces;
318 u8 hw_x_res, hw_y_res;
321 error = data->ops->get_max(data->client, &data->max_x, &data->max_y);
325 error = data->ops->get_num_traces(data->client, &x_traces, &y_traces);
329 data->width_x = data->max_x / x_traces;
330 data->width_y = data->max_y / y_traces;
332 error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res);
336 data->x_res = elan_convert_resolution(hw_x_res);
337 data->y_res = elan_convert_resolution(hw_y_res);
343 **********************************************************
344 * IAP firmware updater related routines
345 **********************************************************
347 static int elan_write_fw_block(struct elan_tp_data *data,
348 const u8 *page, u16 checksum, int idx)
350 int retry = ETP_RETRY_COUNT;
354 error = data->ops->write_fw_block(data->client,
355 page, checksum, idx);
359 dev_dbg(&data->client->dev,
360 "IAP retrying page %d (error: %d)\n", idx, error);
361 } while (--retry > 0);
366 static int __elan_update_firmware(struct elan_tp_data *data,
367 const struct firmware *fw)
369 struct i2c_client *client = data->client;
370 struct device *dev = &client->dev;
375 u16 sw_checksum = 0, fw_checksum = 0;
377 error = data->ops->prepare_fw_update(client);
381 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
383 boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE;
384 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
386 const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE];
388 for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2)
389 checksum += ((page[j + 1] << 8) | page[j]);
391 error = elan_write_fw_block(data, page, checksum, i);
393 dev_err(dev, "write page %d fail: %d\n", i, error);
397 sw_checksum += checksum;
400 /* Wait WDT reset and power on reset */
403 error = data->ops->finish_fw_update(client, &data->fw_completion);
407 error = data->ops->get_checksum(client, true, &fw_checksum);
411 if (sw_checksum != fw_checksum) {
412 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
413 sw_checksum, fw_checksum);
420 static int elan_update_firmware(struct elan_tp_data *data,
421 const struct firmware *fw)
423 struct i2c_client *client = data->client;
426 dev_dbg(&client->dev, "Starting firmware update....\n");
428 disable_irq(client->irq);
429 data->in_fw_update = true;
431 retval = __elan_update_firmware(data, fw);
433 dev_err(&client->dev, "firmware update failed: %d\n", retval);
434 data->ops->iap_reset(client);
436 /* Reinitialize TP after fw is updated */
437 elan_initialize(data);
438 elan_query_device_info(data);
441 data->in_fw_update = false;
442 enable_irq(client->irq);
448 *******************************************************************
450 *******************************************************************
452 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
453 struct device_attribute *attr,
456 struct i2c_client *client = to_i2c_client(dev);
457 struct elan_tp_data *data = i2c_get_clientdata(client);
459 return sprintf(buf, "0x%04x\n", data->fw_checksum);
462 static ssize_t elan_sysfs_read_product_id(struct device *dev,
463 struct device_attribute *attr,
466 struct i2c_client *client = to_i2c_client(dev);
467 struct elan_tp_data *data = i2c_get_clientdata(client);
469 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
473 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
474 struct device_attribute *attr,
477 struct i2c_client *client = to_i2c_client(dev);
478 struct elan_tp_data *data = i2c_get_clientdata(client);
480 return sprintf(buf, "%d.0\n", data->fw_version);
483 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
484 struct device_attribute *attr,
487 struct i2c_client *client = to_i2c_client(dev);
488 struct elan_tp_data *data = i2c_get_clientdata(client);
490 return sprintf(buf, "%d.0\n", data->sm_version);
493 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
494 struct device_attribute *attr,
497 struct i2c_client *client = to_i2c_client(dev);
498 struct elan_tp_data *data = i2c_get_clientdata(client);
500 return sprintf(buf, "%d.0\n", data->iap_version);
503 static ssize_t elan_sysfs_update_fw(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
507 struct elan_tp_data *data = dev_get_drvdata(dev);
508 const struct firmware *fw;
511 const u8 *fw_signature;
512 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
514 if (data->fw_validpage_count == 0)
517 /* Look for a firmware with the product id appended. */
518 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
520 dev_err(dev, "failed to allocate memory for firmware name\n");
524 dev_info(dev, "requesting fw '%s'\n", fw_name);
525 error = reject_firmware(&fw, fw_name, dev);
528 dev_err(dev, "failed to request firmware: %d\n", error);
532 /* Firmware file must match signature data */
533 fw_signature = &fw->data[data->fw_signature_address];
534 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
535 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
536 (int)sizeof(signature), signature,
537 (int)sizeof(signature), fw_signature);
542 error = mutex_lock_interruptible(&data->sysfs_mutex);
546 error = elan_update_firmware(data, fw);
548 mutex_unlock(&data->sysfs_mutex);
551 release_firmware(fw);
552 return error ?: count;
555 static ssize_t calibrate_store(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf, size_t count)
559 struct i2c_client *client = to_i2c_client(dev);
560 struct elan_tp_data *data = i2c_get_clientdata(client);
564 u8 val[ETP_CALIBRATE_MAX_LEN];
566 retval = mutex_lock_interruptible(&data->sysfs_mutex);
570 disable_irq(client->irq);
572 data->mode |= ETP_ENABLE_CALIBRATE;
573 retval = data->ops->set_mode(client, data->mode);
575 dev_err(dev, "failed to enable calibration mode: %d\n",
580 retval = data->ops->calibrate(client);
582 dev_err(dev, "failed to start calibration: %d\n",
584 goto out_disable_calibrate;
589 /* Wait 250ms before checking if calibration has completed. */
592 retval = data->ops->calibrate_result(client, val);
594 dev_err(dev, "failed to check calibration result: %d\n",
596 else if (val[0] == 0)
597 break; /* calibration done */
602 dev_err(dev, "failed to calibrate. Timeout.\n");
606 out_disable_calibrate:
607 data->mode &= ~ETP_ENABLE_CALIBRATE;
608 error = data->ops->set_mode(data->client, data->mode);
610 dev_err(dev, "failed to disable calibration mode: %d\n",
616 enable_irq(client->irq);
617 mutex_unlock(&data->sysfs_mutex);
618 return retval ?: count;
621 static ssize_t elan_sysfs_read_mode(struct device *dev,
622 struct device_attribute *attr,
625 struct i2c_client *client = to_i2c_client(dev);
626 struct elan_tp_data *data = i2c_get_clientdata(client);
630 error = mutex_lock_interruptible(&data->sysfs_mutex);
634 error = data->ops->iap_get_mode(data->client, &mode);
636 mutex_unlock(&data->sysfs_mutex);
641 return sprintf(buf, "%d\n", (int)mode);
644 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
645 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
646 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
647 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
648 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
649 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
650 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
652 static DEVICE_ATTR_WO(calibrate);
654 static struct attribute *elan_sysfs_entries[] = {
655 &dev_attr_product_id.attr,
656 &dev_attr_firmware_version.attr,
657 &dev_attr_sample_version.attr,
658 &dev_attr_iap_version.attr,
659 &dev_attr_fw_checksum.attr,
660 &dev_attr_calibrate.attr,
662 &dev_attr_update_fw.attr,
666 static const struct attribute_group elan_sysfs_group = {
667 .attrs = elan_sysfs_entries,
670 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
671 const char *buf, size_t count)
673 struct i2c_client *client = to_i2c_client(dev);
674 struct elan_tp_data *data = i2c_get_clientdata(client);
678 retval = mutex_lock_interruptible(&data->sysfs_mutex);
682 disable_irq(client->irq);
684 data->baseline_ready = false;
686 data->mode |= ETP_ENABLE_CALIBRATE;
687 retval = data->ops->set_mode(data->client, data->mode);
689 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
696 retval = data->ops->get_baseline_data(data->client, true,
697 &data->max_baseline);
699 dev_err(dev, "Failed to read max baseline form device: %d\n",
701 goto out_disable_calibrate;
704 retval = data->ops->get_baseline_data(data->client, false,
705 &data->min_baseline);
707 dev_err(dev, "Failed to read min baseline form device: %d\n",
709 goto out_disable_calibrate;
712 data->baseline_ready = true;
714 out_disable_calibrate:
715 data->mode &= ~ETP_ENABLE_CALIBRATE;
716 error = data->ops->set_mode(data->client, data->mode);
718 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
724 enable_irq(client->irq);
725 mutex_unlock(&data->sysfs_mutex);
726 return retval ?: count;
729 static ssize_t min_show(struct device *dev,
730 struct device_attribute *attr, char *buf)
732 struct i2c_client *client = to_i2c_client(dev);
733 struct elan_tp_data *data = i2c_get_clientdata(client);
736 retval = mutex_lock_interruptible(&data->sysfs_mutex);
740 if (!data->baseline_ready) {
745 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
748 mutex_unlock(&data->sysfs_mutex);
752 static ssize_t max_show(struct device *dev,
753 struct device_attribute *attr, char *buf)
755 struct i2c_client *client = to_i2c_client(dev);
756 struct elan_tp_data *data = i2c_get_clientdata(client);
759 retval = mutex_lock_interruptible(&data->sysfs_mutex);
763 if (!data->baseline_ready) {
768 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
771 mutex_unlock(&data->sysfs_mutex);
776 static DEVICE_ATTR_WO(acquire);
777 static DEVICE_ATTR_RO(min);
778 static DEVICE_ATTR_RO(max);
780 static struct attribute *elan_baseline_sysfs_entries[] = {
781 &dev_attr_acquire.attr,
787 static const struct attribute_group elan_baseline_sysfs_group = {
789 .attrs = elan_baseline_sysfs_entries,
792 static const struct attribute_group *elan_sysfs_groups[] = {
794 &elan_baseline_sysfs_group,
799 ******************************************************************
801 ******************************************************************
803 static void elan_report_contact(struct elan_tp_data *data,
804 int contact_num, bool contact_valid,
807 struct input_dev *input = data->input;
808 unsigned int pos_x, pos_y;
809 unsigned int pressure, mk_x, mk_y;
810 unsigned int area_x, area_y, major, minor;
811 unsigned int scaled_pressure;
814 pos_x = ((finger_data[0] & 0xf0) << 4) |
816 pos_y = ((finger_data[0] & 0x0f) << 8) |
818 mk_x = (finger_data[3] & 0x0f);
819 mk_y = (finger_data[3] >> 4);
820 pressure = finger_data[4];
822 if (pos_x > data->max_x || pos_y > data->max_y) {
823 dev_dbg(input->dev.parent,
824 "[%d] x=%d y=%d over max (%d, %d)",
825 contact_num, pos_x, pos_y,
826 data->max_x, data->max_y);
831 * To avoid treating large finger as palm, let's reduce the
832 * width x and y per trace.
834 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
835 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
837 major = max(area_x, area_y);
838 minor = min(area_x, area_y);
840 scaled_pressure = pressure + data->pressure_adjustment;
842 if (scaled_pressure > ETP_MAX_PRESSURE)
843 scaled_pressure = ETP_MAX_PRESSURE;
845 input_mt_slot(input, contact_num);
846 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
847 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
848 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
849 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
850 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
851 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
852 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
854 input_mt_slot(input, contact_num);
855 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
859 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet)
861 struct input_dev *input = data->input;
862 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
864 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
865 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
866 bool contact_valid, hover_event;
868 hover_event = hover_info & 0x40;
869 for (i = 0; i < ETP_MAX_FINGERS; i++) {
870 contact_valid = tp_info & (1U << (3 + i));
871 elan_report_contact(data, i, contact_valid, finger_data);
874 finger_data += ETP_FINGER_DATA_LEN;
877 input_report_key(input, BTN_LEFT, tp_info & 0x01);
878 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
879 input_mt_report_pointer_emulation(input, true);
883 static irqreturn_t elan_isr(int irq, void *dev_id)
885 struct elan_tp_data *data = dev_id;
886 struct device *dev = &data->client->dev;
888 u8 report[ETP_MAX_REPORT_LEN];
891 * When device is connected to i2c bus, when all IAP page writes
892 * complete, the driver will receive interrupt and must read
893 * 0000 to confirm that IAP is finished.
895 if (data->in_fw_update) {
896 complete(&data->fw_completion);
900 error = data->ops->get_report(data->client, report);
904 if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID)
905 dev_err(dev, "invalid report id data (%x)\n",
906 report[ETP_REPORT_ID_OFFSET]);
908 elan_report_absolute(data, report);
915 ******************************************************************
916 * Elan initialization functions
917 ******************************************************************
919 static int elan_setup_input_device(struct elan_tp_data *data)
921 struct device *dev = &data->client->dev;
922 struct input_dev *input;
923 unsigned int max_width = max(data->width_x, data->width_y);
924 unsigned int min_width = min(data->width_x, data->width_y);
927 input = devm_input_allocate_device(dev);
931 input->name = "Elan Touchpad";
932 input->id.bustype = BUS_I2C;
933 input->id.vendor = ELAN_VENDOR_ID;
934 input->id.product = data->product_id;
935 input_set_drvdata(input, data);
937 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
938 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
940 dev_err(dev, "failed to initialize MT slots: %d\n", error);
944 __set_bit(EV_ABS, input->evbit);
945 __set_bit(INPUT_PROP_POINTER, input->propbit);
946 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
947 __set_bit(BTN_LEFT, input->keybit);
949 /* Set up ST parameters */
950 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
951 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
952 input_abs_set_res(input, ABS_X, data->x_res);
953 input_abs_set_res(input, ABS_Y, data->y_res);
954 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
955 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0);
956 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
958 /* And MT parameters */
959 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
960 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
961 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
962 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
963 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
964 ETP_MAX_PRESSURE, 0, 0);
965 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
966 ETP_FINGER_WIDTH * max_width, 0, 0);
967 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0,
968 ETP_FINGER_WIDTH * min_width, 0, 0);
975 static void elan_disable_regulator(void *_data)
977 struct elan_tp_data *data = _data;
979 regulator_disable(data->vcc);
982 static void elan_remove_sysfs_groups(void *_data)
984 struct elan_tp_data *data = _data;
986 sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups);
989 static int elan_probe(struct i2c_client *client,
990 const struct i2c_device_id *dev_id)
992 const struct elan_transport_ops *transport_ops;
993 struct device *dev = &client->dev;
994 struct elan_tp_data *data;
995 unsigned long irqflags;
998 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
999 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1000 transport_ops = &elan_i2c_ops;
1001 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1002 i2c_check_functionality(client->adapter,
1003 I2C_FUNC_SMBUS_BYTE_DATA |
1004 I2C_FUNC_SMBUS_BLOCK_DATA |
1005 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1006 transport_ops = &elan_smbus_ops;
1008 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1012 data = devm_kzalloc(&client->dev, sizeof(struct elan_tp_data),
1017 i2c_set_clientdata(client, data);
1019 data->ops = transport_ops;
1020 data->client = client;
1021 init_completion(&data->fw_completion);
1022 mutex_init(&data->sysfs_mutex);
1024 data->vcc = devm_regulator_get(&client->dev, "vcc");
1025 if (IS_ERR(data->vcc)) {
1026 error = PTR_ERR(data->vcc);
1027 if (error != -EPROBE_DEFER)
1028 dev_err(&client->dev,
1029 "Failed to get 'vcc' regulator: %d\n",
1034 error = regulator_enable(data->vcc);
1036 dev_err(&client->dev,
1037 "Failed to enable regulator: %d\n", error);
1041 error = devm_add_action(&client->dev,
1042 elan_disable_regulator, data);
1044 regulator_disable(data->vcc);
1045 dev_err(&client->dev,
1046 "Failed to add disable regulator action: %d\n",
1051 /* Make sure there is something at this address */
1052 error = i2c_smbus_read_byte(client);
1054 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1058 /* Initialize the touchpad. */
1059 error = elan_initialize(data);
1063 error = elan_query_device_info(data);
1067 error = elan_query_device_parameters(data);
1071 dev_dbg(&client->dev,
1072 "Elan Touchpad Information:\n"
1073 " Module product ID: 0x%04x\n"
1074 " Firmware Version: 0x%04x\n"
1075 " Sample Version: 0x%04x\n"
1076 " IAP Version: 0x%04x\n"
1077 " Max ABS X,Y: %d,%d\n"
1078 " Width X,Y: %d,%d\n"
1079 " Resolution X,Y: %d,%d (dots/mm)\n",
1084 data->max_x, data->max_y,
1085 data->width_x, data->width_y,
1086 data->x_res, data->y_res);
1088 /* Set up input device properties based on queried parameters. */
1089 error = elan_setup_input_device(data);
1094 * Systems using device tree should set up interrupt via DTS,
1095 * the rest will use the default falling edge interrupts.
1097 irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
1099 error = devm_request_threaded_irq(&client->dev, client->irq,
1101 irqflags | IRQF_ONESHOT,
1102 client->name, data);
1104 dev_err(&client->dev, "cannot register irq=%d\n", client->irq);
1108 error = sysfs_create_groups(&client->dev.kobj, elan_sysfs_groups);
1110 dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1115 error = devm_add_action(&client->dev,
1116 elan_remove_sysfs_groups, data);
1118 elan_remove_sysfs_groups(data);
1119 dev_err(&client->dev,
1120 "Failed to add sysfs cleanup action: %d\n",
1125 error = input_register_device(data->input);
1127 dev_err(&client->dev, "failed to register input device: %d\n",
1133 * Systems using device tree should set up wakeup via DTS,
1134 * the rest will configure device as wakeup source by default.
1136 if (!client->dev.of_node)
1137 device_init_wakeup(&client->dev, true);
1142 static int __maybe_unused elan_suspend(struct device *dev)
1144 struct i2c_client *client = to_i2c_client(dev);
1145 struct elan_tp_data *data = i2c_get_clientdata(client);
1149 * We are taking the mutex to make sure sysfs operations are
1150 * complete before we attempt to bring the device into low[er]
1153 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1157 disable_irq(client->irq);
1159 if (device_may_wakeup(dev)) {
1160 ret = elan_sleep(data);
1161 /* Enable wake from IRQ */
1162 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1164 ret = elan_set_power(data, false);
1168 ret = regulator_disable(data->vcc);
1170 dev_err(dev, "error %d disabling regulator\n", ret);
1171 /* Attempt to power the chip back up */
1172 elan_set_power(data, true);
1177 mutex_unlock(&data->sysfs_mutex);
1181 static int __maybe_unused elan_resume(struct device *dev)
1183 struct i2c_client *client = to_i2c_client(dev);
1184 struct elan_tp_data *data = i2c_get_clientdata(client);
1187 if (!device_may_wakeup(dev)) {
1188 error = regulator_enable(data->vcc);
1190 dev_err(dev, "error %d enabling regulator\n", error);
1193 } else if (data->irq_wake) {
1194 disable_irq_wake(client->irq);
1195 data->irq_wake = false;
1198 error = elan_set_power(data, true);
1200 dev_err(dev, "power up when resuming failed: %d\n", error);
1204 error = elan_initialize(data);
1206 dev_err(dev, "initialize when resuming failed: %d\n", error);
1209 enable_irq(data->client->irq);
1213 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1215 static const struct i2c_device_id elan_id[] = {
1219 MODULE_DEVICE_TABLE(i2c, elan_id);
1222 static const struct acpi_device_id elan_acpi_id[] = {
1247 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1251 static const struct of_device_id elan_of_match[] = {
1252 { .compatible = "elan,ekth3000" },
1255 MODULE_DEVICE_TABLE(of, elan_of_match);
1258 static struct i2c_driver elan_driver = {
1260 .name = DRIVER_NAME,
1262 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1263 .of_match_table = of_match_ptr(elan_of_match),
1264 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1266 .probe = elan_probe,
1267 .id_table = elan_id,
1270 module_i2c_driver(elan_driver);
1272 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1273 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1274 MODULE_LICENSE("GPL");
1275 MODULE_VERSION(ELAN_DRIVER_VERSION);