blob: 23fbe382da8b420791a1fec28962454132d4e7ef [file] [log] [blame]
Simon Budig43c4d132012-07-24 23:29:36 -07001/*
2 * Copyright (C) 2012 Simon Budig, <simon.budig@kernelconcepts.de>
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07003 * Daniel Wagener <daniel.wagener@kernelconcepts.de> (M09 firmware support)
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07004 * Lothar Waßmann <LW@KARO-electronics.de> (DT support)
Simon Budig43c4d132012-07-24 23:29:36 -07005 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * This is a driver for the EDT "Polytouch" family of touch controllers
22 * based on the FocalTech FT5x06 line of chips.
23 *
24 * Development of this driver has been sponsored by Glyn:
25 * http://www.glyn.com/Products/Displays
26 */
27
28#include <linux/module.h>
29#include <linux/ratelimit.h>
Dmitry Torokhovf0bef752015-09-12 10:11:09 -070030#include <linux/irq.h>
Simon Budig43c4d132012-07-24 23:29:36 -070031#include <linux/interrupt.h>
32#include <linux/input.h>
33#include <linux/i2c.h>
34#include <linux/uaccess.h>
35#include <linux/delay.h>
36#include <linux/debugfs.h>
37#include <linux/slab.h>
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -070038#include <linux/gpio/consumer.h>
Simon Budig43c4d132012-07-24 23:29:36 -070039#include <linux/input/mt.h>
Maxime Ripard2c005592015-03-21 20:18:06 -070040#include <linux/input/touchscreen.h>
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -070041#include <linux/of_device.h>
Simon Budig43c4d132012-07-24 23:29:36 -070042
43#define WORK_REGISTER_THRESHOLD 0x00
44#define WORK_REGISTER_REPORT_RATE 0x08
45#define WORK_REGISTER_GAIN 0x30
46#define WORK_REGISTER_OFFSET 0x31
47#define WORK_REGISTER_NUM_X 0x33
48#define WORK_REGISTER_NUM_Y 0x34
49
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070050#define M09_REGISTER_THRESHOLD 0x80
51#define M09_REGISTER_GAIN 0x92
52#define M09_REGISTER_OFFSET 0x93
53#define M09_REGISTER_NUM_X 0x94
54#define M09_REGISTER_NUM_Y 0x95
55
56#define NO_REGISTER 0xff
57
Simon Budig43c4d132012-07-24 23:29:36 -070058#define WORK_REGISTER_OPMODE 0x3c
59#define FACTORY_REGISTER_OPMODE 0x01
60
61#define TOUCH_EVENT_DOWN 0x00
62#define TOUCH_EVENT_UP 0x01
63#define TOUCH_EVENT_ON 0x02
64#define TOUCH_EVENT_RESERVED 0x03
65
66#define EDT_NAME_LEN 23
67#define EDT_SWITCH_MODE_RETRIES 10
68#define EDT_SWITCH_MODE_DELAY 5 /* msec */
69#define EDT_RAW_DATA_RETRIES 100
70#define EDT_RAW_DATA_DELAY 1 /* msec */
71
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070072enum edt_ver {
73 M06,
74 M09,
75};
76
77struct edt_reg_addr {
78 int reg_threshold;
79 int reg_report_rate;
80 int reg_gain;
81 int reg_offset;
82 int reg_num_x;
83 int reg_num_y;
84};
85
Simon Budig43c4d132012-07-24 23:29:36 -070086struct edt_ft5x06_ts_data {
87 struct i2c_client *client;
88 struct input_dev *input;
89 u16 num_x;
90 u16 num_y;
91
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -070092 struct gpio_desc *reset_gpio;
93 struct gpio_desc *wake_gpio;
Lothar Waßmanndac90dc22014-03-28 09:23:02 -070094
Simon Budig43c4d132012-07-24 23:29:36 -070095#if defined(CONFIG_DEBUG_FS)
96 struct dentry *debug_dir;
97 u8 *raw_buffer;
98 size_t raw_bufsize;
99#endif
100
101 struct mutex mutex;
102 bool factory_mode;
103 int threshold;
104 int gain;
105 int offset;
106 int report_rate;
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700107 int max_support_points;
Simon Budig43c4d132012-07-24 23:29:36 -0700108
109 char name[EDT_NAME_LEN];
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700110
111 struct edt_reg_addr reg_addr;
112 enum edt_ver version;
Simon Budig43c4d132012-07-24 23:29:36 -0700113};
114
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700115struct edt_i2c_chip_data {
116 int max_support_points;
117};
118
Simon Budig43c4d132012-07-24 23:29:36 -0700119static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
120 u16 wr_len, u8 *wr_buf,
121 u16 rd_len, u8 *rd_buf)
122{
123 struct i2c_msg wrmsg[2];
124 int i = 0;
125 int ret;
126
127 if (wr_len) {
128 wrmsg[i].addr = client->addr;
129 wrmsg[i].flags = 0;
130 wrmsg[i].len = wr_len;
131 wrmsg[i].buf = wr_buf;
132 i++;
133 }
134 if (rd_len) {
135 wrmsg[i].addr = client->addr;
136 wrmsg[i].flags = I2C_M_RD;
137 wrmsg[i].len = rd_len;
138 wrmsg[i].buf = rd_buf;
139 i++;
140 }
141
142 ret = i2c_transfer(client->adapter, wrmsg, i);
143 if (ret < 0)
144 return ret;
145 if (ret != i)
146 return -EIO;
147
148 return 0;
149}
150
151static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
152 u8 *buf, int buflen)
153{
154 int i;
155 u8 crc = 0;
156
157 for (i = 0; i < buflen - 1; i++)
158 crc ^= buf[i];
159
160 if (crc != buf[buflen-1]) {
161 dev_err_ratelimited(&tsdata->client->dev,
162 "crc error: 0x%02x expected, got 0x%02x\n",
163 crc, buf[buflen-1]);
164 return false;
165 }
166
167 return true;
168}
169
170static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
171{
172 struct edt_ft5x06_ts_data *tsdata = dev_id;
173 struct device *dev = &tsdata->client->dev;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700174 u8 cmd;
Franklin S Cooper Jr9378c022015-10-16 15:34:41 -0700175 u8 rdbuf[63];
Simon Budig43c4d132012-07-24 23:29:36 -0700176 int i, type, x, y, id;
Franklin S Cooper Jrc789f1f2015-10-16 15:34:05 -0700177 int offset, tplen, datalen, crclen;
Simon Budig43c4d132012-07-24 23:29:36 -0700178 int error;
179
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700180 switch (tsdata->version) {
181 case M06:
182 cmd = 0xf9; /* tell the controller to send touch data */
183 offset = 5; /* where the actual touch data starts */
184 tplen = 4; /* data comes in so called frames */
Franklin S Cooper Jrc789f1f2015-10-16 15:34:05 -0700185 crclen = 1; /* length of the crc data */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700186 break;
187
188 case M09:
Franklin S Cooper Jr9378c022015-10-16 15:34:41 -0700189 cmd = 0x0;
190 offset = 3;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700191 tplen = 6;
Franklin S Cooper Jrc789f1f2015-10-16 15:34:05 -0700192 crclen = 0;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700193 break;
194
195 default:
196 goto out;
197 }
198
Simon Budig43c4d132012-07-24 23:29:36 -0700199 memset(rdbuf, 0, sizeof(rdbuf));
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700200 datalen = tplen * tsdata->max_support_points + offset + crclen;
Simon Budig43c4d132012-07-24 23:29:36 -0700201
202 error = edt_ft5x06_ts_readwrite(tsdata->client,
203 sizeof(cmd), &cmd,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700204 datalen, rdbuf);
Simon Budig43c4d132012-07-24 23:29:36 -0700205 if (error) {
206 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
207 error);
208 goto out;
209 }
210
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700211 /* M09 does not send header or CRC */
212 if (tsdata->version == M06) {
213 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
214 rdbuf[2] != datalen) {
215 dev_err_ratelimited(dev,
216 "Unexpected header: %02x%02x%02x!\n",
217 rdbuf[0], rdbuf[1], rdbuf[2]);
218 goto out;
219 }
220
221 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
222 goto out;
Simon Budig43c4d132012-07-24 23:29:36 -0700223 }
224
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700225 for (i = 0; i < tsdata->max_support_points; i++) {
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700226 u8 *buf = &rdbuf[i * tplen + offset];
Simon Budig43c4d132012-07-24 23:29:36 -0700227 bool down;
228
229 type = buf[0] >> 6;
230 /* ignore Reserved events */
231 if (type == TOUCH_EVENT_RESERVED)
232 continue;
233
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700234 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
235 if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN)
Lothar Waßmannee3e9462014-03-28 09:28:17 -0700236 continue;
237
Simon Budig43c4d132012-07-24 23:29:36 -0700238 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
239 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
240 id = (buf[2] >> 4) & 0x0f;
Lothar Waßmann1730d812014-03-28 09:20:08 -0700241 down = type != TOUCH_EVENT_UP;
Simon Budig43c4d132012-07-24 23:29:36 -0700242
243 input_mt_slot(tsdata->input, id);
244 input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
245
246 if (!down)
247 continue;
248
249 input_report_abs(tsdata->input, ABS_MT_POSITION_X, x);
250 input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y);
251 }
252
253 input_mt_report_pointer_emulation(tsdata->input, true);
254 input_sync(tsdata->input);
255
256out:
257 return IRQ_HANDLED;
258}
259
260static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
261 u8 addr, u8 value)
262{
263 u8 wrbuf[4];
264
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700265 switch (tsdata->version) {
266 case M06:
267 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
268 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700269 wrbuf[2] = value;
270 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
271 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
272 wrbuf, 0, NULL);
273 case M09:
274 wrbuf[0] = addr;
275 wrbuf[1] = value;
Simon Budig43c4d132012-07-24 23:29:36 -0700276
Robert Woerlecc071ac2014-06-07 22:20:23 -0700277 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700278 wrbuf, 0, NULL);
279
280 default:
281 return -EINVAL;
282 }
Simon Budig43c4d132012-07-24 23:29:36 -0700283}
284
285static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
286 u8 addr)
287{
288 u8 wrbuf[2], rdbuf[2];
289 int error;
290
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700291 switch (tsdata->version) {
292 case M06:
293 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
294 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
295 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
Simon Budig43c4d132012-07-24 23:29:36 -0700296
Dan Carpentere2c3ecf2014-04-03 09:17:05 -0700297 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
298 rdbuf);
299 if (error)
300 return error;
Simon Budig43c4d132012-07-24 23:29:36 -0700301
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700302 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
303 dev_err(&tsdata->client->dev,
304 "crc error: 0x%02x expected, got 0x%02x\n",
305 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
306 rdbuf[1]);
307 return -EIO;
308 }
309 break;
310
311 case M09:
312 wrbuf[0] = addr;
313 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
314 wrbuf, 1, rdbuf);
315 if (error)
316 return error;
317 break;
318
319 default:
320 return -EINVAL;
Simon Budig43c4d132012-07-24 23:29:36 -0700321 }
322
323 return rdbuf[0];
324}
325
326struct edt_ft5x06_attribute {
327 struct device_attribute dattr;
328 size_t field_offset;
329 u8 limit_low;
330 u8 limit_high;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700331 u8 addr_m06;
332 u8 addr_m09;
Simon Budig43c4d132012-07-24 23:29:36 -0700333};
334
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700335#define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
336 _limit_low, _limit_high) \
Simon Budig43c4d132012-07-24 23:29:36 -0700337 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
338 .dattr = __ATTR(_field, _mode, \
339 edt_ft5x06_setting_show, \
340 edt_ft5x06_setting_store), \
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700341 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
342 .addr_m06 = _addr_m06, \
343 .addr_m09 = _addr_m09, \
Simon Budig43c4d132012-07-24 23:29:36 -0700344 .limit_low = _limit_low, \
345 .limit_high = _limit_high, \
Simon Budig43c4d132012-07-24 23:29:36 -0700346 }
347
348static ssize_t edt_ft5x06_setting_show(struct device *dev,
349 struct device_attribute *dattr,
350 char *buf)
351{
352 struct i2c_client *client = to_i2c_client(dev);
353 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
354 struct edt_ft5x06_attribute *attr =
355 container_of(dattr, struct edt_ft5x06_attribute, dattr);
Lothar Waßmann1730d812014-03-28 09:20:08 -0700356 u8 *field = (u8 *)tsdata + attr->field_offset;
Simon Budig43c4d132012-07-24 23:29:36 -0700357 int val;
358 size_t count = 0;
359 int error = 0;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700360 u8 addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700361
362 mutex_lock(&tsdata->mutex);
363
364 if (tsdata->factory_mode) {
365 error = -EIO;
366 goto out;
367 }
368
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700369 switch (tsdata->version) {
370 case M06:
371 addr = attr->addr_m06;
372 break;
373
374 case M09:
375 addr = attr->addr_m09;
376 break;
377
378 default:
379 error = -ENODEV;
Simon Budig43c4d132012-07-24 23:29:36 -0700380 goto out;
381 }
382
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700383 if (addr != NO_REGISTER) {
384 val = edt_ft5x06_register_read(tsdata, addr);
385 if (val < 0) {
386 error = val;
387 dev_err(&tsdata->client->dev,
388 "Failed to fetch attribute %s, error %d\n",
389 dattr->attr.name, error);
390 goto out;
391 }
392 } else {
393 val = *field;
394 }
395
Simon Budig43c4d132012-07-24 23:29:36 -0700396 if (val != *field) {
397 dev_warn(&tsdata->client->dev,
398 "%s: read (%d) and stored value (%d) differ\n",
399 dattr->attr.name, val, *field);
400 *field = val;
401 }
402
403 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
404out:
405 mutex_unlock(&tsdata->mutex);
406 return error ?: count;
407}
408
409static ssize_t edt_ft5x06_setting_store(struct device *dev,
410 struct device_attribute *dattr,
411 const char *buf, size_t count)
412{
413 struct i2c_client *client = to_i2c_client(dev);
414 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
415 struct edt_ft5x06_attribute *attr =
416 container_of(dattr, struct edt_ft5x06_attribute, dattr);
Lothar Waßmann1730d812014-03-28 09:20:08 -0700417 u8 *field = (u8 *)tsdata + attr->field_offset;
Simon Budig43c4d132012-07-24 23:29:36 -0700418 unsigned int val;
419 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700420 u8 addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700421
422 mutex_lock(&tsdata->mutex);
423
424 if (tsdata->factory_mode) {
425 error = -EIO;
426 goto out;
427 }
428
429 error = kstrtouint(buf, 0, &val);
430 if (error)
431 goto out;
432
433 if (val < attr->limit_low || val > attr->limit_high) {
434 error = -ERANGE;
435 goto out;
436 }
437
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700438 switch (tsdata->version) {
439 case M06:
440 addr = attr->addr_m06;
441 break;
442
443 case M09:
444 addr = attr->addr_m09;
445 break;
446
447 default:
448 error = -ENODEV;
Simon Budig43c4d132012-07-24 23:29:36 -0700449 goto out;
450 }
451
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700452 if (addr != NO_REGISTER) {
453 error = edt_ft5x06_register_write(tsdata, addr, val);
454 if (error) {
455 dev_err(&tsdata->client->dev,
456 "Failed to update attribute %s, error: %d\n",
457 dattr->attr.name, error);
458 goto out;
459 }
460 }
Simon Budig43c4d132012-07-24 23:29:36 -0700461 *field = val;
462
463out:
464 mutex_unlock(&tsdata->mutex);
465 return error ?: count;
466}
467
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700468static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
469 M09_REGISTER_GAIN, 0, 31);
470static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
471 M09_REGISTER_OFFSET, 0, 31);
472static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
473 M09_REGISTER_THRESHOLD, 20, 80);
474static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
475 NO_REGISTER, 3, 14);
Simon Budig43c4d132012-07-24 23:29:36 -0700476
477static struct attribute *edt_ft5x06_attrs[] = {
478 &edt_ft5x06_attr_gain.dattr.attr,
479 &edt_ft5x06_attr_offset.dattr.attr,
480 &edt_ft5x06_attr_threshold.dattr.attr,
481 &edt_ft5x06_attr_report_rate.dattr.attr,
482 NULL
483};
484
485static const struct attribute_group edt_ft5x06_attr_group = {
486 .attrs = edt_ft5x06_attrs,
487};
488
489#ifdef CONFIG_DEBUG_FS
490static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
491{
492 struct i2c_client *client = tsdata->client;
493 int retries = EDT_SWITCH_MODE_RETRIES;
494 int ret;
495 int error;
496
497 disable_irq(client->irq);
498
499 if (!tsdata->raw_buffer) {
500 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
501 sizeof(u16);
502 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
503 if (!tsdata->raw_buffer) {
504 error = -ENOMEM;
505 goto err_out;
506 }
507 }
508
509 /* mode register is 0x3c when in the work mode */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700510 if (tsdata->version == M09)
511 goto m09_out;
512
Simon Budig43c4d132012-07-24 23:29:36 -0700513 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
514 if (error) {
515 dev_err(&client->dev,
516 "failed to switch to factory mode, error %d\n", error);
517 goto err_out;
518 }
519
520 tsdata->factory_mode = true;
521 do {
522 mdelay(EDT_SWITCH_MODE_DELAY);
523 /* mode register is 0x01 when in factory mode */
524 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
525 if (ret == 0x03)
526 break;
527 } while (--retries > 0);
528
529 if (retries == 0) {
530 dev_err(&client->dev, "not in factory mode after %dms.\n",
531 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
532 error = -EIO;
533 goto err_out;
534 }
535
536 return 0;
537
538err_out:
539 kfree(tsdata->raw_buffer);
540 tsdata->raw_buffer = NULL;
541 tsdata->factory_mode = false;
542 enable_irq(client->irq);
543
544 return error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700545
546m09_out:
547 dev_err(&client->dev, "No factory mode support for M09\n");
548 return -EINVAL;
549
Simon Budig43c4d132012-07-24 23:29:36 -0700550}
551
552static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
553{
554 struct i2c_client *client = tsdata->client;
555 int retries = EDT_SWITCH_MODE_RETRIES;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700556 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700557 int ret;
558 int error;
559
560 /* mode register is 0x01 when in the factory mode */
561 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
562 if (error) {
563 dev_err(&client->dev,
564 "failed to switch to work mode, error: %d\n", error);
565 return error;
566 }
567
568 tsdata->factory_mode = false;
569
570 do {
571 mdelay(EDT_SWITCH_MODE_DELAY);
572 /* mode register is 0x01 when in factory mode */
573 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
574 if (ret == 0x01)
575 break;
576 } while (--retries > 0);
577
578 if (retries == 0) {
579 dev_err(&client->dev, "not in work mode after %dms.\n",
580 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
581 tsdata->factory_mode = true;
582 return -EIO;
583 }
584
Sachin Kamat8efcc502013-03-28 01:14:42 -0700585 kfree(tsdata->raw_buffer);
Simon Budig43c4d132012-07-24 23:29:36 -0700586 tsdata->raw_buffer = NULL;
587
588 /* restore parameters */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700589 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
Simon Budig43c4d132012-07-24 23:29:36 -0700590 tsdata->threshold);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700591 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
Simon Budig43c4d132012-07-24 23:29:36 -0700592 tsdata->gain);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700593 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
Simon Budig43c4d132012-07-24 23:29:36 -0700594 tsdata->offset);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700595 if (reg_addr->reg_report_rate)
596 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
Simon Budig43c4d132012-07-24 23:29:36 -0700597 tsdata->report_rate);
598
599 enable_irq(client->irq);
600
601 return 0;
602}
603
604static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
605{
606 struct edt_ft5x06_ts_data *tsdata = data;
607
608 *mode = tsdata->factory_mode;
609
610 return 0;
611};
612
613static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
614{
615 struct edt_ft5x06_ts_data *tsdata = data;
616 int retval = 0;
617
618 if (mode > 1)
619 return -ERANGE;
620
621 mutex_lock(&tsdata->mutex);
622
623 if (mode != tsdata->factory_mode) {
624 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
Lothar Waßmann1730d812014-03-28 09:20:08 -0700625 edt_ft5x06_work_mode(tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -0700626 }
627
628 mutex_unlock(&tsdata->mutex);
629
630 return retval;
631};
632
633DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
634 edt_ft5x06_debugfs_mode_set, "%llu\n");
635
Simon Budig43c4d132012-07-24 23:29:36 -0700636static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
637 char __user *buf, size_t count, loff_t *off)
638{
639 struct edt_ft5x06_ts_data *tsdata = file->private_data;
640 struct i2c_client *client = tsdata->client;
641 int retries = EDT_RAW_DATA_RETRIES;
642 int val, i, error;
643 size_t read = 0;
644 int colbytes;
645 char wrbuf[3];
646 u8 *rdbuf;
647
648 if (*off < 0 || *off >= tsdata->raw_bufsize)
649 return 0;
650
651 mutex_lock(&tsdata->mutex);
652
653 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
654 error = -EIO;
655 goto out;
656 }
657
658 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
659 if (error) {
660 dev_dbg(&client->dev,
661 "failed to write 0x08 register, error %d\n", error);
662 goto out;
663 }
664
665 do {
666 msleep(EDT_RAW_DATA_DELAY);
667 val = edt_ft5x06_register_read(tsdata, 0x08);
668 if (val < 1)
669 break;
670 } while (--retries > 0);
671
672 if (val < 0) {
673 error = val;
674 dev_dbg(&client->dev,
675 "failed to read 0x08 register, error %d\n", error);
676 goto out;
677 }
678
679 if (retries == 0) {
680 dev_dbg(&client->dev,
681 "timed out waiting for register to settle\n");
682 error = -ETIMEDOUT;
683 goto out;
684 }
685
686 rdbuf = tsdata->raw_buffer;
687 colbytes = tsdata->num_y * sizeof(u16);
688
689 wrbuf[0] = 0xf5;
690 wrbuf[1] = 0x0e;
691 for (i = 0; i < tsdata->num_x; i++) {
692 wrbuf[2] = i; /* column index */
693 error = edt_ft5x06_ts_readwrite(tsdata->client,
694 sizeof(wrbuf), wrbuf,
695 colbytes, rdbuf);
696 if (error)
697 goto out;
698
699 rdbuf += colbytes;
700 }
701
702 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
Axel Lin35b1da42012-09-19 15:56:23 -0700703 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
704 error = -EFAULT;
705 goto out;
706 }
707
708 *off += read;
Simon Budig43c4d132012-07-24 23:29:36 -0700709out:
710 mutex_unlock(&tsdata->mutex);
711 return error ?: read;
712};
713
Simon Budig43c4d132012-07-24 23:29:36 -0700714static const struct file_operations debugfs_raw_data_fops = {
Wei Yongjunf6c0df62012-10-17 23:55:54 -0700715 .open = simple_open,
Simon Budig43c4d132012-07-24 23:29:36 -0700716 .read = edt_ft5x06_debugfs_raw_data_read,
717};
718
Bill Pemberton5298cc42012-11-23 21:38:25 -0800719static void
Simon Budig43c4d132012-07-24 23:29:36 -0700720edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
721 const char *debugfs_name)
722{
723 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
724 if (!tsdata->debug_dir)
725 return;
726
727 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
728 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
729
730 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
731 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
732 debugfs_create_file("raw_data", S_IRUSR,
733 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
734}
735
Bill Pembertone2619cf2012-11-23 21:50:47 -0800736static void
Simon Budig43c4d132012-07-24 23:29:36 -0700737edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
738{
Fabian Frederickf69c6ec2014-07-09 09:45:57 -0700739 debugfs_remove_recursive(tsdata->debug_dir);
Guenter Roecka1d0fa72012-08-21 22:01:45 -0700740 kfree(tsdata->raw_buffer);
Simon Budig43c4d132012-07-24 23:29:36 -0700741}
742
743#else
744
745static inline void
746edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
747 const char *debugfs_name)
748{
749}
750
751static inline void
752edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
753{
754}
755
756#endif /* CONFIG_DEBUGFS */
757
Bill Pemberton5298cc42012-11-23 21:38:25 -0800758static int edt_ft5x06_ts_identify(struct i2c_client *client,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700759 struct edt_ft5x06_ts_data *tsdata,
760 char *fw_version)
Simon Budig43c4d132012-07-24 23:29:36 -0700761{
762 u8 rdbuf[EDT_NAME_LEN];
763 char *p;
764 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700765 char *model_name = tsdata->name;
Simon Budig43c4d132012-07-24 23:29:36 -0700766
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700767 /* see what we find if we assume it is a M06 *
768 * if we get less than EDT_NAME_LEN, we don't want
769 * to have garbage in there
770 */
771 memset(rdbuf, 0, sizeof(rdbuf));
Simon Budig43c4d132012-07-24 23:29:36 -0700772 error = edt_ft5x06_ts_readwrite(client, 1, "\xbb",
773 EDT_NAME_LEN - 1, rdbuf);
774 if (error)
775 return error;
776
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700777 /* if we find something consistent, stay with that assumption
778 * at least M09 won't send 3 bytes here
779 */
Rasmus Villemoes0f3ae5b2014-10-13 15:54:48 -0700780 if (!(strncasecmp(rdbuf + 1, "EP0", 3))) {
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700781 tsdata->version = M06;
Simon Budig43c4d132012-07-24 23:29:36 -0700782
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700783 /* remove last '$' end marker */
784 rdbuf[EDT_NAME_LEN - 1] = '\0';
785 if (rdbuf[EDT_NAME_LEN - 2] == '$')
786 rdbuf[EDT_NAME_LEN - 2] = '\0';
Simon Budig43c4d132012-07-24 23:29:36 -0700787
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700788 /* look for Model/Version separator */
789 p = strchr(rdbuf, '*');
790 if (p)
791 *p++ = '\0';
792 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
793 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
794 } else {
795 /* since there are only two versions around (M06, M09) */
796 tsdata->version = M09;
797
798 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
799 2, rdbuf);
800 if (error)
801 return error;
802
803 strlcpy(fw_version, rdbuf, 2);
804
805 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
806 1, rdbuf);
807 if (error)
808 return error;
809
810 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
811 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
812 }
Simon Budig43c4d132012-07-24 23:29:36 -0700813
814 return 0;
815}
816
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700817static void edt_ft5x06_ts_get_defaults(struct device *dev,
Dmitry Torokhov22ddbac2015-09-12 09:37:03 -0700818 struct edt_ft5x06_ts_data *tsdata)
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700819{
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700820 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700821 u32 val;
822 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700823
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700824 error = device_property_read_u32(dev, "threshold", &val);
Philipp Zabeldc262df2016-02-09 09:32:42 -0800825 if (!error) {
826 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold, val);
827 tsdata->threshold = val;
828 }
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700829
830 error = device_property_read_u32(dev, "gain", &val);
Philipp Zabeldc262df2016-02-09 09:32:42 -0800831 if (!error) {
832 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain, val);
833 tsdata->gain = val;
834 }
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700835
836 error = device_property_read_u32(dev, "offset", &val);
Philipp Zabeldc262df2016-02-09 09:32:42 -0800837 if (!error) {
838 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset, val);
839 tsdata->offset = val;
840 }
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700841}
842
Bill Pemberton5298cc42012-11-23 21:38:25 -0800843static void
Simon Budig43c4d132012-07-24 23:29:36 -0700844edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
845{
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700846 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
847
Simon Budig43c4d132012-07-24 23:29:36 -0700848 tsdata->threshold = edt_ft5x06_register_read(tsdata,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700849 reg_addr->reg_threshold);
850 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
851 tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
852 if (reg_addr->reg_report_rate != NO_REGISTER)
853 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
854 reg_addr->reg_report_rate);
855 tsdata->num_x = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_x);
856 tsdata->num_y = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_y);
857}
858
859static void
860edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
861{
862 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
863
864 switch (tsdata->version) {
865 case M06:
866 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
867 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
868 reg_addr->reg_gain = WORK_REGISTER_GAIN;
869 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
870 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
871 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
872 break;
873
874 case M09:
875 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
876 reg_addr->reg_gain = M09_REGISTER_GAIN;
877 reg_addr->reg_offset = M09_REGISTER_OFFSET;
878 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
879 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
880 break;
881 }
Simon Budig43c4d132012-07-24 23:29:36 -0700882}
883
Bill Pemberton5298cc42012-11-23 21:38:25 -0800884static int edt_ft5x06_ts_probe(struct i2c_client *client,
Simon Budig43c4d132012-07-24 23:29:36 -0700885 const struct i2c_device_id *id)
886{
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700887 const struct edt_i2c_chip_data *chip_data;
Simon Budig43c4d132012-07-24 23:29:36 -0700888 struct edt_ft5x06_ts_data *tsdata;
889 struct input_dev *input;
Dmitry Torokhovf0bef752015-09-12 10:11:09 -0700890 unsigned long irq_flags;
Simon Budig43c4d132012-07-24 23:29:36 -0700891 int error;
892 char fw_version[EDT_NAME_LEN];
893
894 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
895
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800896 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
897 if (!tsdata) {
Simon Budig43c4d132012-07-24 23:29:36 -0700898 dev_err(&client->dev, "failed to allocate driver data.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800899 return -ENOMEM;
900 }
901
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700902 chip_data = of_device_get_match_data(&client->dev);
903 if (!chip_data)
904 chip_data = (const struct edt_i2c_chip_data *)id->driver_data;
905 if (!chip_data || !chip_data->max_support_points) {
906 dev_err(&client->dev, "invalid or missing chip data\n");
907 return -EINVAL;
908 }
909
910 tsdata->max_support_points = chip_data->max_support_points;
911
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -0700912 tsdata->reset_gpio = devm_gpiod_get_optional(&client->dev,
913 "reset", GPIOD_OUT_HIGH);
914 if (IS_ERR(tsdata->reset_gpio)) {
915 error = PTR_ERR(tsdata->reset_gpio);
916 dev_err(&client->dev,
917 "Failed to request GPIO reset pin, error %d\n", error);
918 return error;
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700919 }
920
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -0700921 tsdata->wake_gpio = devm_gpiod_get_optional(&client->dev,
922 "wake", GPIOD_OUT_LOW);
923 if (IS_ERR(tsdata->wake_gpio)) {
924 error = PTR_ERR(tsdata->wake_gpio);
925 dev_err(&client->dev,
926 "Failed to request GPIO wake pin, error %d\n", error);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700927 return error;
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -0700928 }
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700929
Franklin S Cooper Jr13c23cd2015-09-11 17:30:34 -0700930 if (tsdata->wake_gpio) {
931 usleep_range(5000, 6000);
932 gpiod_set_value_cansleep(tsdata->wake_gpio, 1);
933 }
934
935 if (tsdata->reset_gpio) {
936 usleep_range(5000, 6000);
937 gpiod_set_value_cansleep(tsdata->reset_gpio, 0);
938 msleep(300);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700939 }
940
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800941 input = devm_input_allocate_device(&client->dev);
942 if (!input) {
943 dev_err(&client->dev, "failed to allocate input device.\n");
944 return -ENOMEM;
Simon Budig43c4d132012-07-24 23:29:36 -0700945 }
946
947 mutex_init(&tsdata->mutex);
948 tsdata->client = client;
949 tsdata->input = input;
950 tsdata->factory_mode = false;
951
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700952 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
Simon Budig43c4d132012-07-24 23:29:36 -0700953 if (error) {
954 dev_err(&client->dev, "touchscreen probe failed\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800955 return error;
Simon Budig43c4d132012-07-24 23:29:36 -0700956 }
957
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700958 edt_ft5x06_ts_set_regs(tsdata);
Dmitry Torokhov2e23b7a2015-09-12 09:54:15 -0700959 edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -0700960 edt_ft5x06_ts_get_parameters(tsdata);
961
962 dev_dbg(&client->dev,
963 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
964 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
965
966 input->name = tsdata->name;
967 input->id.bustype = BUS_I2C;
968 input->dev.parent = &client->dev;
969
Simon Budig43c4d132012-07-24 23:29:36 -0700970 input_set_abs_params(input, ABS_MT_POSITION_X,
971 0, tsdata->num_x * 64 - 1, 0, 0);
972 input_set_abs_params(input, ABS_MT_POSITION_Y,
973 0, tsdata->num_y * 64 - 1, 0, 0);
Maxime Ripard2c005592015-03-21 20:18:06 -0700974
Dmitry Torokhov22ddbac2015-09-12 09:37:03 -0700975 touchscreen_parse_properties(input, true);
Maxime Ripard2c005592015-03-21 20:18:06 -0700976
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -0700977 error = input_mt_init_slots(input, tsdata->max_support_points,
978 INPUT_MT_DIRECT);
Simon Budig43c4d132012-07-24 23:29:36 -0700979 if (error) {
980 dev_err(&client->dev, "Unable to init MT slots.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800981 return error;
Simon Budig43c4d132012-07-24 23:29:36 -0700982 }
983
984 input_set_drvdata(input, tsdata);
985 i2c_set_clientdata(client, tsdata);
986
Dmitry Torokhovf0bef752015-09-12 10:11:09 -0700987 irq_flags = irq_get_trigger_type(client->irq);
988 if (irq_flags == IRQF_TRIGGER_NONE)
989 irq_flags = IRQF_TRIGGER_FALLING;
990 irq_flags |= IRQF_ONESHOT;
991
992 error = devm_request_threaded_irq(&client->dev, client->irq,
993 NULL, edt_ft5x06_ts_isr, irq_flags,
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700994 client->name, tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -0700995 if (error) {
996 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800997 return error;
Simon Budig43c4d132012-07-24 23:29:36 -0700998 }
999
1000 error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1001 if (error)
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001002 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001003
1004 error = input_register_device(input);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001005 if (error)
1006 goto err_remove_attrs;
Simon Budig43c4d132012-07-24 23:29:36 -07001007
1008 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1009 device_init_wakeup(&client->dev, 1);
1010
1011 dev_dbg(&client->dev,
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001012 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
Franklin S Cooper Jr8b58cc32015-10-06 15:24:34 -07001013 client->irq,
1014 tsdata->wake_gpio ? desc_to_gpio(tsdata->wake_gpio) : -1,
1015 tsdata->reset_gpio ? desc_to_gpio(tsdata->reset_gpio) : -1);
Simon Budig43c4d132012-07-24 23:29:36 -07001016
1017 return 0;
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001018
1019err_remove_attrs:
1020 sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1021 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001022}
1023
Bill Pembertone2619cf2012-11-23 21:50:47 -08001024static int edt_ft5x06_ts_remove(struct i2c_client *client)
Simon Budig43c4d132012-07-24 23:29:36 -07001025{
Simon Budig43c4d132012-07-24 23:29:36 -07001026 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1027
1028 edt_ft5x06_ts_teardown_debugfs(tsdata);
1029 sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1030
Simon Budig43c4d132012-07-24 23:29:36 -07001031 return 0;
1032}
1033
Jingoo Han02b6a582014-11-02 00:04:14 -07001034static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
Simon Budig43c4d132012-07-24 23:29:36 -07001035{
1036 struct i2c_client *client = to_i2c_client(dev);
1037
1038 if (device_may_wakeup(dev))
1039 enable_irq_wake(client->irq);
1040
1041 return 0;
1042}
1043
Jingoo Han02b6a582014-11-02 00:04:14 -07001044static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
Simon Budig43c4d132012-07-24 23:29:36 -07001045{
1046 struct i2c_client *client = to_i2c_client(dev);
1047
1048 if (device_may_wakeup(dev))
1049 disable_irq_wake(client->irq);
1050
1051 return 0;
1052}
Simon Budig43c4d132012-07-24 23:29:36 -07001053
1054static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1055 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1056
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001057static const struct edt_i2c_chip_data edt_ft5x06_data = {
1058 .max_support_points = 5,
1059};
1060
Franklin S Cooper Jraf33e0a2015-10-16 15:34:26 -07001061static const struct edt_i2c_chip_data edt_ft5506_data = {
1062 .max_support_points = 10,
1063};
1064
Simon Budig43c4d132012-07-24 23:29:36 -07001065static const struct i2c_device_id edt_ft5x06_ts_id[] = {
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001066 { .name = "edt-ft5x06", .driver_data = (long)&edt_ft5x06_data },
Franklin S Cooper Jraf33e0a2015-10-16 15:34:26 -07001067 { .name = "edt-ft5506", .driver_data = (long)&edt_ft5506_data },
Lothar Waßmann1730d812014-03-28 09:20:08 -07001068 { /* sentinel */ }
Simon Budig43c4d132012-07-24 23:29:36 -07001069};
1070MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1071
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001072#ifdef CONFIG_OF
1073static const struct of_device_id edt_ft5x06_of_match[] = {
Franklin S Cooper Jrb1d2a3e2015-10-16 15:34:16 -07001074 { .compatible = "edt,edt-ft5206", .data = &edt_ft5x06_data },
1075 { .compatible = "edt,edt-ft5306", .data = &edt_ft5x06_data },
1076 { .compatible = "edt,edt-ft5406", .data = &edt_ft5x06_data },
Franklin S Cooper Jraf33e0a2015-10-16 15:34:26 -07001077 { .compatible = "edt,edt-ft5506", .data = &edt_ft5506_data },
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001078 { /* sentinel */ }
1079};
1080MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1081#endif
1082
Simon Budig43c4d132012-07-24 23:29:36 -07001083static struct i2c_driver edt_ft5x06_ts_driver = {
1084 .driver = {
Simon Budig43c4d132012-07-24 23:29:36 -07001085 .name = "edt_ft5x06",
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001086 .of_match_table = of_match_ptr(edt_ft5x06_of_match),
Simon Budig43c4d132012-07-24 23:29:36 -07001087 .pm = &edt_ft5x06_ts_pm_ops,
1088 },
1089 .id_table = edt_ft5x06_ts_id,
1090 .probe = edt_ft5x06_ts_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001091 .remove = edt_ft5x06_ts_remove,
Simon Budig43c4d132012-07-24 23:29:36 -07001092};
1093
1094module_i2c_driver(edt_ft5x06_ts_driver);
1095
1096MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1097MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1098MODULE_LICENSE("GPL");