1 rotary-encoder - a generic driver for GPIO connected devices
2 Daniel Mack <daniel@caiaq.de>, Feb 2009
7 Rotary encoders are devices which are connected to the CPU or other
8 peripherals with two wires. The outputs are phase-shifted by 90 degrees
9 and by triggering on falling and rising edges, the turn direction can
12 Some encoders have both outputs low in stable states, others also have
13 a stable state with both outputs high (half-period mode) and some have
14 a stable state in all steps (quarter-period mode).
16 The phase diagram of these two outputs look like this:
20 Channel A ____| |_____| |_____| |____
22 : : : : : : : : : : : :
25 Channel B |_____| |_____| |_____| |__
27 : : : : : : : : : : : :
28 Event a b c d a b c d a b c d
34 one step (half-period mode)
37 one step (quarter-period mode)
39 For more information, please see
40 https://en.wikipedia.org/wiki/Rotary_encoder
43 1. Events / state machine
44 -------------------------
46 In half-period mode, state a) and c) above are used to determine the
47 rotational direction based on the last stable state. Events are reported in
48 states b) and d) given that the new stable state is different from the last
49 (i.e. the rotation was not reversed half-way).
51 Otherwise, the following apply:
53 a) Rising edge on channel A, channel B in low state
54 This state is used to recognize a clockwise turn
56 b) Rising edge on channel B, channel A in high state
57 When entering this state, the encoder is put into 'armed' state,
58 meaning that there it has seen half the way of a one-step transition.
60 c) Falling edge on channel A, channel B in high state
61 This state is used to recognize a counter-clockwise turn
63 d) Falling edge on channel B, channel A in low state
64 Parking position. If the encoder enters this state, a full transition
65 should have happened, unless it flipped back on half the way. The
66 'armed' state tells us about that.
68 2. Platform requirements
69 ------------------------
71 As there is no hardware dependent call in this driver, the platform it is
72 used with must support gpiolib. Another requirement is that IRQs must be
73 able to fire on both edges.
79 To use this driver in your system, register a platform_device with the
80 name 'rotary-encoder' and associate the IRQs and some specific platform
83 struct rotary_encoder_platform_data is declared in
84 include/linux/rotary-encoder.h and needs to be filled with the number of
85 steps the encoder has and can carry information about externally inverted
86 signals (because of an inverting buffer or other reasons). The encoder
87 can be set up to deliver input information as either an absolute or relative
88 axes. For relative axes the input event returns +/-1 for each step. For
89 absolute axes the position of the encoder can either roll over between zero
90 and the number of steps or will clamp at the maximum and zero depending on
93 Because GPIO to IRQ mapping is platform specific, this information must
94 be given in separately to the driver. See the example below.
96 ---------<snip>---------
98 /* board support file example */
100 #include <linux/input.h>
101 #include <linux/rotary_encoder.h>
103 #define GPIO_ROTARY_A 1
104 #define GPIO_ROTARY_B 2
106 static struct rotary_encoder_platform_data my_rotary_encoder_info = {
109 .relative_axis = false,
111 .gpio_a = GPIO_ROTARY_A,
112 .gpio_b = GPIO_ROTARY_B,
115 .half_period = false,
116 .wakeup_source = false,
119 static struct platform_device rotary_encoder_device = {
120 .name = "rotary-encoder",
123 .platform_data = &my_rotary_encoder_info,