Skip to contentSkip to conversation

input: rotary-encoder: add support for half-period encoders

PATCH Superseded Johan Hovold 3 participants 12 messages Apr 5, 2011 – May 23, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover input: rotary-encoder: add support for half-period encoders

Hi,

These patches add support for rotary-encoders which have two stable states per input signal period.

The first patch refactors state retrieval and event reporting and the second adds support for such half-period encoders.

Thanks,
Johan

Johan Hovold (2):
input: rotary-encoder: refactor and clean up
input: rotary-encoder: add support for half-period encoders

 Documentation/input/rotary-encoder.txt |   13 ++++
 drivers/input/misc/rotary_encoder.c    |  124 ++++++++++++++++++++++----------
 include/linux/rotary_encoder.h         |    1 +
 3 files changed, 101 insertions(+), 37 deletions(-)
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH 1/2 input: rotary-encoder: refactor and clean up

Refactor state retrieval and event reporting in interrupt handler. Remove a few empty lines.

---
 drivers/input/misc/rotary_encoder.c |   83 ++++++++++++++++++++--------------
 1 files changed, 49 insertions(+), 34 deletions(-)
drivers/input/misc/rotary_encoder.c +49 -34
index 7e64d01..253e502 100644
@@ -40,58 +40,74 @@ struct rotary_encoder {
 	unsigned char dir;	/* 0 - clockwise, 1 - CCW */
 };
 
-static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
+static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata)
 {
-	struct rotary_encoder *encoder = dev_id;
-	struct rotary_encoder_platform_data *pdata = encoder->pdata;
 	int a = !!gpio_get_value(pdata->gpio_a);
 	int b = !!gpio_get_value(pdata->gpio_b);
-	int state;
 
 	a ^= pdata->inverted_a;
 	b ^= pdata->inverted_b;
-	state = (a << 1) | b;
 
-	switch (state) {
+	return ((a << 1) | b);
+}
 
-	case 0x0:
-		if (!encoder->armed)
-			break;
+static void rotary_encoder_report_event(struct rotary_encoder *encoder)
+{
+	struct rotary_encoder_platform_data *pdata = encoder->pdata;
+	unsigned pos;
+	int value;
 
-		if (pdata->relative_axis) {
-			input_report_rel(encoder->input, pdata->axis,
-					 encoder->dir ? -1 : 1);
-		} else {
-			unsigned int pos = encoder->pos;
-
-			if (encoder->dir) {
-				/* turning counter-clockwise */
-				if (pdata->rollover)
-					pos += pdata->steps;
-				if (pos)
-					pos--;
-			} else {
-				/* turning clockwise */
-				if (pdata->rollover || pos < pdata->steps)
-					pos++;
-			}
+	if (pdata->relative_axis) {
+		if (encoder->dir)
+			value = -1;
+		else
+			value = 1;
+
+		input_report_rel(encoder->input, pdata->axis, value);
+	} else {
+		pos = encoder->pos;
+
+		if (encoder->dir) {
+			/* turning counter-clockwise */
 			if (pdata->rollover)
-				pos %= pdata->steps;
-			encoder->pos = pos;
-			input_report_abs(encoder->input, pdata->axis,
-					 encoder->pos);
+				pos += pdata->steps;
+			if (pos)
+				pos--;
+		} else {
+			/* turning clockwise */
+			if (pdata->rollover || pos < pdata->steps)
+				pos++;
 		}
-		input_sync(encoder->input);
 
+		if (pdata->rollover)
+			pos %= pdata->steps;
+
+		encoder->pos = pos;
+		input_report_abs(encoder->input, pdata->axis, encoder->pos);
+	}
+
+	input_sync(encoder->input);
+}
+
+static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
+{
+	struct rotary_encoder *encoder = dev_id;
+	int state;
+
+	state = rotary_encoder_get_state(encoder->pdata);
+
+	switch (state) {
+	case 0x0:
+		if (!encoder->armed)
+			break;
+		rotary_encoder_report_event(encoder);
 		encoder->armed = false;
 		break;
-
 	case 0x1:
 	case 0x2:
 		if (encoder->armed)
 			encoder->dir = state - 1;
 		break;
-
 	case 0x3:
 		encoder->armed = true;
 		break;
@@ -254,4 +270,3 @@ MODULE_ALIAS("platform:" DRV_NAME);
 MODULE_DESCRIPTION("GPIO rotary encoder driver");
 MODULE_AUTHOR("Daniel Mack <[email protected]>");
 MODULE_LICENSE("GPL v2");
-
Signature · 1 line
1.7.4.1
Reviewed-by on v1 1/2
#2 lore raw

RE: [PATCH 1/2] input: rotary-encoder: refactor and clean up

Quoted 8 lines · Johan Hovold

Refactor state retrieval and event reporting in interrupt handler. Remove a few empty lines.

---
 drivers/input/misc/rotary_encoder.c |   83 ++++++++++++++++++++--------------
 1 files changed, 49 insertions(+), 34 deletions(-)

Looks sane to me.

#3 lore raw

PATCH 2/2 input: rotary-encoder: add support for half-period encoders

In reply to Johan Hovold

Add support for encoders that have two detents per input signal period.

---
 Documentation/input/rotary-encoder.txt |   13 ++++++++++
 drivers/input/misc/rotary_encoder.c    |   41 +++++++++++++++++++++++++++++--
 include/linux/rotary_encoder.h         |    1 +
 3 files changed, 52 insertions(+), 3 deletions(-)
Documentation/input/rotary-encoder.txt +13 -0
index 8b4129d..76b707b 100644
@@ -9,6 +9,9 @@ peripherals with two wires. The outputs are phase-shifted by 90 degrees
 and by triggering on falling and rising edges, the turn direction can
 be determined.
 
+Some encoders have both inputs low in stable states, whereas others also have
+a stable state with both inputs high (half-period mode).
+
 The phase diagram of these two outputs look like this:
 
                   _____       _____       _____
@@ -26,6 +29,8 @@ The phase diagram of these two outputs look like this:
                 |<-------->|
 	          one step
 
+                |<-->|
+	          one step (half-period mode)
 
 For more information, please see
 	http://en.wikipedia.org/wiki/Rotary_encoder
@@ -34,6 +39,13 @@ For more information, please see
 1. Events / state machine
 -------------------------
 
+In half-period mode, state a) and c) above are used to determine the
+rotational direction based on the last stable state. Events are reported in
+states b) and d) given that the new stable state is different from the last
+(i.e. the rotation was not reversed half-way).
+
+Otherwise, the following apply:
+
 a) Rising edge on channel A, channel B in low state
 	This state is used to recognize a clockwise turn
 
@@ -96,6 +108,7 @@ static struct rotary_encoder_platform_data my_rotary_encoder_info = {
 	.gpio_b		= GPIO_ROTARY_B,
 	.inverted_a	= 0,
 	.inverted_b	= 0,
+	.half_period	= false,
 };
 
 static struct platform_device rotary_encoder_device = {
drivers/input/misc/rotary_encoder.c +38 -3

80 lines not on this page · raw message

include/linux/rotary_encoder.h +1 -0
index 215278b..3f594dc 100644
@@ -10,6 +10,7 @@ struct rotary_encoder_platform_data {
 	unsigned int inverted_b;
 	bool relative_axis;
 	bool rollover;
+	bool half_period;
 };
 
 #endif /* __ROTARY_ENCODER_H__ */
Signature · 1 line
1.7.4.1
#4 lore raw

PATCH v2 2/2 input: rotary-encoder: add support for half-period encoders

Add support for encoders that have two detents per input signal period.

---

v2: fix typo in documentation (s/inputs/outputs/)

 Documentation/input/rotary-encoder.txt |   13 ++++++++++
 drivers/input/misc/rotary_encoder.c    |   41 +++++++++++++++++++++++++++++--
 include/linux/rotary_encoder.h         |    1 +
 3 files changed, 52 insertions(+), 3 deletions(-)
Documentation/input/rotary-encoder.txt +13 -0
index 8b4129d..64fbed0 100644
@@ -9,6 +9,9 @@ peripherals with two wires. The outputs are phase-shifted by 90 degrees
 and by triggering on falling and rising edges, the turn direction can
 be determined.
 
+Some encoders have both outputs low in stable states, whereas others also have
+a stable state with both ouputs high (half-period mode).
+
 The phase diagram of these two outputs look like this:
 
                   _____       _____       _____
@@ -26,6 +29,8 @@ The phase diagram of these two outputs look like this:
                 |<-------->|
 	          one step
 
+                |<-->|
+	          one step (half-period mode)
 
 For more information, please see
 	http://en.wikipedia.org/wiki/Rotary_encoder
@@ -34,6 +39,13 @@ For more information, please see
 1. Events / state machine
 -------------------------
 
+In half-period mode, state a) and c) above are used to determine the
+rotational direction based on the last stable state. Events are reported in
+states b) and d) given that the new stable state is different from the last
+(i.e. the rotation was not reversed half-way).
+
+Otherwise, the following apply:
+
 a) Rising edge on channel A, channel B in low state
 	This state is used to recognize a clockwise turn
 
@@ -96,6 +108,7 @@ static struct rotary_encoder_platform_data my_rotary_encoder_info = {
 	.gpio_b		= GPIO_ROTARY_B,
 	.inverted_a	= 0,
 	.inverted_b	= 0,
+	.half_period	= false,
 };
 
 static struct platform_device rotary_encoder_device = {
drivers/input/misc/rotary_encoder.c +38 -3

80 lines not on this page · raw message

include/linux/rotary_encoder.h +1 -0
index 215278b..3f594dc 100644
@@ -10,6 +10,7 @@ struct rotary_encoder_platform_data {
 	unsigned int inverted_b;
 	bool relative_axis;
 	bool rollover;
+	bool half_period;
 };
 
 #endif /* __ROTARY_ENCODER_H__ */
Signature · 1 line
1.7.4.1
#5 lore raw

PATCH v3 2/2 input: rotary-encoder: add support for half-period encoders

Add support for encoders that have two detents per input signal period.

---

v2: fix typo in documentation (s/inputs/outputs/)
v3: fix typo in documentation (s/ouputs/outputs/)

 Documentation/input/rotary-encoder.txt |   13 ++++++++++
 drivers/input/misc/rotary_encoder.c    |   41 +++++++++++++++++++++++++++++--
 include/linux/rotary_encoder.h         |    1 +
 3 files changed, 52 insertions(+), 3 deletions(-)
Documentation/input/rotary-encoder.txt +13 -0
index 8b4129d..2ac5c60 100644
@@ -9,6 +9,9 @@ peripherals with two wires. The outputs are phase-shifted by 90 degrees
 and by triggering on falling and rising edges, the turn direction can
 be determined.
 
+Some encoders have both outputs low in stable states, whereas others also have
+a stable state with both outputs high (half-period mode).
+
 The phase diagram of these two outputs look like this:
 
                   _____       _____       _____
@@ -26,6 +29,8 @@ The phase diagram of these two outputs look like this:
                 |<-------->|
 	          one step
 
+                |<-->|
+	          one step (half-period mode)
 
 For more information, please see
 	http://en.wikipedia.org/wiki/Rotary_encoder
@@ -34,6 +39,13 @@ For more information, please see
 1. Events / state machine
 -------------------------
 
+In half-period mode, state a) and c) above are used to determine the
+rotational direction based on the last stable state. Events are reported in
+states b) and d) given that the new stable state is different from the last
+(i.e. the rotation was not reversed half-way).
+
+Otherwise, the following apply:
+
 a) Rising edge on channel A, channel B in low state
 	This state is used to recognize a clockwise turn
 
@@ -96,6 +108,7 @@ static struct rotary_encoder_platform_data my_rotary_encoder_info = {
 	.gpio_b		= GPIO_ROTARY_B,
 	.inverted_a	= 0,
 	.inverted_b	= 0,
+	.half_period	= false,
 };
 
 static struct platform_device rotary_encoder_device = {
drivers/input/misc/rotary_encoder.c +38 -3

80 lines not on this page · raw message

include/linux/rotary_encoder.h +1 -0
index 215278b..3f594dc 100644
@@ -10,6 +10,7 @@ struct rotary_encoder_platform_data {
 	unsigned int inverted_b;
 	bool relative_axis;
 	bool rollover;
+	bool half_period;
 };
 
 #endif /* __ROTARY_ENCODER_H__ */
Signature · 1 line
1.7.4.1
#6 lore raw
In reply to Johan Hovold
Quoted 7 lines · Johan Hovold

Hi,

These patches add support for rotary-encoders which have two stable states per input signal period.

The first patch refactors state retrieval and event reporting and the second adds support for such half-period encoders.

I have no objections to let the code in, and the patch looks sane to me.

However, I don't currently have access to the hardware I wrote the driver for, so I can't test right now, but will do at some later point.

Feel free to add my Acked-by:, and I will get back to you in case I encounter any trouble.

Thanks,
Daniel

#7 lore raw
Quoted 6 lines · Daniel Mack
Quoted 5 lines · Johan Hovold

Hi,

These patches add support for rotary-encoders which have two stable states per input signal period.

Quoted 8 lines
Quoted 2 lines

The first patch refactors state retrieval and event reporting and the second adds support for such half-period encoders.

I have no objections to let the code in, and the patch looks sane to me.

However, I don't currently have access to the hardware I wrote the driver for, so I can't test right now, but will do at some later point.

It is possible to test non-half-period mode with a half-period encoder (and vice verse) and I've verified that my patches do not change the original state machine. In particular, every other event is still missing as no event is delivered in the armed-state in non-half-period mode.

Quoted 2 lines

Feel free to add my Acked-by:, and I will get back to you in case I encounter any trouble.

Thanks,
Johan

#8 lore raw
In reply to Johan Hovold

Hi Dmitry,

Quoted 2 lines · Johan Hovold

These patches add support for rotary-encoders which have two stable states per input signal period.

Is there any chance to get these into 2.6.40? I haven't received any feedback from you so I thought I'd send a reminder.

Thanks,
Johan

#9 lore raw

Hi Johan,

Quoted 8 lines · Johan Hovold

Hi Dmitry,

Quoted 2 lines · Johan Hovold

These patches add support for rotary-encoders which have two stable states per input signal period.

Is there any chance to get these into 2.6.40? I haven't received any feedback from you so I thought I'd send a reminder.

Very sorry for the long delay.

I was finally able to test your patches with real hardware, and they seem to work alright. So I have no problem with them being accepted any time soon, but I don't know if they will still make it to 2.6.40.

Sorry again for not getting back to you earlier, and thanks for your contribution.

Daniel

Acked-by
#10 lore raw
Quoted 9 lines · Daniel Mack

Hi Johan,

Quoted 6 lines · Johan Hovold

Hi Dmitry,

Quoted 2 lines · Johan Hovold

These patches add support for rotary-encoders which have two stable states per input signal period.

Quoted 8 lines
Quoted 2 lines

Is there any chance to get these into 2.6.40? I haven't received any feedback from you so I thought I'd send a reminder.

Very sorry for the long delay.

I was finally able to test your patches with real hardware, and they seem to work alright. So I have no problem with them being accepted any time soon, but I don't know if they will still make it to 2.6.40.

Btw, that also means you can have my

for both patches.

Daniel

#11 lore raw
In reply to Daniel Mack
Quoted 3 lines · Daniel Mack

I was finally able to test your patches with real hardware, and they seem to work alright. So I have no problem with them being accepted any time soon, but I don't know if they will still make it to 2.6.40.

Thanks for testing. It seems they where added to Dmitry's tree about a week ago.

Thanks,
Johan