drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / input / touchscreen / edt-ft5x06.c
blobfda49b2fe088522b8df8ec9353d928522171a38f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
4 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
5 * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
6 * Dario Binacchi <dario.binacchi@amarulasolutions.com> (regmap support)
7 */
9 /*
10 * This is a driver for the EDT "Polytouch" family of touch controllers
11 * based on the FocalTech FT5x06 line of chips.
13 * Development of this driver has been sponsored by Glyn:
14 * http://www.glyn.com/Products/Displays
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/interrupt.h>
22 #include <linux/input.h>
23 #include <linux/input/mt.h>
24 #include <linux/input/touchscreen.h>
25 #include <linux/irq.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/property.h>
29 #include <linux/ratelimit.h>
30 #include <linux/regmap.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/slab.h>
33 #include <linux/uaccess.h>
35 #include <linux/unaligned.h>
37 #define WORK_REGISTER_THRESHOLD 0x00
38 #define WORK_REGISTER_REPORT_RATE 0x08
39 #define WORK_REGISTER_GAIN 0x30
40 #define WORK_REGISTER_OFFSET 0x31
41 #define WORK_REGISTER_NUM_X 0x33
42 #define WORK_REGISTER_NUM_Y 0x34
44 #define PMOD_REGISTER_ACTIVE 0x00
45 #define PMOD_REGISTER_HIBERNATE 0x03
47 #define M09_REGISTER_THRESHOLD 0x80
48 #define M09_REGISTER_GAIN 0x92
49 #define M09_REGISTER_OFFSET 0x93
50 #define M09_REGISTER_NUM_X 0x94
51 #define M09_REGISTER_NUM_Y 0x95
53 #define M12_REGISTER_REPORT_RATE 0x88
55 #define EV_REGISTER_THRESHOLD 0x40
56 #define EV_REGISTER_GAIN 0x41
57 #define EV_REGISTER_OFFSET_Y 0x45
58 #define EV_REGISTER_OFFSET_X 0x46
60 #define NO_REGISTER 0xff
62 #define WORK_REGISTER_OPMODE 0x3c
63 #define FACTORY_REGISTER_OPMODE 0x01
64 #define PMOD_REGISTER_OPMODE 0xa5
66 #define TOUCH_EVENT_DOWN 0x00
67 #define TOUCH_EVENT_UP 0x01
68 #define TOUCH_EVENT_ON 0x02
69 #define TOUCH_EVENT_RESERVED 0x03
71 #define EDT_NAME_LEN 23
72 #define EDT_SWITCH_MODE_RETRIES 10
73 #define EDT_SWITCH_MODE_DELAY 5 /* msec */
74 #define EDT_RAW_DATA_RETRIES 100
75 #define EDT_RAW_DATA_DELAY 1000 /* usec */
77 #define EDT_DEFAULT_NUM_X 1024
78 #define EDT_DEFAULT_NUM_Y 1024
80 #define M06_REG_CMD(factory) ((factory) ? 0xf3 : 0xfc)
81 #define M06_REG_ADDR(factory, addr) ((factory) ? (addr) & 0x7f : (addr) & 0x3f)
83 enum edt_pmode {
84 EDT_PMODE_NOT_SUPPORTED,
85 EDT_PMODE_HIBERNATE,
86 EDT_PMODE_POWEROFF,
89 enum edt_ver {
90 EDT_M06,
91 EDT_M09,
92 EDT_M12,
93 EV_FT,
94 GENERIC_FT,
97 struct edt_reg_addr {
98 int reg_threshold;
99 int reg_report_rate;
100 int reg_gain;
101 int reg_offset;
102 int reg_offset_x;
103 int reg_offset_y;
104 int reg_num_x;
105 int reg_num_y;
108 struct edt_ft5x06_ts_data {
109 struct i2c_client *client;
110 struct input_dev *input;
111 struct touchscreen_properties prop;
112 u16 num_x;
113 u16 num_y;
114 struct regulator *vcc;
115 struct regulator *iovcc;
117 struct gpio_desc *reset_gpio;
118 struct gpio_desc *wake_gpio;
120 struct regmap *regmap;
122 #if defined(CONFIG_DEBUG_FS)
123 struct dentry *debug_dir;
124 u8 *raw_buffer;
125 size_t raw_bufsize;
126 #endif
128 struct mutex mutex;
129 bool factory_mode;
130 enum edt_pmode suspend_mode;
131 int threshold;
132 int gain;
133 int offset;
134 int offset_x;
135 int offset_y;
136 int report_rate;
137 int max_support_points;
138 int point_len;
139 u8 tdata_cmd;
140 int tdata_len;
141 int tdata_offset;
143 char name[EDT_NAME_LEN];
144 char fw_version[EDT_NAME_LEN];
146 struct edt_reg_addr reg_addr;
147 enum edt_ver version;
148 unsigned int crc_errors;
149 unsigned int header_errors;
152 struct edt_i2c_chip_data {
153 int max_support_points;
156 static const struct regmap_config edt_ft5x06_i2c_regmap_config = {
157 .reg_bits = 8,
158 .val_bits = 8,
161 static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
162 u8 *buf, int buflen)
164 int i;
165 u8 crc = 0;
167 for (i = 0; i < buflen - 1; i++)
168 crc ^= buf[i];
170 if (crc != buf[buflen - 1]) {
171 tsdata->crc_errors++;
172 dev_err_ratelimited(&tsdata->client->dev,
173 "crc error: 0x%02x expected, got 0x%02x\n",
174 crc, buf[buflen - 1]);
175 return false;
178 return true;
181 static int edt_M06_i2c_read(void *context, const void *reg_buf, size_t reg_size,
182 void *val_buf, size_t val_size)
184 struct device *dev = context;
185 struct i2c_client *i2c = to_i2c_client(dev);
186 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(i2c);
187 struct i2c_msg xfer[2];
188 bool reg_read = false;
189 u8 addr;
190 u8 wlen;
191 u8 wbuf[4], rbuf[3];
192 int ret;
194 addr = *((u8 *)reg_buf);
195 wbuf[0] = addr;
196 switch (addr) {
197 case 0xf5:
198 wlen = 3;
199 wbuf[0] = 0xf5;
200 wbuf[1] = 0xe;
201 wbuf[2] = *((u8 *)val_buf);
202 break;
203 case 0xf9:
204 wlen = 1;
205 break;
206 default:
207 wlen = 2;
208 reg_read = true;
209 wbuf[0] = M06_REG_CMD(tsdata->factory_mode);
210 wbuf[1] = M06_REG_ADDR(tsdata->factory_mode, addr);
211 wbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
214 xfer[0].addr = i2c->addr;
215 xfer[0].flags = 0;
216 xfer[0].len = wlen;
217 xfer[0].buf = wbuf;
219 xfer[1].addr = i2c->addr;
220 xfer[1].flags = I2C_M_RD;
221 xfer[1].len = reg_read ? 2 : val_size;
222 xfer[1].buf = reg_read ? rbuf : val_buf;
224 ret = i2c_transfer(i2c->adapter, xfer, 2);
225 if (ret != 2) {
226 if (ret < 0)
227 return ret;
229 return -EIO;
232 if (addr == 0xf9) {
233 u8 *buf = (u8 *)val_buf;
235 if (buf[0] != 0xaa || buf[1] != 0xaa ||
236 buf[2] != val_size) {
237 tsdata->header_errors++;
238 dev_err_ratelimited(dev,
239 "Unexpected header: %02x%02x%02x\n",
240 buf[0], buf[1], buf[2]);
241 return -EIO;
244 if (!edt_ft5x06_ts_check_crc(tsdata, val_buf, val_size))
245 return -EIO;
246 } else if (reg_read) {
247 wbuf[2] = rbuf[0];
248 wbuf[3] = rbuf[1];
249 if (!edt_ft5x06_ts_check_crc(tsdata, wbuf, 4))
250 return -EIO;
252 *((u8 *)val_buf) = rbuf[0];
255 return 0;
258 static int edt_M06_i2c_write(void *context, const void *data, size_t count)
260 struct device *dev = context;
261 struct i2c_client *i2c = to_i2c_client(dev);
262 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(i2c);
263 u8 addr, val;
264 u8 wbuf[4];
265 struct i2c_msg xfer;
266 int ret;
268 addr = *((u8 *)data);
269 val = *((u8 *)data + 1);
271 wbuf[0] = M06_REG_CMD(tsdata->factory_mode);
272 wbuf[1] = M06_REG_ADDR(tsdata->factory_mode, addr);
273 wbuf[2] = val;
274 wbuf[3] = wbuf[0] ^ wbuf[1] ^ wbuf[2];
276 xfer.addr = i2c->addr;
277 xfer.flags = 0;
278 xfer.len = 4;
279 xfer.buf = wbuf;
281 ret = i2c_transfer(i2c->adapter, &xfer, 1);
282 if (ret != 1) {
283 if (ret < 0)
284 return ret;
286 return -EIO;
289 return 0;
292 static const struct regmap_config edt_M06_i2c_regmap_config = {
293 .reg_bits = 8,
294 .val_bits = 8,
295 .read = edt_M06_i2c_read,
296 .write = edt_M06_i2c_write,
299 static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
301 struct edt_ft5x06_ts_data *tsdata = dev_id;
302 struct device *dev = &tsdata->client->dev;
303 u8 rdbuf[63];
304 int i, type, x, y, id;
305 int error;
307 memset(rdbuf, 0, sizeof(rdbuf));
308 error = regmap_bulk_read(tsdata->regmap, tsdata->tdata_cmd, rdbuf,
309 tsdata->tdata_len);
310 if (error) {
311 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
312 error);
313 goto out;
316 for (i = 0; i < tsdata->max_support_points; i++) {
317 u8 *buf = &rdbuf[i * tsdata->point_len + tsdata->tdata_offset];
319 type = buf[0] >> 6;
320 /* ignore Reserved events */
321 if (type == TOUCH_EVENT_RESERVED)
322 continue;
324 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
325 if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
326 continue;
328 x = get_unaligned_be16(buf) & 0x0fff;
329 y = get_unaligned_be16(buf + 2) & 0x0fff;
330 /* The FT5x26 send the y coordinate first */
331 if (tsdata->version == EV_FT)
332 swap(x, y);
334 id = (buf[2] >> 4) & 0x0f;
336 input_mt_slot(tsdata->input, id);
337 if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
338 type != TOUCH_EVENT_UP))
339 touchscreen_report_pos(tsdata->input, &tsdata->prop,
340 x, y, true);
343 input_mt_report_pointer_emulation(tsdata->input, true);
344 input_sync(tsdata->input);
346 out:
347 return IRQ_HANDLED;
350 struct edt_ft5x06_attribute {
351 struct device_attribute dattr;
352 size_t field_offset;
353 u8 limit_low;
354 u8 limit_high;
355 u8 addr_m06;
356 u8 addr_m09;
357 u8 addr_ev;
360 #define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
361 _limit_low, _limit_high) \
362 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
363 .dattr = __ATTR(_field, _mode, \
364 edt_ft5x06_setting_show, \
365 edt_ft5x06_setting_store), \
366 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
367 .addr_m06 = _addr_m06, \
368 .addr_m09 = _addr_m09, \
369 .addr_ev = _addr_ev, \
370 .limit_low = _limit_low, \
371 .limit_high = _limit_high, \
374 static ssize_t edt_ft5x06_setting_show(struct device *dev,
375 struct device_attribute *dattr,
376 char *buf)
378 struct i2c_client *client = to_i2c_client(dev);
379 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
380 struct edt_ft5x06_attribute *attr =
381 container_of(dattr, struct edt_ft5x06_attribute, dattr);
382 u8 *field = (u8 *)tsdata + attr->field_offset;
383 unsigned int val;
384 size_t count = 0;
385 int error = 0;
386 u8 addr;
388 mutex_lock(&tsdata->mutex);
390 if (tsdata->factory_mode) {
391 error = -EIO;
392 goto out;
395 switch (tsdata->version) {
396 case EDT_M06:
397 addr = attr->addr_m06;
398 break;
400 case EDT_M09:
401 case EDT_M12:
402 case GENERIC_FT:
403 addr = attr->addr_m09;
404 break;
406 case EV_FT:
407 addr = attr->addr_ev;
408 break;
410 default:
411 error = -ENODEV;
412 goto out;
415 if (addr != NO_REGISTER) {
416 error = regmap_read(tsdata->regmap, addr, &val);
417 if (error) {
418 dev_err(&tsdata->client->dev,
419 "Failed to fetch attribute %s, error %d\n",
420 dattr->attr.name, error);
421 goto out;
423 } else {
424 val = *field;
427 if (val != *field) {
428 dev_warn(&tsdata->client->dev,
429 "%s: read (%d) and stored value (%d) differ\n",
430 dattr->attr.name, val, *field);
431 *field = val;
434 count = sysfs_emit(buf, "%d\n", val);
435 out:
436 mutex_unlock(&tsdata->mutex);
437 return error ?: count;
440 static ssize_t edt_ft5x06_setting_store(struct device *dev,
441 struct device_attribute *dattr,
442 const char *buf, size_t count)
444 struct i2c_client *client = to_i2c_client(dev);
445 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
446 struct edt_ft5x06_attribute *attr =
447 container_of(dattr, struct edt_ft5x06_attribute, dattr);
448 u8 *field = (u8 *)tsdata + attr->field_offset;
449 unsigned int val;
450 int error;
451 u8 addr;
453 mutex_lock(&tsdata->mutex);
455 if (tsdata->factory_mode) {
456 error = -EIO;
457 goto out;
460 error = kstrtouint(buf, 0, &val);
461 if (error)
462 goto out;
464 if (val < attr->limit_low || val > attr->limit_high) {
465 error = -ERANGE;
466 goto out;
469 switch (tsdata->version) {
470 case EDT_M06:
471 addr = attr->addr_m06;
472 break;
474 case EDT_M09:
475 case EDT_M12:
476 case GENERIC_FT:
477 addr = attr->addr_m09;
478 break;
480 case EV_FT:
481 addr = attr->addr_ev;
482 break;
484 default:
485 error = -ENODEV;
486 goto out;
489 if (addr != NO_REGISTER) {
490 error = regmap_write(tsdata->regmap, addr, val);
491 if (error) {
492 dev_err(&tsdata->client->dev,
493 "Failed to update attribute %s, error: %d\n",
494 dattr->attr.name, error);
495 goto out;
498 *field = val;
500 out:
501 mutex_unlock(&tsdata->mutex);
502 return error ?: count;
505 /* m06, m09: range 0-31, m12: range 0-5 */
506 static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
507 M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31);
508 /* m06, m09: range 0-31, m12: range 0-16 */
509 static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
510 M09_REGISTER_OFFSET, NO_REGISTER, 0, 31);
511 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
512 static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
513 EV_REGISTER_OFFSET_X, 0, 80);
514 /* m06, m09, m12: no supported, ev_ft: range 0-80 */
515 static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
516 EV_REGISTER_OFFSET_Y, 0, 80);
517 /* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
518 static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
519 M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255);
520 /* m06: range 3 to 14, m12: range 1 to 255 */
521 static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
522 M12_REGISTER_REPORT_RATE, NO_REGISTER, 0, 255);
524 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
525 char *buf)
527 struct i2c_client *client = to_i2c_client(dev);
528 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
530 return sysfs_emit(buf, "%s\n", tsdata->name);
533 static DEVICE_ATTR_RO(model);
535 static ssize_t fw_version_show(struct device *dev,
536 struct device_attribute *attr, char *buf)
538 struct i2c_client *client = to_i2c_client(dev);
539 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
541 return sysfs_emit(buf, "%s\n", tsdata->fw_version);
544 static DEVICE_ATTR_RO(fw_version);
546 /* m06 only */
547 static ssize_t header_errors_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
550 struct i2c_client *client = to_i2c_client(dev);
551 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
553 return sysfs_emit(buf, "%d\n", tsdata->header_errors);
556 static DEVICE_ATTR_RO(header_errors);
558 /* m06 only */
559 static ssize_t crc_errors_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
562 struct i2c_client *client = to_i2c_client(dev);
563 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
565 return sysfs_emit(buf, "%d\n", tsdata->crc_errors);
568 static DEVICE_ATTR_RO(crc_errors);
570 static struct attribute *edt_ft5x06_attrs[] = {
571 &edt_ft5x06_attr_gain.dattr.attr,
572 &edt_ft5x06_attr_offset.dattr.attr,
573 &edt_ft5x06_attr_offset_x.dattr.attr,
574 &edt_ft5x06_attr_offset_y.dattr.attr,
575 &edt_ft5x06_attr_threshold.dattr.attr,
576 &edt_ft5x06_attr_report_rate.dattr.attr,
577 &dev_attr_model.attr,
578 &dev_attr_fw_version.attr,
579 &dev_attr_header_errors.attr,
580 &dev_attr_crc_errors.attr,
581 NULL
583 ATTRIBUTE_GROUPS(edt_ft5x06);
585 static void edt_ft5x06_restore_reg_parameters(struct edt_ft5x06_ts_data *tsdata)
587 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
588 struct regmap *regmap = tsdata->regmap;
590 regmap_write(regmap, reg_addr->reg_threshold, tsdata->threshold);
591 regmap_write(regmap, reg_addr->reg_gain, tsdata->gain);
592 if (reg_addr->reg_offset != NO_REGISTER)
593 regmap_write(regmap, reg_addr->reg_offset, tsdata->offset);
594 if (reg_addr->reg_offset_x != NO_REGISTER)
595 regmap_write(regmap, reg_addr->reg_offset_x, tsdata->offset_x);
596 if (reg_addr->reg_offset_y != NO_REGISTER)
597 regmap_write(regmap, reg_addr->reg_offset_y, tsdata->offset_y);
598 if (reg_addr->reg_report_rate != NO_REGISTER)
599 regmap_write(regmap, reg_addr->reg_report_rate,
600 tsdata->report_rate);
603 #ifdef CONFIG_DEBUG_FS
604 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
606 struct i2c_client *client = tsdata->client;
607 int retries = EDT_SWITCH_MODE_RETRIES;
608 unsigned int val;
609 int error;
611 if (tsdata->version != EDT_M06) {
612 dev_err(&client->dev,
613 "No factory mode support for non-M06 devices\n");
614 return -EINVAL;
617 disable_irq(client->irq);
619 if (!tsdata->raw_buffer) {
620 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
621 sizeof(u16);
622 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
623 if (!tsdata->raw_buffer) {
624 error = -ENOMEM;
625 goto err_out;
629 /* mode register is 0x3c when in the work mode */
630 error = regmap_write(tsdata->regmap, WORK_REGISTER_OPMODE, 0x03);
631 if (error) {
632 dev_err(&client->dev,
633 "failed to switch to factory mode, error %d\n", error);
634 goto err_out;
637 tsdata->factory_mode = true;
638 do {
639 mdelay(EDT_SWITCH_MODE_DELAY);
640 /* mode register is 0x01 when in factory mode */
641 error = regmap_read(tsdata->regmap, FACTORY_REGISTER_OPMODE,
642 &val);
643 if (!error && val == 0x03)
644 break;
645 } while (--retries > 0);
647 if (retries == 0) {
648 dev_err(&client->dev, "not in factory mode after %dms.\n",
649 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
650 error = -EIO;
651 goto err_out;
654 return 0;
656 err_out:
657 kfree(tsdata->raw_buffer);
658 tsdata->raw_buffer = NULL;
659 tsdata->factory_mode = false;
660 enable_irq(client->irq);
662 return error;
665 static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
667 struct i2c_client *client = tsdata->client;
668 int retries = EDT_SWITCH_MODE_RETRIES;
669 unsigned int val;
670 int error;
672 /* mode register is 0x01 when in the factory mode */
673 error = regmap_write(tsdata->regmap, FACTORY_REGISTER_OPMODE, 0x1);
674 if (error) {
675 dev_err(&client->dev,
676 "failed to switch to work mode, error: %d\n", error);
677 return error;
680 tsdata->factory_mode = false;
682 do {
683 mdelay(EDT_SWITCH_MODE_DELAY);
684 /* mode register is 0x01 when in factory mode */
685 error = regmap_read(tsdata->regmap, WORK_REGISTER_OPMODE, &val);
686 if (!error && val == 0x01)
687 break;
688 } while (--retries > 0);
690 if (retries == 0) {
691 dev_err(&client->dev, "not in work mode after %dms.\n",
692 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
693 tsdata->factory_mode = true;
694 return -EIO;
697 kfree(tsdata->raw_buffer);
698 tsdata->raw_buffer = NULL;
700 edt_ft5x06_restore_reg_parameters(tsdata);
701 enable_irq(client->irq);
703 return 0;
706 static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
708 struct edt_ft5x06_ts_data *tsdata = data;
710 *mode = tsdata->factory_mode;
712 return 0;
715 static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
717 struct edt_ft5x06_ts_data *tsdata = data;
718 int retval = 0;
720 if (mode > 1)
721 return -ERANGE;
723 mutex_lock(&tsdata->mutex);
725 if (mode != tsdata->factory_mode) {
726 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
727 edt_ft5x06_work_mode(tsdata);
730 mutex_unlock(&tsdata->mutex);
732 return retval;
735 DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
736 edt_ft5x06_debugfs_mode_set, "%llu\n");
738 static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
739 char __user *buf, size_t count,
740 loff_t *off)
742 struct edt_ft5x06_ts_data *tsdata = file->private_data;
743 struct i2c_client *client = tsdata->client;
744 int retries = EDT_RAW_DATA_RETRIES;
745 unsigned int val;
746 int i, error;
747 size_t read = 0;
748 int colbytes;
749 u8 *rdbuf;
751 if (*off < 0 || *off >= tsdata->raw_bufsize)
752 return 0;
754 mutex_lock(&tsdata->mutex);
756 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
757 error = -EIO;
758 goto out;
761 error = regmap_write(tsdata->regmap, 0x08, 0x01);
762 if (error) {
763 dev_err(&client->dev,
764 "failed to write 0x08 register, error %d\n", error);
765 goto out;
768 do {
769 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
770 error = regmap_read(tsdata->regmap, 0x08, &val);
771 if (error) {
772 dev_err(&client->dev,
773 "failed to read 0x08 register, error %d\n",
774 error);
775 goto out;
778 if (val == 1)
779 break;
780 } while (--retries > 0);
782 if (retries == 0) {
783 dev_err(&client->dev,
784 "timed out waiting for register to settle\n");
785 error = -ETIMEDOUT;
786 goto out;
789 rdbuf = tsdata->raw_buffer;
790 colbytes = tsdata->num_y * sizeof(u16);
792 for (i = 0; i < tsdata->num_x; i++) {
793 rdbuf[0] = i; /* column index */
794 error = regmap_bulk_read(tsdata->regmap, 0xf5, rdbuf, colbytes);
795 if (error)
796 goto out;
798 rdbuf += colbytes;
801 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
802 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
803 error = -EFAULT;
804 goto out;
807 *off += read;
808 out:
809 mutex_unlock(&tsdata->mutex);
810 return error ?: read;
813 static const struct file_operations debugfs_raw_data_fops = {
814 .open = simple_open,
815 .read = edt_ft5x06_debugfs_raw_data_read,
818 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
819 const char *debugfs_name)
821 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
823 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
824 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
826 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
827 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
828 debugfs_create_file("raw_data", S_IRUSR,
829 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
832 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
834 debugfs_remove_recursive(tsdata->debug_dir);
835 kfree(tsdata->raw_buffer);
838 #else
840 static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
842 return -ENOSYS;
845 static void edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
846 const char *debugfs_name)
850 static void edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
854 #endif /* CONFIG_DEBUGFS */
856 static int edt_ft5x06_ts_identify(struct i2c_client *client,
857 struct edt_ft5x06_ts_data *tsdata)
859 u8 rdbuf[EDT_NAME_LEN];
860 char *p;
861 int error;
862 char *model_name = tsdata->name;
863 char *fw_version = tsdata->fw_version;
865 /* see what we find if we assume it is a M06 *
866 * if we get less than EDT_NAME_LEN, we don't want
867 * to have garbage in there
869 memset(rdbuf, 0, sizeof(rdbuf));
870 error = regmap_bulk_read(tsdata->regmap, 0xBB, rdbuf, EDT_NAME_LEN - 1);
871 if (error)
872 return error;
874 /* Probe content for something consistent.
875 * M06 starts with a response byte, M12 gives the data directly.
876 * M09/Generic does not provide model number information.
878 if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
879 tsdata->version = EDT_M06;
881 /* remove last '$' end marker */
882 rdbuf[EDT_NAME_LEN - 1] = '\0';
883 if (rdbuf[EDT_NAME_LEN - 2] == '$')
884 rdbuf[EDT_NAME_LEN - 2] = '\0';
886 /* look for Model/Version separator */
887 p = strchr(rdbuf, '*');
888 if (p)
889 *p++ = '\0';
890 strscpy(model_name, rdbuf + 1, EDT_NAME_LEN);
891 strscpy(fw_version, p ? p : "", EDT_NAME_LEN);
893 regmap_exit(tsdata->regmap);
894 tsdata->regmap = regmap_init_i2c(client,
895 &edt_M06_i2c_regmap_config);
896 if (IS_ERR(tsdata->regmap)) {
897 dev_err(&client->dev, "regmap allocation failed\n");
898 return PTR_ERR(tsdata->regmap);
900 } else if (!strncasecmp(rdbuf, "EP0", 3)) {
901 tsdata->version = EDT_M12;
903 /* remove last '$' end marker */
904 rdbuf[EDT_NAME_LEN - 2] = '\0';
905 if (rdbuf[EDT_NAME_LEN - 3] == '$')
906 rdbuf[EDT_NAME_LEN - 3] = '\0';
908 /* look for Model/Version separator */
909 p = strchr(rdbuf, '*');
910 if (p)
911 *p++ = '\0';
912 strscpy(model_name, rdbuf, EDT_NAME_LEN);
913 strscpy(fw_version, p ? p : "", EDT_NAME_LEN);
914 } else {
915 /* If it is not an EDT M06/M12 touchscreen, then the model
916 * detection is a bit hairy. The different ft5x06
917 * firmwares around don't reliably implement the
918 * identification registers. Well, we'll take a shot.
920 * The main difference between generic focaltec based
921 * touches and EDT M09 is that we know how to retrieve
922 * the max coordinates for the latter.
924 tsdata->version = GENERIC_FT;
926 error = regmap_bulk_read(tsdata->regmap, 0xA6, rdbuf, 2);
927 if (error)
928 return error;
930 strscpy(fw_version, rdbuf, 2);
932 error = regmap_bulk_read(tsdata->regmap, 0xA8, rdbuf, 1);
933 if (error)
934 return error;
936 /* This "model identification" is not exact. Unfortunately
937 * not all firmwares for the ft5x06 put useful values in
938 * the identification registers.
940 switch (rdbuf[0]) {
941 case 0x11: /* EDT EP0110M09 */
942 case 0x35: /* EDT EP0350M09 */
943 case 0x43: /* EDT EP0430M09 */
944 case 0x50: /* EDT EP0500M09 */
945 case 0x57: /* EDT EP0570M09 */
946 case 0x70: /* EDT EP0700M09 */
947 tsdata->version = EDT_M09;
948 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
949 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
950 break;
951 case 0xa1: /* EDT EP1010ML00 */
952 tsdata->version = EDT_M09;
953 snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
954 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
955 break;
956 case 0x5a: /* Solomon Goldentek Display */
957 snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
958 break;
959 case 0x59: /* Evervision Display with FT5xx6 TS */
960 tsdata->version = EV_FT;
961 error = regmap_bulk_read(tsdata->regmap, 0x53, rdbuf, 1);
962 if (error)
963 return error;
964 strscpy(fw_version, rdbuf, 1);
965 snprintf(model_name, EDT_NAME_LEN,
966 "EVERVISION-FT5726NEi");
967 break;
968 default:
969 snprintf(model_name, EDT_NAME_LEN,
970 "generic ft5x06 (%02x)",
971 rdbuf[0]);
972 break;
976 return 0;
979 static void edt_ft5x06_ts_get_defaults(struct device *dev,
980 struct edt_ft5x06_ts_data *tsdata)
982 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
983 struct regmap *regmap = tsdata->regmap;
984 u32 val;
985 int error;
987 error = device_property_read_u32(dev, "threshold", &val);
988 if (!error) {
989 regmap_write(regmap, reg_addr->reg_threshold, val);
990 tsdata->threshold = val;
993 error = device_property_read_u32(dev, "gain", &val);
994 if (!error) {
995 regmap_write(regmap, reg_addr->reg_gain, val);
996 tsdata->gain = val;
999 error = device_property_read_u32(dev, "offset", &val);
1000 if (!error) {
1001 if (reg_addr->reg_offset != NO_REGISTER)
1002 regmap_write(regmap, reg_addr->reg_offset, val);
1003 tsdata->offset = val;
1006 error = device_property_read_u32(dev, "offset-x", &val);
1007 if (!error) {
1008 if (reg_addr->reg_offset_x != NO_REGISTER)
1009 regmap_write(regmap, reg_addr->reg_offset_x, val);
1010 tsdata->offset_x = val;
1013 error = device_property_read_u32(dev, "offset-y", &val);
1014 if (!error) {
1015 if (reg_addr->reg_offset_y != NO_REGISTER)
1016 regmap_write(regmap, reg_addr->reg_offset_y, val);
1017 tsdata->offset_y = val;
1021 static void edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
1023 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
1024 struct regmap *regmap = tsdata->regmap;
1025 unsigned int val;
1027 regmap_read(regmap, reg_addr->reg_threshold, &tsdata->threshold);
1028 regmap_read(regmap, reg_addr->reg_gain, &tsdata->gain);
1029 if (reg_addr->reg_offset != NO_REGISTER)
1030 regmap_read(regmap, reg_addr->reg_offset, &tsdata->offset);
1031 if (reg_addr->reg_offset_x != NO_REGISTER)
1032 regmap_read(regmap, reg_addr->reg_offset_x, &tsdata->offset_x);
1033 if (reg_addr->reg_offset_y != NO_REGISTER)
1034 regmap_read(regmap, reg_addr->reg_offset_y, &tsdata->offset_y);
1035 if (reg_addr->reg_report_rate != NO_REGISTER)
1036 regmap_read(regmap, reg_addr->reg_report_rate,
1037 &tsdata->report_rate);
1038 tsdata->num_x = EDT_DEFAULT_NUM_X;
1039 if (reg_addr->reg_num_x != NO_REGISTER) {
1040 if (!regmap_read(regmap, reg_addr->reg_num_x, &val))
1041 tsdata->num_x = val;
1043 tsdata->num_y = EDT_DEFAULT_NUM_Y;
1044 if (reg_addr->reg_num_y != NO_REGISTER) {
1045 if (!regmap_read(regmap, reg_addr->reg_num_y, &val))
1046 tsdata->num_y = val;
1050 static void edt_ft5x06_ts_set_tdata_parameters(struct edt_ft5x06_ts_data *tsdata)
1052 int crclen;
1054 if (tsdata->version == EDT_M06) {
1055 tsdata->tdata_cmd = 0xf9;
1056 tsdata->tdata_offset = 5;
1057 tsdata->point_len = 4;
1058 crclen = 1;
1059 } else {
1060 tsdata->tdata_cmd = 0x0;
1061 tsdata->tdata_offset = 3;
1062 tsdata->point_len = 6;
1063 crclen = 0;
1066 tsdata->tdata_len = tsdata->point_len * tsdata->max_support_points +
1067 tsdata->tdata_offset + crclen;
1070 static void edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
1072 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
1074 switch (tsdata->version) {
1075 case EDT_M06:
1076 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
1077 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
1078 reg_addr->reg_gain = WORK_REGISTER_GAIN;
1079 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
1080 reg_addr->reg_offset_x = NO_REGISTER;
1081 reg_addr->reg_offset_y = NO_REGISTER;
1082 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
1083 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
1084 break;
1086 case EDT_M09:
1087 case EDT_M12:
1088 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1089 reg_addr->reg_report_rate = tsdata->version == EDT_M12 ?
1090 M12_REGISTER_REPORT_RATE : NO_REGISTER;
1091 reg_addr->reg_gain = M09_REGISTER_GAIN;
1092 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1093 reg_addr->reg_offset_x = NO_REGISTER;
1094 reg_addr->reg_offset_y = NO_REGISTER;
1095 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
1096 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
1097 break;
1099 case EV_FT:
1100 reg_addr->reg_threshold = EV_REGISTER_THRESHOLD;
1101 reg_addr->reg_report_rate = NO_REGISTER;
1102 reg_addr->reg_gain = EV_REGISTER_GAIN;
1103 reg_addr->reg_offset = NO_REGISTER;
1104 reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1105 reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
1106 reg_addr->reg_num_x = NO_REGISTER;
1107 reg_addr->reg_num_y = NO_REGISTER;
1108 break;
1110 case GENERIC_FT:
1111 /* this is a guesswork */
1112 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1113 reg_addr->reg_report_rate = NO_REGISTER;
1114 reg_addr->reg_gain = M09_REGISTER_GAIN;
1115 reg_addr->reg_offset = M09_REGISTER_OFFSET;
1116 reg_addr->reg_offset_x = NO_REGISTER;
1117 reg_addr->reg_offset_y = NO_REGISTER;
1118 reg_addr->reg_num_x = NO_REGISTER;
1119 reg_addr->reg_num_y = NO_REGISTER;
1120 break;
1124 static void edt_ft5x06_disable_regulators(void *arg)
1126 struct edt_ft5x06_ts_data *data = arg;
1128 regulator_disable(data->vcc);
1129 regulator_disable(data->iovcc);
1132 static int edt_ft5x06_ts_probe(struct i2c_client *client)
1134 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1135 const struct edt_i2c_chip_data *chip_data;
1136 struct edt_ft5x06_ts_data *tsdata;
1137 unsigned int val;
1138 struct input_dev *input;
1139 unsigned long irq_flags;
1140 int error;
1141 u32 report_rate;
1143 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1145 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1146 if (!tsdata) {
1147 dev_err(&client->dev, "failed to allocate driver data.\n");
1148 return -ENOMEM;
1151 tsdata->regmap = regmap_init_i2c(client, &edt_ft5x06_i2c_regmap_config);
1152 if (IS_ERR(tsdata->regmap)) {
1153 dev_err(&client->dev, "regmap allocation failed\n");
1154 return PTR_ERR(tsdata->regmap);
1157 chip_data = device_get_match_data(&client->dev);
1158 if (!chip_data)
1159 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1160 if (!chip_data || !chip_data->max_support_points) {
1161 dev_err(&client->dev, "invalid or missing chip data\n");
1162 return -EINVAL;
1165 tsdata->max_support_points = chip_data->max_support_points;
1167 tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
1168 if (IS_ERR(tsdata->vcc))
1169 return dev_err_probe(&client->dev, PTR_ERR(tsdata->vcc),
1170 "failed to request regulator\n");
1172 tsdata->iovcc = devm_regulator_get(&client->dev, "iovcc");
1173 if (IS_ERR(tsdata->iovcc)) {
1174 error = PTR_ERR(tsdata->iovcc);
1175 if (error != -EPROBE_DEFER)
1176 dev_err(&client->dev,
1177 "failed to request iovcc regulator: %d\n", error);
1178 return error;
1181 error = regulator_enable(tsdata->iovcc);
1182 if (error < 0) {
1183 dev_err(&client->dev, "failed to enable iovcc: %d\n", error);
1184 return error;
1187 /* Delay enabling VCC for > 10us (T_ivd) after IOVCC */
1188 usleep_range(10, 100);
1190 error = regulator_enable(tsdata->vcc);
1191 if (error < 0) {
1192 dev_err(&client->dev, "failed to enable vcc: %d\n", error);
1193 regulator_disable(tsdata->iovcc);
1194 return error;
1197 error = devm_add_action_or_reset(&client->dev,
1198 edt_ft5x06_disable_regulators,
1199 tsdata);
1200 if (error)
1201 return error;
1203 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1204 "reset", GPIOD_OUT_HIGH);
1205 if (IS_ERR(tsdata->reset_gpio)) {
1206 error = PTR_ERR(tsdata->reset_gpio);
1207 dev_err(&client->dev,
1208 "Failed to request GPIO reset pin, error %d\n", error);
1209 return error;
1212 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1213 "wake", GPIOD_OUT_LOW);
1214 if (IS_ERR(tsdata->wake_gpio)) {
1215 error = PTR_ERR(tsdata->wake_gpio);
1216 dev_err(&client->dev,
1217 "Failed to request GPIO wake pin, error %d\n", error);
1218 return error;
1222 * Check which sleep modes we can support. Power-off requieres the
1223 * reset-pin to ensure correct power-down/power-up behaviour. Start with
1224 * the EDT_PMODE_POWEROFF test since this is the deepest possible sleep
1225 * mode.
1227 if (tsdata->reset_gpio)
1228 tsdata->suspend_mode = EDT_PMODE_POWEROFF;
1229 else if (tsdata->wake_gpio)
1230 tsdata->suspend_mode = EDT_PMODE_HIBERNATE;
1231 else
1232 tsdata->suspend_mode = EDT_PMODE_NOT_SUPPORTED;
1234 if (tsdata->wake_gpio) {
1235 usleep_range(5000, 6000);
1236 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1237 usleep_range(5000, 6000);
1240 if (tsdata->reset_gpio) {
1241 usleep_range(5000, 6000);
1242 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1243 msleep(300);
1246 input = devm_input_allocate_device(&client->dev);
1247 if (!input) {
1248 dev_err(&client->dev, "failed to allocate input device.\n");
1249 return -ENOMEM;
1252 mutex_init(&tsdata->mutex);
1253 tsdata->client = client;
1254 tsdata->input = input;
1255 tsdata->factory_mode = false;
1256 i2c_set_clientdata(client, tsdata);
1258 error = edt_ft5x06_ts_identify(client, tsdata);
1259 if (error) {
1260 dev_err(&client->dev, "touchscreen probe failed\n");
1261 return error;
1265 * Dummy read access. EP0700MLP1 returns bogus data on the first
1266 * register read access and ignores writes.
1268 regmap_read(tsdata->regmap, 0x00, &val);
1270 edt_ft5x06_ts_set_tdata_parameters(tsdata);
1271 edt_ft5x06_ts_set_regs(tsdata);
1272 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
1273 edt_ft5x06_ts_get_parameters(tsdata);
1275 if (tsdata->reg_addr.reg_report_rate != NO_REGISTER &&
1276 !device_property_read_u32(&client->dev,
1277 "report-rate-hz", &report_rate)) {
1278 if (tsdata->version == EDT_M06)
1279 tsdata->report_rate = clamp_val(report_rate, 30, 140);
1280 else
1281 tsdata->report_rate = clamp_val(report_rate, 1, 255);
1283 if (report_rate != tsdata->report_rate)
1284 dev_warn(&client->dev,
1285 "report-rate %dHz is unsupported, use %dHz\n",
1286 report_rate, tsdata->report_rate);
1288 if (tsdata->version == EDT_M06)
1289 tsdata->report_rate /= 10;
1291 regmap_write(tsdata->regmap, tsdata->reg_addr.reg_report_rate,
1292 tsdata->report_rate);
1295 dev_dbg(&client->dev,
1296 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1297 tsdata->name, tsdata->fw_version, tsdata->num_x, tsdata->num_y);
1299 input->name = tsdata->name;
1300 input->id.bustype = BUS_I2C;
1301 input->dev.parent = &client->dev;
1303 input_set_abs_params(input, ABS_MT_POSITION_X,
1304 0, tsdata->num_x * 64 - 1, 0, 0);
1305 input_set_abs_params(input, ABS_MT_POSITION_Y,
1306 0, tsdata->num_y * 64 - 1, 0, 0);
1308 touchscreen_parse_properties(input, true, &tsdata->prop);
1310 error = input_mt_init_slots(input, tsdata->max_support_points,
1311 INPUT_MT_DIRECT);
1312 if (error) {
1313 dev_err(&client->dev, "Unable to init MT slots.\n");
1314 return error;
1317 irq_flags = irq_get_trigger_type(client->irq);
1318 if (irq_flags == IRQF_TRIGGER_NONE)
1319 irq_flags = IRQF_TRIGGER_FALLING;
1320 irq_flags |= IRQF_ONESHOT;
1322 error = devm_request_threaded_irq(&client->dev, client->irq,
1323 NULL, edt_ft5x06_ts_isr, irq_flags,
1324 client->name, tsdata);
1325 if (error) {
1326 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
1327 return error;
1330 error = input_register_device(input);
1331 if (error)
1332 return error;
1334 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1336 dev_dbg(&client->dev,
1337 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1338 client->irq,
1339 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1340 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
1342 return 0;
1345 static void edt_ft5x06_ts_remove(struct i2c_client *client)
1347 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1349 edt_ft5x06_ts_teardown_debugfs(tsdata);
1350 regmap_exit(tsdata->regmap);
1353 static int edt_ft5x06_ts_suspend(struct device *dev)
1355 struct i2c_client *client = to_i2c_client(dev);
1356 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1357 struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1358 int ret;
1360 if (device_may_wakeup(dev))
1361 return 0;
1363 if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1364 return 0;
1366 /* Enter hibernate mode. */
1367 ret = regmap_write(tsdata->regmap, PMOD_REGISTER_OPMODE,
1368 PMOD_REGISTER_HIBERNATE);
1369 if (ret)
1370 dev_warn(dev, "Failed to set hibernate mode\n");
1372 if (tsdata->suspend_mode == EDT_PMODE_HIBERNATE)
1373 return 0;
1376 * Power-off according the datasheet. Cut the power may leaf the irq
1377 * line in an undefined state depending on the host pull resistor
1378 * settings. Disable the irq to avoid adjusting each host till the
1379 * device is back in a full functional state.
1381 disable_irq(tsdata->client->irq);
1383 gpiod_set_value_cansleep(reset_gpio, 1);
1384 usleep_range(1000, 2000);
1386 ret = regulator_disable(tsdata->vcc);
1387 if (ret)
1388 dev_warn(dev, "Failed to disable vcc\n");
1389 ret = regulator_disable(tsdata->iovcc);
1390 if (ret)
1391 dev_warn(dev, "Failed to disable iovcc\n");
1393 return 0;
1396 static int edt_ft5x06_ts_resume(struct device *dev)
1398 struct i2c_client *client = to_i2c_client(dev);
1399 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1400 int ret = 0;
1402 if (device_may_wakeup(dev))
1403 return 0;
1405 if (tsdata->suspend_mode == EDT_PMODE_NOT_SUPPORTED)
1406 return 0;
1408 if (tsdata->suspend_mode == EDT_PMODE_POWEROFF) {
1409 struct gpio_desc *reset_gpio = tsdata->reset_gpio;
1412 * We can't check if the regulator is a dummy or a real
1413 * regulator. So we need to specify the 5ms reset time (T_rst)
1414 * here instead of the 100us T_rtp time. We also need to wait
1415 * 300ms in case it was a real supply and the power was cutted
1416 * of. Toggle the reset pin is also a way to exit the hibernate
1417 * mode.
1419 gpiod_set_value_cansleep(reset_gpio, 1);
1420 usleep_range(5000, 6000);
1422 ret = regulator_enable(tsdata->iovcc);
1423 if (ret) {
1424 dev_err(dev, "Failed to enable iovcc\n");
1425 return ret;
1428 /* Delay enabling VCC for > 10us (T_ivd) after IOVCC */
1429 usleep_range(10, 100);
1431 ret = regulator_enable(tsdata->vcc);
1432 if (ret) {
1433 dev_err(dev, "Failed to enable vcc\n");
1434 regulator_disable(tsdata->iovcc);
1435 return ret;
1438 usleep_range(1000, 2000);
1439 gpiod_set_value_cansleep(reset_gpio, 0);
1440 msleep(300);
1442 edt_ft5x06_restore_reg_parameters(tsdata);
1443 enable_irq(tsdata->client->irq);
1445 if (tsdata->factory_mode)
1446 ret = edt_ft5x06_factory_mode(tsdata);
1447 } else {
1448 struct gpio_desc *wake_gpio = tsdata->wake_gpio;
1450 gpiod_set_value_cansleep(wake_gpio, 0);
1451 usleep_range(5000, 6000);
1452 gpiod_set_value_cansleep(wake_gpio, 1);
1455 return ret;
1458 static DEFINE_SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1459 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1461 static const struct edt_i2c_chip_data edt_ft5x06_data = {
1462 .max_support_points = 5,
1465 static const struct edt_i2c_chip_data edt_ft5452_data = {
1466 .max_support_points = 5,
1469 static const struct edt_i2c_chip_data edt_ft5506_data = {
1470 .max_support_points = 10,
1473 static const struct edt_i2c_chip_data edt_ft6236_data = {
1474 .max_support_points = 2,
1477 static const struct edt_i2c_chip_data edt_ft8201_data = {
1478 .max_support_points = 10,
1481 static const struct edt_i2c_chip_data edt_ft8719_data = {
1482 .max_support_points = 10,
1485 static const struct i2c_device_id edt_ft5x06_ts_id[] = {
1486 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
1487 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
1488 { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
1489 { .name = "ft5452", .driver_data = (long)&edt_ft5452_data },
1490 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1491 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
1492 { .name = "ft8201", .driver_data = (long)&edt_ft8201_data },
1493 { .name = "ft8719", .driver_data = (long)&edt_ft8719_data },
1494 { /* sentinel */ }
1496 MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1498 static const struct of_device_id edt_ft5x06_of_match[] = {
1499 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1500 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1501 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
1502 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
1503 { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
1504 { .compatible = "focaltech,ft5426", .data = &edt_ft5506_data },
1505 { .compatible = "focaltech,ft5452", .data = &edt_ft5452_data },
1506 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1507 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
1508 { .compatible = "focaltech,ft8201", .data = &edt_ft8201_data },
1509 { .compatible = "focaltech,ft8719", .data = &edt_ft8719_data },
1510 { /* sentinel */ }
1512 MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1514 static struct i2c_driver edt_ft5x06_ts_driver = {
1515 .driver = {
1516 .name = "edt_ft5x06",
1517 .dev_groups = edt_ft5x06_groups,
1518 .of_match_table = edt_ft5x06_of_match,
1519 .pm = pm_sleep_ptr(&edt_ft5x06_ts_pm_ops),
1520 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1522 .id_table = edt_ft5x06_ts_id,
1523 .probe = edt_ft5x06_ts_probe,
1524 .remove = edt_ft5x06_ts_remove,
1527 module_i2c_driver(edt_ft5x06_ts_driver);
1529 MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1530 MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1531 MODULE_LICENSE("GPL v2");