[PATCH] Input: rotary_encoder - support binary encoding of states

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




A plain binary encoding has some downsides compared to the usual Gray
encoding, but that doesn't stop hardware engineers to eventually use it.
So implement support for this encoding in the rotary encoder driver.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxxxx>
---
Hello,

an alternative to define this difference in the device tree is to use
something like:

	rotary-encoder,encoding = "binary";

or

	rotary-encoder,encoding = <ROTARY_ENCODER_ENCODING_BINARY>;

instead of a property

	rotary-encoder,encoding-binary;

. While the two first solutions make it obvious that there can only be
one encoding, they IMHO look ugly, so I went for the property without
value. What do you think?

Best regards
Uwe

 .../devicetree/bindings/input/rotary-encoder.txt      |  2 ++
 drivers/input/misc/rotary_encoder.c                   | 19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/Documentation/devicetree/bindings/input/rotary-encoder.txt b/Documentation/devicetree/bindings/input/rotary-encoder.txt
index 6c9f0c8a846c..4a96e4a32503 100644
--- a/Documentation/devicetree/bindings/input/rotary-encoder.txt
+++ b/Documentation/devicetree/bindings/input/rotary-encoder.txt
@@ -20,6 +20,8 @@ Optional properties:
   2: Half-period mode
   4: Quarter-period mode
 - wakeup-source: Boolean, rotary encoder can wake up the system.
+- rotary-encoder,encoding-binary: The device uses binary states instead of
+  gray encoding (which is the default).
 
 Deprecated properties:
 - rotary-encoder,half-period: Makes the driver work on half-period mode.
diff --git a/drivers/input/misc/rotary_encoder.c b/drivers/input/misc/rotary_encoder.c
index 96c486de49e0..ce43a59ca41a 100644
--- a/drivers/input/misc/rotary_encoder.c
+++ b/drivers/input/misc/rotary_encoder.c
@@ -28,6 +28,11 @@
 
 #define DRV_NAME "rotary-encoder"
 
+enum rotary_encoder_encoding {
+	ROTENC_GRAY,
+	ROTENC_BINARY,
+};
+
 struct rotary_encoder {
 	struct input_dev *input;
 
@@ -37,6 +42,7 @@ struct rotary_encoder {
 	u32 axis;
 	bool relative_axis;
 	bool rollover;
+	enum rotary_encoder_encoding encoding;
 
 	unsigned int pos;
 
@@ -57,9 +63,11 @@ static unsigned rotary_encoder_get_state(struct rotary_encoder *encoder)
 
 	for (i = 0; i < encoder->gpios->ndescs; ++i) {
 		int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
-		/* convert from gray encoding to normal */
-		if (ret & 1)
-			val = !val;
+
+		if (encoder->encoding == ROTENC_GRAY)
+			/* convert from gray encoding to normal */
+			if (ret & 1)
+				val = !val;
 
 		ret = ret << 1 | val;
 	}
@@ -213,6 +221,11 @@ static int rotary_encoder_probe(struct platform_device *pdev)
 	encoder->rollover =
 		device_property_read_bool(dev, "rotary-encoder,rollover");
 
+	if (device_property_read_bool(dev, "rotary-encoder,encoding-binary"))
+		encoder->encoding = ROTENC_BINARY;
+	else
+		encoder->encoding = ROTENC_GRAY;
+
 	device_property_read_u32(dev, "linux,axis", &encoder->axis);
 	encoder->relative_axis =
 		device_property_read_bool(dev, "rotary-encoder,relative-axis");
-- 
2.7.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]
  Powered by Linux