1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Toradex Colibri VF50 Touchscreen driver
5 * Copyright 2015 Toradex AG
7 * Originally authored by Stefan Agner for 3.0 kernel
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/iio/consumer.h>
14 #include <linux/iio/types.h>
15 #include <linux/input.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
25 #define DRIVER_NAME "colibri-vf50-ts"
27 #define VF_ADC_MAX ((1 << 12) - 1)
29 #define COLI_TOUCH_MIN_DELAY_US 1000
30 #define COLI_TOUCH_MAX_DELAY_US 2000
31 #define COLI_PULLUP_MIN_DELAY_US 10000
32 #define COLI_PULLUP_MAX_DELAY_US 11000
33 #define COLI_TOUCH_NO_OF_AVGS 5
34 #define COLI_TOUCH_REQ_ADC_CHAN 4
36 struct vf50_touch_device
{
37 struct platform_device
*pdev
;
38 struct input_dev
*ts_input
;
39 struct iio_channel
*channels
;
40 struct gpio_desc
*gpio_xp
;
41 struct gpio_desc
*gpio_xm
;
42 struct gpio_desc
*gpio_yp
;
43 struct gpio_desc
*gpio_ym
;
46 bool stop_touchscreen
;
50 * Enables given plates and measures touch parameters using ADC
52 static int adc_ts_measure(struct iio_channel
*channel
,
53 struct gpio_desc
*plate_p
, struct gpio_desc
*plate_m
)
55 int i
, value
= 0, val
= 0;
58 gpiod_set_value(plate_p
, 1);
59 gpiod_set_value(plate_m
, 1);
61 usleep_range(COLI_TOUCH_MIN_DELAY_US
, COLI_TOUCH_MAX_DELAY_US
);
63 for (i
= 0; i
< COLI_TOUCH_NO_OF_AVGS
; i
++) {
64 error
= iio_read_channel_raw(channel
, &val
);
73 value
/= COLI_TOUCH_NO_OF_AVGS
;
76 gpiod_set_value(plate_p
, 0);
77 gpiod_set_value(plate_m
, 0);
83 * Enable touch detection using falling edge detection on XM
85 static void vf50_ts_enable_touch_detection(struct vf50_touch_device
*vf50_ts
)
87 /* Enable plate YM (needs to be strong GND, high active) */
88 gpiod_set_value(vf50_ts
->gpio_ym
, 1);
91 * Let the platform mux to idle state in order to enable
94 pinctrl_pm_select_idle_state(&vf50_ts
->pdev
->dev
);
96 /* Wait for the pull-up to be stable on high */
97 usleep_range(COLI_PULLUP_MIN_DELAY_US
, COLI_PULLUP_MAX_DELAY_US
);
101 * ADC touch screen sampling bottom half irq handler
103 static irqreturn_t
vf50_ts_irq_bh(int irq
, void *private)
105 struct vf50_touch_device
*vf50_ts
= private;
106 struct device
*dev
= &vf50_ts
->pdev
->dev
;
107 int val_x
, val_y
, val_z1
, val_z2
, val_p
= 0;
108 bool discard_val_on_start
= true;
110 /* Disable the touch detection plates */
111 gpiod_set_value(vf50_ts
->gpio_ym
, 0);
113 /* Let the platform mux to default state in order to mux as ADC */
114 pinctrl_pm_select_default_state(dev
);
116 while (!vf50_ts
->stop_touchscreen
) {
118 val_x
= adc_ts_measure(&vf50_ts
->channels
[0],
119 vf50_ts
->gpio_xp
, vf50_ts
->gpio_xm
);
124 val_y
= adc_ts_measure(&vf50_ts
->channels
[1],
125 vf50_ts
->gpio_yp
, vf50_ts
->gpio_ym
);
133 val_z1
= adc_ts_measure(&vf50_ts
->channels
[2],
134 vf50_ts
->gpio_yp
, vf50_ts
->gpio_xm
);
137 val_z2
= adc_ts_measure(&vf50_ts
->channels
[3],
138 vf50_ts
->gpio_yp
, vf50_ts
->gpio_xm
);
142 /* Validate signal (avoid calculation using noise) */
143 if (val_z1
> 64 && val_x
> 64) {
145 * Calculate resistance between the plates
146 * lower resistance means higher pressure
148 int r_x
= (1000 * val_x
) / VF_ADC_MAX
;
150 val_p
= (r_x
* val_z2
) / val_z1
- r_x
;
156 val_p
= 2000 - val_p
;
158 "Measured values: x: %d, y: %d, z1: %d, z2: %d, p: %d\n",
159 val_x
, val_y
, val_z1
, val_z2
, val_p
);
162 * If touch pressure is too low, stop measuring and reenable
165 if (val_p
< vf50_ts
->min_pressure
|| val_p
> 2000)
169 * The pressure may not be enough for the first x and the
170 * second y measurement, but, the pressure is ok when the
171 * driver is doing the third and fourth measurement. To
172 * take care of this, we drop the first measurement always.
174 if (discard_val_on_start
) {
175 discard_val_on_start
= false;
178 * Report touch position and sleep for
179 * the next measurement.
181 input_report_abs(vf50_ts
->ts_input
,
182 ABS_X
, VF_ADC_MAX
- val_x
);
183 input_report_abs(vf50_ts
->ts_input
,
184 ABS_Y
, VF_ADC_MAX
- val_y
);
185 input_report_abs(vf50_ts
->ts_input
,
186 ABS_PRESSURE
, val_p
);
187 input_report_key(vf50_ts
->ts_input
, BTN_TOUCH
, 1);
188 input_sync(vf50_ts
->ts_input
);
191 usleep_range(COLI_PULLUP_MIN_DELAY_US
,
192 COLI_PULLUP_MAX_DELAY_US
);
195 /* Report no more touch, re-enable touch detection */
196 input_report_abs(vf50_ts
->ts_input
, ABS_PRESSURE
, 0);
197 input_report_key(vf50_ts
->ts_input
, BTN_TOUCH
, 0);
198 input_sync(vf50_ts
->ts_input
);
200 vf50_ts_enable_touch_detection(vf50_ts
);
205 static int vf50_ts_open(struct input_dev
*dev_input
)
207 struct vf50_touch_device
*touchdev
= input_get_drvdata(dev_input
);
208 struct device
*dev
= &touchdev
->pdev
->dev
;
210 dev_dbg(dev
, "Input device %s opened, starting touch detection\n",
213 touchdev
->stop_touchscreen
= false;
215 /* Mux detection before request IRQ, wait for pull-up to settle */
216 vf50_ts_enable_touch_detection(touchdev
);
221 static void vf50_ts_close(struct input_dev
*dev_input
)
223 struct vf50_touch_device
*touchdev
= input_get_drvdata(dev_input
);
224 struct device
*dev
= &touchdev
->pdev
->dev
;
226 touchdev
->stop_touchscreen
= true;
228 /* Make sure IRQ is not running past close */
230 synchronize_irq(touchdev
->pen_irq
);
232 gpiod_set_value(touchdev
->gpio_ym
, 0);
233 pinctrl_pm_select_default_state(dev
);
235 dev_dbg(dev
, "Input device %s closed, disable touch detection\n",
239 static int vf50_ts_get_gpiod(struct device
*dev
, struct gpio_desc
**gpio_d
,
240 const char *con_id
, enum gpiod_flags flags
)
242 *gpio_d
= devm_gpiod_get(dev
, con_id
, flags
);
244 return dev_err_probe(dev
, PTR_ERR(*gpio_d
),
245 "Could not get gpio_%s\n", con_id
);
250 static void vf50_ts_channel_release(void *data
)
252 struct iio_channel
*channels
= data
;
254 iio_channel_release_all(channels
);
257 static int vf50_ts_probe(struct platform_device
*pdev
)
259 struct input_dev
*input
;
260 struct iio_channel
*channels
;
261 struct device
*dev
= &pdev
->dev
;
262 struct vf50_touch_device
*touchdev
;
263 int num_adc_channels
;
266 channels
= iio_channel_get_all(dev
);
267 if (IS_ERR(channels
))
268 return PTR_ERR(channels
);
270 error
= devm_add_action(dev
, vf50_ts_channel_release
, channels
);
272 iio_channel_release_all(channels
);
273 dev_err(dev
, "Failed to register iio channel release action");
277 num_adc_channels
= 0;
278 while (channels
[num_adc_channels
].indio_dev
)
281 if (num_adc_channels
!= COLI_TOUCH_REQ_ADC_CHAN
) {
282 dev_err(dev
, "Inadequate ADC channels specified\n");
286 touchdev
= devm_kzalloc(dev
, sizeof(*touchdev
), GFP_KERNEL
);
290 touchdev
->pdev
= pdev
;
291 touchdev
->channels
= channels
;
293 error
= of_property_read_u32(dev
->of_node
, "vf50-ts-min-pressure",
294 &touchdev
->min_pressure
);
298 input
= devm_input_allocate_device(dev
);
300 dev_err(dev
, "Failed to allocate TS input device\n");
304 input
->name
= DRIVER_NAME
;
305 input
->id
.bustype
= BUS_HOST
;
306 input
->dev
.parent
= dev
;
307 input
->open
= vf50_ts_open
;
308 input
->close
= vf50_ts_close
;
310 input_set_capability(input
, EV_KEY
, BTN_TOUCH
);
311 input_set_abs_params(input
, ABS_X
, 0, VF_ADC_MAX
, 0, 0);
312 input_set_abs_params(input
, ABS_Y
, 0, VF_ADC_MAX
, 0, 0);
313 input_set_abs_params(input
, ABS_PRESSURE
, 0, VF_ADC_MAX
, 0, 0);
315 touchdev
->ts_input
= input
;
316 input_set_drvdata(input
, touchdev
);
318 error
= input_register_device(input
);
320 dev_err(dev
, "Failed to register input device\n");
324 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_xp
, "xp", GPIOD_OUT_LOW
);
328 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_xm
,
329 "xm", GPIOD_OUT_LOW
);
333 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_yp
, "yp", GPIOD_OUT_LOW
);
337 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_ym
, "ym", GPIOD_OUT_LOW
);
341 touchdev
->pen_irq
= platform_get_irq(pdev
, 0);
342 if (touchdev
->pen_irq
< 0)
343 return touchdev
->pen_irq
;
345 error
= devm_request_threaded_irq(dev
, touchdev
->pen_irq
,
346 NULL
, vf50_ts_irq_bh
, IRQF_ONESHOT
,
347 "vf50 touch", touchdev
);
349 dev_err(dev
, "Failed to request IRQ %d: %d\n",
350 touchdev
->pen_irq
, error
);
357 static const struct of_device_id vf50_touch_of_match
[] = {
358 { .compatible
= "toradex,vf50-touchscreen", },
361 MODULE_DEVICE_TABLE(of
, vf50_touch_of_match
);
363 static struct platform_driver vf50_touch_driver
= {
365 .name
= "toradex,vf50_touchctrl",
366 .of_match_table
= vf50_touch_of_match
,
368 .probe
= vf50_ts_probe
,
370 module_platform_driver(vf50_touch_driver
);
372 MODULE_AUTHOR("Sanchayan Maity");
373 MODULE_DESCRIPTION("Colibri VF50 Touchscreen driver");
374 MODULE_LICENSE("GPL");