Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 * A generic driver to read multiple gpio lines and translate the
6 * encoded numeric value into an input event.
7 */
8
9#include <linux/bitmap.h>
10#include <linux/dev_printk.h>
11#include <linux/device/devres.h>
12#include <linux/err.h>
13#include <linux/gpio/consumer.h>
14#include <linux/input.h>
15#include <linux/minmax.h>
16#include <linux/mod_devicetable.h>
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/property.h>
20#include <linux/types.h>
21
22struct gpio_decoder {
23 struct gpio_descs *input_gpios;
24 struct device *dev;
25 u32 axis;
26 u32 last_stable;
27};
28
29static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
30{
31 struct gpio_descs *gpios = decoder->input_gpios;
32 DECLARE_BITMAP(values, 32);
33 unsigned int size;
34 int err;
35
36 size = min(gpios->ndescs, 32U);
37 err = gpiod_get_array_value_cansleep(size, gpios->desc, gpios->info, values);
38 if (err) {
39 dev_err(decoder->dev, "Error reading GPIO: %d\n", err);
40 return err;
41 }
42
43 return bitmap_read(values, 0, size);
44}
45
46static void gpio_decoder_poll_gpios(struct input_dev *input)
47{
48 struct gpio_decoder *decoder = input_get_drvdata(input);
49 int state;
50
51 state = gpio_decoder_get_gpios_state(decoder);
52 if (state >= 0 && state != decoder->last_stable) {
53 input_report_abs(input, decoder->axis, state);
54 input_sync(input);
55 decoder->last_stable = state;
56 }
57}
58
59static int gpio_decoder_probe(struct platform_device *pdev)
60{
61 struct device *dev = &pdev->dev;
62 struct gpio_decoder *decoder;
63 struct input_dev *input;
64 u32 max;
65 int err;
66
67 decoder = devm_kzalloc(dev, sizeof(*decoder), GFP_KERNEL);
68 if (!decoder)
69 return -ENOMEM;
70
71 decoder->dev = dev;
72 device_property_read_u32(dev, "linux,axis", &decoder->axis);
73
74 decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
75 if (IS_ERR(decoder->input_gpios))
76 return dev_err_probe(dev, PTR_ERR(decoder->input_gpios),
77 "unable to acquire input gpios\n");
78
79 if (decoder->input_gpios->ndescs < 2)
80 return dev_err_probe(dev, -EINVAL, "not enough gpios found\n");
81
82 if (decoder->input_gpios->ndescs > 31)
83 return dev_err_probe(dev, -EINVAL, "too many gpios found\n");
84
85 if (device_property_read_u32(dev, "decoder-max-value", &max))
86 max = BIT(decoder->input_gpios->ndescs) - 1;
87
88 input = devm_input_allocate_device(dev);
89 if (!input)
90 return -ENOMEM;
91
92 input_set_drvdata(input, decoder);
93
94 input->name = pdev->name;
95 input->id.bustype = BUS_HOST;
96 input_set_abs_params(input, decoder->axis, 0, max, 0, 0);
97
98 err = input_setup_polling(input, gpio_decoder_poll_gpios);
99 if (err)
100 return dev_err_probe(dev, err, "failed to set up polling\n");
101
102 err = input_register_device(input);
103 if (err)
104 return dev_err_probe(dev, err, "failed to register input device\n");
105
106 return 0;
107}
108
109static const struct of_device_id gpio_decoder_of_match[] = {
110 { .compatible = "gpio-decoder", },
111 { }
112};
113MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
114
115static struct platform_driver gpio_decoder_driver = {
116 .probe = gpio_decoder_probe,
117 .driver = {
118 .name = "gpio-decoder",
119 .of_match_table = gpio_decoder_of_match,
120 }
121};
122module_platform_driver(gpio_decoder_driver);
123
124MODULE_DESCRIPTION("GPIO decoder input driver");
125MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
126MODULE_LICENSE("GPL v2");