Linux Audio

Check our new training course

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