b9373012b3e680007cddb60cb584f8ac4dd7a6a9
[linux-2.6.git] / drivers / input / touchscreen / wm831x-ts.c
1 /*
2  * Touchscreen driver for WM831x PMICs
3  *
4  * Copyright 2011 Wolfson Microelectronics plc.
5  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  */
12
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/string.h>
18 #include <linux/pm.h>
19 #include <linux/input.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/irq.h>
24 #include <linux/mfd/wm831x/pdata.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28
29 /*
30  * R16424 (0x4028) - Touch Control 1
31  */
32 #define WM831X_TCH_ENA                          0x8000  /* TCH_ENA */
33 #define WM831X_TCH_CVT_ENA                      0x4000  /* TCH_CVT_ENA */
34 #define WM831X_TCH_SLPENA                       0x1000  /* TCH_SLPENA */
35 #define WM831X_TCH_Z_ENA                        0x0400  /* TCH_Z_ENA */
36 #define WM831X_TCH_Y_ENA                        0x0200  /* TCH_Y_ENA */
37 #define WM831X_TCH_X_ENA                        0x0100  /* TCH_X_ENA */
38 #define WM831X_TCH_DELAY_MASK                   0x00E0  /* TCH_DELAY - [7:5] */
39 #define WM831X_TCH_DELAY_SHIFT                       5  /* TCH_DELAY - [7:5] */
40 #define WM831X_TCH_DELAY_WIDTH                       3  /* TCH_DELAY - [7:5] */
41 #define WM831X_TCH_RATE_MASK                    0x001F  /* TCH_RATE - [4:0] */
42 #define WM831X_TCH_RATE_SHIFT                        0  /* TCH_RATE - [4:0] */
43 #define WM831X_TCH_RATE_WIDTH                        5  /* TCH_RATE - [4:0] */
44
45 /*
46  * R16425 (0x4029) - Touch Control 2
47  */
48 #define WM831X_TCH_PD_WK                        0x2000  /* TCH_PD_WK */
49 #define WM831X_TCH_5WIRE                        0x1000  /* TCH_5WIRE */
50 #define WM831X_TCH_PDONLY                       0x0800  /* TCH_PDONLY */
51 #define WM831X_TCH_ISEL                         0x0100  /* TCH_ISEL */
52 #define WM831X_TCH_RPU_MASK                     0x000F  /* TCH_RPU - [3:0] */
53 #define WM831X_TCH_RPU_SHIFT                         0  /* TCH_RPU - [3:0] */
54 #define WM831X_TCH_RPU_WIDTH                         4  /* TCH_RPU - [3:0] */
55
56 /*
57  * R16426-8 (0x402A-C) - Touch Data X/Y/X
58  */
59 #define WM831X_TCH_PD                           0x8000  /* TCH_PD1 */
60 #define WM831X_TCH_DATA_MASK                    0x0FFF  /* TCH_DATA - [11:0] */
61 #define WM831X_TCH_DATA_SHIFT                        0  /* TCH_DATA - [11:0] */
62 #define WM831X_TCH_DATA_WIDTH                       12  /* TCH_DATA - [11:0] */
63
64 struct wm831x_ts {
65         struct input_dev *input_dev;
66         struct wm831x *wm831x;
67         unsigned int data_irq;
68         unsigned int pd_irq;
69         bool pressure;
70         bool pen_down;
71         struct work_struct pd_data_work;
72 };
73
74 static void wm831x_pd_data_work(struct work_struct *work)
75 {
76         struct wm831x_ts *wm831x_ts =
77                 container_of(work, struct wm831x_ts, pd_data_work);
78
79         if (wm831x_ts->pen_down) {
80                 enable_irq(wm831x_ts->data_irq);
81                 dev_dbg(wm831x_ts->wm831x->dev, "IRQ PD->DATA done\n");
82         } else {
83                 enable_irq(wm831x_ts->pd_irq);
84                 dev_dbg(wm831x_ts->wm831x->dev, "IRQ DATA->PD done\n");
85         }
86 }
87
88 static irqreturn_t wm831x_ts_data_irq(int irq, void *irq_data)
89 {
90         struct wm831x_ts *wm831x_ts = irq_data;
91         struct wm831x *wm831x = wm831x_ts->wm831x;
92         static int data_types[] = { ABS_X, ABS_Y, ABS_PRESSURE };
93         u16 data[3];
94         int count;
95         int i, ret;
96
97         if (wm831x_ts->pressure)
98                 count = 3;
99         else
100                 count = 2;
101
102         wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
103                         WM831X_TCHDATA_EINT, WM831X_TCHDATA_EINT);
104
105         ret = wm831x_bulk_read(wm831x, WM831X_TOUCH_DATA_X, count,
106                                data);
107         if (ret != 0) {
108                 dev_err(wm831x->dev, "Failed to read touch data: %d\n",
109                         ret);
110                 return IRQ_NONE;
111         }
112
113         /*
114          * We get a pen down reading on every reading, report pen up if any
115          * individual reading does so.
116          */
117         wm831x_ts->pen_down = true;
118         for (i = 0; i < count; i++) {
119                 if (!(data[i] & WM831X_TCH_PD)) {
120                         wm831x_ts->pen_down = false;
121                         continue;
122                 }
123                 input_report_abs(wm831x_ts->input_dev, data_types[i],
124                                  data[i] & WM831X_TCH_DATA_MASK);
125         }
126
127         if (!wm831x_ts->pen_down) {
128                 /* Switch from data to pen down */
129                 dev_dbg(wm831x->dev, "IRQ DATA->PD\n");
130
131                 disable_irq_nosync(wm831x_ts->data_irq);
132
133                 /* Don't need data any more */
134                 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
135                                 WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA |
136                                 WM831X_TCH_Z_ENA, 0);
137
138                 /* Flush any final samples that arrived while reading */
139                 wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
140                                 WM831X_TCHDATA_EINT, WM831X_TCHDATA_EINT);
141
142                 wm831x_bulk_read(wm831x, WM831X_TOUCH_DATA_X, count, data);
143
144                 if (wm831x_ts->pressure)
145                         input_report_abs(wm831x_ts->input_dev,
146                                          ABS_PRESSURE, 0);
147
148                 input_report_key(wm831x_ts->input_dev, BTN_TOUCH, 0);
149
150                 schedule_work(&wm831x_ts->pd_data_work);
151         }
152
153         input_sync(wm831x_ts->input_dev);
154
155         return IRQ_HANDLED;
156 }
157
158 static irqreturn_t wm831x_ts_pen_down_irq(int irq, void *irq_data)
159 {
160         struct wm831x_ts *wm831x_ts = irq_data;
161         struct wm831x *wm831x = wm831x_ts->wm831x;
162         int ena = 0;
163
164         if (wm831x_ts->pen_down)
165                 return IRQ_HANDLED;
166
167         disable_irq_nosync(wm831x_ts->pd_irq);
168
169         /* Start collecting data */
170         if (wm831x_ts->pressure)
171                 ena |= WM831X_TCH_Z_ENA;
172
173         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
174                         WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA | WM831X_TCH_Z_ENA,
175                         WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA | ena);
176
177         input_report_key(wm831x_ts->input_dev, BTN_TOUCH, 1);
178         input_sync(wm831x_ts->input_dev);
179
180         wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
181                         WM831X_TCHPD_EINT, WM831X_TCHPD_EINT);
182
183         wm831x_ts->pen_down = true;
184
185         /* Switch from pen down to data */
186         dev_dbg(wm831x->dev, "IRQ PD->DATA\n");
187         schedule_work(&wm831x_ts->pd_data_work);
188
189         return IRQ_HANDLED;
190 }
191
192 static int wm831x_ts_input_open(struct input_dev *idev)
193 {
194         struct wm831x_ts *wm831x_ts = input_get_drvdata(idev);
195         struct wm831x *wm831x = wm831x_ts->wm831x;
196
197         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
198                         WM831X_TCH_ENA | WM831X_TCH_CVT_ENA |
199                         WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA |
200                         WM831X_TCH_Z_ENA, WM831X_TCH_ENA);
201
202         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
203                         WM831X_TCH_CVT_ENA, WM831X_TCH_CVT_ENA);
204
205         return 0;
206 }
207
208 static void wm831x_ts_input_close(struct input_dev *idev)
209 {
210         struct wm831x_ts *wm831x_ts = input_get_drvdata(idev);
211         struct wm831x *wm831x = wm831x_ts->wm831x;
212
213         /* Shut the controller down, disabling all other functionality too */
214         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
215                         WM831X_TCH_ENA | WM831X_TCH_X_ENA |
216                         WM831X_TCH_Y_ENA | WM831X_TCH_Z_ENA, 0);
217
218         /* Make sure any pending IRQs are done, the above will prevent
219          * new ones firing.
220          */
221         synchronize_irq(wm831x_ts->data_irq);
222         synchronize_irq(wm831x_ts->pd_irq);
223
224         /* Make sure the IRQ completion work is quiesced */
225         flush_work_sync(&wm831x_ts->pd_data_work);
226
227         /* If we ended up with the pen down then make sure we revert back
228          * to pen detection state for the next time we start up.
229          */
230         if (wm831x_ts->pen_down) {
231                 disable_irq(wm831x_ts->data_irq);
232                 enable_irq(wm831x_ts->pd_irq);
233                 wm831x_ts->pen_down = false;
234         }
235 }
236
237 static __devinit int wm831x_ts_probe(struct platform_device *pdev)
238 {
239         struct wm831x_ts *wm831x_ts;
240         struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
241         struct wm831x_pdata *core_pdata = dev_get_platdata(pdev->dev.parent);
242         struct wm831x_touch_pdata *pdata = NULL;
243         struct input_dev *input_dev;
244         int error;
245
246         if (core_pdata)
247                 pdata = core_pdata->touch;
248
249         wm831x_ts = kzalloc(sizeof(struct wm831x_ts), GFP_KERNEL);
250         input_dev = input_allocate_device();
251         if (!wm831x_ts || !input_dev) {
252                 error = -ENOMEM;
253                 goto err_alloc;
254         }
255
256         wm831x_ts->wm831x = wm831x;
257         wm831x_ts->input_dev = input_dev;
258         INIT_WORK(&wm831x_ts->pd_data_work, wm831x_pd_data_work);
259
260         /*
261          * If we have a direct IRQ use it, otherwise use the interrupt
262          * from the WM831x IRQ controller.
263          */
264         if (pdata && pdata->data_irq)
265                 wm831x_ts->data_irq = pdata->data_irq;
266         else
267                 wm831x_ts->data_irq = platform_get_irq_byname(pdev, "TCHDATA");
268
269         if (pdata && pdata->pd_irq)
270                 wm831x_ts->pd_irq = pdata->pd_irq;
271         else
272                 wm831x_ts->pd_irq = platform_get_irq_byname(pdev, "TCHPD");
273
274         if (pdata)
275                 wm831x_ts->pressure = pdata->pressure;
276         else
277                 wm831x_ts->pressure = true;
278
279         /* Five wire touchscreens can't report pressure */
280         if (pdata && pdata->fivewire) {
281                 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
282                                 WM831X_TCH_5WIRE, WM831X_TCH_5WIRE);
283
284                 /* Pressure measurements are not possible for five wire mode */
285                 WARN_ON(pdata->pressure && pdata->fivewire);
286                 wm831x_ts->pressure = false;
287         } else {
288                 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
289                                 WM831X_TCH_5WIRE, 0);
290         }
291
292         if (pdata) {
293                 switch (pdata->isel) {
294                 default:
295                         dev_err(&pdev->dev, "Unsupported ISEL setting: %d\n",
296                                 pdata->isel);
297                         /* Fall through */
298                 case 200:
299                 case 0:
300                         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
301                                         WM831X_TCH_ISEL, 0);
302                         break;
303                 case 400:
304                         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
305                                         WM831X_TCH_ISEL, WM831X_TCH_ISEL);
306                         break;
307                 }
308         }
309
310         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
311                         WM831X_TCH_PDONLY, 0);
312
313         /* Default to 96 samples/sec */
314         wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
315                         WM831X_TCH_RATE_MASK, 6);
316
317         error = request_threaded_irq(wm831x_ts->data_irq,
318                                      NULL, wm831x_ts_data_irq,
319                                      IRQF_ONESHOT,
320                                      "Touchscreen data", wm831x_ts);
321         if (error) {
322                 dev_err(&pdev->dev, "Failed to request data IRQ %d: %d\n",
323                         wm831x_ts->data_irq, error);
324                 goto err_alloc;
325         }
326         disable_irq(wm831x_ts->data_irq);
327
328         error = request_threaded_irq(wm831x_ts->pd_irq,
329                                      NULL, wm831x_ts_pen_down_irq,
330                                      IRQF_ONESHOT,
331                                      "Touchscreen pen down", wm831x_ts);
332         if (error) {
333                 dev_err(&pdev->dev, "Failed to request pen down IRQ %d: %d\n",
334                         wm831x_ts->pd_irq, error);
335                 goto err_data_irq;
336         }
337
338         /* set up touch configuration */
339         input_dev->name = "WM831x touchscreen";
340         input_dev->phys = "wm831x";
341         input_dev->open = wm831x_ts_input_open;
342         input_dev->close = wm831x_ts_input_close;
343
344         __set_bit(EV_ABS, input_dev->evbit);
345         __set_bit(EV_KEY, input_dev->evbit);
346         __set_bit(BTN_TOUCH, input_dev->keybit);
347
348         input_set_abs_params(input_dev, ABS_X, 0, 4095, 5, 0);
349         input_set_abs_params(input_dev, ABS_Y, 0, 4095, 5, 0);
350         if (wm831x_ts->pressure)
351                 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 4095, 5, 0);
352
353         input_set_drvdata(input_dev, wm831x_ts);
354         input_dev->dev.parent = &pdev->dev;
355
356         error = input_register_device(input_dev);
357         if (error)
358                 goto err_pd_irq;
359
360         platform_set_drvdata(pdev, wm831x_ts);
361         return 0;
362
363 err_pd_irq:
364         free_irq(wm831x_ts->pd_irq, wm831x_ts);
365 err_data_irq:
366         free_irq(wm831x_ts->data_irq, wm831x_ts);
367 err_alloc:
368         input_free_device(input_dev);
369         kfree(wm831x_ts);
370
371         return error;
372 }
373
374 static __devexit int wm831x_ts_remove(struct platform_device *pdev)
375 {
376         struct wm831x_ts *wm831x_ts = platform_get_drvdata(pdev);
377
378         free_irq(wm831x_ts->pd_irq, wm831x_ts);
379         free_irq(wm831x_ts->data_irq, wm831x_ts);
380         input_unregister_device(wm831x_ts->input_dev);
381         kfree(wm831x_ts);
382
383         platform_set_drvdata(pdev, NULL);
384         return 0;
385 }
386
387 static struct platform_driver wm831x_ts_driver = {
388         .driver = {
389                 .name = "wm831x-touch",
390                 .owner = THIS_MODULE,
391         },
392         .probe = wm831x_ts_probe,
393         .remove = __devexit_p(wm831x_ts_remove),
394 };
395
396 static int __init wm831x_ts_init(void)
397 {
398         return platform_driver_register(&wm831x_ts_driver);
399 }
400 module_init(wm831x_ts_init);
401
402 static void __exit wm831x_ts_exit(void)
403 {
404         platform_driver_unregister(&wm831x_ts_driver);
405 }
406 module_exit(wm831x_ts_exit);
407
408 /* Module information */
409 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
410 MODULE_DESCRIPTION("WM831x PMIC touchscreen driver");
411 MODULE_LICENSE("GPL");
412 MODULE_ALIAS("platform:wm831x-touch");