2 * Touchscreen driver for Dialog Semiconductor DA9034
4 * Copyright (C) 2006-2008 Marvell International Ltd.
5 * Fengwei Yin <fengwei.yin@marvell.com>
6 * Eric Miao <eric.miao@marvell.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/input.h>
19 #include <linux/mfd/da903x.h>
21 #define DA9034_MANUAL_CTRL 0x50
22 #define DA9034_LDO_ADC_EN (1 << 4)
24 #define DA9034_AUTO_CTRL1 0x51
26 #define DA9034_AUTO_CTRL2 0x52
27 #define DA9034_AUTO_TSI_EN (1 << 3)
28 #define DA9034_PEN_DETECT (1 << 4)
30 #define DA9034_TSI_CTRL1 0x53
31 #define DA9034_TSI_CTRL2 0x54
32 #define DA9034_TSI_X_MSB 0x6c
33 #define DA9034_TSI_Y_MSB 0x6d
34 #define DA9034_TSI_XY_LSB 0x6e
37 STATE_IDLE, /* wait for pendown */
38 STATE_BUSY, /* TSI busy sampling */
39 STATE_STOP, /* sample available */
40 STATE_WAIT, /* Wait to start next sample */
51 struct device *da9034_dev;
52 struct input_dev *input_dev;
54 struct delayed_work tsi_work;
55 struct notifier_block notifier;
67 static inline int is_pen_down(struct da9034_touch *touch)
69 return da903x_query_status(touch->da9034_dev, DA9034_STATUS_PEN_DOWN);
72 static inline int detect_pen_down(struct da9034_touch *touch, int on)
75 return da903x_set_bits(touch->da9034_dev,
76 DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
78 return da903x_clr_bits(touch->da9034_dev,
79 DA9034_AUTO_CTRL2, DA9034_PEN_DETECT);
82 static int read_tsi(struct da9034_touch *touch)
87 ret = da903x_read(touch->da9034_dev, DA9034_TSI_X_MSB, &_x);
91 ret = da903x_read(touch->da9034_dev, DA9034_TSI_Y_MSB, &_y);
95 ret = da903x_read(touch->da9034_dev, DA9034_TSI_XY_LSB, &_v);
99 touch->last_x = ((_x << 2) & 0x3fc) | (_v & 0x3);
100 touch->last_y = ((_y << 2) & 0x3fc) | ((_v & 0xc) >> 2);
105 static inline int start_tsi(struct da9034_touch *touch)
107 return da903x_set_bits(touch->da9034_dev,
108 DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
111 static inline int stop_tsi(struct da9034_touch *touch)
113 return da903x_clr_bits(touch->da9034_dev,
114 DA9034_AUTO_CTRL2, DA9034_AUTO_TSI_EN);
117 static inline void report_pen_down(struct da9034_touch *touch)
119 int x = touch->last_x;
120 int y = touch->last_y;
123 if (touch->x_inverted)
126 if (touch->y_inverted)
129 input_report_abs(touch->input_dev, ABS_X, x);
130 input_report_abs(touch->input_dev, ABS_Y, y);
131 input_report_key(touch->input_dev, BTN_TOUCH, 1);
133 input_sync(touch->input_dev);
136 static inline void report_pen_up(struct da9034_touch *touch)
138 input_report_key(touch->input_dev, BTN_TOUCH, 0);
139 input_sync(touch->input_dev);
142 static void da9034_event_handler(struct da9034_touch *touch, int event)
146 switch (touch->state) {
148 if (event != EVENT_PEN_DOWN)
151 /* Enable auto measurement of the TSI, this will
152 * automatically disable pen down detection
154 err = start_tsi(touch);
158 touch->state = STATE_BUSY;
162 if (event != EVENT_TSI_READY)
165 err = read_tsi(touch);
169 /* Disable auto measurement of the TSI, so that
170 * pen down status will be available
172 err = stop_tsi(touch);
176 touch->state = STATE_STOP;
180 if (event == EVENT_PEN_DOWN) {
181 report_pen_down(touch);
182 schedule_delayed_work(&touch->tsi_work,
183 msecs_to_jiffies(touch->interval_ms));
184 touch->state = STATE_WAIT;
187 if (event == EVENT_PEN_UP) {
188 report_pen_up(touch);
189 touch->state = STATE_IDLE;
192 input_sync(touch->input_dev);
196 if (event != EVENT_TIMEDOUT)
199 if (is_pen_down(touch)) {
201 touch->state = STATE_BUSY;
203 touch->state = STATE_IDLE;
209 touch->state = STATE_IDLE;
211 detect_pen_down(touch, 1);
214 static void da9034_tsi_work(struct work_struct *work)
216 struct da9034_touch *touch =
217 container_of(work, struct da9034_touch, tsi_work.work);
219 da9034_event_handler(touch, EVENT_TIMEDOUT);
222 static int da9034_touch_notifier(struct notifier_block *nb,
223 unsigned long event, void *data)
225 struct da9034_touch *touch =
226 container_of(nb, struct da9034_touch, notifier);
228 if (event & DA9034_EVENT_PEN_DOWN) {
229 if (is_pen_down(touch))
230 da9034_event_handler(touch, EVENT_PEN_DOWN);
232 da9034_event_handler(touch, EVENT_PEN_UP);
235 if (event & DA9034_EVENT_TSI_READY)
236 da9034_event_handler(touch, EVENT_TSI_READY);
241 static int da9034_touch_open(struct input_dev *dev)
243 struct da9034_touch *touch = input_get_drvdata(dev);
246 ret = da903x_register_notifier(touch->da9034_dev, &touch->notifier,
247 DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
252 ret = da903x_set_bits(touch->da9034_dev,
253 DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
257 /* TSI_DELAY: 3 slots, TSI_SKIP: 3 slots */
258 ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL1, 0x1b);
262 ret = da903x_write(touch->da9034_dev, DA9034_TSI_CTRL2, 0x00);
266 touch->state = STATE_IDLE;
267 detect_pen_down(touch, 1);
272 static void da9034_touch_close(struct input_dev *dev)
274 struct da9034_touch *touch = input_get_drvdata(dev);
276 da903x_unregister_notifier(touch->da9034_dev, &touch->notifier,
277 DA9034_EVENT_PEN_DOWN | DA9034_EVENT_TSI_READY);
279 cancel_delayed_work_sync(&touch->tsi_work);
281 touch->state = STATE_IDLE;
283 detect_pen_down(touch, 0);
285 /* Disable ADC LDO */
286 da903x_clr_bits(touch->da9034_dev,
287 DA9034_MANUAL_CTRL, DA9034_LDO_ADC_EN);
291 static int __devinit da9034_touch_probe(struct platform_device *pdev)
293 struct da9034_touch_pdata *pdata = pdev->dev.platform_data;
294 struct da9034_touch *touch;
295 struct input_dev *input_dev;
298 touch = kzalloc(sizeof(struct da9034_touch), GFP_KERNEL);
300 dev_err(&pdev->dev, "failed to allocate driver data\n");
304 touch->da9034_dev = pdev->dev.parent;
307 touch->interval_ms = pdata->interval_ms;
308 touch->x_inverted = pdata->x_inverted;
309 touch->y_inverted = pdata->y_inverted;
311 /* fallback into default */
312 touch->interval_ms = 10;
314 INIT_DELAYED_WORK(&touch->tsi_work, da9034_tsi_work);
315 touch->notifier.notifier_call = da9034_touch_notifier;
317 input_dev = input_allocate_device();
319 dev_err(&pdev->dev, "failed to allocate input device\n");
324 input_dev->name = pdev->name;
325 input_dev->open = da9034_touch_open;
326 input_dev->close = da9034_touch_close;
327 input_dev->dev.parent = &pdev->dev;
329 __set_bit(EV_ABS, input_dev->evbit);
330 __set_bit(ABS_X, input_dev->absbit);
331 __set_bit(ABS_Y, input_dev->absbit);
332 input_set_abs_params(input_dev, ABS_X, 0, 1023, 0, 0);
333 input_set_abs_params(input_dev, ABS_Y, 0, 1023, 0, 0);
335 __set_bit(EV_KEY, input_dev->evbit);
336 __set_bit(BTN_TOUCH, input_dev->keybit);
338 touch->input_dev = input_dev;
339 input_set_drvdata(input_dev, touch);
341 ret = input_register_device(input_dev);
345 platform_set_drvdata(pdev, touch);
349 input_free_device(input_dev);
355 static int __devexit da9034_touch_remove(struct platform_device *pdev)
357 struct da9034_touch *touch = platform_get_drvdata(pdev);
359 input_unregister_device(touch->input_dev);
365 static struct platform_driver da9034_touch_driver = {
367 .name = "da9034-touch",
368 .owner = THIS_MODULE,
370 .probe = da9034_touch_probe,
371 .remove = __devexit_p(da9034_touch_remove),
374 static int __init da9034_touch_init(void)
376 return platform_driver_register(&da9034_touch_driver);
378 module_init(da9034_touch_init);
380 static void __exit da9034_touch_exit(void)
382 platform_driver_unregister(&da9034_touch_driver);
384 module_exit(da9034_touch_exit);
386 MODULE_DESCRIPTION("Touchscreen driver for Dialog Semiconductor DA9034");
387 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
388 MODULE_LICENSE("GPL");
389 MODULE_ALIAS("platform:da9034-touch");