aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/wm831x-ts.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/touchscreen/wm831x-ts.c')
-rw-r--r--drivers/input/touchscreen/wm831x-ts.c368
1 files changed, 368 insertions, 0 deletions
diff --git a/drivers/input/touchscreen/wm831x-ts.c b/drivers/input/touchscreen/wm831x-ts.c
new file mode 100644
index 000000000000..6ae054f8e0aa
--- /dev/null
+++ b/drivers/input/touchscreen/wm831x-ts.c
@@ -0,0 +1,368 @@
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
64struct 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};
72
73static irqreturn_t wm831x_ts_data_irq(int irq, void *irq_data)
74{
75 struct wm831x_ts *wm831x_ts = irq_data;
76 struct wm831x *wm831x = wm831x_ts->wm831x;
77 static int data_types[] = { ABS_X, ABS_Y, ABS_PRESSURE };
78 u16 data[3];
79 int count;
80 int i, ret;
81
82 if (wm831x_ts->pressure)
83 count = 3;
84 else
85 count = 2;
86
87 wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
88 WM831X_TCHDATA_EINT, WM831X_TCHDATA_EINT);
89
90 ret = wm831x_bulk_read(wm831x, WM831X_TOUCH_DATA_X, count,
91 data);
92 if (ret != 0) {
93 dev_err(wm831x->dev, "Failed to read touch data: %d\n",
94 ret);
95 return IRQ_NONE;
96 }
97
98 /*
99 * We get a pen down reading on every reading, report pen up if any
100 * individual reading does so.
101 */
102 wm831x_ts->pen_down = true;
103 for (i = 0; i < count; i++) {
104 if (!(data[i] & WM831X_TCH_PD)) {
105 wm831x_ts->pen_down = false;
106 continue;
107 }
108 input_report_abs(wm831x_ts->input_dev, data_types[i],
109 data[i] & WM831X_TCH_DATA_MASK);
110 }
111
112 if (!wm831x_ts->pen_down) {
113 disable_irq_nosync(wm831x_ts->data_irq);
114
115 /* Don't need data any more */
116 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
117 WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA |
118 WM831X_TCH_Z_ENA, 0);
119
120 /* Flush any final samples that arrived while reading */
121 wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
122 WM831X_TCHDATA_EINT, WM831X_TCHDATA_EINT);
123
124 wm831x_bulk_read(wm831x, WM831X_TOUCH_DATA_X, count, data);
125
126 if (wm831x_ts->pressure)
127 input_report_abs(wm831x_ts->input_dev,
128 ABS_PRESSURE, 0);
129
130 input_report_key(wm831x_ts->input_dev, BTN_TOUCH, 0);
131 }
132
133 input_sync(wm831x_ts->input_dev);
134
135 return IRQ_HANDLED;
136}
137
138static irqreturn_t wm831x_ts_pen_down_irq(int irq, void *irq_data)
139{
140 struct wm831x_ts *wm831x_ts = irq_data;
141 struct wm831x *wm831x = wm831x_ts->wm831x;
142 int ena = 0;
143
144 /* Start collecting data */
145 if (wm831x_ts->pressure)
146 ena |= WM831X_TCH_Z_ENA;
147
148 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
149 WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA | WM831X_TCH_Z_ENA,
150 WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA | ena);
151
152 input_report_key(wm831x_ts->input_dev, BTN_TOUCH, 1);
153 input_sync(wm831x_ts->input_dev);
154
155 wm831x_set_bits(wm831x, WM831X_INTERRUPT_STATUS_1,
156 WM831X_TCHPD_EINT, WM831X_TCHPD_EINT);
157
158 wm831x_ts->pen_down = true;
159 enable_irq(wm831x_ts->data_irq);
160
161 return IRQ_HANDLED;
162}
163
164static int wm831x_ts_input_open(struct input_dev *idev)
165{
166 struct wm831x_ts *wm831x_ts = input_get_drvdata(idev);
167 struct wm831x *wm831x = wm831x_ts->wm831x;
168
169 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
170 WM831X_TCH_ENA | WM831X_TCH_CVT_ENA |
171 WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA |
172 WM831X_TCH_Z_ENA, WM831X_TCH_ENA);
173
174 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
175 WM831X_TCH_CVT_ENA, WM831X_TCH_CVT_ENA);
176
177 return 0;
178}
179
180static void wm831x_ts_input_close(struct input_dev *idev)
181{
182 struct wm831x_ts *wm831x_ts = input_get_drvdata(idev);
183 struct wm831x *wm831x = wm831x_ts->wm831x;
184
185 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
186 WM831X_TCH_ENA | WM831X_TCH_CVT_ENA |
187 WM831X_TCH_X_ENA | WM831X_TCH_Y_ENA |
188 WM831X_TCH_Z_ENA, 0);
189
190 if (wm831x_ts->pen_down)
191 disable_irq(wm831x_ts->data_irq);
192}
193
194static __devinit int wm831x_ts_probe(struct platform_device *pdev)
195{
196 struct wm831x_ts *wm831x_ts;
197 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
198 struct wm831x_pdata *core_pdata = dev_get_platdata(pdev->dev.parent);
199 struct wm831x_touch_pdata *pdata = NULL;
200 struct input_dev *input_dev;
201 int error;
202
203 if (core_pdata)
204 pdata = core_pdata->touch;
205
206 wm831x_ts = kzalloc(sizeof(struct wm831x_ts), GFP_KERNEL);
207 input_dev = input_allocate_device();
208 if (!wm831x_ts || !input_dev) {
209 error = -ENOMEM;
210 goto err_alloc;
211 }
212
213 wm831x_ts->wm831x = wm831x;
214 wm831x_ts->input_dev = input_dev;
215
216 /*
217 * If we have a direct IRQ use it, otherwise use the interrupt
218 * from the WM831x IRQ controller.
219 */
220 if (pdata && pdata->data_irq)
221 wm831x_ts->data_irq = pdata->data_irq;
222 else
223 wm831x_ts->data_irq = platform_get_irq_byname(pdev, "TCHDATA");
224
225 if (pdata && pdata->pd_irq)
226 wm831x_ts->pd_irq = pdata->pd_irq;
227 else
228 wm831x_ts->pd_irq = platform_get_irq_byname(pdev, "TCHPD");
229
230 if (pdata)
231 wm831x_ts->pressure = pdata->pressure;
232 else
233 wm831x_ts->pressure = true;
234
235 /* Five wire touchscreens can't report pressure */
236 if (pdata && pdata->fivewire) {
237 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
238 WM831X_TCH_5WIRE, WM831X_TCH_5WIRE);
239
240 /* Pressure measurements are not possible for five wire mode */
241 WARN_ON(pdata->pressure && pdata->fivewire);
242 wm831x_ts->pressure = false;
243 } else {
244 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
245 WM831X_TCH_5WIRE, 0);
246 }
247
248 if (pdata) {
249 switch (pdata->isel) {
250 default:
251 dev_err(&pdev->dev, "Unsupported ISEL setting: %d\n",
252 pdata->isel);
253 /* Fall through */
254 case 200:
255 case 0:
256 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
257 WM831X_TCH_ISEL, 0);
258 break;
259 case 400:
260 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
261 WM831X_TCH_ISEL, WM831X_TCH_ISEL);
262 break;
263 }
264 }
265
266 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_2,
267 WM831X_TCH_PDONLY, 0);
268
269 /* Default to 96 samples/sec */
270 wm831x_set_bits(wm831x, WM831X_TOUCH_CONTROL_1,
271 WM831X_TCH_RATE_MASK, 6);
272
273 error = request_threaded_irq(wm831x_ts->data_irq,
274 NULL, wm831x_ts_data_irq,
275 IRQF_ONESHOT,
276 "Touchscreen data", wm831x_ts);
277 if (error) {
278 dev_err(&pdev->dev, "Failed to request data IRQ %d: %d\n",
279 wm831x_ts->data_irq, error);
280 goto err_alloc;
281 }
282 disable_irq(wm831x_ts->data_irq);
283
284 error = request_threaded_irq(wm831x_ts->pd_irq,
285 NULL, wm831x_ts_pen_down_irq,
286 IRQF_ONESHOT,
287 "Touchscreen pen down", wm831x_ts);
288 if (error) {
289 dev_err(&pdev->dev, "Failed to request pen down IRQ %d: %d\n",
290 wm831x_ts->pd_irq, error);
291 goto err_data_irq;
292 }
293
294 /* set up touch configuration */
295 input_dev->name = "WM831x touchscreen";
296 input_dev->phys = "wm831x";
297 input_dev->open = wm831x_ts_input_open;
298 input_dev->close = wm831x_ts_input_close;
299
300 __set_bit(EV_ABS, input_dev->evbit);
301 __set_bit(EV_KEY, input_dev->evbit);
302 __set_bit(BTN_TOUCH, input_dev->keybit);
303
304 input_set_abs_params(input_dev, ABS_X, 0, 4095, 5, 0);
305 input_set_abs_params(input_dev, ABS_Y, 0, 4095, 5, 0);
306 if (wm831x_ts->pressure)
307 input_set_abs_params(input_dev, ABS_PRESSURE, 0, 4095, 5, 0);
308
309 input_set_drvdata(input_dev, wm831x_ts);
310 input_dev->dev.parent = &pdev->dev;
311
312 error = input_register_device(input_dev);
313 if (error)
314 goto err_pd_irq;
315
316 platform_set_drvdata(pdev, wm831x_ts);
317 return 0;
318
319err_pd_irq:
320 free_irq(wm831x_ts->pd_irq, wm831x_ts);
321err_data_irq:
322 free_irq(wm831x_ts->data_irq, wm831x_ts);
323err_alloc:
324 input_free_device(input_dev);
325 kfree(wm831x_ts);
326
327 return error;
328}
329
330static __devexit int wm831x_ts_remove(struct platform_device *pdev)
331{
332 struct wm831x_ts *wm831x_ts = platform_get_drvdata(pdev);
333
334 free_irq(wm831x_ts->pd_irq, wm831x_ts);
335 free_irq(wm831x_ts->data_irq, wm831x_ts);
336 input_unregister_device(wm831x_ts->input_dev);
337 kfree(wm831x_ts);
338
339 platform_set_drvdata(pdev, NULL);
340 return 0;
341}
342
343static struct platform_driver wm831x_ts_driver = {
344 .driver = {
345 .name = "wm831x-touch",
346 .owner = THIS_MODULE,
347 },
348 .probe = wm831x_ts_probe,
349 .remove = __devexit_p(wm831x_ts_remove),
350};
351
352static int __init wm831x_ts_init(void)
353{
354 return platform_driver_register(&wm831x_ts_driver);
355}
356module_init(wm831x_ts_init);
357
358static void __exit wm831x_ts_exit(void)
359{
360 platform_driver_unregister(&wm831x_ts_driver);
361}
362module_exit(wm831x_ts_exit);
363
364/* Module information */
365MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
366MODULE_DESCRIPTION("WM831x PMIC touchscreen driver");
367MODULE_LICENSE("GPL");
368MODULE_ALIAS("platform:wm831x-touch");