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