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 */
86 struct edt_ft5x06_ts_data
{
87 struct i2c_client
*client
;
88 struct input_dev
*input
;
89 struct touchscreen_properties prop
;
93 struct gpio_desc
*reset_gpio
;
94 struct gpio_desc
*wake_gpio
;
96 #if defined(CONFIG_DEBUG_FS)
97 struct dentry
*debug_dir
;
108 int max_support_points
;
110 char name
[EDT_NAME_LEN
];
112 struct edt_reg_addr reg_addr
;
113 enum edt_ver version
;
116 struct edt_i2c_chip_data
{
117 int max_support_points
;
120 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
121 u16 wr_len
, u8
*wr_buf
,
122 u16 rd_len
, u8
*rd_buf
)
124 struct i2c_msg wrmsg
[2];
129 wrmsg
[i
].addr
= client
->addr
;
131 wrmsg
[i
].len
= wr_len
;
132 wrmsg
[i
].buf
= wr_buf
;
136 wrmsg
[i
].addr
= client
->addr
;
137 wrmsg
[i
].flags
= I2C_M_RD
;
138 wrmsg
[i
].len
= rd_len
;
139 wrmsg
[i
].buf
= rd_buf
;
143 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
152 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
158 for (i
= 0; i
< buflen
- 1; i
++)
161 if (crc
!= buf
[buflen
-1]) {
162 dev_err_ratelimited(&tsdata
->client
->dev
,
163 "crc error: 0x%02x expected, got 0x%02x\n",
171 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
173 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
174 struct device
*dev
= &tsdata
->client
->dev
;
177 int i
, type
, x
, y
, id
;
178 int offset
, tplen
, datalen
, crclen
;
181 switch (tsdata
->version
) {
183 cmd
= 0xf9; /* tell the controller to send touch data */
184 offset
= 5; /* where the actual touch data starts */
185 tplen
= 4; /* data comes in so called frames */
186 crclen
= 1; /* length of the crc data */
200 memset(rdbuf
, 0, sizeof(rdbuf
));
201 datalen
= tplen
* tsdata
->max_support_points
+ offset
+ crclen
;
203 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
207 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
212 /* M09 does not send header or CRC */
213 if (tsdata
->version
== M06
) {
214 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
215 rdbuf
[2] != datalen
) {
216 dev_err_ratelimited(dev
,
217 "Unexpected header: %02x%02x%02x!\n",
218 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
222 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
226 for (i
= 0; i
< tsdata
->max_support_points
; i
++) {
227 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
231 /* ignore Reserved events */
232 if (type
== TOUCH_EVENT_RESERVED
)
235 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
236 if (tsdata
->version
== M06
&& type
== TOUCH_EVENT_DOWN
)
239 x
= ((buf
[0] << 8) | buf
[1]) & 0x0fff;
240 y
= ((buf
[2] << 8) | buf
[3]) & 0x0fff;
241 id
= (buf
[2] >> 4) & 0x0f;
242 down
= type
!= TOUCH_EVENT_UP
;
244 input_mt_slot(tsdata
->input
, id
);
245 input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
, down
);
250 touchscreen_report_pos(tsdata
->input
, &tsdata
->prop
, x
, y
,
254 input_mt_report_pointer_emulation(tsdata
->input
, true);
255 input_sync(tsdata
->input
);
261 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data
*tsdata
,
266 switch (tsdata
->version
) {
268 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
269 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
271 wrbuf
[3] = wrbuf
[0] ^ wrbuf
[1] ^ wrbuf
[2];
272 return edt_ft5x06_ts_readwrite(tsdata
->client
, 4,
278 return edt_ft5x06_ts_readwrite(tsdata
->client
, 2,
286 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data
*tsdata
,
289 u8 wrbuf
[2], rdbuf
[2];
292 switch (tsdata
->version
) {
294 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
295 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
296 wrbuf
[1] |= tsdata
->factory_mode
? 0x80 : 0x40;
298 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 2, wrbuf
, 2,
303 if ((wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0]) != rdbuf
[1]) {
304 dev_err(&tsdata
->client
->dev
,
305 "crc error: 0x%02x expected, got 0x%02x\n",
306 wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0],
314 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
327 struct edt_ft5x06_attribute
{
328 struct device_attribute dattr
;
336 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
337 _limit_low, _limit_high) \
338 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
339 .dattr = __ATTR(_field, _mode, \
340 edt_ft5x06_setting_show, \
341 edt_ft5x06_setting_store), \
342 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
343 .addr_m06 = _addr_m06, \
344 .addr_m09 = _addr_m09, \
345 .limit_low = _limit_low, \
346 .limit_high = _limit_high, \
349 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
350 struct device_attribute
*dattr
,
353 struct i2c_client
*client
= to_i2c_client(dev
);
354 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
355 struct edt_ft5x06_attribute
*attr
=
356 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
357 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
363 mutex_lock(&tsdata
->mutex
);
365 if (tsdata
->factory_mode
) {
370 switch (tsdata
->version
) {
372 addr
= attr
->addr_m06
;
376 addr
= attr
->addr_m09
;
384 if (addr
!= NO_REGISTER
) {
385 val
= edt_ft5x06_register_read(tsdata
, addr
);
388 dev_err(&tsdata
->client
->dev
,
389 "Failed to fetch attribute %s, error %d\n",
390 dattr
->attr
.name
, error
);
398 dev_warn(&tsdata
->client
->dev
,
399 "%s: read (%d) and stored value (%d) differ\n",
400 dattr
->attr
.name
, val
, *field
);
404 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
406 mutex_unlock(&tsdata
->mutex
);
407 return error
?: count
;
410 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
411 struct device_attribute
*dattr
,
412 const char *buf
, size_t count
)
414 struct i2c_client
*client
= to_i2c_client(dev
);
415 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
416 struct edt_ft5x06_attribute
*attr
=
417 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
418 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
423 mutex_lock(&tsdata
->mutex
);
425 if (tsdata
->factory_mode
) {
430 error
= kstrtouint(buf
, 0, &val
);
434 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
439 switch (tsdata
->version
) {
441 addr
= attr
->addr_m06
;
445 addr
= attr
->addr_m09
;
453 if (addr
!= NO_REGISTER
) {
454 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
456 dev_err(&tsdata
->client
->dev
,
457 "Failed to update attribute %s, error: %d\n",
458 dattr
->attr
.name
, error
);
465 mutex_unlock(&tsdata
->mutex
);
466 return error
?: count
;
469 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
470 M09_REGISTER_GAIN
, 0, 31);
471 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
472 M09_REGISTER_OFFSET
, 0, 31);
473 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
474 M09_REGISTER_THRESHOLD
, 0, 80);
475 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
478 static struct attribute
*edt_ft5x06_attrs
[] = {
479 &edt_ft5x06_attr_gain
.dattr
.attr
,
480 &edt_ft5x06_attr_offset
.dattr
.attr
,
481 &edt_ft5x06_attr_threshold
.dattr
.attr
,
482 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
486 static const struct attribute_group edt_ft5x06_attr_group
= {
487 .attrs
= edt_ft5x06_attrs
,
490 #ifdef CONFIG_DEBUG_FS
491 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
493 struct i2c_client
*client
= tsdata
->client
;
494 int retries
= EDT_SWITCH_MODE_RETRIES
;
498 disable_irq(client
->irq
);
500 if (!tsdata
->raw_buffer
) {
501 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
503 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
504 if (!tsdata
->raw_buffer
) {
510 /* mode register is 0x3c when in the work mode */
511 if (tsdata
->version
== M09
)
514 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
516 dev_err(&client
->dev
,
517 "failed to switch to factory mode, error %d\n", error
);
521 tsdata
->factory_mode
= true;
523 mdelay(EDT_SWITCH_MODE_DELAY
);
524 /* mode register is 0x01 when in factory mode */
525 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
528 } while (--retries
> 0);
531 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
532 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
540 kfree(tsdata
->raw_buffer
);
541 tsdata
->raw_buffer
= NULL
;
542 tsdata
->factory_mode
= false;
543 enable_irq(client
->irq
);
548 dev_err(&client
->dev
, "No factory mode support for M09\n");
553 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
555 struct i2c_client
*client
= tsdata
->client
;
556 int retries
= EDT_SWITCH_MODE_RETRIES
;
557 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
561 /* mode register is 0x01 when in the factory mode */
562 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
564 dev_err(&client
->dev
,
565 "failed to switch to work mode, error: %d\n", error
);
569 tsdata
->factory_mode
= false;
572 mdelay(EDT_SWITCH_MODE_DELAY
);
573 /* mode register is 0x01 when in factory mode */
574 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
577 } while (--retries
> 0);
580 dev_err(&client
->dev
, "not in work mode after %dms.\n",
581 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
582 tsdata
->factory_mode
= true;
586 kfree(tsdata
->raw_buffer
);
587 tsdata
->raw_buffer
= NULL
;
589 /* restore parameters */
590 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
592 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
594 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
596 if (reg_addr
->reg_report_rate
)
597 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
598 tsdata
->report_rate
);
600 enable_irq(client
->irq
);
605 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
607 struct edt_ft5x06_ts_data
*tsdata
= data
;
609 *mode
= tsdata
->factory_mode
;
614 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
616 struct edt_ft5x06_ts_data
*tsdata
= data
;
622 mutex_lock(&tsdata
->mutex
);
624 if (mode
!= tsdata
->factory_mode
) {
625 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
626 edt_ft5x06_work_mode(tsdata
);
629 mutex_unlock(&tsdata
->mutex
);
634 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
635 edt_ft5x06_debugfs_mode_set
, "%llu\n");
637 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
638 char __user
*buf
, size_t count
, loff_t
*off
)
640 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
641 struct i2c_client
*client
= tsdata
->client
;
642 int retries
= EDT_RAW_DATA_RETRIES
;
649 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
652 mutex_lock(&tsdata
->mutex
);
654 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
659 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
661 dev_dbg(&client
->dev
,
662 "failed to write 0x08 register, error %d\n", error
);
667 usleep_range(EDT_RAW_DATA_DELAY
, EDT_RAW_DATA_DELAY
+ 100);
668 val
= edt_ft5x06_register_read(tsdata
, 0x08);
671 } while (--retries
> 0);
675 dev_dbg(&client
->dev
,
676 "failed to read 0x08 register, error %d\n", error
);
681 dev_dbg(&client
->dev
,
682 "timed out waiting for register to settle\n");
687 rdbuf
= tsdata
->raw_buffer
;
688 colbytes
= tsdata
->num_y
* sizeof(u16
);
692 for (i
= 0; i
< tsdata
->num_x
; i
++) {
693 wrbuf
[2] = i
; /* column index */
694 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
695 sizeof(wrbuf
), wrbuf
,
703 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
704 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
711 mutex_unlock(&tsdata
->mutex
);
712 return error
?: read
;
715 static const struct file_operations debugfs_raw_data_fops
= {
717 .read
= edt_ft5x06_debugfs_raw_data_read
,
721 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
722 const char *debugfs_name
)
724 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
725 if (!tsdata
->debug_dir
)
728 debugfs_create_u16("num_x", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_x
);
729 debugfs_create_u16("num_y", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_y
);
731 debugfs_create_file("mode", S_IRUSR
| S_IWUSR
,
732 tsdata
->debug_dir
, tsdata
, &debugfs_mode_fops
);
733 debugfs_create_file("raw_data", S_IRUSR
,
734 tsdata
->debug_dir
, tsdata
, &debugfs_raw_data_fops
);
738 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
740 debugfs_remove_recursive(tsdata
->debug_dir
);
741 kfree(tsdata
->raw_buffer
);
747 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
748 const char *debugfs_name
)
753 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
757 #endif /* CONFIG_DEBUGFS */
759 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
760 struct edt_ft5x06_ts_data
*tsdata
,
763 u8 rdbuf
[EDT_NAME_LEN
];
766 char *model_name
= tsdata
->name
;
768 /* see what we find if we assume it is a M06 *
769 * if we get less than EDT_NAME_LEN, we don't want
770 * to have garbage in there
772 memset(rdbuf
, 0, sizeof(rdbuf
));
773 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xbb",
774 EDT_NAME_LEN
- 1, rdbuf
);
778 /* if we find something consistent, stay with that assumption
779 * at least M09 won't send 3 bytes here
781 if (!(strncasecmp(rdbuf
+ 1, "EP0", 3))) {
782 tsdata
->version
= M06
;
784 /* remove last '$' end marker */
785 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
786 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
787 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
789 /* look for Model/Version separator */
790 p
= strchr(rdbuf
, '*');
793 strlcpy(model_name
, rdbuf
+ 1, EDT_NAME_LEN
);
794 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
796 /* since there are only two versions around (M06, M09) */
797 tsdata
->version
= M09
;
799 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
804 strlcpy(fw_version
, rdbuf
, 2);
806 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
811 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
812 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
818 static void edt_ft5x06_ts_get_defaults(struct device
*dev
,
819 struct edt_ft5x06_ts_data
*tsdata
)
821 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
825 error
= device_property_read_u32(dev
, "threshold", &val
);
827 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
, val
);
828 tsdata
->threshold
= val
;
831 error
= device_property_read_u32(dev
, "gain", &val
);
833 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
, val
);
837 error
= device_property_read_u32(dev
, "offset", &val
);
839 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
, val
);
840 tsdata
->offset
= val
;
845 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
847 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
849 tsdata
->threshold
= edt_ft5x06_register_read(tsdata
,
850 reg_addr
->reg_threshold
);
851 tsdata
->gain
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_gain
);
852 tsdata
->offset
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_offset
);
853 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
854 tsdata
->report_rate
= edt_ft5x06_register_read(tsdata
,
855 reg_addr
->reg_report_rate
);
856 tsdata
->num_x
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_num_x
);
857 tsdata
->num_y
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_num_y
);
861 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
863 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
865 switch (tsdata
->version
) {
867 reg_addr
->reg_threshold
= WORK_REGISTER_THRESHOLD
;
868 reg_addr
->reg_report_rate
= WORK_REGISTER_REPORT_RATE
;
869 reg_addr
->reg_gain
= WORK_REGISTER_GAIN
;
870 reg_addr
->reg_offset
= WORK_REGISTER_OFFSET
;
871 reg_addr
->reg_num_x
= WORK_REGISTER_NUM_X
;
872 reg_addr
->reg_num_y
= WORK_REGISTER_NUM_Y
;
876 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
877 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
878 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
879 reg_addr
->reg_num_x
= M09_REGISTER_NUM_X
;
880 reg_addr
->reg_num_y
= M09_REGISTER_NUM_Y
;
885 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
886 const struct i2c_device_id
*id
)
888 const struct edt_i2c_chip_data
*chip_data
;
889 struct edt_ft5x06_ts_data
*tsdata
;
890 struct input_dev
*input
;
891 unsigned long irq_flags
;
893 char fw_version
[EDT_NAME_LEN
];
895 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
897 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
899 dev_err(&client
->dev
, "failed to allocate driver data.\n");
903 chip_data
= of_device_get_match_data(&client
->dev
);
905 chip_data
= (const struct edt_i2c_chip_data
*)id
->driver_data
;
906 if (!chip_data
|| !chip_data
->max_support_points
) {
907 dev_err(&client
->dev
, "invalid or missing chip data\n");
911 tsdata
->max_support_points
= chip_data
->max_support_points
;
913 tsdata
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
914 "reset", GPIOD_OUT_HIGH
);
915 if (IS_ERR(tsdata
->reset_gpio
)) {
916 error
= PTR_ERR(tsdata
->reset_gpio
);
917 dev_err(&client
->dev
,
918 "Failed to request GPIO reset pin, error %d\n", error
);
922 tsdata
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
923 "wake", GPIOD_OUT_LOW
);
924 if (IS_ERR(tsdata
->wake_gpio
)) {
925 error
= PTR_ERR(tsdata
->wake_gpio
);
926 dev_err(&client
->dev
,
927 "Failed to request GPIO wake pin, error %d\n", error
);
931 if (tsdata
->wake_gpio
) {
932 usleep_range(5000, 6000);
933 gpiod_set_value_cansleep(tsdata
->wake_gpio
, 1);
936 if (tsdata
->reset_gpio
) {
937 usleep_range(5000, 6000);
938 gpiod_set_value_cansleep(tsdata
->reset_gpio
, 0);
942 input
= devm_input_allocate_device(&client
->dev
);
944 dev_err(&client
->dev
, "failed to allocate input device.\n");
948 mutex_init(&tsdata
->mutex
);
949 tsdata
->client
= client
;
950 tsdata
->input
= input
;
951 tsdata
->factory_mode
= false;
953 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
955 dev_err(&client
->dev
, "touchscreen probe failed\n");
959 edt_ft5x06_ts_set_regs(tsdata
);
960 edt_ft5x06_ts_get_defaults(&client
->dev
, tsdata
);
961 edt_ft5x06_ts_get_parameters(tsdata
);
963 dev_dbg(&client
->dev
,
964 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
965 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
967 input
->name
= tsdata
->name
;
968 input
->id
.bustype
= BUS_I2C
;
969 input
->dev
.parent
= &client
->dev
;
971 input_set_abs_params(input
, ABS_MT_POSITION_X
,
972 0, tsdata
->num_x
* 64 - 1, 0, 0);
973 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
974 0, tsdata
->num_y
* 64 - 1, 0, 0);
976 touchscreen_parse_properties(input
, true, &tsdata
->prop
);
978 error
= input_mt_init_slots(input
, tsdata
->max_support_points
,
981 dev_err(&client
->dev
, "Unable to init MT slots.\n");
985 i2c_set_clientdata(client
, tsdata
);
987 irq_flags
= irq_get_trigger_type(client
->irq
);
988 if (irq_flags
== IRQF_TRIGGER_NONE
)
989 irq_flags
= IRQF_TRIGGER_FALLING
;
990 irq_flags
|= IRQF_ONESHOT
;
992 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
993 NULL
, edt_ft5x06_ts_isr
, irq_flags
,
994 client
->name
, tsdata
);
996 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
1000 error
= sysfs_create_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1004 error
= input_register_device(input
);
1006 goto err_remove_attrs
;
1008 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1009 device_init_wakeup(&client
->dev
, 1);
1011 dev_dbg(&client
->dev
,
1012 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1014 tsdata
->wake_gpio
? desc_to_gpio(tsdata
->wake_gpio
) : -1,
1015 tsdata
->reset_gpio
? desc_to_gpio(tsdata
->reset_gpio
) : -1);
1020 sysfs_remove_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1024 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1026 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1028 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1029 sysfs_remove_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1034 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1036 struct i2c_client
*client
= to_i2c_client(dev
);
1038 if (device_may_wakeup(dev
))
1039 enable_irq_wake(client
->irq
);
1044 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1046 struct i2c_client
*client
= to_i2c_client(dev
);
1048 if (device_may_wakeup(dev
))
1049 disable_irq_wake(client
->irq
);
1054 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1055 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1057 static const struct edt_i2c_chip_data edt_ft5x06_data
= {
1058 .max_support_points
= 5,
1061 static const struct edt_i2c_chip_data edt_ft5506_data
= {
1062 .max_support_points
= 10,
1065 static const struct edt_i2c_chip_data edt_ft6236_data
= {
1066 .max_support_points
= 2,
1069 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1070 { .name
= "edt-ft5x06", .driver_data
= (long)&edt_ft5x06_data
},
1071 { .name
= "edt-ft5506", .driver_data
= (long)&edt_ft5506_data
},
1072 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1073 { .name
= "ft6236", .driver_data
= (long)&edt_ft6236_data
},
1076 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1079 static const struct of_device_id edt_ft5x06_of_match
[] = {
1080 { .compatible
= "edt,edt-ft5206", .data
= &edt_ft5x06_data
},
1081 { .compatible
= "edt,edt-ft5306", .data
= &edt_ft5x06_data
},
1082 { .compatible
= "edt,edt-ft5406", .data
= &edt_ft5x06_data
},
1083 { .compatible
= "edt,edt-ft5506", .data
= &edt_ft5506_data
},
1084 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1085 { .compatible
= "focaltech,ft6236", .data
= &edt_ft6236_data
},
1088 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1091 static struct i2c_driver edt_ft5x06_ts_driver
= {
1093 .name
= "edt_ft5x06",
1094 .of_match_table
= of_match_ptr(edt_ft5x06_of_match
),
1095 .pm
= &edt_ft5x06_ts_pm_ops
,
1097 .id_table
= edt_ft5x06_ts_id
,
1098 .probe
= edt_ft5x06_ts_probe
,
1099 .remove
= edt_ft5x06_ts_remove
,
1102 module_i2c_driver(edt_ft5x06_ts_driver
);
1104 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1105 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1106 MODULE_LICENSE("GPL");