2 * Elan I2C/SMBus Touchpad driver
4 * Copyright (c) 2013 ELAN Microelectronics Corp.
6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
9 * Based on cyapa driver:
10 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
11 * copyright (c) 2011-2012 Google, Inc.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2 as published
15 * by the Free Software Foundation.
17 * Trademarks are the property of their respective owners.
20 #include <linux/acpi.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/firmware.h>
24 #include <linux/i2c.h>
25 #include <linux/init.h>
26 #include <linux/input/mt.h>
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/input.h>
33 #include <linux/uaccess.h>
34 #include <linux/jiffies.h>
35 #include <linux/completion.h>
37 #include <linux/regulator/consumer.h>
38 #include <asm/unaligned.h>
42 #define DRIVER_NAME "elan_i2c"
43 #define ELAN_DRIVER_VERSION "1.5.5"
44 #define ETP_PRESSURE_OFFSET 25
45 #define ETP_MAX_PRESSURE 255
46 #define ETP_FWIDTH_REDUCE 90
47 #define ETP_FINGER_WIDTH 15
48 #define ETP_RETRY_COUNT 3
50 #define ETP_MAX_FINGERS 5
51 #define ETP_FINGER_DATA_LEN 5
52 #define ETP_REPORT_ID 0x5D
53 #define ETP_REPORT_ID_OFFSET 2
54 #define ETP_TOUCH_INFO_OFFSET 3
55 #define ETP_FINGER_DATA_OFFSET 4
56 #define ETP_MAX_REPORT_LEN 34
58 /* The main device structure */
60 struct i2c_client
*client
;
61 struct input_dev
*input
;
62 struct regulator
*vcc
;
64 const struct elan_transport_ops
*ops
;
67 struct completion fw_completion
;
70 struct mutex sysfs_mutex
;
94 static int elan_enable_power(struct elan_tp_data
*data
)
96 int repeat
= ETP_RETRY_COUNT
;
99 error
= regulator_enable(data
->vcc
);
101 dev_err(&data
->client
->dev
,
102 "Failed to enable regulator: %d\n", error
);
107 error
= data
->ops
->power_control(data
->client
, true);
112 } while (--repeat
> 0);
117 static int elan_disable_power(struct elan_tp_data
*data
)
119 int repeat
= ETP_RETRY_COUNT
;
123 error
= data
->ops
->power_control(data
->client
, false);
125 error
= regulator_disable(data
->vcc
);
127 dev_err(&data
->client
->dev
,
128 "Failed to disable regulator: %d\n",
130 /* Attempt to power the chip back up */
131 data
->ops
->power_control(data
->client
, true);
139 } while (--repeat
> 0);
144 static int elan_sleep(struct elan_tp_data
*data
)
146 int repeat
= ETP_RETRY_COUNT
;
150 error
= data
->ops
->sleep_control(data
->client
, true);
155 } while (--repeat
> 0);
160 static int __elan_initialize(struct elan_tp_data
*data
)
162 struct i2c_client
*client
= data
->client
;
165 error
= data
->ops
->initialize(client
);
167 dev_err(&client
->dev
, "device initialize failed: %d\n", error
);
171 data
->mode
|= ETP_ENABLE_ABS
;
172 error
= data
->ops
->set_mode(client
, data
->mode
);
174 dev_err(&client
->dev
,
175 "failed to switch to absolute mode: %d\n", error
);
179 error
= data
->ops
->sleep_control(client
, false);
181 dev_err(&client
->dev
,
182 "failed to wake device up: %d\n", error
);
189 static int elan_initialize(struct elan_tp_data
*data
)
191 int repeat
= ETP_RETRY_COUNT
;
195 error
= __elan_initialize(data
);
201 } while (--repeat
> 0);
206 static int elan_query_device_info(struct elan_tp_data
*data
)
210 error
= data
->ops
->get_product_id(data
->client
, &data
->product_id
);
214 error
= data
->ops
->get_version(data
->client
, false, &data
->fw_version
);
218 error
= data
->ops
->get_checksum(data
->client
, false,
223 error
= data
->ops
->get_sm_version(data
->client
, &data
->sm_version
);
227 error
= data
->ops
->get_version(data
->client
, true, &data
->iap_version
);
234 static unsigned int elan_convert_resolution(u8 val
)
237 * (value from firmware) * 10 + 790 = dpi
239 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
243 return ((int)(char)val
* 10 + 790) * 10 / 254;
246 static int elan_query_device_parameters(struct elan_tp_data
*data
)
248 unsigned int x_traces
, y_traces
;
249 u8 hw_x_res
, hw_y_res
;
252 error
= data
->ops
->get_max(data
->client
, &data
->max_x
, &data
->max_y
);
256 error
= data
->ops
->get_num_traces(data
->client
, &x_traces
, &y_traces
);
260 data
->width_x
= data
->max_x
/ x_traces
;
261 data
->width_y
= data
->max_y
/ y_traces
;
263 error
= data
->ops
->get_resolution(data
->client
, &hw_x_res
, &hw_y_res
);
267 data
->x_res
= elan_convert_resolution(hw_x_res
);
268 data
->y_res
= elan_convert_resolution(hw_y_res
);
274 **********************************************************
275 * IAP firmware updater related routines
276 **********************************************************
278 static int elan_write_fw_block(struct elan_tp_data
*data
,
279 const u8
*page
, u16 checksum
, int idx
)
281 int retry
= ETP_RETRY_COUNT
;
285 error
= data
->ops
->write_fw_block(data
->client
,
286 page
, checksum
, idx
);
290 dev_dbg(&data
->client
->dev
,
291 "IAP retrying page %d (error: %d)\n", idx
, error
);
292 } while (--retry
> 0);
297 static int __elan_update_firmware(struct elan_tp_data
*data
,
298 const struct firmware
*fw
)
300 struct i2c_client
*client
= data
->client
;
301 struct device
*dev
= &client
->dev
;
306 u16 sw_checksum
= 0, fw_checksum
= 0;
308 error
= data
->ops
->prepare_fw_update(client
);
312 iap_start_addr
= get_unaligned_le16(&fw
->data
[ETP_IAP_START_ADDR
* 2]);
314 boot_page_count
= (iap_start_addr
* 2) / ETP_FW_PAGE_SIZE
;
315 for (i
= boot_page_count
; i
< ETP_FW_PAGE_COUNT
; i
++) {
317 const u8
*page
= &fw
->data
[i
* ETP_FW_PAGE_SIZE
];
319 for (j
= 0; j
< ETP_FW_PAGE_SIZE
; j
+= 2)
320 checksum
+= ((page
[j
+ 1] << 8) | page
[j
]);
322 error
= elan_write_fw_block(data
, page
, checksum
, i
);
324 dev_err(dev
, "write page %d fail: %d\n", i
, error
);
328 sw_checksum
+= checksum
;
331 /* Wait WDT reset and power on reset */
334 error
= data
->ops
->finish_fw_update(client
, &data
->fw_completion
);
338 error
= data
->ops
->get_checksum(client
, true, &fw_checksum
);
342 if (sw_checksum
!= fw_checksum
) {
343 dev_err(dev
, "checksum diff sw=[%04X], fw=[%04X]\n",
344 sw_checksum
, fw_checksum
);
351 static int elan_update_firmware(struct elan_tp_data
*data
,
352 const struct firmware
*fw
)
354 struct i2c_client
*client
= data
->client
;
357 dev_dbg(&client
->dev
, "Starting firmware update....\n");
359 disable_irq(client
->irq
);
360 data
->in_fw_update
= true;
362 retval
= __elan_update_firmware(data
, fw
);
364 dev_err(&client
->dev
, "firmware update failed: %d\n", retval
);
365 data
->ops
->iap_reset(client
);
367 /* Reinitialize TP after fw is updated */
368 elan_initialize(data
);
369 elan_query_device_info(data
);
372 data
->in_fw_update
= false;
373 enable_irq(client
->irq
);
379 *******************************************************************
381 *******************************************************************
383 static ssize_t
elan_sysfs_read_fw_checksum(struct device
*dev
,
384 struct device_attribute
*attr
,
387 struct i2c_client
*client
= to_i2c_client(dev
);
388 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
390 return sprintf(buf
, "0x%04x\n", data
->fw_checksum
);
393 static ssize_t
elan_sysfs_read_product_id(struct device
*dev
,
394 struct device_attribute
*attr
,
397 struct i2c_client
*client
= to_i2c_client(dev
);
398 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
400 return sprintf(buf
, "%d.0\n", data
->product_id
);
403 static ssize_t
elan_sysfs_read_fw_ver(struct device
*dev
,
404 struct device_attribute
*attr
,
407 struct i2c_client
*client
= to_i2c_client(dev
);
408 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
410 return sprintf(buf
, "%d.0\n", data
->fw_version
);
413 static ssize_t
elan_sysfs_read_sm_ver(struct device
*dev
,
414 struct device_attribute
*attr
,
417 struct i2c_client
*client
= to_i2c_client(dev
);
418 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
420 return sprintf(buf
, "%d.0\n", data
->sm_version
);
423 static ssize_t
elan_sysfs_read_iap_ver(struct device
*dev
,
424 struct device_attribute
*attr
,
427 struct i2c_client
*client
= to_i2c_client(dev
);
428 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
430 return sprintf(buf
, "%d.0\n", data
->iap_version
);
433 static ssize_t
elan_sysfs_update_fw(struct device
*dev
,
434 struct device_attribute
*attr
,
435 const char *buf
, size_t count
)
437 struct i2c_client
*client
= to_i2c_client(dev
);
438 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
439 const struct firmware
*fw
;
442 error
= request_firmware(&fw
, ETP_FW_NAME
, dev
);
444 dev_err(dev
, "cannot load firmware %s: %d\n",
449 /* Firmware must be exactly PAGE_NUM * PAGE_SIZE bytes */
450 if (fw
->size
!= ETP_FW_SIZE
) {
451 dev_err(dev
, "invalid firmware size = %zu, expected %d.\n",
452 fw
->size
, ETP_FW_SIZE
);
457 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
461 error
= elan_update_firmware(data
, fw
);
463 mutex_unlock(&data
->sysfs_mutex
);
466 release_firmware(fw
);
467 return error
?: count
;
470 static ssize_t
calibrate_store(struct device
*dev
,
471 struct device_attribute
*attr
,
472 const char *buf
, size_t count
)
474 struct i2c_client
*client
= to_i2c_client(dev
);
475 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
481 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
485 disable_irq(client
->irq
);
487 data
->mode
|= ETP_ENABLE_CALIBRATE
;
488 retval
= data
->ops
->set_mode(client
, data
->mode
);
490 dev_err(dev
, "failed to enable calibration mode: %d\n",
495 retval
= data
->ops
->calibrate(client
);
497 dev_err(dev
, "failed to start calibration: %d\n",
499 goto out_disable_calibrate
;
504 /* Wait 250ms before checking if calibration has completed. */
507 retval
= data
->ops
->calibrate_result(client
, val
);
509 dev_err(dev
, "failed to check calibration result: %d\n",
511 else if (val
[0] == 0)
512 break; /* calibration done */
517 dev_err(dev
, "failed to calibrate. Timeout.\n");
521 out_disable_calibrate
:
522 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
523 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
525 dev_err(dev
, "failed to disable calibration mode: %d\n",
531 enable_irq(client
->irq
);
532 mutex_unlock(&data
->sysfs_mutex
);
533 return retval
?: count
;
536 static ssize_t
elan_sysfs_read_mode(struct device
*dev
,
537 struct device_attribute
*attr
,
540 struct i2c_client
*client
= to_i2c_client(dev
);
541 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
545 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
549 error
= data
->ops
->iap_get_mode(data
->client
, &mode
);
551 mutex_unlock(&data
->sysfs_mutex
);
556 return sprintf(buf
, "%d\n", (int)mode
);
559 static DEVICE_ATTR(product_id
, S_IRUGO
, elan_sysfs_read_product_id
, NULL
);
560 static DEVICE_ATTR(firmware_version
, S_IRUGO
, elan_sysfs_read_fw_ver
, NULL
);
561 static DEVICE_ATTR(sample_version
, S_IRUGO
, elan_sysfs_read_sm_ver
, NULL
);
562 static DEVICE_ATTR(iap_version
, S_IRUGO
, elan_sysfs_read_iap_ver
, NULL
);
563 static DEVICE_ATTR(fw_checksum
, S_IRUGO
, elan_sysfs_read_fw_checksum
, NULL
);
564 static DEVICE_ATTR(mode
, S_IRUGO
, elan_sysfs_read_mode
, NULL
);
565 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, elan_sysfs_update_fw
);
567 static DEVICE_ATTR_WO(calibrate
);
569 static struct attribute
*elan_sysfs_entries
[] = {
570 &dev_attr_product_id
.attr
,
571 &dev_attr_firmware_version
.attr
,
572 &dev_attr_sample_version
.attr
,
573 &dev_attr_iap_version
.attr
,
574 &dev_attr_fw_checksum
.attr
,
575 &dev_attr_calibrate
.attr
,
577 &dev_attr_update_fw
.attr
,
581 static const struct attribute_group elan_sysfs_group
= {
582 .attrs
= elan_sysfs_entries
,
585 static ssize_t
acquire_store(struct device
*dev
, struct device_attribute
*attr
,
586 const char *buf
, size_t count
)
588 struct i2c_client
*client
= to_i2c_client(dev
);
589 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
593 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
597 disable_irq(client
->irq
);
599 data
->baseline_ready
= false;
601 data
->mode
|= ETP_ENABLE_CALIBRATE
;
602 retval
= data
->ops
->set_mode(data
->client
, data
->mode
);
604 dev_err(dev
, "Failed to enable calibration mode to get baseline: %d\n",
611 retval
= data
->ops
->get_baseline_data(data
->client
, true,
612 &data
->max_baseline
);
614 dev_err(dev
, "Failed to read max baseline form device: %d\n",
616 goto out_disable_calibrate
;
619 retval
= data
->ops
->get_baseline_data(data
->client
, false,
620 &data
->min_baseline
);
622 dev_err(dev
, "Failed to read min baseline form device: %d\n",
624 goto out_disable_calibrate
;
627 data
->baseline_ready
= true;
629 out_disable_calibrate
:
630 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
631 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
633 dev_err(dev
, "Failed to disable calibration mode after acquiring baseline: %d\n",
639 enable_irq(client
->irq
);
640 mutex_unlock(&data
->sysfs_mutex
);
641 return retval
?: count
;
644 static ssize_t
min_show(struct device
*dev
,
645 struct device_attribute
*attr
, char *buf
)
647 struct i2c_client
*client
= to_i2c_client(dev
);
648 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
651 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
655 if (!data
->baseline_ready
) {
660 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->min_baseline
);
663 mutex_unlock(&data
->sysfs_mutex
);
667 static ssize_t
max_show(struct device
*dev
,
668 struct device_attribute
*attr
, char *buf
)
670 struct i2c_client
*client
= to_i2c_client(dev
);
671 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
674 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
678 if (!data
->baseline_ready
) {
683 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->max_baseline
);
686 mutex_unlock(&data
->sysfs_mutex
);
691 static DEVICE_ATTR_WO(acquire
);
692 static DEVICE_ATTR_RO(min
);
693 static DEVICE_ATTR_RO(max
);
695 static struct attribute
*elan_baseline_sysfs_entries
[] = {
696 &dev_attr_acquire
.attr
,
702 static const struct attribute_group elan_baseline_sysfs_group
= {
704 .attrs
= elan_baseline_sysfs_entries
,
707 static const struct attribute_group
*elan_sysfs_groups
[] = {
709 &elan_baseline_sysfs_group
,
714 ******************************************************************
716 ******************************************************************
718 static void elan_report_contact(struct elan_tp_data
*data
,
719 int contact_num
, bool contact_valid
,
722 struct input_dev
*input
= data
->input
;
723 unsigned int pos_x
, pos_y
;
724 unsigned int pressure
, mk_x
, mk_y
;
725 unsigned int area_x
, area_y
, major
, minor
, new_pressure
;
729 pos_x
= ((finger_data
[0] & 0xf0) << 4) |
731 pos_y
= ((finger_data
[0] & 0x0f) << 8) |
733 mk_x
= (finger_data
[3] & 0x0f);
734 mk_y
= (finger_data
[3] >> 4);
735 pressure
= finger_data
[4];
737 if (pos_x
> data
->max_x
|| pos_y
> data
->max_y
) {
738 dev_dbg(input
->dev
.parent
,
739 "[%d] x=%d y=%d over max (%d, %d)",
740 contact_num
, pos_x
, pos_y
,
741 data
->max_x
, data
->max_y
);
746 * To avoid treating large finger as palm, let's reduce the
747 * width x and y per trace.
749 area_x
= mk_x
* (data
->width_x
- ETP_FWIDTH_REDUCE
);
750 area_y
= mk_y
* (data
->width_y
- ETP_FWIDTH_REDUCE
);
752 major
= max(area_x
, area_y
);
753 minor
= min(area_x
, area_y
);
755 new_pressure
= pressure
+ ETP_PRESSURE_OFFSET
;
756 if (new_pressure
> ETP_MAX_PRESSURE
)
757 new_pressure
= ETP_MAX_PRESSURE
;
759 input_mt_slot(input
, contact_num
);
760 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
761 input_report_abs(input
, ABS_MT_POSITION_X
, pos_x
);
762 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->max_y
- pos_y
);
763 input_report_abs(input
, ABS_MT_PRESSURE
, new_pressure
);
764 input_report_abs(input
, ABS_TOOL_WIDTH
, mk_x
);
765 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, major
);
766 input_report_abs(input
, ABS_MT_TOUCH_MINOR
, minor
);
768 input_mt_slot(input
, contact_num
);
769 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, false);
773 static void elan_report_absolute(struct elan_tp_data
*data
, u8
*packet
)
775 struct input_dev
*input
= data
->input
;
776 u8
*finger_data
= &packet
[ETP_FINGER_DATA_OFFSET
];
778 u8 tp_info
= packet
[ETP_TOUCH_INFO_OFFSET
];
781 for (i
= 0; i
< ETP_MAX_FINGERS
; i
++) {
782 contact_valid
= tp_info
& (1U << (3 + i
));
783 elan_report_contact(data
, i
, contact_valid
, finger_data
);
786 finger_data
+= ETP_FINGER_DATA_LEN
;
789 input_report_key(input
, BTN_LEFT
, tp_info
& 0x01);
790 input_mt_report_pointer_emulation(input
, true);
794 static irqreturn_t
elan_isr(int irq
, void *dev_id
)
796 struct elan_tp_data
*data
= dev_id
;
797 struct device
*dev
= &data
->client
->dev
;
799 u8 report
[ETP_MAX_REPORT_LEN
];
802 * When device is connected to i2c bus, when all IAP page writes
803 * complete, the driver will receive interrupt and must read
804 * 0000 to confirm that IAP is finished.
806 if (data
->in_fw_update
) {
807 complete(&data
->fw_completion
);
811 error
= data
->ops
->get_report(data
->client
, report
);
815 if (report
[ETP_REPORT_ID_OFFSET
] != ETP_REPORT_ID
)
816 dev_err(dev
, "invalid report id data (%x)\n",
817 report
[ETP_REPORT_ID_OFFSET
]);
819 elan_report_absolute(data
, report
);
826 ******************************************************************
827 * Elan initialization functions
828 ******************************************************************
830 static int elan_setup_input_device(struct elan_tp_data
*data
)
832 struct device
*dev
= &data
->client
->dev
;
833 struct input_dev
*input
;
834 unsigned int max_width
= max(data
->width_x
, data
->width_y
);
835 unsigned int min_width
= min(data
->width_x
, data
->width_y
);
838 input
= devm_input_allocate_device(dev
);
842 input
->name
= "Elan Touchpad";
843 input
->id
.bustype
= BUS_I2C
;
844 input_set_drvdata(input
, data
);
846 error
= input_mt_init_slots(input
, ETP_MAX_FINGERS
,
847 INPUT_MT_POINTER
| INPUT_MT_DROP_UNUSED
);
849 dev_err(dev
, "failed to initialize MT slots: %d\n", error
);
853 __set_bit(EV_ABS
, input
->evbit
);
854 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
855 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
856 __set_bit(BTN_LEFT
, input
->keybit
);
858 /* Set up ST parameters */
859 input_set_abs_params(input
, ABS_X
, 0, data
->max_x
, 0, 0);
860 input_set_abs_params(input
, ABS_Y
, 0, data
->max_y
, 0, 0);
861 input_abs_set_res(input
, ABS_X
, data
->x_res
);
862 input_abs_set_res(input
, ABS_Y
, data
->y_res
);
863 input_set_abs_params(input
, ABS_PRESSURE
, 0, ETP_MAX_PRESSURE
, 0, 0);
864 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0, ETP_FINGER_WIDTH
, 0, 0);
866 /* And MT parameters */
867 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, data
->max_x
, 0, 0);
868 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, data
->max_y
, 0, 0);
869 input_abs_set_res(input
, ABS_MT_POSITION_X
, data
->x_res
);
870 input_abs_set_res(input
, ABS_MT_POSITION_Y
, data
->y_res
);
871 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0,
872 ETP_MAX_PRESSURE
, 0, 0);
873 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0,
874 ETP_FINGER_WIDTH
* max_width
, 0, 0);
875 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0,
876 ETP_FINGER_WIDTH
* min_width
, 0, 0);
883 static void elan_disable_regulator(void *_data
)
885 struct elan_tp_data
*data
= _data
;
887 regulator_disable(data
->vcc
);
890 static void elan_remove_sysfs_groups(void *_data
)
892 struct elan_tp_data
*data
= _data
;
894 sysfs_remove_groups(&data
->client
->dev
.kobj
, elan_sysfs_groups
);
897 static int elan_probe(struct i2c_client
*client
,
898 const struct i2c_device_id
*dev_id
)
900 const struct elan_transport_ops
*transport_ops
;
901 struct device
*dev
= &client
->dev
;
902 struct elan_tp_data
*data
;
903 unsigned long irqflags
;
906 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C
) &&
907 i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
908 transport_ops
= &elan_i2c_ops
;
909 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS
) &&
910 i2c_check_functionality(client
->adapter
,
911 I2C_FUNC_SMBUS_BYTE_DATA
|
912 I2C_FUNC_SMBUS_BLOCK_DATA
|
913 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
914 transport_ops
= &elan_smbus_ops
;
916 dev_err(dev
, "not a supported I2C/SMBus adapter\n");
920 data
= devm_kzalloc(&client
->dev
, sizeof(struct elan_tp_data
),
925 i2c_set_clientdata(client
, data
);
927 data
->ops
= transport_ops
;
928 data
->client
= client
;
929 init_completion(&data
->fw_completion
);
930 mutex_init(&data
->sysfs_mutex
);
932 data
->vcc
= devm_regulator_get(&client
->dev
, "vcc");
933 if (IS_ERR(data
->vcc
)) {
934 error
= PTR_ERR(data
->vcc
);
935 if (error
!= -EPROBE_DEFER
)
936 dev_err(&client
->dev
,
937 "Failed to get 'vcc' regulator: %d\n",
942 error
= regulator_enable(data
->vcc
);
944 dev_err(&client
->dev
,
945 "Failed to enable regulator: %d\n", error
);
949 error
= devm_add_action(&client
->dev
,
950 elan_disable_regulator
, data
);
952 regulator_disable(data
->vcc
);
953 dev_err(&client
->dev
,
954 "Failed to add disable regulator action: %d\n",
959 /* Initialize the touchpad. */
960 error
= elan_initialize(data
);
964 error
= elan_query_device_info(data
);
968 error
= elan_query_device_parameters(data
);
972 dev_dbg(&client
->dev
,
973 "Elan Touchpad Information:\n"
974 " Module product ID: 0x%04x\n"
975 " Firmware Version: 0x%04x\n"
976 " Sample Version: 0x%04x\n"
977 " IAP Version: 0x%04x\n"
978 " Max ABS X,Y: %d,%d\n"
979 " Width X,Y: %d,%d\n"
980 " Resolution X,Y: %d,%d (dots/mm)\n",
985 data
->max_x
, data
->max_y
,
986 data
->width_x
, data
->width_y
,
987 data
->x_res
, data
->y_res
);
989 /* Set up input device properties based on queried parameters. */
990 error
= elan_setup_input_device(data
);
995 * Systems using device tree should set up interrupt via DTS,
996 * the rest will use the default falling edge interrupts.
998 irqflags
= client
->dev
.of_node
? 0 : IRQF_TRIGGER_FALLING
;
1000 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1002 irqflags
| IRQF_ONESHOT
,
1003 client
->name
, data
);
1005 dev_err(&client
->dev
, "cannot register irq=%d\n", client
->irq
);
1009 error
= sysfs_create_groups(&client
->dev
.kobj
, elan_sysfs_groups
);
1011 dev_err(&client
->dev
, "failed to create sysfs attributes: %d\n",
1016 error
= devm_add_action(&client
->dev
,
1017 elan_remove_sysfs_groups
, data
);
1019 elan_remove_sysfs_groups(data
);
1020 dev_err(&client
->dev
,
1021 "Failed to add sysfs cleanup action: %d\n",
1026 error
= input_register_device(data
->input
);
1028 dev_err(&client
->dev
, "failed to register input device: %d\n",
1034 * Systems using device tree should set up wakeup via DTS,
1035 * the rest will configure device as wakeup source by default.
1037 if (!client
->dev
.of_node
)
1038 device_init_wakeup(&client
->dev
, true);
1043 static int __maybe_unused
elan_suspend(struct device
*dev
)
1045 struct i2c_client
*client
= to_i2c_client(dev
);
1046 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1050 * We are taking the mutex to make sure sysfs operations are
1051 * complete before we attempt to bring the device into low[er]
1054 ret
= mutex_lock_interruptible(&data
->sysfs_mutex
);
1058 disable_irq(client
->irq
);
1060 if (device_may_wakeup(dev
)) {
1061 ret
= elan_sleep(data
);
1062 /* Enable wake from IRQ */
1063 data
->irq_wake
= (enable_irq_wake(client
->irq
) == 0);
1065 ret
= elan_disable_power(data
);
1068 mutex_unlock(&data
->sysfs_mutex
);
1072 static int __maybe_unused
elan_resume(struct device
*dev
)
1074 struct i2c_client
*client
= to_i2c_client(dev
);
1075 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1078 if (device_may_wakeup(dev
) && data
->irq_wake
) {
1079 disable_irq_wake(client
->irq
);
1080 data
->irq_wake
= false;
1083 error
= elan_enable_power(data
);
1085 dev_err(dev
, "power up when resuming failed: %d\n", error
);
1087 error
= elan_initialize(data
);
1089 dev_err(dev
, "initialize when resuming failed: %d\n", error
);
1091 enable_irq(data
->client
->irq
);
1096 static SIMPLE_DEV_PM_OPS(elan_pm_ops
, elan_suspend
, elan_resume
);
1098 static const struct i2c_device_id elan_id
[] = {
1102 MODULE_DEVICE_TABLE(i2c
, elan_id
);
1105 static const struct acpi_device_id elan_acpi_id
[] = {
1109 MODULE_DEVICE_TABLE(acpi
, elan_acpi_id
);
1113 static const struct of_device_id elan_of_match
[] = {
1114 { .compatible
= "elan,ekth3000" },
1117 MODULE_DEVICE_TABLE(of
, elan_of_match
);
1120 static struct i2c_driver elan_driver
= {
1122 .name
= DRIVER_NAME
,
1123 .owner
= THIS_MODULE
,
1125 .acpi_match_table
= ACPI_PTR(elan_acpi_id
),
1126 .of_match_table
= of_match_ptr(elan_of_match
),
1128 .probe
= elan_probe
,
1129 .id_table
= elan_id
,
1132 module_i2c_driver(elan_driver
);
1134 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1135 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1136 MODULE_LICENSE("GPL");
1137 MODULE_VERSION(ELAN_DRIVER_VERSION
);