Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / drivers / input / touchscreen / omap / omap_ts.c
1 /*
2  * input/touchscreen/omap/omap_ts.c
3  *
4  * touchscreen input device driver for various TI OMAP boards
5  * Copyright (c) 2002 MontaVista Software Inc.
6  * Copyright (c) 2004 Texas Instruments, Inc.
7  * Cleanup and modularization 2004 by Dirk Behme <dirk.behme@de.bosch.com>
8  *
9  * Assembled using driver code copyright the companies above.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  * History:
25  * 12/12/2004    Srinath Modified and intergrated code for H2 and H3
26  *
27  */
28
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/input.h>
34 #include <linux/init.h>
35 #include <linux/wait.h>
36 #include <linux/interrupt.h>
37 #include <linux/suspend.h>
38 #include <linux/platform_device.h>
39
40 #include <asm/mach-types.h>
41
42 //#define DEBUG
43
44 #include "omap_ts.h"
45
46 #define OMAP_TS_NAME    "omap_ts"
47
48 static struct ts_device *__initdata ts_devs[] = {
49 #if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
50         &hx_ts,
51 #endif
52 };
53
54 static struct omap_ts_t ts_omap;
55
56 static int omap_ts_read(void)
57 {
58         u16 data[4] = { 0, 0, 0, 0 };
59
60         ts_omap.dev->read(data);
61
62         input_report_abs(ts_omap.inputdevice, ABS_X, data[0]);
63         input_report_abs(ts_omap.inputdevice, ABS_Y, data[1]);
64         input_report_abs(ts_omap.inputdevice, ABS_PRESSURE, data[2]);
65         input_sync(ts_omap.inputdevice);
66
67         DEBUG_TS("omap_ts_read: read x=%d,y=%d,p=%d\n", data[0], data[1],
68                  data[2]);
69
70         return 0;
71 }
72
73 static void omap_ts_timer(unsigned long data)
74 {
75         unsigned long flags;
76
77         spin_lock_irqsave(&ts_omap.lock, flags);
78
79         if (!ts_omap.dev->penup()) {
80                 if (!ts_omap.touched) {
81                         DEBUG_TS("omap_ts_timer: pen down\n");
82                         input_report_key(ts_omap.inputdevice, BTN_TOUCH, 1);
83                 }
84                 ts_omap.touched = 1;
85                 omap_ts_read();
86                 ts_omap.ts_timer.expires = jiffies + HZ / 100;
87                 add_timer(&(ts_omap.ts_timer));
88         } else {
89                 if (ts_omap.touched) {
90                         DEBUG_TS("omap_ts_timer: pen up\n");
91                         ts_omap.touched = 0;
92                         input_report_abs(ts_omap.inputdevice, ABS_X, 0);
93                         input_report_abs(ts_omap.inputdevice, ABS_Y, 0);
94                         input_report_abs(ts_omap.inputdevice, ABS_PRESSURE,
95                                          0);
96                         input_sync(ts_omap.inputdevice);
97                         input_report_key(ts_omap.inputdevice, BTN_TOUCH, 0);
98                 }
99                 if (!ts_omap.irq_enabled) {
100                         ts_omap.irq_enabled = 1;
101                         enable_irq(ts_omap.irq);
102                 }
103         }
104
105         spin_unlock_irqrestore(&ts_omap.lock, flags);
106 }
107
108 static irqreturn_t omap_ts_handler(int irq, void *dev_id)
109 {
110         spin_lock(&ts_omap.lock);
111
112         if (ts_omap.irq_enabled) {
113                 ts_omap.irq_enabled = 0;
114                 disable_irq(irq);
115         }
116         // restart acquire
117         mod_timer(&ts_omap.ts_timer, jiffies + HZ / 100);
118
119         spin_unlock(&ts_omap.lock);
120
121         return IRQ_HANDLED;
122 }
123
124 static int __init omap_ts_probe(struct platform_device *pdev)
125 {
126         int i;
127         int status = -ENODEV;
128
129         memset(&ts_omap, 0, sizeof(ts_omap));
130
131         ts_omap.inputdevice = input_allocate_device();
132         if (!ts_omap.inputdevice) {
133                 return -ENOMEM;
134         }
135
136         spin_lock_init(&ts_omap.lock);
137
138         for (i = 0; i < ARRAY_SIZE(ts_devs); i++) {
139                 if (!ts_devs[i] || !ts_devs[i]->probe)
140                         continue;
141                 status = ts_devs[i]->probe(&ts_omap);
142                 if (status == 0) {
143                         ts_omap.dev = ts_devs[i];
144                         break;
145                 }
146         }
147
148         if (status != 0) {
149                 input_free_device(ts_omap.inputdevice);
150                 return status;
151         }
152
153         // Init acquisition timer function
154         init_timer(&ts_omap.ts_timer);
155         ts_omap.ts_timer.function = omap_ts_timer;
156
157         /* request irq */
158         if (ts_omap.irq != -1) {
159                 if (request_irq(ts_omap.irq, omap_ts_handler,
160                                 IRQF_SAMPLE_RANDOM | ts_omap.irq_type,
161                                 OMAP_TS_NAME, &ts_omap)) {
162                         printk(KERN_ERR
163           "omap_ts.c: Could not allocate touchscreen IRQ!\n");
164                         ts_omap.irq = -1;
165                         ts_omap.dev->remove();
166                         input_free_device(ts_omap.inputdevice);
167                         return -EINVAL;
168                 }
169                 ts_omap.irq_enabled = 1;
170         } else {
171                 printk(KERN_ERR "omap_ts.c: No touchscreen IRQ assigned!\n");
172                 ts_omap.dev->remove();
173                 input_free_device(ts_omap.inputdevice);
174                 return -EINVAL;
175         }
176
177         ts_omap.inputdevice->name = OMAP_TS_NAME;
178         ts_omap.inputdevice->dev = &pdev->dev;
179         ts_omap.inputdevice->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
180         ts_omap.inputdevice->keybit[BIT_WORD(BTN_TOUCH)] |= BIT(BTN_TOUCH);
181         ts_omap.inputdevice->absbit[0] =
182             BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE);
183         input_register_device(ts_omap.inputdevice);
184
185         ts_omap.dev->enable();
186
187         printk("OMAP touchscreen driver initialized\n");
188
189         return 0;
190 }
191
192 static int omap_ts_remove(struct platform_device *pdev)
193 {
194         ts_omap.dev->disable();
195         input_unregister_device(ts_omap.inputdevice);
196         if (ts_omap.irq != -1)
197                 free_irq(ts_omap.irq, &ts_omap);
198
199         ts_omap.dev->remove();
200
201         return 0;
202 }
203
204 static int omap_ts_suspend(struct platform_device *pdev, pm_message_t state)
205 {
206         ts_omap.dev->disable();
207         return 0;
208 }
209
210 static int omap_ts_resume(struct platform_device *pdev)
211 {
212         ts_omap.dev->enable();
213         return 0;
214 }
215
216 static void omap_ts_device_release(struct device *dev)
217 {
218         /* Nothing */
219 }
220 static struct platform_driver omap_ts_driver = {
221         .probe          = omap_ts_probe,
222         .remove         = omap_ts_remove,
223         .suspend        = omap_ts_suspend,
224         .resume         = omap_ts_resume,
225         .driver = {
226                 .name   = OMAP_TS_NAME,
227         },
228 };
229
230 static struct platform_device omap_ts_device = {
231         .name           = OMAP_TS_NAME,
232         .id             = -1,
233         .dev = {
234                 .release        = omap_ts_device_release,
235         },
236 };
237
238 static int __init omap_ts_init(void)
239 {
240         int ret;
241
242         if (machine_is_omap_osk() || machine_is_omap_innovator())
243                 return -ENODEV;
244
245         ret = platform_device_register(&omap_ts_device);
246         if (ret != 0)
247                 return -ENODEV;
248
249         ret = platform_driver_register(&omap_ts_driver);
250         if (ret != 0) {
251                 platform_device_unregister(&omap_ts_device);
252                 return -ENODEV;
253         }
254
255         return 0;
256 }
257
258 static void __exit omap_ts_exit(void)
259 {
260         platform_driver_unregister(&omap_ts_driver);
261         platform_device_unregister(&omap_ts_device);
262 }
263
264 module_init(omap_ts_init);
265 module_exit(omap_ts_exit);
266
267 MODULE_LICENSE("GPL");