summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/elan_ts.c
blob: fd85c1b33b7c33cb8fc65565708db3c882169327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * Copyright (C) 2007-2008 HTC Corporation.
 * Copyright (C) 2012 Freescale Semiconductor, Inc.
 *
 * This driver is adapted from elan8232_i2c.c written by Shan-Fu Chiou
 * <sfchiou@gmail.com> and Jay Tu <jay_tu@htc.com>.
 * This driver is also adapted from the ELAN Touch Screen driver
 * written by Stanley Zeng <stanley.zeng@emc.com.tw>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/input.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/hrtimer.h>
#include <linux/gpio.h>

#include <linux/earlysuspend.h>

static const char ELAN_TS_NAME[] = "elan-touch";

#define ELAN_TS_X_MAX 		1088
#define ELAN_TS_Y_MAX 		768
#define ELAN_USER_X_MAX		800
#define ELAN_USER_Y_MAX		600
#define IDX_PACKET_SIZE		8

enum {
	hello_packet = 0x55,
	idx_coordinate_packet = 0x5a,
};

enum {
	idx_finger_state = 7,
};

static struct workqueue_struct *elan_wq;

static struct elan_data {
	int intr_gpio;
	int use_irq;
	struct hrtimer timer;
	struct work_struct work;
	struct i2c_client *client;
	struct input_dev *input;
	wait_queue_head_t wait;
#ifdef CONFIG_EARLYSUSPEND
	struct early_suspend es_handler;
#endif
} elan_touch_data;

/*--------------------------------------------------------------*/
static int elan_touch_detect_int_level(void)
{
	unsigned v;
	v = gpio_get_value(elan_touch_data.intr_gpio);

	return v;
}

static int __elan_touch_poll(struct i2c_client *client)
{
	int status = 0, retry = 20;

	do {
		status = elan_touch_detect_int_level();
		retry--;
		mdelay(20);
	} while (status == 1 && retry > 0);

	return (status == 0 ? 0 : -ETIMEDOUT);
}

static int elan_touch_poll(struct i2c_client *client)
{
	return __elan_touch_poll(client);
}

static int __hello_packet_handler(struct i2c_client *client)
{
	int rc;
	uint8_t buf_recv[4] = { 0 };

	rc = elan_touch_poll(client);

	if (rc < 0) {
		return -EINVAL;
	}

	rc = i2c_master_recv(client, buf_recv, 4);

	if (rc != 4) {
		return rc;
	} else {
		int i;
		printk("hello packet: [0x%02x 0x%02x 0x%02x 0x%02x]\n",
		       buf_recv[0], buf_recv[1], buf_recv[2], buf_recv[3]);

		for (i = 0; i < 4; i++)
			if (buf_recv[i] != hello_packet)
				return -EINVAL;
	}

	return 0;
}

static inline int elan_touch_parse_xy(uint8_t *data, uint16_t *x,
				      uint16_t *y)
{
	*x = (data[0] & 0xf0);
	*x <<= 4;
	*x |= data[1];
	if (*x >= ELAN_TS_X_MAX)
		*x = ELAN_TS_X_MAX;
	*x = ((((ELAN_TS_X_MAX -
		 *x) * 1000) / ELAN_TS_X_MAX) * ELAN_USER_X_MAX) / 1000;

	*y = (data[0] & 0x0f);
	*y <<= 8;
	*y |= data[2];
	if (*y >= ELAN_TS_Y_MAX)
		*y = ELAN_TS_Y_MAX;
	*y = ((((ELAN_TS_Y_MAX -
		 *y) * 1000) / ELAN_TS_Y_MAX) * ELAN_USER_Y_MAX) / 1000;

	return 0;
}

/*	__elan_touch_init -- hand shaking with touch panel
 *
 *	1.recv hello packet
 */
static int __elan_touch_init(struct i2c_client *client)
{
	int rc;
	rc = __hello_packet_handler(client);
	if (rc < 0)
		goto hand_shake_failed;

hand_shake_failed:
	return rc;
}

static int elan_touch_recv_data(struct i2c_client *client, uint8_t * buf)
{
	int rc, bytes_to_recv = IDX_PACKET_SIZE;

	if (buf == NULL)
		return -EINVAL;

	memset(buf, 0, bytes_to_recv);
	rc = i2c_master_recv(client, buf, bytes_to_recv);
	if (rc != bytes_to_recv) {
		return -EINVAL;
	}

	return rc;
}

static void elan_touch_report_data(struct i2c_client *client, uint8_t * buf)
{
	switch (buf[0]) {
	case idx_coordinate_packet:
	{
		uint16_t x1, x2, y1, y2;
		uint8_t finger_stat;

		finger_stat = (buf[idx_finger_state] & 0x06) >> 1;

		if (finger_stat == 0) {
			input_report_key(elan_touch_data.input, BTN_TOUCH, 0);
			input_report_key(elan_touch_data.input, BTN_2, 0);
		} else if (finger_stat == 1) {
			elan_touch_parse_xy(&buf[1], &x1, &y1);
			input_report_abs(elan_touch_data.input, ABS_X, x1);
			input_report_abs(elan_touch_data.input, ABS_Y, y1);
			input_report_key(elan_touch_data.input, BTN_TOUCH, 1);
			input_report_key(elan_touch_data.input, BTN_2, 0);
		} else if (finger_stat == 2) {
			elan_touch_parse_xy(&buf[1], &x1, &y1);
			input_report_abs(elan_touch_data.input, ABS_X, x1);
			input_report_abs(elan_touch_data.input, ABS_Y, y1);
			input_report_key(elan_touch_data.input, BTN_TOUCH, 1);
			elan_touch_parse_xy(&buf[4], &x2, &y2);
			input_report_abs(elan_touch_data.input, ABS_HAT0X, x2);
			input_report_abs(elan_touch_data.input, ABS_HAT0Y, y2);
			input_report_key(elan_touch_data.input, BTN_2, 1);
		}
		input_sync(elan_touch_data.input);
		break;
	}

	default:
		break;
	}
}

static void elan_touch_work_func(struct work_struct *work)
{
	int rc;
	uint8_t buf[IDX_PACKET_SIZE] = { 0 };
	struct i2c_client *client = elan_touch_data.client;

	if (elan_touch_detect_int_level())
		return;

	rc = elan_touch_recv_data(client, buf);
	if (rc < 0)
		return;

	elan_touch_report_data(client, buf);
}

static irqreturn_t elan_touch_ts_interrupt(int irq, void *dev_id)
{
	queue_work(elan_wq, &elan_touch_data.work);

	return IRQ_HANDLED;
}

static enum hrtimer_restart elan_touch_timer_func(struct hrtimer *timer)
{
	queue_work(elan_wq, &elan_touch_data.work);
	hrtimer_start(&elan_touch_data.timer, ktime_set(0, 12500000),
		      HRTIMER_MODE_REL);

	return HRTIMER_NORESTART;
}

static int elan_touch_register_interrupt(struct i2c_client *client)
{
	int err = 0;

	if (client->irq) {
		elan_touch_data.use_irq = 1;
		err =
		    request_irq(client->irq, elan_touch_ts_interrupt,
				IRQF_TRIGGER_FALLING, ELAN_TS_NAME,
				&elan_touch_data);

		if (err < 0) {
			printk("%s(%s): Can't allocate irq %d\n", __FILE__,
			       __func__, client->irq);
			elan_touch_data.use_irq = 0;
		}
	}

	if (!elan_touch_data.use_irq) {
		hrtimer_init(&elan_touch_data.timer, CLOCK_MONOTONIC,
			     HRTIMER_MODE_REL);
		elan_touch_data.timer.function = elan_touch_timer_func;
		hrtimer_start(&elan_touch_data.timer, ktime_set(1, 0),
			      HRTIMER_MODE_REL);
	}

	printk("elan ts starts in %s mode.\n",
	       elan_touch_data.use_irq == 1 ? "interrupt" : "polling");

	return 0;
}

#ifdef CONFIG_EARLYSUSPEND
static void elan_early_suspend(struct early_suspend *h);
static void elan_later_resume(struct early_suspend *h);
#endif


static int elan_touch_probe(struct i2c_client *client,
			    const struct i2c_device_id *id)
{
	int err = 0;

	elan_wq = create_singlethread_workqueue("elan_wq");
	if (!elan_wq) {
		err = -ENOMEM;
		goto fail;
	}

	elan_touch_data.client = client;
	strlcpy(client->name, ELAN_TS_NAME, I2C_NAME_SIZE);

	INIT_WORK(&elan_touch_data.work, elan_touch_work_func);

	elan_touch_data.intr_gpio = irq_to_gpio(client->irq);
	pr_debug("irq_to_gpio irq %d, gpio %d\n", client->irq,
		 elan_touch_data.intr_gpio);

	elan_touch_data.input = input_allocate_device();
	if (elan_touch_data.input == NULL) {
		err = -ENOMEM;
		goto fail;
	}

	err = __elan_touch_init(client);
	if (err < 0) {
		printk("elan - Read Hello Packet Failed\n");
		goto fail;
	}

	elan_touch_data.input->name = ELAN_TS_NAME;
	elan_touch_data.input->id.bustype = BUS_I2C;

	set_bit(EV_SYN, elan_touch_data.input->evbit);

	set_bit(EV_KEY, elan_touch_data.input->evbit);
	set_bit(BTN_TOUCH, elan_touch_data.input->keybit);
	set_bit(BTN_2, elan_touch_data.input->keybit);

	set_bit(EV_ABS, elan_touch_data.input->evbit);
	set_bit(ABS_X, elan_touch_data.input->absbit);
	set_bit(ABS_Y, elan_touch_data.input->absbit);
	set_bit(ABS_HAT0X, elan_touch_data.input->absbit);
	set_bit(ABS_HAT0Y, elan_touch_data.input->absbit);

	input_set_abs_params(elan_touch_data.input, ABS_X, 0, ELAN_USER_X_MAX,
			     0, 0);
	input_set_abs_params(elan_touch_data.input, ABS_Y, 0, ELAN_USER_Y_MAX,
			     0, 0);
	input_set_abs_params(elan_touch_data.input, ABS_HAT0X, 0,
			     ELAN_USER_X_MAX, 0, 0);
	input_set_abs_params(elan_touch_data.input, ABS_HAT0Y, 0,
			     ELAN_USER_Y_MAX, 0, 0);

	err = input_register_device(elan_touch_data.input);
	if (err < 0) {
		goto fail;
	}

#ifdef CONFIG_EARLYSUSPEND
	/* register this client's earlysuspend */
	elan_touch_data.es_handler.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
	elan_touch_data.es_handler.suspend = elan_early_suspend;
	elan_touch_data.es_handler.resume = elan_later_resume;
	elan_touch_data.es_handler.data = (void *)client;
	register_early_suspend(&elan_touch_data.es_handler);
#endif

	elan_touch_register_interrupt(elan_touch_data.client);

	return 0;

fail:
	input_free_device(elan_touch_data.input);
	if (elan_wq)
		destroy_workqueue(elan_wq);
	return err;
}

static int elan_touch_remove(struct i2c_client *client)
{
#ifdef CONFIG_EARLYSUSPEND
	unregister_early_suspend(&elan_touch_data.es_handler);
#endif

	if (elan_wq)
		destroy_workqueue(elan_wq);

	input_unregister_device(elan_touch_data.input);

	if (elan_touch_data.use_irq)
		free_irq(client->irq, client);
	else
		hrtimer_cancel(&elan_touch_data.timer);
	return 0;
}

#ifdef CONFIG_EARLYSUSPEND
static void elan_early_suspend(struct early_suspend *h)
{
}

static void elan_later_resume(struct early_suspend *h)
{
	if (0 == elan_touch_detect_int_level()) {
		pr_debug("elan_ts: got touch in suspend period\n");
		if (h->data) {
			/*if touch screen during suspend, recv and drop the
			  data, then touch interrupt pin will return high after
			  receving data.
			*/
			uint8_t buf_recv[IDX_PACKET_SIZE];
			elan_touch_recv_data((struct i2c_client *)(h->data),
					       buf_recv);
		}
	}
}
#endif

/* -------------------------------------------------------------------- */
static const struct i2c_device_id elan_touch_id[] = {
	{"elan-touch", 0},
	{}
};

static struct i2c_driver elan_touch_driver = {
	.probe = elan_touch_probe,
	.remove = elan_touch_remove,
	.id_table = elan_touch_id,
	.driver = {
		   .name = "elan-touch",
		   .owner = THIS_MODULE,
		   },
};

static int __init elan_touch_init(void)
{
	return i2c_add_driver(&elan_touch_driver);
}

static void __exit elan_touch_exit(void)
{
	i2c_del_driver(&elan_touch_driver);
}

module_init(elan_touch_init);
module_exit(elan_touch_exit);

MODULE_AUTHOR("Stanley Zeng <stanley.zeng@emc.com.tw>");
MODULE_DESCRIPTION("ELAN Touch Screen driver");
MODULE_LICENSE("GPL");