blob: d6f099c47f84b09ab484f0df3628cdaefb5bc1a8 [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;
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +020038 struct mc13xxx *mc13xxx;
Sascha Hauerd201fd52009-12-15 08:48:57 -080039 struct delayed_work work;
40 struct workqueue_struct *workq;
41 unsigned int sample[4];
Michael Thalmeier1039d762012-02-20 12:18:13 +010042 struct mc13xxx_ts_platform_data *touch;
Sascha Hauerd201fd52009-12-15 08:48:57 -080043};
44
45static irqreturn_t mc13783_ts_handler(int irq, void *data)
46{
47 struct mc13783_ts_priv *priv = data;
48
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +020049 mc13xxx_irq_ack(priv->mc13xxx, irq);
Sascha Hauerd201fd52009-12-15 08:48:57 -080050
51 /*
52 * Kick off reading coordinates. Note that if work happens already
53 * be queued for future execution (it rearms itself) it will not
54 * be rescheduled for immediate execution here. However the rearm
55 * delay is HZ / 50 which is acceptable.
56 */
57 queue_delayed_work(priv->workq, &priv->work, 0);
58
59 return IRQ_HANDLED;
60}
61
62#define sort3(a0, a1, a2) ({ \
63 if (a0 > a1) \
64 swap(a0, a1); \
65 if (a1 > a2) \
66 swap(a1, a2); \
67 if (a0 > a1) \
68 swap(a0, a1); \
69 })
70
71static void mc13783_ts_report_sample(struct mc13783_ts_priv *priv)
72{
73 struct input_dev *idev = priv->idev;
74 int x0, x1, x2, y0, y1, y2;
75 int cr0, cr1;
76
77 /*
78 * the values are 10-bit wide only, but the two least significant
79 * bits are for future 12 bit use and reading yields 0
80 */
81 x0 = priv->sample[0] & 0xfff;
82 x1 = priv->sample[1] & 0xfff;
83 x2 = priv->sample[2] & 0xfff;
84 y0 = priv->sample[3] & 0xfff;
85 y1 = (priv->sample[0] >> 12) & 0xfff;
86 y2 = (priv->sample[1] >> 12) & 0xfff;
87 cr0 = (priv->sample[2] >> 12) & 0xfff;
88 cr1 = (priv->sample[3] >> 12) & 0xfff;
89
90 dev_dbg(&idev->dev,
91 "x: (% 4d,% 4d,% 4d) y: (% 4d, % 4d,% 4d) cr: (% 4d, % 4d)\n",
92 x0, x1, x2, y0, y1, y2, cr0, cr1);
93
94 sort3(x0, x1, x2);
95 sort3(y0, y1, y2);
96
97 cr0 = (cr0 + cr1) / 2;
98
99 if (!cr0 || !sample_tolerance ||
100 (x2 - x0 < sample_tolerance &&
101 y2 - y0 < sample_tolerance)) {
102 /* report the median coordinate and average pressure */
103 if (cr0) {
104 input_report_abs(idev, ABS_X, x1);
105 input_report_abs(idev, ABS_Y, y1);
106
107 dev_dbg(&idev->dev, "report (%d, %d, %d)\n",
108 x1, y1, 0x1000 - cr0);
109 queue_delayed_work(priv->workq, &priv->work, HZ / 50);
110 } else
111 dev_dbg(&idev->dev, "report release\n");
112
113 input_report_abs(idev, ABS_PRESSURE,
114 cr0 ? 0x1000 - cr0 : cr0);
115 input_report_key(idev, BTN_TOUCH, cr0);
116 input_sync(idev);
117 } else
118 dev_dbg(&idev->dev, "discard event\n");
119}
120
121static void mc13783_ts_work(struct work_struct *work)
122{
123 struct mc13783_ts_priv *priv =
124 container_of(work, struct mc13783_ts_priv, work.work);
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200125 unsigned int mode = MC13XXX_ADC_MODE_TS;
Sascha Hauerd201fd52009-12-15 08:48:57 -0800126 unsigned int channel = 12;
127
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200128 if (mc13xxx_adc_do_conversion(priv->mc13xxx,
Michael Thalmeier1039d762012-02-20 12:18:13 +0100129 mode, channel,
130 priv->touch->ato, priv->touch->atox,
131 priv->sample) == 0)
Sascha Hauerd201fd52009-12-15 08:48:57 -0800132 mc13783_ts_report_sample(priv);
133}
134
135static int mc13783_ts_open(struct input_dev *dev)
136{
137 struct mc13783_ts_priv *priv = input_get_drvdata(dev);
138 int ret;
139
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200140 mc13xxx_lock(priv->mc13xxx);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800141
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200142 mc13xxx_irq_ack(priv->mc13xxx, MC13XXX_IRQ_TS);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800143
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200144 ret = mc13xxx_irq_request(priv->mc13xxx, MC13XXX_IRQ_TS,
Sascha Hauerd201fd52009-12-15 08:48:57 -0800145 mc13783_ts_handler, MC13783_TS_NAME, priv);
146 if (ret)
147 goto out;
148
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200149 ret = mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
150 MC13XXX_ADC0_TSMOD_MASK, MC13XXX_ADC0_TSMOD0);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800151 if (ret)
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200152 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800153out:
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200154 mc13xxx_unlock(priv->mc13xxx);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800155 return ret;
156}
157
158static void mc13783_ts_close(struct input_dev *dev)
159{
160 struct mc13783_ts_priv *priv = input_get_drvdata(dev);
161
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200162 mc13xxx_lock(priv->mc13xxx);
163 mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
164 MC13XXX_ADC0_TSMOD_MASK, 0);
165 mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
166 mc13xxx_unlock(priv->mc13xxx);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800167
168 cancel_delayed_work_sync(&priv->work);
169}
170
171static int __init mc13783_ts_probe(struct platform_device *pdev)
172{
173 struct mc13783_ts_priv *priv;
174 struct input_dev *idev;
175 int ret = -ENOMEM;
176
177 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
178 idev = input_allocate_device();
179 if (!priv || !idev)
180 goto err_free_mem;
181
182 INIT_DELAYED_WORK(&priv->work, mc13783_ts_work);
Uwe Kleine-König8dd93ee2011-08-24 15:28:22 +0200183 priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800184 priv->idev = idev;
Michael Thalmeier1039d762012-02-20 12:18:13 +0100185 priv->touch = dev_get_platdata(&pdev->dev);
186 if (!priv->touch) {
187 dev_err(&pdev->dev, "missing platform data\n");
188 ret = -ENODEV;
189 goto err_free_mem;
190 }
Sascha Hauerd201fd52009-12-15 08:48:57 -0800191
192 /*
193 * We need separate workqueue because mc13783_adc_do_conversion
194 * uses keventd and thus would deadlock.
195 */
196 priv->workq = create_singlethread_workqueue("mc13783_ts");
197 if (!priv->workq)
198 goto err_free_mem;
199
200 idev->name = MC13783_TS_NAME;
201 idev->dev.parent = &pdev->dev;
202
203 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
204 idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
205 input_set_abs_params(idev, ABS_X, 0, 0xfff, 0, 0);
206 input_set_abs_params(idev, ABS_Y, 0, 0xfff, 0, 0);
207 input_set_abs_params(idev, ABS_PRESSURE, 0, 0xfff, 0, 0);
208
209 idev->open = mc13783_ts_open;
210 idev->close = mc13783_ts_close;
211
212 input_set_drvdata(idev, priv);
213
214 ret = input_register_device(priv->idev);
215 if (ret) {
216 dev_err(&pdev->dev,
217 "register input device failed with %d\n", ret);
218 goto err_destroy_wq;
219 }
220
221 platform_set_drvdata(pdev, priv);
222 return 0;
223
224err_destroy_wq:
225 destroy_workqueue(priv->workq);
226err_free_mem:
227 input_free_device(idev);
228 kfree(priv);
229 return ret;
230}
231
Bill Pembertone2619cf2012-11-23 21:50:47 -0800232static int mc13783_ts_remove(struct platform_device *pdev)
Sascha Hauerd201fd52009-12-15 08:48:57 -0800233{
234 struct mc13783_ts_priv *priv = platform_get_drvdata(pdev);
235
Sascha Hauerd201fd52009-12-15 08:48:57 -0800236 destroy_workqueue(priv->workq);
237 input_unregister_device(priv->idev);
238 kfree(priv);
239
240 return 0;
241}
242
243static struct platform_driver mc13783_ts_driver = {
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800244 .remove = mc13783_ts_remove,
Sascha Hauerd201fd52009-12-15 08:48:57 -0800245 .driver = {
246 .owner = THIS_MODULE,
247 .name = MC13783_TS_NAME,
248 },
249};
Dmitry Torokhovd3d25802012-01-10 15:08:01 -0800250
Fabio Porcedda38a46eb2013-03-17 21:31:03 -0700251module_platform_driver_probe(mc13783_ts_driver, mc13783_ts_probe);
Sascha Hauerd201fd52009-12-15 08:48:57 -0800252
253MODULE_DESCRIPTION("MC13783 input touchscreen driver");
254MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
255MODULE_LICENSE("GPL v2");
256MODULE_ALIAS("platform:" MC13783_TS_NAME);