^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Toradex Colibri VF50 Touchscreen driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2015 Toradex AG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Originally authored by Stefan Agner for 3.0 kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/iio/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/pinctrl/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define DRIVER_NAME "colibri-vf50-ts"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define VF_ADC_MAX ((1 << 12) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define COLI_TOUCH_MIN_DELAY_US 1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define COLI_TOUCH_MAX_DELAY_US 2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define COLI_PULLUP_MIN_DELAY_US 10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define COLI_PULLUP_MAX_DELAY_US 11000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define COLI_TOUCH_NO_OF_AVGS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define COLI_TOUCH_REQ_ADC_CHAN 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct vf50_touch_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct input_dev *ts_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct iio_channel *channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct gpio_desc *gpio_xp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct gpio_desc *gpio_xm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct gpio_desc *gpio_yp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct gpio_desc *gpio_ym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int pen_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int min_pressure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) bool stop_touchscreen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Enables given plates and measures touch parameters using ADC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int adc_ts_measure(struct iio_channel *channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct gpio_desc *plate_p, struct gpio_desc *plate_m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int i, value = 0, val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) gpiod_set_value(plate_p, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) gpiod_set_value(plate_m, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) usleep_range(COLI_TOUCH_MIN_DELAY_US, COLI_TOUCH_MAX_DELAY_US);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) for (i = 0; i < COLI_TOUCH_NO_OF_AVGS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) error = iio_read_channel_raw(channel, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) value = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto error_iio_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) value += val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) value /= COLI_TOUCH_NO_OF_AVGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) error_iio_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) gpiod_set_value(plate_p, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) gpiod_set_value(plate_m, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Enable touch detection using falling edge detection on XM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void vf50_ts_enable_touch_detection(struct vf50_touch_device *vf50_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* Enable plate YM (needs to be strong GND, high active) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) gpiod_set_value(vf50_ts->gpio_ym, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Let the platform mux to idle state in order to enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Pull-Up on GPIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pinctrl_pm_select_idle_state(&vf50_ts->pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Wait for the pull-up to be stable on high */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) usleep_range(COLI_PULLUP_MIN_DELAY_US, COLI_PULLUP_MAX_DELAY_US);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * ADC touch screen sampling bottom half irq handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static irqreturn_t vf50_ts_irq_bh(int irq, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct vf50_touch_device *vf50_ts = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct device *dev = &vf50_ts->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int val_x, val_y, val_z1, val_z2, val_p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) bool discard_val_on_start = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Disable the touch detection plates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) gpiod_set_value(vf50_ts->gpio_ym, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Let the platform mux to default state in order to mux as ADC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pinctrl_pm_select_default_state(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) while (!vf50_ts->stop_touchscreen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* X-Direction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) val_x = adc_ts_measure(&vf50_ts->channels[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) vf50_ts->gpio_xp, vf50_ts->gpio_xm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (val_x < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Y-Direction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) val_y = adc_ts_measure(&vf50_ts->channels[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) vf50_ts->gpio_yp, vf50_ts->gpio_ym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (val_y < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * Touch pressure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * Measure on XP/YM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) val_z1 = adc_ts_measure(&vf50_ts->channels[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) vf50_ts->gpio_yp, vf50_ts->gpio_xm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (val_z1 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) val_z2 = adc_ts_measure(&vf50_ts->channels[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) vf50_ts->gpio_yp, vf50_ts->gpio_xm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (val_z2 < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* Validate signal (avoid calculation using noise) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (val_z1 > 64 && val_x > 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Calculate resistance between the plates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * lower resistance means higher pressure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int r_x = (1000 * val_x) / VF_ADC_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) val_p = (r_x * val_z2) / val_z1 - r_x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) val_p = 2000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) val_p = 2000 - val_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) dev_dbg(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) "Measured values: x: %d, y: %d, z1: %d, z2: %d, p: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) val_x, val_y, val_z1, val_z2, val_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * If touch pressure is too low, stop measuring and reenable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * touch detection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (val_p < vf50_ts->min_pressure || val_p > 2000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * The pressure may not be enough for the first x and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * second y measurement, but, the pressure is ok when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * driver is doing the third and fourth measurement. To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * take care of this, we drop the first measurement always.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (discard_val_on_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) discard_val_on_start = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * Report touch position and sleep for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * the next measurement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) input_report_abs(vf50_ts->ts_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ABS_X, VF_ADC_MAX - val_x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) input_report_abs(vf50_ts->ts_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ABS_Y, VF_ADC_MAX - val_y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) input_report_abs(vf50_ts->ts_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) ABS_PRESSURE, val_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) input_report_key(vf50_ts->ts_input, BTN_TOUCH, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) input_sync(vf50_ts->ts_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) usleep_range(COLI_PULLUP_MIN_DELAY_US,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) COLI_PULLUP_MAX_DELAY_US);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* Report no more touch, re-enable touch detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) input_report_abs(vf50_ts->ts_input, ABS_PRESSURE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) input_report_key(vf50_ts->ts_input, BTN_TOUCH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) input_sync(vf50_ts->ts_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) vf50_ts_enable_touch_detection(vf50_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int vf50_ts_open(struct input_dev *dev_input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct vf50_touch_device *touchdev = input_get_drvdata(dev_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct device *dev = &touchdev->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev_dbg(dev, "Input device %s opened, starting touch detection\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) dev_input->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) touchdev->stop_touchscreen = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* Mux detection before request IRQ, wait for pull-up to settle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) vf50_ts_enable_touch_detection(touchdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static void vf50_ts_close(struct input_dev *dev_input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct vf50_touch_device *touchdev = input_get_drvdata(dev_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct device *dev = &touchdev->pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) touchdev->stop_touchscreen = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* Make sure IRQ is not running past close */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) synchronize_irq(touchdev->pen_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) gpiod_set_value(touchdev->gpio_ym, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) pinctrl_pm_select_default_state(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) dev_dbg(dev, "Input device %s closed, disable touch detection\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) dev_input->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int vf50_ts_get_gpiod(struct device *dev, struct gpio_desc **gpio_d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) const char *con_id, enum gpiod_flags flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *gpio_d = devm_gpiod_get(dev, con_id, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (IS_ERR(*gpio_d)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) error = PTR_ERR(*gpio_d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) dev_err(dev, "Could not get gpio_%s %d\n", con_id, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static void vf50_ts_channel_release(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct iio_channel *channels = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) iio_channel_release_all(channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int vf50_ts_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct iio_channel *channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct vf50_touch_device *touchdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int num_adc_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) channels = iio_channel_get_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (IS_ERR(channels))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return PTR_ERR(channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) error = devm_add_action(dev, vf50_ts_channel_release, channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) iio_channel_release_all(channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) dev_err(dev, "Failed to register iio channel release action");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) num_adc_channels = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) while (channels[num_adc_channels].indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) num_adc_channels++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (num_adc_channels != COLI_TOUCH_REQ_ADC_CHAN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) dev_err(dev, "Inadequate ADC channels specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) touchdev = devm_kzalloc(dev, sizeof(*touchdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!touchdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) touchdev->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) touchdev->channels = channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) error = of_property_read_u32(dev->of_node, "vf50-ts-min-pressure",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) &touchdev->min_pressure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) input = devm_input_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) dev_err(dev, "Failed to allocate TS input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) input->name = DRIVER_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) input->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) input->dev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) input->open = vf50_ts_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) input->close = vf50_ts_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) input_set_capability(input, EV_KEY, BTN_TOUCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) input_set_abs_params(input, ABS_X, 0, VF_ADC_MAX, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) input_set_abs_params(input, ABS_Y, 0, VF_ADC_MAX, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) input_set_abs_params(input, ABS_PRESSURE, 0, VF_ADC_MAX, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) touchdev->ts_input = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) input_set_drvdata(input, touchdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) error = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) dev_err(dev, "Failed to register input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) error = vf50_ts_get_gpiod(dev, &touchdev->gpio_xp, "xp", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) error = vf50_ts_get_gpiod(dev, &touchdev->gpio_xm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) "xm", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) error = vf50_ts_get_gpiod(dev, &touchdev->gpio_yp, "yp", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) error = vf50_ts_get_gpiod(dev, &touchdev->gpio_ym, "ym", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) touchdev->pen_irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (touchdev->pen_irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return touchdev->pen_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) error = devm_request_threaded_irq(dev, touchdev->pen_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) NULL, vf50_ts_irq_bh, IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) "vf50 touch", touchdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) dev_err(dev, "Failed to request IRQ %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) touchdev->pen_irq, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static const struct of_device_id vf50_touch_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) { .compatible = "toradex,vf50-touchscreen", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) MODULE_DEVICE_TABLE(of, vf50_touch_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static struct platform_driver vf50_touch_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) .name = "toradex,vf50_touchctrl",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .of_match_table = vf50_touch_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) .probe = vf50_ts_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) module_platform_driver(vf50_touch_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_AUTHOR("Sanchayan Maity");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) MODULE_DESCRIPTION("Colibri VF50 Touchscreen driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) MODULE_LICENSE("GPL");