Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Elan I2C/SMBus Touchpad driver
4 *
5 * Copyright (c) 2013 ELAN Microelectronics Corp.
6 *
7 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
8 * Author: KT Liao <kt.liao@emc.com.tw>
9 * Version: 1.6.3
10 *
11 * Based on cyapa driver:
12 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
13 * copyright (c) 2011-2012 Google, Inc.
14 *
15 * Trademarks are the property of their respective owners.
16 */
17
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>
35#include <linux/of.h>
36#include <linux/pm_wakeirq.h>
37#include <linux/property.h>
38#include <linux/regulator/consumer.h>
39#include <asm/unaligned.h>
40
41#include "elan_i2c.h"
42
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
49
50/* quirks to control the device */
51#define ETP_QUIRK_QUICK_WAKEUP BIT(0)
52
53/* The main device structure */
54struct elan_tp_data {
55 struct i2c_client *client;
56 struct input_dev *input;
57 struct input_dev *tp_input; /* trackpoint input node */
58 struct regulator *vcc;
59
60 const struct elan_transport_ops *ops;
61
62 /* for fw update */
63 struct completion fw_completion;
64 bool in_fw_update;
65
66 struct mutex sysfs_mutex;
67
68 unsigned int max_x;
69 unsigned int max_y;
70 unsigned int width_x;
71 unsigned int width_y;
72 unsigned int x_res;
73 unsigned int y_res;
74
75 u8 pattern;
76 u16 product_id;
77 u8 fw_version;
78 u8 sm_version;
79 u8 iap_version;
80 u16 fw_checksum;
81 unsigned int report_features;
82 unsigned int report_len;
83 int pressure_adjustment;
84 u8 mode;
85 u16 ic_type;
86 u16 fw_validpage_count;
87 u16 fw_page_size;
88 u32 fw_signature_address;
89
90 u8 min_baseline;
91 u8 max_baseline;
92 bool baseline_ready;
93 u8 clickpad;
94 bool middle_button;
95
96 u32 quirks; /* Various quirks */
97};
98
99static u32 elan_i2c_lookup_quirks(u16 ic_type, u16 product_id)
100{
101 static const struct {
102 u16 ic_type;
103 u16 product_id;
104 u32 quirks;
105 } elan_i2c_quirks[] = {
106 { 0x0D, ETP_PRODUCT_ID_DELBIN, ETP_QUIRK_QUICK_WAKEUP },
107 { 0x0D, ETP_PRODUCT_ID_WHITEBOX, ETP_QUIRK_QUICK_WAKEUP },
108 { 0x10, ETP_PRODUCT_ID_VOXEL, ETP_QUIRK_QUICK_WAKEUP },
109 { 0x14, ETP_PRODUCT_ID_MAGPIE, ETP_QUIRK_QUICK_WAKEUP },
110 { 0x14, ETP_PRODUCT_ID_BOBBA, ETP_QUIRK_QUICK_WAKEUP },
111 };
112 u32 quirks = 0;
113 int i;
114
115 for (i = 0; i < ARRAY_SIZE(elan_i2c_quirks); i++) {
116 if (elan_i2c_quirks[i].ic_type == ic_type &&
117 elan_i2c_quirks[i].product_id == product_id) {
118 quirks = elan_i2c_quirks[i].quirks;
119 }
120 }
121
122 if (ic_type >= 0x0D && product_id >= 0x123)
123 quirks |= ETP_QUIRK_QUICK_WAKEUP;
124
125 return quirks;
126}
127
128static int elan_get_fwinfo(u16 ic_type, u8 iap_version, u16 *validpage_count,
129 u32 *signature_address, u16 *page_size)
130{
131 switch (ic_type) {
132 case 0x00:
133 case 0x06:
134 case 0x08:
135 *validpage_count = 512;
136 break;
137 case 0x03:
138 case 0x07:
139 case 0x09:
140 case 0x0A:
141 case 0x0B:
142 case 0x0C:
143 *validpage_count = 768;
144 break;
145 case 0x0D:
146 *validpage_count = 896;
147 break;
148 case 0x0E:
149 *validpage_count = 640;
150 break;
151 case 0x10:
152 *validpage_count = 1024;
153 break;
154 case 0x11:
155 *validpage_count = 1280;
156 break;
157 case 0x13:
158 *validpage_count = 2048;
159 break;
160 case 0x14:
161 case 0x15:
162 *validpage_count = 1024;
163 break;
164 default:
165 /* unknown ic type clear value */
166 *validpage_count = 0;
167 *signature_address = 0;
168 *page_size = 0;
169 return -ENXIO;
170 }
171
172 *signature_address =
173 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
174
175 if ((ic_type == 0x14 || ic_type == 0x15) && iap_version >= 2) {
176 *validpage_count /= 8;
177 *page_size = ETP_FW_PAGE_SIZE_512;
178 } else if (ic_type >= 0x0D && iap_version >= 1) {
179 *validpage_count /= 2;
180 *page_size = ETP_FW_PAGE_SIZE_128;
181 } else {
182 *page_size = ETP_FW_PAGE_SIZE;
183 }
184
185 return 0;
186}
187
188static int elan_set_power(struct elan_tp_data *data, bool on)
189{
190 int repeat = ETP_RETRY_COUNT;
191 int error;
192
193 do {
194 error = data->ops->power_control(data->client, on);
195 if (error >= 0)
196 return 0;
197
198 msleep(30);
199 } while (--repeat > 0);
200
201 dev_err(&data->client->dev, "failed to set power %s: %d\n",
202 on ? "on" : "off", error);
203 return error;
204}
205
206static int elan_sleep(struct elan_tp_data *data)
207{
208 int repeat = ETP_RETRY_COUNT;
209 int error;
210
211 do {
212 error = data->ops->sleep_control(data->client, true);
213 if (!error)
214 return 0;
215
216 msleep(30);
217 } while (--repeat > 0);
218
219 return error;
220}
221
222static int elan_query_product(struct elan_tp_data *data)
223{
224 int error;
225
226 error = data->ops->get_product_id(data->client, &data->product_id);
227 if (error)
228 return error;
229
230 error = data->ops->get_pattern(data->client, &data->pattern);
231 if (error)
232 return error;
233
234 error = data->ops->get_sm_version(data->client, data->pattern,
235 &data->ic_type, &data->sm_version,
236 &data->clickpad);
237 if (error)
238 return error;
239
240 return 0;
241}
242
243static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
244{
245 if (data->ic_type == 0x0E) {
246 switch (data->product_id) {
247 case 0x05 ... 0x07:
248 case 0x09:
249 case 0x13:
250 return true;
251 }
252 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
253 /* ASUS EeeBook X205TA */
254 return true;
255 }
256
257 return false;
258}
259
260static int __elan_initialize(struct elan_tp_data *data, bool skip_reset)
261{
262 struct i2c_client *client = data->client;
263 bool woken_up = false;
264 int error;
265
266 if (!skip_reset) {
267 error = data->ops->initialize(client);
268 if (error) {
269 dev_err(&client->dev, "device initialize failed: %d\n", error);
270 return error;
271 }
272 }
273
274 error = elan_query_product(data);
275 if (error)
276 return error;
277
278 /*
279 * Some ASUS devices were shipped with firmware that requires
280 * touchpads to be woken up first, before attempting to switch
281 * them into absolute reporting mode.
282 */
283 if (elan_check_ASUS_special_fw(data)) {
284 error = data->ops->sleep_control(client, false);
285 if (error) {
286 dev_err(&client->dev,
287 "failed to wake device up: %d\n", error);
288 return error;
289 }
290
291 msleep(200);
292 woken_up = true;
293 }
294
295 data->mode |= ETP_ENABLE_ABS;
296 error = data->ops->set_mode(client, data->mode);
297 if (error) {
298 dev_err(&client->dev,
299 "failed to switch to absolute mode: %d\n", error);
300 return error;
301 }
302
303 if (!woken_up) {
304 error = data->ops->sleep_control(client, false);
305 if (error) {
306 dev_err(&client->dev,
307 "failed to wake device up: %d\n", error);
308 return error;
309 }
310 }
311
312 return 0;
313}
314
315static int elan_initialize(struct elan_tp_data *data, bool skip_reset)
316{
317 int repeat = ETP_RETRY_COUNT;
318 int error;
319
320 do {
321 error = __elan_initialize(data, skip_reset);
322 if (!error)
323 return 0;
324
325 skip_reset = false;
326 msleep(30);
327 } while (--repeat > 0);
328
329 return error;
330}
331
332static int elan_query_device_info(struct elan_tp_data *data)
333{
334 int error;
335
336 error = data->ops->get_version(data->client, data->pattern, false,
337 &data->fw_version);
338 if (error)
339 return error;
340
341 error = data->ops->get_checksum(data->client, false,
342 &data->fw_checksum);
343 if (error)
344 return error;
345
346 error = data->ops->get_version(data->client, data->pattern,
347 true, &data->iap_version);
348 if (error)
349 return error;
350
351 error = data->ops->get_pressure_adjustment(data->client,
352 &data->pressure_adjustment);
353 if (error)
354 return error;
355
356 error = data->ops->get_report_features(data->client, data->pattern,
357 &data->report_features,
358 &data->report_len);
359 if (error)
360 return error;
361
362 data->quirks = elan_i2c_lookup_quirks(data->ic_type, data->product_id);
363
364 error = elan_get_fwinfo(data->ic_type, data->iap_version,
365 &data->fw_validpage_count,
366 &data->fw_signature_address,
367 &data->fw_page_size);
368 if (error)
369 dev_warn(&data->client->dev,
370 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
371 data->iap_version, data->ic_type);
372
373 return 0;
374}
375
376static unsigned int elan_convert_resolution(u8 val, u8 pattern)
377{
378 /*
379 * pattern <= 0x01:
380 * (value from firmware) * 10 + 790 = dpi
381 * else
382 * ((value from firmware) + 3) * 100 = dpi
383 */
384 int res = pattern <= 0x01 ?
385 (int)(char)val * 10 + 790 : ((int)(char)val + 3) * 100;
386 /*
387 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
388 * point).
389 */
390 return res * 10 / 254;
391}
392
393static int elan_query_device_parameters(struct elan_tp_data *data)
394{
395 struct i2c_client *client = data->client;
396 unsigned int x_traces, y_traces;
397 u32 x_mm, y_mm;
398 u8 hw_x_res, hw_y_res;
399 int error;
400
401 if (device_property_read_u32(&client->dev,
402 "touchscreen-size-x", &data->max_x) ||
403 device_property_read_u32(&client->dev,
404 "touchscreen-size-y", &data->max_y)) {
405 error = data->ops->get_max(data->client,
406 &data->max_x,
407 &data->max_y);
408 if (error)
409 return error;
410 } else {
411 /* size is the maximum + 1 */
412 --data->max_x;
413 --data->max_y;
414 }
415
416 if (device_property_read_u32(&client->dev,
417 "elan,x_traces",
418 &x_traces) ||
419 device_property_read_u32(&client->dev,
420 "elan,y_traces",
421 &y_traces)) {
422 error = data->ops->get_num_traces(data->client,
423 &x_traces, &y_traces);
424 if (error)
425 return error;
426 }
427 data->width_x = data->max_x / x_traces;
428 data->width_y = data->max_y / y_traces;
429
430 if (device_property_read_u32(&client->dev,
431 "touchscreen-x-mm", &x_mm) ||
432 device_property_read_u32(&client->dev,
433 "touchscreen-y-mm", &y_mm)) {
434 error = data->ops->get_resolution(data->client,
435 &hw_x_res, &hw_y_res);
436 if (error)
437 return error;
438
439 data->x_res = elan_convert_resolution(hw_x_res, data->pattern);
440 data->y_res = elan_convert_resolution(hw_y_res, data->pattern);
441 } else {
442 data->x_res = (data->max_x + 1) / x_mm;
443 data->y_res = (data->max_y + 1) / y_mm;
444 }
445
446 if (device_property_read_bool(&client->dev, "elan,clickpad"))
447 data->clickpad = 1;
448
449 if (device_property_read_bool(&client->dev, "elan,middle-button"))
450 data->middle_button = true;
451
452 return 0;
453}
454
455/*
456 **********************************************************
457 * IAP firmware updater related routines
458 **********************************************************
459 */
460static int elan_write_fw_block(struct elan_tp_data *data, u16 page_size,
461 const u8 *page, u16 checksum, int idx)
462{
463 int retry = ETP_RETRY_COUNT;
464 int error;
465
466 do {
467 error = data->ops->write_fw_block(data->client, page_size,
468 page, checksum, idx);
469 if (!error)
470 return 0;
471
472 dev_dbg(&data->client->dev,
473 "IAP retrying page %d (error: %d)\n", idx, error);
474 } while (--retry > 0);
475
476 return error;
477}
478
479static int __elan_update_firmware(struct elan_tp_data *data,
480 const struct firmware *fw)
481{
482 struct i2c_client *client = data->client;
483 struct device *dev = &client->dev;
484 int i, j;
485 int error;
486 u16 iap_start_addr;
487 u16 boot_page_count;
488 u16 sw_checksum = 0, fw_checksum = 0;
489
490 error = data->ops->prepare_fw_update(client, data->ic_type,
491 data->iap_version,
492 data->fw_page_size);
493 if (error)
494 return error;
495
496 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
497
498 boot_page_count = (iap_start_addr * 2) / data->fw_page_size;
499 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
500 u16 checksum = 0;
501 const u8 *page = &fw->data[i * data->fw_page_size];
502
503 for (j = 0; j < data->fw_page_size; j += 2)
504 checksum += ((page[j + 1] << 8) | page[j]);
505
506 error = elan_write_fw_block(data, data->fw_page_size,
507 page, checksum, i);
508 if (error) {
509 dev_err(dev, "write page %d fail: %d\n", i, error);
510 return error;
511 }
512
513 sw_checksum += checksum;
514 }
515
516 /* Wait WDT reset and power on reset */
517 msleep(600);
518
519 error = data->ops->finish_fw_update(client, &data->fw_completion);
520 if (error)
521 return error;
522
523 error = data->ops->get_checksum(client, true, &fw_checksum);
524 if (error)
525 return error;
526
527 if (sw_checksum != fw_checksum) {
528 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
529 sw_checksum, fw_checksum);
530 return -EIO;
531 }
532
533 return 0;
534}
535
536static int elan_update_firmware(struct elan_tp_data *data,
537 const struct firmware *fw)
538{
539 struct i2c_client *client = data->client;
540 int retval;
541
542 dev_dbg(&client->dev, "Starting firmware update....\n");
543
544 disable_irq(client->irq);
545 data->in_fw_update = true;
546
547 retval = __elan_update_firmware(data, fw);
548 if (retval) {
549 dev_err(&client->dev, "firmware update failed: %d\n", retval);
550 data->ops->iap_reset(client);
551 } else {
552 /* Reinitialize TP after fw is updated */
553 elan_initialize(data, false);
554 elan_query_device_info(data);
555 }
556
557 data->in_fw_update = false;
558 enable_irq(client->irq);
559
560 return retval;
561}
562
563/*
564 *******************************************************************
565 * SYSFS attributes
566 *******************************************************************
567 */
568static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
569 struct device_attribute *attr,
570 char *buf)
571{
572 struct i2c_client *client = to_i2c_client(dev);
573 struct elan_tp_data *data = i2c_get_clientdata(client);
574
575 return sysfs_emit(buf, "0x%04x\n", data->fw_checksum);
576}
577
578static ssize_t elan_sysfs_read_product_id(struct device *dev,
579 struct device_attribute *attr,
580 char *buf)
581{
582 struct i2c_client *client = to_i2c_client(dev);
583 struct elan_tp_data *data = i2c_get_clientdata(client);
584
585 return sysfs_emit(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
586 data->product_id);
587}
588
589static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
590 struct device_attribute *attr,
591 char *buf)
592{
593 struct i2c_client *client = to_i2c_client(dev);
594 struct elan_tp_data *data = i2c_get_clientdata(client);
595
596 return sysfs_emit(buf, "%d.0\n", data->fw_version);
597}
598
599static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
600 struct device_attribute *attr,
601 char *buf)
602{
603 struct i2c_client *client = to_i2c_client(dev);
604 struct elan_tp_data *data = i2c_get_clientdata(client);
605
606 return sysfs_emit(buf, "%d.0\n", data->sm_version);
607}
608
609static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
610 struct device_attribute *attr,
611 char *buf)
612{
613 struct i2c_client *client = to_i2c_client(dev);
614 struct elan_tp_data *data = i2c_get_clientdata(client);
615
616 return sysfs_emit(buf, "%d.0\n", data->iap_version);
617}
618
619static ssize_t elan_sysfs_update_fw(struct device *dev,
620 struct device_attribute *attr,
621 const char *buf, size_t count)
622{
623 struct elan_tp_data *data = dev_get_drvdata(dev);
624 const struct firmware *fw;
625 char *fw_name;
626 int error;
627 const u8 *fw_signature;
628 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
629
630 if (data->fw_validpage_count == 0)
631 return -EINVAL;
632
633 /* Look for a firmware with the product id appended. */
634 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
635 if (!fw_name) {
636 dev_err(dev, "failed to allocate memory for firmware name\n");
637 return -ENOMEM;
638 }
639
640 dev_info(dev, "requesting fw '%s'\n", fw_name);
641 error = request_firmware(&fw, fw_name, dev);
642 kfree(fw_name);
643 if (error) {
644 dev_err(dev, "failed to request firmware: %d\n", error);
645 return error;
646 }
647
648 /* Firmware file must match signature data */
649 fw_signature = &fw->data[data->fw_signature_address];
650 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
651 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
652 (int)sizeof(signature), signature,
653 (int)sizeof(signature), fw_signature);
654 error = -EBADF;
655 goto out_release_fw;
656 }
657
658 error = mutex_lock_interruptible(&data->sysfs_mutex);
659 if (error)
660 goto out_release_fw;
661
662 error = elan_update_firmware(data, fw);
663
664 mutex_unlock(&data->sysfs_mutex);
665
666out_release_fw:
667 release_firmware(fw);
668 return error ?: count;
669}
670
671static ssize_t calibrate_store(struct device *dev,
672 struct device_attribute *attr,
673 const char *buf, size_t count)
674{
675 struct i2c_client *client = to_i2c_client(dev);
676 struct elan_tp_data *data = i2c_get_clientdata(client);
677 int tries = 20;
678 int retval;
679 int error;
680 u8 val[ETP_CALIBRATE_MAX_LEN];
681
682 retval = mutex_lock_interruptible(&data->sysfs_mutex);
683 if (retval)
684 return retval;
685
686 disable_irq(client->irq);
687
688 data->mode |= ETP_ENABLE_CALIBRATE;
689 retval = data->ops->set_mode(client, data->mode);
690 if (retval) {
691 dev_err(dev, "failed to enable calibration mode: %d\n",
692 retval);
693 goto out;
694 }
695
696 retval = data->ops->calibrate(client);
697 if (retval) {
698 dev_err(dev, "failed to start calibration: %d\n",
699 retval);
700 goto out_disable_calibrate;
701 }
702
703 val[0] = 0xff;
704 do {
705 /* Wait 250ms before checking if calibration has completed. */
706 msleep(250);
707
708 retval = data->ops->calibrate_result(client, val);
709 if (retval)
710 dev_err(dev, "failed to check calibration result: %d\n",
711 retval);
712 else if (val[0] == 0)
713 break; /* calibration done */
714
715 } while (--tries);
716
717 if (tries == 0) {
718 dev_err(dev, "failed to calibrate. Timeout.\n");
719 retval = -ETIMEDOUT;
720 }
721
722out_disable_calibrate:
723 data->mode &= ~ETP_ENABLE_CALIBRATE;
724 error = data->ops->set_mode(data->client, data->mode);
725 if (error) {
726 dev_err(dev, "failed to disable calibration mode: %d\n",
727 error);
728 if (!retval)
729 retval = error;
730 }
731out:
732 enable_irq(client->irq);
733 mutex_unlock(&data->sysfs_mutex);
734 return retval ?: count;
735}
736
737static ssize_t elan_sysfs_read_mode(struct device *dev,
738 struct device_attribute *attr,
739 char *buf)
740{
741 struct i2c_client *client = to_i2c_client(dev);
742 struct elan_tp_data *data = i2c_get_clientdata(client);
743 int error;
744 enum tp_mode mode;
745
746 error = mutex_lock_interruptible(&data->sysfs_mutex);
747 if (error)
748 return error;
749
750 error = data->ops->iap_get_mode(data->client, &mode);
751
752 mutex_unlock(&data->sysfs_mutex);
753
754 if (error)
755 return error;
756
757 return sysfs_emit(buf, "%d\n", (int)mode);
758}
759
760static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
761static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
762static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
763static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
764static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
765static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
766static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
767
768static DEVICE_ATTR_WO(calibrate);
769
770static struct attribute *elan_sysfs_entries[] = {
771 &dev_attr_product_id.attr,
772 &dev_attr_firmware_version.attr,
773 &dev_attr_sample_version.attr,
774 &dev_attr_iap_version.attr,
775 &dev_attr_fw_checksum.attr,
776 &dev_attr_calibrate.attr,
777 &dev_attr_mode.attr,
778 &dev_attr_update_fw.attr,
779 NULL,
780};
781
782static const struct attribute_group elan_sysfs_group = {
783 .attrs = elan_sysfs_entries,
784};
785
786static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
787 const char *buf, size_t count)
788{
789 struct i2c_client *client = to_i2c_client(dev);
790 struct elan_tp_data *data = i2c_get_clientdata(client);
791 int error;
792 int retval;
793
794 retval = mutex_lock_interruptible(&data->sysfs_mutex);
795 if (retval)
796 return retval;
797
798 disable_irq(client->irq);
799
800 data->baseline_ready = false;
801
802 data->mode |= ETP_ENABLE_CALIBRATE;
803 retval = data->ops->set_mode(data->client, data->mode);
804 if (retval) {
805 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
806 retval);
807 goto out;
808 }
809
810 msleep(250);
811
812 retval = data->ops->get_baseline_data(data->client, true,
813 &data->max_baseline);
814 if (retval) {
815 dev_err(dev, "Failed to read max baseline form device: %d\n",
816 retval);
817 goto out_disable_calibrate;
818 }
819
820 retval = data->ops->get_baseline_data(data->client, false,
821 &data->min_baseline);
822 if (retval) {
823 dev_err(dev, "Failed to read min baseline form device: %d\n",
824 retval);
825 goto out_disable_calibrate;
826 }
827
828 data->baseline_ready = true;
829
830out_disable_calibrate:
831 data->mode &= ~ETP_ENABLE_CALIBRATE;
832 error = data->ops->set_mode(data->client, data->mode);
833 if (error) {
834 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
835 error);
836 if (!retval)
837 retval = error;
838 }
839out:
840 enable_irq(client->irq);
841 mutex_unlock(&data->sysfs_mutex);
842 return retval ?: count;
843}
844
845static ssize_t min_show(struct device *dev,
846 struct device_attribute *attr, char *buf)
847{
848 struct i2c_client *client = to_i2c_client(dev);
849 struct elan_tp_data *data = i2c_get_clientdata(client);
850 int retval;
851
852 retval = mutex_lock_interruptible(&data->sysfs_mutex);
853 if (retval)
854 return retval;
855
856 if (!data->baseline_ready) {
857 retval = -ENODATA;
858 goto out;
859 }
860
861 retval = sysfs_emit(buf, "%d", data->min_baseline);
862
863out:
864 mutex_unlock(&data->sysfs_mutex);
865 return retval;
866}
867
868static ssize_t max_show(struct device *dev,
869 struct device_attribute *attr, char *buf)
870{
871 struct i2c_client *client = to_i2c_client(dev);
872 struct elan_tp_data *data = i2c_get_clientdata(client);
873 int retval;
874
875 retval = mutex_lock_interruptible(&data->sysfs_mutex);
876 if (retval)
877 return retval;
878
879 if (!data->baseline_ready) {
880 retval = -ENODATA;
881 goto out;
882 }
883
884 retval = sysfs_emit(buf, "%d", data->max_baseline);
885
886out:
887 mutex_unlock(&data->sysfs_mutex);
888 return retval;
889}
890
891
892static DEVICE_ATTR_WO(acquire);
893static DEVICE_ATTR_RO(min);
894static DEVICE_ATTR_RO(max);
895
896static struct attribute *elan_baseline_sysfs_entries[] = {
897 &dev_attr_acquire.attr,
898 &dev_attr_min.attr,
899 &dev_attr_max.attr,
900 NULL,
901};
902
903static const struct attribute_group elan_baseline_sysfs_group = {
904 .name = "baseline",
905 .attrs = elan_baseline_sysfs_entries,
906};
907
908static const struct attribute_group *elan_sysfs_groups[] = {
909 &elan_sysfs_group,
910 &elan_baseline_sysfs_group,
911 NULL
912};
913
914/*
915 ******************************************************************
916 * Elan isr functions
917 ******************************************************************
918 */
919static void elan_report_contact(struct elan_tp_data *data, int contact_num,
920 bool contact_valid, bool high_precision,
921 u8 *packet, u8 *finger_data)
922{
923 struct input_dev *input = data->input;
924 unsigned int pos_x, pos_y;
925 unsigned int pressure, scaled_pressure;
926
927 if (contact_valid) {
928 if (high_precision) {
929 pos_x = get_unaligned_be16(&finger_data[0]);
930 pos_y = get_unaligned_be16(&finger_data[2]);
931 } else {
932 pos_x = ((finger_data[0] & 0xf0) << 4) | finger_data[1];
933 pos_y = ((finger_data[0] & 0x0f) << 8) | finger_data[2];
934 }
935
936 if (pos_x > data->max_x || pos_y > data->max_y) {
937 dev_dbg(input->dev.parent,
938 "[%d] x=%d y=%d over max (%d, %d)",
939 contact_num, pos_x, pos_y,
940 data->max_x, data->max_y);
941 return;
942 }
943
944 pressure = finger_data[4];
945 scaled_pressure = pressure + data->pressure_adjustment;
946 if (scaled_pressure > ETP_MAX_PRESSURE)
947 scaled_pressure = ETP_MAX_PRESSURE;
948
949 input_mt_slot(input, contact_num);
950 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
951 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
952 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
953 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
954
955 if (data->report_features & ETP_FEATURE_REPORT_MK) {
956 unsigned int mk_x, mk_y, area_x, area_y;
957 u8 mk_data = high_precision ?
958 packet[ETP_MK_DATA_OFFSET + contact_num] :
959 finger_data[3];
960
961 mk_x = mk_data & 0x0f;
962 mk_y = mk_data >> 4;
963
964 /*
965 * To avoid treating large finger as palm, let's reduce
966 * the width x and y per trace.
967 */
968 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
969 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
970
971 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
972 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
973 max(area_x, area_y));
974 input_report_abs(input, ABS_MT_TOUCH_MINOR,
975 min(area_x, area_y));
976 }
977 } else {
978 input_mt_slot(input, contact_num);
979 input_mt_report_slot_inactive(input);
980 }
981}
982
983static void elan_report_absolute(struct elan_tp_data *data, u8 *packet,
984 bool high_precision)
985{
986 struct input_dev *input = data->input;
987 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
988 int i;
989 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
990 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
991 bool contact_valid, hover_event;
992
993 pm_wakeup_event(&data->client->dev, 0);
994
995 hover_event = hover_info & BIT(6);
996
997 for (i = 0; i < ETP_MAX_FINGERS; i++) {
998 contact_valid = tp_info & BIT(3 + i);
999 elan_report_contact(data, i, contact_valid, high_precision,
1000 packet, finger_data);
1001 if (contact_valid)
1002 finger_data += ETP_FINGER_DATA_LEN;
1003 }
1004
1005 input_report_key(input, BTN_LEFT, tp_info & BIT(0));
1006 input_report_key(input, BTN_MIDDLE, tp_info & BIT(2));
1007 input_report_key(input, BTN_RIGHT, tp_info & BIT(1));
1008 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
1009 input_mt_report_pointer_emulation(input, true);
1010 input_sync(input);
1011}
1012
1013static void elan_report_trackpoint(struct elan_tp_data *data, u8 *report)
1014{
1015 struct input_dev *input = data->tp_input;
1016 u8 *packet = &report[ETP_REPORT_ID_OFFSET + 1];
1017 int x, y;
1018
1019 pm_wakeup_event(&data->client->dev, 0);
1020
1021 if (!data->tp_input) {
1022 dev_warn_once(&data->client->dev,
1023 "received a trackpoint report while no trackpoint device has been created. Please report upstream.\n");
1024 return;
1025 }
1026
1027 input_report_key(input, BTN_LEFT, packet[0] & 0x01);
1028 input_report_key(input, BTN_RIGHT, packet[0] & 0x02);
1029 input_report_key(input, BTN_MIDDLE, packet[0] & 0x04);
1030
1031 if ((packet[3] & 0x0F) == 0x06) {
1032 x = packet[4] - (int)((packet[1] ^ 0x80) << 1);
1033 y = (int)((packet[2] ^ 0x80) << 1) - packet[5];
1034
1035 input_report_rel(input, REL_X, x);
1036 input_report_rel(input, REL_Y, y);
1037 }
1038
1039 input_sync(input);
1040}
1041
1042static irqreturn_t elan_isr(int irq, void *dev_id)
1043{
1044 struct elan_tp_data *data = dev_id;
1045 int error;
1046 u8 report[ETP_MAX_REPORT_LEN];
1047
1048 /*
1049 * When device is connected to i2c bus, when all IAP page writes
1050 * complete, the driver will receive interrupt and must read
1051 * 0000 to confirm that IAP is finished.
1052 */
1053 if (data->in_fw_update) {
1054 complete(&data->fw_completion);
1055 goto out;
1056 }
1057
1058 error = data->ops->get_report(data->client, report, data->report_len);
1059 if (error)
1060 goto out;
1061
1062 switch (report[ETP_REPORT_ID_OFFSET]) {
1063 case ETP_REPORT_ID:
1064 elan_report_absolute(data, report, false);
1065 break;
1066 case ETP_REPORT_ID2:
1067 elan_report_absolute(data, report, true);
1068 break;
1069 case ETP_TP_REPORT_ID:
1070 case ETP_TP_REPORT_ID2:
1071 elan_report_trackpoint(data, report);
1072 break;
1073 default:
1074 dev_err(&data->client->dev, "invalid report id data (%x)\n",
1075 report[ETP_REPORT_ID_OFFSET]);
1076 }
1077
1078out:
1079 return IRQ_HANDLED;
1080}
1081
1082/*
1083 ******************************************************************
1084 * Elan initialization functions
1085 ******************************************************************
1086 */
1087
1088static int elan_setup_trackpoint_input_device(struct elan_tp_data *data)
1089{
1090 struct device *dev = &data->client->dev;
1091 struct input_dev *input;
1092
1093 input = devm_input_allocate_device(dev);
1094 if (!input)
1095 return -ENOMEM;
1096
1097 input->name = "Elan TrackPoint";
1098 input->id.bustype = BUS_I2C;
1099 input->id.vendor = ELAN_VENDOR_ID;
1100 input->id.product = data->product_id;
1101 input_set_drvdata(input, data);
1102
1103 input_set_capability(input, EV_REL, REL_X);
1104 input_set_capability(input, EV_REL, REL_Y);
1105 input_set_capability(input, EV_KEY, BTN_LEFT);
1106 input_set_capability(input, EV_KEY, BTN_RIGHT);
1107 input_set_capability(input, EV_KEY, BTN_MIDDLE);
1108
1109 __set_bit(INPUT_PROP_POINTER, input->propbit);
1110 __set_bit(INPUT_PROP_POINTING_STICK, input->propbit);
1111
1112 data->tp_input = input;
1113
1114 return 0;
1115}
1116
1117static int elan_setup_input_device(struct elan_tp_data *data)
1118{
1119 struct device *dev = &data->client->dev;
1120 struct input_dev *input;
1121 unsigned int max_width = max(data->width_x, data->width_y);
1122 unsigned int min_width = min(data->width_x, data->width_y);
1123 int error;
1124
1125 input = devm_input_allocate_device(dev);
1126 if (!input)
1127 return -ENOMEM;
1128
1129 input->name = "Elan Touchpad";
1130 input->id.bustype = BUS_I2C;
1131 input->id.vendor = ELAN_VENDOR_ID;
1132 input->id.product = data->product_id;
1133 input_set_drvdata(input, data);
1134
1135 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
1136 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
1137 if (error) {
1138 dev_err(dev, "failed to initialize MT slots: %d\n", error);
1139 return error;
1140 }
1141
1142 __set_bit(EV_ABS, input->evbit);
1143 __set_bit(INPUT_PROP_POINTER, input->propbit);
1144 if (data->clickpad) {
1145 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
1146 } else {
1147 __set_bit(BTN_RIGHT, input->keybit);
1148 if (data->middle_button)
1149 __set_bit(BTN_MIDDLE, input->keybit);
1150 }
1151 __set_bit(BTN_LEFT, input->keybit);
1152
1153 /* Set up ST parameters */
1154 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
1155 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
1156 input_abs_set_res(input, ABS_X, data->x_res);
1157 input_abs_set_res(input, ABS_Y, data->y_res);
1158 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
1159 if (data->report_features & ETP_FEATURE_REPORT_MK)
1160 input_set_abs_params(input, ABS_TOOL_WIDTH,
1161 0, ETP_FINGER_WIDTH, 0, 0);
1162 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
1163
1164 /* And MT parameters */
1165 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
1166 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
1167 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
1168 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
1169 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
1170 ETP_MAX_PRESSURE, 0, 0);
1171 if (data->report_features & ETP_FEATURE_REPORT_MK) {
1172 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR,
1173 0, ETP_FINGER_WIDTH * max_width, 0, 0);
1174 input_set_abs_params(input, ABS_MT_TOUCH_MINOR,
1175 0, ETP_FINGER_WIDTH * min_width, 0, 0);
1176 }
1177
1178 data->input = input;
1179
1180 return 0;
1181}
1182
1183static void elan_disable_regulator(void *_data)
1184{
1185 struct elan_tp_data *data = _data;
1186
1187 regulator_disable(data->vcc);
1188}
1189
1190static int elan_probe(struct i2c_client *client)
1191{
1192 const struct elan_transport_ops *transport_ops;
1193 struct device *dev = &client->dev;
1194 struct elan_tp_data *data;
1195 unsigned long irqflags;
1196 int error;
1197
1198 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1199 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1200 transport_ops = &elan_i2c_ops;
1201 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1202 i2c_check_functionality(client->adapter,
1203 I2C_FUNC_SMBUS_BYTE_DATA |
1204 I2C_FUNC_SMBUS_BLOCK_DATA |
1205 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1206 transport_ops = &elan_smbus_ops;
1207 } else {
1208 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1209 return -EIO;
1210 }
1211
1212 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1213 if (!data)
1214 return -ENOMEM;
1215
1216 i2c_set_clientdata(client, data);
1217
1218 data->ops = transport_ops;
1219 data->client = client;
1220 init_completion(&data->fw_completion);
1221 mutex_init(&data->sysfs_mutex);
1222
1223 data->vcc = devm_regulator_get(dev, "vcc");
1224 if (IS_ERR(data->vcc))
1225 return dev_err_probe(dev, PTR_ERR(data->vcc), "Failed to get 'vcc' regulator\n");
1226
1227 error = regulator_enable(data->vcc);
1228 if (error) {
1229 dev_err(dev, "Failed to enable regulator: %d\n", error);
1230 return error;
1231 }
1232
1233 error = devm_add_action_or_reset(dev, elan_disable_regulator, data);
1234 if (error) {
1235 dev_err(dev, "Failed to add disable regulator action: %d\n",
1236 error);
1237 return error;
1238 }
1239
1240 /* Make sure there is something at this address */
1241 error = i2c_smbus_read_byte(client);
1242 if (error < 0) {
1243 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1244 return -ENXIO;
1245 }
1246
1247 /* Initialize the touchpad. */
1248 error = elan_initialize(data, false);
1249 if (error)
1250 return error;
1251
1252 error = elan_query_device_info(data);
1253 if (error)
1254 return error;
1255
1256 error = elan_query_device_parameters(data);
1257 if (error)
1258 return error;
1259
1260 dev_info(dev,
1261 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1262 data->product_id,
1263 data->fw_version,
1264 data->sm_version,
1265 data->iap_version);
1266
1267 dev_dbg(dev,
1268 "Elan Touchpad Extra Information:\n"
1269 " Max ABS X,Y: %d,%d\n"
1270 " Width X,Y: %d,%d\n"
1271 " Resolution X,Y: %d,%d (dots/mm)\n"
1272 " ic type: 0x%x\n"
1273 " info pattern: 0x%x\n",
1274 data->max_x, data->max_y,
1275 data->width_x, data->width_y,
1276 data->x_res, data->y_res,
1277 data->ic_type, data->pattern);
1278
1279 /* Set up input device properties based on queried parameters. */
1280 error = elan_setup_input_device(data);
1281 if (error)
1282 return error;
1283
1284 if (device_property_read_bool(&client->dev, "elan,trackpoint")) {
1285 error = elan_setup_trackpoint_input_device(data);
1286 if (error)
1287 return error;
1288 }
1289
1290 /*
1291 * Platform code (ACPI, DTS) should normally set up interrupt
1292 * for us, but in case it did not let's fall back to using falling
1293 * edge to be compatible with older Chromebooks.
1294 */
1295 irqflags = irq_get_trigger_type(client->irq);
1296 if (!irqflags)
1297 irqflags = IRQF_TRIGGER_FALLING;
1298
1299 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1300 irqflags | IRQF_ONESHOT,
1301 client->name, data);
1302 if (error) {
1303 dev_err(dev, "cannot register irq=%d\n", client->irq);
1304 return error;
1305 }
1306
1307 error = input_register_device(data->input);
1308 if (error) {
1309 dev_err(dev, "failed to register input device: %d\n", error);
1310 return error;
1311 }
1312
1313 if (data->tp_input) {
1314 error = input_register_device(data->tp_input);
1315 if (error) {
1316 dev_err(&client->dev,
1317 "failed to register TrackPoint input device: %d\n",
1318 error);
1319 return error;
1320 }
1321 }
1322
1323 return 0;
1324}
1325
1326static int elan_suspend(struct device *dev)
1327{
1328 struct i2c_client *client = to_i2c_client(dev);
1329 struct elan_tp_data *data = i2c_get_clientdata(client);
1330 int ret;
1331
1332 /*
1333 * We are taking the mutex to make sure sysfs operations are
1334 * complete before we attempt to bring the device into low[er]
1335 * power mode.
1336 */
1337 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1338 if (ret)
1339 return ret;
1340
1341 disable_irq(client->irq);
1342
1343 if (device_may_wakeup(dev)) {
1344 ret = elan_sleep(data);
1345 } else {
1346 ret = elan_set_power(data, false);
1347 if (ret)
1348 goto err;
1349
1350 ret = regulator_disable(data->vcc);
1351 if (ret) {
1352 dev_err(dev, "error %d disabling regulator\n", ret);
1353 /* Attempt to power the chip back up */
1354 elan_set_power(data, true);
1355 }
1356 }
1357
1358err:
1359 mutex_unlock(&data->sysfs_mutex);
1360 return ret;
1361}
1362
1363static int elan_resume(struct device *dev)
1364{
1365 struct i2c_client *client = to_i2c_client(dev);
1366 struct elan_tp_data *data = i2c_get_clientdata(client);
1367 int error;
1368
1369 if (!device_may_wakeup(dev)) {
1370 error = regulator_enable(data->vcc);
1371 if (error) {
1372 dev_err(dev, "error %d enabling regulator\n", error);
1373 goto err;
1374 }
1375 }
1376
1377 error = elan_set_power(data, true);
1378 if (error) {
1379 dev_err(dev, "power up when resuming failed: %d\n", error);
1380 goto err;
1381 }
1382
1383 error = elan_initialize(data, data->quirks & ETP_QUIRK_QUICK_WAKEUP);
1384 if (error)
1385 dev_err(dev, "initialize when resuming failed: %d\n", error);
1386
1387err:
1388 enable_irq(data->client->irq);
1389 return error;
1390}
1391
1392static DEFINE_SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1393
1394static const struct i2c_device_id elan_id[] = {
1395 { DRIVER_NAME, 0 },
1396 { },
1397};
1398MODULE_DEVICE_TABLE(i2c, elan_id);
1399
1400#ifdef CONFIG_ACPI
1401#include <linux/input/elan-i2c-ids.h>
1402MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1403#endif
1404
1405#ifdef CONFIG_OF
1406static const struct of_device_id elan_of_match[] = {
1407 { .compatible = "elan,ekth3000" },
1408 { /* sentinel */ }
1409};
1410MODULE_DEVICE_TABLE(of, elan_of_match);
1411#endif
1412
1413static struct i2c_driver elan_driver = {
1414 .driver = {
1415 .name = DRIVER_NAME,
1416 .pm = pm_sleep_ptr(&elan_pm_ops),
1417 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1418 .of_match_table = of_match_ptr(elan_of_match),
1419 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1420 .dev_groups = elan_sysfs_groups,
1421 },
1422 .probe = elan_probe,
1423 .id_table = elan_id,
1424};
1425
1426module_i2c_driver(elan_driver);
1427
1428MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1429MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1430MODULE_LICENSE("GPL");
1/*
2 * Elan I2C/SMBus Touchpad driver
3 *
4 * Copyright (c) 2013 ELAN Microelectronics Corp.
5 *
6 * Author: 林政維 (Duson Lin) <dusonlin@emc.com.tw>
7 * Author: KT Liao <kt.liao@emc.com.tw>
8 * Version: 1.6.3
9 *
10 * Based on cyapa driver:
11 * copyright (c) 2011-2012 Cypress Semiconductor, Inc.
12 * copyright (c) 2011-2012 Google, Inc.
13 *
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.
17 *
18 * Trademarks are the property of their respective owners.
19 */
20
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>
38#include <linux/of.h>
39#include <linux/regulator/consumer.h>
40#include <asm/unaligned.h>
41
42#include "elan_i2c.h"
43
44#define DRIVER_NAME "elan_i2c"
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
50
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
59
60/* The main device structure */
61struct elan_tp_data {
62 struct i2c_client *client;
63 struct input_dev *input;
64 struct regulator *vcc;
65
66 const struct elan_transport_ops *ops;
67
68 /* for fw update */
69 struct completion fw_completion;
70 bool in_fw_update;
71
72 struct mutex sysfs_mutex;
73
74 unsigned int max_x;
75 unsigned int max_y;
76 unsigned int width_x;
77 unsigned int width_y;
78 unsigned int x_res;
79 unsigned int y_res;
80
81 u8 pattern;
82 u16 product_id;
83 u8 fw_version;
84 u8 sm_version;
85 u8 iap_version;
86 u16 fw_checksum;
87 int pressure_adjustment;
88 u8 mode;
89 u16 ic_type;
90 u16 fw_validpage_count;
91 u16 fw_signature_address;
92
93 bool irq_wake;
94
95 u8 min_baseline;
96 u8 max_baseline;
97 bool baseline_ready;
98 u8 clickpad;
99};
100
101static int elan_get_fwinfo(u16 ic_type, u16 *validpage_count,
102 u16 *signature_address)
103{
104 switch (ic_type) {
105 case 0x00:
106 case 0x06:
107 case 0x08:
108 *validpage_count = 512;
109 break;
110 case 0x03:
111 case 0x07:
112 case 0x09:
113 case 0x0A:
114 case 0x0B:
115 case 0x0C:
116 *validpage_count = 768;
117 break;
118 case 0x0D:
119 *validpage_count = 896;
120 break;
121 case 0x0E:
122 *validpage_count = 640;
123 break;
124 case 0x10:
125 *validpage_count = 1024;
126 break;
127 default:
128 /* unknown ic type clear value */
129 *validpage_count = 0;
130 *signature_address = 0;
131 return -ENXIO;
132 }
133
134 *signature_address =
135 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
136
137 return 0;
138}
139
140static int elan_enable_power(struct elan_tp_data *data)
141{
142 int repeat = ETP_RETRY_COUNT;
143 int error;
144
145 error = regulator_enable(data->vcc);
146 if (error) {
147 dev_err(&data->client->dev,
148 "failed to enable regulator: %d\n", error);
149 return error;
150 }
151
152 do {
153 error = data->ops->power_control(data->client, true);
154 if (error >= 0)
155 return 0;
156
157 msleep(30);
158 } while (--repeat > 0);
159
160 dev_err(&data->client->dev, "failed to enable power: %d\n", error);
161 return error;
162}
163
164static int elan_disable_power(struct elan_tp_data *data)
165{
166 int repeat = ETP_RETRY_COUNT;
167 int error;
168
169 do {
170 error = data->ops->power_control(data->client, false);
171 if (!error) {
172 error = regulator_disable(data->vcc);
173 if (error) {
174 dev_err(&data->client->dev,
175 "failed to disable regulator: %d\n",
176 error);
177 /* Attempt to power the chip back up */
178 data->ops->power_control(data->client, true);
179 break;
180 }
181
182 return 0;
183 }
184
185 msleep(30);
186 } while (--repeat > 0);
187
188 dev_err(&data->client->dev, "failed to disable power: %d\n", error);
189 return error;
190}
191
192static int elan_sleep(struct elan_tp_data *data)
193{
194 int repeat = ETP_RETRY_COUNT;
195 int error;
196
197 do {
198 error = data->ops->sleep_control(data->client, true);
199 if (!error)
200 return 0;
201
202 msleep(30);
203 } while (--repeat > 0);
204
205 return error;
206}
207
208static int elan_query_product(struct elan_tp_data *data)
209{
210 int error;
211
212 error = data->ops->get_product_id(data->client, &data->product_id);
213 if (error)
214 return error;
215
216 error = data->ops->get_sm_version(data->client, &data->ic_type,
217 &data->sm_version, &data->clickpad);
218 if (error)
219 return error;
220
221 return 0;
222}
223
224static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
225{
226 if (data->ic_type == 0x0E) {
227 switch (data->product_id) {
228 case 0x05 ... 0x07:
229 case 0x09:
230 case 0x13:
231 return true;
232 }
233 } else if (data->ic_type == 0x08 && data->product_id == 0x26) {
234 /* ASUS EeeBook X205TA */
235 return true;
236 }
237
238 return false;
239}
240
241static int __elan_initialize(struct elan_tp_data *data)
242{
243 struct i2c_client *client = data->client;
244 bool woken_up = false;
245 int error;
246
247 error = data->ops->initialize(client);
248 if (error) {
249 dev_err(&client->dev, "device initialize failed: %d\n", error);
250 return error;
251 }
252
253 error = elan_query_product(data);
254 if (error)
255 return error;
256
257 /*
258 * Some ASUS devices were shipped with firmware that requires
259 * touchpads to be woken up first, before attempting to switch
260 * them into absolute reporting mode.
261 */
262 if (elan_check_ASUS_special_fw(data)) {
263 error = data->ops->sleep_control(client, false);
264 if (error) {
265 dev_err(&client->dev,
266 "failed to wake device up: %d\n", error);
267 return error;
268 }
269
270 msleep(200);
271 woken_up = true;
272 }
273
274 data->mode |= ETP_ENABLE_ABS;
275 error = data->ops->set_mode(client, data->mode);
276 if (error) {
277 dev_err(&client->dev,
278 "failed to switch to absolute mode: %d\n", error);
279 return error;
280 }
281
282 if (!woken_up) {
283 error = data->ops->sleep_control(client, false);
284 if (error) {
285 dev_err(&client->dev,
286 "failed to wake device up: %d\n", error);
287 return error;
288 }
289 }
290
291 return 0;
292}
293
294static int elan_initialize(struct elan_tp_data *data)
295{
296 int repeat = ETP_RETRY_COUNT;
297 int error;
298
299 do {
300 error = __elan_initialize(data);
301 if (!error)
302 return 0;
303
304 msleep(30);
305 } while (--repeat > 0);
306
307 return error;
308}
309
310static int elan_query_device_info(struct elan_tp_data *data)
311{
312 int error;
313 u16 ic_type;
314
315 error = data->ops->get_version(data->client, false, &data->fw_version);
316 if (error)
317 return error;
318
319 error = data->ops->get_checksum(data->client, false,
320 &data->fw_checksum);
321 if (error)
322 return error;
323
324 error = data->ops->get_version(data->client, true, &data->iap_version);
325 if (error)
326 return error;
327
328 error = data->ops->get_pressure_adjustment(data->client,
329 &data->pressure_adjustment);
330 if (error)
331 return error;
332
333 error = data->ops->get_pattern(data->client, &data->pattern);
334 if (error)
335 return error;
336
337 if (data->pattern == 0x01)
338 ic_type = data->ic_type;
339 else
340 ic_type = data->iap_version;
341
342 error = elan_get_fwinfo(ic_type, &data->fw_validpage_count,
343 &data->fw_signature_address);
344 if (error)
345 dev_warn(&data->client->dev,
346 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
347 data->iap_version, data->ic_type);
348
349 return 0;
350}
351
352static unsigned int elan_convert_resolution(u8 val)
353{
354 /*
355 * (value from firmware) * 10 + 790 = dpi
356 *
357 * We also have to convert dpi to dots/mm (*10/254 to avoid floating
358 * point).
359 */
360
361 return ((int)(char)val * 10 + 790) * 10 / 254;
362}
363
364static int elan_query_device_parameters(struct elan_tp_data *data)
365{
366 unsigned int x_traces, y_traces;
367 u8 hw_x_res, hw_y_res;
368 int error;
369
370 error = data->ops->get_max(data->client, &data->max_x, &data->max_y);
371 if (error)
372 return error;
373
374 error = data->ops->get_num_traces(data->client, &x_traces, &y_traces);
375 if (error)
376 return error;
377
378 data->width_x = data->max_x / x_traces;
379 data->width_y = data->max_y / y_traces;
380
381 error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res);
382 if (error)
383 return error;
384
385 data->x_res = elan_convert_resolution(hw_x_res);
386 data->y_res = elan_convert_resolution(hw_y_res);
387
388 return 0;
389}
390
391/*
392 **********************************************************
393 * IAP firmware updater related routines
394 **********************************************************
395 */
396static int elan_write_fw_block(struct elan_tp_data *data,
397 const u8 *page, u16 checksum, int idx)
398{
399 int retry = ETP_RETRY_COUNT;
400 int error;
401
402 do {
403 error = data->ops->write_fw_block(data->client,
404 page, checksum, idx);
405 if (!error)
406 return 0;
407
408 dev_dbg(&data->client->dev,
409 "IAP retrying page %d (error: %d)\n", idx, error);
410 } while (--retry > 0);
411
412 return error;
413}
414
415static int __elan_update_firmware(struct elan_tp_data *data,
416 const struct firmware *fw)
417{
418 struct i2c_client *client = data->client;
419 struct device *dev = &client->dev;
420 int i, j;
421 int error;
422 u16 iap_start_addr;
423 u16 boot_page_count;
424 u16 sw_checksum = 0, fw_checksum = 0;
425
426 error = data->ops->prepare_fw_update(client);
427 if (error)
428 return error;
429
430 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
431
432 boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE;
433 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
434 u16 checksum = 0;
435 const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE];
436
437 for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2)
438 checksum += ((page[j + 1] << 8) | page[j]);
439
440 error = elan_write_fw_block(data, page, checksum, i);
441 if (error) {
442 dev_err(dev, "write page %d fail: %d\n", i, error);
443 return error;
444 }
445
446 sw_checksum += checksum;
447 }
448
449 /* Wait WDT reset and power on reset */
450 msleep(600);
451
452 error = data->ops->finish_fw_update(client, &data->fw_completion);
453 if (error)
454 return error;
455
456 error = data->ops->get_checksum(client, true, &fw_checksum);
457 if (error)
458 return error;
459
460 if (sw_checksum != fw_checksum) {
461 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
462 sw_checksum, fw_checksum);
463 return -EIO;
464 }
465
466 return 0;
467}
468
469static int elan_update_firmware(struct elan_tp_data *data,
470 const struct firmware *fw)
471{
472 struct i2c_client *client = data->client;
473 int retval;
474
475 dev_dbg(&client->dev, "Starting firmware update....\n");
476
477 disable_irq(client->irq);
478 data->in_fw_update = true;
479
480 retval = __elan_update_firmware(data, fw);
481 if (retval) {
482 dev_err(&client->dev, "firmware update failed: %d\n", retval);
483 data->ops->iap_reset(client);
484 } else {
485 /* Reinitialize TP after fw is updated */
486 elan_initialize(data);
487 elan_query_device_info(data);
488 }
489
490 data->in_fw_update = false;
491 enable_irq(client->irq);
492
493 return retval;
494}
495
496/*
497 *******************************************************************
498 * SYSFS attributes
499 *******************************************************************
500 */
501static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
502 struct device_attribute *attr,
503 char *buf)
504{
505 struct i2c_client *client = to_i2c_client(dev);
506 struct elan_tp_data *data = i2c_get_clientdata(client);
507
508 return sprintf(buf, "0x%04x\n", data->fw_checksum);
509}
510
511static ssize_t elan_sysfs_read_product_id(struct device *dev,
512 struct device_attribute *attr,
513 char *buf)
514{
515 struct i2c_client *client = to_i2c_client(dev);
516 struct elan_tp_data *data = i2c_get_clientdata(client);
517
518 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
519 data->product_id);
520}
521
522static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
523 struct device_attribute *attr,
524 char *buf)
525{
526 struct i2c_client *client = to_i2c_client(dev);
527 struct elan_tp_data *data = i2c_get_clientdata(client);
528
529 return sprintf(buf, "%d.0\n", data->fw_version);
530}
531
532static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
533 struct device_attribute *attr,
534 char *buf)
535{
536 struct i2c_client *client = to_i2c_client(dev);
537 struct elan_tp_data *data = i2c_get_clientdata(client);
538
539 return sprintf(buf, "%d.0\n", data->sm_version);
540}
541
542static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
543 struct device_attribute *attr,
544 char *buf)
545{
546 struct i2c_client *client = to_i2c_client(dev);
547 struct elan_tp_data *data = i2c_get_clientdata(client);
548
549 return sprintf(buf, "%d.0\n", data->iap_version);
550}
551
552static ssize_t elan_sysfs_update_fw(struct device *dev,
553 struct device_attribute *attr,
554 const char *buf, size_t count)
555{
556 struct elan_tp_data *data = dev_get_drvdata(dev);
557 const struct firmware *fw;
558 char *fw_name;
559 int error;
560 const u8 *fw_signature;
561 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
562
563 if (data->fw_validpage_count == 0)
564 return -EINVAL;
565
566 /* Look for a firmware with the product id appended. */
567 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
568 if (!fw_name) {
569 dev_err(dev, "failed to allocate memory for firmware name\n");
570 return -ENOMEM;
571 }
572
573 dev_info(dev, "requesting fw '%s'\n", fw_name);
574 error = request_firmware(&fw, fw_name, dev);
575 kfree(fw_name);
576 if (error) {
577 dev_err(dev, "failed to request firmware: %d\n", error);
578 return error;
579 }
580
581 /* Firmware file must match signature data */
582 fw_signature = &fw->data[data->fw_signature_address];
583 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
584 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
585 (int)sizeof(signature), signature,
586 (int)sizeof(signature), fw_signature);
587 error = -EBADF;
588 goto out_release_fw;
589 }
590
591 error = mutex_lock_interruptible(&data->sysfs_mutex);
592 if (error)
593 goto out_release_fw;
594
595 error = elan_update_firmware(data, fw);
596
597 mutex_unlock(&data->sysfs_mutex);
598
599out_release_fw:
600 release_firmware(fw);
601 return error ?: count;
602}
603
604static ssize_t calibrate_store(struct device *dev,
605 struct device_attribute *attr,
606 const char *buf, size_t count)
607{
608 struct i2c_client *client = to_i2c_client(dev);
609 struct elan_tp_data *data = i2c_get_clientdata(client);
610 int tries = 20;
611 int retval;
612 int error;
613 u8 val[3];
614
615 retval = mutex_lock_interruptible(&data->sysfs_mutex);
616 if (retval)
617 return retval;
618
619 disable_irq(client->irq);
620
621 data->mode |= ETP_ENABLE_CALIBRATE;
622 retval = data->ops->set_mode(client, data->mode);
623 if (retval) {
624 dev_err(dev, "failed to enable calibration mode: %d\n",
625 retval);
626 goto out;
627 }
628
629 retval = data->ops->calibrate(client);
630 if (retval) {
631 dev_err(dev, "failed to start calibration: %d\n",
632 retval);
633 goto out_disable_calibrate;
634 }
635
636 val[0] = 0xff;
637 do {
638 /* Wait 250ms before checking if calibration has completed. */
639 msleep(250);
640
641 retval = data->ops->calibrate_result(client, val);
642 if (retval)
643 dev_err(dev, "failed to check calibration result: %d\n",
644 retval);
645 else if (val[0] == 0)
646 break; /* calibration done */
647
648 } while (--tries);
649
650 if (tries == 0) {
651 dev_err(dev, "failed to calibrate. Timeout.\n");
652 retval = -ETIMEDOUT;
653 }
654
655out_disable_calibrate:
656 data->mode &= ~ETP_ENABLE_CALIBRATE;
657 error = data->ops->set_mode(data->client, data->mode);
658 if (error) {
659 dev_err(dev, "failed to disable calibration mode: %d\n",
660 error);
661 if (!retval)
662 retval = error;
663 }
664out:
665 enable_irq(client->irq);
666 mutex_unlock(&data->sysfs_mutex);
667 return retval ?: count;
668}
669
670static ssize_t elan_sysfs_read_mode(struct device *dev,
671 struct device_attribute *attr,
672 char *buf)
673{
674 struct i2c_client *client = to_i2c_client(dev);
675 struct elan_tp_data *data = i2c_get_clientdata(client);
676 int error;
677 enum tp_mode mode;
678
679 error = mutex_lock_interruptible(&data->sysfs_mutex);
680 if (error)
681 return error;
682
683 error = data->ops->iap_get_mode(data->client, &mode);
684
685 mutex_unlock(&data->sysfs_mutex);
686
687 if (error)
688 return error;
689
690 return sprintf(buf, "%d\n", (int)mode);
691}
692
693static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
694static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
695static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
696static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
697static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
698static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
699static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
700
701static DEVICE_ATTR_WO(calibrate);
702
703static struct attribute *elan_sysfs_entries[] = {
704 &dev_attr_product_id.attr,
705 &dev_attr_firmware_version.attr,
706 &dev_attr_sample_version.attr,
707 &dev_attr_iap_version.attr,
708 &dev_attr_fw_checksum.attr,
709 &dev_attr_calibrate.attr,
710 &dev_attr_mode.attr,
711 &dev_attr_update_fw.attr,
712 NULL,
713};
714
715static const struct attribute_group elan_sysfs_group = {
716 .attrs = elan_sysfs_entries,
717};
718
719static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
720 const char *buf, size_t count)
721{
722 struct i2c_client *client = to_i2c_client(dev);
723 struct elan_tp_data *data = i2c_get_clientdata(client);
724 int error;
725 int retval;
726
727 retval = mutex_lock_interruptible(&data->sysfs_mutex);
728 if (retval)
729 return retval;
730
731 disable_irq(client->irq);
732
733 data->baseline_ready = false;
734
735 data->mode |= ETP_ENABLE_CALIBRATE;
736 retval = data->ops->set_mode(data->client, data->mode);
737 if (retval) {
738 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
739 retval);
740 goto out;
741 }
742
743 msleep(250);
744
745 retval = data->ops->get_baseline_data(data->client, true,
746 &data->max_baseline);
747 if (retval) {
748 dev_err(dev, "Failed to read max baseline form device: %d\n",
749 retval);
750 goto out_disable_calibrate;
751 }
752
753 retval = data->ops->get_baseline_data(data->client, false,
754 &data->min_baseline);
755 if (retval) {
756 dev_err(dev, "Failed to read min baseline form device: %d\n",
757 retval);
758 goto out_disable_calibrate;
759 }
760
761 data->baseline_ready = true;
762
763out_disable_calibrate:
764 data->mode &= ~ETP_ENABLE_CALIBRATE;
765 error = data->ops->set_mode(data->client, data->mode);
766 if (error) {
767 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
768 error);
769 if (!retval)
770 retval = error;
771 }
772out:
773 enable_irq(client->irq);
774 mutex_unlock(&data->sysfs_mutex);
775 return retval ?: count;
776}
777
778static ssize_t min_show(struct device *dev,
779 struct device_attribute *attr, char *buf)
780{
781 struct i2c_client *client = to_i2c_client(dev);
782 struct elan_tp_data *data = i2c_get_clientdata(client);
783 int retval;
784
785 retval = mutex_lock_interruptible(&data->sysfs_mutex);
786 if (retval)
787 return retval;
788
789 if (!data->baseline_ready) {
790 retval = -ENODATA;
791 goto out;
792 }
793
794 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
795
796out:
797 mutex_unlock(&data->sysfs_mutex);
798 return retval;
799}
800
801static ssize_t max_show(struct device *dev,
802 struct device_attribute *attr, char *buf)
803{
804 struct i2c_client *client = to_i2c_client(dev);
805 struct elan_tp_data *data = i2c_get_clientdata(client);
806 int retval;
807
808 retval = mutex_lock_interruptible(&data->sysfs_mutex);
809 if (retval)
810 return retval;
811
812 if (!data->baseline_ready) {
813 retval = -ENODATA;
814 goto out;
815 }
816
817 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
818
819out:
820 mutex_unlock(&data->sysfs_mutex);
821 return retval;
822}
823
824
825static DEVICE_ATTR_WO(acquire);
826static DEVICE_ATTR_RO(min);
827static DEVICE_ATTR_RO(max);
828
829static struct attribute *elan_baseline_sysfs_entries[] = {
830 &dev_attr_acquire.attr,
831 &dev_attr_min.attr,
832 &dev_attr_max.attr,
833 NULL,
834};
835
836static const struct attribute_group elan_baseline_sysfs_group = {
837 .name = "baseline",
838 .attrs = elan_baseline_sysfs_entries,
839};
840
841static const struct attribute_group *elan_sysfs_groups[] = {
842 &elan_sysfs_group,
843 &elan_baseline_sysfs_group,
844 NULL
845};
846
847/*
848 ******************************************************************
849 * Elan isr functions
850 ******************************************************************
851 */
852static void elan_report_contact(struct elan_tp_data *data,
853 int contact_num, bool contact_valid,
854 u8 *finger_data)
855{
856 struct input_dev *input = data->input;
857 unsigned int pos_x, pos_y;
858 unsigned int pressure, mk_x, mk_y;
859 unsigned int area_x, area_y, major, minor;
860 unsigned int scaled_pressure;
861
862 if (contact_valid) {
863 pos_x = ((finger_data[0] & 0xf0) << 4) |
864 finger_data[1];
865 pos_y = ((finger_data[0] & 0x0f) << 8) |
866 finger_data[2];
867 mk_x = (finger_data[3] & 0x0f);
868 mk_y = (finger_data[3] >> 4);
869 pressure = finger_data[4];
870
871 if (pos_x > data->max_x || pos_y > data->max_y) {
872 dev_dbg(input->dev.parent,
873 "[%d] x=%d y=%d over max (%d, %d)",
874 contact_num, pos_x, pos_y,
875 data->max_x, data->max_y);
876 return;
877 }
878
879 /*
880 * To avoid treating large finger as palm, let's reduce the
881 * width x and y per trace.
882 */
883 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
884 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
885
886 major = max(area_x, area_y);
887 minor = min(area_x, area_y);
888
889 scaled_pressure = pressure + data->pressure_adjustment;
890
891 if (scaled_pressure > ETP_MAX_PRESSURE)
892 scaled_pressure = ETP_MAX_PRESSURE;
893
894 input_mt_slot(input, contact_num);
895 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
896 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
897 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
898 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
899 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
900 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
901 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
902 } else {
903 input_mt_slot(input, contact_num);
904 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
905 }
906}
907
908static void elan_report_absolute(struct elan_tp_data *data, u8 *packet)
909{
910 struct input_dev *input = data->input;
911 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
912 int i;
913 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
914 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
915 bool contact_valid, hover_event;
916
917 hover_event = hover_info & 0x40;
918 for (i = 0; i < ETP_MAX_FINGERS; i++) {
919 contact_valid = tp_info & (1U << (3 + i));
920 elan_report_contact(data, i, contact_valid, finger_data);
921
922 if (contact_valid)
923 finger_data += ETP_FINGER_DATA_LEN;
924 }
925
926 input_report_key(input, BTN_LEFT, tp_info & 0x01);
927 input_report_key(input, BTN_RIGHT, tp_info & 0x02);
928 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
929 input_mt_report_pointer_emulation(input, true);
930 input_sync(input);
931}
932
933static irqreturn_t elan_isr(int irq, void *dev_id)
934{
935 struct elan_tp_data *data = dev_id;
936 struct device *dev = &data->client->dev;
937 int error;
938 u8 report[ETP_MAX_REPORT_LEN];
939
940 /*
941 * When device is connected to i2c bus, when all IAP page writes
942 * complete, the driver will receive interrupt and must read
943 * 0000 to confirm that IAP is finished.
944 */
945 if (data->in_fw_update) {
946 complete(&data->fw_completion);
947 goto out;
948 }
949
950 error = data->ops->get_report(data->client, report);
951 if (error)
952 goto out;
953
954 if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID)
955 dev_err(dev, "invalid report id data (%x)\n",
956 report[ETP_REPORT_ID_OFFSET]);
957 else
958 elan_report_absolute(data, report);
959
960out:
961 return IRQ_HANDLED;
962}
963
964/*
965 ******************************************************************
966 * Elan initialization functions
967 ******************************************************************
968 */
969static int elan_setup_input_device(struct elan_tp_data *data)
970{
971 struct device *dev = &data->client->dev;
972 struct input_dev *input;
973 unsigned int max_width = max(data->width_x, data->width_y);
974 unsigned int min_width = min(data->width_x, data->width_y);
975 int error;
976
977 input = devm_input_allocate_device(dev);
978 if (!input)
979 return -ENOMEM;
980
981 input->name = "Elan Touchpad";
982 input->id.bustype = BUS_I2C;
983 input->id.vendor = ELAN_VENDOR_ID;
984 input->id.product = data->product_id;
985 input_set_drvdata(input, data);
986
987 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
988 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
989 if (error) {
990 dev_err(dev, "failed to initialize MT slots: %d\n", error);
991 return error;
992 }
993
994 __set_bit(EV_ABS, input->evbit);
995 __set_bit(INPUT_PROP_POINTER, input->propbit);
996 if (data->clickpad)
997 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
998 else
999 __set_bit(BTN_RIGHT, input->keybit);
1000 __set_bit(BTN_LEFT, input->keybit);
1001
1002 /* Set up ST parameters */
1003 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
1004 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
1005 input_abs_set_res(input, ABS_X, data->x_res);
1006 input_abs_set_res(input, ABS_Y, data->y_res);
1007 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
1008 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0);
1009 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
1010
1011 /* And MT parameters */
1012 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
1013 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
1014 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
1015 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
1016 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
1017 ETP_MAX_PRESSURE, 0, 0);
1018 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
1019 ETP_FINGER_WIDTH * max_width, 0, 0);
1020 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0,
1021 ETP_FINGER_WIDTH * min_width, 0, 0);
1022
1023 data->input = input;
1024
1025 return 0;
1026}
1027
1028static void elan_disable_regulator(void *_data)
1029{
1030 struct elan_tp_data *data = _data;
1031
1032 regulator_disable(data->vcc);
1033}
1034
1035static void elan_remove_sysfs_groups(void *_data)
1036{
1037 struct elan_tp_data *data = _data;
1038
1039 sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups);
1040}
1041
1042static int elan_probe(struct i2c_client *client,
1043 const struct i2c_device_id *dev_id)
1044{
1045 const struct elan_transport_ops *transport_ops;
1046 struct device *dev = &client->dev;
1047 struct elan_tp_data *data;
1048 unsigned long irqflags;
1049 int error;
1050
1051 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1052 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1053 transport_ops = &elan_i2c_ops;
1054 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1055 i2c_check_functionality(client->adapter,
1056 I2C_FUNC_SMBUS_BYTE_DATA |
1057 I2C_FUNC_SMBUS_BLOCK_DATA |
1058 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1059 transport_ops = &elan_smbus_ops;
1060 } else {
1061 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1062 return -EIO;
1063 }
1064
1065 data = devm_kzalloc(dev, sizeof(struct elan_tp_data), GFP_KERNEL);
1066 if (!data)
1067 return -ENOMEM;
1068
1069 i2c_set_clientdata(client, data);
1070
1071 data->ops = transport_ops;
1072 data->client = client;
1073 init_completion(&data->fw_completion);
1074 mutex_init(&data->sysfs_mutex);
1075
1076 data->vcc = devm_regulator_get(dev, "vcc");
1077 if (IS_ERR(data->vcc)) {
1078 error = PTR_ERR(data->vcc);
1079 if (error != -EPROBE_DEFER)
1080 dev_err(dev, "Failed to get 'vcc' regulator: %d\n",
1081 error);
1082 return error;
1083 }
1084
1085 error = regulator_enable(data->vcc);
1086 if (error) {
1087 dev_err(dev, "Failed to enable regulator: %d\n", error);
1088 return error;
1089 }
1090
1091 error = devm_add_action(dev, elan_disable_regulator, data);
1092 if (error) {
1093 regulator_disable(data->vcc);
1094 dev_err(dev, "Failed to add disable regulator action: %d\n",
1095 error);
1096 return error;
1097 }
1098
1099 /* Make sure there is something at this address */
1100 error = i2c_smbus_read_byte(client);
1101 if (error < 0) {
1102 dev_dbg(&client->dev, "nothing at this address: %d\n", error);
1103 return -ENXIO;
1104 }
1105
1106 /* Initialize the touchpad. */
1107 error = elan_initialize(data);
1108 if (error)
1109 return error;
1110
1111 error = elan_query_device_info(data);
1112 if (error)
1113 return error;
1114
1115 error = elan_query_device_parameters(data);
1116 if (error)
1117 return error;
1118
1119 dev_info(dev,
1120 "Elan Touchpad: Module ID: 0x%04x, Firmware: 0x%04x, Sample: 0x%04x, IAP: 0x%04x\n",
1121 data->product_id,
1122 data->fw_version,
1123 data->sm_version,
1124 data->iap_version);
1125
1126 dev_dbg(dev,
1127 "Elan Touchpad Extra Information:\n"
1128 " Max ABS X,Y: %d,%d\n"
1129 " Width X,Y: %d,%d\n"
1130 " Resolution X,Y: %d,%d (dots/mm)\n"
1131 " ic type: 0x%x\n"
1132 " info pattern: 0x%x\n",
1133 data->max_x, data->max_y,
1134 data->width_x, data->width_y,
1135 data->x_res, data->y_res,
1136 data->ic_type, data->pattern);
1137
1138 /* Set up input device properties based on queried parameters. */
1139 error = elan_setup_input_device(data);
1140 if (error)
1141 return error;
1142
1143 /*
1144 * Platform code (ACPI, DTS) should normally set up interrupt
1145 * for us, but in case it did not let's fall back to using falling
1146 * edge to be compatible with older Chromebooks.
1147 */
1148 irqflags = irq_get_trigger_type(client->irq);
1149 if (!irqflags)
1150 irqflags = IRQF_TRIGGER_FALLING;
1151
1152 error = devm_request_threaded_irq(dev, client->irq, NULL, elan_isr,
1153 irqflags | IRQF_ONESHOT,
1154 client->name, data);
1155 if (error) {
1156 dev_err(dev, "cannot register irq=%d\n", client->irq);
1157 return error;
1158 }
1159
1160 error = sysfs_create_groups(&dev->kobj, elan_sysfs_groups);
1161 if (error) {
1162 dev_err(dev, "failed to create sysfs attributes: %d\n", error);
1163 return error;
1164 }
1165
1166 error = devm_add_action(dev, elan_remove_sysfs_groups, data);
1167 if (error) {
1168 elan_remove_sysfs_groups(data);
1169 dev_err(dev, "Failed to add sysfs cleanup action: %d\n",
1170 error);
1171 return error;
1172 }
1173
1174 error = input_register_device(data->input);
1175 if (error) {
1176 dev_err(dev, "failed to register input device: %d\n", error);
1177 return error;
1178 }
1179
1180 /*
1181 * Systems using device tree should set up wakeup via DTS,
1182 * the rest will configure device as wakeup source by default.
1183 */
1184 if (!dev->of_node)
1185 device_init_wakeup(dev, true);
1186
1187 return 0;
1188}
1189
1190static int __maybe_unused elan_suspend(struct device *dev)
1191{
1192 struct i2c_client *client = to_i2c_client(dev);
1193 struct elan_tp_data *data = i2c_get_clientdata(client);
1194 int ret;
1195
1196 /*
1197 * We are taking the mutex to make sure sysfs operations are
1198 * complete before we attempt to bring the device into low[er]
1199 * power mode.
1200 */
1201 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1202 if (ret)
1203 return ret;
1204
1205 disable_irq(client->irq);
1206
1207 if (device_may_wakeup(dev)) {
1208 ret = elan_sleep(data);
1209 /* Enable wake from IRQ */
1210 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1211 } else {
1212 ret = elan_disable_power(data);
1213 }
1214
1215 mutex_unlock(&data->sysfs_mutex);
1216 return ret;
1217}
1218
1219static int __maybe_unused elan_resume(struct device *dev)
1220{
1221 struct i2c_client *client = to_i2c_client(dev);
1222 struct elan_tp_data *data = i2c_get_clientdata(client);
1223 int error;
1224
1225 if (device_may_wakeup(dev) && data->irq_wake) {
1226 disable_irq_wake(client->irq);
1227 data->irq_wake = false;
1228 }
1229
1230 error = elan_enable_power(data);
1231 if (error) {
1232 dev_err(dev, "power up when resuming failed: %d\n", error);
1233 goto err;
1234 }
1235
1236 error = elan_initialize(data);
1237 if (error)
1238 dev_err(dev, "initialize when resuming failed: %d\n", error);
1239
1240err:
1241 enable_irq(data->client->irq);
1242 return error;
1243}
1244
1245static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1246
1247static const struct i2c_device_id elan_id[] = {
1248 { DRIVER_NAME, 0 },
1249 { },
1250};
1251MODULE_DEVICE_TABLE(i2c, elan_id);
1252
1253#ifdef CONFIG_ACPI
1254static const struct acpi_device_id elan_acpi_id[] = {
1255 { "ELAN0000", 0 },
1256 { "ELAN0100", 0 },
1257 { "ELAN0600", 0 },
1258 { "ELAN0602", 0 },
1259 { "ELAN0605", 0 },
1260 { "ELAN0608", 0 },
1261 { "ELAN0609", 0 },
1262 { "ELAN060B", 0 },
1263 { "ELAN060C", 0 },
1264 { "ELAN0611", 0 },
1265 { "ELAN1000", 0 },
1266 { }
1267};
1268MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1269#endif
1270
1271#ifdef CONFIG_OF
1272static const struct of_device_id elan_of_match[] = {
1273 { .compatible = "elan,ekth3000" },
1274 { /* sentinel */ }
1275};
1276MODULE_DEVICE_TABLE(of, elan_of_match);
1277#endif
1278
1279static struct i2c_driver elan_driver = {
1280 .driver = {
1281 .name = DRIVER_NAME,
1282 .pm = &elan_pm_ops,
1283 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1284 .of_match_table = of_match_ptr(elan_of_match),
1285 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1286 },
1287 .probe = elan_probe,
1288 .id_table = elan_id,
1289};
1290
1291module_i2c_driver(elan_driver);
1292
1293MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1294MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1295MODULE_LICENSE("GPL");