blob: 666a6e74be4014c3e33832c1698f7d144805cb92 [file] [log] [blame]
Eric Miao9bcc00b2008-12-23 04:21:04 -05001/*
2 * Touchscreen driver for Dialog Semiconductor DA9034
3 *
4 * Copyright (C) 2006-2008 Marvell International Ltd.
5 * Fengwei Yin <fengwei.yin@marvell.com>
Eric Miao93ff27c2009-04-14 10:38:35 -07006 * Bin Yang <bin.yang@marvell.com>
Eric Miao9bcc00b2008-12-23 04:21:04 -05007 * Eric Miao <eric.miao@marvell.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/platform_device.h>
19#include <linux/input.h>
Alan Cox30aafdb2009-01-07 11:40:03 +000020#include <linux/workqueue.h>
Eric Miao9bcc00b2008-12-23 04:21:04 -050021#include <linux/mfd/da903x.h>
22
23#define DA9034_MANUAL_CTRL 0x50
24#define DA9034_LDO_ADC_EN (1 << 4)
25
26#define DA9034_AUTO_CTRL1 0x51
27
28#define DA9034_AUTO_CTRL2 0x52
29#define DA9034_AUTO_TSI_EN (1 << 3)
30#define DA9034_PEN_DETECT (1 << 4)
31
32#define DA9034_TSI_CTRL1 0x53
33#define DA9034_TSI_CTRL2 0x54
34#define DA9034_TSI_X_MSB 0x6c
35#define DA9034_TSI_Y_MSB 0x6d
36#define DA9034_TSI_XY_LSB 0x6e
37
38enum {
39 STATE_IDLE, /* wait for pendown */
40 STATE_BUSY, /* TSI busy sampling */
41 STATE_STOP, /* sample available */
42 STATE_WAIT, /* Wait to start next sample */
43};
44
45enum {
46 EVENT_PEN_DOWN,
47 EVENT_PEN_UP,
48 EVENT_TSI_READY,
49 EVENT_TIMEDOUT,
50};
51
52struct da9034_touch {
53 struct device *da9034_dev;
54 struct input_dev *input_dev;
55
56 struct delayed_work tsi_work;
57 struct notifier_block notifier;
58
59 int state;
60
61 int interval_ms;
62 int x_inverted;
63 int y_inverted;
64
65 int last_x;
66 int last_y;
67};
68
69static inline int is_pen_down(struct da9034_touch *touch)
70{
71 return da903x_query_status(touch->da9034_dev, DA9034_STATUS_PEN_DOWN);
72}
73
74static inline int detect_pen_down(struct da9034_touch *touch, int on)
75{
76 if (on)
77 return da903x_set_bits(touch->da9034_dev,
78 DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
79 else
80 return da903x_clr_bits(touch->da9034_dev,
81 DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
82}
83
84static int read_tsi(struct da9034_touch *touch)
85{
86 uint8_t _x, _y, _v;
87 int ret;
88
89 ret = da903x_read(touch->da9034_dev, DA9034_TSI_X_MSB, &_x);
90 if (ret)
91 return ret;
92
93 ret = da903x_read(touch->da9034_dev, DA9034_TSI_Y_MSB, &_y);
94 if (ret)
95 return ret;
96
97 ret = da903x_read(touch->da9034_dev, DA9034_TSI_XY_LSB, &_v);
98 if (ret)
99 return ret;
100
101 touch->last_x = ((_x << 2) & 0x3fc) | (_v & 0x3);
102 touch->last_y = ((_y << 2) & 0x3fc) | ((_v & 0xc) >> 2);
103
104 return 0;
105}
106
107static inline int start_tsi(struct da9034_touch *touch)
108{
109 return da903x_set_bits(touch->da9034_dev,
110 DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
111}
112
113static inline int stop_tsi(struct da9034_touch *touch)
114{
115 return da903x_clr_bits(touch->da9034_dev,
116 DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
117}
118
119static inline void report_pen_down(struct da9034_touch *touch)
120{
121 int x = touch->last_x;
122 int y = touch->last_y;
123
124 x &= 0xfff;
125 if (touch->x_inverted)
126 x = 1024 - x;
127 y &= 0xfff;
128 if (touch->y_inverted)
129 y = 1024 - y;
130
131 input_report_abs(touch->input_dev, ABS_X, x);
132 input_report_abs(touch->input_dev, ABS_Y, y);
133 input_report_key(touch->input_dev, BTN_TOUCH, 1);
134
135 input_sync(touch->input_dev);
136}
137
138static inline void report_pen_up(struct da9034_touch *touch)
139{
140 input_report_key(touch->input_dev, BTN_TOUCH, 0);
141 input_sync(touch->input_dev);
142}
143
144static void da9034_event_handler(struct da9034_touch *touch, int event)
145{
146 int err;
147
148 switch (touch->state) {
149 case STATE_IDLE:
150 if (event != EVENT_PEN_DOWN)
151 break;
152
153 /* Enable auto measurement of the TSI, this will
154 * automatically disable pen down detection
155 */
156 err = start_tsi(touch);
157 if (err)
158 goto err_reset;
159
160 touch->state = STATE_BUSY;
161 break;
162
163 case STATE_BUSY:
164 if (event != EVENT_TSI_READY)
165 break;
166
167 err = read_tsi(touch);
168 if (err)
169 goto err_reset;
170
171 /* Disable auto measurement of the TSI, so that
172 * pen down status will be available
173 */
174 err = stop_tsi(touch);
175 if (err)
176 goto err_reset;
177
178 touch->state = STATE_STOP;
179 break;
180
181 case STATE_STOP:
182 if (event == EVENT_PEN_DOWN) {
183 report_pen_down(touch);
184 schedule_delayed_work(&touch->tsi_work,
185 msecs_to_jiffies(touch->interval_ms));
186 touch->state = STATE_WAIT;
187 }
188
189 if (event == EVENT_PEN_UP) {
190 report_pen_up(touch);
191 touch->state = STATE_IDLE;
192 }
193
194 input_sync(touch->input_dev);
195 break;
196
197 case STATE_WAIT:
198 if (event != EVENT_TIMEDOUT)
199 break;
200
201 if (is_pen_down(touch)) {
202 start_tsi(touch);
203 touch->state = STATE_BUSY;
204 } else
205 touch->state = STATE_IDLE;
206 break;
207 }
208 return;
209
210err_reset:
211 touch->state = STATE_IDLE;
212 stop_tsi(touch);
213 detect_pen_down(touch, 1);
214}
215
216static void da9034_tsi_work(struct work_struct *work)
217{
218 struct da9034_touch *touch =
219 container_of(work, struct da9034_touch, tsi_work.work);
220
221 da9034_event_handler(touch, EVENT_TIMEDOUT);
222}
223
224static int da9034_touch_notifier(struct notifier_block *nb,
225 unsigned long event, void *data)
226{
227 struct da9034_touch *touch =
228 container_of(nb, struct da9034_touch, notifier);
229
230 if (event & DA9034_EVENT_PEN_DOWN) {
231 if (is_pen_down(touch))
232 da9034_event_handler(touch, EVENT_PEN_DOWN);
233 else
234 da9034_event_handler(touch, EVENT_PEN_UP);
235 }
236
237 if (event & DA9034_EVENT_TSI_READY)
238 da9034_event_handler(touch, EVENT_TSI_READY);
239
240 return 0;
241}
242
243static int da9034_touch_open(struct input_dev *dev)
244{
245 struct da9034_touch *touch = input_get_drvdata(dev);
246 int ret;
247
248 ret = da903x_register_notifier(touch->da9034_dev, &touch->notifier,
249 DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
250 if (ret)
251 return -EBUSY;
252
253 /* Enable ADC LDO */
254 ret = da903x_set_bits(touch->da9034_dev,
255 DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
256 if (ret)
257 return ret;
258
259 /* TSI_DELAY: 3 slots, TSI_SKIP: 3 slots */
260 ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL1, 0x1b);
261 if (ret)
262 return ret;
263
264 ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL2, 0x00);
265 if (ret)
266 return ret;
267
268 touch->state = STATE_IDLE;
269 detect_pen_down(touch, 1);
270
271 return 0;
272}
273
274static void da9034_touch_close(struct input_dev *dev)
275{
276 struct da9034_touch *touch = input_get_drvdata(dev);
277
278 da903x_unregister_notifier(touch->da9034_dev, &touch->notifier,
279 DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
280
281 cancel_delayed_work_sync(&touch->tsi_work);
282
283 touch->state = STATE_IDLE;
284 stop_tsi(touch);
285 detect_pen_down(touch, 0);
286
287 /* Disable ADC LDO */
288 da903x_clr_bits(touch->da9034_dev,
289 DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
290}
291
292
293static int __devinit da9034_touch_probe(struct platform_device *pdev)
294{
295 struct da9034_touch_pdata *pdata = pdev->dev.platform_data;
296 struct da9034_touch *touch;
297 struct input_dev *input_dev;
298 int ret;
299
300 touch = kzalloc(sizeof(struct da9034_touch), GFP_KERNEL);
301 if (touch == NULL) {
302 dev_err(&pdev->dev, "failed to allocate driver data\n");
303 return -ENOMEM;
304 }
305
306 touch->da9034_dev = pdev->dev.parent;
307
308 if (pdata) {
309 touch->interval_ms = pdata->interval_ms;
310 touch->x_inverted = pdata->x_inverted;
311 touch->y_inverted = pdata->y_inverted;
312 } else
313 /* fallback into default */
314 touch->interval_ms = 10;
315
316 INIT_DELAYED_WORK(&touch->tsi_work, da9034_tsi_work);
317 touch->notifier.notifier_call = da9034_touch_notifier;
318
319 input_dev = input_allocate_device();
320 if (!input_dev) {
321 dev_err(&pdev->dev, "failed to allocate input device\n");
322 ret = -ENOMEM;
323 goto err_free_touch;
324 }
325
326 input_dev->name = pdev->name;
327 input_dev->open = da9034_touch_open;
328 input_dev->close = da9034_touch_close;
329 input_dev->dev.parent = &pdev->dev;
330
331 __set_bit(EV_ABS, input_dev->evbit);
332 __set_bit(ABS_X, input_dev->absbit);
333 __set_bit(ABS_Y, input_dev->absbit);
334 input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0);
335 input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0);
336
337 __set_bit(EV_KEY, input_dev->evbit);
338 __set_bit(BTN_TOUCH, input_dev->keybit);
339
340 touch->input_dev = input_dev;
341 input_set_drvdata(input_dev, touch);
342
343 ret = input_register_device(input_dev);
344 if (ret)
345 goto err_free_input;
346
347 platform_set_drvdata(pdev, touch);
348 return 0;
349
350err_free_input:
351 input_free_device(input_dev);
352err_free_touch:
353 kfree(touch);
354 return ret;
355}
356
357static int __devexit da9034_touch_remove(struct platform_device *pdev)
358{
359 struct da9034_touch *touch = platform_get_drvdata(pdev);
360
361 input_unregister_device(touch->input_dev);
362 kfree(touch);
363
364 return 0;
365}
366
367static struct platform_driver da9034_touch_driver = {
368 .driver = {
369 .name = "da9034-touch",
370 .owner = THIS_MODULE,
371 },
372 .probe = da9034_touch_probe,
373 .remove = __devexit_p(da9034_touch_remove),
374};
375
376static int __init da9034_touch_init(void)
377{
378 return platform_driver_register(&da9034_touch_driver);
379}
380module_init(da9034_touch_init);
381
382static void __exit da9034_touch_exit(void)
383{
384 platform_driver_unregister(&da9034_touch_driver);
385}
386module_exit(da9034_touch_exit);
387
388MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9034");
Eric Miao93ff27c2009-04-14 10:38:35 -0700389MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>, Bin Yang <bin.yang@marvell.com>");
Eric Miao9bcc00b2008-12-23 04:21:04 -0500390MODULE_LICENSE("GPL");
391MODULE_ALIAS("platform:da9034-touch");