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 The phase diagram of these two outputs look like this:
16 Channel A ____| |_____| |_____| |____
18 : : : : : : : : : : : :
21 Channel B |_____| |_____| |_____| |__
23 : : : : : : : : : : : :
24 Event a b c d a b c d a b c d
30 For more information, please see
31 http://en.wikipedia.org/wiki/Rotary_encoder
34 1. Events / state machine
35 -------------------------
37 a) Rising edge on channel A, channel B in low state
38 This state is used to recognize a clockwise turn
40 b) Rising edge on channel B, channel A in high state
41 When entering this state, the encoder is put into 'armed' state,
42 meaning that there it has seen half the way of a one-step transition.
44 c) Falling edge on channel A, channel B in high state
45 This state is used to recognize a counter-clockwise turn
47 d) Falling edge on channel B, channel A in low state
48 Parking position. If the encoder enters this state, a full transition
49 should have happend, unless it flipped back on half the way. The
50 'armed' state tells us about that.
52 2. Platform requirements
53 ------------------------
55 As there is no hardware dependent call in this driver, the platform it is
56 used with must support gpiolib. Another requirement is that IRQs must be
57 able to fire on both edges.
63 To use this driver in your system, register a platform_device with the
64 name 'rotary-encoder' and associate the IRQs and some specific platform
67 struct rotary_encoder_platform_data is declared in
68 include/linux/rotary-encoder.h and needs to be filled with the number of
69 steps the encoder has and can carry information about externally inverted
70 signals (because of used invertig buffer or other reasons).
72 Because GPIO to IRQ mapping is platform specific, this information must
73 be given in seperately to the driver. See the example below.
75 ---------<snip>---------
77 /* board support file example */
79 #include <linux/input.h>
80 #include <linux/rotary_encoder.h>
82 #define GPIO_ROTARY_A 1
83 #define GPIO_ROTARY_B 2
85 static struct rotary_encoder_platform_data my_rotary_encoder_info = {
88 .gpio_a = GPIO_ROTARY_A,
89 .gpio_b = GPIO_ROTARY_B,
94 static struct platform_device rotary_encoder_device = {
95 .name = "rotary-encoder",
98 .platform_data = &my_rotary_encoder_info,