add 3.2 support
[openwrt/staging/mkresin.git] / target / linux / ep93xx / patches-3.2 / 003-ep93xx_touchscreen.patch
1 ---
2 arch/arm/mach-ep93xx/include/mach/hardware.h | 1
3 arch/arm/mach-ep93xx/include/mach/regs_touch.h | 95 ++
4 drivers/input/touchscreen/Kconfig | 5
5 drivers/input/touchscreen/Makefile | 1
6 drivers/input/touchscreen/ep93xx_ts.c | 1117 +++++++++++++++++++++++++
7 drivers/input/touchscreen/ep93xx_ts.h | 53 +
8 6 files changed, 1272 insertions(+)
9
10 --- a/drivers/input/touchscreen/Kconfig
11 +++ b/drivers/input/touchscreen/Kconfig
12 @@ -177,6 +177,15 @@ config TOUCHSCREEN_EETI
13 To compile this driver as a module, choose M here: the
14 module will be called eeti_ts.
15
16 +config TOUCHSCREEN_EP93XX
17 + tristate "EP93xx Touchscreen"
18 + depends on ARM && INPUT && ARCH_EP93XX
19 + help
20 + Say Y here to enable support for EP93xx touch screen.
21 +
22 + To compile this driver as a module, choose M here:
23 + the module will be called ep93xx_ts.
24 +
25 config TOUCHSCREEN_FUJITSU
26 tristate "Fujitsu serial touchscreen"
27 select SERIO
28 --- a/drivers/input/touchscreen/Makefile
29 +++ b/drivers/input/touchscreen/Makefile
30 @@ -23,6 +23,7 @@ obj-$(CONFIG_TOUCHSCREEN_HAMPSHIRE) += h
31 obj-$(CONFIG_TOUCHSCREEN_GUNZE) += gunze.o
32 obj-$(CONFIG_TOUCHSCREEN_EETI) += eeti_ts.o
33 obj-$(CONFIG_TOUCHSCREEN_ELO) += elo.o
34 +obj-$(CONFIG_TOUCHSCREEN_EP93XX) += ep93xx_ts.o
35 obj-$(CONFIG_TOUCHSCREEN_FUJITSU) += fujitsu_ts.o
36 obj-$(CONFIG_TOUCHSCREEN_INEXIO) += inexio.o
37 obj-$(CONFIG_TOUCHSCREEN_INTEL_MID) += intel-mid-touch.o
38 --- /dev/null
39 +++ b/drivers/input/touchscreen/ep93xx_ts.c
40 @@ -0,0 +1,1021 @@
41 +/*
42 + * linux/drivers/input/touchscreen/ep93xx_ts.c
43 + *
44 + * Copyright (C) 2003-2004 Cirrus Corp.
45 + *
46 + * This program is free software; you can redistribute it and/or modify
47 + * it under the terms of the GNU General Public License version 2 as
48 + * published by the Free Software Foundation.
49 + */
50 +
51 +#include <linux/module.h>
52 +#include <linux/types.h>
53 +#include <linux/delay.h>
54 +#include <linux/wait.h>
55 +#include <linux/fs.h>
56 +#include <linux/sched.h>
57 +#include <linux/poll.h>
58 +#include <linux/miscdevice.h>
59 +#include <linux/init.h>
60 +#include <linux/compiler.h>
61 +#include <linux/timer.h>
62 +#include <linux/interrupt.h>
63 +#include <linux/syscalls.h>
64 +#include <linux/input.h>
65 +#include <linux/semaphore.h>
66 +#include <asm/irq.h>
67 +#include <mach/hardware.h>
68 +#include <asm/io.h>
69 +
70 +/* This stuff should be in ep93xx-regs.h */
71 +#define EP93XX_TOUCHSCREEN_REG(x) (EP93XX_TOUCHSCREEN_BASE + (x))
72 +/* R/W touchscreen controller setup control register. */
73 +#define EP93XX_TOUCHSCREEN_SETUP EP93XX_TOUCHSCREEN_REG(0x00)
74 +/* R/W touchscreen controller max/min register. */
75 +#define EP93XX_TOUCHSCREEN_XYMAXMIN EP93XX_TOUCHSCREEN_REG(0x04)
76 +/* R touchscreen controller result register. */
77 +#define EP93XX_TOUCHSCREEN_XYRESULT EP93XX_TOUCHSCREEN_REG(0x08)
78 +/* LOCKED R/W touchscreen Switch Matrix control register. */
79 +#define EP93XX_TOUCHSCREEN_DISCHARGE EP93XX_TOUCHSCREEN_REG(0x0C)
80 +#define EP93XX_TOUCHSCREEN_XSAMPLE EP93XX_TOUCHSCREEN_REG(0x10)
81 +#define EP93XX_TOUCHSCREEN_YSAMPLE EP93XX_TOUCHSCREEN_REG(0x14)
82 +#define EP93XX_TOUCHSCREEN_DIRECT EP93XX_TOUCHSCREEN_REG(0x18)
83 +#define EP93XX_TOUCHSCREEN_DETECT EP93XX_TOUCHSCREEN_REG(0x1C)
84 +/* NA R/W touchscreen software lock register. */
85 +#define EP93XX_TOUCHSCREEN_SWLOCK EP93XX_TOUCHSCREEN_REG(0x20)
86 +/* R/W touchscreen setup control register #2. */
87 +#define EP93XX_TOUCHSCREEN_SETUP2 EP93XX_TOUCHSCREEN_REG(0x24)
88 +
89 +/* These are duplicated in mach-ep93xx/core.c */
90 +#define EP93XX_TIMER_REG(x) (EP93XX_TIMER_BASE + (x))
91 +#define EP93XX_TIMER2_LOAD EP93XX_TIMER_REG(0x20)
92 +#define EP93XX_TIMER2_VALUE EP93XX_TIMER_REG(0x24)
93 +#define EP93XX_TIMER2_CONTROL EP93XX_TIMER_REG(0x28)
94 +#define EP93XX_TIMER2_CLEAR EP93XX_TIMER_REG(0x2c)
95 +
96 +/*
97 + * Register bit definitions
98 + */
99 +#define TSSETUP_SDLY_MASK 0x000003FF
100 +#define TSSETUP_SDLY_SHIFT 0
101 +#define TSSETUP_NSMP_4 0x00000000
102 +#define TSSETUP_NSMP_8 0x00000400
103 +#define TSSETUP_NSMP_16 0x00000800
104 +#define TSSETUP_NSMP_32 0x00000C00
105 +#define TSSETUP_NSMP_MASK 0x00000C00
106 +#define TSSETUP_DEV_4 0x00000000
107 +#define TSSETUP_DEV_8 0x00001000
108 +#define TSSETUP_DEV_12 0x00002000
109 +#define TSSETUP_DEV_16 0x00003000
110 +#define TSSETUP_DEV_24 0x00004000
111 +#define TSSETUP_DEV_32 0x00005000
112 +#define TSSETUP_DEV_64 0x00006000
113 +#define TSSETUP_DEV_128 0x00007000
114 +#define TSSETUP_ENABLE 0x00008000
115 +#define TSSETUP_DLY_MASK 0x03FF0000
116 +#define TSSETUP_DLY_SHIFT 16
117 +#define TSSETUP_TDTCT 0x80000000
118 +
119 +#define TSMAXMIN_XMIN_MASK 0x000000FF
120 +#define TSMAXMIN_XMIN_SHIFT 0
121 +#define TSMAXMIN_YMIN_MASK 0x0000FF00
122 +#define TSMAXMIN_YMIN_SHIFT 8
123 +#define TSMAXMIN_XMAX_MASK 0x00FF0000
124 +#define TSMAXMIN_XMAX_SHIFT 16
125 +#define TSMAXMIN_YMAX_MASK 0xFF000000
126 +#define TSMAXMIN_YMAX_SHIFT 24
127 +
128 +#define TSXYRESULT_X_MASK 0x00000FFF
129 +#define TSXYRESULT_X_SHIFT 0
130 +#define TSXYRESULT_AD_MASK 0x0000FFFF
131 +#define TSXYRESULT_AD_SHIFT 0
132 +#define TSXYRESULT_Y_MASK 0x0FFF0000
133 +#define TSXYRESULT_Y_SHIFT 16
134 +#define TSXYRESULT_SDR 0x80000000
135 +
136 +#define TSX_SAMPLE_MASK 0x00003FFF
137 +#define TSX_SAMPLE_SHIFT 0x00
138 +#define TSY_SAMPLE_MASK 0x3FFF0000
139 +#define TSY_SAMPLE_SHIFT 0x10
140 +
141 +#define TSSETUP2_TINT 0x00000001
142 +#define TSSETUP2_NICOR 0x00000002
143 +#define TSSETUP2_PINT 0x00000004
144 +#define TSSETUP2_PENSTS 0x00000008
145 +#define TSSETUP2_PINTEN 0x00000010
146 +#define TSSETUP2_DEVINT 0x00000020
147 +#define TSSETUP2_DINTEN 0x00000040
148 +#define TSSETUP2_DTMEN 0x00000080
149 +#define TSSETUP2_DISDEV 0x00000100
150 +#define TSSETUP2_NSIGND 0x00000200
151 +#define TSSETUP2_S28EN 0x00000400
152 +#define TSSETUP2_RINTEN 0x00000800
153 +
154 +#define TSXYRESULT_SDR 0x80000000
155 +
156 +/*
157 + * These are used as trigger levels to know when we have pen up/down.
158 + * The rules:
159 + * 1. TS_HEAVY_INV_PRESSURE < TS_LIGHT_INV_PRESSURE because these
160 + * are Inverse pressure.
161 + * 2. Any touch lighter than TS_LIGHT_INV_PRESSURE is a pen up.
162 + * 3. Any touch heavier than TS_HEAVY_INV_PRESSURE is a pen down.
163 + */
164 +#define TS_HEAVY_INV_PRESSURE 0xFE0 /* C00 */
165 +#define TS_LIGHT_INV_PRESSURE 0xFFF /* e00 */
166 +
167 +/*
168 + * If the x, y, or inverse pressure changes more than these values
169 + * between two succeeding points, the point is not reported.
170 + */
171 +#define TS_MAX_VALID_XY_CHANGE 0x300
172 +#define TS_MAX_VALID_PRESSURE_CHANGE 0x100
173 +
174 +/*
175 + * This is the minimum Z1 Value that is valid.
176 + */
177 +#define MIN_Z1_VALUE 0x50
178 +
179 +/*
180 + * Settling delay for taking each ADC measurement. Increase this
181 + * if ts is jittery.
182 + */
183 +#define EP93XX_TS_ADC_DELAY_USEC 2000
184 +
185 +/*
186 + * Delay between TS points.
187 + */
188 +#define EP93XX_TS_PER_POINT_DELAY_USEC 10000
189 +
190 +/*
191 + * A few more macros...
192 + */
193 +#define TSSETUP_DEFAULT (TSSETUP_NSMP_32 | TSSETUP_DEV_64 | \
194 + ((128<<TSSETUP_SDLY_SHIFT) & TSSETUP_SDLY_MASK) | \
195 + ((128<<TSSETUP_DLY_SHIFT) & TSSETUP_DLY_MASK))
196 +
197 +#define TSSETUP2_DEFAULT (TSSETUP2_NSIGND)
198 +
199 +/*
200 + * For now, we use one of the minor numbers from the local/experimental
201 + * range.
202 + */
203 +#define EP93XX_TS_MINOR 240
204 +
205 +/*
206 + * Static Declarations
207 + */
208 +static unsigned int guiLastX, guiLastY;
209 +static unsigned int guiLastInvPressure;
210 +
211 +struct TouchScreenSample
212 +{
213 + int currentX;
214 + int currentY;
215 + int currentButton;
216 + int currentPressure;
217 + struct timeval currentTime;
218 +};
219 +
220 +/*
221 + * This must match the structure in tslib.
222 + */
223 +struct ts_sample {
224 + int x;
225 + int y;
226 + unsigned int pressure;
227 + struct timeval tv;
228 +};
229 +
230 +static struct TouchScreenSample gSample;
231 +static int bFreshTouchData;
232 +static int bCurrentPenDown;
233 +
234 +static DECLARE_WAIT_QUEUE_HEAD(queue);
235 +static DEFINE_SEMAPHORE(open_sem);
236 +static DEFINE_SPINLOCK(event_buffer_lock);
237 +static struct fasync_struct *ep93xx_fasync;
238 +
239 +/*
240 + * Typedef Declarations
241 + */
242 +typedef enum {
243 + TS_MODE_UN_INITIALIZED,
244 + TS_MODE_HARDWARE_SCAN,
245 + TS_MODE_SOFT_SCAN
246 +} ts_mode_t;
247 +
248 +static ts_mode_t gScanningMode;
249 +
250 +typedef enum{
251 + TS_STATE_STOPPED = 0,
252 + TS_STATE_Z1,
253 + TS_STATE_Z2,
254 + TS_STATE_Y,
255 + TS_STATE_X,
256 + TS_STATE_DONE
257 +} ts_states_t;
258 +
259 +typedef struct
260 +{
261 + unsigned int uiX;
262 + unsigned int uiY;
263 + unsigned int uiZ1;
264 + unsigned int uiZ2;
265 + ts_states_t state;
266 +} ts_struct_t;
267 +
268 +static ts_struct_t sTouch;
269 +
270 +/*
271 + * From the spec, here's how to set up the touch screen's switch registers.
272 + */
273 +typedef struct
274 +{
275 + unsigned int uiDetect;
276 + unsigned int uiDischarge;
277 + unsigned int uiXSample;
278 + unsigned int uiYSample;
279 + unsigned int uiSwitchZ1;
280 + unsigned int uiSwitchZ2;
281 +}SwitchStructType;
282 +
283 +/*
284 + * Here's the switch settings for a 4-wire touchscreen. See the spec
285 + * for how to handle a 4, 7, or 8-wire.
286 + */
287 +const static SwitchStructType sSwitchSettings =
288 +/* s28en=0 */
289 +/* TSDetect TSDischarge TSXSample TSYSample SwitchZ1 SwitchZ2 */
290 + {0x00403604, 0x0007fe04, 0x00081604, 0x00104601, 0x00101601, 0x00101608};
291 +
292 +/*
293 + * Function declarations
294 + */
295 +static void ep93xx_ts_set_direct(unsigned int uiADCSwitch);
296 +static irqreturn_t ep93xx_ts_isr(int irq, void *dev_id);
297 +static irqreturn_t ep93xx_timer2_isr(int irq, void *dev_id);
298 +static void ee93xx_ts_evt_add(int button, int dX, int dY, int Pressure);
299 +static ssize_t ep93xx_ts_read(struct file *filp, char *buf,
300 + size_t count, loff_t *l);
301 +static unsigned int ep93xx_ts_poll(struct file *filp, poll_table *wait);
302 +static int ep93xx_ts_open(struct inode *inode, struct file *filp);
303 +static int ep93xx_ts_fasync(int fd, struct file *filp, int on);
304 +static int ep93xx_ts_release(struct inode *inode, struct file *filp);
305 +static ssize_t ep93xx_ts_write(struct file *file, const char *buffer,
306 + size_t count, loff_t *ppos);
307 +static void ep93xx_ts_setup(void);
308 +static void ep93xx_ts_shutdown(void);
309 +int __init ep93xx_ts_init(void);
310 +void __exit ep93xx_ts_exit(void);
311 +static unsigned int CalculateInvPressure(void);
312 +static unsigned int ADCGetData(unsigned int uiSamples, unsigned int uiMaxDiff);
313 +static void TS_Soft_Scan_Mode(void);
314 +static void TS_Hardware_Scan_Mode(void);
315 +static void ProcessPointData(void);
316 +static void Set_Timer2_uSec(unsigned int Delay_mSec);
317 +static void Stop_Timer2(void);
318 +
319 +/*
320 + * ep93xx_ts_isr
321 + */
322 +static irqreturn_t ep93xx_ts_isr(int irq, void *dev_id)
323 +{
324 + /*
325 + * Note that we don't clear the interrupt here. The interrupt
326 + * gets cleared in TS_Soft_Scan_Mode when the TS ENABLE
327 + * bit is cleared.
328 + */
329 +
330 + /*
331 + * Set the ts to manual polling mode and schedule a callback.
332 + * That way we can return from the isr in a reasonable amount of
333 + * time and process the touch in the callback after a brief delay.
334 + */
335 + TS_Soft_Scan_Mode();
336 +
337 + return(IRQ_HANDLED);
338 +}
339 +
340 +/*
341 + * Save the current ts 'event' in an atomic fashion.
342 + */
343 +static void ee93xx_ts_evt_add(int buttons, int iX, int iY, int iPressure)
344 +{
345 + /*
346 + * Note the event, but use spinlocks to keep it from getting
347 + * halfway read if we get interrupted.
348 + */
349 +
350 + spin_lock(&event_buffer_lock);
351 +
352 + gSample.currentX = iX;
353 + gSample.currentY = iY;
354 + gSample.currentButton = buttons;
355 + gSample.currentPressure = iPressure;
356 + bFreshTouchData = 1;
357 + do_gettimeofday(&gSample.currentTime);
358 +
359 + spin_unlock(&event_buffer_lock);
360 +
361 + kill_fasync(&ep93xx_fasync, SIGIO, POLL_IN);
362 + wake_up_interruptible(&queue);
363 +}
364 +
365 +
366 +static ssize_t ep93xx_ts_read(struct file *filp, char *buf, size_t count, loff_t *l)
367 +{
368 + unsigned short data[3];
369 + struct ts_sample ts_data;
370 + int iReturn = -EFAULT;
371 +
372 + if (!bFreshTouchData)
373 + {
374 + iReturn = 0;
375 + }
376 + else if (count == sizeof(data))
377 + {
378 + spin_lock_irq(&event_buffer_lock);
379 + bFreshTouchData = 0;
380 + data[0] = gSample.currentX;
381 + data[1] = gSample.currentY;
382 + data[2] = gSample.currentButton;
383 +
384 + spin_unlock_irq(&event_buffer_lock);
385 +
386 + if (copy_to_user(buf, data, sizeof data))
387 + return -EFAULT;
388 +
389 + count -= sizeof(data);
390 +
391 + /* return the # of bytes that got read */
392 + iReturn = sizeof(data) ;
393 + }
394 + else if (count == sizeof(struct ts_sample) )
395 + {
396 + spin_lock_irq(&event_buffer_lock);
397 + bFreshTouchData = 0;
398 + ts_data.x = gSample.currentX;
399 + ts_data.y = gSample.currentY;
400 + ts_data.pressure = gSample.currentPressure;
401 + ts_data.tv = gSample.currentTime;
402 + spin_unlock_irq(&event_buffer_lock);
403 +
404 + if (copy_to_user(buf, &ts_data, sizeof(struct ts_sample)))
405 + {
406 + iReturn = -EFAULT;
407 + }
408 + else
409 + {
410 + count -= sizeof(ts_data);
411 + iReturn = sizeof(ts_data);
412 + }
413 + }
414 +
415 + return iReturn;
416 +}
417 +
418 +static unsigned int ep93xx_ts_poll(struct file *filp, poll_table *wait)
419 +{
420 + poll_wait(filp, &queue, wait);
421 +
422 + if (bFreshTouchData)
423 + {
424 + return POLLIN | POLLRDNORM;
425 + }
426 +
427 + return 0;
428 +}
429 +
430 +static int ep93xx_ts_open(struct inode *inode, struct file *filp)
431 +{
432 + if (down_trylock(&open_sem))
433 + {
434 + return -EBUSY;
435 + }
436 +
437 + ep93xx_ts_setup();
438 +
439 + return 0;
440 +}
441 +
442 +/*
443 + * Asynchronous I/O support.
444 + */
445 +static int ep93xx_ts_fasync(int fd, struct file *filp, int on)
446 +{
447 + int retval;
448 +
449 + retval = fasync_helper(fd, filp, on, &ep93xx_fasync);
450 + if (retval < 0)
451 + {
452 + return retval;
453 + }
454 +
455 + return 0;
456 +}
457 +
458 +static int ep93xx_ts_release(struct inode *inode, struct file *filp)
459 +{
460 + Stop_Timer2();
461 +
462 + /*
463 + * Call our async I/O support to request that this file
464 + * cease to be used for async I/O.
465 + */
466 + ep93xx_ts_fasync(-1, filp, 0);
467 +
468 + ep93xx_ts_shutdown();
469 +
470 + up(&open_sem);
471 +
472 + return 0;
473 +}
474 +
475 +static ssize_t ep93xx_ts_write(struct file *file, const char *buffer, size_t count,
476 + loff_t *ppos)
477 +{
478 + return -EINVAL;
479 +}
480 +
481 +
482 +static int ep93xx_ts_ioctl(struct inode *inode, struct file *file, uint command, ulong u)
483 +{
484 + static const int version = EV_VERSION;
485 + static const u_int32_t bit =(1 << EV_ABS);
486 + static const u_int32_t absbit = (1 << ABS_X) | (1 << ABS_Y) | (1 << ABS_PRESSURE);
487 + int iReturn ;
488 + int i = 0;
489 +
490 + switch(command)
491 + {
492 + case EVIOCGVERSION:
493 + i = copy_to_user((void __user *)u, (void *)version, sizeof(version));
494 + iReturn = i ? -EFAULT : 0;
495 + break;
496 +
497 + case EVIOCGBIT(0,sizeof(u_int32_t) * 8) :
498 + i = copy_to_user((void __user *)u, (void *)bit, sizeof(bit));
499 + iReturn = i ? -EFAULT : 0;
500 + break;
501 +
502 + case EVIOCGBIT(EV_ABS, sizeof(absbit) * 8):
503 + i = copy_to_user((void __user *)u, (void *)absbit, sizeof(absbit));
504 + iReturn = i ? -EFAULT : 0;
505 + break;
506 + default:
507 + iReturn = -1;
508 + break;
509 + }
510 +
511 + return iReturn;
512 +}
513 +
514 +static struct file_operations ep93xx_ts_fops = {
515 + owner: THIS_MODULE,
516 + read: ep93xx_ts_read,
517 + write: ep93xx_ts_write,
518 + poll: ep93xx_ts_poll,
519 + open: ep93xx_ts_open,
520 + unlocked_ioctl: ep93xx_ts_ioctl,
521 + release: ep93xx_ts_release,
522 + fasync: ep93xx_ts_fasync,
523 +};
524 +
525 +static struct miscdevice ep93xx_ts_miscdev =
526 +{
527 + EP93XX_TS_MINOR,
528 + "ep93xx_ts",
529 + &ep93xx_ts_fops
530 +};
531 +
532 +void ep93xx_ts_setup(void)
533 +{
534 + unsigned int uiKTDIV, uiTSXYMaxMin;
535 +
536 + /*
537 + * Set the TSEN bit in KTDIV so that we are enabling the clock
538 + * for the touchscreen.
539 + */
540 + uiKTDIV = __raw_readl(EP93XX_SYSCON_KEYTCHCLKDIV);
541 + uiKTDIV |= EP93XX_SYSCON_KEYTCHCLKDIV_TSEN;
542 + ep93xx_syscon_swlocked_write(uiKTDIV, EP93XX_SYSCON_KEYTCHCLKDIV);
543 +
544 + /*
545 + * Program the EP93XX_TOUCHSCREEN_SETUP and TSSetup2 registers.
546 + */
547 + __raw_writel(TSSETUP_DEFAULT, EP93XX_TOUCHSCREEN_SETUP);
548 + __raw_writel(TSSETUP2_DEFAULT, EP93XX_TOUCHSCREEN_SETUP2);
549 +
550 + /*
551 + * Set the the touch settings.
552 + */
553 + __raw_writel(0xaa, EP93XX_TOUCHSCREEN_SWLOCK);
554 + __raw_writel(sSwitchSettings.uiDischarge, EP93XX_TOUCHSCREEN_DIRECT);
555 +
556 + __raw_writel(0xaa, EP93XX_TOUCHSCREEN_SWLOCK);
557 + __raw_writel(sSwitchSettings.uiDischarge, EP93XX_TOUCHSCREEN_DISCHARGE);
558 +
559 + __raw_writel(0xaa, EP93XX_TOUCHSCREEN_SWLOCK);
560 + __raw_writel(sSwitchSettings.uiSwitchZ1, EP93XX_TOUCHSCREEN_XSAMPLE);
561 +
562 + __raw_writel(0xaa, EP93XX_TOUCHSCREEN_SWLOCK);
563 + __raw_writel(sSwitchSettings.uiSwitchZ2, EP93XX_TOUCHSCREEN_YSAMPLE);
564 +
565 + __raw_writel(0xaa, EP93XX_TOUCHSCREEN_SWLOCK);
566 + __raw_writel(sSwitchSettings.uiDetect, EP93XX_TOUCHSCREEN_DETECT);
567 +
568 + /*
569 + * X,YMin set to 0x40 = have to drag that many pixels for a new irq.
570 + * X,YMax set to 0x40 = 1024 pixels is the maximum movement within the
571 + * time scan limit.
572 + */
573 + uiTSXYMaxMin = (50 << TSMAXMIN_XMIN_SHIFT) & TSMAXMIN_XMIN_MASK;
574 + uiTSXYMaxMin |= (50 << TSMAXMIN_YMIN_SHIFT) & TSMAXMIN_YMIN_MASK;
575 + uiTSXYMaxMin |= (0xff << TSMAXMIN_XMAX_SHIFT) & TSMAXMIN_XMAX_MASK;
576 + uiTSXYMaxMin |= (0xff << TSMAXMIN_YMAX_SHIFT) & TSMAXMIN_YMAX_MASK;
577 + __raw_writel(uiTSXYMaxMin, EP93XX_TOUCHSCREEN_XYMAXMIN);
578 +
579 + bCurrentPenDown = 0;
580 + bFreshTouchData = 0;
581 + guiLastX = 0;
582 + guiLastY = 0;
583 + guiLastInvPressure = 0xffffff;
584 +
585 + /*
586 + * Enable the touch screen scanning engine.
587 + */
588 + TS_Hardware_Scan_Mode();
589 +}
590 +
591 +/*
592 + * ep93xx_ts_shutdown
593 + *
594 + */
595 +static void
596 +ep93xx_ts_shutdown(void)
597 +{
598 + unsigned int uiKTDIV;
599 +
600 + sTouch.state = TS_STATE_STOPPED;
601 + Stop_Timer2();
602 +
603 + /*
604 + * Disable the scanning engine.
605 + */
606 + __raw_writel(0, EP93XX_TOUCHSCREEN_SETUP);
607 + __raw_writel(0, EP93XX_TOUCHSCREEN_SETUP2);
608 +
609 + /*
610 + * Clear the TSEN bit in KTDIV so that we are disabling the clock
611 + * for the touchscreen.
612 + */
613 + uiKTDIV = __raw_readl(EP93XX_SYSCON_KEYTCHCLKDIV);
614 + uiKTDIV &= ~EP93XX_SYSCON_KEYTCHCLKDIV_TSEN;
615 + ep93xx_syscon_swlocked_write(uiKTDIV, EP93XX_SYSCON_KEYTCHCLKDIV);
616 +
617 +} /* ep93xx_ts_shutdown */
618 +
619 +static irqreturn_t ep93xx_timer2_isr(int irq, void *dev_id)
620 +{
621 + switch(sTouch.state)
622 + {
623 + case TS_STATE_STOPPED:
624 + TS_Hardware_Scan_Mode();
625 + break;
626 +
627 + /*
628 + * Get the Z1 value for pressure measurement and set up
629 + * the switch register for getting the Z2 measurement.
630 + */
631 + case TS_STATE_Z1:
632 + Set_Timer2_uSec(EP93XX_TS_ADC_DELAY_USEC);
633 + sTouch.uiZ1 = ADCGetData(2, 200);
634 + ep93xx_ts_set_direct(sSwitchSettings.uiSwitchZ2);
635 + sTouch.state = TS_STATE_Z2;
636 + break;
637 +
638 + /*
639 + * Get the Z2 value for pressure measurement and set up
640 + * the switch register for getting the Y measurement.
641 + */
642 + case TS_STATE_Z2:
643 + sTouch.uiZ2 = ADCGetData(2, 200);
644 + ep93xx_ts_set_direct(sSwitchSettings.uiYSample);
645 + sTouch.state = TS_STATE_Y;
646 + break;
647 +
648 + /*
649 + * Get the Y value and set up the switch register for
650 + * getting the X measurement.
651 + */
652 + case TS_STATE_Y:
653 + sTouch.uiY = ADCGetData(4, 20);
654 + ep93xx_ts_set_direct(sSwitchSettings.uiXSample);
655 + sTouch.state = TS_STATE_X;
656 + break;
657 +
658 + /*
659 + * Read the X value. This is the last of the 4 adc values
660 + * we need so we continue on to process the data.
661 + */
662 + case TS_STATE_X:
663 + Stop_Timer2();
664 +
665 + sTouch.uiX = ADCGetData(4, 20);
666 +
667 + __raw_writel(0xaa, EP93XX_TOUCHSCREEN_SWLOCK);
668 + __raw_writel(sSwitchSettings.uiDischarge, EP93XX_TOUCHSCREEN_DIRECT);
669 +
670 + sTouch.state = TS_STATE_DONE;
671 +
672 + /*
673 + * Process this set of ADC readings.
674 + */
675 + ProcessPointData();
676 +
677 + break;
678 +
679 + /*
680 + * Shouldn't get here. But if we do, we can recover...
681 + */
682 + case TS_STATE_DONE:
683 + TS_Hardware_Scan_Mode();
684 + break;
685 + }
686 +
687 + /*
688 + * Clear the timer2 interrupt.
689 + */
690 + __raw_writel(1, EP93XX_TIMER2_CLEAR);
691 + return(IRQ_HANDLED);
692 +}
693 +
694 +/*---------------------------------------------------------------------
695 + * ProcessPointData
696 + *
697 + * This routine processes the ADC data into usable point data and then
698 + * puts the driver into hw or sw scanning mode before returning.
699 + *
700 + * We calculate inverse pressure (lower number = more pressure) then
701 + * do a hystheresis with the two pressure values 'light' and 'heavy'.
702 + *
703 + * If we are above the light, we have pen up.
704 + * If we are below the heavy we have pen down.
705 + * As long as the pressure stays below the light, pen stays down.
706 + * When we get above the light again, pen goes back up.
707 + *
708 + */
709 +static void ProcessPointData(void)
710 +{
711 + int bValidPoint = 0;
712 + unsigned int uiXDiff, uiYDiff, uiInvPressureDiff;
713 + unsigned int uiInvPressure;
714 +
715 + /*
716 + * Calculate the current pressure.
717 + */
718 + uiInvPressure = CalculateInvPressure();
719 +
720 + /*
721 + * If pen pressure is so light that it is greater than the 'max' setting
722 + * then we consider this to be a pen up.
723 + */
724 + if (uiInvPressure >= TS_LIGHT_INV_PRESSURE)
725 + {
726 + bCurrentPenDown = 0;
727 + ee93xx_ts_evt_add(0, guiLastX, guiLastY, 0);
728 + TS_Hardware_Scan_Mode();
729 + return;
730 + }
731 +
732 + /*
733 + * Hysteresis:
734 + * If the pen pressure is hard enough to be less than the 'min' OR
735 + * the pen is already down and is still less than the 'max'...
736 + */
737 + if ((uiInvPressure < TS_HEAVY_INV_PRESSURE) ||
738 + (bCurrentPenDown && (uiInvPressure < TS_LIGHT_INV_PRESSURE)))
739 + {
740 + if (bCurrentPenDown)
741 + {
742 + /*
743 + * If pen was previously down, check the difference between
744 + * the last sample and this one... if the difference between
745 + * samples is too great, ignore the sample.
746 + */
747 + uiXDiff = abs(guiLastX - sTouch.uiX);
748 + uiYDiff = abs(guiLastY - sTouch.uiY);
749 + uiInvPressureDiff = abs(guiLastInvPressure - uiInvPressure);
750 +
751 + if (uiXDiff < TS_MAX_VALID_XY_CHANGE
752 + && uiYDiff < TS_MAX_VALID_XY_CHANGE
753 + && uiInvPressureDiff < TS_MAX_VALID_PRESSURE_CHANGE)
754 + {
755 + bValidPoint = 1;
756 + }
757 + }
758 + else
759 + {
760 + bValidPoint = 1;
761 + }
762 +
763 + /*
764 + * If either the pen was put down or dragged make a note of it.
765 + */
766 + if (bValidPoint)
767 + {
768 + guiLastX = sTouch.uiX;
769 + guiLastY = sTouch.uiY;
770 + guiLastInvPressure = uiInvPressure;
771 + bCurrentPenDown = 1;
772 + ee93xx_ts_evt_add(1, sTouch.uiX, sTouch.uiY,
773 + 0x7000000 / uiInvPressure);
774 + }
775 +
776 + TS_Soft_Scan_Mode();
777 + return;
778 + }
779 +
780 + TS_Hardware_Scan_Mode();
781 +}
782 +
783 +static void ep93xx_ts_set_direct(unsigned int uiADCSwitch)
784 +{
785 + unsigned int uiResult;
786 +
787 + /*
788 + * Set the switch settings in the direct register.
789 + */
790 + __raw_writel(0xaa, EP93XX_TOUCHSCREEN_SWLOCK);
791 + __raw_writel(uiADCSwitch, EP93XX_TOUCHSCREEN_DIRECT);
792 +
793 + /*
794 + * Read and throw away the first sample.
795 + */
796 + do {
797 + uiResult = __raw_readl(EP93XX_TOUCHSCREEN_XYRESULT);
798 + } while (!(uiResult & TSXYRESULT_SDR));
799 +
800 +}
801 +
802 +static unsigned int ADCGetData(unsigned int uiSamples, unsigned int uiMaxDiff)
803 +{
804 + unsigned int uiResult, uiValue, uiCount, uiLowest, uiHighest, uiSum, uiAve;
805 +
806 + do
807 + {
808 + /*
809 + * Initialize our values.
810 + */
811 + uiLowest = 0xfffffff;
812 + uiHighest = 0;
813 + uiSum = 0;
814 +
815 + for (uiCount = 0; uiCount < uiSamples; uiCount++)
816 + {
817 + /*
818 + * Read the touch screen four more times and average.
819 + */
820 + do {
821 + uiResult = __raw_readl(EP93XX_TOUCHSCREEN_XYRESULT);
822 + } while (!(uiResult & TSXYRESULT_SDR));
823 +
824 + uiValue = (uiResult & TSXYRESULT_AD_MASK) >> TSXYRESULT_AD_SHIFT;
825 + uiValue = ((uiValue >> 4) + ((1 + TSXYRESULT_X_MASK)>>1)) & TSXYRESULT_X_MASK;
826 +
827 + /*
828 + * Add up the values.
829 + */
830 + uiSum += uiValue;
831 +
832 + /*
833 + * Get the lowest and highest values.
834 + */
835 + if (uiValue < uiLowest)
836 + {
837 + uiLowest = uiValue;
838 + }
839 + if (uiValue > uiHighest)
840 + {
841 + uiHighest = uiValue;
842 + }
843 + }
844 + } while ((uiHighest - uiLowest) > uiMaxDiff);
845 +
846 + /*
847 + * Calculate the Average value.
848 + */
849 + uiAve = uiSum / uiSamples;
850 +
851 + return uiAve;
852 +}
853 +
854 +/*
855 + * CalculateInvPressure
856 + *
857 + * Is the Touch Valid. Touch is not valid if the X or Y value is not
858 + * in range and the pressure is not enough.
859 + *
860 + * Touch resistance can be measured by the following formula:
861 + *
862 + * Rx * X * Z2
863 + * Rtouch = --------- * (-- - 1)
864 + * 4096 Z1
865 + *
866 + * This is simplified in the ration of Rtouch to Rx. The lower the value, the
867 + * higher the pressure.
868 + *
869 + * Z2
870 + * InvPressure = X * (-- - 1)
871 + * Z1
872 + */
873 +static unsigned int CalculateInvPressure(void)
874 +{
875 + unsigned int uiInvPressure;
876 +
877 + /*
878 + * Check to see if the point is valid.
879 + */
880 + if (sTouch.uiZ1 < MIN_Z1_VALUE)
881 + {
882 + uiInvPressure = 0x10000;
883 + }
884 +
885 + /*
886 + * Can omit the pressure calculation if you need to get rid of the division.
887 + */
888 + else
889 + {
890 + uiInvPressure = ((sTouch.uiX * sTouch.uiZ2) / sTouch.uiZ1) - sTouch.uiX;
891 + }
892 +
893 + return uiInvPressure;
894 +}
895 +
896 +/*
897 + * TS_Hardware_Scan_Mode
898 + *
899 + * Enables the ep93xx ts scanning engine so that when the pen goes down
900 + * we will get an interrupt.
901 + */
902 +static void TS_Hardware_Scan_Mode(void)
903 +{
904 + unsigned int uiDevCfg;
905 +
906 + /*
907 + * Disable the soft scanning engine.
908 + */
909 + sTouch.state = TS_STATE_STOPPED;
910 + Stop_Timer2();
911 +
912 + /*
913 + * Clear the TIN (Touchscreen INactive) bit so we can go to
914 + * automatic scanning mode.
915 + */
916 + uiDevCfg = __raw_readl(EP93XX_SYSCON_DEVCFG);
917 + ep93xx_syscon_swlocked_write(uiDevCfg & ~EP93XX_SYSCON_DEVCFG_TIN,
918 + EP93XX_SYSCON_DEVCFG);
919 +
920 + /*
921 + * Enable the touch screen scanning state machine by setting
922 + * the ENABLE bit.
923 + */
924 + __raw_writel(TSSETUP_DEFAULT | TSSETUP_ENABLE, EP93XX_TOUCHSCREEN_SETUP);
925 +
926 + /*
927 + * Set the flag to show that we are in interrupt mode.
928 + */
929 + gScanningMode = TS_MODE_HARDWARE_SCAN;
930 +
931 + /*
932 + * Initialize EP93XX_TOUCHSCREEN_SETUP2 register.
933 + */
934 + __raw_writel(TSSETUP2_DEFAULT, EP93XX_TOUCHSCREEN_SETUP2);
935 +
936 +}
937 +
938 +/*
939 + * TS_Soft_Scan_Mode
940 + *
941 + * Sets the touch screen to manual polling mode.
942 + */
943 +static void TS_Soft_Scan_Mode(void)
944 +{
945 + unsigned int uiDevCfg;
946 +
947 + if (gScanningMode != TS_MODE_SOFT_SCAN)
948 + {
949 + /*
950 + * Disable the touch screen scanning state machine by clearing
951 + * the ENABLE bit.
952 + */
953 + __raw_writel(TSSETUP_DEFAULT, EP93XX_TOUCHSCREEN_SETUP);
954 +
955 + /*
956 + * Set the TIN bit so we can do manual touchscreen polling.
957 + */
958 + uiDevCfg = __raw_readl(EP93XX_SYSCON_DEVCFG);
959 + ep93xx_syscon_swlocked_write(uiDevCfg | EP93XX_SYSCON_DEVCFG_TIN,
960 + EP93XX_SYSCON_DEVCFG);
961 + }
962 +
963 + /*
964 + * Set the switch register up for the first ADC reading
965 + */
966 + ep93xx_ts_set_direct(sSwitchSettings.uiSwitchZ1);
967 +
968 + /*
969 + * Initialize our software state machine to know which ADC
970 + * reading to take
971 + */
972 + sTouch.state = TS_STATE_Z1;
973 +
974 + /*
975 + * Set the timer so after a mSec or two settling delay it will
976 + * take the first ADC reading.
977 + */
978 + Set_Timer2_uSec(EP93XX_TS_PER_POINT_DELAY_USEC);
979 +
980 + /*
981 + * Note that we are in sw scanning mode not hw scanning mode.
982 + */
983 + gScanningMode = TS_MODE_SOFT_SCAN;
984 +
985 +}
986 +
987 +static void Set_Timer2_uSec(unsigned int uiDelay_uSec)
988 +{
989 + unsigned int uiClockTicks;
990 +
991 + /*
992 + * Stop timer 2
993 + */
994 + __raw_writel(0, EP93XX_TIMER2_CONTROL);
995 +
996 + uiClockTicks = ((uiDelay_uSec * 508) + 999) / 1000;
997 + __raw_writel(uiClockTicks, EP93XX_TIMER2_LOAD);
998 + __raw_writel(uiClockTicks, EP93XX_TIMER2_VALUE);
999 +
1000 + /*
1001 + * Set up Timer 2 for 508 kHz clock and periodic mode.
1002 + */
1003 + __raw_writel(0xC8, EP93XX_TIMER2_CONTROL);
1004 +
1005 +}
1006 +
1007 +static void Stop_Timer2(void)
1008 +{
1009 + __raw_writel(0, EP93XX_TIMER2_CONTROL);
1010 +}
1011 +
1012 +/*
1013 + * Initialization and exit routines
1014 + */
1015 +int __init ep93xx_ts_init(void)
1016 +{
1017 + int retval;
1018 +
1019 + retval = request_irq(IRQ_EP93XX_TOUCH, ep93xx_ts_isr,
1020 + IRQF_DISABLED, "ep93xx_ts", 0);
1021 + if (retval)
1022 + {
1023 + printk(KERN_WARNING "ep93xx_ts: failed to get touchscreen IRQ\n");
1024 + return retval;
1025 + }
1026 +
1027 + retval = request_irq(IRQ_EP93XX_TIMER2, ep93xx_timer2_isr,
1028 + IRQF_DISABLED, "ep93xx_timer2", 0);
1029 + if (retval)
1030 + {
1031 + printk(KERN_WARNING "ep93xx_ts: failed to get timer2 IRQ\n");
1032 + free_irq(IRQ_EP93XX_TOUCH, 0);
1033 + return retval;
1034 + }
1035 +
1036 + misc_register(&ep93xx_ts_miscdev);
1037 +
1038 + sTouch.state = TS_STATE_STOPPED;
1039 + gScanningMode = TS_MODE_UN_INITIALIZED;
1040 +
1041 + printk(KERN_NOTICE "ep93xx touchscreen driver configured for 4-wire operation\n");
1042 +
1043 + return 0;
1044 +}
1045 +
1046 +void __exit ep93xx_ts_exit(void)
1047 +{
1048 + Stop_Timer2();
1049 +
1050 + free_irq(IRQ_EP93XX_TOUCH, 0);
1051 + free_irq(IRQ_EP93XX_TIMER2, 0);
1052 +
1053 + misc_deregister(&ep93xx_ts_miscdev);
1054 +}
1055 +
1056 +module_init(ep93xx_ts_init);
1057 +module_exit(ep93xx_ts_exit);
1058 +
1059 +MODULE_DESCRIPTION("Cirrus EP93xx touchscreen driver");
1060 +MODULE_SUPPORTED_DEVICE("touchscreen/ep93xx");
1061 +MODULE_LICENSE("GPL");