AM335x-ICE: Add support for rotary switch
Subsystems (3)
v3 cover AM335x-ICE: Add support for rotary switch
This series adds support for rotary-switch on AM335x-ICE that is connected to TI PCA9536 I2C GPIO expander.
First patch adds new generic driver to read status of group of GPIO lines and report the value as an input event. The second patch adds DT entries for the same.
v2: https://lkml.org/lkml/2016/8/23/111
v1: https://lkml.org/lkml/2016/8/12/7
Vignesh R (2):
input: misc: Add generic input driver to read encoded GPIO lines
ARM: dts: am335x-icev2: Add nodes for gpio-decoder
.../devicetree/bindings/input/gpio-decoder.txt | 23 ++++ arch/arm/boot/dts/am335x-icev2.dts | 9 ++ drivers/input/misc/Kconfig | 12 ++ drivers/input/misc/Makefile | 1 + drivers/input/misc/gpio_decoder.c | 134 +++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/gpio-decoder.txt create mode 100644 drivers/input/misc/gpio_decoder.c
Signature · 1 line
2.9.2
PATCH v3 1/2 input: misc: 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.
---
v3: Fix comments by Andrew and Dmitry
Link to v2: https://lkml.org/lkml/2016/8/23/79
.../devicetree/bindings/input/gpio-decoder.txt | 23 ++++ drivers/input/misc/Kconfig | 12 ++ drivers/input/misc/Makefile | 1 + drivers/input/misc/gpio_decoder.c | 134 +++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/gpio-decoder.txt create mode 100644 drivers/input/misc/gpio_decoder.c
Documentation/devicetree/bindings/input/gpio-decoder.txt new file +23 -0
@@ -0,0 +1,23 @@ +* GPIO Decoder DT bindings + +Required Properties: +- compatible: should be "gpio-decoder" +- gpios: a spec of gpios (at least two) to be decoded to a number with + first entry representing the MSB. + +Optional Properties: +- decoder-max-value: Maximum possible value that can be reported by + the gpios. +- linux,axis: the input subsystem axis to map to (ABS_X/ABS_Y). + Defaults to 0 (ABS_X). + +Example: + gpio-decoder0 { + compatible = "gpio-decoder"; + gpios = <&pca9536 3 GPIO_ACTIVE_HIGH>, + <&pca9536 2 GPIO_ACTIVE_HIGH>, + <&pca9536 1 GPIO_ACTIVE_HIGH>, + <&pca9536 0 GPIO_ACTIVE_HIGH>; + linux,axis = <0>; /* ABS_X */ + decoder-max-value = <9>; + };
drivers/input/misc/Kconfig +12 -0
@@ -292,6 +292,18 @@ config INPUT_GPIO_TILT_POLLED To compile this driver as a module, choose M here: the module will be called gpio_tilt_polled. +config INPUT_GPIO_DECODER + tristate "Polled GPIO Decoder Input driver" + depends on GPIOLIB || COMPILE_TEST + select INPUT_POLLDEV + help + Say Y here if you want driver to read status of multiple GPIO + lines and report the encoded value as an absolute integer to + input subsystem. + + To compile this driver as a module, choose M here: the module + will be called gpio_decoder. + config INPUT_IXP4XX_BEEPER tristate "IXP4XX Beeper support" depends on ARCH_IXP4XX
drivers/input/misc/Makefile +1 -0
@@ -35,6 +35,7 @@ obj-$(CONFIG_INPUT_DRV2667_HAPTICS) += drv2667.o obj-$(CONFIG_INPUT_GP2A) += gp2ap002a00f.o obj-$(CONFIG_INPUT_GPIO_BEEPER) += gpio-beeper.o obj-$(CONFIG_INPUT_GPIO_TILT_POLLED) += gpio_tilt_polled.o +obj-$(CONFIG_INPUT_GPIO_DECODER) += gpio_decoder.o obj-$(CONFIG_INPUT_HISI_POWERKEY) += hisi_powerkey.o obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o obj-$(CONFIG_INPUT_IMS_PCU) += ims-pcu.o
drivers/input/misc/gpio_decoder.c new file +134 -0
134 lines not on this page · raw message
Signature · 1 line
2.9.2
PATCH v3 2/2 ARM: dts: am335x-icev2: Add nodes for gpio-decoder
In reply to Vignesh RAM335x ICE board has a rotary-switch connected to PCA9536 I2C GPIO expander. The position of the rotary-switch is reflected by status of GPIO lines. Add gpio-decoder node to read these GPIO line status via gpio-decoder driver and report it as an input event to the system.
---
v3: s/decoder,max-value/decoder-max-value
arch/arm/boot/dts/am335x-icev2.dts | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/arm/boot/dts/am335x-icev2.dts +9 -0
@@ -139,6 +139,15 @@ default-state = "off"; }; }; + gpio-decoder { + compatible = "gpio-decoder"; + gpios = <&pca9536 3 GPIO_ACTIVE_HIGH>, + <&pca9536 2 GPIO_ACTIVE_HIGH>, + <&pca9536 1 GPIO_ACTIVE_HIGH>, + <&pca9536 0 GPIO_ACTIVE_HIGH>; + linux,axis = <0>; /* ABS_X */ + decoder-max-value = <9>; + }; }; &am33xx_pinmux {
Signature · 1 line
2.9.2
Hi,
Quoted 8 lines · Vignesh R
This series adds support for rotary-switch on AM335x-ICE that is connected to TI PCA9536 I2C GPIO expander.
First patch adds new generic driver to read status of group of GPIO lines and report the value as an input event. The second patch adds DT entries for the same.v2: https://lkml.org/lkml/2016/8/23/111
v1: https://lkml.org/lkml/2016/8/12/7
Is there a reason why the rotary-encoder driver cannot handle this? Commit 7dde4e74744 ("Input: rotary-encoder - support more than 2 gpios as input") added support for that mode AFAIU.
I copied the author of that patch to have a look.
Thanks,
Daniel
Hi,
Quoted 9 lines · Daniel Mack
Hi,
Quoted 6 lines · Vignesh R
This series adds support for rotary-switch on AM335x-ICE that is connected to TI PCA9536 I2C GPIO expander.
First patch adds new generic driver to read status of group of GPIO lines and report the value as an input event. The second patch adds DT entries for the same.
Quoted 7 lines
Quoted 2 lines
v2: https://lkml.org/lkml/2016/8/23/111
v1: https://lkml.org/lkml/2016/8/12/7Is there a reason why the rotary-encoder driver cannot handle this? Commit
7dde4e74744("Input: rotary-encoder - support more than 2 gpios as input") added support for that mode AFAIU.
Rotary encoder driver handles incremental encoders only and does not support absolute encoders. The rotary switch on am335x-ice is different from the incremental encoders in the
sense that GPIO line status directly reflect the position(number) pointed by the dial of the encoder. So, there is no need to count steps or know the direction of rotation as it does not matter.
I did try to enhance rotary-encoder driver to support absolute encoder[1] but the comment there was to write new driver that simply translates gpio-encoded value into ABS* event. Indeed, the new driver looks more simple and can handle more such hardwares.
[1] https://lkml.org/lkml/2016/5/19/98
Signature · 2 lines
Regards Vignesh
Quoted 23 lines · Vignesh R
Quoted 7 lines · Daniel Mack
Quoted 6 lines · Vignesh R
This series adds support for rotary-switch on AM335x-ICE that is connected to TI PCA9536 I2C GPIO expander.
First patch adds new generic driver to read status of group of GPIO lines and report the value as an input event. The second patch adds DT entries for the same.Quoted 7 lines
Quoted 2 lines
v2: https://lkml.org/lkml/2016/8/23/111
v1: https://lkml.org/lkml/2016/8/12/7Is there a reason why the rotary-encoder driver cannot handle this? Commit
7dde4e74744("Input: rotary-encoder - support more than 2 gpios as input") added support for that mode AFAIU.Rotary encoder driver handles incremental encoders only and does not support absolute encoders. The rotary switch on am335x-ice is different from the incremental encoders in the
sense that GPIO line status directly reflect the position(number) pointed by the dial of the encoder. So, there is no need to count steps or know the direction of rotation as it does not matter.
Quoted 4 lines
I did try to enhance rotary-encoder driver to support absolute encoder[1] but the comment there was to write new driver that simply translates gpio-encoded value into ABS* event. Indeed, the new driver looks more simple and can handle more such hardwares.
Okay. Yes, that makes sense. Thanks for the explanation!
Daniel
Dmitry Torokhov
maintainerRe: [PATCH v3 1/2] input: misc: Add generic input driver to read encoded GPIO lines
In reply to Vignesh RQuoted 134 lines · Vignesh R
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 <[email protected]>Acked-by Rob Herring <[email protected]>---
v3: Fix comments by Andrew and Dmitry
Link to v2: https://lkml.org/lkml/2016/8/23/79.../devicetree/bindings/input/gpio-decoder.txt | 23 ++++ drivers/input/misc/Kconfig | 12 ++ drivers/input/misc/Makefile | 1 + drivers/input/misc/gpio_decoder.c | 134 +++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/gpio-decoder.txt create mode 100644 drivers/input/misc/gpio_decoder.cDocumentation/devicetree/bindings/input/gpio-decoder.txt new file +23
-0@@ -0,0 +1,23 @@ +* GPIO Decoder DT bindings + +Required Properties: +- compatible: should be "gpio-decoder" +- gpios: a spec of gpios (at least two) to be decoded to a number with + first entry representing the MSB. + +Optional Properties: +- decoder-max-value: Maximum possible value that can be reported by + the gpios. +- linux,axis: the input subsystem axis to map to (ABS_X/ABS_Y). + Defaults to 0 (ABS_X). + +Example: + gpio-decoder0 { + compatible = "gpio-decoder"; + gpios = <&pca9536 3 GPIO_ACTIVE_HIGH>, + <&pca9536 2 GPIO_ACTIVE_HIGH>, + <&pca9536 1 GPIO_ACTIVE_HIGH>, + <&pca9536 0 GPIO_ACTIVE_HIGH>; + linux,axis = <0>; /* ABS_X */ + decoder-max-value = <9>; + };drivers/input/misc/Kconfig +12
-0@@ -292,6 +292,18 @@ config INPUT_GPIO_TILT_POLLED To compile this driver as a module, choose M here: the module will be called gpio_tilt_polled. +config INPUT_GPIO_DECODER + tristate "Polled GPIO Decoder Input driver" + depends on GPIOLIB || COMPILE_TEST + select INPUT_POLLDEV + help + Say Y here if you want driver to read status of multiple GPIO + lines and report the encoded value as an absolute integer to + input subsystem. + + To compile this driver as a module, choose M here: the module + will be called gpio_decoder. + config INPUT_IXP4XX_BEEPER tristate "IXP4XX Beeper support" depends on ARCH_IXP4XXdrivers/input/misc/Makefile +1
-0@@ -35,6 +35,7 @@ obj-$(CONFIG_INPUT_DRV2667_HAPTICS) += drv2667.o obj-$(CONFIG_INPUT_GP2A) += gp2ap002a00f.o obj-$(CONFIG_INPUT_GPIO_BEEPER) += gpio-beeper.o obj-$(CONFIG_INPUT_GPIO_TILT_POLLED) += gpio_tilt_polled.o +obj-$(CONFIG_INPUT_GPIO_DECODER) += gpio_decoder.o obj-$(CONFIG_INPUT_HISI_POWERKEY) += hisi_powerkey.o obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o obj-$(CONFIG_INPUT_IMS_PCU) += ims-pcu.odrivers/input/misc/gpio_decoder.c new file +37
-0@@ -0,0 +1,134 @@ +/* +* Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/ +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation version 2. +* +* This program is distributed "as is" WITHOUT ANY WARRANTY of any +* kind, whether express or implied; without even the implied warranty +* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* A generic driver to read multiple gpio lines and translate the +* encoded numeric value into an input event. +*/ + +#include <linux/device.h> +#include <linux/gpio/consumer.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> + +struct gpio_decoder { +struct input_polled_dev *poll_dev; +struct gpio_descs *input_gpios; +struct device *dev; +u32 axis; +u32 last_stable; +}; + +static unsigned int gpio_decoder_get_gpios_state (struct gpio_decoder + *decoder) +{ +struct gpio_descs *gpios = decoder->input_gpios;
drivers/input/misc/gpio_decoder.c line 47
Quoted 10 lines
drivers/input/misc/gpio_decoder.c new file +10
-0+unsigned int ret =0 ; +int i, val; + +for (i =0 ; i < gpios->ndescs; i++) { + val =gpiod_get_value_cansleep (gpios->desc[i]); +if (val >=0 ) { + ret = (ret <<1 ) | !!val; + }else { +dev_err (decoder->dev,"Error reading gpio\n" ); +break ;
I think if we fail reading any one of gpio lines we should skip reporting the event.
Quoted 33 lines
drivers/input/misc/gpio_decoder.c new file +33
-0+ } + } + +return ret; +} + +static void gpio_decoder_poll_gpios (struct input_polled_dev *poll_dev) +{ +struct gpio_decoder *decoder = poll_dev->private; +unsigned int state =gpio_decoder_get_gpios_state (decoder); + +if (state != decoder->last_stable) { +input_report_abs (poll_dev->input, decoder->axis, state); +input_sync (poll_dev->input); + decoder->last_stable = state; + } +} + +static int gpio_decoder_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct gpio_decoder *decoder; +struct input_polled_dev *poll_dev; +u32 max; +int err; + + decoder =devm_kzalloc (dev,sizeof (struct gpio_decoder ),GFP_KERNEL ); +if (!decoder) +return -ENOMEM ; + +device_property_read_u32 (dev,"linux,axis" , &decoder->axis); + decoder->input_gpios =devm_gpiod_get_array (dev,NULL ,GPIOD_IN ); +if (IS_ERR (decoder->input_gpios)) {
drivers/input/misc/gpio_decoder.c line 90
Quoted 10 lines
drivers/input/misc/gpio_decoder.c new file +10
-0+dev_err (dev,"unable to acquire input gpios\n" ); +return PTR_ERR (decoder->input_gpios); + } +if (decoder->input_gpios->ndescs <2 ) { +dev_err (dev,"not enough gpios found\n" ); +return -EINVAL ; + } + +if (device_property_read_u32 (dev,"decoder-max-value" , &max)) + max =BIT (decoder->input_gpios->ndescs);
Shouldn't this be
max = (1U << decoder->input_gpios->ndescs) - 1;
?
drivers/input/misc/gpio_decoder.c line 103
Quoted 13 lines
drivers/input/misc/gpio_decoder.c new file +13
-0+ + decoder->dev = dev; + poll_dev =devm_input_allocate_polled_device (decoder->dev); +if (!poll_dev) +return -ENOMEM ; + + poll_dev->private = decoder; + poll_dev->poll = gpio_decoder_poll_gpios; + decoder->poll_dev = poll_dev; + + poll_dev->input->name = pdev->name; + poll_dev->input->id.bustype =BUS_HOST ; +input_set_abs_params (poll_dev->input, decoder->axis,0 , max,0 ,1 );
Why do you need flat == 1? Do you actually use joydev for this device?
Quoted 34 lines
drivers/input/misc/gpio_decoder.c new file +31
-0+ + err =input_register_polled_device (poll_dev); +if (err) { +dev_err (dev,"failed to register polled device\n" ); +return err; + } +platform_set_drvdata (pdev, decoder); + +return 0 ; +} + +#ifdef CONFIG_OF +static const struct of_device_id gpio_decoder_of_match[] = { + { .compatible ="gpio-decoder" , }, + { }, +}; +MODULE_DEVICE_TABLE (of, gpio_decoder_of_match); +#endif + +static struct platform_driver gpio_decoder_driver = { + .probe = gpio_decoder_probe, + .driver = { + .name ="gpio-decoder" , + .of_match_table =of_match_ptr (gpio_decoder_of_match), + } +}; +module_platform_driver (gpio_decoder_driver); + +MODULE_DESCRIPTION ("GPIO decoder input driver" ); +MODULE_AUTHOR ("Vignesh R <[email protected]>" ); +MODULE_LICENSE ("GPL v2" );Signature · 1 line
2.9.2
No need to resubmit, I can adjust on my side.
Signature · 1 line
Dmitry
Re: [PATCH v3 1/2] input: misc: Add generic input driver to read encoded GPIO lines
Quoted 140 lines · Dmitry Torokhov
Quoted 139 lines · Vignesh R
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 <[email protected]>Acked-by Rob Herring <[email protected]>---
v3: Fix comments by Andrew and Dmitry
Link to v2: https://lkml.org/lkml/2016/8/23/79.../devicetree/bindings/input/gpio-decoder.txt | 23 ++++ drivers/input/misc/Kconfig | 12 ++ drivers/input/misc/Makefile | 1 + drivers/input/misc/gpio_decoder.c | 134 +++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/gpio-decoder.txt create mode 100644 drivers/input/misc/gpio_decoder.cDocumentation/devicetree/bindings/input/gpio-decoder.txt new file +23
-0@@ -0,0 +1,23 @@ +* GPIO Decoder DT bindings + +Required Properties: +- compatible: should be "gpio-decoder" +- gpios: a spec of gpios (at least two) to be decoded to a number with + first entry representing the MSB. + +Optional Properties: +- decoder-max-value: Maximum possible value that can be reported by + the gpios. +- linux,axis: the input subsystem axis to map to (ABS_X/ABS_Y). + Defaults to 0 (ABS_X). + +Example: + gpio-decoder0 { + compatible = "gpio-decoder"; + gpios = <&pca9536 3 GPIO_ACTIVE_HIGH>, + <&pca9536 2 GPIO_ACTIVE_HIGH>, + <&pca9536 1 GPIO_ACTIVE_HIGH>, + <&pca9536 0 GPIO_ACTIVE_HIGH>; + linux,axis = <0>; /* ABS_X */ + decoder-max-value = <9>; + };drivers/input/misc/Kconfig +12
-0@@ -292,6 +292,18 @@ config INPUT_GPIO_TILT_POLLED To compile this driver as a module, choose M here: the module will be called gpio_tilt_polled. +config INPUT_GPIO_DECODER + tristate "Polled GPIO Decoder Input driver" + depends on GPIOLIB || COMPILE_TEST + select INPUT_POLLDEV + help + Say Y here if you want driver to read status of multiple GPIO + lines and report the encoded value as an absolute integer to + input subsystem. + + To compile this driver as a module, choose M here: the module + will be called gpio_decoder. + config INPUT_IXP4XX_BEEPER tristate "IXP4XX Beeper support" depends on ARCH_IXP4XXdrivers/input/misc/Makefile +1
-0@@ -35,6 +35,7 @@ obj-$(CONFIG_INPUT_DRV2667_HAPTICS) += drv2667.o obj-$(CONFIG_INPUT_GP2A) += gp2ap002a00f.o obj-$(CONFIG_INPUT_GPIO_BEEPER) += gpio-beeper.o obj-$(CONFIG_INPUT_GPIO_TILT_POLLED) += gpio_tilt_polled.o +obj-$(CONFIG_INPUT_GPIO_DECODER) += gpio_decoder.o obj-$(CONFIG_INPUT_HISI_POWERKEY) += hisi_powerkey.o obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o obj-$(CONFIG_INPUT_IMS_PCU) += ims-pcu.odrivers/input/misc/gpio_decoder.c new file +42
-0@@ -0,0 +1,134 @@ +/* +* Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/ +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation version 2. +* +* This program is distributed "as is" WITHOUT ANY WARRANTY of any +* kind, whether express or implied; without even the implied warranty +* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* A generic driver to read multiple gpio lines and translate the +* encoded numeric value into an input event. +*/ + +#include <linux/device.h> +#include <linux/gpio/consumer.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> + +struct gpio_decoder { +struct input_polled_dev *poll_dev; +struct gpio_descs *input_gpios; +struct device *dev; +u32 axis; +u32 last_stable; +}; + +static unsigned int gpio_decoder_get_gpios_state (struct gpio_decoder + *decoder) +{ +struct gpio_descs *gpios = decoder->input_gpios; +unsigned int ret =0 ; +int i, val; + +for (i =0 ; i < gpios->ndescs; i++) { + val =gpiod_get_value_cansleep (gpios->desc[i]);
Quoted 9 lines
drivers/input/misc/gpio_decoder.c line 47
Quoted 5 lines
drivers/input/misc/gpio_decoder.c new file +5
-0+if (val >=0 ) { + ret = (ret <<1 ) | !!val; + }else { +dev_err (decoder->dev,"Error reading gpio\n" ); +break ;I think if we fail reading any one of gpio lines we should skip reporting the event.
Ok.
Quoted 46 lines
Quoted 33 lines
drivers/input/misc/gpio_decoder.c new file +33
-0+ } + } + +return ret; +} + +static void gpio_decoder_poll_gpios (struct input_polled_dev *poll_dev) +{ +struct gpio_decoder *decoder = poll_dev->private; +unsigned int state =gpio_decoder_get_gpios_state (decoder); + +if (state != decoder->last_stable) { +input_report_abs (poll_dev->input, decoder->axis, state); +input_sync (poll_dev->input); + decoder->last_stable = state; + } +} + +static int gpio_decoder_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct gpio_decoder *decoder; +struct input_polled_dev *poll_dev; +u32 max; +int err; + + decoder =devm_kzalloc (dev,sizeof (struct gpio_decoder ),GFP_KERNEL ); +if (!decoder) +return -ENOMEM ; + +device_property_read_u32 (dev,"linux,axis" , &decoder->axis); + decoder->input_gpios =devm_gpiod_get_array (dev,NULL ,GPIOD_IN ); +if (IS_ERR (decoder->input_gpios)) {drivers/input/misc/gpio_decoder.c line 90
Quoted 10 lines
drivers/input/misc/gpio_decoder.c new file +10
-0+dev_err (dev,"unable to acquire input gpios\n" ); +return PTR_ERR (decoder->input_gpios); + } +if (decoder->input_gpios->ndescs <2 ) { +dev_err (dev,"not enough gpios found\n" ); +return -EINVAL ; + } + +if (device_property_read_u32 (dev,"decoder-max-value" , &max)) + max =BIT (decoder->input_gpios->ndescs);Shouldn't this be
Quoted 3 lines
max = (1U << decoder->input_gpios->ndescs) - 1;?
Oops, you are right.
Quoted 17 lines
Quoted 4 lines
drivers/input/misc/gpio_decoder.c new file +3
-0+ + decoder->dev = dev; + poll_dev =devm_input_allocate_polled_device (decoder->dev);drivers/input/misc/gpio_decoder.c line 103
Quoted 10 lines
drivers/input/misc/gpio_decoder.c new file +10
-0+if (!poll_dev) +return -ENOMEM ; + + poll_dev->private = decoder; + poll_dev->poll = gpio_decoder_poll_gpios; + decoder->poll_dev = poll_dev; + + poll_dev->input->name = pdev->name; + poll_dev->input->id.bustype =BUS_HOST ; +input_set_abs_params (poll_dev->input, decoder->axis,0 , max,0 ,1 );Why do you need flat == 1? Do you actually use joydev for this device?
Sorry, I overlooked flat param when I copied this line from rotary-encoder.c.
Quoted 28 lines
Quoted 28 lines
drivers/input/misc/gpio_decoder.c new file +28
-0+ + err =input_register_polled_device (poll_dev); +if (err) { +dev_err (dev,"failed to register polled device\n" ); +return err; + } +platform_set_drvdata (pdev, decoder); + +return 0 ; +} + +#ifdef CONFIG_OF +static const struct of_device_id gpio_decoder_of_match[] = { + { .compatible ="gpio-decoder" , }, + { }, +}; +MODULE_DEVICE_TABLE (of, gpio_decoder_of_match); +#endif + +static struct platform_driver gpio_decoder_driver = { + .probe = gpio_decoder_probe, + .driver = { + .name ="gpio-decoder" , + .of_match_table =of_match_ptr (gpio_decoder_of_match), + } +}; +module_platform_driver (gpio_decoder_driver); +
Quoted 8 lines
Quoted 6 lines
drivers/input/misc/gpio_decoder.c new file +3
-0+MODULE_DESCRIPTION ("GPIO decoder input driver" ); +MODULE_AUTHOR ("Vignesh R <[email protected]>" ); +MODULE_LICENSE ("GPL v2" );Signature · 1 line
2.9.2No need to resubmit, I can adjust on my side.
Thanks a lot!
Signature · 2 lines
Regards Vignesh
Re: [PATCH v3 1/2] input: misc: Add generic input driver to read encoded GPIO lines
Quoted 265 lines · Vignesh R
Quoted 140 lines · Dmitry Torokhov
Quoted 139 lines · Vignesh R
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 <[email protected]>Acked-by Rob Herring <[email protected]>---
v3: Fix comments by Andrew and Dmitry
Link to v2: https://lkml.org/lkml/2016/8/23/79.../devicetree/bindings/input/gpio-decoder.txt | 23 ++++ drivers/input/misc/Kconfig | 12 ++ drivers/input/misc/Makefile | 1 + drivers/input/misc/gpio_decoder.c | 134 +++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 Documentation/devicetree/bindings/input/gpio-decoder.txt create mode 100644 drivers/input/misc/gpio_decoder.cDocumentation/devicetree/bindings/input/gpio-decoder.txt new file +23
-0@@ -0,0 +1,23 @@ +* GPIO Decoder DT bindings + +Required Properties: +- compatible: should be "gpio-decoder" +- gpios: a spec of gpios (at least two) to be decoded to a number with + first entry representing the MSB. + +Optional Properties: +- decoder-max-value: Maximum possible value that can be reported by + the gpios. +- linux,axis: the input subsystem axis to map to (ABS_X/ABS_Y). + Defaults to 0 (ABS_X). + +Example: + gpio-decoder0 { + compatible = "gpio-decoder"; + gpios = <&pca9536 3 GPIO_ACTIVE_HIGH>, + <&pca9536 2 GPIO_ACTIVE_HIGH>, + <&pca9536 1 GPIO_ACTIVE_HIGH>, + <&pca9536 0 GPIO_ACTIVE_HIGH>; + linux,axis = <0>; /* ABS_X */ + decoder-max-value = <9>; + };drivers/input/misc/Kconfig +12
-0@@ -292,6 +292,18 @@ config INPUT_GPIO_TILT_POLLED To compile this driver as a module, choose M here: the module will be called gpio_tilt_polled. +config INPUT_GPIO_DECODER + tristate "Polled GPIO Decoder Input driver" + depends on GPIOLIB || COMPILE_TEST + select INPUT_POLLDEV + help + Say Y here if you want driver to read status of multiple GPIO + lines and report the encoded value as an absolute integer to + input subsystem. + + To compile this driver as a module, choose M here: the module + will be called gpio_decoder. + config INPUT_IXP4XX_BEEPER tristate "IXP4XX Beeper support" depends on ARCH_IXP4XXdrivers/input/misc/Makefile +1
-0@@ -35,6 +35,7 @@ obj-$(CONFIG_INPUT_DRV2667_HAPTICS) += drv2667.o obj-$(CONFIG_INPUT_GP2A) += gp2ap002a00f.o obj-$(CONFIG_INPUT_GPIO_BEEPER) += gpio-beeper.o obj-$(CONFIG_INPUT_GPIO_TILT_POLLED) += gpio_tilt_polled.o +obj-$(CONFIG_INPUT_GPIO_DECODER) += gpio_decoder.o obj-$(CONFIG_INPUT_HISI_POWERKEY) += hisi_powerkey.o obj-$(CONFIG_HP_SDC_RTC) += hp_sdc_rtc.o obj-$(CONFIG_INPUT_IMS_PCU) += ims-pcu.odrivers/input/misc/gpio_decoder.c new file +42
-0@@ -0,0 +1,134 @@ +/* +* Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/ +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation version 2. +* +* This program is distributed "as is" WITHOUT ANY WARRANTY of any +* kind, whether express or implied; without even the implied warranty +* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* A generic driver to read multiple gpio lines and translate the +* encoded numeric value into an input event. +*/ + +#include <linux/device.h> +#include <linux/gpio/consumer.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> + +struct gpio_decoder { +struct input_polled_dev *poll_dev; +struct gpio_descs *input_gpios; +struct device *dev; +u32 axis; +u32 last_stable; +}; + +static unsigned int gpio_decoder_get_gpios_state (struct gpio_decoder + *decoder) +{ +struct gpio_descs *gpios = decoder->input_gpios; +unsigned int ret =0 ; +int i, val; + +for (i =0 ; i < gpios->ndescs; i++) { + val =gpiod_get_value_cansleep (gpios->desc[i]);Quoted 9 lines
drivers/input/misc/gpio_decoder.c line 47
Quoted 5 lines
drivers/input/misc/gpio_decoder.c new file +5
-0+if (val >=0 ) { + ret = (ret <<1 ) | !!val; + }else { +dev_err (decoder->dev,"Error reading gpio\n" ); +break ;I think if we fail reading any one of gpio lines we should skip reporting the event.
Ok.
Quoted 46 lines
Quoted 33 lines
drivers/input/misc/gpio_decoder.c new file +33
-0+ } + } + +return ret; +} + +static void gpio_decoder_poll_gpios (struct input_polled_dev *poll_dev) +{ +struct gpio_decoder *decoder = poll_dev->private; +unsigned int state =gpio_decoder_get_gpios_state (decoder); + +if (state != decoder->last_stable) { +input_report_abs (poll_dev->input, decoder->axis, state); +input_sync (poll_dev->input); + decoder->last_stable = state; + } +} + +static int gpio_decoder_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct gpio_decoder *decoder; +struct input_polled_dev *poll_dev; +u32 max; +int err; + + decoder =devm_kzalloc (dev,sizeof (struct gpio_decoder ),GFP_KERNEL ); +if (!decoder) +return -ENOMEM ; + +device_property_read_u32 (dev,"linux,axis" , &decoder->axis); + decoder->input_gpios =devm_gpiod_get_array (dev,NULL ,GPIOD_IN ); +if (IS_ERR (decoder->input_gpios)) {drivers/input/misc/gpio_decoder.c line 90
Quoted 10 lines
drivers/input/misc/gpio_decoder.c new file +10
-0+dev_err (dev,"unable to acquire input gpios\n" ); +return PTR_ERR (decoder->input_gpios); + } +if (decoder->input_gpios->ndescs <2 ) { +dev_err (dev,"not enough gpios found\n" ); +return -EINVAL ; + } + +if (device_property_read_u32 (dev,"decoder-max-value" , &max)) + max =BIT (decoder->input_gpios->ndescs);Shouldn't this be
Quoted 3 lines
max = (1U << decoder->input_gpios->ndescs) - 1;?
Oops, you are right.
Quoted 17 lines
Quoted 4 lines
drivers/input/misc/gpio_decoder.c new file +3
-0+ + decoder->dev = dev; + poll_dev =devm_input_allocate_polled_device (decoder->dev);drivers/input/misc/gpio_decoder.c line 103
Quoted 10 lines
drivers/input/misc/gpio_decoder.c new file +10
-0+if (!poll_dev) +return -ENOMEM ; + + poll_dev->private = decoder; + poll_dev->poll = gpio_decoder_poll_gpios; + decoder->poll_dev = poll_dev; + + poll_dev->input->name = pdev->name; + poll_dev->input->id.bustype =BUS_HOST ; +input_set_abs_params (poll_dev->input, decoder->axis,0 , max,0 ,1 );Why do you need flat == 1? Do you actually use joydev for this device?
Sorry, I overlooked flat param when I copied this line from
rotary-encoder.c.Quoted 28 lines
Quoted 28 lines
drivers/input/misc/gpio_decoder.c new file +28
-0+ + err =input_register_polled_device (poll_dev); +if (err) { +dev_err (dev,"failed to register polled device\n" ); +return err; + } +platform_set_drvdata (pdev, decoder); + +return 0 ; +} + +#ifdef CONFIG_OF +static const struct of_device_id gpio_decoder_of_match[] = { + { .compatible ="gpio-decoder" , }, + { }, +}; +MODULE_DEVICE_TABLE (of, gpio_decoder_of_match); +#endif + +static struct platform_driver gpio_decoder_driver = { + .probe = gpio_decoder_probe, + .driver = { + .name ="gpio-decoder" , + .of_match_table =of_match_ptr (gpio_decoder_of_match), + } +}; +module_platform_driver (gpio_decoder_driver); +Quoted 8 lines
Quoted 6 lines
drivers/input/misc/gpio_decoder.c new file +3
-0+MODULE_DESCRIPTION ("GPIO decoder input driver" ); +MODULE_AUTHOR ("Vignesh R <[email protected]>" ); +MODULE_LICENSE ("GPL v2" );Signature · 1 line
2.9.2No need to resubmit, I can adjust on my side.
Quoted 1 line
Thanks a lot!
Applied, thank you.
DTS change should go through some other tree though.
Signature · 1 line
Dmitry
Re: [PATCH v3 1/2] input: misc: Add generic input driver to read encoded GPIO lines
Quoted 3 lines · Dmitry Torokhov
Applied, thank you.
DTS change should go through some other tree though.
Thanks I'm picking up the dts patch into omap-for-v4.9/dt.
Tony
Hello,
Quoted 16 lines · Vignesh R
Quoted 7 lines · Daniel Mack
Quoted 6 lines · Vignesh R
This series adds support for rotary-switch on AM335x-ICE that is connected to TI PCA9536 I2C GPIO expander.
First patch adds new generic driver to read status of group of GPIO lines and report the value as an input event. The second patch adds DT entries for the same.Quoted 7 lines
Quoted 2 lines
v2: https://lkml.org/lkml/2016/8/23/111
v1: https://lkml.org/lkml/2016/8/12/7Is there a reason why the rotary-encoder driver cannot handle this? Commit
7dde4e74744("Input: rotary-encoder - support more than 2 gpios as input") added support for that mode AFAIU.
Quoted 6 lines
Rotary encoder driver handles incremental encoders only and does not support absolute encoders. The rotary switch on am335x-ice is different from the incremental encoders in the
sense that GPIO line status directly reflect the position(number) pointed by the dial of the encoder. So, there is no need to count steps or know the direction of rotation as it does not matter.
I'd still prefer to expand drivers/input/misc/rotary_encoder.c to handle this. Yes, there is no reason to count steps or determine the direction of a rotation in this case, but still there is much code to share and IMHO it's ok that a driver that handles several types of similar devices does too much for some special cases.
Quoted 4 lines
I did try to enhance rotary-encoder driver to support absolute encoder[1] but the comment there was to write new driver that simply translates gpio-encoded value into ABS* event. Indeed, the new driver looks more simple and can handle more such hardwares.
Which type of hardware can be handled by your driver that the generic rotary encoder cannot? I guess it's just a matter of a (simple) patch?
Best regards
Uwe
Quoted 1 line
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-K?nig | Industrial Linux Solutions | http://www.pengutronix.de/ |