summaryrefslogtreecommitdiff
path: root/drivers/input/touchscreen/colibri-vf50-ts.c
blob: bc42253f778b02828bec1bd47c756c8681a063a4 (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
/* Copyright 2013 Toradex AG
 *
 * Toradex Colibri VF50 Touchscreen driver
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/mvf_adc.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <mach/colibri-ts.h>

#define DRIVER_NAME "colibri-vf50-ts"
#define DRV_VERSION "1.0"

#define MVF_ADC_MAX ((1 << 12) - 1)

#define COLI_TOUCH_MIN_DELAY_US 1000
#define COLI_TOUCH_MAX_DELAY_US 2000

struct adc_touch_device {
	struct platform_device	*pdev;

	bool stop_touchscreen;

	int pen_irq;
	struct input_dev	*ts_input;
	struct workqueue_struct *ts_workqueue;
	struct work_struct	ts_work;
};

struct adc_touch_device *touch;

/*
 * Enables given plates and measures touch parameters using ADC
 */
static int adc_ts_measure(int plate_p, int plate_m, int adc, int adc_channel)
{
	int i, value = 0;
	gpio_set_value(plate_p, 0); /* Low active */
	gpio_set_value(plate_m, 1); /* High active */

	/* Use hrtimer sleep since msleep sleeps 10ms+ */
	usleep_range(COLI_TOUCH_MIN_DELAY_US, COLI_TOUCH_MAX_DELAY_US);

	for (i = 0; i < 5; i++) {
		int ret = mvf_adc_register_and_convert(adc, adc_channel);
		if (ret < 0)
			return -EINVAL;

		value += ret;
	}

	value /= 5;

	gpio_set_value(plate_p, 1);
	gpio_set_value(plate_m, 0);

	return value;
}

/*
 * Enable touch detection using falling edge detection on XM 
 */
static void adc_ts_enable_touch_detection(struct adc_touch_device *adc_ts)
{
	struct colibri_ts_platform_data *pdata = adc_ts->pdev->dev.platform_data;

	/* Enable plate YM (needs to be strong GND, high active) */
	gpio_set_value(pdata->gpio_ym, 1); 

	/* Let the platform mux to GPIO in order to enable Pull-Up on GPIO */
	if (pdata->mux_pen_interrupt)
		pdata->mux_pen_interrupt(adc_ts->pdev);
}

/*
 * ADC touch screen sampling worker function
 */
static void adc_ts_work(struct work_struct *ts_work)
{
	struct adc_touch_device *adc_ts = container_of(ts_work,
				struct adc_touch_device, ts_work);
	struct device *dev = &adc_ts->pdev->dev;
	struct colibri_ts_platform_data *pdata = adc_ts->pdev->dev.platform_data;
	int val_x, val_y, val_z1, val_z2, val_p = 0;

	struct adc_feature feature = {
		.channel = ADC0,
		.clk_sel = ADCIOC_BUSCLK_SET,
		.clk_div_num = 1,
		.res_mode = 12,
		.sam_time = 6,
		.lp_con = ADCIOC_LPOFF_SET,
		.hs_oper = ADCIOC_HSOFF_SET,
		.vol_ref = ADCIOC_VR_VREF_SET,
		.tri_sel = ADCIOC_SOFTTS_SET,
		.ha_sel = ADCIOC_HA_SET,
		.ha_sam = 8,
		.do_ena = ADCIOC_DOEOFF_SET,
		.ac_ena = ADCIOC_ADACKENOFF_SET,
		.dma_ena = ADCIDC_DMAOFF_SET,
		.cc_ena = ADCIOC_CCEOFF_SET,
		.compare_func_ena = ADCIOC_ACFEOFF_SET,
		.range_ena = ADCIOC_ACRENOFF_SET,
		.greater_ena = ADCIOC_ACFGTOFF_SET,
		.result0 = 0,
		.result1 = 0,
	};

	mvf_adc_initiate(0);
	mvf_adc_set(0, &feature);

	mvf_adc_initiate(1);
	mvf_adc_set(1, &feature);

	while (!adc_ts->stop_touchscreen)
	{
		/* X-Direction */
		val_x = adc_ts_measure(pdata->gpio_xp, pdata->gpio_xm, 1, 0);
		if (val_x < 0)
			continue;

		/* Y-Direction */
		val_y = adc_ts_measure(pdata->gpio_yp, pdata->gpio_ym, 0, 0);
		if (val_y < 0)
			continue;

		/* Touch pressure
		 * Measure on XP/YM
		 */
		val_z1 = adc_ts_measure(pdata->gpio_yp, pdata->gpio_xm, 0, 1);
		if (val_z1 < 0)
			continue;
		val_z2 = adc_ts_measure(pdata->gpio_yp, pdata->gpio_xm, 1, 2);
		if (val_z2 < 0)
			continue;

		/* According to datasheet of our touchscreen, 
		 * resistance on X axis is 400~1200.. 
		 */
		/* Validate signal (avoid calculation using noise) */
		if (val_z1 > 64 && val_x > 64) {
			/* Calculate resistance between the plates
			 * lower resistance means higher pressure */
			int r_x = (1000 * val_x) / MVF_ADC_MAX;
			val_p = (r_x * val_z2) / val_z1 - r_x;
		} else {
			val_p = 2000;
		}

		dev_dbg(dev, "Measured values: x: %d, y: %d, z1: %d, z2: %d, "
			"p: %d\n", val_x, val_y, val_z1, val_z2, val_p);

		/*
		 * If touch pressure is too low, stop measuring and reenable
		 * touch detection
		 */
		if (val_p > 1800)
			break;

		/* Report touch position and sleep for next measurement */
		input_report_abs(adc_ts->ts_input, ABS_X, MVF_ADC_MAX - val_x);
		input_report_abs(adc_ts->ts_input, ABS_Y, MVF_ADC_MAX - val_y);
		input_report_abs(adc_ts->ts_input, ABS_PRESSURE, 2000 - val_p);
		input_report_key(adc_ts->ts_input, BTN_TOUCH, 1);
		input_sync(adc_ts->ts_input);

		msleep(10);
	}

	/* Report no more touch, reenable touch detection */
	input_report_abs(adc_ts->ts_input, ABS_PRESSURE, 0);
	input_report_key(adc_ts->ts_input, BTN_TOUCH, 0);
	input_sync(adc_ts->ts_input);

	/* Wait the pull-up to be stable on high */
	adc_ts_enable_touch_detection(adc_ts);
	msleep(10);

	/* Reenable IRQ to detect touch */
	enable_irq(adc_ts->pen_irq);

	dev_dbg(dev, "Reenabled touch detection interrupt\n");
}

static irqreturn_t adc_tc_touched(int irq, void *dev_id)
{
	struct adc_touch_device *adc_ts = (struct adc_touch_device *)dev_id;
	struct device *dev = &adc_ts->pdev->dev;
	struct colibri_ts_platform_data *pdata = adc_ts->pdev->dev.platform_data;

	dev_dbg(dev, "Touch detected, start worker thread\n");

	/* Stop IRQ */
	disable_irq_nosync(irq);

	/* Disable the touch detection plates */
	gpio_set_value(pdata->gpio_ym, 0);

	/* Let the platform mux to GPIO in order to enable Pull-Up on GPIO */
	if (pdata->mux_adc)
		pdata->mux_adc(adc_ts->pdev);

	/* Start worker thread */
	queue_work(adc_ts->ts_workqueue, &adc_ts->ts_work);

	return IRQ_HANDLED;
}

static int adc_ts_open(struct input_dev *dev_input)
{
	int ret;
	struct adc_touch_device *adc_ts = input_get_drvdata(dev_input);
	struct device *dev = &adc_ts->pdev->dev;
	struct colibri_ts_platform_data *pdata = adc_ts->pdev->dev.platform_data;

	dev_dbg(dev, "Input device %s opened, starting touch detection\n", 
			dev_input->name);

	adc_ts->stop_touchscreen = false;

	/* Initialize GPIOs, leave FETs closed by default */
	gpio_direction_output(pdata->gpio_xp, 1); /* Low active */
	gpio_direction_output(pdata->gpio_xm, 0); /* High active */
	gpio_direction_output(pdata->gpio_yp, 1); /* Low active */
	gpio_direction_output(pdata->gpio_ym, 0); /* High active */

	/* Mux detection before request IRQ, wait for pull-up to settle */
	adc_ts_enable_touch_detection(adc_ts);
	msleep(10);

	adc_ts->pen_irq = gpio_to_irq(pdata->gpio_pen_detect);
	if (adc_ts->pen_irq < 0) {
		dev_err(dev, "Unable to get IRQ for GPIO %d\n", 
				pdata->gpio_pen_detect);
		return adc_ts->pen_irq;
	}

	ret = request_irq(adc_ts->pen_irq, adc_tc_touched, IRQF_TRIGGER_FALLING,
			"touch detected", adc_ts);
	if (ret < 0) {
		dev_err(dev, "Unable to request IRQ %d\n", adc_ts->pen_irq);
		return ret;
	}

	return 0;
}

static void adc_ts_close(struct input_dev *dev_input)
{
	struct adc_touch_device *adc_ts = input_get_drvdata(dev_input);
	struct device *dev = &adc_ts->pdev->dev;

	free_irq(adc_ts->pen_irq, adc_ts);

	adc_ts->stop_touchscreen = true;

	/* Wait until touchscreen thread finishes any possible remnants. */
	cancel_work_sync(&adc_ts->ts_work);

	dev_dbg(dev, "Input device %s closed, disable touch detection\n", 
			dev_input->name);
}


static int __devinit adc_ts_probe(struct platform_device *pdev)
{
	int ret = 0;
	struct device *dev = &pdev->dev;
	struct input_dev *input;
	struct adc_touch_device *adc_ts;
	struct colibri_ts_platform_data *pdata = pdev->dev.platform_data;

	adc_ts = kzalloc(sizeof(struct adc_touch_device), GFP_KERNEL);
	if (!adc_ts) {
		dev_err(dev, "Failed to allocate TS device!\n");
		return -ENOMEM;
	}

	adc_ts->pdev = pdev;

	input = input_allocate_device();
	if (!input) {
		dev_err(dev, "Failed to allocate TS input device!\n");
		ret = -ENOMEM;
		goto err_input_allocate;
	}

	input->name = DRIVER_NAME;
	input->id.bustype = BUS_HOST;
	input->dev.parent = dev;
	input->open = adc_ts_open;
	input->close = adc_ts_close;

	__set_bit(EV_ABS, input->evbit);
	__set_bit(EV_KEY, input->evbit);
	__set_bit(BTN_TOUCH, input->keybit);
	input_set_abs_params(input, ABS_X, 0, MVF_ADC_MAX, 0, 0);
	input_set_abs_params(input, ABS_Y, 0, MVF_ADC_MAX, 0, 0);
	input_set_abs_params(input, ABS_PRESSURE, 0, MVF_ADC_MAX, 0, 0);

	adc_ts->ts_input = input;
	input_set_drvdata(input, adc_ts);
	ret = input_register_device(input);
	if (ret) {
		dev_err(dev, "failed to register input device\n");
		goto err;
	}

	/* Create workqueue for ADC sampling and calculation */
	INIT_WORK(&adc_ts->ts_work, adc_ts_work);
	adc_ts->ts_workqueue = create_singlethread_workqueue("mvf-adc-touch");

	if (!adc_ts->ts_workqueue) {
		dev_err(dev, "failed to create workqueue");
		goto err;
	}

	/* Request GPIOs for FETs and touch detection */
	ret = gpio_request(pdata->gpio_xp, "Touchscreen XP");
	if (ret)
		goto err;
	ret = gpio_request(pdata->gpio_xm, "Touchscreen XM");
	if (ret)
		goto err;
	ret = gpio_request(pdata->gpio_yp, "Touchscreen YP");
	if (ret)
		goto err;
	ret = gpio_request(pdata->gpio_ym, "Touchscreen YM");
	if (ret)
		goto err;
	ret = gpio_request(pdata->gpio_pen_detect, "Pen/Touch detect");
	if (ret)
		goto err;
	ret = gpio_request(pdata->gpio_pen_detect_pullup,
			"Pen/Touch detect pull-up");
	if (ret)
		goto err;

	dev_info(dev, "attached driver successfully\n");

	return 0;
err:
	input_free_device(touch->ts_input);

err_input_allocate:
	kfree(adc_ts);

	return ret;
}

static int __devexit adc_ts_remove(struct platform_device *pdev)
{
	struct adc_touch_device *adc_ts = platform_get_drvdata(pdev);

	input_unregister_device(adc_ts->ts_input);

	destroy_workqueue(adc_ts->ts_workqueue);
	kfree(adc_ts->ts_input);
	kfree(adc_ts);

	return 0;
}

static struct platform_driver adc_ts_driver = {
	.driver		= {
		.name	= DRIVER_NAME,
		.owner	= THIS_MODULE,
	},
	.probe		= adc_ts_probe,
	.remove		= __devexit_p(adc_ts_remove),
};

static int __init adc_ts_init(void)
{
	int ret;

	ret = platform_driver_register(&adc_ts_driver);
	if (ret)
		printk(KERN_ERR "%s: failed to add adc touchscreen driver\n", 
				__func__);

	return ret;
}

static void __exit adc_ts_exit(void)
{
	platform_driver_unregister(&adc_ts_driver);
}
module_init(adc_ts_init);
module_exit(adc_ts_exit);

MODULE_AUTHOR("Stefan Agner");
MODULE_DESCRIPTION("Colibri VF50 Touchscreen driver");
MODULE_LICENSE("GPL v2");
MODULE_VERSION(DRV_VERSION);