aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input
diff options
context:
space:
mode:
authorVignesh R <vigneshr@ti.com>2016-08-25 12:46:41 -0400
committerDmitry Torokhov <dmitry.torokhov@gmail.com>2016-08-29 23:24:23 -0400
commit694641616448a9c5b30b3a5e5f51da73cb3a0016 (patch)
tree7f1a864463f8def2c717e94a67b46a478fa59a35 /drivers/input
parent680772647d96ed853d20f837a2726151f24d8b20 (diff)
Input: add generic input driver to read encoded GPIO lines
Add a driver to read group of GPIO lines and provide its status as a numerical value as input event to the system. This will help in interfacing devices, that can be connected over GPIOs, that provide input to the system by driving GPIO lines connected to them like a rotary dial or a switch. For example, a rotary switch can be connected to four GPIO lines. The status of the GPIO lines reflect the actual position of the rotary switch dial. For example, if dial points to 9, then the four GPIO lines connected to the switch will read HLLH(0b'1001 = 9). This value can be reported as an ABS_* event to the input subsystem. Signed-off-by: Vignesh R <vigneshr@ti.com> Acked-by: Rob Herring <robh@kernel.org> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Diffstat (limited to 'drivers/input')
-rw-r--r--drivers/input/misc/Kconfig12
-rw-r--r--drivers/input/misc/Makefile1
-rw-r--r--drivers/input/misc/gpio_decoder.c137
3 files changed, 150 insertions, 0 deletions
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index efb0ca871327..7cdb89397d18 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -292,6 +292,18 @@ config INPUT_GPIO_TILT_POLLED
292 To compile this driver as a module, choose M here: the 292 To compile this driver as a module, choose M here: the
293 module will be called gpio_tilt_polled. 293 module will be called gpio_tilt_polled.
294 294
295config INPUT_GPIO_DECODER
296 tristate "Polled GPIO Decoder Input driver"
297 depends on GPIOLIB || COMPILE_TEST
298 select INPUT_POLLDEV
299 help
300 Say Y here if you want driver to read status of multiple GPIO
301 lines and report the encoded value as an absolute integer to
302 input subsystem.
303
304 To compile this driver as a module, choose M here: the module
305 will be called gpio_decoder.
306
295config INPUT_IXP4XX_BEEPER 307config INPUT_IXP4XX_BEEPER
296 tristate "IXP4XX Beeper support" 308 tristate "IXP4XX Beeper support"
297 depends on ARCH_IXP4XX 309 depends on ARCH_IXP4XX
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index 6a1e5e20fc1c..0b6d025f0487 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_INPUT_DRV2667_HAPTICS) += drv2667.o
35obj-$(CONFIG_INPUT_GP2A) += gp2ap002a00f.o 35obj-$(CONFIG_INPUT_GP2A) += gp2ap002a00f.o
36obj-$(CONFIG_INPUT_GPIO_BEEPER) += gpio-beeper.o 36obj-$(CONFIG_INPUT_GPIO_BEEPER) += gpio-beeper.o
37obj-$(CONFIG_INPUT_GPIO_TILT_POLLED) += gpio_tilt_polled.o 37obj-$(CONFIG_INPUT_GPIO_TILT_POLLED) += gpio_tilt_polled.o
38obj-$(CONFIG_INPUT_GPIO_DECODER) += gpio_decoder.o
38obj-$(CONFIG_INPUT_HISI_POWERKEY) += hisi_powerkey.o 39obj-$(CONFIG_INPUT_HISI_POWERKEY) += hisi_powerkey.o
39obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o 40obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o
40obj-$(CONFIG_INPUT_IMS_PCU) += ims-pcu.o 41obj-$(CONFIG_INPUT_IMS_PCU) += ims-pcu.o
diff --git a/drivers/input/misc/gpio_decoder.c b/drivers/input/misc/gpio_decoder.c
new file mode 100644
index 000000000000..ca7e0bacb2d8
--- /dev/null
+++ b/drivers/input/misc/gpio_decoder.c
@@ -0,0 +1,137 @@
1/*
2 * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * A generic driver to read multiple gpio lines and translate the
14 * encoded numeric value into an input event.
15 */
16
17#include <linux/device.h>
18#include <linux/gpio/consumer.h>
19#include <linux/input.h>
20#include <linux/input-polldev.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/platform_device.h>
25
26struct gpio_decoder {
27 struct input_polled_dev *poll_dev;
28 struct gpio_descs *input_gpios;
29 struct device *dev;
30 u32 axis;
31 u32 last_stable;
32};
33
34static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
35{
36 struct gpio_descs *gpios = decoder->input_gpios;
37 unsigned int ret = 0;
38 int i, val;
39
40 for (i = 0; i < gpios->ndescs; i++) {
41 val = gpiod_get_value_cansleep(gpios->desc[i]);
42 if (val < 0) {
43 dev_err(decoder->dev,
44 "Error reading gpio %d: %d\n",
45 desc_to_gpio(gpios->desc[i]), val);
46 return val;
47 }
48
49 val = !!val;
50 ret = (ret << 1) | val;
51 }
52
53 return ret;
54}
55
56static void gpio_decoder_poll_gpios(struct input_polled_dev *poll_dev)
57{
58 struct gpio_decoder *decoder = poll_dev->private;
59 int state;
60
61 state = gpio_decoder_get_gpios_state(decoder);
62 if (state >= 0 && state != decoder->last_stable) {
63 input_report_abs(poll_dev->input, decoder->axis, state);
64 input_sync(poll_dev->input);
65 decoder->last_stable = state;
66 }
67}
68
69static int gpio_decoder_probe(struct platform_device *pdev)
70{
71 struct device *dev = &pdev->dev;
72 struct gpio_decoder *decoder;
73 struct input_polled_dev *poll_dev;
74 u32 max;
75 int err;
76
77 decoder = devm_kzalloc(dev, sizeof(struct gpio_decoder), GFP_KERNEL);
78 if (!decoder)
79 return -ENOMEM;
80
81 device_property_read_u32(dev, "linux,axis", &decoder->axis);
82 decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
83 if (IS_ERR(decoder->input_gpios)) {
84 dev_err(dev, "unable to acquire input gpios\n");
85 return PTR_ERR(decoder->input_gpios);
86 }
87 if (decoder->input_gpios->ndescs < 2) {
88 dev_err(dev, "not enough gpios found\n");
89 return -EINVAL;
90 }
91
92 if (device_property_read_u32(dev, "decoder-max-value", &max))
93 max = (1U << decoder->input_gpios->ndescs) - 1;
94
95 decoder->dev = dev;
96 poll_dev = devm_input_allocate_polled_device(decoder->dev);
97 if (!poll_dev)
98 return -ENOMEM;
99
100 poll_dev->private = decoder;
101 poll_dev->poll = gpio_decoder_poll_gpios;
102 decoder->poll_dev = poll_dev;
103
104 poll_dev->input->name = pdev->name;
105 poll_dev->input->id.bustype = BUS_HOST;
106 input_set_abs_params(poll_dev->input, decoder->axis, 0, max, 0, 0);
107
108 err = input_register_polled_device(poll_dev);
109 if (err) {
110 dev_err(dev, "failed to register polled device\n");
111 return err;
112 }
113 platform_set_drvdata(pdev, decoder);
114
115 return 0;
116}
117
118#ifdef CONFIG_OF
119static const struct of_device_id gpio_decoder_of_match[] = {
120 { .compatible = "gpio-decoder", },
121 { },
122};
123MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
124#endif
125
126static struct platform_driver gpio_decoder_driver = {
127 .probe = gpio_decoder_probe,
128 .driver = {
129 .name = "gpio-decoder",
130 .of_match_table = of_match_ptr(gpio_decoder_of_match),
131 }
132};
133module_platform_driver(gpio_decoder_driver);
134
135MODULE_DESCRIPTION("GPIO decoder input driver");
136MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
137MODULE_LICENSE("GPL v2");