2 * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
13 * A generic driver to read multiple gpio lines and translate the
14 * encoded numeric value into an input event.
17 #include <linux/device.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/input.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/platform_device.h>
26 struct gpio_descs
*input_gpios
;
32 static int gpio_decoder_get_gpios_state(struct gpio_decoder
*decoder
)
34 struct gpio_descs
*gpios
= decoder
->input_gpios
;
38 for (i
= 0; i
< gpios
->ndescs
; i
++) {
39 val
= gpiod_get_value_cansleep(gpios
->desc
[i
]);
42 "Error reading gpio %d: %d\n",
43 desc_to_gpio(gpios
->desc
[i
]), val
);
48 ret
= (ret
<< 1) | val
;
54 static void gpio_decoder_poll_gpios(struct input_dev
*input
)
56 struct gpio_decoder
*decoder
= input_get_drvdata(input
);
59 state
= gpio_decoder_get_gpios_state(decoder
);
60 if (state
>= 0 && state
!= decoder
->last_stable
) {
61 input_report_abs(input
, decoder
->axis
, state
);
63 decoder
->last_stable
= state
;
67 static int gpio_decoder_probe(struct platform_device
*pdev
)
69 struct device
*dev
= &pdev
->dev
;
70 struct gpio_decoder
*decoder
;
71 struct input_dev
*input
;
75 decoder
= devm_kzalloc(dev
, sizeof(*decoder
), GFP_KERNEL
);
80 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
);
88 if (decoder
->input_gpios
->ndescs
< 2) {
89 dev_err(dev
, "not enough gpios found\n");
93 if (device_property_read_u32(dev
, "decoder-max-value", &max
))
94 max
= (1U << decoder
->input_gpios
->ndescs
) - 1;
96 input
= devm_input_allocate_device(dev
);
100 input_set_drvdata(input
, decoder
);
102 input
->name
= pdev
->name
;
103 input
->id
.bustype
= BUS_HOST
;
104 input_set_abs_params(input
, decoder
->axis
, 0, max
, 0, 0);
106 err
= input_setup_polling(input
, gpio_decoder_poll_gpios
);
108 dev_err(dev
, "failed to set up polling\n");
112 err
= input_register_device(input
);
114 dev_err(dev
, "failed to register input device\n");
122 static const struct of_device_id gpio_decoder_of_match
[] = {
123 { .compatible
= "gpio-decoder", },
126 MODULE_DEVICE_TABLE(of
, gpio_decoder_of_match
);
129 static struct platform_driver gpio_decoder_driver
= {
130 .probe
= gpio_decoder_probe
,
132 .name
= "gpio-decoder",
133 .of_match_table
= of_match_ptr(gpio_decoder_of_match
),
136 module_platform_driver(gpio_decoder_driver
);
138 MODULE_DESCRIPTION("GPIO decoder input driver");
139 MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
140 MODULE_LICENSE("GPL v2");