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,
505 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
507 boot_page_count = (iap_start_addr * 2) / data->fw_page_size;
508 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
510 const u8 *page = &fw->data[i * data->fw_page_size];
512 for (j = 0; j < data->fw_page_size; j += 2)
513 checksum += ((page[j + 1] << 8) | page[j]);
515 error = elan_write_fw_block(data, data->fw_page_size,
518 dev_err(dev, "write page %d fail: %d\n", i, error);
522 sw_checksum += checksum;
525 /* Wait WDT reset and power on reset */
528 error = data->ops->finish_fw_update(client, &data->fw_completion);
532 error = data->ops->get_checksum(client, true, &fw_checksum);
536 if (sw_checksum != fw_checksum) {
537 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
538 sw_checksum, fw_checksum);
545 static int elan_update_firmware(struct elan_tp_data *data,
546 const struct firmware *fw)
548 struct i2c_client *client = data->client;
551 dev_dbg(&client->dev, "Starting firmware update....\n");
553 disable_irq(client->irq);
554 data->in_fw_update = true;
556 retval = __elan_update_firmware(data, fw);
558 dev_err(&client->dev, "firmware update failed: %d\n", retval);
559 data->ops->iap_reset(client);
561 /* Reinitialize TP after fw is updated */
562 elan_initialize(data);
563 elan_query_device_info(data);
566 data->in_fw_update = false;
567 enable_irq(client->irq);
573 *******************************************************************
575 *******************************************************************
577 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
578 struct device_attribute *attr,
581 struct i2c_client *client = to_i2c_client(dev);
582 struct elan_tp_data *data = i2c_get_clientdata(client);
584 return sprintf(buf, "0x%04x\n", data->fw_checksum);
587 static ssize_t elan_sysfs_read_product_id(struct device *dev,
588 struct device_attribute *attr,
591 struct i2c_client *client = to_i2c_client(dev);
592 struct elan_tp_data *data = i2c_get_clientdata(client);
594 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
598 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
599 struct device_attribute *attr,
602 struct i2c_client *client = to_i2c_client(dev);
603 struct elan_tp_data *data = i2c_get_clientdata(client);
605 return sprintf(buf, "%d.0\n", data->fw_version);
608 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
609 struct device_attribute *attr,
612 struct i2c_client *client = to_i2c_client(dev);
613 struct elan_tp_data *data = i2c_get_clientdata(client);
615 return sprintf(buf, "%d.0\n", data->sm_version);
618 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
619 struct device_attribute *attr,
622 struct i2c_client *client = to_i2c_client(dev);
623 struct elan_tp_data *data = i2c_get_clientdata(client);
625 return sprintf(buf, "%d.0\n", data->iap_version);
628 static ssize_t elan_sysfs_update_fw(struct device *dev,
629 struct device_attribute *attr,
630 const char *buf, size_t count)
632 struct elan_tp_data *data = dev_get_drvdata(dev);
633 const struct firmware *fw;
636 const u8 *fw_signature;
637 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
639 if (data->fw_validpage_count == 0)
642 /* Look for a firmware with the product id appended. */
643 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
645 dev_err(dev, "failed to allocate memory for firmware name\n");
649 dev_info(dev, "requesting fw '%s'\n", fw_name);
650 error = request_firmware(&fw, fw_name, dev);
653 dev_err(dev, "failed to request firmware: %d\n", error);
657 /* Firmware file must match signature data */
658 fw_signature = &fw->data[data->fw_signature_address];
659 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
660 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
661 (int)sizeof(signature), signature,
662 (int)sizeof(signature), fw_signature);
667 error = mutex_lock_interruptible(&data->sysfs_mutex);
671 error = elan_update_firmware(data, fw);
673 mutex_unlock(&data->sysfs_mutex);
676 release_firmware(fw);
677 return error ?: count;
680 static ssize_t calibrate_store(struct device *dev,
681 struct device_attribute *attr,
682 const char *buf, size_t count)
684 struct i2c_client *client = to_i2c_client(dev);
685 struct elan_tp_data *data = i2c_get_clientdata(client);
689 u8 val[ETP_CALIBRATE_MAX_LEN];
691 retval = mutex_lock_interruptible(&data->sysfs_mutex);
695 disable_irq(client->irq);
697 data->mode |= ETP_ENABLE_CALIBRATE;
698 retval = data->ops->set_mode(client, data->mode);
700 dev_err(dev, "failed to enable calibration mode: %d\n",
705 retval = data->ops->calibrate(client);
707 dev_err(dev, "failed to start calibration: %d\n",
709 goto out_disable_calibrate;
714 /* Wait 250ms before checking if calibration has completed. */
717 retval = data->ops->calibrate_result(client, val);
719 dev_err(dev, "failed to check calibration result: %d\n",
721 else if (val[0] == 0)
722 break; /* calibration done */
727 dev_err(dev, "failed to calibrate. Timeout.\n");
731 out_disable_calibrate:
732 data->mode &= ~ETP_ENABLE_CALIBRATE;
733 error = data->ops->set_mode(data->client, data->mode);
735 dev_err(dev, "failed to disable calibration mode: %d\n",
741 enable_irq(client->irq);
742 mutex_unlock(&data->sysfs_mutex);
743 return retval ?: count;
746 static ssize_t elan_sysfs_read_mode(struct device *dev,
747 struct device_attribute *attr,
750 struct i2c_client *client = to_i2c_client(dev);
751 struct elan_tp_data *data = i2c_get_clientdata(client);
755 error = mutex_lock_interruptible(&data->sysfs_mutex);
759 error = data->ops->iap_get_mode(data->client, &mode);
761 mutex_unlock(&data->sysfs_mutex);
766 return sprintf(buf, "%d\n", (int)mode);
769 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
770 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
771 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
772 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
773 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
774 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
775 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
777 static DEVICE_ATTR_WO(calibrate);
779 static struct attribute *elan_sysfs_entries[] = {
780 &dev_attr_product_id.attr,
781 &dev_attr_firmware_version.attr,
782 &dev_attr_sample_version.attr,
783 &dev_attr_iap_version.attr,
784 &dev_attr_fw_checksum.attr,
785 &dev_attr_calibrate.attr,
787 &dev_attr_update_fw.attr,
791 static const struct attribute_group elan_sysfs_group = {
792 .attrs = elan_sysfs_entries,
795 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
796 const char *buf, size_t count)
798 struct i2c_client *client = to_i2c_client(dev);
799 struct elan_tp_data *data = i2c_get_clientdata(client);
803 retval = mutex_lock_interruptible(&data->sysfs_mutex);
807 disable_irq(client->irq);
809 data->baseline_ready = false;
811 data->mode |= ETP_ENABLE_CALIBRATE;
812 retval = data->ops->set_mode(data->client, data->mode);
814 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
821 retval = data->ops->get_baseline_data(data->client, true,
822 &data->max_baseline);
824 dev_err(dev, "Failed to read max baseline form device: %d\n",
826 goto out_disable_calibrate;
829 retval = data->ops->get_baseline_data(data->client, false,
830 &data->min_baseline);
832 dev_err(dev, "Failed to read min baseline form device: %d\n",
834 goto out_disable_calibrate;
837 data->baseline_ready = true;
839 out_disable_calibrate:
840 data->mode &= ~ETP_ENABLE_CALIBRATE;
841 error = data->ops->set_mode(data->client, data->mode);
843 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
849 enable_irq(client->irq);
850 mutex_unlock(&data->sysfs_mutex);
851 return retval ?: count;
854 static ssize_t min_show(struct device *dev,
855 struct device_attribute *attr, char *buf)
857 struct i2c_client *client = to_i2c_client(dev);
858 struct elan_tp_data *data = i2c_get_clientdata(client);
861 retval = mutex_lock_interruptible(&data->sysfs_mutex);
865 if (!data->baseline_ready) {
870 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
873 mutex_unlock(&data->sysfs_mutex);
877 static ssize_t max_show(struct device *dev,
878 struct device_attribute *attr, char *buf)
880 struct i2c_client *client = to_i2c_client(dev);
881 struct elan_tp_data *data = i2c_get_clientdata(client);
884 retval = mutex_lock_interruptible(&data->sysfs_mutex);
888 if (!data->baseline_ready) {
893 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
896 mutex_unlock(&data->sysfs_mutex);
901 static DEVICE_ATTR_WO(acquire);
902 static DEVICE_ATTR_RO(min);
903 static DEVICE_ATTR_RO(max);
905 static struct attribute *elan_baseline_sysfs_entries[] = {
906 &dev_attr_acquire.attr,
912 static const struct attribute_group elan_baseline_sysfs_group = {
914 .attrs = elan_baseline_sysfs_entries,
917 static const struct attribute_group *elan_sysfs_groups[] = {
919 &elan_baseline_sysfs_group,
924 ******************************************************************
926 ******************************************************************
928 static void elan_report_contact(struct elan_tp_data *data, int contact_num,
929 bool contact_valid, bool high_precision,
930 u8 *packet, u8 *finger_data)
932 struct input_dev *input = data->input;
933 unsigned int pos_x, pos_y;
934 unsigned int pressure, scaled_pressure;
937 if (high_precision) {
938 pos_x = get_unaligned_be16(&finger_data[0]);
939 pos_y = get_unaligned_be16(&finger_data[2]);
941 pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1];
942 pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2];
945 if (pos_x > data->max_x || pos_y > data->max_y) {
946 dev_dbg(input->dev.parent,
947 "[%d] x=%d y=%d over max (%d, %d)",
948 contact_num, pos_x, pos_y,
949 data->max_x, data->max_y);
953 pressure = finger_data[4];
954 scaled_pressure = pressure + data->pressure_adjustment;
955 if (scaled_pressure > ETP_MAX_PRESSURE)
956 scaled_pressure = ETP_MAX_PRESSURE;
958 input_mt_slot(input, contact_num);
959 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
960 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
961 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
962 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
964 if (data->report_features & ETP_FEATURE_REPORT_MK) {
965 unsigned int mk_x, mk_y, area_x, area_y;
966 u8 mk_data = high_precision ?
967 packet[ETP_MK_DATA_OFFSET + contact_num] :
970 mk_x = mk_data & 0x0f;
974 * To avoid treating large finger as palm, let's reduce
975 * the width x and y per trace.
977 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
978 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
980 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
981 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
982 max(area_x, area_y));
983 input_report_abs(input, ABS_MT_TOUCH_MINOR,
984 min(area_x, area_y));
987 input_mt_slot(input, contact_num);
988 input_mt_report_slot_inactive(input);
992 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet,
995 struct input_dev *input = data->input;
996 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
998 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
999 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
1000 bool contact_valid, hover_event;
1002 pm_wakeup_event(&data->client->dev, 0);
1004 hover_event = hover_info & BIT(6);
1006 for (i = 0; i < ETP_MAX_FINGERS; i++) {
1007 contact_valid = tp_info & BIT(3 + i);
1008 elan_report_contact(data, i, contact_valid, high_precision,
1009 packet, finger_data);
1011 finger_data += ETP_FINGER_DATA_LEN;
1014 input_report_key(input, BTN_LEFT, tp_info & BIT(0));
1015 input_report_key(input, BTN_MIDDLE, tp_info & BIT(2));
1016 input_report_key(input, BTN_RIGHT, tp_info & BIT(1));
1017 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
1018 input_mt_report_pointer_emulation(input, true);
1022 static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report)
1024 struct input_dev *input = data->tp_input;
1025 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1];
1028 pm_wakeup_event(&data->client->dev, 0);
1030 if (!data->tp_input) {
1031 dev_warn_once(&data->client->dev,
1032 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
1036 input_report_key(input, BTN_LEFT, packet[0] & 0x01);
1037 input_report_key(input, BTN_RIGHT, packet[0] & 0x02);
1038 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04);
1040 if ((packet[3] & 0x0F) == 0x06) {
1041 x = packet[4] - (int)((packet[1] ^ 0x80) << 1);
1042 y = (int)((packet[2] ^ 0x80) << 1) - packet[5];
1044 input_report_rel(input, REL_X, x);
1045 input_report_rel(input, REL_Y, y);
1051 static irqreturn_t elan_isr(int irq, void *dev_id)
1053 struct elan_tp_data *data = dev_id;
1055 u8 report[ETP_MAX_REPORT_LEN];
1058 * When device is connected to i2c bus, when all IAP page writes
1059 * complete, the driver will receive interrupt and must read
1060 * 0000 to confirm that IAP is finished.
1062 if (data->in_fw_update) {
1063 complete(&data->fw_completion);
1067 error = data->ops->get_report(data->client, report, data->report_len);
1071 switch (report[ETP_REPORT_ID_OFFSET]) {
1073 elan_report_absolute(data, report, false);
1075 case ETP_REPORT_ID2:
1076 elan_report_absolute(data, report, true);
1078 case ETP_TP_REPORT_ID:
1079 elan_report_trackpoint(data, report);
1082 dev_err(&data->client->dev, "invalid report id data (%x)\n",
1083 report[ETP_REPORT_ID_OFFSET]);
1091 ******************************************************************
1092 * Elan initialization functions
1093 ******************************************************************
1096 static int elan_setup_trackpoint_input_device(struct elan_tp_data *data)
1098 struct device *dev = &data->client->dev;
1099 struct input_dev *input;
1101 input = devm_input_allocate_device(dev);
1105 input->name = "Elan TrackPoint";
1106 input->id.bustype = BUS_I2C;
1107 input->id.vendor = ELAN_VENDOR_ID;
1108 input->id.product = data->product_id;
1109 input_set_drvdata(input, data);
1111 input_set_capability(input, EV_REL, REL_X);
1112 input_set_capability(input, EV_REL, REL_Y);
1113 input_set_capability(input, EV_KEY, BTN_LEFT);
1114 input_set_capability(input, EV_KEY, BTN_RIGHT);
1115 input_set_capability(input, EV_KEY, BTN_MIDDLE);
1117 __set_bit(INPUT_PROP_POINTER, input->propbit);
1118 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit);
1120 data->tp_input = input;
1125 static int elan_setup_input_device(struct elan_tp_data *data)
1127 struct device *dev = &data->client->dev;
1128 struct input_dev *input;
1129 unsigned int max_width = max(data->width_x, data->width_y);
1130 unsigned int min_width = min(data->width_x, data->width_y);
1133 input = devm_input_allocate_device(dev);
1137 input->name = "Elan Touchpad";
1138 input->id.bustype = BUS_I2C;
1139 input->id.vendor = ELAN_VENDOR_ID;
1140 input->id.product = data->product_id;
1141 input_set_drvdata(input, data);
1143 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
1144 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
1146 dev_err(dev, "failed to initialize MT slots: %d\n", error);
1150 __set_bit(EV_ABS, input->evbit);
1151 __set_bit(INPUT_PROP_POINTER, input->propbit);
1152 if (data->clickpad) {
1153 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1155 __set_bit(BTN_RIGHT, input->keybit);
1156 if (data->middle_button)
1157 __set_bit(BTN_MIDDLE, input->keybit);
1159 __set_bit(BTN_LEFT, input->keybit);
1161 /* Set up ST parameters */
1162 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
1163 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
1164 input_abs_set_res(input, ABS_X, data->x_res);
1165 input_abs_set_res(input, ABS_Y, data->y_res);
1166 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
1167 if (data->report_features & ETP_FEATURE_REPORT_MK)
1168 input_set_abs_params(input, ABS_TOOL_WIDTH,
1169 0, ETP_FINGER_WIDTH, 0, 0);
1170 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
1172 /* And MT parameters */
1173 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
1174 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
1175 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
1176 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
1177 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
1178 ETP_MAX_PRESSURE, 0, 0);
1179 if (data->report_features & ETP_FEATURE_REPORT_MK) {
1180 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1181 0, ETP_FINGER_WIDTH * max_width, 0, 0);
1182 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1183 0, ETP_FINGER_WIDTH * min_width, 0, 0);
1186 data->input = input;
1191 static void elan_disable_regulator(void *_data)
1193 struct elan_tp_data *data = _data;
1195 regulator_disable(data->vcc);
1198 static int elan_probe(struct i2c_client *client,
1199 const struct i2c_device_id *dev_id)
1201 const struct elan_transport_ops *transport_ops;
1202 struct device *dev = &client->dev;
1203 struct elan_tp_data *data;
1204 unsigned long irqflags;
1207 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1208 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1209 transport_ops = &elan_i2c_ops;
1210 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1211 i2c_check_functionality(client->adapter,
1212 I2C_FUNC_SMBUS_BYTE_DATA |
1213 I2C_FUNC_SMBUS_BLOCK_DATA |
1214 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1215 transport_ops = &elan_smbus_ops;
1217 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1221 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1225 i2c_set_clientdata(client, data);
1227 data->ops = transport_ops;
1228 data->client = client;
1229 init_completion(&data->fw_completion);
1230 mutex_init(&data->sysfs_mutex);
1232 data->vcc = devm_regulator_get(dev, "vcc");
1233 if (IS_ERR(data->vcc)) {
1234 error = PTR_ERR(data->vcc);
1235 if (error != -EPROBE_DEFER)
1236 dev_err(dev, "Failed to get 'vcc' regulator: %d\n",
1241 error = regulator_enable(data->vcc);
1243 dev_err(dev, "Failed to enable regulator: %d\n", error);
1247 error = devm_add_action_or_reset(dev, elan_disable_regulator, data);
1249 dev_err(dev, "Failed to add disable regulator action: %d\n",
1254 /* Make sure there is something at this address */
1255 error = i2c_smbus_read_byte(client);
1257 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1261 /* Initialize the touchpad. */
1262 error = elan_initialize(data);
1266 error = elan_query_device_info(data);
1270 error = elan_query_device_parameters(data);
1275 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1282 "Elan Touchpad Extra Information:\n"
1283 " Max ABS X,Y: %d,%d\n"
1284 " Width X,Y: %d,%d\n"
1285 " Resolution X,Y: %d,%d (dots/mm)\n"
1287 " info pattern: 0x%x\n",
1288 data->max_x, data->max_y,
1289 data->width_x, data->width_y,
1290 data->x_res, data->y_res,
1291 data->ic_type, data->pattern);
1293 /* Set up input device properties based on queried parameters. */
1294 error = elan_setup_input_device(data);
1298 if (device_property_read_bool(&client->dev, "elan,trackpoint")) {
1299 error = elan_setup_trackpoint_input_device(data);
1305 * Platform code (ACPI, DTS) should normally set up interrupt
1306 * for us, but in case it did not let's fall back to using falling
1307 * edge to be compatible with older Chromebooks.
1309 irqflags = irq_get_trigger_type(client->irq);
1311 irqflags = IRQF_TRIGGER_FALLING;
1313 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1314 irqflags | IRQF_ONESHOT,
1315 client->name, data);
1317 dev_err(dev, "cannot register irq=%d\n", client->irq);
1321 error = devm_device_add_groups(dev, elan_sysfs_groups);
1323 dev_err(dev, "failed to create sysfs attributes: %d\n", error);
1327 error = input_register_device(data->input);
1329 dev_err(dev, "failed to register input device: %d\n", error);
1333 if (data->tp_input) {
1334 error = input_register_device(data->tp_input);
1336 dev_err(&client->dev,
1337 "failed to register TrackPoint input device: %d\n",
1344 * Systems using device tree should set up wakeup via DTS,
1345 * the rest will configure device as wakeup source by default.
1348 device_init_wakeup(dev, true);
1353 static int __maybe_unused elan_suspend(struct device *dev)
1355 struct i2c_client *client = to_i2c_client(dev);
1356 struct elan_tp_data *data = i2c_get_clientdata(client);
1360 * We are taking the mutex to make sure sysfs operations are
1361 * complete before we attempt to bring the device into low[er]
1364 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1368 disable_irq(client->irq);
1370 if (device_may_wakeup(dev)) {
1371 ret = elan_sleep(data);
1372 /* Enable wake from IRQ */
1373 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1375 ret = elan_disable_power(data);
1378 mutex_unlock(&data->sysfs_mutex);
1382 static int __maybe_unused elan_resume(struct device *dev)
1384 struct i2c_client *client = to_i2c_client(dev);
1385 struct elan_tp_data *data = i2c_get_clientdata(client);
1388 if (device_may_wakeup(dev) && data->irq_wake) {
1389 disable_irq_wake(client->irq);
1390 data->irq_wake = false;
1393 error = elan_enable_power(data);
1395 dev_err(dev, "power up when resuming failed: %d\n", error);
1399 error = elan_initialize(data);
1401 dev_err(dev, "initialize when resuming failed: %d\n", error);
1404 enable_irq(data->client->irq);
1408 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1410 static const struct i2c_device_id elan_id[] = {
1414 MODULE_DEVICE_TABLE(i2c, elan_id);
1417 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1421 static const struct of_device_id elan_of_match[] = {
1422 { .compatible = "elan,ekth3000" },
1425 MODULE_DEVICE_TABLE(of, elan_of_match);
1428 static struct i2c_driver elan_driver = {
1430 .name = DRIVER_NAME,
1432 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1433 .of_match_table = of_match_ptr(elan_of_match),
1434 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1436 .probe = elan_probe,
1437 .id_table = elan_id,
1440 module_i2c_driver(elan_driver);
1442 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1443 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1444 MODULE_LICENSE("GPL");