1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
4 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
5 * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
9 * This is a driver for the EDT "Polytouch" family of touch controllers
10 * based on the FocalTech FT5x06 line of chips.
12 * Development of this driver has been sponsored by Glyn:
13 * http://www.glyn.com/Products/Displays
16 #include <linux/debugfs.h>
17 #include <linux/delay.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/input.h>
22 #include <linux/input/mt.h>
23 #include <linux/input/touchscreen.h>
24 #include <linux/irq.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/ratelimit.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
32 #include <asm/unaligned.h>
34 #define WORK_REGISTER_THRESHOLD 0x00
35 #define WORK_REGISTER_REPORT_RATE 0x08
36 #define WORK_REGISTER_GAIN 0x30
37 #define WORK_REGISTER_OFFSET 0x31
38 #define WORK_REGISTER_NUM_X 0x33
39 #define WORK_REGISTER_NUM_Y 0x34
41 #define PMOD_REGISTER_ACTIVE 0x00
42 #define PMOD_REGISTER_HIBERNATE 0x03
44 #define M09_REGISTER_THRESHOLD 0x80
45 #define M09_REGISTER_GAIN 0x92
46 #define M09_REGISTER_OFFSET 0x93
47 #define M09_REGISTER_NUM_X 0x94
48 #define M09_REGISTER_NUM_Y 0x95
50 #define EV_REGISTER_THRESHOLD 0x40
51 #define EV_REGISTER_GAIN 0x41
52 #define EV_REGISTER_OFFSET_Y 0x45
53 #define EV_REGISTER_OFFSET_X 0x46
55 #define NO_REGISTER 0xff
57 #define WORK_REGISTER_OPMODE 0x3c
58 #define FACTORY_REGISTER_OPMODE 0x01
59 #define PMOD_REGISTER_OPMODE 0xa5
61 #define TOUCH_EVENT_DOWN 0x00
62 #define TOUCH_EVENT_UP 0x01
63 #define TOUCH_EVENT_ON 0x02
64 #define TOUCH_EVENT_RESERVED 0x03
66 #define EDT_NAME_LEN 23
67 #define EDT_SWITCH_MODE_RETRIES 10
68 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
69 #define EDT_RAW_DATA_RETRIES 100
70 #define EDT_RAW_DATA_DELAY 1000 /* usec */
72 #define EDT_DEFAULT_NUM_X 1024
73 #define EDT_DEFAULT_NUM_Y 1024
76 EDT_PMODE_NOT_SUPPORTED
,
100 struct edt_ft5x06_ts_data
{
101 struct i2c_client
*client
;
102 struct input_dev
*input
;
103 struct touchscreen_properties prop
;
106 struct regulator
*vcc
;
108 struct gpio_desc
*reset_gpio
;
109 struct gpio_desc
*wake_gpio
;
111 #if defined(CONFIG_DEBUG_FS)
112 struct dentry
*debug_dir
;
119 enum edt_pmode suspend_mode
;
126 int max_support_points
;
128 char name
[EDT_NAME_LEN
];
130 struct edt_reg_addr reg_addr
;
131 enum edt_ver version
;
134 struct edt_i2c_chip_data
{
135 int max_support_points
;
138 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
139 u16 wr_len
, u8
*wr_buf
,
140 u16 rd_len
, u8
*rd_buf
)
142 struct i2c_msg wrmsg
[2];
147 wrmsg
[i
].addr
= client
->addr
;
149 wrmsg
[i
].len
= wr_len
;
150 wrmsg
[i
].buf
= wr_buf
;
154 wrmsg
[i
].addr
= client
->addr
;
155 wrmsg
[i
].flags
= I2C_M_RD
;
156 wrmsg
[i
].len
= rd_len
;
157 wrmsg
[i
].buf
= rd_buf
;
161 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
170 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
176 for (i
= 0; i
< buflen
- 1; i
++)
179 if (crc
!= buf
[buflen
-1]) {
180 dev_err_ratelimited(&tsdata
->client
->dev
,
181 "crc error: 0x%02x expected, got 0x%02x\n",
189 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
191 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
192 struct device
*dev
= &tsdata
->client
->dev
;
195 int i
, type
, x
, y
, id
;
196 int offset
, tplen
, datalen
, crclen
;
199 switch (tsdata
->version
) {
201 cmd
= 0xf9; /* tell the controller to send touch data */
202 offset
= 5; /* where the actual touch data starts */
203 tplen
= 4; /* data comes in so called frames */
204 crclen
= 1; /* length of the crc data */
221 memset(rdbuf
, 0, sizeof(rdbuf
));
222 datalen
= tplen
* tsdata
->max_support_points
+ offset
+ crclen
;
224 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
228 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
233 /* M09/M12 does not send header or CRC */
234 if (tsdata
->version
== EDT_M06
) {
235 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
236 rdbuf
[2] != datalen
) {
237 dev_err_ratelimited(dev
,
238 "Unexpected header: %02x%02x%02x!\n",
239 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
243 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
247 for (i
= 0; i
< tsdata
->max_support_points
; i
++) {
248 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
251 /* ignore Reserved events */
252 if (type
== TOUCH_EVENT_RESERVED
)
255 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
256 if (tsdata
->version
== EDT_M06
&& type
== TOUCH_EVENT_DOWN
)
259 x
= get_unaligned_be16(buf
) & 0x0fff;
260 y
= get_unaligned_be16(buf
+ 2) & 0x0fff;
261 /* The FT5x26 send the y coordinate first */
262 if (tsdata
->version
== EV_FT
)
265 id
= (buf
[2] >> 4) & 0x0f;
267 input_mt_slot(tsdata
->input
, id
);
268 if (input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
,
269 type
!= TOUCH_EVENT_UP
))
270 touchscreen_report_pos(tsdata
->input
, &tsdata
->prop
,
274 input_mt_report_pointer_emulation(tsdata
->input
, true);
275 input_sync(tsdata
->input
);
281 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data
*tsdata
,
286 switch (tsdata
->version
) {
288 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
289 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
291 wrbuf
[3] = wrbuf
[0] ^ wrbuf
[1] ^ wrbuf
[2];
292 return edt_ft5x06_ts_readwrite(tsdata
->client
, 4,
302 return edt_ft5x06_ts_readwrite(tsdata
->client
, 2,
310 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data
*tsdata
,
313 u8 wrbuf
[2], rdbuf
[2];
316 switch (tsdata
->version
) {
318 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
319 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
320 wrbuf
[1] |= tsdata
->factory_mode
? 0x80 : 0x40;
322 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 2, wrbuf
, 2,
327 if ((wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0]) != rdbuf
[1]) {
328 dev_err(&tsdata
->client
->dev
,
329 "crc error: 0x%02x expected, got 0x%02x\n",
330 wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0],
341 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
354 struct edt_ft5x06_attribute
{
355 struct device_attribute dattr
;
364 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
365 _limit_low, _limit_high) \
366 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
367 .dattr = __ATTR(_field, _mode, \
368 edt_ft5x06_setting_show, \
369 edt_ft5x06_setting_store), \
370 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
371 .addr_m06 = _addr_m06, \
372 .addr_m09 = _addr_m09, \
373 .addr_ev = _addr_ev, \
374 .limit_low = _limit_low, \
375 .limit_high = _limit_high, \
378 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
379 struct device_attribute
*dattr
,
382 struct i2c_client
*client
= to_i2c_client(dev
);
383 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
384 struct edt_ft5x06_attribute
*attr
=
385 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
386 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
392 mutex_lock(&tsdata
->mutex
);
394 if (tsdata
->factory_mode
) {
399 switch (tsdata
->version
) {
401 addr
= attr
->addr_m06
;
407 addr
= attr
->addr_m09
;
411 addr
= attr
->addr_ev
;
419 if (addr
!= NO_REGISTER
) {
420 val
= edt_ft5x06_register_read(tsdata
, addr
);
423 dev_err(&tsdata
->client
->dev
,
424 "Failed to fetch attribute %s, error %d\n",
425 dattr
->attr
.name
, error
);
433 dev_warn(&tsdata
->client
->dev
,
434 "%s: read (%d) and stored value (%d) differ\n",
435 dattr
->attr
.name
, val
, *field
);
439 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
441 mutex_unlock(&tsdata
->mutex
);
442 return error
?: count
;
445 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
446 struct device_attribute
*dattr
,
447 const char *buf
, size_t count
)
449 struct i2c_client
*client
= to_i2c_client(dev
);
450 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
451 struct edt_ft5x06_attribute
*attr
=
452 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
453 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
458 mutex_lock(&tsdata
->mutex
);
460 if (tsdata
->factory_mode
) {
465 error
= kstrtouint(buf
, 0, &val
);
469 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
474 switch (tsdata
->version
) {
476 addr
= attr
->addr_m06
;
482 addr
= attr
->addr_m09
;
486 addr
= attr
->addr_ev
;
494 if (addr
!= NO_REGISTER
) {
495 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
497 dev_err(&tsdata
->client
->dev
,
498 "Failed to update attribute %s, error: %d\n",
499 dattr
->attr
.name
, error
);
506 mutex_unlock(&tsdata
->mutex
);
507 return error
?: count
;
510 /* m06, m09: range 0-31, m12: range 0-5 */
511 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
512 M09_REGISTER_GAIN
, EV_REGISTER_GAIN
, 0, 31);
513 /* m06, m09: range 0-31, m12: range 0-16 */
514 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
515 M09_REGISTER_OFFSET
, NO_REGISTER
, 0, 31);
516 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
517 static EDT_ATTR(offset_x
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
518 EV_REGISTER_OFFSET_X
, 0, 80);
519 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
520 static EDT_ATTR(offset_y
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
521 EV_REGISTER_OFFSET_Y
, 0, 80);
522 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
523 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
524 M09_REGISTER_THRESHOLD
, EV_REGISTER_THRESHOLD
, 0, 255);
525 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
526 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
527 NO_REGISTER
, NO_REGISTER
, 0, 255);
529 static struct attribute
*edt_ft5x06_attrs
[] = {
530 &edt_ft5x06_attr_gain
.dattr
.attr
,
531 &edt_ft5x06_attr_offset
.dattr
.attr
,
532 &edt_ft5x06_attr_offset_x
.dattr
.attr
,
533 &edt_ft5x06_attr_offset_y
.dattr
.attr
,
534 &edt_ft5x06_attr_threshold
.dattr
.attr
,
535 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
539 static const struct attribute_group edt_ft5x06_attr_group
= {
540 .attrs
= edt_ft5x06_attrs
,
543 static void edt_ft5x06_restore_reg_parameters(struct edt_ft5x06_ts_data
*tsdata
)
545 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
547 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
549 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
551 if (reg_addr
->reg_offset
!= NO_REGISTER
)
552 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
554 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
555 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_x
,
557 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
558 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_y
,
560 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
561 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
562 tsdata
->report_rate
);
566 #ifdef CONFIG_DEBUG_FS
567 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
569 struct i2c_client
*client
= tsdata
->client
;
570 int retries
= EDT_SWITCH_MODE_RETRIES
;
574 if (tsdata
->version
!= EDT_M06
) {
575 dev_err(&client
->dev
,
576 "No factory mode support for non-M06 devices\n");
580 disable_irq(client
->irq
);
582 if (!tsdata
->raw_buffer
) {
583 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
585 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
586 if (!tsdata
->raw_buffer
) {
592 /* mode register is 0x3c when in the work mode */
593 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
595 dev_err(&client
->dev
,
596 "failed to switch to factory mode, error %d\n", error
);
600 tsdata
->factory_mode
= true;
602 mdelay(EDT_SWITCH_MODE_DELAY
);
603 /* mode register is 0x01 when in factory mode */
604 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
607 } while (--retries
> 0);
610 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
611 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
619 kfree(tsdata
->raw_buffer
);
620 tsdata
->raw_buffer
= NULL
;
621 tsdata
->factory_mode
= false;
622 enable_irq(client
->irq
);
627 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
629 struct i2c_client
*client
= tsdata
->client
;
630 int retries
= EDT_SWITCH_MODE_RETRIES
;
634 /* mode register is 0x01 when in the factory mode */
635 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
637 dev_err(&client
->dev
,
638 "failed to switch to work mode, error: %d\n", error
);
642 tsdata
->factory_mode
= false;
645 mdelay(EDT_SWITCH_MODE_DELAY
);
646 /* mode register is 0x01 when in factory mode */
647 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
650 } while (--retries
> 0);
653 dev_err(&client
->dev
, "not in work mode after %dms.\n",
654 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
655 tsdata
->factory_mode
= true;
659 kfree(tsdata
->raw_buffer
);
660 tsdata
->raw_buffer
= NULL
;
662 edt_ft5x06_restore_reg_parameters(tsdata
);
663 enable_irq(client
->irq
);
668 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
670 struct edt_ft5x06_ts_data
*tsdata
= data
;
672 *mode
= tsdata
->factory_mode
;
677 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
679 struct edt_ft5x06_ts_data
*tsdata
= data
;
685 mutex_lock(&tsdata
->mutex
);
687 if (mode
!= tsdata
->factory_mode
) {
688 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
689 edt_ft5x06_work_mode(tsdata
);
692 mutex_unlock(&tsdata
->mutex
);
697 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
698 edt_ft5x06_debugfs_mode_set
, "%llu\n");
700 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
701 char __user
*buf
, size_t count
, loff_t
*off
)
703 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
704 struct i2c_client
*client
= tsdata
->client
;
705 int retries
= EDT_RAW_DATA_RETRIES
;
712 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
715 mutex_lock(&tsdata
->mutex
);
717 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
722 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
724 dev_dbg(&client
->dev
,
725 "failed to write 0x08 register, error %d\n", error
);
730 usleep_range(EDT_RAW_DATA_DELAY
, EDT_RAW_DATA_DELAY
+ 100);
731 val
= edt_ft5x06_register_read(tsdata
, 0x08);
734 } while (--retries
> 0);
738 dev_dbg(&client
->dev
,
739 "failed to read 0x08 register, error %d\n", error
);
744 dev_dbg(&client
->dev
,
745 "timed out waiting for register to settle\n");
750 rdbuf
= tsdata
->raw_buffer
;
751 colbytes
= tsdata
->num_y
* sizeof(u16
);
755 for (i
= 0; i
< tsdata
->num_x
; i
++) {
756 wrbuf
[2] = i
; /* column index */
757 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
758 sizeof(wrbuf
), wrbuf
,
766 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
767 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
774 mutex_unlock(&tsdata
->mutex
);
775 return error
?: read
;
778 static const struct file_operations debugfs_raw_data_fops
= {
780 .read
= edt_ft5x06_debugfs_raw_data_read
,
783 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
784 const char *debugfs_name
)
786 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
788 debugfs_create_u16("num_x", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_x
);
789 debugfs_create_u16("num_y", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_y
);
791 debugfs_create_file("mode", S_IRUSR
| S_IWUSR
,
792 tsdata
->debug_dir
, tsdata
, &debugfs_mode_fops
);
793 debugfs_create_file("raw_data", S_IRUSR
,
794 tsdata
->debug_dir
, tsdata
, &debugfs_raw_data_fops
);
797 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
799 debugfs_remove_recursive(tsdata
->debug_dir
);
800 kfree(tsdata
->raw_buffer
);
805 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
810 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
811 const char *debugfs_name
)
815 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
819 #endif /* CONFIG_DEBUGFS */
821 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
822 struct edt_ft5x06_ts_data
*tsdata
,
825 u8 rdbuf
[EDT_NAME_LEN
];
828 char *model_name
= tsdata
->name
;
830 /* see what we find if we assume it is a M06 *
831 * if we get less than EDT_NAME_LEN, we don't want
832 * to have garbage in there
834 memset(rdbuf
, 0, sizeof(rdbuf
));
835 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xBB",
836 EDT_NAME_LEN
- 1, rdbuf
);
840 /* Probe content for something consistent.
841 * M06 starts with a response byte, M12 gives the data directly.
842 * M09/Generic does not provide model number information.
844 if (!strncasecmp(rdbuf
+ 1, "EP0", 3)) {
845 tsdata
->version
= EDT_M06
;
847 /* remove last '$' end marker */
848 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
849 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
850 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
852 /* look for Model/Version separator */
853 p
= strchr(rdbuf
, '*');
856 strlcpy(model_name
, rdbuf
+ 1, EDT_NAME_LEN
);
857 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
858 } else if (!strncasecmp(rdbuf
, "EP0", 3)) {
859 tsdata
->version
= EDT_M12
;
861 /* remove last '$' end marker */
862 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
863 if (rdbuf
[EDT_NAME_LEN
- 3] == '$')
864 rdbuf
[EDT_NAME_LEN
- 3] = '\0';
866 /* look for Model/Version separator */
867 p
= strchr(rdbuf
, '*');
870 strlcpy(model_name
, rdbuf
, EDT_NAME_LEN
);
871 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
873 /* If it is not an EDT M06/M12 touchscreen, then the model
874 * detection is a bit hairy. The different ft5x06
875 * firmares around don't reliably implement the
876 * identification registers. Well, we'll take a shot.
878 * The main difference between generic focaltec based
879 * touches and EDT M09 is that we know how to retrieve
880 * the max coordinates for the latter.
882 tsdata
->version
= GENERIC_FT
;
884 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
889 strlcpy(fw_version
, rdbuf
, 2);
891 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
896 /* This "model identification" is not exact. Unfortunately
897 * not all firmwares for the ft5x06 put useful values in
898 * the identification registers.
901 case 0x35: /* EDT EP0350M09 */
902 case 0x43: /* EDT EP0430M09 */
903 case 0x50: /* EDT EP0500M09 */
904 case 0x57: /* EDT EP0570M09 */
905 case 0x70: /* EDT EP0700M09 */
906 tsdata
->version
= EDT_M09
;
907 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
908 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
910 case 0xa1: /* EDT EP1010ML00 */
911 tsdata
->version
= EDT_M09
;
912 snprintf(model_name
, EDT_NAME_LEN
, "EP%i%i0ML00",
913 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
915 case 0x5a: /* Solomon Goldentek Display */
916 snprintf(model_name
, EDT_NAME_LEN
, "GKTW50SCED1R0");
918 case 0x59: /* Evervision Display with FT5xx6 TS */
919 tsdata
->version
= EV_FT
;
920 error
= edt_ft5x06_ts_readwrite(client
, 1, "\x53",
924 strlcpy(fw_version
, rdbuf
, 1);
925 snprintf(model_name
, EDT_NAME_LEN
,
926 "EVERVISION-FT5726NEi");
929 snprintf(model_name
, EDT_NAME_LEN
,
930 "generic ft5x06 (%02x)",
939 static void edt_ft5x06_ts_get_defaults(struct device
*dev
,
940 struct edt_ft5x06_ts_data
*tsdata
)
942 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
946 error
= device_property_read_u32(dev
, "threshold", &val
);
948 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
, val
);
949 tsdata
->threshold
= val
;
952 error
= device_property_read_u32(dev
, "gain", &val
);
954 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
, val
);
958 error
= device_property_read_u32(dev
, "offset", &val
);
960 if (reg_addr
->reg_offset
!= NO_REGISTER
)
961 edt_ft5x06_register_write(tsdata
,
962 reg_addr
->reg_offset
, val
);
963 tsdata
->offset
= val
;
966 error
= device_property_read_u32(dev
, "offset-x", &val
);
968 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
969 edt_ft5x06_register_write(tsdata
,
970 reg_addr
->reg_offset_x
, val
);
971 tsdata
->offset_x
= val
;
974 error
= device_property_read_u32(dev
, "offset-y", &val
);
976 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
977 edt_ft5x06_register_write(tsdata
,
978 reg_addr
->reg_offset_y
, val
);
979 tsdata
->offset_y
= val
;
983 static void edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
985 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
987 tsdata
->threshold
= edt_ft5x06_register_read(tsdata
,
988 reg_addr
->reg_threshold
);
989 tsdata
->gain
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_gain
);
990 if (reg_addr
->reg_offset
!= NO_REGISTER
)
992 edt_ft5x06_register_read(tsdata
, reg_addr
->reg_offset
);
993 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
994 tsdata
->offset_x
= edt_ft5x06_register_read(tsdata
,
995 reg_addr
->reg_offset_x
);
996 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
997 tsdata
->offset_y
= edt_ft5x06_register_read(tsdata
,
998 reg_addr
->reg_offset_y
);
999 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
1000 tsdata
->report_rate
= edt_ft5x06_register_read(tsdata
,
1001 reg_addr
->reg_report_rate
);
1002 tsdata
->num_x
= EDT_DEFAULT_NUM_X
;
1003 if (reg_addr
->reg_num_x
!= NO_REGISTER
)
1004 tsdata
->num_x
= edt_ft5x06_register_read(tsdata
,
1005 reg_addr
->reg_num_x
);
1006 tsdata
->num_y
= EDT_DEFAULT_NUM_Y
;
1007 if (reg_addr
->reg_num_y
!= NO_REGISTER
)
1008 tsdata
->num_y
= edt_ft5x06_register_read(tsdata
,
1009 reg_addr
->reg_num_y
);
1012 static void edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
1014 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
1016 switch (tsdata
->version
) {
1018 reg_addr
->reg_threshold
= WORK_REGISTER_THRESHOLD
;
1019 reg_addr
->reg_report_rate
= WORK_REGISTER_REPORT_RATE
;
1020 reg_addr
->reg_gain
= WORK_REGISTER_GAIN
;
1021 reg_addr
->reg_offset
= WORK_REGISTER_OFFSET
;
1022 reg_addr
->reg_offset_x
= NO_REGISTER
;
1023 reg_addr
->reg_offset_y
= NO_REGISTER
;
1024 reg_addr
->reg_num_x
= WORK_REGISTER_NUM_X
;
1025 reg_addr
->reg_num_y
= WORK_REGISTER_NUM_Y
;
1030 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
1031 reg_addr
->reg_report_rate
= NO_REGISTER
;
1032 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
1033 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
1034 reg_addr
->reg_offset_x
= NO_REGISTER
;
1035 reg_addr
->reg_offset_y
= NO_REGISTER
;
1036 reg_addr
->reg_num_x
= M09_REGISTER_NUM_X
;
1037 reg_addr
->reg_num_y
= M09_REGISTER_NUM_Y
;
1041 reg_addr
->reg_threshold
= EV_REGISTER_THRESHOLD
;
1042 reg_addr
->reg_report_rate
= NO_REGISTER
;
1043 reg_addr
->reg_gain
= EV_REGISTER_GAIN
;
1044 reg_addr
->reg_offset
= NO_REGISTER
;
1045 reg_addr
->reg_offset_x
= EV_REGISTER_OFFSET_X
;
1046 reg_addr
->reg_offset_y
= EV_REGISTER_OFFSET_Y
;
1047 reg_addr
->reg_num_x
= NO_REGISTER
;
1048 reg_addr
->reg_num_y
= NO_REGISTER
;
1052 /* this is a guesswork */
1053 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
1054 reg_addr
->reg_report_rate
= NO_REGISTER
;
1055 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
1056 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
1057 reg_addr
->reg_offset_x
= NO_REGISTER
;
1058 reg_addr
->reg_offset_y
= NO_REGISTER
;
1059 reg_addr
->reg_num_x
= NO_REGISTER
;
1060 reg_addr
->reg_num_y
= NO_REGISTER
;
1065 static void edt_ft5x06_disable_regulator(void *arg
)
1067 struct edt_ft5x06_ts_data
*data
= arg
;
1069 regulator_disable(data
->vcc
);
1072 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
1073 const struct i2c_device_id
*id
)
1075 const struct edt_i2c_chip_data
*chip_data
;
1076 struct edt_ft5x06_ts_data
*tsdata
;
1077 u8 buf
[2] = { 0xfc, 0x00 };
1078 struct input_dev
*input
;
1079 unsigned long irq_flags
;
1081 char fw_version
[EDT_NAME_LEN
];
1083 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
1085 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
1087 dev_err(&client
->dev
, "failed to allocate driver data.\n");
1091 chip_data
= device_get_match_data(&client
->dev
);
1093 chip_data
= (const struct edt_i2c_chip_data
*)id
->driver_data
;
1094 if (!chip_data
|| !chip_data
->max_support_points
) {
1095 dev_err(&client
->dev
, "invalid or missing chip data\n");
1099 tsdata
->max_support_points
= chip_data
->max_support_points
;
1101 tsdata
->vcc
= devm_regulator_get(&client
->dev
, "vcc");
1102 if (IS_ERR(tsdata
->vcc
)) {
1103 error
= PTR_ERR(tsdata
->vcc
);
1104 if (error
!= -EPROBE_DEFER
)
1105 dev_err(&client
->dev
,
1106 "failed to request regulator: %d\n", error
);
1110 error
= regulator_enable(tsdata
->vcc
);
1112 dev_err(&client
->dev
, "failed to enable vcc: %d\n", error
);
1116 error
= devm_add_action_or_reset(&client
->dev
,
1117 edt_ft5x06_disable_regulator
,
1122 tsdata
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
1123 "reset", GPIOD_OUT_HIGH
);
1124 if (IS_ERR(tsdata
->reset_gpio
)) {
1125 error
= PTR_ERR(tsdata
->reset_gpio
);
1126 dev_err(&client
->dev
,
1127 "Failed to request GPIO reset pin, error %d\n", error
);
1131 tsdata
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
1132 "wake", GPIOD_OUT_LOW
);
1133 if (IS_ERR(tsdata
->wake_gpio
)) {
1134 error
= PTR_ERR(tsdata
->wake_gpio
);
1135 dev_err(&client
->dev
,
1136 "Failed to request GPIO wake pin, error %d\n", error
);
1141 * Check which sleep modes we can support. Power-off requieres the
1142 * reset-pin to ensure correct power-down/power-up behaviour. Start with
1143 * the EDT_PMODE_POWEROFF test since this is the deepest possible sleep
1146 if (tsdata
->reset_gpio
)
1147 tsdata
->suspend_mode
= EDT_PMODE_POWEROFF
;
1148 else if (tsdata
->wake_gpio
)
1149 tsdata
->suspend_mode
= EDT_PMODE_HIBERNATE
;
1151 tsdata
->suspend_mode
= EDT_PMODE_NOT_SUPPORTED
;
1153 if (tsdata
->wake_gpio
) {
1154 usleep_range(5000, 6000);
1155 gpiod_set_value_cansleep(tsdata
->wake_gpio
, 1);
1158 if (tsdata
->reset_gpio
) {
1159 usleep_range(5000, 6000);
1160 gpiod_set_value_cansleep(tsdata
->reset_gpio
, 0);
1164 input
= devm_input_allocate_device(&client
->dev
);
1166 dev_err(&client
->dev
, "failed to allocate input device.\n");
1170 mutex_init(&tsdata
->mutex
);
1171 tsdata
->client
= client
;
1172 tsdata
->input
= input
;
1173 tsdata
->factory_mode
= false;
1175 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
1177 dev_err(&client
->dev
, "touchscreen probe failed\n");
1182 * Dummy read access. EP0700MLP1 returns bogus data on the first
1183 * register read access and ignores writes.
1185 edt_ft5x06_ts_readwrite(tsdata
->client
, 2, buf
, 2, buf
);
1187 edt_ft5x06_ts_set_regs(tsdata
);
1188 edt_ft5x06_ts_get_defaults(&client
->dev
, tsdata
);
1189 edt_ft5x06_ts_get_parameters(tsdata
);
1191 dev_dbg(&client
->dev
,
1192 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1193 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
1195 input
->name
= tsdata
->name
;
1196 input
->id
.bustype
= BUS_I2C
;
1197 input
->dev
.parent
= &client
->dev
;
1199 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1200 0, tsdata
->num_x
* 64 - 1, 0, 0);
1201 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1202 0, tsdata
->num_y
* 64 - 1, 0, 0);
1204 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
1206 error
= input_mt_init_slots(input
, tsdata
->max_support_points
,
1209 dev_err(&client
->dev
, "Unable to init MT slots.\n");
1213 i2c_set_clientdata(client
, tsdata
);
1215 irq_flags
= irq_get_trigger_type(client
->irq
);
1216 if (irq_flags
== IRQF_TRIGGER_NONE
)
1217 irq_flags
= IRQF_TRIGGER_FALLING
;
1218 irq_flags
|= IRQF_ONESHOT
;
1220 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1221 NULL
, edt_ft5x06_ts_isr
, irq_flags
,
1222 client
->name
, tsdata
);
1224 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
1228 error
= devm_device_add_group(&client
->dev
, &edt_ft5x06_attr_group
);
1232 error
= input_register_device(input
);
1236 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1238 dev_dbg(&client
->dev
,
1239 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1241 tsdata
->wake_gpio
? desc_to_gpio(tsdata
->wake_gpio
) : -1,
1242 tsdata
->reset_gpio
? desc_to_gpio(tsdata
->reset_gpio
) : -1);
1247 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1249 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1251 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1256 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1258 struct i2c_client
*client
= to_i2c_client(dev
);
1259 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1260 struct gpio_desc
*reset_gpio
= tsdata
->reset_gpio
;
1263 if (device_may_wakeup(dev
))
1266 if (tsdata
->suspend_mode
== EDT_PMODE_NOT_SUPPORTED
)
1269 /* Enter hibernate mode. */
1270 ret
= edt_ft5x06_register_write(tsdata
, PMOD_REGISTER_OPMODE
,
1271 PMOD_REGISTER_HIBERNATE
);
1273 dev_warn(dev
, "Failed to set hibernate mode\n");
1275 if (tsdata
->suspend_mode
== EDT_PMODE_HIBERNATE
)
1279 * Power-off according the datasheet. Cut the power may leaf the irq
1280 * line in an undefined state depending on the host pull resistor
1281 * settings. Disable the irq to avoid adjusting each host till the
1282 * device is back in a full functional state.
1284 disable_irq(tsdata
->client
->irq
);
1286 gpiod_set_value_cansleep(reset_gpio
, 1);
1287 usleep_range(1000, 2000);
1289 ret
= regulator_disable(tsdata
->vcc
);
1291 dev_warn(dev
, "Failed to disable vcc\n");
1296 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1298 struct i2c_client
*client
= to_i2c_client(dev
);
1299 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1302 if (device_may_wakeup(dev
))
1305 if (tsdata
->suspend_mode
== EDT_PMODE_NOT_SUPPORTED
)
1308 if (tsdata
->suspend_mode
== EDT_PMODE_POWEROFF
) {
1309 struct gpio_desc
*reset_gpio
= tsdata
->reset_gpio
;
1312 * We can't check if the regulator is a dummy or a real
1313 * regulator. So we need to specify the 5ms reset time (T_rst)
1314 * here instead of the 100us T_rtp time. We also need to wait
1315 * 300ms in case it was a real supply and the power was cutted
1316 * of. Toggle the reset pin is also a way to exit the hibernate
1319 gpiod_set_value_cansleep(reset_gpio
, 1);
1320 usleep_range(5000, 6000);
1322 ret
= regulator_enable(tsdata
->vcc
);
1324 dev_err(dev
, "Failed to enable vcc\n");
1328 usleep_range(1000, 2000);
1329 gpiod_set_value_cansleep(reset_gpio
, 0);
1332 edt_ft5x06_restore_reg_parameters(tsdata
);
1333 enable_irq(tsdata
->client
->irq
);
1335 if (tsdata
->factory_mode
)
1336 ret
= edt_ft5x06_factory_mode(tsdata
);
1338 struct gpio_desc
*wake_gpio
= tsdata
->wake_gpio
;
1340 gpiod_set_value_cansleep(wake_gpio
, 0);
1341 usleep_range(5000, 6000);
1342 gpiod_set_value_cansleep(wake_gpio
, 1);
1349 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1350 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1352 static const struct edt_i2c_chip_data edt_ft5x06_data
= {
1353 .max_support_points
= 5,
1356 static const struct edt_i2c_chip_data edt_ft5506_data
= {
1357 .max_support_points
= 10,
1360 static const struct edt_i2c_chip_data edt_ft6236_data
= {
1361 .max_support_points
= 2,
1364 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1365 { .name
= "edt-ft5x06", .driver_data
= (long)&edt_ft5x06_data
},
1366 { .name
= "edt-ft5506", .driver_data
= (long)&edt_ft5506_data
},
1367 { .name
= "ev-ft5726", .driver_data
= (long)&edt_ft5506_data
},
1368 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1369 { .name
= "ft6236", .driver_data
= (long)&edt_ft6236_data
},
1372 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1374 static const struct of_device_id edt_ft5x06_of_match
[] = {
1375 { .compatible
= "edt,edt-ft5206", .data
= &edt_ft5x06_data
},
1376 { .compatible
= "edt,edt-ft5306", .data
= &edt_ft5x06_data
},
1377 { .compatible
= "edt,edt-ft5406", .data
= &edt_ft5x06_data
},
1378 { .compatible
= "edt,edt-ft5506", .data
= &edt_ft5506_data
},
1379 { .compatible
= "evervision,ev-ft5726", .data
= &edt_ft5506_data
},
1380 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1381 { .compatible
= "focaltech,ft6236", .data
= &edt_ft6236_data
},
1384 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1386 static struct i2c_driver edt_ft5x06_ts_driver
= {
1388 .name
= "edt_ft5x06",
1389 .of_match_table
= edt_ft5x06_of_match
,
1390 .pm
= &edt_ft5x06_ts_pm_ops
,
1391 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1393 .id_table
= edt_ft5x06_ts_id
,
1394 .probe
= edt_ft5x06_ts_probe
,
1395 .remove
= edt_ft5x06_ts_remove
,
1398 module_i2c_driver(edt_ft5x06_ts_driver
);
1400 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1401 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1402 MODULE_LICENSE("GPL v2");