blob: c5bc62d85bb6e1987ebd1377888de3f1ddfa6a79 [file] [log] [blame]
Sascha Hauerd201fd52009-12-15 08:48:57 -08001/*
2 * Driver for the Freescale Semiconductor MC13783 touchscreen.
3 *
4 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
5 * Copyright (C) 2009 Sascha Hauer, Pengutronix
6 *
7 * Initial development of this code was funded by
8 * Phytec Messtechnik GmbH, http://www.phytec.de/
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 */
14#include <linux/platform_device.h>
15#include <linux/mfd/mc13783.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/input.h>
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Sascha Hauerd201fd52009-12-15 08:48:57 -080021#include <linux/init.h>
22
23#define MC13783_TS_NAME "mc13783-ts"
24
25#define DEFAULT_SAMPLE_TOLERANCE 300
26
27static unsigned int sample_tolerance = DEFAULT_SAMPLE_TOLERANCE;
28module_param(sample_tolerance, uint, S_IRUGO | S_IWUSR);
29MODULE_PARM_DESC(sample_tolerance,
30 "If the minimal and maximal value read out for one axis (out "
31 "of three) differ by this value (default: "
32 __stringify(DEFAULT_SAMPLE_TOLERANCE) ") or more, the reading "
33 "is supposed to be wrong and is discarded. Set to 0 to "
34 "disable this check.");
35
36struct mc13783_ts_priv {
37 struct input_dev *idev;
38 struct mc13783 *mc13783;
39 struct delayed_work work;
40 struct workqueue_struct *workq;
41 unsigned int sample[4];
42};
43
44static irqreturn_t mc13783_ts_handler(int irq, void *data)
45{
46 struct mc13783_ts_priv *priv = data;
47
Uwe Kleine-König2a8331912010-03-05 13:44:26 -080048 mc13783_irq_ack(priv->mc13783, irq);
Sascha Hauerd201fd52009-12-15 08:48:57 -080049
50 /*
51 * Kick off reading coordinates. Note that if work happens already
52 * be queued for future execution (it rearms itself) it will not
53 * be rescheduled for immediate execution here. However the rearm
54 * delay is HZ / 50 which is acceptable.
55 */
56 queue_delayed_work(priv->workq, &priv->work, 0);
57
58 return IRQ_HANDLED;
59}
60
61#define sort3(a0, a1, a2) ({ \
62 if (a0 > a1) \
63 swap(a0, a1); \
64 if (a1 > a2) \
65 swap(a1, a2); \
66 if (a0 > a1) \
67 swap(a0, a1); \
68 })
69
70static void mc13783_ts_report_sample(struct mc13783_ts_priv *priv)
71{
72 struct input_dev *idev = priv->idev;
73 int x0, x1, x2, y0, y1, y2;
74 int cr0, cr1;
75
76 /*
77 * the values are 10-bit wide only, but the two least significant
78 * bits are for future 12 bit use and reading yields 0
79 */
80 x0 = priv->sample[0] & 0xfff;
81 x1 = priv->sample[1] & 0xfff;
82 x2 = priv->sample[2] & 0xfff;
83 y0 = priv->sample[3] & 0xfff;
84 y1 = (priv->sample[0] >> 12) & 0xfff;
85 y2 = (priv->sample[1] >> 12) & 0xfff;
86 cr0 = (priv->sample[2] >> 12) & 0xfff;
87 cr1 = (priv->sample[3] >> 12) & 0xfff;
88
89 dev_dbg(&idev->dev,
90 "x: (% 4d,% 4d,% 4d) y: (% 4d, % 4d,% 4d) cr: (% 4d, % 4d)\n",
91 x0, x1, x2, y0, y1, y2, cr0, cr1);
92
93 sort3(x0, x1, x2);
94 sort3(y0, y1, y2);
95
96 cr0 = (cr0 + cr1) / 2;
97
98 if (!cr0 || !sample_tolerance ||
99 (x2 - x0 < sample_tolerance &&
100 y2 - y0 < sample_tolerance)) {
101 /* report the median coordinate and average pressure */
102 if (cr0) {
103 input_report_abs(idev, ABS_X, x1);
104 input_report_abs(idev, ABS_Y, y1);
105
106 dev_dbg(&idev->dev, "report (%d, %d, %d)\n",
107 x1, y1, 0x1000 - cr0);
108 queue_delayed_work(priv->workq, &priv->work, HZ / 50);
109 } else
110 dev_dbg(&idev->dev, "report release\n");
111
112 input_report_abs(idev, ABS_PRESSURE,
113 cr0 ? 0x1000 - cr0 : cr0);
114 input_report_key(idev, BTN_TOUCH, cr0);
115 input_sync(idev);
116 } else
117 dev_dbg(&idev->dev, "discard event\n");
118}
119
120static void mc13783_ts_work(struct work_struct *work)
121{
122 struct mc13783_ts_priv *priv =
123 container_of(work, struct mc13783_ts_priv, work.work);
124 unsigned int mode = MC13783_ADC_MODE_TS;
125 unsigned int channel = 12;
126
127 if (mc13783_adc_do_conversion(priv->mc13783,
128 mode, channel, priv->sample) == 0)
129 mc13783_ts_report_sample(priv);
130}
131
132static int mc13783_ts_open(struct input_dev *dev)
133{
134 struct mc13783_ts_priv *priv = input_get_drvdata(dev);
135 int ret;
136
137 mc13783_lock(priv->mc13783);
138
Uwe Kleine-König2a8331912010-03-05 13:44:26 -0800139 mc13783_irq_ack(priv->mc13783, MC13783_IRQ_TS);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800140
141 ret = mc13783_irq_request(priv->mc13783, MC13783_IRQ_TS,
142 mc13783_ts_handler, MC13783_TS_NAME, priv);
143 if (ret)
144 goto out;
145
146 ret = mc13783_reg_rmw(priv->mc13783, MC13783_ADC0,
147 MC13783_ADC0_TSMOD_MASK, MC13783_ADC0_TSMOD0);
148 if (ret)
149 mc13783_irq_free(priv->mc13783, MC13783_IRQ_TS, priv);
150out:
151 mc13783_unlock(priv->mc13783);
152 return ret;
153}
154
155static void mc13783_ts_close(struct input_dev *dev)
156{
157 struct mc13783_ts_priv *priv = input_get_drvdata(dev);
158
159 mc13783_lock(priv->mc13783);
160 mc13783_reg_rmw(priv->mc13783, MC13783_ADC0,
161 MC13783_ADC0_TSMOD_MASK, 0);
162 mc13783_irq_free(priv->mc13783, MC13783_IRQ_TS, priv);
163 mc13783_unlock(priv->mc13783);
164
165 cancel_delayed_work_sync(&priv->work);
166}
167
168static int __init mc13783_ts_probe(struct platform_device *pdev)
169{
170 struct mc13783_ts_priv *priv;
171 struct input_dev *idev;
172 int ret = -ENOMEM;
173
174 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
175 idev = input_allocate_device();
176 if (!priv || !idev)
177 goto err_free_mem;
178
179 INIT_DELAYED_WORK(&priv->work, mc13783_ts_work);
180 priv->mc13783 = dev_get_drvdata(pdev->dev.parent);
181 priv->idev = idev;
182
183 /*
184 * We need separate workqueue because mc13783_adc_do_conversion
185 * uses keventd and thus would deadlock.
186 */
187 priv->workq = create_singlethread_workqueue("mc13783_ts");
188 if (!priv->workq)
189 goto err_free_mem;
190
191 idev->name = MC13783_TS_NAME;
192 idev->dev.parent = &pdev->dev;
193
194 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
195 idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
196 input_set_abs_params(idev, ABS_X, 0, 0xfff, 0, 0);
197 input_set_abs_params(idev, ABS_Y, 0, 0xfff, 0, 0);
198 input_set_abs_params(idev, ABS_PRESSURE, 0, 0xfff, 0, 0);
199
200 idev->open = mc13783_ts_open;
201 idev->close = mc13783_ts_close;
202
203 input_set_drvdata(idev, priv);
204
205 ret = input_register_device(priv->idev);
206 if (ret) {
207 dev_err(&pdev->dev,
208 "register input device failed with %d\n", ret);
209 goto err_destroy_wq;
210 }
211
212 platform_set_drvdata(pdev, priv);
213 return 0;
214
215err_destroy_wq:
216 destroy_workqueue(priv->workq);
217err_free_mem:
218 input_free_device(idev);
219 kfree(priv);
220 return ret;
221}
222
223static int __devexit mc13783_ts_remove(struct platform_device *pdev)
224{
225 struct mc13783_ts_priv *priv = platform_get_drvdata(pdev);
226
227 platform_set_drvdata(pdev, NULL);
228
229 destroy_workqueue(priv->workq);
230 input_unregister_device(priv->idev);
231 kfree(priv);
232
233 return 0;
234}
235
236static struct platform_driver mc13783_ts_driver = {
237 .remove = __devexit_p(mc13783_ts_remove),
238 .driver = {
239 .owner = THIS_MODULE,
240 .name = MC13783_TS_NAME,
241 },
242};
243
244static int __init mc13783_ts_init(void)
245{
246 return platform_driver_probe(&mc13783_ts_driver, &mc13783_ts_probe);
247}
248module_init(mc13783_ts_init);
249
250static void __exit mc13783_ts_exit(void)
251{
252 platform_driver_unregister(&mc13783_ts_driver);
253}
254module_exit(mc13783_ts_exit);
255
256MODULE_DESCRIPTION("MC13783 input touchscreen driver");
257MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
258MODULE_LICENSE("GPL v2");
259MODULE_ALIAS("platform:" MC13783_TS_NAME);