UPSTREAM: Input: add ADC resistor ladder driver
authorAlexandre Belloni <alexandre.belloni@free-electrons.com>
Tue, 30 Aug 2016 02:57:06 +0000 (19:57 -0700)
committerHuang, Tao <huangtao@rock-chips.com>
Fri, 13 Jan 2017 11:41:22 +0000 (19:41 +0800)
A common way of multiplexing buttons on a single input in cheap devices is
to use a resistor ladder on an ADC. This driver supports that configuration
by polling an ADC channel provided by IIO.

Change-Id: I110d95d7787a3ad42b5d4040d73b01efe2ca76e4
Acked-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Huang, Tao <huangtao@rock-chips.com>
(cherry picked from commit 680772647d96ed853d20f837a2726151f24d8b20)

Documentation/devicetree/bindings/input/adc-keys.txt [new file with mode: 0644]
drivers/input/keyboard/Kconfig
drivers/input/keyboard/Makefile
drivers/input/keyboard/adc-keys.c [new file with mode: 0644]

diff --git a/Documentation/devicetree/bindings/input/adc-keys.txt b/Documentation/devicetree/bindings/input/adc-keys.txt
new file mode 100644 (file)
index 0000000..e551814
--- /dev/null
@@ -0,0 +1,49 @@
+ADC attached resistor ladder buttons
+------------------------------------
+
+Required properties:
+ - compatible: "adc-keys"
+ - io-channels: Phandle to an ADC channel
+ - io-channel-names = "buttons";
+ - keyup-threshold-microvolt: Voltage at which all the keys are considered up.
+
+Optional properties:
+       - poll-interval: Poll interval time in milliseconds
+       - autorepeat: Boolean, Enable auto repeat feature of Linux input
+         subsystem.
+
+Each button (key) is represented as a sub-node of "adc-keys":
+
+Required subnode-properties:
+       - label: Descriptive name of the key.
+       - linux,code: Keycode to emit.
+       - press-threshold-microvolt: Voltage ADC input when this key is pressed.
+
+Example:
+
+#include <dt-bindings/input/input.h>
+
+       adc-keys {
+               compatible = "adc-keys";
+               io-channels = <&lradc 0>;
+               io-channel-names = "buttons";
+               keyup-threshold-microvolt = <2000000>;
+
+               button-up {
+                       label = "Volume Up";
+                       linux,code = <KEY_VOLUMEUP>;
+                       press-threshold-microvolt = <1500000>;
+               };
+
+               button-down {
+                       label = "Volume Down";
+                       linux,code = <KEY_VOLUMEDOWN>;
+                       press-threshold-microvolt = <1000000>;
+               };
+
+               button-enter {
+                       label = "Enter";
+                       linux,code = <KEY_ENTER>;
+                       press-threshold-microvolt = <500000>;
+               };
+       };
index 56759912e9dda853782b4adedc62ee53c239cd10..d24fc86bc615f994065ac9dd45a6d54fa4441e87 100644 (file)
@@ -12,6 +12,21 @@ menuconfig INPUT_KEYBOARD
 
 if INPUT_KEYBOARD
 
+config KEYBOARD_ADC
+       tristate "ADC Ladder Buttons"
+       depends on IIO
+       select INPUT_POLLDEV
+       help
+         This driver implements support for buttons connected
+         to an ADC using a resistor ladder.
+
+         Say Y here if your device has such buttons connected to an ADC.  Your
+         board-specific setup logic must also provide a configuration data
+         for mapping voltages to buttons.
+
+         To compile this driver as a module, choose M here: the
+         module will be called adc_keys.
+
 config KEYBOARD_ADP5520
        tristate "Keypad Support for ADP5520 PMIC"
        depends on PMIC_ADP5520
index 42c5806d5bb659f54044da0d2f4e58e5d49140c8..ad7486b76e1f894d5427a618e6c15c879e551e33 100644 (file)
@@ -4,6 +4,7 @@
 
 # Each configuration option enables a list of files.
 
+obj-$(CONFIG_KEYBOARD_ADC)             += adc-keys.o
 obj-$(CONFIG_KEYBOARD_ADP5520)         += adp5520-keys.o
 obj-$(CONFIG_KEYBOARD_ADP5588)         += adp5588-keys.o
 obj-$(CONFIG_KEYBOARD_ADP5589)         += adp5589-keys.o
diff --git a/drivers/input/keyboard/adc-keys.c b/drivers/input/keyboard/adc-keys.c
new file mode 100644 (file)
index 0000000..f8cf2cc
--- /dev/null
@@ -0,0 +1,210 @@
+/*
+ * Input driver for resistor ladder connected on ADC
+ *
+ * Copyright (c) 2016 Alexandre Belloni
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ */
+
+#include <linux/err.h>
+#include <linux/iio/consumer.h>
+#include <linux/iio/types.h>
+#include <linux/input.h>
+#include <linux/input-polldev.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+
+struct adc_keys_button {
+       u32 voltage;
+       u32 keycode;
+};
+
+struct adc_keys_state {
+       struct iio_channel *channel;
+       u32 num_keys;
+       u32 last_key;
+       u32 keyup_voltage;
+       const struct adc_keys_button *map;
+};
+
+static void adc_keys_poll(struct input_polled_dev *dev)
+{
+       struct adc_keys_state *st = dev->private;
+       int i, value, ret;
+       u32 diff, closest = 0xffffffff;
+       int keycode = 0;
+
+       ret = iio_read_channel_processed(st->channel, &value);
+       if (unlikely(ret < 0)) {
+               /* Forcibly release key if any was pressed */
+               value = st->keyup_voltage;
+       } else {
+               for (i = 0; i < st->num_keys; i++) {
+                       diff = abs(st->map[i].voltage - value);
+                       if (diff < closest) {
+                               closest = diff;
+                               keycode = st->map[i].keycode;
+                       }
+               }
+       }
+
+       if (abs(st->keyup_voltage - value) < closest)
+               keycode = 0;
+
+       if (st->last_key && st->last_key != keycode)
+               input_report_key(dev->input, st->last_key, 0);
+
+       if (keycode)
+               input_report_key(dev->input, keycode, 1);
+
+       input_sync(dev->input);
+       st->last_key = keycode;
+}
+
+static int adc_keys_load_keymap(struct device *dev, struct adc_keys_state *st)
+{
+       struct adc_keys_button *map;
+       struct fwnode_handle *child;
+       int i;
+
+       st->num_keys = device_get_child_node_count(dev);
+       if (st->num_keys == 0) {
+               dev_err(dev, "keymap is missing\n");
+               return -EINVAL;
+       }
+
+       map = devm_kmalloc_array(dev, st->num_keys, sizeof(*map), GFP_KERNEL);
+       if (!map)
+               return -ENOMEM;
+
+       i = 0;
+       device_for_each_child_node(dev, child) {
+               if (fwnode_property_read_u32(child, "press-threshold-microvolt",
+                                            &map[i].voltage)) {
+                       dev_err(dev, "Key with invalid or missing voltage\n");
+                       fwnode_handle_put(child);
+                       return -EINVAL;
+               }
+               map[i].voltage /= 1000;
+
+               if (fwnode_property_read_u32(child, "linux,code",
+                                            &map[i].keycode)) {
+                       dev_err(dev, "Key with invalid or missing linux,code\n");
+                       fwnode_handle_put(child);
+                       return -EINVAL;
+               }
+
+               i++;
+       }
+
+       st->map = map;
+       return 0;
+}
+
+static int adc_keys_probe(struct platform_device *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct adc_keys_state *st;
+       struct input_polled_dev *poll_dev;
+       struct input_dev *input;
+       enum iio_chan_type type;
+       int i, value;
+       int error;
+
+       st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
+       if (!st)
+               return -ENOMEM;
+
+       st->channel = devm_iio_channel_get(dev, "buttons");
+       if (IS_ERR(st->channel))
+               return PTR_ERR(st->channel);
+
+       if (!st->channel->indio_dev)
+               return -ENXIO;
+
+       error = iio_get_channel_type(st->channel, &type);
+       if (error < 0)
+               return error;
+
+       if (type != IIO_VOLTAGE) {
+               dev_err(dev, "Incompatible channel type %d\n", type);
+               return -EINVAL;
+       }
+
+       if (device_property_read_u32(dev, "keyup-threshold-microvolt",
+                                    &st->keyup_voltage)) {
+               dev_err(dev, "Invalid or missing keyup voltage\n");
+               return -EINVAL;
+       }
+       st->keyup_voltage /= 1000;
+
+       error = adc_keys_load_keymap(dev, st);
+       if (error)
+               return error;
+
+       platform_set_drvdata(pdev, st);
+
+       poll_dev = devm_input_allocate_polled_device(dev);
+       if (!poll_dev) {
+               dev_err(dev, "failed to allocate input device\n");
+               return -ENOMEM;
+       }
+
+       if (!device_property_read_u32(dev, "poll-interval", &value))
+               poll_dev->poll_interval = value;
+
+       poll_dev->poll = adc_keys_poll;
+       poll_dev->private = st;
+
+       input = poll_dev->input;
+
+       input->name = pdev->name;
+       input->phys = "adc-keys/input0";
+
+       input->id.bustype = BUS_HOST;
+       input->id.vendor = 0x0001;
+       input->id.product = 0x0001;
+       input->id.version = 0x0100;
+
+       __set_bit(EV_KEY, input->evbit);
+       for (i = 0; i < st->num_keys; i++)
+               __set_bit(st->map[i].keycode, input->keybit);
+
+       if (device_property_read_bool(dev, "autorepeat"))
+               __set_bit(EV_REP, input->evbit);
+
+       error = input_register_polled_device(poll_dev);
+       if (error) {
+               dev_err(dev, "Unable to register input device: %d\n", error);
+               return error;
+       }
+
+       return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id adc_keys_of_match[] = {
+       { .compatible = "adc-keys", },
+       { }
+};
+MODULE_DEVICE_TABLE(of, adc_keys_of_match);
+#endif
+
+static struct platform_driver __refdata adc_keys_driver = {
+       .driver = {
+               .name = "adc_keys",
+               .of_match_table = of_match_ptr(adc_keys_of_match),
+       },
+       .probe = adc_keys_probe,
+};
+module_platform_driver(adc_keys_driver);
+
+MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
+MODULE_DESCRIPTION("Input driver for resistor ladder connected on ADC");
+MODULE_LICENSE("GPL v2");