blob: cd8a7472266b99fa9edfcc584f35160894a21228 [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>
30#include <linux/interrupt.h>
31#include <linux/input.h>
32#include <linux/i2c.h>
33#include <linux/uaccess.h>
34#include <linux/delay.h>
35#include <linux/debugfs.h>
36#include <linux/slab.h>
37#include <linux/gpio.h>
Lothar Waßmanndac90dc22014-03-28 09:23:02 -070038#include <linux/of_gpio.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>
Simon Budig43c4d132012-07-24 23:29:36 -070041#include <linux/input/edt-ft5x06.h>
42
43#define MAX_SUPPORT_POINTS 5
44
45#define WORK_REGISTER_THRESHOLD 0x00
46#define WORK_REGISTER_REPORT_RATE 0x08
47#define WORK_REGISTER_GAIN 0x30
48#define WORK_REGISTER_OFFSET 0x31
49#define WORK_REGISTER_NUM_X 0x33
50#define WORK_REGISTER_NUM_Y 0x34
51
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070052#define M09_REGISTER_THRESHOLD 0x80
53#define M09_REGISTER_GAIN 0x92
54#define M09_REGISTER_OFFSET 0x93
55#define M09_REGISTER_NUM_X 0x94
56#define M09_REGISTER_NUM_Y 0x95
57
58#define NO_REGISTER 0xff
59
Simon Budig43c4d132012-07-24 23:29:36 -070060#define WORK_REGISTER_OPMODE 0x3c
61#define FACTORY_REGISTER_OPMODE 0x01
62
63#define TOUCH_EVENT_DOWN 0x00
64#define TOUCH_EVENT_UP 0x01
65#define TOUCH_EVENT_ON 0x02
66#define TOUCH_EVENT_RESERVED 0x03
67
68#define EDT_NAME_LEN 23
69#define EDT_SWITCH_MODE_RETRIES 10
70#define EDT_SWITCH_MODE_DELAY 5 /* msec */
71#define EDT_RAW_DATA_RETRIES 100
72#define EDT_RAW_DATA_DELAY 1 /* msec */
73
Lothar Waßmannfd335ab2014-03-28 09:31:14 -070074enum edt_ver {
75 M06,
76 M09,
77};
78
79struct edt_reg_addr {
80 int reg_threshold;
81 int reg_report_rate;
82 int reg_gain;
83 int reg_offset;
84 int reg_num_x;
85 int reg_num_y;
86};
87
Simon Budig43c4d132012-07-24 23:29:36 -070088struct edt_ft5x06_ts_data {
89 struct i2c_client *client;
90 struct input_dev *input;
91 u16 num_x;
92 u16 num_y;
93
Lothar Waßmanndac90dc22014-03-28 09:23:02 -070094 int reset_pin;
95 int irq_pin;
96 int wake_pin;
97
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;
109 int report_rate;
110
111 char name[EDT_NAME_LEN];
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700112
113 struct edt_reg_addr reg_addr;
114 enum edt_ver version;
Simon Budig43c4d132012-07-24 23:29:36 -0700115};
116
117static int edt_ft5x06_ts_readwrite(struct i2c_client *client,
118 u16 wr_len, u8 *wr_buf,
119 u16 rd_len, u8 *rd_buf)
120{
121 struct i2c_msg wrmsg[2];
122 int i = 0;
123 int ret;
124
125 if (wr_len) {
126 wrmsg[i].addr = client->addr;
127 wrmsg[i].flags = 0;
128 wrmsg[i].len = wr_len;
129 wrmsg[i].buf = wr_buf;
130 i++;
131 }
132 if (rd_len) {
133 wrmsg[i].addr = client->addr;
134 wrmsg[i].flags = I2C_M_RD;
135 wrmsg[i].len = rd_len;
136 wrmsg[i].buf = rd_buf;
137 i++;
138 }
139
140 ret = i2c_transfer(client->adapter, wrmsg, i);
141 if (ret < 0)
142 return ret;
143 if (ret != i)
144 return -EIO;
145
146 return 0;
147}
148
149static bool edt_ft5x06_ts_check_crc(struct edt_ft5x06_ts_data *tsdata,
150 u8 *buf, int buflen)
151{
152 int i;
153 u8 crc = 0;
154
155 for (i = 0; i < buflen - 1; i++)
156 crc ^= buf[i];
157
158 if (crc != buf[buflen-1]) {
159 dev_err_ratelimited(&tsdata->client->dev,
160 "crc error: 0x%02x expected, got 0x%02x\n",
161 crc, buf[buflen-1]);
162 return false;
163 }
164
165 return true;
166}
167
168static irqreturn_t edt_ft5x06_ts_isr(int irq, void *dev_id)
169{
170 struct edt_ft5x06_ts_data *tsdata = dev_id;
171 struct device *dev = &tsdata->client->dev;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700172 u8 cmd;
173 u8 rdbuf[29];
Simon Budig43c4d132012-07-24 23:29:36 -0700174 int i, type, x, y, id;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700175 int offset, tplen, datalen;
Simon Budig43c4d132012-07-24 23:29:36 -0700176 int error;
177
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700178 switch (tsdata->version) {
179 case M06:
180 cmd = 0xf9; /* tell the controller to send touch data */
181 offset = 5; /* where the actual touch data starts */
182 tplen = 4; /* data comes in so called frames */
183 datalen = 26; /* how much bytes to listen for */
184 break;
185
186 case M09:
187 cmd = 0x02;
188 offset = 1;
189 tplen = 6;
190 datalen = 29;
191 break;
192
193 default:
194 goto out;
195 }
196
Simon Budig43c4d132012-07-24 23:29:36 -0700197 memset(rdbuf, 0, sizeof(rdbuf));
198
199 error = edt_ft5x06_ts_readwrite(tsdata->client,
200 sizeof(cmd), &cmd,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700201 datalen, rdbuf);
Simon Budig43c4d132012-07-24 23:29:36 -0700202 if (error) {
203 dev_err_ratelimited(dev, "Unable to fetch data, error: %d\n",
204 error);
205 goto out;
206 }
207
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700208 /* M09 does not send header or CRC */
209 if (tsdata->version == M06) {
210 if (rdbuf[0] != 0xaa || rdbuf[1] != 0xaa ||
211 rdbuf[2] != datalen) {
212 dev_err_ratelimited(dev,
213 "Unexpected header: %02x%02x%02x!\n",
214 rdbuf[0], rdbuf[1], rdbuf[2]);
215 goto out;
216 }
217
218 if (!edt_ft5x06_ts_check_crc(tsdata, rdbuf, datalen))
219 goto out;
Simon Budig43c4d132012-07-24 23:29:36 -0700220 }
221
Simon Budig43c4d132012-07-24 23:29:36 -0700222 for (i = 0; i < MAX_SUPPORT_POINTS; i++) {
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700223 u8 *buf = &rdbuf[i * tplen + offset];
Simon Budig43c4d132012-07-24 23:29:36 -0700224 bool down;
225
226 type = buf[0] >> 6;
227 /* ignore Reserved events */
228 if (type == TOUCH_EVENT_RESERVED)
229 continue;
230
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700231 /* M06 sometimes sends bogus coordinates in TOUCH_DOWN */
232 if (tsdata->version == M06 && type == TOUCH_EVENT_DOWN)
Lothar Waßmannee3e9462014-03-28 09:28:17 -0700233 continue;
234
Simon Budig43c4d132012-07-24 23:29:36 -0700235 x = ((buf[0] << 8) | buf[1]) & 0x0fff;
236 y = ((buf[2] << 8) | buf[3]) & 0x0fff;
237 id = (buf[2] >> 4) & 0x0f;
Lothar Waßmann1730d812014-03-28 09:20:08 -0700238 down = type != TOUCH_EVENT_UP;
Simon Budig43c4d132012-07-24 23:29:36 -0700239
240 input_mt_slot(tsdata->input, id);
241 input_mt_report_slot_state(tsdata->input, MT_TOOL_FINGER, down);
242
243 if (!down)
244 continue;
245
246 input_report_abs(tsdata->input, ABS_MT_POSITION_X, x);
247 input_report_abs(tsdata->input, ABS_MT_POSITION_Y, y);
248 }
249
250 input_mt_report_pointer_emulation(tsdata->input, true);
251 input_sync(tsdata->input);
252
253out:
254 return IRQ_HANDLED;
255}
256
257static int edt_ft5x06_register_write(struct edt_ft5x06_ts_data *tsdata,
258 u8 addr, u8 value)
259{
260 u8 wrbuf[4];
261
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700262 switch (tsdata->version) {
263 case M06:
264 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
265 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700266 wrbuf[2] = value;
267 wrbuf[3] = wrbuf[0] ^ wrbuf[1] ^ wrbuf[2];
268 return edt_ft5x06_ts_readwrite(tsdata->client, 4,
269 wrbuf, 0, NULL);
270 case M09:
271 wrbuf[0] = addr;
272 wrbuf[1] = value;
Simon Budig43c4d132012-07-24 23:29:36 -0700273
Robert Woerlecc071ac2014-06-07 22:20:23 -0700274 return edt_ft5x06_ts_readwrite(tsdata->client, 2,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700275 wrbuf, 0, NULL);
276
277 default:
278 return -EINVAL;
279 }
Simon Budig43c4d132012-07-24 23:29:36 -0700280}
281
282static int edt_ft5x06_register_read(struct edt_ft5x06_ts_data *tsdata,
283 u8 addr)
284{
285 u8 wrbuf[2], rdbuf[2];
286 int error;
287
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700288 switch (tsdata->version) {
289 case M06:
290 wrbuf[0] = tsdata->factory_mode ? 0xf3 : 0xfc;
291 wrbuf[1] = tsdata->factory_mode ? addr & 0x7f : addr & 0x3f;
292 wrbuf[1] |= tsdata->factory_mode ? 0x80 : 0x40;
Simon Budig43c4d132012-07-24 23:29:36 -0700293
Dan Carpentere2c3ecf2014-04-03 09:17:05 -0700294 error = edt_ft5x06_ts_readwrite(tsdata->client, 2, wrbuf, 2,
295 rdbuf);
296 if (error)
297 return error;
Simon Budig43c4d132012-07-24 23:29:36 -0700298
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700299 if ((wrbuf[0] ^ wrbuf[1] ^ rdbuf[0]) != rdbuf[1]) {
300 dev_err(&tsdata->client->dev,
301 "crc error: 0x%02x expected, got 0x%02x\n",
302 wrbuf[0] ^ wrbuf[1] ^ rdbuf[0],
303 rdbuf[1]);
304 return -EIO;
305 }
306 break;
307
308 case M09:
309 wrbuf[0] = addr;
310 error = edt_ft5x06_ts_readwrite(tsdata->client, 1,
311 wrbuf, 1, rdbuf);
312 if (error)
313 return error;
314 break;
315
316 default:
317 return -EINVAL;
Simon Budig43c4d132012-07-24 23:29:36 -0700318 }
319
320 return rdbuf[0];
321}
322
323struct edt_ft5x06_attribute {
324 struct device_attribute dattr;
325 size_t field_offset;
326 u8 limit_low;
327 u8 limit_high;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700328 u8 addr_m06;
329 u8 addr_m09;
Simon Budig43c4d132012-07-24 23:29:36 -0700330};
331
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700332#define EDT_ATTR(_field, _mode, _addr_m06, _addr_m09, \
333 _limit_low, _limit_high) \
Simon Budig43c4d132012-07-24 23:29:36 -0700334 struct edt_ft5x06_attribute edt_ft5x06_attr_##_field = { \
335 .dattr = __ATTR(_field, _mode, \
336 edt_ft5x06_setting_show, \
337 edt_ft5x06_setting_store), \
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700338 .field_offset = offsetof(struct edt_ft5x06_ts_data, _field), \
339 .addr_m06 = _addr_m06, \
340 .addr_m09 = _addr_m09, \
Simon Budig43c4d132012-07-24 23:29:36 -0700341 .limit_low = _limit_low, \
342 .limit_high = _limit_high, \
Simon Budig43c4d132012-07-24 23:29:36 -0700343 }
344
345static ssize_t edt_ft5x06_setting_show(struct device *dev,
346 struct device_attribute *dattr,
347 char *buf)
348{
349 struct i2c_client *client = to_i2c_client(dev);
350 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
351 struct edt_ft5x06_attribute *attr =
352 container_of(dattr, struct edt_ft5x06_attribute, dattr);
Lothar Waßmann1730d812014-03-28 09:20:08 -0700353 u8 *field = (u8 *)tsdata + attr->field_offset;
Simon Budig43c4d132012-07-24 23:29:36 -0700354 int val;
355 size_t count = 0;
356 int error = 0;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700357 u8 addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700358
359 mutex_lock(&tsdata->mutex);
360
361 if (tsdata->factory_mode) {
362 error = -EIO;
363 goto out;
364 }
365
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700366 switch (tsdata->version) {
367 case M06:
368 addr = attr->addr_m06;
369 break;
370
371 case M09:
372 addr = attr->addr_m09;
373 break;
374
375 default:
376 error = -ENODEV;
Simon Budig43c4d132012-07-24 23:29:36 -0700377 goto out;
378 }
379
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700380 if (addr != NO_REGISTER) {
381 val = edt_ft5x06_register_read(tsdata, addr);
382 if (val < 0) {
383 error = val;
384 dev_err(&tsdata->client->dev,
385 "Failed to fetch attribute %s, error %d\n",
386 dattr->attr.name, error);
387 goto out;
388 }
389 } else {
390 val = *field;
391 }
392
Simon Budig43c4d132012-07-24 23:29:36 -0700393 if (val != *field) {
394 dev_warn(&tsdata->client->dev,
395 "%s: read (%d) and stored value (%d) differ\n",
396 dattr->attr.name, val, *field);
397 *field = val;
398 }
399
400 count = scnprintf(buf, PAGE_SIZE, "%d\n", val);
401out:
402 mutex_unlock(&tsdata->mutex);
403 return error ?: count;
404}
405
406static ssize_t edt_ft5x06_setting_store(struct device *dev,
407 struct device_attribute *dattr,
408 const char *buf, size_t count)
409{
410 struct i2c_client *client = to_i2c_client(dev);
411 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
412 struct edt_ft5x06_attribute *attr =
413 container_of(dattr, struct edt_ft5x06_attribute, dattr);
Lothar Waßmann1730d812014-03-28 09:20:08 -0700414 u8 *field = (u8 *)tsdata + attr->field_offset;
Simon Budig43c4d132012-07-24 23:29:36 -0700415 unsigned int val;
416 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700417 u8 addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700418
419 mutex_lock(&tsdata->mutex);
420
421 if (tsdata->factory_mode) {
422 error = -EIO;
423 goto out;
424 }
425
426 error = kstrtouint(buf, 0, &val);
427 if (error)
428 goto out;
429
430 if (val < attr->limit_low || val > attr->limit_high) {
431 error = -ERANGE;
432 goto out;
433 }
434
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700435 switch (tsdata->version) {
436 case M06:
437 addr = attr->addr_m06;
438 break;
439
440 case M09:
441 addr = attr->addr_m09;
442 break;
443
444 default:
445 error = -ENODEV;
Simon Budig43c4d132012-07-24 23:29:36 -0700446 goto out;
447 }
448
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700449 if (addr != NO_REGISTER) {
450 error = edt_ft5x06_register_write(tsdata, addr, val);
451 if (error) {
452 dev_err(&tsdata->client->dev,
453 "Failed to update attribute %s, error: %d\n",
454 dattr->attr.name, error);
455 goto out;
456 }
457 }
Simon Budig43c4d132012-07-24 23:29:36 -0700458 *field = val;
459
460out:
461 mutex_unlock(&tsdata->mutex);
462 return error ?: count;
463}
464
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700465static EDT_ATTR(gain, S_IWUSR | S_IRUGO, WORK_REGISTER_GAIN,
466 M09_REGISTER_GAIN, 0, 31);
467static EDT_ATTR(offset, S_IWUSR | S_IRUGO, WORK_REGISTER_OFFSET,
468 M09_REGISTER_OFFSET, 0, 31);
469static EDT_ATTR(threshold, S_IWUSR | S_IRUGO, WORK_REGISTER_THRESHOLD,
470 M09_REGISTER_THRESHOLD, 20, 80);
471static EDT_ATTR(report_rate, S_IWUSR | S_IRUGO, WORK_REGISTER_REPORT_RATE,
472 NO_REGISTER, 3, 14);
Simon Budig43c4d132012-07-24 23:29:36 -0700473
474static struct attribute *edt_ft5x06_attrs[] = {
475 &edt_ft5x06_attr_gain.dattr.attr,
476 &edt_ft5x06_attr_offset.dattr.attr,
477 &edt_ft5x06_attr_threshold.dattr.attr,
478 &edt_ft5x06_attr_report_rate.dattr.attr,
479 NULL
480};
481
482static const struct attribute_group edt_ft5x06_attr_group = {
483 .attrs = edt_ft5x06_attrs,
484};
485
486#ifdef CONFIG_DEBUG_FS
487static int edt_ft5x06_factory_mode(struct edt_ft5x06_ts_data *tsdata)
488{
489 struct i2c_client *client = tsdata->client;
490 int retries = EDT_SWITCH_MODE_RETRIES;
491 int ret;
492 int error;
493
494 disable_irq(client->irq);
495
496 if (!tsdata->raw_buffer) {
497 tsdata->raw_bufsize = tsdata->num_x * tsdata->num_y *
498 sizeof(u16);
499 tsdata->raw_buffer = kzalloc(tsdata->raw_bufsize, GFP_KERNEL);
500 if (!tsdata->raw_buffer) {
501 error = -ENOMEM;
502 goto err_out;
503 }
504 }
505
506 /* mode register is 0x3c when in the work mode */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700507 if (tsdata->version == M09)
508 goto m09_out;
509
Simon Budig43c4d132012-07-24 23:29:36 -0700510 error = edt_ft5x06_register_write(tsdata, WORK_REGISTER_OPMODE, 0x03);
511 if (error) {
512 dev_err(&client->dev,
513 "failed to switch to factory mode, error %d\n", error);
514 goto err_out;
515 }
516
517 tsdata->factory_mode = true;
518 do {
519 mdelay(EDT_SWITCH_MODE_DELAY);
520 /* mode register is 0x01 when in factory mode */
521 ret = edt_ft5x06_register_read(tsdata, FACTORY_REGISTER_OPMODE);
522 if (ret == 0x03)
523 break;
524 } while (--retries > 0);
525
526 if (retries == 0) {
527 dev_err(&client->dev, "not in factory mode after %dms.\n",
528 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
529 error = -EIO;
530 goto err_out;
531 }
532
533 return 0;
534
535err_out:
536 kfree(tsdata->raw_buffer);
537 tsdata->raw_buffer = NULL;
538 tsdata->factory_mode = false;
539 enable_irq(client->irq);
540
541 return error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700542
543m09_out:
544 dev_err(&client->dev, "No factory mode support for M09\n");
545 return -EINVAL;
546
Simon Budig43c4d132012-07-24 23:29:36 -0700547}
548
549static int edt_ft5x06_work_mode(struct edt_ft5x06_ts_data *tsdata)
550{
551 struct i2c_client *client = tsdata->client;
552 int retries = EDT_SWITCH_MODE_RETRIES;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700553 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
Simon Budig43c4d132012-07-24 23:29:36 -0700554 int ret;
555 int error;
556
557 /* mode register is 0x01 when in the factory mode */
558 error = edt_ft5x06_register_write(tsdata, FACTORY_REGISTER_OPMODE, 0x1);
559 if (error) {
560 dev_err(&client->dev,
561 "failed to switch to work mode, error: %d\n", error);
562 return error;
563 }
564
565 tsdata->factory_mode = false;
566
567 do {
568 mdelay(EDT_SWITCH_MODE_DELAY);
569 /* mode register is 0x01 when in factory mode */
570 ret = edt_ft5x06_register_read(tsdata, WORK_REGISTER_OPMODE);
571 if (ret == 0x01)
572 break;
573 } while (--retries > 0);
574
575 if (retries == 0) {
576 dev_err(&client->dev, "not in work mode after %dms.\n",
577 EDT_SWITCH_MODE_RETRIES * EDT_SWITCH_MODE_DELAY);
578 tsdata->factory_mode = true;
579 return -EIO;
580 }
581
Sachin Kamat8efcc502013-03-28 01:14:42 -0700582 kfree(tsdata->raw_buffer);
Simon Budig43c4d132012-07-24 23:29:36 -0700583 tsdata->raw_buffer = NULL;
584
585 /* restore parameters */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700586 edt_ft5x06_register_write(tsdata, reg_addr->reg_threshold,
Simon Budig43c4d132012-07-24 23:29:36 -0700587 tsdata->threshold);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700588 edt_ft5x06_register_write(tsdata, reg_addr->reg_gain,
Simon Budig43c4d132012-07-24 23:29:36 -0700589 tsdata->gain);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700590 edt_ft5x06_register_write(tsdata, reg_addr->reg_offset,
Simon Budig43c4d132012-07-24 23:29:36 -0700591 tsdata->offset);
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700592 if (reg_addr->reg_report_rate)
593 edt_ft5x06_register_write(tsdata, reg_addr->reg_report_rate,
Simon Budig43c4d132012-07-24 23:29:36 -0700594 tsdata->report_rate);
595
596 enable_irq(client->irq);
597
598 return 0;
599}
600
601static int edt_ft5x06_debugfs_mode_get(void *data, u64 *mode)
602{
603 struct edt_ft5x06_ts_data *tsdata = data;
604
605 *mode = tsdata->factory_mode;
606
607 return 0;
608};
609
610static int edt_ft5x06_debugfs_mode_set(void *data, u64 mode)
611{
612 struct edt_ft5x06_ts_data *tsdata = data;
613 int retval = 0;
614
615 if (mode > 1)
616 return -ERANGE;
617
618 mutex_lock(&tsdata->mutex);
619
620 if (mode != tsdata->factory_mode) {
621 retval = mode ? edt_ft5x06_factory_mode(tsdata) :
Lothar Waßmann1730d812014-03-28 09:20:08 -0700622 edt_ft5x06_work_mode(tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -0700623 }
624
625 mutex_unlock(&tsdata->mutex);
626
627 return retval;
628};
629
630DEFINE_SIMPLE_ATTRIBUTE(debugfs_mode_fops, edt_ft5x06_debugfs_mode_get,
631 edt_ft5x06_debugfs_mode_set, "%llu\n");
632
Simon Budig43c4d132012-07-24 23:29:36 -0700633static ssize_t edt_ft5x06_debugfs_raw_data_read(struct file *file,
634 char __user *buf, size_t count, loff_t *off)
635{
636 struct edt_ft5x06_ts_data *tsdata = file->private_data;
637 struct i2c_client *client = tsdata->client;
638 int retries = EDT_RAW_DATA_RETRIES;
639 int val, i, error;
640 size_t read = 0;
641 int colbytes;
642 char wrbuf[3];
643 u8 *rdbuf;
644
645 if (*off < 0 || *off >= tsdata->raw_bufsize)
646 return 0;
647
648 mutex_lock(&tsdata->mutex);
649
650 if (!tsdata->factory_mode || !tsdata->raw_buffer) {
651 error = -EIO;
652 goto out;
653 }
654
655 error = edt_ft5x06_register_write(tsdata, 0x08, 0x01);
656 if (error) {
657 dev_dbg(&client->dev,
658 "failed to write 0x08 register, error %d\n", error);
659 goto out;
660 }
661
662 do {
663 msleep(EDT_RAW_DATA_DELAY);
664 val = edt_ft5x06_register_read(tsdata, 0x08);
665 if (val < 1)
666 break;
667 } while (--retries > 0);
668
669 if (val < 0) {
670 error = val;
671 dev_dbg(&client->dev,
672 "failed to read 0x08 register, error %d\n", error);
673 goto out;
674 }
675
676 if (retries == 0) {
677 dev_dbg(&client->dev,
678 "timed out waiting for register to settle\n");
679 error = -ETIMEDOUT;
680 goto out;
681 }
682
683 rdbuf = tsdata->raw_buffer;
684 colbytes = tsdata->num_y * sizeof(u16);
685
686 wrbuf[0] = 0xf5;
687 wrbuf[1] = 0x0e;
688 for (i = 0; i < tsdata->num_x; i++) {
689 wrbuf[2] = i; /* column index */
690 error = edt_ft5x06_ts_readwrite(tsdata->client,
691 sizeof(wrbuf), wrbuf,
692 colbytes, rdbuf);
693 if (error)
694 goto out;
695
696 rdbuf += colbytes;
697 }
698
699 read = min_t(size_t, count, tsdata->raw_bufsize - *off);
Axel Lin35b1da42012-09-19 15:56:23 -0700700 if (copy_to_user(buf, tsdata->raw_buffer + *off, read)) {
701 error = -EFAULT;
702 goto out;
703 }
704
705 *off += read;
Simon Budig43c4d132012-07-24 23:29:36 -0700706out:
707 mutex_unlock(&tsdata->mutex);
708 return error ?: read;
709};
710
Simon Budig43c4d132012-07-24 23:29:36 -0700711static const struct file_operations debugfs_raw_data_fops = {
Wei Yongjunf6c0df62012-10-17 23:55:54 -0700712 .open = simple_open,
Simon Budig43c4d132012-07-24 23:29:36 -0700713 .read = edt_ft5x06_debugfs_raw_data_read,
714};
715
Bill Pemberton5298cc42012-11-23 21:38:25 -0800716static void
Simon Budig43c4d132012-07-24 23:29:36 -0700717edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
718 const char *debugfs_name)
719{
720 tsdata->debug_dir = debugfs_create_dir(debugfs_name, NULL);
721 if (!tsdata->debug_dir)
722 return;
723
724 debugfs_create_u16("num_x", S_IRUSR, tsdata->debug_dir, &tsdata->num_x);
725 debugfs_create_u16("num_y", S_IRUSR, tsdata->debug_dir, &tsdata->num_y);
726
727 debugfs_create_file("mode", S_IRUSR | S_IWUSR,
728 tsdata->debug_dir, tsdata, &debugfs_mode_fops);
729 debugfs_create_file("raw_data", S_IRUSR,
730 tsdata->debug_dir, tsdata, &debugfs_raw_data_fops);
731}
732
Bill Pembertone2619cf2012-11-23 21:50:47 -0800733static void
Simon Budig43c4d132012-07-24 23:29:36 -0700734edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
735{
Fabian Frederickf69c6ec2014-07-09 09:45:57 -0700736 debugfs_remove_recursive(tsdata->debug_dir);
Guenter Roecka1d0fa72012-08-21 22:01:45 -0700737 kfree(tsdata->raw_buffer);
Simon Budig43c4d132012-07-24 23:29:36 -0700738}
739
740#else
741
742static inline void
743edt_ft5x06_ts_prepare_debugfs(struct edt_ft5x06_ts_data *tsdata,
744 const char *debugfs_name)
745{
746}
747
748static inline void
749edt_ft5x06_ts_teardown_debugfs(struct edt_ft5x06_ts_data *tsdata)
750{
751}
752
753#endif /* CONFIG_DEBUGFS */
754
Bill Pemberton5298cc42012-11-23 21:38:25 -0800755static int edt_ft5x06_ts_reset(struct i2c_client *client,
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700756 struct edt_ft5x06_ts_data *tsdata)
Simon Budig43c4d132012-07-24 23:29:36 -0700757{
758 int error;
759
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700760 if (gpio_is_valid(tsdata->wake_pin)) {
761 error = devm_gpio_request_one(&client->dev,
762 tsdata->wake_pin, GPIOF_OUT_INIT_LOW,
763 "edt-ft5x06 wake");
764 if (error) {
765 dev_err(&client->dev,
766 "Failed to request GPIO %d as wake pin, error %d\n",
767 tsdata->wake_pin, error);
768 return error;
769 }
770
Lothar Waßmannc0808462014-03-28 09:27:50 -0700771 msleep(5);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700772 gpio_set_value(tsdata->wake_pin, 1);
773 }
774 if (gpio_is_valid(tsdata->reset_pin)) {
Simon Budig43c4d132012-07-24 23:29:36 -0700775 /* this pulls reset down, enabling the low active reset */
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700776 error = devm_gpio_request_one(&client->dev,
777 tsdata->reset_pin, GPIOF_OUT_INIT_LOW,
778 "edt-ft5x06 reset");
Simon Budig43c4d132012-07-24 23:29:36 -0700779 if (error) {
780 dev_err(&client->dev,
781 "Failed to request GPIO %d as reset pin, error %d\n",
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700782 tsdata->reset_pin, error);
Simon Budig43c4d132012-07-24 23:29:36 -0700783 return error;
784 }
785
Lothar Waßmannc0808462014-03-28 09:27:50 -0700786 msleep(5);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700787 gpio_set_value(tsdata->reset_pin, 1);
Lothar Waßmannc0808462014-03-28 09:27:50 -0700788 msleep(300);
Simon Budig43c4d132012-07-24 23:29:36 -0700789 }
790
791 return 0;
792}
793
Bill Pemberton5298cc42012-11-23 21:38:25 -0800794static int edt_ft5x06_ts_identify(struct i2c_client *client,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700795 struct edt_ft5x06_ts_data *tsdata,
796 char *fw_version)
Simon Budig43c4d132012-07-24 23:29:36 -0700797{
798 u8 rdbuf[EDT_NAME_LEN];
799 char *p;
800 int error;
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700801 char *model_name = tsdata->name;
Simon Budig43c4d132012-07-24 23:29:36 -0700802
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700803 /* see what we find if we assume it is a M06 *
804 * if we get less than EDT_NAME_LEN, we don't want
805 * to have garbage in there
806 */
807 memset(rdbuf, 0, sizeof(rdbuf));
Simon Budig43c4d132012-07-24 23:29:36 -0700808 error = edt_ft5x06_ts_readwrite(client, 1, "\xbb",
809 EDT_NAME_LEN - 1, rdbuf);
810 if (error)
811 return error;
812
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700813 /* if we find something consistent, stay with that assumption
814 * at least M09 won't send 3 bytes here
815 */
Rasmus Villemoes0f3ae5b2014-10-13 15:54:48 -0700816 if (!(strncasecmp(rdbuf + 1, "EP0", 3))) {
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700817 tsdata->version = M06;
Simon Budig43c4d132012-07-24 23:29:36 -0700818
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700819 /* remove last '$' end marker */
820 rdbuf[EDT_NAME_LEN - 1] = '\0';
821 if (rdbuf[EDT_NAME_LEN - 2] == '$')
822 rdbuf[EDT_NAME_LEN - 2] = '\0';
Simon Budig43c4d132012-07-24 23:29:36 -0700823
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700824 /* look for Model/Version separator */
825 p = strchr(rdbuf, '*');
826 if (p)
827 *p++ = '\0';
828 strlcpy(model_name, rdbuf + 1, EDT_NAME_LEN);
829 strlcpy(fw_version, p ? p : "", EDT_NAME_LEN);
830 } else {
831 /* since there are only two versions around (M06, M09) */
832 tsdata->version = M09;
833
834 error = edt_ft5x06_ts_readwrite(client, 1, "\xA6",
835 2, rdbuf);
836 if (error)
837 return error;
838
839 strlcpy(fw_version, rdbuf, 2);
840
841 error = edt_ft5x06_ts_readwrite(client, 1, "\xA8",
842 1, rdbuf);
843 if (error)
844 return error;
845
846 snprintf(model_name, EDT_NAME_LEN, "EP0%i%i0M09",
847 rdbuf[0] >> 4, rdbuf[0] & 0x0F);
848 }
Simon Budig43c4d132012-07-24 23:29:36 -0700849
850 return 0;
851}
852
853#define EDT_ATTR_CHECKSET(name, reg) \
Asaf Vertz189387f2014-12-13 10:59:04 -0800854do { \
Simon Budig43c4d132012-07-24 23:29:36 -0700855 if (pdata->name >= edt_ft5x06_attr_##name.limit_low && \
856 pdata->name <= edt_ft5x06_attr_##name.limit_high) \
Asaf Vertz189387f2014-12-13 10:59:04 -0800857 edt_ft5x06_register_write(tsdata, reg, pdata->name); \
858} while (0)
Simon Budig43c4d132012-07-24 23:29:36 -0700859
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700860#define EDT_GET_PROP(name, reg) { \
861 u32 val; \
862 if (of_property_read_u32(np, #name, &val) == 0) \
863 edt_ft5x06_register_write(tsdata, reg, val); \
864}
865
866static void edt_ft5x06_ts_get_dt_defaults(struct device_node *np,
867 struct edt_ft5x06_ts_data *tsdata)
868{
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700869 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
870
871 EDT_GET_PROP(threshold, reg_addr->reg_threshold);
872 EDT_GET_PROP(gain, reg_addr->reg_gain);
873 EDT_GET_PROP(offset, reg_addr->reg_offset);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700874}
875
Bill Pemberton5298cc42012-11-23 21:38:25 -0800876static void
Simon Budig43c4d132012-07-24 23:29:36 -0700877edt_ft5x06_ts_get_defaults(struct edt_ft5x06_ts_data *tsdata,
878 const struct edt_ft5x06_platform_data *pdata)
879{
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700880 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
881
Simon Budig43c4d132012-07-24 23:29:36 -0700882 if (!pdata->use_parameters)
883 return;
884
885 /* pick up defaults from the platform data */
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700886 EDT_ATTR_CHECKSET(threshold, reg_addr->reg_threshold);
887 EDT_ATTR_CHECKSET(gain, reg_addr->reg_gain);
888 EDT_ATTR_CHECKSET(offset, reg_addr->reg_offset);
889 if (reg_addr->reg_report_rate != NO_REGISTER)
890 EDT_ATTR_CHECKSET(report_rate, reg_addr->reg_report_rate);
Simon Budig43c4d132012-07-24 23:29:36 -0700891}
892
Bill Pemberton5298cc42012-11-23 21:38:25 -0800893static void
Simon Budig43c4d132012-07-24 23:29:36 -0700894edt_ft5x06_ts_get_parameters(struct edt_ft5x06_ts_data *tsdata)
895{
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700896 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
897
Simon Budig43c4d132012-07-24 23:29:36 -0700898 tsdata->threshold = edt_ft5x06_register_read(tsdata,
Lothar Waßmannfd335ab2014-03-28 09:31:14 -0700899 reg_addr->reg_threshold);
900 tsdata->gain = edt_ft5x06_register_read(tsdata, reg_addr->reg_gain);
901 tsdata->offset = edt_ft5x06_register_read(tsdata, reg_addr->reg_offset);
902 if (reg_addr->reg_report_rate != NO_REGISTER)
903 tsdata->report_rate = edt_ft5x06_register_read(tsdata,
904 reg_addr->reg_report_rate);
905 tsdata->num_x = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_x);
906 tsdata->num_y = edt_ft5x06_register_read(tsdata, reg_addr->reg_num_y);
907}
908
909static void
910edt_ft5x06_ts_set_regs(struct edt_ft5x06_ts_data *tsdata)
911{
912 struct edt_reg_addr *reg_addr = &tsdata->reg_addr;
913
914 switch (tsdata->version) {
915 case M06:
916 reg_addr->reg_threshold = WORK_REGISTER_THRESHOLD;
917 reg_addr->reg_report_rate = WORK_REGISTER_REPORT_RATE;
918 reg_addr->reg_gain = WORK_REGISTER_GAIN;
919 reg_addr->reg_offset = WORK_REGISTER_OFFSET;
920 reg_addr->reg_num_x = WORK_REGISTER_NUM_X;
921 reg_addr->reg_num_y = WORK_REGISTER_NUM_Y;
922 break;
923
924 case M09:
925 reg_addr->reg_threshold = M09_REGISTER_THRESHOLD;
926 reg_addr->reg_gain = M09_REGISTER_GAIN;
927 reg_addr->reg_offset = M09_REGISTER_OFFSET;
928 reg_addr->reg_num_x = M09_REGISTER_NUM_X;
929 reg_addr->reg_num_y = M09_REGISTER_NUM_Y;
930 break;
931 }
Simon Budig43c4d132012-07-24 23:29:36 -0700932}
933
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700934#ifdef CONFIG_OF
935static int edt_ft5x06_i2c_ts_probe_dt(struct device *dev,
936 struct edt_ft5x06_ts_data *tsdata)
937{
938 struct device_node *np = dev->of_node;
939
940 /*
941 * irq_pin is not needed for DT setup.
942 * irq is associated via 'interrupts' property in DT
943 */
944 tsdata->irq_pin = -EINVAL;
945 tsdata->reset_pin = of_get_named_gpio(np, "reset-gpios", 0);
946 tsdata->wake_pin = of_get_named_gpio(np, "wake-gpios", 0);
947
948 return 0;
949}
950#else
951static inline int edt_ft5x06_i2c_ts_probe_dt(struct device *dev,
952 struct edt_ft5x06_ts_data *tsdata)
953{
954 return -ENODEV;
955}
956#endif
957
Bill Pemberton5298cc42012-11-23 21:38:25 -0800958static int edt_ft5x06_ts_probe(struct i2c_client *client,
Simon Budig43c4d132012-07-24 23:29:36 -0700959 const struct i2c_device_id *id)
960{
961 const struct edt_ft5x06_platform_data *pdata =
Jingoo Hanc838cb32013-12-05 19:21:10 -0800962 dev_get_platdata(&client->dev);
Simon Budig43c4d132012-07-24 23:29:36 -0700963 struct edt_ft5x06_ts_data *tsdata;
964 struct input_dev *input;
965 int error;
966 char fw_version[EDT_NAME_LEN];
967
968 dev_dbg(&client->dev, "probing for EDT FT5x06 I2C\n");
969
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800970 tsdata = devm_kzalloc(&client->dev, sizeof(*tsdata), GFP_KERNEL);
971 if (!tsdata) {
Simon Budig43c4d132012-07-24 23:29:36 -0700972 dev_err(&client->dev, "failed to allocate driver data.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -0800973 return -ENOMEM;
974 }
975
Lothar Waßmanndac90dc22014-03-28 09:23:02 -0700976 if (!pdata) {
977 error = edt_ft5x06_i2c_ts_probe_dt(&client->dev, tsdata);
978 if (error) {
979 dev_err(&client->dev,
980 "DT probe failed and no platform data present\n");
981 return error;
982 }
983 } else {
984 tsdata->reset_pin = pdata->reset_pin;
985 tsdata->irq_pin = pdata->irq_pin;
986 tsdata->wake_pin = -EINVAL;
987 }
988
989 error = edt_ft5x06_ts_reset(client, tsdata);
990 if (error)
991 return error;
992
993 if (gpio_is_valid(tsdata->irq_pin)) {
994 error = devm_gpio_request_one(&client->dev, tsdata->irq_pin,
995 GPIOF_IN, "edt-ft5x06 irq");
996 if (error) {
997 dev_err(&client->dev,
998 "Failed to request GPIO %d, error %d\n",
999 tsdata->irq_pin, error);
1000 return error;
1001 }
1002 }
1003
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001004 input = devm_input_allocate_device(&client->dev);
1005 if (!input) {
1006 dev_err(&client->dev, "failed to allocate input device.\n");
1007 return -ENOMEM;
Simon Budig43c4d132012-07-24 23:29:36 -07001008 }
1009
1010 mutex_init(&tsdata->mutex);
1011 tsdata->client = client;
1012 tsdata->input = input;
1013 tsdata->factory_mode = false;
1014
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001015 error = edt_ft5x06_ts_identify(client, tsdata, fw_version);
Simon Budig43c4d132012-07-24 23:29:36 -07001016 if (error) {
1017 dev_err(&client->dev, "touchscreen probe failed\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001018 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001019 }
1020
Lothar Waßmannfd335ab2014-03-28 09:31:14 -07001021 edt_ft5x06_ts_set_regs(tsdata);
1022
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001023 if (!pdata)
1024 edt_ft5x06_ts_get_dt_defaults(client->dev.of_node, tsdata);
1025 else
1026 edt_ft5x06_ts_get_defaults(tsdata, pdata);
1027
Simon Budig43c4d132012-07-24 23:29:36 -07001028 edt_ft5x06_ts_get_parameters(tsdata);
1029
1030 dev_dbg(&client->dev,
1031 "Model \"%s\", Rev. \"%s\", %dx%d sensors\n",
1032 tsdata->name, fw_version, tsdata->num_x, tsdata->num_y);
1033
1034 input->name = tsdata->name;
1035 input->id.bustype = BUS_I2C;
1036 input->dev.parent = &client->dev;
1037
1038 __set_bit(EV_SYN, input->evbit);
1039 __set_bit(EV_KEY, input->evbit);
1040 __set_bit(EV_ABS, input->evbit);
1041 __set_bit(BTN_TOUCH, input->keybit);
1042 input_set_abs_params(input, ABS_X, 0, tsdata->num_x * 64 - 1, 0, 0);
1043 input_set_abs_params(input, ABS_Y, 0, tsdata->num_y * 64 - 1, 0, 0);
1044 input_set_abs_params(input, ABS_MT_POSITION_X,
1045 0, tsdata->num_x * 64 - 1, 0, 0);
1046 input_set_abs_params(input, ABS_MT_POSITION_Y,
1047 0, tsdata->num_y * 64 - 1, 0, 0);
Maxime Ripard2c005592015-03-21 20:18:06 -07001048
1049 if (!pdata)
1050 touchscreen_parse_of_params(input);
1051
Henrik Rydbergb4adbbe2012-08-11 22:07:55 +02001052 error = input_mt_init_slots(input, MAX_SUPPORT_POINTS, 0);
Simon Budig43c4d132012-07-24 23:29:36 -07001053 if (error) {
1054 dev_err(&client->dev, "Unable to init MT slots.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001055 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001056 }
1057
1058 input_set_drvdata(input, tsdata);
1059 i2c_set_clientdata(client, tsdata);
1060
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001061 error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
1062 edt_ft5x06_ts_isr,
1063 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1064 client->name, tsdata);
Simon Budig43c4d132012-07-24 23:29:36 -07001065 if (error) {
1066 dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001067 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001068 }
1069
1070 error = sysfs_create_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1071 if (error)
Lothar Waßmann02300bd2014-01-16 16:26:55 -08001072 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001073
1074 error = input_register_device(input);
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001075 if (error)
1076 goto err_remove_attrs;
Simon Budig43c4d132012-07-24 23:29:36 -07001077
1078 edt_ft5x06_ts_prepare_debugfs(tsdata, dev_driver_string(&client->dev));
1079 device_init_wakeup(&client->dev, 1);
1080
1081 dev_dbg(&client->dev,
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001082 "EDT FT5x06 initialized: IRQ %d, WAKE pin %d, Reset pin %d.\n",
1083 client->irq, tsdata->wake_pin, tsdata->reset_pin);
Simon Budig43c4d132012-07-24 23:29:36 -07001084
1085 return 0;
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001086
1087err_remove_attrs:
1088 sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1089 return error;
Simon Budig43c4d132012-07-24 23:29:36 -07001090}
1091
Bill Pembertone2619cf2012-11-23 21:50:47 -08001092static int edt_ft5x06_ts_remove(struct i2c_client *client)
Simon Budig43c4d132012-07-24 23:29:36 -07001093{
Simon Budig43c4d132012-07-24 23:29:36 -07001094 struct edt_ft5x06_ts_data *tsdata = i2c_get_clientdata(client);
1095
1096 edt_ft5x06_ts_teardown_debugfs(tsdata);
1097 sysfs_remove_group(&client->dev.kobj, &edt_ft5x06_attr_group);
1098
Simon Budig43c4d132012-07-24 23:29:36 -07001099 return 0;
1100}
1101
Jingoo Han02b6a582014-11-02 00:04:14 -07001102static int __maybe_unused edt_ft5x06_ts_suspend(struct device *dev)
Simon Budig43c4d132012-07-24 23:29:36 -07001103{
1104 struct i2c_client *client = to_i2c_client(dev);
1105
1106 if (device_may_wakeup(dev))
1107 enable_irq_wake(client->irq);
1108
1109 return 0;
1110}
1111
Jingoo Han02b6a582014-11-02 00:04:14 -07001112static int __maybe_unused edt_ft5x06_ts_resume(struct device *dev)
Simon Budig43c4d132012-07-24 23:29:36 -07001113{
1114 struct i2c_client *client = to_i2c_client(dev);
1115
1116 if (device_may_wakeup(dev))
1117 disable_irq_wake(client->irq);
1118
1119 return 0;
1120}
Simon Budig43c4d132012-07-24 23:29:36 -07001121
1122static SIMPLE_DEV_PM_OPS(edt_ft5x06_ts_pm_ops,
1123 edt_ft5x06_ts_suspend, edt_ft5x06_ts_resume);
1124
1125static const struct i2c_device_id edt_ft5x06_ts_id[] = {
Lothar Waßmann1730d812014-03-28 09:20:08 -07001126 { "edt-ft5x06", 0, },
1127 { /* sentinel */ }
Simon Budig43c4d132012-07-24 23:29:36 -07001128};
1129MODULE_DEVICE_TABLE(i2c, edt_ft5x06_ts_id);
1130
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001131#ifdef CONFIG_OF
1132static const struct of_device_id edt_ft5x06_of_match[] = {
1133 { .compatible = "edt,edt-ft5206", },
1134 { .compatible = "edt,edt-ft5306", },
1135 { .compatible = "edt,edt-ft5406", },
1136 { /* sentinel */ }
1137};
1138MODULE_DEVICE_TABLE(of, edt_ft5x06_of_match);
1139#endif
1140
Simon Budig43c4d132012-07-24 23:29:36 -07001141static struct i2c_driver edt_ft5x06_ts_driver = {
1142 .driver = {
1143 .owner = THIS_MODULE,
1144 .name = "edt_ft5x06",
Lothar Waßmanndac90dc22014-03-28 09:23:02 -07001145 .of_match_table = of_match_ptr(edt_ft5x06_of_match),
Simon Budig43c4d132012-07-24 23:29:36 -07001146 .pm = &edt_ft5x06_ts_pm_ops,
1147 },
1148 .id_table = edt_ft5x06_ts_id,
1149 .probe = edt_ft5x06_ts_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -08001150 .remove = edt_ft5x06_ts_remove,
Simon Budig43c4d132012-07-24 23:29:36 -07001151};
1152
1153module_i2c_driver(edt_ft5x06_ts_driver);
1154
1155MODULE_AUTHOR("Simon Budig <simon.budig@kernelconcepts.de>");
1156MODULE_DESCRIPTION("EDT FT5x06 I2C Touchscreen Driver");
1157MODULE_LICENSE("GPL");