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