mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / input / touchscreen / edt-ft5x06.c
blob23fbe382da8b420791a1fec28962454132d4e7ef
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 1 /* msec */
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 u16 num_x;
90 u16 num_y;
92 struct gpio_desc *reset_gpio;
93 struct gpio_desc *wake_gpio;
95 #if defined(CONFIG_DEBUG_FS)
96 struct dentry *debug_dir;
97 u8 *raw_buffer;
98 size_t raw_bufsize;
99 #endif
101 struct mutex mutex;
102 bool factory_mode;
103 int threshold;
104 int gain;
105 int offset;
106 int report_rate;
107 int max_support_points;
109 char name[EDT_NAME_LEN];
111 struct edt_reg_addr reg_addr;
112 enum edt_ver version;
115 struct edt_i2c_chip_data {
116 int max_support_points;
119 static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
120 u16 wr_len, u8 *wr_buf,
121 u16 rd_len, u8 *rd_buf)
123 struct i2c_msg wrmsg[2];
124 int i = 0;
125 int ret;
127 if (wr_len) {
128 wrmsg[i].addr = client->addr;
129 wrmsg[i].flags = 0;
130 wrmsg[i].len = wr_len;
131 wrmsg[i].buf = wr_buf;
132 i++;
134 if (rd_len) {
135 wrmsg[i].addr = client->addr;
136 wrmsg[i].flags = I2C_M_RD;
137 wrmsg[i].len = rd_len;
138 wrmsg[i].buf = rd_buf;
139 i++;
142 ret = i2c_transfer(client->adapter, wrmsg, i);
143 if (ret < 0)
144 return ret;
145 if (ret != i)
146 return -EIO;
148 return 0;
151 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
152 u8 *buf, int buflen)
154 int i;
155 u8 crc = 0;
157 for (i = 0; i < buflen - 1; i++)
158 crc ^= buf[i];
160 if (crc != buf[buflen-1]) {
161 dev_err_ratelimited(&tsdata->client->dev,
162 "crc error: 0x%02x expected, got 0x%02x\n",
163 crc, buf[buflen-1]);
164 return false;
167 return true;
170 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
172 struct edt_ft5x06_ts_data *tsdata = dev_id;
173 struct device *dev = &tsdata->client->dev;
174 u8 cmd;
175 u8 rdbuf[63];
176 int i, type, x, y, id;
177 int offset, tplen, datalen, crclen;
178 int error;
180 switch (tsdata->version) {
181 case M06:
182 cmd = 0xf9; /* tell the controller to send touch data */
183 offset = 5; /* where the actual touch data starts */
184 tplen = 4; /* data comes in so called frames */
185 crclen = 1; /* length of the crc data */
186 break;
188 case M09:
189 cmd = 0x0;
190 offset = 3;
191 tplen = 6;
192 crclen = 0;
193 break;
195 default:
196 goto out;
199 memset(rdbuf, 0, sizeof(rdbuf));
200 datalen = tplen * tsdata->max_support_points + offset + crclen;
202 error = edt_ft5x06_ts_readwrite(tsdata->client,
203 sizeof(cmd), &cmd,
204 datalen, rdbuf);
205 if (error) {
206 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
207 error);
208 goto out;
211 /* M09 does not send header or CRC */
212 if (tsdata->version == M06) {
213 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
214 rdbuf[2] != datalen) {
215 dev_err_ratelimited(dev,
216 "Unexpected header: %02x%02x%02x!\n",
217 rdbuf[0], rdbuf[1], rdbuf[2]);
218 goto out;
221 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
222 goto out;
225 for (i = 0; i < tsdata->max_support_points; i++) {
226 u8 *buf = &rdbuf[i * tplen + offset];
227 bool down;
229 type = buf[0] >> 6;
230 /* ignore Reserved events */
231 if (type == TOUCH_EVENT_RESERVED)
232 continue;
234 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
235 if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN)
236 continue;
238 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
239 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
240 id = (buf[2] >> 4) & 0x0f;
241 down = type != TOUCH_EVENT_UP;
243 input_mt_slot(tsdata->input, id);
244 input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
246 if (!down)
247 continue;
249 input_report_abs(tsdata->input, ABS_MT_POSITION_X, x);
250 input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y);
253 input_mt_report_pointer_emulation(tsdata->input, true);
254 input_sync(tsdata->input);
256 out:
257 return IRQ_HANDLED;
260 static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
261 u8 addr, u8 value)
263 u8 wrbuf[4];
265 switch (tsdata->version) {
266 case M06:
267 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
268 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
269 wrbuf[2] = value;
270 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
271 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
272 wrbuf, 0, NULL);
273 case M09:
274 wrbuf[0] = addr;
275 wrbuf[1] = value;
277 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
278 wrbuf, 0, NULL);
280 default:
281 return -EINVAL;
285 static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
286 u8 addr)
288 u8 wrbuf[2], rdbuf[2];
289 int error;
291 switch (tsdata->version) {
292 case M06:
293 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
294 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
295 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
297 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
298 rdbuf);
299 if (error)
300 return error;
302 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
303 dev_err(&tsdata->client->dev,
304 "crc error: 0x%02x expected, got 0x%02x\n",
305 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
306 rdbuf[1]);
307 return -EIO;
309 break;
311 case M09:
312 wrbuf[0] = addr;
313 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
314 wrbuf, 1, rdbuf);
315 if (error)
316 return error;
317 break;
319 default:
320 return -EINVAL;
323 return rdbuf[0];
326 struct edt_ft5x06_attribute {
327 struct device_attribute dattr;
328 size_t field_offset;
329 u8 limit_low;
330 u8 limit_high;
331 u8 addr_m06;
332 u8 addr_m09;
335 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
336 _limit_low, _limit_high) \
337 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
338 .dattr = __ATTR(_field, _mode, \
339 edt_ft5x06_setting_show, \
340 edt_ft5x06_setting_store), \
341 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
342 .addr_m06 = _addr_m06, \
343 .addr_m09 = _addr_m09, \
344 .limit_low = _limit_low, \
345 .limit_high = _limit_high, \
348 static ssize_t edt_ft5x06_setting_show(struct device *dev,
349 struct device_attribute *dattr,
350 char *buf)
352 struct i2c_client *client = to_i2c_client(dev);
353 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
354 struct edt_ft5x06_attribute *attr =
355 container_of(dattr, struct edt_ft5x06_attribute, dattr);
356 u8 *field = (u8 *)tsdata + attr->field_offset;
357 int val;
358 size_t count = 0;
359 int error = 0;
360 u8 addr;
362 mutex_lock(&tsdata->mutex);
364 if (tsdata->factory_mode) {
365 error = -EIO;
366 goto out;
369 switch (tsdata->version) {
370 case M06:
371 addr = attr->addr_m06;
372 break;
374 case M09:
375 addr = attr->addr_m09;
376 break;
378 default:
379 error = -ENODEV;
380 goto out;
383 if (addr != NO_REGISTER) {
384 val = edt_ft5x06_register_read(tsdata, addr);
385 if (val < 0) {
386 error = val;
387 dev_err(&tsdata->client->dev,
388 "Failed to fetch attribute %s, error %d\n",
389 dattr->attr.name, error);
390 goto out;
392 } else {
393 val = *field;
396 if (val != *field) {
397 dev_warn(&tsdata->client->dev,
398 "%s: read (%d) and stored value (%d) differ\n",
399 dattr->attr.name, val, *field);
400 *field = val;
403 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
404 out:
405 mutex_unlock(&tsdata->mutex);
406 return error ?: count;
409 static ssize_t edt_ft5x06_setting_store(struct device *dev,
410 struct device_attribute *dattr,
411 const char *buf, size_t count)
413 struct i2c_client *client = to_i2c_client(dev);
414 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
415 struct edt_ft5x06_attribute *attr =
416 container_of(dattr, struct edt_ft5x06_attribute, dattr);
417 u8 *field = (u8 *)tsdata + attr->field_offset;
418 unsigned int val;
419 int error;
420 u8 addr;
422 mutex_lock(&tsdata->mutex);
424 if (tsdata->factory_mode) {
425 error = -EIO;
426 goto out;
429 error = kstrtouint(buf, 0, &val);
430 if (error)
431 goto out;
433 if (val < attr->limit_low || val > attr->limit_high) {
434 error = -ERANGE;
435 goto out;
438 switch (tsdata->version) {
439 case M06:
440 addr = attr->addr_m06;
441 break;
443 case M09:
444 addr = attr->addr_m09;
445 break;
447 default:
448 error = -ENODEV;
449 goto out;
452 if (addr != NO_REGISTER) {
453 error = edt_ft5x06_register_write(tsdata, addr, val);
454 if (error) {
455 dev_err(&tsdata->client->dev,
456 "Failed to update attribute %s, error: %d\n",
457 dattr->attr.name, error);
458 goto out;
461 *field = val;
463 out:
464 mutex_unlock(&tsdata->mutex);
465 return error ?: count;
468 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
469 M09_REGISTER_GAIN, 0, 31);
470 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
471 M09_REGISTER_OFFSET, 0, 31);
472 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
473 M09_REGISTER_THRESHOLD, 20, 80);
474 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
475 NO_REGISTER, 3, 14);
477 static struct attribute *edt_ft5x06_attrs[] = {
478 &edt_ft5x06_attr_gain.dattr.attr,
479 &edt_ft5x06_attr_offset.dattr.attr,
480 &edt_ft5x06_attr_threshold.dattr.attr,
481 &edt_ft5x06_attr_report_rate.dattr.attr,
482 NULL
485 static const struct attribute_group edt_ft5x06_attr_group = {
486 .attrs = edt_ft5x06_attrs,
489 #ifdef CONFIG_DEBUG_FS
490 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
492 struct i2c_client *client = tsdata->client;
493 int retries = EDT_SWITCH_MODE_RETRIES;
494 int ret;
495 int error;
497 disable_irq(client->irq);
499 if (!tsdata->raw_buffer) {
500 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
501 sizeof(u16);
502 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
503 if (!tsdata->raw_buffer) {
504 error = -ENOMEM;
505 goto err_out;
509 /* mode register is 0x3c when in the work mode */
510 if (tsdata->version == M09)
511 goto m09_out;
513 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
514 if (error) {
515 dev_err(&client->dev,
516 "failed to switch to factory mode, error %d\n", error);
517 goto err_out;
520 tsdata->factory_mode = true;
521 do {
522 mdelay(EDT_SWITCH_MODE_DELAY);
523 /* mode register is 0x01 when in factory mode */
524 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
525 if (ret == 0x03)
526 break;
527 } while (--retries > 0);
529 if (retries == 0) {
530 dev_err(&client->dev, "not in factory mode after %dms.\n",
531 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
532 error = -EIO;
533 goto err_out;
536 return 0;
538 err_out:
539 kfree(tsdata->raw_buffer);
540 tsdata->raw_buffer = NULL;
541 tsdata->factory_mode = false;
542 enable_irq(client->irq);
544 return error;
546 m09_out:
547 dev_err(&client->dev, "No factory mode support for M09\n");
548 return -EINVAL;
552 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
554 struct i2c_client *client = tsdata->client;
555 int retries = EDT_SWITCH_MODE_RETRIES;
556 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
557 int ret;
558 int error;
560 /* mode register is 0x01 when in the factory mode */
561 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
562 if (error) {
563 dev_err(&client->dev,
564 "failed to switch to work mode, error: %d\n", error);
565 return error;
568 tsdata->factory_mode = false;
570 do {
571 mdelay(EDT_SWITCH_MODE_DELAY);
572 /* mode register is 0x01 when in factory mode */
573 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
574 if (ret == 0x01)
575 break;
576 } while (--retries > 0);
578 if (retries == 0) {
579 dev_err(&client->dev, "not in work mode after %dms.\n",
580 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
581 tsdata->factory_mode = true;
582 return -EIO;
585 kfree(tsdata->raw_buffer);
586 tsdata->raw_buffer = NULL;
588 /* restore parameters */
589 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
590 tsdata->threshold);
591 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
592 tsdata->gain);
593 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
594 tsdata->offset);
595 if (reg_addr->reg_report_rate)
596 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
597 tsdata->report_rate);
599 enable_irq(client->irq);
601 return 0;
604 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
606 struct edt_ft5x06_ts_data *tsdata = data;
608 *mode = tsdata->factory_mode;
610 return 0;
613 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
615 struct edt_ft5x06_ts_data *tsdata = data;
616 int retval = 0;
618 if (mode > 1)
619 return -ERANGE;
621 mutex_lock(&tsdata->mutex);
623 if (mode != tsdata->factory_mode) {
624 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
625 edt_ft5x06_work_mode(tsdata);
628 mutex_unlock(&tsdata->mutex);
630 return retval;
633 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
634 edt_ft5x06_debugfs_mode_set, "%llu\n");
636 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
637 char __user *buf, size_t count, loff_t *off)
639 struct edt_ft5x06_ts_data *tsdata = file->private_data;
640 struct i2c_client *client = tsdata->client;
641 int retries = EDT_RAW_DATA_RETRIES;
642 int val, i, error;
643 size_t read = 0;
644 int colbytes;
645 char wrbuf[3];
646 u8 *rdbuf;
648 if (*off < 0 || *off >= tsdata->raw_bufsize)
649 return 0;
651 mutex_lock(&tsdata->mutex);
653 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
654 error = -EIO;
655 goto out;
658 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
659 if (error) {
660 dev_dbg(&client->dev,
661 "failed to write 0x08 register, error %d\n", error);
662 goto out;
665 do {
666 msleep(EDT_RAW_DATA_DELAY);
667 val = edt_ft5x06_register_read(tsdata, 0x08);
668 if (val < 1)
669 break;
670 } while (--retries > 0);
672 if (val < 0) {
673 error = val;
674 dev_dbg(&client->dev,
675 "failed to read 0x08 register, error %d\n", error);
676 goto out;
679 if (retries == 0) {
680 dev_dbg(&client->dev,
681 "timed out waiting for register to settle\n");
682 error = -ETIMEDOUT;
683 goto out;
686 rdbuf = tsdata->raw_buffer;
687 colbytes = tsdata->num_y * sizeof(u16);
689 wrbuf[0] = 0xf5;
690 wrbuf[1] = 0x0e;
691 for (i = 0; i < tsdata->num_x; i++) {
692 wrbuf[2] = i; /* column index */
693 error = edt_ft5x06_ts_readwrite(tsdata->client,
694 sizeof(wrbuf), wrbuf,
695 colbytes, rdbuf);
696 if (error)
697 goto out;
699 rdbuf += colbytes;
702 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
703 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
704 error = -EFAULT;
705 goto out;
708 *off += read;
709 out:
710 mutex_unlock(&tsdata->mutex);
711 return error ?: read;
714 static const struct file_operations debugfs_raw_data_fops = {
715 .open = simple_open,
716 .read = edt_ft5x06_debugfs_raw_data_read,
719 static void
720 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
721 const char *debugfs_name)
723 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
724 if (!tsdata->debug_dir)
725 return;
727 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
728 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
730 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
731 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
732 debugfs_create_file("raw_data", S_IRUSR,
733 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
736 static void
737 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
739 debugfs_remove_recursive(tsdata->debug_dir);
740 kfree(tsdata->raw_buffer);
743 #else
745 static inline void
746 edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
747 const char *debugfs_name)
751 static inline void
752 edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
756 #endif /* CONFIG_DEBUGFS */
758 static int edt_ft5x06_ts_identify(struct i2c_client *client,
759 struct edt_ft5x06_ts_data *tsdata,
760 char *fw_version)
762 u8 rdbuf[EDT_NAME_LEN];
763 char *p;
764 int error;
765 char *model_name = tsdata->name;
767 /* see what we find if we assume it is a M06 *
768 * if we get less than EDT_NAME_LEN, we don't want
769 * to have garbage in there
771 memset(rdbuf, 0, sizeof(rdbuf));
772 error = edt_ft5x06_ts_readwrite(client, 1, "\xbb",
773 EDT_NAME_LEN - 1, rdbuf);
774 if (error)
775 return error;
777 /* if we find something consistent, stay with that assumption
778 * at least M09 won't send 3 bytes here
780 if (!(strncasecmp(rdbuf + 1, "EP0", 3))) {
781 tsdata->version = M06;
783 /* remove last '$' end marker */
784 rdbuf[EDT_NAME_LEN - 1] = '\0';
785 if (rdbuf[EDT_NAME_LEN - 2] == '$')
786 rdbuf[EDT_NAME_LEN - 2] = '\0';
788 /* look for Model/Version separator */
789 p = strchr(rdbuf, '*');
790 if (p)
791 *p++ = '\0';
792 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
793 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
794 } else {
795 /* since there are only two versions around (M06, M09) */
796 tsdata->version = M09;
798 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
799 2, rdbuf);
800 if (error)
801 return error;
803 strlcpy(fw_version, rdbuf, 2);
805 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
806 1, rdbuf);
807 if (error)
808 return error;
810 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
811 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
814 return 0;
817 static void edt_ft5x06_ts_get_defaults(struct device *dev,
818 struct edt_ft5x06_ts_data *tsdata)
820 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
821 u32 val;
822 int error;
824 error = device_property_read_u32(dev, "threshold", &val);
825 if (!error) {
826 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
827 tsdata->threshold = val;
830 error = device_property_read_u32(dev, "gain", &val);
831 if (!error) {
832 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
833 tsdata->gain = val;
836 error = device_property_read_u32(dev, "offset", &val);
837 if (!error) {
838 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
839 tsdata->offset = val;
843 static void
844 edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
846 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
848 tsdata->threshold = edt_ft5x06_register_read(tsdata,
849 reg_addr->reg_threshold);
850 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
851 tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
852 if (reg_addr->reg_report_rate != NO_REGISTER)
853 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
854 reg_addr->reg_report_rate);
855 tsdata->num_x = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_x);
856 tsdata->num_y = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_y);
859 static void
860 edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
862 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
864 switch (tsdata->version) {
865 case M06:
866 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
867 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
868 reg_addr->reg_gain = WORK_REGISTER_GAIN;
869 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
870 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
871 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
872 break;
874 case M09:
875 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
876 reg_addr->reg_gain = M09_REGISTER_GAIN;
877 reg_addr->reg_offset = M09_REGISTER_OFFSET;
878 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
879 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
880 break;
884 static int edt_ft5x06_ts_probe(struct i2c_client *client,
885 const struct i2c_device_id *id)
887 const struct edt_i2c_chip_data *chip_data;
888 struct edt_ft5x06_ts_data *tsdata;
889 struct input_dev *input;
890 unsigned long irq_flags;
891 int error;
892 char fw_version[EDT_NAME_LEN];
894 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
896 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
897 if (!tsdata) {
898 dev_err(&client->dev, "failed to allocate driver data.\n");
899 return -ENOMEM;
902 chip_data = of_device_get_match_data(&client->dev);
903 if (!chip_data)
904 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
905 if (!chip_data || !chip_data->max_support_points) {
906 dev_err(&client->dev, "invalid or missing chip data\n");
907 return -EINVAL;
910 tsdata->max_support_points = chip_data->max_support_points;
912 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
913 "reset", GPIOD_OUT_HIGH);
914 if (IS_ERR(tsdata->reset_gpio)) {
915 error = PTR_ERR(tsdata->reset_gpio);
916 dev_err(&client->dev,
917 "Failed to request GPIO reset pin, error %d\n", error);
918 return error;
921 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
922 "wake", GPIOD_OUT_LOW);
923 if (IS_ERR(tsdata->wake_gpio)) {
924 error = PTR_ERR(tsdata->wake_gpio);
925 dev_err(&client->dev,
926 "Failed to request GPIO wake pin, error %d\n", error);
927 return error;
930 if (tsdata->wake_gpio) {
931 usleep_range(5000, 6000);
932 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
935 if (tsdata->reset_gpio) {
936 usleep_range(5000, 6000);
937 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
938 msleep(300);
941 input = devm_input_allocate_device(&client->dev);
942 if (!input) {
943 dev_err(&client->dev, "failed to allocate input device.\n");
944 return -ENOMEM;
947 mutex_init(&tsdata->mutex);
948 tsdata->client = client;
949 tsdata->input = input;
950 tsdata->factory_mode = false;
952 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
953 if (error) {
954 dev_err(&client->dev, "touchscreen probe failed\n");
955 return error;
958 edt_ft5x06_ts_set_regs(tsdata);
959 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
960 edt_ft5x06_ts_get_parameters(tsdata);
962 dev_dbg(&client->dev,
963 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
964 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
966 input->name = tsdata->name;
967 input->id.bustype = BUS_I2C;
968 input->dev.parent = &client->dev;
970 input_set_abs_params(input, ABS_MT_POSITION_X,
971 0, tsdata->num_x * 64 - 1, 0, 0);
972 input_set_abs_params(input, ABS_MT_POSITION_Y,
973 0, tsdata->num_y * 64 - 1, 0, 0);
975 touchscreen_parse_properties(input, true);
977 error = input_mt_init_slots(input, tsdata->max_support_points,
978 INPUT_MT_DIRECT);
979 if (error) {
980 dev_err(&client->dev, "Unable to init MT slots.\n");
981 return error;
984 input_set_drvdata(input, tsdata);
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 i2c_device_id edt_ft5x06_ts_id[] = {
1066 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1067 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1068 { /* sentinel */ }
1070 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1072 #ifdef CONFIG_OF
1073 static const struct of_device_id edt_ft5x06_of_match[] = {
1074 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1075 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1076 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1077 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1078 { /* sentinel */ }
1080 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1081 #endif
1083 static struct i2c_driver edt_ft5x06_ts_driver = {
1084 .driver = {
1085 .name = "edt_ft5x06",
1086 .of_match_table = of_match_ptr(edt_ft5x06_of_match),
1087 .pm = &edt_ft5x06_ts_pm_ops,
1089 .id_table = edt_ft5x06_ts_id,
1090 .probe = edt_ft5x06_ts_probe,
1091 .remove = edt_ft5x06_ts_remove,
1094 module_i2c_driver(edt_ft5x06_ts_driver);
1096 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1097 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1098 MODULE_LICENSE("GPL");