1 // SPDX-License-Identifier: GPL-2.0
3 * ADC generic resistive touchscreen (GRTS)
4 * This is a generic input driver that connects to an ADC
5 * given the channels in device tree, and reports events to the input
8 * Copyright (C) 2017,2018 Microchip Technology,
12 #include <linux/input.h>
13 #include <linux/input/touchscreen.h>
14 #include <linux/iio/consumer.h>
15 #include <linux/iio/iio.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
21 #define DRIVER_NAME "resistive-adc-touch"
22 #define GRTS_DEFAULT_PRESSURE_MIN 50000
23 #define GRTS_MAX_POS_MASK GENMASK(11, 0)
26 * grts_state - generic resistive touch screen information struct
27 * @pressure_min: number representing the minimum for the pressure
28 * @pressure: are we getting pressure info or not
29 * @iio_chans: list of channels acquired
30 * @iio_cb: iio_callback buffer for the data
31 * @input: the input device structure that we register
32 * @prop: touchscreen properties struct
37 struct iio_channel *iio_chans;
38 struct iio_cb_buffer *iio_cb;
39 struct input_dev *input;
40 struct touchscreen_properties prop;
43 static int grts_cb(const void *data, void *private)
45 const u16 *touch_info = data;
46 struct grts_state *st = private;
47 unsigned int x, y, press = 0x0;
49 /* channel data coming in buffer in the order below */
53 press = touch_info[2];
55 if ((!x && !y) || (st->pressure && (press < st->pressure_min))) {
56 /* report end of touch */
57 input_report_key(st->input, BTN_TOUCH, 0);
58 input_sync(st->input);
62 /* report proper touch to subsystem*/
63 touchscreen_report_pos(st->input, &st->prop, x, y, false);
65 input_report_abs(st->input, ABS_PRESSURE, press);
66 input_report_key(st->input, BTN_TOUCH, 1);
67 input_sync(st->input);
72 static int grts_open(struct input_dev *dev)
75 struct grts_state *st = input_get_drvdata(dev);
77 error = iio_channel_start_all_cb(st->iio_cb);
79 dev_err(dev->dev.parent, "failed to start callback buffer.\n");
85 static void grts_close(struct input_dev *dev)
87 struct grts_state *st = input_get_drvdata(dev);
89 iio_channel_stop_all_cb(st->iio_cb);
92 static void grts_disable(void *data)
94 iio_channel_release_all_cb(data);
97 static int grts_probe(struct platform_device *pdev)
99 struct grts_state *st;
100 struct input_dev *input;
101 struct device *dev = &pdev->dev;
102 struct iio_channel *chan;
105 st = devm_kzalloc(dev, sizeof(struct grts_state), GFP_KERNEL);
109 /* get the channels from IIO device */
110 st->iio_chans = devm_iio_channel_get_all(dev);
111 if (IS_ERR(st->iio_chans)) {
112 error = PTR_ERR(st->iio_chans);
113 if (error != -EPROBE_DEFER)
114 dev_err(dev, "can't get iio channels.\n");
118 chan = &st->iio_chans[0];
119 st->pressure = false;
120 while (chan && chan->indio_dev) {
121 if (!strcmp(chan->channel->datasheet_name, "pressure"))
127 error = device_property_read_u32(dev,
128 "touchscreen-min-pressure",
131 dev_dbg(dev, "can't get touchscreen-min-pressure property.\n");
132 st->pressure_min = GRTS_DEFAULT_PRESSURE_MIN;
136 input = devm_input_allocate_device(dev);
138 dev_err(dev, "failed to allocate input device.\n");
142 input->name = DRIVER_NAME;
143 input->id.bustype = BUS_HOST;
144 input->open = grts_open;
145 input->close = grts_close;
147 input_set_abs_params(input, ABS_X, 0, GRTS_MAX_POS_MASK - 1, 0, 0);
148 input_set_abs_params(input, ABS_Y, 0, GRTS_MAX_POS_MASK - 1, 0, 0);
150 input_set_abs_params(input, ABS_PRESSURE, st->pressure_min,
153 input_set_capability(input, EV_KEY, BTN_TOUCH);
155 /* parse optional device tree properties */
156 touchscreen_parse_properties(input, false, &st->prop);
159 input_set_drvdata(input, st);
161 error = input_register_device(input);
163 dev_err(dev, "failed to register input device.");
167 st->iio_cb = iio_channel_get_all_cb(dev, grts_cb, st);
168 if (IS_ERR(st->iio_cb)) {
169 dev_err(dev, "failed to allocate callback buffer.\n");
170 return PTR_ERR(st->iio_cb);
173 error = devm_add_action_or_reset(dev, grts_disable, st->iio_cb);
175 dev_err(dev, "failed to add disable action.\n");
182 static const struct of_device_id grts_of_match[] = {
184 .compatible = "resistive-adc-touch",
190 MODULE_DEVICE_TABLE(of, grts_of_match);
192 static struct platform_driver grts_driver = {
196 .of_match_table = of_match_ptr(grts_of_match),
200 module_platform_driver(grts_driver);
203 MODULE_DESCRIPTION("Generic ADC Resistive Touch Driver");
204 MODULE_LICENSE("GPL v2");