1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Freescale Semiconductor MC13783 touchscreen.
5 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
6 * Copyright (C) 2009 Sascha Hauer, Pengutronix
8 * Initial development of this code was funded by
9 * Phytec Messtechnik GmbH, http://www.phytec.de/
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>
20 #define MC13783_TS_NAME "mc13783-ts"
22 #define DEFAULT_SAMPLE_TOLERANCE 300
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.");
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
;
41 static irqreturn_t
mc13783_ts_handler(int irq
, void *data
)
43 struct mc13783_ts_priv
*priv
= data
;
45 mc13xxx_irq_ack(priv
->mc13xxx
, irq
);
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.
53 schedule_delayed_work(&priv
->work
, 0);
58 #define sort3(a0, a1, a2) ({ \
67 static void mc13783_ts_report_sample(struct mc13783_ts_priv
*priv
)
69 struct input_dev
*idev
= priv
->idev
;
70 int x0
, x1
, x2
, y0
, y1
, y2
;
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
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;
87 "x: (% 4d,% 4d,% 4d) y: (% 4d, % 4d,% 4d) cr: (% 4d, % 4d)\n",
88 x0
, x1
, x2
, y0
, y1
, y2
, cr0
, cr1
);
93 cr0
= (cr0
+ cr1
) / 2;
95 if (!cr0
|| !sample_tolerance
||
96 (x2
- x0
< sample_tolerance
&&
97 y2
- y0
< sample_tolerance
)) {
98 /* report the median coordinate and average pressure */
100 input_report_abs(idev
, ABS_X
, x1
);
101 input_report_abs(idev
, ABS_Y
, y1
);
103 dev_dbg(&idev
->dev
, "report (%d, %d, %d)\n",
104 x1
, y1
, 0x1000 - cr0
);
105 schedule_delayed_work(&priv
->work
, HZ
/ 50);
107 dev_dbg(&idev
->dev
, "report release\n");
110 input_report_abs(idev
, ABS_PRESSURE
,
111 cr0
? 0x1000 - cr0
: cr0
);
112 input_report_key(idev
, BTN_TOUCH
, cr0
);
115 dev_dbg(&idev
->dev
, "discard event\n");
119 static void mc13783_ts_work(struct work_struct
*work
)
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;
126 if (mc13xxx_adc_do_conversion(priv
->mc13xxx
,
128 priv
->touch
->ato
, priv
->touch
->atox
,
130 mc13783_ts_report_sample(priv
);
133 static int mc13783_ts_open(struct input_dev
*dev
)
135 struct mc13783_ts_priv
*priv
= input_get_drvdata(dev
);
138 mc13xxx_lock(priv
->mc13xxx
);
140 mc13xxx_irq_ack(priv
->mc13xxx
, MC13XXX_IRQ_TS
);
142 ret
= mc13xxx_irq_request(priv
->mc13xxx
, MC13XXX_IRQ_TS
,
143 mc13783_ts_handler
, MC13783_TS_NAME
, priv
);
147 ret
= mc13xxx_reg_rmw(priv
->mc13xxx
, MC13XXX_ADC0
,
148 MC13XXX_ADC0_TSMOD_MASK
, MC13XXX_ADC0_TSMOD0
);
150 mc13xxx_irq_free(priv
->mc13xxx
, MC13XXX_IRQ_TS
, priv
);
152 mc13xxx_unlock(priv
->mc13xxx
);
156 static void mc13783_ts_close(struct input_dev
*dev
)
158 struct mc13783_ts_priv
*priv
= input_get_drvdata(dev
);
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
);
166 cancel_delayed_work_sync(&priv
->work
);
169 static int __init
mc13783_ts_probe(struct platform_device
*pdev
)
171 struct mc13783_ts_priv
*priv
;
172 struct input_dev
*idev
;
175 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
176 idev
= input_allocate_device();
180 INIT_DELAYED_WORK(&priv
->work
, mc13783_ts_work
);
181 priv
->mc13xxx
= dev_get_drvdata(pdev
->dev
.parent
);
183 priv
->touch
= dev_get_platdata(&pdev
->dev
);
185 dev_err(&pdev
->dev
, "missing platform data\n");
190 idev
->name
= MC13783_TS_NAME
;
191 idev
->dev
.parent
= &pdev
->dev
;
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);
199 idev
->open
= mc13783_ts_open
;
200 idev
->close
= mc13783_ts_close
;
202 input_set_drvdata(idev
, priv
);
204 ret
= input_register_device(priv
->idev
);
207 "register input device failed with %d\n", ret
);
211 platform_set_drvdata(pdev
, priv
);
215 input_free_device(idev
);
220 static void mc13783_ts_remove(struct platform_device
*pdev
)
222 struct mc13783_ts_priv
*priv
= platform_get_drvdata(pdev
);
224 input_unregister_device(priv
->idev
);
228 static struct platform_driver mc13783_ts_driver
= {
229 .remove
= mc13783_ts_remove
,
231 .name
= MC13783_TS_NAME
,
235 module_platform_driver_probe(mc13783_ts_driver
, mc13783_ts_probe
);
237 MODULE_DESCRIPTION("MC13783 input touchscreen driver");
238 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
239 MODULE_LICENSE("GPL v2");
240 MODULE_ALIAS("platform:" MC13783_TS_NAME
);