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