]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/input/touchscreen/edt-ft5x06.c
edab79093fe1137180c44555f025d4fc8a675f42
[karo-tx-linux.git] / drivers / input / touchscreen / edt-ft5x06.c
1 /*
2  * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 /*
19  * This is a driver for the EDT "Polytouch" family of touch controllers
20  * based on the FocalTech FT5x06 line of chips.
21  *
22  * Development of this driver has been sponsored by Glyn:
23  *    http://www.glyn.com/Products/Displays
24  */
25
26 #include <linux/module.h>
27 #include <linux/ratelimit.h>
28 #include <linux/interrupt.h>
29 #include <linux/input.h>
30 #include <linux/i2c.h>
31 #include <linux/uaccess.h>
32 #include <linux/delay.h>
33 #include <linux/debugfs.h>
34 #include <linux/slab.h>
35 #include <linux/gpio.h>
36 #include <linux/of_gpio.h>
37 #include <linux/input/mt.h>
38 #include <linux/input/edt-ft5x06.h>
39
40 #define MAX_SUPPORT_POINTS              5
41
42 #define WORK_REGISTER_THRESHOLD         0x00
43 #define WORK_REGISTER_REPORT_RATE       0x08
44 #define WORK_REGISTER_GAIN              0x30
45 #define WORK_REGISTER_OFFSET            0x31
46 #define WORK_REGISTER_NUM_X             0x33
47 #define WORK_REGISTER_NUM_Y             0x34
48
49 #define WORK_REGISTER_OPMODE            0x3c
50 #define FACTORY_REGISTER_OPMODE         0x01
51
52 #define TOUCH_EVENT_DOWN                0x00
53 #define TOUCH_EVENT_UP                  0x01
54 #define TOUCH_EVENT_ON                  0x02
55 #define TOUCH_EVENT_RESERVED            0x03
56
57 #define EDT_NAME_LEN                    23
58 #define EDT_SWITCH_MODE_RETRIES         10
59 #define EDT_SWITCH_MODE_DELAY           5 /* msec */
60 #define EDT_RAW_DATA_RETRIES            100
61 #define EDT_RAW_DATA_DELAY              1 /* msec */
62
63 struct edt_ft5x06_ts_data {
64         struct i2c_client *client;
65         struct input_dev *input;
66         u16 num_x;
67         u16 num_y;
68
69         int reset_pin;
70         int irq_pin;
71         int wake_pin;
72
73 #if defined(CONFIG_DEBUG_FS)
74         struct dentry *debug_dir;
75         u8 *raw_buffer;
76         size_t raw_bufsize;
77 #endif
78
79         struct mutex mutex;
80         bool factory_mode;
81         int threshold;
82         int gain;
83         int offset;
84         int report_rate;
85
86         char name[EDT_NAME_LEN];
87 };
88
89 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
90                                    u16 wr_len, u8 *wr_buf,
91                                    u16 rd_len, u8 *rd_buf)
92 {
93         struct i2c_msg wrmsg[2];
94         int i = 0;
95         int ret;
96
97         if (wr_len) {
98                 wrmsg[i].addr  = client->addr;
99                 wrmsg[i].flags = 0;
100                 wrmsg[i].len = wr_len;
101                 wrmsg[i].buf = wr_buf;
102                 i++;
103         }
104         if (rd_len) {
105                 wrmsg[i].addr  = client->addr;
106                 wrmsg[i].flags = I2C_M_RD;
107                 wrmsg[i].len = rd_len;
108                 wrmsg[i].buf = rd_buf;
109                 i++;
110         }
111
112         ret = i2c_transfer(client->adapter, wrmsg, i);
113         if (ret < 0)
114                 return ret;
115         if (ret != i)
116                 return -EIO;
117
118         return 0;
119 }
120
121 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
122                                     u8 *buf, int buflen)
123 {
124         int i;
125         u8 crc = 0;
126
127         for (i = 0; i < buflen - 1; i++)
128                 crc ^= buf[i];
129
130         if (crc != buf[buflen-1]) {
131                 dev_err_ratelimited(&tsdata->client->dev,
132                                     "crc error: 0x%02x expected, got 0x%02x\n",
133                                     crc, buf[buflen-1]);
134                 return false;
135         }
136
137         return true;
138 }
139
140 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
141 {
142         struct edt_ft5x06_ts_data *tsdata = dev_id;
143         struct device *dev = &tsdata->client->dev;
144         u8 cmd = 0xf9;
145         u8 rdbuf[26];
146         int i, type, x, y, id;
147         int error;
148
149         memset(rdbuf, 0, sizeof(rdbuf));
150
151         error = edt_ft5x06_ts_readwrite(tsdata->client,
152                                         sizeof(cmd), &cmd,
153                                         sizeof(rdbuf), rdbuf);
154         if (error) {
155                 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
156                                     error);
157                 goto out;
158         }
159
160         if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa || rdbuf[2] != 26) {
161                 dev_err_ratelimited(dev, "Unexpected header: %02x%02x%02x!\n",
162                                     rdbuf[0], rdbuf[1], rdbuf[2]);
163                 goto out;
164         }
165
166         if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, 26))
167                 goto out;
168
169         for (i = 0; i < MAX_SUPPORT_POINTS; i++) {
170                 u8 *buf = &rdbuf[i * 4 + 5];
171                 bool down;
172
173                 type = buf[0] >> 6;
174                 /* ignore Reserved events */
175                 if (type == TOUCH_EVENT_RESERVED)
176                         continue;
177
178                 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
179                 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
180                 id = (buf[2] >> 4) & 0x0f;
181                 down = (type != TOUCH_EVENT_UP);
182
183                 input_mt_slot(tsdata->input, id);
184                 input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
185
186                 if (!down)
187                         continue;
188
189                 input_report_abs(tsdata->input, ABS_MT_POSITION_X, x);
190                 input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y);
191         }
192
193         input_mt_report_pointer_emulation(tsdata->input, true);
194         input_sync(tsdata->input);
195
196 out:
197         return IRQ_HANDLED;
198 }
199
200 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
201                                      u8 addr, u8 value)
202 {
203         u8 wrbuf[4];
204
205         wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
206         wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
207         wrbuf[2] = value;
208         wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
209
210         return edt_ft5x06_ts_readwrite(tsdata->client, 4, wrbuf, 0, NULL);
211 }
212
213 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
214                                     u8 addr)
215 {
216         u8 wrbuf[2], rdbuf[2];
217         int error;
218
219         wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
220         wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
221         wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
222
223         error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2, rdbuf);
224         if (error)
225                 return error;
226
227         if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
228                 dev_err(&tsdata->client->dev,
229                         "crc error: 0x%02x expected, got 0x%02x\n",
230                         wrbuf[0] ^ wrbuf[1] ^ rdbuf[0], rdbuf[1]);
231                 return -EIO;
232         }
233
234         return rdbuf[0];
235 }
236
237 struct edt_ft5x06_attribute {
238         struct device_attribute dattr;
239         size_t field_offset;
240         u8 limit_low;
241         u8 limit_high;
242         u8 addr;
243 };
244
245 #define EDT_ATTR(_field, _mode, _addr, _limit_low, _limit_high)         \
246         struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = {        \
247                 .dattr = __ATTR(_field, _mode,                          \
248                                 edt_ft5x06_setting_show,                \
249                                 edt_ft5x06_setting_store),              \
250                 .field_offset =                                         \
251                         offsetof(struct edt_ft5x06_ts_data, _field),    \
252                 .limit_low = _limit_low,                                \
253                 .limit_high = _limit_high,                              \
254                 .addr = _addr,                                          \
255         }
256
257 static ssize_t edt_ft5x06_setting_show(struct device *dev,
258                                        struct device_attribute *dattr,
259                                        char *buf)
260 {
261         struct i2c_client *client = to_i2c_client(dev);
262         struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
263         struct edt_ft5x06_attribute *attr =
264                         container_of(dattr, struct edt_ft5x06_attribute, dattr);
265         u8 *field = (u8 *)((char *)tsdata + attr->field_offset);
266         int val;
267         size_t count = 0;
268         int error = 0;
269
270         mutex_lock(&tsdata->mutex);
271
272         if (tsdata->factory_mode) {
273                 error = -EIO;
274                 goto out;
275         }
276
277         val = edt_ft5x06_register_read(tsdata, attr->addr);
278         if (val < 0) {
279                 error = val;
280                 dev_err(&tsdata->client->dev,
281                         "Failed to fetch attribute %s, error %d\n",
282                         dattr->attr.name, error);
283                 goto out;
284         }
285
286         if (val != *field) {
287                 dev_warn(&tsdata->client->dev,
288                          "%s: read (%d) and stored value (%d) differ\n",
289                          dattr->attr.name, val, *field);
290                 *field = val;
291         }
292
293         count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
294 out:
295         mutex_unlock(&tsdata->mutex);
296         return error ?: count;
297 }
298
299 static ssize_t edt_ft5x06_setting_store(struct device *dev,
300                                         struct device_attribute *dattr,
301                                         const char *buf, size_t count)
302 {
303         struct i2c_client *client = to_i2c_client(dev);
304         struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
305         struct edt_ft5x06_attribute *attr =
306                         container_of(dattr, struct edt_ft5x06_attribute, dattr);
307         u8 *field = (u8 *)((char *)tsdata + attr->field_offset);
308         unsigned int val;
309         int error;
310
311         mutex_lock(&tsdata->mutex);
312
313         if (tsdata->factory_mode) {
314                 error = -EIO;
315                 goto out;
316         }
317
318         error = kstrtouint(buf, 0, &val);
319         if (error)
320                 goto out;
321
322         if (val < attr->limit_low || val > attr->limit_high) {
323                 error = -ERANGE;
324                 goto out;
325         }
326
327         error = edt_ft5x06_register_write(tsdata, attr->addr, val);
328         if (error) {
329                 dev_err(&tsdata->client->dev,
330                         "Failed to update attribute %s, error: %d\n",
331                         dattr->attr.name, error);
332                 goto out;
333         }
334
335         *field = val;
336
337 out:
338         mutex_unlock(&tsdata->mutex);
339         return error ?: count;
340 }
341
342 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN, 0, 31);
343 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET, 0, 31);
344 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO,
345                 WORK_REGISTER_THRESHOLD, 20, 80);
346 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO,
347                 WORK_REGISTER_REPORT_RATE, 3, 14);
348
349 static struct attribute *edt_ft5x06_attrs[] = {
350         &edt_ft5x06_attr_gain.dattr.attr,
351         &edt_ft5x06_attr_offset.dattr.attr,
352         &edt_ft5x06_attr_threshold.dattr.attr,
353         &edt_ft5x06_attr_report_rate.dattr.attr,
354         NULL
355 };
356
357 static const struct attribute_group edt_ft5x06_attr_group = {
358         .attrs = edt_ft5x06_attrs,
359 };
360
361 #ifdef CONFIG_DEBUG_FS
362 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
363 {
364         struct i2c_client *client = tsdata->client;
365         int retries = EDT_SWITCH_MODE_RETRIES;
366         int ret;
367         int error;
368
369         disable_irq(client->irq);
370
371         if (!tsdata->raw_buffer) {
372                 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
373                                       sizeof(u16);
374                 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
375                 if (!tsdata->raw_buffer) {
376                         error = -ENOMEM;
377                         goto err_out;
378                 }
379         }
380
381         /* mode register is 0x3c when in the work mode */
382         error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
383         if (error) {
384                 dev_err(&client->dev,
385                         "failed to switch to factory mode, error %d\n", error);
386                 goto err_out;
387         }
388
389         tsdata->factory_mode = true;
390         do {
391                 mdelay(EDT_SWITCH_MODE_DELAY);
392                 /* mode register is 0x01 when in factory mode */
393                 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
394                 if (ret == 0x03)
395                         break;
396         } while (--retries > 0);
397
398         if (retries == 0) {
399                 dev_err(&client->dev, "not in factory mode after %dms.\n",
400                         EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
401                 error = -EIO;
402                 goto err_out;
403         }
404
405         return 0;
406
407 err_out:
408         kfree(tsdata->raw_buffer);
409         tsdata->raw_buffer = NULL;
410         tsdata->factory_mode = false;
411         enable_irq(client->irq);
412
413         return error;
414 }
415
416 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
417 {
418         struct i2c_client *client = tsdata->client;
419         int retries = EDT_SWITCH_MODE_RETRIES;
420         int ret;
421         int error;
422
423         /* mode register is 0x01 when in the factory mode */
424         error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
425         if (error) {
426                 dev_err(&client->dev,
427                         "failed to switch to work mode, error: %d\n", error);
428                 return error;
429         }
430
431         tsdata->factory_mode = false;
432
433         do {
434                 mdelay(EDT_SWITCH_MODE_DELAY);
435                 /* mode register is 0x01 when in factory mode */
436                 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
437                 if (ret == 0x01)
438                         break;
439         } while (--retries > 0);
440
441         if (retries == 0) {
442                 dev_err(&client->dev, "not in work mode after %dms.\n",
443                         EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
444                 tsdata->factory_mode = true;
445                 return -EIO;
446         }
447
448         kfree(tsdata->raw_buffer);
449         tsdata->raw_buffer = NULL;
450
451         /* restore parameters */
452         edt_ft5x06_register_write(tsdata, WORK_REGISTER_THRESHOLD,
453                                   tsdata->threshold);
454         edt_ft5x06_register_write(tsdata, WORK_REGISTER_GAIN,
455                                   tsdata->gain);
456         edt_ft5x06_register_write(tsdata, WORK_REGISTER_OFFSET,
457                                   tsdata->offset);
458         edt_ft5x06_register_write(tsdata, WORK_REGISTER_REPORT_RATE,
459                                   tsdata->report_rate);
460
461         enable_irq(client->irq);
462
463         return 0;
464 }
465
466 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
467 {
468         struct edt_ft5x06_ts_data *tsdata = data;
469
470         *mode = tsdata->factory_mode;
471
472         return 0;
473 };
474
475 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
476 {
477         struct edt_ft5x06_ts_data *tsdata = data;
478         int retval = 0;
479
480         if (mode > 1)
481                 return -ERANGE;
482
483         mutex_lock(&tsdata->mutex);
484
485         if (mode != tsdata->factory_mode) {
486                 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
487                                 edt_ft5x06_work_mode(tsdata);
488         }
489
490         mutex_unlock(&tsdata->mutex);
491
492         return retval;
493 };
494
495 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
496                         edt_ft5x06_debugfs_mode_set, "%llu\n");
497
498 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
499                                 char __user *buf, size_t count, loff_t *off)
500 {
501         struct edt_ft5x06_ts_data *tsdata = file->private_data;
502         struct i2c_client *client = tsdata->client;
503         int retries  = EDT_RAW_DATA_RETRIES;
504         int val, i, error;
505         size_t read = 0;
506         int colbytes;
507         char wrbuf[3];
508         u8 *rdbuf;
509
510         if (*off < 0 || *off >= tsdata->raw_bufsize)
511                 return 0;
512
513         mutex_lock(&tsdata->mutex);
514
515         if (!tsdata->factory_mode || !tsdata->raw_buffer) {
516                 error = -EIO;
517                 goto out;
518         }
519
520         error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
521         if (error) {
522                 dev_dbg(&client->dev,
523                         "failed to write 0x08 register, error %d\n", error);
524                 goto out;
525         }
526
527         do {
528                 msleep(EDT_RAW_DATA_DELAY);
529                 val = edt_ft5x06_register_read(tsdata, 0x08);
530                 if (val < 1)
531                         break;
532         } while (--retries > 0);
533
534         if (val < 0) {
535                 error = val;
536                 dev_dbg(&client->dev,
537                         "failed to read 0x08 register, error %d\n", error);
538                 goto out;
539         }
540
541         if (retries == 0) {
542                 dev_dbg(&client->dev,
543                         "timed out waiting for register to settle\n");
544                 error = -ETIMEDOUT;
545                 goto out;
546         }
547
548         rdbuf = tsdata->raw_buffer;
549         colbytes = tsdata->num_y * sizeof(u16);
550
551         wrbuf[0] = 0xf5;
552         wrbuf[1] = 0x0e;
553         for (i = 0; i < tsdata->num_x; i++) {
554                 wrbuf[2] = i;  /* column index */
555                 error = edt_ft5x06_ts_readwrite(tsdata->client,
556                                                 sizeof(wrbuf), wrbuf,
557                                                 colbytes, rdbuf);
558                 if (error)
559                         goto out;
560
561                 rdbuf += colbytes;
562         }
563
564         read = min_t(size_t, count, tsdata->raw_bufsize - *off);
565         if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
566                 error = -EFAULT;
567                 goto out;
568         }
569
570         *off += read;
571 out:
572         mutex_unlock(&tsdata->mutex);
573         return error ?: read;
574 };
575
576
577 static const struct file_operations debugfs_raw_data_fops = {
578         .open = simple_open,
579         .read = edt_ft5x06_debugfs_raw_data_read,
580 };
581
582 static void
583 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
584                               const char *debugfs_name)
585 {
586         tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
587         if (!tsdata->debug_dir)
588                 return;
589
590         debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
591         debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
592
593         debugfs_create_file("mode", S_IRUSR | S_IWUSR,
594                             tsdata->debug_dir, tsdata, &debugfs_mode_fops);
595         debugfs_create_file("raw_data", S_IRUSR,
596                             tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
597 }
598
599 static void
600 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
601 {
602         if (tsdata->debug_dir)
603                 debugfs_remove_recursive(tsdata->debug_dir);
604         kfree(tsdata->raw_buffer);
605 }
606
607 #else
608
609 static inline void
610 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
611                               const char *debugfs_name)
612 {
613 }
614
615 static inline void
616 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
617 {
618 }
619
620 #endif /* CONFIG_DEBUGFS */
621
622
623
624 static int edt_ft5x06_ts_reset(struct i2c_client *client,
625                         struct edt_ft5x06_ts_data *tsdata)
626 {
627         int error;
628
629         if (gpio_is_valid(tsdata->wake_pin)) {
630                 error = gpio_request_one(tsdata->wake_pin, GPIOF_OUT_INIT_LOW,
631                                          "edt-ft5x06 wake");
632                 if (error) {
633                         dev_err(&client->dev,
634                                 "Failed to request GPIO %d as wake pin, error %d\n",
635                                 tsdata->wake_pin, error);
636                         return error;
637                 }
638
639                 mdelay(5);
640                 gpio_set_value(tsdata->wake_pin, 1);
641         }
642         if (gpio_is_valid(tsdata->reset_pin)) {
643                 /* this pulls reset down, enabling the low active reset */
644                 error = gpio_request_one(tsdata->reset_pin, GPIOF_OUT_INIT_LOW,
645                                          "edt-ft5x06 reset");
646                 if (error) {
647                         dev_err(&client->dev,
648                                 "Failed to request GPIO %d as reset pin, error %d\n",
649                                 tsdata->reset_pin, error);
650                         return error;
651                 }
652
653                 mdelay(5);
654                 gpio_set_value(tsdata->reset_pin, 1);
655                 mdelay(300);
656         }
657
658         return 0;
659 }
660
661 static int edt_ft5x06_ts_identify(struct i2c_client *client,
662                                             char *model_name,
663                                             char *fw_version)
664 {
665         u8 rdbuf[EDT_NAME_LEN];
666         char *p;
667         int error;
668
669         error = edt_ft5x06_ts_readwrite(client, 1, "\xbb",
670                                         EDT_NAME_LEN - 1, rdbuf);
671         if (error)
672                 return error;
673
674         /* remove last '$' end marker */
675         rdbuf[EDT_NAME_LEN - 1] = '\0';
676         if (rdbuf[EDT_NAME_LEN - 2] == '$')
677                 rdbuf[EDT_NAME_LEN - 2] = '\0';
678
679         /* look for Model/Version separator */
680         p = strchr(rdbuf, '*');
681         if (p)
682                 *p++ = '\0';
683
684         strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
685         strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
686
687         return 0;
688 }
689
690 #define EDT_ATTR_CHECKSET(name, reg) \
691         if (pdata->name >= edt_ft5x06_attr_##name.limit_low &&          \
692             pdata->name <= edt_ft5x06_attr_##name.limit_high)           \
693                 edt_ft5x06_register_write(tsdata, reg, pdata->name)
694
695 #define EDT_GET_PROP(name, reg) {                                       \
696         const u32 *prop = of_get_property(np, #name, NULL);             \
697         if (prop)                                                       \
698                 edt_ft5x06_register_write(tsdata, reg, be32_to_cpu(*prop)); \
699 }
700
701 static void edt_ft5x06_ts_get_dt_defaults(struct device_node *np,
702                                         struct edt_ft5x06_ts_data *tsdata)
703 {
704         EDT_GET_PROP(threshold, WORK_REGISTER_THRESHOLD);
705         EDT_GET_PROP(gain, WORK_REGISTER_GAIN);
706         EDT_GET_PROP(offset, WORK_REGISTER_OFFSET);
707         EDT_GET_PROP(report_rate, WORK_REGISTER_REPORT_RATE);
708 }
709
710 static void
711 edt_ft5x06_ts_get_defaults(struct edt_ft5x06_ts_data *tsdata,
712                            const struct edt_ft5x06_platform_data *pdata)
713 {
714         if (!pdata->use_parameters)
715                 return;
716
717         /* pick up defaults from the platform data */
718         EDT_ATTR_CHECKSET(threshold, WORK_REGISTER_THRESHOLD);
719         EDT_ATTR_CHECKSET(gain, WORK_REGISTER_GAIN);
720         EDT_ATTR_CHECKSET(offset, WORK_REGISTER_OFFSET);
721         EDT_ATTR_CHECKSET(report_rate, WORK_REGISTER_REPORT_RATE);
722 }
723
724 static void
725 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
726 {
727         tsdata->threshold = edt_ft5x06_register_read(tsdata,
728                                                      WORK_REGISTER_THRESHOLD);
729         tsdata->gain = edt_ft5x06_register_read(tsdata, WORK_REGISTER_GAIN);
730         tsdata->offset = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OFFSET);
731         tsdata->report_rate = edt_ft5x06_register_read(tsdata,
732                                                 WORK_REGISTER_REPORT_RATE);
733         tsdata->num_x = edt_ft5x06_register_read(tsdata, WORK_REGISTER_NUM_X);
734         tsdata->num_y = edt_ft5x06_register_read(tsdata, WORK_REGISTER_NUM_Y);
735 }
736
737 #ifdef CONFIG_OF
738 static int edt_ft5x06_i2c_ts_probe_dt(struct device *dev,
739                                 struct edt_ft5x06_ts_data *tsdata)
740 {
741         int ret;
742         struct device_node *np = dev->of_node;
743         enum of_gpio_flags gpio_flags;
744
745         if (!np)
746                 return -ENODEV;
747
748         /*
749          * irq_pin is not needed for DT setup.
750          * irq is associated via 'interrupts' property in DT
751          */
752         tsdata->irq_pin = -EINVAL;
753
754         ret = of_get_named_gpio_flags(np, "reset-gpios", 0, &gpio_flags);
755         tsdata->reset_pin = ret;
756
757         ret = of_get_named_gpio_flags(np, "wake-gpios", 0, &gpio_flags);
758         tsdata->wake_pin = ret;
759
760         return 0;
761 }
762 #else
763 static inline int edt_ft5x06_i2c_ts_probe_dt(struct device *dev,
764                                         struct edt_ft5x06_i2c_ts_data *tsdata)
765 {
766         return -ENODEV;
767 }
768 #endif
769
770 static int edt_ft5x06_ts_probe(struct i2c_client *client,
771                                          const struct i2c_device_id *id)
772 {
773         const struct edt_ft5x06_platform_data *pdata =
774                                                 client->dev.platform_data;
775         struct edt_ft5x06_ts_data *tsdata;
776         struct input_dev *input;
777         int error;
778         char fw_version[EDT_NAME_LEN];
779
780         dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
781
782         tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
783         if (!tsdata) {
784                 dev_err(&client->dev, "failed to allocate driver data.\n");
785                 return -ENOMEM;
786         }
787
788         if (!pdata) {
789                 error = edt_ft5x06_i2c_ts_probe_dt(&client->dev, tsdata);
790                 if (error) {
791                         dev_err(&client->dev,
792                                 "DT probe failed and no platform data present\n");
793                         return error;
794                 }
795         } else {
796                 tsdata->reset_pin = pdata->reset_pin;
797                 tsdata->irq_pin = pdata->irq_pin;
798                 tsdata->wake_pin = -EINVAL;
799         }
800
801         error = edt_ft5x06_ts_reset(client, tsdata);
802         if (error)
803                 return error;
804
805         if (gpio_is_valid(tsdata->irq_pin)) {
806                 error = gpio_request_one(tsdata->irq_pin,
807                                          GPIOF_IN, "edt-ft5x06 irq");
808                 if (error) {
809                         dev_err(&client->dev,
810                                 "Failed to request GPIO %d, error %d\n",
811                                 tsdata->irq_pin, error);
812                         return error;
813                 }
814         }
815
816         input = input_allocate_device();
817         if (!input) {
818                 dev_err(&client->dev, "failed to allocate input device.\n");
819                 error = -ENOMEM;
820                 goto err_free_mem;
821         }
822
823         mutex_init(&tsdata->mutex);
824         tsdata->client = client;
825         tsdata->input = input;
826         tsdata->factory_mode = false;
827
828         error = edt_ft5x06_ts_identify(client, tsdata->name, fw_version);
829         if (error) {
830                 dev_err(&client->dev, "touchscreen probe failed\n");
831                 goto err_free_mem;
832         }
833
834         if (!pdata)
835                 edt_ft5x06_ts_get_dt_defaults(client->dev.of_node, tsdata);
836         else
837                 edt_ft5x06_ts_get_defaults(tsdata, pdata);
838
839         edt_ft5x06_ts_get_parameters(tsdata);
840
841         dev_dbg(&client->dev,
842                 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
843                 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
844
845         input->name = tsdata->name;
846         input->id.bustype = BUS_I2C;
847         input->dev.parent = &client->dev;
848
849         __set_bit(EV_SYN, input->evbit);
850         __set_bit(EV_KEY, input->evbit);
851         __set_bit(EV_ABS, input->evbit);
852         __set_bit(BTN_TOUCH, input->keybit);
853         input_set_abs_params(input, ABS_X, 0, tsdata->num_x * 64 - 1, 0, 0);
854         input_set_abs_params(input, ABS_Y, 0, tsdata->num_y * 64 - 1, 0, 0);
855         input_set_abs_params(input, ABS_MT_POSITION_X,
856                              0, tsdata->num_x * 64 - 1, 0, 0);
857         input_set_abs_params(input, ABS_MT_POSITION_Y,
858                              0, tsdata->num_y * 64 - 1, 0, 0);
859         error = input_mt_init_slots(input, MAX_SUPPORT_POINTS, 0);
860         if (error) {
861                 dev_err(&client->dev, "Unable to init MT slots.\n");
862                 goto err_free_mem;
863         }
864
865         input_set_drvdata(input, tsdata);
866         i2c_set_clientdata(client, tsdata);
867
868         error = request_threaded_irq(client->irq, NULL, edt_ft5x06_ts_isr,
869                                      IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
870                                      client->name, tsdata);
871         if (error) {
872                 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
873                 goto err_free_mem;
874         }
875
876         error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group);
877         if (error)
878                 goto err_free_irq;
879
880         error = input_register_device(input);
881         if (error)
882                 goto err_remove_attrs;
883
884         edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
885         device_init_wakeup(&client->dev, 1);
886
887         dev_dbg(&client->dev,
888                 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
889                 client->irq, tsdata->wake_pin, tsdata->reset_pin);
890
891         return 0;
892
893 err_remove_attrs:
894         sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
895 err_free_irq:
896         free_irq(client->irq, tsdata);
897 err_free_mem:
898         input_free_device(input);
899         if (gpio_is_valid(tsdata->irq_pin))
900                 gpio_free(tsdata->irq_pin);
901         if (gpio_is_valid(tsdata->reset_pin))
902                 gpio_free(tsdata->reset_pin);
903         if (gpio_is_valid(tsdata->wake_pin))
904                 gpio_free(tsdata->wake_pin);
905
906         return error;
907 }
908
909 static int edt_ft5x06_ts_remove(struct i2c_client *client)
910 {
911         struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
912
913         edt_ft5x06_ts_teardown_debugfs(tsdata);
914         sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
915
916         free_irq(client->irq, tsdata);
917         input_unregister_device(tsdata->input);
918
919         if (gpio_is_valid(tsdata->irq_pin))
920                 gpio_free(tsdata->irq_pin);
921         if (gpio_is_valid(tsdata->reset_pin))
922                 gpio_free(tsdata->reset_pin);
923         if (gpio_is_valid(tsdata->wake_pin))
924                 gpio_free(tsdata->wake_pin);
925
926         return 0;
927 }
928
929 #ifdef CONFIG_PM_SLEEP
930 static int edt_ft5x06_ts_suspend(struct device *dev)
931 {
932         struct i2c_client *client = to_i2c_client(dev);
933
934         if (device_may_wakeup(dev))
935                 enable_irq_wake(client->irq);
936
937         return 0;
938 }
939
940 static int edt_ft5x06_ts_resume(struct device *dev)
941 {
942         struct i2c_client *client = to_i2c_client(dev);
943
944         if (device_may_wakeup(dev))
945                 disable_irq_wake(client->irq);
946
947         return 0;
948 }
949 #endif
950
951 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
952                          edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
953
954 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
955         { "edt-ft5x06", 0, },
956         { /* sentinel */ }
957 };
958 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
959
960 static const struct of_device_id edt_ft5x06_of_match[] = {
961         { .compatible = "edt,edt-ft5x06", },
962         { /* sentinel */ }
963 };
964 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
965
966 static struct i2c_driver edt_ft5x06_ts_driver = {
967         .driver = {
968                 .owner = THIS_MODULE,
969                 .name = "edt_ft5x06",
970                 .of_match_table = edt_ft5x06_of_match,
971                 .pm = &edt_ft5x06_ts_pm_ops,
972         },
973         .id_table = edt_ft5x06_ts_id,
974         .probe    = edt_ft5x06_ts_probe,
975         .remove   = edt_ft5x06_ts_remove,
976 };
977
978 module_i2c_driver(edt_ft5x06_ts_driver);
979
980 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
981 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
982 MODULE_LICENSE("GPL");
983 MODULE_ALIAS("platform:edt-ft5x06");