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/property.h>
40 #include <linux/regulator/consumer.h>
41 #include <asm/unaligned.h>
45 #define DRIVER_NAME "elan_i2c"
46 #define ELAN_VENDOR_ID 0x04f3
47 #define ETP_MAX_PRESSURE 255
48 #define ETP_FWIDTH_REDUCE 90
49 #define ETP_FINGER_WIDTH 15
50 #define ETP_RETRY_COUNT 3
52 #define ETP_MAX_FINGERS 5
53 #define ETP_FINGER_DATA_LEN 5
54 #define ETP_REPORT_ID 0x5D
55 #define ETP_TP_REPORT_ID 0x5E
56 #define ETP_REPORT_ID_OFFSET 2
57 #define ETP_TOUCH_INFO_OFFSET 3
58 #define ETP_FINGER_DATA_OFFSET 4
59 #define ETP_HOVER_INFO_OFFSET 30
60 #define ETP_MAX_REPORT_LEN 34
62 /* The main device structure */
64 struct i2c_client
*client
;
65 struct input_dev
*input
;
66 struct input_dev
*tp_input
; /* trackpoint input node */
67 struct regulator
*vcc
;
69 const struct elan_transport_ops
*ops
;
72 struct completion fw_completion
;
75 struct mutex sysfs_mutex
;
90 int pressure_adjustment
;
93 u16 fw_validpage_count
;
94 u16 fw_signature_address
;
104 static int elan_get_fwinfo(u16 ic_type
, u16
*validpage_count
,
105 u16
*signature_address
)
111 *validpage_count
= 512;
119 *validpage_count
= 768;
122 *validpage_count
= 896;
125 *validpage_count
= 640;
128 *validpage_count
= 1024;
131 /* unknown ic type clear value */
132 *validpage_count
= 0;
133 *signature_address
= 0;
138 (*validpage_count
* ETP_FW_PAGE_SIZE
) - ETP_FW_SIGNATURE_SIZE
;
143 static int elan_enable_power(struct elan_tp_data
*data
)
145 int repeat
= ETP_RETRY_COUNT
;
148 error
= regulator_enable(data
->vcc
);
150 dev_err(&data
->client
->dev
,
151 "failed to enable regulator: %d\n", error
);
156 error
= data
->ops
->power_control(data
->client
, true);
161 } while (--repeat
> 0);
163 dev_err(&data
->client
->dev
, "failed to enable power: %d\n", error
);
167 static int elan_disable_power(struct elan_tp_data
*data
)
169 int repeat
= ETP_RETRY_COUNT
;
173 error
= data
->ops
->power_control(data
->client
, false);
175 error
= regulator_disable(data
->vcc
);
177 dev_err(&data
->client
->dev
,
178 "failed to disable regulator: %d\n",
180 /* Attempt to power the chip back up */
181 data
->ops
->power_control(data
->client
, true);
189 } while (--repeat
> 0);
191 dev_err(&data
->client
->dev
, "failed to disable power: %d\n", error
);
195 static int elan_sleep(struct elan_tp_data
*data
)
197 int repeat
= ETP_RETRY_COUNT
;
201 error
= data
->ops
->sleep_control(data
->client
, true);
206 } while (--repeat
> 0);
211 static int elan_query_product(struct elan_tp_data
*data
)
215 error
= data
->ops
->get_product_id(data
->client
, &data
->product_id
);
219 error
= data
->ops
->get_sm_version(data
->client
, &data
->ic_type
,
220 &data
->sm_version
, &data
->clickpad
);
227 static int elan_check_ASUS_special_fw(struct elan_tp_data
*data
)
229 if (data
->ic_type
== 0x0E) {
230 switch (data
->product_id
) {
236 } else if (data
->ic_type
== 0x08 && data
->product_id
== 0x26) {
237 /* ASUS EeeBook X205TA */
244 static int __elan_initialize(struct elan_tp_data
*data
)
246 struct i2c_client
*client
= data
->client
;
247 bool woken_up
= false;
250 error
= data
->ops
->initialize(client
);
252 dev_err(&client
->dev
, "device initialize failed: %d\n", error
);
256 error
= elan_query_product(data
);
261 * Some ASUS devices were shipped with firmware that requires
262 * touchpads to be woken up first, before attempting to switch
263 * them into absolute reporting mode.
265 if (elan_check_ASUS_special_fw(data
)) {
266 error
= data
->ops
->sleep_control(client
, false);
268 dev_err(&client
->dev
,
269 "failed to wake device up: %d\n", error
);
277 data
->mode
|= ETP_ENABLE_ABS
;
278 error
= data
->ops
->set_mode(client
, data
->mode
);
280 dev_err(&client
->dev
,
281 "failed to switch to absolute mode: %d\n", error
);
286 error
= data
->ops
->sleep_control(client
, false);
288 dev_err(&client
->dev
,
289 "failed to wake device up: %d\n", error
);
297 static int elan_initialize(struct elan_tp_data
*data
)
299 int repeat
= ETP_RETRY_COUNT
;
303 error
= __elan_initialize(data
);
308 } while (--repeat
> 0);
313 static int elan_query_device_info(struct elan_tp_data
*data
)
318 error
= data
->ops
->get_version(data
->client
, false, &data
->fw_version
);
322 error
= data
->ops
->get_checksum(data
->client
, false,
327 error
= data
->ops
->get_version(data
->client
, true, &data
->iap_version
);
331 error
= data
->ops
->get_pressure_adjustment(data
->client
,
332 &data
->pressure_adjustment
);
336 error
= data
->ops
->get_pattern(data
->client
, &data
->pattern
);
340 if (data
->pattern
== 0x01)
341 ic_type
= data
->ic_type
;
343 ic_type
= data
->iap_version
;
345 error
= elan_get_fwinfo(ic_type
, &data
->fw_validpage_count
,
346 &data
->fw_signature_address
);
348 dev_warn(&data
->client
->dev
,
349 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
350 data
->iap_version
, data
->ic_type
);
355 static unsigned int elan_convert_resolution(u8 val
)
358 * (value from firmware) * 10 + 790 = dpi
360 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
364 return ((int)(char)val
* 10 + 790) * 10 / 254;
367 static int elan_query_device_parameters(struct elan_tp_data
*data
)
369 unsigned int x_traces
, y_traces
;
370 u8 hw_x_res
, hw_y_res
;
373 error
= data
->ops
->get_max(data
->client
, &data
->max_x
, &data
->max_y
);
377 error
= data
->ops
->get_num_traces(data
->client
, &x_traces
, &y_traces
);
381 data
->width_x
= data
->max_x
/ x_traces
;
382 data
->width_y
= data
->max_y
/ y_traces
;
384 error
= data
->ops
->get_resolution(data
->client
, &hw_x_res
, &hw_y_res
);
388 data
->x_res
= elan_convert_resolution(hw_x_res
);
389 data
->y_res
= elan_convert_resolution(hw_y_res
);
395 **********************************************************
396 * IAP firmware updater related routines
397 **********************************************************
399 static int elan_write_fw_block(struct elan_tp_data
*data
,
400 const u8
*page
, u16 checksum
, int idx
)
402 int retry
= ETP_RETRY_COUNT
;
406 error
= data
->ops
->write_fw_block(data
->client
,
407 page
, checksum
, idx
);
411 dev_dbg(&data
->client
->dev
,
412 "IAP retrying page %d (error: %d)\n", idx
, error
);
413 } while (--retry
> 0);
418 static int __elan_update_firmware(struct elan_tp_data
*data
,
419 const struct firmware
*fw
)
421 struct i2c_client
*client
= data
->client
;
422 struct device
*dev
= &client
->dev
;
427 u16 sw_checksum
= 0, fw_checksum
= 0;
429 error
= data
->ops
->prepare_fw_update(client
);
433 iap_start_addr
= get_unaligned_le16(&fw
->data
[ETP_IAP_START_ADDR
* 2]);
435 boot_page_count
= (iap_start_addr
* 2) / ETP_FW_PAGE_SIZE
;
436 for (i
= boot_page_count
; i
< data
->fw_validpage_count
; i
++) {
438 const u8
*page
= &fw
->data
[i
* ETP_FW_PAGE_SIZE
];
440 for (j
= 0; j
< ETP_FW_PAGE_SIZE
; j
+= 2)
441 checksum
+= ((page
[j
+ 1] << 8) | page
[j
]);
443 error
= elan_write_fw_block(data
, page
, checksum
, i
);
445 dev_err(dev
, "write page %d fail: %d\n", i
, error
);
449 sw_checksum
+= checksum
;
452 /* Wait WDT reset and power on reset */
455 error
= data
->ops
->finish_fw_update(client
, &data
->fw_completion
);
459 error
= data
->ops
->get_checksum(client
, true, &fw_checksum
);
463 if (sw_checksum
!= fw_checksum
) {
464 dev_err(dev
, "checksum diff sw=[%04X], fw=[%04X]\n",
465 sw_checksum
, fw_checksum
);
472 static int elan_update_firmware(struct elan_tp_data
*data
,
473 const struct firmware
*fw
)
475 struct i2c_client
*client
= data
->client
;
478 dev_dbg(&client
->dev
, "Starting firmware update....\n");
480 disable_irq(client
->irq
);
481 data
->in_fw_update
= true;
483 retval
= __elan_update_firmware(data
, fw
);
485 dev_err(&client
->dev
, "firmware update failed: %d\n", retval
);
486 data
->ops
->iap_reset(client
);
488 /* Reinitialize TP after fw is updated */
489 elan_initialize(data
);
490 elan_query_device_info(data
);
493 data
->in_fw_update
= false;
494 enable_irq(client
->irq
);
500 *******************************************************************
502 *******************************************************************
504 static ssize_t
elan_sysfs_read_fw_checksum(struct device
*dev
,
505 struct device_attribute
*attr
,
508 struct i2c_client
*client
= to_i2c_client(dev
);
509 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
511 return sprintf(buf
, "0x%04x\n", data
->fw_checksum
);
514 static ssize_t
elan_sysfs_read_product_id(struct device
*dev
,
515 struct device_attribute
*attr
,
518 struct i2c_client
*client
= to_i2c_client(dev
);
519 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
521 return sprintf(buf
, ETP_PRODUCT_ID_FORMAT_STRING
"\n",
525 static ssize_t
elan_sysfs_read_fw_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
->fw_version
);
535 static ssize_t
elan_sysfs_read_sm_ver(struct device
*dev
,
536 struct device_attribute
*attr
,
539 struct i2c_client
*client
= to_i2c_client(dev
);
540 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
542 return sprintf(buf
, "%d.0\n", data
->sm_version
);
545 static ssize_t
elan_sysfs_read_iap_ver(struct device
*dev
,
546 struct device_attribute
*attr
,
549 struct i2c_client
*client
= to_i2c_client(dev
);
550 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
552 return sprintf(buf
, "%d.0\n", data
->iap_version
);
555 static ssize_t
elan_sysfs_update_fw(struct device
*dev
,
556 struct device_attribute
*attr
,
557 const char *buf
, size_t count
)
559 struct elan_tp_data
*data
= dev_get_drvdata(dev
);
560 const struct firmware
*fw
;
563 const u8
*fw_signature
;
564 static const u8 signature
[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
566 if (data
->fw_validpage_count
== 0)
569 /* Look for a firmware with the product id appended. */
570 fw_name
= kasprintf(GFP_KERNEL
, ETP_FW_NAME
, data
->product_id
);
572 dev_err(dev
, "failed to allocate memory for firmware name\n");
576 dev_info(dev
, "requesting fw '%s'\n", fw_name
);
577 error
= request_firmware(&fw
, fw_name
, dev
);
580 dev_err(dev
, "failed to request firmware: %d\n", error
);
584 /* Firmware file must match signature data */
585 fw_signature
= &fw
->data
[data
->fw_signature_address
];
586 if (memcmp(fw_signature
, signature
, sizeof(signature
)) != 0) {
587 dev_err(dev
, "signature mismatch (expected %*ph, got %*ph)\n",
588 (int)sizeof(signature
), signature
,
589 (int)sizeof(signature
), fw_signature
);
594 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
598 error
= elan_update_firmware(data
, fw
);
600 mutex_unlock(&data
->sysfs_mutex
);
603 release_firmware(fw
);
604 return error
?: count
;
607 static ssize_t
calibrate_store(struct device
*dev
,
608 struct device_attribute
*attr
,
609 const char *buf
, size_t count
)
611 struct i2c_client
*client
= to_i2c_client(dev
);
612 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
616 u8 val
[ETP_CALIBRATE_MAX_LEN
];
618 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
622 disable_irq(client
->irq
);
624 data
->mode
|= ETP_ENABLE_CALIBRATE
;
625 retval
= data
->ops
->set_mode(client
, data
->mode
);
627 dev_err(dev
, "failed to enable calibration mode: %d\n",
632 retval
= data
->ops
->calibrate(client
);
634 dev_err(dev
, "failed to start calibration: %d\n",
636 goto out_disable_calibrate
;
641 /* Wait 250ms before checking if calibration has completed. */
644 retval
= data
->ops
->calibrate_result(client
, val
);
646 dev_err(dev
, "failed to check calibration result: %d\n",
648 else if (val
[0] == 0)
649 break; /* calibration done */
654 dev_err(dev
, "failed to calibrate. Timeout.\n");
658 out_disable_calibrate
:
659 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
660 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
662 dev_err(dev
, "failed to disable calibration mode: %d\n",
668 enable_irq(client
->irq
);
669 mutex_unlock(&data
->sysfs_mutex
);
670 return retval
?: count
;
673 static ssize_t
elan_sysfs_read_mode(struct device
*dev
,
674 struct device_attribute
*attr
,
677 struct i2c_client
*client
= to_i2c_client(dev
);
678 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
682 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
686 error
= data
->ops
->iap_get_mode(data
->client
, &mode
);
688 mutex_unlock(&data
->sysfs_mutex
);
693 return sprintf(buf
, "%d\n", (int)mode
);
696 static DEVICE_ATTR(product_id
, S_IRUGO
, elan_sysfs_read_product_id
, NULL
);
697 static DEVICE_ATTR(firmware_version
, S_IRUGO
, elan_sysfs_read_fw_ver
, NULL
);
698 static DEVICE_ATTR(sample_version
, S_IRUGO
, elan_sysfs_read_sm_ver
, NULL
);
699 static DEVICE_ATTR(iap_version
, S_IRUGO
, elan_sysfs_read_iap_ver
, NULL
);
700 static DEVICE_ATTR(fw_checksum
, S_IRUGO
, elan_sysfs_read_fw_checksum
, NULL
);
701 static DEVICE_ATTR(mode
, S_IRUGO
, elan_sysfs_read_mode
, NULL
);
702 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, elan_sysfs_update_fw
);
704 static DEVICE_ATTR_WO(calibrate
);
706 static struct attribute
*elan_sysfs_entries
[] = {
707 &dev_attr_product_id
.attr
,
708 &dev_attr_firmware_version
.attr
,
709 &dev_attr_sample_version
.attr
,
710 &dev_attr_iap_version
.attr
,
711 &dev_attr_fw_checksum
.attr
,
712 &dev_attr_calibrate
.attr
,
714 &dev_attr_update_fw
.attr
,
718 static const struct attribute_group elan_sysfs_group
= {
719 .attrs
= elan_sysfs_entries
,
722 static ssize_t
acquire_store(struct device
*dev
, struct device_attribute
*attr
,
723 const char *buf
, size_t count
)
725 struct i2c_client
*client
= to_i2c_client(dev
);
726 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
730 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
734 disable_irq(client
->irq
);
736 data
->baseline_ready
= false;
738 data
->mode
|= ETP_ENABLE_CALIBRATE
;
739 retval
= data
->ops
->set_mode(data
->client
, data
->mode
);
741 dev_err(dev
, "Failed to enable calibration mode to get baseline: %d\n",
748 retval
= data
->ops
->get_baseline_data(data
->client
, true,
749 &data
->max_baseline
);
751 dev_err(dev
, "Failed to read max baseline form device: %d\n",
753 goto out_disable_calibrate
;
756 retval
= data
->ops
->get_baseline_data(data
->client
, false,
757 &data
->min_baseline
);
759 dev_err(dev
, "Failed to read min baseline form device: %d\n",
761 goto out_disable_calibrate
;
764 data
->baseline_ready
= true;
766 out_disable_calibrate
:
767 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
768 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
770 dev_err(dev
, "Failed to disable calibration mode after acquiring baseline: %d\n",
776 enable_irq(client
->irq
);
777 mutex_unlock(&data
->sysfs_mutex
);
778 return retval
?: count
;
781 static ssize_t
min_show(struct device
*dev
,
782 struct device_attribute
*attr
, char *buf
)
784 struct i2c_client
*client
= to_i2c_client(dev
);
785 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
788 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
792 if (!data
->baseline_ready
) {
797 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->min_baseline
);
800 mutex_unlock(&data
->sysfs_mutex
);
804 static ssize_t
max_show(struct device
*dev
,
805 struct device_attribute
*attr
, char *buf
)
807 struct i2c_client
*client
= to_i2c_client(dev
);
808 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
811 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
815 if (!data
->baseline_ready
) {
820 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->max_baseline
);
823 mutex_unlock(&data
->sysfs_mutex
);
828 static DEVICE_ATTR_WO(acquire
);
829 static DEVICE_ATTR_RO(min
);
830 static DEVICE_ATTR_RO(max
);
832 static struct attribute
*elan_baseline_sysfs_entries
[] = {
833 &dev_attr_acquire
.attr
,
839 static const struct attribute_group elan_baseline_sysfs_group
= {
841 .attrs
= elan_baseline_sysfs_entries
,
844 static const struct attribute_group
*elan_sysfs_groups
[] = {
846 &elan_baseline_sysfs_group
,
851 ******************************************************************
853 ******************************************************************
855 static void elan_report_contact(struct elan_tp_data
*data
,
856 int contact_num
, bool contact_valid
,
859 struct input_dev
*input
= data
->input
;
860 unsigned int pos_x
, pos_y
;
861 unsigned int pressure
, mk_x
, mk_y
;
862 unsigned int area_x
, area_y
, major
, minor
;
863 unsigned int scaled_pressure
;
866 pos_x
= ((finger_data
[0] & 0xf0) << 4) |
868 pos_y
= ((finger_data
[0] & 0x0f) << 8) |
870 mk_x
= (finger_data
[3] & 0x0f);
871 mk_y
= (finger_data
[3] >> 4);
872 pressure
= finger_data
[4];
874 if (pos_x
> data
->max_x
|| pos_y
> data
->max_y
) {
875 dev_dbg(input
->dev
.parent
,
876 "[%d] x=%d y=%d over max (%d, %d)",
877 contact_num
, pos_x
, pos_y
,
878 data
->max_x
, data
->max_y
);
883 * To avoid treating large finger as palm, let's reduce the
884 * width x and y per trace.
886 area_x
= mk_x
* (data
->width_x
- ETP_FWIDTH_REDUCE
);
887 area_y
= mk_y
* (data
->width_y
- ETP_FWIDTH_REDUCE
);
889 major
= max(area_x
, area_y
);
890 minor
= min(area_x
, area_y
);
892 scaled_pressure
= pressure
+ data
->pressure_adjustment
;
894 if (scaled_pressure
> ETP_MAX_PRESSURE
)
895 scaled_pressure
= ETP_MAX_PRESSURE
;
897 input_mt_slot(input
, contact_num
);
898 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
899 input_report_abs(input
, ABS_MT_POSITION_X
, pos_x
);
900 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->max_y
- pos_y
);
901 input_report_abs(input
, ABS_MT_PRESSURE
, scaled_pressure
);
902 input_report_abs(input
, ABS_TOOL_WIDTH
, mk_x
);
903 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, major
);
904 input_report_abs(input
, ABS_MT_TOUCH_MINOR
, minor
);
906 input_mt_slot(input
, contact_num
);
907 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, false);
911 static void elan_report_absolute(struct elan_tp_data
*data
, u8
*packet
)
913 struct input_dev
*input
= data
->input
;
914 u8
*finger_data
= &packet
[ETP_FINGER_DATA_OFFSET
];
916 u8 tp_info
= packet
[ETP_TOUCH_INFO_OFFSET
];
917 u8 hover_info
= packet
[ETP_HOVER_INFO_OFFSET
];
918 bool contact_valid
, hover_event
;
920 hover_event
= hover_info
& 0x40;
921 for (i
= 0; i
< ETP_MAX_FINGERS
; i
++) {
922 contact_valid
= tp_info
& (1U << (3 + i
));
923 elan_report_contact(data
, i
, contact_valid
, finger_data
);
926 finger_data
+= ETP_FINGER_DATA_LEN
;
929 input_report_key(input
, BTN_LEFT
, tp_info
& 0x01);
930 input_report_key(input
, BTN_RIGHT
, tp_info
& 0x02);
931 input_report_abs(input
, ABS_DISTANCE
, hover_event
!= 0);
932 input_mt_report_pointer_emulation(input
, true);
936 static void elan_report_trackpoint(struct elan_tp_data
*data
, u8
*report
)
938 struct input_dev
*input
= data
->tp_input
;
939 u8
*packet
= &report
[ETP_REPORT_ID_OFFSET
+ 1];
942 if (!data
->tp_input
) {
943 dev_warn_once(&data
->client
->dev
,
944 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
948 input_report_key(input
, BTN_LEFT
, packet
[0] & 0x01);
949 input_report_key(input
, BTN_RIGHT
, packet
[0] & 0x02);
950 input_report_key(input
, BTN_MIDDLE
, packet
[0] & 0x04);
952 if ((packet
[3] & 0x0F) == 0x06) {
953 x
= packet
[4] - (int)((packet
[1] ^ 0x80) << 1);
954 y
= (int)((packet
[2] ^ 0x80) << 1) - packet
[5];
956 input_report_rel(input
, REL_X
, x
);
957 input_report_rel(input
, REL_Y
, y
);
963 static irqreturn_t
elan_isr(int irq
, void *dev_id
)
965 struct elan_tp_data
*data
= dev_id
;
966 struct device
*dev
= &data
->client
->dev
;
968 u8 report
[ETP_MAX_REPORT_LEN
];
971 * When device is connected to i2c bus, when all IAP page writes
972 * complete, the driver will receive interrupt and must read
973 * 0000 to confirm that IAP is finished.
975 if (data
->in_fw_update
) {
976 complete(&data
->fw_completion
);
980 error
= data
->ops
->get_report(data
->client
, report
);
984 switch (report
[ETP_REPORT_ID_OFFSET
]) {
986 elan_report_absolute(data
, report
);
988 case ETP_TP_REPORT_ID
:
989 elan_report_trackpoint(data
, report
);
992 dev_err(dev
, "invalid report id data (%x)\n",
993 report
[ETP_REPORT_ID_OFFSET
]);
1001 ******************************************************************
1002 * Elan initialization functions
1003 ******************************************************************
1006 static int elan_setup_trackpoint_input_device(struct elan_tp_data
*data
)
1008 struct device
*dev
= &data
->client
->dev
;
1009 struct input_dev
*input
;
1011 input
= devm_input_allocate_device(dev
);
1015 input
->name
= "Elan TrackPoint";
1016 input
->id
.bustype
= BUS_I2C
;
1017 input
->id
.vendor
= ELAN_VENDOR_ID
;
1018 input
->id
.product
= data
->product_id
;
1019 input_set_drvdata(input
, data
);
1021 input_set_capability(input
, EV_REL
, REL_X
);
1022 input_set_capability(input
, EV_REL
, REL_Y
);
1023 input_set_capability(input
, EV_KEY
, BTN_LEFT
);
1024 input_set_capability(input
, EV_KEY
, BTN_RIGHT
);
1025 input_set_capability(input
, EV_KEY
, BTN_MIDDLE
);
1027 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
1028 __set_bit(INPUT_PROP_POINTING_STICK
, input
->propbit
);
1030 data
->tp_input
= input
;
1035 static int elan_setup_input_device(struct elan_tp_data
*data
)
1037 struct device
*dev
= &data
->client
->dev
;
1038 struct input_dev
*input
;
1039 unsigned int max_width
= max(data
->width_x
, data
->width_y
);
1040 unsigned int min_width
= min(data
->width_x
, data
->width_y
);
1043 input
= devm_input_allocate_device(dev
);
1047 input
->name
= "Elan Touchpad";
1048 input
->id
.bustype
= BUS_I2C
;
1049 input
->id
.vendor
= ELAN_VENDOR_ID
;
1050 input
->id
.product
= data
->product_id
;
1051 input_set_drvdata(input
, data
);
1053 error
= input_mt_init_slots(input
, ETP_MAX_FINGERS
,
1054 INPUT_MT_POINTER
| INPUT_MT_DROP_UNUSED
);
1056 dev_err(dev
, "failed to initialize MT slots: %d\n", error
);
1060 __set_bit(EV_ABS
, input
->evbit
);
1061 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
1063 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
1065 __set_bit(BTN_RIGHT
, input
->keybit
);
1066 __set_bit(BTN_LEFT
, input
->keybit
);
1068 /* Set up ST parameters */
1069 input_set_abs_params(input
, ABS_X
, 0, data
->max_x
, 0, 0);
1070 input_set_abs_params(input
, ABS_Y
, 0, data
->max_y
, 0, 0);
1071 input_abs_set_res(input
, ABS_X
, data
->x_res
);
1072 input_abs_set_res(input
, ABS_Y
, data
->y_res
);
1073 input_set_abs_params(input
, ABS_PRESSURE
, 0, ETP_MAX_PRESSURE
, 0, 0);
1074 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0, ETP_FINGER_WIDTH
, 0, 0);
1075 input_set_abs_params(input
, ABS_DISTANCE
, 0, 1, 0, 0);
1077 /* And MT parameters */
1078 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, data
->max_x
, 0, 0);
1079 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, data
->max_y
, 0, 0);
1080 input_abs_set_res(input
, ABS_MT_POSITION_X
, data
->x_res
);
1081 input_abs_set_res(input
, ABS_MT_POSITION_Y
, data
->y_res
);
1082 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0,
1083 ETP_MAX_PRESSURE
, 0, 0);
1084 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0,
1085 ETP_FINGER_WIDTH
* max_width
, 0, 0);
1086 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0,
1087 ETP_FINGER_WIDTH
* min_width
, 0, 0);
1089 data
->input
= input
;
1094 static void elan_disable_regulator(void *_data
)
1096 struct elan_tp_data
*data
= _data
;
1098 regulator_disable(data
->vcc
);
1101 static void elan_remove_sysfs_groups(void *_data
)
1103 struct elan_tp_data
*data
= _data
;
1105 sysfs_remove_groups(&data
->client
->dev
.kobj
, elan_sysfs_groups
);
1108 static int elan_probe(struct i2c_client
*client
,
1109 const struct i2c_device_id
*dev_id
)
1111 const struct elan_transport_ops
*transport_ops
;
1112 struct device
*dev
= &client
->dev
;
1113 struct elan_tp_data
*data
;
1114 unsigned long irqflags
;
1117 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C
) &&
1118 i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1119 transport_ops
= &elan_i2c_ops
;
1120 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS
) &&
1121 i2c_check_functionality(client
->adapter
,
1122 I2C_FUNC_SMBUS_BYTE_DATA
|
1123 I2C_FUNC_SMBUS_BLOCK_DATA
|
1124 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1125 transport_ops
= &elan_smbus_ops
;
1127 dev_err(dev
, "not a supported I2C/SMBus adapter\n");
1131 data
= devm_kzalloc(dev
, sizeof(struct elan_tp_data
), GFP_KERNEL
);
1135 i2c_set_clientdata(client
, data
);
1137 data
->ops
= transport_ops
;
1138 data
->client
= client
;
1139 init_completion(&data
->fw_completion
);
1140 mutex_init(&data
->sysfs_mutex
);
1142 data
->vcc
= devm_regulator_get(dev
, "vcc");
1143 if (IS_ERR(data
->vcc
)) {
1144 error
= PTR_ERR(data
->vcc
);
1145 if (error
!= -EPROBE_DEFER
)
1146 dev_err(dev
, "Failed to get 'vcc' regulator: %d\n",
1151 error
= regulator_enable(data
->vcc
);
1153 dev_err(dev
, "Failed to enable regulator: %d\n", error
);
1157 error
= devm_add_action(dev
, elan_disable_regulator
, data
);
1159 regulator_disable(data
->vcc
);
1160 dev_err(dev
, "Failed to add disable regulator action: %d\n",
1165 /* Make sure there is something at this address */
1166 error
= i2c_smbus_read_byte(client
);
1168 dev_dbg(&client
->dev
, "nothing at this address: %d\n", error
);
1172 /* Initialize the touchpad. */
1173 error
= elan_initialize(data
);
1177 error
= elan_query_device_info(data
);
1181 error
= elan_query_device_parameters(data
);
1186 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1193 "Elan Touchpad Extra Information:\n"
1194 " Max ABS X,Y: %d,%d\n"
1195 " Width X,Y: %d,%d\n"
1196 " Resolution X,Y: %d,%d (dots/mm)\n"
1198 " info pattern: 0x%x\n",
1199 data
->max_x
, data
->max_y
,
1200 data
->width_x
, data
->width_y
,
1201 data
->x_res
, data
->y_res
,
1202 data
->ic_type
, data
->pattern
);
1204 /* Set up input device properties based on queried parameters. */
1205 error
= elan_setup_input_device(data
);
1209 if (device_property_read_bool(&client
->dev
, "elan,trackpoint")) {
1210 error
= elan_setup_trackpoint_input_device(data
);
1216 * Platform code (ACPI, DTS) should normally set up interrupt
1217 * for us, but in case it did not let's fall back to using falling
1218 * edge to be compatible with older Chromebooks.
1220 irqflags
= irq_get_trigger_type(client
->irq
);
1222 irqflags
= IRQF_TRIGGER_FALLING
;
1224 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, elan_isr
,
1225 irqflags
| IRQF_ONESHOT
,
1226 client
->name
, data
);
1228 dev_err(dev
, "cannot register irq=%d\n", client
->irq
);
1232 error
= sysfs_create_groups(&dev
->kobj
, elan_sysfs_groups
);
1234 dev_err(dev
, "failed to create sysfs attributes: %d\n", error
);
1238 error
= devm_add_action(dev
, elan_remove_sysfs_groups
, data
);
1240 elan_remove_sysfs_groups(data
);
1241 dev_err(dev
, "Failed to add sysfs cleanup action: %d\n",
1246 error
= input_register_device(data
->input
);
1248 dev_err(dev
, "failed to register input device: %d\n", error
);
1252 if (data
->tp_input
) {
1253 error
= input_register_device(data
->tp_input
);
1255 dev_err(&client
->dev
,
1256 "failed to register TrackPoint input device: %d\n",
1263 * Systems using device tree should set up wakeup via DTS,
1264 * the rest will configure device as wakeup source by default.
1267 device_init_wakeup(dev
, true);
1272 static int __maybe_unused
elan_suspend(struct device
*dev
)
1274 struct i2c_client
*client
= to_i2c_client(dev
);
1275 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1279 * We are taking the mutex to make sure sysfs operations are
1280 * complete before we attempt to bring the device into low[er]
1283 ret
= mutex_lock_interruptible(&data
->sysfs_mutex
);
1287 disable_irq(client
->irq
);
1289 if (device_may_wakeup(dev
)) {
1290 ret
= elan_sleep(data
);
1291 /* Enable wake from IRQ */
1292 data
->irq_wake
= (enable_irq_wake(client
->irq
) == 0);
1294 ret
= elan_disable_power(data
);
1297 mutex_unlock(&data
->sysfs_mutex
);
1301 static int __maybe_unused
elan_resume(struct device
*dev
)
1303 struct i2c_client
*client
= to_i2c_client(dev
);
1304 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1307 if (device_may_wakeup(dev
) && data
->irq_wake
) {
1308 disable_irq_wake(client
->irq
);
1309 data
->irq_wake
= false;
1312 error
= elan_enable_power(data
);
1314 dev_err(dev
, "power up when resuming failed: %d\n", error
);
1318 error
= elan_initialize(data
);
1320 dev_err(dev
, "initialize when resuming failed: %d\n", error
);
1323 enable_irq(data
->client
->irq
);
1327 static SIMPLE_DEV_PM_OPS(elan_pm_ops
, elan_suspend
, elan_resume
);
1329 static const struct i2c_device_id elan_id
[] = {
1333 MODULE_DEVICE_TABLE(i2c
, elan_id
);
1336 static const struct acpi_device_id elan_acpi_id
[] = {
1360 MODULE_DEVICE_TABLE(acpi
, elan_acpi_id
);
1364 static const struct of_device_id elan_of_match
[] = {
1365 { .compatible
= "elan,ekth3000" },
1368 MODULE_DEVICE_TABLE(of
, elan_of_match
);
1371 static struct i2c_driver elan_driver
= {
1373 .name
= DRIVER_NAME
,
1375 .acpi_match_table
= ACPI_PTR(elan_acpi_id
),
1376 .of_match_table
= of_match_ptr(elan_of_match
),
1377 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1379 .probe
= elan_probe
,
1380 .id_table
= elan_id
,
1383 module_i2c_driver(elan_driver
);
1385 MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1386 MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1387 MODULE_LICENSE("GPL");