1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/module.h>
17#include <linux/ratelimit.h>
18#include <linux/irq.h>
19#include <linux/interrupt.h>
20#include <linux/input.h>
21#include <linux/i2c.h>
22#include <linux/kernel.h>
23#include <linux/uaccess.h>
24#include <linux/delay.h>
25#include <linux/debugfs.h>
26#include <linux/slab.h>
27#include <linux/gpio/consumer.h>
28#include <linux/input/mt.h>
29#include <linux/input/touchscreen.h>
30#include <asm/unaligned.h>
31
32#define WORK_REGISTER_THRESHOLD 0x00
33#define WORK_REGISTER_REPORT_RATE 0x08
34#define WORK_REGISTER_GAIN 0x30
35#define WORK_REGISTER_OFFSET 0x31
36#define WORK_REGISTER_NUM_X 0x33
37#define WORK_REGISTER_NUM_Y 0x34
38
39#define M09_REGISTER_THRESHOLD 0x80
40#define M09_REGISTER_GAIN 0x92
41#define M09_REGISTER_OFFSET 0x93
42#define M09_REGISTER_NUM_X 0x94
43#define M09_REGISTER_NUM_Y 0x95
44
45#define EV_REGISTER_THRESHOLD 0x40
46#define EV_REGISTER_GAIN 0x41
47#define EV_REGISTER_OFFSET_Y 0x45
48#define EV_REGISTER_OFFSET_X 0x46
49
50#define NO_REGISTER 0xff
51
52#define WORK_REGISTER_OPMODE 0x3c
53#define FACTORY_REGISTER_OPMODE 0x01
54
55#define TOUCH_EVENT_DOWN 0x00
56#define TOUCH_EVENT_UP 0x01
57#define TOUCH_EVENT_ON 0x02
58#define TOUCH_EVENT_RESERVED 0x03
59
60#define EDT_NAME_LEN 23
61#define EDT_SWITCH_MODE_RETRIES 10
62#define EDT_SWITCH_MODE_DELAY 5
63#define EDT_RAW_DATA_RETRIES 100
64#define EDT_RAW_DATA_DELAY 1000
65
66enum edt_ver {
67 EDT_M06,
68 EDT_M09,
69 EDT_M12,
70 EV_FT,
71 GENERIC_FT,
72};
73
74struct edt_reg_addr {
75 int reg_threshold;
76 int reg_report_rate;
77 int reg_gain;
78 int reg_offset;
79 int reg_offset_x;
80 int reg_offset_y;
81 int reg_num_x;
82 int reg_num_y;
83};
84
85struct edt_ft5x06_ts_data {
86 struct i2c_client *client;
87 struct input_dev *input;
88 struct touchscreen_properties prop;
89 u16 num_x;
90 u16 num_y;
91
92 struct gpio_desc *reset_gpio;
93 struct gpio_desc *wake_gpio;
94
95#if defined(CONFIG_DEBUG_FS)
96 struct dentry *debug_dir;
97 u8 *raw_buffer;
98 size_t raw_bufsize;
99#endif
100
101 struct mutex mutex;
102 bool factory_mode;
103 int threshold;
104 int gain;
105 int offset;
106 int offset_x;
107 int offset_y;
108 int report_rate;
109 int max_support_points;
110
111 char name[EDT_NAME_LEN];
112
113 struct edt_reg_addr reg_addr;
114 enum edt_ver version;
115};
116
117struct edt_i2c_chip_data {
118 int max_support_points;
119};
120
121static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
122 u16 wr_len, u8 *wr_buf,
123 u16 rd_len, u8 *rd_buf)
124{
125 struct i2c_msg wrmsg[2];
126 int i = 0;
127 int ret;
128
129 if (wr_len) {
130 wrmsg[i].addr = client->addr;
131 wrmsg[i].flags = 0;
132 wrmsg[i].len = wr_len;
133 wrmsg[i].buf = wr_buf;
134 i++;
135 }
136 if (rd_len) {
137 wrmsg[i].addr = client->addr;
138 wrmsg[i].flags = I2C_M_RD;
139 wrmsg[i].len = rd_len;
140 wrmsg[i].buf = rd_buf;
141 i++;
142 }
143
144 ret = i2c_transfer(client->adapter, wrmsg, i);
145 if (ret < 0)
146 return ret;
147 if (ret != i)
148 return -EIO;
149
150 return 0;
151}
152
153static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
154 u8 *buf, int buflen)
155{
156 int i;
157 u8 crc = 0;
158
159 for (i = 0; i < buflen - 1; i++)
160 crc ^= buf[i];
161
162 if (crc != buf[buflen-1]) {
163 dev_err_ratelimited(&tsdata->client->dev,
164 "crc error: 0x%02x expected, got 0x%02x\n",
165 crc, buf[buflen-1]);
166 return false;
167 }
168
169 return true;
170}
171
172static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
173{
174 struct edt_ft5x06_ts_data *tsdata = dev_id;
175 struct device *dev = &tsdata->client->dev;
176 u8 cmd;
177 u8 rdbuf[63];
178 int i, type, x, y, id;
179 int offset, tplen, datalen, crclen;
180 int error;
181
182 switch (tsdata->version) {
183 case EDT_M06:
184 cmd = 0xf9;
185 offset = 5;
186 tplen = 4;
187 crclen = 1;
188 break;
189
190 case EDT_M09:
191 case EDT_M12:
192 case EV_FT:
193 case GENERIC_FT:
194 cmd = 0x0;
195 offset = 3;
196 tplen = 6;
197 crclen = 0;
198 break;
199
200 default:
201 goto out;
202 }
203
204 memset(rdbuf, 0, sizeof(rdbuf));
205 datalen = tplen * tsdata->max_support_points + offset + crclen;
206
207 error = edt_ft5x06_ts_readwrite(tsdata->client,
208 sizeof(cmd), &cmd,
209 datalen, rdbuf);
210 if (error) {
211 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
212 error);
213 goto out;
214 }
215
216
217 if (tsdata->version == EDT_M06) {
218 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
219 rdbuf[2] != datalen) {
220 dev_err_ratelimited(dev,
221 "Unexpected header: %02x%02x%02x!\n",
222 rdbuf[0], rdbuf[1], rdbuf[2]);
223 goto out;
224 }
225
226 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
227 goto out;
228 }
229
230 for (i = 0; i < tsdata->max_support_points; i++) {
231 u8 *buf = &rdbuf[i * tplen + offset];
232
233 type = buf[0] >> 6;
234
235 if (type == TOUCH_EVENT_RESERVED)
236 continue;
237
238
239 if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
240 continue;
241
242 x = get_unaligned_be16(buf) & 0x0fff;
243 y = get_unaligned_be16(buf + 2) & 0x0fff;
244
245 if (tsdata->version == EV_FT)
246 swap(x, y);
247
248 id = (buf[2] >> 4) & 0x0f;
249
250 input_mt_slot(tsdata->input, id);
251 if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
252 type != TOUCH_EVENT_UP))
253 touchscreen_report_pos(tsdata->input, &tsdata->prop,
254 x, y, true);
255 }
256
257 input_mt_report_pointer_emulation(tsdata->input, true);
258 input_sync(tsdata->input);
259
260out:
261 return IRQ_HANDLED;
262}
263
264static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
265 u8 addr, u8 value)
266{
267 u8 wrbuf[4];
268
269 switch (tsdata->version) {
270 case EDT_M06:
271 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
272 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
273 wrbuf[2] = value;
274 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
275 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
276 wrbuf, 0, NULL);
277
278 case EDT_M09:
279 case EDT_M12:
280 case EV_FT:
281 case GENERIC_FT:
282 wrbuf[0] = addr;
283 wrbuf[1] = value;
284
285 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
286 wrbuf, 0, NULL);
287
288 default:
289 return -EINVAL;
290 }
291}
292
293static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
294 u8 addr)
295{
296 u8 wrbuf[2], rdbuf[2];
297 int error;
298
299 switch (tsdata->version) {
300 case EDT_M06:
301 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
302 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
303 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
304
305 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
306 rdbuf);
307 if (error)
308 return error;
309
310 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
311 dev_err(&tsdata->client->dev,
312 "crc error: 0x%02x expected, got 0x%02x\n",
313 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
314 rdbuf[1]);
315 return -EIO;
316 }
317 break;
318
319
320 case EDT_M09:
321 case EDT_M12:
322 case EV_FT:
323 case GENERIC_FT:
324 wrbuf[0] = addr;
325 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
326 wrbuf, 1, rdbuf);
327 if (error)
328 return error;
329 break;
330
331 default:
332 return -EINVAL;
333 }
334
335 return rdbuf[0];
336}
337
338struct edt_ft5x06_attribute {
339 struct device_attribute dattr;
340 size_t field_offset;
341 u8 limit_low;
342 u8 limit_high;
343 u8 addr_m06;
344 u8 addr_m09;
345 u8 addr_ev;
346};
347
348#define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
349 _limit_low, _limit_high) \
350 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
351 .dattr = __ATTR(_field, _mode, \
352 edt_ft5x06_setting_show, \
353 edt_ft5x06_setting_store), \
354 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
355 .addr_m06 = _addr_m06, \
356 .addr_m09 = _addr_m09, \
357 .addr_ev = _addr_ev, \
358 .limit_low = _limit_low, \
359 .limit_high = _limit_high, \
360 }
361
362static ssize_t edt_ft5x06_setting_show(struct device *dev,
363 struct device_attribute *dattr,
364 char *buf)
365{
366 struct i2c_client *client = to_i2c_client(dev);
367 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
368 struct edt_ft5x06_attribute *attr =
369 container_of(dattr, struct edt_ft5x06_attribute, dattr);
370 u8 *field = (u8 *)tsdata + attr->field_offset;
371 int val;
372 size_t count = 0;
373 int error = 0;
374 u8 addr;
375
376 mutex_lock(&tsdata->mutex);
377
378 if (tsdata->factory_mode) {
379 error = -EIO;
380 goto out;
381 }
382
383 switch (tsdata->version) {
384 case EDT_M06:
385 addr = attr->addr_m06;
386 break;
387
388 case EDT_M09:
389 case EDT_M12:
390 case GENERIC_FT:
391 addr = attr->addr_m09;
392 break;
393
394 case EV_FT:
395 addr = attr->addr_ev;
396 break;
397
398 default:
399 error = -ENODEV;
400 goto out;
401 }
402
403 if (addr != NO_REGISTER) {
404 val = edt_ft5x06_register_read(tsdata, addr);
405 if (val < 0) {
406 error = val;
407 dev_err(&tsdata->client->dev,
408 "Failed to fetch attribute %s, error %d\n",
409 dattr->attr.name, error);
410 goto out;
411 }
412 } else {
413 val = *field;
414 }
415
416 if (val != *field) {
417 dev_warn(&tsdata->client->dev,
418 "%s: read (%d) and stored value (%d) differ\n",
419 dattr->attr.name, val, *field);
420 *field = val;
421 }
422
423 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
424out:
425 mutex_unlock(&tsdata->mutex);
426 return error ?: count;
427}
428
429static ssize_t edt_ft5x06_setting_store(struct device *dev,
430 struct device_attribute *dattr,
431 const char *buf, size_t count)
432{
433 struct i2c_client *client = to_i2c_client(dev);
434 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
435 struct edt_ft5x06_attribute *attr =
436 container_of(dattr, struct edt_ft5x06_attribute, dattr);
437 u8 *field = (u8 *)tsdata + attr->field_offset;
438 unsigned int val;
439 int error;
440 u8 addr;
441
442 mutex_lock(&tsdata->mutex);
443
444 if (tsdata->factory_mode) {
445 error = -EIO;
446 goto out;
447 }
448
449 error = kstrtouint(buf, 0, &val);
450 if (error)
451 goto out;
452
453 if (val < attr->limit_low || val > attr->limit_high) {
454 error = -ERANGE;
455 goto out;
456 }
457
458 switch (tsdata->version) {
459 case EDT_M06:
460 addr = attr->addr_m06;
461 break;
462
463 case EDT_M09:
464 case EDT_M12:
465 case GENERIC_FT:
466 addr = attr->addr_m09;
467 break;
468
469 case EV_FT:
470 addr = attr->addr_ev;
471 break;
472
473 default:
474 error = -ENODEV;
475 goto out;
476 }
477
478 if (addr != NO_REGISTER) {
479 error = edt_ft5x06_register_write(tsdata, addr, val);
480 if (error) {
481 dev_err(&tsdata->client->dev,
482 "Failed to update attribute %s, error: %d\n",
483 dattr->attr.name, error);
484 goto out;
485 }
486 }
487 *field = val;
488
489out:
490 mutex_unlock(&tsdata->mutex);
491 return error ?: count;
492}
493
494
495static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
496 M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31);
497
498static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
499 M09_REGISTER_OFFSET, NO_REGISTER, 0, 31);
500
501static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
502 EV_REGISTER_OFFSET_X, 0, 80);
503
504static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
505 EV_REGISTER_OFFSET_Y, 0, 80);
506
507static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
508 M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255);
509
510static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
511 NO_REGISTER, NO_REGISTER, 0, 255);
512
513static struct attribute *edt_ft5x06_attrs[] = {
514 &edt_ft5x06_attr_gain.dattr.attr,
515 &edt_ft5x06_attr_offset.dattr.attr,
516 &edt_ft5x06_attr_offset_x.dattr.attr,
517 &edt_ft5x06_attr_offset_y.dattr.attr,
518 &edt_ft5x06_attr_threshold.dattr.attr,
519 &edt_ft5x06_attr_report_rate.dattr.attr,
520 NULL
521};
522
523static const struct attribute_group edt_ft5x06_attr_group = {
524 .attrs = edt_ft5x06_attrs,
525};
526
527#ifdef CONFIG_DEBUG_FS
528static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
529{
530 struct i2c_client *client = tsdata->client;
531 int retries = EDT_SWITCH_MODE_RETRIES;
532 int ret;
533 int error;
534
535 if (tsdata->version != EDT_M06) {
536 dev_err(&client->dev,
537 "No factory mode support for non-M06 devices\n");
538 return -EINVAL;
539 }
540
541 disable_irq(client->irq);
542
543 if (!tsdata->raw_buffer) {
544 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
545 sizeof(u16);
546 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
547 if (!tsdata->raw_buffer) {
548 error = -ENOMEM;
549 goto err_out;
550 }
551 }
552
553
554 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
555 if (error) {
556 dev_err(&client->dev,
557 "failed to switch to factory mode, error %d\n", error);
558 goto err_out;
559 }
560
561 tsdata->factory_mode = true;
562 do {
563 mdelay(EDT_SWITCH_MODE_DELAY);
564
565 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
566 if (ret == 0x03)
567 break;
568 } while (--retries > 0);
569
570 if (retries == 0) {
571 dev_err(&client->dev, "not in factory mode after %dms.\n",
572 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
573 error = -EIO;
574 goto err_out;
575 }
576
577 return 0;
578
579err_out:
580 kfree(tsdata->raw_buffer);
581 tsdata->raw_buffer = NULL;
582 tsdata->factory_mode = false;
583 enable_irq(client->irq);
584
585 return error;
586}
587
588static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
589{
590 struct i2c_client *client = tsdata->client;
591 int retries = EDT_SWITCH_MODE_RETRIES;
592 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
593 int ret;
594 int error;
595
596
597 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
598 if (error) {
599 dev_err(&client->dev,
600 "failed to switch to work mode, error: %d\n", error);
601 return error;
602 }
603
604 tsdata->factory_mode = false;
605
606 do {
607 mdelay(EDT_SWITCH_MODE_DELAY);
608
609 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
610 if (ret == 0x01)
611 break;
612 } while (--retries > 0);
613
614 if (retries == 0) {
615 dev_err(&client->dev, "not in work mode after %dms.\n",
616 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
617 tsdata->factory_mode = true;
618 return -EIO;
619 }
620
621 kfree(tsdata->raw_buffer);
622 tsdata->raw_buffer = NULL;
623
624
625 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
626 tsdata->threshold);
627 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
628 tsdata->gain);
629 if (reg_addr->reg_offset != NO_REGISTER)
630 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
631 tsdata->offset);
632 if (reg_addr->reg_offset_x != NO_REGISTER)
633 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x,
634 tsdata->offset_x);
635 if (reg_addr->reg_offset_y != NO_REGISTER)
636 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y,
637 tsdata->offset_y);
638 if (reg_addr->reg_report_rate != NO_REGISTER)
639 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
640 tsdata->report_rate);
641
642 enable_irq(client->irq);
643
644 return 0;
645}
646
647static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
648{
649 struct edt_ft5x06_ts_data *tsdata = data;
650
651 *mode = tsdata->factory_mode;
652
653 return 0;
654};
655
656static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
657{
658 struct edt_ft5x06_ts_data *tsdata = data;
659 int retval = 0;
660
661 if (mode > 1)
662 return -ERANGE;
663
664 mutex_lock(&tsdata->mutex);
665
666 if (mode != tsdata->factory_mode) {
667 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
668 edt_ft5x06_work_mode(tsdata);
669 }
670
671 mutex_unlock(&tsdata->mutex);
672
673 return retval;
674};
675
676DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
677 edt_ft5x06_debugfs_mode_set, "%llu\n");
678
679static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
680 char __user *buf, size_t count, loff_t *off)
681{
682 struct edt_ft5x06_ts_data *tsdata = file->private_data;
683 struct i2c_client *client = tsdata->client;
684 int retries = EDT_RAW_DATA_RETRIES;
685 int val, i, error;
686 size_t read = 0;
687 int colbytes;
688 char wrbuf[3];
689 u8 *rdbuf;
690
691 if (*off < 0 || *off >= tsdata->raw_bufsize)
692 return 0;
693
694 mutex_lock(&tsdata->mutex);
695
696 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
697 error = -EIO;
698 goto out;
699 }
700
701 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
702 if (error) {
703 dev_dbg(&client->dev,
704 "failed to write 0x08 register, error %d\n", error);
705 goto out;
706 }
707
708 do {
709 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
710 val = edt_ft5x06_register_read(tsdata, 0x08);
711 if (val < 1)
712 break;
713 } while (--retries > 0);
714
715 if (val < 0) {
716 error = val;
717 dev_dbg(&client->dev,
718 "failed to read 0x08 register, error %d\n", error);
719 goto out;
720 }
721
722 if (retries == 0) {
723 dev_dbg(&client->dev,
724 "timed out waiting for register to settle\n");
725 error = -ETIMEDOUT;
726 goto out;
727 }
728
729 rdbuf = tsdata->raw_buffer;
730 colbytes = tsdata->num_y * sizeof(u16);
731
732 wrbuf[0] = 0xf5;
733 wrbuf[1] = 0x0e;
734 for (i = 0; i < tsdata->num_x; i++) {
735 wrbuf[2] = i;
736 error = edt_ft5x06_ts_readwrite(tsdata->client,
737 sizeof(wrbuf), wrbuf,
738 colbytes, rdbuf);
739 if (error)
740 goto out;
741
742 rdbuf += colbytes;
743 }
744
745 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
746 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
747 error = -EFAULT;
748 goto out;
749 }
750
751 *off += read;
752out:
753 mutex_unlock(&tsdata->mutex);
754 return error ?: read;
755};
756
757static const struct file_operations debugfs_raw_data_fops = {
758 .open = simple_open,
759 .read = edt_ft5x06_debugfs_raw_data_read,
760};
761
762static void
763edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
764 const char *debugfs_name)
765{
766 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
767 if (!tsdata->debug_dir)
768 return;
769
770 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
771 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
772
773 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
774 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
775 debugfs_create_file("raw_data", S_IRUSR,
776 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
777}
778
779static void
780edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
781{
782 debugfs_remove_recursive(tsdata->debug_dir);
783 kfree(tsdata->raw_buffer);
784}
785
786#else
787
788static inline void
789edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
790 const char *debugfs_name)
791{
792}
793
794static inline void
795edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
796{
797}
798
799#endif
800
801static int edt_ft5x06_ts_identify(struct i2c_client *client,
802 struct edt_ft5x06_ts_data *tsdata,
803 char *fw_version)
804{
805 u8 rdbuf[EDT_NAME_LEN];
806 char *p;
807 int error;
808 char *model_name = tsdata->name;
809
810
811
812
813
814 memset(rdbuf, 0, sizeof(rdbuf));
815 error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
816 EDT_NAME_LEN - 1, rdbuf);
817 if (error)
818 return error;
819
820
821
822
823
824 if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
825 tsdata->version = EDT_M06;
826
827
828 rdbuf[EDT_NAME_LEN - 1] = '\0';
829 if (rdbuf[EDT_NAME_LEN - 2] == '$')
830 rdbuf[EDT_NAME_LEN - 2] = '\0';
831
832
833 p = strchr(rdbuf, '*');
834 if (p)
835 *p++ = '\0';
836 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
837 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
838 } else if (!strncasecmp(rdbuf, "EP0", 3)) {
839 tsdata->version = EDT_M12;
840
841
842 rdbuf[EDT_NAME_LEN - 2] = '\0';
843 if (rdbuf[EDT_NAME_LEN - 3] == '$')
844 rdbuf[EDT_NAME_LEN - 3] = '\0';
845
846
847 p = strchr(rdbuf, '*');
848 if (p)
849 *p++ = '\0';
850 strlcpy(model_name, rdbuf, EDT_NAME_LEN);
851 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
852 } else {
853
854
855
856
857
858
859
860
861
862 tsdata->version = GENERIC_FT;
863
864 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
865 2, rdbuf);
866 if (error)
867 return error;
868
869 strlcpy(fw_version, rdbuf, 2);
870
871 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
872 1, rdbuf);
873 if (error)
874 return error;
875
876
877
878
879
880 switch (rdbuf[0]) {
881 case 0x35:
882 case 0x43:
883 case 0x50:
884 case 0x57:
885 case 0x70:
886 tsdata->version = EDT_M09;
887 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
888 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
889 break;
890 case 0xa1:
891 tsdata->version = EDT_M09;
892 snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
893 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
894 break;
895 case 0x5a:
896 snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
897 break;
898 case 0x59:
899 tsdata->version = EV_FT;
900 error = edt_ft5x06_ts_readwrite(client, 1, "\x53",
901 1, rdbuf);
902 if (error)
903 return error;
904 strlcpy(fw_version, rdbuf, 1);
905 snprintf(model_name, EDT_NAME_LEN,
906 "EVERVISION-FT5726NEi");
907 break;
908 default:
909 snprintf(model_name, EDT_NAME_LEN,
910 "generic ft5x06 (%02x)",
911 rdbuf[0]);
912 break;
913 }
914 }
915
916 return 0;
917}
918
919static void edt_ft5x06_ts_get_defaults(struct device *dev,
920 struct edt_ft5x06_ts_data *tsdata)
921{
922 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
923 u32 val;
924 int error;
925
926 error = device_property_read_u32(dev, "threshold", &val);
927 if (!error) {
928 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
929 tsdata->threshold = val;
930 }
931
932 error = device_property_read_u32(dev, "gain", &val);
933 if (!error) {
934 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
935 tsdata->gain = val;
936 }
937
938 error = device_property_read_u32(dev, "offset", &val);
939 if (!error) {
940 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
941 tsdata->offset = val;
942 }
943
944 error = device_property_read_u32(dev, "offset-x", &val);
945 if (!error) {
946 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x, val);
947 tsdata->offset_x = val;
948 }
949
950 error = device_property_read_u32(dev, "offset-y", &val);
951 if (!error) {
952 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y, val);
953 tsdata->offset_y = val;
954 }
955}
956
957static void
958edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
959{
960 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
961
962 tsdata->threshold = edt_ft5x06_register_read(tsdata,
963 reg_addr->reg_threshold);
964 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
965 if (reg_addr->reg_offset != NO_REGISTER)
966 tsdata->offset =
967 edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
968 if (reg_addr->reg_offset_x != NO_REGISTER)
969 tsdata->offset_x = edt_ft5x06_register_read(tsdata,
970 reg_addr->reg_offset_x);
971 if (reg_addr->reg_offset_y != NO_REGISTER)
972 tsdata->offset_y = edt_ft5x06_register_read(tsdata,
973 reg_addr->reg_offset_y);
974 if (reg_addr->reg_report_rate != NO_REGISTER)
975 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
976 reg_addr->reg_report_rate);
977 if (tsdata->version == EDT_M06 ||
978 tsdata->version == EDT_M09 ||
979 tsdata->version == EDT_M12) {
980 tsdata->num_x = edt_ft5x06_register_read(tsdata,
981 reg_addr->reg_num_x);
982 tsdata->num_y = edt_ft5x06_register_read(tsdata,
983 reg_addr->reg_num_y);
984 } else {
985 tsdata->num_x = -1;
986 tsdata->num_y = -1;
987 }
988}
989
990static void
991edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
992{
993 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
994
995 switch (tsdata->version) {
996 case EDT_M06:
997 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
998 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
999 reg_addr->reg_gain = WORK_REGISTER_GAIN;
1000 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
1001 reg_addr->reg_offset_x = NO_REGISTER;
1002 reg_addr->reg_offset_y = NO_REGISTER;
1003 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
1004 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
1005 break;
1006
1007 case EDT_M09:
1008 case EDT_M12:
1009 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1010 reg_addr->reg_report_rate = NO_REGISTER;
1011 reg_addr->reg_gain = M09_REGISTER_GAIN;
1012 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1013 reg_addr->reg_offset_x = NO_REGISTER;
1014 reg_addr->reg_offset_y = NO_REGISTER;
1015 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
1016 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
1017 break;
1018
1019 case EV_FT:
1020 reg_addr->reg_threshold = EV_REGISTER_THRESHOLD;
1021 reg_addr->reg_gain = EV_REGISTER_GAIN;
1022 reg_addr->reg_offset = NO_REGISTER;
1023 reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1024 reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
1025 reg_addr->reg_num_x = NO_REGISTER;
1026 reg_addr->reg_num_y = NO_REGISTER;
1027 reg_addr->reg_report_rate = NO_REGISTER;
1028 break;
1029
1030 case GENERIC_FT:
1031
1032 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1033 reg_addr->reg_gain = M09_REGISTER_GAIN;
1034 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1035 reg_addr->reg_offset_x = NO_REGISTER;
1036 reg_addr->reg_offset_y = NO_REGISTER;
1037 break;
1038 }
1039}
1040
1041static int edt_ft5x06_ts_probe(struct i2c_client *client,
1042 const struct i2c_device_id *id)
1043{
1044 const struct edt_i2c_chip_data *chip_data;
1045 struct edt_ft5x06_ts_data *tsdata;
1046 struct input_dev *input;
1047 unsigned long irq_flags;
1048 int error;
1049 char fw_version[EDT_NAME_LEN];
1050
1051 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1052
1053 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1054 if (!tsdata) {
1055 dev_err(&client->dev, "failed to allocate driver data.\n");
1056 return -ENOMEM;
1057 }
1058
1059 chip_data = device_get_match_data(&client->dev);
1060 if (!chip_data)
1061 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1062 if (!chip_data || !chip_data->max_support_points) {
1063 dev_err(&client->dev, "invalid or missing chip data\n");
1064 return -EINVAL;
1065 }
1066
1067 tsdata->max_support_points = chip_data->max_support_points;
1068
1069 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1070 "reset", GPIOD_OUT_HIGH);
1071 if (IS_ERR(tsdata->reset_gpio)) {
1072 error = PTR_ERR(tsdata->reset_gpio);
1073 dev_err(&client->dev,
1074 "Failed to request GPIO reset pin, error %d\n", error);
1075 return error;
1076 }
1077
1078 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1079 "wake", GPIOD_OUT_LOW);
1080 if (IS_ERR(tsdata->wake_gpio)) {
1081 error = PTR_ERR(tsdata->wake_gpio);
1082 dev_err(&client->dev,
1083 "Failed to request GPIO wake pin, error %d\n", error);
1084 return error;
1085 }
1086
1087 if (tsdata->wake_gpio) {
1088 usleep_range(5000, 6000);
1089 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1090 }
1091
1092 if (tsdata->reset_gpio) {
1093 usleep_range(5000, 6000);
1094 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1095 msleep(300);
1096 }
1097
1098 input = devm_input_allocate_device(&client->dev);
1099 if (!input) {
1100 dev_err(&client->dev, "failed to allocate input device.\n");
1101 return -ENOMEM;
1102 }
1103
1104 mutex_init(&tsdata->mutex);
1105 tsdata->client = client;
1106 tsdata->input = input;
1107 tsdata->factory_mode = false;
1108
1109 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
1110 if (error) {
1111 dev_err(&client->dev, "touchscreen probe failed\n");
1112 return error;
1113 }
1114
1115 edt_ft5x06_ts_set_regs(tsdata);
1116 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1117 edt_ft5x06_ts_get_parameters(tsdata);
1118
1119 dev_dbg(&client->dev,
1120 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1121 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1122
1123 input->name = tsdata->name;
1124 input->id.bustype = BUS_I2C;
1125 input->dev.parent = &client->dev;
1126
1127 if (tsdata->version == EDT_M06 ||
1128 tsdata->version == EDT_M09 ||
1129 tsdata->version == EDT_M12) {
1130 input_set_abs_params(input, ABS_MT_POSITION_X,
1131 0, tsdata->num_x * 64 - 1, 0, 0);
1132 input_set_abs_params(input, ABS_MT_POSITION_Y,
1133 0, tsdata->num_y * 64 - 1, 0, 0);
1134 } else {
1135
1136 input_set_abs_params(input, ABS_MT_POSITION_X,
1137 0, 65535, 0, 0);
1138 input_set_abs_params(input, ABS_MT_POSITION_Y,
1139 0, 65535, 0, 0);
1140 }
1141
1142 touchscreen_parse_properties(input, true, &tsdata->prop);
1143
1144 error = input_mt_init_slots(input, tsdata->max_support_points,
1145 INPUT_MT_DIRECT);
1146 if (error) {
1147 dev_err(&client->dev, "Unable to init MT slots.\n");
1148 return error;
1149 }
1150
1151 i2c_set_clientdata(client, tsdata);
1152
1153 irq_flags = irq_get_trigger_type(client->irq);
1154 if (irq_flags == IRQF_TRIGGER_NONE)
1155 irq_flags = IRQF_TRIGGER_FALLING;
1156 irq_flags |= IRQF_ONESHOT;
1157
1158 error = devm_request_threaded_irq(&client->dev, client->irq,
1159 NULL, edt_ft5x06_ts_isr, irq_flags,
1160 client->name, tsdata);
1161 if (error) {
1162 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1163 return error;
1164 }
1165
1166 error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
1167 if (error)
1168 return error;
1169
1170 error = input_register_device(input);
1171 if (error)
1172 return error;
1173
1174 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1175 device_init_wakeup(&client->dev, 1);
1176
1177 dev_dbg(&client->dev,
1178 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1179 client->irq,
1180 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1181 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1182
1183 return 0;
1184}
1185
1186static int edt_ft5x06_ts_remove(struct i2c_client *client)
1187{
1188 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1189
1190 edt_ft5x06_ts_teardown_debugfs(tsdata);
1191
1192 return 0;
1193}
1194
1195static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1196{
1197 struct i2c_client *client = to_i2c_client(dev);
1198
1199 if (device_may_wakeup(dev))
1200 enable_irq_wake(client->irq);
1201
1202 return 0;
1203}
1204
1205static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1206{
1207 struct i2c_client *client = to_i2c_client(dev);
1208
1209 if (device_may_wakeup(dev))
1210 disable_irq_wake(client->irq);
1211
1212 return 0;
1213}
1214
1215static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1216 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1217
1218static const struct edt_i2c_chip_data edt_ft5x06_data = {
1219 .max_support_points = 5,
1220};
1221
1222static const struct edt_i2c_chip_data edt_ft5506_data = {
1223 .max_support_points = 10,
1224};
1225
1226static const struct edt_i2c_chip_data edt_ft6236_data = {
1227 .max_support_points = 2,
1228};
1229
1230static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1231 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1232 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1233 { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
1234
1235 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1236 { }
1237};
1238MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1239
1240static const struct of_device_id edt_ft5x06_of_match[] = {
1241 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1242 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1243 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1244 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1245 { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
1246
1247 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1248 { }
1249};
1250MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1251
1252static struct i2c_driver edt_ft5x06_ts_driver = {
1253 .driver = {
1254 .name = "edt_ft5x06",
1255 .of_match_table = edt_ft5x06_of_match,
1256 .pm = &edt_ft5x06_ts_pm_ops,
1257 },
1258 .id_table = edt_ft5x06_ts_id,
1259 .probe = edt_ft5x06_ts_probe,
1260 .remove = edt_ft5x06_ts_remove,
1261};
1262
1263module_i2c_driver(edt_ft5x06_ts_driver);
1264
1265MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1266MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1267MODULE_LICENSE("GPL v2");
1268