2 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
3 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
4 * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This is a driver for the EDT "Polytouch" family of touch controllers
22 * based on the FocalTech FT5x06 line of chips.
24 * Development of this driver has been sponsored by Glyn:
25 * http://www.glyn.com/Products/Displays
28 #include <linux/module.h>
29 #include <linux/ratelimit.h>
30 #include <linux/irq.h>
31 #include <linux/interrupt.h>
32 #include <linux/input.h>
33 #include <linux/i2c.h>
34 #include <linux/kernel.h>
35 #include <linux/uaccess.h>
36 #include <linux/delay.h>
37 #include <linux/debugfs.h>
38 #include <linux/slab.h>
39 #include <linux/gpio/consumer.h>
40 #include <linux/input/mt.h>
41 #include <linux/input/touchscreen.h>
42 #include <linux/of_device.h>
44 #define WORK_REGISTER_THRESHOLD 0x00
45 #define WORK_REGISTER_REPORT_RATE 0x08
46 #define WORK_REGISTER_GAIN 0x30
47 #define WORK_REGISTER_OFFSET 0x31
48 #define WORK_REGISTER_NUM_X 0x33
49 #define WORK_REGISTER_NUM_Y 0x34
51 #define M09_REGISTER_THRESHOLD 0x80
52 #define M09_REGISTER_GAIN 0x92
53 #define M09_REGISTER_OFFSET 0x93
54 #define M09_REGISTER_NUM_X 0x94
55 #define M09_REGISTER_NUM_Y 0x95
57 #define EV_REGISTER_THRESHOLD 0x40
58 #define EV_REGISTER_GAIN 0x41
59 #define EV_REGISTER_OFFSET_Y 0x45
60 #define EV_REGISTER_OFFSET_X 0x46
62 #define NO_REGISTER 0xff
64 #define WORK_REGISTER_OPMODE 0x3c
65 #define FACTORY_REGISTER_OPMODE 0x01
67 #define TOUCH_EVENT_DOWN 0x00
68 #define TOUCH_EVENT_UP 0x01
69 #define TOUCH_EVENT_ON 0x02
70 #define TOUCH_EVENT_RESERVED 0x03
72 #define EDT_NAME_LEN 23
73 #define EDT_SWITCH_MODE_RETRIES 10
74 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
75 #define EDT_RAW_DATA_RETRIES 100
76 #define EDT_RAW_DATA_DELAY 1000 /* usec */
97 struct edt_ft5x06_ts_data
{
98 struct i2c_client
*client
;
99 struct input_dev
*input
;
100 struct touchscreen_properties prop
;
104 struct gpio_desc
*reset_gpio
;
105 struct gpio_desc
*wake_gpio
;
107 #if defined(CONFIG_DEBUG_FS)
108 struct dentry
*debug_dir
;
121 int max_support_points
;
123 char name
[EDT_NAME_LEN
];
125 struct edt_reg_addr reg_addr
;
126 enum edt_ver version
;
129 struct edt_i2c_chip_data
{
130 int max_support_points
;
133 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
134 u16 wr_len
, u8
*wr_buf
,
135 u16 rd_len
, u8
*rd_buf
)
137 struct i2c_msg wrmsg
[2];
142 wrmsg
[i
].addr
= client
->addr
;
144 wrmsg
[i
].len
= wr_len
;
145 wrmsg
[i
].buf
= wr_buf
;
149 wrmsg
[i
].addr
= client
->addr
;
150 wrmsg
[i
].flags
= I2C_M_RD
;
151 wrmsg
[i
].len
= rd_len
;
152 wrmsg
[i
].buf
= rd_buf
;
156 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
165 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
171 for (i
= 0; i
< buflen
- 1; i
++)
174 if (crc
!= buf
[buflen
-1]) {
175 dev_err_ratelimited(&tsdata
->client
->dev
,
176 "crc error: 0x%02x expected, got 0x%02x\n",
184 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
186 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
187 struct device
*dev
= &tsdata
->client
->dev
;
190 int i
, type
, x
, y
, id
;
191 int offset
, tplen
, datalen
, crclen
;
194 switch (tsdata
->version
) {
196 cmd
= 0xf9; /* tell the controller to send touch data */
197 offset
= 5; /* where the actual touch data starts */
198 tplen
= 4; /* data comes in so called frames */
199 crclen
= 1; /* length of the crc data */
216 memset(rdbuf
, 0, sizeof(rdbuf
));
217 datalen
= tplen
* tsdata
->max_support_points
+ offset
+ crclen
;
219 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
223 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
228 /* M09/M12 does not send header or CRC */
229 if (tsdata
->version
== EDT_M06
) {
230 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
231 rdbuf
[2] != datalen
) {
232 dev_err_ratelimited(dev
,
233 "Unexpected header: %02x%02x%02x!\n",
234 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
238 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
242 for (i
= 0; i
< tsdata
->max_support_points
; i
++) {
243 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
247 /* ignore Reserved events */
248 if (type
== TOUCH_EVENT_RESERVED
)
251 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
252 if (tsdata
->version
== EDT_M06
&& type
== TOUCH_EVENT_DOWN
)
255 x
= ((buf
[0] << 8) | buf
[1]) & 0x0fff;
256 y
= ((buf
[2] << 8) | buf
[3]) & 0x0fff;
257 /* The FT5x26 send the y coordinate first */
258 if (tsdata
->version
== EV_FT
)
261 id
= (buf
[2] >> 4) & 0x0f;
262 down
= type
!= TOUCH_EVENT_UP
;
264 input_mt_slot(tsdata
->input
, id
);
265 input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
, down
);
270 touchscreen_report_pos(tsdata
->input
, &tsdata
->prop
, x
, y
,
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],
342 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
355 struct edt_ft5x06_attribute
{
356 struct device_attribute dattr
;
365 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
366 _limit_low, _limit_high) \
367 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
368 .dattr = __ATTR(_field, _mode, \
369 edt_ft5x06_setting_show, \
370 edt_ft5x06_setting_store), \
371 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
372 .addr_m06 = _addr_m06, \
373 .addr_m09 = _addr_m09, \
374 .addr_ev = _addr_ev, \
375 .limit_low = _limit_low, \
376 .limit_high = _limit_high, \
379 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
380 struct device_attribute
*dattr
,
383 struct i2c_client
*client
= to_i2c_client(dev
);
384 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
385 struct edt_ft5x06_attribute
*attr
=
386 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
387 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
393 mutex_lock(&tsdata
->mutex
);
395 if (tsdata
->factory_mode
) {
400 switch (tsdata
->version
) {
402 addr
= attr
->addr_m06
;
408 addr
= attr
->addr_m09
;
412 addr
= attr
->addr_ev
;
420 if (addr
!= NO_REGISTER
) {
421 val
= edt_ft5x06_register_read(tsdata
, addr
);
424 dev_err(&tsdata
->client
->dev
,
425 "Failed to fetch attribute %s, error %d\n",
426 dattr
->attr
.name
, error
);
434 dev_warn(&tsdata
->client
->dev
,
435 "%s: read (%d) and stored value (%d) differ\n",
436 dattr
->attr
.name
, val
, *field
);
440 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
442 mutex_unlock(&tsdata
->mutex
);
443 return error
?: count
;
446 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
447 struct device_attribute
*dattr
,
448 const char *buf
, size_t count
)
450 struct i2c_client
*client
= to_i2c_client(dev
);
451 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
452 struct edt_ft5x06_attribute
*attr
=
453 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
454 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
459 mutex_lock(&tsdata
->mutex
);
461 if (tsdata
->factory_mode
) {
466 error
= kstrtouint(buf
, 0, &val
);
470 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
475 switch (tsdata
->version
) {
477 addr
= attr
->addr_m06
;
483 addr
= attr
->addr_m09
;
487 addr
= attr
->addr_ev
;
495 if (addr
!= NO_REGISTER
) {
496 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
498 dev_err(&tsdata
->client
->dev
,
499 "Failed to update attribute %s, error: %d\n",
500 dattr
->attr
.name
, error
);
507 mutex_unlock(&tsdata
->mutex
);
508 return error
?: count
;
511 /* m06, m09: range 0-31, m12: range 0-5 */
512 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
513 M09_REGISTER_GAIN
, EV_REGISTER_GAIN
, 0, 31);
514 /* m06, m09: range 0-31, m12: range 0-16 */
515 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
516 M09_REGISTER_OFFSET
, NO_REGISTER
, 0, 31);
517 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
518 static EDT_ATTR(offset_x
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
519 EV_REGISTER_OFFSET_X
, 0, 80);
520 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
521 static EDT_ATTR(offset_y
, S_IWUSR
| S_IRUGO
, NO_REGISTER
, NO_REGISTER
,
522 EV_REGISTER_OFFSET_Y
, 0, 80);
523 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
524 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
525 M09_REGISTER_THRESHOLD
, EV_REGISTER_THRESHOLD
, 0, 255);
526 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
527 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
528 NO_REGISTER
, NO_REGISTER
, 0, 255);
530 static struct attribute
*edt_ft5x06_attrs
[] = {
531 &edt_ft5x06_attr_gain
.dattr
.attr
,
532 &edt_ft5x06_attr_offset
.dattr
.attr
,
533 &edt_ft5x06_attr_offset_x
.dattr
.attr
,
534 &edt_ft5x06_attr_offset_y
.dattr
.attr
,
535 &edt_ft5x06_attr_threshold
.dattr
.attr
,
536 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
540 static const struct attribute_group edt_ft5x06_attr_group
= {
541 .attrs
= edt_ft5x06_attrs
,
544 #ifdef CONFIG_DEBUG_FS
545 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
547 struct i2c_client
*client
= tsdata
->client
;
548 int retries
= EDT_SWITCH_MODE_RETRIES
;
552 if (tsdata
->version
!= EDT_M06
) {
553 dev_err(&client
->dev
,
554 "No factory mode support for non-M06 devices\n");
558 disable_irq(client
->irq
);
560 if (!tsdata
->raw_buffer
) {
561 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
563 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
564 if (!tsdata
->raw_buffer
) {
570 /* mode register is 0x3c when in the work mode */
571 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
573 dev_err(&client
->dev
,
574 "failed to switch to factory mode, error %d\n", error
);
578 tsdata
->factory_mode
= true;
580 mdelay(EDT_SWITCH_MODE_DELAY
);
581 /* mode register is 0x01 when in factory mode */
582 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
585 } while (--retries
> 0);
588 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
589 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
597 kfree(tsdata
->raw_buffer
);
598 tsdata
->raw_buffer
= NULL
;
599 tsdata
->factory_mode
= false;
600 enable_irq(client
->irq
);
605 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
607 struct i2c_client
*client
= tsdata
->client
;
608 int retries
= EDT_SWITCH_MODE_RETRIES
;
609 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
613 /* mode register is 0x01 when in the factory mode */
614 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
616 dev_err(&client
->dev
,
617 "failed to switch to work mode, error: %d\n", error
);
621 tsdata
->factory_mode
= false;
624 mdelay(EDT_SWITCH_MODE_DELAY
);
625 /* mode register is 0x01 when in factory mode */
626 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
629 } while (--retries
> 0);
632 dev_err(&client
->dev
, "not in work mode after %dms.\n",
633 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
634 tsdata
->factory_mode
= true;
638 kfree(tsdata
->raw_buffer
);
639 tsdata
->raw_buffer
= NULL
;
641 /* restore parameters */
642 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
644 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
646 if (reg_addr
->reg_offset
!= NO_REGISTER
)
647 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
649 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
650 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_x
,
652 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
653 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_y
,
655 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
656 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
657 tsdata
->report_rate
);
659 enable_irq(client
->irq
);
664 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
666 struct edt_ft5x06_ts_data
*tsdata
= data
;
668 *mode
= tsdata
->factory_mode
;
673 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
675 struct edt_ft5x06_ts_data
*tsdata
= data
;
681 mutex_lock(&tsdata
->mutex
);
683 if (mode
!= tsdata
->factory_mode
) {
684 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
685 edt_ft5x06_work_mode(tsdata
);
688 mutex_unlock(&tsdata
->mutex
);
693 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
694 edt_ft5x06_debugfs_mode_set
, "%llu\n");
696 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
697 char __user
*buf
, size_t count
, loff_t
*off
)
699 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
700 struct i2c_client
*client
= tsdata
->client
;
701 int retries
= EDT_RAW_DATA_RETRIES
;
708 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
711 mutex_lock(&tsdata
->mutex
);
713 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
718 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
720 dev_dbg(&client
->dev
,
721 "failed to write 0x08 register, error %d\n", error
);
726 usleep_range(EDT_RAW_DATA_DELAY
, EDT_RAW_DATA_DELAY
+ 100);
727 val
= edt_ft5x06_register_read(tsdata
, 0x08);
730 } while (--retries
> 0);
734 dev_dbg(&client
->dev
,
735 "failed to read 0x08 register, error %d\n", error
);
740 dev_dbg(&client
->dev
,
741 "timed out waiting for register to settle\n");
746 rdbuf
= tsdata
->raw_buffer
;
747 colbytes
= tsdata
->num_y
* sizeof(u16
);
751 for (i
= 0; i
< tsdata
->num_x
; i
++) {
752 wrbuf
[2] = i
; /* column index */
753 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
754 sizeof(wrbuf
), wrbuf
,
762 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
763 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
770 mutex_unlock(&tsdata
->mutex
);
771 return error
?: read
;
774 static const struct file_operations debugfs_raw_data_fops
= {
776 .read
= edt_ft5x06_debugfs_raw_data_read
,
780 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
781 const char *debugfs_name
)
783 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
784 if (!tsdata
->debug_dir
)
787 debugfs_create_u16("num_x", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_x
);
788 debugfs_create_u16("num_y", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_y
);
790 debugfs_create_file("mode", S_IRUSR
| S_IWUSR
,
791 tsdata
->debug_dir
, tsdata
, &debugfs_mode_fops
);
792 debugfs_create_file("raw_data", S_IRUSR
,
793 tsdata
->debug_dir
, tsdata
, &debugfs_raw_data_fops
);
797 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
799 debugfs_remove_recursive(tsdata
->debug_dir
);
800 kfree(tsdata
->raw_buffer
);
806 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
807 const char *debugfs_name
)
812 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
816 #endif /* CONFIG_DEBUGFS */
818 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
819 struct edt_ft5x06_ts_data
*tsdata
,
822 u8 rdbuf
[EDT_NAME_LEN
];
825 char *model_name
= tsdata
->name
;
827 /* see what we find if we assume it is a M06 *
828 * if we get less than EDT_NAME_LEN, we don't want
829 * to have garbage in there
831 memset(rdbuf
, 0, sizeof(rdbuf
));
832 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xBB",
833 EDT_NAME_LEN
- 1, rdbuf
);
837 /* Probe content for something consistent.
838 * M06 starts with a response byte, M12 gives the data directly.
839 * M09/Generic does not provide model number information.
841 if (!strncasecmp(rdbuf
+ 1, "EP0", 3)) {
842 tsdata
->version
= EDT_M06
;
844 /* remove last '$' end marker */
845 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
846 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
847 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
849 /* look for Model/Version separator */
850 p
= strchr(rdbuf
, '*');
853 strlcpy(model_name
, rdbuf
+ 1, EDT_NAME_LEN
);
854 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
855 } else if (!strncasecmp(rdbuf
, "EP0", 3)) {
856 tsdata
->version
= EDT_M12
;
858 /* remove last '$' end marker */
859 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
860 if (rdbuf
[EDT_NAME_LEN
- 3] == '$')
861 rdbuf
[EDT_NAME_LEN
- 3] = '\0';
863 /* look for Model/Version separator */
864 p
= strchr(rdbuf
, '*');
867 strlcpy(model_name
, rdbuf
, EDT_NAME_LEN
);
868 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
870 /* If it is not an EDT M06/M12 touchscreen, then the model
871 * detection is a bit hairy. The different ft5x06
872 * firmares around don't reliably implement the
873 * identification registers. Well, we'll take a shot.
875 * The main difference between generic focaltec based
876 * touches and EDT M09 is that we know how to retrieve
877 * the max coordinates for the latter.
879 tsdata
->version
= GENERIC_FT
;
881 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
886 strlcpy(fw_version
, rdbuf
, 2);
888 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
893 /* This "model identification" is not exact. Unfortunately
894 * not all firmwares for the ft5x06 put useful values in
895 * the identification registers.
898 case 0x35: /* EDT EP0350M09 */
899 case 0x43: /* EDT EP0430M09 */
900 case 0x50: /* EDT EP0500M09 */
901 case 0x57: /* EDT EP0570M09 */
902 case 0x70: /* EDT EP0700M09 */
903 tsdata
->version
= EDT_M09
;
904 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
905 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
907 case 0xa1: /* EDT EP1010ML00 */
908 tsdata
->version
= EDT_M09
;
909 snprintf(model_name
, EDT_NAME_LEN
, "EP%i%i0ML00",
910 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
912 case 0x5a: /* Solomon Goldentek Display */
913 snprintf(model_name
, EDT_NAME_LEN
, "GKTW50SCED1R0");
915 case 0x59: /* Evervision Display with FT5xx6 TS */
916 tsdata
->version
= EV_FT
;
917 error
= edt_ft5x06_ts_readwrite(client
, 1, "\x53",
921 strlcpy(fw_version
, rdbuf
, 1);
922 snprintf(model_name
, EDT_NAME_LEN
,
923 "EVERVISION-FT5726NEi");
926 snprintf(model_name
, EDT_NAME_LEN
,
927 "generic ft5x06 (%02x)",
936 static void edt_ft5x06_ts_get_defaults(struct device
*dev
,
937 struct edt_ft5x06_ts_data
*tsdata
)
939 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
943 error
= device_property_read_u32(dev
, "threshold", &val
);
945 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
, val
);
946 tsdata
->threshold
= val
;
949 error
= device_property_read_u32(dev
, "gain", &val
);
951 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
, val
);
955 error
= device_property_read_u32(dev
, "offset", &val
);
957 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
, val
);
958 tsdata
->offset
= val
;
961 error
= device_property_read_u32(dev
, "offset-x", &val
);
963 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_x
, val
);
964 tsdata
->offset_x
= val
;
967 error
= device_property_read_u32(dev
, "offset-y", &val
);
969 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset_y
, val
);
970 tsdata
->offset_y
= val
;
975 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
977 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
979 tsdata
->threshold
= edt_ft5x06_register_read(tsdata
,
980 reg_addr
->reg_threshold
);
981 tsdata
->gain
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_gain
);
982 if (reg_addr
->reg_offset
!= NO_REGISTER
)
984 edt_ft5x06_register_read(tsdata
, reg_addr
->reg_offset
);
985 if (reg_addr
->reg_offset_x
!= NO_REGISTER
)
986 tsdata
->offset_x
= edt_ft5x06_register_read(tsdata
,
987 reg_addr
->reg_offset_x
);
988 if (reg_addr
->reg_offset_y
!= NO_REGISTER
)
989 tsdata
->offset_y
= edt_ft5x06_register_read(tsdata
,
990 reg_addr
->reg_offset_y
);
991 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
992 tsdata
->report_rate
= edt_ft5x06_register_read(tsdata
,
993 reg_addr
->reg_report_rate
);
994 if (tsdata
->version
== EDT_M06
||
995 tsdata
->version
== EDT_M09
||
996 tsdata
->version
== EDT_M12
) {
997 tsdata
->num_x
= edt_ft5x06_register_read(tsdata
,
998 reg_addr
->reg_num_x
);
999 tsdata
->num_y
= edt_ft5x06_register_read(tsdata
,
1000 reg_addr
->reg_num_y
);
1008 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
1010 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
1012 switch (tsdata
->version
) {
1014 reg_addr
->reg_threshold
= WORK_REGISTER_THRESHOLD
;
1015 reg_addr
->reg_report_rate
= WORK_REGISTER_REPORT_RATE
;
1016 reg_addr
->reg_gain
= WORK_REGISTER_GAIN
;
1017 reg_addr
->reg_offset
= WORK_REGISTER_OFFSET
;
1018 reg_addr
->reg_offset_x
= NO_REGISTER
;
1019 reg_addr
->reg_offset_y
= NO_REGISTER
;
1020 reg_addr
->reg_num_x
= WORK_REGISTER_NUM_X
;
1021 reg_addr
->reg_num_y
= WORK_REGISTER_NUM_Y
;
1026 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
1027 reg_addr
->reg_report_rate
= NO_REGISTER
;
1028 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
1029 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
1030 reg_addr
->reg_offset_x
= NO_REGISTER
;
1031 reg_addr
->reg_offset_y
= NO_REGISTER
;
1032 reg_addr
->reg_num_x
= M09_REGISTER_NUM_X
;
1033 reg_addr
->reg_num_y
= M09_REGISTER_NUM_Y
;
1037 reg_addr
->reg_threshold
= EV_REGISTER_THRESHOLD
;
1038 reg_addr
->reg_gain
= EV_REGISTER_GAIN
;
1039 reg_addr
->reg_offset
= NO_REGISTER
;
1040 reg_addr
->reg_offset_x
= EV_REGISTER_OFFSET_X
;
1041 reg_addr
->reg_offset_y
= EV_REGISTER_OFFSET_Y
;
1042 reg_addr
->reg_num_x
= NO_REGISTER
;
1043 reg_addr
->reg_num_y
= NO_REGISTER
;
1044 reg_addr
->reg_report_rate
= NO_REGISTER
;
1048 /* this is a guesswork */
1049 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
1050 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
1051 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
1052 reg_addr
->reg_offset_x
= NO_REGISTER
;
1053 reg_addr
->reg_offset_y
= NO_REGISTER
;
1058 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
1059 const struct i2c_device_id
*id
)
1061 const struct edt_i2c_chip_data
*chip_data
;
1062 struct edt_ft5x06_ts_data
*tsdata
;
1063 struct input_dev
*input
;
1064 unsigned long irq_flags
;
1066 char fw_version
[EDT_NAME_LEN
];
1068 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
1070 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
1072 dev_err(&client
->dev
, "failed to allocate driver data.\n");
1076 chip_data
= of_device_get_match_data(&client
->dev
);
1078 chip_data
= (const struct edt_i2c_chip_data
*)id
->driver_data
;
1079 if (!chip_data
|| !chip_data
->max_support_points
) {
1080 dev_err(&client
->dev
, "invalid or missing chip data\n");
1084 tsdata
->max_support_points
= chip_data
->max_support_points
;
1086 tsdata
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
1087 "reset", GPIOD_OUT_HIGH
);
1088 if (IS_ERR(tsdata
->reset_gpio
)) {
1089 error
= PTR_ERR(tsdata
->reset_gpio
);
1090 dev_err(&client
->dev
,
1091 "Failed to request GPIO reset pin, error %d\n", error
);
1095 tsdata
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
1096 "wake", GPIOD_OUT_LOW
);
1097 if (IS_ERR(tsdata
->wake_gpio
)) {
1098 error
= PTR_ERR(tsdata
->wake_gpio
);
1099 dev_err(&client
->dev
,
1100 "Failed to request GPIO wake pin, error %d\n", error
);
1104 if (tsdata
->wake_gpio
) {
1105 usleep_range(5000, 6000);
1106 gpiod_set_value_cansleep(tsdata
->wake_gpio
, 1);
1109 if (tsdata
->reset_gpio
) {
1110 usleep_range(5000, 6000);
1111 gpiod_set_value_cansleep(tsdata
->reset_gpio
, 0);
1115 input
= devm_input_allocate_device(&client
->dev
);
1117 dev_err(&client
->dev
, "failed to allocate input device.\n");
1121 mutex_init(&tsdata
->mutex
);
1122 tsdata
->client
= client
;
1123 tsdata
->input
= input
;
1124 tsdata
->factory_mode
= false;
1126 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
1128 dev_err(&client
->dev
, "touchscreen probe failed\n");
1132 edt_ft5x06_ts_set_regs(tsdata
);
1133 edt_ft5x06_ts_get_defaults(&client
->dev
, tsdata
);
1134 edt_ft5x06_ts_get_parameters(tsdata
);
1136 dev_dbg(&client
->dev
,
1137 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1138 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
1140 input
->name
= tsdata
->name
;
1141 input
->id
.bustype
= BUS_I2C
;
1142 input
->dev
.parent
= &client
->dev
;
1144 if (tsdata
->version
== EDT_M06
||
1145 tsdata
->version
== EDT_M09
||
1146 tsdata
->version
== EDT_M12
) {
1147 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1148 0, tsdata
->num_x
* 64 - 1, 0, 0);
1149 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1150 0, tsdata
->num_y
* 64 - 1, 0, 0);
1152 /* Unknown maximum values. Specify via devicetree */
1153 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1155 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1159 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
1161 error
= input_mt_init_slots(input
, tsdata
->max_support_points
,
1164 dev_err(&client
->dev
, "Unable to init MT slots.\n");
1168 i2c_set_clientdata(client
, tsdata
);
1170 irq_flags
= irq_get_trigger_type(client
->irq
);
1171 if (irq_flags
== IRQF_TRIGGER_NONE
)
1172 irq_flags
= IRQF_TRIGGER_FALLING
;
1173 irq_flags
|= IRQF_ONESHOT
;
1175 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1176 NULL
, edt_ft5x06_ts_isr
, irq_flags
,
1177 client
->name
, tsdata
);
1179 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
1183 error
= devm_device_add_group(&client
->dev
, &edt_ft5x06_attr_group
);
1187 error
= input_register_device(input
);
1191 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1192 device_init_wakeup(&client
->dev
, 1);
1194 dev_dbg(&client
->dev
,
1195 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1197 tsdata
->wake_gpio
? desc_to_gpio(tsdata
->wake_gpio
) : -1,
1198 tsdata
->reset_gpio
? desc_to_gpio(tsdata
->reset_gpio
) : -1);
1203 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1205 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1207 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1212 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1214 struct i2c_client
*client
= to_i2c_client(dev
);
1216 if (device_may_wakeup(dev
))
1217 enable_irq_wake(client
->irq
);
1222 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1224 struct i2c_client
*client
= to_i2c_client(dev
);
1226 if (device_may_wakeup(dev
))
1227 disable_irq_wake(client
->irq
);
1232 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1233 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1235 static const struct edt_i2c_chip_data edt_ft5x06_data
= {
1236 .max_support_points
= 5,
1239 static const struct edt_i2c_chip_data edt_ft5506_data
= {
1240 .max_support_points
= 10,
1243 static const struct edt_i2c_chip_data edt_ft6236_data
= {
1244 .max_support_points
= 2,
1247 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1248 { .name
= "edt-ft5x06", .driver_data
= (long)&edt_ft5x06_data
},
1249 { .name
= "edt-ft5506", .driver_data
= (long)&edt_ft5506_data
},
1250 { .name
= "ev-ft5726", .driver_data
= (long)&edt_ft5506_data
},
1251 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1252 { .name
= "ft6236", .driver_data
= (long)&edt_ft6236_data
},
1255 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1258 static const struct of_device_id edt_ft5x06_of_match
[] = {
1259 { .compatible
= "edt,edt-ft5206", .data
= &edt_ft5x06_data
},
1260 { .compatible
= "edt,edt-ft5306", .data
= &edt_ft5x06_data
},
1261 { .compatible
= "edt,edt-ft5406", .data
= &edt_ft5x06_data
},
1262 { .compatible
= "edt,edt-ft5506", .data
= &edt_ft5506_data
},
1263 { .compatible
= "evervision,ev-ft5726", .data
= &edt_ft5506_data
},
1264 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1265 { .compatible
= "focaltech,ft6236", .data
= &edt_ft6236_data
},
1268 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1271 static struct i2c_driver edt_ft5x06_ts_driver
= {
1273 .name
= "edt_ft5x06",
1274 .of_match_table
= of_match_ptr(edt_ft5x06_of_match
),
1275 .pm
= &edt_ft5x06_ts_pm_ops
,
1277 .id_table
= edt_ft5x06_ts_id
,
1278 .probe
= edt_ft5x06_ts_probe
,
1279 .remove
= edt_ft5x06_ts_remove
,
1282 module_i2c_driver(edt_ft5x06_ts_driver
);
1284 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1285 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1286 MODULE_LICENSE("GPL");