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