1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * mainstone-wm97xx.c -- Mainstone Continuous Touch screen driver for
4 * Wolfson WM97xx AC97 Codecs.
6 * Copyright 2004, 2007 Wolfson Microelectronics PLC.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * Parts Copyright : Ian Molton <spyro@f2s.com>
9 * Andrew Zabolotny <zap@homelink.ru>
12 * This is a wm97xx extended touch driver to capture touch
13 * data in a continuous manner on the Intel XScale architecture
16 * - codecs supported:- WM9705, WM9712, WM9713
17 * - processors supported:- Intel XScale PXA25x, PXA26x, PXA27x
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/kernel.h>
23 #include <linux/delay.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/irq.h>
26 #include <linux/interrupt.h>
28 #include <linux/soc/pxa/cpu.h>
29 #include <linux/wm97xx.h>
31 #include <sound/pxa2xx-lib.h>
33 #include <asm/mach-types.h>
36 u16 id; /* codec id */
37 u8 code; /* continuous code */
38 u8 reads; /* number of coord reads per read cycle */
39 u32 speed; /* number of coords per second */
42 #define WM_READS(sp) ((sp / HZ) + 1)
44 static const struct continuous cinfo[] = {
45 { WM9705_ID2, 0, WM_READS(94), 94 },
46 { WM9705_ID2, 1, WM_READS(188), 188 },
47 { WM9705_ID2, 2, WM_READS(375), 375 },
48 { WM9705_ID2, 3, WM_READS(750), 750 },
49 { WM9712_ID2, 0, WM_READS(94), 94 },
50 { WM9712_ID2, 1, WM_READS(188), 188 },
51 { WM9712_ID2, 2, WM_READS(375), 375 },
52 { WM9712_ID2, 3, WM_READS(750), 750 },
53 { WM9713_ID2, 0, WM_READS(94), 94 },
54 { WM9713_ID2, 1, WM_READS(120), 120 },
55 { WM9713_ID2, 2, WM_READS(154), 154 },
56 { WM9713_ID2, 3, WM_READS(188), 188 },
59 /* continuous speed index */
61 static struct gpio_desc *gpiod_irq;
64 * Pen sampling frequency (Hz) in continuous mode.
66 static int cont_rate = 200;
67 module_param(cont_rate, int, 0);
68 MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");
73 * This driver can either poll or use an interrupt to indicate a pen down
74 * event. If the irq request fails then it will fall back to polling mode.
77 module_param(pen_int, int, 0);
78 MODULE_PARM_DESC(pen_int, "Pen down detection (1 = interrupt, 0 = polling)");
83 * Set to 1 to read back pen down pressure
86 module_param(pressure, int, 0);
87 MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");
90 * AC97 touch data slot.
92 * Touch screen readback data ac97 slot
94 static int ac97_touch_slot = 5;
95 module_param(ac97_touch_slot, int, 0);
96 MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");
99 /* flush AC97 slot 5 FIFO on pxa machines */
100 static void wm97xx_acc_pen_up(struct wm97xx *wm)
106 if (cpu_is_pxa27x()) {
107 while (pxa2xx_ac97_read_misr() & (1 << 2))
108 pxa2xx_ac97_read_modr();
109 } else if (cpu_is_pxa3xx()) {
110 for (count = 0; count < 16; count++)
111 pxa2xx_ac97_read_modr();
115 static int wm97xx_acc_pen_down(struct wm97xx *wm)
117 u16 x, y, p = 0x100 | WM97XX_ADCSEL_PRES;
119 static u16 last, tries;
121 /* When the AC97 queue has been drained we need to allow time
122 * to buffer up samples otherwise we end up spinning polling
123 * for samples. The controller can't have a suitably low
124 * threshold set to use the notifications it gives.
133 x = pxa2xx_ac97_read_modr();
141 x = pxa2xx_ac97_read_modr();
142 y = pxa2xx_ac97_read_modr();
144 p = pxa2xx_ac97_read_modr();
146 dev_dbg(wm->dev, "Raw coordinates: x=%x, y=%x, p=%x\n",
149 /* are samples valid */
150 if ((x & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_X ||
151 (y & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_Y ||
152 (p & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_PRES)
155 /* coordinate is good */
157 input_report_abs(wm->input_dev, ABS_X, x & 0xfff);
158 input_report_abs(wm->input_dev, ABS_Y, y & 0xfff);
159 input_report_abs(wm->input_dev, ABS_PRESSURE, p & 0xfff);
160 input_report_key(wm->input_dev, BTN_TOUCH, (p != 0));
161 input_sync(wm->input_dev);
163 } while (reads < cinfo[sp_idx].reads);
165 return RC_PENDOWN | RC_AGAIN;
168 static int wm97xx_acc_startup(struct wm97xx *wm)
170 int idx = 0, ret = 0;
172 /* check we have a codec */
173 if (wm->ac97 == NULL)
176 /* Go you big red fire engine */
177 for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
178 if (wm->id != cinfo[idx].id)
181 if (cont_rate <= cinfo[idx].speed)
184 wm->acc_rate = cinfo[sp_idx].code;
185 wm->acc_slot = ac97_touch_slot;
187 "mainstone accelerated touchscreen driver, %d samples/sec\n",
188 cinfo[sp_idx].speed);
190 /* IRQ driven touchscreen is used on Palm hardware */
191 if (machine_is_palmt5() || machine_is_palmtx() || machine_is_palmld()) {
193 /* There is some obscure mutant of WM9712 interbred with WM9713
195 wm->variant = WM97xx_WM1613;
196 } else if (machine_is_zylonite()) {
201 gpiod_irq = gpiod_get(wm->dev, "touch", GPIOD_IN);
202 if (IS_ERR(gpiod_irq))
207 wm->pen_irq = gpiod_to_irq(gpiod_irq);
208 irq_set_irq_type(wm->pen_irq, IRQ_TYPE_EDGE_BOTH);
211 /* codec specific irq config */
218 /* use PEN_DOWN GPIO 13 to assert IRQ on GPIO line 2 */
219 wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
220 WM97XX_GPIO_POL_HIGH,
223 wm97xx_config_gpio(wm, WM97XX_GPIO_2, WM97XX_GPIO_OUT,
224 WM97XX_GPIO_POL_HIGH,
225 WM97XX_GPIO_NOTSTICKY,
230 "pen down irq not supported on this device\n");
239 static void wm97xx_acc_shutdown(struct wm97xx *wm)
241 /* codec specific deconfig */
244 gpiod_put(gpiod_irq);
249 static struct wm97xx_mach_ops mainstone_mach_ops = {
251 .acc_pen_up = wm97xx_acc_pen_up,
252 .acc_pen_down = wm97xx_acc_pen_down,
253 .acc_startup = wm97xx_acc_startup,
254 .acc_shutdown = wm97xx_acc_shutdown,
255 .irq_gpio = WM97XX_GPIO_2,
258 static int mainstone_wm97xx_probe(struct platform_device *pdev)
260 struct wm97xx *wm = platform_get_drvdata(pdev);
262 return wm97xx_register_mach_ops(wm, &mainstone_mach_ops);
265 static int mainstone_wm97xx_remove(struct platform_device *pdev)
267 struct wm97xx *wm = platform_get_drvdata(pdev);
269 wm97xx_unregister_mach_ops(wm);
274 static struct platform_driver mainstone_wm97xx_driver = {
275 .probe = mainstone_wm97xx_probe,
276 .remove = mainstone_wm97xx_remove,
278 .name = "wm97xx-touch",
281 module_platform_driver(mainstone_wm97xx_driver);
283 /* Module information */
284 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
285 MODULE_DESCRIPTION("wm97xx continuous touch driver for mainstone");
286 MODULE_LICENSE("GPL");