1 /*
2  * Copyright (C) 2012-2013 MundoReader S.L.
3  * Author: Heiko Stuebner <heiko@sntech.de>
4  *
5  * based in parts on Nook zforce driver
6  *
7  * Copyright (C) 2010 Barnes & Noble, Inc.
8  * Author: Pieter Truter<ptruter@intrinsyc.com>
9  *
10  * This software is licensed under the terms of the GNU General Public
11  * License version 2, as published by the Free Software Foundation, and
12  * may be copied, distributed, and modified under those terms.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #include <linux/module.h>
21 #include <linux/hrtimer.h>
22 #include <linux/slab.h>
23 #include <linux/input.h>
24 #include <linux/interrupt.h>
25 #include <linux/i2c.h>
26 #include <linux/delay.h>
27 #include <linux/gpio.h>
28 #include <linux/device.h>
29 #include <linux/sysfs.h>
30 #include <linux/input/mt.h>
31 #include <linux/platform_data/zforce_ts.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/delay.h>
34 #include <linux/of.h>
35 #include <linux/of_gpio.h>
36 
37 #define WAIT_TIMEOUT		msecs_to_jiffies(1000)
38 
39 #define FRAME_START		0xee
40 #define FRAME_MAXSIZE		257
41 
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
46 
47 /* Response offsets */
48 #define RESPONSE_ID		0
49 #define RESPONSE_DATA		1
50 
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
59 
60 /*
61  * Responses the controller sends as a result of
62  * command requests
63  */
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
70 
71 /*
72  * Notifications are sent by the touch controller without
73  * being requested by the driver and include for example
74  * touch indications
75  */
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
81 
82 #define ZFORCE_REPORT_POINTS		2
83 #define ZFORCE_MAX_AREA			0xff
84 
85 #define STATE_DOWN			0
86 #define STATE_MOVE			1
87 #define STATE_UP			2
88 
89 #define SETCONFIG_DUALTOUCH		(1 << 0)
90 
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;
101 };
102 
103 /*
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
115  */
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];
121 
122 	struct regulator	*reg_vdd;
123 
124 	bool			suspending;
125 	bool			suspended;
126 	bool			boot_complete;
127 
128 	/* Firmware version information */
129 	u16			version_major;
130 	u16			version_minor;
131 	u16			version_build;
132 	u16			version_rev;
133 
134 	struct mutex		access_mutex;
135 
136 	struct completion	command_done;
137 	struct mutex		command_mutex;
138 	int			command_waiting;
139 	int			command_result;
140 };
141 
142 static int zforce_command(struct zforce_ts *ts, u8 cmd)
143 {
144 	struct i2c_client *client = ts->client;
145 	char buf[3];
146 	int ret;
147 
148 	dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
149 
150 	buf[0] = FRAME_START;
151 	buf[1] = 1; /* data size, command only */
152 	buf[2] = cmd;
153 
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;
160 	}
161 
162 	return 0;
163 }
164 
165 static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
166 {
167 	struct i2c_client *client = ts->client;
168 	int ret;
169 
170 	ret = mutex_trylock(&ts->command_mutex);
171 	if (!ret) {
172 		dev_err(&client->dev, "already waiting for a command\n");
173 		return -EBUSY;
174 	}
175 
176 	dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
177 		buf[1], buf[2]);
178 
179 	ts->command_waiting = buf[2];
180 
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;
187 	}
188 
189 	dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
190 
191 	if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) {
192 		ret = -ETIME;
193 		goto unlock;
194 	}
195 
196 	ret = ts->command_result;
197 
198 unlock:
199 	mutex_unlock(&ts->command_mutex);
200 	return ret;
201 }
202 
203 static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
204 {
205 	struct i2c_client *client = ts->client;
206 	char buf[3];
207 	int ret;
208 
209 	dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
210 
211 	buf[0] = FRAME_START;
212 	buf[1] = 1; /* data size, command only */
213 	buf[2] = cmd;
214 
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;
219 	}
220 
221 	return 0;
222 }
223 
224 static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
225 {
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) };
230 
231 	dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
232 
233 	return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
234 }
235 
236 static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
237 				 u16 stylus)
238 {
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) };
244 
245 	dev_dbg(&client->dev,
246 		"set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
247 		idle, finger, stylus);
248 
249 	return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
250 }
251 
252 static int zforce_setconfig(struct zforce_ts *ts, char b1)
253 {
254 	struct i2c_client *client = ts->client;
255 	char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
256 			b1, 0, 0, 0 };
257 
258 	dev_dbg(&client->dev, "set config to (%d)\n", b1);
259 
260 	return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
261 }
262 
263 static int zforce_start(struct zforce_ts *ts)
264 {
265 	struct i2c_client *client = ts->client;
266 	const struct zforce_ts_platdata *pdata = ts->pdata;
267 	int ret;
268 
269 	dev_dbg(&client->dev, "starting device\n");
270 
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;
275 	}
276 
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;
281 	}
282 
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;
288 	}
289 
290 	ret = zforce_setconfig(ts, SETCONFIG_DUALTOUCH);
291 	if (ret) {
292 		dev_err(&client->dev, "Unable to set config\n");
293 		goto error;
294 	}
295 
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;
301 	}
302 
303 	/*
304 	 * Per NN, initial cal. take max. of 200msec.
305 	 * Allow time to complete this calibration
306 	 */
307 	msleep(200);
308 
309 	return 0;
310 
311 error:
312 	zforce_command_wait(ts, COMMAND_DEACTIVATE);
313 	return ret;
314 }
315 
316 static int zforce_stop(struct zforce_ts *ts)
317 {
318 	struct i2c_client *client = ts->client;
319 	int ret;
320 
321 	dev_dbg(&client->dev, "stopping device\n");
322 
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;
329 	}
330 
331 	return 0;
332 }
333 
334 static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
335 {
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;
340 
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;
347 	}
348 
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];
354 
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;
360 		}
361 
362 		point.state = payload[9 * i + 5] & 0x03;
363 		point.id = (payload[9 * i + 5] & 0xfc) >> 2;
364 
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];
371 
372 		point.pressure = payload[9 * i + 8];
373 		point.prblty = payload[9 * i + 9];
374 
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);
382 
383 		/* the zforce id starts with "1", so needs to be decreased */
384 		input_mt_slot(ts->input, point.id - 1);
385 
386 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
387 						point.state != STATE_UP);
388 
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++;
401 		}
402 	}
403 
404 	input_mt_sync_frame(ts->input);
405 
406 	input_mt_report_finger_count(ts->input, num);
407 
408 	input_sync(ts->input);
409 
410 	return 0;
411 }
412 
413 static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
414 {
415 	struct i2c_client *client = ts->client;
416 	int ret;
417 
418 	mutex_lock(&ts->access_mutex);
419 
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;
425 	}
426 
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;
431 	}
432 
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;
438 	}
439 
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;
445 	}
446 
447 	dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
448 		buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
449 
450 unlock:
451 	mutex_unlock(&ts->access_mutex);
452 	return ret;
453 }
454 
455 static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
456 {
457 	struct i2c_client *client = ts->client;
458 
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);
465 	}
466 }
467 
468 static irqreturn_t zforce_irq(int irq, void *dev_id)
469 {
470 	struct zforce_ts *ts = dev_id;
471 	struct i2c_client *client = ts->client;
472 
473 	if (ts->suspended && device_may_wakeup(&client->dev))
474 		pm_wakeup_event(&client->dev, 500);
475 
476 	return IRQ_WAKE_THREAD;
477 }
478 
479 static irqreturn_t zforce_irq_thread(int irq, void *dev_id)
480 {
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;
487 
488 	/*
489 	 * When still suspended, return.
490 	 * Due to the level-interrupt we will get re-triggered later.
491 	 */
492 	if (ts->suspended) {
493 		msleep(20);
494 		return IRQ_HANDLED;
495 	}
496 
497 	dev_dbg(&client->dev, "handling interrupt\n");
498 
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);
502 
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;
509 		}
510 
511 		payload =  &payload_buffer[PAYLOAD_BODY];
512 
513 		switch (payload[RESPONSE_ID]) {
514 		case NOTIFICATION_TOUCH:
515 			/*
516 			 * Always report touch-events received while
517 			 * suspending, when being a wakeup source
518 			 */
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;
523 
524 		case NOTIFICATION_BOOTCOMPLETE:
525 			ts->boot_complete = payload[RESPONSE_DATA];
526 			zforce_complete(ts, payload[RESPONSE_ID], 0);
527 			break;
528 
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;
537 
538 		case RESPONSE_STATUS:
539 			/*
540 			 * Version Payload Results
541 			 * [2:major] [2:minor] [2:build] [2:rev]
542 			 */
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);
555 
556 			zforce_complete(ts, payload[RESPONSE_ID], 0);
557 			break;
558 
559 		case NOTIFICATION_INVALID_COMMAND:
560 			dev_err(&ts->client->dev, "invalid command: 0x%x\n",
561 				payload[RESPONSE_DATA]);
562 			break;
563 
564 		default:
565 			dev_err(&ts->client->dev,
566 				"unrecognized response id: 0x%x\n",
567 				payload[RESPONSE_ID]);
568 			break;
569 		}
570 	}
571 
572 	if (!ts->suspending && device_may_wakeup(&client->dev))
573 		pm_relax(&client->dev);
574 
575 	dev_dbg(&client->dev, "finished interrupt\n");
576 
577 	return IRQ_HANDLED;
578 }
579 
580 static int zforce_input_open(struct input_dev *dev)
581 {
582 	struct zforce_ts *ts = input_get_drvdata(dev);
583 	int ret;
584 
585 	ret = zforce_start(ts);
586 	if (ret)
587 		return ret;
588 
589 	return 0;
590 }
591 
592 static void zforce_input_close(struct input_dev *dev)
593 {
594 	struct zforce_ts *ts = input_get_drvdata(dev);
595 	struct i2c_client *client = ts->client;
596 	int ret;
597 
598 	ret = zforce_stop(ts);
599 	if (ret)
600 		dev_warn(&client->dev, "stopping zforce failed\n");
601 
602 	return;
603 }
604 
605 #ifdef CONFIG_PM_SLEEP
606 static int zforce_suspend(struct device *dev)
607 {
608 	struct i2c_client *client = to_i2c_client(dev);
609 	struct zforce_ts *ts = i2c_get_clientdata(client);
610 	struct input_dev *input = ts->input;
611 	int ret = 0;
612 
613 	mutex_lock(&input->mutex);
614 	ts->suspending = true;
615 
616 	/*
617 	 * When configured as a wakeup source device should always wake
618 	 * the system, therefore start device if necessary.
619 	 */
620 	if (device_may_wakeup(&client->dev)) {
621 		dev_dbg(&client->dev, "suspend while being a wakeup source\n");
622 
623 		/* Need to start device, if not open, to be a wakeup source. */
624 		if (!input->users) {
625 			ret = zforce_start(ts);
626 			if (ret)
627 				goto unlock;
628 		}
629 
630 		enable_irq_wake(client->irq);
631 	} else if (input->users) {
632 		dev_dbg(&client->dev,
633 			"suspend without being a wakeup source\n");
634 
635 		ret = zforce_stop(ts);
636 		if (ret)
637 			goto unlock;
638 
639 		disable_irq(client->irq);
640 	}
641 
642 	ts->suspended = true;
643 
644 unlock:
645 	ts->suspending = false;
646 	mutex_unlock(&input->mutex);
647 
648 	return ret;
649 }
650 
651 static int zforce_resume(struct device *dev)
652 {
653 	struct i2c_client *client = to_i2c_client(dev);
654 	struct zforce_ts *ts = i2c_get_clientdata(client);
655 	struct input_dev *input = ts->input;
656 	int ret = 0;
657 
658 	mutex_lock(&input->mutex);
659 
660 	ts->suspended = false;
661 
662 	if (device_may_wakeup(&client->dev)) {
663 		dev_dbg(&client->dev, "resume from being a wakeup source\n");
664 
665 		disable_irq_wake(client->irq);
666 
667 		/* need to stop device if it was not open on suspend */
668 		if (!input->users) {
669 			ret = zforce_stop(ts);
670 			if (ret)
671 				goto unlock;
672 		}
673 	} else if (input->users) {
674 		dev_dbg(&client->dev, "resume without being a wakeup source\n");
675 
676 		enable_irq(client->irq);
677 
678 		ret = zforce_start(ts);
679 		if (ret < 0)
680 			goto unlock;
681 	}
682 
683 unlock:
684 	mutex_unlock(&input->mutex);
685 
686 	return ret;
687 }
688 #endif
689 
690 static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
691 
692 static void zforce_reset(void *data)
693 {
694 	struct zforce_ts *ts = data;
695 
696 	gpio_set_value(ts->pdata->gpio_rst, 0);
697 
698 	udelay(10);
699 
700 	if (!IS_ERR(ts->reg_vdd))
701 		regulator_disable(ts->reg_vdd);
702 }
703 
704 static struct zforce_ts_platdata *zforce_parse_dt(struct device *dev)
705 {
706 	struct zforce_ts_platdata *pdata;
707 	struct device_node *np = dev->of_node;
708 
709 	if (!np)
710 		return ERR_PTR(-ENOENT);
711 
712 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
713 	if (!pdata) {
714 		dev_err(dev, "failed to allocate platform data\n");
715 		return ERR_PTR(-ENOMEM);
716 	}
717 
718 	pdata->gpio_int = of_get_gpio(np, 0);
719 	if (!gpio_is_valid(pdata->gpio_int)) {
720 		dev_err(dev, "failed to get interrupt gpio\n");
721 		return ERR_PTR(-EINVAL);
722 	}
723 
724 	pdata->gpio_rst = of_get_gpio(np, 1);
725 	if (!gpio_is_valid(pdata->gpio_rst)) {
726 		dev_err(dev, "failed to get reset gpio\n");
727 		return ERR_PTR(-EINVAL);
728 	}
729 
730 	if (of_property_read_u32(np, "x-size", &pdata->x_max)) {
731 		dev_err(dev, "failed to get x-size property\n");
732 		return ERR_PTR(-EINVAL);
733 	}
734 
735 	if (of_property_read_u32(np, "y-size", &pdata->y_max)) {
736 		dev_err(dev, "failed to get y-size property\n");
737 		return ERR_PTR(-EINVAL);
738 	}
739 
740 	return pdata;
741 }
742 
743 static int zforce_probe(struct i2c_client *client,
744 			const struct i2c_device_id *id)
745 {
746 	const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
747 	struct zforce_ts *ts;
748 	struct input_dev *input_dev;
749 	int ret;
750 
751 	if (!pdata) {
752 		pdata = zforce_parse_dt(&client->dev);
753 		if (IS_ERR(pdata))
754 			return PTR_ERR(pdata);
755 	}
756 
757 	ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
758 	if (!ts)
759 		return -ENOMEM;
760 
761 	ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN,
762 				    "zforce_ts_int");
763 	if (ret) {
764 		dev_err(&client->dev, "request of gpio %d failed, %d\n",
765 			pdata->gpio_int, ret);
766 		return ret;
767 	}
768 
769 	ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
770 				    GPIOF_OUT_INIT_LOW, "zforce_ts_rst");
771 	if (ret) {
772 		dev_err(&client->dev, "request of gpio %d failed, %d\n",
773 			pdata->gpio_rst, ret);
774 		return ret;
775 	}
776 
777 	ts->reg_vdd = devm_regulator_get_optional(&client->dev, "vdd");
778 	if (IS_ERR(ts->reg_vdd)) {
779 		ret = PTR_ERR(ts->reg_vdd);
780 		if (ret == -EPROBE_DEFER)
781 			return ret;
782 	} else {
783 		ret = regulator_enable(ts->reg_vdd);
784 		if (ret)
785 			return ret;
786 
787 		/*
788 		 * according to datasheet add 100us grace time after regular
789 		 * regulator enable delay.
790 		 */
791 		udelay(100);
792 	}
793 
794 	ret = devm_add_action(&client->dev, zforce_reset, ts);
795 	if (ret) {
796 		dev_err(&client->dev, "failed to register reset action, %d\n",
797 			ret);
798 
799 		/* hereafter the regulator will be disabled by the action */
800 		if (!IS_ERR(ts->reg_vdd))
801 			regulator_disable(ts->reg_vdd);
802 
803 		return ret;
804 	}
805 
806 	snprintf(ts->phys, sizeof(ts->phys),
807 		 "%s/input0", dev_name(&client->dev));
808 
809 	input_dev = devm_input_allocate_device(&client->dev);
810 	if (!input_dev) {
811 		dev_err(&client->dev, "could not allocate input device\n");
812 		return -ENOMEM;
813 	}
814 
815 	mutex_init(&ts->access_mutex);
816 	mutex_init(&ts->command_mutex);
817 
818 	ts->pdata = pdata;
819 	ts->client = client;
820 	ts->input = input_dev;
821 
822 	input_dev->name = "Neonode zForce touchscreen";
823 	input_dev->phys = ts->phys;
824 	input_dev->id.bustype = BUS_I2C;
825 
826 	input_dev->open = zforce_input_open;
827 	input_dev->close = zforce_input_close;
828 
829 	__set_bit(EV_KEY, input_dev->evbit);
830 	__set_bit(EV_SYN, input_dev->evbit);
831 	__set_bit(EV_ABS, input_dev->evbit);
832 
833 	/* For multi touch */
834 	input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
835 			     pdata->x_max, 0, 0);
836 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
837 			     pdata->y_max, 0, 0);
838 
839 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
840 			     ZFORCE_MAX_AREA, 0, 0);
841 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
842 			     ZFORCE_MAX_AREA, 0, 0);
843 	input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
844 	input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
845 
846 	input_set_drvdata(ts->input, ts);
847 
848 	init_completion(&ts->command_done);
849 
850 	/*
851 	 * The zforce pulls the interrupt low when it has data ready.
852 	 * After it is triggered the isr thread runs until all the available
853 	 * packets have been read and the interrupt is high again.
854 	 * Therefore we can trigger the interrupt anytime it is low and do
855 	 * not need to limit it to the interrupt edge.
856 	 */
857 	ret = devm_request_threaded_irq(&client->dev, client->irq,
858 					zforce_irq, zforce_irq_thread,
859 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
860 					input_dev->name, ts);
861 	if (ret) {
862 		dev_err(&client->dev, "irq %d request failed\n", client->irq);
863 		return ret;
864 	}
865 
866 	i2c_set_clientdata(client, ts);
867 
868 	/* let the controller boot */
869 	gpio_set_value(pdata->gpio_rst, 1);
870 
871 	ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
872 	if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
873 		dev_warn(&client->dev, "bootcomplete timed out\n");
874 
875 	/* need to start device to get version information */
876 	ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
877 	if (ret) {
878 		dev_err(&client->dev, "unable to initialize, %d\n", ret);
879 		return ret;
880 	}
881 
882 	/* this gets the firmware version among other information */
883 	ret = zforce_command_wait(ts, COMMAND_STATUS);
884 	if (ret < 0) {
885 		dev_err(&client->dev, "couldn't get status, %d\n", ret);
886 		zforce_stop(ts);
887 		return ret;
888 	}
889 
890 	/* stop device and put it into sleep until it is opened */
891 	ret = zforce_stop(ts);
892 	if (ret < 0)
893 		return ret;
894 
895 	device_set_wakeup_capable(&client->dev, true);
896 
897 	ret = input_register_device(input_dev);
898 	if (ret) {
899 		dev_err(&client->dev, "could not register input device, %d\n",
900 			ret);
901 		return ret;
902 	}
903 
904 	return 0;
905 }
906 
907 static struct i2c_device_id zforce_idtable[] = {
908 	{ "zforce-ts", 0 },
909 	{ }
910 };
911 MODULE_DEVICE_TABLE(i2c, zforce_idtable);
912 
913 #ifdef CONFIG_OF
914 static const struct of_device_id zforce_dt_idtable[] = {
915 	{ .compatible = "neonode,zforce" },
916 	{},
917 };
918 MODULE_DEVICE_TABLE(of, zforce_dt_idtable);
919 #endif
920 
921 static struct i2c_driver zforce_driver = {
922 	.driver = {
923 		.owner	= THIS_MODULE,
924 		.name	= "zforce-ts",
925 		.pm	= &zforce_pm_ops,
926 		.of_match_table	= of_match_ptr(zforce_dt_idtable),
927 	},
928 	.probe		= zforce_probe,
929 	.id_table	= zforce_idtable,
930 };
931 
932 module_i2c_driver(zforce_driver);
933 
934 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
935 MODULE_DESCRIPTION("zForce TouchScreen Driver");
936 MODULE_LICENSE("GPL");
937