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 /* The main device structure */
52 struct i2c_client *client;
53 struct input_dev *input;
54 struct input_dev *tp_input; /* trackpoint input node */
55 struct regulator *vcc;
57 const struct elan_transport_ops *ops;
60 struct completion fw_completion;
63 struct mutex sysfs_mutex;
78 unsigned int report_features;
79 unsigned int report_len;
80 int pressure_adjustment;
83 u16 fw_validpage_count;
85 u32 fw_signature_address;
96 static int elan_get_fwinfo(u16 ic_type, u8 iap_version, u16 *validpage_count,
97 u32 *signature_address, u16 *page_size)
103 *validpage_count = 512;
111 *validpage_count = 768;
114 *validpage_count = 896;
117 *validpage_count = 640;
120 *validpage_count = 1024;
123 *validpage_count = 1280;
126 *validpage_count = 2048;
130 *validpage_count = 1024;
133 /* unknown ic type clear value */
134 *validpage_count = 0;
135 *signature_address = 0;
141 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
143 if ((ic_type == 0x14 || ic_type == 0x15) && iap_version >= 2) {
144 *validpage_count /= 8;
145 *page_size = ETP_FW_PAGE_SIZE_512;
146 } else if (ic_type >= 0x0D && iap_version >= 1) {
147 *validpage_count /= 2;
148 *page_size = ETP_FW_PAGE_SIZE_128;
150 *page_size = ETP_FW_PAGE_SIZE;
156 static int elan_set_power(struct elan_tp_data *data, bool on)
158 int repeat = ETP_RETRY_COUNT;
162 error = data->ops->power_control(data->client, on);
167 } while (--repeat > 0);
169 dev_err(&data->client->dev, "failed to set power %s: %d\n",
170 on ? "on" : "off", error);
174 static int elan_sleep(struct elan_tp_data *data)
176 int repeat = ETP_RETRY_COUNT;
180 error = data->ops->sleep_control(data->client, true);
185 } while (--repeat > 0);
190 static int elan_query_product(struct elan_tp_data *data)
194 error = data->ops->get_product_id(data->client, &data->product_id);
198 error = data->ops->get_pattern(data->client, &data->pattern);
202 error = data->ops->get_sm_version(data->client, data->pattern,
203 &data->ic_type, &data->sm_version,
211 static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
213 if (data->ic_type == 0x0E) {
214 switch (data->product_id) {
220 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
221 /* ASUS EeeBook X205TA */
228 static int __elan_initialize(struct elan_tp_data *data)
230 struct i2c_client *client = data->client;
231 bool woken_up = false;
234 error = data->ops->initialize(client);
236 dev_err(&client->dev, "device initialize failed: %d\n", error);
240 error = elan_query_product(data);
245 * Some ASUS devices were shipped with firmware that requires
246 * touchpads to be woken up first, before attempting to switch
247 * them into absolute reporting mode.
249 if (elan_check_ASUS_special_fw(data)) {
250 error = data->ops->sleep_control(client, false);
252 dev_err(&client->dev,
253 "failed to wake device up: %d\n", error);
261 data->mode |= ETP_ENABLE_ABS;
262 error = data->ops->set_mode(client, data->mode);
264 dev_err(&client->dev,
265 "failed to switch to absolute mode: %d\n", error);
270 error = data->ops->sleep_control(client, false);
272 dev_err(&client->dev,
273 "failed to wake device up: %d\n", error);
281 static int elan_initialize(struct elan_tp_data *data)
283 int repeat = ETP_RETRY_COUNT;
287 error = __elan_initialize(data);
292 } while (--repeat > 0);
297 static int elan_query_device_info(struct elan_tp_data *data)
301 error = data->ops->get_version(data->client, data->pattern, false,
306 error = data->ops->get_checksum(data->client, false,
311 error = data->ops->get_version(data->client, data->pattern,
312 true, &data->iap_version);
316 error = data->ops->get_pressure_adjustment(data->client,
317 &data->pressure_adjustment);
321 error = data->ops->get_report_features(data->client, data->pattern,
322 &data->report_features,
327 error = elan_get_fwinfo(data->ic_type, data->iap_version,
328 &data->fw_validpage_count,
329 &data->fw_signature_address,
330 &data->fw_page_size);
332 dev_warn(&data->client->dev,
333 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
334 data->iap_version, data->ic_type);
339 static unsigned int elan_convert_resolution(u8 val, u8 pattern)
343 * (value from firmware) * 10 + 790 = dpi
345 * ((value from firmware) + 3) * 100 = dpi
347 int res = pattern <= 0x01 ?
348 (int)(char)val * 10 + 790 : ((int)(char)val + 3) * 100;
350 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
353 return res * 10 / 254;
356 static int elan_query_device_parameters(struct elan_tp_data *data)
358 struct i2c_client *client = data->client;
359 unsigned int x_traces, y_traces;
361 u8 hw_x_res, hw_y_res;
364 if (device_property_read_u32(&client->dev,
365 "touchscreen-size-x", &data->max_x) ||
366 device_property_read_u32(&client->dev,
367 "touchscreen-size-y", &data->max_y)) {
368 error = data->ops->get_max(data->client,
374 /* size is the maximum + 1 */
379 if (device_property_read_u32(&client->dev,
382 device_property_read_u32(&client->dev,
385 error = data->ops->get_num_traces(data->client,
386 &x_traces, &y_traces);
390 data->width_x = data->max_x / x_traces;
391 data->width_y = data->max_y / y_traces;
393 if (device_property_read_u32(&client->dev,
394 "touchscreen-x-mm", &x_mm) ||
395 device_property_read_u32(&client->dev,
396 "touchscreen-y-mm", &y_mm)) {
397 error = data->ops->get_resolution(data->client,
398 &hw_x_res, &hw_y_res);
402 data->x_res = elan_convert_resolution(hw_x_res, data->pattern);
403 data->y_res = elan_convert_resolution(hw_y_res, data->pattern);
405 data->x_res = (data->max_x + 1) / x_mm;
406 data->y_res = (data->max_y + 1) / y_mm;
409 if (device_property_read_bool(&client->dev, "elan,clickpad"))
412 if (device_property_read_bool(&client->dev, "elan,middle-button"))
413 data->middle_button = true;
419 **********************************************************
420 * IAP firmware updater related routines
421 **********************************************************
423 static int elan_write_fw_block(struct elan_tp_data *data, u16 page_size,
424 const u8 *page, u16 checksum, int idx)
426 int retry = ETP_RETRY_COUNT;
430 error = data->ops->write_fw_block(data->client, page_size,
431 page, checksum, idx);
435 dev_dbg(&data->client->dev,
436 "IAP retrying page %d (error: %d)\n", idx, error);
437 } while (--retry > 0);
442 static int __elan_update_firmware(struct elan_tp_data *data,
443 const struct firmware *fw)
445 struct i2c_client *client = data->client;
446 struct device *dev = &client->dev;
451 u16 sw_checksum = 0, fw_checksum = 0;
453 error = data->ops->prepare_fw_update(client, data->ic_type,
459 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
461 boot_page_count = (iap_start_addr * 2) / data->fw_page_size;
462 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
464 const u8 *page = &fw->data[i * data->fw_page_size];
466 for (j = 0; j < data->fw_page_size; j += 2)
467 checksum += ((page[j + 1] << 8) | page[j]);
469 error = elan_write_fw_block(data, data->fw_page_size,
472 dev_err(dev, "write page %d fail: %d\n", i, error);
476 sw_checksum += checksum;
479 /* Wait WDT reset and power on reset */
482 error = data->ops->finish_fw_update(client, &data->fw_completion);
486 error = data->ops->get_checksum(client, true, &fw_checksum);
490 if (sw_checksum != fw_checksum) {
491 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
492 sw_checksum, fw_checksum);
499 static int elan_update_firmware(struct elan_tp_data *data,
500 const struct firmware *fw)
502 struct i2c_client *client = data->client;
505 dev_dbg(&client->dev, "Starting firmware update....\n");
507 disable_irq(client->irq);
508 data->in_fw_update = true;
510 retval = __elan_update_firmware(data, fw);
512 dev_err(&client->dev, "firmware update failed: %d\n", retval);
513 data->ops->iap_reset(client);
515 /* Reinitialize TP after fw is updated */
516 elan_initialize(data);
517 elan_query_device_info(data);
520 data->in_fw_update = false;
521 enable_irq(client->irq);
527 *******************************************************************
529 *******************************************************************
531 static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
532 struct device_attribute *attr,
535 struct i2c_client *client = to_i2c_client(dev);
536 struct elan_tp_data *data = i2c_get_clientdata(client);
538 return sprintf(buf, "0x%04x\n", data->fw_checksum);
541 static ssize_t elan_sysfs_read_product_id(struct device *dev,
542 struct device_attribute *attr,
545 struct i2c_client *client = to_i2c_client(dev);
546 struct elan_tp_data *data = i2c_get_clientdata(client);
548 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
552 static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
553 struct device_attribute *attr,
556 struct i2c_client *client = to_i2c_client(dev);
557 struct elan_tp_data *data = i2c_get_clientdata(client);
559 return sprintf(buf, "%d.0\n", data->fw_version);
562 static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
563 struct device_attribute *attr,
566 struct i2c_client *client = to_i2c_client(dev);
567 struct elan_tp_data *data = i2c_get_clientdata(client);
569 return sprintf(buf, "%d.0\n", data->sm_version);
572 static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
573 struct device_attribute *attr,
576 struct i2c_client *client = to_i2c_client(dev);
577 struct elan_tp_data *data = i2c_get_clientdata(client);
579 return sprintf(buf, "%d.0\n", data->iap_version);
582 static ssize_t elan_sysfs_update_fw(struct device *dev,
583 struct device_attribute *attr,
584 const char *buf, size_t count)
586 struct elan_tp_data *data = dev_get_drvdata(dev);
587 const struct firmware *fw;
590 const u8 *fw_signature;
591 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
593 if (data->fw_validpage_count == 0)
596 /* Look for a firmware with the product id appended. */
597 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
599 dev_err(dev, "failed to allocate memory for firmware name\n");
603 dev_info(dev, "requesting fw '%s'\n", fw_name);
604 error = reject_firmware(&fw, fw_name, dev);
607 dev_err(dev, "failed to request firmware: %d\n", error);
611 /* Firmware file must match signature data */
612 fw_signature = &fw->data[data->fw_signature_address];
613 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
614 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
615 (int)sizeof(signature), signature,
616 (int)sizeof(signature), fw_signature);
621 error = mutex_lock_interruptible(&data->sysfs_mutex);
625 error = elan_update_firmware(data, fw);
627 mutex_unlock(&data->sysfs_mutex);
630 release_firmware(fw);
631 return error ?: count;
634 static ssize_t calibrate_store(struct device *dev,
635 struct device_attribute *attr,
636 const char *buf, size_t count)
638 struct i2c_client *client = to_i2c_client(dev);
639 struct elan_tp_data *data = i2c_get_clientdata(client);
643 u8 val[ETP_CALIBRATE_MAX_LEN];
645 retval = mutex_lock_interruptible(&data->sysfs_mutex);
649 disable_irq(client->irq);
651 data->mode |= ETP_ENABLE_CALIBRATE;
652 retval = data->ops->set_mode(client, data->mode);
654 dev_err(dev, "failed to enable calibration mode: %d\n",
659 retval = data->ops->calibrate(client);
661 dev_err(dev, "failed to start calibration: %d\n",
663 goto out_disable_calibrate;
668 /* Wait 250ms before checking if calibration has completed. */
671 retval = data->ops->calibrate_result(client, val);
673 dev_err(dev, "failed to check calibration result: %d\n",
675 else if (val[0] == 0)
676 break; /* calibration done */
681 dev_err(dev, "failed to calibrate. Timeout.\n");
685 out_disable_calibrate:
686 data->mode &= ~ETP_ENABLE_CALIBRATE;
687 error = data->ops->set_mode(data->client, data->mode);
689 dev_err(dev, "failed to disable calibration mode: %d\n",
695 enable_irq(client->irq);
696 mutex_unlock(&data->sysfs_mutex);
697 return retval ?: count;
700 static ssize_t elan_sysfs_read_mode(struct device *dev,
701 struct device_attribute *attr,
704 struct i2c_client *client = to_i2c_client(dev);
705 struct elan_tp_data *data = i2c_get_clientdata(client);
709 error = mutex_lock_interruptible(&data->sysfs_mutex);
713 error = data->ops->iap_get_mode(data->client, &mode);
715 mutex_unlock(&data->sysfs_mutex);
720 return sprintf(buf, "%d\n", (int)mode);
723 static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
724 static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
725 static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
726 static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
727 static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
728 static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
729 static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
731 static DEVICE_ATTR_WO(calibrate);
733 static struct attribute *elan_sysfs_entries[] = {
734 &dev_attr_product_id.attr,
735 &dev_attr_firmware_version.attr,
736 &dev_attr_sample_version.attr,
737 &dev_attr_iap_version.attr,
738 &dev_attr_fw_checksum.attr,
739 &dev_attr_calibrate.attr,
741 &dev_attr_update_fw.attr,
745 static const struct attribute_group elan_sysfs_group = {
746 .attrs = elan_sysfs_entries,
749 static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
752 struct i2c_client *client = to_i2c_client(dev);
753 struct elan_tp_data *data = i2c_get_clientdata(client);
757 retval = mutex_lock_interruptible(&data->sysfs_mutex);
761 disable_irq(client->irq);
763 data->baseline_ready = false;
765 data->mode |= ETP_ENABLE_CALIBRATE;
766 retval = data->ops->set_mode(data->client, data->mode);
768 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
775 retval = data->ops->get_baseline_data(data->client, true,
776 &data->max_baseline);
778 dev_err(dev, "Failed to read max baseline form device: %d\n",
780 goto out_disable_calibrate;
783 retval = data->ops->get_baseline_data(data->client, false,
784 &data->min_baseline);
786 dev_err(dev, "Failed to read min baseline form device: %d\n",
788 goto out_disable_calibrate;
791 data->baseline_ready = true;
793 out_disable_calibrate:
794 data->mode &= ~ETP_ENABLE_CALIBRATE;
795 error = data->ops->set_mode(data->client, data->mode);
797 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
803 enable_irq(client->irq);
804 mutex_unlock(&data->sysfs_mutex);
805 return retval ?: count;
808 static ssize_t min_show(struct device *dev,
809 struct device_attribute *attr, char *buf)
811 struct i2c_client *client = to_i2c_client(dev);
812 struct elan_tp_data *data = i2c_get_clientdata(client);
815 retval = mutex_lock_interruptible(&data->sysfs_mutex);
819 if (!data->baseline_ready) {
824 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
827 mutex_unlock(&data->sysfs_mutex);
831 static ssize_t max_show(struct device *dev,
832 struct device_attribute *attr, char *buf)
834 struct i2c_client *client = to_i2c_client(dev);
835 struct elan_tp_data *data = i2c_get_clientdata(client);
838 retval = mutex_lock_interruptible(&data->sysfs_mutex);
842 if (!data->baseline_ready) {
847 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
850 mutex_unlock(&data->sysfs_mutex);
855 static DEVICE_ATTR_WO(acquire);
856 static DEVICE_ATTR_RO(min);
857 static DEVICE_ATTR_RO(max);
859 static struct attribute *elan_baseline_sysfs_entries[] = {
860 &dev_attr_acquire.attr,
866 static const struct attribute_group elan_baseline_sysfs_group = {
868 .attrs = elan_baseline_sysfs_entries,
871 static const struct attribute_group *elan_sysfs_groups[] = {
873 &elan_baseline_sysfs_group,
878 ******************************************************************
880 ******************************************************************
882 static void elan_report_contact(struct elan_tp_data *data, int contact_num,
883 bool contact_valid, bool high_precision,
884 u8 *packet, u8 *finger_data)
886 struct input_dev *input = data->input;
887 unsigned int pos_x, pos_y;
888 unsigned int pressure, scaled_pressure;
891 if (high_precision) {
892 pos_x = get_unaligned_be16(&finger_data[0]);
893 pos_y = get_unaligned_be16(&finger_data[2]);
895 pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1];
896 pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2];
899 if (pos_x > data->max_x || pos_y > data->max_y) {
900 dev_dbg(input->dev.parent,
901 "[%d] x=%d y=%d over max (%d, %d)",
902 contact_num, pos_x, pos_y,
903 data->max_x, data->max_y);
907 pressure = finger_data[4];
908 scaled_pressure = pressure + data->pressure_adjustment;
909 if (scaled_pressure > ETP_MAX_PRESSURE)
910 scaled_pressure = ETP_MAX_PRESSURE;
912 input_mt_slot(input, contact_num);
913 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
914 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
915 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
916 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
918 if (data->report_features & ETP_FEATURE_REPORT_MK) {
919 unsigned int mk_x, mk_y, area_x, area_y;
920 u8 mk_data = high_precision ?
921 packet[ETP_MK_DATA_OFFSET + contact_num] :
924 mk_x = mk_data & 0x0f;
928 * To avoid treating large finger as palm, let's reduce
929 * the width x and y per trace.
931 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
932 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
934 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
935 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
936 max(area_x, area_y));
937 input_report_abs(input, ABS_MT_TOUCH_MINOR,
938 min(area_x, area_y));
941 input_mt_slot(input, contact_num);
942 input_mt_report_slot_inactive(input);
946 static void elan_report_absolute(struct elan_tp_data *data, u8 *packet,
949 struct input_dev *input = data->input;
950 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
952 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
953 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
954 bool contact_valid, hover_event;
956 pm_wakeup_event(&data->client->dev, 0);
958 hover_event = hover_info & BIT(6);
960 for (i = 0; i < ETP_MAX_FINGERS; i++) {
961 contact_valid = tp_info & BIT(3 + i);
962 elan_report_contact(data, i, contact_valid, high_precision,
963 packet, finger_data);
965 finger_data += ETP_FINGER_DATA_LEN;
968 input_report_key(input, BTN_LEFT, tp_info & BIT(0));
969 input_report_key(input, BTN_MIDDLE, tp_info & BIT(2));
970 input_report_key(input, BTN_RIGHT, tp_info & BIT(1));
971 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
972 input_mt_report_pointer_emulation(input, true);
976 static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report)
978 struct input_dev *input = data->tp_input;
979 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1];
982 pm_wakeup_event(&data->client->dev, 0);
984 if (!data->tp_input) {
985 dev_warn_once(&data->client->dev,
986 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
990 input_report_key(input, BTN_LEFT, packet[0] & 0x01);
991 input_report_key(input, BTN_RIGHT, packet[0] & 0x02);
992 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04);
994 if ((packet[3] & 0x0F) == 0x06) {
995 x = packet[4] - (int)((packet[1] ^ 0x80) << 1);
996 y = (int)((packet[2] ^ 0x80) << 1) - packet[5];
998 input_report_rel(input, REL_X, x);
999 input_report_rel(input, REL_Y, y);
1005 static irqreturn_t elan_isr(int irq, void *dev_id)
1007 struct elan_tp_data *data = dev_id;
1009 u8 report[ETP_MAX_REPORT_LEN];
1012 * When device is connected to i2c bus, when all IAP page writes
1013 * complete, the driver will receive interrupt and must read
1014 * 0000 to confirm that IAP is finished.
1016 if (data->in_fw_update) {
1017 complete(&data->fw_completion);
1021 error = data->ops->get_report(data->client, report, data->report_len);
1025 switch (report[ETP_REPORT_ID_OFFSET]) {
1027 elan_report_absolute(data, report, false);
1029 case ETP_REPORT_ID2:
1030 elan_report_absolute(data, report, true);
1032 case ETP_TP_REPORT_ID:
1033 case ETP_TP_REPORT_ID2:
1034 elan_report_trackpoint(data, report);
1037 dev_err(&data->client->dev, "invalid report id data (%x)\n",
1038 report[ETP_REPORT_ID_OFFSET]);
1046 ******************************************************************
1047 * Elan initialization functions
1048 ******************************************************************
1051 static int elan_setup_trackpoint_input_device(struct elan_tp_data *data)
1053 struct device *dev = &data->client->dev;
1054 struct input_dev *input;
1056 input = devm_input_allocate_device(dev);
1060 input->name = "Elan TrackPoint";
1061 input->id.bustype = BUS_I2C;
1062 input->id.vendor = ELAN_VENDOR_ID;
1063 input->id.product = data->product_id;
1064 input_set_drvdata(input, data);
1066 input_set_capability(input, EV_REL, REL_X);
1067 input_set_capability(input, EV_REL, REL_Y);
1068 input_set_capability(input, EV_KEY, BTN_LEFT);
1069 input_set_capability(input, EV_KEY, BTN_RIGHT);
1070 input_set_capability(input, EV_KEY, BTN_MIDDLE);
1072 __set_bit(INPUT_PROP_POINTER, input->propbit);
1073 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit);
1075 data->tp_input = input;
1080 static int elan_setup_input_device(struct elan_tp_data *data)
1082 struct device *dev = &data->client->dev;
1083 struct input_dev *input;
1084 unsigned int max_width = max(data->width_x, data->width_y);
1085 unsigned int min_width = min(data->width_x, data->width_y);
1088 input = devm_input_allocate_device(dev);
1092 input->name = "Elan Touchpad";
1093 input->id.bustype = BUS_I2C;
1094 input->id.vendor = ELAN_VENDOR_ID;
1095 input->id.product = data->product_id;
1096 input_set_drvdata(input, data);
1098 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
1099 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
1101 dev_err(dev, "failed to initialize MT slots: %d\n", error);
1105 __set_bit(EV_ABS, input->evbit);
1106 __set_bit(INPUT_PROP_POINTER, input->propbit);
1107 if (data->clickpad) {
1108 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1110 __set_bit(BTN_RIGHT, input->keybit);
1111 if (data->middle_button)
1112 __set_bit(BTN_MIDDLE, input->keybit);
1114 __set_bit(BTN_LEFT, input->keybit);
1116 /* Set up ST parameters */
1117 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
1118 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
1119 input_abs_set_res(input, ABS_X, data->x_res);
1120 input_abs_set_res(input, ABS_Y, data->y_res);
1121 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
1122 if (data->report_features & ETP_FEATURE_REPORT_MK)
1123 input_set_abs_params(input, ABS_TOOL_WIDTH,
1124 0, ETP_FINGER_WIDTH, 0, 0);
1125 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
1127 /* And MT parameters */
1128 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
1129 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
1130 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
1131 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
1132 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
1133 ETP_MAX_PRESSURE, 0, 0);
1134 if (data->report_features & ETP_FEATURE_REPORT_MK) {
1135 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1136 0, ETP_FINGER_WIDTH * max_width, 0, 0);
1137 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1138 0, ETP_FINGER_WIDTH * min_width, 0, 0);
1141 data->input = input;
1146 static void elan_disable_regulator(void *_data)
1148 struct elan_tp_data *data = _data;
1150 regulator_disable(data->vcc);
1153 static int elan_probe(struct i2c_client *client,
1154 const struct i2c_device_id *dev_id)
1156 const struct elan_transport_ops *transport_ops;
1157 struct device *dev = &client->dev;
1158 struct elan_tp_data *data;
1159 unsigned long irqflags;
1162 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1163 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1164 transport_ops = &elan_i2c_ops;
1165 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1166 i2c_check_functionality(client->adapter,
1167 I2C_FUNC_SMBUS_BYTE_DATA |
1168 I2C_FUNC_SMBUS_BLOCK_DATA |
1169 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1170 transport_ops = &elan_smbus_ops;
1172 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1176 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1180 i2c_set_clientdata(client, data);
1182 data->ops = transport_ops;
1183 data->client = client;
1184 init_completion(&data->fw_completion);
1185 mutex_init(&data->sysfs_mutex);
1187 data->vcc = devm_regulator_get(dev, "vcc");
1188 if (IS_ERR(data->vcc)) {
1189 error = PTR_ERR(data->vcc);
1190 if (error != -EPROBE_DEFER)
1191 dev_err(dev, "Failed to get 'vcc' regulator: %d\n",
1196 error = regulator_enable(data->vcc);
1198 dev_err(dev, "Failed to enable regulator: %d\n", error);
1202 error = devm_add_action_or_reset(dev, elan_disable_regulator, data);
1204 dev_err(dev, "Failed to add disable regulator action: %d\n",
1209 /* Make sure there is something at this address */
1210 error = i2c_smbus_read_byte(client);
1212 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1216 /* Initialize the touchpad. */
1217 error = elan_initialize(data);
1221 error = elan_query_device_info(data);
1225 error = elan_query_device_parameters(data);
1230 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1237 "Elan Touchpad Extra Information:\n"
1238 " Max ABS X,Y: %d,%d\n"
1239 " Width X,Y: %d,%d\n"
1240 " Resolution X,Y: %d,%d (dots/mm)\n"
1242 " info pattern: 0x%x\n",
1243 data->max_x, data->max_y,
1244 data->width_x, data->width_y,
1245 data->x_res, data->y_res,
1246 data->ic_type, data->pattern);
1248 /* Set up input device properties based on queried parameters. */
1249 error = elan_setup_input_device(data);
1253 if (device_property_read_bool(&client->dev, "elan,trackpoint")) {
1254 error = elan_setup_trackpoint_input_device(data);
1260 * Platform code (ACPI, DTS) should normally set up interrupt
1261 * for us, but in case it did not let's fall back to using falling
1262 * edge to be compatible with older Chromebooks.
1264 irqflags = irq_get_trigger_type(client->irq);
1266 irqflags = IRQF_TRIGGER_FALLING;
1268 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1269 irqflags | IRQF_ONESHOT,
1270 client->name, data);
1272 dev_err(dev, "cannot register irq=%d\n", client->irq);
1276 error = devm_device_add_groups(dev, elan_sysfs_groups);
1278 dev_err(dev, "failed to create sysfs attributes: %d\n", error);
1282 error = input_register_device(data->input);
1284 dev_err(dev, "failed to register input device: %d\n", error);
1288 if (data->tp_input) {
1289 error = input_register_device(data->tp_input);
1291 dev_err(&client->dev,
1292 "failed to register TrackPoint input device: %d\n",
1299 * Systems using device tree should set up wakeup via DTS,
1300 * the rest will configure device as wakeup source by default.
1303 device_init_wakeup(dev, true);
1308 static int __maybe_unused elan_suspend(struct device *dev)
1310 struct i2c_client *client = to_i2c_client(dev);
1311 struct elan_tp_data *data = i2c_get_clientdata(client);
1315 * We are taking the mutex to make sure sysfs operations are
1316 * complete before we attempt to bring the device into low[er]
1319 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1323 disable_irq(client->irq);
1325 if (device_may_wakeup(dev)) {
1326 ret = elan_sleep(data);
1327 /* Enable wake from IRQ */
1328 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1330 ret = elan_set_power(data, false);
1334 ret = regulator_disable(data->vcc);
1336 dev_err(dev, "error %d disabling regulator\n", ret);
1337 /* Attempt to power the chip back up */
1338 elan_set_power(data, true);
1343 mutex_unlock(&data->sysfs_mutex);
1347 static int __maybe_unused elan_resume(struct device *dev)
1349 struct i2c_client *client = to_i2c_client(dev);
1350 struct elan_tp_data *data = i2c_get_clientdata(client);
1353 if (!device_may_wakeup(dev)) {
1354 error = regulator_enable(data->vcc);
1356 dev_err(dev, "error %d enabling regulator\n", error);
1359 } else if (data->irq_wake) {
1360 disable_irq_wake(client->irq);
1361 data->irq_wake = false;
1364 error = elan_set_power(data, true);
1366 dev_err(dev, "power up when resuming failed: %d\n", error);
1370 error = elan_initialize(data);
1372 dev_err(dev, "initialize when resuming failed: %d\n", error);
1375 enable_irq(data->client->irq);
1379 static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1381 static const struct i2c_device_id elan_id[] = {
1385 MODULE_DEVICE_TABLE(i2c, elan_id);
1388 MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1392 static const struct of_device_id elan_of_match[] = {
1393 { .compatible = "elan,ekth3000" },
1396 MODULE_DEVICE_TABLE(of, elan_of_match);
1399 static struct i2c_driver elan_driver = {
1401 .name = DRIVER_NAME,
1403 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1404 .of_match_table = of_match_ptr(elan_of_match),
1405 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1407 .probe = elan_probe,
1408 .id_table = elan_id,
1411 module_i2c_driver(elan_driver);
1413 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1414 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1415 MODULE_LICENSE("GPL");