blob: d2587724c52ada7256bec4da1dcd96798dedbf8f [file] [log] [blame]
Andy Shevchenko6d3a41ab2019-03-03 23:21:56 -08001// SPDX-License-Identifier: GPL-2.0
Simon Budig43c4d132012-07-24 23:29:36 -07002/*
3 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07004 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07005 * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
Simon Budig43c4d132012-07-24 23:29:36 -07006 */
7
8/*
9 * This is a driver for the EDT "Polytouch" family of touch controllers
10 * based on the FocalTech FT5x06 line of chips.
11 *
12 * Development of this driver has been sponsored by Glyn:
13 * http://www.glyn.com/Products/Displays
14 */
15
Marco Felsch8726e4c2020-01-09 17:03:32 -080016#include <linux/debugfs.h>
17#include <linux/delay.h>
18#include <linux/gpio/consumer.h>
19#include <linux/i2c.h>
Simon Budig43c4d132012-07-24 23:29:36 -070020#include <linux/interrupt.h>
21#include <linux/input.h>
Simon Budig43c4d132012-07-24 23:29:36 -070022#include <linux/input/mt.h>
Maxime Ripard2c005592015-03-21 20:18:06 -070023#include <linux/input/touchscreen.h>
Marco Felsch8726e4c2020-01-09 17:03:32 -080024#include <linux/irq.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/ratelimit.h>
Mylène Josserand7448bfe2019-10-28 20:56:23 -070028#include <linux/regulator/consumer.h>
Marco Felsch8726e4c2020-01-09 17:03:32 -080029#include <linux/slab.h>
30#include <linux/uaccess.h>
31
32#include <asm/unaligned.h>
Simon Budig43c4d132012-07-24 23:29:36 -070033
34#define WORK_REGISTER_THRESHOLD 0x00
35#define WORK_REGISTER_REPORT_RATE 0x08
36#define WORK_REGISTER_GAIN 0x30
37#define WORK_REGISTER_OFFSET 0x31
38#define WORK_REGISTER_NUM_X 0x33
39#define WORK_REGISTER_NUM_Y 0x34
40
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070041#define M09_REGISTER_THRESHOLD 0x80
42#define M09_REGISTER_GAIN 0x92
43#define M09_REGISTER_OFFSET 0x93
44#define M09_REGISTER_NUM_X 0x94
45#define M09_REGISTER_NUM_Y 0x95
46
Marco Felscha2f39da2019-01-13 23:08:32 -080047#define EV_REGISTER_THRESHOLD 0x40
48#define EV_REGISTER_GAIN 0x41
Marco Felschb6eba862019-01-13 23:10:50 -080049#define EV_REGISTER_OFFSET_Y 0x45
50#define EV_REGISTER_OFFSET_X 0x46
Marco Felscha2f39da2019-01-13 23:08:32 -080051
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070052#define NO_REGISTER 0xff
53
Simon Budig43c4d132012-07-24 23:29:36 -070054#define WORK_REGISTER_OPMODE 0x3c
55#define FACTORY_REGISTER_OPMODE 0x01
56
57#define TOUCH_EVENT_DOWN 0x00
58#define TOUCH_EVENT_UP 0x01
59#define TOUCH_EVENT_ON 0x02
60#define TOUCH_EVENT_RESERVED 0x03
61
62#define EDT_NAME_LEN 23
63#define EDT_SWITCH_MODE_RETRIES 10
64#define EDT_SWITCH_MODE_DELAY 5 /* msec */
65#define EDT_RAW_DATA_RETRIES 100
Aniroop Mathur0eeecf62017-01-04 10:57:51 -080066#define EDT_RAW_DATA_DELAY 1000 /* usec */
Simon Budig43c4d132012-07-24 23:29:36 -070067
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070068enum edt_ver {
Simon Budig169110c2017-10-09 20:58:11 -070069 EDT_M06,
70 EDT_M09,
Simon Budigaed5d0e2017-10-09 21:00:16 -070071 EDT_M12,
Marco Felscha2f39da2019-01-13 23:08:32 -080072 EV_FT,
Simon Budig169110c2017-10-09 20:58:11 -070073 GENERIC_FT,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070074};
75
76struct edt_reg_addr {
77 int reg_threshold;
78 int reg_report_rate;
79 int reg_gain;
80 int reg_offset;
Marco Felschb6eba862019-01-13 23:10:50 -080081 int reg_offset_x;
82 int reg_offset_y;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070083 int reg_num_x;
84 int reg_num_y;
85};
86
Simon Budig43c4d132012-07-24 23:29:36 -070087struct edt_ft5x06_ts_data {
88 struct i2c_client *client;
89 struct input_dev *input;
Hans de Goedead368eb2016-07-15 14:26:53 -070090 struct touchscreen_properties prop;
Simon Budig43c4d132012-07-24 23:29:36 -070091 u16 num_x;
92 u16 num_y;
Mylène Josserand7448bfe2019-10-28 20:56:23 -070093 struct regulator *vcc;
Simon Budig43c4d132012-07-24 23:29:36 -070094
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -070095 struct gpio_desc *reset_gpio;
96 struct gpio_desc *wake_gpio;
Lothar Waßmanndac90dc22014-03-28 09:23:02 -070097
Simon Budig43c4d132012-07-24 23:29:36 -070098#if defined(CONFIG_DEBUG_FS)
99 struct dentry *debug_dir;
100 u8 *raw_buffer;
101 size_t raw_bufsize;
102#endif
103
104 struct mutex mutex;
105 bool factory_mode;
106 int threshold;
107 int gain;
108 int offset;
Marco Felschb6eba862019-01-13 23:10:50 -0800109 int offset_x;
110 int offset_y;
Simon Budig43c4d132012-07-24 23:29:36 -0700111 int report_rate;
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700112 int max_support_points;
Simon Budig43c4d132012-07-24 23:29:36 -0700113
114 char name[EDT_NAME_LEN];
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700115
116 struct edt_reg_addr reg_addr;
117 enum edt_ver version;
Simon Budig43c4d132012-07-24 23:29:36 -0700118};
119
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700120struct edt_i2c_chip_data {
121 int max_support_points;
122};
123
Simon Budig43c4d132012-07-24 23:29:36 -0700124static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
125 u16 wr_len, u8 *wr_buf,
126 u16 rd_len, u8 *rd_buf)
127{
128 struct i2c_msg wrmsg[2];
129 int i = 0;
130 int ret;
131
132 if (wr_len) {
133 wrmsg[i].addr = client->addr;
134 wrmsg[i].flags = 0;
135 wrmsg[i].len = wr_len;
136 wrmsg[i].buf = wr_buf;
137 i++;
138 }
139 if (rd_len) {
140 wrmsg[i].addr = client->addr;
141 wrmsg[i].flags = I2C_M_RD;
142 wrmsg[i].len = rd_len;
143 wrmsg[i].buf = rd_buf;
144 i++;
145 }
146
147 ret = i2c_transfer(client->adapter, wrmsg, i);
148 if (ret < 0)
149 return ret;
150 if (ret != i)
151 return -EIO;
152
153 return 0;
154}
155
156static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
157 u8 *buf, int buflen)
158{
159 int i;
160 u8 crc = 0;
161
162 for (i = 0; i < buflen - 1; i++)
163 crc ^= buf[i];
164
165 if (crc != buf[buflen-1]) {
166 dev_err_ratelimited(&tsdata->client->dev,
167 "crc error: 0x%02x expected, got 0x%02x\n",
168 crc, buf[buflen-1]);
169 return false;
170 }
171
172 return true;
173}
174
175static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
176{
177 struct edt_ft5x06_ts_data *tsdata = dev_id;
178 struct device *dev = &tsdata->client->dev;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700179 u8 cmd;
Franklin S Cooper Jr9378c022015-10-16 15:34:41 -0700180 u8 rdbuf[63];
Simon Budig43c4d132012-07-24 23:29:36 -0700181 int i, type, x, y, id;
Franklin S Cooper Jrc789f1f2015-10-16 15:34:05 -0700182 int offset, tplen, datalen, crclen;
Simon Budig43c4d132012-07-24 23:29:36 -0700183 int error;
184
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700185 switch (tsdata->version) {
Simon Budig169110c2017-10-09 20:58:11 -0700186 case EDT_M06:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700187 cmd = 0xf9; /* tell the controller to send touch data */
188 offset = 5; /* where the actual touch data starts */
189 tplen = 4; /* data comes in so called frames */
Franklin S Cooper Jrc789f1f2015-10-16 15:34:05 -0700190 crclen = 1; /* length of the crc data */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700191 break;
192
Simon Budig169110c2017-10-09 20:58:11 -0700193 case EDT_M09:
Simon Budigaed5d0e2017-10-09 21:00:16 -0700194 case EDT_M12:
Marco Felscha2f39da2019-01-13 23:08:32 -0800195 case EV_FT:
Simon Budig169110c2017-10-09 20:58:11 -0700196 case GENERIC_FT:
Franklin S Cooper Jr9378c022015-10-16 15:34:41 -0700197 cmd = 0x0;
198 offset = 3;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700199 tplen = 6;
Franklin S Cooper Jrc789f1f2015-10-16 15:34:05 -0700200 crclen = 0;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700201 break;
202
203 default:
204 goto out;
205 }
206
Simon Budig43c4d132012-07-24 23:29:36 -0700207 memset(rdbuf, 0, sizeof(rdbuf));
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700208 datalen = tplen * tsdata->max_support_points + offset + crclen;
Simon Budig43c4d132012-07-24 23:29:36 -0700209
210 error = edt_ft5x06_ts_readwrite(tsdata->client,
211 sizeof(cmd), &cmd,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700212 datalen, rdbuf);
Simon Budig43c4d132012-07-24 23:29:36 -0700213 if (error) {
214 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
215 error);
216 goto out;
217 }
218
Simon Budigaed5d0e2017-10-09 21:00:16 -0700219 /* M09/M12 does not send header or CRC */
Simon Budig169110c2017-10-09 20:58:11 -0700220 if (tsdata->version == EDT_M06) {
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700221 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
222 rdbuf[2] != datalen) {
223 dev_err_ratelimited(dev,
224 "Unexpected header: %02x%02x%02x!\n",
225 rdbuf[0], rdbuf[1], rdbuf[2]);
226 goto out;
227 }
228
229 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
230 goto out;
Simon Budig43c4d132012-07-24 23:29:36 -0700231 }
232
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700233 for (i = 0; i < tsdata->max_support_points; i++) {
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700234 u8 *buf = &rdbuf[i * tplen + offset];
Simon Budig43c4d132012-07-24 23:29:36 -0700235
236 type = buf[0] >> 6;
237 /* ignore Reserved events */
238 if (type == TOUCH_EVENT_RESERVED)
239 continue;
240
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700241 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
Simon Budig169110c2017-10-09 20:58:11 -0700242 if (tsdata->version == EDT_M06 && type == TOUCH_EVENT_DOWN)
Lothar Waßmannee3e9462014-03-28 09:28:17 -0700243 continue;
244
Dmitry Torokhov1b9c6982019-06-22 23:11:51 -0700245 x = get_unaligned_be16(buf) & 0x0fff;
246 y = get_unaligned_be16(buf + 2) & 0x0fff;
Marco Felscha2f39da2019-01-13 23:08:32 -0800247 /* The FT5x26 send the y coordinate first */
248 if (tsdata->version == EV_FT)
249 swap(x, y);
250
Simon Budig43c4d132012-07-24 23:29:36 -0700251 id = (buf[2] >> 4) & 0x0f;
Simon Budig43c4d132012-07-24 23:29:36 -0700252
253 input_mt_slot(tsdata->input, id);
Dmitry Torokhov17b92922019-06-22 23:21:24 -0700254 if (input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER,
255 type != TOUCH_EVENT_UP))
256 touchscreen_report_pos(tsdata->input, &tsdata->prop,
257 x, y, true);
Simon Budig43c4d132012-07-24 23:29:36 -0700258 }
259
260 input_mt_report_pointer_emulation(tsdata->input, true);
261 input_sync(tsdata->input);
262
263out:
264 return IRQ_HANDLED;
265}
266
267static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
268 u8 addr, u8 value)
269{
270 u8 wrbuf[4];
271
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700272 switch (tsdata->version) {
Simon Budig169110c2017-10-09 20:58:11 -0700273 case EDT_M06:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700274 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
275 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700276 wrbuf[2] = value;
277 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
278 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
279 wrbuf, 0, NULL);
Marco Felscha2f39da2019-01-13 23:08:32 -0800280 /* fallthrough */
Simon Budig169110c2017-10-09 20:58:11 -0700281 case EDT_M09:
Simon Budigaed5d0e2017-10-09 21:00:16 -0700282 case EDT_M12:
Marco Felscha2f39da2019-01-13 23:08:32 -0800283 case EV_FT:
Simon Budig169110c2017-10-09 20:58:11 -0700284 case GENERIC_FT:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700285 wrbuf[0] = addr;
286 wrbuf[1] = value;
Simon Budig43c4d132012-07-24 23:29:36 -0700287
Robert Woerlecc071ac2014-06-07 22:20:23 -0700288 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700289 wrbuf, 0, NULL);
290
291 default:
292 return -EINVAL;
293 }
Simon Budig43c4d132012-07-24 23:29:36 -0700294}
295
296static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
297 u8 addr)
298{
299 u8 wrbuf[2], rdbuf[2];
300 int error;
301
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700302 switch (tsdata->version) {
Simon Budig169110c2017-10-09 20:58:11 -0700303 case EDT_M06:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700304 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
305 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
306 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
Simon Budig43c4d132012-07-24 23:29:36 -0700307
Dan Carpentere2c3ecf2014-04-03 09:17:05 -0700308 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
309 rdbuf);
310 if (error)
311 return error;
Simon Budig43c4d132012-07-24 23:29:36 -0700312
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700313 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
314 dev_err(&tsdata->client->dev,
315 "crc error: 0x%02x expected, got 0x%02x\n",
316 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
317 rdbuf[1]);
318 return -EIO;
319 }
320 break;
321
Marco Felscha2f39da2019-01-13 23:08:32 -0800322 /* fallthrough */
Simon Budig169110c2017-10-09 20:58:11 -0700323 case EDT_M09:
Simon Budigaed5d0e2017-10-09 21:00:16 -0700324 case EDT_M12:
Marco Felscha2f39da2019-01-13 23:08:32 -0800325 case EV_FT:
Simon Budig169110c2017-10-09 20:58:11 -0700326 case GENERIC_FT:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700327 wrbuf[0] = addr;
328 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
329 wrbuf, 1, rdbuf);
330 if (error)
331 return error;
332 break;
333
334 default:
335 return -EINVAL;
Simon Budig43c4d132012-07-24 23:29:36 -0700336 }
337
338 return rdbuf[0];
339}
340
341struct edt_ft5x06_attribute {
342 struct device_attribute dattr;
343 size_t field_offset;
344 u8 limit_low;
345 u8 limit_high;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700346 u8 addr_m06;
347 u8 addr_m09;
Marco Felsch2ebc1912019-01-13 23:10:22 -0800348 u8 addr_ev;
Simon Budig43c4d132012-07-24 23:29:36 -0700349};
350
Marco Felsch2ebc1912019-01-13 23:10:22 -0800351#define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, _addr_ev, \
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700352 _limit_low, _limit_high) \
Simon Budig43c4d132012-07-24 23:29:36 -0700353 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
354 .dattr = __ATTR(_field, _mode, \
355 edt_ft5x06_setting_show, \
356 edt_ft5x06_setting_store), \
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700357 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
358 .addr_m06 = _addr_m06, \
359 .addr_m09 = _addr_m09, \
Marco Felsch2ebc1912019-01-13 23:10:22 -0800360 .addr_ev = _addr_ev, \
Simon Budig43c4d132012-07-24 23:29:36 -0700361 .limit_low = _limit_low, \
362 .limit_high = _limit_high, \
Simon Budig43c4d132012-07-24 23:29:36 -0700363 }
364
365static ssize_t edt_ft5x06_setting_show(struct device *dev,
366 struct device_attribute *dattr,
367 char *buf)
368{
369 struct i2c_client *client = to_i2c_client(dev);
370 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
371 struct edt_ft5x06_attribute *attr =
372 container_of(dattr, struct edt_ft5x06_attribute, dattr);
Lothar Waßmann1730d812014-03-28 09:20:08 -0700373 u8 *field = (u8 *)tsdata + attr->field_offset;
Simon Budig43c4d132012-07-24 23:29:36 -0700374 int val;
375 size_t count = 0;
376 int error = 0;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700377 u8 addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700378
379 mutex_lock(&tsdata->mutex);
380
381 if (tsdata->factory_mode) {
382 error = -EIO;
383 goto out;
384 }
385
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700386 switch (tsdata->version) {
Simon Budig169110c2017-10-09 20:58:11 -0700387 case EDT_M06:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700388 addr = attr->addr_m06;
389 break;
390
Simon Budig169110c2017-10-09 20:58:11 -0700391 case EDT_M09:
Simon Budigaed5d0e2017-10-09 21:00:16 -0700392 case EDT_M12:
Simon Budig169110c2017-10-09 20:58:11 -0700393 case GENERIC_FT:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700394 addr = attr->addr_m09;
395 break;
396
Marco Felsch2ebc1912019-01-13 23:10:22 -0800397 case EV_FT:
398 addr = attr->addr_ev;
399 break;
400
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700401 default:
402 error = -ENODEV;
Simon Budig43c4d132012-07-24 23:29:36 -0700403 goto out;
404 }
405
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700406 if (addr != NO_REGISTER) {
407 val = edt_ft5x06_register_read(tsdata, addr);
408 if (val < 0) {
409 error = val;
410 dev_err(&tsdata->client->dev,
411 "Failed to fetch attribute %s, error %d\n",
412 dattr->attr.name, error);
413 goto out;
414 }
415 } else {
416 val = *field;
417 }
418
Simon Budig43c4d132012-07-24 23:29:36 -0700419 if (val != *field) {
420 dev_warn(&tsdata->client->dev,
421 "%s: read (%d) and stored value (%d) differ\n",
422 dattr->attr.name, val, *field);
423 *field = val;
424 }
425
426 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
427out:
428 mutex_unlock(&tsdata->mutex);
429 return error ?: count;
430}
431
432static ssize_t edt_ft5x06_setting_store(struct device *dev,
433 struct device_attribute *dattr,
434 const char *buf, size_t count)
435{
436 struct i2c_client *client = to_i2c_client(dev);
437 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
438 struct edt_ft5x06_attribute *attr =
439 container_of(dattr, struct edt_ft5x06_attribute, dattr);
Lothar Waßmann1730d812014-03-28 09:20:08 -0700440 u8 *field = (u8 *)tsdata + attr->field_offset;
Simon Budig43c4d132012-07-24 23:29:36 -0700441 unsigned int val;
442 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700443 u8 addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700444
445 mutex_lock(&tsdata->mutex);
446
447 if (tsdata->factory_mode) {
448 error = -EIO;
449 goto out;
450 }
451
452 error = kstrtouint(buf, 0, &val);
453 if (error)
454 goto out;
455
456 if (val < attr->limit_low || val > attr->limit_high) {
457 error = -ERANGE;
458 goto out;
459 }
460
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700461 switch (tsdata->version) {
Simon Budig169110c2017-10-09 20:58:11 -0700462 case EDT_M06:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700463 addr = attr->addr_m06;
464 break;
465
Simon Budig169110c2017-10-09 20:58:11 -0700466 case EDT_M09:
Simon Budigaed5d0e2017-10-09 21:00:16 -0700467 case EDT_M12:
Simon Budig169110c2017-10-09 20:58:11 -0700468 case GENERIC_FT:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700469 addr = attr->addr_m09;
470 break;
471
Marco Felsch2ebc1912019-01-13 23:10:22 -0800472 case EV_FT:
473 addr = attr->addr_ev;
474 break;
475
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700476 default:
477 error = -ENODEV;
Simon Budig43c4d132012-07-24 23:29:36 -0700478 goto out;
479 }
480
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700481 if (addr != NO_REGISTER) {
482 error = edt_ft5x06_register_write(tsdata, addr, val);
483 if (error) {
484 dev_err(&tsdata->client->dev,
485 "Failed to update attribute %s, error: %d\n",
486 dattr->attr.name, error);
487 goto out;
488 }
489 }
Simon Budig43c4d132012-07-24 23:29:36 -0700490 *field = val;
491
492out:
493 mutex_unlock(&tsdata->mutex);
494 return error ?: count;
495}
496
Simon Budigaed5d0e2017-10-09 21:00:16 -0700497/* m06, m09: range 0-31, m12: range 0-5 */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700498static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
Marco Felsch2ebc1912019-01-13 23:10:22 -0800499 M09_REGISTER_GAIN, EV_REGISTER_GAIN, 0, 31);
Simon Budigaed5d0e2017-10-09 21:00:16 -0700500/* m06, m09: range 0-31, m12: range 0-16 */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700501static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
Marco Felsch2ebc1912019-01-13 23:10:22 -0800502 M09_REGISTER_OFFSET, NO_REGISTER, 0, 31);
Marco Felschb6eba862019-01-13 23:10:50 -0800503/* m06, m09, m12: no supported, ev_ft: range 0-80 */
504static EDT_ATTR(offset_x, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
505 EV_REGISTER_OFFSET_X, 0, 80);
506/* m06, m09, m12: no supported, ev_ft: range 0-80 */
507static EDT_ATTR(offset_y, S_IWUSR | S_IRUGO, NO_REGISTER, NO_REGISTER,
508 EV_REGISTER_OFFSET_Y, 0, 80);
Simon Budigaed5d0e2017-10-09 21:00:16 -0700509/* m06: range 20 to 80, m09: range 0 to 30, m12: range 1 to 255... */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700510static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
Marco Felsch2ebc1912019-01-13 23:10:22 -0800511 M09_REGISTER_THRESHOLD, EV_REGISTER_THRESHOLD, 0, 255);
Simon Budigaed5d0e2017-10-09 21:00:16 -0700512/* m06: range 3 to 14, m12: (0x64: 100Hz) */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700513static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
Marco Felsch2ebc1912019-01-13 23:10:22 -0800514 NO_REGISTER, NO_REGISTER, 0, 255);
Simon Budig43c4d132012-07-24 23:29:36 -0700515
516static struct attribute *edt_ft5x06_attrs[] = {
517 &edt_ft5x06_attr_gain.dattr.attr,
518 &edt_ft5x06_attr_offset.dattr.attr,
Marco Felschb6eba862019-01-13 23:10:50 -0800519 &edt_ft5x06_attr_offset_x.dattr.attr,
520 &edt_ft5x06_attr_offset_y.dattr.attr,
Simon Budig43c4d132012-07-24 23:29:36 -0700521 &edt_ft5x06_attr_threshold.dattr.attr,
522 &edt_ft5x06_attr_report_rate.dattr.attr,
523 NULL
524};
525
526static const struct attribute_group edt_ft5x06_attr_group = {
527 .attrs = edt_ft5x06_attrs,
528};
529
530#ifdef CONFIG_DEBUG_FS
531static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
532{
533 struct i2c_client *client = tsdata->client;
534 int retries = EDT_SWITCH_MODE_RETRIES;
535 int ret;
536 int error;
537
Dmitry Torokhov4b3e910d2018-01-23 10:01:57 -0800538 if (tsdata->version != EDT_M06) {
539 dev_err(&client->dev,
540 "No factory mode support for non-M06 devices\n");
541 return -EINVAL;
542 }
543
Simon Budig43c4d132012-07-24 23:29:36 -0700544 disable_irq(client->irq);
545
546 if (!tsdata->raw_buffer) {
547 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
548 sizeof(u16);
549 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
550 if (!tsdata->raw_buffer) {
551 error = -ENOMEM;
552 goto err_out;
553 }
554 }
555
556 /* mode register is 0x3c when in the work mode */
557 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
558 if (error) {
559 dev_err(&client->dev,
560 "failed to switch to factory mode, error %d\n", error);
561 goto err_out;
562 }
563
564 tsdata->factory_mode = true;
565 do {
566 mdelay(EDT_SWITCH_MODE_DELAY);
567 /* mode register is 0x01 when in factory mode */
568 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
569 if (ret == 0x03)
570 break;
571 } while (--retries > 0);
572
573 if (retries == 0) {
574 dev_err(&client->dev, "not in factory mode after %dms.\n",
575 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
576 error = -EIO;
577 goto err_out;
578 }
579
580 return 0;
581
582err_out:
583 kfree(tsdata->raw_buffer);
584 tsdata->raw_buffer = NULL;
585 tsdata->factory_mode = false;
586 enable_irq(client->irq);
587
588 return error;
589}
590
591static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
592{
593 struct i2c_client *client = tsdata->client;
594 int retries = EDT_SWITCH_MODE_RETRIES;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700595 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700596 int ret;
597 int error;
598
599 /* mode register is 0x01 when in the factory mode */
600 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
601 if (error) {
602 dev_err(&client->dev,
603 "failed to switch to work mode, error: %d\n", error);
604 return error;
605 }
606
607 tsdata->factory_mode = false;
608
609 do {
610 mdelay(EDT_SWITCH_MODE_DELAY);
611 /* mode register is 0x01 when in factory mode */
612 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
613 if (ret == 0x01)
614 break;
615 } while (--retries > 0);
616
617 if (retries == 0) {
618 dev_err(&client->dev, "not in work mode after %dms.\n",
619 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
620 tsdata->factory_mode = true;
621 return -EIO;
622 }
623
Sachin Kamat8efcc502013-03-28 01:14:42 -0700624 kfree(tsdata->raw_buffer);
Simon Budig43c4d132012-07-24 23:29:36 -0700625 tsdata->raw_buffer = NULL;
626
627 /* restore parameters */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700628 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
Simon Budig43c4d132012-07-24 23:29:36 -0700629 tsdata->threshold);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700630 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
Simon Budig43c4d132012-07-24 23:29:36 -0700631 tsdata->gain);
Marco Felscha2f39da2019-01-13 23:08:32 -0800632 if (reg_addr->reg_offset != NO_REGISTER)
633 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
634 tsdata->offset);
Marco Felschb6eba862019-01-13 23:10:50 -0800635 if (reg_addr->reg_offset_x != NO_REGISTER)
636 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x,
637 tsdata->offset_x);
638 if (reg_addr->reg_offset_y != NO_REGISTER)
639 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y,
640 tsdata->offset_y);
Luca Ceresoli47014752017-09-07 14:28:28 -0700641 if (reg_addr->reg_report_rate != NO_REGISTER)
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700642 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
Simon Budig43c4d132012-07-24 23:29:36 -0700643 tsdata->report_rate);
644
645 enable_irq(client->irq);
646
647 return 0;
648}
649
650static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
651{
652 struct edt_ft5x06_ts_data *tsdata = data;
653
654 *mode = tsdata->factory_mode;
655
656 return 0;
657};
658
659static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
660{
661 struct edt_ft5x06_ts_data *tsdata = data;
662 int retval = 0;
663
664 if (mode > 1)
665 return -ERANGE;
666
667 mutex_lock(&tsdata->mutex);
668
669 if (mode != tsdata->factory_mode) {
670 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
Lothar Waßmann1730d812014-03-28 09:20:08 -0700671 edt_ft5x06_work_mode(tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -0700672 }
673
674 mutex_unlock(&tsdata->mutex);
675
676 return retval;
677};
678
679DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
680 edt_ft5x06_debugfs_mode_set, "%llu\n");
681
Simon Budig43c4d132012-07-24 23:29:36 -0700682static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
683 char __user *buf, size_t count, loff_t *off)
684{
685 struct edt_ft5x06_ts_data *tsdata = file->private_data;
686 struct i2c_client *client = tsdata->client;
687 int retries = EDT_RAW_DATA_RETRIES;
688 int val, i, error;
689 size_t read = 0;
690 int colbytes;
691 char wrbuf[3];
692 u8 *rdbuf;
693
694 if (*off < 0 || *off >= tsdata->raw_bufsize)
695 return 0;
696
697 mutex_lock(&tsdata->mutex);
698
699 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
700 error = -EIO;
701 goto out;
702 }
703
704 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
705 if (error) {
706 dev_dbg(&client->dev,
707 "failed to write 0x08 register, error %d\n", error);
708 goto out;
709 }
710
711 do {
Aniroop Mathur0eeecf62017-01-04 10:57:51 -0800712 usleep_range(EDT_RAW_DATA_DELAY, EDT_RAW_DATA_DELAY + 100);
Simon Budig43c4d132012-07-24 23:29:36 -0700713 val = edt_ft5x06_register_read(tsdata, 0x08);
714 if (val < 1)
715 break;
716 } while (--retries > 0);
717
718 if (val < 0) {
719 error = val;
720 dev_dbg(&client->dev,
721 "failed to read 0x08 register, error %d\n", error);
722 goto out;
723 }
724
725 if (retries == 0) {
726 dev_dbg(&client->dev,
727 "timed out waiting for register to settle\n");
728 error = -ETIMEDOUT;
729 goto out;
730 }
731
732 rdbuf = tsdata->raw_buffer;
733 colbytes = tsdata->num_y * sizeof(u16);
734
735 wrbuf[0] = 0xf5;
736 wrbuf[1] = 0x0e;
737 for (i = 0; i < tsdata->num_x; i++) {
738 wrbuf[2] = i; /* column index */
739 error = edt_ft5x06_ts_readwrite(tsdata->client,
740 sizeof(wrbuf), wrbuf,
741 colbytes, rdbuf);
742 if (error)
743 goto out;
744
745 rdbuf += colbytes;
746 }
747
748 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
Axel Lin35b1da42012-09-19 15:56:23 -0700749 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
750 error = -EFAULT;
751 goto out;
752 }
753
754 *off += read;
Simon Budig43c4d132012-07-24 23:29:36 -0700755out:
756 mutex_unlock(&tsdata->mutex);
757 return error ?: read;
758};
759
Simon Budig43c4d132012-07-24 23:29:36 -0700760static const struct file_operations debugfs_raw_data_fops = {
Wei Yongjunf6c0df62012-10-17 23:55:54 -0700761 .open = simple_open,
Simon Budig43c4d132012-07-24 23:29:36 -0700762 .read = edt_ft5x06_debugfs_raw_data_read,
763};
764
Bill Pemberton5298cc42012-11-23 21:38:25 -0800765static void
Simon Budig43c4d132012-07-24 23:29:36 -0700766edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
767 const char *debugfs_name)
768{
769 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
Simon Budig43c4d132012-07-24 23:29:36 -0700770
771 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
772 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
773
774 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
775 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
776 debugfs_create_file("raw_data", S_IRUSR,
777 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
778}
779
Bill Pembertone2619cf2012-11-23 21:50:47 -0800780static void
Simon Budig43c4d132012-07-24 23:29:36 -0700781edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
782{
Fabian Frederickf69c6ec2014-07-09 09:45:57 -0700783 debugfs_remove_recursive(tsdata->debug_dir);
Guenter Roecka1d0fa72012-08-21 22:01:45 -0700784 kfree(tsdata->raw_buffer);
Simon Budig43c4d132012-07-24 23:29:36 -0700785}
786
787#else
788
789static inline void
790edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
791 const char *debugfs_name)
792{
793}
794
795static inline void
796edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
797{
798}
799
800#endif /* CONFIG_DEBUGFS */
801
Bill Pemberton5298cc42012-11-23 21:38:25 -0800802static int edt_ft5x06_ts_identify(struct i2c_client *client,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700803 struct edt_ft5x06_ts_data *tsdata,
804 char *fw_version)
Simon Budig43c4d132012-07-24 23:29:36 -0700805{
806 u8 rdbuf[EDT_NAME_LEN];
807 char *p;
808 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700809 char *model_name = tsdata->name;
Simon Budig43c4d132012-07-24 23:29:36 -0700810
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700811 /* see what we find if we assume it is a M06 *
812 * if we get less than EDT_NAME_LEN, we don't want
813 * to have garbage in there
814 */
815 memset(rdbuf, 0, sizeof(rdbuf));
Simon Budigaed5d0e2017-10-09 21:00:16 -0700816 error = edt_ft5x06_ts_readwrite(client, 1, "\xBB",
Simon Budig43c4d132012-07-24 23:29:36 -0700817 EDT_NAME_LEN - 1, rdbuf);
818 if (error)
819 return error;
820
Simon Budigaed5d0e2017-10-09 21:00:16 -0700821 /* Probe content for something consistent.
822 * M06 starts with a response byte, M12 gives the data directly.
823 * M09/Generic does not provide model number information.
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700824 */
Simon Budigaed5d0e2017-10-09 21:00:16 -0700825 if (!strncasecmp(rdbuf + 1, "EP0", 3)) {
Simon Budig169110c2017-10-09 20:58:11 -0700826 tsdata->version = EDT_M06;
Simon Budig43c4d132012-07-24 23:29:36 -0700827
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700828 /* remove last '$' end marker */
829 rdbuf[EDT_NAME_LEN - 1] = '\0';
830 if (rdbuf[EDT_NAME_LEN - 2] == '$')
831 rdbuf[EDT_NAME_LEN - 2] = '\0';
Simon Budig43c4d132012-07-24 23:29:36 -0700832
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700833 /* look for Model/Version separator */
834 p = strchr(rdbuf, '*');
835 if (p)
836 *p++ = '\0';
837 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
838 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
Simon Budigaed5d0e2017-10-09 21:00:16 -0700839 } else if (!strncasecmp(rdbuf, "EP0", 3)) {
840 tsdata->version = EDT_M12;
841
842 /* remove last '$' end marker */
843 rdbuf[EDT_NAME_LEN - 2] = '\0';
844 if (rdbuf[EDT_NAME_LEN - 3] == '$')
845 rdbuf[EDT_NAME_LEN - 3] = '\0';
846
847 /* look for Model/Version separator */
848 p = strchr(rdbuf, '*');
849 if (p)
850 *p++ = '\0';
851 strlcpy(model_name, rdbuf, EDT_NAME_LEN);
852 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700853 } else {
Simon Budigaed5d0e2017-10-09 21:00:16 -0700854 /* If it is not an EDT M06/M12 touchscreen, then the model
Simon Budig169110c2017-10-09 20:58:11 -0700855 * detection is a bit hairy. The different ft5x06
856 * firmares around don't reliably implement the
857 * identification registers. Well, we'll take a shot.
858 *
859 * The main difference between generic focaltec based
860 * touches and EDT M09 is that we know how to retrieve
861 * the max coordinates for the latter.
862 */
863 tsdata->version = GENERIC_FT;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700864
865 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
866 2, rdbuf);
867 if (error)
868 return error;
869
870 strlcpy(fw_version, rdbuf, 2);
871
872 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
873 1, rdbuf);
874 if (error)
875 return error;
876
Simon Budig169110c2017-10-09 20:58:11 -0700877 /* This "model identification" is not exact. Unfortunately
878 * not all firmwares for the ft5x06 put useful values in
879 * the identification registers.
880 */
881 switch (rdbuf[0]) {
882 case 0x35: /* EDT EP0350M09 */
883 case 0x43: /* EDT EP0430M09 */
884 case 0x50: /* EDT EP0500M09 */
885 case 0x57: /* EDT EP0570M09 */
886 case 0x70: /* EDT EP0700M09 */
887 tsdata->version = EDT_M09;
888 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
889 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
890 break;
891 case 0xa1: /* EDT EP1010ML00 */
892 tsdata->version = EDT_M09;
893 snprintf(model_name, EDT_NAME_LEN, "EP%i%i0ML00",
894 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
895 break;
896 case 0x5a: /* Solomon Goldentek Display */
897 snprintf(model_name, EDT_NAME_LEN, "GKTW50SCED1R0");
898 break;
Marco Felscha2f39da2019-01-13 23:08:32 -0800899 case 0x59: /* Evervision Display with FT5xx6 TS */
900 tsdata->version = EV_FT;
901 error = edt_ft5x06_ts_readwrite(client, 1, "\x53",
902 1, rdbuf);
903 if (error)
904 return error;
905 strlcpy(fw_version, rdbuf, 1);
906 snprintf(model_name, EDT_NAME_LEN,
907 "EVERVISION-FT5726NEi");
908 break;
Simon Budig169110c2017-10-09 20:58:11 -0700909 default:
910 snprintf(model_name, EDT_NAME_LEN,
911 "generic ft5x06 (%02x)",
912 rdbuf[0]);
913 break;
914 }
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700915 }
Simon Budig43c4d132012-07-24 23:29:36 -0700916
917 return 0;
918}
919
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700920static void edt_ft5x06_ts_get_defaults(struct device *dev,
Dmitry Torokhov22ddbac2015-09-12 09:37:03 -0700921 struct edt_ft5x06_ts_data *tsdata)
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700922{
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700923 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700924 u32 val;
925 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700926
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700927 error = device_property_read_u32(dev, "threshold", &val);
Philipp Zabeldc262df2016-02-09 09:32:42 -0800928 if (!error) {
929 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
930 tsdata->threshold = val;
931 }
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700932
933 error = device_property_read_u32(dev, "gain", &val);
Philipp Zabeldc262df2016-02-09 09:32:42 -0800934 if (!error) {
935 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
936 tsdata->gain = val;
937 }
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700938
939 error = device_property_read_u32(dev, "offset", &val);
Philipp Zabeldc262df2016-02-09 09:32:42 -0800940 if (!error) {
941 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
942 tsdata->offset = val;
943 }
Marco Felschb6eba862019-01-13 23:10:50 -0800944
945 error = device_property_read_u32(dev, "offset-x", &val);
946 if (!error) {
947 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_x, val);
948 tsdata->offset_x = val;
949 }
950
951 error = device_property_read_u32(dev, "offset-y", &val);
952 if (!error) {
953 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset_y, val);
954 tsdata->offset_y = val;
955 }
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700956}
957
Bill Pemberton5298cc42012-11-23 21:38:25 -0800958static void
Simon Budig43c4d132012-07-24 23:29:36 -0700959edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
960{
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700961 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
962
Simon Budig43c4d132012-07-24 23:29:36 -0700963 tsdata->threshold = edt_ft5x06_register_read(tsdata,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700964 reg_addr->reg_threshold);
965 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
Marco Felscha2f39da2019-01-13 23:08:32 -0800966 if (reg_addr->reg_offset != NO_REGISTER)
967 tsdata->offset =
968 edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
Marco Felschb6eba862019-01-13 23:10:50 -0800969 if (reg_addr->reg_offset_x != NO_REGISTER)
970 tsdata->offset_x = edt_ft5x06_register_read(tsdata,
971 reg_addr->reg_offset_x);
972 if (reg_addr->reg_offset_y != NO_REGISTER)
973 tsdata->offset_y = edt_ft5x06_register_read(tsdata,
974 reg_addr->reg_offset_y);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700975 if (reg_addr->reg_report_rate != NO_REGISTER)
976 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
977 reg_addr->reg_report_rate);
Simon Budig169110c2017-10-09 20:58:11 -0700978 if (tsdata->version == EDT_M06 ||
Simon Budigaed5d0e2017-10-09 21:00:16 -0700979 tsdata->version == EDT_M09 ||
980 tsdata->version == EDT_M12) {
Simon Budig169110c2017-10-09 20:58:11 -0700981 tsdata->num_x = edt_ft5x06_register_read(tsdata,
982 reg_addr->reg_num_x);
983 tsdata->num_y = edt_ft5x06_register_read(tsdata,
984 reg_addr->reg_num_y);
985 } else {
986 tsdata->num_x = -1;
987 tsdata->num_y = -1;
988 }
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700989}
990
991static void
992edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
993{
994 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
995
996 switch (tsdata->version) {
Simon Budig169110c2017-10-09 20:58:11 -0700997 case EDT_M06:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700998 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
999 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
1000 reg_addr->reg_gain = WORK_REGISTER_GAIN;
1001 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
Marco Felschb6eba862019-01-13 23:10:50 -08001002 reg_addr->reg_offset_x = NO_REGISTER;
1003 reg_addr->reg_offset_y = NO_REGISTER;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001004 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
1005 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
1006 break;
1007
Simon Budig169110c2017-10-09 20:58:11 -07001008 case EDT_M09:
Simon Budigaed5d0e2017-10-09 21:00:16 -07001009 case EDT_M12:
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001010 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
Luca Ceresoli47014752017-09-07 14:28:28 -07001011 reg_addr->reg_report_rate = NO_REGISTER;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001012 reg_addr->reg_gain = M09_REGISTER_GAIN;
1013 reg_addr->reg_offset = M09_REGISTER_OFFSET;
Marco Felschb6eba862019-01-13 23:10:50 -08001014 reg_addr->reg_offset_x = NO_REGISTER;
1015 reg_addr->reg_offset_y = NO_REGISTER;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001016 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
1017 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
1018 break;
Simon Budig169110c2017-10-09 20:58:11 -07001019
Marco Felscha2f39da2019-01-13 23:08:32 -08001020 case EV_FT:
1021 reg_addr->reg_threshold = EV_REGISTER_THRESHOLD;
1022 reg_addr->reg_gain = EV_REGISTER_GAIN;
1023 reg_addr->reg_offset = NO_REGISTER;
Marco Felschb6eba862019-01-13 23:10:50 -08001024 reg_addr->reg_offset_x = EV_REGISTER_OFFSET_X;
1025 reg_addr->reg_offset_y = EV_REGISTER_OFFSET_Y;
Marco Felscha2f39da2019-01-13 23:08:32 -08001026 reg_addr->reg_num_x = NO_REGISTER;
1027 reg_addr->reg_num_y = NO_REGISTER;
1028 reg_addr->reg_report_rate = NO_REGISTER;
1029 break;
1030
Simon Budig169110c2017-10-09 20:58:11 -07001031 case GENERIC_FT:
1032 /* this is a guesswork */
1033 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
1034 reg_addr->reg_gain = M09_REGISTER_GAIN;
1035 reg_addr->reg_offset = M09_REGISTER_OFFSET;
Marco Felschb6eba862019-01-13 23:10:50 -08001036 reg_addr->reg_offset_x = NO_REGISTER;
1037 reg_addr->reg_offset_y = NO_REGISTER;
Simon Budig169110c2017-10-09 20:58:11 -07001038 break;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001039 }
Simon Budig43c4d132012-07-24 23:29:36 -07001040}
1041
Mylène Josserand7448bfe2019-10-28 20:56:23 -07001042static void edt_ft5x06_disable_regulator(void *arg)
1043{
1044 struct edt_ft5x06_ts_data *data = arg;
1045
1046 regulator_disable(data->vcc);
1047}
1048
Bill Pemberton5298cc42012-11-23 21:38:25 -08001049static int edt_ft5x06_ts_probe(struct i2c_client *client,
Simon Budig43c4d132012-07-24 23:29:36 -07001050 const struct i2c_device_id *id)
1051{
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001052 const struct edt_i2c_chip_data *chip_data;
Simon Budig43c4d132012-07-24 23:29:36 -07001053 struct edt_ft5x06_ts_data *tsdata;
Philipp Zabele1123242020-01-09 17:03:21 -08001054 u8 buf[2] = { 0xfc, 0x00 };
Simon Budig43c4d132012-07-24 23:29:36 -07001055 struct input_dev *input;
Dmitry Torokhovf0bef752015-09-12 10:11:09 -07001056 unsigned long irq_flags;
Simon Budig43c4d132012-07-24 23:29:36 -07001057 int error;
1058 char fw_version[EDT_NAME_LEN];
1059
1060 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
1061
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001062 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
1063 if (!tsdata) {
Simon Budig43c4d132012-07-24 23:29:36 -07001064 dev_err(&client->dev, "failed to allocate driver data.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001065 return -ENOMEM;
1066 }
1067
Andy Shevchenkofc226eb2019-03-03 23:21:28 -08001068 chip_data = device_get_match_data(&client->dev);
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001069 if (!chip_data)
1070 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
1071 if (!chip_data || !chip_data->max_support_points) {
1072 dev_err(&client->dev, "invalid or missing chip data\n");
1073 return -EINVAL;
1074 }
1075
1076 tsdata->max_support_points = chip_data->max_support_points;
1077
Mylène Josserand7448bfe2019-10-28 20:56:23 -07001078 tsdata->vcc = devm_regulator_get(&client->dev, "vcc");
1079 if (IS_ERR(tsdata->vcc)) {
1080 error = PTR_ERR(tsdata->vcc);
1081 if (error != -EPROBE_DEFER)
1082 dev_err(&client->dev,
1083 "failed to request regulator: %d\n", error);
1084 return error;
1085 }
1086
1087 error = regulator_enable(tsdata->vcc);
1088 if (error < 0) {
1089 dev_err(&client->dev, "failed to enable vcc: %d\n", error);
1090 return error;
1091 }
1092
1093 error = devm_add_action_or_reset(&client->dev,
1094 edt_ft5x06_disable_regulator,
1095 tsdata);
1096 if (error)
1097 return error;
1098
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -07001099 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
1100 "reset", GPIOD_OUT_HIGH);
1101 if (IS_ERR(tsdata->reset_gpio)) {
1102 error = PTR_ERR(tsdata->reset_gpio);
1103 dev_err(&client->dev,
1104 "Failed to request GPIO reset pin, error %d\n", error);
1105 return error;
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001106 }
1107
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -07001108 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
1109 "wake", GPIOD_OUT_LOW);
1110 if (IS_ERR(tsdata->wake_gpio)) {
1111 error = PTR_ERR(tsdata->wake_gpio);
1112 dev_err(&client->dev,
1113 "Failed to request GPIO wake pin, error %d\n", error);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001114 return error;
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -07001115 }
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001116
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -07001117 if (tsdata->wake_gpio) {
1118 usleep_range(5000, 6000);
1119 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
1120 }
1121
1122 if (tsdata->reset_gpio) {
1123 usleep_range(5000, 6000);
1124 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
1125 msleep(300);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001126 }
1127
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001128 input = devm_input_allocate_device(&client->dev);
1129 if (!input) {
1130 dev_err(&client->dev, "failed to allocate input device.\n");
1131 return -ENOMEM;
Simon Budig43c4d132012-07-24 23:29:36 -07001132 }
1133
1134 mutex_init(&tsdata->mutex);
1135 tsdata->client = client;
1136 tsdata->input = input;
1137 tsdata->factory_mode = false;
1138
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001139 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
Simon Budig43c4d132012-07-24 23:29:36 -07001140 if (error) {
1141 dev_err(&client->dev, "touchscreen probe failed\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001142 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001143 }
1144
Philipp Zabele1123242020-01-09 17:03:21 -08001145 /*
1146 * Dummy read access. EP0700MLP1 returns bogus data on the first
1147 * register read access and ignores writes.
1148 */
1149 edt_ft5x06_ts_readwrite(tsdata->client, 2, buf, 2, buf);
1150
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001151 edt_ft5x06_ts_set_regs(tsdata);
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -07001152 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -07001153 edt_ft5x06_ts_get_parameters(tsdata);
1154
1155 dev_dbg(&client->dev,
1156 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1157 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1158
1159 input->name = tsdata->name;
1160 input->id.bustype = BUS_I2C;
1161 input->dev.parent = &client->dev;
1162
Simon Budig169110c2017-10-09 20:58:11 -07001163 if (tsdata->version == EDT_M06 ||
Simon Budigaed5d0e2017-10-09 21:00:16 -07001164 tsdata->version == EDT_M09 ||
1165 tsdata->version == EDT_M12) {
Simon Budig169110c2017-10-09 20:58:11 -07001166 input_set_abs_params(input, ABS_MT_POSITION_X,
1167 0, tsdata->num_x * 64 - 1, 0, 0);
1168 input_set_abs_params(input, ABS_MT_POSITION_Y,
1169 0, tsdata->num_y * 64 - 1, 0, 0);
1170 } else {
1171 /* Unknown maximum values. Specify via devicetree */
1172 input_set_abs_params(input, ABS_MT_POSITION_X,
1173 0, 65535, 0, 0);
1174 input_set_abs_params(input, ABS_MT_POSITION_Y,
1175 0, 65535, 0, 0);
1176 }
Maxime Ripard2c005592015-03-21 20:18:06 -07001177
Hans de Goedead368eb2016-07-15 14:26:53 -07001178 touchscreen_parse_properties(input, true, &tsdata->prop);
Maxime Ripard2c005592015-03-21 20:18:06 -07001179
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001180 error = input_mt_init_slots(input, tsdata->max_support_points,
1181 INPUT_MT_DIRECT);
Simon Budig43c4d132012-07-24 23:29:36 -07001182 if (error) {
1183 dev_err(&client->dev, "Unable to init MT slots.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001184 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001185 }
1186
Simon Budig43c4d132012-07-24 23:29:36 -07001187 i2c_set_clientdata(client, tsdata);
1188
Dmitry Torokhovf0bef752015-09-12 10:11:09 -07001189 irq_flags = irq_get_trigger_type(client->irq);
1190 if (irq_flags == IRQF_TRIGGER_NONE)
1191 irq_flags = IRQF_TRIGGER_FALLING;
1192 irq_flags |= IRQF_ONESHOT;
1193
1194 error = devm_request_threaded_irq(&client->dev, client->irq,
1195 NULL, edt_ft5x06_ts_isr, irq_flags,
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001196 client->name, tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -07001197 if (error) {
1198 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001199 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001200 }
1201
Andi Shytie3adf552017-09-29 16:42:06 -07001202 error = devm_device_add_group(&client->dev, &edt_ft5x06_attr_group);
Simon Budig43c4d132012-07-24 23:29:36 -07001203 if (error)
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001204 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001205
1206 error = input_register_device(input);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001207 if (error)
Andi Shytie3adf552017-09-29 16:42:06 -07001208 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001209
1210 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
Simon Budig43c4d132012-07-24 23:29:36 -07001211
1212 dev_dbg(&client->dev,
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001213 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
Franklin S Cooper Jr8b58cc32015-10-06 15:24:34 -07001214 client->irq,
1215 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1216 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
Simon Budig43c4d132012-07-24 23:29:36 -07001217
1218 return 0;
Simon Budig43c4d132012-07-24 23:29:36 -07001219}
1220
Bill Pembertone2619cf2012-11-23 21:50:47 -08001221static int edt_ft5x06_ts_remove(struct i2c_client *client)
Simon Budig43c4d132012-07-24 23:29:36 -07001222{
Simon Budig43c4d132012-07-24 23:29:36 -07001223 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1224
1225 edt_ft5x06_ts_teardown_debugfs(tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -07001226
Simon Budig43c4d132012-07-24 23:29:36 -07001227 return 0;
1228}
1229
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001230static const struct edt_i2c_chip_data edt_ft5x06_data = {
1231 .max_support_points = 5,
1232};
1233
Franklin S Cooper Jraf33e0a2015-10-16 15:34:26 -07001234static const struct edt_i2c_chip_data edt_ft5506_data = {
1235 .max_support_points = 10,
1236};
1237
Hans de Goeded1871652016-08-04 08:21:19 -07001238static const struct edt_i2c_chip_data edt_ft6236_data = {
1239 .max_support_points = 2,
1240};
1241
Simon Budig43c4d132012-07-24 23:29:36 -07001242static const struct i2c_device_id edt_ft5x06_ts_id[] = {
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001243 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
Franklin S Cooper Jraf33e0a2015-10-16 15:34:26 -07001244 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
Marco Felscha2f39da2019-01-13 23:08:32 -08001245 { .name = "ev-ft5726", .driver_data = (long)&edt_ft5506_data },
Hans de Goeded1871652016-08-04 08:21:19 -07001246 /* Note no edt- prefix for compatibility with the ft6236.c driver */
1247 { .name = "ft6236", .driver_data = (long)&edt_ft6236_data },
Lothar Waßmann1730d812014-03-28 09:20:08 -07001248 { /* sentinel */ }
Simon Budig43c4d132012-07-24 23:29:36 -07001249};
1250MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1251
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001252static const struct of_device_id edt_ft5x06_of_match[] = {
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001253 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1254 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1255 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
Franklin S Cooper Jraf33e0a2015-10-16 15:34:26 -07001256 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
Marco Felscha2f39da2019-01-13 23:08:32 -08001257 { .compatible = "evervision,ev-ft5726", .data = &edt_ft5506_data },
Hans de Goeded1871652016-08-04 08:21:19 -07001258 /* Note focaltech vendor prefix for compatibility with ft6236.c */
1259 { .compatible = "focaltech,ft6236", .data = &edt_ft6236_data },
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001260 { /* sentinel */ }
1261};
1262MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001263
Simon Budig43c4d132012-07-24 23:29:36 -07001264static struct i2c_driver edt_ft5x06_ts_driver = {
1265 .driver = {
Simon Budig43c4d132012-07-24 23:29:36 -07001266 .name = "edt_ft5x06",
Andy Shevchenkofc226eb2019-03-03 23:21:28 -08001267 .of_match_table = edt_ft5x06_of_match,
Simon Budig43c4d132012-07-24 23:29:36 -07001268 },
1269 .id_table = edt_ft5x06_ts_id,
1270 .probe = edt_ft5x06_ts_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001271 .remove = edt_ft5x06_ts_remove,
Simon Budig43c4d132012-07-24 23:29:36 -07001272};
1273
1274module_i2c_driver(edt_ft5x06_ts_driver);
1275
1276MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1277MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
Andy Shevchenko6d3a41ab2019-03-03 23:21:56 -08001278MODULE_LICENSE("GPL v2");