From: Kamel Bouhara <kamel.bouhara@bootlin.com>
To: Dmitry Torokhov <dmitry.torokhov@gmail.com>,
William Breathitt Gray <vilhelm.gray@gmail.com>,
Rob Herring <robh+dt@kernel.org>,
Mark Rutland <mark.rutland@arm.com>
Cc: devicetree@vger.kernel.org,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Kamel Bouhara <kamel.bouhara@bootlin.com>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
Ludovic Desroches <ludovic.desroches@microchip.com>,
Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
linux-input@vger.kernel.org,
linux-arm-kernel@lists.infradead.org
Subject: [PATCH 3/3] Input: add a rotary encoders based on counter devices
Date: Mon, 6 Apr 2020 17:58:06 +0200 [thread overview]
Message-ID: <20200406155806.1295169-4-kamel.bouhara@bootlin.com> (raw)
In-Reply-To: <20200406155806.1295169-1-kamel.bouhara@bootlin.com>
This add support for rotary encoders that use the counter subsystem to
expose an input device.
Signed-off-by: Kamel Bouhara <kamel.bouhara@bootlin.com>
---
drivers/input/misc/Kconfig | 9 ++
drivers/input/misc/Makefile | 1 +
drivers/input/misc/rotary_encoder_counter.c | 152 ++++++++++++++++++++
3 files changed, 162 insertions(+)
create mode 100644 drivers/input/misc/rotary_encoder_counter.c
diff --git a/drivers/input/misc/Kconfig b/drivers/input/misc/Kconfig
index 7e2e658d551c..b91b4257e337 100644
--- a/drivers/input/misc/Kconfig
+++ b/drivers/input/misc/Kconfig
@@ -619,6 +619,15 @@ config INPUT_GPIO_ROTARY_ENCODER
To compile this driver as a module, choose M here: the
module will be called rotary_encoder.
+config INPUT_COUNTER_ROTARY_ENCODER
+ tristate "Rotary encoders connected to counter devices"
+ depends on COUNTER || COMPILE_TEST
+ help
+ Say Y here to add support for rotary encoders connected to counter devices.
+
+ To compile this driver as a module, choose M here: the
+ module will be called rotary_encoder_counter.
+
config INPUT_RB532_BUTTON
tristate "Mikrotik Routerboard 532 button interface"
depends on MIKROTIK_RB532
diff --git a/drivers/input/misc/Makefile b/drivers/input/misc/Makefile
index 8fd187f314bd..74bbe6d400a3 100644
--- a/drivers/input/misc/Makefile
+++ b/drivers/input/misc/Makefile
@@ -68,6 +68,7 @@ obj-$(CONFIG_INPUT_REGULATOR_HAPTIC) += regulator-haptic.o
obj-$(CONFIG_INPUT_RETU_PWRBUTTON) += retu-pwrbutton.o
obj-$(CONFIG_INPUT_AXP20X_PEK) += axp20x-pek.o
obj-$(CONFIG_INPUT_GPIO_ROTARY_ENCODER) += rotary_encoder.o
+obj-$(CONFIG_INPUT_COUNTER_ROTARY_ENCODER) += rotary_encoder_counter.o
obj-$(CONFIG_INPUT_RK805_PWRKEY) += rk805-pwrkey.o
obj-$(CONFIG_INPUT_SC27XX_VIBRA) += sc27xx-vibra.o
obj-$(CONFIG_INPUT_SGI_BTNS) += sgi_btns.o
diff --git a/drivers/input/misc/rotary_encoder_counter.c b/drivers/input/misc/rotary_encoder_counter.c
new file mode 100644
index 000000000000..20017308f4f3
--- /dev/null
+++ b/drivers/input/misc/rotary_encoder_counter.c
@@ -0,0 +1,152 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * A rotary encoder driver using the generic counter interface.
+ *
+ * Author: Kamel Bouhara <kamel.bouhara@bootlin.com>
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/input.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/counter.h>
+#include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/pm.h>
+#include <linux/property.h>
+
+#define MAX_STEPS 24
+
+struct rotary_encoder {
+ struct input_dev *input;
+ u32 steps;
+ u32 axis;
+ bool relative_axis;
+ bool rollover;
+ long last_pos;
+ struct counter_device *counter;
+};
+
+static void rotary_encoder_poll(struct input_dev *input)
+{
+ struct rotary_encoder *encoder = input_get_drvdata(input);
+ long rotary_pos;
+ int ret;
+
+ ret = counter_count_get(encoder->counter, &rotary_pos);
+ if (ret)
+ return;
+
+ if (encoder->relative_axis) {
+ input_report_rel(encoder->input, encoder->axis,
+ rotary_pos - encoder->last_pos);
+ } else {
+ if (encoder->rollover)
+ rotary_pos %= encoder->steps;
+ input_report_abs(encoder->input, encoder->axis, rotary_pos);
+ }
+
+ encoder->last_pos = rotary_pos;
+ input_sync(encoder->input);
+}
+
+static int rotary_encoder_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct counter_device *counter;
+ struct rotary_encoder *encoder;
+ struct input_dev *input;
+ int qdec_mode;
+ u32 poll_interval;
+ int err;
+
+ encoder = devm_kzalloc(dev, sizeof(struct rotary_encoder), GFP_KERNEL);
+ if (!encoder)
+ return -ENOMEM;
+
+ encoder->rollover =
+ device_property_read_bool(dev, "rollover");
+
+ device_property_read_u32(dev, "steps", &encoder->steps);
+
+ device_property_read_u32(dev, "linux-axis", &encoder->axis);
+
+ encoder->relative_axis =
+ device_property_read_bool(dev, "relative-axis");
+
+ counter = devm_counter_get(dev);
+ if (IS_ERR(counter))
+ return PTR_ERR(counter);
+
+ if (device_property_read_u32(dev, "qdec-mode", &qdec_mode)) {
+ dev_err(dev, "Invalid or missing quadrature mode\n");
+ return -EINVAL;
+ }
+
+ err = counter_function_set(counter, qdec_mode);
+ if (err) {
+ dev_err(dev, "Failed to set quadrature mode %d\n",
+ qdec_mode);
+ return err;
+ }
+
+ input = devm_input_allocate_device(dev);
+ if (!input)
+ return -ENOMEM;
+
+ input_set_drvdata(input, encoder);
+ encoder->input = input;
+ encoder->counter = counter;
+ encoder->steps = (!encoder->steps) ? MAX_STEPS : encoder->steps;
+ input->name = pdev->name;
+ input->id.bustype = BUS_HOST;
+ input->dev.parent = dev;
+
+ if (encoder->relative_axis)
+ input_set_capability(input, EV_REL, encoder->axis);
+ else
+ input_set_abs_params(input, encoder->axis, 0,
+ encoder->steps, 0, 1);
+
+ err = input_setup_polling(input, rotary_encoder_poll);
+ if (err)
+ return err;
+
+ if (!device_property_read_u32(dev, "poll-interval",
+ &poll_interval))
+ input_set_poll_interval(input, poll_interval);
+
+ err = input_register_device(input);
+ if (err) {
+ dev_err(dev, "failed to register device, err=%d\n", err);
+ return err;
+ }
+
+ platform_set_drvdata(pdev, encoder);
+
+ return 0;
+}
+
+#ifdef CONFIG_OF
+static const struct of_device_id rotary_encoder_of_match[] = {
+ { .compatible = "rotary-encoder-counter", },
+ { /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(of, rotary_encoder_of_match);
+#endif
+
+static struct platform_driver rotary_encoder_driver = {
+ .probe = rotary_encoder_probe,
+ .driver = {
+ .name = "rotary-encoder-counter",
+ .of_match_table = of_match_ptr(rotary_encoder_of_match),
+ }
+};
+module_platform_driver(rotary_encoder_driver);
+
+MODULE_DESCRIPTION("Counter rotary encoder driver");
+MODULE_AUTHOR("Kamel Bouhara <kamel.bouhara@bootlin.com>");
+MODULE_LICENSE("GPL v2");
--
2.25.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2020-04-06 15:59 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-06 15:58 [PATCH 0/3] Introduce a counter inkernel API Kamel Bouhara
2020-04-06 15:58 ` [PATCH 1/3] counter: add an " Kamel Bouhara
2020-04-10 17:34 ` Randy Dunlap
2020-04-11 9:58 ` Kamel Bouhara
2020-04-06 15:58 ` [PATCH 2/3] Input: rotary-encoder-counter: add DT bindings Kamel Bouhara
2020-04-07 9:41 ` Maxime Ripard
2020-04-07 11:03 ` Kamel Bouhara
2020-04-07 14:22 ` Maxime Ripard
2020-04-07 14:55 ` Kamel Bouhara
2020-04-09 22:21 ` Dmitry Torokhov
2020-04-09 22:39 ` Alexandre Belloni
2020-04-09 23:46 ` Dmitry Torokhov
2020-04-11 10:43 ` Kamel Bouhara
2020-04-06 15:58 ` Kamel Bouhara [this message]
2020-04-11 17:22 ` [PATCH 0/3] Introduce a counter inkernel API William Breathitt Gray
2020-04-11 23:31 ` Alexandre Belloni
2020-04-12 1:48 ` William Breathitt Gray
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200406155806.1295169-4-kamel.bouhara@bootlin.com \
--to=kamel.bouhara@bootlin.com \
--cc=alexandre.belloni@bootlin.com \
--cc=devicetree@vger.kernel.org \
--cc=dmitry.torokhov@gmail.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ludovic.desroches@microchip.com \
--cc=mark.rutland@arm.com \
--cc=robh+dt@kernel.org \
--cc=thomas.petazzoni@bootlin.com \
--cc=vilhelm.gray@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).