Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 69464161 authored by Vignesh R's avatar Vignesh R Committed by Dmitry Torokhov
Browse files

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: default avatarVignesh R <vigneshr@ti.com>
Acked-by: default avatarRob Herring <robh@kernel.org>
Signed-off-by: default avatarDmitry Torokhov <dmitry.torokhov@gmail.com>
parent 68077264
Loading
Loading
Loading
Loading
+23 −0
Original line number Original line Diff line number Diff line
* 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>;
	};
+12 −0
Original line number Original line Diff line number Diff line
@@ -292,6 +292,18 @@ config INPUT_GPIO_TILT_POLLED
	  To compile this driver as a module, choose M here: the
	  To compile this driver as a module, choose M here: the
	  module will be called gpio_tilt_polled.
	  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
config INPUT_IXP4XX_BEEPER
	tristate "IXP4XX Beeper support"
	tristate "IXP4XX Beeper support"
	depends on ARCH_IXP4XX
	depends on ARCH_IXP4XX
+1 −0
Original line number Original line Diff line number Diff line
@@ -35,6 +35,7 @@ obj-$(CONFIG_INPUT_DRV2667_HAPTICS) += drv2667.o
obj-$(CONFIG_INPUT_GP2A)		+= gp2ap002a00f.o
obj-$(CONFIG_INPUT_GP2A)		+= gp2ap002a00f.o
obj-$(CONFIG_INPUT_GPIO_BEEPER)		+= gpio-beeper.o
obj-$(CONFIG_INPUT_GPIO_BEEPER)		+= gpio-beeper.o
obj-$(CONFIG_INPUT_GPIO_TILT_POLLED)	+= gpio_tilt_polled.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_INPUT_HISI_POWERKEY)	+= hisi_powerkey.o
obj-$(CONFIG_HP_SDC_RTC)		+= hp_sdc_rtc.o
obj-$(CONFIG_HP_SDC_RTC)		+= hp_sdc_rtc.o
obj-$(CONFIG_INPUT_IMS_PCU)		+= ims-pcu.o
obj-$(CONFIG_INPUT_IMS_PCU)		+= ims-pcu.o
+137 −0
Original line number Original line Diff line number Diff line
/*
 * 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 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]);
		if (val < 0) {
			dev_err(decoder->dev,
				"Error reading gpio %d: %d\n",
				desc_to_gpio(gpios->desc[i]), val);
			return val;
		}

		val = !!val;
		ret = (ret << 1) | val;
	}

	return ret;
}

static void gpio_decoder_poll_gpios(struct input_polled_dev *poll_dev)
{
	struct gpio_decoder *decoder = poll_dev->private;
	int state;

	state = gpio_decoder_get_gpios_state(decoder);
	if (state >= 0 && 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)) {
		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 = (1U << decoder->input_gpios->ndescs) - 1;

	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, 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 <vigneshr@ti.com>");
MODULE_LICENSE("GPL v2");