Skip to content

Commit e70bdd4

Browse files
jhovolddtor
authored andcommitted
Input: rotary-encoder - add support for half-period encoders
Add support for encoders that have two detents per input signal period. Signed-off-by: Johan Hovold <jhovold@gmail.com> Acked-by: Daniel Mack <zonque@gmail.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 521a8f5 commit e70bdd4

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎Documentation/input/rotary-encoder.txt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ peripherals with two wires. The outputs are phase-shifted by 90 degrees
99
and by triggering on falling and rising edges, the turn direction can
1010
be determined.
1111

12+
Some encoders have both outputs low in stable states, whereas others also have
13+
a stable state with both outputs high (half-period mode).
14+
1215
The phase diagram of these two outputs look like this:
1316

1417
_____ _____ _____
@@ -26,6 +29,8 @@ The phase diagram of these two outputs look like this:
2629
|<-------->|
2730
one step
2831

32+
|<-->|
33+
one step (half-period mode)
2934

3035
For more information, please see
3136
http://en.wikipedia.org/wiki/Rotary_encoder
@@ -34,6 +39,13 @@ For more information, please see
3439
1. Events / state machine
3540
-------------------------
3641

42+
In half-period mode, state a) and c) above are used to determine the
43+
rotational direction based on the last stable state. Events are reported in
44+
states b) and d) given that the new stable state is different from the last
45+
(i.e. the rotation was not reversed half-way).
46+
47+
Otherwise, the following apply:
48+
3749
a) Rising edge on channel A, channel B in low state
3850
This state is used to recognize a clockwise turn
3951

@@ -96,6 +108,7 @@ static struct rotary_encoder_platform_data my_rotary_encoder_info = {
96108
.gpio_b = GPIO_ROTARY_B,
97109
.inverted_a = 0,
98110
.inverted_b = 0,
111+
.half_period = false,
99112
};
100113

101114
static struct platform_device rotary_encoder_device = {

‎drivers/input/misc/rotary_encoder.c‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* rotary_encoder.c
33
*
44
* (c) 2009 Daniel Mack <daniel@caiaq.de>
5+
* Copyright (C) 2011 Johan Hovold <jhovold@gmail.com>
56
*
67
* state machine code inspired by code from Tim Ruetz
78
*
@@ -38,6 +39,8 @@ struct rotary_encoder {
3839

3940
bool armed;
4041
unsigned char dir; /* 0 - clockwise, 1 - CCW */
42+
43+
char last_stable;
4144
};
4245

4346
static int rotary_encoder_get_state(struct rotary_encoder_platform_data *pdata)
@@ -112,11 +115,37 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
112115
return IRQ_HANDLED;
113116
}
114117

118+
static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
119+
{
120+
struct rotary_encoder *encoder = dev_id;
121+
int state;
122+
123+
state = rotary_encoder_get_state(encoder->pdata);
124+
125+
switch (state) {
126+
case 0x00:
127+
case 0x03:
128+
if (state != encoder->last_stable) {
129+
rotary_encoder_report_event(encoder);
130+
encoder->last_stable = state;
131+
}
132+
break;
133+
134+
case 0x01:
135+
case 0x02:
136+
encoder->dir = (encoder->last_stable + state) & 0x01;
137+
break;
138+
}
139+
140+
return IRQ_HANDLED;
141+
}
142+
115143
static int __devinit rotary_encoder_probe(struct platform_device *pdev)
116144
{
117145
struct rotary_encoder_platform_data *pdata = pdev->dev.platform_data;
118146
struct rotary_encoder *encoder;
119147
struct input_dev *input;
148+
irq_handler_t handler;
120149
int err;
121150

122151
if (!pdata) {
@@ -187,7 +216,14 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
187216
}
188217

189218
/* request the IRQs */
190-
err = request_irq(encoder->irq_a, &rotary_encoder_irq,
219+
if (pdata->half_period) {
220+
handler = &rotary_encoder_half_period_irq;
221+
encoder->last_stable = rotary_encoder_get_state(pdata);
222+
} else {
223+
handler = &rotary_encoder_irq;
224+
}
225+
226+
err = request_irq(encoder->irq_a, handler,
191227
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
192228
DRV_NAME, encoder);
193229
if (err) {
@@ -196,7 +232,7 @@ static int __devinit rotary_encoder_probe(struct platform_device *pdev)
196232
goto exit_free_gpio_b;
197233
}
198234

199-
err = request_irq(encoder->irq_b, &rotary_encoder_irq,
235+
err = request_irq(encoder->irq_b, handler,
200236
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
201237
DRV_NAME, encoder);
202238
if (err) {
@@ -264,5 +300,5 @@ module_exit(rotary_encoder_exit);
264300

265301
MODULE_ALIAS("platform:" DRV_NAME);
266302
MODULE_DESCRIPTION("GPIO rotary encoder driver");
267-
MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
303+
MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>, Johan Hovold");
268304
MODULE_LICENSE("GPL v2");

‎include/linux/rotary_encoder.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct rotary_encoder_platform_data {
1010
unsigned int inverted_b;
1111
bool relative_axis;
1212
bool rollover;
13+
bool half_period;
1314
};
1415

1516
#endif /* __ROTARY_ENCODER_H__ */

0 commit comments

Comments
 (0)