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/interrupt.h>
31 #include <linux/input.h>
32 #include <linux/i2c.h>
33 #include <linux/uaccess.h>
34 #include <linux/delay.h>
35 #include <linux/debugfs.h>
36 #include <linux/slab.h>
37 #include <linux/gpio.h>
38 #include <linux/of_gpio.h>
39 #include <linux/input/mt.h>
40 #include <linux/input/touchscreen.h>
41 #include <linux/input/edt-ft5x06.h>
43 #define MAX_SUPPORT_POINTS 5
45 #define WORK_REGISTER_THRESHOLD 0x00
46 #define WORK_REGISTER_REPORT_RATE 0x08
47 #define WORK_REGISTER_GAIN 0x30
48 #define WORK_REGISTER_OFFSET 0x31
49 #define WORK_REGISTER_NUM_X 0x33
50 #define WORK_REGISTER_NUM_Y 0x34
52 #define M09_REGISTER_THRESHOLD 0x80
53 #define M09_REGISTER_GAIN 0x92
54 #define M09_REGISTER_OFFSET 0x93
55 #define M09_REGISTER_NUM_X 0x94
56 #define M09_REGISTER_NUM_Y 0x95
58 #define NO_REGISTER 0xff
60 #define WORK_REGISTER_OPMODE 0x3c
61 #define FACTORY_REGISTER_OPMODE 0x01
63 #define TOUCH_EVENT_DOWN 0x00
64 #define TOUCH_EVENT_UP 0x01
65 #define TOUCH_EVENT_ON 0x02
66 #define TOUCH_EVENT_RESERVED 0x03
68 #define EDT_NAME_LEN 23
69 #define EDT_SWITCH_MODE_RETRIES 10
70 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
71 #define EDT_RAW_DATA_RETRIES 100
72 #define EDT_RAW_DATA_DELAY 1 /* msec */
88 struct edt_ft5x06_ts_data
{
89 struct i2c_client
*client
;
90 struct input_dev
*input
;
98 #if defined(CONFIG_DEBUG_FS)
99 struct dentry
*debug_dir
;
111 char name
[EDT_NAME_LEN
];
113 struct edt_reg_addr reg_addr
;
114 enum edt_ver version
;
117 static int edt_ft5x06_ts_readwrite(struct i2c_client
*client
,
118 u16 wr_len
, u8
*wr_buf
,
119 u16 rd_len
, u8
*rd_buf
)
121 struct i2c_msg wrmsg
[2];
126 wrmsg
[i
].addr
= client
->addr
;
128 wrmsg
[i
].len
= wr_len
;
129 wrmsg
[i
].buf
= wr_buf
;
133 wrmsg
[i
].addr
= client
->addr
;
134 wrmsg
[i
].flags
= I2C_M_RD
;
135 wrmsg
[i
].len
= rd_len
;
136 wrmsg
[i
].buf
= rd_buf
;
140 ret
= i2c_transfer(client
->adapter
, wrmsg
, i
);
149 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data
*tsdata
,
155 for (i
= 0; i
< buflen
- 1; i
++)
158 if (crc
!= buf
[buflen
-1]) {
159 dev_err_ratelimited(&tsdata
->client
->dev
,
160 "crc error: 0x%02x expected, got 0x%02x\n",
168 static irqreturn_t
edt_ft5x06_ts_isr(int irq
, void *dev_id
)
170 struct edt_ft5x06_ts_data
*tsdata
= dev_id
;
171 struct device
*dev
= &tsdata
->client
->dev
;
174 int i
, type
, x
, y
, id
;
175 int offset
, tplen
, datalen
;
178 switch (tsdata
->version
) {
180 cmd
= 0xf9; /* tell the controller to send touch data */
181 offset
= 5; /* where the actual touch data starts */
182 tplen
= 4; /* data comes in so called frames */
183 datalen
= 26; /* how much bytes to listen for */
197 memset(rdbuf
, 0, sizeof(rdbuf
));
199 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
203 dev_err_ratelimited(dev
, "Unable to fetch data, error: %d\n",
208 /* M09 does not send header or CRC */
209 if (tsdata
->version
== M06
) {
210 if (rdbuf
[0] != 0xaa || rdbuf
[1] != 0xaa ||
211 rdbuf
[2] != datalen
) {
212 dev_err_ratelimited(dev
,
213 "Unexpected header: %02x%02x%02x!\n",
214 rdbuf
[0], rdbuf
[1], rdbuf
[2]);
218 if (!edt_ft5x06_ts_check_crc(tsdata
, rdbuf
, datalen
))
222 for (i
= 0; i
< MAX_SUPPORT_POINTS
; i
++) {
223 u8
*buf
= &rdbuf
[i
* tplen
+ offset
];
227 /* ignore Reserved events */
228 if (type
== TOUCH_EVENT_RESERVED
)
231 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
232 if (tsdata
->version
== M06
&& type
== TOUCH_EVENT_DOWN
)
235 x
= ((buf
[0] << 8) | buf
[1]) & 0x0fff;
236 y
= ((buf
[2] << 8) | buf
[3]) & 0x0fff;
237 id
= (buf
[2] >> 4) & 0x0f;
238 down
= type
!= TOUCH_EVENT_UP
;
240 input_mt_slot(tsdata
->input
, id
);
241 input_mt_report_slot_state(tsdata
->input
, MT_TOOL_FINGER
, down
);
246 input_report_abs(tsdata
->input
, ABS_MT_POSITION_X
, x
);
247 input_report_abs(tsdata
->input
, ABS_MT_POSITION_Y
, y
);
250 input_mt_report_pointer_emulation(tsdata
->input
, true);
251 input_sync(tsdata
->input
);
257 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data
*tsdata
,
262 switch (tsdata
->version
) {
264 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
265 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
267 wrbuf
[3] = wrbuf
[0] ^ wrbuf
[1] ^ wrbuf
[2];
268 return edt_ft5x06_ts_readwrite(tsdata
->client
, 4,
274 return edt_ft5x06_ts_readwrite(tsdata
->client
, 2,
282 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data
*tsdata
,
285 u8 wrbuf
[2], rdbuf
[2];
288 switch (tsdata
->version
) {
290 wrbuf
[0] = tsdata
->factory_mode
? 0xf3 : 0xfc;
291 wrbuf
[1] = tsdata
->factory_mode
? addr
& 0x7f : addr
& 0x3f;
292 wrbuf
[1] |= tsdata
->factory_mode
? 0x80 : 0x40;
294 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 2, wrbuf
, 2,
299 if ((wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0]) != rdbuf
[1]) {
300 dev_err(&tsdata
->client
->dev
,
301 "crc error: 0x%02x expected, got 0x%02x\n",
302 wrbuf
[0] ^ wrbuf
[1] ^ rdbuf
[0],
310 error
= edt_ft5x06_ts_readwrite(tsdata
->client
, 1,
323 struct edt_ft5x06_attribute
{
324 struct device_attribute dattr
;
332 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
333 _limit_low, _limit_high) \
334 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
335 .dattr = __ATTR(_field, _mode, \
336 edt_ft5x06_setting_show, \
337 edt_ft5x06_setting_store), \
338 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
339 .addr_m06 = _addr_m06, \
340 .addr_m09 = _addr_m09, \
341 .limit_low = _limit_low, \
342 .limit_high = _limit_high, \
345 static ssize_t
edt_ft5x06_setting_show(struct device
*dev
,
346 struct device_attribute
*dattr
,
349 struct i2c_client
*client
= to_i2c_client(dev
);
350 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
351 struct edt_ft5x06_attribute
*attr
=
352 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
353 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
359 mutex_lock(&tsdata
->mutex
);
361 if (tsdata
->factory_mode
) {
366 switch (tsdata
->version
) {
368 addr
= attr
->addr_m06
;
372 addr
= attr
->addr_m09
;
380 if (addr
!= NO_REGISTER
) {
381 val
= edt_ft5x06_register_read(tsdata
, addr
);
384 dev_err(&tsdata
->client
->dev
,
385 "Failed to fetch attribute %s, error %d\n",
386 dattr
->attr
.name
, error
);
394 dev_warn(&tsdata
->client
->dev
,
395 "%s: read (%d) and stored value (%d) differ\n",
396 dattr
->attr
.name
, val
, *field
);
400 count
= scnprintf(buf
, PAGE_SIZE
, "%d\n", val
);
402 mutex_unlock(&tsdata
->mutex
);
403 return error
?: count
;
406 static ssize_t
edt_ft5x06_setting_store(struct device
*dev
,
407 struct device_attribute
*dattr
,
408 const char *buf
, size_t count
)
410 struct i2c_client
*client
= to_i2c_client(dev
);
411 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
412 struct edt_ft5x06_attribute
*attr
=
413 container_of(dattr
, struct edt_ft5x06_attribute
, dattr
);
414 u8
*field
= (u8
*)tsdata
+ attr
->field_offset
;
419 mutex_lock(&tsdata
->mutex
);
421 if (tsdata
->factory_mode
) {
426 error
= kstrtouint(buf
, 0, &val
);
430 if (val
< attr
->limit_low
|| val
> attr
->limit_high
) {
435 switch (tsdata
->version
) {
437 addr
= attr
->addr_m06
;
441 addr
= attr
->addr_m09
;
449 if (addr
!= NO_REGISTER
) {
450 error
= edt_ft5x06_register_write(tsdata
, addr
, val
);
452 dev_err(&tsdata
->client
->dev
,
453 "Failed to update attribute %s, error: %d\n",
454 dattr
->attr
.name
, error
);
461 mutex_unlock(&tsdata
->mutex
);
462 return error
?: count
;
465 static EDT_ATTR(gain
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_GAIN
,
466 M09_REGISTER_GAIN
, 0, 31);
467 static EDT_ATTR(offset
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_OFFSET
,
468 M09_REGISTER_OFFSET
, 0, 31);
469 static EDT_ATTR(threshold
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_THRESHOLD
,
470 M09_REGISTER_THRESHOLD
, 20, 80);
471 static EDT_ATTR(report_rate
, S_IWUSR
| S_IRUGO
, WORK_REGISTER_REPORT_RATE
,
474 static struct attribute
*edt_ft5x06_attrs
[] = {
475 &edt_ft5x06_attr_gain
.dattr
.attr
,
476 &edt_ft5x06_attr_offset
.dattr
.attr
,
477 &edt_ft5x06_attr_threshold
.dattr
.attr
,
478 &edt_ft5x06_attr_report_rate
.dattr
.attr
,
482 static const struct attribute_group edt_ft5x06_attr_group
= {
483 .attrs
= edt_ft5x06_attrs
,
486 #ifdef CONFIG_DEBUG_FS
487 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data
*tsdata
)
489 struct i2c_client
*client
= tsdata
->client
;
490 int retries
= EDT_SWITCH_MODE_RETRIES
;
494 disable_irq(client
->irq
);
496 if (!tsdata
->raw_buffer
) {
497 tsdata
->raw_bufsize
= tsdata
->num_x
* tsdata
->num_y
*
499 tsdata
->raw_buffer
= kzalloc(tsdata
->raw_bufsize
, GFP_KERNEL
);
500 if (!tsdata
->raw_buffer
) {
506 /* mode register is 0x3c when in the work mode */
507 if (tsdata
->version
== M09
)
510 error
= edt_ft5x06_register_write(tsdata
, WORK_REGISTER_OPMODE
, 0x03);
512 dev_err(&client
->dev
,
513 "failed to switch to factory mode, error %d\n", error
);
517 tsdata
->factory_mode
= true;
519 mdelay(EDT_SWITCH_MODE_DELAY
);
520 /* mode register is 0x01 when in factory mode */
521 ret
= edt_ft5x06_register_read(tsdata
, FACTORY_REGISTER_OPMODE
);
524 } while (--retries
> 0);
527 dev_err(&client
->dev
, "not in factory mode after %dms.\n",
528 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
536 kfree(tsdata
->raw_buffer
);
537 tsdata
->raw_buffer
= NULL
;
538 tsdata
->factory_mode
= false;
539 enable_irq(client
->irq
);
544 dev_err(&client
->dev
, "No factory mode support for M09\n");
549 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data
*tsdata
)
551 struct i2c_client
*client
= tsdata
->client
;
552 int retries
= EDT_SWITCH_MODE_RETRIES
;
553 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
557 /* mode register is 0x01 when in the factory mode */
558 error
= edt_ft5x06_register_write(tsdata
, FACTORY_REGISTER_OPMODE
, 0x1);
560 dev_err(&client
->dev
,
561 "failed to switch to work mode, error: %d\n", error
);
565 tsdata
->factory_mode
= false;
568 mdelay(EDT_SWITCH_MODE_DELAY
);
569 /* mode register is 0x01 when in factory mode */
570 ret
= edt_ft5x06_register_read(tsdata
, WORK_REGISTER_OPMODE
);
573 } while (--retries
> 0);
576 dev_err(&client
->dev
, "not in work mode after %dms.\n",
577 EDT_SWITCH_MODE_RETRIES
* EDT_SWITCH_MODE_DELAY
);
578 tsdata
->factory_mode
= true;
582 kfree(tsdata
->raw_buffer
);
583 tsdata
->raw_buffer
= NULL
;
585 /* restore parameters */
586 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_threshold
,
588 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_gain
,
590 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_offset
,
592 if (reg_addr
->reg_report_rate
)
593 edt_ft5x06_register_write(tsdata
, reg_addr
->reg_report_rate
,
594 tsdata
->report_rate
);
596 enable_irq(client
->irq
);
601 static int edt_ft5x06_debugfs_mode_get(void *data
, u64
*mode
)
603 struct edt_ft5x06_ts_data
*tsdata
= data
;
605 *mode
= tsdata
->factory_mode
;
610 static int edt_ft5x06_debugfs_mode_set(void *data
, u64 mode
)
612 struct edt_ft5x06_ts_data
*tsdata
= data
;
618 mutex_lock(&tsdata
->mutex
);
620 if (mode
!= tsdata
->factory_mode
) {
621 retval
= mode
? edt_ft5x06_factory_mode(tsdata
) :
622 edt_ft5x06_work_mode(tsdata
);
625 mutex_unlock(&tsdata
->mutex
);
630 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops
, edt_ft5x06_debugfs_mode_get
,
631 edt_ft5x06_debugfs_mode_set
, "%llu\n");
633 static ssize_t
edt_ft5x06_debugfs_raw_data_read(struct file
*file
,
634 char __user
*buf
, size_t count
, loff_t
*off
)
636 struct edt_ft5x06_ts_data
*tsdata
= file
->private_data
;
637 struct i2c_client
*client
= tsdata
->client
;
638 int retries
= EDT_RAW_DATA_RETRIES
;
645 if (*off
< 0 || *off
>= tsdata
->raw_bufsize
)
648 mutex_lock(&tsdata
->mutex
);
650 if (!tsdata
->factory_mode
|| !tsdata
->raw_buffer
) {
655 error
= edt_ft5x06_register_write(tsdata
, 0x08, 0x01);
657 dev_dbg(&client
->dev
,
658 "failed to write 0x08 register, error %d\n", error
);
663 msleep(EDT_RAW_DATA_DELAY
);
664 val
= edt_ft5x06_register_read(tsdata
, 0x08);
667 } while (--retries
> 0);
671 dev_dbg(&client
->dev
,
672 "failed to read 0x08 register, error %d\n", error
);
677 dev_dbg(&client
->dev
,
678 "timed out waiting for register to settle\n");
683 rdbuf
= tsdata
->raw_buffer
;
684 colbytes
= tsdata
->num_y
* sizeof(u16
);
688 for (i
= 0; i
< tsdata
->num_x
; i
++) {
689 wrbuf
[2] = i
; /* column index */
690 error
= edt_ft5x06_ts_readwrite(tsdata
->client
,
691 sizeof(wrbuf
), wrbuf
,
699 read
= min_t(size_t, count
, tsdata
->raw_bufsize
- *off
);
700 if (copy_to_user(buf
, tsdata
->raw_buffer
+ *off
, read
)) {
707 mutex_unlock(&tsdata
->mutex
);
708 return error
?: read
;
711 static const struct file_operations debugfs_raw_data_fops
= {
713 .read
= edt_ft5x06_debugfs_raw_data_read
,
717 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
718 const char *debugfs_name
)
720 tsdata
->debug_dir
= debugfs_create_dir(debugfs_name
, NULL
);
721 if (!tsdata
->debug_dir
)
724 debugfs_create_u16("num_x", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_x
);
725 debugfs_create_u16("num_y", S_IRUSR
, tsdata
->debug_dir
, &tsdata
->num_y
);
727 debugfs_create_file("mode", S_IRUSR
| S_IWUSR
,
728 tsdata
->debug_dir
, tsdata
, &debugfs_mode_fops
);
729 debugfs_create_file("raw_data", S_IRUSR
,
730 tsdata
->debug_dir
, tsdata
, &debugfs_raw_data_fops
);
734 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
736 debugfs_remove_recursive(tsdata
->debug_dir
);
737 kfree(tsdata
->raw_buffer
);
743 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data
*tsdata
,
744 const char *debugfs_name
)
749 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data
*tsdata
)
753 #endif /* CONFIG_DEBUGFS */
755 static int edt_ft5x06_ts_reset(struct i2c_client
*client
,
756 struct edt_ft5x06_ts_data
*tsdata
)
760 if (gpio_is_valid(tsdata
->wake_pin
)) {
761 error
= devm_gpio_request_one(&client
->dev
,
762 tsdata
->wake_pin
, GPIOF_OUT_INIT_LOW
,
765 dev_err(&client
->dev
,
766 "Failed to request GPIO %d as wake pin, error %d\n",
767 tsdata
->wake_pin
, error
);
772 gpio_set_value(tsdata
->wake_pin
, 1);
774 if (gpio_is_valid(tsdata
->reset_pin
)) {
775 /* this pulls reset down, enabling the low active reset */
776 error
= devm_gpio_request_one(&client
->dev
,
777 tsdata
->reset_pin
, GPIOF_OUT_INIT_LOW
,
780 dev_err(&client
->dev
,
781 "Failed to request GPIO %d as reset pin, error %d\n",
782 tsdata
->reset_pin
, error
);
787 gpio_set_value(tsdata
->reset_pin
, 1);
794 static int edt_ft5x06_ts_identify(struct i2c_client
*client
,
795 struct edt_ft5x06_ts_data
*tsdata
,
798 u8 rdbuf
[EDT_NAME_LEN
];
801 char *model_name
= tsdata
->name
;
803 /* see what we find if we assume it is a M06 *
804 * if we get less than EDT_NAME_LEN, we don't want
805 * to have garbage in there
807 memset(rdbuf
, 0, sizeof(rdbuf
));
808 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xbb",
809 EDT_NAME_LEN
- 1, rdbuf
);
813 /* if we find something consistent, stay with that assumption
814 * at least M09 won't send 3 bytes here
816 if (!(strncasecmp(rdbuf
+ 1, "EP0", 3))) {
817 tsdata
->version
= M06
;
819 /* remove last '$' end marker */
820 rdbuf
[EDT_NAME_LEN
- 1] = '\0';
821 if (rdbuf
[EDT_NAME_LEN
- 2] == '$')
822 rdbuf
[EDT_NAME_LEN
- 2] = '\0';
824 /* look for Model/Version separator */
825 p
= strchr(rdbuf
, '*');
828 strlcpy(model_name
, rdbuf
+ 1, EDT_NAME_LEN
);
829 strlcpy(fw_version
, p
? p
: "", EDT_NAME_LEN
);
831 /* since there are only two versions around (M06, M09) */
832 tsdata
->version
= M09
;
834 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA6",
839 strlcpy(fw_version
, rdbuf
, 2);
841 error
= edt_ft5x06_ts_readwrite(client
, 1, "\xA8",
846 snprintf(model_name
, EDT_NAME_LEN
, "EP0%i%i0M09",
847 rdbuf
[0] >> 4, rdbuf
[0] & 0x0F);
853 #define EDT_ATTR_CHECKSET(name, reg) \
855 if (pdata->name >= edt_ft5x06_attr_##name.limit_low && \
856 pdata->name <= edt_ft5x06_attr_##name.limit_high) \
857 edt_ft5x06_register_write(tsdata, reg, pdata->name); \
860 #define EDT_GET_PROP(name, reg) { \
862 if (of_property_read_u32(np, #name, &val) == 0) \
863 edt_ft5x06_register_write(tsdata, reg, val); \
866 static void edt_ft5x06_ts_get_dt_defaults(struct device_node
*np
,
867 struct edt_ft5x06_ts_data
*tsdata
)
869 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
871 EDT_GET_PROP(threshold
, reg_addr
->reg_threshold
);
872 EDT_GET_PROP(gain
, reg_addr
->reg_gain
);
873 EDT_GET_PROP(offset
, reg_addr
->reg_offset
);
877 edt_ft5x06_ts_get_defaults(struct edt_ft5x06_ts_data
*tsdata
,
878 const struct edt_ft5x06_platform_data
*pdata
)
880 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
882 if (!pdata
->use_parameters
)
885 /* pick up defaults from the platform data */
886 EDT_ATTR_CHECKSET(threshold
, reg_addr
->reg_threshold
);
887 EDT_ATTR_CHECKSET(gain
, reg_addr
->reg_gain
);
888 EDT_ATTR_CHECKSET(offset
, reg_addr
->reg_offset
);
889 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
890 EDT_ATTR_CHECKSET(report_rate
, reg_addr
->reg_report_rate
);
894 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data
*tsdata
)
896 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
898 tsdata
->threshold
= edt_ft5x06_register_read(tsdata
,
899 reg_addr
->reg_threshold
);
900 tsdata
->gain
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_gain
);
901 tsdata
->offset
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_offset
);
902 if (reg_addr
->reg_report_rate
!= NO_REGISTER
)
903 tsdata
->report_rate
= edt_ft5x06_register_read(tsdata
,
904 reg_addr
->reg_report_rate
);
905 tsdata
->num_x
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_num_x
);
906 tsdata
->num_y
= edt_ft5x06_register_read(tsdata
, reg_addr
->reg_num_y
);
910 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data
*tsdata
)
912 struct edt_reg_addr
*reg_addr
= &tsdata
->reg_addr
;
914 switch (tsdata
->version
) {
916 reg_addr
->reg_threshold
= WORK_REGISTER_THRESHOLD
;
917 reg_addr
->reg_report_rate
= WORK_REGISTER_REPORT_RATE
;
918 reg_addr
->reg_gain
= WORK_REGISTER_GAIN
;
919 reg_addr
->reg_offset
= WORK_REGISTER_OFFSET
;
920 reg_addr
->reg_num_x
= WORK_REGISTER_NUM_X
;
921 reg_addr
->reg_num_y
= WORK_REGISTER_NUM_Y
;
925 reg_addr
->reg_threshold
= M09_REGISTER_THRESHOLD
;
926 reg_addr
->reg_gain
= M09_REGISTER_GAIN
;
927 reg_addr
->reg_offset
= M09_REGISTER_OFFSET
;
928 reg_addr
->reg_num_x
= M09_REGISTER_NUM_X
;
929 reg_addr
->reg_num_y
= M09_REGISTER_NUM_Y
;
935 static int edt_ft5x06_i2c_ts_probe_dt(struct device
*dev
,
936 struct edt_ft5x06_ts_data
*tsdata
)
938 struct device_node
*np
= dev
->of_node
;
941 * irq_pin is not needed for DT setup.
942 * irq is associated via 'interrupts' property in DT
944 tsdata
->irq_pin
= -EINVAL
;
945 tsdata
->reset_pin
= of_get_named_gpio(np
, "reset-gpios", 0);
946 tsdata
->wake_pin
= of_get_named_gpio(np
, "wake-gpios", 0);
951 static inline int edt_ft5x06_i2c_ts_probe_dt(struct device
*dev
,
952 struct edt_ft5x06_ts_data
*tsdata
)
958 static int edt_ft5x06_ts_probe(struct i2c_client
*client
,
959 const struct i2c_device_id
*id
)
961 const struct edt_ft5x06_platform_data
*pdata
=
962 dev_get_platdata(&client
->dev
);
963 struct edt_ft5x06_ts_data
*tsdata
;
964 struct input_dev
*input
;
966 char fw_version
[EDT_NAME_LEN
];
968 dev_dbg(&client
->dev
, "probing for EDT FT5x06 I2C\n");
970 tsdata
= devm_kzalloc(&client
->dev
, sizeof(*tsdata
), GFP_KERNEL
);
972 dev_err(&client
->dev
, "failed to allocate driver data.\n");
977 error
= edt_ft5x06_i2c_ts_probe_dt(&client
->dev
, tsdata
);
979 dev_err(&client
->dev
,
980 "DT probe failed and no platform data present\n");
984 tsdata
->reset_pin
= pdata
->reset_pin
;
985 tsdata
->irq_pin
= pdata
->irq_pin
;
986 tsdata
->wake_pin
= -EINVAL
;
989 error
= edt_ft5x06_ts_reset(client
, tsdata
);
993 if (gpio_is_valid(tsdata
->irq_pin
)) {
994 error
= devm_gpio_request_one(&client
->dev
, tsdata
->irq_pin
,
995 GPIOF_IN
, "edt-ft5x06 irq");
997 dev_err(&client
->dev
,
998 "Failed to request GPIO %d, error %d\n",
999 tsdata
->irq_pin
, error
);
1004 input
= devm_input_allocate_device(&client
->dev
);
1006 dev_err(&client
->dev
, "failed to allocate input device.\n");
1010 mutex_init(&tsdata
->mutex
);
1011 tsdata
->client
= client
;
1012 tsdata
->input
= input
;
1013 tsdata
->factory_mode
= false;
1015 error
= edt_ft5x06_ts_identify(client
, tsdata
, fw_version
);
1017 dev_err(&client
->dev
, "touchscreen probe failed\n");
1021 edt_ft5x06_ts_set_regs(tsdata
);
1024 edt_ft5x06_ts_get_dt_defaults(client
->dev
.of_node
, tsdata
);
1026 edt_ft5x06_ts_get_defaults(tsdata
, pdata
);
1028 edt_ft5x06_ts_get_parameters(tsdata
);
1030 dev_dbg(&client
->dev
,
1031 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1032 tsdata
->name
, fw_version
, tsdata
->num_x
, tsdata
->num_y
);
1034 input
->name
= tsdata
->name
;
1035 input
->id
.bustype
= BUS_I2C
;
1036 input
->dev
.parent
= &client
->dev
;
1038 __set_bit(EV_KEY
, input
->evbit
);
1039 __set_bit(EV_ABS
, input
->evbit
);
1040 __set_bit(BTN_TOUCH
, input
->keybit
);
1041 input_set_abs_params(input
, ABS_X
, 0, tsdata
->num_x
* 64 - 1, 0, 0);
1042 input_set_abs_params(input
, ABS_Y
, 0, tsdata
->num_y
* 64 - 1, 0, 0);
1043 input_set_abs_params(input
, ABS_MT_POSITION_X
,
1044 0, tsdata
->num_x
* 64 - 1, 0, 0);
1045 input_set_abs_params(input
, ABS_MT_POSITION_Y
,
1046 0, tsdata
->num_y
* 64 - 1, 0, 0);
1049 touchscreen_parse_of_params(input
);
1051 error
= input_mt_init_slots(input
, MAX_SUPPORT_POINTS
, 0);
1053 dev_err(&client
->dev
, "Unable to init MT slots.\n");
1057 input_set_drvdata(input
, tsdata
);
1058 i2c_set_clientdata(client
, tsdata
);
1060 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
, NULL
,
1062 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1063 client
->name
, tsdata
);
1065 dev_err(&client
->dev
, "Unable to request touchscreen IRQ.\n");
1069 error
= sysfs_create_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1073 error
= input_register_device(input
);
1075 goto err_remove_attrs
;
1077 edt_ft5x06_ts_prepare_debugfs(tsdata
, dev_driver_string(&client
->dev
));
1078 device_init_wakeup(&client
->dev
, 1);
1080 dev_dbg(&client
->dev
,
1081 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1082 client
->irq
, tsdata
->wake_pin
, tsdata
->reset_pin
);
1087 sysfs_remove_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1091 static int edt_ft5x06_ts_remove(struct i2c_client
*client
)
1093 struct edt_ft5x06_ts_data
*tsdata
= i2c_get_clientdata(client
);
1095 edt_ft5x06_ts_teardown_debugfs(tsdata
);
1096 sysfs_remove_group(&client
->dev
.kobj
, &edt_ft5x06_attr_group
);
1101 static int __maybe_unused
edt_ft5x06_ts_suspend(struct device
*dev
)
1103 struct i2c_client
*client
= to_i2c_client(dev
);
1105 if (device_may_wakeup(dev
))
1106 enable_irq_wake(client
->irq
);
1111 static int __maybe_unused
edt_ft5x06_ts_resume(struct device
*dev
)
1113 struct i2c_client
*client
= to_i2c_client(dev
);
1115 if (device_may_wakeup(dev
))
1116 disable_irq_wake(client
->irq
);
1121 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops
,
1122 edt_ft5x06_ts_suspend
, edt_ft5x06_ts_resume
);
1124 static const struct i2c_device_id edt_ft5x06_ts_id
[] = {
1125 { "edt-ft5x06", 0, },
1128 MODULE_DEVICE_TABLE(i2c
, edt_ft5x06_ts_id
);
1131 static const struct of_device_id edt_ft5x06_of_match
[] = {
1132 { .compatible
= "edt,edt-ft5206", },
1133 { .compatible
= "edt,edt-ft5306", },
1134 { .compatible
= "edt,edt-ft5406", },
1137 MODULE_DEVICE_TABLE(of
, edt_ft5x06_of_match
);
1140 static struct i2c_driver edt_ft5x06_ts_driver
= {
1142 .owner
= THIS_MODULE
,
1143 .name
= "edt_ft5x06",
1144 .of_match_table
= of_match_ptr(edt_ft5x06_of_match
),
1145 .pm
= &edt_ft5x06_ts_pm_ops
,
1147 .id_table
= edt_ft5x06_ts_id
,
1148 .probe
= edt_ft5x06_ts_probe
,
1149 .remove
= edt_ft5x06_ts_remove
,
1152 module_i2c_driver(edt_ft5x06_ts_driver
);
1154 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1155 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1156 MODULE_LICENSE("GPL");