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;
142 *validpage_count = 1024;
145 /* unknown ic type clear value */
146 *validpage_count = 0;
147 *signature_address = 0;
153 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
155 if ((ic_type == 0x14 || ic_type == 0x15) && iap_version >= 2) {
156 *validpage_count /= 8;
157 *page_size = ETP_FW_PAGE_SIZE_512;
158 } else if (ic_type >= 0x0D && iap_version >= 1) {
159 *validpage_count /= 2;
160 *page_size = ETP_FW_PAGE_SIZE_128;
162 *page_size = ETP_FW_PAGE_SIZE;
168 static int elan_enable_power(struct elan_tp_data *data)
170 int repeat = ETP_RETRY_COUNT;
173 error = regulator_enable(data->vcc);
175 dev_err(&data->client->dev,
176 "failed to enable regulator: %d\n", error);
181 error = data->ops->power_control(data->client, true);
186 } while (--repeat > 0);
188 dev_err(&data->client->dev, "failed to enable power: %d\n", error);
192 static int elan_disable_power(struct elan_tp_data *data)
194 int repeat = ETP_RETRY_COUNT;
198 error = data->ops->power_control(data->client, false);
200 error = regulator_disable(data->vcc);
202 dev_err(&data->client->dev,
203 "failed to disable regulator: %d\n",
205 /* Attempt to power the chip back up */
206 data->ops->power_control(data->client, true);
214 } while (--repeat > 0);
216 dev_err(&data->client->dev, "failed to disable power: %d\n", error);
220 static int elan_sleep(struct elan_tp_data *data)
222 int repeat = ETP_RETRY_COUNT;
226 error = data->ops->sleep_control(data->client, true);
231 } while (--repeat > 0);
236 static int elan_query_product(struct elan_tp_data *data)
240 error = data->ops->get_product_id(data->client, &data->product_id);
244 error = data->ops->get_pattern(data->client, &data->pattern);
248 error = data->ops->get_sm_version(data->client, data->pattern,
249 &data->ic_type, &data->sm_version,
257 static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
259 if (data->ic_type == 0x0E) {
260 switch (data->product_id) {
266 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
267 /* ASUS EeeBook X205TA */
274 static int __elan_initialize(struct elan_tp_data *data)
276 struct i2c_client *client = data->client;
277 bool woken_up = false;
280 error = data->ops->initialize(client);
282 dev_err(&client->dev, "device initialize failed: %d\n", error);
286 error = elan_query_product(data);
291 * Some ASUS devices were shipped with firmware that requires
292 * touchpads to be woken up first, before attempting to switch
293 * them into absolute reporting mode.
295 if (elan_check_ASUS_special_fw(data)) {
296 error = data->ops->sleep_control(client, false);
298 dev_err(&client->dev,
299 "failed to wake device up: %d\n", error);
307 data->mode |= ETP_ENABLE_ABS;
308 error = data->ops->set_mode(client, data->mode);
310 dev_err(&client->dev,
311 "failed to switch to absolute mode: %d\n", error);
316 error = data->ops->sleep_control(client, false);
318 dev_err(&client->dev,
319 "failed to wake device up: %d\n", error);
327 static int elan_initialize(struct elan_tp_data *data)
329 int repeat = ETP_RETRY_COUNT;
333 error = __elan_initialize(data);
338 } while (--repeat > 0);
343 static int elan_query_device_info(struct elan_tp_data *data)
347 error = data->ops->get_version(data->client, data->pattern, false,
352 error = data->ops->get_checksum(data->client, false,
357 error = data->ops->get_version(data->client, data->pattern,
358 true, &data->iap_version);
362 error = data->ops->get_pressure_adjustment(data->client,
363 &data->pressure_adjustment);
367 error = data->ops->get_report_features(data->client, data->pattern,
368 &data->report_features,
373 error = elan_get_fwinfo(data->ic_type, data->iap_version,
374 &data->fw_validpage_count,
375 &data->fw_signature_address,
376 &data->fw_page_size);
378 dev_warn(&data->client->dev,
379 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
380 data->iap_version, data->ic_type);
385 static unsigned int elan_convert_resolution(u8 val, u8 pattern)
389 * (value from firmware) * 10 + 790 = dpi
391 * ((value from firmware) + 3) * 100 = dpi
393 int res = pattern <= 0x01 ?
394 (int)(char)val * 10 + 790 : ((int)(char)val + 3) * 100;
396 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
399 return res * 10 / 254;
402 static int elan_query_device_parameters(struct elan_tp_data *data)
404 struct i2c_client *client = data->client;
405 unsigned int x_traces, y_traces;
407 u8 hw_x_res, hw_y_res;
410 if (device_property_read_u32(&client->dev,
411 "touchscreen-size-x", &data->max_x) ||
412 device_property_read_u32(&client->dev,
413 "touchscreen-size-y", &data->max_y)) {
414 error = data->ops->get_max(data->client,
420 /* size is the maximum + 1 */
425 if (device_property_read_u32(&client->dev,
428 device_property_read_u32(&client->dev,
431 error = data->ops->get_num_traces(data->client,
432 &x_traces, &y_traces);
436 data->width_x = data->max_x / x_traces;
437 data->width_y = data->max_y / y_traces;
439 if (device_property_read_u32(&client->dev,
440 "touchscreen-x-mm", &x_mm) ||
441 device_property_read_u32(&client->dev,
442 "touchscreen-y-mm", &y_mm)) {
443 error = data->ops->get_resolution(data->client,
444 &hw_x_res, &hw_y_res);
448 data->x_res = elan_convert_resolution(hw_x_res, data->pattern);
449 data->y_res = elan_convert_resolution(hw_y_res, data->pattern);
451 data->x_res = (data->max_x + 1) / x_mm;
452 data->y_res = (data->max_y + 1) / y_mm;
455 if (device_property_read_bool(&client->dev, "elan,clickpad"))
458 if (device_property_read_bool(&client->dev, "elan,middle-button"))
459 data->middle_button = true;
465 **********************************************************
466 * IAP firmware updater related routines
467 **********************************************************
469 static int elan_write_fw_block(struct elan_tp_data *data, u16 page_size,
470 const u8 *page, u16 checksum, int idx)
472 int retry = ETP_RETRY_COUNT;
476 error = data->ops->write_fw_block(data->client, page_size,
477 page, checksum, idx);
481 dev_dbg(&data->client->dev,
482 "IAP retrying page %d (error: %d)\n", idx, error);
483 } while (--retry > 0);
488 static int __elan_update_firmware(struct elan_tp_data *data,
489 const struct firmware *fw)
491 struct i2c_client *client = data->client;
492 struct device *dev = &client->dev;
497 u16 sw_checksum = 0, fw_checksum = 0;
499 error = data->ops->prepare_fw_update(client, data->ic_type,
504 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
506 boot_page_count = (iap_start_addr * 2) / data->fw_page_size;
507 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
509 const u8 *page = &fw->data[i * data->fw_page_size];
511 for (j = 0; j < data->fw_page_size; j += 2)
512 checksum += ((page[j + 1] << 8) | page[j]);
514 error = elan_write_fw_block(data, data->fw_page_size,
517 dev_err(dev, "write page %d fail: %d\n", i, error);
521 sw_checksum += checksum;
524 /* Wait WDT reset and power on reset */
527 error = data->ops->finish_fw_update(client, &data->fw_completion);
531 error = data->ops->get_checksum(client, true, &fw_checksum);
535 if (sw_checksum != fw_checksum) {
536 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
537 sw_checksum, fw_checksum);
544 static int elan_update_firmware(struct elan_tp_data *data,
545 const struct firmware *fw)
547 struct i2c_client *client = data->client;
550 dev_dbg(&client->dev, "Starting firmware update....\n");
552 disable_irq(client->irq);
553 data->in_fw_update = true;
555 retval = __elan_update_firmware(data, fw);
557 dev_err(&client->dev, "firmware update failed: %d\n", retval);
558 data->ops->iap_reset(client);
560 /* Reinitialize TP after fw is updated */
561 elan_initialize(data);
562 elan_query_device_info(data);
565 data->in_fw_update = false;
566 enable_irq(client->irq);
572 *******************************************************************
574 *******************************************************************
576 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
577 struct device_attribute *attr,
580 struct i2c_client *client = to_i2c_client(dev);
581 struct elan_tp_data *data = i2c_get_clientdata(client);
583 return sprintf(buf, "0x%04x\n", data->fw_checksum);
586 static ssize_t elan_sysfs_read_product_id(struct device *dev,
587 struct device_attribute *attr,
590 struct i2c_client *client = to_i2c_client(dev);
591 struct elan_tp_data *data = i2c_get_clientdata(client);
593 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
597 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
598 struct device_attribute *attr,
601 struct i2c_client *client = to_i2c_client(dev);
602 struct elan_tp_data *data = i2c_get_clientdata(client);
604 return sprintf(buf, "%d.0\n", data->fw_version);
607 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
608 struct device_attribute *attr,
611 struct i2c_client *client = to_i2c_client(dev);
612 struct elan_tp_data *data = i2c_get_clientdata(client);
614 return sprintf(buf, "%d.0\n", data->sm_version);
617 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
618 struct device_attribute *attr,
621 struct i2c_client *client = to_i2c_client(dev);
622 struct elan_tp_data *data = i2c_get_clientdata(client);
624 return sprintf(buf, "%d.0\n", data->iap_version);
627 static ssize_t elan_sysfs_update_fw(struct device *dev,
628 struct device_attribute *attr,
629 const char *buf, size_t count)
631 struct elan_tp_data *data = dev_get_drvdata(dev);
632 const struct firmware *fw;
635 const u8 *fw_signature;
636 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
638 if (data->fw_validpage_count == 0)
641 /* Look for a firmware with the product id appended. */
642 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
644 dev_err(dev, "failed to allocate memory for firmware name\n");
648 dev_info(dev, "requesting fw '%s'\n", fw_name);
649 error = request_firmware(&fw, fw_name, dev);
652 dev_err(dev, "failed to request firmware: %d\n", error);
656 /* Firmware file must match signature data */
657 fw_signature = &fw->data[data->fw_signature_address];
658 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
659 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
660 (int)sizeof(signature), signature,
661 (int)sizeof(signature), fw_signature);
666 error = mutex_lock_interruptible(&data->sysfs_mutex);
670 error = elan_update_firmware(data, fw);
672 mutex_unlock(&data->sysfs_mutex);
675 release_firmware(fw);
676 return error ?: count;
679 static ssize_t calibrate_store(struct device *dev,
680 struct device_attribute *attr,
681 const char *buf, size_t count)
683 struct i2c_client *client = to_i2c_client(dev);
684 struct elan_tp_data *data = i2c_get_clientdata(client);
688 u8 val[ETP_CALIBRATE_MAX_LEN];
690 retval = mutex_lock_interruptible(&data->sysfs_mutex);
694 disable_irq(client->irq);
696 data->mode |= ETP_ENABLE_CALIBRATE;
697 retval = data->ops->set_mode(client, data->mode);
699 dev_err(dev, "failed to enable calibration mode: %d\n",
704 retval = data->ops->calibrate(client);
706 dev_err(dev, "failed to start calibration: %d\n",
708 goto out_disable_calibrate;
713 /* Wait 250ms before checking if calibration has completed. */
716 retval = data->ops->calibrate_result(client, val);
718 dev_err(dev, "failed to check calibration result: %d\n",
720 else if (val[0] == 0)
721 break; /* calibration done */
726 dev_err(dev, "failed to calibrate. Timeout.\n");
730 out_disable_calibrate:
731 data->mode &= ~ETP_ENABLE_CALIBRATE;
732 error = data->ops->set_mode(data->client, data->mode);
734 dev_err(dev, "failed to disable calibration mode: %d\n",
740 enable_irq(client->irq);
741 mutex_unlock(&data->sysfs_mutex);
742 return retval ?: count;
745 static ssize_t elan_sysfs_read_mode(struct device *dev,
746 struct device_attribute *attr,
749 struct i2c_client *client = to_i2c_client(dev);
750 struct elan_tp_data *data = i2c_get_clientdata(client);
754 error = mutex_lock_interruptible(&data->sysfs_mutex);
758 error = data->ops->iap_get_mode(data->client, &mode);
760 mutex_unlock(&data->sysfs_mutex);
765 return sprintf(buf, "%d\n", (int)mode);
768 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
769 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
770 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
771 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
772 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
773 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
774 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
776 static DEVICE_ATTR_WO(calibrate);
778 static struct attribute *elan_sysfs_entries[] = {
779 &dev_attr_product_id.attr,
780 &dev_attr_firmware_version.attr,
781 &dev_attr_sample_version.attr,
782 &dev_attr_iap_version.attr,
783 &dev_attr_fw_checksum.attr,
784 &dev_attr_calibrate.attr,
786 &dev_attr_update_fw.attr,
790 static const struct attribute_group elan_sysfs_group = {
791 .attrs = elan_sysfs_entries,
794 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
795 const char *buf, size_t count)
797 struct i2c_client *client = to_i2c_client(dev);
798 struct elan_tp_data *data = i2c_get_clientdata(client);
802 retval = mutex_lock_interruptible(&data->sysfs_mutex);
806 disable_irq(client->irq);
808 data->baseline_ready = false;
810 data->mode |= ETP_ENABLE_CALIBRATE;
811 retval = data->ops->set_mode(data->client, data->mode);
813 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
820 retval = data->ops->get_baseline_data(data->client, true,
821 &data->max_baseline);
823 dev_err(dev, "Failed to read max baseline form device: %d\n",
825 goto out_disable_calibrate;
828 retval = data->ops->get_baseline_data(data->client, false,
829 &data->min_baseline);
831 dev_err(dev, "Failed to read min baseline form device: %d\n",
833 goto out_disable_calibrate;
836 data->baseline_ready = true;
838 out_disable_calibrate:
839 data->mode &= ~ETP_ENABLE_CALIBRATE;
840 error = data->ops->set_mode(data->client, data->mode);
842 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
848 enable_irq(client->irq);
849 mutex_unlock(&data->sysfs_mutex);
850 return retval ?: count;
853 static ssize_t min_show(struct device *dev,
854 struct device_attribute *attr, char *buf)
856 struct i2c_client *client = to_i2c_client(dev);
857 struct elan_tp_data *data = i2c_get_clientdata(client);
860 retval = mutex_lock_interruptible(&data->sysfs_mutex);
864 if (!data->baseline_ready) {
869 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
872 mutex_unlock(&data->sysfs_mutex);
876 static ssize_t max_show(struct device *dev,
877 struct device_attribute *attr, char *buf)
879 struct i2c_client *client = to_i2c_client(dev);
880 struct elan_tp_data *data = i2c_get_clientdata(client);
883 retval = mutex_lock_interruptible(&data->sysfs_mutex);
887 if (!data->baseline_ready) {
892 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
895 mutex_unlock(&data->sysfs_mutex);
900 static DEVICE_ATTR_WO(acquire);
901 static DEVICE_ATTR_RO(min);
902 static DEVICE_ATTR_RO(max);
904 static struct attribute *elan_baseline_sysfs_entries[] = {
905 &dev_attr_acquire.attr,
911 static const struct attribute_group elan_baseline_sysfs_group = {
913 .attrs = elan_baseline_sysfs_entries,
916 static const struct attribute_group *elan_sysfs_groups[] = {
918 &elan_baseline_sysfs_group,
923 ******************************************************************
925 ******************************************************************
927 static void elan_report_contact(struct elan_tp_data *data, int contact_num,
928 bool contact_valid, bool high_precision,
929 u8 *packet, u8 *finger_data)
931 struct input_dev *input = data->input;
932 unsigned int pos_x, pos_y;
933 unsigned int pressure, scaled_pressure;
936 if (high_precision) {
937 pos_x = get_unaligned_be16(&finger_data[0]);
938 pos_y = get_unaligned_be16(&finger_data[2]);
940 pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1];
941 pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2];
944 if (pos_x > data->max_x || pos_y > data->max_y) {
945 dev_dbg(input->dev.parent,
946 "[%d] x=%d y=%d over max (%d, %d)",
947 contact_num, pos_x, pos_y,
948 data->max_x, data->max_y);
952 pressure = finger_data[4];
953 scaled_pressure = pressure + data->pressure_adjustment;
954 if (scaled_pressure > ETP_MAX_PRESSURE)
955 scaled_pressure = ETP_MAX_PRESSURE;
957 input_mt_slot(input, contact_num);
958 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
959 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
960 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
961 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
963 if (data->report_features & ETP_FEATURE_REPORT_MK) {
964 unsigned int mk_x, mk_y, area_x, area_y;
965 u8 mk_data = high_precision ?
966 packet[ETP_MK_DATA_OFFSET + contact_num] :
969 mk_x = mk_data & 0x0f;
973 * To avoid treating large finger as palm, let's reduce
974 * the width x and y per trace.
976 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
977 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
979 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
980 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
981 max(area_x, area_y));
982 input_report_abs(input, ABS_MT_TOUCH_MINOR,
983 min(area_x, area_y));
986 input_mt_slot(input, contact_num);
987 input_mt_report_slot_inactive(input);
991 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet,
994 struct input_dev *input = data->input;
995 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
997 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
998 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
999 bool contact_valid, hover_event;
1001 pm_wakeup_event(&data->client->dev, 0);
1003 hover_event = hover_info & BIT(6);
1005 for (i = 0; i < ETP_MAX_FINGERS; i++) {
1006 contact_valid = tp_info & BIT(3 + i);
1007 elan_report_contact(data, i, contact_valid, high_precision,
1008 packet, finger_data);
1010 finger_data += ETP_FINGER_DATA_LEN;
1013 input_report_key(input, BTN_LEFT, tp_info & BIT(0));
1014 input_report_key(input, BTN_MIDDLE, tp_info & BIT(2));
1015 input_report_key(input, BTN_RIGHT, tp_info & BIT(1));
1016 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
1017 input_mt_report_pointer_emulation(input, true);
1021 static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report)
1023 struct input_dev *input = data->tp_input;
1024 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1];
1027 pm_wakeup_event(&data->client->dev, 0);
1029 if (!data->tp_input) {
1030 dev_warn_once(&data->client->dev,
1031 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
1035 input_report_key(input, BTN_LEFT, packet[0] & 0x01);
1036 input_report_key(input, BTN_RIGHT, packet[0] & 0x02);
1037 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04);
1039 if ((packet[3] & 0x0F) == 0x06) {
1040 x = packet[4] - (int)((packet[1] ^ 0x80) << 1);
1041 y = (int)((packet[2] ^ 0x80) << 1) - packet[5];
1043 input_report_rel(input, REL_X, x);
1044 input_report_rel(input, REL_Y, y);
1050 static irqreturn_t elan_isr(int irq, void *dev_id)
1052 struct elan_tp_data *data = dev_id;
1054 u8 report[ETP_MAX_REPORT_LEN];
1057 * When device is connected to i2c bus, when all IAP page writes
1058 * complete, the driver will receive interrupt and must read
1059 * 0000 to confirm that IAP is finished.
1061 if (data->in_fw_update) {
1062 complete(&data->fw_completion);
1066 error = data->ops->get_report(data->client, report, data->report_len);
1070 switch (report[ETP_REPORT_ID_OFFSET]) {
1072 elan_report_absolute(data, report, false);
1074 case ETP_REPORT_ID2:
1075 elan_report_absolute(data, report, true);
1077 case ETP_TP_REPORT_ID:
1078 elan_report_trackpoint(data, report);
1081 dev_err(&data->client->dev, "invalid report id data (%x)\n",
1082 report[ETP_REPORT_ID_OFFSET]);
1090 ******************************************************************
1091 * Elan initialization functions
1092 ******************************************************************
1095 static int elan_setup_trackpoint_input_device(struct elan_tp_data *data)
1097 struct device *dev = &data->client->dev;
1098 struct input_dev *input;
1100 input = devm_input_allocate_device(dev);
1104 input->name = "Elan TrackPoint";
1105 input->id.bustype = BUS_I2C;
1106 input->id.vendor = ELAN_VENDOR_ID;
1107 input->id.product = data->product_id;
1108 input_set_drvdata(input, data);
1110 input_set_capability(input, EV_REL, REL_X);
1111 input_set_capability(input, EV_REL, REL_Y);
1112 input_set_capability(input, EV_KEY, BTN_LEFT);
1113 input_set_capability(input, EV_KEY, BTN_RIGHT);
1114 input_set_capability(input, EV_KEY, BTN_MIDDLE);
1116 __set_bit(INPUT_PROP_POINTER, input->propbit);
1117 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit);
1119 data->tp_input = input;
1124 static int elan_setup_input_device(struct elan_tp_data *data)
1126 struct device *dev = &data->client->dev;
1127 struct input_dev *input;
1128 unsigned int max_width = max(data->width_x, data->width_y);
1129 unsigned int min_width = min(data->width_x, data->width_y);
1132 input = devm_input_allocate_device(dev);
1136 input->name = "Elan Touchpad";
1137 input->id.bustype = BUS_I2C;
1138 input->id.vendor = ELAN_VENDOR_ID;
1139 input->id.product = data->product_id;
1140 input_set_drvdata(input, data);
1142 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
1143 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
1145 dev_err(dev, "failed to initialize MT slots: %d\n", error);
1149 __set_bit(EV_ABS, input->evbit);
1150 __set_bit(INPUT_PROP_POINTER, input->propbit);
1151 if (data->clickpad) {
1152 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1154 __set_bit(BTN_RIGHT, input->keybit);
1155 if (data->middle_button)
1156 __set_bit(BTN_MIDDLE, input->keybit);
1158 __set_bit(BTN_LEFT, input->keybit);
1160 /* Set up ST parameters */
1161 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
1162 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
1163 input_abs_set_res(input, ABS_X, data->x_res);
1164 input_abs_set_res(input, ABS_Y, data->y_res);
1165 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
1166 if (data->report_features & ETP_FEATURE_REPORT_MK)
1167 input_set_abs_params(input, ABS_TOOL_WIDTH,
1168 0, ETP_FINGER_WIDTH, 0, 0);
1169 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
1171 /* And MT parameters */
1172 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
1173 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
1174 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
1175 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
1176 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
1177 ETP_MAX_PRESSURE, 0, 0);
1178 if (data->report_features & ETP_FEATURE_REPORT_MK) {
1179 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1180 0, ETP_FINGER_WIDTH * max_width, 0, 0);
1181 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1182 0, ETP_FINGER_WIDTH * min_width, 0, 0);
1185 data->input = input;
1190 static void elan_disable_regulator(void *_data)
1192 struct elan_tp_data *data = _data;
1194 regulator_disable(data->vcc);
1197 static int elan_probe(struct i2c_client *client,
1198 const struct i2c_device_id *dev_id)
1200 const struct elan_transport_ops *transport_ops;
1201 struct device *dev = &client->dev;
1202 struct elan_tp_data *data;
1203 unsigned long irqflags;
1206 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1207 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1208 transport_ops = &elan_i2c_ops;
1209 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1210 i2c_check_functionality(client->adapter,
1211 I2C_FUNC_SMBUS_BYTE_DATA |
1212 I2C_FUNC_SMBUS_BLOCK_DATA |
1213 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1214 transport_ops = &elan_smbus_ops;
1216 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1220 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1224 i2c_set_clientdata(client, data);
1226 data->ops = transport_ops;
1227 data->client = client;
1228 init_completion(&data->fw_completion);
1229 mutex_init(&data->sysfs_mutex);
1231 data->vcc = devm_regulator_get(dev, "vcc");
1232 if (IS_ERR(data->vcc)) {
1233 error = PTR_ERR(data->vcc);
1234 if (error != -EPROBE_DEFER)
1235 dev_err(dev, "Failed to get 'vcc' regulator: %d\n",
1240 error = regulator_enable(data->vcc);
1242 dev_err(dev, "Failed to enable regulator: %d\n", error);
1246 error = devm_add_action_or_reset(dev, elan_disable_regulator, data);
1248 dev_err(dev, "Failed to add disable regulator action: %d\n",
1253 /* Make sure there is something at this address */
1254 error = i2c_smbus_read_byte(client);
1256 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1260 /* Initialize the touchpad. */
1261 error = elan_initialize(data);
1265 error = elan_query_device_info(data);
1269 error = elan_query_device_parameters(data);
1274 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1281 "Elan Touchpad Extra Information:\n"
1282 " Max ABS X,Y: %d,%d\n"
1283 " Width X,Y: %d,%d\n"
1284 " Resolution X,Y: %d,%d (dots/mm)\n"
1286 " info pattern: 0x%x\n",
1287 data->max_x, data->max_y,
1288 data->width_x, data->width_y,
1289 data->x_res, data->y_res,
1290 data->ic_type, data->pattern);
1292 /* Set up input device properties based on queried parameters. */
1293 error = elan_setup_input_device(data);
1297 if (device_property_read_bool(&client->dev, "elan,trackpoint")) {
1298 error = elan_setup_trackpoint_input_device(data);
1304 * Platform code (ACPI, DTS) should normally set up interrupt
1305 * for us, but in case it did not let's fall back to using falling
1306 * edge to be compatible with older Chromebooks.
1308 irqflags = irq_get_trigger_type(client->irq);
1310 irqflags = IRQF_TRIGGER_FALLING;
1312 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1313 irqflags | IRQF_ONESHOT,
1314 client->name, data);
1316 dev_err(dev, "cannot register irq=%d\n", client->irq);
1320 error = devm_device_add_groups(dev, elan_sysfs_groups);
1322 dev_err(dev, "failed to create sysfs attributes: %d\n", error);
1326 error = input_register_device(data->input);
1328 dev_err(dev, "failed to register input device: %d\n", error);
1332 if (data->tp_input) {
1333 error = input_register_device(data->tp_input);
1335 dev_err(&client->dev,
1336 "failed to register TrackPoint input device: %d\n",
1343 * Systems using device tree should set up wakeup via DTS,
1344 * the rest will configure device as wakeup source by default.
1347 device_init_wakeup(dev, true);
1352 static int __maybe_unused elan_suspend(struct device *dev)
1354 struct i2c_client *client = to_i2c_client(dev);
1355 struct elan_tp_data *data = i2c_get_clientdata(client);
1359 * We are taking the mutex to make sure sysfs operations are
1360 * complete before we attempt to bring the device into low[er]
1363 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1367 disable_irq(client->irq);
1369 if (device_may_wakeup(dev)) {
1370 ret = elan_sleep(data);
1371 /* Enable wake from IRQ */
1372 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1374 ret = elan_disable_power(data);
1377 mutex_unlock(&data->sysfs_mutex);
1381 static int __maybe_unused elan_resume(struct device *dev)
1383 struct i2c_client *client = to_i2c_client(dev);
1384 struct elan_tp_data *data = i2c_get_clientdata(client);
1387 if (device_may_wakeup(dev) && data->irq_wake) {
1388 disable_irq_wake(client->irq);
1389 data->irq_wake = false;
1392 error = elan_enable_power(data);
1394 dev_err(dev, "power up when resuming failed: %d\n", error);
1398 error = elan_initialize(data);
1400 dev_err(dev, "initialize when resuming failed: %d\n", error);
1403 enable_irq(data->client->irq);
1407 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1409 static const struct i2c_device_id elan_id[] = {
1413 MODULE_DEVICE_TABLE(i2c, elan_id);
1416 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1420 static const struct of_device_id elan_of_match[] = {
1421 { .compatible = "elan,ekth3000" },
1424 MODULE_DEVICE_TABLE(of, elan_of_match);
1427 static struct i2c_driver elan_driver = {
1429 .name = DRIVER_NAME,
1431 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1432 .of_match_table = of_match_ptr(elan_of_match),
1433 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1435 .probe = elan_probe,
1436 .id_table = elan_id,
1439 module_i2c_driver(elan_driver);
1441 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1442 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1443 MODULE_LICENSE("GPL");