Input: dlink-dir685-touchkeys - fix a typo in driver name
[linux/fpc-iii.git] / drivers / input / touchscreen / edt-ft5x06.c
blob3fdaa644a82c14e85002eaaec4466a2e9423b73f
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/uaccess.h>
35 #include <linux/delay.h>
36 #include <linux/debugfs.h>
37 #include <linux/slab.h>
38 #include <linux/gpio/consumer.h>
39 #include <linux/input/mt.h>
40 #include <linux/input/touchscreen.h>
41 #include <linux/of_device.h>
43 #define WORK_REGISTER_THRESHOLD 0x00
44 #define WORK_REGISTER_REPORT_RATE 0x08
45 #define WORK_REGISTER_GAIN 0x30
46 #define WORK_REGISTER_OFFSET 0x31
47 #define WORK_REGISTER_NUM_X 0x33
48 #define WORK_REGISTER_NUM_Y 0x34
50 #define M09_REGISTER_THRESHOLD 0x80
51 #define M09_REGISTER_GAIN 0x92
52 #define M09_REGISTER_OFFSET 0x93
53 #define M09_REGISTER_NUM_X 0x94
54 #define M09_REGISTER_NUM_Y 0x95
56 #define NO_REGISTER 0xff
58 #define WORK_REGISTER_OPMODE 0x3c
59 #define FACTORY_REGISTER_OPMODE 0x01
61 #define TOUCH_EVENT_DOWN 0x00
62 #define TOUCH_EVENT_UP 0x01
63 #define TOUCH_EVENT_ON 0x02
64 #define TOUCH_EVENT_RESERVED 0x03
66 #define EDT_NAME_LEN 23
67 #define EDT_SWITCH_MODE_RETRIES 10
68 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
69 #define EDT_RAW_DATA_RETRIES 100
70 #define EDT_RAW_DATA_DELAY 1000 /* usec */
72 enum edt_ver {
73 EDT_M06,
74 EDT_M09,
75 EDT_M12,
76 GENERIC_FT,
79 struct edt_reg_addr {
80 int reg_threshold;
81 int reg_report_rate;
82 int reg_gain;
83 int reg_offset;
84 int reg_num_x;
85 int reg_num_y;
88 struct edt_ft5x06_ts_data {
89 struct i2c_client *client;
90 struct input_dev *input;
91 struct touchscreen_properties prop;
92 u16 num_x;
93 u16 num_y;
95 struct gpio_desc *reset_gpio;
96 struct gpio_desc *wake_gpio;
98 #if defined(CONFIG_DEBUG_FS)
99 struct dentry *debug_dir;
100 u8 *raw_buffer;
101 size_t raw_bufsize;
102 #endif
104 struct mutex mutex;
105 bool factory_mode;
106 int threshold;
107 int gain;
108 int offset;
109 int report_rate;
110 int max_support_points;
112 char name[EDT_NAME_LEN];
114 struct edt_reg_addr reg_addr;
115 enum edt_ver version;
118 struct edt_i2c_chip_data {
119 int max_support_points;
122 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
123 u16 wr_len, u8 *wr_buf,
124 u16 rd_len, u8 *rd_buf)
126 struct i2c_msg wrmsg[2];
127 int i = 0;
128 int ret;
130 if (wr_len) {
131 wrmsg[i].addr = client->addr;
132 wrmsg[i].flags = 0;
133 wrmsg[i].len = wr_len;
134 wrmsg[i].buf = wr_buf;
135 i++;
137 if (rd_len) {
138 wrmsg[i].addr = client->addr;
139 wrmsg[i].flags = I2C_M_RD;
140 wrmsg[i].len = rd_len;
141 wrmsg[i].buf = rd_buf;
142 i++;
145 ret = i2c_transfer(client->adapter, wrmsg, i);
146 if (ret < 0)
147 return ret;
148 if (ret != i)
149 return -EIO;
151 return 0;
154 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
155 u8 *buf, int buflen)
157 int i;
158 u8 crc = 0;
160 for (i = 0; i < buflen - 1; i++)
161 crc ^= buf[i];
163 if (crc != buf[buflen-1]) {
164 dev_err_ratelimited(&tsdata->client->dev,
165 "crc error: 0x%02x expected, got 0x%02x\n",
166 crc, buf[buflen-1]);
167 return false;
170 return true;
173 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
175 struct edt_ft5x06_ts_data *tsdata = dev_id;
176 struct device *dev = &tsdata->client->dev;
177 u8 cmd;
178 u8 rdbuf[63];
179 int i, type, x, y, id;
180 int offset, tplen, datalen, crclen;
181 int error;
183 switch (tsdata->version) {
184 case EDT_M06:
185 cmd = 0xf9; /* tell the controller to send touch data */
186 offset = 5; /* where the actual touch data starts */
187 tplen = 4; /* data comes in so called frames */
188 crclen = 1; /* length of the crc data */
189 break;
191 case EDT_M09:
192 case EDT_M12:
193 case GENERIC_FT:
194 cmd = 0x0;
195 offset = 3;
196 tplen = 6;
197 crclen = 0;
198 break;
200 default:
201 goto out;
204 memset(rdbuf, 0, sizeof(rdbuf));
205 datalen = tplen * tsdata->max_support_points + offset + crclen;
207 error = edt_ft5x06_ts_readwrite(tsdata->client,
208 sizeof(cmd), &cmd,
209 datalen, rdbuf);
210 if (error) {
211 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
212 error);
213 goto out;
216 /* M09/M12 does not send header or CRC */
217 if (tsdata->version == EDT_M06) {
218 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
219 rdbuf[2] != datalen) {
220 dev_err_ratelimited(dev,
221 "Unexpected header: %02x%02x%02x!\n",
222 rdbuf[0], rdbuf[1], rdbuf[2]);
223 goto out;
226 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
227 goto out;
230 for (i = 0; i < tsdata->max_support_points; i++) {
231 u8 *buf = &rdbuf[i * tplen + offset];
232 bool down;
234 type = buf[0] >> 6;
235 /* ignore Reserved events */
236 if (type == TOUCH_EVENT_RESERVED)
237 continue;
239 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
240 if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
241 continue;
243 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
244 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
245 id = (buf[2] >> 4) & 0x0f;
246 down = type != TOUCH_EVENT_UP;
248 input_mt_slot(tsdata->input, id);
249 input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
251 if (!down)
252 continue;
254 touchscreen_report_pos(tsdata->input, &tsdata->prop, x, y,
255 true);
258 input_mt_report_pointer_emulation(tsdata->input, true);
259 input_sync(tsdata->input);
261 out:
262 return IRQ_HANDLED;
265 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
266 u8 addr, u8 value)
268 u8 wrbuf[4];
270 switch (tsdata->version) {
271 case EDT_M06:
272 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
273 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
274 wrbuf[2] = value;
275 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
276 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
277 wrbuf, 0, NULL);
278 case EDT_M09:
279 case EDT_M12:
280 case GENERIC_FT:
281 wrbuf[0] = addr;
282 wrbuf[1] = value;
284 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
285 wrbuf, 0, NULL);
287 default:
288 return -EINVAL;
292 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
293 u8 addr)
295 u8 wrbuf[2], rdbuf[2];
296 int error;
298 switch (tsdata->version) {
299 case EDT_M06:
300 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
301 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
302 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
304 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
305 rdbuf);
306 if (error)
307 return error;
309 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
310 dev_err(&tsdata->client->dev,
311 "crc error: 0x%02x expected, got 0x%02x\n",
312 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
313 rdbuf[1]);
314 return -EIO;
316 break;
318 case EDT_M09:
319 case EDT_M12:
320 case GENERIC_FT:
321 wrbuf[0] = addr;
322 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
323 wrbuf, 1, rdbuf);
324 if (error)
325 return error;
326 break;
328 default:
329 return -EINVAL;
332 return rdbuf[0];
335 struct edt_ft5x06_attribute {
336 struct device_attribute dattr;
337 size_t field_offset;
338 u8 limit_low;
339 u8 limit_high;
340 u8 addr_m06;
341 u8 addr_m09;
344 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
345 _limit_low, _limit_high) \
346 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
347 .dattr = __ATTR(_field, _mode, \
348 edt_ft5x06_setting_show, \
349 edt_ft5x06_setting_store), \
350 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
351 .addr_m06 = _addr_m06, \
352 .addr_m09 = _addr_m09, \
353 .limit_low = _limit_low, \
354 .limit_high = _limit_high, \
357 static ssize_t edt_ft5x06_setting_show(struct device *dev,
358 struct device_attribute *dattr,
359 char *buf)
361 struct i2c_client *client = to_i2c_client(dev);
362 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
363 struct edt_ft5x06_attribute *attr =
364 container_of(dattr, struct edt_ft5x06_attribute, dattr);
365 u8 *field = (u8 *)tsdata + attr->field_offset;
366 int val;
367 size_t count = 0;
368 int error = 0;
369 u8 addr;
371 mutex_lock(&tsdata->mutex);
373 if (tsdata->factory_mode) {
374 error = -EIO;
375 goto out;
378 switch (tsdata->version) {
379 case EDT_M06:
380 addr = attr->addr_m06;
381 break;
383 case EDT_M09:
384 case EDT_M12:
385 case GENERIC_FT:
386 addr = attr->addr_m09;
387 break;
389 default:
390 error = -ENODEV;
391 goto out;
394 if (addr != NO_REGISTER) {
395 val = edt_ft5x06_register_read(tsdata, addr);
396 if (val < 0) {
397 error = val;
398 dev_err(&tsdata->client->dev,
399 "Failed to fetch attribute %s, error %d\n",
400 dattr->attr.name, error);
401 goto out;
403 } else {
404 val = *field;
407 if (val != *field) {
408 dev_warn(&tsdata->client->dev,
409 "%s: read (%d) and stored value (%d) differ\n",
410 dattr->attr.name, val, *field);
411 *field = val;
414 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
415 out:
416 mutex_unlock(&tsdata->mutex);
417 return error ?: count;
420 static ssize_t edt_ft5x06_setting_store(struct device *dev,
421 struct device_attribute *dattr,
422 const char *buf, size_t count)
424 struct i2c_client *client = to_i2c_client(dev);
425 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
426 struct edt_ft5x06_attribute *attr =
427 container_of(dattr, struct edt_ft5x06_attribute, dattr);
428 u8 *field = (u8 *)tsdata + attr->field_offset;
429 unsigned int val;
430 int error;
431 u8 addr;
433 mutex_lock(&tsdata->mutex);
435 if (tsdata->factory_mode) {
436 error = -EIO;
437 goto out;
440 error = kstrtouint(buf, 0, &val);
441 if (error)
442 goto out;
444 if (val < attr->limit_low || val > attr->limit_high) {
445 error = -ERANGE;
446 goto out;
449 switch (tsdata->version) {
450 case EDT_M06:
451 addr = attr->addr_m06;
452 break;
454 case EDT_M09:
455 case EDT_M12:
456 case GENERIC_FT:
457 addr = attr->addr_m09;
458 break;
460 default:
461 error = -ENODEV;
462 goto out;
465 if (addr != NO_REGISTER) {
466 error = edt_ft5x06_register_write(tsdata, addr, val);
467 if (error) {
468 dev_err(&tsdata->client->dev,
469 "Failed to update attribute %s, error: %d\n",
470 dattr->attr.name, error);
471 goto out;
474 *field = val;
476 out:
477 mutex_unlock(&tsdata->mutex);
478 return error ?: count;
481 /* m06, m09: range 0-31, m12: range 0-5 */
482 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
483 M09_REGISTER_GAIN, 0, 31);
484 /* m06, m09: range 0-31, m12: range 0-16 */
485 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
486 M09_REGISTER_OFFSET, 0, 31);
487 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
488 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
489 M09_REGISTER_THRESHOLD, 0, 255);
490 /* m06: range 3 to 14, m12: (0x64: 100Hz) */
491 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
492 NO_REGISTER, 0, 255);
494 static struct attribute *edt_ft5x06_attrs[] = {
495 &edt_ft5x06_attr_gain.dattr.attr,
496 &edt_ft5x06_attr_offset.dattr.attr,
497 &edt_ft5x06_attr_threshold.dattr.attr,
498 &edt_ft5x06_attr_report_rate.dattr.attr,
499 NULL
502 static const struct attribute_group edt_ft5x06_attr_group = {
503 .attrs = edt_ft5x06_attrs,
506 #ifdef CONFIG_DEBUG_FS
507 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
509 struct i2c_client *client = tsdata->client;
510 int retries = EDT_SWITCH_MODE_RETRIES;
511 int ret;
512 int error;
514 if (tsdata->version != EDT_M06) {
515 dev_err(&client->dev,
516 "No factory mode support for non-M06 devices\n");
517 return -EINVAL;
520 disable_irq(client->irq);
522 if (!tsdata->raw_buffer) {
523 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
524 sizeof(u16);
525 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
526 if (!tsdata->raw_buffer) {
527 error = -ENOMEM;
528 goto err_out;
532 /* mode register is 0x3c when in the work mode */
533 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
534 if (error) {
535 dev_err(&client->dev,
536 "failed to switch to factory mode, error %d\n", error);
537 goto err_out;
540 tsdata->factory_mode = true;
541 do {
542 mdelay(EDT_SWITCH_MODE_DELAY);
543 /* mode register is 0x01 when in factory mode */
544 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
545 if (ret == 0x03)
546 break;
547 } while (--retries > 0);
549 if (retries == 0) {
550 dev_err(&client->dev, "not in factory mode after %dms.\n",
551 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
552 error = -EIO;
553 goto err_out;
556 return 0;
558 err_out:
559 kfree(tsdata->raw_buffer);
560 tsdata->raw_buffer = NULL;
561 tsdata->factory_mode = false;
562 enable_irq(client->irq);
564 return error;
567 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
569 struct i2c_client *client = tsdata->client;
570 int retries = EDT_SWITCH_MODE_RETRIES;
571 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
572 int ret;
573 int error;
575 /* mode register is 0x01 when in the factory mode */
576 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
577 if (error) {
578 dev_err(&client->dev,
579 "failed to switch to work mode, error: %d\n", error);
580 return error;
583 tsdata->factory_mode = false;
585 do {
586 mdelay(EDT_SWITCH_MODE_DELAY);
587 /* mode register is 0x01 when in factory mode */
588 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
589 if (ret == 0x01)
590 break;
591 } while (--retries > 0);
593 if (retries == 0) {
594 dev_err(&client->dev, "not in work mode after %dms.\n",
595 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
596 tsdata->factory_mode = true;
597 return -EIO;
600 kfree(tsdata->raw_buffer);
601 tsdata->raw_buffer = NULL;
603 /* restore parameters */
604 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
605 tsdata->threshold);
606 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
607 tsdata->gain);
608 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
609 tsdata->offset);
610 if (reg_addr->reg_report_rate != NO_REGISTER)
611 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
612 tsdata->report_rate);
614 enable_irq(client->irq);
616 return 0;
619 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
621 struct edt_ft5x06_ts_data *tsdata = data;
623 *mode = tsdata->factory_mode;
625 return 0;
628 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
630 struct edt_ft5x06_ts_data *tsdata = data;
631 int retval = 0;
633 if (mode > 1)
634 return -ERANGE;
636 mutex_lock(&tsdata->mutex);
638 if (mode != tsdata->factory_mode) {
639 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
640 edt_ft5x06_work_mode(tsdata);
643 mutex_unlock(&tsdata->mutex);
645 return retval;
648 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
649 edt_ft5x06_debugfs_mode_set, "%llu\n");
651 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
652 char __user *buf, size_t count, loff_t *off)
654 struct edt_ft5x06_ts_data *tsdata = file->private_data;
655 struct i2c_client *client = tsdata->client;
656 int retries = EDT_RAW_DATA_RETRIES;
657 int val, i, error;
658 size_t read = 0;
659 int colbytes;
660 char wrbuf[3];
661 u8 *rdbuf;
663 if (*off < 0 || *off >= tsdata->raw_bufsize)
664 return 0;
666 mutex_lock(&tsdata->mutex);
668 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
669 error = -EIO;
670 goto out;
673 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
674 if (error) {
675 dev_dbg(&client->dev,
676 "failed to write 0x08 register, error %d\n", error);
677 goto out;
680 do {
681 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
682 val = edt_ft5x06_register_read(tsdata, 0x08);
683 if (val < 1)
684 break;
685 } while (--retries > 0);
687 if (val < 0) {
688 error = val;
689 dev_dbg(&client->dev,
690 "failed to read 0x08 register, error %d\n", error);
691 goto out;
694 if (retries == 0) {
695 dev_dbg(&client->dev,
696 "timed out waiting for register to settle\n");
697 error = -ETIMEDOUT;
698 goto out;
701 rdbuf = tsdata->raw_buffer;
702 colbytes = tsdata->num_y * sizeof(u16);
704 wrbuf[0] = 0xf5;
705 wrbuf[1] = 0x0e;
706 for (i = 0; i < tsdata->num_x; i++) {
707 wrbuf[2] = i; /* column index */
708 error = edt_ft5x06_ts_readwrite(tsdata->client,
709 sizeof(wrbuf), wrbuf,
710 colbytes, rdbuf);
711 if (error)
712 goto out;
714 rdbuf += colbytes;
717 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
718 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
719 error = -EFAULT;
720 goto out;
723 *off += read;
724 out:
725 mutex_unlock(&tsdata->mutex);
726 return error ?: read;
729 static const struct file_operations debugfs_raw_data_fops = {
730 .open = simple_open,
731 .read = edt_ft5x06_debugfs_raw_data_read,
734 static void
735 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
736 const char *debugfs_name)
738 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
739 if (!tsdata->debug_dir)
740 return;
742 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
743 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
745 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
746 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
747 debugfs_create_file("raw_data", S_IRUSR,
748 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
751 static void
752 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
754 debugfs_remove_recursive(tsdata->debug_dir);
755 kfree(tsdata->raw_buffer);
758 #else
760 static inline void
761 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
762 const char *debugfs_name)
766 static inline void
767 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
771 #endif /* CONFIG_DEBUGFS */
773 static int edt_ft5x06_ts_identify(struct i2c_client *client,
774 struct edt_ft5x06_ts_data *tsdata,
775 char *fw_version)
777 u8 rdbuf[EDT_NAME_LEN];
778 char *p;
779 int error;
780 char *model_name = tsdata->name;
782 /* see what we find if we assume it is a M06 *
783 * if we get less than EDT_NAME_LEN, we don't want
784 * to have garbage in there
786 memset(rdbuf, 0, sizeof(rdbuf));
787 error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
788 EDT_NAME_LEN - 1, rdbuf);
789 if (error)
790 return error;
792 /* Probe content for something consistent.
793 * M06 starts with a response byte, M12 gives the data directly.
794 * M09/Generic does not provide model number information.
796 if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
797 tsdata->version = EDT_M06;
799 /* remove last '$' end marker */
800 rdbuf[EDT_NAME_LEN - 1] = '\0';
801 if (rdbuf[EDT_NAME_LEN - 2] == '$')
802 rdbuf[EDT_NAME_LEN - 2] = '\0';
804 /* look for Model/Version separator */
805 p = strchr(rdbuf, '*');
806 if (p)
807 *p++ = '\0';
808 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
809 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
810 } else if (!strncasecmp(rdbuf, "EP0", 3)) {
811 tsdata->version = EDT_M12;
813 /* remove last '$' end marker */
814 rdbuf[EDT_NAME_LEN - 2] = '\0';
815 if (rdbuf[EDT_NAME_LEN - 3] == '$')
816 rdbuf[EDT_NAME_LEN - 3] = '\0';
818 /* look for Model/Version separator */
819 p = strchr(rdbuf, '*');
820 if (p)
821 *p++ = '\0';
822 strlcpy(model_name, rdbuf, EDT_NAME_LEN);
823 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
824 } else {
825 /* If it is not an EDT M06/M12 touchscreen, then the model
826 * detection is a bit hairy. The different ft5x06
827 * firmares around don't reliably implement the
828 * identification registers. Well, we'll take a shot.
830 * The main difference between generic focaltec based
831 * touches and EDT M09 is that we know how to retrieve
832 * the max coordinates for the latter.
834 tsdata->version = GENERIC_FT;
836 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
837 2, rdbuf);
838 if (error)
839 return error;
841 strlcpy(fw_version, rdbuf, 2);
843 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
844 1, rdbuf);
845 if (error)
846 return error;
848 /* This "model identification" is not exact. Unfortunately
849 * not all firmwares for the ft5x06 put useful values in
850 * the identification registers.
852 switch (rdbuf[0]) {
853 case 0x35: /* EDT EP0350M09 */
854 case 0x43: /* EDT EP0430M09 */
855 case 0x50: /* EDT EP0500M09 */
856 case 0x57: /* EDT EP0570M09 */
857 case 0x70: /* EDT EP0700M09 */
858 tsdata->version = EDT_M09;
859 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
860 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
861 break;
862 case 0xa1: /* EDT EP1010ML00 */
863 tsdata->version = EDT_M09;
864 snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
865 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
866 break;
867 case 0x5a: /* Solomon Goldentek Display */
868 snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
869 break;
870 default:
871 snprintf(model_name, EDT_NAME_LEN,
872 "generic ft5x06 (%02x)",
873 rdbuf[0]);
874 break;
878 return 0;
881 static void edt_ft5x06_ts_get_defaults(struct device *dev,
882 struct edt_ft5x06_ts_data *tsdata)
884 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
885 u32 val;
886 int error;
888 error = device_property_read_u32(dev, "threshold", &val);
889 if (!error) {
890 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
891 tsdata->threshold = val;
894 error = device_property_read_u32(dev, "gain", &val);
895 if (!error) {
896 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
897 tsdata->gain = val;
900 error = device_property_read_u32(dev, "offset", &val);
901 if (!error) {
902 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
903 tsdata->offset = val;
907 static void
908 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
910 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
912 tsdata->threshold = edt_ft5x06_register_read(tsdata,
913 reg_addr->reg_threshold);
914 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
915 tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
916 if (reg_addr->reg_report_rate != NO_REGISTER)
917 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
918 reg_addr->reg_report_rate);
919 if (tsdata->version == EDT_M06 ||
920 tsdata->version == EDT_M09 ||
921 tsdata->version == EDT_M12) {
922 tsdata->num_x = edt_ft5x06_register_read(tsdata,
923 reg_addr->reg_num_x);
924 tsdata->num_y = edt_ft5x06_register_read(tsdata,
925 reg_addr->reg_num_y);
926 } else {
927 tsdata->num_x = -1;
928 tsdata->num_y = -1;
932 static void
933 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
935 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
937 switch (tsdata->version) {
938 case EDT_M06:
939 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
940 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
941 reg_addr->reg_gain = WORK_REGISTER_GAIN;
942 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
943 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
944 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
945 break;
947 case EDT_M09:
948 case EDT_M12:
949 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
950 reg_addr->reg_report_rate = NO_REGISTER;
951 reg_addr->reg_gain = M09_REGISTER_GAIN;
952 reg_addr->reg_offset = M09_REGISTER_OFFSET;
953 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
954 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
955 break;
957 case GENERIC_FT:
958 /* this is a guesswork */
959 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
960 reg_addr->reg_gain = M09_REGISTER_GAIN;
961 reg_addr->reg_offset = M09_REGISTER_OFFSET;
962 break;
966 static int edt_ft5x06_ts_probe(struct i2c_client *client,
967 const struct i2c_device_id *id)
969 const struct edt_i2c_chip_data *chip_data;
970 struct edt_ft5x06_ts_data *tsdata;
971 u8 buf[2] = { 0xfc, 0x00 };
972 struct input_dev *input;
973 unsigned long irq_flags;
974 int error;
975 char fw_version[EDT_NAME_LEN];
977 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
979 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
980 if (!tsdata) {
981 dev_err(&client->dev, "failed to allocate driver data.\n");
982 return -ENOMEM;
985 chip_data = of_device_get_match_data(&client->dev);
986 if (!chip_data)
987 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
988 if (!chip_data || !chip_data->max_support_points) {
989 dev_err(&client->dev, "invalid or missing chip data\n");
990 return -EINVAL;
993 tsdata->max_support_points = chip_data->max_support_points;
995 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
996 "reset", GPIOD_OUT_HIGH);
997 if (IS_ERR(tsdata->reset_gpio)) {
998 error = PTR_ERR(tsdata->reset_gpio);
999 dev_err(&client->dev,
1000 "Failed to request GPIO reset pin, error %d\n", error);
1001 return error;
1004 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1005 "wake", GPIOD_OUT_LOW);
1006 if (IS_ERR(tsdata->wake_gpio)) {
1007 error = PTR_ERR(tsdata->wake_gpio);
1008 dev_err(&client->dev,
1009 "Failed to request GPIO wake pin, error %d\n", error);
1010 return error;
1013 if (tsdata->wake_gpio) {
1014 usleep_range(5000, 6000);
1015 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1018 if (tsdata->reset_gpio) {
1019 usleep_range(5000, 6000);
1020 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1021 msleep(300);
1024 input = devm_input_allocate_device(&client->dev);
1025 if (!input) {
1026 dev_err(&client->dev, "failed to allocate input device.\n");
1027 return -ENOMEM;
1030 mutex_init(&tsdata->mutex);
1031 tsdata->client = client;
1032 tsdata->input = input;
1033 tsdata->factory_mode = false;
1035 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
1036 if (error) {
1037 dev_err(&client->dev, "touchscreen probe failed\n");
1038 return error;
1042 * Dummy read access. EP0700MLP1 returns bogus data on the first
1043 * register read access and ignores writes.
1045 edt_ft5x06_ts_readwrite(tsdata->client, 2, buf, 2, buf);
1047 edt_ft5x06_ts_set_regs(tsdata);
1048 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1049 edt_ft5x06_ts_get_parameters(tsdata);
1051 dev_dbg(&client->dev,
1052 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1053 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1055 input->name = tsdata->name;
1056 input->id.bustype = BUS_I2C;
1057 input->dev.parent = &client->dev;
1059 if (tsdata->version == EDT_M06 ||
1060 tsdata->version == EDT_M09 ||
1061 tsdata->version == EDT_M12) {
1062 input_set_abs_params(input, ABS_MT_POSITION_X,
1063 0, tsdata->num_x * 64 - 1, 0, 0);
1064 input_set_abs_params(input, ABS_MT_POSITION_Y,
1065 0, tsdata->num_y * 64 - 1, 0, 0);
1066 } else {
1067 /* Unknown maximum values. Specify via devicetree */
1068 input_set_abs_params(input, ABS_MT_POSITION_X,
1069 0, 65535, 0, 0);
1070 input_set_abs_params(input, ABS_MT_POSITION_Y,
1071 0, 65535, 0, 0);
1074 touchscreen_parse_properties(input, true, &tsdata->prop);
1076 error = input_mt_init_slots(input, tsdata->max_support_points,
1077 INPUT_MT_DIRECT);
1078 if (error) {
1079 dev_err(&client->dev, "Unable to init MT slots.\n");
1080 return error;
1083 i2c_set_clientdata(client, tsdata);
1085 irq_flags = irq_get_trigger_type(client->irq);
1086 if (irq_flags == IRQF_TRIGGER_NONE)
1087 irq_flags = IRQF_TRIGGER_FALLING;
1088 irq_flags |= IRQF_ONESHOT;
1090 error = devm_request_threaded_irq(&client->dev, client->irq,
1091 NULL, edt_ft5x06_ts_isr, irq_flags,
1092 client->name, tsdata);
1093 if (error) {
1094 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1095 return error;
1098 error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
1099 if (error)
1100 return error;
1102 error = input_register_device(input);
1103 if (error)
1104 return error;
1106 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1107 device_init_wakeup(&client->dev, 1);
1109 dev_dbg(&client->dev,
1110 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1111 client->irq,
1112 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1113 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1115 return 0;
1118 static int edt_ft5x06_ts_remove(struct i2c_client *client)
1120 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1122 edt_ft5x06_ts_teardown_debugfs(tsdata);
1124 return 0;
1127 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1129 struct i2c_client *client = to_i2c_client(dev);
1131 if (device_may_wakeup(dev))
1132 enable_irq_wake(client->irq);
1134 return 0;
1137 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1139 struct i2c_client *client = to_i2c_client(dev);
1141 if (device_may_wakeup(dev))
1142 disable_irq_wake(client->irq);
1144 return 0;
1147 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1148 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1150 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1151 .max_support_points = 5,
1154 static const struct edt_i2c_chip_data edt_ft5506_data = {
1155 .max_support_points = 10,
1158 static const struct edt_i2c_chip_data edt_ft6236_data = {
1159 .max_support_points = 2,
1162 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1163 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1164 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1165 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1166 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1167 { /* sentinel */ }
1169 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1171 #ifdef CONFIG_OF
1172 static const struct of_device_id edt_ft5x06_of_match[] = {
1173 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1174 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1175 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1176 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1177 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1178 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1179 { /* sentinel */ }
1181 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1182 #endif
1184 static struct i2c_driver edt_ft5x06_ts_driver = {
1185 .driver = {
1186 .name = "edt_ft5x06",
1187 .of_match_table = of_match_ptr(edt_ft5x06_of_match),
1188 .pm = &edt_ft5x06_ts_pm_ops,
1190 .id_table = edt_ft5x06_ts_id,
1191 .probe = edt_ft5x06_ts_probe,
1192 .remove = edt_ft5x06_ts_remove,
1195 module_i2c_driver(edt_ft5x06_ts_driver);
1197 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1198 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1199 MODULE_LICENSE("GPL");