sync hh.org
[hh.org.git] / drivers / misc / ucb1x00-ts.c
blob14fe00a50358ef667be3500d1d536dd30d193049
1 /*
2 * linux/drivers/misc/ucb1x00-ts.c
4 * Copyright (C) 2001 Russell King, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * 21-Jan-2002 <jco@ict.es> :
12 * Added support for synchronous A/D mode. This mode is useful to
13 * avoid noise induced in the touchpanel by the LCD, provided that
14 * the UCB1x00 has a valid LCD sync signal routed to its ADCSYNC pin.
15 * It is important to note that the signal connected to the ADCSYNC
16 * pin should provide pulses even when the LCD is blanked, otherwise
17 * a pen touch needed to unblank the LCD will never be read.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/sched.h>
24 #include <linux/completion.h>
25 #include <linux/delay.h>
26 #include <linux/string.h>
27 #include <linux/input.h>
28 #include <linux/device.h>
29 #include <linux/slab.h>
31 #include <asm/dma.h>
32 #include <asm/semaphore.h>
34 #include "ucb1x00.h"
37 struct ucb1x00_ts {
38 struct input_dev idev;
39 struct ucb1x00 *ucb;
41 wait_queue_head_t irq_wait;
42 struct semaphore sem;
43 struct completion init_exit;
44 struct task_struct *rtask;
45 int use_count;
46 u16 x_res;
47 u16 y_res;
49 int restart:1;
50 int adcsync:1;
53 static int adcsync = UCB_NOSYNC;
55 static inline void ucb1x00_ts_evt_add(struct ucb1x00_ts *ts, u16 pressure, u16 x, u16 y)
57 input_report_abs(&ts->idev, ABS_X, x);
58 input_report_abs(&ts->idev, ABS_Y, y);
59 input_report_abs(&ts->idev, ABS_PRESSURE, pressure);
60 input_sync(&ts->idev);
63 static inline void ucb1x00_ts_event_release(struct ucb1x00_ts *ts)
65 input_report_abs(&ts->idev, ABS_PRESSURE, 0);
66 input_sync(&ts->idev);
70 * Switch to interrupt mode.
72 static inline void ucb1x00_ts_mode_int(struct ucb1x00_ts *ts)
74 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
75 UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
76 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
77 UCB_TS_CR_MODE_INT);
81 * Switch to pressure mode, and read pressure. We don't need to wait
82 * here, since both plates are being driven.
84 static inline unsigned int ucb1x00_ts_read_pressure(struct ucb1x00_ts *ts)
86 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
87 UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
88 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
89 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
91 return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
95 * Switch to X position mode and measure Y plate. We switch the plate
96 * configuration in pressure mode, then switch to position mode. This
97 * gives a faster response time. Even so, we need to wait about 55us
98 * for things to stabilise.
100 static inline unsigned int ucb1x00_ts_read_xpos(struct ucb1x00_ts *ts)
102 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
103 UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
104 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
105 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
106 UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
107 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
108 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
109 UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
110 UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
112 udelay(55);
114 return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
118 * Switch to Y position mode and measure X plate. We switch the plate
119 * configuration in pressure mode, then switch to position mode. This
120 * gives a faster response time. Even so, we need to wait about 55us
121 * for things to stabilise.
123 static inline unsigned int ucb1x00_ts_read_ypos(struct ucb1x00_ts *ts)
125 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
126 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
127 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
128 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
129 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
130 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
131 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
132 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
133 UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
135 udelay(55);
137 return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPX, ts->adcsync);
141 * Switch to X plate resistance mode. Set MX to ground, PX to
142 * supply. Measure current.
144 static inline unsigned int ucb1x00_ts_read_xres(struct ucb1x00_ts *ts)
146 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
147 UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
148 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
149 return ucb1x00_adc_read(ts->ucb, 0, ts->adcsync);
153 * Switch to Y plate resistance mode. Set MY to ground, PY to
154 * supply. Measure current.
156 static inline unsigned int ucb1x00_ts_read_yres(struct ucb1x00_ts *ts)
158 ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
159 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
160 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
161 return ucb1x00_adc_read(ts->ucb, 0, ts->adcsync);
165 * This is a RT kernel thread that handles the ADC accesses
166 * (mainly so we can use semaphores in the UCB1200 core code
167 * to serialise accesses to the ADC).
169 static int ucb1x00_thread(void *_ts)
171 struct ucb1x00_ts *ts = _ts;
172 struct task_struct *tsk = current;
173 DECLARE_WAITQUEUE(wait, tsk);
174 int valid;
176 ts->rtask = tsk;
178 daemonize("ktsd");
179 /* only want to receive SIGKILL */
180 allow_signal(SIGKILL);
183 * We could run as a real-time thread. However, thus far
184 * this doesn't seem to be necessary.
186 // tsk->policy = SCHED_FIFO;
187 // tsk->rt_priority = 1;
189 complete(&ts->init_exit);
191 valid = 0;
193 add_wait_queue(&ts->irq_wait, &wait);
194 for (;;) {
195 unsigned int x, y, p, val;
196 signed long timeout;
198 ts->restart = 0;
200 ucb1x00_adc_enable(ts->ucb);
202 x = ucb1x00_ts_read_xpos(ts);
203 y = ucb1x00_ts_read_ypos(ts);
204 p = ucb1x00_ts_read_pressure(ts);
207 * Switch back to interrupt mode.
209 ucb1x00_ts_mode_int(ts);
210 ucb1x00_adc_disable(ts->ucb);
212 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
213 schedule_timeout(HZ / 100);
214 if (signal_pending(tsk))
215 break;
217 ucb1x00_enable(ts->ucb);
218 val = ucb1x00_reg_read(ts->ucb, UCB_TS_CR);
220 if (val & (UCB_TS_CR_TSPX_LOW | UCB_TS_CR_TSMX_LOW)) {
221 set_task_state(tsk, TASK_INTERRUPTIBLE);
223 ucb1x00_enable_irq(ts->ucb, UCB_IRQ_TSPX, UCB_FALLING);
224 ucb1x00_disable(ts->ucb);
227 * If we spat out a valid sample set last time,
228 * spit out a "pen off" sample here.
230 if (valid) {
231 ucb1x00_ts_event_release(ts);
232 valid = 0;
235 timeout = MAX_SCHEDULE_TIMEOUT;
236 } else {
237 ucb1x00_disable(ts->ucb);
240 * Filtering is policy. Policy belongs in user
241 * space. We therefore leave it to user space
242 * to do any filtering they please.
244 if (!ts->restart) {
245 ucb1x00_ts_evt_add(ts, p, x, y);
246 valid = 1;
249 set_task_state(tsk, TASK_INTERRUPTIBLE);
250 timeout = HZ / 100;
253 schedule_timeout(timeout);
254 if (signal_pending(tsk))
255 break;
258 remove_wait_queue(&ts->irq_wait, &wait);
260 ts->rtask = NULL;
261 complete_and_exit(&ts->init_exit, 0);
265 * We only detect touch screen _touches_ with this interrupt
266 * handler, and even then we just schedule our task.
268 static void ucb1x00_ts_irq(int idx, void *id)
270 struct ucb1x00_ts *ts = id;
271 ucb1x00_disable_irq(ts->ucb, UCB_IRQ_TSPX, UCB_FALLING);
272 wake_up(&ts->irq_wait);
275 static int ucb1x00_ts_open(struct input_dev *idev)
277 struct ucb1x00_ts *ts = (struct ucb1x00_ts *)idev;
278 int ret = 0;
280 if (down_interruptible(&ts->sem))
281 return -EINTR;
283 if (ts->use_count++ != 0)
284 goto out;
286 if (ts->rtask)
287 panic("ucb1x00: rtask running?");
289 init_waitqueue_head(&ts->irq_wait);
290 ret = ucb1x00_hook_irq(ts->ucb, UCB_IRQ_TSPX, ucb1x00_ts_irq, ts);
291 if (ret < 0)
292 goto out;
295 * If we do this at all, we should allow the user to
296 * measure and read the X and Y resistance at any time.
298 ucb1x00_adc_enable(ts->ucb);
299 ts->x_res = ucb1x00_ts_read_xres(ts);
300 ts->y_res = ucb1x00_ts_read_yres(ts);
301 ucb1x00_adc_disable(ts->ucb);
303 init_completion(&ts->init_exit);
304 ret = kernel_thread(ucb1x00_thread, ts, 0);
305 if (ret >= 0) {
306 wait_for_completion(&ts->init_exit);
307 ret = 0;
308 } else {
309 ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
312 out:
313 if (ret)
314 ts->use_count--;
315 up(&ts->sem);
316 return ret;
320 * Release touchscreen resources. Disable IRQs.
322 static void ucb1x00_ts_close(struct input_dev *idev)
324 struct ucb1x00_ts *ts = (struct ucb1x00_ts *)idev;
326 down(&ts->sem);
327 if (--ts->use_count == 0) {
328 if (ts->rtask) {
329 send_sig(SIGKILL, ts->rtask, 1);
330 wait_for_completion(&ts->init_exit);
333 ucb1x00_enable(ts->ucb);
334 ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
335 ucb1x00_reg_write(ts->ucb, UCB_TS_CR, 0);
336 ucb1x00_disable(ts->ucb);
338 up(&ts->sem);
341 static int ucb1x00_ts_resume(struct ucb1x00 *ucb)
343 struct ucb1x00_ts *ts = (struct ucb1x00_ts*)ucb->ts_data;
345 if (ts->rtask != NULL) {
347 * Restart the TS thread to ensure the
348 * TS interrupt mode is set up again
349 * after sleep.
351 ts->restart = 1;
352 wake_up(&ts->irq_wait);
355 return 0;
360 * Initialisation.
362 static int ucb1x00_ts_add(struct class_device *dev)
364 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
365 struct ucb1x00_ts *ts;
367 ts = kmalloc(sizeof(struct ucb1x00_ts), GFP_KERNEL);
368 if (!ts)
369 return -ENOMEM;
371 memset(ts, 0, sizeof(struct ucb1x00_ts));
373 ts->ucb = ucb;
374 ts->adcsync = adcsync;
375 init_MUTEX(&ts->sem);
377 ts->idev.name = "Touchscreen panel";
378 ts->idev.id.product = ts->ucb->id;
379 ts->idev.open = ucb1x00_ts_open;
380 ts->idev.close = ucb1x00_ts_close;
382 __set_bit(EV_ABS, ts->idev.evbit);
383 __set_bit(ABS_X, ts->idev.absbit);
384 __set_bit(ABS_Y, ts->idev.absbit);
385 __set_bit(ABS_PRESSURE, ts->idev.absbit);
387 input_register_device(&ts->idev);
389 ucb->ts_data = ts;
391 return 0;
394 static void ucb1x00_ts_remove(struct class_device *dev)
396 struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
397 struct ucb1x00_ts *ts = ucb->ts_data;
399 input_unregister_device(&ts->idev);
400 kfree(ts);
403 static struct ucb1x00_class_interface ucb1x00_ts_interface = {
404 .interface = {
405 .add = ucb1x00_ts_add,
406 .remove = ucb1x00_ts_remove,
408 .resume = ucb1x00_ts_resume,
411 static int __init ucb1x00_ts_init(void)
413 return ucb1x00_register_interface(&ucb1x00_ts_interface);
416 static void __exit ucb1x00_ts_exit(void)
418 ucb1x00_unregister_interface(&ucb1x00_ts_interface);
421 #ifndef MODULE
424 * Parse kernel command-line options.
426 * syntax : ucbts=[sync|nosync],...
428 static int __init ucb1x00_ts_setup(char *str)
430 char *p;
432 while ((p = strsep(&str, ",")) != NULL) {
433 if (strcmp(p, "sync") == 0)
434 adcsync = UCB_SYNC;
437 return 1;
440 __setup("ucbts=", ucb1x00_ts_setup);
442 #else
444 MODULE_PARM(adcsync, "i");
445 MODULE_PARM_DESC(adcsync, "Enable use of ADCSYNC signal");
447 #endif
449 module_init(ucb1x00_ts_init);
450 module_exit(ucb1x00_ts_exit);
452 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
453 MODULE_DESCRIPTION("UCB1x00 touchscreen driver");
454 MODULE_LICENSE("GPL");