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>
30 #include <asm/unaligned.h>
31 #include <linux/regulator/consumer.h>
33 #define WORK_REGISTER_THRESHOLD 0x00
34 #define WORK_REGISTER_REPORT_RATE 0x08
35 #define WORK_REGISTER_GAIN 0x30
36 #define WORK_REGISTER_OFFSET 0x31
37 #define WORK_REGISTER_NUM_X 0x33
38 #define WORK_REGISTER_NUM_Y 0x34
40 #define M09_REGISTER_THRESHOLD 0x80
41 #define M09_REGISTER_GAIN 0x92
42 #define M09_REGISTER_OFFSET 0x93
43 #define M09_REGISTER_NUM_X 0x94
44 #define M09_REGISTER_NUM_Y 0x95
46 #define EV_REGISTER_THRESHOLD 0x40
47 #define EV_REGISTER_GAIN 0x41
48 #define EV_REGISTER_OFFSET_Y 0x45
49 #define EV_REGISTER_OFFSET_X 0x46
51 #define NO_REGISTER 0xff
53 #define WORK_REGISTER_OPMODE 0x3c
54 #define FACTORY_REGISTER_OPMODE 0x01
56 #define TOUCH_EVENT_DOWN 0x00
57 #define TOUCH_EVENT_UP 0x01
58 #define TOUCH_EVENT_ON 0x02
59 #define TOUCH_EVENT_RESERVED 0x03
61 #define EDT_NAME_LEN 23
62 #define EDT_SWITCH_MODE_RETRIES 10
63 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
64 #define EDT_RAW_DATA_RETRIES 100
65 #define EDT_RAW_DATA_DELAY 1000 /* usec */
86 struct edt_ft5x06_ts_data
{
87 struct i2c_client
*client
;
88 struct input_dev
*input
;
89 struct touchscreen_properties prop
;
92 struct regulator
*vcc
;
94 struct gpio_desc
*reset_gpio
;
95 struct gpio_desc
*wake_gpio
;
97 #if defined(CONFIG_DEBUG_FS)
98 struct dentry
*debug_dir
;
111 int max_support_points
;
113 char name
[EDT_NAME_LEN
];
115 struct edt_reg_addr reg_addr
;
116 enum edt_ver version
;
119 struct edt_i2c_chip_data
{
120 int max_support_points
;
123 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
124 u16 wr_len
, u8
*wr_buf
,
125 u16 rd_len
, u8
*rd_buf
)
127 struct i2c_msg wrmsg
[2];
132 wrmsg
[i
].addr
= client
->addr
;
134 wrmsg
[i
].len
= wr_len
;
135 wrmsg
[i
].buf
= wr_buf
;
139 wrmsg
[i
].addr
= client
->addr
;
140 wrmsg
[i
].flags
= I2C_M_RD
;
141 wrmsg
[i
].len
= rd_len
;
142 wrmsg
[i
].buf
= rd_buf
;
146 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
155 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
161 for (i
= 0; i
< buflen
- 1; i
++)
164 if (crc
!= buf
[buflen
-1]) {
165 dev_err_ratelimited(&tsdata
->client
->dev
,
166 "crc error: 0x%02x expected, got 0x%02x\n",
174 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
176 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
177 struct device
*dev
= &tsdata
->client
->dev
;
180 int i
, type
, x
, y
, id
;
181 int offset
, tplen
, datalen
, crclen
;
184 switch (tsdata
->version
) {
186 cmd
= 0xf9; /* tell the controller to send touch data */
187 offset
= 5; /* where the actual touch data starts */
188 tplen
= 4; /* data comes in so called frames */
189 crclen
= 1; /* length of the crc data */
206 memset(rdbuf
, 0, sizeof(rdbuf
));
207 datalen
= tplen
* tsdata
->max_support_points
+ offset
+ crclen
;
209 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
213 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
218 /* M09/M12 does not send header or CRC */
219 if (tsdata
->version
== EDT_M06
) {
220 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
221 rdbuf
[2] != datalen
) {
222 dev_err_ratelimited(dev
,
223 "Unexpected header: %02x%02x%02x!\n",
224 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
228 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
232 for (i
= 0; i
< tsdata
->max_support_points
; i
++) {
233 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
236 /* ignore Reserved events */
237 if (type
== TOUCH_EVENT_RESERVED
)
240 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
241 if (tsdata
->version
== EDT_M06
&& type
== TOUCH_EVENT_DOWN
)
244 x
= get_unaligned_be16(buf
) & 0x0fff;
245 y
= get_unaligned_be16(buf
+ 2) & 0x0fff;
246 /* The FT5x26 send the y coordinate first */
247 if (tsdata
->version
== EV_FT
)
250 id
= (buf
[2] >> 4) & 0x0f;
252 input_mt_slot(tsdata
->input
, id
);
253 if (input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
,
254 type
!= TOUCH_EVENT_UP
))
255 touchscreen_report_pos(tsdata
->input
, &tsdata
->prop
,
259 input_mt_report_pointer_emulation(tsdata
->input
, true);
260 input_sync(tsdata
->input
);
266 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data
*tsdata
,
271 switch (tsdata
->version
) {
273 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
274 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
276 wrbuf
[3] = wrbuf
[0] ^ wrbuf
[1] ^ wrbuf
[2];
277 return edt_ft5x06_ts_readwrite(tsdata
->client
, 4,
287 return edt_ft5x06_ts_readwrite(tsdata
->client
, 2,
295 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data
*tsdata
,
298 u8 wrbuf
[2], rdbuf
[2];
301 switch (tsdata
->version
) {
303 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
304 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
305 wrbuf
[1] |= tsdata
->factory_mode
? 0x80 : 0x40;
307 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 2, wrbuf
, 2,
312 if ((wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0]) != rdbuf
[1]) {
313 dev_err(&tsdata
->client
->dev
,
314 "crc error: 0x%02x expected, got 0x%02x\n",
315 wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0],
327 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
340 struct edt_ft5x06_attribute
{
341 struct device_attribute dattr
;
350 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
351 _limit_low, _limit_high) \
352 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
353 .dattr = __ATTR(_field, _mode, \
354 edt_ft5x06_setting_show, \
355 edt_ft5x06_setting_store), \
356 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
357 .addr_m06 = _addr_m06, \
358 .addr_m09 = _addr_m09, \
359 .addr_ev = _addr_ev, \
360 .limit_low = _limit_low, \
361 .limit_high = _limit_high, \
364 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
365 struct device_attribute
*dattr
,
368 struct i2c_client
*client
= to_i2c_client(dev
);
369 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
370 struct edt_ft5x06_attribute
*attr
=
371 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
372 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
378 mutex_lock(&tsdata
->mutex
);
380 if (tsdata
->factory_mode
) {
385 switch (tsdata
->version
) {
387 addr
= attr
->addr_m06
;
393 addr
= attr
->addr_m09
;
397 addr
= attr
->addr_ev
;
405 if (addr
!= NO_REGISTER
) {
406 val
= edt_ft5x06_register_read(tsdata
, addr
);
409 dev_err(&tsdata
->client
->dev
,
410 "Failed to fetch attribute %s, error %d\n",
411 dattr
->attr
.name
, error
);
419 dev_warn(&tsdata
->client
->dev
,
420 "%s: read (%d) and stored value (%d) differ\n",
421 dattr
->attr
.name
, val
, *field
);
425 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
427 mutex_unlock(&tsdata
->mutex
);
428 return error
?: count
;
431 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
432 struct device_attribute
*dattr
,
433 const char *buf
, size_t count
)
435 struct i2c_client
*client
= to_i2c_client(dev
);
436 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
437 struct edt_ft5x06_attribute
*attr
=
438 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
439 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
444 mutex_lock(&tsdata
->mutex
);
446 if (tsdata
->factory_mode
) {
451 error
= kstrtouint(buf
, 0, &val
);
455 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
460 switch (tsdata
->version
) {
462 addr
= attr
->addr_m06
;
468 addr
= attr
->addr_m09
;
472 addr
= attr
->addr_ev
;
480 if (addr
!= NO_REGISTER
) {
481 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
483 dev_err(&tsdata
->client
->dev
,
484 "Failed to update attribute %s, error: %d\n",
485 dattr
->attr
.name
, error
);
492 mutex_unlock(&tsdata
->mutex
);
493 return error
?: count
;
496 /* m06, m09: range 0-31, m12: range 0-5 */
497 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
498 M09_REGISTER_GAIN
, EV_REGISTER_GAIN
, 0, 31);
499 /* m06, m09: range 0-31, m12: range 0-16 */
500 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
501 M09_REGISTER_OFFSET
, NO_REGISTER
, 0, 31);
502 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
503 static EDT_ATTR(offset_x
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
504 EV_REGISTER_OFFSET_X
, 0, 80);
505 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
506 static EDT_ATTR(offset_y
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
507 EV_REGISTER_OFFSET_Y
, 0, 80);
508 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
509 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
510 M09_REGISTER_THRESHOLD
, EV_REGISTER_THRESHOLD
, 0, 255);
511 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
512 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
513 NO_REGISTER
, NO_REGISTER
, 0, 255);
515 static struct attribute
*edt_ft5x06_attrs
[] = {
516 &edt_ft5x06_attr_gain
.dattr
.attr
,
517 &edt_ft5x06_attr_offset
.dattr
.attr
,
518 &edt_ft5x06_attr_offset_x
.dattr
.attr
,
519 &edt_ft5x06_attr_offset_y
.dattr
.attr
,
520 &edt_ft5x06_attr_threshold
.dattr
.attr
,
521 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
525 static const struct attribute_group edt_ft5x06_attr_group
= {
526 .attrs
= edt_ft5x06_attrs
,
529 #ifdef CONFIG_DEBUG_FS
530 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
532 struct i2c_client
*client
= tsdata
->client
;
533 int retries
= EDT_SWITCH_MODE_RETRIES
;
537 if (tsdata
->version
!= EDT_M06
) {
538 dev_err(&client
->dev
,
539 "No factory mode support for non-M06 devices\n");
543 disable_irq(client
->irq
);
545 if (!tsdata
->raw_buffer
) {
546 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
548 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
549 if (!tsdata
->raw_buffer
) {
555 /* mode register is 0x3c when in the work mode */
556 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
558 dev_err(&client
->dev
,
559 "failed to switch to factory mode, error %d\n", error
);
563 tsdata
->factory_mode
= true;
565 mdelay(EDT_SWITCH_MODE_DELAY
);
566 /* mode register is 0x01 when in factory mode */
567 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
570 } while (--retries
> 0);
573 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
574 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
582 kfree(tsdata
->raw_buffer
);
583 tsdata
->raw_buffer
= NULL
;
584 tsdata
->factory_mode
= false;
585 enable_irq(client
->irq
);
590 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
592 struct i2c_client
*client
= tsdata
->client
;
593 int retries
= EDT_SWITCH_MODE_RETRIES
;
594 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
598 /* mode register is 0x01 when in the factory mode */
599 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
601 dev_err(&client
->dev
,
602 "failed to switch to work mode, error: %d\n", error
);
606 tsdata
->factory_mode
= false;
609 mdelay(EDT_SWITCH_MODE_DELAY
);
610 /* mode register is 0x01 when in factory mode */
611 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
614 } while (--retries
> 0);
617 dev_err(&client
->dev
, "not in work mode after %dms.\n",
618 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
619 tsdata
->factory_mode
= true;
623 kfree(tsdata
->raw_buffer
);
624 tsdata
->raw_buffer
= NULL
;
626 /* restore parameters */
627 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
629 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
631 if (reg_addr
->reg_offset
!= NO_REGISTER
)
632 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
634 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
635 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_x
,
637 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
638 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_y
,
640 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
641 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
642 tsdata
->report_rate
);
644 enable_irq(client
->irq
);
649 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
651 struct edt_ft5x06_ts_data
*tsdata
= data
;
653 *mode
= tsdata
->factory_mode
;
658 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
660 struct edt_ft5x06_ts_data
*tsdata
= data
;
666 mutex_lock(&tsdata
->mutex
);
668 if (mode
!= tsdata
->factory_mode
) {
669 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
670 edt_ft5x06_work_mode(tsdata
);
673 mutex_unlock(&tsdata
->mutex
);
678 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
679 edt_ft5x06_debugfs_mode_set
, "%llu\n");
681 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
682 char __user
*buf
, size_t count
, loff_t
*off
)
684 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
685 struct i2c_client
*client
= tsdata
->client
;
686 int retries
= EDT_RAW_DATA_RETRIES
;
693 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
696 mutex_lock(&tsdata
->mutex
);
698 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
703 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
705 dev_dbg(&client
->dev
,
706 "failed to write 0x08 register, error %d\n", error
);
711 usleep_range(EDT_RAW_DATA_DELAY
, EDT_RAW_DATA_DELAY
+ 100);
712 val
= edt_ft5x06_register_read(tsdata
, 0x08);
715 } while (--retries
> 0);
719 dev_dbg(&client
->dev
,
720 "failed to read 0x08 register, error %d\n", error
);
725 dev_dbg(&client
->dev
,
726 "timed out waiting for register to settle\n");
731 rdbuf
= tsdata
->raw_buffer
;
732 colbytes
= tsdata
->num_y
* sizeof(u16
);
736 for (i
= 0; i
< tsdata
->num_x
; i
++) {
737 wrbuf
[2] = i
; /* column index */
738 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
739 sizeof(wrbuf
), wrbuf
,
747 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
748 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
755 mutex_unlock(&tsdata
->mutex
);
756 return error
?: read
;
759 static const struct file_operations debugfs_raw_data_fops
= {
761 .read
= edt_ft5x06_debugfs_raw_data_read
,
765 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
766 const char *debugfs_name
)
768 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
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
);
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
);
780 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
782 debugfs_remove_recursive(tsdata
->debug_dir
);
783 kfree(tsdata
->raw_buffer
);
789 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
790 const char *debugfs_name
)
795 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
799 #endif /* CONFIG_DEBUGFS */
801 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
802 struct edt_ft5x06_ts_data
*tsdata
,
805 u8 rdbuf
[EDT_NAME_LEN
];
808 char *model_name
= tsdata
->name
;
810 /* see what we find if we assume it is a M06 *
811 * if we get less than EDT_NAME_LEN, we don't want
812 * to have garbage in there
814 memset(rdbuf
, 0, sizeof(rdbuf
));
815 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xBB",
816 EDT_NAME_LEN
- 1, rdbuf
);
820 /* Probe content for something consistent.
821 * M06 starts with a response byte, M12 gives the data directly.
822 * M09/Generic does not provide model number information.
824 if (!strncasecmp(rdbuf
+ 1, "EP0", 3)) {
825 tsdata
->version
= EDT_M06
;
827 /* remove last '$' end marker */
828 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
829 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
830 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
832 /* look for Model/Version separator */
833 p
= strchr(rdbuf
, '*');
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
;
841 /* remove last '$' end marker */
842 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
843 if (rdbuf
[EDT_NAME_LEN
- 3] == '$')
844 rdbuf
[EDT_NAME_LEN
- 3] = '\0';
846 /* look for Model/Version separator */
847 p
= strchr(rdbuf
, '*');
850 strlcpy(model_name
, rdbuf
, EDT_NAME_LEN
);
851 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
853 /* If it is not an EDT M06/M12 touchscreen, then the model
854 * detection is a bit hairy. The different ft5x06
855 * firmares around don't reliably implement the
856 * identification registers. Well, we'll take a shot.
858 * The main difference between generic focaltec based
859 * touches and EDT M09 is that we know how to retrieve
860 * the max coordinates for the latter.
862 tsdata
->version
= GENERIC_FT
;
864 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
869 strlcpy(fw_version
, rdbuf
, 2);
871 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
876 /* This "model identification" is not exact. Unfortunately
877 * not all firmwares for the ft5x06 put useful values in
878 * the identification registers.
881 case 0x35: /* EDT EP0350M09 */
882 case 0x43: /* EDT EP0430M09 */
883 case 0x50: /* EDT EP0500M09 */
884 case 0x57: /* EDT EP0570M09 */
885 case 0x70: /* EDT EP0700M09 */
886 tsdata
->version
= EDT_M09
;
887 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
888 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
890 case 0xa1: /* EDT EP1010ML00 */
891 tsdata
->version
= EDT_M09
;
892 snprintf(model_name
, EDT_NAME_LEN
, "EP%i%i0ML00",
893 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
895 case 0x5a: /* Solomon Goldentek Display */
896 snprintf(model_name
, EDT_NAME_LEN
, "GKTW50SCED1R0");
898 case 0x59: /* Evervision Display with FT5xx6 TS */
899 tsdata
->version
= EV_FT
;
900 error
= edt_ft5x06_ts_readwrite(client
, 1, "\x53",
904 strlcpy(fw_version
, rdbuf
, 1);
905 snprintf(model_name
, EDT_NAME_LEN
,
906 "EVERVISION-FT5726NEi");
909 snprintf(model_name
, EDT_NAME_LEN
,
910 "generic ft5x06 (%02x)",
919 static void edt_ft5x06_ts_get_defaults(struct device
*dev
,
920 struct edt_ft5x06_ts_data
*tsdata
)
922 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
926 error
= device_property_read_u32(dev
, "threshold", &val
);
928 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
, val
);
929 tsdata
->threshold
= val
;
932 error
= device_property_read_u32(dev
, "gain", &val
);
934 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
, val
);
938 error
= device_property_read_u32(dev
, "offset", &val
);
940 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
, val
);
941 tsdata
->offset
= val
;
944 error
= device_property_read_u32(dev
, "offset-x", &val
);
946 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_x
, val
);
947 tsdata
->offset_x
= val
;
950 error
= device_property_read_u32(dev
, "offset-y", &val
);
952 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_y
, val
);
953 tsdata
->offset_y
= val
;
958 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
960 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
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
)
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
);
991 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
993 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
995 switch (tsdata
->version
) {
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
;
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
;
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
;
1031 /* this is a guesswork */
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
;
1041 static void edt_ft5x06_disable_regulator(void *arg
)
1043 struct edt_ft5x06_ts_data
*data
= arg
;
1045 regulator_disable(data
->vcc
);
1048 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
1049 const struct i2c_device_id
*id
)
1051 const struct edt_i2c_chip_data
*chip_data
;
1052 struct edt_ft5x06_ts_data
*tsdata
;
1053 struct input_dev
*input
;
1054 unsigned long irq_flags
;
1056 char fw_version
[EDT_NAME_LEN
];
1058 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
1060 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
1062 dev_err(&client
->dev
, "failed to allocate driver data.\n");
1066 chip_data
= device_get_match_data(&client
->dev
);
1068 chip_data
= (const struct edt_i2c_chip_data
*)id
->driver_data
;
1069 if (!chip_data
|| !chip_data
->max_support_points
) {
1070 dev_err(&client
->dev
, "invalid or missing chip data\n");
1074 tsdata
->max_support_points
= chip_data
->max_support_points
;
1076 tsdata
->vcc
= devm_regulator_get(&client
->dev
, "vcc");
1077 if (IS_ERR(tsdata
->vcc
)) {
1078 error
= PTR_ERR(tsdata
->vcc
);
1079 if (error
!= -EPROBE_DEFER
)
1080 dev_err(&client
->dev
,
1081 "failed to request regulator: %d\n", error
);
1085 error
= regulator_enable(tsdata
->vcc
);
1087 dev_err(&client
->dev
, "failed to enable vcc: %d\n", error
);
1091 error
= devm_add_action_or_reset(&client
->dev
,
1092 edt_ft5x06_disable_regulator
,
1097 tsdata
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
1098 "reset", GPIOD_OUT_HIGH
);
1099 if (IS_ERR(tsdata
->reset_gpio
)) {
1100 error
= PTR_ERR(tsdata
->reset_gpio
);
1101 dev_err(&client
->dev
,
1102 "Failed to request GPIO reset pin, error %d\n", error
);
1106 tsdata
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
1107 "wake", GPIOD_OUT_LOW
);
1108 if (IS_ERR(tsdata
->wake_gpio
)) {
1109 error
= PTR_ERR(tsdata
->wake_gpio
);
1110 dev_err(&client
->dev
,
1111 "Failed to request GPIO wake pin, error %d\n", error
);
1115 if (tsdata
->wake_gpio
) {
1116 usleep_range(5000, 6000);
1117 gpiod_set_value_cansleep(tsdata
->wake_gpio
, 1);
1120 if (tsdata
->reset_gpio
) {
1121 usleep_range(5000, 6000);
1122 gpiod_set_value_cansleep(tsdata
->reset_gpio
, 0);
1126 input
= devm_input_allocate_device(&client
->dev
);
1128 dev_err(&client
->dev
, "failed to allocate input device.\n");
1132 mutex_init(&tsdata
->mutex
);
1133 tsdata
->client
= client
;
1134 tsdata
->input
= input
;
1135 tsdata
->factory_mode
= false;
1137 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
1139 dev_err(&client
->dev
, "touchscreen probe failed\n");
1143 edt_ft5x06_ts_set_regs(tsdata
);
1144 edt_ft5x06_ts_get_defaults(&client
->dev
, tsdata
);
1145 edt_ft5x06_ts_get_parameters(tsdata
);
1147 dev_dbg(&client
->dev
,
1148 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1149 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
1151 input
->name
= tsdata
->name
;
1152 input
->id
.bustype
= BUS_I2C
;
1153 input
->dev
.parent
= &client
->dev
;
1155 if (tsdata
->version
== EDT_M06
||
1156 tsdata
->version
== EDT_M09
||
1157 tsdata
->version
== EDT_M12
) {
1158 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1159 0, tsdata
->num_x
* 64 - 1, 0, 0);
1160 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1161 0, tsdata
->num_y
* 64 - 1, 0, 0);
1163 /* Unknown maximum values. Specify via devicetree */
1164 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1166 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1170 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
1172 error
= input_mt_init_slots(input
, tsdata
->max_support_points
,
1175 dev_err(&client
->dev
, "Unable to init MT slots.\n");
1179 i2c_set_clientdata(client
, tsdata
);
1181 irq_flags
= irq_get_trigger_type(client
->irq
);
1182 if (irq_flags
== IRQF_TRIGGER_NONE
)
1183 irq_flags
= IRQF_TRIGGER_FALLING
;
1184 irq_flags
|= IRQF_ONESHOT
;
1186 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1187 NULL
, edt_ft5x06_ts_isr
, irq_flags
,
1188 client
->name
, tsdata
);
1190 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
1194 error
= devm_device_add_group(&client
->dev
, &edt_ft5x06_attr_group
);
1198 error
= input_register_device(input
);
1202 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1203 device_init_wakeup(&client
->dev
, 1);
1205 dev_dbg(&client
->dev
,
1206 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1208 tsdata
->wake_gpio
? desc_to_gpio(tsdata
->wake_gpio
) : -1,
1209 tsdata
->reset_gpio
? desc_to_gpio(tsdata
->reset_gpio
) : -1);
1214 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1216 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1218 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1223 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1225 struct i2c_client
*client
= to_i2c_client(dev
);
1227 if (device_may_wakeup(dev
))
1228 enable_irq_wake(client
->irq
);
1233 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1235 struct i2c_client
*client
= to_i2c_client(dev
);
1237 if (device_may_wakeup(dev
))
1238 disable_irq_wake(client
->irq
);
1243 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1244 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1246 static const struct edt_i2c_chip_data edt_ft5x06_data
= {
1247 .max_support_points
= 5,
1250 static const struct edt_i2c_chip_data edt_ft5506_data
= {
1251 .max_support_points
= 10,
1254 static const struct edt_i2c_chip_data edt_ft6236_data
= {
1255 .max_support_points
= 2,
1258 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1259 { .name
= "edt-ft5x06", .driver_data
= (long)&edt_ft5x06_data
},
1260 { .name
= "edt-ft5506", .driver_data
= (long)&edt_ft5506_data
},
1261 { .name
= "ev-ft5726", .driver_data
= (long)&edt_ft5506_data
},
1262 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1263 { .name
= "ft6236", .driver_data
= (long)&edt_ft6236_data
},
1266 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1268 static const struct of_device_id edt_ft5x06_of_match
[] = {
1269 { .compatible
= "edt,edt-ft5206", .data
= &edt_ft5x06_data
},
1270 { .compatible
= "edt,edt-ft5306", .data
= &edt_ft5x06_data
},
1271 { .compatible
= "edt,edt-ft5406", .data
= &edt_ft5x06_data
},
1272 { .compatible
= "edt,edt-ft5506", .data
= &edt_ft5506_data
},
1273 { .compatible
= "evervision,ev-ft5726", .data
= &edt_ft5506_data
},
1274 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1275 { .compatible
= "focaltech,ft6236", .data
= &edt_ft6236_data
},
1278 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1280 static struct i2c_driver edt_ft5x06_ts_driver
= {
1282 .name
= "edt_ft5x06",
1283 .of_match_table
= edt_ft5x06_of_match
,
1284 .pm
= &edt_ft5x06_ts_pm_ops
,
1286 .id_table
= edt_ft5x06_ts_id
,
1287 .probe
= edt_ft5x06_ts_probe
,
1288 .remove
= edt_ft5x06_ts_remove
,
1291 module_i2c_driver(edt_ft5x06_ts_driver
);
1293 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1294 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1295 MODULE_LICENSE("GPL v2");