blob: bdc936cb844542ec55d9bbb9c5f63a062798f69d [file] [log] [blame]
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -07001/*
2 * Copyright (C) 2012-2013 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
4 *
5 * based in parts on Nook zforce driver
6 *
7 * Copyright (C) 2010 Barnes & Noble, Inc.
8 * Author: Pieter Truter<ptruter@intrinsyc.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/module.h>
21#include <linux/hrtimer.h>
22#include <linux/slab.h>
23#include <linux/input.h>
24#include <linux/interrupt.h>
25#include <linux/i2c.h>
26#include <linux/delay.h>
27#include <linux/gpio.h>
28#include <linux/device.h>
29#include <linux/sysfs.h>
30#include <linux/input/mt.h>
31#include <linux/platform_data/zforce_ts.h>
Heiko Stübner6f2e6c92014-01-27 12:37:21 -080032#include <linux/of.h>
33#include <linux/of_gpio.h>
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -070034
35#define WAIT_TIMEOUT msecs_to_jiffies(1000)
36
37#define FRAME_START 0xee
Luis Ortegad333b602014-01-27 12:28:33 -080038#define FRAME_MAXSIZE 257
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -070039
40/* Offsets of the different parts of the payload the controller sends */
41#define PAYLOAD_HEADER 0
42#define PAYLOAD_LENGTH 1
43#define PAYLOAD_BODY 2
44
45/* Response offsets */
46#define RESPONSE_ID 0
47#define RESPONSE_DATA 1
48
49/* Commands */
50#define COMMAND_DEACTIVATE 0x00
51#define COMMAND_INITIALIZE 0x01
52#define COMMAND_RESOLUTION 0x02
53#define COMMAND_SETCONFIG 0x03
54#define COMMAND_DATAREQUEST 0x04
55#define COMMAND_SCANFREQ 0x08
56#define COMMAND_STATUS 0X1e
57
58/*
59 * Responses the controller sends as a result of
60 * command requests
61 */
62#define RESPONSE_DEACTIVATE 0x00
63#define RESPONSE_INITIALIZE 0x01
64#define RESPONSE_RESOLUTION 0x02
65#define RESPONSE_SETCONFIG 0x03
66#define RESPONSE_SCANFREQ 0x08
67#define RESPONSE_STATUS 0X1e
68
69/*
Luis Ortegadeb49812014-01-27 12:27:06 -080070 * Notifications are sent by the touch controller without
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -070071 * being requested by the driver and include for example
72 * touch indications
73 */
74#define NOTIFICATION_TOUCH 0x04
75#define NOTIFICATION_BOOTCOMPLETE 0x07
76#define NOTIFICATION_OVERRUN 0x25
77#define NOTIFICATION_PROXIMITY 0x26
78#define NOTIFICATION_INVALID_COMMAND 0xfe
79
80#define ZFORCE_REPORT_POINTS 2
81#define ZFORCE_MAX_AREA 0xff
82
83#define STATE_DOWN 0
84#define STATE_MOVE 1
85#define STATE_UP 2
86
87#define SETCONFIG_DUALTOUCH (1 << 0)
88
89struct zforce_point {
90 int coord_x;
91 int coord_y;
92 int state;
93 int id;
94 int area_major;
95 int area_minor;
96 int orientation;
97 int pressure;
98 int prblty;
99};
100
101/*
102 * @client the i2c_client
103 * @input the input device
104 * @suspending in the process of going to suspend (don't emit wakeup
105 * events for commands executed to suspend the device)
106 * @suspended device suspended
107 * @access_mutex serialize i2c-access, to keep multipart reads together
108 * @command_done completion to wait for the command result
Luis Ortegadeb49812014-01-27 12:27:06 -0800109 * @command_mutex serialize commands sent to the ic
110 * @command_waiting the id of the command that is currently waiting
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700111 * for a result
112 * @command_result returned result of the command
113 */
114struct zforce_ts {
115 struct i2c_client *client;
116 struct input_dev *input;
117 const struct zforce_ts_platdata *pdata;
118 char phys[32];
119
120 bool suspending;
121 bool suspended;
122 bool boot_complete;
123
124 /* Firmware version information */
125 u16 version_major;
126 u16 version_minor;
127 u16 version_build;
128 u16 version_rev;
129
130 struct mutex access_mutex;
131
132 struct completion command_done;
133 struct mutex command_mutex;
134 int command_waiting;
135 int command_result;
136};
137
138static int zforce_command(struct zforce_ts *ts, u8 cmd)
139{
140 struct i2c_client *client = ts->client;
141 char buf[3];
142 int ret;
143
144 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
145
146 buf[0] = FRAME_START;
147 buf[1] = 1; /* data size, command only */
148 buf[2] = cmd;
149
150 mutex_lock(&ts->access_mutex);
151 ret = i2c_master_send(client, &buf[0], ARRAY_SIZE(buf));
152 mutex_unlock(&ts->access_mutex);
153 if (ret < 0) {
154 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
155 return ret;
156 }
157
158 return 0;
159}
160
161static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
162{
163 struct i2c_client *client = ts->client;
164 int ret;
165
166 ret = mutex_trylock(&ts->command_mutex);
167 if (!ret) {
168 dev_err(&client->dev, "already waiting for a command\n");
169 return -EBUSY;
170 }
171
172 dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
173 buf[1], buf[2]);
174
175 ts->command_waiting = buf[2];
176
177 mutex_lock(&ts->access_mutex);
178 ret = i2c_master_send(client, buf, len);
179 mutex_unlock(&ts->access_mutex);
180 if (ret < 0) {
181 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
182 goto unlock;
183 }
184
185 dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
186
187 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) {
188 ret = -ETIME;
189 goto unlock;
190 }
191
192 ret = ts->command_result;
193
194unlock:
195 mutex_unlock(&ts->command_mutex);
196 return ret;
197}
198
199static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
200{
201 struct i2c_client *client = ts->client;
202 char buf[3];
203 int ret;
204
205 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
206
207 buf[0] = FRAME_START;
208 buf[1] = 1; /* data size, command only */
209 buf[2] = cmd;
210
211 ret = zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
212 if (ret < 0) {
213 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
214 return ret;
215 }
216
217 return 0;
218}
219
220static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
221{
222 struct i2c_client *client = ts->client;
223 char buf[7] = { FRAME_START, 5, COMMAND_RESOLUTION,
224 (x & 0xff), ((x >> 8) & 0xff),
225 (y & 0xff), ((y >> 8) & 0xff) };
226
227 dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
228
229 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
230}
231
232static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
233 u16 stylus)
234{
235 struct i2c_client *client = ts->client;
236 char buf[9] = { FRAME_START, 7, COMMAND_SCANFREQ,
237 (idle & 0xff), ((idle >> 8) & 0xff),
238 (finger & 0xff), ((finger >> 8) & 0xff),
239 (stylus & 0xff), ((stylus >> 8) & 0xff) };
240
Luis Ortegaad697b92014-01-27 12:27:35 -0800241 dev_dbg(&client->dev,
242 "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700243 idle, finger, stylus);
244
245 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
246}
247
248static int zforce_setconfig(struct zforce_ts *ts, char b1)
249{
250 struct i2c_client *client = ts->client;
251 char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
252 b1, 0, 0, 0 };
253
254 dev_dbg(&client->dev, "set config to (%d)\n", b1);
255
256 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
257}
258
259static int zforce_start(struct zforce_ts *ts)
260{
261 struct i2c_client *client = ts->client;
Heiko Stübner87273362014-01-27 12:32:38 -0800262 const struct zforce_ts_platdata *pdata = ts->pdata;
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700263 int ret;
264
265 dev_dbg(&client->dev, "starting device\n");
266
267 ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
268 if (ret) {
269 dev_err(&client->dev, "Unable to initialize, %d\n", ret);
270 return ret;
271 }
272
273 ret = zforce_resolution(ts, pdata->x_max, pdata->y_max);
274 if (ret) {
275 dev_err(&client->dev, "Unable to set resolution, %d\n", ret);
276 goto error;
277 }
278
279 ret = zforce_scan_frequency(ts, 10, 50, 50);
280 if (ret) {
281 dev_err(&client->dev, "Unable to set scan frequency, %d\n",
282 ret);
283 goto error;
284 }
285
Wei Yongjun3c4396b2013-12-17 08:58:18 -0800286 ret = zforce_setconfig(ts, SETCONFIG_DUALTOUCH);
287 if (ret) {
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700288 dev_err(&client->dev, "Unable to set config\n");
289 goto error;
290 }
291
292 /* start sending touch events */
293 ret = zforce_command(ts, COMMAND_DATAREQUEST);
294 if (ret) {
295 dev_err(&client->dev, "Unable to request data\n");
296 goto error;
297 }
298
299 /*
300 * Per NN, initial cal. take max. of 200msec.
301 * Allow time to complete this calibration
302 */
303 msleep(200);
304
305 return 0;
306
307error:
308 zforce_command_wait(ts, COMMAND_DEACTIVATE);
309 return ret;
310}
311
312static int zforce_stop(struct zforce_ts *ts)
313{
314 struct i2c_client *client = ts->client;
315 int ret;
316
317 dev_dbg(&client->dev, "stopping device\n");
318
319 /* Deactivates touch sensing and puts the device into sleep. */
320 ret = zforce_command_wait(ts, COMMAND_DEACTIVATE);
321 if (ret != 0) {
322 dev_err(&client->dev, "could not deactivate device, %d\n",
323 ret);
324 return ret;
325 }
326
327 return 0;
328}
329
330static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
331{
332 struct i2c_client *client = ts->client;
Heiko Stübner87273362014-01-27 12:32:38 -0800333 const struct zforce_ts_platdata *pdata = ts->pdata;
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700334 struct zforce_point point;
335 int count, i, num = 0;
336
337 count = payload[0];
338 if (count > ZFORCE_REPORT_POINTS) {
Luis Ortegaad697b92014-01-27 12:27:35 -0800339 dev_warn(&client->dev,
340 "too many coordinates %d, expected max %d\n",
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700341 count, ZFORCE_REPORT_POINTS);
342 count = ZFORCE_REPORT_POINTS;
343 }
344
345 for (i = 0; i < count; i++) {
346 point.coord_x =
347 payload[9 * i + 2] << 8 | payload[9 * i + 1];
348 point.coord_y =
349 payload[9 * i + 4] << 8 | payload[9 * i + 3];
350
351 if (point.coord_x > pdata->x_max ||
352 point.coord_y > pdata->y_max) {
353 dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
354 point.coord_x, point.coord_y);
355 point.coord_x = point.coord_y = 0;
356 }
357
358 point.state = payload[9 * i + 5] & 0x03;
359 point.id = (payload[9 * i + 5] & 0xfc) >> 2;
360
361 /* determine touch major, minor and orientation */
362 point.area_major = max(payload[9 * i + 6],
363 payload[9 * i + 7]);
364 point.area_minor = min(payload[9 * i + 6],
365 payload[9 * i + 7]);
366 point.orientation = payload[9 * i + 6] > payload[9 * i + 7];
367
368 point.pressure = payload[9 * i + 8];
369 point.prblty = payload[9 * i + 9];
370
371 dev_dbg(&client->dev,
372 "point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n",
373 i, count, point.state, point.id,
374 point.pressure, point.prblty,
375 point.coord_x, point.coord_y,
376 point.area_major, point.area_minor,
377 point.orientation);
378
379 /* the zforce id starts with "1", so needs to be decreased */
380 input_mt_slot(ts->input, point.id - 1);
381
382 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
383 point.state != STATE_UP);
384
385 if (point.state != STATE_UP) {
386 input_report_abs(ts->input, ABS_MT_POSITION_X,
387 point.coord_x);
388 input_report_abs(ts->input, ABS_MT_POSITION_Y,
389 point.coord_y);
390 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
391 point.area_major);
392 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
393 point.area_minor);
394 input_report_abs(ts->input, ABS_MT_ORIENTATION,
395 point.orientation);
396 num++;
397 }
398 }
399
400 input_mt_sync_frame(ts->input);
401
402 input_mt_report_finger_count(ts->input, num);
403
404 input_sync(ts->input);
405
406 return 0;
407}
408
409static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
410{
411 struct i2c_client *client = ts->client;
412 int ret;
413
414 mutex_lock(&ts->access_mutex);
415
416 /* read 2 byte message header */
417 ret = i2c_master_recv(client, buf, 2);
418 if (ret < 0) {
419 dev_err(&client->dev, "error reading header: %d\n", ret);
420 goto unlock;
421 }
422
423 if (buf[PAYLOAD_HEADER] != FRAME_START) {
424 dev_err(&client->dev, "invalid frame start: %d\n", buf[0]);
425 ret = -EIO;
426 goto unlock;
427 }
428
Luis Ortega5aee41a2014-01-27 12:27:49 -0800429 if (buf[PAYLOAD_LENGTH] == 0) {
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700430 dev_err(&client->dev, "invalid payload length: %d\n",
431 buf[PAYLOAD_LENGTH]);
432 ret = -EIO;
433 goto unlock;
434 }
435
436 /* read the message */
437 ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]);
438 if (ret < 0) {
439 dev_err(&client->dev, "error reading payload: %d\n", ret);
440 goto unlock;
441 }
442
443 dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
444 buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
445
446unlock:
447 mutex_unlock(&ts->access_mutex);
448 return ret;
449}
450
451static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
452{
453 struct i2c_client *client = ts->client;
454
455 if (ts->command_waiting == cmd) {
456 dev_dbg(&client->dev, "completing command 0x%x\n", cmd);
457 ts->command_result = result;
458 complete(&ts->command_done);
459 } else {
460 dev_dbg(&client->dev, "command %d not for us\n", cmd);
461 }
462}
463
464static irqreturn_t zforce_interrupt(int irq, void *dev_id)
465{
466 struct zforce_ts *ts = dev_id;
467 struct i2c_client *client = ts->client;
Heiko Stübner87273362014-01-27 12:32:38 -0800468 const struct zforce_ts_platdata *pdata = ts->pdata;
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700469 int ret;
Luis Ortegad333b602014-01-27 12:28:33 -0800470 u8 payload_buffer[FRAME_MAXSIZE];
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700471 u8 *payload;
472
473 /*
474 * When suspended, emit a wakeup signal if necessary and return.
475 * Due to the level-interrupt we will get re-triggered later.
476 */
477 if (ts->suspended) {
478 if (device_may_wakeup(&client->dev))
479 pm_wakeup_event(&client->dev, 500);
480 msleep(20);
481 return IRQ_HANDLED;
482 }
483
484 dev_dbg(&client->dev, "handling interrupt\n");
485
486 /* Don't emit wakeup events from commands run by zforce_suspend */
487 if (!ts->suspending && device_may_wakeup(&client->dev))
488 pm_stay_awake(&client->dev);
489
490 while (!gpio_get_value(pdata->gpio_int)) {
491 ret = zforce_read_packet(ts, payload_buffer);
492 if (ret < 0) {
Luis Ortegaad697b92014-01-27 12:27:35 -0800493 dev_err(&client->dev,
494 "could not read packet, ret: %d\n", ret);
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700495 break;
496 }
497
498 payload = &payload_buffer[PAYLOAD_BODY];
499
500 switch (payload[RESPONSE_ID]) {
501 case NOTIFICATION_TOUCH:
502 /*
503 * Always report touch-events received while
504 * suspending, when being a wakeup source
505 */
506 if (ts->suspending && device_may_wakeup(&client->dev))
507 pm_wakeup_event(&client->dev, 500);
508 zforce_touch_event(ts, &payload[RESPONSE_DATA]);
509 break;
510
511 case NOTIFICATION_BOOTCOMPLETE:
512 ts->boot_complete = payload[RESPONSE_DATA];
513 zforce_complete(ts, payload[RESPONSE_ID], 0);
514 break;
515
516 case RESPONSE_INITIALIZE:
517 case RESPONSE_DEACTIVATE:
518 case RESPONSE_SETCONFIG:
519 case RESPONSE_RESOLUTION:
520 case RESPONSE_SCANFREQ:
521 zforce_complete(ts, payload[RESPONSE_ID],
522 payload[RESPONSE_DATA]);
523 break;
524
525 case RESPONSE_STATUS:
526 /*
527 * Version Payload Results
528 * [2:major] [2:minor] [2:build] [2:rev]
529 */
530 ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
531 payload[RESPONSE_DATA];
532 ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
533 payload[RESPONSE_DATA + 2];
534 ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
535 payload[RESPONSE_DATA + 4];
536 ts->version_rev = (payload[RESPONSE_DATA + 7] << 8) |
537 payload[RESPONSE_DATA + 6];
Luis Ortegaad697b92014-01-27 12:27:35 -0800538 dev_dbg(&ts->client->dev,
539 "Firmware Version %04x:%04x %04x:%04x\n",
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700540 ts->version_major, ts->version_minor,
541 ts->version_build, ts->version_rev);
542
543 zforce_complete(ts, payload[RESPONSE_ID], 0);
544 break;
545
546 case NOTIFICATION_INVALID_COMMAND:
547 dev_err(&ts->client->dev, "invalid command: 0x%x\n",
548 payload[RESPONSE_DATA]);
549 break;
550
551 default:
Luis Ortegaad697b92014-01-27 12:27:35 -0800552 dev_err(&ts->client->dev,
553 "unrecognized response id: 0x%x\n",
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700554 payload[RESPONSE_ID]);
555 break;
556 }
557 }
558
559 if (!ts->suspending && device_may_wakeup(&client->dev))
560 pm_relax(&client->dev);
561
562 dev_dbg(&client->dev, "finished interrupt\n");
563
564 return IRQ_HANDLED;
565}
566
567static int zforce_input_open(struct input_dev *dev)
568{
569 struct zforce_ts *ts = input_get_drvdata(dev);
570 int ret;
571
572 ret = zforce_start(ts);
573 if (ret)
574 return ret;
575
576 return 0;
577}
578
579static void zforce_input_close(struct input_dev *dev)
580{
581 struct zforce_ts *ts = input_get_drvdata(dev);
582 struct i2c_client *client = ts->client;
583 int ret;
584
585 ret = zforce_stop(ts);
586 if (ret)
587 dev_warn(&client->dev, "stopping zforce failed\n");
588
589 return;
590}
591
592#ifdef CONFIG_PM_SLEEP
593static int zforce_suspend(struct device *dev)
594{
595 struct i2c_client *client = to_i2c_client(dev);
596 struct zforce_ts *ts = i2c_get_clientdata(client);
597 struct input_dev *input = ts->input;
598 int ret = 0;
599
600 mutex_lock(&input->mutex);
601 ts->suspending = true;
602
603 /*
604 * When configured as a wakeup source device should always wake
605 * the system, therefore start device if necessary.
606 */
607 if (device_may_wakeup(&client->dev)) {
608 dev_dbg(&client->dev, "suspend while being a wakeup source\n");
609
610 /* Need to start device, if not open, to be a wakeup source. */
611 if (!input->users) {
612 ret = zforce_start(ts);
613 if (ret)
614 goto unlock;
615 }
616
617 enable_irq_wake(client->irq);
618 } else if (input->users) {
Luis Ortegaad697b92014-01-27 12:27:35 -0800619 dev_dbg(&client->dev,
620 "suspend without being a wakeup source\n");
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700621
622 ret = zforce_stop(ts);
623 if (ret)
624 goto unlock;
625
626 disable_irq(client->irq);
627 }
628
629 ts->suspended = true;
630
631unlock:
632 ts->suspending = false;
633 mutex_unlock(&input->mutex);
634
635 return ret;
636}
637
638static int zforce_resume(struct device *dev)
639{
640 struct i2c_client *client = to_i2c_client(dev);
641 struct zforce_ts *ts = i2c_get_clientdata(client);
642 struct input_dev *input = ts->input;
643 int ret = 0;
644
645 mutex_lock(&input->mutex);
646
647 ts->suspended = false;
648
649 if (device_may_wakeup(&client->dev)) {
650 dev_dbg(&client->dev, "resume from being a wakeup source\n");
651
652 disable_irq_wake(client->irq);
653
654 /* need to stop device if it was not open on suspend */
655 if (!input->users) {
656 ret = zforce_stop(ts);
657 if (ret)
658 goto unlock;
659 }
660 } else if (input->users) {
661 dev_dbg(&client->dev, "resume without being a wakeup source\n");
662
663 enable_irq(client->irq);
664
665 ret = zforce_start(ts);
666 if (ret < 0)
667 goto unlock;
668 }
669
670unlock:
671 mutex_unlock(&input->mutex);
672
673 return ret;
674}
675#endif
676
677static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
678
679static void zforce_reset(void *data)
680{
681 struct zforce_ts *ts = data;
682
683 gpio_set_value(ts->pdata->gpio_rst, 0);
684}
685
Heiko Stübner6f2e6c92014-01-27 12:37:21 -0800686static struct zforce_ts_platdata *zforce_parse_dt(struct device *dev)
687{
688 struct zforce_ts_platdata *pdata;
689 struct device_node *np = dev->of_node;
690
691 if (!np)
692 return ERR_PTR(-ENOENT);
693
694 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
695 if (!pdata) {
696 dev_err(dev, "failed to allocate platform data\n");
697 return ERR_PTR(-ENOMEM);
698 }
699
700 pdata->gpio_int = of_get_gpio(np, 0);
701 if (!gpio_is_valid(pdata->gpio_int)) {
702 dev_err(dev, "failed to get interrupt gpio\n");
703 return ERR_PTR(-EINVAL);
704 }
705
706 pdata->gpio_rst = of_get_gpio(np, 1);
707 if (!gpio_is_valid(pdata->gpio_rst)) {
708 dev_err(dev, "failed to get reset gpio\n");
709 return ERR_PTR(-EINVAL);
710 }
711
712 if (of_property_read_u32(np, "x-size", &pdata->x_max)) {
713 dev_err(dev, "failed to get x-size property\n");
714 return ERR_PTR(-EINVAL);
715 }
716
717 if (of_property_read_u32(np, "y-size", &pdata->y_max)) {
718 dev_err(dev, "failed to get y-size property\n");
719 return ERR_PTR(-EINVAL);
720 }
721
722 return pdata;
723}
724
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700725static int zforce_probe(struct i2c_client *client,
726 const struct i2c_device_id *id)
727{
728 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
729 struct zforce_ts *ts;
730 struct input_dev *input_dev;
731 int ret;
732
Heiko Stübner6f2e6c92014-01-27 12:37:21 -0800733 if (!pdata) {
734 pdata = zforce_parse_dt(&client->dev);
735 if (IS_ERR(pdata))
736 return PTR_ERR(pdata);
737 }
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700738
739 ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
740 if (!ts)
741 return -ENOMEM;
742
743 ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN,
744 "zforce_ts_int");
745 if (ret) {
746 dev_err(&client->dev, "request of gpio %d failed, %d\n",
747 pdata->gpio_int, ret);
748 return ret;
749 }
750
751 ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
752 GPIOF_OUT_INIT_LOW, "zforce_ts_rst");
753 if (ret) {
754 dev_err(&client->dev, "request of gpio %d failed, %d\n",
755 pdata->gpio_rst, ret);
756 return ret;
757 }
758
759 ret = devm_add_action(&client->dev, zforce_reset, ts);
760 if (ret) {
761 dev_err(&client->dev, "failed to register reset action, %d\n",
762 ret);
763 return ret;
764 }
765
766 snprintf(ts->phys, sizeof(ts->phys),
767 "%s/input0", dev_name(&client->dev));
768
769 input_dev = devm_input_allocate_device(&client->dev);
770 if (!input_dev) {
771 dev_err(&client->dev, "could not allocate input device\n");
772 return -ENOMEM;
773 }
774
775 mutex_init(&ts->access_mutex);
776 mutex_init(&ts->command_mutex);
777
778 ts->pdata = pdata;
779 ts->client = client;
780 ts->input = input_dev;
781
782 input_dev->name = "Neonode zForce touchscreen";
783 input_dev->phys = ts->phys;
784 input_dev->id.bustype = BUS_I2C;
785
786 input_dev->open = zforce_input_open;
787 input_dev->close = zforce_input_close;
788
789 __set_bit(EV_KEY, input_dev->evbit);
790 __set_bit(EV_SYN, input_dev->evbit);
791 __set_bit(EV_ABS, input_dev->evbit);
792
793 /* For multi touch */
794 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
795 pdata->x_max, 0, 0);
796 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
797 pdata->y_max, 0, 0);
798
799 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
800 ZFORCE_MAX_AREA, 0, 0);
801 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
802 ZFORCE_MAX_AREA, 0, 0);
803 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
804 input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
805
806 input_set_drvdata(ts->input, ts);
807
808 init_completion(&ts->command_done);
809
810 /*
811 * The zforce pulls the interrupt low when it has data ready.
812 * After it is triggered the isr thread runs until all the available
813 * packets have been read and the interrupt is high again.
814 * Therefore we can trigger the interrupt anytime it is low and do
815 * not need to limit it to the interrupt edge.
816 */
817 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
818 zforce_interrupt,
819 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
820 input_dev->name, ts);
821 if (ret) {
822 dev_err(&client->dev, "irq %d request failed\n", client->irq);
823 return ret;
824 }
825
826 i2c_set_clientdata(client, ts);
827
828 /* let the controller boot */
829 gpio_set_value(pdata->gpio_rst, 1);
830
831 ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
832 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
833 dev_warn(&client->dev, "bootcomplete timed out\n");
834
835 /* need to start device to get version information */
836 ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
837 if (ret) {
838 dev_err(&client->dev, "unable to initialize, %d\n", ret);
839 return ret;
840 }
841
Luis Ortegadeb49812014-01-27 12:27:06 -0800842 /* this gets the firmware version among other information */
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700843 ret = zforce_command_wait(ts, COMMAND_STATUS);
844 if (ret < 0) {
845 dev_err(&client->dev, "couldn't get status, %d\n", ret);
846 zforce_stop(ts);
847 return ret;
848 }
849
850 /* stop device and put it into sleep until it is opened */
851 ret = zforce_stop(ts);
852 if (ret < 0)
853 return ret;
854
855 device_set_wakeup_capable(&client->dev, true);
856
857 ret = input_register_device(input_dev);
858 if (ret) {
859 dev_err(&client->dev, "could not register input device, %d\n",
860 ret);
861 return ret;
862 }
863
864 return 0;
865}
866
867static struct i2c_device_id zforce_idtable[] = {
868 { "zforce-ts", 0 },
869 { }
870};
871MODULE_DEVICE_TABLE(i2c, zforce_idtable);
872
Heiko Stübner6f2e6c92014-01-27 12:37:21 -0800873#ifdef CONFIG_OF
874static struct of_device_id zforce_dt_idtable[] = {
875 { .compatible = "neonode,zforce" },
876 {},
877};
878MODULE_DEVICE_TABLE(of, zforce_dt_idtable);
879#endif
880
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700881static struct i2c_driver zforce_driver = {
882 .driver = {
883 .owner = THIS_MODULE,
884 .name = "zforce-ts",
885 .pm = &zforce_pm_ops,
Heiko Stübner6f2e6c92014-01-27 12:37:21 -0800886 .of_match_table = of_match_ptr(zforce_dt_idtable),
Heiko Stübnerc6d81bd2013-10-31 01:25:32 -0700887 },
888 .probe = zforce_probe,
889 .id_table = zforce_idtable,
890};
891
892module_i2c_driver(zforce_driver);
893
894MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
895MODULE_DESCRIPTION("zForce TouchScreen Driver");
896MODULE_LICENSE("GPL");