1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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/module.h>
30#include <linux/slab.h>
31#include <linux/kernel.h>
32#include <linux/sched.h>
33#include <linux/input.h>
34#include <linux/uaccess.h>
35#include <linux/jiffies.h>
36#include <linux/completion.h>
37#include <linux/of.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_DRIVER_VERSION "1.6.2"
45#define ELAN_VENDOR_ID 0x04f3
46#define ETP_MAX_PRESSURE 255
47#define ETP_FWIDTH_REDUCE 90
48#define ETP_FINGER_WIDTH 15
49#define ETP_RETRY_COUNT 3
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
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
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 u16 product_id;
82 u8 fw_version;
83 u8 sm_version;
84 u8 iap_version;
85 u16 fw_checksum;
86 int pressure_adjustment;
87 u8 mode;
88 u8 ic_type;
89 u16 fw_validpage_count;
90 u16 fw_signature_address;
91
92 bool irq_wake;
93
94 u8 min_baseline;
95 u8 max_baseline;
96 bool baseline_ready;
97};
98
99static int elan_get_fwinfo(u8 iap_version, u16 *validpage_count,
100 u16 *signature_address)
101{
102 switch (iap_version) {
103 case 0x00:
104 case 0x06:
105 case 0x08:
106 *validpage_count = 512;
107 break;
108 case 0x03:
109 case 0x07:
110 case 0x09:
111 case 0x0A:
112 case 0x0B:
113 case 0x0C:
114 *validpage_count = 768;
115 break;
116 case 0x0D:
117 *validpage_count = 896;
118 break;
119 case 0x0E:
120 *validpage_count = 640;
121 break;
122 default:
123
124 *validpage_count = 0;
125 *signature_address = 0;
126 return -ENXIO;
127 }
128
129 *signature_address =
130 (*validpage_count * ETP_FW_PAGE_SIZE) - ETP_FW_SIGNATURE_SIZE;
131
132 return 0;
133}
134
135static int elan_enable_power(struct elan_tp_data *data)
136{
137 int repeat = ETP_RETRY_COUNT;
138 int error;
139
140 error = regulator_enable(data->vcc);
141 if (error) {
142 dev_err(&data->client->dev,
143 "failed to enable regulator: %d\n", error);
144 return error;
145 }
146
147 do {
148 error = data->ops->power_control(data->client, true);
149 if (error >= 0)
150 return 0;
151
152 msleep(30);
153 } while (--repeat > 0);
154
155 dev_err(&data->client->dev, "failed to enable power: %d\n", error);
156 return error;
157}
158
159static int elan_disable_power(struct elan_tp_data *data)
160{
161 int repeat = ETP_RETRY_COUNT;
162 int error;
163
164 do {
165 error = data->ops->power_control(data->client, false);
166 if (!error) {
167 error = regulator_disable(data->vcc);
168 if (error) {
169 dev_err(&data->client->dev,
170 "failed to disable regulator: %d\n",
171 error);
172
173 data->ops->power_control(data->client, true);
174 break;
175 }
176
177 return 0;
178 }
179
180 msleep(30);
181 } while (--repeat > 0);
182
183 dev_err(&data->client->dev, "failed to disable power: %d\n", error);
184 return error;
185}
186
187static int elan_sleep(struct elan_tp_data *data)
188{
189 int repeat = ETP_RETRY_COUNT;
190 int error;
191
192 do {
193 error = data->ops->sleep_control(data->client, true);
194 if (!error)
195 return 0;
196
197 msleep(30);
198 } while (--repeat > 0);
199
200 return error;
201}
202
203static int elan_query_product(struct elan_tp_data *data)
204{
205 int error;
206
207 error = data->ops->get_product_id(data->client, &data->product_id);
208 if (error)
209 return error;
210
211 error = data->ops->get_sm_version(data->client, &data->ic_type,
212 &data->sm_version);
213 if (error)
214 return error;
215
216 return 0;
217}
218
219static int elan_check_ASUS_special_fw(struct elan_tp_data *data)
220{
221 if (data->ic_type != 0x0E)
222 return false;
223
224 switch (data->product_id) {
225 case 0x05 ... 0x07:
226 case 0x09:
227 case 0x13:
228 return true;
229 default:
230 return false;
231 }
232}
233
234static int __elan_initialize(struct elan_tp_data *data)
235{
236 struct i2c_client *client = data->client;
237 bool woken_up = false;
238 int error;
239
240 error = data->ops->initialize(client);
241 if (error) {
242 dev_err(&client->dev, "device initialize failed: %d\n", error);
243 return error;
244 }
245
246 error = elan_query_product(data);
247 if (error)
248 return error;
249
250
251
252
253
254
255 if (elan_check_ASUS_special_fw(data)) {
256 error = data->ops->sleep_control(client, false);
257 if (error) {
258 dev_err(&client->dev,
259 "failed to wake device up: %d\n", error);
260 return error;
261 }
262
263 msleep(200);
264 woken_up = true;
265 }
266
267 data->mode |= ETP_ENABLE_ABS;
268 error = data->ops->set_mode(client, data->mode);
269 if (error) {
270 dev_err(&client->dev,
271 "failed to switch to absolute mode: %d\n", error);
272 return error;
273 }
274
275 if (!woken_up) {
276 error = data->ops->sleep_control(client, false);
277 if (error) {
278 dev_err(&client->dev,
279 "failed to wake device up: %d\n", error);
280 return error;
281 }
282 }
283
284 return 0;
285}
286
287static int elan_initialize(struct elan_tp_data *data)
288{
289 int repeat = ETP_RETRY_COUNT;
290 int error;
291
292 do {
293 error = __elan_initialize(data);
294 if (!error)
295 return 0;
296
297 msleep(30);
298 } while (--repeat > 0);
299
300 return error;
301}
302
303static int elan_query_device_info(struct elan_tp_data *data)
304{
305 int error;
306
307 error = data->ops->get_version(data->client, false, &data->fw_version);
308 if (error)
309 return error;
310
311 error = data->ops->get_checksum(data->client, false,
312 &data->fw_checksum);
313 if (error)
314 return error;
315
316 error = data->ops->get_version(data->client, true, &data->iap_version);
317 if (error)
318 return error;
319
320 error = data->ops->get_pressure_adjustment(data->client,
321 &data->pressure_adjustment);
322 if (error)
323 return error;
324
325 error = elan_get_fwinfo(data->iap_version, &data->fw_validpage_count,
326 &data->fw_signature_address);
327 if (error)
328 dev_warn(&data->client->dev,
329 "unexpected iap version %#04x (ic type: %#04x), firmware update will not work\n",
330 data->iap_version, data->ic_type);
331
332 return 0;
333}
334
335static unsigned int elan_convert_resolution(u8 val)
336{
337
338
339
340
341
342
343
344 return ((int)(char)val * 10 + 790) * 10 / 254;
345}
346
347static int elan_query_device_parameters(struct elan_tp_data *data)
348{
349 unsigned int x_traces, y_traces;
350 u8 hw_x_res, hw_y_res;
351 int error;
352
353 error = data->ops->get_max(data->client, &data->max_x, &data->max_y);
354 if (error)
355 return error;
356
357 error = data->ops->get_num_traces(data->client, &x_traces, &y_traces);
358 if (error)
359 return error;
360
361 data->width_x = data->max_x / x_traces;
362 data->width_y = data->max_y / y_traces;
363
364 error = data->ops->get_resolution(data->client, &hw_x_res, &hw_y_res);
365 if (error)
366 return error;
367
368 data->x_res = elan_convert_resolution(hw_x_res);
369 data->y_res = elan_convert_resolution(hw_y_res);
370
371 return 0;
372}
373
374
375
376
377
378
379static int elan_write_fw_block(struct elan_tp_data *data,
380 const u8 *page, u16 checksum, int idx)
381{
382 int retry = ETP_RETRY_COUNT;
383 int error;
384
385 do {
386 error = data->ops->write_fw_block(data->client,
387 page, checksum, idx);
388 if (!error)
389 return 0;
390
391 dev_dbg(&data->client->dev,
392 "IAP retrying page %d (error: %d)\n", idx, error);
393 } while (--retry > 0);
394
395 return error;
396}
397
398static int __elan_update_firmware(struct elan_tp_data *data,
399 const struct firmware *fw)
400{
401 struct i2c_client *client = data->client;
402 struct device *dev = &client->dev;
403 int i, j;
404 int error;
405 u16 iap_start_addr;
406 u16 boot_page_count;
407 u16 sw_checksum = 0, fw_checksum = 0;
408
409 error = data->ops->prepare_fw_update(client);
410 if (error)
411 return error;
412
413 iap_start_addr = get_unaligned_le16(&fw->data[ETP_IAP_START_ADDR * 2]);
414
415 boot_page_count = (iap_start_addr * 2) / ETP_FW_PAGE_SIZE;
416 for (i = boot_page_count; i < data->fw_validpage_count; i++) {
417 u16 checksum = 0;
418 const u8 *page = &fw->data[i * ETP_FW_PAGE_SIZE];
419
420 for (j = 0; j < ETP_FW_PAGE_SIZE; j += 2)
421 checksum += ((page[j + 1] << 8) | page[j]);
422
423 error = elan_write_fw_block(data, page, checksum, i);
424 if (error) {
425 dev_err(dev, "write page %d fail: %d\n", i, error);
426 return error;
427 }
428
429 sw_checksum += checksum;
430 }
431
432
433 msleep(600);
434
435 error = data->ops->finish_fw_update(client, &data->fw_completion);
436 if (error)
437 return error;
438
439 error = data->ops->get_checksum(client, true, &fw_checksum);
440 if (error)
441 return error;
442
443 if (sw_checksum != fw_checksum) {
444 dev_err(dev, "checksum diff sw=[%04X], fw=[%04X]\n",
445 sw_checksum, fw_checksum);
446 return -EIO;
447 }
448
449 return 0;
450}
451
452static int elan_update_firmware(struct elan_tp_data *data,
453 const struct firmware *fw)
454{
455 struct i2c_client *client = data->client;
456 int retval;
457
458 dev_dbg(&client->dev, "Starting firmware update....\n");
459
460 disable_irq(client->irq);
461 data->in_fw_update = true;
462
463 retval = __elan_update_firmware(data, fw);
464 if (retval) {
465 dev_err(&client->dev, "firmware update failed: %d\n", retval);
466 data->ops->iap_reset(client);
467 } else {
468
469 elan_initialize(data);
470 elan_query_device_info(data);
471 }
472
473 data->in_fw_update = false;
474 enable_irq(client->irq);
475
476 return retval;
477}
478
479
480
481
482
483
484static ssize_t elan_sysfs_read_fw_checksum(struct device *dev,
485 struct device_attribute *attr,
486 char *buf)
487{
488 struct i2c_client *client = to_i2c_client(dev);
489 struct elan_tp_data *data = i2c_get_clientdata(client);
490
491 return sprintf(buf, "0x%04x\n", data->fw_checksum);
492}
493
494static ssize_t elan_sysfs_read_product_id(struct device *dev,
495 struct device_attribute *attr,
496 char *buf)
497{
498 struct i2c_client *client = to_i2c_client(dev);
499 struct elan_tp_data *data = i2c_get_clientdata(client);
500
501 return sprintf(buf, ETP_PRODUCT_ID_FORMAT_STRING "\n",
502 data->product_id);
503}
504
505static ssize_t elan_sysfs_read_fw_ver(struct device *dev,
506 struct device_attribute *attr,
507 char *buf)
508{
509 struct i2c_client *client = to_i2c_client(dev);
510 struct elan_tp_data *data = i2c_get_clientdata(client);
511
512 return sprintf(buf, "%d.0\n", data->fw_version);
513}
514
515static ssize_t elan_sysfs_read_sm_ver(struct device *dev,
516 struct device_attribute *attr,
517 char *buf)
518{
519 struct i2c_client *client = to_i2c_client(dev);
520 struct elan_tp_data *data = i2c_get_clientdata(client);
521
522 return sprintf(buf, "%d.0\n", data->sm_version);
523}
524
525static ssize_t elan_sysfs_read_iap_ver(struct device *dev,
526 struct device_attribute *attr,
527 char *buf)
528{
529 struct i2c_client *client = to_i2c_client(dev);
530 struct elan_tp_data *data = i2c_get_clientdata(client);
531
532 return sprintf(buf, "%d.0\n", data->iap_version);
533}
534
535static ssize_t elan_sysfs_update_fw(struct device *dev,
536 struct device_attribute *attr,
537 const char *buf, size_t count)
538{
539 struct elan_tp_data *data = dev_get_drvdata(dev);
540 const struct firmware *fw;
541 char *fw_name;
542 int error;
543 const u8 *fw_signature;
544 static const u8 signature[] = {0xAA, 0x55, 0xCC, 0x33, 0xFF, 0xFF};
545
546 if (data->fw_validpage_count == 0)
547 return -EINVAL;
548
549
550 fw_name = kasprintf(GFP_KERNEL, ETP_FW_NAME, data->product_id);
551 if (!fw_name) {
552 dev_err(dev, "failed to allocate memory for firmware name\n");
553 return -ENOMEM;
554 }
555
556 dev_info(dev, "requesting fw '%s'\n", fw_name);
557 error = request_firmware(&fw, fw_name, dev);
558 kfree(fw_name);
559 if (error) {
560 dev_err(dev, "failed to request firmware: %d\n", error);
561 return error;
562 }
563
564
565 fw_signature = &fw->data[data->fw_signature_address];
566 if (memcmp(fw_signature, signature, sizeof(signature)) != 0) {
567 dev_err(dev, "signature mismatch (expected %*ph, got %*ph)\n",
568 (int)sizeof(signature), signature,
569 (int)sizeof(signature), fw_signature);
570 error = -EBADF;
571 goto out_release_fw;
572 }
573
574 error = mutex_lock_interruptible(&data->sysfs_mutex);
575 if (error)
576 goto out_release_fw;
577
578 error = elan_update_firmware(data, fw);
579
580 mutex_unlock(&data->sysfs_mutex);
581
582out_release_fw:
583 release_firmware(fw);
584 return error ?: count;
585}
586
587static ssize_t calibrate_store(struct device *dev,
588 struct device_attribute *attr,
589 const char *buf, size_t count)
590{
591 struct i2c_client *client = to_i2c_client(dev);
592 struct elan_tp_data *data = i2c_get_clientdata(client);
593 int tries = 20;
594 int retval;
595 int error;
596 u8 val[3];
597
598 retval = mutex_lock_interruptible(&data->sysfs_mutex);
599 if (retval)
600 return retval;
601
602 disable_irq(client->irq);
603
604 data->mode |= ETP_ENABLE_CALIBRATE;
605 retval = data->ops->set_mode(client, data->mode);
606 if (retval) {
607 dev_err(dev, "failed to enable calibration mode: %d\n",
608 retval);
609 goto out;
610 }
611
612 retval = data->ops->calibrate(client);
613 if (retval) {
614 dev_err(dev, "failed to start calibration: %d\n",
615 retval);
616 goto out_disable_calibrate;
617 }
618
619 val[0] = 0xff;
620 do {
621
622 msleep(250);
623
624 retval = data->ops->calibrate_result(client, val);
625 if (retval)
626 dev_err(dev, "failed to check calibration result: %d\n",
627 retval);
628 else if (val[0] == 0)
629 break;
630
631 } while (--tries);
632
633 if (tries == 0) {
634 dev_err(dev, "failed to calibrate. Timeout.\n");
635 retval = -ETIMEDOUT;
636 }
637
638out_disable_calibrate:
639 data->mode &= ~ETP_ENABLE_CALIBRATE;
640 error = data->ops->set_mode(data->client, data->mode);
641 if (error) {
642 dev_err(dev, "failed to disable calibration mode: %d\n",
643 error);
644 if (!retval)
645 retval = error;
646 }
647out:
648 enable_irq(client->irq);
649 mutex_unlock(&data->sysfs_mutex);
650 return retval ?: count;
651}
652
653static ssize_t elan_sysfs_read_mode(struct device *dev,
654 struct device_attribute *attr,
655 char *buf)
656{
657 struct i2c_client *client = to_i2c_client(dev);
658 struct elan_tp_data *data = i2c_get_clientdata(client);
659 int error;
660 enum tp_mode mode;
661
662 error = mutex_lock_interruptible(&data->sysfs_mutex);
663 if (error)
664 return error;
665
666 error = data->ops->iap_get_mode(data->client, &mode);
667
668 mutex_unlock(&data->sysfs_mutex);
669
670 if (error)
671 return error;
672
673 return sprintf(buf, "%d\n", (int)mode);
674}
675
676static DEVICE_ATTR(product_id, S_IRUGO, elan_sysfs_read_product_id, NULL);
677static DEVICE_ATTR(firmware_version, S_IRUGO, elan_sysfs_read_fw_ver, NULL);
678static DEVICE_ATTR(sample_version, S_IRUGO, elan_sysfs_read_sm_ver, NULL);
679static DEVICE_ATTR(iap_version, S_IRUGO, elan_sysfs_read_iap_ver, NULL);
680static DEVICE_ATTR(fw_checksum, S_IRUGO, elan_sysfs_read_fw_checksum, NULL);
681static DEVICE_ATTR(mode, S_IRUGO, elan_sysfs_read_mode, NULL);
682static DEVICE_ATTR(update_fw, S_IWUSR, NULL, elan_sysfs_update_fw);
683
684static DEVICE_ATTR_WO(calibrate);
685
686static struct attribute *elan_sysfs_entries[] = {
687 &dev_attr_product_id.attr,
688 &dev_attr_firmware_version.attr,
689 &dev_attr_sample_version.attr,
690 &dev_attr_iap_version.attr,
691 &dev_attr_fw_checksum.attr,
692 &dev_attr_calibrate.attr,
693 &dev_attr_mode.attr,
694 &dev_attr_update_fw.attr,
695 NULL,
696};
697
698static const struct attribute_group elan_sysfs_group = {
699 .attrs = elan_sysfs_entries,
700};
701
702static ssize_t acquire_store(struct device *dev, struct device_attribute *attr,
703 const char *buf, size_t count)
704{
705 struct i2c_client *client = to_i2c_client(dev);
706 struct elan_tp_data *data = i2c_get_clientdata(client);
707 int error;
708 int retval;
709
710 retval = mutex_lock_interruptible(&data->sysfs_mutex);
711 if (retval)
712 return retval;
713
714 disable_irq(client->irq);
715
716 data->baseline_ready = false;
717
718 data->mode |= ETP_ENABLE_CALIBRATE;
719 retval = data->ops->set_mode(data->client, data->mode);
720 if (retval) {
721 dev_err(dev, "Failed to enable calibration mode to get baseline: %d\n",
722 retval);
723 goto out;
724 }
725
726 msleep(250);
727
728 retval = data->ops->get_baseline_data(data->client, true,
729 &data->max_baseline);
730 if (retval) {
731 dev_err(dev, "Failed to read max baseline form device: %d\n",
732 retval);
733 goto out_disable_calibrate;
734 }
735
736 retval = data->ops->get_baseline_data(data->client, false,
737 &data->min_baseline);
738 if (retval) {
739 dev_err(dev, "Failed to read min baseline form device: %d\n",
740 retval);
741 goto out_disable_calibrate;
742 }
743
744 data->baseline_ready = true;
745
746out_disable_calibrate:
747 data->mode &= ~ETP_ENABLE_CALIBRATE;
748 error = data->ops->set_mode(data->client, data->mode);
749 if (error) {
750 dev_err(dev, "Failed to disable calibration mode after acquiring baseline: %d\n",
751 error);
752 if (!retval)
753 retval = error;
754 }
755out:
756 enable_irq(client->irq);
757 mutex_unlock(&data->sysfs_mutex);
758 return retval ?: count;
759}
760
761static ssize_t min_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
763{
764 struct i2c_client *client = to_i2c_client(dev);
765 struct elan_tp_data *data = i2c_get_clientdata(client);
766 int retval;
767
768 retval = mutex_lock_interruptible(&data->sysfs_mutex);
769 if (retval)
770 return retval;
771
772 if (!data->baseline_ready) {
773 retval = -ENODATA;
774 goto out;
775 }
776
777 retval = snprintf(buf, PAGE_SIZE, "%d", data->min_baseline);
778
779out:
780 mutex_unlock(&data->sysfs_mutex);
781 return retval;
782}
783
784static ssize_t max_show(struct device *dev,
785 struct device_attribute *attr, char *buf)
786{
787 struct i2c_client *client = to_i2c_client(dev);
788 struct elan_tp_data *data = i2c_get_clientdata(client);
789 int retval;
790
791 retval = mutex_lock_interruptible(&data->sysfs_mutex);
792 if (retval)
793 return retval;
794
795 if (!data->baseline_ready) {
796 retval = -ENODATA;
797 goto out;
798 }
799
800 retval = snprintf(buf, PAGE_SIZE, "%d", data->max_baseline);
801
802out:
803 mutex_unlock(&data->sysfs_mutex);
804 return retval;
805}
806
807
808static DEVICE_ATTR_WO(acquire);
809static DEVICE_ATTR_RO(min);
810static DEVICE_ATTR_RO(max);
811
812static struct attribute *elan_baseline_sysfs_entries[] = {
813 &dev_attr_acquire.attr,
814 &dev_attr_min.attr,
815 &dev_attr_max.attr,
816 NULL,
817};
818
819static const struct attribute_group elan_baseline_sysfs_group = {
820 .name = "baseline",
821 .attrs = elan_baseline_sysfs_entries,
822};
823
824static const struct attribute_group *elan_sysfs_groups[] = {
825 &elan_sysfs_group,
826 &elan_baseline_sysfs_group,
827 NULL
828};
829
830
831
832
833
834
835static void elan_report_contact(struct elan_tp_data *data,
836 int contact_num, bool contact_valid,
837 u8 *finger_data)
838{
839 struct input_dev *input = data->input;
840 unsigned int pos_x, pos_y;
841 unsigned int pressure, mk_x, mk_y;
842 unsigned int area_x, area_y, major, minor;
843 unsigned int scaled_pressure;
844
845 if (contact_valid) {
846 pos_x = ((finger_data[0] & 0xf0) << 4) |
847 finger_data[1];
848 pos_y = ((finger_data[0] & 0x0f) << 8) |
849 finger_data[2];
850 mk_x = (finger_data[3] & 0x0f);
851 mk_y = (finger_data[3] >> 4);
852 pressure = finger_data[4];
853
854 if (pos_x > data->max_x || pos_y > data->max_y) {
855 dev_dbg(input->dev.parent,
856 "[%d] x=%d y=%d over max (%d, %d)",
857 contact_num, pos_x, pos_y,
858 data->max_x, data->max_y);
859 return;
860 }
861
862
863
864
865
866 area_x = mk_x * (data->width_x - ETP_FWIDTH_REDUCE);
867 area_y = mk_y * (data->width_y - ETP_FWIDTH_REDUCE);
868
869 major = max(area_x, area_y);
870 minor = min(area_x, area_y);
871
872 scaled_pressure = pressure + data->pressure_adjustment;
873
874 if (scaled_pressure > ETP_MAX_PRESSURE)
875 scaled_pressure = ETP_MAX_PRESSURE;
876
877 input_mt_slot(input, contact_num);
878 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
879 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
880 input_report_abs(input, ABS_MT_POSITION_Y, data->max_y - pos_y);
881 input_report_abs(input, ABS_MT_PRESSURE, scaled_pressure);
882 input_report_abs(input, ABS_TOOL_WIDTH, mk_x);
883 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
884 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
885 } else {
886 input_mt_slot(input, contact_num);
887 input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
888 }
889}
890
891static void elan_report_absolute(struct elan_tp_data *data, u8 *packet)
892{
893 struct input_dev *input = data->input;
894 u8 *finger_data = &packet[ETP_FINGER_DATA_OFFSET];
895 int i;
896 u8 tp_info = packet[ETP_TOUCH_INFO_OFFSET];
897 u8 hover_info = packet[ETP_HOVER_INFO_OFFSET];
898 bool contact_valid, hover_event;
899
900 hover_event = hover_info & 0x40;
901 for (i = 0; i < ETP_MAX_FINGERS; i++) {
902 contact_valid = tp_info & (1U << (3 + i));
903 elan_report_contact(data, i, contact_valid, finger_data);
904
905 if (contact_valid)
906 finger_data += ETP_FINGER_DATA_LEN;
907 }
908
909 input_report_key(input, BTN_LEFT, tp_info & 0x01);
910 input_report_abs(input, ABS_DISTANCE, hover_event != 0);
911 input_mt_report_pointer_emulation(input, true);
912 input_sync(input);
913}
914
915static irqreturn_t elan_isr(int irq, void *dev_id)
916{
917 struct elan_tp_data *data = dev_id;
918 struct device *dev = &data->client->dev;
919 int error;
920 u8 report[ETP_MAX_REPORT_LEN];
921
922
923
924
925
926
927 if (data->in_fw_update) {
928 complete(&data->fw_completion);
929 goto out;
930 }
931
932 error = data->ops->get_report(data->client, report);
933 if (error)
934 goto out;
935
936 if (report[ETP_REPORT_ID_OFFSET] != ETP_REPORT_ID)
937 dev_err(dev, "invalid report id data (%x)\n",
938 report[ETP_REPORT_ID_OFFSET]);
939 else
940 elan_report_absolute(data, report);
941
942out:
943 return IRQ_HANDLED;
944}
945
946
947
948
949
950
951static int elan_setup_input_device(struct elan_tp_data *data)
952{
953 struct device *dev = &data->client->dev;
954 struct input_dev *input;
955 unsigned int max_width = max(data->width_x, data->width_y);
956 unsigned int min_width = min(data->width_x, data->width_y);
957 int error;
958
959 input = devm_input_allocate_device(dev);
960 if (!input)
961 return -ENOMEM;
962
963 input->name = "Elan Touchpad";
964 input->id.bustype = BUS_I2C;
965 input->id.vendor = ELAN_VENDOR_ID;
966 input->id.product = data->product_id;
967 input_set_drvdata(input, data);
968
969 error = input_mt_init_slots(input, ETP_MAX_FINGERS,
970 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED);
971 if (error) {
972 dev_err(dev, "failed to initialize MT slots: %d\n", error);
973 return error;
974 }
975
976 __set_bit(EV_ABS, input->evbit);
977 __set_bit(INPUT_PROP_POINTER, input->propbit);
978 __set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
979 __set_bit(BTN_LEFT, input->keybit);
980
981
982 input_set_abs_params(input, ABS_X, 0, data->max_x, 0, 0);
983 input_set_abs_params(input, ABS_Y, 0, data->max_y, 0, 0);
984 input_abs_set_res(input, ABS_X, data->x_res);
985 input_abs_set_res(input, ABS_Y, data->y_res);
986 input_set_abs_params(input, ABS_PRESSURE, 0, ETP_MAX_PRESSURE, 0, 0);
987 input_set_abs_params(input, ABS_TOOL_WIDTH, 0, ETP_FINGER_WIDTH, 0, 0);
988 input_set_abs_params(input, ABS_DISTANCE, 0, 1, 0, 0);
989
990
991 input_set_abs_params(input, ABS_MT_POSITION_X, 0, data->max_x, 0, 0);
992 input_set_abs_params(input, ABS_MT_POSITION_Y, 0, data->max_y, 0, 0);
993 input_abs_set_res(input, ABS_MT_POSITION_X, data->x_res);
994 input_abs_set_res(input, ABS_MT_POSITION_Y, data->y_res);
995 input_set_abs_params(input, ABS_MT_PRESSURE, 0,
996 ETP_MAX_PRESSURE, 0, 0);
997 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0,
998 ETP_FINGER_WIDTH * max_width, 0, 0);
999 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0,
1000 ETP_FINGER_WIDTH * min_width, 0, 0);
1001
1002 data->input = input;
1003
1004 return 0;
1005}
1006
1007static void elan_disable_regulator(void *_data)
1008{
1009 struct elan_tp_data *data = _data;
1010
1011 regulator_disable(data->vcc);
1012}
1013
1014static void elan_remove_sysfs_groups(void *_data)
1015{
1016 struct elan_tp_data *data = _data;
1017
1018 sysfs_remove_groups(&data->client->dev.kobj, elan_sysfs_groups);
1019}
1020
1021static int elan_probe(struct i2c_client *client,
1022 const struct i2c_device_id *dev_id)
1023{
1024 const struct elan_transport_ops *transport_ops;
1025 struct device *dev = &client->dev;
1026 struct elan_tp_data *data;
1027 unsigned long irqflags;
1028 int error;
1029
1030 if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_I2C) &&
1031 i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1032 transport_ops = &elan_i2c_ops;
1033 } else if (IS_ENABLED(CONFIG_MOUSE_ELAN_I2C_SMBUS) &&
1034 i2c_check_functionality(client->adapter,
1035 I2C_FUNC_SMBUS_BYTE_DATA |
1036 I2C_FUNC_SMBUS_BLOCK_DATA |
1037 I2C_FUNC_SMBUS_I2C_BLOCK)) {
1038 transport_ops = &elan_smbus_ops;
1039 } else {
1040 dev_err(dev, "not a supported I2C/SMBus adapter\n");
1041 return -EIO;
1042 }
1043
1044 data = devm_kzalloc(&client->dev, sizeof(struct elan_tp_data),
1045 GFP_KERNEL);
1046 if (!data)
1047 return -ENOMEM;
1048
1049 i2c_set_clientdata(client, data);
1050
1051 data->ops = transport_ops;
1052 data->client = client;
1053 init_completion(&data->fw_completion);
1054 mutex_init(&data->sysfs_mutex);
1055
1056 data->vcc = devm_regulator_get(&client->dev, "vcc");
1057 if (IS_ERR(data->vcc)) {
1058 error = PTR_ERR(data->vcc);
1059 if (error != -EPROBE_DEFER)
1060 dev_err(&client->dev,
1061 "Failed to get 'vcc' regulator: %d\n",
1062 error);
1063 return error;
1064 }
1065
1066 error = regulator_enable(data->vcc);
1067 if (error) {
1068 dev_err(&client->dev,
1069 "Failed to enable regulator: %d\n", error);
1070 return error;
1071 }
1072
1073 error = devm_add_action(&client->dev,
1074 elan_disable_regulator, data);
1075 if (error) {
1076 regulator_disable(data->vcc);
1077 dev_err(&client->dev,
1078 "Failed to add disable regulator action: %d\n",
1079 error);
1080 return error;
1081 }
1082
1083
1084 error = elan_initialize(data);
1085 if (error)
1086 return error;
1087
1088 error = elan_query_device_info(data);
1089 if (error)
1090 return error;
1091
1092 error = elan_query_device_parameters(data);
1093 if (error)
1094 return error;
1095
1096 dev_dbg(&client->dev,
1097 "Elan Touchpad Information:\n"
1098 " Module product ID: 0x%04x\n"
1099 " Firmware Version: 0x%04x\n"
1100 " Sample Version: 0x%04x\n"
1101 " IAP Version: 0x%04x\n"
1102 " Max ABS X,Y: %d,%d\n"
1103 " Width X,Y: %d,%d\n"
1104 " Resolution X,Y: %d,%d (dots/mm)\n",
1105 data->product_id,
1106 data->fw_version,
1107 data->sm_version,
1108 data->iap_version,
1109 data->max_x, data->max_y,
1110 data->width_x, data->width_y,
1111 data->x_res, data->y_res);
1112
1113
1114 error = elan_setup_input_device(data);
1115 if (error)
1116 return error;
1117
1118
1119
1120
1121
1122 irqflags = client->dev.of_node ? 0 : IRQF_TRIGGER_FALLING;
1123
1124 error = devm_request_threaded_irq(&client->dev, client->irq,
1125 NULL, elan_isr,
1126 irqflags | IRQF_ONESHOT,
1127 client->name, data);
1128 if (error) {
1129 dev_err(&client->dev, "cannot register irq=%d\n", client->irq);
1130 return error;
1131 }
1132
1133 error = sysfs_create_groups(&client->dev.kobj, elan_sysfs_groups);
1134 if (error) {
1135 dev_err(&client->dev, "failed to create sysfs attributes: %d\n",
1136 error);
1137 return error;
1138 }
1139
1140 error = devm_add_action(&client->dev,
1141 elan_remove_sysfs_groups, data);
1142 if (error) {
1143 elan_remove_sysfs_groups(data);
1144 dev_err(&client->dev,
1145 "Failed to add sysfs cleanup action: %d\n",
1146 error);
1147 return error;
1148 }
1149
1150 error = input_register_device(data->input);
1151 if (error) {
1152 dev_err(&client->dev, "failed to register input device: %d\n",
1153 error);
1154 return error;
1155 }
1156
1157
1158
1159
1160
1161 if (!client->dev.of_node)
1162 device_init_wakeup(&client->dev, true);
1163
1164 return 0;
1165}
1166
1167static int __maybe_unused elan_suspend(struct device *dev)
1168{
1169 struct i2c_client *client = to_i2c_client(dev);
1170 struct elan_tp_data *data = i2c_get_clientdata(client);
1171 int ret;
1172
1173
1174
1175
1176
1177
1178 ret = mutex_lock_interruptible(&data->sysfs_mutex);
1179 if (ret)
1180 return ret;
1181
1182 disable_irq(client->irq);
1183
1184 if (device_may_wakeup(dev)) {
1185 ret = elan_sleep(data);
1186
1187 data->irq_wake = (enable_irq_wake(client->irq) == 0);
1188 } else {
1189 ret = elan_disable_power(data);
1190 }
1191
1192 mutex_unlock(&data->sysfs_mutex);
1193 return ret;
1194}
1195
1196static int __maybe_unused elan_resume(struct device *dev)
1197{
1198 struct i2c_client *client = to_i2c_client(dev);
1199 struct elan_tp_data *data = i2c_get_clientdata(client);
1200 int error;
1201
1202 if (device_may_wakeup(dev) && data->irq_wake) {
1203 disable_irq_wake(client->irq);
1204 data->irq_wake = false;
1205 }
1206
1207 error = elan_enable_power(data);
1208 if (error) {
1209 dev_err(dev, "power up when resuming failed: %d\n", error);
1210 goto err;
1211 }
1212
1213 error = elan_initialize(data);
1214 if (error)
1215 dev_err(dev, "initialize when resuming failed: %d\n", error);
1216
1217err:
1218 enable_irq(data->client->irq);
1219 return error;
1220}
1221
1222static SIMPLE_DEV_PM_OPS(elan_pm_ops, elan_suspend, elan_resume);
1223
1224static const struct i2c_device_id elan_id[] = {
1225 { DRIVER_NAME, 0 },
1226 { },
1227};
1228MODULE_DEVICE_TABLE(i2c, elan_id);
1229
1230#ifdef CONFIG_ACPI
1231static const struct acpi_device_id elan_acpi_id[] = {
1232 { "ELAN0000", 0 },
1233 { "ELAN0100", 0 },
1234 { "ELAN0600", 0 },
1235 { "ELAN1000", 0 },
1236 { }
1237};
1238MODULE_DEVICE_TABLE(acpi, elan_acpi_id);
1239#endif
1240
1241#ifdef CONFIG_OF
1242static const struct of_device_id elan_of_match[] = {
1243 { .compatible = "elan,ekth3000" },
1244 { }
1245};
1246MODULE_DEVICE_TABLE(of, elan_of_match);
1247#endif
1248
1249static struct i2c_driver elan_driver = {
1250 .driver = {
1251 .name = DRIVER_NAME,
1252 .pm = &elan_pm_ops,
1253 .acpi_match_table = ACPI_PTR(elan_acpi_id),
1254 .of_match_table = of_match_ptr(elan_of_match),
1255 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1256 },
1257 .probe = elan_probe,
1258 .id_table = elan_id,
1259};
1260
1261module_i2c_driver(elan_driver);
1262
1263MODULE_AUTHOR("Duson Lin <dusonlin@emc.com.tw>");
1264MODULE_DESCRIPTION("Elan I2C/SMBus Touchpad driver");
1265MODULE_LICENSE("GPL");
1266MODULE_VERSION(ELAN_DRIVER_VERSION);
1267