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.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/iio/consumer.h>
15 #include <linux/iio/types.h>
16 #include <linux/input.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
26 #define DRIVER_NAME "colibri-vf50-ts"
28 #define VF_ADC_MAX ((1 << 12) - 1)
30 #define COLI_TOUCH_MIN_DELAY_US 1000
31 #define COLI_TOUCH_MAX_DELAY_US 2000
32 #define COLI_PULLUP_MIN_DELAY_US 10000
33 #define COLI_PULLUP_MAX_DELAY_US 11000
34 #define COLI_TOUCH_NO_OF_AVGS 5
35 #define COLI_TOUCH_REQ_ADC_CHAN 4
37 struct vf50_touch_device
{
38 struct platform_device
*pdev
;
39 struct input_dev
*ts_input
;
40 struct iio_channel
*channels
;
41 struct gpio_desc
*gpio_xp
;
42 struct gpio_desc
*gpio_xm
;
43 struct gpio_desc
*gpio_yp
;
44 struct gpio_desc
*gpio_ym
;
47 bool stop_touchscreen
;
51 * Enables given plates and measures touch parameters using ADC
53 static int adc_ts_measure(struct iio_channel
*channel
,
54 struct gpio_desc
*plate_p
, struct gpio_desc
*plate_m
)
56 int i
, value
= 0, val
= 0;
59 gpiod_set_value(plate_p
, 1);
60 gpiod_set_value(plate_m
, 1);
62 usleep_range(COLI_TOUCH_MIN_DELAY_US
, COLI_TOUCH_MAX_DELAY_US
);
64 for (i
= 0; i
< COLI_TOUCH_NO_OF_AVGS
; i
++) {
65 error
= iio_read_channel_raw(channel
, &val
);
74 value
/= COLI_TOUCH_NO_OF_AVGS
;
77 gpiod_set_value(plate_p
, 0);
78 gpiod_set_value(plate_m
, 0);
84 * Enable touch detection using falling edge detection on XM
86 static void vf50_ts_enable_touch_detection(struct vf50_touch_device
*vf50_ts
)
88 /* Enable plate YM (needs to be strong GND, high active) */
89 gpiod_set_value(vf50_ts
->gpio_ym
, 1);
92 * Let the platform mux to idle state in order to enable
95 pinctrl_pm_select_idle_state(&vf50_ts
->pdev
->dev
);
97 /* Wait for the pull-up to be stable on high */
98 usleep_range(COLI_PULLUP_MIN_DELAY_US
, COLI_PULLUP_MAX_DELAY_US
);
102 * ADC touch screen sampling bottom half irq handler
104 static irqreturn_t
vf50_ts_irq_bh(int irq
, void *private)
106 struct vf50_touch_device
*vf50_ts
= private;
107 struct device
*dev
= &vf50_ts
->pdev
->dev
;
108 int val_x
, val_y
, val_z1
, val_z2
, val_p
= 0;
109 bool discard_val_on_start
= true;
111 /* Disable the touch detection plates */
112 gpiod_set_value(vf50_ts
->gpio_ym
, 0);
114 /* Let the platform mux to default state in order to mux as ADC */
115 pinctrl_pm_select_default_state(dev
);
117 while (!vf50_ts
->stop_touchscreen
) {
119 val_x
= adc_ts_measure(&vf50_ts
->channels
[0],
120 vf50_ts
->gpio_xp
, vf50_ts
->gpio_xm
);
125 val_y
= adc_ts_measure(&vf50_ts
->channels
[1],
126 vf50_ts
->gpio_yp
, vf50_ts
->gpio_ym
);
134 val_z1
= adc_ts_measure(&vf50_ts
->channels
[2],
135 vf50_ts
->gpio_yp
, vf50_ts
->gpio_xm
);
138 val_z2
= adc_ts_measure(&vf50_ts
->channels
[3],
139 vf50_ts
->gpio_yp
, vf50_ts
->gpio_xm
);
143 /* Validate signal (avoid calculation using noise) */
144 if (val_z1
> 64 && val_x
> 64) {
146 * Calculate resistance between the plates
147 * lower resistance means higher pressure
149 int r_x
= (1000 * val_x
) / VF_ADC_MAX
;
151 val_p
= (r_x
* val_z2
) / val_z1
- r_x
;
157 val_p
= 2000 - val_p
;
159 "Measured values: x: %d, y: %d, z1: %d, z2: %d, p: %d\n",
160 val_x
, val_y
, val_z1
, val_z2
, val_p
);
163 * If touch pressure is too low, stop measuring and reenable
166 if (val_p
< vf50_ts
->min_pressure
|| val_p
> 2000)
170 * The pressure may not be enough for the first x and the
171 * second y measurement, but, the pressure is ok when the
172 * driver is doing the third and fourth measurement. To
173 * take care of this, we drop the first measurement always.
175 if (discard_val_on_start
) {
176 discard_val_on_start
= false;
179 * Report touch position and sleep for
180 * the next measurement.
182 input_report_abs(vf50_ts
->ts_input
,
183 ABS_X
, VF_ADC_MAX
- val_x
);
184 input_report_abs(vf50_ts
->ts_input
,
185 ABS_Y
, VF_ADC_MAX
- val_y
);
186 input_report_abs(vf50_ts
->ts_input
,
187 ABS_PRESSURE
, val_p
);
188 input_report_key(vf50_ts
->ts_input
, BTN_TOUCH
, 1);
189 input_sync(vf50_ts
->ts_input
);
192 usleep_range(COLI_PULLUP_MIN_DELAY_US
,
193 COLI_PULLUP_MAX_DELAY_US
);
196 /* Report no more touch, re-enable touch detection */
197 input_report_abs(vf50_ts
->ts_input
, ABS_PRESSURE
, 0);
198 input_report_key(vf50_ts
->ts_input
, BTN_TOUCH
, 0);
199 input_sync(vf50_ts
->ts_input
);
201 vf50_ts_enable_touch_detection(vf50_ts
);
206 static int vf50_ts_open(struct input_dev
*dev_input
)
208 struct vf50_touch_device
*touchdev
= input_get_drvdata(dev_input
);
209 struct device
*dev
= &touchdev
->pdev
->dev
;
211 dev_dbg(dev
, "Input device %s opened, starting touch detection\n",
214 touchdev
->stop_touchscreen
= false;
216 /* Mux detection before request IRQ, wait for pull-up to settle */
217 vf50_ts_enable_touch_detection(touchdev
);
222 static void vf50_ts_close(struct input_dev
*dev_input
)
224 struct vf50_touch_device
*touchdev
= input_get_drvdata(dev_input
);
225 struct device
*dev
= &touchdev
->pdev
->dev
;
227 touchdev
->stop_touchscreen
= true;
229 /* Make sure IRQ is not running past close */
231 synchronize_irq(touchdev
->pen_irq
);
233 gpiod_set_value(touchdev
->gpio_ym
, 0);
234 pinctrl_pm_select_default_state(dev
);
236 dev_dbg(dev
, "Input device %s closed, disable touch detection\n",
240 static int vf50_ts_get_gpiod(struct device
*dev
, struct gpio_desc
**gpio_d
,
241 const char *con_id
, enum gpiod_flags flags
)
245 *gpio_d
= devm_gpiod_get(dev
, con_id
, flags
);
246 if (IS_ERR(*gpio_d
)) {
247 error
= PTR_ERR(*gpio_d
);
248 dev_err(dev
, "Could not get gpio_%s %d\n", con_id
, error
);
255 static void vf50_ts_channel_release(void *data
)
257 struct iio_channel
*channels
= data
;
259 iio_channel_release_all(channels
);
262 static int vf50_ts_probe(struct platform_device
*pdev
)
264 struct input_dev
*input
;
265 struct iio_channel
*channels
;
266 struct device
*dev
= &pdev
->dev
;
267 struct vf50_touch_device
*touchdev
;
268 int num_adc_channels
;
271 channels
= iio_channel_get_all(dev
);
272 if (IS_ERR(channels
))
273 return PTR_ERR(channels
);
275 error
= devm_add_action(dev
, vf50_ts_channel_release
, channels
);
277 iio_channel_release_all(channels
);
278 dev_err(dev
, "Failed to register iio channel release action");
282 num_adc_channels
= 0;
283 while (channels
[num_adc_channels
].indio_dev
)
286 if (num_adc_channels
!= COLI_TOUCH_REQ_ADC_CHAN
) {
287 dev_err(dev
, "Inadequate ADC channels specified\n");
291 touchdev
= devm_kzalloc(dev
, sizeof(*touchdev
), GFP_KERNEL
);
295 touchdev
->pdev
= pdev
;
296 touchdev
->channels
= channels
;
298 error
= of_property_read_u32(dev
->of_node
, "vf50-ts-min-pressure",
299 &touchdev
->min_pressure
);
303 input
= devm_input_allocate_device(dev
);
305 dev_err(dev
, "Failed to allocate TS input device\n");
309 input
->name
= DRIVER_NAME
;
310 input
->id
.bustype
= BUS_HOST
;
311 input
->dev
.parent
= dev
;
312 input
->open
= vf50_ts_open
;
313 input
->close
= vf50_ts_close
;
315 input_set_capability(input
, EV_KEY
, BTN_TOUCH
);
316 input_set_abs_params(input
, ABS_X
, 0, VF_ADC_MAX
, 0, 0);
317 input_set_abs_params(input
, ABS_Y
, 0, VF_ADC_MAX
, 0, 0);
318 input_set_abs_params(input
, ABS_PRESSURE
, 0, VF_ADC_MAX
, 0, 0);
320 touchdev
->ts_input
= input
;
321 input_set_drvdata(input
, touchdev
);
323 error
= input_register_device(input
);
325 dev_err(dev
, "Failed to register input device\n");
329 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_xp
, "xp", GPIOD_OUT_LOW
);
333 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_xm
,
334 "xm", GPIOD_OUT_LOW
);
338 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_yp
, "yp", GPIOD_OUT_LOW
);
342 error
= vf50_ts_get_gpiod(dev
, &touchdev
->gpio_ym
, "ym", GPIOD_OUT_LOW
);
346 touchdev
->pen_irq
= platform_get_irq(pdev
, 0);
347 if (touchdev
->pen_irq
< 0)
348 return touchdev
->pen_irq
;
350 error
= devm_request_threaded_irq(dev
, touchdev
->pen_irq
,
351 NULL
, vf50_ts_irq_bh
, IRQF_ONESHOT
,
352 "vf50 touch", touchdev
);
354 dev_err(dev
, "Failed to request IRQ %d: %d\n",
355 touchdev
->pen_irq
, error
);
362 static const struct of_device_id vf50_touch_of_match
[] = {
363 { .compatible
= "toradex,vf50-touchscreen", },
366 MODULE_DEVICE_TABLE(of
, vf50_touch_of_match
);
368 static struct platform_driver vf50_touch_driver
= {
370 .name
= "toradex,vf50_touchctrl",
371 .of_match_table
= vf50_touch_of_match
,
373 .probe
= vf50_ts_probe
,
375 module_platform_driver(vf50_touch_driver
);
377 MODULE_AUTHOR("Sanchayan Maity");
378 MODULE_DESCRIPTION("Colibri VF50 Touchscreen driver");
379 MODULE_LICENSE("GPL");