1aa132ffbSEugen Hristev // SPDX-License-Identifier: GPL-2.0
2aa132ffbSEugen Hristev /*
3aa132ffbSEugen Hristev * ADC generic resistive touchscreen (GRTS)
4aa132ffbSEugen Hristev * This is a generic input driver that connects to an ADC
5aa132ffbSEugen Hristev * given the channels in device tree, and reports events to the input
6aa132ffbSEugen Hristev * subsystem.
7aa132ffbSEugen Hristev *
8aa132ffbSEugen Hristev * Copyright (C) 2017,2018 Microchip Technology,
9aa132ffbSEugen Hristev * Author: Eugen Hristev <eugen.hristev@microchip.com>
10aa132ffbSEugen Hristev *
11aa132ffbSEugen Hristev */
12aa132ffbSEugen Hristev #include <linux/input.h>
13aa132ffbSEugen Hristev #include <linux/input/touchscreen.h>
14aa132ffbSEugen Hristev #include <linux/iio/consumer.h>
15aa132ffbSEugen Hristev #include <linux/iio/iio.h>
1608729298SAndy Shevchenko #include <linux/mod_devicetable.h>
17aa132ffbSEugen Hristev #include <linux/module.h>
18aa132ffbSEugen Hristev #include <linux/platform_device.h>
1908729298SAndy Shevchenko #include <linux/property.h>
20aa132ffbSEugen Hristev
21aa132ffbSEugen Hristev #define DRIVER_NAME "resistive-adc-touch"
22aa132ffbSEugen Hristev #define GRTS_DEFAULT_PRESSURE_MIN 50000
23fb082cd5SOleksij Rempel #define GRTS_DEFAULT_PRESSURE_MAX 65535
24aa132ffbSEugen Hristev #define GRTS_MAX_POS_MASK GENMASK(11, 0)
25fb082cd5SOleksij Rempel #define GRTS_MAX_CHANNELS 4
26fb082cd5SOleksij Rempel
27fb082cd5SOleksij Rempel enum grts_ch_type {
28fb082cd5SOleksij Rempel GRTS_CH_X,
29fb082cd5SOleksij Rempel GRTS_CH_Y,
30fb082cd5SOleksij Rempel GRTS_CH_PRESSURE,
31fb082cd5SOleksij Rempel GRTS_CH_Z1,
32fb082cd5SOleksij Rempel GRTS_CH_Z2,
3360b7db91SDmitry Torokhov GRTS_CH_MAX = GRTS_CH_Z2 + 1
34fb082cd5SOleksij Rempel };
35aa132ffbSEugen Hristev
36aa132ffbSEugen Hristev /**
37136feb4cSLee Jones * struct grts_state - generic resistive touch screen information struct
3802e28cf7SAndy Shevchenko * @x_plate_ohms: resistance of the X plate
39aa132ffbSEugen Hristev * @pressure_min: number representing the minimum for the pressure
40aa132ffbSEugen Hristev * @pressure: are we getting pressure info or not
41aa132ffbSEugen Hristev * @iio_chans: list of channels acquired
42aa132ffbSEugen Hristev * @iio_cb: iio_callback buffer for the data
43aa132ffbSEugen Hristev * @input: the input device structure that we register
44aa132ffbSEugen Hristev * @prop: touchscreen properties struct
4560b7db91SDmitry Torokhov * @ch_map: map of channels that are defined for the touchscreen
46aa132ffbSEugen Hristev */
47aa132ffbSEugen Hristev struct grts_state {
48fb082cd5SOleksij Rempel u32 x_plate_ohms;
4902e28cf7SAndy Shevchenko u32 pressure_min;
50aa132ffbSEugen Hristev bool pressure;
51aa132ffbSEugen Hristev struct iio_channel *iio_chans;
52aa132ffbSEugen Hristev struct iio_cb_buffer *iio_cb;
53aa132ffbSEugen Hristev struct input_dev *input;
54aa132ffbSEugen Hristev struct touchscreen_properties prop;
5560b7db91SDmitry Torokhov u8 ch_map[GRTS_CH_MAX];
56aa132ffbSEugen Hristev };
57aa132ffbSEugen Hristev
grts_cb(const void * data,void * private)58aa132ffbSEugen Hristev static int grts_cb(const void *data, void *private)
59aa132ffbSEugen Hristev {
60aa132ffbSEugen Hristev const u16 *touch_info = data;
61aa132ffbSEugen Hristev struct grts_state *st = private;
626cdc1ef8SColin Ian King unsigned int x, y, press = 0;
63aa132ffbSEugen Hristev
6460b7db91SDmitry Torokhov x = touch_info[st->ch_map[GRTS_CH_X]];
6560b7db91SDmitry Torokhov y = touch_info[st->ch_map[GRTS_CH_Y]];
66fb082cd5SOleksij Rempel
6760b7db91SDmitry Torokhov if (st->ch_map[GRTS_CH_PRESSURE] < GRTS_MAX_CHANNELS) {
6860b7db91SDmitry Torokhov press = touch_info[st->ch_map[GRTS_CH_PRESSURE]];
6960b7db91SDmitry Torokhov } else if (st->ch_map[GRTS_CH_Z1] < GRTS_MAX_CHANNELS) {
7060b7db91SDmitry Torokhov unsigned int z1 = touch_info[st->ch_map[GRTS_CH_Z1]];
7160b7db91SDmitry Torokhov unsigned int z2 = touch_info[st->ch_map[GRTS_CH_Z2]];
7260b7db91SDmitry Torokhov unsigned int Rt;
7360b7db91SDmitry Torokhov
74fe0a7e3dSOleksij Rempel if (likely(x && z1)) {
75fb082cd5SOleksij Rempel Rt = z2;
76fb082cd5SOleksij Rempel Rt -= z1;
77fb082cd5SOleksij Rempel Rt *= st->x_plate_ohms;
78fb082cd5SOleksij Rempel Rt = DIV_ROUND_CLOSEST(Rt, 16);
79fb082cd5SOleksij Rempel Rt *= x;
80fb082cd5SOleksij Rempel Rt /= z1;
81fb082cd5SOleksij Rempel Rt = DIV_ROUND_CLOSEST(Rt, 256);
82fb082cd5SOleksij Rempel /*
83fe0a7e3dSOleksij Rempel * On increased pressure the resistance (Rt) is
84fe0a7e3dSOleksij Rempel * decreasing so, convert values to make it looks as
85fe0a7e3dSOleksij Rempel * real pressure.
86fb082cd5SOleksij Rempel */
87fb082cd5SOleksij Rempel if (Rt < GRTS_DEFAULT_PRESSURE_MAX)
88fb082cd5SOleksij Rempel press = GRTS_DEFAULT_PRESSURE_MAX - Rt;
89fb082cd5SOleksij Rempel }
90fe0a7e3dSOleksij Rempel }
91aa132ffbSEugen Hristev
92aa132ffbSEugen Hristev if ((!x && !y) || (st->pressure && (press < st->pressure_min))) {
93aa132ffbSEugen Hristev /* report end of touch */
94aa132ffbSEugen Hristev input_report_key(st->input, BTN_TOUCH, 0);
95aa132ffbSEugen Hristev input_sync(st->input);
96aa132ffbSEugen Hristev return 0;
97aa132ffbSEugen Hristev }
98aa132ffbSEugen Hristev
99aa132ffbSEugen Hristev /* report proper touch to subsystem*/
100aa132ffbSEugen Hristev touchscreen_report_pos(st->input, &st->prop, x, y, false);
101aa132ffbSEugen Hristev if (st->pressure)
102aa132ffbSEugen Hristev input_report_abs(st->input, ABS_PRESSURE, press);
103aa132ffbSEugen Hristev input_report_key(st->input, BTN_TOUCH, 1);
104aa132ffbSEugen Hristev input_sync(st->input);
105aa132ffbSEugen Hristev
106aa132ffbSEugen Hristev return 0;
107aa132ffbSEugen Hristev }
108aa132ffbSEugen Hristev
grts_open(struct input_dev * dev)109aa132ffbSEugen Hristev static int grts_open(struct input_dev *dev)
110aa132ffbSEugen Hristev {
111aa132ffbSEugen Hristev int error;
112aa132ffbSEugen Hristev struct grts_state *st = input_get_drvdata(dev);
113aa132ffbSEugen Hristev
114aa132ffbSEugen Hristev error = iio_channel_start_all_cb(st->iio_cb);
115aa132ffbSEugen Hristev if (error) {
116aa132ffbSEugen Hristev dev_err(dev->dev.parent, "failed to start callback buffer.\n");
117aa132ffbSEugen Hristev return error;
118aa132ffbSEugen Hristev }
119aa132ffbSEugen Hristev return 0;
120aa132ffbSEugen Hristev }
121aa132ffbSEugen Hristev
grts_close(struct input_dev * dev)122aa132ffbSEugen Hristev static void grts_close(struct input_dev *dev)
123aa132ffbSEugen Hristev {
124aa132ffbSEugen Hristev struct grts_state *st = input_get_drvdata(dev);
125aa132ffbSEugen Hristev
126aa132ffbSEugen Hristev iio_channel_stop_all_cb(st->iio_cb);
127aa132ffbSEugen Hristev }
128aa132ffbSEugen Hristev
grts_disable(void * data)129aa132ffbSEugen Hristev static void grts_disable(void *data)
130aa132ffbSEugen Hristev {
131aa132ffbSEugen Hristev iio_channel_release_all_cb(data);
132aa132ffbSEugen Hristev }
133aa132ffbSEugen Hristev
grts_map_channel(struct grts_state * st,struct device * dev,enum grts_ch_type type,const char * name,bool optional)13460b7db91SDmitry Torokhov static int grts_map_channel(struct grts_state *st, struct device *dev,
13560b7db91SDmitry Torokhov enum grts_ch_type type, const char *name,
13660b7db91SDmitry Torokhov bool optional)
13760b7db91SDmitry Torokhov {
13860b7db91SDmitry Torokhov int idx;
13960b7db91SDmitry Torokhov
14060b7db91SDmitry Torokhov idx = device_property_match_string(dev, "io-channel-names", name);
14160b7db91SDmitry Torokhov if (idx < 0) {
14260b7db91SDmitry Torokhov if (!optional)
14360b7db91SDmitry Torokhov return idx;
14460b7db91SDmitry Torokhov idx = GRTS_MAX_CHANNELS;
14560b7db91SDmitry Torokhov } else if (idx >= GRTS_MAX_CHANNELS) {
14660b7db91SDmitry Torokhov return -EOVERFLOW;
14760b7db91SDmitry Torokhov }
14860b7db91SDmitry Torokhov
14960b7db91SDmitry Torokhov st->ch_map[type] = idx;
15060b7db91SDmitry Torokhov return 0;
15160b7db91SDmitry Torokhov }
15260b7db91SDmitry Torokhov
grts_get_properties(struct grts_state * st,struct device * dev)153fb082cd5SOleksij Rempel static int grts_get_properties(struct grts_state *st, struct device *dev)
154fb082cd5SOleksij Rempel {
15560b7db91SDmitry Torokhov int error;
156fb082cd5SOleksij Rempel
15760b7db91SDmitry Torokhov error = grts_map_channel(st, dev, GRTS_CH_X, "x", false);
15860b7db91SDmitry Torokhov if (error)
15960b7db91SDmitry Torokhov return error;
160fb082cd5SOleksij Rempel
16160b7db91SDmitry Torokhov error = grts_map_channel(st, dev, GRTS_CH_Y, "y", false);
16260b7db91SDmitry Torokhov if (error)
16360b7db91SDmitry Torokhov return error;
164fb082cd5SOleksij Rempel
165fb082cd5SOleksij Rempel /* pressure is optional */
16660b7db91SDmitry Torokhov error = grts_map_channel(st, dev, GRTS_CH_PRESSURE, "pressure", true);
16760b7db91SDmitry Torokhov if (error)
16860b7db91SDmitry Torokhov return error;
169fb082cd5SOleksij Rempel
17060b7db91SDmitry Torokhov if (st->ch_map[GRTS_CH_PRESSURE] < GRTS_MAX_CHANNELS) {
171fb082cd5SOleksij Rempel st->pressure = true;
172fb082cd5SOleksij Rempel return 0;
173fb082cd5SOleksij Rempel }
174fb082cd5SOleksij Rempel
175fb082cd5SOleksij Rempel /* if no pressure is defined, try optional z1 + z2 */
17660b7db91SDmitry Torokhov error = grts_map_channel(st, dev, GRTS_CH_Z1, "z1", true);
17760b7db91SDmitry Torokhov if (error)
17860b7db91SDmitry Torokhov return error;
17960b7db91SDmitry Torokhov
18060b7db91SDmitry Torokhov if (st->ch_map[GRTS_CH_Z1] >= GRTS_MAX_CHANNELS)
181fb082cd5SOleksij Rempel return 0;
182fb082cd5SOleksij Rempel
183fb082cd5SOleksij Rempel /* if z1 is provided z2 is not optional */
18460b7db91SDmitry Torokhov error = grts_map_channel(st, dev, GRTS_CH_Z2, "z2", true);
18560b7db91SDmitry Torokhov if (error)
18660b7db91SDmitry Torokhov return error;
187fb082cd5SOleksij Rempel
188fb082cd5SOleksij Rempel error = device_property_read_u32(dev,
189fb082cd5SOleksij Rempel "touchscreen-x-plate-ohms",
190fb082cd5SOleksij Rempel &st->x_plate_ohms);
191fb082cd5SOleksij Rempel if (error) {
192fb082cd5SOleksij Rempel dev_err(dev, "can't get touchscreen-x-plate-ohms property\n");
193fb082cd5SOleksij Rempel return error;
194fb082cd5SOleksij Rempel }
195fb082cd5SOleksij Rempel
19660b7db91SDmitry Torokhov st->pressure = true;
197fb082cd5SOleksij Rempel return 0;
198fb082cd5SOleksij Rempel }
199fb082cd5SOleksij Rempel
grts_probe(struct platform_device * pdev)200aa132ffbSEugen Hristev static int grts_probe(struct platform_device *pdev)
201aa132ffbSEugen Hristev {
202aa132ffbSEugen Hristev struct grts_state *st;
203aa132ffbSEugen Hristev struct input_dev *input;
204aa132ffbSEugen Hristev struct device *dev = &pdev->dev;
205aa132ffbSEugen Hristev int error;
206aa132ffbSEugen Hristev
207aa132ffbSEugen Hristev st = devm_kzalloc(dev, sizeof(struct grts_state), GFP_KERNEL);
208aa132ffbSEugen Hristev if (!st)
209aa132ffbSEugen Hristev return -ENOMEM;
210aa132ffbSEugen Hristev
211aa132ffbSEugen Hristev /* get the channels from IIO device */
212aa132ffbSEugen Hristev st->iio_chans = devm_iio_channel_get_all(dev);
213*17d981faSKrzysztof Kozlowski if (IS_ERR(st->iio_chans))
214*17d981faSKrzysztof Kozlowski return dev_err_probe(dev, PTR_ERR(st->iio_chans), "can't get iio channels\n");
215aa132ffbSEugen Hristev
216fb082cd5SOleksij Rempel if (!device_property_present(dev, "io-channel-names"))
217fb082cd5SOleksij Rempel return -ENODEV;
218fb082cd5SOleksij Rempel
219fb082cd5SOleksij Rempel error = grts_get_properties(st, dev);
220fb082cd5SOleksij Rempel if (error) {
221fb082cd5SOleksij Rempel dev_err(dev, "Failed to parse properties\n");
222fb082cd5SOleksij Rempel return error;
223aa132ffbSEugen Hristev }
224aa132ffbSEugen Hristev
225aa132ffbSEugen Hristev if (st->pressure) {
226aa132ffbSEugen Hristev error = device_property_read_u32(dev,
227aa132ffbSEugen Hristev "touchscreen-min-pressure",
228aa132ffbSEugen Hristev &st->pressure_min);
229aa132ffbSEugen Hristev if (error) {
230aa132ffbSEugen Hristev dev_dbg(dev, "can't get touchscreen-min-pressure property.\n");
231aa132ffbSEugen Hristev st->pressure_min = GRTS_DEFAULT_PRESSURE_MIN;
232aa132ffbSEugen Hristev }
233aa132ffbSEugen Hristev }
234aa132ffbSEugen Hristev
235aa132ffbSEugen Hristev input = devm_input_allocate_device(dev);
236aa132ffbSEugen Hristev if (!input) {
237aa132ffbSEugen Hristev dev_err(dev, "failed to allocate input device.\n");
238aa132ffbSEugen Hristev return -ENOMEM;
239aa132ffbSEugen Hristev }
240aa132ffbSEugen Hristev
241aa132ffbSEugen Hristev input->name = DRIVER_NAME;
242aa132ffbSEugen Hristev input->id.bustype = BUS_HOST;
243aa132ffbSEugen Hristev input->open = grts_open;
244aa132ffbSEugen Hristev input->close = grts_close;
245aa132ffbSEugen Hristev
246aa132ffbSEugen Hristev input_set_abs_params(input, ABS_X, 0, GRTS_MAX_POS_MASK - 1, 0, 0);
247aa132ffbSEugen Hristev input_set_abs_params(input, ABS_Y, 0, GRTS_MAX_POS_MASK - 1, 0, 0);
248aa132ffbSEugen Hristev if (st->pressure)
249aa132ffbSEugen Hristev input_set_abs_params(input, ABS_PRESSURE, st->pressure_min,
250fb082cd5SOleksij Rempel GRTS_DEFAULT_PRESSURE_MAX, 0, 0);
251aa132ffbSEugen Hristev
252aa132ffbSEugen Hristev input_set_capability(input, EV_KEY, BTN_TOUCH);
253aa132ffbSEugen Hristev
254aa132ffbSEugen Hristev /* parse optional device tree properties */
255aa132ffbSEugen Hristev touchscreen_parse_properties(input, false, &st->prop);
256aa132ffbSEugen Hristev
257aa132ffbSEugen Hristev st->input = input;
258aa132ffbSEugen Hristev input_set_drvdata(input, st);
259aa132ffbSEugen Hristev
260aa132ffbSEugen Hristev error = input_register_device(input);
261aa132ffbSEugen Hristev if (error) {
262aa132ffbSEugen Hristev dev_err(dev, "failed to register input device.");
263aa132ffbSEugen Hristev return error;
264aa132ffbSEugen Hristev }
265aa132ffbSEugen Hristev
266aa132ffbSEugen Hristev st->iio_cb = iio_channel_get_all_cb(dev, grts_cb, st);
267aa132ffbSEugen Hristev if (IS_ERR(st->iio_cb)) {
268aa132ffbSEugen Hristev dev_err(dev, "failed to allocate callback buffer.\n");
269aa132ffbSEugen Hristev return PTR_ERR(st->iio_cb);
270aa132ffbSEugen Hristev }
271aa132ffbSEugen Hristev
272aa132ffbSEugen Hristev error = devm_add_action_or_reset(dev, grts_disable, st->iio_cb);
273aa132ffbSEugen Hristev if (error) {
274aa132ffbSEugen Hristev dev_err(dev, "failed to add disable action.\n");
275aa132ffbSEugen Hristev return error;
276aa132ffbSEugen Hristev }
277aa132ffbSEugen Hristev
278aa132ffbSEugen Hristev return 0;
279aa132ffbSEugen Hristev }
280aa132ffbSEugen Hristev
281aa132ffbSEugen Hristev static const struct of_device_id grts_of_match[] = {
282aa132ffbSEugen Hristev {
283aa132ffbSEugen Hristev .compatible = "resistive-adc-touch",
284aa132ffbSEugen Hristev }, {
285aa132ffbSEugen Hristev /* sentinel */
286aa132ffbSEugen Hristev },
287aa132ffbSEugen Hristev };
288aa132ffbSEugen Hristev
289aa132ffbSEugen Hristev MODULE_DEVICE_TABLE(of, grts_of_match);
290aa132ffbSEugen Hristev
291aa132ffbSEugen Hristev static struct platform_driver grts_driver = {
292aa132ffbSEugen Hristev .probe = grts_probe,
293aa132ffbSEugen Hristev .driver = {
294aa132ffbSEugen Hristev .name = DRIVER_NAME,
29508729298SAndy Shevchenko .of_match_table = grts_of_match,
296aa132ffbSEugen Hristev },
297aa132ffbSEugen Hristev };
298aa132ffbSEugen Hristev
299aa132ffbSEugen Hristev module_platform_driver(grts_driver);
300aa132ffbSEugen Hristev
301aa132ffbSEugen Hristev MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com>");
302aa132ffbSEugen Hristev MODULE_DESCRIPTION("Generic ADC Resistive Touch Driver");
303aa132ffbSEugen Hristev MODULE_LICENSE("GPL v2");
304