Linux 5.1.15
[linux/fpc-iii.git] / drivers / input / touchscreen / edt-ft5x06.c
blob702bfda7ee777eb66fa75d2d33b8edacb01cbfa5
1 /*
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/kernel.h>
35 #include <linux/uaccess.h>
36 #include <linux/delay.h>
37 #include <linux/debugfs.h>
38 #include <linux/slab.h>
39 #include <linux/gpio/consumer.h>
40 #include <linux/input/mt.h>
41 #include <linux/input/touchscreen.h>
42 #include <linux/of_device.h>
44 #define WORK_REGISTER_THRESHOLD 0x00
45 #define WORK_REGISTER_REPORT_RATE 0x08
46 #define WORK_REGISTER_GAIN 0x30
47 #define WORK_REGISTER_OFFSET 0x31
48 #define WORK_REGISTER_NUM_X 0x33
49 #define WORK_REGISTER_NUM_Y 0x34
51 #define M09_REGISTER_THRESHOLD 0x80
52 #define M09_REGISTER_GAIN 0x92
53 #define M09_REGISTER_OFFSET 0x93
54 #define M09_REGISTER_NUM_X 0x94
55 #define M09_REGISTER_NUM_Y 0x95
57 #define EV_REGISTER_THRESHOLD 0x40
58 #define EV_REGISTER_GAIN 0x41
59 #define EV_REGISTER_OFFSET_Y 0x45
60 #define EV_REGISTER_OFFSET_X 0x46
62 #define NO_REGISTER 0xff
64 #define WORK_REGISTER_OPMODE 0x3c
65 #define FACTORY_REGISTER_OPMODE 0x01
67 #define TOUCH_EVENT_DOWN 0x00
68 #define TOUCH_EVENT_UP 0x01
69 #define TOUCH_EVENT_ON 0x02
70 #define TOUCH_EVENT_RESERVED 0x03
72 #define EDT_NAME_LEN 23
73 #define EDT_SWITCH_MODE_RETRIES 10
74 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
75 #define EDT_RAW_DATA_RETRIES 100
76 #define EDT_RAW_DATA_DELAY 1000 /* usec */
78 enum edt_ver {
79 EDT_M06,
80 EDT_M09,
81 EDT_M12,
82 EV_FT,
83 GENERIC_FT,
86 struct edt_reg_addr {
87 int reg_threshold;
88 int reg_report_rate;
89 int reg_gain;
90 int reg_offset;
91 int reg_offset_x;
92 int reg_offset_y;
93 int reg_num_x;
94 int reg_num_y;
97 struct edt_ft5x06_ts_data {
98 struct i2c_client *client;
99 struct input_dev *input;
100 struct touchscreen_properties prop;
101 u16 num_x;
102 u16 num_y;
104 struct gpio_desc *reset_gpio;
105 struct gpio_desc *wake_gpio;
107 #if defined(CONFIG_DEBUG_FS)
108 struct dentry *debug_dir;
109 u8 *raw_buffer;
110 size_t raw_bufsize;
111 #endif
113 struct mutex mutex;
114 bool factory_mode;
115 int threshold;
116 int gain;
117 int offset;
118 int offset_x;
119 int offset_y;
120 int report_rate;
121 int max_support_points;
123 char name[EDT_NAME_LEN];
125 struct edt_reg_addr reg_addr;
126 enum edt_ver version;
129 struct edt_i2c_chip_data {
130 int max_support_points;
133 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
134 u16 wr_len, u8 *wr_buf,
135 u16 rd_len, u8 *rd_buf)
137 struct i2c_msg wrmsg[2];
138 int i = 0;
139 int ret;
141 if (wr_len) {
142 wrmsg[i].addr = client->addr;
143 wrmsg[i].flags = 0;
144 wrmsg[i].len = wr_len;
145 wrmsg[i].buf = wr_buf;
146 i++;
148 if (rd_len) {
149 wrmsg[i].addr = client->addr;
150 wrmsg[i].flags = I2C_M_RD;
151 wrmsg[i].len = rd_len;
152 wrmsg[i].buf = rd_buf;
153 i++;
156 ret = i2c_transfer(client->adapter, wrmsg, i);
157 if (ret < 0)
158 return ret;
159 if (ret != i)
160 return -EIO;
162 return 0;
165 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
166 u8 *buf, int buflen)
168 int i;
169 u8 crc = 0;
171 for (i = 0; i < buflen - 1; i++)
172 crc ^= buf[i];
174 if (crc != buf[buflen-1]) {
175 dev_err_ratelimited(&tsdata->client->dev,
176 "crc error: 0x%02x expected, got 0x%02x\n",
177 crc, buf[buflen-1]);
178 return false;
181 return true;
184 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
186 struct edt_ft5x06_ts_data *tsdata = dev_id;
187 struct device *dev = &tsdata->client->dev;
188 u8 cmd;
189 u8 rdbuf[63];
190 int i, type, x, y, id;
191 int offset, tplen, datalen, crclen;
192 int error;
194 switch (tsdata->version) {
195 case EDT_M06:
196 cmd = 0xf9; /* tell the controller to send touch data */
197 offset = 5; /* where the actual touch data starts */
198 tplen = 4; /* data comes in so called frames */
199 crclen = 1; /* length of the crc data */
200 break;
202 case EDT_M09:
203 case EDT_M12:
204 case EV_FT:
205 case GENERIC_FT:
206 cmd = 0x0;
207 offset = 3;
208 tplen = 6;
209 crclen = 0;
210 break;
212 default:
213 goto out;
216 memset(rdbuf, 0, sizeof(rdbuf));
217 datalen = tplen * tsdata->max_support_points + offset + crclen;
219 error = edt_ft5x06_ts_readwrite(tsdata->client,
220 sizeof(cmd), &cmd,
221 datalen, rdbuf);
222 if (error) {
223 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
224 error);
225 goto out;
228 /* M09/M12 does not send header or CRC */
229 if (tsdata->version == EDT_M06) {
230 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
231 rdbuf[2] != datalen) {
232 dev_err_ratelimited(dev,
233 "Unexpected header: %02x%02x%02x!\n",
234 rdbuf[0], rdbuf[1], rdbuf[2]);
235 goto out;
238 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
239 goto out;
242 for (i = 0; i < tsdata->max_support_points; i++) {
243 u8 *buf = &rdbuf[i * tplen + offset];
244 bool down;
246 type = buf[0] >> 6;
247 /* ignore Reserved events */
248 if (type == TOUCH_EVENT_RESERVED)
249 continue;
251 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
252 if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
253 continue;
255 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
256 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
257 /* The FT5x26 send the y coordinate first */
258 if (tsdata->version == EV_FT)
259 swap(x, y);
261 id = (buf[2] >> 4) & 0x0f;
262 down = type != TOUCH_EVENT_UP;
264 input_mt_slot(tsdata->input, id);
265 input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
267 if (!down)
268 continue;
270 touchscreen_report_pos(tsdata->input, &tsdata->prop, x, y,
271 true);
274 input_mt_report_pointer_emulation(tsdata->input, true);
275 input_sync(tsdata->input);
277 out:
278 return IRQ_HANDLED;
281 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
282 u8 addr, u8 value)
284 u8 wrbuf[4];
286 switch (tsdata->version) {
287 case EDT_M06:
288 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
289 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
290 wrbuf[2] = value;
291 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
292 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
293 wrbuf, 0, NULL);
294 /* fallthrough */
295 case EDT_M09:
296 case EDT_M12:
297 case EV_FT:
298 case GENERIC_FT:
299 wrbuf[0] = addr;
300 wrbuf[1] = value;
302 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
303 wrbuf, 0, NULL);
305 default:
306 return -EINVAL;
310 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
311 u8 addr)
313 u8 wrbuf[2], rdbuf[2];
314 int error;
316 switch (tsdata->version) {
317 case EDT_M06:
318 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
319 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
320 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
322 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
323 rdbuf);
324 if (error)
325 return error;
327 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
328 dev_err(&tsdata->client->dev,
329 "crc error: 0x%02x expected, got 0x%02x\n",
330 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
331 rdbuf[1]);
332 return -EIO;
334 break;
336 /* fallthrough */
337 case EDT_M09:
338 case EDT_M12:
339 case EV_FT:
340 case GENERIC_FT:
341 wrbuf[0] = addr;
342 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
343 wrbuf, 1, rdbuf);
344 if (error)
345 return error;
346 break;
348 default:
349 return -EINVAL;
352 return rdbuf[0];
355 struct edt_ft5x06_attribute {
356 struct device_attribute dattr;
357 size_t field_offset;
358 u8 limit_low;
359 u8 limit_high;
360 u8 addr_m06;
361 u8 addr_m09;
362 u8 addr_ev;
365 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
366 _limit_low, _limit_high) \
367 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
368 .dattr = __ATTR(_field, _mode, \
369 edt_ft5x06_setting_show, \
370 edt_ft5x06_setting_store), \
371 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
372 .addr_m06 = _addr_m06, \
373 .addr_m09 = _addr_m09, \
374 .addr_ev = _addr_ev, \
375 .limit_low = _limit_low, \
376 .limit_high = _limit_high, \
379 static ssize_t edt_ft5x06_setting_show(struct device *dev,
380 struct device_attribute *dattr,
381 char *buf)
383 struct i2c_client *client = to_i2c_client(dev);
384 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
385 struct edt_ft5x06_attribute *attr =
386 container_of(dattr, struct edt_ft5x06_attribute, dattr);
387 u8 *field = (u8 *)tsdata + attr->field_offset;
388 int val;
389 size_t count = 0;
390 int error = 0;
391 u8 addr;
393 mutex_lock(&tsdata->mutex);
395 if (tsdata->factory_mode) {
396 error = -EIO;
397 goto out;
400 switch (tsdata->version) {
401 case EDT_M06:
402 addr = attr->addr_m06;
403 break;
405 case EDT_M09:
406 case EDT_M12:
407 case GENERIC_FT:
408 addr = attr->addr_m09;
409 break;
411 case EV_FT:
412 addr = attr->addr_ev;
413 break;
415 default:
416 error = -ENODEV;
417 goto out;
420 if (addr != NO_REGISTER) {
421 val = edt_ft5x06_register_read(tsdata, addr);
422 if (val < 0) {
423 error = val;
424 dev_err(&tsdata->client->dev,
425 "Failed to fetch attribute %s, error %d\n",
426 dattr->attr.name, error);
427 goto out;
429 } else {
430 val = *field;
433 if (val != *field) {
434 dev_warn(&tsdata->client->dev,
435 "%s: read (%d) and stored value (%d) differ\n",
436 dattr->attr.name, val, *field);
437 *field = val;
440 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
441 out:
442 mutex_unlock(&tsdata->mutex);
443 return error ?: count;
446 static ssize_t edt_ft5x06_setting_store(struct device *dev,
447 struct device_attribute *dattr,
448 const char *buf, size_t count)
450 struct i2c_client *client = to_i2c_client(dev);
451 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
452 struct edt_ft5x06_attribute *attr =
453 container_of(dattr, struct edt_ft5x06_attribute, dattr);
454 u8 *field = (u8 *)tsdata + attr->field_offset;
455 unsigned int val;
456 int error;
457 u8 addr;
459 mutex_lock(&tsdata->mutex);
461 if (tsdata->factory_mode) {
462 error = -EIO;
463 goto out;
466 error = kstrtouint(buf, 0, &val);
467 if (error)
468 goto out;
470 if (val < attr->limit_low || val > attr->limit_high) {
471 error = -ERANGE;
472 goto out;
475 switch (tsdata->version) {
476 case EDT_M06:
477 addr = attr->addr_m06;
478 break;
480 case EDT_M09:
481 case EDT_M12:
482 case GENERIC_FT:
483 addr = attr->addr_m09;
484 break;
486 case EV_FT:
487 addr = attr->addr_ev;
488 break;
490 default:
491 error = -ENODEV;
492 goto out;
495 if (addr != NO_REGISTER) {
496 error = edt_ft5x06_register_write(tsdata, addr, val);
497 if (error) {
498 dev_err(&tsdata->client->dev,
499 "Failed to update attribute %s, error: %d\n",
500 dattr->attr.name, error);
501 goto out;
504 *field = val;
506 out:
507 mutex_unlock(&tsdata->mutex);
508 return error ?: count;
511 /* m06, m09: range 0-31, m12: range 0-5 */
512 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
513 M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31);
514 /* m06, m09: range 0-31, m12: range 0-16 */
515 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
516 M09_REGISTER_OFFSET, NO_REGISTER, 0, 31);
517 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
518 static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
519 EV_REGISTER_OFFSET_X, 0, 80);
520 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
521 static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
522 EV_REGISTER_OFFSET_Y, 0, 80);
523 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
524 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
525 M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255);
526 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
527 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
528 NO_REGISTER, NO_REGISTER, 0, 255);
530 static struct attribute *edt_ft5x06_attrs[] = {
531 &edt_ft5x06_attr_gain.dattr.attr,
532 &edt_ft5x06_attr_offset.dattr.attr,
533 &edt_ft5x06_attr_offset_x.dattr.attr,
534 &edt_ft5x06_attr_offset_y.dattr.attr,
535 &edt_ft5x06_attr_threshold.dattr.attr,
536 &edt_ft5x06_attr_report_rate.dattr.attr,
537 NULL
540 static const struct attribute_group edt_ft5x06_attr_group = {
541 .attrs = edt_ft5x06_attrs,
544 #ifdef CONFIG_DEBUG_FS
545 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
547 struct i2c_client *client = tsdata->client;
548 int retries = EDT_SWITCH_MODE_RETRIES;
549 int ret;
550 int error;
552 if (tsdata->version != EDT_M06) {
553 dev_err(&client->dev,
554 "No factory mode support for non-M06 devices\n");
555 return -EINVAL;
558 disable_irq(client->irq);
560 if (!tsdata->raw_buffer) {
561 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
562 sizeof(u16);
563 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
564 if (!tsdata->raw_buffer) {
565 error = -ENOMEM;
566 goto err_out;
570 /* mode register is 0x3c when in the work mode */
571 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
572 if (error) {
573 dev_err(&client->dev,
574 "failed to switch to factory mode, error %d\n", error);
575 goto err_out;
578 tsdata->factory_mode = true;
579 do {
580 mdelay(EDT_SWITCH_MODE_DELAY);
581 /* mode register is 0x01 when in factory mode */
582 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
583 if (ret == 0x03)
584 break;
585 } while (--retries > 0);
587 if (retries == 0) {
588 dev_err(&client->dev, "not in factory mode after %dms.\n",
589 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
590 error = -EIO;
591 goto err_out;
594 return 0;
596 err_out:
597 kfree(tsdata->raw_buffer);
598 tsdata->raw_buffer = NULL;
599 tsdata->factory_mode = false;
600 enable_irq(client->irq);
602 return error;
605 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
607 struct i2c_client *client = tsdata->client;
608 int retries = EDT_SWITCH_MODE_RETRIES;
609 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
610 int ret;
611 int error;
613 /* mode register is 0x01 when in the factory mode */
614 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
615 if (error) {
616 dev_err(&client->dev,
617 "failed to switch to work mode, error: %d\n", error);
618 return error;
621 tsdata->factory_mode = false;
623 do {
624 mdelay(EDT_SWITCH_MODE_DELAY);
625 /* mode register is 0x01 when in factory mode */
626 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
627 if (ret == 0x01)
628 break;
629 } while (--retries > 0);
631 if (retries == 0) {
632 dev_err(&client->dev, "not in work mode after %dms.\n",
633 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
634 tsdata->factory_mode = true;
635 return -EIO;
638 kfree(tsdata->raw_buffer);
639 tsdata->raw_buffer = NULL;
641 /* restore parameters */
642 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
643 tsdata->threshold);
644 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
645 tsdata->gain);
646 if (reg_addr->reg_offset != NO_REGISTER)
647 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
648 tsdata->offset);
649 if (reg_addr->reg_offset_x != NO_REGISTER)
650 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x,
651 tsdata->offset_x);
652 if (reg_addr->reg_offset_y != NO_REGISTER)
653 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y,
654 tsdata->offset_y);
655 if (reg_addr->reg_report_rate != NO_REGISTER)
656 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
657 tsdata->report_rate);
659 enable_irq(client->irq);
661 return 0;
664 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
666 struct edt_ft5x06_ts_data *tsdata = data;
668 *mode = tsdata->factory_mode;
670 return 0;
673 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
675 struct edt_ft5x06_ts_data *tsdata = data;
676 int retval = 0;
678 if (mode > 1)
679 return -ERANGE;
681 mutex_lock(&tsdata->mutex);
683 if (mode != tsdata->factory_mode) {
684 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
685 edt_ft5x06_work_mode(tsdata);
688 mutex_unlock(&tsdata->mutex);
690 return retval;
693 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
694 edt_ft5x06_debugfs_mode_set, "%llu\n");
696 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
697 char __user *buf, size_t count, loff_t *off)
699 struct edt_ft5x06_ts_data *tsdata = file->private_data;
700 struct i2c_client *client = tsdata->client;
701 int retries = EDT_RAW_DATA_RETRIES;
702 int val, i, error;
703 size_t read = 0;
704 int colbytes;
705 char wrbuf[3];
706 u8 *rdbuf;
708 if (*off < 0 || *off >= tsdata->raw_bufsize)
709 return 0;
711 mutex_lock(&tsdata->mutex);
713 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
714 error = -EIO;
715 goto out;
718 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
719 if (error) {
720 dev_dbg(&client->dev,
721 "failed to write 0x08 register, error %d\n", error);
722 goto out;
725 do {
726 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
727 val = edt_ft5x06_register_read(tsdata, 0x08);
728 if (val < 1)
729 break;
730 } while (--retries > 0);
732 if (val < 0) {
733 error = val;
734 dev_dbg(&client->dev,
735 "failed to read 0x08 register, error %d\n", error);
736 goto out;
739 if (retries == 0) {
740 dev_dbg(&client->dev,
741 "timed out waiting for register to settle\n");
742 error = -ETIMEDOUT;
743 goto out;
746 rdbuf = tsdata->raw_buffer;
747 colbytes = tsdata->num_y * sizeof(u16);
749 wrbuf[0] = 0xf5;
750 wrbuf[1] = 0x0e;
751 for (i = 0; i < tsdata->num_x; i++) {
752 wrbuf[2] = i; /* column index */
753 error = edt_ft5x06_ts_readwrite(tsdata->client,
754 sizeof(wrbuf), wrbuf,
755 colbytes, rdbuf);
756 if (error)
757 goto out;
759 rdbuf += colbytes;
762 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
763 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
764 error = -EFAULT;
765 goto out;
768 *off += read;
769 out:
770 mutex_unlock(&tsdata->mutex);
771 return error ?: read;
774 static const struct file_operations debugfs_raw_data_fops = {
775 .open = simple_open,
776 .read = edt_ft5x06_debugfs_raw_data_read,
779 static void
780 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
781 const char *debugfs_name)
783 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
784 if (!tsdata->debug_dir)
785 return;
787 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
788 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
790 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
791 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
792 debugfs_create_file("raw_data", S_IRUSR,
793 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
796 static void
797 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
799 debugfs_remove_recursive(tsdata->debug_dir);
800 kfree(tsdata->raw_buffer);
803 #else
805 static inline void
806 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
807 const char *debugfs_name)
811 static inline void
812 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
816 #endif /* CONFIG_DEBUGFS */
818 static int edt_ft5x06_ts_identify(struct i2c_client *client,
819 struct edt_ft5x06_ts_data *tsdata,
820 char *fw_version)
822 u8 rdbuf[EDT_NAME_LEN];
823 char *p;
824 int error;
825 char *model_name = tsdata->name;
827 /* see what we find if we assume it is a M06 *
828 * if we get less than EDT_NAME_LEN, we don't want
829 * to have garbage in there
831 memset(rdbuf, 0, sizeof(rdbuf));
832 error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
833 EDT_NAME_LEN - 1, rdbuf);
834 if (error)
835 return error;
837 /* Probe content for something consistent.
838 * M06 starts with a response byte, M12 gives the data directly.
839 * M09/Generic does not provide model number information.
841 if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
842 tsdata->version = EDT_M06;
844 /* remove last '$' end marker */
845 rdbuf[EDT_NAME_LEN - 1] = '\0';
846 if (rdbuf[EDT_NAME_LEN - 2] == '$')
847 rdbuf[EDT_NAME_LEN - 2] = '\0';
849 /* look for Model/Version separator */
850 p = strchr(rdbuf, '*');
851 if (p)
852 *p++ = '\0';
853 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
854 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
855 } else if (!strncasecmp(rdbuf, "EP0", 3)) {
856 tsdata->version = EDT_M12;
858 /* remove last '$' end marker */
859 rdbuf[EDT_NAME_LEN - 2] = '\0';
860 if (rdbuf[EDT_NAME_LEN - 3] == '$')
861 rdbuf[EDT_NAME_LEN - 3] = '\0';
863 /* look for Model/Version separator */
864 p = strchr(rdbuf, '*');
865 if (p)
866 *p++ = '\0';
867 strlcpy(model_name, rdbuf, EDT_NAME_LEN);
868 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
869 } else {
870 /* If it is not an EDT M06/M12 touchscreen, then the model
871 * detection is a bit hairy. The different ft5x06
872 * firmares around don't reliably implement the
873 * identification registers. Well, we'll take a shot.
875 * The main difference between generic focaltec based
876 * touches and EDT M09 is that we know how to retrieve
877 * the max coordinates for the latter.
879 tsdata->version = GENERIC_FT;
881 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
882 2, rdbuf);
883 if (error)
884 return error;
886 strlcpy(fw_version, rdbuf, 2);
888 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
889 1, rdbuf);
890 if (error)
891 return error;
893 /* This "model identification" is not exact. Unfortunately
894 * not all firmwares for the ft5x06 put useful values in
895 * the identification registers.
897 switch (rdbuf[0]) {
898 case 0x35: /* EDT EP0350M09 */
899 case 0x43: /* EDT EP0430M09 */
900 case 0x50: /* EDT EP0500M09 */
901 case 0x57: /* EDT EP0570M09 */
902 case 0x70: /* EDT EP0700M09 */
903 tsdata->version = EDT_M09;
904 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
905 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
906 break;
907 case 0xa1: /* EDT EP1010ML00 */
908 tsdata->version = EDT_M09;
909 snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
910 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
911 break;
912 case 0x5a: /* Solomon Goldentek Display */
913 snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
914 break;
915 case 0x59: /* Evervision Display with FT5xx6 TS */
916 tsdata->version = EV_FT;
917 error = edt_ft5x06_ts_readwrite(client, 1, "\x53",
918 1, rdbuf);
919 if (error)
920 return error;
921 strlcpy(fw_version, rdbuf, 1);
922 snprintf(model_name, EDT_NAME_LEN,
923 "EVERVISION-FT5726NEi");
924 break;
925 default:
926 snprintf(model_name, EDT_NAME_LEN,
927 "generic ft5x06 (%02x)",
928 rdbuf[0]);
929 break;
933 return 0;
936 static void edt_ft5x06_ts_get_defaults(struct device *dev,
937 struct edt_ft5x06_ts_data *tsdata)
939 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
940 u32 val;
941 int error;
943 error = device_property_read_u32(dev, "threshold", &val);
944 if (!error) {
945 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
946 tsdata->threshold = val;
949 error = device_property_read_u32(dev, "gain", &val);
950 if (!error) {
951 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
952 tsdata->gain = val;
955 error = device_property_read_u32(dev, "offset", &val);
956 if (!error) {
957 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
958 tsdata->offset = val;
961 error = device_property_read_u32(dev, "offset-x", &val);
962 if (!error) {
963 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x, val);
964 tsdata->offset_x = val;
967 error = device_property_read_u32(dev, "offset-y", &val);
968 if (!error) {
969 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y, val);
970 tsdata->offset_y = val;
974 static void
975 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
977 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
979 tsdata->threshold = edt_ft5x06_register_read(tsdata,
980 reg_addr->reg_threshold);
981 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
982 if (reg_addr->reg_offset != NO_REGISTER)
983 tsdata->offset =
984 edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
985 if (reg_addr->reg_offset_x != NO_REGISTER)
986 tsdata->offset_x = edt_ft5x06_register_read(tsdata,
987 reg_addr->reg_offset_x);
988 if (reg_addr->reg_offset_y != NO_REGISTER)
989 tsdata->offset_y = edt_ft5x06_register_read(tsdata,
990 reg_addr->reg_offset_y);
991 if (reg_addr->reg_report_rate != NO_REGISTER)
992 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
993 reg_addr->reg_report_rate);
994 if (tsdata->version == EDT_M06 ||
995 tsdata->version == EDT_M09 ||
996 tsdata->version == EDT_M12) {
997 tsdata->num_x = edt_ft5x06_register_read(tsdata,
998 reg_addr->reg_num_x);
999 tsdata->num_y = edt_ft5x06_register_read(tsdata,
1000 reg_addr->reg_num_y);
1001 } else {
1002 tsdata->num_x = -1;
1003 tsdata->num_y = -1;
1007 static void
1008 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
1010 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
1012 switch (tsdata->version) {
1013 case EDT_M06:
1014 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
1015 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
1016 reg_addr->reg_gain = WORK_REGISTER_GAIN;
1017 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
1018 reg_addr->reg_offset_x = NO_REGISTER;
1019 reg_addr->reg_offset_y = NO_REGISTER;
1020 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
1021 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
1022 break;
1024 case EDT_M09:
1025 case EDT_M12:
1026 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1027 reg_addr->reg_report_rate = NO_REGISTER;
1028 reg_addr->reg_gain = M09_REGISTER_GAIN;
1029 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1030 reg_addr->reg_offset_x = NO_REGISTER;
1031 reg_addr->reg_offset_y = NO_REGISTER;
1032 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
1033 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
1034 break;
1036 case EV_FT:
1037 reg_addr->reg_threshold = EV_REGISTER_THRESHOLD;
1038 reg_addr->reg_gain = EV_REGISTER_GAIN;
1039 reg_addr->reg_offset = NO_REGISTER;
1040 reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1041 reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
1042 reg_addr->reg_num_x = NO_REGISTER;
1043 reg_addr->reg_num_y = NO_REGISTER;
1044 reg_addr->reg_report_rate = NO_REGISTER;
1045 break;
1047 case GENERIC_FT:
1048 /* this is a guesswork */
1049 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1050 reg_addr->reg_gain = M09_REGISTER_GAIN;
1051 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1052 reg_addr->reg_offset_x = NO_REGISTER;
1053 reg_addr->reg_offset_y = NO_REGISTER;
1054 break;
1058 static int edt_ft5x06_ts_probe(struct i2c_client *client,
1059 const struct i2c_device_id *id)
1061 const struct edt_i2c_chip_data *chip_data;
1062 struct edt_ft5x06_ts_data *tsdata;
1063 struct input_dev *input;
1064 unsigned long irq_flags;
1065 int error;
1066 char fw_version[EDT_NAME_LEN];
1068 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1070 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1071 if (!tsdata) {
1072 dev_err(&client->dev, "failed to allocate driver data.\n");
1073 return -ENOMEM;
1076 chip_data = of_device_get_match_data(&client->dev);
1077 if (!chip_data)
1078 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1079 if (!chip_data || !chip_data->max_support_points) {
1080 dev_err(&client->dev, "invalid or missing chip data\n");
1081 return -EINVAL;
1084 tsdata->max_support_points = chip_data->max_support_points;
1086 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1087 "reset", GPIOD_OUT_HIGH);
1088 if (IS_ERR(tsdata->reset_gpio)) {
1089 error = PTR_ERR(tsdata->reset_gpio);
1090 dev_err(&client->dev,
1091 "Failed to request GPIO reset pin, error %d\n", error);
1092 return error;
1095 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1096 "wake", GPIOD_OUT_LOW);
1097 if (IS_ERR(tsdata->wake_gpio)) {
1098 error = PTR_ERR(tsdata->wake_gpio);
1099 dev_err(&client->dev,
1100 "Failed to request GPIO wake pin, error %d\n", error);
1101 return error;
1104 if (tsdata->wake_gpio) {
1105 usleep_range(5000, 6000);
1106 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1109 if (tsdata->reset_gpio) {
1110 usleep_range(5000, 6000);
1111 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1112 msleep(300);
1115 input = devm_input_allocate_device(&client->dev);
1116 if (!input) {
1117 dev_err(&client->dev, "failed to allocate input device.\n");
1118 return -ENOMEM;
1121 mutex_init(&tsdata->mutex);
1122 tsdata->client = client;
1123 tsdata->input = input;
1124 tsdata->factory_mode = false;
1126 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
1127 if (error) {
1128 dev_err(&client->dev, "touchscreen probe failed\n");
1129 return error;
1132 edt_ft5x06_ts_set_regs(tsdata);
1133 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1134 edt_ft5x06_ts_get_parameters(tsdata);
1136 dev_dbg(&client->dev,
1137 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1138 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1140 input->name = tsdata->name;
1141 input->id.bustype = BUS_I2C;
1142 input->dev.parent = &client->dev;
1144 if (tsdata->version == EDT_M06 ||
1145 tsdata->version == EDT_M09 ||
1146 tsdata->version == EDT_M12) {
1147 input_set_abs_params(input, ABS_MT_POSITION_X,
1148 0, tsdata->num_x * 64 - 1, 0, 0);
1149 input_set_abs_params(input, ABS_MT_POSITION_Y,
1150 0, tsdata->num_y * 64 - 1, 0, 0);
1151 } else {
1152 /* Unknown maximum values. Specify via devicetree */
1153 input_set_abs_params(input, ABS_MT_POSITION_X,
1154 0, 65535, 0, 0);
1155 input_set_abs_params(input, ABS_MT_POSITION_Y,
1156 0, 65535, 0, 0);
1159 touchscreen_parse_properties(input, true, &tsdata->prop);
1161 error = input_mt_init_slots(input, tsdata->max_support_points,
1162 INPUT_MT_DIRECT);
1163 if (error) {
1164 dev_err(&client->dev, "Unable to init MT slots.\n");
1165 return error;
1168 i2c_set_clientdata(client, tsdata);
1170 irq_flags = irq_get_trigger_type(client->irq);
1171 if (irq_flags == IRQF_TRIGGER_NONE)
1172 irq_flags = IRQF_TRIGGER_FALLING;
1173 irq_flags |= IRQF_ONESHOT;
1175 error = devm_request_threaded_irq(&client->dev, client->irq,
1176 NULL, edt_ft5x06_ts_isr, irq_flags,
1177 client->name, tsdata);
1178 if (error) {
1179 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1180 return error;
1183 error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
1184 if (error)
1185 return error;
1187 error = input_register_device(input);
1188 if (error)
1189 return error;
1191 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1192 device_init_wakeup(&client->dev, 1);
1194 dev_dbg(&client->dev,
1195 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1196 client->irq,
1197 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1198 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1200 return 0;
1203 static int edt_ft5x06_ts_remove(struct i2c_client *client)
1205 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1207 edt_ft5x06_ts_teardown_debugfs(tsdata);
1209 return 0;
1212 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1214 struct i2c_client *client = to_i2c_client(dev);
1216 if (device_may_wakeup(dev))
1217 enable_irq_wake(client->irq);
1219 return 0;
1222 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1224 struct i2c_client *client = to_i2c_client(dev);
1226 if (device_may_wakeup(dev))
1227 disable_irq_wake(client->irq);
1229 return 0;
1232 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1233 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1235 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1236 .max_support_points = 5,
1239 static const struct edt_i2c_chip_data edt_ft5506_data = {
1240 .max_support_points = 10,
1243 static const struct edt_i2c_chip_data edt_ft6236_data = {
1244 .max_support_points = 2,
1247 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1248 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1249 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1250 { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
1251 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1252 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1253 { /* sentinel */ }
1255 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1257 #ifdef CONFIG_OF
1258 static const struct of_device_id edt_ft5x06_of_match[] = {
1259 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1260 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1261 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1262 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1263 { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
1264 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1265 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1266 { /* sentinel */ }
1268 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1269 #endif
1271 static struct i2c_driver edt_ft5x06_ts_driver = {
1272 .driver = {
1273 .name = "edt_ft5x06",
1274 .of_match_table = of_match_ptr(edt_ft5x06_of_match),
1275 .pm = &edt_ft5x06_ts_pm_ops,
1277 .id_table = edt_ft5x06_ts_id,
1278 .probe = edt_ft5x06_ts_probe,
1279 .remove = edt_ft5x06_ts_remove,
1282 module_i2c_driver(edt_ft5x06_ts_driver);
1284 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1285 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1286 MODULE_LICENSE("GPL");