^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ADC generic resistive touchscreen (GRTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This is a generic input driver that connects to an ADC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * given the channels in device tree, and reports events to the input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * subsystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2017,2018 Microchip Technology,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author: Eugen Hristev <eugen.hristev@microchip.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/input/touchscreen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DRIVER_NAME "resistive-adc-touch"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define GRTS_DEFAULT_PRESSURE_MIN 50000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define GRTS_MAX_POS_MASK GENMASK(11, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * grts_state - generic resistive touch screen information struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @pressure_min: number representing the minimum for the pressure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * @pressure: are we getting pressure info or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @iio_chans: list of channels acquired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @iio_cb: iio_callback buffer for the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @input: the input device structure that we register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * @prop: touchscreen properties struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct grts_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) u32 pressure_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) bool pressure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct iio_channel *iio_chans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct iio_cb_buffer *iio_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct touchscreen_properties prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int grts_cb(const void *data, void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) const u16 *touch_info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct grts_state *st = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned int x, y, press = 0x0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* channel data coming in buffer in the order below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) x = touch_info[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) y = touch_info[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (st->pressure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) press = touch_info[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if ((!x && !y) || (st->pressure && (press < st->pressure_min))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* report end of touch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) input_report_key(st->input, BTN_TOUCH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) input_sync(st->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* report proper touch to subsystem*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) touchscreen_report_pos(st->input, &st->prop, x, y, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (st->pressure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) input_report_abs(st->input, ABS_PRESSURE, press);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) input_report_key(st->input, BTN_TOUCH, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) input_sync(st->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int grts_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct grts_state *st = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) error = iio_channel_start_all_cb(st->iio_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) dev_err(dev->dev.parent, "failed to start callback buffer.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void grts_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct grts_state *st = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) iio_channel_stop_all_cb(st->iio_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static void grts_disable(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) iio_channel_release_all_cb(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int grts_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct grts_state *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct iio_channel *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) st = devm_kzalloc(dev, sizeof(struct grts_state), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* get the channels from IIO device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) st->iio_chans = devm_iio_channel_get_all(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (IS_ERR(st->iio_chans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) error = PTR_ERR(st->iio_chans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (error != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dev_err(dev, "can't get iio channels.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) chan = &st->iio_chans[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) st->pressure = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) while (chan && chan->indio_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!strcmp(chan->channel->datasheet_name, "pressure"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) st->pressure = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) chan++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (st->pressure) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) error = device_property_read_u32(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "touchscreen-min-pressure",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) &st->pressure_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dev_dbg(dev, "can't get touchscreen-min-pressure property.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) st->pressure_min = GRTS_DEFAULT_PRESSURE_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) input = devm_input_allocate_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) dev_err(dev, "failed to allocate input device.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) input->name = DRIVER_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) input->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) input->open = grts_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) input->close = grts_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) input_set_abs_params(input, ABS_X, 0, GRTS_MAX_POS_MASK - 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) input_set_abs_params(input, ABS_Y, 0, GRTS_MAX_POS_MASK - 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (st->pressure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) input_set_abs_params(input, ABS_PRESSURE, st->pressure_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 0xffff, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) input_set_capability(input, EV_KEY, BTN_TOUCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* parse optional device tree properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) touchscreen_parse_properties(input, false, &st->prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) st->input = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) input_set_drvdata(input, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) error = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) dev_err(dev, "failed to register input device.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) st->iio_cb = iio_channel_get_all_cb(dev, grts_cb, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (IS_ERR(st->iio_cb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_err(dev, "failed to allocate callback buffer.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return PTR_ERR(st->iio_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) error = devm_add_action_or_reset(dev, grts_disable, st->iio_cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) dev_err(dev, "failed to add disable action.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static const struct of_device_id grts_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .compatible = "resistive-adc-touch",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* sentinel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) MODULE_DEVICE_TABLE(of, grts_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static struct platform_driver grts_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .probe = grts_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .name = DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .of_match_table = of_match_ptr(grts_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) module_platform_driver(grts_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) MODULE_DESCRIPTION("Generic ADC Resistive Touch Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_LICENSE("GPL v2");