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