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 1 /* msec */
86 struct edt_ft5x06_ts_data
{
87 struct i2c_client
*client
;
88 struct input_dev
*input
;
92 struct gpio_desc
*reset_gpio
;
93 struct gpio_desc
*wake_gpio
;
95 #if defined(CONFIG_DEBUG_FS)
96 struct dentry
*debug_dir
;
107 int max_support_points
;
109 char name
[EDT_NAME_LEN
];
111 struct edt_reg_addr reg_addr
;
112 enum edt_ver version
;
115 struct edt_i2c_chip_data
{
116 int max_support_points
;
119 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
120 u16 wr_len
, u8
*wr_buf
,
121 u16 rd_len
, u8
*rd_buf
)
123 struct i2c_msg wrmsg
[2];
128 wrmsg
[i
].addr
= client
->addr
;
130 wrmsg
[i
].len
= wr_len
;
131 wrmsg
[i
].buf
= wr_buf
;
135 wrmsg
[i
].addr
= client
->addr
;
136 wrmsg
[i
].flags
= I2C_M_RD
;
137 wrmsg
[i
].len
= rd_len
;
138 wrmsg
[i
].buf
= rd_buf
;
142 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
151 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
157 for (i
= 0; i
< buflen
- 1; i
++)
160 if (crc
!= buf
[buflen
-1]) {
161 dev_err_ratelimited(&tsdata
->client
->dev
,
162 "crc error: 0x%02x expected, got 0x%02x\n",
170 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
172 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
173 struct device
*dev
= &tsdata
->client
->dev
;
176 int i
, type
, x
, y
, id
;
177 int offset
, tplen
, datalen
, crclen
;
180 switch (tsdata
->version
) {
182 cmd
= 0xf9; /* tell the controller to send touch data */
183 offset
= 5; /* where the actual touch data starts */
184 tplen
= 4; /* data comes in so called frames */
185 crclen
= 1; /* length of the crc data */
199 memset(rdbuf
, 0, sizeof(rdbuf
));
200 datalen
= tplen
* tsdata
->max_support_points
+ offset
+ crclen
;
202 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
206 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
211 /* M09 does not send header or CRC */
212 if (tsdata
->version
== M06
) {
213 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
214 rdbuf
[2] != datalen
) {
215 dev_err_ratelimited(dev
,
216 "Unexpected header: %02x%02x%02x!\n",
217 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
221 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
225 for (i
= 0; i
< tsdata
->max_support_points
; i
++) {
226 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
230 /* ignore Reserved events */
231 if (type
== TOUCH_EVENT_RESERVED
)
234 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
235 if (tsdata
->version
== M06
&& type
== TOUCH_EVENT_DOWN
)
238 x
= ((buf
[0] << 8) | buf
[1]) & 0x0fff;
239 y
= ((buf
[2] << 8) | buf
[3]) & 0x0fff;
240 id
= (buf
[2] >> 4) & 0x0f;
241 down
= type
!= TOUCH_EVENT_UP
;
243 input_mt_slot(tsdata
->input
, id
);
244 input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
, down
);
249 input_report_abs(tsdata
->input
, ABS_MT_POSITION_X
, x
);
250 input_report_abs(tsdata
->input
, ABS_MT_POSITION_Y
, y
);
253 input_mt_report_pointer_emulation(tsdata
->input
, true);
254 input_sync(tsdata
->input
);
260 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data
*tsdata
,
265 switch (tsdata
->version
) {
267 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
268 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
270 wrbuf
[3] = wrbuf
[0] ^ wrbuf
[1] ^ wrbuf
[2];
271 return edt_ft5x06_ts_readwrite(tsdata
->client
, 4,
277 return edt_ft5x06_ts_readwrite(tsdata
->client
, 2,
285 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data
*tsdata
,
288 u8 wrbuf
[2], rdbuf
[2];
291 switch (tsdata
->version
) {
293 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
294 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
295 wrbuf
[1] |= tsdata
->factory_mode
? 0x80 : 0x40;
297 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 2, wrbuf
, 2,
302 if ((wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0]) != rdbuf
[1]) {
303 dev_err(&tsdata
->client
->dev
,
304 "crc error: 0x%02x expected, got 0x%02x\n",
305 wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0],
313 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
326 struct edt_ft5x06_attribute
{
327 struct device_attribute dattr
;
335 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
336 _limit_low, _limit_high) \
337 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
338 .dattr = __ATTR(_field, _mode, \
339 edt_ft5x06_setting_show, \
340 edt_ft5x06_setting_store), \
341 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
342 .addr_m06 = _addr_m06, \
343 .addr_m09 = _addr_m09, \
344 .limit_low = _limit_low, \
345 .limit_high = _limit_high, \
348 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
349 struct device_attribute
*dattr
,
352 struct i2c_client
*client
= to_i2c_client(dev
);
353 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
354 struct edt_ft5x06_attribute
*attr
=
355 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
356 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
362 mutex_lock(&tsdata
->mutex
);
364 if (tsdata
->factory_mode
) {
369 switch (tsdata
->version
) {
371 addr
= attr
->addr_m06
;
375 addr
= attr
->addr_m09
;
383 if (addr
!= NO_REGISTER
) {
384 val
= edt_ft5x06_register_read(tsdata
, addr
);
387 dev_err(&tsdata
->client
->dev
,
388 "Failed to fetch attribute %s, error %d\n",
389 dattr
->attr
.name
, error
);
397 dev_warn(&tsdata
->client
->dev
,
398 "%s: read (%d) and stored value (%d) differ\n",
399 dattr
->attr
.name
, val
, *field
);
403 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
405 mutex_unlock(&tsdata
->mutex
);
406 return error
?: count
;
409 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
410 struct device_attribute
*dattr
,
411 const char *buf
, size_t count
)
413 struct i2c_client
*client
= to_i2c_client(dev
);
414 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
415 struct edt_ft5x06_attribute
*attr
=
416 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
417 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
422 mutex_lock(&tsdata
->mutex
);
424 if (tsdata
->factory_mode
) {
429 error
= kstrtouint(buf
, 0, &val
);
433 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
438 switch (tsdata
->version
) {
440 addr
= attr
->addr_m06
;
444 addr
= attr
->addr_m09
;
452 if (addr
!= NO_REGISTER
) {
453 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
455 dev_err(&tsdata
->client
->dev
,
456 "Failed to update attribute %s, error: %d\n",
457 dattr
->attr
.name
, error
);
464 mutex_unlock(&tsdata
->mutex
);
465 return error
?: count
;
468 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
469 M09_REGISTER_GAIN
, 0, 31);
470 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
471 M09_REGISTER_OFFSET
, 0, 31);
472 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
473 M09_REGISTER_THRESHOLD
, 20, 80);
474 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
477 static struct attribute
*edt_ft5x06_attrs
[] = {
478 &edt_ft5x06_attr_gain
.dattr
.attr
,
479 &edt_ft5x06_attr_offset
.dattr
.attr
,
480 &edt_ft5x06_attr_threshold
.dattr
.attr
,
481 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
485 static const struct attribute_group edt_ft5x06_attr_group
= {
486 .attrs
= edt_ft5x06_attrs
,
489 #ifdef CONFIG_DEBUG_FS
490 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
492 struct i2c_client
*client
= tsdata
->client
;
493 int retries
= EDT_SWITCH_MODE_RETRIES
;
497 disable_irq(client
->irq
);
499 if (!tsdata
->raw_buffer
) {
500 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
502 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
503 if (!tsdata
->raw_buffer
) {
509 /* mode register is 0x3c when in the work mode */
510 if (tsdata
->version
== M09
)
513 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
515 dev_err(&client
->dev
,
516 "failed to switch to factory mode, error %d\n", error
);
520 tsdata
->factory_mode
= true;
522 mdelay(EDT_SWITCH_MODE_DELAY
);
523 /* mode register is 0x01 when in factory mode */
524 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
527 } while (--retries
> 0);
530 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
531 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
539 kfree(tsdata
->raw_buffer
);
540 tsdata
->raw_buffer
= NULL
;
541 tsdata
->factory_mode
= false;
542 enable_irq(client
->irq
);
547 dev_err(&client
->dev
, "No factory mode support for M09\n");
552 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
554 struct i2c_client
*client
= tsdata
->client
;
555 int retries
= EDT_SWITCH_MODE_RETRIES
;
556 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
560 /* mode register is 0x01 when in the factory mode */
561 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
563 dev_err(&client
->dev
,
564 "failed to switch to work mode, error: %d\n", error
);
568 tsdata
->factory_mode
= false;
571 mdelay(EDT_SWITCH_MODE_DELAY
);
572 /* mode register is 0x01 when in factory mode */
573 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
576 } while (--retries
> 0);
579 dev_err(&client
->dev
, "not in work mode after %dms.\n",
580 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
581 tsdata
->factory_mode
= true;
585 kfree(tsdata
->raw_buffer
);
586 tsdata
->raw_buffer
= NULL
;
588 /* restore parameters */
589 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
591 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
593 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
595 if (reg_addr
->reg_report_rate
)
596 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
597 tsdata
->report_rate
);
599 enable_irq(client
->irq
);
604 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
606 struct edt_ft5x06_ts_data
*tsdata
= data
;
608 *mode
= tsdata
->factory_mode
;
613 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
615 struct edt_ft5x06_ts_data
*tsdata
= data
;
621 mutex_lock(&tsdata
->mutex
);
623 if (mode
!= tsdata
->factory_mode
) {
624 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
625 edt_ft5x06_work_mode(tsdata
);
628 mutex_unlock(&tsdata
->mutex
);
633 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
634 edt_ft5x06_debugfs_mode_set
, "%llu\n");
636 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
637 char __user
*buf
, size_t count
, loff_t
*off
)
639 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
640 struct i2c_client
*client
= tsdata
->client
;
641 int retries
= EDT_RAW_DATA_RETRIES
;
648 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
651 mutex_lock(&tsdata
->mutex
);
653 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
658 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
660 dev_dbg(&client
->dev
,
661 "failed to write 0x08 register, error %d\n", error
);
666 msleep(EDT_RAW_DATA_DELAY
);
667 val
= edt_ft5x06_register_read(tsdata
, 0x08);
670 } while (--retries
> 0);
674 dev_dbg(&client
->dev
,
675 "failed to read 0x08 register, error %d\n", error
);
680 dev_dbg(&client
->dev
,
681 "timed out waiting for register to settle\n");
686 rdbuf
= tsdata
->raw_buffer
;
687 colbytes
= tsdata
->num_y
* sizeof(u16
);
691 for (i
= 0; i
< tsdata
->num_x
; i
++) {
692 wrbuf
[2] = i
; /* column index */
693 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
694 sizeof(wrbuf
), wrbuf
,
702 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
703 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
710 mutex_unlock(&tsdata
->mutex
);
711 return error
?: read
;
714 static const struct file_operations debugfs_raw_data_fops
= {
716 .read
= edt_ft5x06_debugfs_raw_data_read
,
720 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
721 const char *debugfs_name
)
723 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
724 if (!tsdata
->debug_dir
)
727 debugfs_create_u16("num_x", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_x
);
728 debugfs_create_u16("num_y", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_y
);
730 debugfs_create_file("mode", S_IRUSR
| S_IWUSR
,
731 tsdata
->debug_dir
, tsdata
, &debugfs_mode_fops
);
732 debugfs_create_file("raw_data", S_IRUSR
,
733 tsdata
->debug_dir
, tsdata
, &debugfs_raw_data_fops
);
737 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
739 debugfs_remove_recursive(tsdata
->debug_dir
);
740 kfree(tsdata
->raw_buffer
);
746 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
747 const char *debugfs_name
)
752 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
756 #endif /* CONFIG_DEBUGFS */
758 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
759 struct edt_ft5x06_ts_data
*tsdata
,
762 u8 rdbuf
[EDT_NAME_LEN
];
765 char *model_name
= tsdata
->name
;
767 /* see what we find if we assume it is a M06 *
768 * if we get less than EDT_NAME_LEN, we don't want
769 * to have garbage in there
771 memset(rdbuf
, 0, sizeof(rdbuf
));
772 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xbb",
773 EDT_NAME_LEN
- 1, rdbuf
);
777 /* if we find something consistent, stay with that assumption
778 * at least M09 won't send 3 bytes here
780 if (!(strncasecmp(rdbuf
+ 1, "EP0", 3))) {
781 tsdata
->version
= M06
;
783 /* remove last '$' end marker */
784 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
785 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
786 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
788 /* look for Model/Version separator */
789 p
= strchr(rdbuf
, '*');
792 strlcpy(model_name
, rdbuf
+ 1, EDT_NAME_LEN
);
793 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
795 /* since there are only two versions around (M06, M09) */
796 tsdata
->version
= M09
;
798 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
803 strlcpy(fw_version
, rdbuf
, 2);
805 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
810 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
811 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
817 static void edt_ft5x06_ts_get_defaults(struct device
*dev
,
818 struct edt_ft5x06_ts_data
*tsdata
)
820 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
824 error
= device_property_read_u32(dev
, "threshold", &val
);
826 reg_addr
->reg_threshold
= val
;
828 error
= device_property_read_u32(dev
, "gain", &val
);
830 reg_addr
->reg_gain
= val
;
832 error
= device_property_read_u32(dev
, "offset", &val
);
834 reg_addr
->reg_offset
= val
;
838 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
840 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
842 tsdata
->threshold
= edt_ft5x06_register_read(tsdata
,
843 reg_addr
->reg_threshold
);
844 tsdata
->gain
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_gain
);
845 tsdata
->offset
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_offset
);
846 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
847 tsdata
->report_rate
= edt_ft5x06_register_read(tsdata
,
848 reg_addr
->reg_report_rate
);
849 tsdata
->num_x
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_num_x
);
850 tsdata
->num_y
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_num_y
);
854 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
856 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
858 switch (tsdata
->version
) {
860 reg_addr
->reg_threshold
= WORK_REGISTER_THRESHOLD
;
861 reg_addr
->reg_report_rate
= WORK_REGISTER_REPORT_RATE
;
862 reg_addr
->reg_gain
= WORK_REGISTER_GAIN
;
863 reg_addr
->reg_offset
= WORK_REGISTER_OFFSET
;
864 reg_addr
->reg_num_x
= WORK_REGISTER_NUM_X
;
865 reg_addr
->reg_num_y
= WORK_REGISTER_NUM_Y
;
869 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
870 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
871 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
872 reg_addr
->reg_num_x
= M09_REGISTER_NUM_X
;
873 reg_addr
->reg_num_y
= M09_REGISTER_NUM_Y
;
878 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
879 const struct i2c_device_id
*id
)
881 const struct edt_i2c_chip_data
*chip_data
;
882 struct edt_ft5x06_ts_data
*tsdata
;
883 struct input_dev
*input
;
884 unsigned long irq_flags
;
886 char fw_version
[EDT_NAME_LEN
];
888 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
890 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
892 dev_err(&client
->dev
, "failed to allocate driver data.\n");
896 chip_data
= of_device_get_match_data(&client
->dev
);
898 chip_data
= (const struct edt_i2c_chip_data
*)id
->driver_data
;
899 if (!chip_data
|| !chip_data
->max_support_points
) {
900 dev_err(&client
->dev
, "invalid or missing chip data\n");
904 tsdata
->max_support_points
= chip_data
->max_support_points
;
906 tsdata
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
907 "reset", GPIOD_OUT_HIGH
);
908 if (IS_ERR(tsdata
->reset_gpio
)) {
909 error
= PTR_ERR(tsdata
->reset_gpio
);
910 dev_err(&client
->dev
,
911 "Failed to request GPIO reset pin, error %d\n", error
);
915 tsdata
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
916 "wake", GPIOD_OUT_LOW
);
917 if (IS_ERR(tsdata
->wake_gpio
)) {
918 error
= PTR_ERR(tsdata
->wake_gpio
);
919 dev_err(&client
->dev
,
920 "Failed to request GPIO wake pin, error %d\n", error
);
924 if (tsdata
->wake_gpio
) {
925 usleep_range(5000, 6000);
926 gpiod_set_value_cansleep(tsdata
->wake_gpio
, 1);
929 if (tsdata
->reset_gpio
) {
930 usleep_range(5000, 6000);
931 gpiod_set_value_cansleep(tsdata
->reset_gpio
, 0);
935 input
= devm_input_allocate_device(&client
->dev
);
937 dev_err(&client
->dev
, "failed to allocate input device.\n");
941 mutex_init(&tsdata
->mutex
);
942 tsdata
->client
= client
;
943 tsdata
->input
= input
;
944 tsdata
->factory_mode
= false;
946 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
948 dev_err(&client
->dev
, "touchscreen probe failed\n");
952 edt_ft5x06_ts_set_regs(tsdata
);
953 edt_ft5x06_ts_get_defaults(&client
->dev
, tsdata
);
954 edt_ft5x06_ts_get_parameters(tsdata
);
956 dev_dbg(&client
->dev
,
957 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
958 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
960 input
->name
= tsdata
->name
;
961 input
->id
.bustype
= BUS_I2C
;
962 input
->dev
.parent
= &client
->dev
;
964 input_set_abs_params(input
, ABS_MT_POSITION_X
,
965 0, tsdata
->num_x
* 64 - 1, 0, 0);
966 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
967 0, tsdata
->num_y
* 64 - 1, 0, 0);
969 touchscreen_parse_properties(input
, true);
971 error
= input_mt_init_slots(input
, tsdata
->max_support_points
,
974 dev_err(&client
->dev
, "Unable to init MT slots.\n");
978 input_set_drvdata(input
, tsdata
);
979 i2c_set_clientdata(client
, tsdata
);
981 irq_flags
= irq_get_trigger_type(client
->irq
);
982 if (irq_flags
== IRQF_TRIGGER_NONE
)
983 irq_flags
= IRQF_TRIGGER_FALLING
;
984 irq_flags
|= IRQF_ONESHOT
;
986 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
987 NULL
, edt_ft5x06_ts_isr
, irq_flags
,
988 client
->name
, tsdata
);
990 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
994 error
= sysfs_create_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
998 error
= input_register_device(input
);
1000 goto err_remove_attrs
;
1002 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1003 device_init_wakeup(&client
->dev
, 1);
1005 dev_dbg(&client
->dev
,
1006 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1008 tsdata
->wake_gpio
? desc_to_gpio(tsdata
->wake_gpio
) : -1,
1009 tsdata
->reset_gpio
? desc_to_gpio(tsdata
->reset_gpio
) : -1);
1014 sysfs_remove_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1018 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1020 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1022 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1023 sysfs_remove_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1028 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1030 struct i2c_client
*client
= to_i2c_client(dev
);
1032 if (device_may_wakeup(dev
))
1033 enable_irq_wake(client
->irq
);
1038 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1040 struct i2c_client
*client
= to_i2c_client(dev
);
1042 if (device_may_wakeup(dev
))
1043 disable_irq_wake(client
->irq
);
1048 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1049 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1051 static const struct edt_i2c_chip_data edt_ft5x06_data
= {
1052 .max_support_points
= 5,
1055 static const struct edt_i2c_chip_data edt_ft5506_data
= {
1056 .max_support_points
= 10,
1059 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1060 { .name
= "edt-ft5x06", .driver_data
= (long)&edt_ft5x06_data
},
1061 { .name
= "edt-ft5506", .driver_data
= (long)&edt_ft5506_data
},
1064 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1067 static const struct of_device_id edt_ft5x06_of_match
[] = {
1068 { .compatible
= "edt,edt-ft5206", .data
= &edt_ft5x06_data
},
1069 { .compatible
= "edt,edt-ft5306", .data
= &edt_ft5x06_data
},
1070 { .compatible
= "edt,edt-ft5406", .data
= &edt_ft5x06_data
},
1071 { .compatible
= "edt,edt-ft5506", .data
= &edt_ft5506_data
},
1074 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1077 static struct i2c_driver edt_ft5x06_ts_driver
= {
1079 .name
= "edt_ft5x06",
1080 .of_match_table
= of_match_ptr(edt_ft5x06_of_match
),
1081 .pm
= &edt_ft5x06_ts_pm_ops
,
1083 .id_table
= edt_ft5x06_ts_id
,
1084 .probe
= edt_ft5x06_ts_probe
,
1085 .remove
= edt_ft5x06_ts_remove
,
1088 module_i2c_driver(edt_ft5x06_ts_driver
);
1090 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1091 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1092 MODULE_LICENSE("GPL");