1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
268077264SAlexandre Belloni /*
368077264SAlexandre Belloni * Input driver for resistor ladder connected on ADC
468077264SAlexandre Belloni *
568077264SAlexandre Belloni * Copyright (c) 2016 Alexandre Belloni
668077264SAlexandre Belloni */
768077264SAlexandre Belloni
868077264SAlexandre Belloni #include <linux/err.h>
968077264SAlexandre Belloni #include <linux/iio/consumer.h>
1068077264SAlexandre Belloni #include <linux/iio/types.h>
1168077264SAlexandre Belloni #include <linux/input.h>
1268077264SAlexandre Belloni #include <linux/kernel.h>
1368077264SAlexandre Belloni #include <linux/module.h>
1468077264SAlexandre Belloni #include <linux/of.h>
1568077264SAlexandre Belloni #include <linux/platform_device.h>
1668077264SAlexandre Belloni #include <linux/property.h>
1768077264SAlexandre Belloni #include <linux/slab.h>
1868077264SAlexandre Belloni
1968077264SAlexandre Belloni struct adc_keys_button {
2068077264SAlexandre Belloni u32 voltage;
2168077264SAlexandre Belloni u32 keycode;
2268077264SAlexandre Belloni };
2368077264SAlexandre Belloni
2468077264SAlexandre Belloni struct adc_keys_state {
2568077264SAlexandre Belloni struct iio_channel *channel;
2668077264SAlexandre Belloni u32 num_keys;
2768077264SAlexandre Belloni u32 last_key;
2868077264SAlexandre Belloni u32 keyup_voltage;
2968077264SAlexandre Belloni const struct adc_keys_button *map;
3068077264SAlexandre Belloni };
3168077264SAlexandre Belloni
adc_keys_poll(struct input_dev * input)32d0fe37b9SDmitry Torokhov static void adc_keys_poll(struct input_dev *input)
3368077264SAlexandre Belloni {
34d0fe37b9SDmitry Torokhov struct adc_keys_state *st = input_get_drvdata(input);
3568077264SAlexandre Belloni int i, value, ret;
3668077264SAlexandre Belloni u32 diff, closest = 0xffffffff;
3768077264SAlexandre Belloni int keycode = 0;
3868077264SAlexandre Belloni
3968077264SAlexandre Belloni ret = iio_read_channel_processed(st->channel, &value);
4068077264SAlexandre Belloni if (unlikely(ret < 0)) {
4168077264SAlexandre Belloni /* Forcibly release key if any was pressed */
4268077264SAlexandre Belloni value = st->keyup_voltage;
4368077264SAlexandre Belloni } else {
4468077264SAlexandre Belloni for (i = 0; i < st->num_keys; i++) {
4568077264SAlexandre Belloni diff = abs(st->map[i].voltage - value);
4668077264SAlexandre Belloni if (diff < closest) {
4768077264SAlexandre Belloni closest = diff;
4868077264SAlexandre Belloni keycode = st->map[i].keycode;
4968077264SAlexandre Belloni }
5068077264SAlexandre Belloni }
5168077264SAlexandre Belloni }
5268077264SAlexandre Belloni
5368077264SAlexandre Belloni if (abs(st->keyup_voltage - value) < closest)
5468077264SAlexandre Belloni keycode = 0;
5568077264SAlexandre Belloni
5668077264SAlexandre Belloni if (st->last_key && st->last_key != keycode)
57d0fe37b9SDmitry Torokhov input_report_key(input, st->last_key, 0);
5868077264SAlexandre Belloni
5968077264SAlexandre Belloni if (keycode)
60d0fe37b9SDmitry Torokhov input_report_key(input, keycode, 1);
6168077264SAlexandre Belloni
62d0fe37b9SDmitry Torokhov input_sync(input);
6368077264SAlexandre Belloni st->last_key = keycode;
6468077264SAlexandre Belloni }
6568077264SAlexandre Belloni
adc_keys_load_keymap(struct device * dev,struct adc_keys_state * st)6668077264SAlexandre Belloni static int adc_keys_load_keymap(struct device *dev, struct adc_keys_state *st)
6768077264SAlexandre Belloni {
6868077264SAlexandre Belloni struct adc_keys_button *map;
6968077264SAlexandre Belloni struct fwnode_handle *child;
7068077264SAlexandre Belloni int i;
7168077264SAlexandre Belloni
7268077264SAlexandre Belloni st->num_keys = device_get_child_node_count(dev);
7368077264SAlexandre Belloni if (st->num_keys == 0) {
7468077264SAlexandre Belloni dev_err(dev, "keymap is missing\n");
7568077264SAlexandre Belloni return -EINVAL;
7668077264SAlexandre Belloni }
7768077264SAlexandre Belloni
7868077264SAlexandre Belloni map = devm_kmalloc_array(dev, st->num_keys, sizeof(*map), GFP_KERNEL);
7968077264SAlexandre Belloni if (!map)
8068077264SAlexandre Belloni return -ENOMEM;
8168077264SAlexandre Belloni
8268077264SAlexandre Belloni i = 0;
8368077264SAlexandre Belloni device_for_each_child_node(dev, child) {
8468077264SAlexandre Belloni if (fwnode_property_read_u32(child, "press-threshold-microvolt",
8568077264SAlexandre Belloni &map[i].voltage)) {
8668077264SAlexandre Belloni dev_err(dev, "Key with invalid or missing voltage\n");
8768077264SAlexandre Belloni fwnode_handle_put(child);
8868077264SAlexandre Belloni return -EINVAL;
8968077264SAlexandre Belloni }
9068077264SAlexandre Belloni map[i].voltage /= 1000;
9168077264SAlexandre Belloni
9268077264SAlexandre Belloni if (fwnode_property_read_u32(child, "linux,code",
9368077264SAlexandre Belloni &map[i].keycode)) {
9468077264SAlexandre Belloni dev_err(dev, "Key with invalid or missing linux,code\n");
9568077264SAlexandre Belloni fwnode_handle_put(child);
9668077264SAlexandre Belloni return -EINVAL;
9768077264SAlexandre Belloni }
9868077264SAlexandre Belloni
9968077264SAlexandre Belloni i++;
10068077264SAlexandre Belloni }
10168077264SAlexandre Belloni
10268077264SAlexandre Belloni st->map = map;
10368077264SAlexandre Belloni return 0;
10468077264SAlexandre Belloni }
10568077264SAlexandre Belloni
adc_keys_probe(struct platform_device * pdev)10668077264SAlexandre Belloni static int adc_keys_probe(struct platform_device *pdev)
10768077264SAlexandre Belloni {
10868077264SAlexandre Belloni struct device *dev = &pdev->dev;
10968077264SAlexandre Belloni struct adc_keys_state *st;
11068077264SAlexandre Belloni struct input_dev *input;
11168077264SAlexandre Belloni enum iio_chan_type type;
11268077264SAlexandre Belloni int i, value;
11368077264SAlexandre Belloni int error;
11468077264SAlexandre Belloni
11568077264SAlexandre Belloni st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
11668077264SAlexandre Belloni if (!st)
11768077264SAlexandre Belloni return -ENOMEM;
11868077264SAlexandre Belloni
11968077264SAlexandre Belloni st->channel = devm_iio_channel_get(dev, "buttons");
12068077264SAlexandre Belloni if (IS_ERR(st->channel))
12168077264SAlexandre Belloni return PTR_ERR(st->channel);
12268077264SAlexandre Belloni
12368077264SAlexandre Belloni if (!st->channel->indio_dev)
12468077264SAlexandre Belloni return -ENXIO;
12568077264SAlexandre Belloni
12668077264SAlexandre Belloni error = iio_get_channel_type(st->channel, &type);
12768077264SAlexandre Belloni if (error < 0)
12868077264SAlexandre Belloni return error;
12968077264SAlexandre Belloni
13068077264SAlexandre Belloni if (type != IIO_VOLTAGE) {
13168077264SAlexandre Belloni dev_err(dev, "Incompatible channel type %d\n", type);
13268077264SAlexandre Belloni return -EINVAL;
13368077264SAlexandre Belloni }
13468077264SAlexandre Belloni
13568077264SAlexandre Belloni if (device_property_read_u32(dev, "keyup-threshold-microvolt",
13668077264SAlexandre Belloni &st->keyup_voltage)) {
13768077264SAlexandre Belloni dev_err(dev, "Invalid or missing keyup voltage\n");
13868077264SAlexandre Belloni return -EINVAL;
13968077264SAlexandre Belloni }
14068077264SAlexandre Belloni st->keyup_voltage /= 1000;
14168077264SAlexandre Belloni
14268077264SAlexandre Belloni error = adc_keys_load_keymap(dev, st);
14368077264SAlexandre Belloni if (error)
14468077264SAlexandre Belloni return error;
14568077264SAlexandre Belloni
146d0fe37b9SDmitry Torokhov input = devm_input_allocate_device(dev);
147d0fe37b9SDmitry Torokhov if (!input) {
14868077264SAlexandre Belloni dev_err(dev, "failed to allocate input device\n");
14968077264SAlexandre Belloni return -ENOMEM;
15068077264SAlexandre Belloni }
15168077264SAlexandre Belloni
152d0fe37b9SDmitry Torokhov input_set_drvdata(input, st);
15368077264SAlexandre Belloni
15468077264SAlexandre Belloni input->name = pdev->name;
15568077264SAlexandre Belloni input->phys = "adc-keys/input0";
15668077264SAlexandre Belloni
15768077264SAlexandre Belloni input->id.bustype = BUS_HOST;
15868077264SAlexandre Belloni input->id.vendor = 0x0001;
15968077264SAlexandre Belloni input->id.product = 0x0001;
16068077264SAlexandre Belloni input->id.version = 0x0100;
16168077264SAlexandre Belloni
16268077264SAlexandre Belloni __set_bit(EV_KEY, input->evbit);
16368077264SAlexandre Belloni for (i = 0; i < st->num_keys; i++)
16468077264SAlexandre Belloni __set_bit(st->map[i].keycode, input->keybit);
16568077264SAlexandre Belloni
16668077264SAlexandre Belloni if (device_property_read_bool(dev, "autorepeat"))
16768077264SAlexandre Belloni __set_bit(EV_REP, input->evbit);
16868077264SAlexandre Belloni
169d0fe37b9SDmitry Torokhov
170d0fe37b9SDmitry Torokhov error = input_setup_polling(input, adc_keys_poll);
171d0fe37b9SDmitry Torokhov if (error) {
172d0fe37b9SDmitry Torokhov dev_err(dev, "Unable to set up polling: %d\n", error);
173d0fe37b9SDmitry Torokhov return error;
174d0fe37b9SDmitry Torokhov }
175d0fe37b9SDmitry Torokhov
176d0fe37b9SDmitry Torokhov if (!device_property_read_u32(dev, "poll-interval", &value))
177d0fe37b9SDmitry Torokhov input_set_poll_interval(input, value);
178d0fe37b9SDmitry Torokhov
179d0fe37b9SDmitry Torokhov error = input_register_device(input);
18068077264SAlexandre Belloni if (error) {
18168077264SAlexandre Belloni dev_err(dev, "Unable to register input device: %d\n", error);
18268077264SAlexandre Belloni return error;
18368077264SAlexandre Belloni }
18468077264SAlexandre Belloni
18568077264SAlexandre Belloni return 0;
18668077264SAlexandre Belloni }
18768077264SAlexandre Belloni
18868077264SAlexandre Belloni #ifdef CONFIG_OF
18968077264SAlexandre Belloni static const struct of_device_id adc_keys_of_match[] = {
19068077264SAlexandre Belloni { .compatible = "adc-keys", },
19168077264SAlexandre Belloni { }
19268077264SAlexandre Belloni };
19368077264SAlexandre Belloni MODULE_DEVICE_TABLE(of, adc_keys_of_match);
19468077264SAlexandre Belloni #endif
19568077264SAlexandre Belloni
196*3e204d6bSGeert Uytterhoeven static struct platform_driver adc_keys_driver = {
19768077264SAlexandre Belloni .driver = {
19868077264SAlexandre Belloni .name = "adc_keys",
19968077264SAlexandre Belloni .of_match_table = of_match_ptr(adc_keys_of_match),
20068077264SAlexandre Belloni },
20168077264SAlexandre Belloni .probe = adc_keys_probe,
20268077264SAlexandre Belloni };
20368077264SAlexandre Belloni module_platform_driver(adc_keys_driver);
20468077264SAlexandre Belloni
20568077264SAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
20668077264SAlexandre Belloni MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
20768077264SAlexandre Belloni MODULE_LICENSE("GPL v2");
208