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