Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / input / touchscreen / edt-ft5x06.c
blobf872817e81e46ba6d962a5edc207302e9bd25f3f
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 M06,
74 M09,
77 struct edt_reg_addr {
78 int reg_threshold;
79 int reg_report_rate;
80 int reg_gain;
81 int reg_offset;
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;
93 struct gpio_desc *reset_gpio;
94 struct gpio_desc *wake_gpio;
96 #if defined(CONFIG_DEBUG_FS)
97 struct dentry *debug_dir;
98 u8 *raw_buffer;
99 size_t raw_bufsize;
100 #endif
102 struct mutex mutex;
103 bool factory_mode;
104 int threshold;
105 int gain;
106 int offset;
107 int report_rate;
108 int max_support_points;
110 char name[EDT_NAME_LEN];
112 struct edt_reg_addr reg_addr;
113 enum edt_ver version;
116 struct edt_i2c_chip_data {
117 int max_support_points;
120 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
121 u16 wr_len, u8 *wr_buf,
122 u16 rd_len, u8 *rd_buf)
124 struct i2c_msg wrmsg[2];
125 int i = 0;
126 int ret;
128 if (wr_len) {
129 wrmsg[i].addr = client->addr;
130 wrmsg[i].flags = 0;
131 wrmsg[i].len = wr_len;
132 wrmsg[i].buf = wr_buf;
133 i++;
135 if (rd_len) {
136 wrmsg[i].addr = client->addr;
137 wrmsg[i].flags = I2C_M_RD;
138 wrmsg[i].len = rd_len;
139 wrmsg[i].buf = rd_buf;
140 i++;
143 ret = i2c_transfer(client->adapter, wrmsg, i);
144 if (ret < 0)
145 return ret;
146 if (ret != i)
147 return -EIO;
149 return 0;
152 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
153 u8 *buf, int buflen)
155 int i;
156 u8 crc = 0;
158 for (i = 0; i < buflen - 1; i++)
159 crc ^= buf[i];
161 if (crc != buf[buflen-1]) {
162 dev_err_ratelimited(&tsdata->client->dev,
163 "crc error: 0x%02x expected, got 0x%02x\n",
164 crc, buf[buflen-1]);
165 return false;
168 return true;
171 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
173 struct edt_ft5x06_ts_data *tsdata = dev_id;
174 struct device *dev = &tsdata->client->dev;
175 u8 cmd;
176 u8 rdbuf[63];
177 int i, type, x, y, id;
178 int offset, tplen, datalen, crclen;
179 int error;
181 switch (tsdata->version) {
182 case M06:
183 cmd = 0xf9; /* tell the controller to send touch data */
184 offset = 5; /* where the actual touch data starts */
185 tplen = 4; /* data comes in so called frames */
186 crclen = 1; /* length of the crc data */
187 break;
189 case M09:
190 cmd = 0x0;
191 offset = 3;
192 tplen = 6;
193 crclen = 0;
194 break;
196 default:
197 goto out;
200 memset(rdbuf, 0, sizeof(rdbuf));
201 datalen = tplen * tsdata->max_support_points + offset + crclen;
203 error = edt_ft5x06_ts_readwrite(tsdata->client,
204 sizeof(cmd), &cmd,
205 datalen, rdbuf);
206 if (error) {
207 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
208 error);
209 goto out;
212 /* M09 does not send header or CRC */
213 if (tsdata->version == M06) {
214 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
215 rdbuf[2] != datalen) {
216 dev_err_ratelimited(dev,
217 "Unexpected header: %02x%02x%02x!\n",
218 rdbuf[0], rdbuf[1], rdbuf[2]);
219 goto out;
222 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
223 goto out;
226 for (i = 0; i < tsdata->max_support_points; i++) {
227 u8 *buf = &rdbuf[i * tplen + offset];
228 bool down;
230 type = buf[0] >> 6;
231 /* ignore Reserved events */
232 if (type == TOUCH_EVENT_RESERVED)
233 continue;
235 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
236 if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN)
237 continue;
239 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
240 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
241 id = (buf[2] >> 4) & 0x0f;
242 down = type != TOUCH_EVENT_UP;
244 input_mt_slot(tsdata->input, id);
245 input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
247 if (!down)
248 continue;
250 touchscreen_report_pos(tsdata->input, &tsdata->prop, x, y,
251 true);
254 input_mt_report_pointer_emulation(tsdata->input, true);
255 input_sync(tsdata->input);
257 out:
258 return IRQ_HANDLED;
261 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
262 u8 addr, u8 value)
264 u8 wrbuf[4];
266 switch (tsdata->version) {
267 case M06:
268 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
269 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
270 wrbuf[2] = value;
271 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
272 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
273 wrbuf, 0, NULL);
274 case M09:
275 wrbuf[0] = addr;
276 wrbuf[1] = value;
278 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
279 wrbuf, 0, NULL);
281 default:
282 return -EINVAL;
286 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
287 u8 addr)
289 u8 wrbuf[2], rdbuf[2];
290 int error;
292 switch (tsdata->version) {
293 case M06:
294 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
295 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
296 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
298 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
299 rdbuf);
300 if (error)
301 return error;
303 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
304 dev_err(&tsdata->client->dev,
305 "crc error: 0x%02x expected, got 0x%02x\n",
306 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
307 rdbuf[1]);
308 return -EIO;
310 break;
312 case M09:
313 wrbuf[0] = addr;
314 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
315 wrbuf, 1, rdbuf);
316 if (error)
317 return error;
318 break;
320 default:
321 return -EINVAL;
324 return rdbuf[0];
327 struct edt_ft5x06_attribute {
328 struct device_attribute dattr;
329 size_t field_offset;
330 u8 limit_low;
331 u8 limit_high;
332 u8 addr_m06;
333 u8 addr_m09;
336 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
337 _limit_low, _limit_high) \
338 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
339 .dattr = __ATTR(_field, _mode, \
340 edt_ft5x06_setting_show, \
341 edt_ft5x06_setting_store), \
342 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
343 .addr_m06 = _addr_m06, \
344 .addr_m09 = _addr_m09, \
345 .limit_low = _limit_low, \
346 .limit_high = _limit_high, \
349 static ssize_t edt_ft5x06_setting_show(struct device *dev,
350 struct device_attribute *dattr,
351 char *buf)
353 struct i2c_client *client = to_i2c_client(dev);
354 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
355 struct edt_ft5x06_attribute *attr =
356 container_of(dattr, struct edt_ft5x06_attribute, dattr);
357 u8 *field = (u8 *)tsdata + attr->field_offset;
358 int val;
359 size_t count = 0;
360 int error = 0;
361 u8 addr;
363 mutex_lock(&tsdata->mutex);
365 if (tsdata->factory_mode) {
366 error = -EIO;
367 goto out;
370 switch (tsdata->version) {
371 case M06:
372 addr = attr->addr_m06;
373 break;
375 case M09:
376 addr = attr->addr_m09;
377 break;
379 default:
380 error = -ENODEV;
381 goto out;
384 if (addr != NO_REGISTER) {
385 val = edt_ft5x06_register_read(tsdata, addr);
386 if (val < 0) {
387 error = val;
388 dev_err(&tsdata->client->dev,
389 "Failed to fetch attribute %s, error %d\n",
390 dattr->attr.name, error);
391 goto out;
393 } else {
394 val = *field;
397 if (val != *field) {
398 dev_warn(&tsdata->client->dev,
399 "%s: read (%d) and stored value (%d) differ\n",
400 dattr->attr.name, val, *field);
401 *field = val;
404 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
405 out:
406 mutex_unlock(&tsdata->mutex);
407 return error ?: count;
410 static ssize_t edt_ft5x06_setting_store(struct device *dev,
411 struct device_attribute *dattr,
412 const char *buf, size_t count)
414 struct i2c_client *client = to_i2c_client(dev);
415 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
416 struct edt_ft5x06_attribute *attr =
417 container_of(dattr, struct edt_ft5x06_attribute, dattr);
418 u8 *field = (u8 *)tsdata + attr->field_offset;
419 unsigned int val;
420 int error;
421 u8 addr;
423 mutex_lock(&tsdata->mutex);
425 if (tsdata->factory_mode) {
426 error = -EIO;
427 goto out;
430 error = kstrtouint(buf, 0, &val);
431 if (error)
432 goto out;
434 if (val < attr->limit_low || val > attr->limit_high) {
435 error = -ERANGE;
436 goto out;
439 switch (tsdata->version) {
440 case M06:
441 addr = attr->addr_m06;
442 break;
444 case M09:
445 addr = attr->addr_m09;
446 break;
448 default:
449 error = -ENODEV;
450 goto out;
453 if (addr != NO_REGISTER) {
454 error = edt_ft5x06_register_write(tsdata, addr, val);
455 if (error) {
456 dev_err(&tsdata->client->dev,
457 "Failed to update attribute %s, error: %d\n",
458 dattr->attr.name, error);
459 goto out;
462 *field = val;
464 out:
465 mutex_unlock(&tsdata->mutex);
466 return error ?: count;
469 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
470 M09_REGISTER_GAIN, 0, 31);
471 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
472 M09_REGISTER_OFFSET, 0, 31);
473 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
474 M09_REGISTER_THRESHOLD, 0, 80);
475 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
476 NO_REGISTER, 3, 14);
478 static struct attribute *edt_ft5x06_attrs[] = {
479 &edt_ft5x06_attr_gain.dattr.attr,
480 &edt_ft5x06_attr_offset.dattr.attr,
481 &edt_ft5x06_attr_threshold.dattr.attr,
482 &edt_ft5x06_attr_report_rate.dattr.attr,
483 NULL
486 static const struct attribute_group edt_ft5x06_attr_group = {
487 .attrs = edt_ft5x06_attrs,
490 #ifdef CONFIG_DEBUG_FS
491 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
493 struct i2c_client *client = tsdata->client;
494 int retries = EDT_SWITCH_MODE_RETRIES;
495 int ret;
496 int error;
498 disable_irq(client->irq);
500 if (!tsdata->raw_buffer) {
501 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
502 sizeof(u16);
503 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
504 if (!tsdata->raw_buffer) {
505 error = -ENOMEM;
506 goto err_out;
510 /* mode register is 0x3c when in the work mode */
511 if (tsdata->version == M09)
512 goto m09_out;
514 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
515 if (error) {
516 dev_err(&client->dev,
517 "failed to switch to factory mode, error %d\n", error);
518 goto err_out;
521 tsdata->factory_mode = true;
522 do {
523 mdelay(EDT_SWITCH_MODE_DELAY);
524 /* mode register is 0x01 when in factory mode */
525 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
526 if (ret == 0x03)
527 break;
528 } while (--retries > 0);
530 if (retries == 0) {
531 dev_err(&client->dev, "not in factory mode after %dms.\n",
532 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
533 error = -EIO;
534 goto err_out;
537 return 0;
539 err_out:
540 kfree(tsdata->raw_buffer);
541 tsdata->raw_buffer = NULL;
542 tsdata->factory_mode = false;
543 enable_irq(client->irq);
545 return error;
547 m09_out:
548 dev_err(&client->dev, "No factory mode support for M09\n");
549 return -EINVAL;
553 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
555 struct i2c_client *client = tsdata->client;
556 int retries = EDT_SWITCH_MODE_RETRIES;
557 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
558 int ret;
559 int error;
561 /* mode register is 0x01 when in the factory mode */
562 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
563 if (error) {
564 dev_err(&client->dev,
565 "failed to switch to work mode, error: %d\n", error);
566 return error;
569 tsdata->factory_mode = false;
571 do {
572 mdelay(EDT_SWITCH_MODE_DELAY);
573 /* mode register is 0x01 when in factory mode */
574 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
575 if (ret == 0x01)
576 break;
577 } while (--retries > 0);
579 if (retries == 0) {
580 dev_err(&client->dev, "not in work mode after %dms.\n",
581 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
582 tsdata->factory_mode = true;
583 return -EIO;
586 kfree(tsdata->raw_buffer);
587 tsdata->raw_buffer = NULL;
589 /* restore parameters */
590 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
591 tsdata->threshold);
592 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
593 tsdata->gain);
594 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
595 tsdata->offset);
596 if (reg_addr->reg_report_rate)
597 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
598 tsdata->report_rate);
600 enable_irq(client->irq);
602 return 0;
605 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
607 struct edt_ft5x06_ts_data *tsdata = data;
609 *mode = tsdata->factory_mode;
611 return 0;
614 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
616 struct edt_ft5x06_ts_data *tsdata = data;
617 int retval = 0;
619 if (mode > 1)
620 return -ERANGE;
622 mutex_lock(&tsdata->mutex);
624 if (mode != tsdata->factory_mode) {
625 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
626 edt_ft5x06_work_mode(tsdata);
629 mutex_unlock(&tsdata->mutex);
631 return retval;
634 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
635 edt_ft5x06_debugfs_mode_set, "%llu\n");
637 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
638 char __user *buf, size_t count, loff_t *off)
640 struct edt_ft5x06_ts_data *tsdata = file->private_data;
641 struct i2c_client *client = tsdata->client;
642 int retries = EDT_RAW_DATA_RETRIES;
643 int val, i, error;
644 size_t read = 0;
645 int colbytes;
646 char wrbuf[3];
647 u8 *rdbuf;
649 if (*off < 0 || *off >= tsdata->raw_bufsize)
650 return 0;
652 mutex_lock(&tsdata->mutex);
654 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
655 error = -EIO;
656 goto out;
659 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
660 if (error) {
661 dev_dbg(&client->dev,
662 "failed to write 0x08 register, error %d\n", error);
663 goto out;
666 do {
667 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
668 val = edt_ft5x06_register_read(tsdata, 0x08);
669 if (val < 1)
670 break;
671 } while (--retries > 0);
673 if (val < 0) {
674 error = val;
675 dev_dbg(&client->dev,
676 "failed to read 0x08 register, error %d\n", error);
677 goto out;
680 if (retries == 0) {
681 dev_dbg(&client->dev,
682 "timed out waiting for register to settle\n");
683 error = -ETIMEDOUT;
684 goto out;
687 rdbuf = tsdata->raw_buffer;
688 colbytes = tsdata->num_y * sizeof(u16);
690 wrbuf[0] = 0xf5;
691 wrbuf[1] = 0x0e;
692 for (i = 0; i < tsdata->num_x; i++) {
693 wrbuf[2] = i; /* column index */
694 error = edt_ft5x06_ts_readwrite(tsdata->client,
695 sizeof(wrbuf), wrbuf,
696 colbytes, rdbuf);
697 if (error)
698 goto out;
700 rdbuf += colbytes;
703 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
704 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
705 error = -EFAULT;
706 goto out;
709 *off += read;
710 out:
711 mutex_unlock(&tsdata->mutex);
712 return error ?: read;
715 static const struct file_operations debugfs_raw_data_fops = {
716 .open = simple_open,
717 .read = edt_ft5x06_debugfs_raw_data_read,
720 static void
721 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
722 const char *debugfs_name)
724 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
725 if (!tsdata->debug_dir)
726 return;
728 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
729 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
731 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
732 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
733 debugfs_create_file("raw_data", S_IRUSR,
734 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
737 static void
738 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
740 debugfs_remove_recursive(tsdata->debug_dir);
741 kfree(tsdata->raw_buffer);
744 #else
746 static inline void
747 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
748 const char *debugfs_name)
752 static inline void
753 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
757 #endif /* CONFIG_DEBUGFS */
759 static int edt_ft5x06_ts_identify(struct i2c_client *client,
760 struct edt_ft5x06_ts_data *tsdata,
761 char *fw_version)
763 u8 rdbuf[EDT_NAME_LEN];
764 char *p;
765 int error;
766 char *model_name = tsdata->name;
768 /* see what we find if we assume it is a M06 *
769 * if we get less than EDT_NAME_LEN, we don't want
770 * to have garbage in there
772 memset(rdbuf, 0, sizeof(rdbuf));
773 error = edt_ft5x06_ts_readwrite(client, 1, "\xbb",
774 EDT_NAME_LEN - 1, rdbuf);
775 if (error)
776 return error;
778 /* if we find something consistent, stay with that assumption
779 * at least M09 won't send 3 bytes here
781 if (!(strncasecmp(rdbuf + 1, "EP0", 3))) {
782 tsdata->version = M06;
784 /* remove last '$' end marker */
785 rdbuf[EDT_NAME_LEN - 1] = '\0';
786 if (rdbuf[EDT_NAME_LEN - 2] == '$')
787 rdbuf[EDT_NAME_LEN - 2] = '\0';
789 /* look for Model/Version separator */
790 p = strchr(rdbuf, '*');
791 if (p)
792 *p++ = '\0';
793 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
794 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
795 } else {
796 /* since there are only two versions around (M06, M09) */
797 tsdata->version = M09;
799 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
800 2, rdbuf);
801 if (error)
802 return error;
804 strlcpy(fw_version, rdbuf, 2);
806 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
807 1, rdbuf);
808 if (error)
809 return error;
811 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
812 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
815 return 0;
818 static void edt_ft5x06_ts_get_defaults(struct device *dev,
819 struct edt_ft5x06_ts_data *tsdata)
821 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
822 u32 val;
823 int error;
825 error = device_property_read_u32(dev, "threshold", &val);
826 if (!error) {
827 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
828 tsdata->threshold = val;
831 error = device_property_read_u32(dev, "gain", &val);
832 if (!error) {
833 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
834 tsdata->gain = val;
837 error = device_property_read_u32(dev, "offset", &val);
838 if (!error) {
839 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
840 tsdata->offset = val;
844 static void
845 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
847 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
849 tsdata->threshold = edt_ft5x06_register_read(tsdata,
850 reg_addr->reg_threshold);
851 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
852 tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
853 if (reg_addr->reg_report_rate != NO_REGISTER)
854 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
855 reg_addr->reg_report_rate);
856 tsdata->num_x = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_x);
857 tsdata->num_y = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_y);
860 static void
861 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
863 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
865 switch (tsdata->version) {
866 case M06:
867 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
868 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
869 reg_addr->reg_gain = WORK_REGISTER_GAIN;
870 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
871 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
872 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
873 break;
875 case M09:
876 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
877 reg_addr->reg_gain = M09_REGISTER_GAIN;
878 reg_addr->reg_offset = M09_REGISTER_OFFSET;
879 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
880 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
881 break;
885 static int edt_ft5x06_ts_probe(struct i2c_client *client,
886 const struct i2c_device_id *id)
888 const struct edt_i2c_chip_data *chip_data;
889 struct edt_ft5x06_ts_data *tsdata;
890 struct input_dev *input;
891 unsigned long irq_flags;
892 int error;
893 char fw_version[EDT_NAME_LEN];
895 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
897 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
898 if (!tsdata) {
899 dev_err(&client->dev, "failed to allocate driver data.\n");
900 return -ENOMEM;
903 chip_data = of_device_get_match_data(&client->dev);
904 if (!chip_data)
905 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
906 if (!chip_data || !chip_data->max_support_points) {
907 dev_err(&client->dev, "invalid or missing chip data\n");
908 return -EINVAL;
911 tsdata->max_support_points = chip_data->max_support_points;
913 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
914 "reset", GPIOD_OUT_HIGH);
915 if (IS_ERR(tsdata->reset_gpio)) {
916 error = PTR_ERR(tsdata->reset_gpio);
917 dev_err(&client->dev,
918 "Failed to request GPIO reset pin, error %d\n", error);
919 return error;
922 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
923 "wake", GPIOD_OUT_LOW);
924 if (IS_ERR(tsdata->wake_gpio)) {
925 error = PTR_ERR(tsdata->wake_gpio);
926 dev_err(&client->dev,
927 "Failed to request GPIO wake pin, error %d\n", error);
928 return error;
931 if (tsdata->wake_gpio) {
932 usleep_range(5000, 6000);
933 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
936 if (tsdata->reset_gpio) {
937 usleep_range(5000, 6000);
938 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
939 msleep(300);
942 input = devm_input_allocate_device(&client->dev);
943 if (!input) {
944 dev_err(&client->dev, "failed to allocate input device.\n");
945 return -ENOMEM;
948 mutex_init(&tsdata->mutex);
949 tsdata->client = client;
950 tsdata->input = input;
951 tsdata->factory_mode = false;
953 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
954 if (error) {
955 dev_err(&client->dev, "touchscreen probe failed\n");
956 return error;
959 edt_ft5x06_ts_set_regs(tsdata);
960 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
961 edt_ft5x06_ts_get_parameters(tsdata);
963 dev_dbg(&client->dev,
964 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
965 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
967 input->name = tsdata->name;
968 input->id.bustype = BUS_I2C;
969 input->dev.parent = &client->dev;
971 input_set_abs_params(input, ABS_MT_POSITION_X,
972 0, tsdata->num_x * 64 - 1, 0, 0);
973 input_set_abs_params(input, ABS_MT_POSITION_Y,
974 0, tsdata->num_y * 64 - 1, 0, 0);
976 touchscreen_parse_properties(input, true, &tsdata->prop);
978 error = input_mt_init_slots(input, tsdata->max_support_points,
979 INPUT_MT_DIRECT);
980 if (error) {
981 dev_err(&client->dev, "Unable to init MT slots.\n");
982 return error;
985 i2c_set_clientdata(client, tsdata);
987 irq_flags = irq_get_trigger_type(client->irq);
988 if (irq_flags == IRQF_TRIGGER_NONE)
989 irq_flags = IRQF_TRIGGER_FALLING;
990 irq_flags |= IRQF_ONESHOT;
992 error = devm_request_threaded_irq(&client->dev, client->irq,
993 NULL, edt_ft5x06_ts_isr, irq_flags,
994 client->name, tsdata);
995 if (error) {
996 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
997 return error;
1000 error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1001 if (error)
1002 return error;
1004 error = input_register_device(input);
1005 if (error)
1006 goto err_remove_attrs;
1008 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1009 device_init_wakeup(&client->dev, 1);
1011 dev_dbg(&client->dev,
1012 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1013 client->irq,
1014 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1015 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1017 return 0;
1019 err_remove_attrs:
1020 sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1021 return error;
1024 static int edt_ft5x06_ts_remove(struct i2c_client *client)
1026 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1028 edt_ft5x06_ts_teardown_debugfs(tsdata);
1029 sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1031 return 0;
1034 static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
1036 struct i2c_client *client = to_i2c_client(dev);
1038 if (device_may_wakeup(dev))
1039 enable_irq_wake(client->irq);
1041 return 0;
1044 static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
1046 struct i2c_client *client = to_i2c_client(dev);
1048 if (device_may_wakeup(dev))
1049 disable_irq_wake(client->irq);
1051 return 0;
1054 static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1055 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1057 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1058 .max_support_points = 5,
1061 static const struct edt_i2c_chip_data edt_ft5506_data = {
1062 .max_support_points = 10,
1065 static const struct edt_i2c_chip_data edt_ft6236_data = {
1066 .max_support_points = 2,
1069 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1070 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1071 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1072 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1073 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1074 { /* sentinel */ }
1076 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1078 #ifdef CONFIG_OF
1079 static const struct of_device_id edt_ft5x06_of_match[] = {
1080 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1081 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1082 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1083 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1084 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1085 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1086 { /* sentinel */ }
1088 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1089 #endif
1091 static struct i2c_driver edt_ft5x06_ts_driver = {
1092 .driver = {
1093 .name = "edt_ft5x06",
1094 .of_match_table = of_match_ptr(edt_ft5x06_of_match),
1095 .pm = &edt_ft5x06_ts_pm_ops,
1097 .id_table = edt_ft5x06_ts_id,
1098 .probe = edt_ft5x06_ts_probe,
1099 .remove = edt_ft5x06_ts_remove,
1102 module_i2c_driver(edt_ft5x06_ts_driver);
1104 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1105 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1106 MODULE_LICENSE("GPL");