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