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) {
222 switch (data
->product_id
) {
228 } else if (data
->ic_type
== 0x08 && data
->product_id
== 0x26) {
229 /* ASUS EeeBook X205TA */
236 static int __elan_initialize(struct elan_tp_data
*data
)
238 struct i2c_client
*client
= data
->client
;
239 bool woken_up
= false;
242 error
= data
->ops
->initialize(client
);
244 dev_err(&client
->dev
, "device initialize failed: %d\n", error
);
248 error
= elan_query_product(data
);
253 * Some ASUS devices were shipped with firmware that requires
254 * touchpads to be woken up first, before attempting to switch
255 * them into absolute reporting mode.
257 if (elan_check_ASUS_special_fw(data
)) {
258 error
= data
->ops
->sleep_control(client
, false);
260 dev_err(&client
->dev
,
261 "failed to wake device up: %d\n", error
);
269 data
->mode
|= ETP_ENABLE_ABS
;
270 error
= data
->ops
->set_mode(client
, data
->mode
);
272 dev_err(&client
->dev
,
273 "failed to switch to absolute mode: %d\n", error
);
278 error
= data
->ops
->sleep_control(client
, false);
280 dev_err(&client
->dev
,
281 "failed to wake device up: %d\n", error
);
289 static int elan_initialize(struct elan_tp_data
*data
)
291 int repeat
= ETP_RETRY_COUNT
;
295 error
= __elan_initialize(data
);
300 } while (--repeat
> 0);
305 static int elan_query_device_info(struct elan_tp_data
*data
)
309 error
= data
->ops
->get_version(data
->client
, false, &data
->fw_version
);
313 error
= data
->ops
->get_checksum(data
->client
, false,
318 error
= data
->ops
->get_version(data
->client
, true, &data
->iap_version
);
322 error
= data
->ops
->get_pressure_adjustment(data
->client
,
323 &data
->pressure_adjustment
);
327 error
= elan_get_fwinfo(data
->iap_version
, &data
->fw_validpage_count
,
328 &data
->fw_signature_address
);
330 dev_warn(&data
->client
->dev
,
331 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
332 data
->iap_version
, data
->ic_type
);
337 static unsigned int elan_convert_resolution(u8 val
)
340 * (value from firmware) * 10 + 790 = dpi
342 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
346 return ((int)(char)val
* 10 + 790) * 10 / 254;
349 static int elan_query_device_parameters(struct elan_tp_data
*data
)
351 unsigned int x_traces
, y_traces
;
352 u8 hw_x_res
, hw_y_res
;
355 error
= data
->ops
->get_max(data
->client
, &data
->max_x
, &data
->max_y
);
359 error
= data
->ops
->get_num_traces(data
->client
, &x_traces
, &y_traces
);
363 data
->width_x
= data
->max_x
/ x_traces
;
364 data
->width_y
= data
->max_y
/ y_traces
;
366 error
= data
->ops
->get_resolution(data
->client
, &hw_x_res
, &hw_y_res
);
370 data
->x_res
= elan_convert_resolution(hw_x_res
);
371 data
->y_res
= elan_convert_resolution(hw_y_res
);
377 **********************************************************
378 * IAP firmware updater related routines
379 **********************************************************
381 static int elan_write_fw_block(struct elan_tp_data
*data
,
382 const u8
*page
, u16 checksum
, int idx
)
384 int retry
= ETP_RETRY_COUNT
;
388 error
= data
->ops
->write_fw_block(data
->client
,
389 page
, checksum
, idx
);
393 dev_dbg(&data
->client
->dev
,
394 "IAP retrying page %d (error: %d)\n", idx
, error
);
395 } while (--retry
> 0);
400 static int __elan_update_firmware(struct elan_tp_data
*data
,
401 const struct firmware
*fw
)
403 struct i2c_client
*client
= data
->client
;
404 struct device
*dev
= &client
->dev
;
409 u16 sw_checksum
= 0, fw_checksum
= 0;
411 error
= data
->ops
->prepare_fw_update(client
);
415 iap_start_addr
= get_unaligned_le16(&fw
->data
[ETP_IAP_START_ADDR
* 2]);
417 boot_page_count
= (iap_start_addr
* 2) / ETP_FW_PAGE_SIZE
;
418 for (i
= boot_page_count
; i
< data
->fw_validpage_count
; i
++) {
420 const u8
*page
= &fw
->data
[i
* ETP_FW_PAGE_SIZE
];
422 for (j
= 0; j
< ETP_FW_PAGE_SIZE
; j
+= 2)
423 checksum
+= ((page
[j
+ 1] << 8) | page
[j
]);
425 error
= elan_write_fw_block(data
, page
, checksum
, i
);
427 dev_err(dev
, "write page %d fail: %d\n", i
, error
);
431 sw_checksum
+= checksum
;
434 /* Wait WDT reset and power on reset */
437 error
= data
->ops
->finish_fw_update(client
, &data
->fw_completion
);
441 error
= data
->ops
->get_checksum(client
, true, &fw_checksum
);
445 if (sw_checksum
!= fw_checksum
) {
446 dev_err(dev
, "checksum diff sw=[%04X], fw=[%04X]\n",
447 sw_checksum
, fw_checksum
);
454 static int elan_update_firmware(struct elan_tp_data
*data
,
455 const struct firmware
*fw
)
457 struct i2c_client
*client
= data
->client
;
460 dev_dbg(&client
->dev
, "Starting firmware update....\n");
462 disable_irq(client
->irq
);
463 data
->in_fw_update
= true;
465 retval
= __elan_update_firmware(data
, fw
);
467 dev_err(&client
->dev
, "firmware update failed: %d\n", retval
);
468 data
->ops
->iap_reset(client
);
470 /* Reinitialize TP after fw is updated */
471 elan_initialize(data
);
472 elan_query_device_info(data
);
475 data
->in_fw_update
= false;
476 enable_irq(client
->irq
);
482 *******************************************************************
484 *******************************************************************
486 static ssize_t
elan_sysfs_read_fw_checksum(struct device
*dev
,
487 struct device_attribute
*attr
,
490 struct i2c_client
*client
= to_i2c_client(dev
);
491 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
493 return sprintf(buf
, "0x%04x\n", data
->fw_checksum
);
496 static ssize_t
elan_sysfs_read_product_id(struct device
*dev
,
497 struct device_attribute
*attr
,
500 struct i2c_client
*client
= to_i2c_client(dev
);
501 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
503 return sprintf(buf
, ETP_PRODUCT_ID_FORMAT_STRING
"\n",
507 static ssize_t
elan_sysfs_read_fw_ver(struct device
*dev
,
508 struct device_attribute
*attr
,
511 struct i2c_client
*client
= to_i2c_client(dev
);
512 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
514 return sprintf(buf
, "%d.0\n", data
->fw_version
);
517 static ssize_t
elan_sysfs_read_sm_ver(struct device
*dev
,
518 struct device_attribute
*attr
,
521 struct i2c_client
*client
= to_i2c_client(dev
);
522 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
524 return sprintf(buf
, "%d.0\n", data
->sm_version
);
527 static ssize_t
elan_sysfs_read_iap_ver(struct device
*dev
,
528 struct device_attribute
*attr
,
531 struct i2c_client
*client
= to_i2c_client(dev
);
532 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
534 return sprintf(buf
, "%d.0\n", data
->iap_version
);
537 static ssize_t
elan_sysfs_update_fw(struct device
*dev
,
538 struct device_attribute
*attr
,
539 const char *buf
, size_t count
)
541 struct elan_tp_data
*data
= dev_get_drvdata(dev
);
542 const struct firmware
*fw
;
545 const u8
*fw_signature
;
546 static const u8 signature
[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
548 if (data
->fw_validpage_count
== 0)
551 /* Look for a firmware with the product id appended. */
552 fw_name
= kasprintf(GFP_KERNEL
, ETP_FW_NAME
, data
->product_id
);
554 dev_err(dev
, "failed to allocate memory for firmware name\n");
558 dev_info(dev
, "requesting fw '%s'\n", fw_name
);
559 error
= request_firmware(&fw
, fw_name
, dev
);
562 dev_err(dev
, "failed to request firmware: %d\n", error
);
566 /* Firmware file must match signature data */
567 fw_signature
= &fw
->data
[data
->fw_signature_address
];
568 if (memcmp(fw_signature
, signature
, sizeof(signature
)) != 0) {
569 dev_err(dev
, "signature mismatch (expected %*ph, got %*ph)\n",
570 (int)sizeof(signature
), signature
,
571 (int)sizeof(signature
), fw_signature
);
576 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
580 error
= elan_update_firmware(data
, fw
);
582 mutex_unlock(&data
->sysfs_mutex
);
585 release_firmware(fw
);
586 return error
?: count
;
589 static ssize_t
calibrate_store(struct device
*dev
,
590 struct device_attribute
*attr
,
591 const char *buf
, size_t count
)
593 struct i2c_client
*client
= to_i2c_client(dev
);
594 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
600 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
604 disable_irq(client
->irq
);
606 data
->mode
|= ETP_ENABLE_CALIBRATE
;
607 retval
= data
->ops
->set_mode(client
, data
->mode
);
609 dev_err(dev
, "failed to enable calibration mode: %d\n",
614 retval
= data
->ops
->calibrate(client
);
616 dev_err(dev
, "failed to start calibration: %d\n",
618 goto out_disable_calibrate
;
623 /* Wait 250ms before checking if calibration has completed. */
626 retval
= data
->ops
->calibrate_result(client
, val
);
628 dev_err(dev
, "failed to check calibration result: %d\n",
630 else if (val
[0] == 0)
631 break; /* calibration done */
636 dev_err(dev
, "failed to calibrate. Timeout.\n");
640 out_disable_calibrate
:
641 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
642 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
644 dev_err(dev
, "failed to disable calibration mode: %d\n",
650 enable_irq(client
->irq
);
651 mutex_unlock(&data
->sysfs_mutex
);
652 return retval
?: count
;
655 static ssize_t
elan_sysfs_read_mode(struct device
*dev
,
656 struct device_attribute
*attr
,
659 struct i2c_client
*client
= to_i2c_client(dev
);
660 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
664 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
668 error
= data
->ops
->iap_get_mode(data
->client
, &mode
);
670 mutex_unlock(&data
->sysfs_mutex
);
675 return sprintf(buf
, "%d\n", (int)mode
);
678 static DEVICE_ATTR(product_id
, S_IRUGO
, elan_sysfs_read_product_id
, NULL
);
679 static DEVICE_ATTR(firmware_version
, S_IRUGO
, elan_sysfs_read_fw_ver
, NULL
);
680 static DEVICE_ATTR(sample_version
, S_IRUGO
, elan_sysfs_read_sm_ver
, NULL
);
681 static DEVICE_ATTR(iap_version
, S_IRUGO
, elan_sysfs_read_iap_ver
, NULL
);
682 static DEVICE_ATTR(fw_checksum
, S_IRUGO
, elan_sysfs_read_fw_checksum
, NULL
);
683 static DEVICE_ATTR(mode
, S_IRUGO
, elan_sysfs_read_mode
, NULL
);
684 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, elan_sysfs_update_fw
);
686 static DEVICE_ATTR_WO(calibrate
);
688 static struct attribute
*elan_sysfs_entries
[] = {
689 &dev_attr_product_id
.attr
,
690 &dev_attr_firmware_version
.attr
,
691 &dev_attr_sample_version
.attr
,
692 &dev_attr_iap_version
.attr
,
693 &dev_attr_fw_checksum
.attr
,
694 &dev_attr_calibrate
.attr
,
696 &dev_attr_update_fw
.attr
,
700 static const struct attribute_group elan_sysfs_group
= {
701 .attrs
= elan_sysfs_entries
,
704 static ssize_t
acquire_store(struct device
*dev
, struct device_attribute
*attr
,
705 const char *buf
, size_t count
)
707 struct i2c_client
*client
= to_i2c_client(dev
);
708 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
712 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
716 disable_irq(client
->irq
);
718 data
->baseline_ready
= false;
720 data
->mode
|= ETP_ENABLE_CALIBRATE
;
721 retval
= data
->ops
->set_mode(data
->client
, data
->mode
);
723 dev_err(dev
, "Failed to enable calibration mode to get baseline: %d\n",
730 retval
= data
->ops
->get_baseline_data(data
->client
, true,
731 &data
->max_baseline
);
733 dev_err(dev
, "Failed to read max baseline form device: %d\n",
735 goto out_disable_calibrate
;
738 retval
= data
->ops
->get_baseline_data(data
->client
, false,
739 &data
->min_baseline
);
741 dev_err(dev
, "Failed to read min baseline form device: %d\n",
743 goto out_disable_calibrate
;
746 data
->baseline_ready
= true;
748 out_disable_calibrate
:
749 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
750 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
752 dev_err(dev
, "Failed to disable calibration mode after acquiring baseline: %d\n",
758 enable_irq(client
->irq
);
759 mutex_unlock(&data
->sysfs_mutex
);
760 return retval
?: count
;
763 static ssize_t
min_show(struct device
*dev
,
764 struct device_attribute
*attr
, char *buf
)
766 struct i2c_client
*client
= to_i2c_client(dev
);
767 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
770 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
774 if (!data
->baseline_ready
) {
779 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->min_baseline
);
782 mutex_unlock(&data
->sysfs_mutex
);
786 static ssize_t
max_show(struct device
*dev
,
787 struct device_attribute
*attr
, char *buf
)
789 struct i2c_client
*client
= to_i2c_client(dev
);
790 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
793 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
797 if (!data
->baseline_ready
) {
802 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->max_baseline
);
805 mutex_unlock(&data
->sysfs_mutex
);
810 static DEVICE_ATTR_WO(acquire
);
811 static DEVICE_ATTR_RO(min
);
812 static DEVICE_ATTR_RO(max
);
814 static struct attribute
*elan_baseline_sysfs_entries
[] = {
815 &dev_attr_acquire
.attr
,
821 static const struct attribute_group elan_baseline_sysfs_group
= {
823 .attrs
= elan_baseline_sysfs_entries
,
826 static const struct attribute_group
*elan_sysfs_groups
[] = {
828 &elan_baseline_sysfs_group
,
833 ******************************************************************
835 ******************************************************************
837 static void elan_report_contact(struct elan_tp_data
*data
,
838 int contact_num
, bool contact_valid
,
841 struct input_dev
*input
= data
->input
;
842 unsigned int pos_x
, pos_y
;
843 unsigned int pressure
, mk_x
, mk_y
;
844 unsigned int area_x
, area_y
, major
, minor
;
845 unsigned int scaled_pressure
;
848 pos_x
= ((finger_data
[0] & 0xf0) << 4) |
850 pos_y
= ((finger_data
[0] & 0x0f) << 8) |
852 mk_x
= (finger_data
[3] & 0x0f);
853 mk_y
= (finger_data
[3] >> 4);
854 pressure
= finger_data
[4];
856 if (pos_x
> data
->max_x
|| pos_y
> data
->max_y
) {
857 dev_dbg(input
->dev
.parent
,
858 "[%d] x=%d y=%d over max (%d, %d)",
859 contact_num
, pos_x
, pos_y
,
860 data
->max_x
, data
->max_y
);
865 * To avoid treating large finger as palm, let's reduce the
866 * width x and y per trace.
868 area_x
= mk_x
* (data
->width_x
- ETP_FWIDTH_REDUCE
);
869 area_y
= mk_y
* (data
->width_y
- ETP_FWIDTH_REDUCE
);
871 major
= max(area_x
, area_y
);
872 minor
= min(area_x
, area_y
);
874 scaled_pressure
= pressure
+ data
->pressure_adjustment
;
876 if (scaled_pressure
> ETP_MAX_PRESSURE
)
877 scaled_pressure
= ETP_MAX_PRESSURE
;
879 input_mt_slot(input
, contact_num
);
880 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
881 input_report_abs(input
, ABS_MT_POSITION_X
, pos_x
);
882 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->max_y
- pos_y
);
883 input_report_abs(input
, ABS_MT_PRESSURE
, scaled_pressure
);
884 input_report_abs(input
, ABS_TOOL_WIDTH
, mk_x
);
885 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, major
);
886 input_report_abs(input
, ABS_MT_TOUCH_MINOR
, minor
);
888 input_mt_slot(input
, contact_num
);
889 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, false);
893 static void elan_report_absolute(struct elan_tp_data
*data
, u8
*packet
)
895 struct input_dev
*input
= data
->input
;
896 u8
*finger_data
= &packet
[ETP_FINGER_DATA_OFFSET
];
898 u8 tp_info
= packet
[ETP_TOUCH_INFO_OFFSET
];
899 u8 hover_info
= packet
[ETP_HOVER_INFO_OFFSET
];
900 bool contact_valid
, hover_event
;
902 hover_event
= hover_info
& 0x40;
903 for (i
= 0; i
< ETP_MAX_FINGERS
; i
++) {
904 contact_valid
= tp_info
& (1U << (3 + i
));
905 elan_report_contact(data
, i
, contact_valid
, finger_data
);
908 finger_data
+= ETP_FINGER_DATA_LEN
;
911 input_report_key(input
, BTN_LEFT
, tp_info
& 0x01);
912 input_report_abs(input
, ABS_DISTANCE
, hover_event
!= 0);
913 input_mt_report_pointer_emulation(input
, true);
917 static irqreturn_t
elan_isr(int irq
, void *dev_id
)
919 struct elan_tp_data
*data
= dev_id
;
920 struct device
*dev
= &data
->client
->dev
;
922 u8 report
[ETP_MAX_REPORT_LEN
];
925 * When device is connected to i2c bus, when all IAP page writes
926 * complete, the driver will receive interrupt and must read
927 * 0000 to confirm that IAP is finished.
929 if (data
->in_fw_update
) {
930 complete(&data
->fw_completion
);
934 error
= data
->ops
->get_report(data
->client
, report
);
938 if (report
[ETP_REPORT_ID_OFFSET
] != ETP_REPORT_ID
)
939 dev_err(dev
, "invalid report id data (%x)\n",
940 report
[ETP_REPORT_ID_OFFSET
]);
942 elan_report_absolute(data
, report
);
949 ******************************************************************
950 * Elan initialization functions
951 ******************************************************************
953 static int elan_setup_input_device(struct elan_tp_data
*data
)
955 struct device
*dev
= &data
->client
->dev
;
956 struct input_dev
*input
;
957 unsigned int max_width
= max(data
->width_x
, data
->width_y
);
958 unsigned int min_width
= min(data
->width_x
, data
->width_y
);
961 input
= devm_input_allocate_device(dev
);
965 input
->name
= "Elan Touchpad";
966 input
->id
.bustype
= BUS_I2C
;
967 input
->id
.vendor
= ELAN_VENDOR_ID
;
968 input
->id
.product
= data
->product_id
;
969 input_set_drvdata(input
, data
);
971 error
= input_mt_init_slots(input
, ETP_MAX_FINGERS
,
972 INPUT_MT_POINTER
| INPUT_MT_DROP_UNUSED
);
974 dev_err(dev
, "failed to initialize MT slots: %d\n", error
);
978 __set_bit(EV_ABS
, input
->evbit
);
979 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
980 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
981 __set_bit(BTN_LEFT
, input
->keybit
);
983 /* Set up ST parameters */
984 input_set_abs_params(input
, ABS_X
, 0, data
->max_x
, 0, 0);
985 input_set_abs_params(input
, ABS_Y
, 0, data
->max_y
, 0, 0);
986 input_abs_set_res(input
, ABS_X
, data
->x_res
);
987 input_abs_set_res(input
, ABS_Y
, data
->y_res
);
988 input_set_abs_params(input
, ABS_PRESSURE
, 0, ETP_MAX_PRESSURE
, 0, 0);
989 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0, ETP_FINGER_WIDTH
, 0, 0);
990 input_set_abs_params(input
, ABS_DISTANCE
, 0, 1, 0, 0);
992 /* And MT parameters */
993 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, data
->max_x
, 0, 0);
994 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, data
->max_y
, 0, 0);
995 input_abs_set_res(input
, ABS_MT_POSITION_X
, data
->x_res
);
996 input_abs_set_res(input
, ABS_MT_POSITION_Y
, data
->y_res
);
997 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0,
998 ETP_MAX_PRESSURE
, 0, 0);
999 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0,
1000 ETP_FINGER_WIDTH
* max_width
, 0, 0);
1001 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0,
1002 ETP_FINGER_WIDTH
* min_width
, 0, 0);
1004 data
->input
= input
;
1009 static void elan_disable_regulator(void *_data
)
1011 struct elan_tp_data
*data
= _data
;
1013 regulator_disable(data
->vcc
);
1016 static void elan_remove_sysfs_groups(void *_data
)
1018 struct elan_tp_data
*data
= _data
;
1020 sysfs_remove_groups(&data
->client
->dev
.kobj
, elan_sysfs_groups
);
1023 static int elan_probe(struct i2c_client
*client
,
1024 const struct i2c_device_id
*dev_id
)
1026 const struct elan_transport_ops
*transport_ops
;
1027 struct device
*dev
= &client
->dev
;
1028 struct elan_tp_data
*data
;
1029 unsigned long irqflags
;
1032 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C
) &&
1033 i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1034 transport_ops
= &elan_i2c_ops
;
1035 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS
) &&
1036 i2c_check_functionality(client
->adapter
,
1037 I2C_FUNC_SMBUS_BYTE_DATA
|
1038 I2C_FUNC_SMBUS_BLOCK_DATA
|
1039 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1040 transport_ops
= &elan_smbus_ops
;
1042 dev_err(dev
, "not a supported I2C/SMBus adapter\n");
1046 data
= devm_kzalloc(&client
->dev
, sizeof(struct elan_tp_data
),
1051 i2c_set_clientdata(client
, data
);
1053 data
->ops
= transport_ops
;
1054 data
->client
= client
;
1055 init_completion(&data
->fw_completion
);
1056 mutex_init(&data
->sysfs_mutex
);
1058 data
->vcc
= devm_regulator_get(&client
->dev
, "vcc");
1059 if (IS_ERR(data
->vcc
)) {
1060 error
= PTR_ERR(data
->vcc
);
1061 if (error
!= -EPROBE_DEFER
)
1062 dev_err(&client
->dev
,
1063 "Failed to get 'vcc' regulator: %d\n",
1068 error
= regulator_enable(data
->vcc
);
1070 dev_err(&client
->dev
,
1071 "Failed to enable regulator: %d\n", error
);
1075 error
= devm_add_action(&client
->dev
,
1076 elan_disable_regulator
, data
);
1078 regulator_disable(data
->vcc
);
1079 dev_err(&client
->dev
,
1080 "Failed to add disable regulator action: %d\n",
1085 /* Make sure there is something at this address */
1086 error
= i2c_smbus_read_byte(client
);
1088 dev_dbg(&client
->dev
, "nothing at this address: %d\n", error
);
1092 /* Initialize the touchpad. */
1093 error
= elan_initialize(data
);
1097 error
= elan_query_device_info(data
);
1101 error
= elan_query_device_parameters(data
);
1105 dev_dbg(&client
->dev
,
1106 "Elan Touchpad Information:\n"
1107 " Module product ID: 0x%04x\n"
1108 " Firmware Version: 0x%04x\n"
1109 " Sample Version: 0x%04x\n"
1110 " IAP Version: 0x%04x\n"
1111 " Max ABS X,Y: %d,%d\n"
1112 " Width X,Y: %d,%d\n"
1113 " Resolution X,Y: %d,%d (dots/mm)\n",
1118 data
->max_x
, data
->max_y
,
1119 data
->width_x
, data
->width_y
,
1120 data
->x_res
, data
->y_res
);
1122 /* Set up input device properties based on queried parameters. */
1123 error
= elan_setup_input_device(data
);
1128 * Systems using device tree should set up interrupt via DTS,
1129 * the rest will use the default falling edge interrupts.
1131 irqflags
= client
->dev
.of_node
? 0 : IRQF_TRIGGER_FALLING
;
1133 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1135 irqflags
| IRQF_ONESHOT
,
1136 client
->name
, data
);
1138 dev_err(&client
->dev
, "cannot register irq=%d\n", client
->irq
);
1142 error
= sysfs_create_groups(&client
->dev
.kobj
, elan_sysfs_groups
);
1144 dev_err(&client
->dev
, "failed to create sysfs attributes: %d\n",
1149 error
= devm_add_action(&client
->dev
,
1150 elan_remove_sysfs_groups
, data
);
1152 elan_remove_sysfs_groups(data
);
1153 dev_err(&client
->dev
,
1154 "Failed to add sysfs cleanup action: %d\n",
1159 error
= input_register_device(data
->input
);
1161 dev_err(&client
->dev
, "failed to register input device: %d\n",
1167 * Systems using device tree should set up wakeup via DTS,
1168 * the rest will configure device as wakeup source by default.
1170 if (!client
->dev
.of_node
)
1171 device_init_wakeup(&client
->dev
, true);
1176 static int __maybe_unused
elan_suspend(struct device
*dev
)
1178 struct i2c_client
*client
= to_i2c_client(dev
);
1179 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1183 * We are taking the mutex to make sure sysfs operations are
1184 * complete before we attempt to bring the device into low[er]
1187 ret
= mutex_lock_interruptible(&data
->sysfs_mutex
);
1191 disable_irq(client
->irq
);
1193 if (device_may_wakeup(dev
)) {
1194 ret
= elan_sleep(data
);
1195 /* Enable wake from IRQ */
1196 data
->irq_wake
= (enable_irq_wake(client
->irq
) == 0);
1198 ret
= elan_disable_power(data
);
1201 mutex_unlock(&data
->sysfs_mutex
);
1205 static int __maybe_unused
elan_resume(struct device
*dev
)
1207 struct i2c_client
*client
= to_i2c_client(dev
);
1208 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1211 if (device_may_wakeup(dev
) && data
->irq_wake
) {
1212 disable_irq_wake(client
->irq
);
1213 data
->irq_wake
= false;
1216 error
= elan_enable_power(data
);
1218 dev_err(dev
, "power up when resuming failed: %d\n", error
);
1222 error
= elan_initialize(data
);
1224 dev_err(dev
, "initialize when resuming failed: %d\n", error
);
1227 enable_irq(data
->client
->irq
);
1231 static SIMPLE_DEV_PM_OPS(elan_pm_ops
, elan_suspend
, elan_resume
);
1233 static const struct i2c_device_id elan_id
[] = {
1237 MODULE_DEVICE_TABLE(i2c
, elan_id
);
1240 static const struct acpi_device_id elan_acpi_id
[] = {
1255 MODULE_DEVICE_TABLE(acpi
, elan_acpi_id
);
1259 static const struct of_device_id elan_of_match
[] = {
1260 { .compatible
= "elan,ekth3000" },
1263 MODULE_DEVICE_TABLE(of
, elan_of_match
);
1266 static struct i2c_driver elan_driver
= {
1268 .name
= DRIVER_NAME
,
1270 .acpi_match_table
= ACPI_PTR(elan_acpi_id
),
1271 .of_match_table
= of_match_ptr(elan_of_match
),
1272 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1274 .probe
= elan_probe
,
1275 .id_table
= elan_id
,
1278 module_i2c_driver(elan_driver
);
1280 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1281 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1282 MODULE_LICENSE("GPL");
1283 MODULE_VERSION(ELAN_DRIVER_VERSION
);