xref: /openbmc/linux/drivers/mfd/ucb1x00-ts.c (revision 7658e7f9)
1 /*
2  *  Touchscreen driver for UCB1x00-based touchscreens
3  *
4  *  Copyright (C) 2001 Russell King, All Rights Reserved.
5  *  Copyright (C) 2005 Pavel Machek
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * 21-Jan-2002 <jco@ict.es> :
12  *
13  * Added support for synchronous A/D mode. This mode is useful to
14  * avoid noise induced in the touchpanel by the LCD, provided that
15  * the UCB1x00 has a valid LCD sync signal routed to its ADCSYNC pin.
16  * It is important to note that the signal connected to the ADCSYNC
17  * pin should provide pulses even when the LCD is blanked, otherwise
18  * a pen touch needed to unblank the LCD will never be read.
19  */
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/smp.h>
24 #include <linux/sched.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/string.h>
28 #include <linux/input.h>
29 #include <linux/device.h>
30 #include <linux/freezer.h>
31 #include <linux/slab.h>
32 #include <linux/kthread.h>
33 #include <linux/mfd/ucb1x00.h>
34 
35 #include <mach/collie.h>
36 #include <asm/mach-types.h>
37 
38 
39 
40 struct ucb1x00_ts {
41 	struct input_dev	*idev;
42 	struct ucb1x00		*ucb;
43 
44 	wait_queue_head_t	irq_wait;
45 	struct task_struct	*rtask;
46 	u16			x_res;
47 	u16			y_res;
48 
49 	unsigned int		adcsync:1;
50 };
51 
52 static int adcsync;
53 
54 static inline void ucb1x00_ts_evt_add(struct ucb1x00_ts *ts, u16 pressure, u16 x, u16 y)
55 {
56 	struct input_dev *idev = ts->idev;
57 
58 	input_report_abs(idev, ABS_X, x);
59 	input_report_abs(idev, ABS_Y, y);
60 	input_report_abs(idev, ABS_PRESSURE, pressure);
61 	input_report_key(idev, BTN_TOUCH, 1);
62 	input_sync(idev);
63 }
64 
65 static inline void ucb1x00_ts_event_release(struct ucb1x00_ts *ts)
66 {
67 	struct input_dev *idev = ts->idev;
68 
69 	input_report_abs(idev, ABS_PRESSURE, 0);
70 	input_report_key(idev, BTN_TOUCH, 0);
71 	input_sync(idev);
72 }
73 
74 /*
75  * Switch to interrupt mode.
76  */
77 static inline void ucb1x00_ts_mode_int(struct ucb1x00_ts *ts)
78 {
79 	ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
80 			UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
81 			UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
82 			UCB_TS_CR_MODE_INT);
83 }
84 
85 /*
86  * Switch to pressure mode, and read pressure.  We don't need to wait
87  * here, since both plates are being driven.
88  */
89 static inline unsigned int ucb1x00_ts_read_pressure(struct ucb1x00_ts *ts)
90 {
91 	if (machine_is_collie()) {
92 		ucb1x00_io_write(ts->ucb, COLLIE_TC35143_GPIO_TBL_CHK, 0);
93 		ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
94 				  UCB_TS_CR_TSPX_POW | UCB_TS_CR_TSMX_POW |
95 				  UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
96 
97 		udelay(55);
98 
99 		return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_AD2, ts->adcsync);
100 	} else {
101 		ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
102 				  UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
103 				  UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
104 				  UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
105 
106 		return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
107 	}
108 }
109 
110 /*
111  * Switch to X position mode and measure Y plate.  We switch the plate
112  * configuration in pressure mode, then switch to position mode.  This
113  * gives a faster response time.  Even so, we need to wait about 55us
114  * for things to stabilise.
115  */
116 static inline unsigned int ucb1x00_ts_read_xpos(struct ucb1x00_ts *ts)
117 {
118 	if (machine_is_collie())
119 		ucb1x00_io_write(ts->ucb, 0, COLLIE_TC35143_GPIO_TBL_CHK);
120 	else {
121 		ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
122 				  UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
123 				  UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
124 		ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
125 				  UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
126 				  UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
127 	}
128 	ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
129 			UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
130 			UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
131 
132 	udelay(55);
133 
134 	return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
135 }
136 
137 /*
138  * Switch to Y position mode and measure X plate.  We switch the plate
139  * configuration in pressure mode, then switch to position mode.  This
140  * gives a faster response time.  Even so, we need to wait about 55us
141  * for things to stabilise.
142  */
143 static inline unsigned int ucb1x00_ts_read_ypos(struct ucb1x00_ts *ts)
144 {
145 	if (machine_is_collie())
146 		ucb1x00_io_write(ts->ucb, 0, COLLIE_TC35143_GPIO_TBL_CHK);
147 	else {
148 		ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
149 				  UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
150 				  UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
151 		ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
152 				  UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
153 				  UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
154 	}
155 
156 	ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
157 			UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
158 			UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
159 
160 	udelay(55);
161 
162 	return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPX, ts->adcsync);
163 }
164 
165 /*
166  * Switch to X plate resistance mode.  Set MX to ground, PX to
167  * supply.  Measure current.
168  */
169 static inline unsigned int ucb1x00_ts_read_xres(struct ucb1x00_ts *ts)
170 {
171 	ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
172 			UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
173 			UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
174 	return ucb1x00_adc_read(ts->ucb, 0, ts->adcsync);
175 }
176 
177 /*
178  * Switch to Y plate resistance mode.  Set MY to ground, PY to
179  * supply.  Measure current.
180  */
181 static inline unsigned int ucb1x00_ts_read_yres(struct ucb1x00_ts *ts)
182 {
183 	ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
184 			UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
185 			UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
186 	return ucb1x00_adc_read(ts->ucb, 0, ts->adcsync);
187 }
188 
189 static inline int ucb1x00_ts_pen_down(struct ucb1x00_ts *ts)
190 {
191 	unsigned int val = ucb1x00_reg_read(ts->ucb, UCB_TS_CR);
192 
193 	if (machine_is_collie())
194 		return (!(val & (UCB_TS_CR_TSPX_LOW)));
195 	else
196 		return (val & (UCB_TS_CR_TSPX_LOW | UCB_TS_CR_TSMX_LOW));
197 }
198 
199 /*
200  * This is a RT kernel thread that handles the ADC accesses
201  * (mainly so we can use semaphores in the UCB1200 core code
202  * to serialise accesses to the ADC).
203  */
204 static int ucb1x00_thread(void *_ts)
205 {
206 	struct ucb1x00_ts *ts = _ts;
207 	DECLARE_WAITQUEUE(wait, current);
208 	bool frozen, ignore = false;
209 	int valid = 0;
210 
211 	set_freezable();
212 	add_wait_queue(&ts->irq_wait, &wait);
213 	while (!kthread_freezable_should_stop(&frozen)) {
214 		unsigned int x, y, p;
215 		signed long timeout;
216 
217 		if (frozen)
218 			ignore = true;
219 
220 		ucb1x00_adc_enable(ts->ucb);
221 
222 		x = ucb1x00_ts_read_xpos(ts);
223 		y = ucb1x00_ts_read_ypos(ts);
224 		p = ucb1x00_ts_read_pressure(ts);
225 
226 		/*
227 		 * Switch back to interrupt mode.
228 		 */
229 		ucb1x00_ts_mode_int(ts);
230 		ucb1x00_adc_disable(ts->ucb);
231 
232 		msleep(10);
233 
234 		ucb1x00_enable(ts->ucb);
235 
236 
237 		if (ucb1x00_ts_pen_down(ts)) {
238 			set_current_state(TASK_INTERRUPTIBLE);
239 
240 			ucb1x00_enable_irq(ts->ucb, UCB_IRQ_TSPX, machine_is_collie() ? UCB_RISING : UCB_FALLING);
241 			ucb1x00_disable(ts->ucb);
242 
243 			/*
244 			 * If we spat out a valid sample set last time,
245 			 * spit out a "pen off" sample here.
246 			 */
247 			if (valid) {
248 				ucb1x00_ts_event_release(ts);
249 				valid = 0;
250 			}
251 
252 			timeout = MAX_SCHEDULE_TIMEOUT;
253 		} else {
254 			ucb1x00_disable(ts->ucb);
255 
256 			/*
257 			 * Filtering is policy.  Policy belongs in user
258 			 * space.  We therefore leave it to user space
259 			 * to do any filtering they please.
260 			 */
261 			if (!ignore) {
262 				ucb1x00_ts_evt_add(ts, p, x, y);
263 				valid = 1;
264 			}
265 
266 			set_current_state(TASK_INTERRUPTIBLE);
267 			timeout = HZ / 100;
268 		}
269 
270 		schedule_timeout(timeout);
271 	}
272 
273 	remove_wait_queue(&ts->irq_wait, &wait);
274 
275 	ts->rtask = NULL;
276 	return 0;
277 }
278 
279 /*
280  * We only detect touch screen _touches_ with this interrupt
281  * handler, and even then we just schedule our task.
282  */
283 static void ucb1x00_ts_irq(int idx, void *id)
284 {
285 	struct ucb1x00_ts *ts = id;
286 
287 	ucb1x00_disable_irq(ts->ucb, UCB_IRQ_TSPX, UCB_FALLING);
288 	wake_up(&ts->irq_wait);
289 }
290 
291 static int ucb1x00_ts_open(struct input_dev *idev)
292 {
293 	struct ucb1x00_ts *ts = input_get_drvdata(idev);
294 	int ret = 0;
295 
296 	BUG_ON(ts->rtask);
297 
298 	init_waitqueue_head(&ts->irq_wait);
299 	ret = ucb1x00_hook_irq(ts->ucb, UCB_IRQ_TSPX, ucb1x00_ts_irq, ts);
300 	if (ret < 0)
301 		goto out;
302 
303 	/*
304 	 * If we do this at all, we should allow the user to
305 	 * measure and read the X and Y resistance at any time.
306 	 */
307 	ucb1x00_adc_enable(ts->ucb);
308 	ts->x_res = ucb1x00_ts_read_xres(ts);
309 	ts->y_res = ucb1x00_ts_read_yres(ts);
310 	ucb1x00_adc_disable(ts->ucb);
311 
312 	ts->rtask = kthread_run(ucb1x00_thread, ts, "ktsd");
313 	if (!IS_ERR(ts->rtask)) {
314 		ret = 0;
315 	} else {
316 		ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
317 		ts->rtask = NULL;
318 		ret = -EFAULT;
319 	}
320 
321  out:
322 	return ret;
323 }
324 
325 /*
326  * Release touchscreen resources.  Disable IRQs.
327  */
328 static void ucb1x00_ts_close(struct input_dev *idev)
329 {
330 	struct ucb1x00_ts *ts = input_get_drvdata(idev);
331 
332 	if (ts->rtask)
333 		kthread_stop(ts->rtask);
334 
335 	ucb1x00_enable(ts->ucb);
336 	ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
337 	ucb1x00_reg_write(ts->ucb, UCB_TS_CR, 0);
338 	ucb1x00_disable(ts->ucb);
339 }
340 
341 
342 /*
343  * Initialisation.
344  */
345 static int ucb1x00_ts_add(struct ucb1x00_dev *dev)
346 {
347 	struct ucb1x00_ts *ts;
348 	struct input_dev *idev;
349 	int err;
350 
351 	ts = kzalloc(sizeof(struct ucb1x00_ts), GFP_KERNEL);
352 	idev = input_allocate_device();
353 	if (!ts || !idev) {
354 		err = -ENOMEM;
355 		goto fail;
356 	}
357 
358 	ts->ucb = dev->ucb;
359 	ts->idev = idev;
360 	ts->adcsync = adcsync ? UCB_SYNC : UCB_NOSYNC;
361 
362 	idev->name       = "Touchscreen panel";
363 	idev->id.product = ts->ucb->id;
364 	idev->open       = ucb1x00_ts_open;
365 	idev->close      = ucb1x00_ts_close;
366 
367 	idev->evbit[0]   = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY);
368 	idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
369 
370 	input_set_drvdata(idev, ts);
371 
372 	ucb1x00_adc_enable(ts->ucb);
373 	ts->x_res = ucb1x00_ts_read_xres(ts);
374 	ts->y_res = ucb1x00_ts_read_yres(ts);
375 	ucb1x00_adc_disable(ts->ucb);
376 
377 	input_set_abs_params(idev, ABS_X, 0, ts->x_res, 0, 0);
378 	input_set_abs_params(idev, ABS_Y, 0, ts->y_res, 0, 0);
379 	input_set_abs_params(idev, ABS_PRESSURE, 0, 0, 0, 0);
380 
381 	err = input_register_device(idev);
382 	if (err)
383 		goto fail;
384 
385 	dev->priv = ts;
386 
387 	return 0;
388 
389  fail:
390 	input_free_device(idev);
391 	kfree(ts);
392 	return err;
393 }
394 
395 static void ucb1x00_ts_remove(struct ucb1x00_dev *dev)
396 {
397 	struct ucb1x00_ts *ts = dev->priv;
398 
399 	input_unregister_device(ts->idev);
400 	kfree(ts);
401 }
402 
403 static struct ucb1x00_driver ucb1x00_ts_driver = {
404 	.add		= ucb1x00_ts_add,
405 	.remove		= ucb1x00_ts_remove,
406 };
407 
408 static int __init ucb1x00_ts_init(void)
409 {
410 	return ucb1x00_register_driver(&ucb1x00_ts_driver);
411 }
412 
413 static void __exit ucb1x00_ts_exit(void)
414 {
415 	ucb1x00_unregister_driver(&ucb1x00_ts_driver);
416 }
417 
418 module_param(adcsync, int, 0444);
419 module_init(ucb1x00_ts_init);
420 module_exit(ucb1x00_ts_exit);
421 
422 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
423 MODULE_DESCRIPTION("UCB1x00 touchscreen driver");
424 MODULE_LICENSE("GPL");
425