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