2 * Atmel AT91 and AVR32 continuous touch screen driver for Wolfson WM97xx AC97
5 * Copyright (C) 2008 - 2009 Atmel Corporation
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/wm97xx.h>
19 #include <linux/timer.h>
20 #include <linux/gpio.h>
22 #include <linux/slab.h>
24 #define AC97C_ICA 0x10
25 #define AC97C_CBRHR 0x30
26 #define AC97C_CBSR 0x38
27 #define AC97C_CBMR 0x3c
28 #define AC97C_IER 0x54
29 #define AC97C_IDR 0x58
31 #define AC97C_RXRDY (1 << 4)
32 #define AC97C_OVRUN (1 << 5)
34 #define AC97C_CMR_SIZE_20 (0 << 16)
35 #define AC97C_CMR_SIZE_18 (1 << 16)
36 #define AC97C_CMR_SIZE_16 (2 << 16)
37 #define AC97C_CMR_SIZE_10 (3 << 16)
38 #define AC97C_CMR_CEM_LITTLE (1 << 18)
39 #define AC97C_CMR_CEM_BIG (0 << 18)
40 #define AC97C_CMR_CENA (1 << 21)
42 #define AC97C_INT_CBEVT (1 << 4)
44 #define AC97C_SR_CAEVT (1 << 3)
46 #define AC97C_CH_MASK(slot) \
47 (0x7 << (3 * (slot - 3)))
48 #define AC97C_CH_ASSIGN(slot, channel) \
49 (AC97C_CHANNEL_##channel << (3 * (slot - 3)))
50 #define AC97C_CHANNEL_NONE 0x0
51 #define AC97C_CHANNEL_B 0x2
53 #define ac97c_writel(chip, reg, val) \
54 __raw_writel((val), (chip)->regs + AC97C_##reg)
55 #define ac97c_readl(chip, reg) \
56 __raw_readl((chip)->regs + AC97C_##reg)
58 #ifdef CONFIG_CPU_AT32AP700X
59 #define ATMEL_WM97XX_AC97C_IOMEM (0xfff02800)
60 #define ATMEL_WM97XX_AC97C_IRQ (29)
61 #define ATMEL_WM97XX_GPIO_DEFAULT (32+16) /* Pin 16 on port B. */
63 #error Unknown CPU, this driver only supports AT32AP700X CPUs.
67 u16 id; /* codec id */
68 u8 code; /* continuous code */
69 u8 reads; /* number of coord reads per read cycle */
70 u32 speed; /* number of coords per second */
73 #define WM_READS(sp) ((sp / HZ) + 1)
75 static const struct continuous cinfo[] = {
76 {WM9705_ID2, 0, WM_READS(94), 94},
77 {WM9705_ID2, 1, WM_READS(188), 188},
78 {WM9705_ID2, 2, WM_READS(375), 375},
79 {WM9705_ID2, 3, WM_READS(750), 750},
80 {WM9712_ID2, 0, WM_READS(94), 94},
81 {WM9712_ID2, 1, WM_READS(188), 188},
82 {WM9712_ID2, 2, WM_READS(375), 375},
83 {WM9712_ID2, 3, WM_READS(750), 750},
84 {WM9713_ID2, 0, WM_READS(94), 94},
85 {WM9713_ID2, 1, WM_READS(120), 120},
86 {WM9713_ID2, 2, WM_READS(154), 154},
87 {WM9713_ID2, 3, WM_READS(188), 188},
90 /* Continuous speed index. */
94 * Pen sampling frequency (Hz) in continuous mode.
96 static int cont_rate = 188;
97 module_param(cont_rate, int, 0);
98 MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");
101 * Pen down detection.
103 * This driver can either poll or use an interrupt to indicate a pen down
104 * event. If the irq request fails then it will fall back to polling mode.
106 static int pen_int = 1;
107 module_param(pen_int, int, 0);
108 MODULE_PARM_DESC(pen_int, "Pen down detection (1 = interrupt, 0 = polling)");
113 * Set to 1 to read back pen down pressure.
116 module_param(pressure, int, 0);
117 MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");
120 * AC97 touch data slot.
122 * Touch screen readback data ac97 slot.
124 static int ac97_touch_slot = 5;
125 module_param(ac97_touch_slot, int, 0);
126 MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");
131 * Set to GPIO number where the signal from the WM97xx device is hooked up.
133 static int atmel_gpio_line = ATMEL_WM97XX_GPIO_DEFAULT;
134 module_param(atmel_gpio_line, int, 0);
135 MODULE_PARM_DESC(atmel_gpio_line, "GPIO line number connected to WM97xx");
137 struct atmel_wm97xx {
139 struct timer_list pen_timer;
141 unsigned long ac97c_irq;
142 unsigned long gpio_pen;
143 unsigned long gpio_irq;
148 static irqreturn_t atmel_wm97xx_channel_b_interrupt(int irq, void *dev_id)
150 struct atmel_wm97xx *atmel_wm97xx = dev_id;
151 struct wm97xx *wm = atmel_wm97xx->wm;
152 int status = ac97c_readl(atmel_wm97xx, CBSR);
153 irqreturn_t retval = IRQ_NONE;
155 if (status & AC97C_OVRUN) {
156 dev_dbg(&wm->touch_dev->dev, "AC97C overrun\n");
157 ac97c_readl(atmel_wm97xx, CBRHR);
158 retval = IRQ_HANDLED;
159 } else if (status & AC97C_RXRDY) {
165 data = ac97c_readl(atmel_wm97xx, CBRHR);
166 value = data & 0x0fff;
167 source = data & WM97XX_ADCSEL_MASK;
168 pen_down = (data & WM97XX_PEN_DOWN) >> 8;
170 if (source == WM97XX_ADCSEL_X)
171 atmel_wm97xx->x = value;
172 if (source == WM97XX_ADCSEL_Y)
173 atmel_wm97xx->y = value;
175 if (!pressure && source == WM97XX_ADCSEL_Y) {
176 input_report_abs(wm->input_dev, ABS_X, atmel_wm97xx->x);
177 input_report_abs(wm->input_dev, ABS_Y, atmel_wm97xx->y);
178 input_report_key(wm->input_dev, BTN_TOUCH, pen_down);
179 input_sync(wm->input_dev);
180 } else if (pressure && source == WM97XX_ADCSEL_PRES) {
181 input_report_abs(wm->input_dev, ABS_X, atmel_wm97xx->x);
182 input_report_abs(wm->input_dev, ABS_Y, atmel_wm97xx->y);
183 input_report_abs(wm->input_dev, ABS_PRESSURE, value);
184 input_report_key(wm->input_dev, BTN_TOUCH, value);
185 input_sync(wm->input_dev);
188 retval = IRQ_HANDLED;
194 static void atmel_wm97xx_acc_pen_up(struct wm97xx *wm)
196 struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(wm->touch_dev);
197 struct input_dev *input_dev = wm->input_dev;
198 int pen_down = gpio_get_value(atmel_wm97xx->gpio_pen);
201 mod_timer(&atmel_wm97xx->pen_timer,
202 jiffies + msecs_to_jiffies(1));
205 input_report_abs(input_dev, ABS_PRESSURE, 0);
206 input_report_key(input_dev, BTN_TOUCH, 0);
207 input_sync(input_dev);
211 static void atmel_wm97xx_pen_timer(unsigned long data)
213 atmel_wm97xx_acc_pen_up((struct wm97xx *)data);
216 static int atmel_wm97xx_acc_startup(struct wm97xx *wm)
218 struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(wm->touch_dev);
221 if (wm->ac97 == NULL)
224 for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
225 if (wm->id != cinfo[idx].id)
230 if (cont_rate <= cinfo[idx].speed)
234 wm->acc_rate = cinfo[sp_idx].code;
235 wm->acc_slot = ac97_touch_slot;
236 dev_info(&wm->touch_dev->dev, "atmel accelerated touchscreen driver, "
237 "%d samples/sec\n", cinfo[sp_idx].speed);
242 wm->pen_irq = atmel_wm97xx->gpio_irq;
245 case WM9712_ID2: /* Fall through. */
248 * Use GPIO 13 (PEN_DOWN) to assert GPIO line 3
251 wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
252 WM97XX_GPIO_POL_HIGH,
255 wm97xx_config_gpio(wm, WM97XX_GPIO_3, WM97XX_GPIO_OUT,
256 WM97XX_GPIO_POL_HIGH,
257 WM97XX_GPIO_NOTSTICKY,
259 case WM9705_ID2: /* Fall through. */
261 * Enable touch data slot in AC97 controller channel B.
263 reg = ac97c_readl(atmel_wm97xx, ICA);
264 reg &= ~AC97C_CH_MASK(wm->acc_slot);
265 reg |= AC97C_CH_ASSIGN(wm->acc_slot, B);
266 ac97c_writel(atmel_wm97xx, ICA, reg);
269 * Enable channel and interrupt for RXRDY and OVERRUN.
271 ac97c_writel(atmel_wm97xx, CBMR, AC97C_CMR_CENA
276 /* Dummy read to empty RXRHR. */
277 ac97c_readl(atmel_wm97xx, CBRHR);
279 * Enable interrupt for channel B in the AC97
282 ac97c_writel(atmel_wm97xx, IER, AC97C_INT_CBEVT);
285 dev_err(&wm->touch_dev->dev, "pen down irq not "
286 "supported on this device\n");
295 static void atmel_wm97xx_acc_shutdown(struct wm97xx *wm)
298 struct atmel_wm97xx *atmel_wm97xx =
299 platform_get_drvdata(wm->touch_dev);
302 switch (wm->id & 0xffff) {
303 case WM9705_ID2: /* Fall through. */
304 case WM9712_ID2: /* Fall through. */
306 /* Disable slot and turn off channel B interrupts. */
307 ica = ac97c_readl(atmel_wm97xx, ICA);
308 ica &= ~AC97C_CH_MASK(wm->acc_slot);
309 ac97c_writel(atmel_wm97xx, ICA, ica);
310 ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);
311 ac97c_writel(atmel_wm97xx, CBMR, 0);
315 dev_err(&wm->touch_dev->dev, "unknown codec\n");
321 static void atmel_wm97xx_irq_enable(struct wm97xx *wm, int enable)
323 /* Intentionally left empty. */
326 static struct wm97xx_mach_ops atmel_mach_ops = {
328 .acc_pen_up = atmel_wm97xx_acc_pen_up,
329 .acc_startup = atmel_wm97xx_acc_startup,
330 .acc_shutdown = atmel_wm97xx_acc_shutdown,
331 .irq_enable = atmel_wm97xx_irq_enable,
332 .irq_gpio = WM97XX_GPIO_3,
335 static int __init atmel_wm97xx_probe(struct platform_device *pdev)
337 struct wm97xx *wm = platform_get_drvdata(pdev);
338 struct atmel_wm97xx *atmel_wm97xx;
341 atmel_wm97xx = kzalloc(sizeof(struct atmel_wm97xx), GFP_KERNEL);
343 dev_dbg(&pdev->dev, "out of memory\n");
347 atmel_wm97xx->wm = wm;
348 atmel_wm97xx->regs = (void *)ATMEL_WM97XX_AC97C_IOMEM;
349 atmel_wm97xx->ac97c_irq = ATMEL_WM97XX_AC97C_IRQ;
350 atmel_wm97xx->gpio_pen = atmel_gpio_line;
351 atmel_wm97xx->gpio_irq = gpio_to_irq(atmel_wm97xx->gpio_pen);
353 setup_timer(&atmel_wm97xx->pen_timer, atmel_wm97xx_pen_timer,
356 ret = request_irq(atmel_wm97xx->ac97c_irq,
357 atmel_wm97xx_channel_b_interrupt,
358 IRQF_SHARED, "atmel-wm97xx-ch-b", atmel_wm97xx);
360 dev_dbg(&pdev->dev, "could not request ac97c irq\n");
364 platform_set_drvdata(pdev, atmel_wm97xx);
366 ret = wm97xx_register_mach_ops(wm, &atmel_mach_ops);
373 free_irq(atmel_wm97xx->ac97c_irq, atmel_wm97xx);
379 static int __exit atmel_wm97xx_remove(struct platform_device *pdev)
381 struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);
382 struct wm97xx *wm = atmel_wm97xx->wm;
384 ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);
385 free_irq(atmel_wm97xx->ac97c_irq, atmel_wm97xx);
386 del_timer_sync(&atmel_wm97xx->pen_timer);
387 wm97xx_unregister_mach_ops(wm);
393 #ifdef CONFIG_PM_SLEEP
394 static int atmel_wm97xx_suspend(struct device *dev)
396 struct platform_device *pdev = to_platform_device(dev);
397 struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);
399 ac97c_writel(atmel_wm97xx, IDR, AC97C_INT_CBEVT);
400 disable_irq(atmel_wm97xx->gpio_irq);
401 del_timer_sync(&atmel_wm97xx->pen_timer);
406 static int atmel_wm97xx_resume(struct device *dev)
408 struct platform_device *pdev = to_platform_device(dev);
409 struct atmel_wm97xx *atmel_wm97xx = platform_get_drvdata(pdev);
410 struct wm97xx *wm = atmel_wm97xx->wm;
412 if (wm->input_dev->users) {
413 enable_irq(atmel_wm97xx->gpio_irq);
414 ac97c_writel(atmel_wm97xx, IER, AC97C_INT_CBEVT);
421 static SIMPLE_DEV_PM_OPS(atmel_wm97xx_pm_ops,
422 atmel_wm97xx_suspend, atmel_wm97xx_resume);
424 static struct platform_driver atmel_wm97xx_driver = {
425 .remove = __exit_p(atmel_wm97xx_remove),
427 .name = "wm97xx-touch",
428 .owner = THIS_MODULE,
429 .pm = &atmel_wm97xx_pm_ops,
433 module_platform_driver_probe(atmel_wm97xx_driver, atmel_wm97xx_probe);
435 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
436 MODULE_DESCRIPTION("wm97xx continuous touch driver for Atmel AT91 and AVR32");
437 MODULE_LICENSE("GPL");