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