Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / touchscreen / zforce_ts.c
blob495629628af646c329a2585524f2f9bb309a4dc5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012-2013 MundoReader S.L.
4 * Author: Heiko Stuebner <heiko@sntech.de>
6 * based in parts on Nook zforce driver
8 * Copyright (C) 2010 Barnes & Noble, Inc.
9 * Author: Pieter Truter<ptruter@intrinsyc.com>
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>
27 #define WAIT_TIMEOUT msecs_to_jiffies(1000)
29 #define FRAME_START 0xee
30 #define FRAME_MAXSIZE 257
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
37 /* Response offsets */
38 #define RESPONSE_ID 0
39 #define RESPONSE_DATA 1
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
51 * Responses the controller sends as a result of
52 * command requests
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
62 * Notifications are sent by the touch controller without
63 * being requested by the driver and include for example
64 * touch indications
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
72 #define ZFORCE_REPORT_POINTS 2
73 #define ZFORCE_MAX_AREA 0xff
75 #define STATE_DOWN 0
76 #define STATE_MOVE 1
77 #define STATE_UP 2
79 #define SETCONFIG_DUALTOUCH (1 << 0)
81 struct 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;
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
106 struct zforce_ts {
107 struct i2c_client *client;
108 struct input_dev *input;
109 const struct zforce_ts_platdata *pdata;
110 char phys[32];
112 struct regulator *reg_vdd;
114 struct gpio_desc *gpio_int;
115 struct gpio_desc *gpio_rst;
117 bool suspending;
118 bool suspended;
119 bool boot_complete;
121 /* Firmware version information */
122 u16 version_major;
123 u16 version_minor;
124 u16 version_build;
125 u16 version_rev;
127 struct mutex access_mutex;
129 struct completion command_done;
130 struct mutex command_mutex;
131 int command_waiting;
132 int command_result;
135 static int zforce_command(struct zforce_ts *ts, u8 cmd)
137 struct i2c_client *client = ts->client;
138 char buf[3];
139 int ret;
141 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
143 buf[0] = FRAME_START;
144 buf[1] = 1; /* data size, command only */
145 buf[2] = cmd;
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;
155 return 0;
158 static void zforce_reset_assert(struct zforce_ts *ts)
160 gpiod_set_value_cansleep(ts->gpio_rst, 1);
163 static void zforce_reset_deassert(struct zforce_ts *ts)
165 gpiod_set_value_cansleep(ts->gpio_rst, 0);
168 static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
170 struct i2c_client *client = ts->client;
171 int ret;
173 ret = mutex_trylock(&ts->command_mutex);
174 if (!ret) {
175 dev_err(&client->dev, "already waiting for a command\n");
176 return -EBUSY;
179 dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
180 buf[1], buf[2]);
182 ts->command_waiting = buf[2];
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;
192 dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
194 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) {
195 ret = -ETIME;
196 goto unlock;
199 ret = ts->command_result;
201 unlock:
202 mutex_unlock(&ts->command_mutex);
203 return ret;
206 static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
208 struct i2c_client *client = ts->client;
209 char buf[3];
210 int ret;
212 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
214 buf[0] = FRAME_START;
215 buf[1] = 1; /* data size, command only */
216 buf[2] = cmd;
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;
224 return 0;
227 static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
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) };
234 dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
236 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
239 static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
240 u16 stylus)
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) };
248 dev_dbg(&client->dev,
249 "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
250 idle, finger, stylus);
252 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
255 static int zforce_setconfig(struct zforce_ts *ts, char b1)
257 struct i2c_client *client = ts->client;
258 char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
259 b1, 0, 0, 0 };
261 dev_dbg(&client->dev, "set config to (%d)\n", b1);
263 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
266 static int zforce_start(struct zforce_ts *ts)
268 struct i2c_client *client = ts->client;
269 const struct zforce_ts_platdata *pdata = ts->pdata;
270 int ret;
272 dev_dbg(&client->dev, "starting device\n");
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;
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;
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;
293 ret = zforce_setconfig(ts, SETCONFIG_DUALTOUCH);
294 if (ret) {
295 dev_err(&client->dev, "Unable to set config\n");
296 goto error;
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;
307 * Per NN, initial cal. take max. of 200msec.
308 * Allow time to complete this calibration
310 msleep(200);
312 return 0;
314 error:
315 zforce_command_wait(ts, COMMAND_DEACTIVATE);
316 return ret;
319 static int zforce_stop(struct zforce_ts *ts)
321 struct i2c_client *client = ts->client;
322 int ret;
324 dev_dbg(&client->dev, "stopping device\n");
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;
334 return 0;
337 static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
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;
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;
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];
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;
365 point.state = payload[9 * i + 5] & 0x0f;
366 point.id = (payload[9 * i + 5] & 0xf0) >> 4;
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];
375 point.pressure = payload[9 * i + 8];
376 point.prblty = payload[9 * i + 9];
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);
386 /* the zforce id starts with "1", so needs to be decreased */
387 input_mt_slot(ts->input, point.id - 1);
389 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
390 point.state != STATE_UP);
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++;
407 input_mt_sync_frame(ts->input);
409 input_mt_report_finger_count(ts->input, num);
411 input_sync(ts->input);
413 return 0;
416 static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
418 struct i2c_client *client = ts->client;
419 int ret;
421 mutex_lock(&ts->access_mutex);
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;
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;
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;
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;
450 dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
451 buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
453 unlock:
454 mutex_unlock(&ts->access_mutex);
455 return ret;
458 static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
460 struct i2c_client *client = ts->client;
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);
471 static irqreturn_t zforce_irq(int irq, void *dev_id)
473 struct zforce_ts *ts = dev_id;
474 struct i2c_client *client = ts->client;
476 if (ts->suspended && device_may_wakeup(&client->dev))
477 pm_wakeup_event(&client->dev, 500);
479 return IRQ_WAKE_THREAD;
482 static irqreturn_t zforce_irq_thread(int irq, void *dev_id)
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;
491 * When still suspended, return.
492 * Due to the level-interrupt we will get re-triggered later.
494 if (ts->suspended) {
495 msleep(20);
496 return IRQ_HANDLED;
499 dev_dbg(&client->dev, "handling interrupt\n");
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);
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)
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;
522 payload = &payload_buffer[PAYLOAD_BODY];
524 switch (payload[RESPONSE_ID]) {
525 case NOTIFICATION_TOUCH:
527 * Always report touch-events received while
528 * suspending, when being a wakeup source
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;
535 case NOTIFICATION_BOOTCOMPLETE:
536 ts->boot_complete = payload[RESPONSE_DATA];
537 zforce_complete(ts, payload[RESPONSE_ID], 0);
538 break;
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;
549 case RESPONSE_STATUS:
551 * Version Payload Results
552 * [2:major] [2:minor] [2:build] [2:rev]
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);
567 zforce_complete(ts, payload[RESPONSE_ID], 0);
568 break;
570 case NOTIFICATION_INVALID_COMMAND:
571 dev_err(&ts->client->dev, "invalid command: 0x%x\n",
572 payload[RESPONSE_DATA]);
573 break;
575 default:
576 dev_err(&ts->client->dev,
577 "unrecognized response id: 0x%x\n",
578 payload[RESPONSE_ID]);
579 break;
581 } while (gpiod_get_value_cansleep(ts->gpio_int));
583 if (!ts->suspending && device_may_wakeup(&client->dev))
584 pm_relax(&client->dev);
586 dev_dbg(&client->dev, "finished interrupt\n");
588 return IRQ_HANDLED;
591 static int zforce_input_open(struct input_dev *dev)
593 struct zforce_ts *ts = input_get_drvdata(dev);
595 return zforce_start(ts);
598 static void zforce_input_close(struct input_dev *dev)
600 struct zforce_ts *ts = input_get_drvdata(dev);
601 struct i2c_client *client = ts->client;
602 int ret;
604 ret = zforce_stop(ts);
605 if (ret)
606 dev_warn(&client->dev, "stopping zforce failed\n");
608 return;
611 static int __maybe_unused zforce_suspend(struct device *dev)
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;
618 mutex_lock(&input->mutex);
619 ts->suspending = true;
622 * When configured as a wakeup source device should always wake
623 * the system, therefore start device if necessary.
625 if (device_may_wakeup(&client->dev)) {
626 dev_dbg(&client->dev, "suspend while being a wakeup source\n");
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;
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");
640 ret = zforce_stop(ts);
641 if (ret)
642 goto unlock;
644 disable_irq(client->irq);
647 ts->suspended = true;
649 unlock:
650 ts->suspending = false;
651 mutex_unlock(&input->mutex);
653 return ret;
656 static int __maybe_unused zforce_resume(struct device *dev)
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;
663 mutex_lock(&input->mutex);
665 ts->suspended = false;
667 if (device_may_wakeup(&client->dev)) {
668 dev_dbg(&client->dev, "resume from being a wakeup source\n");
670 disable_irq_wake(client->irq);
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;
678 } else if (input_device_enabled(input)) {
679 dev_dbg(&client->dev, "resume without being a wakeup source\n");
681 enable_irq(client->irq);
683 ret = zforce_start(ts);
684 if (ret < 0)
685 goto unlock;
688 unlock:
689 mutex_unlock(&input->mutex);
691 return ret;
694 static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
696 static void zforce_reset(void *data)
698 struct zforce_ts *ts = data;
700 zforce_reset_assert(ts);
702 udelay(10);
704 if (!IS_ERR(ts->reg_vdd))
705 regulator_disable(ts->reg_vdd);
708 static struct zforce_ts_platdata *zforce_parse_dt(struct device *dev)
710 struct zforce_ts_platdata *pdata;
711 struct device_node *np = dev->of_node;
713 if (!np)
714 return ERR_PTR(-ENOENT);
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);
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);
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);
732 return pdata;
735 static int zforce_probe(struct i2c_client *client,
736 const struct i2c_device_id *id)
738 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
739 struct zforce_ts *ts;
740 struct input_dev *input_dev;
741 int ret;
743 if (!pdata) {
744 pdata = zforce_parse_dt(&client->dev);
745 if (IS_ERR(pdata))
746 return PTR_ERR(pdata);
749 ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
750 if (!ts)
751 return -ENOMEM;
753 ts->gpio_rst = devm_gpiod_get_optional(&client->dev, "reset",
754 GPIOD_OUT_HIGH);
755 if (IS_ERR(ts->gpio_rst)) {
756 ret = PTR_ERR(ts->gpio_rst);
757 dev_err(&client->dev,
758 "failed to request reset GPIO: %d\n", ret);
759 return ret;
762 if (ts->gpio_rst) {
763 ts->gpio_int = devm_gpiod_get_optional(&client->dev, "irq",
764 GPIOD_IN);
765 if (IS_ERR(ts->gpio_int)) {
766 ret = PTR_ERR(ts->gpio_int);
767 dev_err(&client->dev,
768 "failed to request interrupt GPIO: %d\n", ret);
769 return ret;
771 } else {
773 * Deprecated GPIO handling for compatibility
774 * with legacy binding.
777 /* INT GPIO */
778 ts->gpio_int = devm_gpiod_get_index(&client->dev, NULL, 0,
779 GPIOD_IN);
780 if (IS_ERR(ts->gpio_int)) {
781 ret = PTR_ERR(ts->gpio_int);
782 dev_err(&client->dev,
783 "failed to request interrupt GPIO: %d\n", ret);
784 return ret;
787 /* RST GPIO */
788 ts->gpio_rst = devm_gpiod_get_index(&client->dev, NULL, 1,
789 GPIOD_OUT_HIGH);
790 if (IS_ERR(ts->gpio_rst)) {
791 ret = PTR_ERR(ts->gpio_rst);
792 dev_err(&client->dev,
793 "failed to request reset GPIO: %d\n", ret);
794 return ret;
798 ts->reg_vdd = devm_regulator_get_optional(&client->dev, "vdd");
799 if (IS_ERR(ts->reg_vdd)) {
800 ret = PTR_ERR(ts->reg_vdd);
801 if (ret == -EPROBE_DEFER)
802 return ret;
803 } else {
804 ret = regulator_enable(ts->reg_vdd);
805 if (ret)
806 return ret;
809 * according to datasheet add 100us grace time after regular
810 * regulator enable delay.
812 udelay(100);
815 ret = devm_add_action(&client->dev, zforce_reset, ts);
816 if (ret) {
817 dev_err(&client->dev, "failed to register reset action, %d\n",
818 ret);
820 /* hereafter the regulator will be disabled by the action */
821 if (!IS_ERR(ts->reg_vdd))
822 regulator_disable(ts->reg_vdd);
824 return ret;
827 snprintf(ts->phys, sizeof(ts->phys),
828 "%s/input0", dev_name(&client->dev));
830 input_dev = devm_input_allocate_device(&client->dev);
831 if (!input_dev) {
832 dev_err(&client->dev, "could not allocate input device\n");
833 return -ENOMEM;
836 mutex_init(&ts->access_mutex);
837 mutex_init(&ts->command_mutex);
839 ts->pdata = pdata;
840 ts->client = client;
841 ts->input = input_dev;
843 input_dev->name = "Neonode zForce touchscreen";
844 input_dev->phys = ts->phys;
845 input_dev->id.bustype = BUS_I2C;
847 input_dev->open = zforce_input_open;
848 input_dev->close = zforce_input_close;
850 __set_bit(EV_KEY, input_dev->evbit);
851 __set_bit(EV_SYN, input_dev->evbit);
852 __set_bit(EV_ABS, input_dev->evbit);
854 /* For multi touch */
855 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
856 pdata->x_max, 0, 0);
857 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
858 pdata->y_max, 0, 0);
860 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
861 ZFORCE_MAX_AREA, 0, 0);
862 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
863 ZFORCE_MAX_AREA, 0, 0);
864 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
865 input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
867 input_set_drvdata(ts->input, ts);
869 init_completion(&ts->command_done);
872 * The zforce pulls the interrupt low when it has data ready.
873 * After it is triggered the isr thread runs until all the available
874 * packets have been read and the interrupt is high again.
875 * Therefore we can trigger the interrupt anytime it is low and do
876 * not need to limit it to the interrupt edge.
878 ret = devm_request_threaded_irq(&client->dev, client->irq,
879 zforce_irq, zforce_irq_thread,
880 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
881 input_dev->name, ts);
882 if (ret) {
883 dev_err(&client->dev, "irq %d request failed\n", client->irq);
884 return ret;
887 i2c_set_clientdata(client, ts);
889 /* let the controller boot */
890 zforce_reset_deassert(ts);
892 ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
893 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
894 dev_warn(&client->dev, "bootcomplete timed out\n");
896 /* need to start device to get version information */
897 ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
898 if (ret) {
899 dev_err(&client->dev, "unable to initialize, %d\n", ret);
900 return ret;
903 /* this gets the firmware version among other information */
904 ret = zforce_command_wait(ts, COMMAND_STATUS);
905 if (ret < 0) {
906 dev_err(&client->dev, "couldn't get status, %d\n", ret);
907 zforce_stop(ts);
908 return ret;
911 /* stop device and put it into sleep until it is opened */
912 ret = zforce_stop(ts);
913 if (ret < 0)
914 return ret;
916 device_set_wakeup_capable(&client->dev, true);
918 ret = input_register_device(input_dev);
919 if (ret) {
920 dev_err(&client->dev, "could not register input device, %d\n",
921 ret);
922 return ret;
925 return 0;
928 static struct i2c_device_id zforce_idtable[] = {
929 { "zforce-ts", 0 },
932 MODULE_DEVICE_TABLE(i2c, zforce_idtable);
934 #ifdef CONFIG_OF
935 static const struct of_device_id zforce_dt_idtable[] = {
936 { .compatible = "neonode,zforce" },
939 MODULE_DEVICE_TABLE(of, zforce_dt_idtable);
940 #endif
942 static struct i2c_driver zforce_driver = {
943 .driver = {
944 .name = "zforce-ts",
945 .pm = &zforce_pm_ops,
946 .of_match_table = of_match_ptr(zforce_dt_idtable),
948 .probe = zforce_probe,
949 .id_table = zforce_idtable,
952 module_i2c_driver(zforce_driver);
954 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
955 MODULE_DESCRIPTION("zForce TouchScreen Driver");
956 MODULE_LICENSE("GPL");