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