1 // SPDX-License-Identifier: GPL-2.0-only
3 * Elan I2C/SMBus Touchpad driver
5 * Copyright (c) 2013 ELAN Microelectronics Corp.
7 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
8 * Author: KT Liao <kt.liao@emc.com.tw>
11 * Based on cyapa driver:
12 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
13 * copyright (c) 2011-2012 Google, Inc.
15 * Trademarks are the property of their respective owners.
18 #include <linux/acpi.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/firmware.h>
22 #include <linux/i2c.h>
23 #include <linux/init.h>
24 #include <linux/input/mt.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/input.h>
32 #include <linux/uaccess.h>
33 #include <linux/jiffies.h>
34 #include <linux/completion.h>
36 #include <linux/property.h>
37 #include <linux/input/elan-i2c-ids.h>
38 #include <linux/regulator/consumer.h>
39 #include <asm/unaligned.h>
43 #define DRIVER_NAME "elan_i2c"
44 #define ELAN_VENDOR_ID 0x04f3
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_TP_REPORT_ID 0x5E
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 input_dev
*tp_input
; /* trackpoint input node */
65 struct regulator
*vcc
;
67 const struct elan_transport_ops
*ops
;
70 struct completion fw_completion
;
73 struct mutex sysfs_mutex
;
88 int pressure_adjustment
;
91 u16 fw_validpage_count
;
92 u16 fw_signature_address
;
103 static int elan_get_fwinfo(u16 ic_type
, u16
*validpage_count
,
104 u16
*signature_address
)
110 *validpage_count
= 512;
118 *validpage_count
= 768;
121 *validpage_count
= 896;
124 *validpage_count
= 640;
127 *validpage_count
= 1024;
130 /* unknown ic type clear value */
131 *validpage_count
= 0;
132 *signature_address
= 0;
137 (*validpage_count
* ETP_FW_PAGE_SIZE
) - ETP_FW_SIGNATURE_SIZE
;
142 static int elan_enable_power(struct elan_tp_data
*data
)
144 int repeat
= ETP_RETRY_COUNT
;
147 error
= regulator_enable(data
->vcc
);
149 dev_err(&data
->client
->dev
,
150 "failed to enable regulator: %d\n", error
);
155 error
= data
->ops
->power_control(data
->client
, true);
160 } while (--repeat
> 0);
162 dev_err(&data
->client
->dev
, "failed to enable power: %d\n", error
);
166 static int elan_disable_power(struct elan_tp_data
*data
)
168 int repeat
= ETP_RETRY_COUNT
;
172 error
= data
->ops
->power_control(data
->client
, false);
174 error
= regulator_disable(data
->vcc
);
176 dev_err(&data
->client
->dev
,
177 "failed to disable regulator: %d\n",
179 /* Attempt to power the chip back up */
180 data
->ops
->power_control(data
->client
, true);
188 } while (--repeat
> 0);
190 dev_err(&data
->client
->dev
, "failed to disable power: %d\n", error
);
194 static int elan_sleep(struct elan_tp_data
*data
)
196 int repeat
= ETP_RETRY_COUNT
;
200 error
= data
->ops
->sleep_control(data
->client
, true);
205 } while (--repeat
> 0);
210 static int elan_query_product(struct elan_tp_data
*data
)
214 error
= data
->ops
->get_product_id(data
->client
, &data
->product_id
);
218 error
= data
->ops
->get_sm_version(data
->client
, &data
->ic_type
,
219 &data
->sm_version
, &data
->clickpad
);
226 static int elan_check_ASUS_special_fw(struct elan_tp_data
*data
)
228 if (data
->ic_type
== 0x0E) {
229 switch (data
->product_id
) {
235 } else if (data
->ic_type
== 0x08 && data
->product_id
== 0x26) {
236 /* ASUS EeeBook X205TA */
243 static int __elan_initialize(struct elan_tp_data
*data
)
245 struct i2c_client
*client
= data
->client
;
246 bool woken_up
= false;
249 error
= data
->ops
->initialize(client
);
251 dev_err(&client
->dev
, "device initialize failed: %d\n", error
);
255 error
= elan_query_product(data
);
260 * Some ASUS devices were shipped with firmware that requires
261 * touchpads to be woken up first, before attempting to switch
262 * them into absolute reporting mode.
264 if (elan_check_ASUS_special_fw(data
)) {
265 error
= data
->ops
->sleep_control(client
, false);
267 dev_err(&client
->dev
,
268 "failed to wake device up: %d\n", error
);
276 data
->mode
|= ETP_ENABLE_ABS
;
277 error
= data
->ops
->set_mode(client
, data
->mode
);
279 dev_err(&client
->dev
,
280 "failed to switch to absolute mode: %d\n", error
);
285 error
= data
->ops
->sleep_control(client
, false);
287 dev_err(&client
->dev
,
288 "failed to wake device up: %d\n", error
);
296 static int elan_initialize(struct elan_tp_data
*data
)
298 int repeat
= ETP_RETRY_COUNT
;
302 error
= __elan_initialize(data
);
307 } while (--repeat
> 0);
312 static int elan_query_device_info(struct elan_tp_data
*data
)
317 error
= data
->ops
->get_version(data
->client
, false, &data
->fw_version
);
321 error
= data
->ops
->get_checksum(data
->client
, false,
326 error
= data
->ops
->get_version(data
->client
, true, &data
->iap_version
);
330 error
= data
->ops
->get_pressure_adjustment(data
->client
,
331 &data
->pressure_adjustment
);
335 error
= data
->ops
->get_pattern(data
->client
, &data
->pattern
);
339 if (data
->pattern
== 0x01)
340 ic_type
= data
->ic_type
;
342 ic_type
= data
->iap_version
;
344 error
= elan_get_fwinfo(ic_type
, &data
->fw_validpage_count
,
345 &data
->fw_signature_address
);
347 dev_warn(&data
->client
->dev
,
348 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
349 data
->iap_version
, data
->ic_type
);
354 static unsigned int elan_convert_resolution(u8 val
)
357 * (value from firmware) * 10 + 790 = dpi
359 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
363 return ((int)(char)val
* 10 + 790) * 10 / 254;
366 static int elan_query_device_parameters(struct elan_tp_data
*data
)
368 struct i2c_client
*client
= data
->client
;
369 unsigned int x_traces
, y_traces
;
371 u8 hw_x_res
, hw_y_res
;
374 if (device_property_read_u32(&client
->dev
,
375 "touchscreen-size-x", &data
->max_x
) ||
376 device_property_read_u32(&client
->dev
,
377 "touchscreen-size-y", &data
->max_y
)) {
378 error
= data
->ops
->get_max(data
->client
,
384 /* size is the maximum + 1 */
389 if (device_property_read_u32(&client
->dev
,
392 device_property_read_u32(&client
->dev
,
395 error
= data
->ops
->get_num_traces(data
->client
,
396 &x_traces
, &y_traces
);
400 data
->width_x
= data
->max_x
/ x_traces
;
401 data
->width_y
= data
->max_y
/ y_traces
;
403 if (device_property_read_u32(&client
->dev
,
404 "touchscreen-x-mm", &x_mm
) ||
405 device_property_read_u32(&client
->dev
,
406 "touchscreen-y-mm", &y_mm
)) {
407 error
= data
->ops
->get_resolution(data
->client
,
408 &hw_x_res
, &hw_y_res
);
412 data
->x_res
= elan_convert_resolution(hw_x_res
);
413 data
->y_res
= elan_convert_resolution(hw_y_res
);
415 data
->x_res
= (data
->max_x
+ 1) / x_mm
;
416 data
->y_res
= (data
->max_y
+ 1) / y_mm
;
419 if (device_property_read_bool(&client
->dev
, "elan,clickpad"))
422 if (device_property_read_bool(&client
->dev
, "elan,middle-button"))
423 data
->middle_button
= true;
429 **********************************************************
430 * IAP firmware updater related routines
431 **********************************************************
433 static int elan_write_fw_block(struct elan_tp_data
*data
,
434 const u8
*page
, u16 checksum
, int idx
)
436 int retry
= ETP_RETRY_COUNT
;
440 error
= data
->ops
->write_fw_block(data
->client
,
441 page
, checksum
, idx
);
445 dev_dbg(&data
->client
->dev
,
446 "IAP retrying page %d (error: %d)\n", idx
, error
);
447 } while (--retry
> 0);
452 static int __elan_update_firmware(struct elan_tp_data
*data
,
453 const struct firmware
*fw
)
455 struct i2c_client
*client
= data
->client
;
456 struct device
*dev
= &client
->dev
;
461 u16 sw_checksum
= 0, fw_checksum
= 0;
463 error
= data
->ops
->prepare_fw_update(client
);
467 iap_start_addr
= get_unaligned_le16(&fw
->data
[ETP_IAP_START_ADDR
* 2]);
469 boot_page_count
= (iap_start_addr
* 2) / ETP_FW_PAGE_SIZE
;
470 for (i
= boot_page_count
; i
< data
->fw_validpage_count
; i
++) {
472 const u8
*page
= &fw
->data
[i
* ETP_FW_PAGE_SIZE
];
474 for (j
= 0; j
< ETP_FW_PAGE_SIZE
; j
+= 2)
475 checksum
+= ((page
[j
+ 1] << 8) | page
[j
]);
477 error
= elan_write_fw_block(data
, page
, checksum
, i
);
479 dev_err(dev
, "write page %d fail: %d\n", i
, error
);
483 sw_checksum
+= checksum
;
486 /* Wait WDT reset and power on reset */
489 error
= data
->ops
->finish_fw_update(client
, &data
->fw_completion
);
493 error
= data
->ops
->get_checksum(client
, true, &fw_checksum
);
497 if (sw_checksum
!= fw_checksum
) {
498 dev_err(dev
, "checksum diff sw=[%04X], fw=[%04X]\n",
499 sw_checksum
, fw_checksum
);
506 static int elan_update_firmware(struct elan_tp_data
*data
,
507 const struct firmware
*fw
)
509 struct i2c_client
*client
= data
->client
;
512 dev_dbg(&client
->dev
, "Starting firmware update....\n");
514 disable_irq(client
->irq
);
515 data
->in_fw_update
= true;
517 retval
= __elan_update_firmware(data
, fw
);
519 dev_err(&client
->dev
, "firmware update failed: %d\n", retval
);
520 data
->ops
->iap_reset(client
);
522 /* Reinitialize TP after fw is updated */
523 elan_initialize(data
);
524 elan_query_device_info(data
);
527 data
->in_fw_update
= false;
528 enable_irq(client
->irq
);
534 *******************************************************************
536 *******************************************************************
538 static ssize_t
elan_sysfs_read_fw_checksum(struct device
*dev
,
539 struct device_attribute
*attr
,
542 struct i2c_client
*client
= to_i2c_client(dev
);
543 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
545 return sprintf(buf
, "0x%04x\n", data
->fw_checksum
);
548 static ssize_t
elan_sysfs_read_product_id(struct device
*dev
,
549 struct device_attribute
*attr
,
552 struct i2c_client
*client
= to_i2c_client(dev
);
553 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
555 return sprintf(buf
, ETP_PRODUCT_ID_FORMAT_STRING
"\n",
559 static ssize_t
elan_sysfs_read_fw_ver(struct device
*dev
,
560 struct device_attribute
*attr
,
563 struct i2c_client
*client
= to_i2c_client(dev
);
564 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
566 return sprintf(buf
, "%d.0\n", data
->fw_version
);
569 static ssize_t
elan_sysfs_read_sm_ver(struct device
*dev
,
570 struct device_attribute
*attr
,
573 struct i2c_client
*client
= to_i2c_client(dev
);
574 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
576 return sprintf(buf
, "%d.0\n", data
->sm_version
);
579 static ssize_t
elan_sysfs_read_iap_ver(struct device
*dev
,
580 struct device_attribute
*attr
,
583 struct i2c_client
*client
= to_i2c_client(dev
);
584 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
586 return sprintf(buf
, "%d.0\n", data
->iap_version
);
589 static ssize_t
elan_sysfs_update_fw(struct device
*dev
,
590 struct device_attribute
*attr
,
591 const char *buf
, size_t count
)
593 struct elan_tp_data
*data
= dev_get_drvdata(dev
);
594 const struct firmware
*fw
;
597 const u8
*fw_signature
;
598 static const u8 signature
[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
600 if (data
->fw_validpage_count
== 0)
603 /* Look for a firmware with the product id appended. */
604 fw_name
= kasprintf(GFP_KERNEL
, ETP_FW_NAME
, data
->product_id
);
606 dev_err(dev
, "failed to allocate memory for firmware name\n");
610 dev_info(dev
, "requesting fw '%s'\n", fw_name
);
611 error
= request_firmware(&fw
, fw_name
, dev
);
614 dev_err(dev
, "failed to request firmware: %d\n", error
);
618 /* Firmware file must match signature data */
619 fw_signature
= &fw
->data
[data
->fw_signature_address
];
620 if (memcmp(fw_signature
, signature
, sizeof(signature
)) != 0) {
621 dev_err(dev
, "signature mismatch (expected %*ph, got %*ph)\n",
622 (int)sizeof(signature
), signature
,
623 (int)sizeof(signature
), fw_signature
);
628 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
632 error
= elan_update_firmware(data
, fw
);
634 mutex_unlock(&data
->sysfs_mutex
);
637 release_firmware(fw
);
638 return error
?: count
;
641 static ssize_t
calibrate_store(struct device
*dev
,
642 struct device_attribute
*attr
,
643 const char *buf
, size_t count
)
645 struct i2c_client
*client
= to_i2c_client(dev
);
646 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
650 u8 val
[ETP_CALIBRATE_MAX_LEN
];
652 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
656 disable_irq(client
->irq
);
658 data
->mode
|= ETP_ENABLE_CALIBRATE
;
659 retval
= data
->ops
->set_mode(client
, data
->mode
);
661 dev_err(dev
, "failed to enable calibration mode: %d\n",
666 retval
= data
->ops
->calibrate(client
);
668 dev_err(dev
, "failed to start calibration: %d\n",
670 goto out_disable_calibrate
;
675 /* Wait 250ms before checking if calibration has completed. */
678 retval
= data
->ops
->calibrate_result(client
, val
);
680 dev_err(dev
, "failed to check calibration result: %d\n",
682 else if (val
[0] == 0)
683 break; /* calibration done */
688 dev_err(dev
, "failed to calibrate. Timeout.\n");
692 out_disable_calibrate
:
693 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
694 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
696 dev_err(dev
, "failed to disable calibration mode: %d\n",
702 enable_irq(client
->irq
);
703 mutex_unlock(&data
->sysfs_mutex
);
704 return retval
?: count
;
707 static ssize_t
elan_sysfs_read_mode(struct device
*dev
,
708 struct device_attribute
*attr
,
711 struct i2c_client
*client
= to_i2c_client(dev
);
712 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
716 error
= mutex_lock_interruptible(&data
->sysfs_mutex
);
720 error
= data
->ops
->iap_get_mode(data
->client
, &mode
);
722 mutex_unlock(&data
->sysfs_mutex
);
727 return sprintf(buf
, "%d\n", (int)mode
);
730 static DEVICE_ATTR(product_id
, S_IRUGO
, elan_sysfs_read_product_id
, NULL
);
731 static DEVICE_ATTR(firmware_version
, S_IRUGO
, elan_sysfs_read_fw_ver
, NULL
);
732 static DEVICE_ATTR(sample_version
, S_IRUGO
, elan_sysfs_read_sm_ver
, NULL
);
733 static DEVICE_ATTR(iap_version
, S_IRUGO
, elan_sysfs_read_iap_ver
, NULL
);
734 static DEVICE_ATTR(fw_checksum
, S_IRUGO
, elan_sysfs_read_fw_checksum
, NULL
);
735 static DEVICE_ATTR(mode
, S_IRUGO
, elan_sysfs_read_mode
, NULL
);
736 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, elan_sysfs_update_fw
);
738 static DEVICE_ATTR_WO(calibrate
);
740 static struct attribute
*elan_sysfs_entries
[] = {
741 &dev_attr_product_id
.attr
,
742 &dev_attr_firmware_version
.attr
,
743 &dev_attr_sample_version
.attr
,
744 &dev_attr_iap_version
.attr
,
745 &dev_attr_fw_checksum
.attr
,
746 &dev_attr_calibrate
.attr
,
748 &dev_attr_update_fw
.attr
,
752 static const struct attribute_group elan_sysfs_group
= {
753 .attrs
= elan_sysfs_entries
,
756 static ssize_t
acquire_store(struct device
*dev
, struct device_attribute
*attr
,
757 const char *buf
, size_t count
)
759 struct i2c_client
*client
= to_i2c_client(dev
);
760 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
764 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
768 disable_irq(client
->irq
);
770 data
->baseline_ready
= false;
772 data
->mode
|= ETP_ENABLE_CALIBRATE
;
773 retval
= data
->ops
->set_mode(data
->client
, data
->mode
);
775 dev_err(dev
, "Failed to enable calibration mode to get baseline: %d\n",
782 retval
= data
->ops
->get_baseline_data(data
->client
, true,
783 &data
->max_baseline
);
785 dev_err(dev
, "Failed to read max baseline form device: %d\n",
787 goto out_disable_calibrate
;
790 retval
= data
->ops
->get_baseline_data(data
->client
, false,
791 &data
->min_baseline
);
793 dev_err(dev
, "Failed to read min baseline form device: %d\n",
795 goto out_disable_calibrate
;
798 data
->baseline_ready
= true;
800 out_disable_calibrate
:
801 data
->mode
&= ~ETP_ENABLE_CALIBRATE
;
802 error
= data
->ops
->set_mode(data
->client
, data
->mode
);
804 dev_err(dev
, "Failed to disable calibration mode after acquiring baseline: %d\n",
810 enable_irq(client
->irq
);
811 mutex_unlock(&data
->sysfs_mutex
);
812 return retval
?: count
;
815 static ssize_t
min_show(struct device
*dev
,
816 struct device_attribute
*attr
, char *buf
)
818 struct i2c_client
*client
= to_i2c_client(dev
);
819 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
822 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
826 if (!data
->baseline_ready
) {
831 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->min_baseline
);
834 mutex_unlock(&data
->sysfs_mutex
);
838 static ssize_t
max_show(struct device
*dev
,
839 struct device_attribute
*attr
, char *buf
)
841 struct i2c_client
*client
= to_i2c_client(dev
);
842 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
845 retval
= mutex_lock_interruptible(&data
->sysfs_mutex
);
849 if (!data
->baseline_ready
) {
854 retval
= snprintf(buf
, PAGE_SIZE
, "%d", data
->max_baseline
);
857 mutex_unlock(&data
->sysfs_mutex
);
862 static DEVICE_ATTR_WO(acquire
);
863 static DEVICE_ATTR_RO(min
);
864 static DEVICE_ATTR_RO(max
);
866 static struct attribute
*elan_baseline_sysfs_entries
[] = {
867 &dev_attr_acquire
.attr
,
873 static const struct attribute_group elan_baseline_sysfs_group
= {
875 .attrs
= elan_baseline_sysfs_entries
,
878 static const struct attribute_group
*elan_sysfs_groups
[] = {
880 &elan_baseline_sysfs_group
,
885 ******************************************************************
887 ******************************************************************
889 static void elan_report_contact(struct elan_tp_data
*data
,
890 int contact_num
, bool contact_valid
,
893 struct input_dev
*input
= data
->input
;
894 unsigned int pos_x
, pos_y
;
895 unsigned int pressure
, mk_x
, mk_y
;
896 unsigned int area_x
, area_y
, major
, minor
;
897 unsigned int scaled_pressure
;
900 pos_x
= ((finger_data
[0] & 0xf0) << 4) |
902 pos_y
= ((finger_data
[0] & 0x0f) << 8) |
904 mk_x
= (finger_data
[3] & 0x0f);
905 mk_y
= (finger_data
[3] >> 4);
906 pressure
= finger_data
[4];
908 if (pos_x
> data
->max_x
|| pos_y
> data
->max_y
) {
909 dev_dbg(input
->dev
.parent
,
910 "[%d] x=%d y=%d over max (%d, %d)",
911 contact_num
, pos_x
, pos_y
,
912 data
->max_x
, data
->max_y
);
917 * To avoid treating large finger as palm, let's reduce the
918 * width x and y per trace.
920 area_x
= mk_x
* (data
->width_x
- ETP_FWIDTH_REDUCE
);
921 area_y
= mk_y
* (data
->width_y
- ETP_FWIDTH_REDUCE
);
923 major
= max(area_x
, area_y
);
924 minor
= min(area_x
, area_y
);
926 scaled_pressure
= pressure
+ data
->pressure_adjustment
;
928 if (scaled_pressure
> ETP_MAX_PRESSURE
)
929 scaled_pressure
= ETP_MAX_PRESSURE
;
931 input_mt_slot(input
, contact_num
);
932 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, true);
933 input_report_abs(input
, ABS_MT_POSITION_X
, pos_x
);
934 input_report_abs(input
, ABS_MT_POSITION_Y
, data
->max_y
- pos_y
);
935 input_report_abs(input
, ABS_MT_PRESSURE
, scaled_pressure
);
936 input_report_abs(input
, ABS_TOOL_WIDTH
, mk_x
);
937 input_report_abs(input
, ABS_MT_TOUCH_MAJOR
, major
);
938 input_report_abs(input
, ABS_MT_TOUCH_MINOR
, minor
);
940 input_mt_slot(input
, contact_num
);
941 input_mt_report_slot_state(input
, MT_TOOL_FINGER
, false);
945 static void elan_report_absolute(struct elan_tp_data
*data
, u8
*packet
)
947 struct input_dev
*input
= data
->input
;
948 u8
*finger_data
= &packet
[ETP_FINGER_DATA_OFFSET
];
950 u8 tp_info
= packet
[ETP_TOUCH_INFO_OFFSET
];
951 u8 hover_info
= packet
[ETP_HOVER_INFO_OFFSET
];
952 bool contact_valid
, hover_event
;
954 hover_event
= hover_info
& 0x40;
955 for (i
= 0; i
< ETP_MAX_FINGERS
; i
++) {
956 contact_valid
= tp_info
& (1U << (3 + i
));
957 elan_report_contact(data
, i
, contact_valid
, finger_data
);
960 finger_data
+= ETP_FINGER_DATA_LEN
;
963 input_report_key(input
, BTN_LEFT
, tp_info
& BIT(0));
964 input_report_key(input
, BTN_MIDDLE
, tp_info
& BIT(2));
965 input_report_key(input
, BTN_RIGHT
, tp_info
& BIT(1));
966 input_report_abs(input
, ABS_DISTANCE
, hover_event
!= 0);
967 input_mt_report_pointer_emulation(input
, true);
971 static void elan_report_trackpoint(struct elan_tp_data
*data
, u8
*report
)
973 struct input_dev
*input
= data
->tp_input
;
974 u8
*packet
= &report
[ETP_REPORT_ID_OFFSET
+ 1];
977 if (!data
->tp_input
) {
978 dev_warn_once(&data
->client
->dev
,
979 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
983 input_report_key(input
, BTN_LEFT
, packet
[0] & 0x01);
984 input_report_key(input
, BTN_RIGHT
, packet
[0] & 0x02);
985 input_report_key(input
, BTN_MIDDLE
, packet
[0] & 0x04);
987 if ((packet
[3] & 0x0F) == 0x06) {
988 x
= packet
[4] - (int)((packet
[1] ^ 0x80) << 1);
989 y
= (int)((packet
[2] ^ 0x80) << 1) - packet
[5];
991 input_report_rel(input
, REL_X
, x
);
992 input_report_rel(input
, REL_Y
, y
);
998 static irqreturn_t
elan_isr(int irq
, void *dev_id
)
1000 struct elan_tp_data
*data
= dev_id
;
1001 struct device
*dev
= &data
->client
->dev
;
1003 u8 report
[ETP_MAX_REPORT_LEN
];
1006 * When device is connected to i2c bus, when all IAP page writes
1007 * complete, the driver will receive interrupt and must read
1008 * 0000 to confirm that IAP is finished.
1010 if (data
->in_fw_update
) {
1011 complete(&data
->fw_completion
);
1015 error
= data
->ops
->get_report(data
->client
, report
);
1019 pm_wakeup_event(dev
, 0);
1021 switch (report
[ETP_REPORT_ID_OFFSET
]) {
1023 elan_report_absolute(data
, report
);
1025 case ETP_TP_REPORT_ID
:
1026 elan_report_trackpoint(data
, report
);
1029 dev_err(dev
, "invalid report id data (%x)\n",
1030 report
[ETP_REPORT_ID_OFFSET
]);
1038 ******************************************************************
1039 * Elan initialization functions
1040 ******************************************************************
1043 static int elan_setup_trackpoint_input_device(struct elan_tp_data
*data
)
1045 struct device
*dev
= &data
->client
->dev
;
1046 struct input_dev
*input
;
1048 input
= devm_input_allocate_device(dev
);
1052 input
->name
= "Elan TrackPoint";
1053 input
->id
.bustype
= BUS_I2C
;
1054 input
->id
.vendor
= ELAN_VENDOR_ID
;
1055 input
->id
.product
= data
->product_id
;
1056 input_set_drvdata(input
, data
);
1058 input_set_capability(input
, EV_REL
, REL_X
);
1059 input_set_capability(input
, EV_REL
, REL_Y
);
1060 input_set_capability(input
, EV_KEY
, BTN_LEFT
);
1061 input_set_capability(input
, EV_KEY
, BTN_RIGHT
);
1062 input_set_capability(input
, EV_KEY
, BTN_MIDDLE
);
1064 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
1065 __set_bit(INPUT_PROP_POINTING_STICK
, input
->propbit
);
1067 data
->tp_input
= input
;
1072 static int elan_setup_input_device(struct elan_tp_data
*data
)
1074 struct device
*dev
= &data
->client
->dev
;
1075 struct input_dev
*input
;
1076 unsigned int max_width
= max(data
->width_x
, data
->width_y
);
1077 unsigned int min_width
= min(data
->width_x
, data
->width_y
);
1080 input
= devm_input_allocate_device(dev
);
1084 input
->name
= "Elan Touchpad";
1085 input
->id
.bustype
= BUS_I2C
;
1086 input
->id
.vendor
= ELAN_VENDOR_ID
;
1087 input
->id
.product
= data
->product_id
;
1088 input_set_drvdata(input
, data
);
1090 error
= input_mt_init_slots(input
, ETP_MAX_FINGERS
,
1091 INPUT_MT_POINTER
| INPUT_MT_DROP_UNUSED
);
1093 dev_err(dev
, "failed to initialize MT slots: %d\n", error
);
1097 __set_bit(EV_ABS
, input
->evbit
);
1098 __set_bit(INPUT_PROP_POINTER
, input
->propbit
);
1099 if (data
->clickpad
) {
1100 __set_bit(INPUT_PROP_BUTTONPAD
, input
->propbit
);
1102 __set_bit(BTN_RIGHT
, input
->keybit
);
1103 if (data
->middle_button
)
1104 __set_bit(BTN_MIDDLE
, input
->keybit
);
1106 __set_bit(BTN_LEFT
, input
->keybit
);
1108 /* Set up ST parameters */
1109 input_set_abs_params(input
, ABS_X
, 0, data
->max_x
, 0, 0);
1110 input_set_abs_params(input
, ABS_Y
, 0, data
->max_y
, 0, 0);
1111 input_abs_set_res(input
, ABS_X
, data
->x_res
);
1112 input_abs_set_res(input
, ABS_Y
, data
->y_res
);
1113 input_set_abs_params(input
, ABS_PRESSURE
, 0, ETP_MAX_PRESSURE
, 0, 0);
1114 input_set_abs_params(input
, ABS_TOOL_WIDTH
, 0, ETP_FINGER_WIDTH
, 0, 0);
1115 input_set_abs_params(input
, ABS_DISTANCE
, 0, 1, 0, 0);
1117 /* And MT parameters */
1118 input_set_abs_params(input
, ABS_MT_POSITION_X
, 0, data
->max_x
, 0, 0);
1119 input_set_abs_params(input
, ABS_MT_POSITION_Y
, 0, data
->max_y
, 0, 0);
1120 input_abs_set_res(input
, ABS_MT_POSITION_X
, data
->x_res
);
1121 input_abs_set_res(input
, ABS_MT_POSITION_Y
, data
->y_res
);
1122 input_set_abs_params(input
, ABS_MT_PRESSURE
, 0,
1123 ETP_MAX_PRESSURE
, 0, 0);
1124 input_set_abs_params(input
, ABS_MT_TOUCH_MAJOR
, 0,
1125 ETP_FINGER_WIDTH
* max_width
, 0, 0);
1126 input_set_abs_params(input
, ABS_MT_TOUCH_MINOR
, 0,
1127 ETP_FINGER_WIDTH
* min_width
, 0, 0);
1129 data
->input
= input
;
1134 static void elan_disable_regulator(void *_data
)
1136 struct elan_tp_data
*data
= _data
;
1138 regulator_disable(data
->vcc
);
1141 static int elan_probe(struct i2c_client
*client
,
1142 const struct i2c_device_id
*dev_id
)
1144 const struct elan_transport_ops
*transport_ops
;
1145 struct device
*dev
= &client
->dev
;
1146 struct elan_tp_data
*data
;
1147 unsigned long irqflags
;
1150 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C
) &&
1151 i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1152 transport_ops
= &elan_i2c_ops
;
1153 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS
) &&
1154 i2c_check_functionality(client
->adapter
,
1155 I2C_FUNC_SMBUS_BYTE_DATA
|
1156 I2C_FUNC_SMBUS_BLOCK_DATA
|
1157 I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1158 transport_ops
= &elan_smbus_ops
;
1160 dev_err(dev
, "not a supported I2C/SMBus adapter\n");
1164 data
= devm_kzalloc(dev
, sizeof(struct elan_tp_data
), GFP_KERNEL
);
1168 i2c_set_clientdata(client
, data
);
1170 data
->ops
= transport_ops
;
1171 data
->client
= client
;
1172 init_completion(&data
->fw_completion
);
1173 mutex_init(&data
->sysfs_mutex
);
1175 data
->vcc
= devm_regulator_get(dev
, "vcc");
1176 if (IS_ERR(data
->vcc
)) {
1177 error
= PTR_ERR(data
->vcc
);
1178 if (error
!= -EPROBE_DEFER
)
1179 dev_err(dev
, "Failed to get 'vcc' regulator: %d\n",
1184 error
= regulator_enable(data
->vcc
);
1186 dev_err(dev
, "Failed to enable regulator: %d\n", error
);
1190 error
= devm_add_action_or_reset(dev
, elan_disable_regulator
, data
);
1192 dev_err(dev
, "Failed to add disable regulator action: %d\n",
1197 /* Make sure there is something at this address */
1198 error
= i2c_smbus_read_byte(client
);
1200 dev_dbg(&client
->dev
, "nothing at this address: %d\n", error
);
1204 /* Initialize the touchpad. */
1205 error
= elan_initialize(data
);
1209 error
= elan_query_device_info(data
);
1213 error
= elan_query_device_parameters(data
);
1218 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1225 "Elan Touchpad Extra Information:\n"
1226 " Max ABS X,Y: %d,%d\n"
1227 " Width X,Y: %d,%d\n"
1228 " Resolution X,Y: %d,%d (dots/mm)\n"
1230 " info pattern: 0x%x\n",
1231 data
->max_x
, data
->max_y
,
1232 data
->width_x
, data
->width_y
,
1233 data
->x_res
, data
->y_res
,
1234 data
->ic_type
, data
->pattern
);
1236 /* Set up input device properties based on queried parameters. */
1237 error
= elan_setup_input_device(data
);
1241 if (device_property_read_bool(&client
->dev
, "elan,trackpoint")) {
1242 error
= elan_setup_trackpoint_input_device(data
);
1248 * Platform code (ACPI, DTS) should normally set up interrupt
1249 * for us, but in case it did not let's fall back to using falling
1250 * edge to be compatible with older Chromebooks.
1252 irqflags
= irq_get_trigger_type(client
->irq
);
1254 irqflags
= IRQF_TRIGGER_FALLING
;
1256 error
= devm_request_threaded_irq(dev
, client
->irq
, NULL
, elan_isr
,
1257 irqflags
| IRQF_ONESHOT
,
1258 client
->name
, data
);
1260 dev_err(dev
, "cannot register irq=%d\n", client
->irq
);
1264 error
= devm_device_add_groups(dev
, elan_sysfs_groups
);
1266 dev_err(dev
, "failed to create sysfs attributes: %d\n", error
);
1270 error
= input_register_device(data
->input
);
1272 dev_err(dev
, "failed to register input device: %d\n", error
);
1276 if (data
->tp_input
) {
1277 error
= input_register_device(data
->tp_input
);
1279 dev_err(&client
->dev
,
1280 "failed to register TrackPoint input device: %d\n",
1287 * Systems using device tree should set up wakeup via DTS,
1288 * the rest will configure device as wakeup source by default.
1291 device_init_wakeup(dev
, true);
1296 static int __maybe_unused
elan_suspend(struct device
*dev
)
1298 struct i2c_client
*client
= to_i2c_client(dev
);
1299 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1303 * We are taking the mutex to make sure sysfs operations are
1304 * complete before we attempt to bring the device into low[er]
1307 ret
= mutex_lock_interruptible(&data
->sysfs_mutex
);
1311 disable_irq(client
->irq
);
1313 if (device_may_wakeup(dev
)) {
1314 ret
= elan_sleep(data
);
1315 /* Enable wake from IRQ */
1316 data
->irq_wake
= (enable_irq_wake(client
->irq
) == 0);
1318 ret
= elan_disable_power(data
);
1321 mutex_unlock(&data
->sysfs_mutex
);
1325 static int __maybe_unused
elan_resume(struct device
*dev
)
1327 struct i2c_client
*client
= to_i2c_client(dev
);
1328 struct elan_tp_data
*data
= i2c_get_clientdata(client
);
1331 if (device_may_wakeup(dev
) && data
->irq_wake
) {
1332 disable_irq_wake(client
->irq
);
1333 data
->irq_wake
= false;
1336 error
= elan_enable_power(data
);
1338 dev_err(dev
, "power up when resuming failed: %d\n", error
);
1342 error
= elan_initialize(data
);
1344 dev_err(dev
, "initialize when resuming failed: %d\n", error
);
1347 enable_irq(data
->client
->irq
);
1351 static SIMPLE_DEV_PM_OPS(elan_pm_ops
, elan_suspend
, elan_resume
);
1353 static const struct i2c_device_id elan_id
[] = {
1357 MODULE_DEVICE_TABLE(i2c
, elan_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");