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/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>
31 #define WORK_REGISTER_THRESHOLD 0x00
32 #define WORK_REGISTER_REPORT_RATE 0x08
33 #define WORK_REGISTER_GAIN 0x30
34 #define WORK_REGISTER_OFFSET 0x31
35 #define WORK_REGISTER_NUM_X 0x33
36 #define WORK_REGISTER_NUM_Y 0x34
38 #define M09_REGISTER_THRESHOLD 0x80
39 #define M09_REGISTER_GAIN 0x92
40 #define M09_REGISTER_OFFSET 0x93
41 #define M09_REGISTER_NUM_X 0x94
42 #define M09_REGISTER_NUM_Y 0x95
44 #define EV_REGISTER_THRESHOLD 0x40
45 #define EV_REGISTER_GAIN 0x41
46 #define EV_REGISTER_OFFSET_Y 0x45
47 #define EV_REGISTER_OFFSET_X 0x46
49 #define NO_REGISTER 0xff
51 #define WORK_REGISTER_OPMODE 0x3c
52 #define FACTORY_REGISTER_OPMODE 0x01
54 #define TOUCH_EVENT_DOWN 0x00
55 #define TOUCH_EVENT_UP 0x01
56 #define TOUCH_EVENT_ON 0x02
57 #define TOUCH_EVENT_RESERVED 0x03
59 #define EDT_NAME_LEN 23
60 #define EDT_SWITCH_MODE_RETRIES 10
61 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
62 #define EDT_RAW_DATA_RETRIES 100
63 #define EDT_RAW_DATA_DELAY 1000 /* usec */
84 struct edt_ft5x06_ts_data
{
85 struct i2c_client
*client
;
86 struct input_dev
*input
;
87 struct touchscreen_properties prop
;
91 struct gpio_desc
*reset_gpio
;
92 struct gpio_desc
*wake_gpio
;
94 #if defined(CONFIG_DEBUG_FS)
95 struct dentry
*debug_dir
;
108 int max_support_points
;
110 char name
[EDT_NAME_LEN
];
112 struct edt_reg_addr reg_addr
;
113 enum edt_ver version
;
116 struct edt_i2c_chip_data
{
117 int max_support_points
;
120 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
121 u16 wr_len
, u8
*wr_buf
,
122 u16 rd_len
, u8
*rd_buf
)
124 struct i2c_msg wrmsg
[2];
129 wrmsg
[i
].addr
= client
->addr
;
131 wrmsg
[i
].len
= wr_len
;
132 wrmsg
[i
].buf
= wr_buf
;
136 wrmsg
[i
].addr
= client
->addr
;
137 wrmsg
[i
].flags
= I2C_M_RD
;
138 wrmsg
[i
].len
= rd_len
;
139 wrmsg
[i
].buf
= rd_buf
;
143 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
152 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
158 for (i
= 0; i
< buflen
- 1; i
++)
161 if (crc
!= buf
[buflen
-1]) {
162 dev_err_ratelimited(&tsdata
->client
->dev
,
163 "crc error: 0x%02x expected, got 0x%02x\n",
171 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
173 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
174 struct device
*dev
= &tsdata
->client
->dev
;
177 int i
, type
, x
, y
, id
;
178 int offset
, tplen
, datalen
, crclen
;
181 switch (tsdata
->version
) {
183 cmd
= 0xf9; /* tell the controller to send touch data */
184 offset
= 5; /* where the actual touch data starts */
185 tplen
= 4; /* data comes in so called frames */
186 crclen
= 1; /* length of the crc data */
203 memset(rdbuf
, 0, sizeof(rdbuf
));
204 datalen
= tplen
* tsdata
->max_support_points
+ offset
+ crclen
;
206 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
210 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
215 /* M09/M12 does not send header or CRC */
216 if (tsdata
->version
== EDT_M06
) {
217 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
218 rdbuf
[2] != datalen
) {
219 dev_err_ratelimited(dev
,
220 "Unexpected header: %02x%02x%02x!\n",
221 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
225 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
229 for (i
= 0; i
< tsdata
->max_support_points
; i
++) {
230 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
234 /* ignore Reserved events */
235 if (type
== TOUCH_EVENT_RESERVED
)
238 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
239 if (tsdata
->version
== EDT_M06
&& type
== TOUCH_EVENT_DOWN
)
242 x
= ((buf
[0] << 8) | buf
[1]) & 0x0fff;
243 y
= ((buf
[2] << 8) | buf
[3]) & 0x0fff;
244 /* The FT5x26 send the y coordinate first */
245 if (tsdata
->version
== EV_FT
)
248 id
= (buf
[2] >> 4) & 0x0f;
249 down
= type
!= TOUCH_EVENT_UP
;
251 input_mt_slot(tsdata
->input
, id
);
252 input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
, down
);
257 touchscreen_report_pos(tsdata
->input
, &tsdata
->prop
, x
, y
,
261 input_mt_report_pointer_emulation(tsdata
->input
, true);
262 input_sync(tsdata
->input
);
268 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data
*tsdata
,
273 switch (tsdata
->version
) {
275 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
276 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
278 wrbuf
[3] = wrbuf
[0] ^ wrbuf
[1] ^ wrbuf
[2];
279 return edt_ft5x06_ts_readwrite(tsdata
->client
, 4,
289 return edt_ft5x06_ts_readwrite(tsdata
->client
, 2,
297 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data
*tsdata
,
300 u8 wrbuf
[2], rdbuf
[2];
303 switch (tsdata
->version
) {
305 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
306 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
307 wrbuf
[1] |= tsdata
->factory_mode
? 0x80 : 0x40;
309 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 2, wrbuf
, 2,
314 if ((wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0]) != rdbuf
[1]) {
315 dev_err(&tsdata
->client
->dev
,
316 "crc error: 0x%02x expected, got 0x%02x\n",
317 wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0],
329 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
342 struct edt_ft5x06_attribute
{
343 struct device_attribute dattr
;
352 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
353 _limit_low, _limit_high) \
354 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
355 .dattr = __ATTR(_field, _mode, \
356 edt_ft5x06_setting_show, \
357 edt_ft5x06_setting_store), \
358 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
359 .addr_m06 = _addr_m06, \
360 .addr_m09 = _addr_m09, \
361 .addr_ev = _addr_ev, \
362 .limit_low = _limit_low, \
363 .limit_high = _limit_high, \
366 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
367 struct device_attribute
*dattr
,
370 struct i2c_client
*client
= to_i2c_client(dev
);
371 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
372 struct edt_ft5x06_attribute
*attr
=
373 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
374 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
380 mutex_lock(&tsdata
->mutex
);
382 if (tsdata
->factory_mode
) {
387 switch (tsdata
->version
) {
389 addr
= attr
->addr_m06
;
395 addr
= attr
->addr_m09
;
399 addr
= attr
->addr_ev
;
407 if (addr
!= NO_REGISTER
) {
408 val
= edt_ft5x06_register_read(tsdata
, addr
);
411 dev_err(&tsdata
->client
->dev
,
412 "Failed to fetch attribute %s, error %d\n",
413 dattr
->attr
.name
, error
);
421 dev_warn(&tsdata
->client
->dev
,
422 "%s: read (%d) and stored value (%d) differ\n",
423 dattr
->attr
.name
, val
, *field
);
427 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
429 mutex_unlock(&tsdata
->mutex
);
430 return error
?: count
;
433 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
434 struct device_attribute
*dattr
,
435 const char *buf
, size_t count
)
437 struct i2c_client
*client
= to_i2c_client(dev
);
438 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
439 struct edt_ft5x06_attribute
*attr
=
440 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
441 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
446 mutex_lock(&tsdata
->mutex
);
448 if (tsdata
->factory_mode
) {
453 error
= kstrtouint(buf
, 0, &val
);
457 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
462 switch (tsdata
->version
) {
464 addr
= attr
->addr_m06
;
470 addr
= attr
->addr_m09
;
474 addr
= attr
->addr_ev
;
482 if (addr
!= NO_REGISTER
) {
483 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
485 dev_err(&tsdata
->client
->dev
,
486 "Failed to update attribute %s, error: %d\n",
487 dattr
->attr
.name
, error
);
494 mutex_unlock(&tsdata
->mutex
);
495 return error
?: count
;
498 /* m06, m09: range 0-31, m12: range 0-5 */
499 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
500 M09_REGISTER_GAIN
, EV_REGISTER_GAIN
, 0, 31);
501 /* m06, m09: range 0-31, m12: range 0-16 */
502 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
503 M09_REGISTER_OFFSET
, NO_REGISTER
, 0, 31);
504 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
505 static EDT_ATTR(offset_x
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
506 EV_REGISTER_OFFSET_X
, 0, 80);
507 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
508 static EDT_ATTR(offset_y
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
509 EV_REGISTER_OFFSET_Y
, 0, 80);
510 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
511 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
512 M09_REGISTER_THRESHOLD
, EV_REGISTER_THRESHOLD
, 0, 255);
513 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
514 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
515 NO_REGISTER
, NO_REGISTER
, 0, 255);
517 static struct attribute
*edt_ft5x06_attrs
[] = {
518 &edt_ft5x06_attr_gain
.dattr
.attr
,
519 &edt_ft5x06_attr_offset
.dattr
.attr
,
520 &edt_ft5x06_attr_offset_x
.dattr
.attr
,
521 &edt_ft5x06_attr_offset_y
.dattr
.attr
,
522 &edt_ft5x06_attr_threshold
.dattr
.attr
,
523 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
527 static const struct attribute_group edt_ft5x06_attr_group
= {
528 .attrs
= edt_ft5x06_attrs
,
531 #ifdef CONFIG_DEBUG_FS
532 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
534 struct i2c_client
*client
= tsdata
->client
;
535 int retries
= EDT_SWITCH_MODE_RETRIES
;
539 if (tsdata
->version
!= EDT_M06
) {
540 dev_err(&client
->dev
,
541 "No factory mode support for non-M06 devices\n");
545 disable_irq(client
->irq
);
547 if (!tsdata
->raw_buffer
) {
548 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
550 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
551 if (!tsdata
->raw_buffer
) {
557 /* mode register is 0x3c when in the work mode */
558 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
560 dev_err(&client
->dev
,
561 "failed to switch to factory mode, error %d\n", error
);
565 tsdata
->factory_mode
= true;
567 mdelay(EDT_SWITCH_MODE_DELAY
);
568 /* mode register is 0x01 when in factory mode */
569 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
572 } while (--retries
> 0);
575 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
576 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
584 kfree(tsdata
->raw_buffer
);
585 tsdata
->raw_buffer
= NULL
;
586 tsdata
->factory_mode
= false;
587 enable_irq(client
->irq
);
592 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
594 struct i2c_client
*client
= tsdata
->client
;
595 int retries
= EDT_SWITCH_MODE_RETRIES
;
596 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
600 /* mode register is 0x01 when in the factory mode */
601 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
603 dev_err(&client
->dev
,
604 "failed to switch to work mode, error: %d\n", error
);
608 tsdata
->factory_mode
= false;
611 mdelay(EDT_SWITCH_MODE_DELAY
);
612 /* mode register is 0x01 when in factory mode */
613 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
616 } while (--retries
> 0);
619 dev_err(&client
->dev
, "not in work mode after %dms.\n",
620 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
621 tsdata
->factory_mode
= true;
625 kfree(tsdata
->raw_buffer
);
626 tsdata
->raw_buffer
= NULL
;
628 /* restore parameters */
629 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
631 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
633 if (reg_addr
->reg_offset
!= NO_REGISTER
)
634 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
636 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
637 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_x
,
639 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
640 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_y
,
642 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
643 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
644 tsdata
->report_rate
);
646 enable_irq(client
->irq
);
651 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
653 struct edt_ft5x06_ts_data
*tsdata
= data
;
655 *mode
= tsdata
->factory_mode
;
660 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
662 struct edt_ft5x06_ts_data
*tsdata
= data
;
668 mutex_lock(&tsdata
->mutex
);
670 if (mode
!= tsdata
->factory_mode
) {
671 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
672 edt_ft5x06_work_mode(tsdata
);
675 mutex_unlock(&tsdata
->mutex
);
680 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
681 edt_ft5x06_debugfs_mode_set
, "%llu\n");
683 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
684 char __user
*buf
, size_t count
, loff_t
*off
)
686 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
687 struct i2c_client
*client
= tsdata
->client
;
688 int retries
= EDT_RAW_DATA_RETRIES
;
695 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
698 mutex_lock(&tsdata
->mutex
);
700 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
705 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
707 dev_dbg(&client
->dev
,
708 "failed to write 0x08 register, error %d\n", error
);
713 usleep_range(EDT_RAW_DATA_DELAY
, EDT_RAW_DATA_DELAY
+ 100);
714 val
= edt_ft5x06_register_read(tsdata
, 0x08);
717 } while (--retries
> 0);
721 dev_dbg(&client
->dev
,
722 "failed to read 0x08 register, error %d\n", error
);
727 dev_dbg(&client
->dev
,
728 "timed out waiting for register to settle\n");
733 rdbuf
= tsdata
->raw_buffer
;
734 colbytes
= tsdata
->num_y
* sizeof(u16
);
738 for (i
= 0; i
< tsdata
->num_x
; i
++) {
739 wrbuf
[2] = i
; /* column index */
740 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
741 sizeof(wrbuf
), wrbuf
,
749 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
750 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
757 mutex_unlock(&tsdata
->mutex
);
758 return error
?: read
;
761 static const struct file_operations debugfs_raw_data_fops
= {
763 .read
= edt_ft5x06_debugfs_raw_data_read
,
767 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
768 const char *debugfs_name
)
770 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
771 if (!tsdata
->debug_dir
)
774 debugfs_create_u16("num_x", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_x
);
775 debugfs_create_u16("num_y", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_y
);
777 debugfs_create_file("mode", S_IRUSR
| S_IWUSR
,
778 tsdata
->debug_dir
, tsdata
, &debugfs_mode_fops
);
779 debugfs_create_file("raw_data", S_IRUSR
,
780 tsdata
->debug_dir
, tsdata
, &debugfs_raw_data_fops
);
784 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
786 debugfs_remove_recursive(tsdata
->debug_dir
);
787 kfree(tsdata
->raw_buffer
);
793 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
794 const char *debugfs_name
)
799 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
803 #endif /* CONFIG_DEBUGFS */
805 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
806 struct edt_ft5x06_ts_data
*tsdata
,
809 u8 rdbuf
[EDT_NAME_LEN
];
812 char *model_name
= tsdata
->name
;
814 /* see what we find if we assume it is a M06 *
815 * if we get less than EDT_NAME_LEN, we don't want
816 * to have garbage in there
818 memset(rdbuf
, 0, sizeof(rdbuf
));
819 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xBB",
820 EDT_NAME_LEN
- 1, rdbuf
);
824 /* Probe content for something consistent.
825 * M06 starts with a response byte, M12 gives the data directly.
826 * M09/Generic does not provide model number information.
828 if (!strncasecmp(rdbuf
+ 1, "EP0", 3)) {
829 tsdata
->version
= EDT_M06
;
831 /* remove last '$' end marker */
832 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
833 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
834 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
836 /* look for Model/Version separator */
837 p
= strchr(rdbuf
, '*');
840 strlcpy(model_name
, rdbuf
+ 1, EDT_NAME_LEN
);
841 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
842 } else if (!strncasecmp(rdbuf
, "EP0", 3)) {
843 tsdata
->version
= EDT_M12
;
845 /* remove last '$' end marker */
846 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
847 if (rdbuf
[EDT_NAME_LEN
- 3] == '$')
848 rdbuf
[EDT_NAME_LEN
- 3] = '\0';
850 /* look for Model/Version separator */
851 p
= strchr(rdbuf
, '*');
854 strlcpy(model_name
, rdbuf
, EDT_NAME_LEN
);
855 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
857 /* If it is not an EDT M06/M12 touchscreen, then the model
858 * detection is a bit hairy. The different ft5x06
859 * firmares around don't reliably implement the
860 * identification registers. Well, we'll take a shot.
862 * The main difference between generic focaltec based
863 * touches and EDT M09 is that we know how to retrieve
864 * the max coordinates for the latter.
866 tsdata
->version
= GENERIC_FT
;
868 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
873 strlcpy(fw_version
, rdbuf
, 2);
875 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
880 /* This "model identification" is not exact. Unfortunately
881 * not all firmwares for the ft5x06 put useful values in
882 * the identification registers.
885 case 0x35: /* EDT EP0350M09 */
886 case 0x43: /* EDT EP0430M09 */
887 case 0x50: /* EDT EP0500M09 */
888 case 0x57: /* EDT EP0570M09 */
889 case 0x70: /* EDT EP0700M09 */
890 tsdata
->version
= EDT_M09
;
891 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
892 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
894 case 0xa1: /* EDT EP1010ML00 */
895 tsdata
->version
= EDT_M09
;
896 snprintf(model_name
, EDT_NAME_LEN
, "EP%i%i0ML00",
897 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
899 case 0x5a: /* Solomon Goldentek Display */
900 snprintf(model_name
, EDT_NAME_LEN
, "GKTW50SCED1R0");
902 case 0x59: /* Evervision Display with FT5xx6 TS */
903 tsdata
->version
= EV_FT
;
904 error
= edt_ft5x06_ts_readwrite(client
, 1, "\x53",
908 strlcpy(fw_version
, rdbuf
, 1);
909 snprintf(model_name
, EDT_NAME_LEN
,
910 "EVERVISION-FT5726NEi");
913 snprintf(model_name
, EDT_NAME_LEN
,
914 "generic ft5x06 (%02x)",
923 static void edt_ft5x06_ts_get_defaults(struct device
*dev
,
924 struct edt_ft5x06_ts_data
*tsdata
)
926 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
930 error
= device_property_read_u32(dev
, "threshold", &val
);
932 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
, val
);
933 tsdata
->threshold
= val
;
936 error
= device_property_read_u32(dev
, "gain", &val
);
938 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
, val
);
942 error
= device_property_read_u32(dev
, "offset", &val
);
944 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
, val
);
945 tsdata
->offset
= val
;
948 error
= device_property_read_u32(dev
, "offset-x", &val
);
950 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_x
, val
);
951 tsdata
->offset_x
= val
;
954 error
= device_property_read_u32(dev
, "offset-y", &val
);
956 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_y
, val
);
957 tsdata
->offset_y
= val
;
962 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
964 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
966 tsdata
->threshold
= edt_ft5x06_register_read(tsdata
,
967 reg_addr
->reg_threshold
);
968 tsdata
->gain
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_gain
);
969 if (reg_addr
->reg_offset
!= NO_REGISTER
)
971 edt_ft5x06_register_read(tsdata
, reg_addr
->reg_offset
);
972 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
973 tsdata
->offset_x
= edt_ft5x06_register_read(tsdata
,
974 reg_addr
->reg_offset_x
);
975 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
976 tsdata
->offset_y
= edt_ft5x06_register_read(tsdata
,
977 reg_addr
->reg_offset_y
);
978 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
979 tsdata
->report_rate
= edt_ft5x06_register_read(tsdata
,
980 reg_addr
->reg_report_rate
);
981 if (tsdata
->version
== EDT_M06
||
982 tsdata
->version
== EDT_M09
||
983 tsdata
->version
== EDT_M12
) {
984 tsdata
->num_x
= edt_ft5x06_register_read(tsdata
,
985 reg_addr
->reg_num_x
);
986 tsdata
->num_y
= edt_ft5x06_register_read(tsdata
,
987 reg_addr
->reg_num_y
);
995 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
997 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
999 switch (tsdata
->version
) {
1001 reg_addr
->reg_threshold
= WORK_REGISTER_THRESHOLD
;
1002 reg_addr
->reg_report_rate
= WORK_REGISTER_REPORT_RATE
;
1003 reg_addr
->reg_gain
= WORK_REGISTER_GAIN
;
1004 reg_addr
->reg_offset
= WORK_REGISTER_OFFSET
;
1005 reg_addr
->reg_offset_x
= NO_REGISTER
;
1006 reg_addr
->reg_offset_y
= NO_REGISTER
;
1007 reg_addr
->reg_num_x
= WORK_REGISTER_NUM_X
;
1008 reg_addr
->reg_num_y
= WORK_REGISTER_NUM_Y
;
1013 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
1014 reg_addr
->reg_report_rate
= NO_REGISTER
;
1015 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
1016 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
1017 reg_addr
->reg_offset_x
= NO_REGISTER
;
1018 reg_addr
->reg_offset_y
= NO_REGISTER
;
1019 reg_addr
->reg_num_x
= M09_REGISTER_NUM_X
;
1020 reg_addr
->reg_num_y
= M09_REGISTER_NUM_Y
;
1024 reg_addr
->reg_threshold
= EV_REGISTER_THRESHOLD
;
1025 reg_addr
->reg_gain
= EV_REGISTER_GAIN
;
1026 reg_addr
->reg_offset
= NO_REGISTER
;
1027 reg_addr
->reg_offset_x
= EV_REGISTER_OFFSET_X
;
1028 reg_addr
->reg_offset_y
= EV_REGISTER_OFFSET_Y
;
1029 reg_addr
->reg_num_x
= NO_REGISTER
;
1030 reg_addr
->reg_num_y
= NO_REGISTER
;
1031 reg_addr
->reg_report_rate
= NO_REGISTER
;
1035 /* this is a guesswork */
1036 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
1037 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
1038 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
1039 reg_addr
->reg_offset_x
= NO_REGISTER
;
1040 reg_addr
->reg_offset_y
= NO_REGISTER
;
1045 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
1046 const struct i2c_device_id
*id
)
1048 const struct edt_i2c_chip_data
*chip_data
;
1049 struct edt_ft5x06_ts_data
*tsdata
;
1050 struct input_dev
*input
;
1051 unsigned long irq_flags
;
1053 char fw_version
[EDT_NAME_LEN
];
1055 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
1057 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
1059 dev_err(&client
->dev
, "failed to allocate driver data.\n");
1063 chip_data
= device_get_match_data(&client
->dev
);
1065 chip_data
= (const struct edt_i2c_chip_data
*)id
->driver_data
;
1066 if (!chip_data
|| !chip_data
->max_support_points
) {
1067 dev_err(&client
->dev
, "invalid or missing chip data\n");
1071 tsdata
->max_support_points
= chip_data
->max_support_points
;
1073 tsdata
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
1074 "reset", GPIOD_OUT_HIGH
);
1075 if (IS_ERR(tsdata
->reset_gpio
)) {
1076 error
= PTR_ERR(tsdata
->reset_gpio
);
1077 dev_err(&client
->dev
,
1078 "Failed to request GPIO reset pin, error %d\n", error
);
1082 tsdata
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
1083 "wake", GPIOD_OUT_LOW
);
1084 if (IS_ERR(tsdata
->wake_gpio
)) {
1085 error
= PTR_ERR(tsdata
->wake_gpio
);
1086 dev_err(&client
->dev
,
1087 "Failed to request GPIO wake pin, error %d\n", error
);
1091 if (tsdata
->wake_gpio
) {
1092 usleep_range(5000, 6000);
1093 gpiod_set_value_cansleep(tsdata
->wake_gpio
, 1);
1096 if (tsdata
->reset_gpio
) {
1097 usleep_range(5000, 6000);
1098 gpiod_set_value_cansleep(tsdata
->reset_gpio
, 0);
1102 input
= devm_input_allocate_device(&client
->dev
);
1104 dev_err(&client
->dev
, "failed to allocate input device.\n");
1108 mutex_init(&tsdata
->mutex
);
1109 tsdata
->client
= client
;
1110 tsdata
->input
= input
;
1111 tsdata
->factory_mode
= false;
1113 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
1115 dev_err(&client
->dev
, "touchscreen probe failed\n");
1119 edt_ft5x06_ts_set_regs(tsdata
);
1120 edt_ft5x06_ts_get_defaults(&client
->dev
, tsdata
);
1121 edt_ft5x06_ts_get_parameters(tsdata
);
1123 dev_dbg(&client
->dev
,
1124 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1125 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
1127 input
->name
= tsdata
->name
;
1128 input
->id
.bustype
= BUS_I2C
;
1129 input
->dev
.parent
= &client
->dev
;
1131 if (tsdata
->version
== EDT_M06
||
1132 tsdata
->version
== EDT_M09
||
1133 tsdata
->version
== EDT_M12
) {
1134 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1135 0, tsdata
->num_x
* 64 - 1, 0, 0);
1136 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1137 0, tsdata
->num_y
* 64 - 1, 0, 0);
1139 /* Unknown maximum values. Specify via devicetree */
1140 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1142 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1146 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
1148 error
= input_mt_init_slots(input
, tsdata
->max_support_points
,
1151 dev_err(&client
->dev
, "Unable to init MT slots.\n");
1155 i2c_set_clientdata(client
, tsdata
);
1157 irq_flags
= irq_get_trigger_type(client
->irq
);
1158 if (irq_flags
== IRQF_TRIGGER_NONE
)
1159 irq_flags
= IRQF_TRIGGER_FALLING
;
1160 irq_flags
|= IRQF_ONESHOT
;
1162 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1163 NULL
, edt_ft5x06_ts_isr
, irq_flags
,
1164 client
->name
, tsdata
);
1166 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
1170 error
= devm_device_add_group(&client
->dev
, &edt_ft5x06_attr_group
);
1174 error
= input_register_device(input
);
1178 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1179 device_init_wakeup(&client
->dev
, 1);
1181 dev_dbg(&client
->dev
,
1182 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1184 tsdata
->wake_gpio
? desc_to_gpio(tsdata
->wake_gpio
) : -1,
1185 tsdata
->reset_gpio
? desc_to_gpio(tsdata
->reset_gpio
) : -1);
1190 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1192 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1194 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1199 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1201 struct i2c_client
*client
= to_i2c_client(dev
);
1203 if (device_may_wakeup(dev
))
1204 enable_irq_wake(client
->irq
);
1209 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1211 struct i2c_client
*client
= to_i2c_client(dev
);
1213 if (device_may_wakeup(dev
))
1214 disable_irq_wake(client
->irq
);
1219 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1220 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1222 static const struct edt_i2c_chip_data edt_ft5x06_data
= {
1223 .max_support_points
= 5,
1226 static const struct edt_i2c_chip_data edt_ft5506_data
= {
1227 .max_support_points
= 10,
1230 static const struct edt_i2c_chip_data edt_ft6236_data
= {
1231 .max_support_points
= 2,
1234 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1235 { .name
= "edt-ft5x06", .driver_data
= (long)&edt_ft5x06_data
},
1236 { .name
= "edt-ft5506", .driver_data
= (long)&edt_ft5506_data
},
1237 { .name
= "ev-ft5726", .driver_data
= (long)&edt_ft5506_data
},
1238 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1239 { .name
= "ft6236", .driver_data
= (long)&edt_ft6236_data
},
1242 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1244 static const struct of_device_id edt_ft5x06_of_match
[] = {
1245 { .compatible
= "edt,edt-ft5206", .data
= &edt_ft5x06_data
},
1246 { .compatible
= "edt,edt-ft5306", .data
= &edt_ft5x06_data
},
1247 { .compatible
= "edt,edt-ft5406", .data
= &edt_ft5x06_data
},
1248 { .compatible
= "edt,edt-ft5506", .data
= &edt_ft5506_data
},
1249 { .compatible
= "evervision,ev-ft5726", .data
= &edt_ft5506_data
},
1250 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1251 { .compatible
= "focaltech,ft6236", .data
= &edt_ft6236_data
},
1254 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1256 static struct i2c_driver edt_ft5x06_ts_driver
= {
1258 .name
= "edt_ft5x06",
1259 .of_match_table
= edt_ft5x06_of_match
,
1260 .pm
= &edt_ft5x06_ts_pm_ops
,
1262 .id_table
= edt_ft5x06_ts_id
,
1263 .probe
= edt_ft5x06_ts_probe
,
1264 .remove
= edt_ft5x06_ts_remove
,
1267 module_i2c_driver(edt_ft5x06_ts_driver
);
1269 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1270 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1271 MODULE_LICENSE("GPL v2");