1 // SPDX-License-Identifier: GPL-2.0-only
3 * Input driver for resistor ladder connected on ADC
5 * Copyright (c) 2016 Alexandre Belloni
9 #include <linux/iio/consumer.h>
10 #include <linux/iio/types.h>
11 #include <linux/input.h>
12 #include <linux/input-polldev.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/slab.h>
20 struct adc_keys_button {
25 struct adc_keys_state {
26 struct iio_channel *channel;
30 const struct adc_keys_button *map;
33 static void adc_keys_poll(struct input_polled_dev *dev)
35 struct adc_keys_state *st = dev->private;
37 u32 diff, closest = 0xffffffff;
40 ret = iio_read_channel_processed(st->channel, &value);
41 if (unlikely(ret < 0)) {
42 /* Forcibly release key if any was pressed */
43 value = st->keyup_voltage;
45 for (i = 0; i < st->num_keys; i++) {
46 diff = abs(st->map[i].voltage - value);
49 keycode = st->map[i].keycode;
54 if (abs(st->keyup_voltage - value) < closest)
57 if (st->last_key && st->last_key != keycode)
58 input_report_key(dev->input, st->last_key, 0);
61 input_report_key(dev->input, keycode, 1);
63 input_sync(dev->input);
64 st->last_key = keycode;
67 static int adc_keys_load_keymap(struct device *dev, struct adc_keys_state *st)
69 struct adc_keys_button *map;
70 struct fwnode_handle *child;
73 st->num_keys = device_get_child_node_count(dev);
74 if (st->num_keys == 0) {
75 dev_err(dev, "keymap is missing\n");
79 map = devm_kmalloc_array(dev, st->num_keys, sizeof(*map), GFP_KERNEL);
84 device_for_each_child_node(dev, child) {
85 if (fwnode_property_read_u32(child, "press-threshold-microvolt",
87 dev_err(dev, "Key with invalid or missing voltage\n");
88 fwnode_handle_put(child);
91 map[i].voltage /= 1000;
93 if (fwnode_property_read_u32(child, "linux,code",
95 dev_err(dev, "Key with invalid or missing linux,code\n");
96 fwnode_handle_put(child);
107 static int adc_keys_probe(struct platform_device *pdev)
109 struct device *dev = &pdev->dev;
110 struct adc_keys_state *st;
111 struct input_polled_dev *poll_dev;
112 struct input_dev *input;
113 enum iio_chan_type type;
117 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
121 st->channel = devm_iio_channel_get(dev, "buttons");
122 if (IS_ERR(st->channel))
123 return PTR_ERR(st->channel);
125 if (!st->channel->indio_dev)
128 error = iio_get_channel_type(st->channel, &type);
132 if (type != IIO_VOLTAGE) {
133 dev_err(dev, "Incompatible channel type %d\n", type);
137 if (device_property_read_u32(dev, "keyup-threshold-microvolt",
138 &st->keyup_voltage)) {
139 dev_err(dev, "Invalid or missing keyup voltage\n");
142 st->keyup_voltage /= 1000;
144 error = adc_keys_load_keymap(dev, st);
148 poll_dev = devm_input_allocate_polled_device(dev);
150 dev_err(dev, "failed to allocate input device\n");
154 if (!device_property_read_u32(dev, "poll-interval", &value))
155 poll_dev->poll_interval = value;
157 poll_dev->poll = adc_keys_poll;
158 poll_dev->private = st;
160 input = poll_dev->input;
162 input->name = pdev->name;
163 input->phys = "adc-keys/input0";
165 input->id.bustype = BUS_HOST;
166 input->id.vendor = 0x0001;
167 input->id.product = 0x0001;
168 input->id.version = 0x0100;
170 __set_bit(EV_KEY, input->evbit);
171 for (i = 0; i < st->num_keys; i++)
172 __set_bit(st->map[i].keycode, input->keybit);
174 if (device_property_read_bool(dev, "autorepeat"))
175 __set_bit(EV_REP, input->evbit);
177 error = input_register_polled_device(poll_dev);
179 dev_err(dev, "Unable to register input device: %d\n", error);
187 static const struct of_device_id adc_keys_of_match[] = {
188 { .compatible = "adc-keys", },
191 MODULE_DEVICE_TABLE(of, adc_keys_of_match);
194 static struct platform_driver __refdata adc_keys_driver = {
197 .of_match_table = of_match_ptr(adc_keys_of_match),
199 .probe = adc_keys_probe,
201 module_platform_driver(adc_keys_driver);
203 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
204 MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
205 MODULE_LICENSE("GPL v2");