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