blob: 0786010d7ed02aeaad3b2b20f38431272d56818f [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
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017 * data in a continuous manner on the Intel XScale architecture
Mark Brown4db8a5f2008-04-02 00:51:46 -040018 *
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>
Mark Brown4db8a5f2008-04-02 00:51:46 -040028#include <linux/delay.h>
29#include <linux/irq.h>
30#include <linux/interrupt.h>
31#include <linux/wm97xx.h>
32#include <linux/io.h>
Marek Vasutb8333062009-07-20 22:26:37 -070033#include <linux/gpio.h>
34
Eric Miao1f017a92008-11-28 14:19:33 +080035#include <mach/regs-ac97.h>
Mark Brown4db8a5f2008-04-02 00:51:46 -040036
Marek Vasutb8333062009-07-20 22:26:37 -070037#include <asm/mach-types.h>
Mark Brown4db8a5f2008-04-02 00:51:46 -040038
39struct 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
48static 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 */
64static int sp_idx;
65static u16 last, tries;
Marek Vasutb8333062009-07-20 22:26:37 -070066static int irq;
Mark Brown4db8a5f2008-04-02 00:51:46 -040067
68/*
69 * Pen sampling frequency (Hz) in continuous mode.
70 */
71static int cont_rate = 200;
72module_param(cont_rate, int, 0);
73MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");
74
75/*
76 * Pen down detection.
77 *
78 * This driver can either poll or use an interrupt to indicate a pen down
79 * event. If the irq request fails then it will fall back to polling mode.
80 */
81static int pen_int;
82module_param(pen_int, int, 0);
83MODULE_PARM_DESC(pen_int, "Pen down detection (1 = interrupt, 0 = polling)");
84
85/*
86 * Pressure readback.
87 *
88 * Set to 1 to read back pen down pressure
89 */
90static int pressure;
91module_param(pressure, int, 0);
92MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");
93
94/*
95 * AC97 touch data slot.
96 *
97 * Touch screen readback data ac97 slot
98 */
99static int ac97_touch_slot = 5;
100module_param(ac97_touch_slot, int, 0);
101MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");
102
103
104/* flush AC97 slot 5 FIFO on pxa machines */
105#ifdef CONFIG_PXA27x
106static void wm97xx_acc_pen_up(struct wm97xx *wm)
107{
108 schedule_timeout_uninterruptible(1);
109
110 while (MISR & (1 << 2))
111 MODR;
112}
113#else
114static void wm97xx_acc_pen_up(struct wm97xx *wm)
115{
Jiri Slaby180deb52009-04-15 09:03:07 -0700116 unsigned int count;
117
Mark Brown4db8a5f2008-04-02 00:51:46 -0400118 schedule_timeout_uninterruptible(1);
119
Jiri Slaby180deb52009-04-15 09:03:07 -0700120 for (count = 0; count < 16; count++)
Mark Brown4db8a5f2008-04-02 00:51:46 -0400121 MODR;
Mark Brown4db8a5f2008-04-02 00:51:46 -0400122}
123#endif
124
125static 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
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200133 * threshold set to use the notifications it gives.
Mark Brown4db8a5f2008-04-02 00:51:46 -0400134 */
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
Mark Browndcfc32b2010-01-28 22:37:39 -0800155 dev_dbg(wm->dev, "Raw coordinates: x=%x, y=%x, p=%x\n",
156 x, y, p);
157
Mark Brown4db8a5f2008-04-02 00:51:46 -0400158 /* are samples valid */
Wolfram Sang24566892011-07-04 19:22:00 -0700159 if ((x & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_X ||
160 (y & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_Y ||
161 (p & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_PRES)
Mark Brown4db8a5f2008-04-02 00:51:46 -0400162 goto up;
163
164 /* coordinate is good */
165 tries = 0;
166 input_report_abs(wm->input_dev, ABS_X, x & 0xfff);
167 input_report_abs(wm->input_dev, ABS_Y, y & 0xfff);
168 input_report_abs(wm->input_dev, ABS_PRESSURE, p & 0xfff);
Mike Rapoportcd2d64b2009-03-04 01:12:49 -0800169 input_report_key(wm->input_dev, BTN_TOUCH, (p != 0));
Mark Brown4db8a5f2008-04-02 00:51:46 -0400170 input_sync(wm->input_dev);
171 reads++;
172 } while (reads < cinfo[sp_idx].reads);
173up:
174 return RC_PENDOWN | RC_AGAIN;
175}
176
177static int wm97xx_acc_startup(struct wm97xx *wm)
178{
Marek Vasutb8333062009-07-20 22:26:37 -0700179 int idx = 0, ret = 0;
Mark Brown4db8a5f2008-04-02 00:51:46 -0400180
181 /* check we have a codec */
182 if (wm->ac97 == NULL)
183 return -ENODEV;
184
185 /* Go you big red fire engine */
186 for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
187 if (wm->id != cinfo[idx].id)
188 continue;
189 sp_idx = idx;
190 if (cont_rate <= cinfo[idx].speed)
191 break;
192 }
193 wm->acc_rate = cinfo[sp_idx].code;
194 wm->acc_slot = ac97_touch_slot;
195 dev_info(wm->dev,
196 "mainstone accelerated touchscreen driver, %d samples/sec\n",
197 cinfo[sp_idx].speed);
198
Marek Vasutb8333062009-07-20 22:26:37 -0700199 /* IRQ driven touchscreen is used on Palm hardware */
200 if (machine_is_palmt5() || machine_is_palmtx() || machine_is_palmld()) {
201 pen_int = 1;
202 irq = 27;
Marek Vasut99fde512009-07-20 22:28:50 -0700203 /* There is some obscure mutant of WM9712 interbred with WM9713
204 * used on Palm HW */
205 wm->variant = WM97xx_WM1613;
Marek Vasutb8333062009-07-20 22:26:37 -0700206 } else if (machine_is_mainstone() && pen_int)
207 irq = 4;
208
209 if (irq) {
210 ret = gpio_request(irq, "Touchscreen IRQ");
211 if (ret)
212 goto out;
213
214 ret = gpio_direction_input(irq);
215 if (ret) {
216 gpio_free(irq);
217 goto out;
218 }
219
220 wm->pen_irq = gpio_to_irq(irq);
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200221 irq_set_irq_type(wm->pen_irq, IRQ_TYPE_EDGE_BOTH);
Marek Vasutb8333062009-07-20 22:26:37 -0700222 } else /* pen irq not supported */
223 pen_int = 0;
224
Mark Brown4db8a5f2008-04-02 00:51:46 -0400225 /* codec specific irq config */
226 if (pen_int) {
227 switch (wm->id) {
228 case WM9705_ID2:
Mark Brown4db8a5f2008-04-02 00:51:46 -0400229 break;
230 case WM9712_ID2:
231 case WM9713_ID2:
Mark Brown4db8a5f2008-04-02 00:51:46 -0400232 /* use PEN_DOWN GPIO 13 to assert IRQ on GPIO line 2 */
Mark Brown4db8a5f2008-04-02 00:51:46 -0400233 wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
234 WM97XX_GPIO_POL_HIGH,
235 WM97XX_GPIO_STICKY,
236 WM97XX_GPIO_WAKE);
237 wm97xx_config_gpio(wm, WM97XX_GPIO_2, WM97XX_GPIO_OUT,
238 WM97XX_GPIO_POL_HIGH,
239 WM97XX_GPIO_NOTSTICKY,
240 WM97XX_GPIO_NOWAKE);
241 break;
242 default:
243 dev_err(wm->dev,
244 "pen down irq not supported on this device\n");
245 pen_int = 0;
246 break;
247 }
248 }
249
Marek Vasutb8333062009-07-20 22:26:37 -0700250out:
251 return ret;
Mark Brown4db8a5f2008-04-02 00:51:46 -0400252}
253
254static void wm97xx_acc_shutdown(struct wm97xx *wm)
255{
256 /* codec specific deconfig */
257 if (pen_int) {
Marek Vasutb8333062009-07-20 22:26:37 -0700258 if (irq)
259 gpio_free(irq);
260 wm->pen_irq = 0;
Mark Brown4db8a5f2008-04-02 00:51:46 -0400261 }
262}
263
264static void wm97xx_irq_enable(struct wm97xx *wm, int enable)
265{
266 if (enable)
267 enable_irq(wm->pen_irq);
268 else
Mark Browna700e722009-03-04 01:12:49 -0800269 disable_irq_nosync(wm->pen_irq);
Mark Brown4db8a5f2008-04-02 00:51:46 -0400270}
271
272static struct wm97xx_mach_ops mainstone_mach_ops = {
273 .acc_enabled = 1,
274 .acc_pen_up = wm97xx_acc_pen_up,
275 .acc_pen_down = wm97xx_acc_pen_down,
276 .acc_startup = wm97xx_acc_startup,
277 .acc_shutdown = wm97xx_acc_shutdown,
278 .irq_enable = wm97xx_irq_enable,
279 .irq_gpio = WM97XX_GPIO_2,
280};
281
282static int mainstone_wm97xx_probe(struct platform_device *pdev)
283{
284 struct wm97xx *wm = platform_get_drvdata(pdev);
285
286 return wm97xx_register_mach_ops(wm, &mainstone_mach_ops);
287}
288
289static int mainstone_wm97xx_remove(struct platform_device *pdev)
290{
291 struct wm97xx *wm = platform_get_drvdata(pdev);
292
293 wm97xx_unregister_mach_ops(wm);
294 return 0;
295}
296
297static struct platform_driver mainstone_wm97xx_driver = {
298 .probe = mainstone_wm97xx_probe,
299 .remove = mainstone_wm97xx_remove,
300 .driver = {
301 .name = "wm97xx-touch",
302 },
303};
JJ Dingcdcc96e2011-11-29 11:14:13 -0800304module_platform_driver(mainstone_wm97xx_driver);
Mark Brown4db8a5f2008-04-02 00:51:46 -0400305
306/* Module information */
Liam Girdwoodb8d055a2008-10-13 23:00:15 -0400307MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
Mark Brown4db8a5f2008-04-02 00:51:46 -0400308MODULE_DESCRIPTION("wm97xx continuous touch driver for mainstone");
309MODULE_LICENSE("GPL");