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/module.h>
30 #include <linux/slab.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/input.h>
34 #include <linux/uaccess.h>
35 #include <linux/jiffies.h>
36 #include <linux/completion.h>
38 #include <linux/regulator/consumer.h>
39 #include <asm/unaligned.h>
43 #define DRIVER_NAME "elan_i2c"
44 #define ELAN_DRIVER_VERSION "1.6.2"
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
;
86 int pressure_adjustment
;
89 u16 fw_validpage_count
;
90 u16 fw_signature_address
;
99 static int elan_get_fwinfo(u8 iap_version
, u16
*validpage_count
,
100 u16
*signature_address
)
102 switch (iap_version
) {
106 *validpage_count
= 512;
114 *validpage_count
= 768;
117 *validpage_count
= 896;
120 *validpage_count
= 640;
123 /* unknown ic type clear value */
124 *validpage_count
= 0;
125 *signature_address
= 0;
130 (*validpage_count
* ETP_FW_PAGE_SIZE
) - ETP_FW_SIGNATURE_SIZE
;
135 static int elan_enable_power(struct elan_tp_data
*data
)
137 int repeat
= ETP_RETRY_COUNT
;
140 error
= regulator_enable(data
->vcc
);
142 dev_err(&data
->client
->dev
,
143 "failed to enable regulator: %d\n", error
);
148 error
= data
->ops
->power_control(data
->client
, true);
153 } while (--repeat
> 0);
155 dev_err(&data
->client
->dev
, "failed to enable power: %d\n", error
);
159 static int elan_disable_power(struct elan_tp_data
*data
)
161 int repeat
= ETP_RETRY_COUNT
;
165 error
= data
->ops
->power_control(data
->client
, false);
167 error
= regulator_disable(data
->vcc
);
169 dev_err(&data
->client
->dev
,
170 "failed to disable regulator: %d\n",
172 /* Attempt to power the chip back up */
173 data
->ops
->power_control(data
->client
, true);
181 } while (--repeat
> 0);
183 dev_err(&data
->client
->dev
, "failed to disable power: %d\n", error
);
187 static int elan_sleep(struct elan_tp_data
*data
)
189 int repeat
= ETP_RETRY_COUNT
;
193 error
= data
->ops
->sleep_control(data
->client
, true);
198 } while (--repeat
> 0);
203 static int elan_query_product(struct elan_tp_data
*data
)
207 error
= data
->ops
->get_product_id(data
->client
, &data
->product_id
);
211 error
= data
->ops
->get_sm_version(data
->client
, &data
->ic_type
,
219 static int elan_check_ASUS_special_fw(struct elan_tp_data
*data
)
221 if (data
->ic_type
!= 0x0E)
224 switch (data
->product_id
) {
234 static int __elan_initialize(struct elan_tp_data
*data
)
236 struct i2c_client
*client
= data
->client
;
237 bool woken_up
= false;
240 error
= data
->ops
->initialize(client
);
242 dev_err(&client
->dev
, "device initialize failed: %d\n", error
);
246 error
= elan_query_product(data
);
251 * Some ASUS devices were shipped with firmware that requires
252 * touchpads to be woken up first, before attempting to switch
253 * them into absolute reporting mode.
255 if (elan_check_ASUS_special_fw(data
)) {
256 error
= data
->ops
->sleep_control(client
, false);
258 dev_err(&client
->dev
,
259 "failed to wake device up: %d\n", error
);
267 data
->mode
|= ETP_ENABLE_ABS
;
268 error
= data
->ops
->set_mode(client
, data
->mode
);
270 dev_err(&client
->dev
,
271 "failed to switch to absolute mode: %d\n", error
);
276 error
= data
->ops
->sleep_control(client
, false);
278 dev_err(&client
->dev
,
279 "failed to wake device up: %d\n", error
);
287 static int elan_initialize(struct elan_tp_data
*data
)
289 int repeat
= ETP_RETRY_COUNT
;
293 error
= __elan_initialize(data
);
298 } while (--repeat
> 0);
303 static int elan_query_device_info(struct elan_tp_data
*data
)
307 error
= data
->ops
->get_version(data
->client
, false, &data
->fw_version
);
311 error
= data
->ops
->get_checksum(data
->client
, false,
316 error
= data
->ops
->get_version(data
->client
, true, &data
->iap_version
);
320 error
= data
->ops
->get_pressure_adjustment(data
->client
,
321 &data
->pressure_adjustment
);
325 error
= elan_get_fwinfo(data
->iap_version
, &data
->fw_validpage_count
,
326 &data
->fw_signature_address
);
328 dev_warn(&data
->client
->dev
,
329 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
330 data
->iap_version
, data
->ic_type
);
335 static unsigned int elan_convert_resolution(u8 val
)
338 * (value from firmware) * 10 + 790 = dpi
340 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
344 return ((int)(char)val
* 10 + 790) * 10 / 254;
347 static int elan_query_device_parameters(struct elan_tp_data
*data
)
349 unsigned int x_traces
, y_traces
;
350 u8 hw_x_res
, hw_y_res
;
353 error
= data
->ops
->get_max(data
->client
, &data
->max_x
, &data
->max_y
);
357 error
= data
->ops
->get_num_traces(data
->client
, &x_traces
, &y_traces
);
361 data
->width_x
= data
->max_x
/ x_traces
;
362 data
->width_y
= data
->max_y
/ y_traces
;
364 error
= data
->ops
->get_resolution(data
->client
, &hw_x_res
, &hw_y_res
);
368 data
->x_res
= elan_convert_resolution(hw_x_res
);
369 data
->y_res
= elan_convert_resolution(hw_y_res
);
375 **********************************************************
376 * IAP firmware updater related routines
377 **********************************************************
379 static int elan_write_fw_block(struct elan_tp_data
*data
,
380 const u8
*page
, u16 checksum
, int idx
)
382 int retry
= ETP_RETRY_COUNT
;
386 error
= data
->ops
->write_fw_block(data
->client
,
387 page
, checksum
, idx
);
391 dev_dbg(&data
->client
->dev
,
392 "IAP retrying page %d (error: %d)\n", idx
, error
);
393 } while (--retry
> 0);
398 static int __elan_update_firmware(struct elan_tp_data
*data
,
399 const struct firmware
*fw
)
401 struct i2c_client
*client
= data
->client
;
402 struct device
*dev
= &client
->dev
;
407 u16 sw_checksum
= 0, fw_checksum
= 0;
409 error
= data
->ops
->prepare_fw_update(client
);
413 iap_start_addr
= get_unaligned_le16(&fw
->data
[ETP_IAP_START_ADDR
* 2]);
415 boot_page_count
= (iap_start_addr
* 2) / ETP_FW_PAGE_SIZE
;
416 for (i
= boot_page_count
; i
< data
->fw_validpage_count
; i
++) {
418 const u8
*page
= &fw
->data
[i
* ETP_FW_PAGE_SIZE
];
420 for (j
= 0; j
< ETP_FW_PAGE_SIZE
; j
+= 2)
421 checksum
+= ((page
[j
+ 1] << 8) | page
[j
]);
423 error
= elan_write_fw_block(data
, page
, checksum
, i
);
425 dev_err(dev
, "write page %d fail: %d\n", i
, error
);
429 sw_checksum
+= checksum
;
432 /* Wait WDT reset and power on reset */
435 error
= data
->ops
->finish_fw_update(client
, &data
->fw_completion
);
439 error
= data
->ops
->get_checksum(client
, true, &fw_checksum
);
443 if (sw_checksum
!= fw_checksum
) {
444 dev_err(dev
, "checksum diff sw=[%04X], fw=[%04X]\n",
445 sw_checksum
, fw_checksum
);
452 static int elan_update_firmware(struct elan_tp_data
*data
,
453 const struct firmware
*fw
)
455 struct i2c_client
*client
= data
->client
;
458 dev_dbg(&client
->dev
, "Starting firmware update....\n");
460 disable_irq(client
->irq
);
461 data
->in_fw_update
= true;
463 retval
= __elan_update_firmware(data
, fw
);
465 dev_err(&client
->dev
, "firmware update failed: %d\n", retval
);
466 data
->ops
->iap_reset(client
);
468 /* Reinitialize TP after fw is updated */
469 elan_initialize(data
);
470 elan_query_device_info(data
);
473 data
->in_fw_update
= false;
474 enable_irq(client
->irq
);
480 *******************************************************************
482 *******************************************************************
484 static ssize_t
elan_sysfs_read_fw_checksum(struct device
*dev
,
485 struct device_attribute
*attr
,
488 struct i2c_client
*client
= to_i2c_client(dev
);
489 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
491 return sprintf(buf
, "0x%04x\n", data
->fw_checksum
);
494 static ssize_t
elan_sysfs_read_product_id(struct device
*dev
,
495 struct device_attribute
*attr
,
498 struct i2c_client
*client
= to_i2c_client(dev
);
499 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
501 return sprintf(buf
, ETP_PRODUCT_ID_FORMAT_STRING
"\n",
505 static ssize_t
elan_sysfs_read_fw_ver(struct device
*dev
,
506 struct device_attribute
*attr
,
509 struct i2c_client
*client
= to_i2c_client(dev
);
510 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
512 return sprintf(buf
, "%d.0\n", data
->fw_version
);
515 static ssize_t
elan_sysfs_read_sm_ver(struct device
*dev
,
516 struct device_attribute
*attr
,
519 struct i2c_client
*client
= to_i2c_client(dev
);
520 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
522 return sprintf(buf
, "%d.0\n", data
->sm_version
);
525 static ssize_t
elan_sysfs_read_iap_ver(struct device
*dev
,
526 struct device_attribute
*attr
,
529 struct i2c_client
*client
= to_i2c_client(dev
);
530 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
532 return sprintf(buf
, "%d.0\n", data
->iap_version
);
535 static ssize_t
elan_sysfs_update_fw(struct device
*dev
,
536 struct device_attribute
*attr
,
537 const char *buf
, size_t count
)
539 struct elan_tp_data
*data
= dev_get_drvdata(dev
);
540 const struct firmware
*fw
;
543 const u8
*fw_signature
;
544 static const u8 signature
[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
546 if (data
->fw_validpage_count
== 0)
549 /* Look for a firmware with the product id appended. */
550 fw_name
= kasprintf(GFP_KERNEL
, ETP_FW_NAME
, data
->product_id
);
552 dev_err(dev
, "failed to allocate memory for firmware name\n");
556 dev_info(dev
, "requesting fw '%s'\n", fw_name
);
557 error
= request_firmware(&fw
, fw_name
, dev
);
560 dev_err(dev
, "failed to request firmware: %d\n", error
);
564 /* Firmware file must match signature data */
565 fw_signature
= &fw
->data
[data
->fw_signature_address
];
566 if (memcmp(fw_signature
, signature
, sizeof(signature
)) != 0) {
567 dev_err(dev
, "signature mismatch (expected %*ph, got %*ph)\n",
568 (int)sizeof(signature
), signature
,
569 (int)sizeof(signature
), fw_signature
);
574 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
578 error
= elan_update_firmware(data
, fw
);
580 mutex_unlock(&data
->sysfs_mutex
);
583 release_firmware(fw
);
584 return error
?: count
;
587 static ssize_t
calibrate_store(struct device
*dev
,
588 struct device_attribute
*attr
,
589 const char *buf
, size_t count
)
591 struct i2c_client
*client
= to_i2c_client(dev
);
592 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
598 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
602 disable_irq(client
->irq
);
604 data
->mode
|= ETP_ENABLE_CALIBRATE
;
605 retval
= data
->ops
->set_mode(client
, data
->mode
);
607 dev_err(dev
, "failed to enable calibration mode: %d\n",
612 retval
= data
->ops
->calibrate(client
);
614 dev_err(dev
, "failed to start calibration: %d\n",
616 goto out_disable_calibrate
;
621 /* Wait 250ms before checking if calibration has completed. */
624 retval
= data
->ops
->calibrate_result(client
, val
);
626 dev_err(dev
, "failed to check calibration result: %d\n",
628 else if (val
[0] == 0)
629 break; /* calibration done */
634 dev_err(dev
, "failed to calibrate. Timeout.\n");
638 out_disable_calibrate
:
639 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
640 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
642 dev_err(dev
, "failed to disable calibration mode: %d\n",
648 enable_irq(client
->irq
);
649 mutex_unlock(&data
->sysfs_mutex
);
650 return retval
?: count
;
653 static ssize_t
elan_sysfs_read_mode(struct device
*dev
,
654 struct device_attribute
*attr
,
657 struct i2c_client
*client
= to_i2c_client(dev
);
658 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
662 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
666 error
= data
->ops
->iap_get_mode(data
->client
, &mode
);
668 mutex_unlock(&data
->sysfs_mutex
);
673 return sprintf(buf
, "%d\n", (int)mode
);
676 static DEVICE_ATTR(product_id
, S_IRUGO
, elan_sysfs_read_product_id
, NULL
);
677 static DEVICE_ATTR(firmware_version
, S_IRUGO
, elan_sysfs_read_fw_ver
, NULL
);
678 static DEVICE_ATTR(sample_version
, S_IRUGO
, elan_sysfs_read_sm_ver
, NULL
);
679 static DEVICE_ATTR(iap_version
, S_IRUGO
, elan_sysfs_read_iap_ver
, NULL
);
680 static DEVICE_ATTR(fw_checksum
, S_IRUGO
, elan_sysfs_read_fw_checksum
, NULL
);
681 static DEVICE_ATTR(mode
, S_IRUGO
, elan_sysfs_read_mode
, NULL
);
682 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, elan_sysfs_update_fw
);
684 static DEVICE_ATTR_WO(calibrate
);
686 static struct attribute
*elan_sysfs_entries
[] = {
687 &dev_attr_product_id
.attr
,
688 &dev_attr_firmware_version
.attr
,
689 &dev_attr_sample_version
.attr
,
690 &dev_attr_iap_version
.attr
,
691 &dev_attr_fw_checksum
.attr
,
692 &dev_attr_calibrate
.attr
,
694 &dev_attr_update_fw
.attr
,
698 static const struct attribute_group elan_sysfs_group
= {
699 .attrs
= elan_sysfs_entries
,
702 static ssize_t
acquire_store(struct device
*dev
, struct device_attribute
*attr
,
703 const char *buf
, size_t count
)
705 struct i2c_client
*client
= to_i2c_client(dev
);
706 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
710 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
714 disable_irq(client
->irq
);
716 data
->baseline_ready
= false;
718 data
->mode
|= ETP_ENABLE_CALIBRATE
;
719 retval
= data
->ops
->set_mode(data
->client
, data
->mode
);
721 dev_err(dev
, "Failed to enable calibration mode to get baseline: %d\n",
728 retval
= data
->ops
->get_baseline_data(data
->client
, true,
729 &data
->max_baseline
);
731 dev_err(dev
, "Failed to read max baseline form device: %d\n",
733 goto out_disable_calibrate
;
736 retval
= data
->ops
->get_baseline_data(data
->client
, false,
737 &data
->min_baseline
);
739 dev_err(dev
, "Failed to read min baseline form device: %d\n",
741 goto out_disable_calibrate
;
744 data
->baseline_ready
= true;
746 out_disable_calibrate
:
747 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
748 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
750 dev_err(dev
, "Failed to disable calibration mode after acquiring baseline: %d\n",
756 enable_irq(client
->irq
);
757 mutex_unlock(&data
->sysfs_mutex
);
758 return retval
?: count
;
761 static ssize_t
min_show(struct device
*dev
,
762 struct device_attribute
*attr
, char *buf
)
764 struct i2c_client
*client
= to_i2c_client(dev
);
765 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
768 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
772 if (!data
->baseline_ready
) {
777 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->min_baseline
);
780 mutex_unlock(&data
->sysfs_mutex
);
784 static ssize_t
max_show(struct device
*dev
,
785 struct device_attribute
*attr
, char *buf
)
787 struct i2c_client
*client
= to_i2c_client(dev
);
788 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
791 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
795 if (!data
->baseline_ready
) {
800 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->max_baseline
);
803 mutex_unlock(&data
->sysfs_mutex
);
808 static DEVICE_ATTR_WO(acquire
);
809 static DEVICE_ATTR_RO(min
);
810 static DEVICE_ATTR_RO(max
);
812 static struct attribute
*elan_baseline_sysfs_entries
[] = {
813 &dev_attr_acquire
.attr
,
819 static const struct attribute_group elan_baseline_sysfs_group
= {
821 .attrs
= elan_baseline_sysfs_entries
,
824 static const struct attribute_group
*elan_sysfs_groups
[] = {
826 &elan_baseline_sysfs_group
,
831 ******************************************************************
833 ******************************************************************
835 static void elan_report_contact(struct elan_tp_data
*data
,
836 int contact_num
, bool contact_valid
,
839 struct input_dev
*input
= data
->input
;
840 unsigned int pos_x
, pos_y
;
841 unsigned int pressure
, mk_x
, mk_y
;
842 unsigned int area_x
, area_y
, major
, minor
;
843 unsigned int scaled_pressure
;
846 pos_x
= ((finger_data
[0] & 0xf0) << 4) |
848 pos_y
= ((finger_data
[0] & 0x0f) << 8) |
850 mk_x
= (finger_data
[3] & 0x0f);
851 mk_y
= (finger_data
[3] >> 4);
852 pressure
= finger_data
[4];
854 if (pos_x
> data
->max_x
|| pos_y
> data
->max_y
) {
855 dev_dbg(input
->dev
.parent
,
856 "[%d] x=%d y=%d over max (%d, %d)",
857 contact_num
, pos_x
, pos_y
,
858 data
->max_x
, data
->max_y
);
863 * To avoid treating large finger as palm, let's reduce the
864 * width x and y per trace.
866 area_x
= mk_x
* (data
->width_x
- ETP_FWIDTH_REDUCE
);
867 area_y
= mk_y
* (data
->width_y
- ETP_FWIDTH_REDUCE
);
869 major
= max(area_x
, area_y
);
870 minor
= min(area_x
, area_y
);
872 scaled_pressure
= pressure
+ data
->pressure_adjustment
;
874 if (scaled_pressure
> ETP_MAX_PRESSURE
)
875 scaled_pressure
= ETP_MAX_PRESSURE
;
877 input_mt_slot(input
, contact_num
);
878 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
879 input_report_abs(input
, ABS_MT_POSITION_X
, pos_x
);
880 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->max_y
- pos_y
);
881 input_report_abs(input
, ABS_MT_PRESSURE
, scaled_pressure
);
882 input_report_abs(input
, ABS_TOOL_WIDTH
, mk_x
);
883 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, major
);
884 input_report_abs(input
, ABS_MT_TOUCH_MINOR
, minor
);
886 input_mt_slot(input
, contact_num
);
887 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, false);
891 static void elan_report_absolute(struct elan_tp_data
*data
, u8
*packet
)
893 struct input_dev
*input
= data
->input
;
894 u8
*finger_data
= &packet
[ETP_FINGER_DATA_OFFSET
];
896 u8 tp_info
= packet
[ETP_TOUCH_INFO_OFFSET
];
897 u8 hover_info
= packet
[ETP_HOVER_INFO_OFFSET
];
898 bool contact_valid
, hover_event
;
900 hover_event
= hover_info
& 0x40;
901 for (i
= 0; i
< ETP_MAX_FINGERS
; i
++) {
902 contact_valid
= tp_info
& (1U << (3 + i
));
903 elan_report_contact(data
, i
, contact_valid
, finger_data
);
906 finger_data
+= ETP_FINGER_DATA_LEN
;
909 input_report_key(input
, BTN_LEFT
, tp_info
& 0x01);
910 input_report_abs(input
, ABS_DISTANCE
, hover_event
!= 0);
911 input_mt_report_pointer_emulation(input
, true);
915 static irqreturn_t
elan_isr(int irq
, void *dev_id
)
917 struct elan_tp_data
*data
= dev_id
;
918 struct device
*dev
= &data
->client
->dev
;
920 u8 report
[ETP_MAX_REPORT_LEN
];
923 * When device is connected to i2c bus, when all IAP page writes
924 * complete, the driver will receive interrupt and must read
925 * 0000 to confirm that IAP is finished.
927 if (data
->in_fw_update
) {
928 complete(&data
->fw_completion
);
932 error
= data
->ops
->get_report(data
->client
, report
);
936 if (report
[ETP_REPORT_ID_OFFSET
] != ETP_REPORT_ID
)
937 dev_err(dev
, "invalid report id data (%x)\n",
938 report
[ETP_REPORT_ID_OFFSET
]);
940 elan_report_absolute(data
, report
);
947 ******************************************************************
948 * Elan initialization functions
949 ******************************************************************
951 static int elan_setup_input_device(struct elan_tp_data
*data
)
953 struct device
*dev
= &data
->client
->dev
;
954 struct input_dev
*input
;
955 unsigned int max_width
= max(data
->width_x
, data
->width_y
);
956 unsigned int min_width
= min(data
->width_x
, data
->width_y
);
959 input
= devm_input_allocate_device(dev
);
963 input
->name
= "Elan Touchpad";
964 input
->id
.bustype
= BUS_I2C
;
965 input
->id
.vendor
= ELAN_VENDOR_ID
;
966 input
->id
.product
= data
->product_id
;
967 input_set_drvdata(input
, data
);
969 error
= input_mt_init_slots(input
, ETP_MAX_FINGERS
,
970 INPUT_MT_POINTER
| INPUT_MT_DROP_UNUSED
);
972 dev_err(dev
, "failed to initialize MT slots: %d\n", error
);
976 __set_bit(EV_ABS
, input
->evbit
);
977 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
978 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
979 __set_bit(BTN_LEFT
, input
->keybit
);
981 /* Set up ST parameters */
982 input_set_abs_params(input
, ABS_X
, 0, data
->max_x
, 0, 0);
983 input_set_abs_params(input
, ABS_Y
, 0, data
->max_y
, 0, 0);
984 input_abs_set_res(input
, ABS_X
, data
->x_res
);
985 input_abs_set_res(input
, ABS_Y
, data
->y_res
);
986 input_set_abs_params(input
, ABS_PRESSURE
, 0, ETP_MAX_PRESSURE
, 0, 0);
987 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0, ETP_FINGER_WIDTH
, 0, 0);
988 input_set_abs_params(input
, ABS_DISTANCE
, 0, 1, 0, 0);
990 /* And MT parameters */
991 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, data
->max_x
, 0, 0);
992 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, data
->max_y
, 0, 0);
993 input_abs_set_res(input
, ABS_MT_POSITION_X
, data
->x_res
);
994 input_abs_set_res(input
, ABS_MT_POSITION_Y
, data
->y_res
);
995 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0,
996 ETP_MAX_PRESSURE
, 0, 0);
997 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0,
998 ETP_FINGER_WIDTH
* max_width
, 0, 0);
999 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0,
1000 ETP_FINGER_WIDTH
* min_width
, 0, 0);
1002 data
->input
= input
;
1007 static void elan_disable_regulator(void *_data
)
1009 struct elan_tp_data
*data
= _data
;
1011 regulator_disable(data
->vcc
);
1014 static void elan_remove_sysfs_groups(void *_data
)
1016 struct elan_tp_data
*data
= _data
;
1018 sysfs_remove_groups(&data
->client
->dev
.kobj
, elan_sysfs_groups
);
1021 static int elan_probe(struct i2c_client
*client
,
1022 const struct i2c_device_id
*dev_id
)
1024 const struct elan_transport_ops
*transport_ops
;
1025 struct device
*dev
= &client
->dev
;
1026 struct elan_tp_data
*data
;
1027 unsigned long irqflags
;
1030 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C
) &&
1031 i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1032 transport_ops
= &elan_i2c_ops
;
1033 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS
) &&
1034 i2c_check_functionality(client
->adapter
,
1035 I2C_FUNC_SMBUS_BYTE_DATA
|
1036 I2C_FUNC_SMBUS_BLOCK_DATA
|
1037 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1038 transport_ops
= &elan_smbus_ops
;
1040 dev_err(dev
, "not a supported I2C/SMBus adapter\n");
1044 data
= devm_kzalloc(&client
->dev
, sizeof(struct elan_tp_data
),
1049 i2c_set_clientdata(client
, data
);
1051 data
->ops
= transport_ops
;
1052 data
->client
= client
;
1053 init_completion(&data
->fw_completion
);
1054 mutex_init(&data
->sysfs_mutex
);
1056 data
->vcc
= devm_regulator_get(&client
->dev
, "vcc");
1057 if (IS_ERR(data
->vcc
)) {
1058 error
= PTR_ERR(data
->vcc
);
1059 if (error
!= -EPROBE_DEFER
)
1060 dev_err(&client
->dev
,
1061 "Failed to get 'vcc' regulator: %d\n",
1066 error
= regulator_enable(data
->vcc
);
1068 dev_err(&client
->dev
,
1069 "Failed to enable regulator: %d\n", error
);
1073 error
= devm_add_action(&client
->dev
,
1074 elan_disable_regulator
, data
);
1076 regulator_disable(data
->vcc
);
1077 dev_err(&client
->dev
,
1078 "Failed to add disable regulator action: %d\n",
1083 /* Initialize the touchpad. */
1084 error
= elan_initialize(data
);
1088 error
= elan_query_device_info(data
);
1092 error
= elan_query_device_parameters(data
);
1096 dev_dbg(&client
->dev
,
1097 "Elan Touchpad Information:\n"
1098 " Module product ID: 0x%04x\n"
1099 " Firmware Version: 0x%04x\n"
1100 " Sample Version: 0x%04x\n"
1101 " IAP Version: 0x%04x\n"
1102 " Max ABS X,Y: %d,%d\n"
1103 " Width X,Y: %d,%d\n"
1104 " Resolution X,Y: %d,%d (dots/mm)\n",
1109 data
->max_x
, data
->max_y
,
1110 data
->width_x
, data
->width_y
,
1111 data
->x_res
, data
->y_res
);
1113 /* Set up input device properties based on queried parameters. */
1114 error
= elan_setup_input_device(data
);
1119 * Systems using device tree should set up interrupt via DTS,
1120 * the rest will use the default falling edge interrupts.
1122 irqflags
= client
->dev
.of_node
? 0 : IRQF_TRIGGER_FALLING
;
1124 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1126 irqflags
| IRQF_ONESHOT
,
1127 client
->name
, data
);
1129 dev_err(&client
->dev
, "cannot register irq=%d\n", client
->irq
);
1133 error
= sysfs_create_groups(&client
->dev
.kobj
, elan_sysfs_groups
);
1135 dev_err(&client
->dev
, "failed to create sysfs attributes: %d\n",
1140 error
= devm_add_action(&client
->dev
,
1141 elan_remove_sysfs_groups
, data
);
1143 elan_remove_sysfs_groups(data
);
1144 dev_err(&client
->dev
,
1145 "Failed to add sysfs cleanup action: %d\n",
1150 error
= input_register_device(data
->input
);
1152 dev_err(&client
->dev
, "failed to register input device: %d\n",
1158 * Systems using device tree should set up wakeup via DTS,
1159 * the rest will configure device as wakeup source by default.
1161 if (!client
->dev
.of_node
)
1162 device_init_wakeup(&client
->dev
, true);
1167 static int __maybe_unused
elan_suspend(struct device
*dev
)
1169 struct i2c_client
*client
= to_i2c_client(dev
);
1170 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1174 * We are taking the mutex to make sure sysfs operations are
1175 * complete before we attempt to bring the device into low[er]
1178 ret
= mutex_lock_interruptible(&data
->sysfs_mutex
);
1182 disable_irq(client
->irq
);
1184 if (device_may_wakeup(dev
)) {
1185 ret
= elan_sleep(data
);
1186 /* Enable wake from IRQ */
1187 data
->irq_wake
= (enable_irq_wake(client
->irq
) == 0);
1189 ret
= elan_disable_power(data
);
1192 mutex_unlock(&data
->sysfs_mutex
);
1196 static int __maybe_unused
elan_resume(struct device
*dev
)
1198 struct i2c_client
*client
= to_i2c_client(dev
);
1199 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1202 if (device_may_wakeup(dev
) && data
->irq_wake
) {
1203 disable_irq_wake(client
->irq
);
1204 data
->irq_wake
= false;
1207 error
= elan_enable_power(data
);
1209 dev_err(dev
, "power up when resuming failed: %d\n", error
);
1213 error
= elan_initialize(data
);
1215 dev_err(dev
, "initialize when resuming failed: %d\n", error
);
1218 enable_irq(data
->client
->irq
);
1222 static SIMPLE_DEV_PM_OPS(elan_pm_ops
, elan_suspend
, elan_resume
);
1224 static const struct i2c_device_id elan_id
[] = {
1228 MODULE_DEVICE_TABLE(i2c
, elan_id
);
1231 static const struct acpi_device_id elan_acpi_id
[] = {
1238 MODULE_DEVICE_TABLE(acpi
, elan_acpi_id
);
1242 static const struct of_device_id elan_of_match
[] = {
1243 { .compatible
= "elan,ekth3000" },
1246 MODULE_DEVICE_TABLE(of
, elan_of_match
);
1249 static struct i2c_driver elan_driver
= {
1251 .name
= DRIVER_NAME
,
1253 .acpi_match_table
= ACPI_PTR(elan_acpi_id
),
1254 .of_match_table
= of_match_ptr(elan_of_match
),
1255 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1257 .probe
= elan_probe
,
1258 .id_table
= elan_id
,
1261 module_i2c_driver(elan_driver
);
1263 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1264 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1265 MODULE_LICENSE("GPL");
1266 MODULE_VERSION(ELAN_DRIVER_VERSION
);