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 
33 #define WAIT_TIMEOUT		msecs_to_jiffies(1000)
34 
35 #define FRAME_START		0xee
36 
37 /* Offsets of the different parts of the payload the controller sends */
38 #define PAYLOAD_HEADER		0
39 #define PAYLOAD_LENGTH		1
40 #define PAYLOAD_BODY		2
41 
42 /* Response offsets */
43 #define RESPONSE_ID		0
44 #define RESPONSE_DATA		1
45 
46 /* Commands */
47 #define COMMAND_DEACTIVATE	0x00
48 #define COMMAND_INITIALIZE	0x01
49 #define COMMAND_RESOLUTION	0x02
50 #define COMMAND_SETCONFIG	0x03
51 #define COMMAND_DATAREQUEST	0x04
52 #define COMMAND_SCANFREQ	0x08
53 #define COMMAND_STATUS		0X1e
54 
55 /*
56  * Responses the controller sends as a result of
57  * command requests
58  */
59 #define RESPONSE_DEACTIVATE	0x00
60 #define RESPONSE_INITIALIZE	0x01
61 #define RESPONSE_RESOLUTION	0x02
62 #define RESPONSE_SETCONFIG	0x03
63 #define RESPONSE_SCANFREQ	0x08
64 #define RESPONSE_STATUS		0X1e
65 
66 /*
67  * Notifications are send by the touch controller without
68  * being requested by the driver and include for example
69  * touch indications
70  */
71 #define NOTIFICATION_TOUCH		0x04
72 #define NOTIFICATION_BOOTCOMPLETE	0x07
73 #define NOTIFICATION_OVERRUN		0x25
74 #define NOTIFICATION_PROXIMITY		0x26
75 #define NOTIFICATION_INVALID_COMMAND	0xfe
76 
77 #define ZFORCE_REPORT_POINTS		2
78 #define ZFORCE_MAX_AREA			0xff
79 
80 #define STATE_DOWN			0
81 #define STATE_MOVE			1
82 #define STATE_UP			2
83 
84 #define SETCONFIG_DUALTOUCH		(1 << 0)
85 
86 struct zforce_point {
87 	int coord_x;
88 	int coord_y;
89 	int state;
90 	int id;
91 	int area_major;
92 	int area_minor;
93 	int orientation;
94 	int pressure;
95 	int prblty;
96 };
97 
98 /*
99  * @client		the i2c_client
100  * @input		the input device
101  * @suspending		in the process of going to suspend (don't emit wakeup
102  *			events for commands executed to suspend the device)
103  * @suspended		device suspended
104  * @access_mutex	serialize i2c-access, to keep multipart reads together
105  * @command_done	completion to wait for the command result
106  * @command_mutex	serialize commands send to the ic
107  * @command_waiting	the id of the command that that is currently waiting
108  *			for a result
109  * @command_result	returned result of the command
110  */
111 struct zforce_ts {
112 	struct i2c_client	*client;
113 	struct input_dev	*input;
114 	const struct zforce_ts_platdata *pdata;
115 	char			phys[32];
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 
135 static 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 
158 static int zforce_send_wait(struct zforce_ts *ts, const char *buf, int len)
159 {
160 	struct i2c_client *client = ts->client;
161 	int ret;
162 
163 	ret = mutex_trylock(&ts->command_mutex);
164 	if (!ret) {
165 		dev_err(&client->dev, "already waiting for a command\n");
166 		return -EBUSY;
167 	}
168 
169 	dev_dbg(&client->dev, "sending %d bytes for command 0x%x\n",
170 		buf[1], buf[2]);
171 
172 	ts->command_waiting = buf[2];
173 
174 	mutex_lock(&ts->access_mutex);
175 	ret = i2c_master_send(client, buf, len);
176 	mutex_unlock(&ts->access_mutex);
177 	if (ret < 0) {
178 		dev_err(&client->dev, "i2c send data request error: %d\n", ret);
179 		goto unlock;
180 	}
181 
182 	dev_dbg(&client->dev, "waiting for result for command 0x%x\n", buf[2]);
183 
184 	if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0) {
185 		ret = -ETIME;
186 		goto unlock;
187 	}
188 
189 	ret = ts->command_result;
190 
191 unlock:
192 	mutex_unlock(&ts->command_mutex);
193 	return ret;
194 }
195 
196 static int zforce_command_wait(struct zforce_ts *ts, u8 cmd)
197 {
198 	struct i2c_client *client = ts->client;
199 	char buf[3];
200 	int ret;
201 
202 	dev_dbg(&client->dev, "%s: 0x%x\n", __func__, cmd);
203 
204 	buf[0] = FRAME_START;
205 	buf[1] = 1; /* data size, command only */
206 	buf[2] = cmd;
207 
208 	ret = zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
209 	if (ret < 0) {
210 		dev_err(&client->dev, "i2c send data request error: %d\n", ret);
211 		return ret;
212 	}
213 
214 	return 0;
215 }
216 
217 static int zforce_resolution(struct zforce_ts *ts, u16 x, u16 y)
218 {
219 	struct i2c_client *client = ts->client;
220 	char buf[7] = { FRAME_START, 5, COMMAND_RESOLUTION,
221 			(x & 0xff), ((x >> 8) & 0xff),
222 			(y & 0xff), ((y >> 8) & 0xff) };
223 
224 	dev_dbg(&client->dev, "set resolution to (%d,%d)\n", x, y);
225 
226 	return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
227 }
228 
229 static int zforce_scan_frequency(struct zforce_ts *ts, u16 idle, u16 finger,
230 				 u16 stylus)
231 {
232 	struct i2c_client *client = ts->client;
233 	char buf[9] = { FRAME_START, 7, COMMAND_SCANFREQ,
234 			(idle & 0xff), ((idle >> 8) & 0xff),
235 			(finger & 0xff), ((finger >> 8) & 0xff),
236 			(stylus & 0xff), ((stylus >> 8) & 0xff) };
237 
238 	dev_dbg(&client->dev, "set scan frequency to (idle: %d, finger: %d, stylus: %d)\n",
239 		idle, finger, stylus);
240 
241 	return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
242 }
243 
244 static int zforce_setconfig(struct zforce_ts *ts, char b1)
245 {
246 	struct i2c_client *client = ts->client;
247 	char buf[7] = { FRAME_START, 5, COMMAND_SETCONFIG,
248 			b1, 0, 0, 0 };
249 
250 	dev_dbg(&client->dev, "set config to (%d)\n", b1);
251 
252 	return zforce_send_wait(ts, &buf[0], ARRAY_SIZE(buf));
253 }
254 
255 static int zforce_start(struct zforce_ts *ts)
256 {
257 	struct i2c_client *client = ts->client;
258 	const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
259 	int ret;
260 
261 	dev_dbg(&client->dev, "starting device\n");
262 
263 	ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
264 	if (ret) {
265 		dev_err(&client->dev, "Unable to initialize, %d\n", ret);
266 		return ret;
267 	}
268 
269 	ret = zforce_resolution(ts, pdata->x_max, pdata->y_max);
270 	if (ret) {
271 		dev_err(&client->dev, "Unable to set resolution, %d\n", ret);
272 		goto error;
273 	}
274 
275 	ret = zforce_scan_frequency(ts, 10, 50, 50);
276 	if (ret) {
277 		dev_err(&client->dev, "Unable to set scan frequency, %d\n",
278 			ret);
279 		goto error;
280 	}
281 
282 	if (zforce_setconfig(ts, SETCONFIG_DUALTOUCH)) {
283 		dev_err(&client->dev, "Unable to set config\n");
284 		goto error;
285 	}
286 
287 	/* start sending touch events */
288 	ret = zforce_command(ts, COMMAND_DATAREQUEST);
289 	if (ret) {
290 		dev_err(&client->dev, "Unable to request data\n");
291 		goto error;
292 	}
293 
294 	/*
295 	 * Per NN, initial cal. take max. of 200msec.
296 	 * Allow time to complete this calibration
297 	 */
298 	msleep(200);
299 
300 	return 0;
301 
302 error:
303 	zforce_command_wait(ts, COMMAND_DEACTIVATE);
304 	return ret;
305 }
306 
307 static int zforce_stop(struct zforce_ts *ts)
308 {
309 	struct i2c_client *client = ts->client;
310 	int ret;
311 
312 	dev_dbg(&client->dev, "stopping device\n");
313 
314 	/* Deactivates touch sensing and puts the device into sleep. */
315 	ret = zforce_command_wait(ts, COMMAND_DEACTIVATE);
316 	if (ret != 0) {
317 		dev_err(&client->dev, "could not deactivate device, %d\n",
318 			ret);
319 		return ret;
320 	}
321 
322 	return 0;
323 }
324 
325 static int zforce_touch_event(struct zforce_ts *ts, u8 *payload)
326 {
327 	struct i2c_client *client = ts->client;
328 	const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
329 	struct zforce_point point;
330 	int count, i, num = 0;
331 
332 	count = payload[0];
333 	if (count > ZFORCE_REPORT_POINTS) {
334 		dev_warn(&client->dev, "to many coordinates %d, expected max %d\n",
335 			 count, ZFORCE_REPORT_POINTS);
336 		count = ZFORCE_REPORT_POINTS;
337 	}
338 
339 	for (i = 0; i < count; i++) {
340 		point.coord_x =
341 			payload[9 * i + 2] << 8 | payload[9 * i + 1];
342 		point.coord_y =
343 			payload[9 * i + 4] << 8 | payload[9 * i + 3];
344 
345 		if (point.coord_x > pdata->x_max ||
346 		    point.coord_y > pdata->y_max) {
347 			dev_warn(&client->dev, "coordinates (%d,%d) invalid\n",
348 				point.coord_x, point.coord_y);
349 			point.coord_x = point.coord_y = 0;
350 		}
351 
352 		point.state = payload[9 * i + 5] & 0x03;
353 		point.id = (payload[9 * i + 5] & 0xfc) >> 2;
354 
355 		/* determine touch major, minor and orientation */
356 		point.area_major = max(payload[9 * i + 6],
357 					  payload[9 * i + 7]);
358 		point.area_minor = min(payload[9 * i + 6],
359 					  payload[9 * i + 7]);
360 		point.orientation = payload[9 * i + 6] > payload[9 * i + 7];
361 
362 		point.pressure = payload[9 * i + 8];
363 		point.prblty = payload[9 * i + 9];
364 
365 		dev_dbg(&client->dev,
366 			"point %d/%d: state %d, id %d, pressure %d, prblty %d, x %d, y %d, amajor %d, aminor %d, ori %d\n",
367 			i, count, point.state, point.id,
368 			point.pressure, point.prblty,
369 			point.coord_x, point.coord_y,
370 			point.area_major, point.area_minor,
371 			point.orientation);
372 
373 		/* the zforce id starts with "1", so needs to be decreased */
374 		input_mt_slot(ts->input, point.id - 1);
375 
376 		input_mt_report_slot_state(ts->input, MT_TOOL_FINGER,
377 						point.state != STATE_UP);
378 
379 		if (point.state != STATE_UP) {
380 			input_report_abs(ts->input, ABS_MT_POSITION_X,
381 					 point.coord_x);
382 			input_report_abs(ts->input, ABS_MT_POSITION_Y,
383 					 point.coord_y);
384 			input_report_abs(ts->input, ABS_MT_TOUCH_MAJOR,
385 					 point.area_major);
386 			input_report_abs(ts->input, ABS_MT_TOUCH_MINOR,
387 					 point.area_minor);
388 			input_report_abs(ts->input, ABS_MT_ORIENTATION,
389 					 point.orientation);
390 			num++;
391 		}
392 	}
393 
394 	input_mt_sync_frame(ts->input);
395 
396 	input_mt_report_finger_count(ts->input, num);
397 
398 	input_sync(ts->input);
399 
400 	return 0;
401 }
402 
403 static int zforce_read_packet(struct zforce_ts *ts, u8 *buf)
404 {
405 	struct i2c_client *client = ts->client;
406 	int ret;
407 
408 	mutex_lock(&ts->access_mutex);
409 
410 	/* read 2 byte message header */
411 	ret = i2c_master_recv(client, buf, 2);
412 	if (ret < 0) {
413 		dev_err(&client->dev, "error reading header: %d\n", ret);
414 		goto unlock;
415 	}
416 
417 	if (buf[PAYLOAD_HEADER] != FRAME_START) {
418 		dev_err(&client->dev, "invalid frame start: %d\n", buf[0]);
419 		ret = -EIO;
420 		goto unlock;
421 	}
422 
423 	if (buf[PAYLOAD_LENGTH] <= 0 || buf[PAYLOAD_LENGTH] > 255) {
424 		dev_err(&client->dev, "invalid payload length: %d\n",
425 			buf[PAYLOAD_LENGTH]);
426 		ret = -EIO;
427 		goto unlock;
428 	}
429 
430 	/* read the message */
431 	ret = i2c_master_recv(client, &buf[PAYLOAD_BODY], buf[PAYLOAD_LENGTH]);
432 	if (ret < 0) {
433 		dev_err(&client->dev, "error reading payload: %d\n", ret);
434 		goto unlock;
435 	}
436 
437 	dev_dbg(&client->dev, "read %d bytes for response command 0x%x\n",
438 		buf[PAYLOAD_LENGTH], buf[PAYLOAD_BODY]);
439 
440 unlock:
441 	mutex_unlock(&ts->access_mutex);
442 	return ret;
443 }
444 
445 static void zforce_complete(struct zforce_ts *ts, int cmd, int result)
446 {
447 	struct i2c_client *client = ts->client;
448 
449 	if (ts->command_waiting == cmd) {
450 		dev_dbg(&client->dev, "completing command 0x%x\n", cmd);
451 		ts->command_result = result;
452 		complete(&ts->command_done);
453 	} else {
454 		dev_dbg(&client->dev, "command %d not for us\n", cmd);
455 	}
456 }
457 
458 static irqreturn_t zforce_irq(int irq, void *dev_id)
459 {
460 	struct zforce_ts *ts = dev_id;
461 	struct i2c_client *client = ts->client;
462 
463 	if (ts->suspended && device_may_wakeup(&client->dev))
464 		pm_wakeup_event(&client->dev, 500);
465 
466 	return IRQ_WAKE_THREAD;
467 }
468 
469 static irqreturn_t zforce_irq_thread(int irq, void *dev_id)
470 {
471 	struct zforce_ts *ts = dev_id;
472 	struct i2c_client *client = ts->client;
473 	const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
474 	int ret;
475 	u8 payload_buffer[512];
476 	u8 *payload;
477 
478 	/*
479 	 * When still suspended, return.
480 	 * Due to the level-interrupt we will get re-triggered later.
481 	 */
482 	if (ts->suspended) {
483 		msleep(20);
484 		return IRQ_HANDLED;
485 	}
486 
487 	dev_dbg(&client->dev, "handling interrupt\n");
488 
489 	/* Don't emit wakeup events from commands run by zforce_suspend */
490 	if (!ts->suspending && device_may_wakeup(&client->dev))
491 		pm_stay_awake(&client->dev);
492 
493 	while (!gpio_get_value(pdata->gpio_int)) {
494 		ret = zforce_read_packet(ts, payload_buffer);
495 		if (ret < 0) {
496 			dev_err(&client->dev, "could not read packet, ret: %d\n",
497 				ret);
498 			break;
499 		}
500 
501 		payload =  &payload_buffer[PAYLOAD_BODY];
502 
503 		switch (payload[RESPONSE_ID]) {
504 		case NOTIFICATION_TOUCH:
505 			/*
506 			 * Always report touch-events received while
507 			 * suspending, when being a wakeup source
508 			 */
509 			if (ts->suspending && device_may_wakeup(&client->dev))
510 				pm_wakeup_event(&client->dev, 500);
511 			zforce_touch_event(ts, &payload[RESPONSE_DATA]);
512 			break;
513 
514 		case NOTIFICATION_BOOTCOMPLETE:
515 			ts->boot_complete = payload[RESPONSE_DATA];
516 			zforce_complete(ts, payload[RESPONSE_ID], 0);
517 			break;
518 
519 		case RESPONSE_INITIALIZE:
520 		case RESPONSE_DEACTIVATE:
521 		case RESPONSE_SETCONFIG:
522 		case RESPONSE_RESOLUTION:
523 		case RESPONSE_SCANFREQ:
524 			zforce_complete(ts, payload[RESPONSE_ID],
525 					payload[RESPONSE_DATA]);
526 			break;
527 
528 		case RESPONSE_STATUS:
529 			/*
530 			 * Version Payload Results
531 			 * [2:major] [2:minor] [2:build] [2:rev]
532 			 */
533 			ts->version_major = (payload[RESPONSE_DATA + 1] << 8) |
534 						payload[RESPONSE_DATA];
535 			ts->version_minor = (payload[RESPONSE_DATA + 3] << 8) |
536 						payload[RESPONSE_DATA + 2];
537 			ts->version_build = (payload[RESPONSE_DATA + 5] << 8) |
538 						payload[RESPONSE_DATA + 4];
539 			ts->version_rev   = (payload[RESPONSE_DATA + 7] << 8) |
540 						payload[RESPONSE_DATA + 6];
541 			dev_dbg(&ts->client->dev, "Firmware Version %04x:%04x %04x:%04x\n",
542 				ts->version_major, ts->version_minor,
543 				ts->version_build, ts->version_rev);
544 
545 			zforce_complete(ts, payload[RESPONSE_ID], 0);
546 			break;
547 
548 		case NOTIFICATION_INVALID_COMMAND:
549 			dev_err(&ts->client->dev, "invalid command: 0x%x\n",
550 				payload[RESPONSE_DATA]);
551 			break;
552 
553 		default:
554 			dev_err(&ts->client->dev, "unrecognized response id: 0x%x\n",
555 				payload[RESPONSE_ID]);
556 			break;
557 		}
558 	}
559 
560 	if (!ts->suspending && device_may_wakeup(&client->dev))
561 		pm_relax(&client->dev);
562 
563 	dev_dbg(&client->dev, "finished interrupt\n");
564 
565 	return IRQ_HANDLED;
566 }
567 
568 static int zforce_input_open(struct input_dev *dev)
569 {
570 	struct zforce_ts *ts = input_get_drvdata(dev);
571 	int ret;
572 
573 	ret = zforce_start(ts);
574 	if (ret)
575 		return ret;
576 
577 	return 0;
578 }
579 
580 static void zforce_input_close(struct input_dev *dev)
581 {
582 	struct zforce_ts *ts = input_get_drvdata(dev);
583 	struct i2c_client *client = ts->client;
584 	int ret;
585 
586 	ret = zforce_stop(ts);
587 	if (ret)
588 		dev_warn(&client->dev, "stopping zforce failed\n");
589 
590 	return;
591 }
592 
593 #ifdef CONFIG_PM_SLEEP
594 static int zforce_suspend(struct device *dev)
595 {
596 	struct i2c_client *client = to_i2c_client(dev);
597 	struct zforce_ts *ts = i2c_get_clientdata(client);
598 	struct input_dev *input = ts->input;
599 	int ret = 0;
600 
601 	mutex_lock(&input->mutex);
602 	ts->suspending = true;
603 
604 	/*
605 	 * When configured as a wakeup source device should always wake
606 	 * the system, therefore start device if necessary.
607 	 */
608 	if (device_may_wakeup(&client->dev)) {
609 		dev_dbg(&client->dev, "suspend while being a wakeup source\n");
610 
611 		/* Need to start device, if not open, to be a wakeup source. */
612 		if (!input->users) {
613 			ret = zforce_start(ts);
614 			if (ret)
615 				goto unlock;
616 		}
617 
618 		enable_irq_wake(client->irq);
619 	} else if (input->users) {
620 		dev_dbg(&client->dev, "suspend without being a wakeup source\n");
621 
622 		ret = zforce_stop(ts);
623 		if (ret)
624 			goto unlock;
625 
626 		disable_irq(client->irq);
627 	}
628 
629 	ts->suspended = true;
630 
631 unlock:
632 	ts->suspending = false;
633 	mutex_unlock(&input->mutex);
634 
635 	return ret;
636 }
637 
638 static int zforce_resume(struct device *dev)
639 {
640 	struct i2c_client *client = to_i2c_client(dev);
641 	struct zforce_ts *ts = i2c_get_clientdata(client);
642 	struct input_dev *input = ts->input;
643 	int ret = 0;
644 
645 	mutex_lock(&input->mutex);
646 
647 	ts->suspended = false;
648 
649 	if (device_may_wakeup(&client->dev)) {
650 		dev_dbg(&client->dev, "resume from being a wakeup source\n");
651 
652 		disable_irq_wake(client->irq);
653 
654 		/* need to stop device if it was not open on suspend */
655 		if (!input->users) {
656 			ret = zforce_stop(ts);
657 			if (ret)
658 				goto unlock;
659 		}
660 	} else if (input->users) {
661 		dev_dbg(&client->dev, "resume without being a wakeup source\n");
662 
663 		enable_irq(client->irq);
664 
665 		ret = zforce_start(ts);
666 		if (ret < 0)
667 			goto unlock;
668 	}
669 
670 unlock:
671 	mutex_unlock(&input->mutex);
672 
673 	return ret;
674 }
675 #endif
676 
677 static SIMPLE_DEV_PM_OPS(zforce_pm_ops, zforce_suspend, zforce_resume);
678 
679 static void zforce_reset(void *data)
680 {
681 	struct zforce_ts *ts = data;
682 
683 	gpio_set_value(ts->pdata->gpio_rst, 0);
684 }
685 
686 static int zforce_probe(struct i2c_client *client,
687 			const struct i2c_device_id *id)
688 {
689 	const struct zforce_ts_platdata *pdata = dev_get_platdata(&client->dev);
690 	struct zforce_ts *ts;
691 	struct input_dev *input_dev;
692 	int ret;
693 
694 	if (!pdata)
695 		return -EINVAL;
696 
697 	ts = devm_kzalloc(&client->dev, sizeof(struct zforce_ts), GFP_KERNEL);
698 	if (!ts)
699 		return -ENOMEM;
700 
701 	ret = devm_gpio_request_one(&client->dev, pdata->gpio_int, GPIOF_IN,
702 				    "zforce_ts_int");
703 	if (ret) {
704 		dev_err(&client->dev, "request of gpio %d failed, %d\n",
705 			pdata->gpio_int, ret);
706 		return ret;
707 	}
708 
709 	ret = devm_gpio_request_one(&client->dev, pdata->gpio_rst,
710 				    GPIOF_OUT_INIT_LOW, "zforce_ts_rst");
711 	if (ret) {
712 		dev_err(&client->dev, "request of gpio %d failed, %d\n",
713 			pdata->gpio_rst, ret);
714 		return ret;
715 	}
716 
717 	ret = devm_add_action(&client->dev, zforce_reset, ts);
718 	if (ret) {
719 		dev_err(&client->dev, "failed to register reset action, %d\n",
720 			ret);
721 		return ret;
722 	}
723 
724 	snprintf(ts->phys, sizeof(ts->phys),
725 		 "%s/input0", dev_name(&client->dev));
726 
727 	input_dev = devm_input_allocate_device(&client->dev);
728 	if (!input_dev) {
729 		dev_err(&client->dev, "could not allocate input device\n");
730 		return -ENOMEM;
731 	}
732 
733 	mutex_init(&ts->access_mutex);
734 	mutex_init(&ts->command_mutex);
735 
736 	ts->pdata = pdata;
737 	ts->client = client;
738 	ts->input = input_dev;
739 
740 	input_dev->name = "Neonode zForce touchscreen";
741 	input_dev->phys = ts->phys;
742 	input_dev->id.bustype = BUS_I2C;
743 
744 	input_dev->open = zforce_input_open;
745 	input_dev->close = zforce_input_close;
746 
747 	__set_bit(EV_KEY, input_dev->evbit);
748 	__set_bit(EV_SYN, input_dev->evbit);
749 	__set_bit(EV_ABS, input_dev->evbit);
750 
751 	/* For multi touch */
752 	input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0,
753 			     pdata->x_max, 0, 0);
754 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0,
755 			     pdata->y_max, 0, 0);
756 
757 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR, 0,
758 			     ZFORCE_MAX_AREA, 0, 0);
759 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR, 0,
760 			     ZFORCE_MAX_AREA, 0, 0);
761 	input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
762 	input_mt_init_slots(input_dev, ZFORCE_REPORT_POINTS, INPUT_MT_DIRECT);
763 
764 	input_set_drvdata(ts->input, ts);
765 
766 	init_completion(&ts->command_done);
767 
768 	/*
769 	 * The zforce pulls the interrupt low when it has data ready.
770 	 * After it is triggered the isr thread runs until all the available
771 	 * packets have been read and the interrupt is high again.
772 	 * Therefore we can trigger the interrupt anytime it is low and do
773 	 * not need to limit it to the interrupt edge.
774 	 */
775 	ret = devm_request_threaded_irq(&client->dev, client->irq,
776 					zforce_irq, zforce_irq_thread,
777 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
778 					input_dev->name, ts);
779 	if (ret) {
780 		dev_err(&client->dev, "irq %d request failed\n", client->irq);
781 		return ret;
782 	}
783 
784 	i2c_set_clientdata(client, ts);
785 
786 	/* let the controller boot */
787 	gpio_set_value(pdata->gpio_rst, 1);
788 
789 	ts->command_waiting = NOTIFICATION_BOOTCOMPLETE;
790 	if (wait_for_completion_timeout(&ts->command_done, WAIT_TIMEOUT) == 0)
791 		dev_warn(&client->dev, "bootcomplete timed out\n");
792 
793 	/* need to start device to get version information */
794 	ret = zforce_command_wait(ts, COMMAND_INITIALIZE);
795 	if (ret) {
796 		dev_err(&client->dev, "unable to initialize, %d\n", ret);
797 		return ret;
798 	}
799 
800 	/* this gets the firmware version among other informations */
801 	ret = zforce_command_wait(ts, COMMAND_STATUS);
802 	if (ret < 0) {
803 		dev_err(&client->dev, "couldn't get status, %d\n", ret);
804 		zforce_stop(ts);
805 		return ret;
806 	}
807 
808 	/* stop device and put it into sleep until it is opened */
809 	ret = zforce_stop(ts);
810 	if (ret < 0)
811 		return ret;
812 
813 	device_set_wakeup_capable(&client->dev, true);
814 
815 	ret = input_register_device(input_dev);
816 	if (ret) {
817 		dev_err(&client->dev, "could not register input device, %d\n",
818 			ret);
819 		return ret;
820 	}
821 
822 	return 0;
823 }
824 
825 static struct i2c_device_id zforce_idtable[] = {
826 	{ "zforce-ts", 0 },
827 	{ }
828 };
829 MODULE_DEVICE_TABLE(i2c, zforce_idtable);
830 
831 static struct i2c_driver zforce_driver = {
832 	.driver = {
833 		.owner	= THIS_MODULE,
834 		.name	= "zforce-ts",
835 		.pm	= &zforce_pm_ops,
836 	},
837 	.probe		= zforce_probe,
838 	.id_table	= zforce_idtable,
839 };
840 
841 module_i2c_driver(zforce_driver);
842 
843 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
844 MODULE_DESCRIPTION("zForce TouchScreen Driver");
845 MODULE_LICENSE("GPL");
846