2 * Atmel Touch Screen Driver
4 * Copyright (c) 2008 ATMEL
5 * Copyright (c) 2008 Dan Liang
6 * Copyright (c) 2008 TimeSys Corporation
7 * Copyright (c) 2008 Justin Waters
9 * Based on touchscreen code from Atmel Corporation.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/input.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/clk.h>
23 #include <linux/platform_device.h>
26 /* Register definitions based on AT91SAM9RL64 preliminary draft datasheet */
28 #define ATMEL_TSADCC_CR 0x00 /* Control register */
29 #define ATMEL_TSADCC_SWRST (1 << 0) /* Software Reset*/
30 #define ATMEL_TSADCC_START (1 << 1) /* Start conversion */
32 #define ATMEL_TSADCC_MR 0x04 /* Mode register */
33 #define ATMEL_TSADCC_TSAMOD (3 << 0) /* ADC mode */
34 #define ATMEL_TSADCC_TSAMOD_ADC_ONLY_MODE (0x0) /* ADC Mode */
35 #define ATMEL_TSADCC_TSAMOD_TS_ONLY_MODE (0x1) /* Touch Screen Only Mode */
36 #define ATMEL_TSADCC_LOWRES (1 << 4) /* Resolution selection */
37 #define ATMEL_TSADCC_SLEEP (1 << 5) /* Sleep mode */
38 #define ATMEL_TSADCC_PENDET (1 << 6) /* Pen Detect selection */
39 #define ATMEL_TSADCC_PRESCAL (0x3f << 8) /* Prescalar Rate Selection */
40 #define ATMEL_TSADCC_STARTUP (0x7f << 16) /* Start Up time */
41 #define ATMEL_TSADCC_SHTIM (0xf << 24) /* Sample & Hold time */
42 #define ATMEL_TSADCC_PENDBC (0xf << 28) /* Pen Detect debouncing time */
44 #define ATMEL_TSADCC_TRGR 0x08 /* Trigger register */
45 #define ATMEL_TSADCC_TRGMOD (7 << 0) /* Trigger mode */
46 #define ATMEL_TSADCC_TRGMOD_NONE (0 << 0)
47 #define ATMEL_TSADCC_TRGMOD_EXT_RISING (1 << 0)
48 #define ATMEL_TSADCC_TRGMOD_EXT_FALLING (2 << 0)
49 #define ATMEL_TSADCC_TRGMOD_EXT_ANY (3 << 0)
50 #define ATMEL_TSADCC_TRGMOD_PENDET (4 << 0)
51 #define ATMEL_TSADCC_TRGMOD_PERIOD (5 << 0)
52 #define ATMEL_TSADCC_TRGMOD_CONTINUOUS (6 << 0)
53 #define ATMEL_TSADCC_TRGPER (0xffff << 16) /* Trigger period */
55 #define ATMEL_TSADCC_TSR 0x0C /* Touch Screen register */
56 #define ATMEL_TSADCC_TSFREQ (0xf << 0) /* TS Frequency in Interleaved mode */
57 #define ATMEL_TSADCC_TSSHTIM (0xf << 24) /* Sample & Hold time */
59 #define ATMEL_TSADCC_CHER 0x10 /* Channel Enable register */
60 #define ATMEL_TSADCC_CHDR 0x14 /* Channel Disable register */
61 #define ATMEL_TSADCC_CHSR 0x18 /* Channel Status register */
62 #define ATMEL_TSADCC_CH(n) (1 << (n)) /* Channel number */
64 #define ATMEL_TSADCC_SR 0x1C /* Status register */
65 #define ATMEL_TSADCC_EOC(n) (1 << ((n)+0)) /* End of conversion for channel N */
66 #define ATMEL_TSADCC_OVRE(n) (1 << ((n)+8)) /* Overrun error for channel N */
67 #define ATMEL_TSADCC_DRDY (1 << 16) /* Data Ready */
68 #define ATMEL_TSADCC_GOVRE (1 << 17) /* General Overrun Error */
69 #define ATMEL_TSADCC_ENDRX (1 << 18) /* End of RX Buffer */
70 #define ATMEL_TSADCC_RXBUFF (1 << 19) /* TX Buffer full */
71 #define ATMEL_TSADCC_PENCNT (1 << 20) /* Pen contact */
72 #define ATMEL_TSADCC_NOCNT (1 << 21) /* No contact */
74 #define ATMEL_TSADCC_LCDR 0x20 /* Last Converted Data register */
75 #define ATMEL_TSADCC_DATA (0x3ff << 0) /* Channel data */
77 #define ATMEL_TSADCC_IER 0x24 /* Interrupt Enable register */
78 #define ATMEL_TSADCC_IDR 0x28 /* Interrupt Disable register */
79 #define ATMEL_TSADCC_IMR 0x2C /* Interrupt Mask register */
80 #define ATMEL_TSADCC_CDR0 0x30 /* Channel Data 0 */
81 #define ATMEL_TSADCC_CDR1 0x34 /* Channel Data 1 */
82 #define ATMEL_TSADCC_CDR2 0x38 /* Channel Data 2 */
83 #define ATMEL_TSADCC_CDR3 0x3C /* Channel Data 3 */
84 #define ATMEL_TSADCC_CDR4 0x40 /* Channel Data 4 */
85 #define ATMEL_TSADCC_CDR5 0x44 /* Channel Data 5 */
87 #define ADC_CLOCK 1000000
90 struct input_dev *input;
96 static void __iomem *tsc_base;
98 #define atmel_tsadcc_read(reg) __raw_readl(tsc_base + (reg))
99 #define atmel_tsadcc_write(reg, val) __raw_writel((val), tsc_base + (reg))
101 static irqreturn_t atmel_tsadcc_interrupt(int irq, void *dev)
103 struct input_dev *input_dev = ((struct atmel_tsadcc *)dev)->input;
110 status = atmel_tsadcc_read(ATMEL_TSADCC_SR);
111 status &= atmel_tsadcc_read(ATMEL_TSADCC_IMR);
113 if (status & ATMEL_TSADCC_NOCNT) {
115 reg = atmel_tsadcc_read(ATMEL_TSADCC_MR) | ATMEL_TSADCC_PENDBC;
117 atmel_tsadcc_write(ATMEL_TSADCC_MR, reg);
118 atmel_tsadcc_write(ATMEL_TSADCC_TRGR, ATMEL_TSADCC_TRGMOD_NONE);
119 atmel_tsadcc_write(ATMEL_TSADCC_IDR,
120 ATMEL_TSADCC_EOC(3) | ATMEL_TSADCC_NOCNT);
121 atmel_tsadcc_write(ATMEL_TSADCC_IER, ATMEL_TSADCC_PENCNT);
123 input_report_key(input_dev, BTN_TOUCH, 0);
124 input_sync(input_dev);
126 } else if (status & ATMEL_TSADCC_PENCNT) {
128 reg = atmel_tsadcc_read(ATMEL_TSADCC_MR);
129 reg &= ~ATMEL_TSADCC_PENDBC;
131 atmel_tsadcc_write(ATMEL_TSADCC_IDR, ATMEL_TSADCC_PENCNT);
132 atmel_tsadcc_write(ATMEL_TSADCC_MR, reg);
133 atmel_tsadcc_write(ATMEL_TSADCC_IER,
134 ATMEL_TSADCC_EOC(3) | ATMEL_TSADCC_NOCNT);
135 atmel_tsadcc_write(ATMEL_TSADCC_TRGR,
136 ATMEL_TSADCC_TRGMOD_PERIOD | (0x0FFF << 16));
138 } else if (status & ATMEL_TSADCC_EOC(3)) {
139 /* Conversion finished */
141 absx = atmel_tsadcc_read(ATMEL_TSADCC_CDR3) << 10;
142 absx /= atmel_tsadcc_read(ATMEL_TSADCC_CDR2);
144 absy = atmel_tsadcc_read(ATMEL_TSADCC_CDR1) << 10;
145 absy /= atmel_tsadcc_read(ATMEL_TSADCC_CDR0);
147 input_report_abs(input_dev, ABS_X, absx);
148 input_report_abs(input_dev, ABS_Y, absy);
149 input_report_key(input_dev, BTN_TOUCH, 1);
150 input_sync(input_dev);
157 * The functions for inserting/removing us as a module.
160 static int __devinit atmel_tsadcc_probe(struct platform_device *pdev)
162 struct atmel_tsadcc *ts_dev;
163 struct input_dev *input_dev;
164 struct resource *res;
169 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
171 dev_err(&pdev->dev, "no mmio resource defined.\n");
175 /* Allocate memory for device */
176 ts_dev = kzalloc(sizeof(struct atmel_tsadcc), GFP_KERNEL);
178 dev_err(&pdev->dev, "failed to allocate memory.\n");
181 platform_set_drvdata(pdev, ts_dev);
183 input_dev = input_allocate_device();
185 dev_err(&pdev->dev, "failed to allocate input device.\n");
190 ts_dev->irq = platform_get_irq(pdev, 0);
191 if (ts_dev->irq < 0) {
192 dev_err(&pdev->dev, "no irq ID is designated.\n");
197 if (!request_mem_region(res->start, res->end - res->start + 1,
198 "atmel tsadcc regs")) {
199 dev_err(&pdev->dev, "resources is unavailable.\n");
204 tsc_base = ioremap(res->start, res->end - res->start + 1);
206 dev_err(&pdev->dev, "failed to map registers.\n");
208 goto err_release_mem;
211 err = request_irq(ts_dev->irq, atmel_tsadcc_interrupt, IRQF_DISABLED,
212 pdev->dev.driver->name, ts_dev);
214 dev_err(&pdev->dev, "failed to allocate irq.\n");
218 ts_dev->clk = clk_get(&pdev->dev, "tsc_clk");
219 if (IS_ERR(ts_dev->clk)) {
220 dev_err(&pdev->dev, "failed to get ts_clk\n");
221 err = PTR_ERR(ts_dev->clk);
225 ts_dev->input = input_dev;
227 snprintf(ts_dev->phys, sizeof(ts_dev->phys),
228 "%s/input0", pdev->dev.bus_id);
230 input_dev->name = "atmel touch screen controller";
231 input_dev->phys = ts_dev->phys;
232 input_dev->dev.parent = &pdev->dev;
234 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
235 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
237 input_set_abs_params(input_dev, ABS_X, 0, 0x3FF, 0, 0);
238 input_set_abs_params(input_dev, ABS_Y, 0, 0x3FF, 0, 0);
240 /* clk_enable() always returns 0, no need to check it */
241 clk_enable(ts_dev->clk);
243 prsc = clk_get_rate(ts_dev->clk);
244 dev_info(&pdev->dev, "Master clock is set at: %d Hz\n", prsc);
246 prsc = prsc / ADC_CLOCK / 2 - 1;
248 reg = ATMEL_TSADCC_TSAMOD_TS_ONLY_MODE |
249 ((0x00 << 5) & ATMEL_TSADCC_SLEEP) | /* Normal Mode */
250 ((0x01 << 6) & ATMEL_TSADCC_PENDET) | /* Enable Pen Detect */
251 ((prsc << 8) & ATMEL_TSADCC_PRESCAL) | /* PRESCAL */
252 ((0x13 << 16) & ATMEL_TSADCC_STARTUP) | /* STARTUP */
253 ((0x0F << 28) & ATMEL_TSADCC_PENDBC); /* PENDBC */
255 atmel_tsadcc_write(ATMEL_TSADCC_CR, ATMEL_TSADCC_SWRST);
256 atmel_tsadcc_write(ATMEL_TSADCC_MR, reg);
257 atmel_tsadcc_write(ATMEL_TSADCC_TRGR, ATMEL_TSADCC_TRGMOD_NONE);
258 atmel_tsadcc_write(ATMEL_TSADCC_TSR, (0x3 << 24) & ATMEL_TSADCC_TSSHTIM);
260 atmel_tsadcc_read(ATMEL_TSADCC_SR);
261 atmel_tsadcc_write(ATMEL_TSADCC_IER, ATMEL_TSADCC_PENCNT);
263 /* All went ok, so register to the input system */
264 err = input_register_device(input_dev);
271 clk_disable(ts_dev->clk);
272 clk_put(ts_dev->clk);
274 free_irq(ts_dev->irq, ts_dev);
278 release_mem_region(res->start, res->end - res->start + 1);
280 input_free_device(ts_dev->input);
286 static int __devexit atmel_tsadcc_remove(struct platform_device *pdev)
288 struct atmel_tsadcc *ts_dev = dev_get_drvdata(&pdev->dev);
289 struct resource *res;
291 free_irq(ts_dev->irq, ts_dev);
293 input_unregister_device(ts_dev->input);
295 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
297 release_mem_region(res->start, res->end - res->start + 1);
299 clk_disable(ts_dev->clk);
300 clk_put(ts_dev->clk);
307 static struct platform_driver atmel_tsadcc_driver = {
308 .probe = atmel_tsadcc_probe,
309 .remove = __devexit_p(atmel_tsadcc_remove),
311 .name = "atmel_tsadcc",
315 static int __init atmel_tsadcc_init(void)
317 return platform_driver_register(&atmel_tsadcc_driver);
320 static void __exit atmel_tsadcc_exit(void)
322 platform_driver_unregister(&atmel_tsadcc_driver);
325 module_init(atmel_tsadcc_init);
326 module_exit(atmel_tsadcc_exit);
329 MODULE_LICENSE("GPL");
330 MODULE_DESCRIPTION("Atmel TouchScreen Driver");
331 MODULE_AUTHOR("Dan Liang <dan.liang@atmel.com>");