2 * Elan I2C/SMBus Touchpad driver
4 * Copyright (c) 2013 ELAN Microelectronics Corp.
6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
7 * Author: KT Liao <kt.liao@emc.com.tw>
10 * Based on cyapa driver:
11 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
12 * copyright (c) 2011-2012 Google, Inc.
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as published
16 * by the Free Software Foundation.
18 * Trademarks are the property of their respective owners.
21 #include <linux/acpi.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/firmware.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/input/mt.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/input.h>
35 #include <linux/uaccess.h>
36 #include <linux/jiffies.h>
37 #include <linux/completion.h>
39 #include <linux/regulator/consumer.h>
40 #include <asm/unaligned.h>
44 #define DRIVER_NAME "elan_i2c"
45 #define ELAN_VENDOR_ID 0x04f3
46 #define ETP_MAX_PRESSURE 255
47 #define ETP_FWIDTH_REDUCE 90
48 #define ETP_FINGER_WIDTH 15
49 #define ETP_RETRY_COUNT 3
51 #define ETP_MAX_FINGERS 5
52 #define ETP_FINGER_DATA_LEN 5
53 #define ETP_REPORT_ID 0x5D
54 #define ETP_REPORT_ID_OFFSET 2
55 #define ETP_TOUCH_INFO_OFFSET 3
56 #define ETP_FINGER_DATA_OFFSET 4
57 #define ETP_HOVER_INFO_OFFSET 30
58 #define ETP_MAX_REPORT_LEN 34
60 /* The main device structure */
62 struct i2c_client
*client
;
63 struct input_dev
*input
;
64 struct regulator
*vcc
;
66 const struct elan_transport_ops
*ops
;
69 struct completion fw_completion
;
72 struct mutex sysfs_mutex
;
87 int pressure_adjustment
;
90 u16 fw_validpage_count
;
91 u16 fw_signature_address
;
101 static int elan_get_fwinfo(u16 ic_type
, u16
*validpage_count
,
102 u16
*signature_address
)
108 *validpage_count
= 512;
116 *validpage_count
= 768;
119 *validpage_count
= 896;
122 *validpage_count
= 640;
125 *validpage_count
= 1024;
128 /* unknown ic type clear value */
129 *validpage_count
= 0;
130 *signature_address
= 0;
135 (*validpage_count
* ETP_FW_PAGE_SIZE
) - ETP_FW_SIGNATURE_SIZE
;
140 static int elan_enable_power(struct elan_tp_data
*data
)
142 int repeat
= ETP_RETRY_COUNT
;
145 error
= regulator_enable(data
->vcc
);
147 dev_err(&data
->client
->dev
,
148 "failed to enable regulator: %d\n", error
);
153 error
= data
->ops
->power_control(data
->client
, true);
158 } while (--repeat
> 0);
160 dev_err(&data
->client
->dev
, "failed to enable power: %d\n", error
);
164 static int elan_disable_power(struct elan_tp_data
*data
)
166 int repeat
= ETP_RETRY_COUNT
;
170 error
= data
->ops
->power_control(data
->client
, false);
172 error
= regulator_disable(data
->vcc
);
174 dev_err(&data
->client
->dev
,
175 "failed to disable regulator: %d\n",
177 /* Attempt to power the chip back up */
178 data
->ops
->power_control(data
->client
, true);
186 } while (--repeat
> 0);
188 dev_err(&data
->client
->dev
, "failed to disable power: %d\n", error
);
192 static int elan_sleep(struct elan_tp_data
*data
)
194 int repeat
= ETP_RETRY_COUNT
;
198 error
= data
->ops
->sleep_control(data
->client
, true);
203 } while (--repeat
> 0);
208 static int elan_query_product(struct elan_tp_data
*data
)
212 error
= data
->ops
->get_product_id(data
->client
, &data
->product_id
);
216 error
= data
->ops
->get_sm_version(data
->client
, &data
->ic_type
,
217 &data
->sm_version
, &data
->clickpad
);
224 static int elan_check_ASUS_special_fw(struct elan_tp_data
*data
)
226 if (data
->ic_type
== 0x0E) {
227 switch (data
->product_id
) {
233 } else if (data
->ic_type
== 0x08 && data
->product_id
== 0x26) {
234 /* ASUS EeeBook X205TA */
241 static int __elan_initialize(struct elan_tp_data
*data
)
243 struct i2c_client
*client
= data
->client
;
244 bool woken_up
= false;
247 error
= data
->ops
->initialize(client
);
249 dev_err(&client
->dev
, "device initialize failed: %d\n", error
);
253 error
= elan_query_product(data
);
258 * Some ASUS devices were shipped with firmware that requires
259 * touchpads to be woken up first, before attempting to switch
260 * them into absolute reporting mode.
262 if (elan_check_ASUS_special_fw(data
)) {
263 error
= data
->ops
->sleep_control(client
, false);
265 dev_err(&client
->dev
,
266 "failed to wake device up: %d\n", error
);
274 data
->mode
|= ETP_ENABLE_ABS
;
275 error
= data
->ops
->set_mode(client
, data
->mode
);
277 dev_err(&client
->dev
,
278 "failed to switch to absolute mode: %d\n", error
);
283 error
= data
->ops
->sleep_control(client
, false);
285 dev_err(&client
->dev
,
286 "failed to wake device up: %d\n", error
);
294 static int elan_initialize(struct elan_tp_data
*data
)
296 int repeat
= ETP_RETRY_COUNT
;
300 error
= __elan_initialize(data
);
305 } while (--repeat
> 0);
310 static int elan_query_device_info(struct elan_tp_data
*data
)
315 error
= data
->ops
->get_version(data
->client
, false, &data
->fw_version
);
319 error
= data
->ops
->get_checksum(data
->client
, false,
324 error
= data
->ops
->get_version(data
->client
, true, &data
->iap_version
);
328 error
= data
->ops
->get_pressure_adjustment(data
->client
,
329 &data
->pressure_adjustment
);
333 error
= data
->ops
->get_pattern(data
->client
, &data
->pattern
);
337 if (data
->pattern
== 0x01)
338 ic_type
= data
->ic_type
;
340 ic_type
= data
->iap_version
;
342 error
= elan_get_fwinfo(ic_type
, &data
->fw_validpage_count
,
343 &data
->fw_signature_address
);
345 dev_warn(&data
->client
->dev
,
346 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
347 data
->iap_version
, data
->ic_type
);
352 static unsigned int elan_convert_resolution(u8 val
)
355 * (value from firmware) * 10 + 790 = dpi
357 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
361 return ((int)(char)val
* 10 + 790) * 10 / 254;
364 static int elan_query_device_parameters(struct elan_tp_data
*data
)
366 unsigned int x_traces
, y_traces
;
367 u8 hw_x_res
, hw_y_res
;
370 error
= data
->ops
->get_max(data
->client
, &data
->max_x
, &data
->max_y
);
374 error
= data
->ops
->get_num_traces(data
->client
, &x_traces
, &y_traces
);
378 data
->width_x
= data
->max_x
/ x_traces
;
379 data
->width_y
= data
->max_y
/ y_traces
;
381 error
= data
->ops
->get_resolution(data
->client
, &hw_x_res
, &hw_y_res
);
385 data
->x_res
= elan_convert_resolution(hw_x_res
);
386 data
->y_res
= elan_convert_resolution(hw_y_res
);
392 **********************************************************
393 * IAP firmware updater related routines
394 **********************************************************
396 static int elan_write_fw_block(struct elan_tp_data
*data
,
397 const u8
*page
, u16 checksum
, int idx
)
399 int retry
= ETP_RETRY_COUNT
;
403 error
= data
->ops
->write_fw_block(data
->client
,
404 page
, checksum
, idx
);
408 dev_dbg(&data
->client
->dev
,
409 "IAP retrying page %d (error: %d)\n", idx
, error
);
410 } while (--retry
> 0);
415 static int __elan_update_firmware(struct elan_tp_data
*data
,
416 const struct firmware
*fw
)
418 struct i2c_client
*client
= data
->client
;
419 struct device
*dev
= &client
->dev
;
424 u16 sw_checksum
= 0, fw_checksum
= 0;
426 error
= data
->ops
->prepare_fw_update(client
);
430 iap_start_addr
= get_unaligned_le16(&fw
->data
[ETP_IAP_START_ADDR
* 2]);
432 boot_page_count
= (iap_start_addr
* 2) / ETP_FW_PAGE_SIZE
;
433 for (i
= boot_page_count
; i
< data
->fw_validpage_count
; i
++) {
435 const u8
*page
= &fw
->data
[i
* ETP_FW_PAGE_SIZE
];
437 for (j
= 0; j
< ETP_FW_PAGE_SIZE
; j
+= 2)
438 checksum
+= ((page
[j
+ 1] << 8) | page
[j
]);
440 error
= elan_write_fw_block(data
, page
, checksum
, i
);
442 dev_err(dev
, "write page %d fail: %d\n", i
, error
);
446 sw_checksum
+= checksum
;
449 /* Wait WDT reset and power on reset */
452 error
= data
->ops
->finish_fw_update(client
, &data
->fw_completion
);
456 error
= data
->ops
->get_checksum(client
, true, &fw_checksum
);
460 if (sw_checksum
!= fw_checksum
) {
461 dev_err(dev
, "checksum diff sw=[%04X], fw=[%04X]\n",
462 sw_checksum
, fw_checksum
);
469 static int elan_update_firmware(struct elan_tp_data
*data
,
470 const struct firmware
*fw
)
472 struct i2c_client
*client
= data
->client
;
475 dev_dbg(&client
->dev
, "Starting firmware update....\n");
477 disable_irq(client
->irq
);
478 data
->in_fw_update
= true;
480 retval
= __elan_update_firmware(data
, fw
);
482 dev_err(&client
->dev
, "firmware update failed: %d\n", retval
);
483 data
->ops
->iap_reset(client
);
485 /* Reinitialize TP after fw is updated */
486 elan_initialize(data
);
487 elan_query_device_info(data
);
490 data
->in_fw_update
= false;
491 enable_irq(client
->irq
);
497 *******************************************************************
499 *******************************************************************
501 static ssize_t
elan_sysfs_read_fw_checksum(struct device
*dev
,
502 struct device_attribute
*attr
,
505 struct i2c_client
*client
= to_i2c_client(dev
);
506 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
508 return sprintf(buf
, "0x%04x\n", data
->fw_checksum
);
511 static ssize_t
elan_sysfs_read_product_id(struct device
*dev
,
512 struct device_attribute
*attr
,
515 struct i2c_client
*client
= to_i2c_client(dev
);
516 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
518 return sprintf(buf
, ETP_PRODUCT_ID_FORMAT_STRING
"\n",
522 static ssize_t
elan_sysfs_read_fw_ver(struct device
*dev
,
523 struct device_attribute
*attr
,
526 struct i2c_client
*client
= to_i2c_client(dev
);
527 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
529 return sprintf(buf
, "%d.0\n", data
->fw_version
);
532 static ssize_t
elan_sysfs_read_sm_ver(struct device
*dev
,
533 struct device_attribute
*attr
,
536 struct i2c_client
*client
= to_i2c_client(dev
);
537 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
539 return sprintf(buf
, "%d.0\n", data
->sm_version
);
542 static ssize_t
elan_sysfs_read_iap_ver(struct device
*dev
,
543 struct device_attribute
*attr
,
546 struct i2c_client
*client
= to_i2c_client(dev
);
547 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
549 return sprintf(buf
, "%d.0\n", data
->iap_version
);
552 static ssize_t
elan_sysfs_update_fw(struct device
*dev
,
553 struct device_attribute
*attr
,
554 const char *buf
, size_t count
)
556 struct elan_tp_data
*data
= dev_get_drvdata(dev
);
557 const struct firmware
*fw
;
560 const u8
*fw_signature
;
561 static const u8 signature
[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
563 if (data
->fw_validpage_count
== 0)
566 /* Look for a firmware with the product id appended. */
567 fw_name
= kasprintf(GFP_KERNEL
, ETP_FW_NAME
, data
->product_id
);
569 dev_err(dev
, "failed to allocate memory for firmware name\n");
573 dev_info(dev
, "requesting fw '%s'\n", fw_name
);
574 error
= request_firmware(&fw
, fw_name
, dev
);
577 dev_err(dev
, "failed to request firmware: %d\n", error
);
581 /* Firmware file must match signature data */
582 fw_signature
= &fw
->data
[data
->fw_signature_address
];
583 if (memcmp(fw_signature
, signature
, sizeof(signature
)) != 0) {
584 dev_err(dev
, "signature mismatch (expected %*ph, got %*ph)\n",
585 (int)sizeof(signature
), signature
,
586 (int)sizeof(signature
), fw_signature
);
591 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
595 error
= elan_update_firmware(data
, fw
);
597 mutex_unlock(&data
->sysfs_mutex
);
600 release_firmware(fw
);
601 return error
?: count
;
604 static ssize_t
calibrate_store(struct device
*dev
,
605 struct device_attribute
*attr
,
606 const char *buf
, size_t count
)
608 struct i2c_client
*client
= to_i2c_client(dev
);
609 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
615 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
619 disable_irq(client
->irq
);
621 data
->mode
|= ETP_ENABLE_CALIBRATE
;
622 retval
= data
->ops
->set_mode(client
, data
->mode
);
624 dev_err(dev
, "failed to enable calibration mode: %d\n",
629 retval
= data
->ops
->calibrate(client
);
631 dev_err(dev
, "failed to start calibration: %d\n",
633 goto out_disable_calibrate
;
638 /* Wait 250ms before checking if calibration has completed. */
641 retval
= data
->ops
->calibrate_result(client
, val
);
643 dev_err(dev
, "failed to check calibration result: %d\n",
645 else if (val
[0] == 0)
646 break; /* calibration done */
651 dev_err(dev
, "failed to calibrate. Timeout.\n");
655 out_disable_calibrate
:
656 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
657 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
659 dev_err(dev
, "failed to disable calibration mode: %d\n",
665 enable_irq(client
->irq
);
666 mutex_unlock(&data
->sysfs_mutex
);
667 return retval
?: count
;
670 static ssize_t
elan_sysfs_read_mode(struct device
*dev
,
671 struct device_attribute
*attr
,
674 struct i2c_client
*client
= to_i2c_client(dev
);
675 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
679 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
683 error
= data
->ops
->iap_get_mode(data
->client
, &mode
);
685 mutex_unlock(&data
->sysfs_mutex
);
690 return sprintf(buf
, "%d\n", (int)mode
);
693 static DEVICE_ATTR(product_id
, S_IRUGO
, elan_sysfs_read_product_id
, NULL
);
694 static DEVICE_ATTR(firmware_version
, S_IRUGO
, elan_sysfs_read_fw_ver
, NULL
);
695 static DEVICE_ATTR(sample_version
, S_IRUGO
, elan_sysfs_read_sm_ver
, NULL
);
696 static DEVICE_ATTR(iap_version
, S_IRUGO
, elan_sysfs_read_iap_ver
, NULL
);
697 static DEVICE_ATTR(fw_checksum
, S_IRUGO
, elan_sysfs_read_fw_checksum
, NULL
);
698 static DEVICE_ATTR(mode
, S_IRUGO
, elan_sysfs_read_mode
, NULL
);
699 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, elan_sysfs_update_fw
);
701 static DEVICE_ATTR_WO(calibrate
);
703 static struct attribute
*elan_sysfs_entries
[] = {
704 &dev_attr_product_id
.attr
,
705 &dev_attr_firmware_version
.attr
,
706 &dev_attr_sample_version
.attr
,
707 &dev_attr_iap_version
.attr
,
708 &dev_attr_fw_checksum
.attr
,
709 &dev_attr_calibrate
.attr
,
711 &dev_attr_update_fw
.attr
,
715 static const struct attribute_group elan_sysfs_group
= {
716 .attrs
= elan_sysfs_entries
,
719 static ssize_t
acquire_store(struct device
*dev
, struct device_attribute
*attr
,
720 const char *buf
, size_t count
)
722 struct i2c_client
*client
= to_i2c_client(dev
);
723 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
727 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
731 disable_irq(client
->irq
);
733 data
->baseline_ready
= false;
735 data
->mode
|= ETP_ENABLE_CALIBRATE
;
736 retval
= data
->ops
->set_mode(data
->client
, data
->mode
);
738 dev_err(dev
, "Failed to enable calibration mode to get baseline: %d\n",
745 retval
= data
->ops
->get_baseline_data(data
->client
, true,
746 &data
->max_baseline
);
748 dev_err(dev
, "Failed to read max baseline form device: %d\n",
750 goto out_disable_calibrate
;
753 retval
= data
->ops
->get_baseline_data(data
->client
, false,
754 &data
->min_baseline
);
756 dev_err(dev
, "Failed to read min baseline form device: %d\n",
758 goto out_disable_calibrate
;
761 data
->baseline_ready
= true;
763 out_disable_calibrate
:
764 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
765 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
767 dev_err(dev
, "Failed to disable calibration mode after acquiring baseline: %d\n",
773 enable_irq(client
->irq
);
774 mutex_unlock(&data
->sysfs_mutex
);
775 return retval
?: count
;
778 static ssize_t
min_show(struct device
*dev
,
779 struct device_attribute
*attr
, char *buf
)
781 struct i2c_client
*client
= to_i2c_client(dev
);
782 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
785 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
789 if (!data
->baseline_ready
) {
794 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->min_baseline
);
797 mutex_unlock(&data
->sysfs_mutex
);
801 static ssize_t
max_show(struct device
*dev
,
802 struct device_attribute
*attr
, char *buf
)
804 struct i2c_client
*client
= to_i2c_client(dev
);
805 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
808 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
812 if (!data
->baseline_ready
) {
817 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->max_baseline
);
820 mutex_unlock(&data
->sysfs_mutex
);
825 static DEVICE_ATTR_WO(acquire
);
826 static DEVICE_ATTR_RO(min
);
827 static DEVICE_ATTR_RO(max
);
829 static struct attribute
*elan_baseline_sysfs_entries
[] = {
830 &dev_attr_acquire
.attr
,
836 static const struct attribute_group elan_baseline_sysfs_group
= {
838 .attrs
= elan_baseline_sysfs_entries
,
841 static const struct attribute_group
*elan_sysfs_groups
[] = {
843 &elan_baseline_sysfs_group
,
848 ******************************************************************
850 ******************************************************************
852 static void elan_report_contact(struct elan_tp_data
*data
,
853 int contact_num
, bool contact_valid
,
856 struct input_dev
*input
= data
->input
;
857 unsigned int pos_x
, pos_y
;
858 unsigned int pressure
, mk_x
, mk_y
;
859 unsigned int area_x
, area_y
, major
, minor
;
860 unsigned int scaled_pressure
;
863 pos_x
= ((finger_data
[0] & 0xf0) << 4) |
865 pos_y
= ((finger_data
[0] & 0x0f) << 8) |
867 mk_x
= (finger_data
[3] & 0x0f);
868 mk_y
= (finger_data
[3] >> 4);
869 pressure
= finger_data
[4];
871 if (pos_x
> data
->max_x
|| pos_y
> data
->max_y
) {
872 dev_dbg(input
->dev
.parent
,
873 "[%d] x=%d y=%d over max (%d, %d)",
874 contact_num
, pos_x
, pos_y
,
875 data
->max_x
, data
->max_y
);
880 * To avoid treating large finger as palm, let's reduce the
881 * width x and y per trace.
883 area_x
= mk_x
* (data
->width_x
- ETP_FWIDTH_REDUCE
);
884 area_y
= mk_y
* (data
->width_y
- ETP_FWIDTH_REDUCE
);
886 major
= max(area_x
, area_y
);
887 minor
= min(area_x
, area_y
);
889 scaled_pressure
= pressure
+ data
->pressure_adjustment
;
891 if (scaled_pressure
> ETP_MAX_PRESSURE
)
892 scaled_pressure
= ETP_MAX_PRESSURE
;
894 input_mt_slot(input
, contact_num
);
895 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
896 input_report_abs(input
, ABS_MT_POSITION_X
, pos_x
);
897 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->max_y
- pos_y
);
898 input_report_abs(input
, ABS_MT_PRESSURE
, scaled_pressure
);
899 input_report_abs(input
, ABS_TOOL_WIDTH
, mk_x
);
900 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, major
);
901 input_report_abs(input
, ABS_MT_TOUCH_MINOR
, minor
);
903 input_mt_slot(input
, contact_num
);
904 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, false);
908 static void elan_report_absolute(struct elan_tp_data
*data
, u8
*packet
)
910 struct input_dev
*input
= data
->input
;
911 u8
*finger_data
= &packet
[ETP_FINGER_DATA_OFFSET
];
913 u8 tp_info
= packet
[ETP_TOUCH_INFO_OFFSET
];
914 u8 hover_info
= packet
[ETP_HOVER_INFO_OFFSET
];
915 bool contact_valid
, hover_event
;
917 hover_event
= hover_info
& 0x40;
918 for (i
= 0; i
< ETP_MAX_FINGERS
; i
++) {
919 contact_valid
= tp_info
& (1U << (3 + i
));
920 elan_report_contact(data
, i
, contact_valid
, finger_data
);
923 finger_data
+= ETP_FINGER_DATA_LEN
;
926 input_report_key(input
, BTN_LEFT
, tp_info
& 0x01);
927 input_report_key(input
, BTN_RIGHT
, tp_info
& 0x02);
928 input_report_abs(input
, ABS_DISTANCE
, hover_event
!= 0);
929 input_mt_report_pointer_emulation(input
, true);
933 static irqreturn_t
elan_isr(int irq
, void *dev_id
)
935 struct elan_tp_data
*data
= dev_id
;
936 struct device
*dev
= &data
->client
->dev
;
938 u8 report
[ETP_MAX_REPORT_LEN
];
941 * When device is connected to i2c bus, when all IAP page writes
942 * complete, the driver will receive interrupt and must read
943 * 0000 to confirm that IAP is finished.
945 if (data
->in_fw_update
) {
946 complete(&data
->fw_completion
);
950 error
= data
->ops
->get_report(data
->client
, report
);
954 if (report
[ETP_REPORT_ID_OFFSET
] != ETP_REPORT_ID
)
955 dev_err(dev
, "invalid report id data (%x)\n",
956 report
[ETP_REPORT_ID_OFFSET
]);
958 elan_report_absolute(data
, report
);
965 ******************************************************************
966 * Elan initialization functions
967 ******************************************************************
969 static int elan_setup_input_device(struct elan_tp_data
*data
)
971 struct device
*dev
= &data
->client
->dev
;
972 struct input_dev
*input
;
973 unsigned int max_width
= max(data
->width_x
, data
->width_y
);
974 unsigned int min_width
= min(data
->width_x
, data
->width_y
);
977 input
= devm_input_allocate_device(dev
);
981 input
->name
= "Elan Touchpad";
982 input
->id
.bustype
= BUS_I2C
;
983 input
->id
.vendor
= ELAN_VENDOR_ID
;
984 input
->id
.product
= data
->product_id
;
985 input_set_drvdata(input
, data
);
987 error
= input_mt_init_slots(input
, ETP_MAX_FINGERS
,
988 INPUT_MT_POINTER
| INPUT_MT_DROP_UNUSED
);
990 dev_err(dev
, "failed to initialize MT slots: %d\n", error
);
994 __set_bit(EV_ABS
, input
->evbit
);
995 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
997 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
999 __set_bit(BTN_RIGHT
, input
->keybit
);
1000 __set_bit(BTN_LEFT
, input
->keybit
);
1002 /* Set up ST parameters */
1003 input_set_abs_params(input
, ABS_X
, 0, data
->max_x
, 0, 0);
1004 input_set_abs_params(input
, ABS_Y
, 0, data
->max_y
, 0, 0);
1005 input_abs_set_res(input
, ABS_X
, data
->x_res
);
1006 input_abs_set_res(input
, ABS_Y
, data
->y_res
);
1007 input_set_abs_params(input
, ABS_PRESSURE
, 0, ETP_MAX_PRESSURE
, 0, 0);
1008 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0, ETP_FINGER_WIDTH
, 0, 0);
1009 input_set_abs_params(input
, ABS_DISTANCE
, 0, 1, 0, 0);
1011 /* And MT parameters */
1012 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, data
->max_x
, 0, 0);
1013 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, data
->max_y
, 0, 0);
1014 input_abs_set_res(input
, ABS_MT_POSITION_X
, data
->x_res
);
1015 input_abs_set_res(input
, ABS_MT_POSITION_Y
, data
->y_res
);
1016 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0,
1017 ETP_MAX_PRESSURE
, 0, 0);
1018 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0,
1019 ETP_FINGER_WIDTH
* max_width
, 0, 0);
1020 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0,
1021 ETP_FINGER_WIDTH
* min_width
, 0, 0);
1023 data
->input
= input
;
1028 static void elan_disable_regulator(void *_data
)
1030 struct elan_tp_data
*data
= _data
;
1032 regulator_disable(data
->vcc
);
1035 static void elan_remove_sysfs_groups(void *_data
)
1037 struct elan_tp_data
*data
= _data
;
1039 sysfs_remove_groups(&data
->client
->dev
.kobj
, elan_sysfs_groups
);
1042 static int elan_probe(struct i2c_client
*client
,
1043 const struct i2c_device_id
*dev_id
)
1045 const struct elan_transport_ops
*transport_ops
;
1046 struct device
*dev
= &client
->dev
;
1047 struct elan_tp_data
*data
;
1048 unsigned long irqflags
;
1051 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C
) &&
1052 i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1053 transport_ops
= &elan_i2c_ops
;
1054 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS
) &&
1055 i2c_check_functionality(client
->adapter
,
1056 I2C_FUNC_SMBUS_BYTE_DATA
|
1057 I2C_FUNC_SMBUS_BLOCK_DATA
|
1058 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1059 transport_ops
= &elan_smbus_ops
;
1061 dev_err(dev
, "not a supported I2C/SMBus adapter\n");
1065 data
= devm_kzalloc(dev
, sizeof(struct elan_tp_data
), GFP_KERNEL
);
1069 i2c_set_clientdata(client
, data
);
1071 data
->ops
= transport_ops
;
1072 data
->client
= client
;
1073 init_completion(&data
->fw_completion
);
1074 mutex_init(&data
->sysfs_mutex
);
1076 data
->vcc
= devm_regulator_get(dev
, "vcc");
1077 if (IS_ERR(data
->vcc
)) {
1078 error
= PTR_ERR(data
->vcc
);
1079 if (error
!= -EPROBE_DEFER
)
1080 dev_err(dev
, "Failed to get 'vcc' regulator: %d\n",
1085 error
= regulator_enable(data
->vcc
);
1087 dev_err(dev
, "Failed to enable regulator: %d\n", error
);
1091 error
= devm_add_action(dev
, elan_disable_regulator
, data
);
1093 regulator_disable(data
->vcc
);
1094 dev_err(dev
, "Failed to add disable regulator action: %d\n",
1099 /* Make sure there is something at this address */
1100 error
= i2c_smbus_read_byte(client
);
1102 dev_dbg(&client
->dev
, "nothing at this address: %d\n", error
);
1106 /* Initialize the touchpad. */
1107 error
= elan_initialize(data
);
1111 error
= elan_query_device_info(data
);
1115 error
= elan_query_device_parameters(data
);
1120 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1127 "Elan Touchpad Extra Information:\n"
1128 " Max ABS X,Y: %d,%d\n"
1129 " Width X,Y: %d,%d\n"
1130 " Resolution X,Y: %d,%d (dots/mm)\n"
1132 " info pattern: 0x%x\n",
1133 data
->max_x
, data
->max_y
,
1134 data
->width_x
, data
->width_y
,
1135 data
->x_res
, data
->y_res
,
1136 data
->ic_type
, data
->pattern
);
1138 /* Set up input device properties based on queried parameters. */
1139 error
= elan_setup_input_device(data
);
1144 * Platform code (ACPI, DTS) should normally set up interrupt
1145 * for us, but in case it did not let's fall back to using falling
1146 * edge to be compatible with older Chromebooks.
1148 irqflags
= irq_get_trigger_type(client
->irq
);
1150 irqflags
= IRQF_TRIGGER_FALLING
;
1152 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, elan_isr
,
1153 irqflags
| IRQF_ONESHOT
,
1154 client
->name
, data
);
1156 dev_err(dev
, "cannot register irq=%d\n", client
->irq
);
1160 error
= sysfs_create_groups(&dev
->kobj
, elan_sysfs_groups
);
1162 dev_err(dev
, "failed to create sysfs attributes: %d\n", error
);
1166 error
= devm_add_action(dev
, elan_remove_sysfs_groups
, data
);
1168 elan_remove_sysfs_groups(data
);
1169 dev_err(dev
, "Failed to add sysfs cleanup action: %d\n",
1174 error
= input_register_device(data
->input
);
1176 dev_err(dev
, "failed to register input device: %d\n", error
);
1181 * Systems using device tree should set up wakeup via DTS,
1182 * the rest will configure device as wakeup source by default.
1185 device_init_wakeup(dev
, true);
1190 static int __maybe_unused
elan_suspend(struct device
*dev
)
1192 struct i2c_client
*client
= to_i2c_client(dev
);
1193 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1197 * We are taking the mutex to make sure sysfs operations are
1198 * complete before we attempt to bring the device into low[er]
1201 ret
= mutex_lock_interruptible(&data
->sysfs_mutex
);
1205 disable_irq(client
->irq
);
1207 if (device_may_wakeup(dev
)) {
1208 ret
= elan_sleep(data
);
1209 /* Enable wake from IRQ */
1210 data
->irq_wake
= (enable_irq_wake(client
->irq
) == 0);
1212 ret
= elan_disable_power(data
);
1215 mutex_unlock(&data
->sysfs_mutex
);
1219 static int __maybe_unused
elan_resume(struct device
*dev
)
1221 struct i2c_client
*client
= to_i2c_client(dev
);
1222 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1225 if (device_may_wakeup(dev
) && data
->irq_wake
) {
1226 disable_irq_wake(client
->irq
);
1227 data
->irq_wake
= false;
1230 error
= elan_enable_power(data
);
1232 dev_err(dev
, "power up when resuming failed: %d\n", error
);
1236 error
= elan_initialize(data
);
1238 dev_err(dev
, "initialize when resuming failed: %d\n", error
);
1241 enable_irq(data
->client
->irq
);
1245 static SIMPLE_DEV_PM_OPS(elan_pm_ops
, elan_suspend
, elan_resume
);
1247 static const struct i2c_device_id elan_id
[] = {
1251 MODULE_DEVICE_TABLE(i2c
, elan_id
);
1254 static const struct acpi_device_id elan_acpi_id
[] = {
1268 MODULE_DEVICE_TABLE(acpi
, elan_acpi_id
);
1272 static const struct of_device_id elan_of_match
[] = {
1273 { .compatible
= "elan,ekth3000" },
1276 MODULE_DEVICE_TABLE(of
, elan_of_match
);
1279 static struct i2c_driver elan_driver
= {
1281 .name
= DRIVER_NAME
,
1283 .acpi_match_table
= ACPI_PTR(elan_acpi_id
),
1284 .of_match_table
= of_match_ptr(elan_of_match
),
1285 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1287 .probe
= elan_probe
,
1288 .id_table
= elan_id
,
1291 module_i2c_driver(elan_driver
);
1293 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1294 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1295 MODULE_LICENSE("GPL");