treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / input / touchscreen / edt-ft5x06.c
blobd61731c0037d15a93bd564afba83b83f353bb560
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
4 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
5 * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
6 */
8 /*
9 * This is a driver for the EDT "Polytouch" family of touch controllers
10 * based on the FocalTech FT5x06 line of chips.
12 * Development of this driver has been sponsored by Glyn:
13 * http://www.glyn.com/Products/Displays
16 #include <linux/module.h>
17 #include <linux/ratelimit.h>
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/input.h>
21 #include <linux/i2c.h>
22 #include <linux/kernel.h>
23 #include <linux/uaccess.h>
24 #include <linux/delay.h>
25 #include <linux/debugfs.h>
26 #include <linux/slab.h>
27 #include <linux/gpio/consumer.h>
28 #include <linux/input/mt.h>
29 #include <linux/input/touchscreen.h>
30 #include <asm/unaligned.h>
31 #include <linux/regulator/consumer.h>
33 #define WORK_REGISTER_THRESHOLD 0x00
34 #define WORK_REGISTER_REPORT_RATE 0x08
35 #define WORK_REGISTER_GAIN 0x30
36 #define WORK_REGISTER_OFFSET 0x31
37 #define WORK_REGISTER_NUM_X 0x33
38 #define WORK_REGISTER_NUM_Y 0x34
40 #define M09_REGISTER_THRESHOLD 0x80
41 #define M09_REGISTER_GAIN 0x92
42 #define M09_REGISTER_OFFSET 0x93
43 #define M09_REGISTER_NUM_X 0x94
44 #define M09_REGISTER_NUM_Y 0x95
46 #define EV_REGISTER_THRESHOLD 0x40
47 #define EV_REGISTER_GAIN 0x41
48 #define EV_REGISTER_OFFSET_Y 0x45
49 #define EV_REGISTER_OFFSET_X 0x46
51 #define NO_REGISTER 0xff
53 #define WORK_REGISTER_OPMODE 0x3c
54 #define FACTORY_REGISTER_OPMODE 0x01
56 #define TOUCH_EVENT_DOWN 0x00
57 #define TOUCH_EVENT_UP 0x01
58 #define TOUCH_EVENT_ON 0x02
59 #define TOUCH_EVENT_RESERVED 0x03
61 #define EDT_NAME_LEN 23
62 #define EDT_SWITCH_MODE_RETRIES 10
63 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
64 #define EDT_RAW_DATA_RETRIES 100
65 #define EDT_RAW_DATA_DELAY 1000 /* usec */
67 enum edt_ver {
68 EDT_M06,
69 EDT_M09,
70 EDT_M12,
71 EV_FT,
72 GENERIC_FT,
75 struct edt_reg_addr {
76 int reg_threshold;
77 int reg_report_rate;
78 int reg_gain;
79 int reg_offset;
80 int reg_offset_x;
81 int reg_offset_y;
82 int reg_num_x;
83 int reg_num_y;
86 struct edt_ft5x06_ts_data {
87 struct i2c_client *client;
88 struct input_dev *input;
89 struct touchscreen_properties prop;
90 u16 num_x;
91 u16 num_y;
92 struct regulator *vcc;
94 struct gpio_desc *reset_gpio;
95 struct gpio_desc *wake_gpio;
97 #if defined(CONFIG_DEBUG_FS)
98 struct dentry *debug_dir;
99 u8 *raw_buffer;
100 size_t raw_bufsize;
101 #endif
103 struct mutex mutex;
104 bool factory_mode;
105 int threshold;
106 int gain;
107 int offset;
108 int offset_x;
109 int offset_y;
110 int report_rate;
111 int max_support_points;
113 char name[EDT_NAME_LEN];
115 struct edt_reg_addr reg_addr;
116 enum edt_ver version;
119 struct edt_i2c_chip_data {
120 int max_support_points;
123 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
124 u16 wr_len, u8 *wr_buf,
125 u16 rd_len, u8 *rd_buf)
127 struct i2c_msg wrmsg[2];
128 int i = 0;
129 int ret;
131 if (wr_len) {
132 wrmsg[i].addr = client->addr;
133 wrmsg[i].flags = 0;
134 wrmsg[i].len = wr_len;
135 wrmsg[i].buf = wr_buf;
136 i++;
138 if (rd_len) {
139 wrmsg[i].addr = client->addr;
140 wrmsg[i].flags = I2C_M_RD;
141 wrmsg[i].len = rd_len;
142 wrmsg[i].buf = rd_buf;
143 i++;
146 ret = i2c_transfer(client->adapter, wrmsg, i);
147 if (ret < 0)
148 return ret;
149 if (ret != i)
150 return -EIO;
152 return 0;
155 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
156 u8 *buf, int buflen)
158 int i;
159 u8 crc = 0;
161 for (i = 0; i < buflen - 1; i++)
162 crc ^= buf[i];
164 if (crc != buf[buflen-1]) {
165 dev_err_ratelimited(&tsdata->client->dev,
166 "crc error: 0x%02x expected, got 0x%02x\n",
167 crc, buf[buflen-1]);
168 return false;
171 return true;
174 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
176 struct edt_ft5x06_ts_data *tsdata = dev_id;
177 struct device *dev = &tsdata->client->dev;
178 u8 cmd;
179 u8 rdbuf[63];
180 int i, type, x, y, id;
181 int offset, tplen, datalen, crclen;
182 int error;
184 switch (tsdata->version) {
185 case EDT_M06:
186 cmd = 0xf9; /* tell the controller to send touch data */
187 offset = 5; /* where the actual touch data starts */
188 tplen = 4; /* data comes in so called frames */
189 crclen = 1; /* length of the crc data */
190 break;
192 case EDT_M09:
193 case EDT_M12:
194 case EV_FT:
195 case GENERIC_FT:
196 cmd = 0x0;
197 offset = 3;
198 tplen = 6;
199 crclen = 0;
200 break;
202 default:
203 goto out;
206 memset(rdbuf, 0, sizeof(rdbuf));
207 datalen = tplen * tsdata->max_support_points + offset + crclen;
209 error = edt_ft5x06_ts_readwrite(tsdata->client,
210 sizeof(cmd), &cmd,
211 datalen, rdbuf);
212 if (error) {
213 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
214 error);
215 goto out;
218 /* M09/M12 does not send header or CRC */
219 if (tsdata->version == EDT_M06) {
220 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
221 rdbuf[2] != datalen) {
222 dev_err_ratelimited(dev,
223 "Unexpected header: %02x%02x%02x!\n",
224 rdbuf[0], rdbuf[1], rdbuf[2]);
225 goto out;
228 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
229 goto out;
232 for (i = 0; i < tsdata->max_support_points; i++) {
233 u8 *buf = &rdbuf[i * tplen + offset];
235 type = buf[0] >> 6;
236 /* ignore Reserved events */
237 if (type == TOUCH_EVENT_RESERVED)
238 continue;
240 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
241 if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
242 continue;
244 x = get_unaligned_be16(buf) & 0x0fff;
245 y = get_unaligned_be16(buf + 2) & 0x0fff;
246 /* The FT5x26 send the y coordinate first */
247 if (tsdata->version == EV_FT)
248 swap(x, y);
250 id = (buf[2] >> 4) & 0x0f;
252 input_mt_slot(tsdata->input, id);
253 if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
254 type != TOUCH_EVENT_UP))
255 touchscreen_report_pos(tsdata->input, &tsdata->prop,
256 x, y, true);
259 input_mt_report_pointer_emulation(tsdata->input, true);
260 input_sync(tsdata->input);
262 out:
263 return IRQ_HANDLED;
266 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
267 u8 addr, u8 value)
269 u8 wrbuf[4];
271 switch (tsdata->version) {
272 case EDT_M06:
273 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
274 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
275 wrbuf[2] = value;
276 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
277 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
278 wrbuf, 0, NULL);
279 /* fallthrough */
280 case EDT_M09:
281 case EDT_M12:
282 case EV_FT:
283 case GENERIC_FT:
284 wrbuf[0] = addr;
285 wrbuf[1] = value;
287 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
288 wrbuf, 0, NULL);
290 default:
291 return -EINVAL;
295 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
296 u8 addr)
298 u8 wrbuf[2], rdbuf[2];
299 int error;
301 switch (tsdata->version) {
302 case EDT_M06:
303 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
304 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
305 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
307 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
308 rdbuf);
309 if (error)
310 return error;
312 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
313 dev_err(&tsdata->client->dev,
314 "crc error: 0x%02x expected, got 0x%02x\n",
315 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
316 rdbuf[1]);
317 return -EIO;
319 break;
321 /* fallthrough */
322 case EDT_M09:
323 case EDT_M12:
324 case EV_FT:
325 case GENERIC_FT:
326 wrbuf[0] = addr;
327 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
328 wrbuf, 1, rdbuf);
329 if (error)
330 return error;
331 break;
333 default:
334 return -EINVAL;
337 return rdbuf[0];
340 struct edt_ft5x06_attribute {
341 struct device_attribute dattr;
342 size_t field_offset;
343 u8 limit_low;
344 u8 limit_high;
345 u8 addr_m06;
346 u8 addr_m09;
347 u8 addr_ev;
350 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
351 _limit_low, _limit_high) \
352 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
353 .dattr = __ATTR(_field, _mode, \
354 edt_ft5x06_setting_show, \
355 edt_ft5x06_setting_store), \
356 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
357 .addr_m06 = _addr_m06, \
358 .addr_m09 = _addr_m09, \
359 .addr_ev = _addr_ev, \
360 .limit_low = _limit_low, \
361 .limit_high = _limit_high, \
364 static ssize_t edt_ft5x06_setting_show(struct device *dev,
365 struct device_attribute *dattr,
366 char *buf)
368 struct i2c_client *client = to_i2c_client(dev);
369 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
370 struct edt_ft5x06_attribute *attr =
371 container_of(dattr, struct edt_ft5x06_attribute, dattr);
372 u8 *field = (u8 *)tsdata + attr->field_offset;
373 int val;
374 size_t count = 0;
375 int error = 0;
376 u8 addr;
378 mutex_lock(&tsdata->mutex);
380 if (tsdata->factory_mode) {
381 error = -EIO;
382 goto out;
385 switch (tsdata->version) {
386 case EDT_M06:
387 addr = attr->addr_m06;
388 break;
390 case EDT_M09:
391 case EDT_M12:
392 case GENERIC_FT:
393 addr = attr->addr_m09;
394 break;
396 case EV_FT:
397 addr = attr->addr_ev;
398 break;
400 default:
401 error = -ENODEV;
402 goto out;
405 if (addr != NO_REGISTER) {
406 val = edt_ft5x06_register_read(tsdata, addr);
407 if (val < 0) {
408 error = val;
409 dev_err(&tsdata->client->dev,
410 "Failed to fetch attribute %s, error %d\n",
411 dattr->attr.name, error);
412 goto out;
414 } else {
415 val = *field;
418 if (val != *field) {
419 dev_warn(&tsdata->client->dev,
420 "%s: read (%d) and stored value (%d) differ\n",
421 dattr->attr.name, val, *field);
422 *field = val;
425 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
426 out:
427 mutex_unlock(&tsdata->mutex);
428 return error ?: count;
431 static ssize_t edt_ft5x06_setting_store(struct device *dev,
432 struct device_attribute *dattr,
433 const char *buf, size_t count)
435 struct i2c_client *client = to_i2c_client(dev);
436 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
437 struct edt_ft5x06_attribute *attr =
438 container_of(dattr, struct edt_ft5x06_attribute, dattr);
439 u8 *field = (u8 *)tsdata + attr->field_offset;
440 unsigned int val;
441 int error;
442 u8 addr;
444 mutex_lock(&tsdata->mutex);
446 if (tsdata->factory_mode) {
447 error = -EIO;
448 goto out;
451 error = kstrtouint(buf, 0, &val);
452 if (error)
453 goto out;
455 if (val < attr->limit_low || val > attr->limit_high) {
456 error = -ERANGE;
457 goto out;
460 switch (tsdata->version) {
461 case EDT_M06:
462 addr = attr->addr_m06;
463 break;
465 case EDT_M09:
466 case EDT_M12:
467 case GENERIC_FT:
468 addr = attr->addr_m09;
469 break;
471 case EV_FT:
472 addr = attr->addr_ev;
473 break;
475 default:
476 error = -ENODEV;
477 goto out;
480 if (addr != NO_REGISTER) {
481 error = edt_ft5x06_register_write(tsdata, addr, val);
482 if (error) {
483 dev_err(&tsdata->client->dev,
484 "Failed to update attribute %s, error: %d\n",
485 dattr->attr.name, error);
486 goto out;
489 *field = val;
491 out:
492 mutex_unlock(&tsdata->mutex);
493 return error ?: count;
496 /* m06, m09: range 0-31, m12: range 0-5 */
497 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
498 M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31);
499 /* m06, m09: range 0-31, m12: range 0-16 */
500 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
501 M09_REGISTER_OFFSET, NO_REGISTER, 0, 31);
502 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
503 static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
504 EV_REGISTER_OFFSET_X, 0, 80);
505 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
506 static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
507 EV_REGISTER_OFFSET_Y, 0, 80);
508 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
509 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
510 M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255);
511 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
512 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
513 NO_REGISTER, NO_REGISTER, 0, 255);
515 static struct attribute *edt_ft5x06_attrs[] = {
516 &edt_ft5x06_attr_gain.dattr.attr,
517 &edt_ft5x06_attr_offset.dattr.attr,
518 &edt_ft5x06_attr_offset_x.dattr.attr,
519 &edt_ft5x06_attr_offset_y.dattr.attr,
520 &edt_ft5x06_attr_threshold.dattr.attr,
521 &edt_ft5x06_attr_report_rate.dattr.attr,
522 NULL
525 static const struct attribute_group edt_ft5x06_attr_group = {
526 .attrs = edt_ft5x06_attrs,
529 #ifdef CONFIG_DEBUG_FS
530 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
532 struct i2c_client *client = tsdata->client;
533 int retries = EDT_SWITCH_MODE_RETRIES;
534 int ret;
535 int error;
537 if (tsdata->version != EDT_M06) {
538 dev_err(&client->dev,
539 "No factory mode support for non-M06 devices\n");
540 return -EINVAL;
543 disable_irq(client->irq);
545 if (!tsdata->raw_buffer) {
546 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
547 sizeof(u16);
548 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
549 if (!tsdata->raw_buffer) {
550 error = -ENOMEM;
551 goto err_out;
555 /* mode register is 0x3c when in the work mode */
556 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
557 if (error) {
558 dev_err(&client->dev,
559 "failed to switch to factory mode, error %d\n", error);
560 goto err_out;
563 tsdata->factory_mode = true;
564 do {
565 mdelay(EDT_SWITCH_MODE_DELAY);
566 /* mode register is 0x01 when in factory mode */
567 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
568 if (ret == 0x03)
569 break;
570 } while (--retries > 0);
572 if (retries == 0) {
573 dev_err(&client->dev, "not in factory mode after %dms.\n",
574 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
575 error = -EIO;
576 goto err_out;
579 return 0;
581 err_out:
582 kfree(tsdata->raw_buffer);
583 tsdata->raw_buffer = NULL;
584 tsdata->factory_mode = false;
585 enable_irq(client->irq);
587 return error;
590 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
592 struct i2c_client *client = tsdata->client;
593 int retries = EDT_SWITCH_MODE_RETRIES;
594 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
595 int ret;
596 int error;
598 /* mode register is 0x01 when in the factory mode */
599 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
600 if (error) {
601 dev_err(&client->dev,
602 "failed to switch to work mode, error: %d\n", error);
603 return error;
606 tsdata->factory_mode = false;
608 do {
609 mdelay(EDT_SWITCH_MODE_DELAY);
610 /* mode register is 0x01 when in factory mode */
611 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
612 if (ret == 0x01)
613 break;
614 } while (--retries > 0);
616 if (retries == 0) {
617 dev_err(&client->dev, "not in work mode after %dms.\n",
618 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
619 tsdata->factory_mode = true;
620 return -EIO;
623 kfree(tsdata->raw_buffer);
624 tsdata->raw_buffer = NULL;
626 /* restore parameters */
627 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
628 tsdata->threshold);
629 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
630 tsdata->gain);
631 if (reg_addr->reg_offset != NO_REGISTER)
632 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
633 tsdata->offset);
634 if (reg_addr->reg_offset_x != NO_REGISTER)
635 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x,
636 tsdata->offset_x);
637 if (reg_addr->reg_offset_y != NO_REGISTER)
638 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y,
639 tsdata->offset_y);
640 if (reg_addr->reg_report_rate != NO_REGISTER)
641 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
642 tsdata->report_rate);
644 enable_irq(client->irq);
646 return 0;
649 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
651 struct edt_ft5x06_ts_data *tsdata = data;
653 *mode = tsdata->factory_mode;
655 return 0;
658 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
660 struct edt_ft5x06_ts_data *tsdata = data;
661 int retval = 0;
663 if (mode > 1)
664 return -ERANGE;
666 mutex_lock(&tsdata->mutex);
668 if (mode != tsdata->factory_mode) {
669 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
670 edt_ft5x06_work_mode(tsdata);
673 mutex_unlock(&tsdata->mutex);
675 return retval;
678 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
679 edt_ft5x06_debugfs_mode_set, "%llu\n");
681 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
682 char __user *buf, size_t count, loff_t *off)
684 struct edt_ft5x06_ts_data *tsdata = file->private_data;
685 struct i2c_client *client = tsdata->client;
686 int retries = EDT_RAW_DATA_RETRIES;
687 int val, i, error;
688 size_t read = 0;
689 int colbytes;
690 char wrbuf[3];
691 u8 *rdbuf;
693 if (*off < 0 || *off >= tsdata->raw_bufsize)
694 return 0;
696 mutex_lock(&tsdata->mutex);
698 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
699 error = -EIO;
700 goto out;
703 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
704 if (error) {
705 dev_dbg(&client->dev,
706 "failed to write 0x08 register, error %d\n", error);
707 goto out;
710 do {
711 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
712 val = edt_ft5x06_register_read(tsdata, 0x08);
713 if (val < 1)
714 break;
715 } while (--retries > 0);
717 if (val < 0) {
718 error = val;
719 dev_dbg(&client->dev,
720 "failed to read 0x08 register, error %d\n", error);
721 goto out;
724 if (retries == 0) {
725 dev_dbg(&client->dev,
726 "timed out waiting for register to settle\n");
727 error = -ETIMEDOUT;
728 goto out;
731 rdbuf = tsdata->raw_buffer;
732 colbytes = tsdata->num_y * sizeof(u16);
734 wrbuf[0] = 0xf5;
735 wrbuf[1] = 0x0e;
736 for (i = 0; i < tsdata->num_x; i++) {
737 wrbuf[2] = i; /* column index */
738 error = edt_ft5x06_ts_readwrite(tsdata->client,
739 sizeof(wrbuf), wrbuf,
740 colbytes, rdbuf);
741 if (error)
742 goto out;
744 rdbuf += colbytes;
747 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
748 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
749 error = -EFAULT;
750 goto out;
753 *off += read;
754 out:
755 mutex_unlock(&tsdata->mutex);
756 return error ?: read;
759 static const struct file_operations debugfs_raw_data_fops = {
760 .open = simple_open,
761 .read = edt_ft5x06_debugfs_raw_data_read,
764 static void
765 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
766 const char *debugfs_name)
768 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
770 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
771 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
773 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
774 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
775 debugfs_create_file("raw_data", S_IRUSR,
776 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
779 static void
780 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
782 debugfs_remove_recursive(tsdata->debug_dir);
783 kfree(tsdata->raw_buffer);
786 #else
788 static inline void
789 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
790 const char *debugfs_name)
794 static inline void
795 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
799 #endif /* CONFIG_DEBUGFS */
801 static int edt_ft5x06_ts_identify(struct i2c_client *client,
802 struct edt_ft5x06_ts_data *tsdata,
803 char *fw_version)
805 u8 rdbuf[EDT_NAME_LEN];
806 char *p;
807 int error;
808 char *model_name = tsdata->name;
810 /* see what we find if we assume it is a M06 *
811 * if we get less than EDT_NAME_LEN, we don't want
812 * to have garbage in there
814 memset(rdbuf, 0, sizeof(rdbuf));
815 error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
816 EDT_NAME_LEN - 1, rdbuf);
817 if (error)
818 return error;
820 /* Probe content for something consistent.
821 * M06 starts with a response byte, M12 gives the data directly.
822 * M09/Generic does not provide model number information.
824 if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
825 tsdata->version = EDT_M06;
827 /* remove last '$' end marker */
828 rdbuf[EDT_NAME_LEN - 1] = '\0';
829 if (rdbuf[EDT_NAME_LEN - 2] == '$')
830 rdbuf[EDT_NAME_LEN - 2] = '\0';
832 /* look for Model/Version separator */
833 p = strchr(rdbuf, '*');
834 if (p)
835 *p++ = '\0';
836 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
837 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
838 } else if (!strncasecmp(rdbuf, "EP0", 3)) {
839 tsdata->version = EDT_M12;
841 /* remove last '$' end marker */
842 rdbuf[EDT_NAME_LEN - 2] = '\0';
843 if (rdbuf[EDT_NAME_LEN - 3] == '$')
844 rdbuf[EDT_NAME_LEN - 3] = '\0';
846 /* look for Model/Version separator */
847 p = strchr(rdbuf, '*');
848 if (p)
849 *p++ = '\0';
850 strlcpy(model_name, rdbuf, EDT_NAME_LEN);
851 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
852 } else {
853 /* If it is not an EDT M06/M12 touchscreen, then the model
854 * detection is a bit hairy. The different ft5x06
855 * firmares around don't reliably implement the
856 * identification registers. Well, we'll take a shot.
858 * The main difference between generic focaltec based
859 * touches and EDT M09 is that we know how to retrieve
860 * the max coordinates for the latter.
862 tsdata->version = GENERIC_FT;
864 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
865 2, rdbuf);
866 if (error)
867 return error;
869 strlcpy(fw_version, rdbuf, 2);
871 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
872 1, rdbuf);
873 if (error)
874 return error;
876 /* This "model identification" is not exact. Unfortunately
877 * not all firmwares for the ft5x06 put useful values in
878 * the identification registers.
880 switch (rdbuf[0]) {
881 case 0x35: /* EDT EP0350M09 */
882 case 0x43: /* EDT EP0430M09 */
883 case 0x50: /* EDT EP0500M09 */
884 case 0x57: /* EDT EP0570M09 */
885 case 0x70: /* EDT EP0700M09 */
886 tsdata->version = EDT_M09;
887 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
888 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
889 break;
890 case 0xa1: /* EDT EP1010ML00 */
891 tsdata->version = EDT_M09;
892 snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
893 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
894 break;
895 case 0x5a: /* Solomon Goldentek Display */
896 snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
897 break;
898 case 0x59: /* Evervision Display with FT5xx6 TS */
899 tsdata->version = EV_FT;
900 error = edt_ft5x06_ts_readwrite(client, 1, "\x53",
901 1, rdbuf);
902 if (error)
903 return error;
904 strlcpy(fw_version, rdbuf, 1);
905 snprintf(model_name, EDT_NAME_LEN,
906 "EVERVISION-FT5726NEi");
907 break;
908 default:
909 snprintf(model_name, EDT_NAME_LEN,
910 "generic ft5x06 (%02x)",
911 rdbuf[0]);
912 break;
916 return 0;
919 static void edt_ft5x06_ts_get_defaults(struct device *dev,
920 struct edt_ft5x06_ts_data *tsdata)
922 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
923 u32 val;
924 int error;
926 error = device_property_read_u32(dev, "threshold", &val);
927 if (!error) {
928 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
929 tsdata->threshold = val;
932 error = device_property_read_u32(dev, "gain", &val);
933 if (!error) {
934 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
935 tsdata->gain = val;
938 error = device_property_read_u32(dev, "offset", &val);
939 if (!error) {
940 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
941 tsdata->offset = val;
944 error = device_property_read_u32(dev, "offset-x", &val);
945 if (!error) {
946 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x, val);
947 tsdata->offset_x = val;
950 error = device_property_read_u32(dev, "offset-y", &val);
951 if (!error) {
952 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y, val);
953 tsdata->offset_y = val;
957 static void
958 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
960 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
962 tsdata->threshold = edt_ft5x06_register_read(tsdata,
963 reg_addr->reg_threshold);
964 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
965 if (reg_addr->reg_offset != NO_REGISTER)
966 tsdata->offset =
967 edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
968 if (reg_addr->reg_offset_x != NO_REGISTER)
969 tsdata->offset_x = edt_ft5x06_register_read(tsdata,
970 reg_addr->reg_offset_x);
971 if (reg_addr->reg_offset_y != NO_REGISTER)
972 tsdata->offset_y = edt_ft5x06_register_read(tsdata,
973 reg_addr->reg_offset_y);
974 if (reg_addr->reg_report_rate != NO_REGISTER)
975 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
976 reg_addr->reg_report_rate);
977 if (tsdata->version == EDT_M06 ||
978 tsdata->version == EDT_M09 ||
979 tsdata->version == EDT_M12) {
980 tsdata->num_x = edt_ft5x06_register_read(tsdata,
981 reg_addr->reg_num_x);
982 tsdata->num_y = edt_ft5x06_register_read(tsdata,
983 reg_addr->reg_num_y);
984 } else {
985 tsdata->num_x = -1;
986 tsdata->num_y = -1;
990 static void
991 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
993 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
995 switch (tsdata->version) {
996 case EDT_M06:
997 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
998 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
999 reg_addr->reg_gain = WORK_REGISTER_GAIN;
1000 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
1001 reg_addr->reg_offset_x = NO_REGISTER;
1002 reg_addr->reg_offset_y = NO_REGISTER;
1003 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
1004 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
1005 break;
1007 case EDT_M09:
1008 case EDT_M12:
1009 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1010 reg_addr->reg_report_rate = NO_REGISTER;
1011 reg_addr->reg_gain = M09_REGISTER_GAIN;
1012 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1013 reg_addr->reg_offset_x = NO_REGISTER;
1014 reg_addr->reg_offset_y = NO_REGISTER;
1015 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
1016 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
1017 break;
1019 case EV_FT:
1020 reg_addr->reg_threshold = EV_REGISTER_THRESHOLD;
1021 reg_addr->reg_gain = EV_REGISTER_GAIN;
1022 reg_addr->reg_offset = NO_REGISTER;
1023 reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1024 reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
1025 reg_addr->reg_num_x = NO_REGISTER;
1026 reg_addr->reg_num_y = NO_REGISTER;
1027 reg_addr->reg_report_rate = NO_REGISTER;
1028 break;
1030 case GENERIC_FT:
1031 /* this is a guesswork */
1032 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1033 reg_addr->reg_gain = M09_REGISTER_GAIN;
1034 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1035 reg_addr->reg_offset_x = NO_REGISTER;
1036 reg_addr->reg_offset_y = NO_REGISTER;
1037 break;
1041 static void edt_ft5x06_disable_regulator(void *arg)
1043 struct edt_ft5x06_ts_data *data = arg;
1045 regulator_disable(data->vcc);
1048 static int edt_ft5x06_ts_probe(struct i2c_client *client,
1049 const struct i2c_device_id *id)
1051 const struct edt_i2c_chip_data *chip_data;
1052 struct edt_ft5x06_ts_data *tsdata;
1053 struct input_dev *input;
1054 unsigned long irq_flags;
1055 int error;
1056 char fw_version[EDT_NAME_LEN];
1058 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1060 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1061 if (!tsdata) {
1062 dev_err(&client->dev, "failed to allocate driver data.\n");
1063 return -ENOMEM;
1066 chip_data = device_get_match_data(&client->dev);
1067 if (!chip_data)
1068 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1069 if (!chip_data || !chip_data->max_support_points) {
1070 dev_err(&client->dev, "invalid or missing chip data\n");
1071 return -EINVAL;
1074 tsdata->max_support_points = chip_data->max_support_points;
1076 tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
1077 if (IS_ERR(tsdata->vcc)) {
1078 error = PTR_ERR(tsdata->vcc);
1079 if (error != -EPROBE_DEFER)
1080 dev_err(&client->dev,
1081 "failed to request regulator: %d\n", error);
1082 return error;
1085 error = regulator_enable(tsdata->vcc);
1086 if (error < 0) {
1087 dev_err(&client->dev, "failed to enable vcc: %d\n", error);
1088 return error;
1091 error = devm_add_action_or_reset(&client->dev,
1092 edt_ft5x06_disable_regulator,
1093 tsdata);
1094 if (error)
1095 return error;
1097 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1098 "reset", GPIOD_OUT_HIGH);
1099 if (IS_ERR(tsdata->reset_gpio)) {
1100 error = PTR_ERR(tsdata->reset_gpio);
1101 dev_err(&client->dev,
1102 "Failed to request GPIO reset pin, error %d\n", error);
1103 return error;
1106 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1107 "wake", GPIOD_OUT_LOW);
1108 if (IS_ERR(tsdata->wake_gpio)) {
1109 error = PTR_ERR(tsdata->wake_gpio);
1110 dev_err(&client->dev,
1111 "Failed to request GPIO wake pin, error %d\n", error);
1112 return error;
1115 if (tsdata->wake_gpio) {
1116 usleep_range(5000, 6000);
1117 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1120 if (tsdata->reset_gpio) {
1121 usleep_range(5000, 6000);
1122 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1123 msleep(300);
1126 input = devm_input_allocate_device(&client->dev);
1127 if (!input) {
1128 dev_err(&client->dev, "failed to allocate input device.\n");
1129 return -ENOMEM;
1132 mutex_init(&tsdata->mutex);
1133 tsdata->client = client;
1134 tsdata->input = input;
1135 tsdata->factory_mode = false;
1137 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
1138 if (error) {
1139 dev_err(&client->dev, "touchscreen probe failed\n");
1140 return error;
1143 edt_ft5x06_ts_set_regs(tsdata);
1144 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1145 edt_ft5x06_ts_get_parameters(tsdata);
1147 dev_dbg(&client->dev,
1148 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1149 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1151 input->name = tsdata->name;
1152 input->id.bustype = BUS_I2C;
1153 input->dev.parent = &client->dev;
1155 if (tsdata->version == EDT_M06 ||
1156 tsdata->version == EDT_M09 ||
1157 tsdata->version == EDT_M12) {
1158 input_set_abs_params(input, ABS_MT_POSITION_X,
1159 0, tsdata->num_x * 64 - 1, 0, 0);
1160 input_set_abs_params(input, ABS_MT_POSITION_Y,
1161 0, tsdata->num_y * 64 - 1, 0, 0);
1162 } else {
1163 /* Unknown maximum values. Specify via devicetree */
1164 input_set_abs_params(input, ABS_MT_POSITION_X,
1165 0, 65535, 0, 0);
1166 input_set_abs_params(input, ABS_MT_POSITION_Y,
1167 0, 65535, 0, 0);
1170 touchscreen_parse_properties(input, true, &tsdata->prop);
1172 error = input_mt_init_slots(input, tsdata->max_support_points,
1173 INPUT_MT_DIRECT);
1174 if (error) {
1175 dev_err(&client->dev, "Unable to init MT slots.\n");
1176 return error;
1179 i2c_set_clientdata(client, tsdata);
1181 irq_flags = irq_get_trigger_type(client->irq);
1182 if (irq_flags == IRQF_TRIGGER_NONE)
1183 irq_flags = IRQF_TRIGGER_FALLING;
1184 irq_flags |= IRQF_ONESHOT;
1186 error = devm_request_threaded_irq(&client->dev, client->irq,
1187 NULL, edt_ft5x06_ts_isr, irq_flags,
1188 client->name, tsdata);
1189 if (error) {
1190 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1191 return error;
1194 error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
1195 if (error)
1196 return error;
1198 error = input_register_device(input);
1199 if (error)
1200 return error;
1202 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1203 device_init_wakeup(&client->dev, 1);
1205 dev_dbg(&client->dev,
1206 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1207 client->irq,
1208 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1209 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1211 return 0;
1214 static int edt_ft5x06_ts_remove(struct i2c_client *client)
1216 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1218 edt_ft5x06_ts_teardown_debugfs(tsdata);
1220 return 0;
1223 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1225 struct i2c_client *client = to_i2c_client(dev);
1227 if (device_may_wakeup(dev))
1228 enable_irq_wake(client->irq);
1230 return 0;
1233 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1235 struct i2c_client *client = to_i2c_client(dev);
1237 if (device_may_wakeup(dev))
1238 disable_irq_wake(client->irq);
1240 return 0;
1243 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1244 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1246 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1247 .max_support_points = 5,
1250 static const struct edt_i2c_chip_data edt_ft5506_data = {
1251 .max_support_points = 10,
1254 static const struct edt_i2c_chip_data edt_ft6236_data = {
1255 .max_support_points = 2,
1258 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1259 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1260 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1261 { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
1262 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1263 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1264 { /* sentinel */ }
1266 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1268 static const struct of_device_id edt_ft5x06_of_match[] = {
1269 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1270 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1271 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1272 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1273 { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
1274 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1275 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1276 { /* sentinel */ }
1278 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1280 static struct i2c_driver edt_ft5x06_ts_driver = {
1281 .driver = {
1282 .name = "edt_ft5x06",
1283 .of_match_table = edt_ft5x06_of_match,
1284 .pm = &edt_ft5x06_ts_pm_ops,
1286 .id_table = edt_ft5x06_ts_id,
1287 .probe = edt_ft5x06_ts_probe,
1288 .remove = edt_ft5x06_ts_remove,
1291 module_i2c_driver(edt_ft5x06_ts_driver);
1293 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1294 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1295 MODULE_LICENSE("GPL v2");