Linux 4.1.16
[linux/fpc-iii.git] / drivers / input / touchscreen / zforce_ts.c
blob19880c7385e35ab0c93d4d5746e081ca84b9b108
1 /*
2 * Copyright (C) 2012-2013 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
5 * based in parts on Nook zforce driver
7 * Copyright (C) 2010 Barnes & Noble, Inc.
8 * Author: Pieter Truter<ptruter@intrinsyc.com>
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.
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.
20 #include <linux/module.h>
21 #include <linux/hrtimer.h>
22 #include <linux/slab.h>
23 #include <linux/input.h>
24 #include <linux/interrupt.h>
25 #include <linux/i2c.h>
26 #include <linux/delay.h>
27 #include <linux/gpio.h>
28 #include <linux/device.h>
29 #include <linux/sysfs.h>
30 #include <linux/input/mt.h>
31 #include <linux/platform_data/zforce_ts.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/delay.h>
34 #include <linux/of.h>
35 #include <linux/of_gpio.h>
37 #define WAIT_TIMEOUT msecs_to_jiffies(1000)
39 #define FRAME_START 0xee
40 #define FRAME_MAXSIZE 257
42 /* Offsets of the different parts of the payload the controller sends */
43 #define PAYLOAD_HEADER 0
44 #define PAYLOAD_LENGTH 1
45 #define PAYLOAD_BODY 2
47 /* Response offsets */
48 #define RESPONSE_ID 0
49 #define RESPONSE_DATA 1
51 /* Commands */
52 #define COMMAND_DEACTIVATE 0x00
53 #define COMMAND_INITIALIZE 0x01
54 #define COMMAND_RESOLUTION 0x02
55 #define COMMAND_SETCONFIG 0x03
56 #define COMMAND_DATAREQUEST 0x04
57 #define COMMAND_SCANFREQ 0x08
58 #define COMMAND_STATUS 0X1e
61 * Responses the controller sends as a result of
62 * command requests
64 #define RESPONSE_DEACTIVATE 0x00
65 #define RESPONSE_INITIALIZE 0x01
66 #define RESPONSE_RESOLUTION 0x02
67 #define RESPONSE_SETCONFIG 0x03
68 #define RESPONSE_SCANFREQ 0x08
69 #define RESPONSE_STATUS 0X1e
72 * Notifications are sent by the touch controller without
73 * being requested by the driver and include for example
74 * touch indications
76 #define NOTIFICATION_TOUCH 0x04
77 #define NOTIFICATION_BOOTCOMPLETE 0x07
78 #define NOTIFICATION_OVERRUN 0x25
79 #define NOTIFICATION_PROXIMITY 0x26
80 #define NOTIFICATION_INVALID_COMMAND 0xfe
82 #define ZFORCE_REPORT_POINTS 2
83 #define ZFORCE_MAX_AREA 0xff
85 #define STATE_DOWN 0
86 #define STATE_MOVE 1
87 #define STATE_UP 2
89 #define SETCONFIG_DUALTOUCH (1 << 0)
91 struct zforce_point {
92 int coord_x;
93 int coord_y;
94 int state;
95 int id;
96 int area_major;
97 int area_minor;
98 int orientation;
99 int pressure;
100 int prblty;
104 * @client the i2c_client
105 * @input the input device
106 * @suspending in the process of going to suspend (don't emit wakeup
107 * events for commands executed to suspend the device)
108 * @suspended device suspended
109 * @access_mutex serialize i2c-access, to keep multipart reads together
110 * @command_done completion to wait for the command result
111 * @command_mutex serialize commands sent to the ic
112 * @command_waiting the id of the command that is currently waiting
113 * for a result
114 * @command_result returned result of the command
116 struct zforce_ts {
117 struct i2c_client *client;
118 struct input_dev *input;
119 const struct zforce_ts_platdata *pdata;
120 char phys[32];
122 struct regulator *reg_vdd;
124 bool suspending;
125 bool suspended;
126 bool boot_complete;
128 /* Firmware version information */
129 u16 version_major;
130 u16 version_minor;
131 u16 version_build;
132 u16 version_rev;
134 struct mutex access_mutex;
136 struct completion command_done;
137 struct mutex command_mutex;
138 int command_waiting;
139 int command_result;
142 static int zforce_command(struct zforce_ts *ts, u8 cmd)
144 struct i2c_client *client = ts->client;
145 char buf[3];
146 int ret;
148 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
150 buf[0] = FRAME_START;
151 buf[1] = 1; /* data size, command only */
152 buf[2] = cmd;
154 mutex_lock(&ts->access_mutex);
155 ret = i2c_master_send(client, &buf[0], ARRAY_SIZE(buf));
156 mutex_unlock(&ts->access_mutex);
157 if (ret < 0) {
158 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
159 return ret;
162 return 0;
165 static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
167 struct i2c_client *client = ts->client;
168 int ret;
170 ret = mutex_trylock(&ts->command_mutex);
171 if (!ret) {
172 dev_err(&client->dev, "already waiting for a command\n");
173 return -EBUSY;
176 dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
177 buf[1], buf[2]);
179 ts->command_waiting = buf[2];
181 mutex_lock(&ts->access_mutex);
182 ret = i2c_master_send(client, buf, len);
183 mutex_unlock(&ts->access_mutex);
184 if (ret < 0) {
185 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
186 goto unlock;
189 dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
191 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) {
192 ret = -ETIME;
193 goto unlock;
196 ret = ts->command_result;
198 unlock:
199 mutex_unlock(&ts->command_mutex);
200 return ret;
203 static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
205 struct i2c_client *client = ts->client;
206 char buf[3];
207 int ret;
209 dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
211 buf[0] = FRAME_START;
212 buf[1] = 1; /* data size, command only */
213 buf[2] = cmd;
215 ret = zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
216 if (ret < 0) {
217 dev_err(&client->dev, "i2c send data request error: %d\n", ret);
218 return ret;
221 return 0;
224 static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
226 struct i2c_client *client = ts->client;
227 char buf[7] = { FRAME_START, 5, COMMAND_RESOLUTION,
228 (x & 0xff), ((x >> 8) & 0xff),
229 (y & 0xff), ((y >> 8) & 0xff) };
231 dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
233 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
236 static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
237 u16 stylus)
239 struct i2c_client *client = ts->client;
240 char buf[9] = { FRAME_START, 7, COMMAND_SCANFREQ,
241 (idle & 0xff), ((idle >> 8) & 0xff),
242 (finger & 0xff), ((finger >> 8) & 0xff),
243 (stylus & 0xff), ((stylus >> 8) & 0xff) };
245 dev_dbg(&client->dev,
246 "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
247 idle, finger, stylus);
249 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
252 static int zforce_setconfig(struct zforce_ts *ts, char b1)
254 struct i2c_client *client = ts->client;
255 char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
256 b1, 0, 0, 0 };
258 dev_dbg(&client->dev, "set config to (%d)\n", b1);
260 return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
263 static int zforce_start(struct zforce_ts *ts)
265 struct i2c_client *client = ts->client;
266 const struct zforce_ts_platdata *pdata = ts->pdata;
267 int ret;
269 dev_dbg(&client->dev, "starting device\n");
271 ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
272 if (ret) {
273 dev_err(&client->dev, "Unable to initialize, %d\n", ret);
274 return ret;
277 ret = zforce_resolution(ts, pdata->x_max, pdata->y_max);
278 if (ret) {
279 dev_err(&client->dev, "Unable to set resolution, %d\n", ret);
280 goto error;
283 ret = zforce_scan_frequency(ts, 10, 50, 50);
284 if (ret) {
285 dev_err(&client->dev, "Unable to set scan frequency, %d\n",
286 ret);
287 goto error;
290 ret = zforce_setconfig(ts, SETCONFIG_DUALTOUCH);
291 if (ret) {
292 dev_err(&client->dev, "Unable to set config\n");
293 goto error;
296 /* start sending touch events */
297 ret = zforce_command(ts, COMMAND_DATAREQUEST);
298 if (ret) {
299 dev_err(&client->dev, "Unable to request data\n");
300 goto error;
304 * Per NN, initial cal. take max. of 200msec.
305 * Allow time to complete this calibration
307 msleep(200);
309 return 0;
311 error:
312 zforce_command_wait(ts, COMMAND_DEACTIVATE);
313 return ret;
316 static int zforce_stop(struct zforce_ts *ts)
318 struct i2c_client *client = ts->client;
319 int ret;
321 dev_dbg(&client->dev, "stopping device\n");
323 /* Deactivates touch sensing and puts the device into sleep. */
324 ret = zforce_command_wait(ts, COMMAND_DEACTIVATE);
325 if (ret != 0) {
326 dev_err(&client->dev, "could not deactivate device, %d\n",
327 ret);
328 return ret;
331 return 0;
334 static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
336 struct i2c_client *client = ts->client;
337 const struct zforce_ts_platdata *pdata = ts->pdata;
338 struct zforce_point point;
339 int count, i, num = 0;
341 count = payload[0];
342 if (count > ZFORCE_REPORT_POINTS) {
343 dev_warn(&client->dev,
344 "too many coordinates %d, expected max %d\n",
345 count, ZFORCE_REPORT_POINTS);
346 count = ZFORCE_REPORT_POINTS;
349 for (i = 0; i < count; i++) {
350 point.coord_x =
351 payload[9 * i + 2] << 8 | payload[9 * i + 1];
352 point.coord_y =
353 payload[9 * i + 4] << 8 | payload[9 * i + 3];
355 if (point.coord_x > pdata->x_max ||
356 point.coord_y > pdata->y_max) {
357 dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
358 point.coord_x, point.coord_y);
359 point.coord_x = point.coord_y = 0;
362 point.state = payload[9 * i + 5] & 0x03;
363 point.id = (payload[9 * i + 5] & 0xfc) >> 2;
365 /* determine touch major, minor and orientation */
366 point.area_major = max(payload[9 * i + 6],
367 payload[9 * i + 7]);
368 point.area_minor = min(payload[9 * i + 6],
369 payload[9 * i + 7]);
370 point.orientation = payload[9 * i + 6] > payload[9 * i + 7];
372 point.pressure = payload[9 * i + 8];
373 point.prblty = payload[9 * i + 9];
375 dev_dbg(&client->dev,
376 "point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n",
377 i, count, point.state, point.id,
378 point.pressure, point.prblty,
379 point.coord_x, point.coord_y,
380 point.area_major, point.area_minor,
381 point.orientation);
383 /* the zforce id starts with "1", so needs to be decreased */
384 input_mt_slot(ts->input, point.id - 1);
386 input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
387 point.state != STATE_UP);
389 if (point.state != STATE_UP) {
390 input_report_abs(ts->input, ABS_MT_POSITION_X,
391 point.coord_x);
392 input_report_abs(ts->input, ABS_MT_POSITION_Y,
393 point.coord_y);
394 input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
395 point.area_major);
396 input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
397 point.area_minor);
398 input_report_abs(ts->input, ABS_MT_ORIENTATION,
399 point.orientation);
400 num++;
404 input_mt_sync_frame(ts->input);
406 input_mt_report_finger_count(ts->input, num);
408 input_sync(ts->input);
410 return 0;
413 static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
415 struct i2c_client *client = ts->client;
416 int ret;
418 mutex_lock(&ts->access_mutex);
420 /* read 2 byte message header */
421 ret = i2c_master_recv(client, buf, 2);
422 if (ret < 0) {
423 dev_err(&client->dev, "error reading header: %d\n", ret);
424 goto unlock;
427 if (buf[PAYLOAD_HEADER] != FRAME_START) {
428 dev_err(&client->dev, "invalid frame start: %d\n", buf[0]);
429 ret = -EIO;
430 goto unlock;
433 if (buf[PAYLOAD_LENGTH] == 0) {
434 dev_err(&client->dev, "invalid payload length: %d\n",
435 buf[PAYLOAD_LENGTH]);
436 ret = -EIO;
437 goto unlock;
440 /* read the message */
441 ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]);
442 if (ret < 0) {
443 dev_err(&client->dev, "error reading payload: %d\n", ret);
444 goto unlock;
447 dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
448 buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
450 unlock:
451 mutex_unlock(&ts->access_mutex);
452 return ret;
455 static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
457 struct i2c_client *client = ts->client;
459 if (ts->command_waiting == cmd) {
460 dev_dbg(&client->dev, "completing command 0x%x\n", cmd);
461 ts->command_result = result;
462 complete(&ts->command_done);
463 } else {
464 dev_dbg(&client->dev, "command %d not for us\n", cmd);
468 static irqreturn_t zforce_irq(int irq, void *dev_id)
470 struct zforce_ts *ts = dev_id;
471 struct i2c_client *client = ts->client;
473 if (ts->suspended && device_may_wakeup(&client->dev))
474 pm_wakeup_event(&client->dev, 500);
476 return IRQ_WAKE_THREAD;
479 static irqreturn_t zforce_irq_thread(int irq, void *dev_id)
481 struct zforce_ts *ts = dev_id;
482 struct i2c_client *client = ts->client;
483 const struct zforce_ts_platdata *pdata = ts->pdata;
484 int ret;
485 u8 payload_buffer[FRAME_MAXSIZE];
486 u8 *payload;
489 * When still suspended, return.
490 * Due to the level-interrupt we will get re-triggered later.
492 if (ts->suspended) {
493 msleep(20);
494 return IRQ_HANDLED;
497 dev_dbg(&client->dev, "handling interrupt\n");
499 /* Don't emit wakeup events from commands run by zforce_suspend */
500 if (!ts->suspending && device_may_wakeup(&client->dev))
501 pm_stay_awake(&client->dev);
503 while (!gpio_get_value(pdata->gpio_int)) {
504 ret = zforce_read_packet(ts, payload_buffer);
505 if (ret < 0) {
506 dev_err(&client->dev,
507 "could not read packet, ret: %d\n", ret);
508 break;
511 payload = &payload_buffer[PAYLOAD_BODY];
513 switch (payload[RESPONSE_ID]) {
514 case NOTIFICATION_TOUCH:
516 * Always report touch-events received while
517 * suspending, when being a wakeup source
519 if (ts->suspending && device_may_wakeup(&client->dev))
520 pm_wakeup_event(&client->dev, 500);
521 zforce_touch_event(ts, &payload[RESPONSE_DATA]);
522 break;
524 case NOTIFICATION_BOOTCOMPLETE:
525 ts->boot_complete = payload[RESPONSE_DATA];
526 zforce_complete(ts, payload[RESPONSE_ID], 0);
527 break;
529 case RESPONSE_INITIALIZE:
530 case RESPONSE_DEACTIVATE:
531 case RESPONSE_SETCONFIG:
532 case RESPONSE_RESOLUTION:
533 case RESPONSE_SCANFREQ:
534 zforce_complete(ts, payload[RESPONSE_ID],
535 payload[RESPONSE_DATA]);
536 break;
538 case RESPONSE_STATUS:
540 * Version Payload Results
541 * [2:major] [2:minor] [2:build] [2:rev]
543 ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
544 payload[RESPONSE_DATA];
545 ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
546 payload[RESPONSE_DATA + 2];
547 ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
548 payload[RESPONSE_DATA + 4];
549 ts->version_rev = (payload[RESPONSE_DATA + 7] << 8) |
550 payload[RESPONSE_DATA + 6];
551 dev_dbg(&ts->client->dev,
552 "Firmware Version %04x:%04x %04x:%04x\n",
553 ts->version_major, ts->version_minor,
554 ts->version_build, ts->version_rev);
556 zforce_complete(ts, payload[RESPONSE_ID], 0);
557 break;
559 case NOTIFICATION_INVALID_COMMAND:
560 dev_err(&ts->client->dev, "invalid command: 0x%x\n",
561 payload[RESPONSE_DATA]);
562 break;
564 default:
565 dev_err(&ts->client->dev,
566 "unrecognized response id: 0x%x\n",
567 payload[RESPONSE_ID]);
568 break;
572 if (!ts->suspending && device_may_wakeup(&client->dev))
573 pm_relax(&client->dev);
575 dev_dbg(&client->dev, "finished interrupt\n");
577 return IRQ_HANDLED;
580 static int zforce_input_open(struct input_dev *dev)
582 struct zforce_ts *ts = input_get_drvdata(dev);
583 int ret;
585 ret = zforce_start(ts);
586 if (ret)
587 return ret;
589 return 0;
592 static void zforce_input_close(struct input_dev *dev)
594 struct zforce_ts *ts = input_get_drvdata(dev);
595 struct i2c_client *client = ts->client;
596 int ret;
598 ret = zforce_stop(ts);
599 if (ret)
600 dev_warn(&client->dev, "stopping zforce failed\n");
602 return;
605 static int __maybe_unused zforce_suspend(struct device *dev)
607 struct i2c_client *client = to_i2c_client(dev);
608 struct zforce_ts *ts = i2c_get_clientdata(client);
609 struct input_dev *input = ts->input;
610 int ret = 0;
612 mutex_lock(&input->mutex);
613 ts->suspending = true;
616 * When configured as a wakeup source device should always wake
617 * the system, therefore start device if necessary.
619 if (device_may_wakeup(&client->dev)) {
620 dev_dbg(&client->dev, "suspend while being a wakeup source\n");
622 /* Need to start device, if not open, to be a wakeup source. */
623 if (!input->users) {
624 ret = zforce_start(ts);
625 if (ret)
626 goto unlock;
629 enable_irq_wake(client->irq);
630 } else if (input->users) {
631 dev_dbg(&client->dev,
632 "suspend without being a wakeup source\n");
634 ret = zforce_stop(ts);
635 if (ret)
636 goto unlock;
638 disable_irq(client->irq);
641 ts->suspended = true;
643 unlock:
644 ts->suspending = false;
645 mutex_unlock(&input->mutex);
647 return ret;
650 static int __maybe_unused zforce_resume(struct device *dev)
652 struct i2c_client *client = to_i2c_client(dev);
653 struct zforce_ts *ts = i2c_get_clientdata(client);
654 struct input_dev *input = ts->input;
655 int ret = 0;
657 mutex_lock(&input->mutex);
659 ts->suspended = false;
661 if (device_may_wakeup(&client->dev)) {
662 dev_dbg(&client->dev, "resume from being a wakeup source\n");
664 disable_irq_wake(client->irq);
666 /* need to stop device if it was not open on suspend */
667 if (!input->users) {
668 ret = zforce_stop(ts);
669 if (ret)
670 goto unlock;
672 } else if (input->users) {
673 dev_dbg(&client->dev, "resume without being a wakeup source\n");
675 enable_irq(client->irq);
677 ret = zforce_start(ts);
678 if (ret < 0)
679 goto unlock;
682 unlock:
683 mutex_unlock(&input->mutex);
685 return ret;
688 static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
690 static void zforce_reset(void *data)
692 struct zforce_ts *ts = data;
694 gpio_set_value(ts->pdata->gpio_rst, 0);
696 udelay(10);
698 if (!IS_ERR(ts->reg_vdd))
699 regulator_disable(ts->reg_vdd);
702 static struct zforce_ts_platdata *zforce_parse_dt(struct device *dev)
704 struct zforce_ts_platdata *pdata;
705 struct device_node *np = dev->of_node;
707 if (!np)
708 return ERR_PTR(-ENOENT);
710 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
711 if (!pdata) {
712 dev_err(dev, "failed to allocate platform data\n");
713 return ERR_PTR(-ENOMEM);
716 pdata->gpio_int = of_get_gpio(np, 0);
717 if (!gpio_is_valid(pdata->gpio_int)) {
718 dev_err(dev, "failed to get interrupt gpio\n");
719 return ERR_PTR(-EINVAL);
722 pdata->gpio_rst = of_get_gpio(np, 1);
723 if (!gpio_is_valid(pdata->gpio_rst)) {
724 dev_err(dev, "failed to get reset gpio\n");
725 return ERR_PTR(-EINVAL);
728 if (of_property_read_u32(np, "x-size", &pdata->x_max)) {
729 dev_err(dev, "failed to get x-size property\n");
730 return ERR_PTR(-EINVAL);
733 if (of_property_read_u32(np, "y-size", &pdata->y_max)) {
734 dev_err(dev, "failed to get y-size property\n");
735 return ERR_PTR(-EINVAL);
738 return pdata;
741 static int zforce_probe(struct i2c_client *client,
742 const struct i2c_device_id *id)
744 const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
745 struct zforce_ts *ts;
746 struct input_dev *input_dev;
747 int ret;
749 if (!pdata) {
750 pdata = zforce_parse_dt(&client->dev);
751 if (IS_ERR(pdata))
752 return PTR_ERR(pdata);
755 ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
756 if (!ts)
757 return -ENOMEM;
759 ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN,
760 "zforce_ts_int");
761 if (ret) {
762 dev_err(&client->dev, "request of gpio %d failed, %d\n",
763 pdata->gpio_int, ret);
764 return ret;
767 ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
768 GPIOF_OUT_INIT_LOW, "zforce_ts_rst");
769 if (ret) {
770 dev_err(&client->dev, "request of gpio %d failed, %d\n",
771 pdata->gpio_rst, ret);
772 return ret;
775 ts->reg_vdd = devm_regulator_get_optional(&client->dev, "vdd");
776 if (IS_ERR(ts->reg_vdd)) {
777 ret = PTR_ERR(ts->reg_vdd);
778 if (ret == -EPROBE_DEFER)
779 return ret;
780 } else {
781 ret = regulator_enable(ts->reg_vdd);
782 if (ret)
783 return ret;
786 * according to datasheet add 100us grace time after regular
787 * regulator enable delay.
789 udelay(100);
792 ret = devm_add_action(&client->dev, zforce_reset, ts);
793 if (ret) {
794 dev_err(&client->dev, "failed to register reset action, %d\n",
795 ret);
797 /* hereafter the regulator will be disabled by the action */
798 if (!IS_ERR(ts->reg_vdd))
799 regulator_disable(ts->reg_vdd);
801 return ret;
804 snprintf(ts->phys, sizeof(ts->phys),
805 "%s/input0", dev_name(&client->dev));
807 input_dev = devm_input_allocate_device(&client->dev);
808 if (!input_dev) {
809 dev_err(&client->dev, "could not allocate input device\n");
810 return -ENOMEM;
813 mutex_init(&ts->access_mutex);
814 mutex_init(&ts->command_mutex);
816 ts->pdata = pdata;
817 ts->client = client;
818 ts->input = input_dev;
820 input_dev->name = "Neonode zForce touchscreen";
821 input_dev->phys = ts->phys;
822 input_dev->id.bustype = BUS_I2C;
824 input_dev->open = zforce_input_open;
825 input_dev->close = zforce_input_close;
827 __set_bit(EV_KEY, input_dev->evbit);
828 __set_bit(EV_SYN, input_dev->evbit);
829 __set_bit(EV_ABS, input_dev->evbit);
831 /* For multi touch */
832 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
833 pdata->x_max, 0, 0);
834 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
835 pdata->y_max, 0, 0);
837 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
838 ZFORCE_MAX_AREA, 0, 0);
839 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
840 ZFORCE_MAX_AREA, 0, 0);
841 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
842 input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
844 input_set_drvdata(ts->input, ts);
846 init_completion(&ts->command_done);
849 * The zforce pulls the interrupt low when it has data ready.
850 * After it is triggered the isr thread runs until all the available
851 * packets have been read and the interrupt is high again.
852 * Therefore we can trigger the interrupt anytime it is low and do
853 * not need to limit it to the interrupt edge.
855 ret = devm_request_threaded_irq(&client->dev, client->irq,
856 zforce_irq, zforce_irq_thread,
857 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
858 input_dev->name, ts);
859 if (ret) {
860 dev_err(&client->dev, "irq %d request failed\n", client->irq);
861 return ret;
864 i2c_set_clientdata(client, ts);
866 /* let the controller boot */
867 gpio_set_value(pdata->gpio_rst, 1);
869 ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
870 if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
871 dev_warn(&client->dev, "bootcomplete timed out\n");
873 /* need to start device to get version information */
874 ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
875 if (ret) {
876 dev_err(&client->dev, "unable to initialize, %d\n", ret);
877 return ret;
880 /* this gets the firmware version among other information */
881 ret = zforce_command_wait(ts, COMMAND_STATUS);
882 if (ret < 0) {
883 dev_err(&client->dev, "couldn't get status, %d\n", ret);
884 zforce_stop(ts);
885 return ret;
888 /* stop device and put it into sleep until it is opened */
889 ret = zforce_stop(ts);
890 if (ret < 0)
891 return ret;
893 device_set_wakeup_capable(&client->dev, true);
895 ret = input_register_device(input_dev);
896 if (ret) {
897 dev_err(&client->dev, "could not register input device, %d\n",
898 ret);
899 return ret;
902 return 0;
905 static struct i2c_device_id zforce_idtable[] = {
906 { "zforce-ts", 0 },
909 MODULE_DEVICE_TABLE(i2c, zforce_idtable);
911 #ifdef CONFIG_OF
912 static const struct of_device_id zforce_dt_idtable[] = {
913 { .compatible = "neonode,zforce" },
916 MODULE_DEVICE_TABLE(of, zforce_dt_idtable);
917 #endif
919 static struct i2c_driver zforce_driver = {
920 .driver = {
921 .owner = THIS_MODULE,
922 .name = "zforce-ts",
923 .pm = &zforce_pm_ops,
924 .of_match_table = of_match_ptr(zforce_dt_idtable),
926 .probe = zforce_probe,
927 .id_table = zforce_idtable,
930 module_i2c_driver(zforce_driver);
932 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
933 MODULE_DESCRIPTION("zForce TouchScreen Driver");
934 MODULE_LICENSE("GPL");