Merge commit 'v2.6.27-rc7' into x86/pebs
[linux-2.6] / drivers / input / touchscreen / mainstone-wm97xx.c
1 /*
2  * mainstone-wm97xx.c  --  Mainstone Continuous Touch screen driver for
3  *                         Wolfson WM97xx AC97 Codecs.
4  *
5  * Copyright 2004, 2007 Wolfson Microelectronics PLC.
6  * Author: Liam Girdwood
7  *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8  * Parts Copyright : Ian Molton <spyro@f2s.com>
9  *                   Andrew Zabolotny <zap@homelink.ru>
10  *
11  *  This program is free software; you can redistribute  it and/or modify it
12  *  under  the terms of  the GNU General  Public License as published by the
13  *  Free Software Foundation;  either version 2 of the  License, or (at your
14  *  option) any later version.
15  *
16  * Notes:
17  *     This is a wm97xx extended touch driver to capture touch
18  *     data in a continuous manner on the Intel XScale archictecture
19  *
20  *  Features:
21  *       - codecs supported:- WM9705, WM9712, WM9713
22  *       - processors supported:- Intel XScale PXA25x, PXA26x, PXA27x
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/irq.h>
32 #include <linux/interrupt.h>
33 #include <linux/wm97xx.h>
34 #include <linux/io.h>
35 #include <mach/pxa-regs.h>
36
37 #define VERSION         "0.13"
38
39 struct continuous {
40         u16 id;    /* codec id */
41         u8 code;   /* continuous code */
42         u8 reads;  /* number of coord reads per read cycle */
43         u32 speed; /* number of coords per second */
44 };
45
46 #define WM_READS(sp) ((sp / HZ) + 1)
47
48 static const struct continuous cinfo[] = {
49         {WM9705_ID2, 0, WM_READS(94), 94},
50         {WM9705_ID2, 1, WM_READS(188), 188},
51         {WM9705_ID2, 2, WM_READS(375), 375},
52         {WM9705_ID2, 3, WM_READS(750), 750},
53         {WM9712_ID2, 0, WM_READS(94), 94},
54         {WM9712_ID2, 1, WM_READS(188), 188},
55         {WM9712_ID2, 2, WM_READS(375), 375},
56         {WM9712_ID2, 3, WM_READS(750), 750},
57         {WM9713_ID2, 0, WM_READS(94), 94},
58         {WM9713_ID2, 1, WM_READS(120), 120},
59         {WM9713_ID2, 2, WM_READS(154), 154},
60         {WM9713_ID2, 3, WM_READS(188), 188},
61 };
62
63 /* continuous speed index */
64 static int sp_idx;
65 static u16 last, tries;
66
67 /*
68  * Pen sampling frequency (Hz) in continuous mode.
69  */
70 static int cont_rate = 200;
71 module_param(cont_rate, int, 0);
72 MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");
73
74 /*
75  * Pen down detection.
76  *
77  * This driver can either poll or use an interrupt to indicate a pen down
78  * event. If the irq request fails then it will fall back to polling mode.
79  */
80 static int pen_int;
81 module_param(pen_int, int, 0);
82 MODULE_PARM_DESC(pen_int, "Pen down detection (1 = interrupt, 0 = polling)");
83
84 /*
85  * Pressure readback.
86  *
87  * Set to 1 to read back pen down pressure
88  */
89 static int pressure;
90 module_param(pressure, int, 0);
91 MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");
92
93 /*
94  * AC97 touch data slot.
95  *
96  * Touch screen readback data ac97 slot
97  */
98 static int ac97_touch_slot = 5;
99 module_param(ac97_touch_slot, int, 0);
100 MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");
101
102
103 /* flush AC97 slot 5 FIFO on pxa machines */
104 #ifdef CONFIG_PXA27x
105 static void wm97xx_acc_pen_up(struct wm97xx *wm)
106 {
107         schedule_timeout_uninterruptible(1);
108
109         while (MISR & (1 << 2))
110                 MODR;
111 }
112 #else
113 static void wm97xx_acc_pen_up(struct wm97xx *wm)
114 {
115         int count = 16;
116         schedule_timeout_uninterruptible(1);
117
118         while (count < 16) {
119                 MODR;
120                 count--;
121         }
122 }
123 #endif
124
125 static int wm97xx_acc_pen_down(struct wm97xx *wm)
126 {
127         u16 x, y, p = 0x100 | WM97XX_ADCSEL_PRES;
128         int reads = 0;
129
130         /* When the AC97 queue has been drained we need to allow time
131          * to buffer up samples otherwise we end up spinning polling
132          * for samples.  The controller can't have a suitably low
133          * threashold set to use the notifications it gives.
134          */
135         schedule_timeout_uninterruptible(1);
136
137         if (tries > 5) {
138                 tries = 0;
139                 return RC_PENUP;
140         }
141
142         x = MODR;
143         if (x == last) {
144                 tries++;
145                 return RC_AGAIN;
146         }
147         last = x;
148         do {
149                 if (reads)
150                         x = MODR;
151                 y = MODR;
152                 if (pressure)
153                         p = MODR;
154
155                 /* are samples valid */
156                 if ((x & WM97XX_ADCSRC_MASK) != WM97XX_ADCSEL_X ||
157                     (y & WM97XX_ADCSRC_MASK) != WM97XX_ADCSEL_Y ||
158                     (p & WM97XX_ADCSRC_MASK) != WM97XX_ADCSEL_PRES)
159                         goto up;
160
161                 /* coordinate is good */
162                 tries = 0;
163                 input_report_abs(wm->input_dev, ABS_X, x & 0xfff);
164                 input_report_abs(wm->input_dev, ABS_Y, y & 0xfff);
165                 input_report_abs(wm->input_dev, ABS_PRESSURE, p & 0xfff);
166                 input_sync(wm->input_dev);
167                 reads++;
168         } while (reads < cinfo[sp_idx].reads);
169 up:
170         return RC_PENDOWN | RC_AGAIN;
171 }
172
173 static int wm97xx_acc_startup(struct wm97xx *wm)
174 {
175         int idx = 0;
176
177         /* check we have a codec */
178         if (wm->ac97 == NULL)
179                 return -ENODEV;
180
181         /* Go you big red fire engine */
182         for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
183                 if (wm->id != cinfo[idx].id)
184                         continue;
185                 sp_idx = idx;
186                 if (cont_rate <= cinfo[idx].speed)
187                         break;
188         }
189         wm->acc_rate = cinfo[sp_idx].code;
190         wm->acc_slot = ac97_touch_slot;
191         dev_info(wm->dev,
192                  "mainstone accelerated touchscreen driver, %d samples/sec\n",
193                  cinfo[sp_idx].speed);
194
195         /* codec specific irq config */
196         if (pen_int) {
197                 switch (wm->id) {
198                 case WM9705_ID2:
199                         wm->pen_irq = IRQ_GPIO(4);
200                         set_irq_type(IRQ_GPIO(4), IRQ_TYPE_EDGE_BOTH);
201                         break;
202                 case WM9712_ID2:
203                 case WM9713_ID2:
204                         /* enable pen down interrupt */
205                         /* use PEN_DOWN GPIO 13 to assert IRQ on GPIO line 2 */
206                         wm->pen_irq = MAINSTONE_AC97_IRQ;
207                         wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
208                                            WM97XX_GPIO_POL_HIGH,
209                                            WM97XX_GPIO_STICKY,
210                                            WM97XX_GPIO_WAKE);
211                         wm97xx_config_gpio(wm, WM97XX_GPIO_2, WM97XX_GPIO_OUT,
212                                            WM97XX_GPIO_POL_HIGH,
213                                            WM97XX_GPIO_NOTSTICKY,
214                                            WM97XX_GPIO_NOWAKE);
215                         break;
216                 default:
217                         dev_err(wm->dev,
218                                 "pen down irq not supported on this device\n");
219                         pen_int = 0;
220                         break;
221                 }
222         }
223
224         return 0;
225 }
226
227 static void wm97xx_acc_shutdown(struct wm97xx *wm)
228 {
229         /* codec specific deconfig */
230         if (pen_int) {
231                 switch (wm->id & 0xffff) {
232                 case WM9705_ID2:
233                         wm->pen_irq = 0;
234                         break;
235                 case WM9712_ID2:
236                 case WM9713_ID2:
237                         /* disable interrupt */
238                         wm->pen_irq = 0;
239                         break;
240                 }
241         }
242 }
243
244 static void wm97xx_irq_enable(struct wm97xx *wm, int enable)
245 {
246         if (enable)
247                 enable_irq(wm->pen_irq);
248         else
249                 disable_irq(wm->pen_irq);
250 }
251
252 static struct wm97xx_mach_ops mainstone_mach_ops = {
253         .acc_enabled = 1,
254         .acc_pen_up = wm97xx_acc_pen_up,
255         .acc_pen_down = wm97xx_acc_pen_down,
256         .acc_startup = wm97xx_acc_startup,
257         .acc_shutdown = wm97xx_acc_shutdown,
258         .irq_enable = wm97xx_irq_enable,
259         .irq_gpio = WM97XX_GPIO_2,
260 };
261
262 static int mainstone_wm97xx_probe(struct platform_device *pdev)
263 {
264         struct wm97xx *wm = platform_get_drvdata(pdev);
265
266         return wm97xx_register_mach_ops(wm, &mainstone_mach_ops);
267 }
268
269 static int mainstone_wm97xx_remove(struct platform_device *pdev)
270 {
271         struct wm97xx *wm = platform_get_drvdata(pdev);
272
273         wm97xx_unregister_mach_ops(wm);
274         return 0;
275 }
276
277 static struct platform_driver mainstone_wm97xx_driver = {
278         .probe = mainstone_wm97xx_probe,
279         .remove = mainstone_wm97xx_remove,
280         .driver = {
281                 .name = "wm97xx-touch",
282         },
283 };
284
285 static int __init mainstone_wm97xx_init(void)
286 {
287         return platform_driver_register(&mainstone_wm97xx_driver);
288 }
289
290 static void __exit mainstone_wm97xx_exit(void)
291 {
292         platform_driver_unregister(&mainstone_wm97xx_driver);
293 }
294
295 module_init(mainstone_wm97xx_init);
296 module_exit(mainstone_wm97xx_exit);
297
298 /* Module information */
299 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
300 MODULE_DESCRIPTION("wm97xx continuous touch driver for mainstone");
301 MODULE_LICENSE("GPL");