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/uaccess.h>
35 #include <linux/delay.h>
36 #include <linux/debugfs.h>
37 #include <linux/slab.h>
38 #include <linux/gpio/consumer.h>
39 #include <linux/input/mt.h>
40 #include <linux/input/touchscreen.h>
41 #include <linux/of_device.h>
43 #define WORK_REGISTER_THRESHOLD 0x00
44 #define WORK_REGISTER_REPORT_RATE 0x08
45 #define WORK_REGISTER_GAIN 0x30
46 #define WORK_REGISTER_OFFSET 0x31
47 #define WORK_REGISTER_NUM_X 0x33
48 #define WORK_REGISTER_NUM_Y 0x34
50 #define M09_REGISTER_THRESHOLD 0x80
51 #define M09_REGISTER_GAIN 0x92
52 #define M09_REGISTER_OFFSET 0x93
53 #define M09_REGISTER_NUM_X 0x94
54 #define M09_REGISTER_NUM_Y 0x95
56 #define NO_REGISTER 0xff
58 #define WORK_REGISTER_OPMODE 0x3c
59 #define FACTORY_REGISTER_OPMODE 0x01
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 */
88 struct edt_ft5x06_ts_data
{
89 struct i2c_client
*client
;
90 struct input_dev
*input
;
91 struct touchscreen_properties prop
;
95 struct gpio_desc
*reset_gpio
;
96 struct gpio_desc
*wake_gpio
;
98 #if defined(CONFIG_DEBUG_FS)
99 struct dentry
*debug_dir
;
110 int max_support_points
;
112 char name
[EDT_NAME_LEN
];
114 struct edt_reg_addr reg_addr
;
115 enum edt_ver version
;
118 struct edt_i2c_chip_data
{
119 int max_support_points
;
122 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
123 u16 wr_len
, u8
*wr_buf
,
124 u16 rd_len
, u8
*rd_buf
)
126 struct i2c_msg wrmsg
[2];
131 wrmsg
[i
].addr
= client
->addr
;
133 wrmsg
[i
].len
= wr_len
;
134 wrmsg
[i
].buf
= wr_buf
;
138 wrmsg
[i
].addr
= client
->addr
;
139 wrmsg
[i
].flags
= I2C_M_RD
;
140 wrmsg
[i
].len
= rd_len
;
141 wrmsg
[i
].buf
= rd_buf
;
145 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
154 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
160 for (i
= 0; i
< buflen
- 1; i
++)
163 if (crc
!= buf
[buflen
-1]) {
164 dev_err_ratelimited(&tsdata
->client
->dev
,
165 "crc error: 0x%02x expected, got 0x%02x\n",
173 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
175 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
176 struct device
*dev
= &tsdata
->client
->dev
;
179 int i
, type
, x
, y
, id
;
180 int offset
, tplen
, datalen
, crclen
;
183 switch (tsdata
->version
) {
185 cmd
= 0xf9; /* tell the controller to send touch data */
186 offset
= 5; /* where the actual touch data starts */
187 tplen
= 4; /* data comes in so called frames */
188 crclen
= 1; /* length of the crc data */
204 memset(rdbuf
, 0, sizeof(rdbuf
));
205 datalen
= tplen
* tsdata
->max_support_points
+ offset
+ crclen
;
207 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
211 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
216 /* M09/M12 does not send header or CRC */
217 if (tsdata
->version
== EDT_M06
) {
218 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
219 rdbuf
[2] != datalen
) {
220 dev_err_ratelimited(dev
,
221 "Unexpected header: %02x%02x%02x!\n",
222 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
226 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
230 for (i
= 0; i
< tsdata
->max_support_points
; i
++) {
231 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
235 /* ignore Reserved events */
236 if (type
== TOUCH_EVENT_RESERVED
)
239 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
240 if (tsdata
->version
== EDT_M06
&& type
== TOUCH_EVENT_DOWN
)
243 x
= ((buf
[0] << 8) | buf
[1]) & 0x0fff;
244 y
= ((buf
[2] << 8) | buf
[3]) & 0x0fff;
245 id
= (buf
[2] >> 4) & 0x0f;
246 down
= type
!= TOUCH_EVENT_UP
;
248 input_mt_slot(tsdata
->input
, id
);
249 input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
, down
);
254 touchscreen_report_pos(tsdata
->input
, &tsdata
->prop
, x
, y
,
258 input_mt_report_pointer_emulation(tsdata
->input
, true);
259 input_sync(tsdata
->input
);
265 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data
*tsdata
,
270 switch (tsdata
->version
) {
272 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
273 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
275 wrbuf
[3] = wrbuf
[0] ^ wrbuf
[1] ^ wrbuf
[2];
276 return edt_ft5x06_ts_readwrite(tsdata
->client
, 4,
284 return edt_ft5x06_ts_readwrite(tsdata
->client
, 2,
292 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data
*tsdata
,
295 u8 wrbuf
[2], rdbuf
[2];
298 switch (tsdata
->version
) {
300 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
301 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
302 wrbuf
[1] |= tsdata
->factory_mode
? 0x80 : 0x40;
304 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 2, wrbuf
, 2,
309 if ((wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0]) != rdbuf
[1]) {
310 dev_err(&tsdata
->client
->dev
,
311 "crc error: 0x%02x expected, got 0x%02x\n",
312 wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0],
322 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
335 struct edt_ft5x06_attribute
{
336 struct device_attribute dattr
;
344 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
345 _limit_low, _limit_high) \
346 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
347 .dattr = __ATTR(_field, _mode, \
348 edt_ft5x06_setting_show, \
349 edt_ft5x06_setting_store), \
350 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
351 .addr_m06 = _addr_m06, \
352 .addr_m09 = _addr_m09, \
353 .limit_low = _limit_low, \
354 .limit_high = _limit_high, \
357 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
358 struct device_attribute
*dattr
,
361 struct i2c_client
*client
= to_i2c_client(dev
);
362 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
363 struct edt_ft5x06_attribute
*attr
=
364 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
365 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
371 mutex_lock(&tsdata
->mutex
);
373 if (tsdata
->factory_mode
) {
378 switch (tsdata
->version
) {
380 addr
= attr
->addr_m06
;
386 addr
= attr
->addr_m09
;
394 if (addr
!= NO_REGISTER
) {
395 val
= edt_ft5x06_register_read(tsdata
, addr
);
398 dev_err(&tsdata
->client
->dev
,
399 "Failed to fetch attribute %s, error %d\n",
400 dattr
->attr
.name
, error
);
408 dev_warn(&tsdata
->client
->dev
,
409 "%s: read (%d) and stored value (%d) differ\n",
410 dattr
->attr
.name
, val
, *field
);
414 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
416 mutex_unlock(&tsdata
->mutex
);
417 return error
?: count
;
420 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
421 struct device_attribute
*dattr
,
422 const char *buf
, size_t count
)
424 struct i2c_client
*client
= to_i2c_client(dev
);
425 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
426 struct edt_ft5x06_attribute
*attr
=
427 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
428 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
433 mutex_lock(&tsdata
->mutex
);
435 if (tsdata
->factory_mode
) {
440 error
= kstrtouint(buf
, 0, &val
);
444 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
449 switch (tsdata
->version
) {
451 addr
= attr
->addr_m06
;
457 addr
= attr
->addr_m09
;
465 if (addr
!= NO_REGISTER
) {
466 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
468 dev_err(&tsdata
->client
->dev
,
469 "Failed to update attribute %s, error: %d\n",
470 dattr
->attr
.name
, error
);
477 mutex_unlock(&tsdata
->mutex
);
478 return error
?: count
;
481 /* m06, m09: range 0-31, m12: range 0-5 */
482 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
483 M09_REGISTER_GAIN
, 0, 31);
484 /* m06, m09: range 0-31, m12: range 0-16 */
485 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
486 M09_REGISTER_OFFSET
, 0, 31);
487 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
488 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
489 M09_REGISTER_THRESHOLD
, 0, 255);
490 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
491 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
492 NO_REGISTER
, 0, 255);
494 static struct attribute
*edt_ft5x06_attrs
[] = {
495 &edt_ft5x06_attr_gain
.dattr
.attr
,
496 &edt_ft5x06_attr_offset
.dattr
.attr
,
497 &edt_ft5x06_attr_threshold
.dattr
.attr
,
498 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
502 static const struct attribute_group edt_ft5x06_attr_group
= {
503 .attrs
= edt_ft5x06_attrs
,
506 #ifdef CONFIG_DEBUG_FS
507 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
509 struct i2c_client
*client
= tsdata
->client
;
510 int retries
= EDT_SWITCH_MODE_RETRIES
;
514 if (tsdata
->version
!= EDT_M06
) {
515 dev_err(&client
->dev
,
516 "No factory mode support for non-M06 devices\n");
520 disable_irq(client
->irq
);
522 if (!tsdata
->raw_buffer
) {
523 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
525 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
526 if (!tsdata
->raw_buffer
) {
532 /* mode register is 0x3c when in the work mode */
533 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
535 dev_err(&client
->dev
,
536 "failed to switch to factory mode, error %d\n", error
);
540 tsdata
->factory_mode
= true;
542 mdelay(EDT_SWITCH_MODE_DELAY
);
543 /* mode register is 0x01 when in factory mode */
544 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
547 } while (--retries
> 0);
550 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
551 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
559 kfree(tsdata
->raw_buffer
);
560 tsdata
->raw_buffer
= NULL
;
561 tsdata
->factory_mode
= false;
562 enable_irq(client
->irq
);
567 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
569 struct i2c_client
*client
= tsdata
->client
;
570 int retries
= EDT_SWITCH_MODE_RETRIES
;
571 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
575 /* mode register is 0x01 when in the factory mode */
576 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
578 dev_err(&client
->dev
,
579 "failed to switch to work mode, error: %d\n", error
);
583 tsdata
->factory_mode
= false;
586 mdelay(EDT_SWITCH_MODE_DELAY
);
587 /* mode register is 0x01 when in factory mode */
588 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
591 } while (--retries
> 0);
594 dev_err(&client
->dev
, "not in work mode after %dms.\n",
595 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
596 tsdata
->factory_mode
= true;
600 kfree(tsdata
->raw_buffer
);
601 tsdata
->raw_buffer
= NULL
;
603 /* restore parameters */
604 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
606 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
608 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
610 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
611 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
612 tsdata
->report_rate
);
614 enable_irq(client
->irq
);
619 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
621 struct edt_ft5x06_ts_data
*tsdata
= data
;
623 *mode
= tsdata
->factory_mode
;
628 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
630 struct edt_ft5x06_ts_data
*tsdata
= data
;
636 mutex_lock(&tsdata
->mutex
);
638 if (mode
!= tsdata
->factory_mode
) {
639 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
640 edt_ft5x06_work_mode(tsdata
);
643 mutex_unlock(&tsdata
->mutex
);
648 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
649 edt_ft5x06_debugfs_mode_set
, "%llu\n");
651 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
652 char __user
*buf
, size_t count
, loff_t
*off
)
654 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
655 struct i2c_client
*client
= tsdata
->client
;
656 int retries
= EDT_RAW_DATA_RETRIES
;
663 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
666 mutex_lock(&tsdata
->mutex
);
668 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
673 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
675 dev_dbg(&client
->dev
,
676 "failed to write 0x08 register, error %d\n", error
);
681 usleep_range(EDT_RAW_DATA_DELAY
, EDT_RAW_DATA_DELAY
+ 100);
682 val
= edt_ft5x06_register_read(tsdata
, 0x08);
685 } while (--retries
> 0);
689 dev_dbg(&client
->dev
,
690 "failed to read 0x08 register, error %d\n", error
);
695 dev_dbg(&client
->dev
,
696 "timed out waiting for register to settle\n");
701 rdbuf
= tsdata
->raw_buffer
;
702 colbytes
= tsdata
->num_y
* sizeof(u16
);
706 for (i
= 0; i
< tsdata
->num_x
; i
++) {
707 wrbuf
[2] = i
; /* column index */
708 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
709 sizeof(wrbuf
), wrbuf
,
717 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
718 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
725 mutex_unlock(&tsdata
->mutex
);
726 return error
?: read
;
729 static const struct file_operations debugfs_raw_data_fops
= {
731 .read
= edt_ft5x06_debugfs_raw_data_read
,
735 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
736 const char *debugfs_name
)
738 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
739 if (!tsdata
->debug_dir
)
742 debugfs_create_u16("num_x", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_x
);
743 debugfs_create_u16("num_y", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_y
);
745 debugfs_create_file("mode", S_IRUSR
| S_IWUSR
,
746 tsdata
->debug_dir
, tsdata
, &debugfs_mode_fops
);
747 debugfs_create_file("raw_data", S_IRUSR
,
748 tsdata
->debug_dir
, tsdata
, &debugfs_raw_data_fops
);
752 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
754 debugfs_remove_recursive(tsdata
->debug_dir
);
755 kfree(tsdata
->raw_buffer
);
761 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
762 const char *debugfs_name
)
767 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
771 #endif /* CONFIG_DEBUGFS */
773 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
774 struct edt_ft5x06_ts_data
*tsdata
,
777 u8 rdbuf
[EDT_NAME_LEN
];
780 char *model_name
= tsdata
->name
;
782 /* see what we find if we assume it is a M06 *
783 * if we get less than EDT_NAME_LEN, we don't want
784 * to have garbage in there
786 memset(rdbuf
, 0, sizeof(rdbuf
));
787 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xBB",
788 EDT_NAME_LEN
- 1, rdbuf
);
792 /* Probe content for something consistent.
793 * M06 starts with a response byte, M12 gives the data directly.
794 * M09/Generic does not provide model number information.
796 if (!strncasecmp(rdbuf
+ 1, "EP0", 3)) {
797 tsdata
->version
= EDT_M06
;
799 /* remove last '$' end marker */
800 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
801 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
802 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
804 /* look for Model/Version separator */
805 p
= strchr(rdbuf
, '*');
808 strlcpy(model_name
, rdbuf
+ 1, EDT_NAME_LEN
);
809 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
810 } else if (!strncasecmp(rdbuf
, "EP0", 3)) {
811 tsdata
->version
= EDT_M12
;
813 /* remove last '$' end marker */
814 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
815 if (rdbuf
[EDT_NAME_LEN
- 3] == '$')
816 rdbuf
[EDT_NAME_LEN
- 3] = '\0';
818 /* look for Model/Version separator */
819 p
= strchr(rdbuf
, '*');
822 strlcpy(model_name
, rdbuf
, EDT_NAME_LEN
);
823 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
825 /* If it is not an EDT M06/M12 touchscreen, then the model
826 * detection is a bit hairy. The different ft5x06
827 * firmares around don't reliably implement the
828 * identification registers. Well, we'll take a shot.
830 * The main difference between generic focaltec based
831 * touches and EDT M09 is that we know how to retrieve
832 * the max coordinates for the latter.
834 tsdata
->version
= GENERIC_FT
;
836 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
841 strlcpy(fw_version
, rdbuf
, 2);
843 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
848 /* This "model identification" is not exact. Unfortunately
849 * not all firmwares for the ft5x06 put useful values in
850 * the identification registers.
853 case 0x35: /* EDT EP0350M09 */
854 case 0x43: /* EDT EP0430M09 */
855 case 0x50: /* EDT EP0500M09 */
856 case 0x57: /* EDT EP0570M09 */
857 case 0x70: /* EDT EP0700M09 */
858 tsdata
->version
= EDT_M09
;
859 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
860 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
862 case 0xa1: /* EDT EP1010ML00 */
863 tsdata
->version
= EDT_M09
;
864 snprintf(model_name
, EDT_NAME_LEN
, "EP%i%i0ML00",
865 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
867 case 0x5a: /* Solomon Goldentek Display */
868 snprintf(model_name
, EDT_NAME_LEN
, "GKTW50SCED1R0");
871 snprintf(model_name
, EDT_NAME_LEN
,
872 "generic ft5x06 (%02x)",
881 static void edt_ft5x06_ts_get_defaults(struct device
*dev
,
882 struct edt_ft5x06_ts_data
*tsdata
)
884 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
888 error
= device_property_read_u32(dev
, "threshold", &val
);
890 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
, val
);
891 tsdata
->threshold
= val
;
894 error
= device_property_read_u32(dev
, "gain", &val
);
896 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
, val
);
900 error
= device_property_read_u32(dev
, "offset", &val
);
902 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
, val
);
903 tsdata
->offset
= val
;
908 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
910 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
912 tsdata
->threshold
= edt_ft5x06_register_read(tsdata
,
913 reg_addr
->reg_threshold
);
914 tsdata
->gain
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_gain
);
915 tsdata
->offset
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_offset
);
916 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
917 tsdata
->report_rate
= edt_ft5x06_register_read(tsdata
,
918 reg_addr
->reg_report_rate
);
919 if (tsdata
->version
== EDT_M06
||
920 tsdata
->version
== EDT_M09
||
921 tsdata
->version
== EDT_M12
) {
922 tsdata
->num_x
= edt_ft5x06_register_read(tsdata
,
923 reg_addr
->reg_num_x
);
924 tsdata
->num_y
= edt_ft5x06_register_read(tsdata
,
925 reg_addr
->reg_num_y
);
933 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
935 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
937 switch (tsdata
->version
) {
939 reg_addr
->reg_threshold
= WORK_REGISTER_THRESHOLD
;
940 reg_addr
->reg_report_rate
= WORK_REGISTER_REPORT_RATE
;
941 reg_addr
->reg_gain
= WORK_REGISTER_GAIN
;
942 reg_addr
->reg_offset
= WORK_REGISTER_OFFSET
;
943 reg_addr
->reg_num_x
= WORK_REGISTER_NUM_X
;
944 reg_addr
->reg_num_y
= WORK_REGISTER_NUM_Y
;
949 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
950 reg_addr
->reg_report_rate
= NO_REGISTER
;
951 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
952 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
953 reg_addr
->reg_num_x
= M09_REGISTER_NUM_X
;
954 reg_addr
->reg_num_y
= M09_REGISTER_NUM_Y
;
958 /* this is a guesswork */
959 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
960 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
961 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
966 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
967 const struct i2c_device_id
*id
)
969 const struct edt_i2c_chip_data
*chip_data
;
970 struct edt_ft5x06_ts_data
*tsdata
;
971 u8 buf
[2] = { 0xfc, 0x00 };
972 struct input_dev
*input
;
973 unsigned long irq_flags
;
975 char fw_version
[EDT_NAME_LEN
];
977 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
979 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
981 dev_err(&client
->dev
, "failed to allocate driver data.\n");
985 chip_data
= of_device_get_match_data(&client
->dev
);
987 chip_data
= (const struct edt_i2c_chip_data
*)id
->driver_data
;
988 if (!chip_data
|| !chip_data
->max_support_points
) {
989 dev_err(&client
->dev
, "invalid or missing chip data\n");
993 tsdata
->max_support_points
= chip_data
->max_support_points
;
995 tsdata
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
996 "reset", GPIOD_OUT_HIGH
);
997 if (IS_ERR(tsdata
->reset_gpio
)) {
998 error
= PTR_ERR(tsdata
->reset_gpio
);
999 dev_err(&client
->dev
,
1000 "Failed to request GPIO reset pin, error %d\n", error
);
1004 tsdata
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
1005 "wake", GPIOD_OUT_LOW
);
1006 if (IS_ERR(tsdata
->wake_gpio
)) {
1007 error
= PTR_ERR(tsdata
->wake_gpio
);
1008 dev_err(&client
->dev
,
1009 "Failed to request GPIO wake pin, error %d\n", error
);
1013 if (tsdata
->wake_gpio
) {
1014 usleep_range(5000, 6000);
1015 gpiod_set_value_cansleep(tsdata
->wake_gpio
, 1);
1018 if (tsdata
->reset_gpio
) {
1019 usleep_range(5000, 6000);
1020 gpiod_set_value_cansleep(tsdata
->reset_gpio
, 0);
1024 input
= devm_input_allocate_device(&client
->dev
);
1026 dev_err(&client
->dev
, "failed to allocate input device.\n");
1030 mutex_init(&tsdata
->mutex
);
1031 tsdata
->client
= client
;
1032 tsdata
->input
= input
;
1033 tsdata
->factory_mode
= false;
1035 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
1037 dev_err(&client
->dev
, "touchscreen probe failed\n");
1042 * Dummy read access. EP0700MLP1 returns bogus data on the first
1043 * register read access and ignores writes.
1045 edt_ft5x06_ts_readwrite(tsdata
->client
, 2, buf
, 2, buf
);
1047 edt_ft5x06_ts_set_regs(tsdata
);
1048 edt_ft5x06_ts_get_defaults(&client
->dev
, tsdata
);
1049 edt_ft5x06_ts_get_parameters(tsdata
);
1051 dev_dbg(&client
->dev
,
1052 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1053 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
1055 input
->name
= tsdata
->name
;
1056 input
->id
.bustype
= BUS_I2C
;
1057 input
->dev
.parent
= &client
->dev
;
1059 if (tsdata
->version
== EDT_M06
||
1060 tsdata
->version
== EDT_M09
||
1061 tsdata
->version
== EDT_M12
) {
1062 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1063 0, tsdata
->num_x
* 64 - 1, 0, 0);
1064 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1065 0, tsdata
->num_y
* 64 - 1, 0, 0);
1067 /* Unknown maximum values. Specify via devicetree */
1068 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1070 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1074 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
1076 error
= input_mt_init_slots(input
, tsdata
->max_support_points
,
1079 dev_err(&client
->dev
, "Unable to init MT slots.\n");
1083 i2c_set_clientdata(client
, tsdata
);
1085 irq_flags
= irq_get_trigger_type(client
->irq
);
1086 if (irq_flags
== IRQF_TRIGGER_NONE
)
1087 irq_flags
= IRQF_TRIGGER_FALLING
;
1088 irq_flags
|= IRQF_ONESHOT
;
1090 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1091 NULL
, edt_ft5x06_ts_isr
, irq_flags
,
1092 client
->name
, tsdata
);
1094 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
1098 error
= devm_device_add_group(&client
->dev
, &edt_ft5x06_attr_group
);
1102 error
= input_register_device(input
);
1106 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1107 device_init_wakeup(&client
->dev
, 1);
1109 dev_dbg(&client
->dev
,
1110 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1112 tsdata
->wake_gpio
? desc_to_gpio(tsdata
->wake_gpio
) : -1,
1113 tsdata
->reset_gpio
? desc_to_gpio(tsdata
->reset_gpio
) : -1);
1118 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1120 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1122 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1127 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1129 struct i2c_client
*client
= to_i2c_client(dev
);
1131 if (device_may_wakeup(dev
))
1132 enable_irq_wake(client
->irq
);
1137 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1139 struct i2c_client
*client
= to_i2c_client(dev
);
1141 if (device_may_wakeup(dev
))
1142 disable_irq_wake(client
->irq
);
1147 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1148 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1150 static const struct edt_i2c_chip_data edt_ft5x06_data
= {
1151 .max_support_points
= 5,
1154 static const struct edt_i2c_chip_data edt_ft5506_data
= {
1155 .max_support_points
= 10,
1158 static const struct edt_i2c_chip_data edt_ft6236_data
= {
1159 .max_support_points
= 2,
1162 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1163 { .name
= "edt-ft5x06", .driver_data
= (long)&edt_ft5x06_data
},
1164 { .name
= "edt-ft5506", .driver_data
= (long)&edt_ft5506_data
},
1165 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1166 { .name
= "ft6236", .driver_data
= (long)&edt_ft6236_data
},
1169 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1172 static const struct of_device_id edt_ft5x06_of_match
[] = {
1173 { .compatible
= "edt,edt-ft5206", .data
= &edt_ft5x06_data
},
1174 { .compatible
= "edt,edt-ft5306", .data
= &edt_ft5x06_data
},
1175 { .compatible
= "edt,edt-ft5406", .data
= &edt_ft5x06_data
},
1176 { .compatible
= "edt,edt-ft5506", .data
= &edt_ft5506_data
},
1177 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1178 { .compatible
= "focaltech,ft6236", .data
= &edt_ft6236_data
},
1181 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1184 static struct i2c_driver edt_ft5x06_ts_driver
= {
1186 .name
= "edt_ft5x06",
1187 .of_match_table
= of_match_ptr(edt_ft5x06_of_match
),
1188 .pm
= &edt_ft5x06_ts_pm_ops
,
1190 .id_table
= edt_ft5x06_ts_id
,
1191 .probe
= edt_ft5x06_ts_probe
,
1192 .remove
= edt_ft5x06_ts_remove
,
1195 module_i2c_driver(edt_ft5x06_ts_driver
);
1197 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1198 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1199 MODULE_LICENSE("GPL");