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