xref: /linux/drivers/input/touchscreen/mc13783_ts.c (revision 30bbcb44707a97fcb62246bebc8b413b5ab293f8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Freescale Semiconductor MC13783 touchscreen.
4  *
5  * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
6  * Copyright (C) 2009 Sascha Hauer, Pengutronix
7  *
8  * Initial development of this code was funded by
9  * Phytec Messtechnik GmbH, http://www.phytec.de/
10  */
11 #include <linux/platform_device.h>
12 #include <linux/mfd/mc13783.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/input.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 
20 #define MC13783_TS_NAME	"mc13783-ts"
21 
22 #define DEFAULT_SAMPLE_TOLERANCE 300
23 
24 static unsigned int sample_tolerance = DEFAULT_SAMPLE_TOLERANCE;
25 module_param(sample_tolerance, uint, S_IRUGO | S_IWUSR);
26 MODULE_PARM_DESC(sample_tolerance,
27 		"If the minimal and maximal value read out for one axis (out "
28 		"of three) differ by this value (default: "
29 		__stringify(DEFAULT_SAMPLE_TOLERANCE) ") or more, the reading "
30 		"is supposed to be wrong and is discarded.  Set to 0 to "
31 		"disable this check.");
32 
33 struct mc13783_ts_priv {
34 	struct input_dev *idev;
35 	struct mc13xxx *mc13xxx;
36 	struct delayed_work work;
37 	unsigned int sample[4];
38 	struct mc13xxx_ts_platform_data *touch;
39 };
40 
41 static irqreturn_t mc13783_ts_handler(int irq, void *data)
42 {
43 	struct mc13783_ts_priv *priv = data;
44 
45 	/*
46 	 * Kick off reading coordinates. Note that if work happens already
47 	 * be queued for future execution (it rearms itself) it will not
48 	 * be rescheduled for immediate execution here. However the rearm
49 	 * delay is HZ / 50 which is acceptable.
50 	 */
51 	schedule_delayed_work(&priv->work, 0);
52 
53 	return IRQ_HANDLED;
54 }
55 
56 #define sort3(a0, a1, a2) ({						\
57 		if (a0 > a1)						\
58 			swap(a0, a1);					\
59 		if (a1 > a2)						\
60 			swap(a1, a2);					\
61 		if (a0 > a1)						\
62 			swap(a0, a1);					\
63 		})
64 
65 static void mc13783_ts_report_sample(struct mc13783_ts_priv *priv)
66 {
67 	struct input_dev *idev = priv->idev;
68 	int x0, x1, x2, y0, y1, y2;
69 	int cr0, cr1;
70 
71 	/*
72 	 * the values are 10-bit wide only, but the two least significant
73 	 * bits are for future 12 bit use and reading yields 0
74 	 */
75 	x0 = priv->sample[0] & 0xfff;
76 	x1 = priv->sample[1] & 0xfff;
77 	x2 = priv->sample[2] & 0xfff;
78 	y0 = priv->sample[3] & 0xfff;
79 	y1 = (priv->sample[0] >> 12) & 0xfff;
80 	y2 = (priv->sample[1] >> 12) & 0xfff;
81 	cr0 = (priv->sample[2] >> 12) & 0xfff;
82 	cr1 = (priv->sample[3] >> 12) & 0xfff;
83 
84 	dev_dbg(&idev->dev,
85 		"x: (% 4d,% 4d,% 4d) y: (% 4d, % 4d,% 4d) cr: (% 4d, % 4d)\n",
86 		x0, x1, x2, y0, y1, y2, cr0, cr1);
87 
88 	sort3(x0, x1, x2);
89 	sort3(y0, y1, y2);
90 
91 	cr0 = (cr0 + cr1) / 2;
92 
93 	if (!cr0 || !sample_tolerance ||
94 			(x2 - x0 < sample_tolerance &&
95 			 y2 - y0 < sample_tolerance)) {
96 		/* report the median coordinate and average pressure */
97 		if (cr0) {
98 			input_report_abs(idev, ABS_X, x1);
99 			input_report_abs(idev, ABS_Y, y1);
100 
101 			dev_dbg(&idev->dev, "report (%d, %d, %d)\n",
102 					x1, y1, 0x1000 - cr0);
103 			schedule_delayed_work(&priv->work, HZ / 50);
104 		} else {
105 			dev_dbg(&idev->dev, "report release\n");
106 		}
107 
108 		input_report_abs(idev, ABS_PRESSURE,
109 				cr0 ? 0x1000 - cr0 : cr0);
110 		input_report_key(idev, BTN_TOUCH, cr0);
111 		input_sync(idev);
112 	} else {
113 		dev_dbg(&idev->dev, "discard event\n");
114 	}
115 }
116 
117 static void mc13783_ts_work(struct work_struct *work)
118 {
119 	struct mc13783_ts_priv *priv =
120 		container_of(work, struct mc13783_ts_priv, work.work);
121 	unsigned int mode = MC13XXX_ADC_MODE_TS;
122 	unsigned int channel = 12;
123 
124 	if (mc13xxx_adc_do_conversion(priv->mc13xxx,
125 				mode, channel,
126 				priv->touch->ato, priv->touch->atox,
127 				priv->sample) == 0)
128 		mc13783_ts_report_sample(priv);
129 }
130 
131 static int mc13783_ts_open(struct input_dev *dev)
132 {
133 	struct mc13783_ts_priv *priv = input_get_drvdata(dev);
134 	int ret;
135 
136 	mc13xxx_lock(priv->mc13xxx);
137 
138 	ret = mc13xxx_irq_request(priv->mc13xxx, MC13XXX_IRQ_TS,
139 		mc13783_ts_handler, MC13783_TS_NAME, priv);
140 	if (ret)
141 		goto out;
142 
143 	ret = mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
144 			MC13XXX_ADC0_TSMOD_MASK, MC13XXX_ADC0_TSMOD0);
145 	if (ret)
146 		mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
147 out:
148 	mc13xxx_unlock(priv->mc13xxx);
149 	return ret;
150 }
151 
152 static void mc13783_ts_close(struct input_dev *dev)
153 {
154 	struct mc13783_ts_priv *priv = input_get_drvdata(dev);
155 
156 	mc13xxx_lock(priv->mc13xxx);
157 	mc13xxx_reg_rmw(priv->mc13xxx, MC13XXX_ADC0,
158 			MC13XXX_ADC0_TSMOD_MASK, 0);
159 	mc13xxx_irq_free(priv->mc13xxx, MC13XXX_IRQ_TS, priv);
160 	mc13xxx_unlock(priv->mc13xxx);
161 
162 	cancel_delayed_work_sync(&priv->work);
163 }
164 
165 static int __init mc13783_ts_probe(struct platform_device *pdev)
166 {
167 	struct mc13783_ts_priv *priv;
168 	struct input_dev *idev;
169 	int ret = -ENOMEM;
170 
171 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
172 	idev = input_allocate_device();
173 	if (!priv || !idev)
174 		goto err_free_mem;
175 
176 	INIT_DELAYED_WORK(&priv->work, mc13783_ts_work);
177 	priv->mc13xxx = dev_get_drvdata(pdev->dev.parent);
178 	priv->idev = idev;
179 	priv->touch = dev_get_platdata(&pdev->dev);
180 	if (!priv->touch) {
181 		dev_err(&pdev->dev, "missing platform data\n");
182 		ret = -ENODEV;
183 		goto err_free_mem;
184 	}
185 
186 	idev->name = MC13783_TS_NAME;
187 	idev->dev.parent = &pdev->dev;
188 
189 	idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
190 	idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
191 	input_set_abs_params(idev, ABS_X, 0, 0xfff, 0, 0);
192 	input_set_abs_params(idev, ABS_Y, 0, 0xfff, 0, 0);
193 	input_set_abs_params(idev, ABS_PRESSURE, 0, 0xfff, 0, 0);
194 
195 	idev->open = mc13783_ts_open;
196 	idev->close = mc13783_ts_close;
197 
198 	input_set_drvdata(idev, priv);
199 
200 	ret = input_register_device(priv->idev);
201 	if (ret) {
202 		dev_err(&pdev->dev,
203 			"register input device failed with %d\n", ret);
204 		goto err_free_mem;
205 	}
206 
207 	platform_set_drvdata(pdev, priv);
208 	return 0;
209 
210 err_free_mem:
211 	input_free_device(idev);
212 	kfree(priv);
213 	return ret;
214 }
215 
216 static void mc13783_ts_remove(struct platform_device *pdev)
217 {
218 	struct mc13783_ts_priv *priv = platform_get_drvdata(pdev);
219 
220 	input_unregister_device(priv->idev);
221 	kfree(priv);
222 }
223 
224 static struct platform_driver mc13783_ts_driver = {
225 	.remove		= mc13783_ts_remove,
226 	.driver		= {
227 		.name	= MC13783_TS_NAME,
228 	},
229 };
230 
231 module_platform_driver_probe(mc13783_ts_driver, mc13783_ts_probe);
232 
233 MODULE_DESCRIPTION("MC13783 input touchscreen driver");
234 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
235 MODULE_LICENSE("GPL v2");
236 MODULE_ALIAS("platform:" MC13783_TS_NAME);
237