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/regulator/consumer.h>
38 #include <asm/unaligned.h>
42 #define DRIVER_NAME "elan_i2c"
43 #define ELAN_VENDOR_ID 0x04f3
44 #define ETP_MAX_PRESSURE 255
45 #define ETP_FWIDTH_REDUCE 90
46 #define ETP_FINGER_WIDTH 15
47 #define ETP_RETRY_COUNT 3
49 /* The main device structure */
51 struct i2c_client
*client
;
52 struct input_dev
*input
;
53 struct input_dev
*tp_input
; /* trackpoint input node */
54 struct regulator
*vcc
;
56 const struct elan_transport_ops
*ops
;
59 struct completion fw_completion
;
62 struct mutex sysfs_mutex
;
77 unsigned int report_features
;
78 unsigned int report_len
;
79 int pressure_adjustment
;
82 u16 fw_validpage_count
;
84 u32 fw_signature_address
;
95 static int elan_get_fwinfo(u16 ic_type
, u8 iap_version
, u16
*validpage_count
,
96 u32
*signature_address
, u16
*page_size
)
102 *validpage_count
= 512;
110 *validpage_count
= 768;
113 *validpage_count
= 896;
116 *validpage_count
= 640;
119 *validpage_count
= 1024;
122 *validpage_count
= 1280;
125 *validpage_count
= 2048;
129 *validpage_count
= 1024;
132 /* unknown ic type clear value */
133 *validpage_count
= 0;
134 *signature_address
= 0;
140 (*validpage_count
* ETP_FW_PAGE_SIZE
) - ETP_FW_SIGNATURE_SIZE
;
142 if ((ic_type
== 0x14 || ic_type
== 0x15) && iap_version
>= 2) {
143 *validpage_count
/= 8;
144 *page_size
= ETP_FW_PAGE_SIZE_512
;
145 } else if (ic_type
>= 0x0D && iap_version
>= 1) {
146 *validpage_count
/= 2;
147 *page_size
= ETP_FW_PAGE_SIZE_128
;
149 *page_size
= ETP_FW_PAGE_SIZE
;
155 static int elan_enable_power(struct elan_tp_data
*data
)
157 int repeat
= ETP_RETRY_COUNT
;
160 error
= regulator_enable(data
->vcc
);
162 dev_err(&data
->client
->dev
,
163 "failed to enable regulator: %d\n", error
);
168 error
= data
->ops
->power_control(data
->client
, true);
173 } while (--repeat
> 0);
175 dev_err(&data
->client
->dev
, "failed to enable power: %d\n", error
);
179 static int elan_disable_power(struct elan_tp_data
*data
)
181 int repeat
= ETP_RETRY_COUNT
;
185 error
= data
->ops
->power_control(data
->client
, false);
187 error
= regulator_disable(data
->vcc
);
189 dev_err(&data
->client
->dev
,
190 "failed to disable regulator: %d\n",
192 /* Attempt to power the chip back up */
193 data
->ops
->power_control(data
->client
, true);
201 } while (--repeat
> 0);
203 dev_err(&data
->client
->dev
, "failed to disable power: %d\n", error
);
207 static int elan_sleep(struct elan_tp_data
*data
)
209 int repeat
= ETP_RETRY_COUNT
;
213 error
= data
->ops
->sleep_control(data
->client
, true);
218 } while (--repeat
> 0);
223 static int elan_query_product(struct elan_tp_data
*data
)
227 error
= data
->ops
->get_product_id(data
->client
, &data
->product_id
);
231 error
= data
->ops
->get_pattern(data
->client
, &data
->pattern
);
235 error
= data
->ops
->get_sm_version(data
->client
, data
->pattern
,
236 &data
->ic_type
, &data
->sm_version
,
244 static int elan_check_ASUS_special_fw(struct elan_tp_data
*data
)
246 if (data
->ic_type
== 0x0E) {
247 switch (data
->product_id
) {
253 } else if (data
->ic_type
== 0x08 && data
->product_id
== 0x26) {
254 /* ASUS EeeBook X205TA */
261 static int __elan_initialize(struct elan_tp_data
*data
)
263 struct i2c_client
*client
= data
->client
;
264 bool woken_up
= false;
267 error
= data
->ops
->initialize(client
);
269 dev_err(&client
->dev
, "device initialize failed: %d\n", error
);
273 error
= elan_query_product(data
);
278 * Some ASUS devices were shipped with firmware that requires
279 * touchpads to be woken up first, before attempting to switch
280 * them into absolute reporting mode.
282 if (elan_check_ASUS_special_fw(data
)) {
283 error
= data
->ops
->sleep_control(client
, false);
285 dev_err(&client
->dev
,
286 "failed to wake device up: %d\n", error
);
294 data
->mode
|= ETP_ENABLE_ABS
;
295 error
= data
->ops
->set_mode(client
, data
->mode
);
297 dev_err(&client
->dev
,
298 "failed to switch to absolute mode: %d\n", error
);
303 error
= data
->ops
->sleep_control(client
, false);
305 dev_err(&client
->dev
,
306 "failed to wake device up: %d\n", error
);
314 static int elan_initialize(struct elan_tp_data
*data
)
316 int repeat
= ETP_RETRY_COUNT
;
320 error
= __elan_initialize(data
);
325 } while (--repeat
> 0);
330 static int elan_query_device_info(struct elan_tp_data
*data
)
334 error
= data
->ops
->get_version(data
->client
, data
->pattern
, false,
339 error
= data
->ops
->get_checksum(data
->client
, false,
344 error
= data
->ops
->get_version(data
->client
, data
->pattern
,
345 true, &data
->iap_version
);
349 error
= data
->ops
->get_pressure_adjustment(data
->client
,
350 &data
->pressure_adjustment
);
354 error
= data
->ops
->get_report_features(data
->client
, data
->pattern
,
355 &data
->report_features
,
360 error
= elan_get_fwinfo(data
->ic_type
, data
->iap_version
,
361 &data
->fw_validpage_count
,
362 &data
->fw_signature_address
,
363 &data
->fw_page_size
);
365 dev_warn(&data
->client
->dev
,
366 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
367 data
->iap_version
, data
->ic_type
);
372 static unsigned int elan_convert_resolution(u8 val
, u8 pattern
)
376 * (value from firmware) * 10 + 790 = dpi
378 * ((value from firmware) + 3) * 100 = dpi
380 int res
= pattern
<= 0x01 ?
381 (int)(char)val
* 10 + 790 : ((int)(char)val
+ 3) * 100;
383 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
386 return res
* 10 / 254;
389 static int elan_query_device_parameters(struct elan_tp_data
*data
)
391 struct i2c_client
*client
= data
->client
;
392 unsigned int x_traces
, y_traces
;
394 u8 hw_x_res
, hw_y_res
;
397 if (device_property_read_u32(&client
->dev
,
398 "touchscreen-size-x", &data
->max_x
) ||
399 device_property_read_u32(&client
->dev
,
400 "touchscreen-size-y", &data
->max_y
)) {
401 error
= data
->ops
->get_max(data
->client
,
407 /* size is the maximum + 1 */
412 if (device_property_read_u32(&client
->dev
,
415 device_property_read_u32(&client
->dev
,
418 error
= data
->ops
->get_num_traces(data
->client
,
419 &x_traces
, &y_traces
);
423 data
->width_x
= data
->max_x
/ x_traces
;
424 data
->width_y
= data
->max_y
/ y_traces
;
426 if (device_property_read_u32(&client
->dev
,
427 "touchscreen-x-mm", &x_mm
) ||
428 device_property_read_u32(&client
->dev
,
429 "touchscreen-y-mm", &y_mm
)) {
430 error
= data
->ops
->get_resolution(data
->client
,
431 &hw_x_res
, &hw_y_res
);
435 data
->x_res
= elan_convert_resolution(hw_x_res
, data
->pattern
);
436 data
->y_res
= elan_convert_resolution(hw_y_res
, data
->pattern
);
438 data
->x_res
= (data
->max_x
+ 1) / x_mm
;
439 data
->y_res
= (data
->max_y
+ 1) / y_mm
;
442 if (device_property_read_bool(&client
->dev
, "elan,clickpad"))
445 if (device_property_read_bool(&client
->dev
, "elan,middle-button"))
446 data
->middle_button
= true;
452 **********************************************************
453 * IAP firmware updater related routines
454 **********************************************************
456 static int elan_write_fw_block(struct elan_tp_data
*data
, u16 page_size
,
457 const u8
*page
, u16 checksum
, int idx
)
459 int retry
= ETP_RETRY_COUNT
;
463 error
= data
->ops
->write_fw_block(data
->client
, page_size
,
464 page
, checksum
, idx
);
468 dev_dbg(&data
->client
->dev
,
469 "IAP retrying page %d (error: %d)\n", idx
, error
);
470 } while (--retry
> 0);
475 static int __elan_update_firmware(struct elan_tp_data
*data
,
476 const struct firmware
*fw
)
478 struct i2c_client
*client
= data
->client
;
479 struct device
*dev
= &client
->dev
;
484 u16 sw_checksum
= 0, fw_checksum
= 0;
486 error
= data
->ops
->prepare_fw_update(client
, data
->ic_type
,
492 iap_start_addr
= get_unaligned_le16(&fw
->data
[ETP_IAP_START_ADDR
* 2]);
494 boot_page_count
= (iap_start_addr
* 2) / data
->fw_page_size
;
495 for (i
= boot_page_count
; i
< data
->fw_validpage_count
; i
++) {
497 const u8
*page
= &fw
->data
[i
* data
->fw_page_size
];
499 for (j
= 0; j
< data
->fw_page_size
; j
+= 2)
500 checksum
+= ((page
[j
+ 1] << 8) | page
[j
]);
502 error
= elan_write_fw_block(data
, data
->fw_page_size
,
505 dev_err(dev
, "write page %d fail: %d\n", i
, error
);
509 sw_checksum
+= checksum
;
512 /* Wait WDT reset and power on reset */
515 error
= data
->ops
->finish_fw_update(client
, &data
->fw_completion
);
519 error
= data
->ops
->get_checksum(client
, true, &fw_checksum
);
523 if (sw_checksum
!= fw_checksum
) {
524 dev_err(dev
, "checksum diff sw=[%04X], fw=[%04X]\n",
525 sw_checksum
, fw_checksum
);
532 static int elan_update_firmware(struct elan_tp_data
*data
,
533 const struct firmware
*fw
)
535 struct i2c_client
*client
= data
->client
;
538 dev_dbg(&client
->dev
, "Starting firmware update....\n");
540 disable_irq(client
->irq
);
541 data
->in_fw_update
= true;
543 retval
= __elan_update_firmware(data
, fw
);
545 dev_err(&client
->dev
, "firmware update failed: %d\n", retval
);
546 data
->ops
->iap_reset(client
);
548 /* Reinitialize TP after fw is updated */
549 elan_initialize(data
);
550 elan_query_device_info(data
);
553 data
->in_fw_update
= false;
554 enable_irq(client
->irq
);
560 *******************************************************************
562 *******************************************************************
564 static ssize_t
elan_sysfs_read_fw_checksum(struct device
*dev
,
565 struct device_attribute
*attr
,
568 struct i2c_client
*client
= to_i2c_client(dev
);
569 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
571 return sprintf(buf
, "0x%04x\n", data
->fw_checksum
);
574 static ssize_t
elan_sysfs_read_product_id(struct device
*dev
,
575 struct device_attribute
*attr
,
578 struct i2c_client
*client
= to_i2c_client(dev
);
579 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
581 return sprintf(buf
, ETP_PRODUCT_ID_FORMAT_STRING
"\n",
585 static ssize_t
elan_sysfs_read_fw_ver(struct device
*dev
,
586 struct device_attribute
*attr
,
589 struct i2c_client
*client
= to_i2c_client(dev
);
590 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
592 return sprintf(buf
, "%d.0\n", data
->fw_version
);
595 static ssize_t
elan_sysfs_read_sm_ver(struct device
*dev
,
596 struct device_attribute
*attr
,
599 struct i2c_client
*client
= to_i2c_client(dev
);
600 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
602 return sprintf(buf
, "%d.0\n", data
->sm_version
);
605 static ssize_t
elan_sysfs_read_iap_ver(struct device
*dev
,
606 struct device_attribute
*attr
,
609 struct i2c_client
*client
= to_i2c_client(dev
);
610 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
612 return sprintf(buf
, "%d.0\n", data
->iap_version
);
615 static ssize_t
elan_sysfs_update_fw(struct device
*dev
,
616 struct device_attribute
*attr
,
617 const char *buf
, size_t count
)
619 struct elan_tp_data
*data
= dev_get_drvdata(dev
);
620 const struct firmware
*fw
;
623 const u8
*fw_signature
;
624 static const u8 signature
[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
626 if (data
->fw_validpage_count
== 0)
629 /* Look for a firmware with the product id appended. */
630 fw_name
= kasprintf(GFP_KERNEL
, ETP_FW_NAME
, data
->product_id
);
632 dev_err(dev
, "failed to allocate memory for firmware name\n");
636 dev_info(dev
, "requesting fw '%s'\n", fw_name
);
637 error
= request_firmware(&fw
, fw_name
, dev
);
640 dev_err(dev
, "failed to request firmware: %d\n", error
);
644 /* Firmware file must match signature data */
645 fw_signature
= &fw
->data
[data
->fw_signature_address
];
646 if (memcmp(fw_signature
, signature
, sizeof(signature
)) != 0) {
647 dev_err(dev
, "signature mismatch (expected %*ph, got %*ph)\n",
648 (int)sizeof(signature
), signature
,
649 (int)sizeof(signature
), fw_signature
);
654 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
658 error
= elan_update_firmware(data
, fw
);
660 mutex_unlock(&data
->sysfs_mutex
);
663 release_firmware(fw
);
664 return error
?: count
;
667 static ssize_t
calibrate_store(struct device
*dev
,
668 struct device_attribute
*attr
,
669 const char *buf
, size_t count
)
671 struct i2c_client
*client
= to_i2c_client(dev
);
672 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
676 u8 val
[ETP_CALIBRATE_MAX_LEN
];
678 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
682 disable_irq(client
->irq
);
684 data
->mode
|= ETP_ENABLE_CALIBRATE
;
685 retval
= data
->ops
->set_mode(client
, data
->mode
);
687 dev_err(dev
, "failed to enable calibration mode: %d\n",
692 retval
= data
->ops
->calibrate(client
);
694 dev_err(dev
, "failed to start calibration: %d\n",
696 goto out_disable_calibrate
;
701 /* Wait 250ms before checking if calibration has completed. */
704 retval
= data
->ops
->calibrate_result(client
, val
);
706 dev_err(dev
, "failed to check calibration result: %d\n",
708 else if (val
[0] == 0)
709 break; /* calibration done */
714 dev_err(dev
, "failed to calibrate. Timeout.\n");
718 out_disable_calibrate
:
719 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
720 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
722 dev_err(dev
, "failed to disable calibration mode: %d\n",
728 enable_irq(client
->irq
);
729 mutex_unlock(&data
->sysfs_mutex
);
730 return retval
?: count
;
733 static ssize_t
elan_sysfs_read_mode(struct device
*dev
,
734 struct device_attribute
*attr
,
737 struct i2c_client
*client
= to_i2c_client(dev
);
738 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
742 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
746 error
= data
->ops
->iap_get_mode(data
->client
, &mode
);
748 mutex_unlock(&data
->sysfs_mutex
);
753 return sprintf(buf
, "%d\n", (int)mode
);
756 static DEVICE_ATTR(product_id
, S_IRUGO
, elan_sysfs_read_product_id
, NULL
);
757 static DEVICE_ATTR(firmware_version
, S_IRUGO
, elan_sysfs_read_fw_ver
, NULL
);
758 static DEVICE_ATTR(sample_version
, S_IRUGO
, elan_sysfs_read_sm_ver
, NULL
);
759 static DEVICE_ATTR(iap_version
, S_IRUGO
, elan_sysfs_read_iap_ver
, NULL
);
760 static DEVICE_ATTR(fw_checksum
, S_IRUGO
, elan_sysfs_read_fw_checksum
, NULL
);
761 static DEVICE_ATTR(mode
, S_IRUGO
, elan_sysfs_read_mode
, NULL
);
762 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, elan_sysfs_update_fw
);
764 static DEVICE_ATTR_WO(calibrate
);
766 static struct attribute
*elan_sysfs_entries
[] = {
767 &dev_attr_product_id
.attr
,
768 &dev_attr_firmware_version
.attr
,
769 &dev_attr_sample_version
.attr
,
770 &dev_attr_iap_version
.attr
,
771 &dev_attr_fw_checksum
.attr
,
772 &dev_attr_calibrate
.attr
,
774 &dev_attr_update_fw
.attr
,
778 static const struct attribute_group elan_sysfs_group
= {
779 .attrs
= elan_sysfs_entries
,
782 static ssize_t
acquire_store(struct device
*dev
, struct device_attribute
*attr
,
783 const char *buf
, size_t count
)
785 struct i2c_client
*client
= to_i2c_client(dev
);
786 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
790 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
794 disable_irq(client
->irq
);
796 data
->baseline_ready
= false;
798 data
->mode
|= ETP_ENABLE_CALIBRATE
;
799 retval
= data
->ops
->set_mode(data
->client
, data
->mode
);
801 dev_err(dev
, "Failed to enable calibration mode to get baseline: %d\n",
808 retval
= data
->ops
->get_baseline_data(data
->client
, true,
809 &data
->max_baseline
);
811 dev_err(dev
, "Failed to read max baseline form device: %d\n",
813 goto out_disable_calibrate
;
816 retval
= data
->ops
->get_baseline_data(data
->client
, false,
817 &data
->min_baseline
);
819 dev_err(dev
, "Failed to read min baseline form device: %d\n",
821 goto out_disable_calibrate
;
824 data
->baseline_ready
= true;
826 out_disable_calibrate
:
827 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
828 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
830 dev_err(dev
, "Failed to disable calibration mode after acquiring baseline: %d\n",
836 enable_irq(client
->irq
);
837 mutex_unlock(&data
->sysfs_mutex
);
838 return retval
?: count
;
841 static ssize_t
min_show(struct device
*dev
,
842 struct device_attribute
*attr
, char *buf
)
844 struct i2c_client
*client
= to_i2c_client(dev
);
845 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
848 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
852 if (!data
->baseline_ready
) {
857 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->min_baseline
);
860 mutex_unlock(&data
->sysfs_mutex
);
864 static ssize_t
max_show(struct device
*dev
,
865 struct device_attribute
*attr
, char *buf
)
867 struct i2c_client
*client
= to_i2c_client(dev
);
868 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
871 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
875 if (!data
->baseline_ready
) {
880 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->max_baseline
);
883 mutex_unlock(&data
->sysfs_mutex
);
888 static DEVICE_ATTR_WO(acquire
);
889 static DEVICE_ATTR_RO(min
);
890 static DEVICE_ATTR_RO(max
);
892 static struct attribute
*elan_baseline_sysfs_entries
[] = {
893 &dev_attr_acquire
.attr
,
899 static const struct attribute_group elan_baseline_sysfs_group
= {
901 .attrs
= elan_baseline_sysfs_entries
,
904 static const struct attribute_group
*elan_sysfs_groups
[] = {
906 &elan_baseline_sysfs_group
,
911 ******************************************************************
913 ******************************************************************
915 static void elan_report_contact(struct elan_tp_data
*data
, int contact_num
,
916 bool contact_valid
, bool high_precision
,
917 u8
*packet
, u8
*finger_data
)
919 struct input_dev
*input
= data
->input
;
920 unsigned int pos_x
, pos_y
;
921 unsigned int pressure
, scaled_pressure
;
924 if (high_precision
) {
925 pos_x
= get_unaligned_be16(&finger_data
[0]);
926 pos_y
= get_unaligned_be16(&finger_data
[2]);
928 pos_x
= ((finger_data
[0] & 0xf0) << 4) | finger_data
[1];
929 pos_y
= ((finger_data
[0] & 0x0f) << 8) | finger_data
[2];
932 if (pos_x
> data
->max_x
|| pos_y
> data
->max_y
) {
933 dev_dbg(input
->dev
.parent
,
934 "[%d] x=%d y=%d over max (%d, %d)",
935 contact_num
, pos_x
, pos_y
,
936 data
->max_x
, data
->max_y
);
940 pressure
= finger_data
[4];
941 scaled_pressure
= pressure
+ data
->pressure_adjustment
;
942 if (scaled_pressure
> ETP_MAX_PRESSURE
)
943 scaled_pressure
= ETP_MAX_PRESSURE
;
945 input_mt_slot(input
, contact_num
);
946 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
947 input_report_abs(input
, ABS_MT_POSITION_X
, pos_x
);
948 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->max_y
- pos_y
);
949 input_report_abs(input
, ABS_MT_PRESSURE
, scaled_pressure
);
951 if (data
->report_features
& ETP_FEATURE_REPORT_MK
) {
952 unsigned int mk_x
, mk_y
, area_x
, area_y
;
953 u8 mk_data
= high_precision
?
954 packet
[ETP_MK_DATA_OFFSET
+ contact_num
] :
957 mk_x
= mk_data
& 0x0f;
961 * To avoid treating large finger as palm, let's reduce
962 * the width x and y per trace.
964 area_x
= mk_x
* (data
->width_x
- ETP_FWIDTH_REDUCE
);
965 area_y
= mk_y
* (data
->width_y
- ETP_FWIDTH_REDUCE
);
967 input_report_abs(input
, ABS_TOOL_WIDTH
, mk_x
);
968 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
,
969 max(area_x
, area_y
));
970 input_report_abs(input
, ABS_MT_TOUCH_MINOR
,
971 min(area_x
, area_y
));
974 input_mt_slot(input
, contact_num
);
975 input_mt_report_slot_inactive(input
);
979 static void elan_report_absolute(struct elan_tp_data
*data
, u8
*packet
,
982 struct input_dev
*input
= data
->input
;
983 u8
*finger_data
= &packet
[ETP_FINGER_DATA_OFFSET
];
985 u8 tp_info
= packet
[ETP_TOUCH_INFO_OFFSET
];
986 u8 hover_info
= packet
[ETP_HOVER_INFO_OFFSET
];
987 bool contact_valid
, hover_event
;
989 pm_wakeup_event(&data
->client
->dev
, 0);
991 hover_event
= hover_info
& BIT(6);
993 for (i
= 0; i
< ETP_MAX_FINGERS
; i
++) {
994 contact_valid
= tp_info
& BIT(3 + i
);
995 elan_report_contact(data
, i
, contact_valid
, high_precision
,
996 packet
, finger_data
);
998 finger_data
+= ETP_FINGER_DATA_LEN
;
1001 input_report_key(input
, BTN_LEFT
, tp_info
& BIT(0));
1002 input_report_key(input
, BTN_MIDDLE
, tp_info
& BIT(2));
1003 input_report_key(input
, BTN_RIGHT
, tp_info
& BIT(1));
1004 input_report_abs(input
, ABS_DISTANCE
, hover_event
!= 0);
1005 input_mt_report_pointer_emulation(input
, true);
1009 static void elan_report_trackpoint(struct elan_tp_data
*data
, u8
*report
)
1011 struct input_dev
*input
= data
->tp_input
;
1012 u8
*packet
= &report
[ETP_REPORT_ID_OFFSET
+ 1];
1015 pm_wakeup_event(&data
->client
->dev
, 0);
1017 if (!data
->tp_input
) {
1018 dev_warn_once(&data
->client
->dev
,
1019 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
1023 input_report_key(input
, BTN_LEFT
, packet
[0] & 0x01);
1024 input_report_key(input
, BTN_RIGHT
, packet
[0] & 0x02);
1025 input_report_key(input
, BTN_MIDDLE
, packet
[0] & 0x04);
1027 if ((packet
[3] & 0x0F) == 0x06) {
1028 x
= packet
[4] - (int)((packet
[1] ^ 0x80) << 1);
1029 y
= (int)((packet
[2] ^ 0x80) << 1) - packet
[5];
1031 input_report_rel(input
, REL_X
, x
);
1032 input_report_rel(input
, REL_Y
, y
);
1038 static irqreturn_t
elan_isr(int irq
, void *dev_id
)
1040 struct elan_tp_data
*data
= dev_id
;
1042 u8 report
[ETP_MAX_REPORT_LEN
];
1045 * When device is connected to i2c bus, when all IAP page writes
1046 * complete, the driver will receive interrupt and must read
1047 * 0000 to confirm that IAP is finished.
1049 if (data
->in_fw_update
) {
1050 complete(&data
->fw_completion
);
1054 error
= data
->ops
->get_report(data
->client
, report
, data
->report_len
);
1058 switch (report
[ETP_REPORT_ID_OFFSET
]) {
1060 elan_report_absolute(data
, report
, false);
1062 case ETP_REPORT_ID2
:
1063 elan_report_absolute(data
, report
, true);
1065 case ETP_TP_REPORT_ID
:
1066 case ETP_TP_REPORT_ID2
:
1067 elan_report_trackpoint(data
, report
);
1070 dev_err(&data
->client
->dev
, "invalid report id data (%x)\n",
1071 report
[ETP_REPORT_ID_OFFSET
]);
1079 ******************************************************************
1080 * Elan initialization functions
1081 ******************************************************************
1084 static int elan_setup_trackpoint_input_device(struct elan_tp_data
*data
)
1086 struct device
*dev
= &data
->client
->dev
;
1087 struct input_dev
*input
;
1089 input
= devm_input_allocate_device(dev
);
1093 input
->name
= "Elan TrackPoint";
1094 input
->id
.bustype
= BUS_I2C
;
1095 input
->id
.vendor
= ELAN_VENDOR_ID
;
1096 input
->id
.product
= data
->product_id
;
1097 input_set_drvdata(input
, data
);
1099 input_set_capability(input
, EV_REL
, REL_X
);
1100 input_set_capability(input
, EV_REL
, REL_Y
);
1101 input_set_capability(input
, EV_KEY
, BTN_LEFT
);
1102 input_set_capability(input
, EV_KEY
, BTN_RIGHT
);
1103 input_set_capability(input
, EV_KEY
, BTN_MIDDLE
);
1105 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
1106 __set_bit(INPUT_PROP_POINTING_STICK
, input
->propbit
);
1108 data
->tp_input
= input
;
1113 static int elan_setup_input_device(struct elan_tp_data
*data
)
1115 struct device
*dev
= &data
->client
->dev
;
1116 struct input_dev
*input
;
1117 unsigned int max_width
= max(data
->width_x
, data
->width_y
);
1118 unsigned int min_width
= min(data
->width_x
, data
->width_y
);
1121 input
= devm_input_allocate_device(dev
);
1125 input
->name
= "Elan Touchpad";
1126 input
->id
.bustype
= BUS_I2C
;
1127 input
->id
.vendor
= ELAN_VENDOR_ID
;
1128 input
->id
.product
= data
->product_id
;
1129 input_set_drvdata(input
, data
);
1131 error
= input_mt_init_slots(input
, ETP_MAX_FINGERS
,
1132 INPUT_MT_POINTER
| INPUT_MT_DROP_UNUSED
);
1134 dev_err(dev
, "failed to initialize MT slots: %d\n", error
);
1138 __set_bit(EV_ABS
, input
->evbit
);
1139 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
1140 if (data
->clickpad
) {
1141 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
1143 __set_bit(BTN_RIGHT
, input
->keybit
);
1144 if (data
->middle_button
)
1145 __set_bit(BTN_MIDDLE
, input
->keybit
);
1147 __set_bit(BTN_LEFT
, input
->keybit
);
1149 /* Set up ST parameters */
1150 input_set_abs_params(input
, ABS_X
, 0, data
->max_x
, 0, 0);
1151 input_set_abs_params(input
, ABS_Y
, 0, data
->max_y
, 0, 0);
1152 input_abs_set_res(input
, ABS_X
, data
->x_res
);
1153 input_abs_set_res(input
, ABS_Y
, data
->y_res
);
1154 input_set_abs_params(input
, ABS_PRESSURE
, 0, ETP_MAX_PRESSURE
, 0, 0);
1155 if (data
->report_features
& ETP_FEATURE_REPORT_MK
)
1156 input_set_abs_params(input
, ABS_TOOL_WIDTH
,
1157 0, ETP_FINGER_WIDTH
, 0, 0);
1158 input_set_abs_params(input
, ABS_DISTANCE
, 0, 1, 0, 0);
1160 /* And MT parameters */
1161 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, data
->max_x
, 0, 0);
1162 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, data
->max_y
, 0, 0);
1163 input_abs_set_res(input
, ABS_MT_POSITION_X
, data
->x_res
);
1164 input_abs_set_res(input
, ABS_MT_POSITION_Y
, data
->y_res
);
1165 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0,
1166 ETP_MAX_PRESSURE
, 0, 0);
1167 if (data
->report_features
& ETP_FEATURE_REPORT_MK
) {
1168 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
,
1169 0, ETP_FINGER_WIDTH
* max_width
, 0, 0);
1170 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
,
1171 0, ETP_FINGER_WIDTH
* min_width
, 0, 0);
1174 data
->input
= input
;
1179 static void elan_disable_regulator(void *_data
)
1181 struct elan_tp_data
*data
= _data
;
1183 regulator_disable(data
->vcc
);
1186 static int elan_probe(struct i2c_client
*client
,
1187 const struct i2c_device_id
*dev_id
)
1189 const struct elan_transport_ops
*transport_ops
;
1190 struct device
*dev
= &client
->dev
;
1191 struct elan_tp_data
*data
;
1192 unsigned long irqflags
;
1195 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C
) &&
1196 i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1197 transport_ops
= &elan_i2c_ops
;
1198 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS
) &&
1199 i2c_check_functionality(client
->adapter
,
1200 I2C_FUNC_SMBUS_BYTE_DATA
|
1201 I2C_FUNC_SMBUS_BLOCK_DATA
|
1202 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1203 transport_ops
= &elan_smbus_ops
;
1205 dev_err(dev
, "not a supported I2C/SMBus adapter\n");
1209 data
= devm_kzalloc(dev
, sizeof(struct elan_tp_data
), GFP_KERNEL
);
1213 i2c_set_clientdata(client
, data
);
1215 data
->ops
= transport_ops
;
1216 data
->client
= client
;
1217 init_completion(&data
->fw_completion
);
1218 mutex_init(&data
->sysfs_mutex
);
1220 data
->vcc
= devm_regulator_get(dev
, "vcc");
1221 if (IS_ERR(data
->vcc
)) {
1222 error
= PTR_ERR(data
->vcc
);
1223 if (error
!= -EPROBE_DEFER
)
1224 dev_err(dev
, "Failed to get 'vcc' regulator: %d\n",
1229 error
= regulator_enable(data
->vcc
);
1231 dev_err(dev
, "Failed to enable regulator: %d\n", error
);
1235 error
= devm_add_action_or_reset(dev
, elan_disable_regulator
, data
);
1237 dev_err(dev
, "Failed to add disable regulator action: %d\n",
1242 /* Make sure there is something at this address */
1243 error
= i2c_smbus_read_byte(client
);
1245 dev_dbg(&client
->dev
, "nothing at this address: %d\n", error
);
1249 /* Initialize the touchpad. */
1250 error
= elan_initialize(data
);
1254 error
= elan_query_device_info(data
);
1258 error
= elan_query_device_parameters(data
);
1263 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1270 "Elan Touchpad Extra Information:\n"
1271 " Max ABS X,Y: %d,%d\n"
1272 " Width X,Y: %d,%d\n"
1273 " Resolution X,Y: %d,%d (dots/mm)\n"
1275 " info pattern: 0x%x\n",
1276 data
->max_x
, data
->max_y
,
1277 data
->width_x
, data
->width_y
,
1278 data
->x_res
, data
->y_res
,
1279 data
->ic_type
, data
->pattern
);
1281 /* Set up input device properties based on queried parameters. */
1282 error
= elan_setup_input_device(data
);
1286 if (device_property_read_bool(&client
->dev
, "elan,trackpoint")) {
1287 error
= elan_setup_trackpoint_input_device(data
);
1293 * Platform code (ACPI, DTS) should normally set up interrupt
1294 * for us, but in case it did not let's fall back to using falling
1295 * edge to be compatible with older Chromebooks.
1297 irqflags
= irq_get_trigger_type(client
->irq
);
1299 irqflags
= IRQF_TRIGGER_FALLING
;
1301 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, elan_isr
,
1302 irqflags
| IRQF_ONESHOT
,
1303 client
->name
, data
);
1305 dev_err(dev
, "cannot register irq=%d\n", client
->irq
);
1309 error
= devm_device_add_groups(dev
, elan_sysfs_groups
);
1311 dev_err(dev
, "failed to create sysfs attributes: %d\n", error
);
1315 error
= input_register_device(data
->input
);
1317 dev_err(dev
, "failed to register input device: %d\n", error
);
1321 if (data
->tp_input
) {
1322 error
= input_register_device(data
->tp_input
);
1324 dev_err(&client
->dev
,
1325 "failed to register TrackPoint input device: %d\n",
1332 * Systems using device tree should set up wakeup via DTS,
1333 * the rest will configure device as wakeup source by default.
1336 device_init_wakeup(dev
, true);
1341 static int __maybe_unused
elan_suspend(struct device
*dev
)
1343 struct i2c_client
*client
= to_i2c_client(dev
);
1344 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1348 * We are taking the mutex to make sure sysfs operations are
1349 * complete before we attempt to bring the device into low[er]
1352 ret
= mutex_lock_interruptible(&data
->sysfs_mutex
);
1356 disable_irq(client
->irq
);
1358 if (device_may_wakeup(dev
)) {
1359 ret
= elan_sleep(data
);
1360 /* Enable wake from IRQ */
1361 data
->irq_wake
= (enable_irq_wake(client
->irq
) == 0);
1363 ret
= elan_disable_power(data
);
1366 mutex_unlock(&data
->sysfs_mutex
);
1370 static int __maybe_unused
elan_resume(struct device
*dev
)
1372 struct i2c_client
*client
= to_i2c_client(dev
);
1373 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1376 if (device_may_wakeup(dev
) && data
->irq_wake
) {
1377 disable_irq_wake(client
->irq
);
1378 data
->irq_wake
= false;
1381 error
= elan_enable_power(data
);
1383 dev_err(dev
, "power up when resuming failed: %d\n", error
);
1387 error
= elan_initialize(data
);
1389 dev_err(dev
, "initialize when resuming failed: %d\n", error
);
1392 enable_irq(data
->client
->irq
);
1396 static SIMPLE_DEV_PM_OPS(elan_pm_ops
, elan_suspend
, elan_resume
);
1398 static const struct i2c_device_id elan_id
[] = {
1402 MODULE_DEVICE_TABLE(i2c
, elan_id
);
1405 #include <linux/input/elan-i2c-ids.h>
1406 MODULE_DEVICE_TABLE(acpi
, elan_acpi_id
);
1410 static const struct of_device_id elan_of_match
[] = {
1411 { .compatible
= "elan,ekth3000" },
1414 MODULE_DEVICE_TABLE(of
, elan_of_match
);
1417 static struct i2c_driver elan_driver
= {
1419 .name
= DRIVER_NAME
,
1421 .acpi_match_table
= ACPI_PTR(elan_acpi_id
),
1422 .of_match_table
= of_match_ptr(elan_of_match
),
1423 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1425 .probe
= elan_probe
,
1426 .id_table
= elan_id
,
1429 module_i2c_driver(elan_driver
);
1431 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1432 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1433 MODULE_LICENSE("GPL");