blob: 46a74f0c551a1b5a1ada3dc7e3ae248df1edbbff [file] [log] [blame]
Daniel Mack73969ff2009-03-04 23:27:14 -08001rotary-encoder - a generic driver for GPIO connected devices
2Daniel Mack <daniel@caiaq.de>, Feb 2009
3
40. Function
5-----------
6
7Rotary encoders are devices which are connected to the CPU or other
8peripherals with two wires. The outputs are phase-shifted by 90 degrees
9and by triggering on falling and rising edges, the turn direction can
10be determined.
11
Ezequiel Garcia3a341a42015-10-13 23:39:50 -070012Some encoders have both outputs low in stable states, others also have
13a stable state with both outputs high (half-period mode) and some have
14a stable state in all steps (quarter-period mode).
Johan Hovolde70bdd42011-05-11 16:35:30 -070015
Daniel Mack73969ff2009-03-04 23:27:14 -080016The phase diagram of these two outputs look like this:
17
18 _____ _____ _____
19 | | | | | |
20 Channel A ____| |_____| |_____| |____
21
22 : : : : : : : : : : : :
23 __ _____ _____ _____
24 | | | | | | |
25 Channel B |_____| |_____| |_____| |__
26
27 : : : : : : : : : : : :
28 Event a b c d a b c d a b c d
29
30 |<-------->|
31 one step
32
Johan Hovolde70bdd42011-05-11 16:35:30 -070033 |<-->|
34 one step (half-period mode)
Daniel Mack73969ff2009-03-04 23:27:14 -080035
Ezequiel Garcia3a341a42015-10-13 23:39:50 -070036 |<>|
37 one step (quarter-period mode)
38
Daniel Mack73969ff2009-03-04 23:27:14 -080039For more information, please see
Masanari Iidaae13c652015-06-18 00:12:02 +090040 https://en.wikipedia.org/wiki/Rotary_encoder
Daniel Mack73969ff2009-03-04 23:27:14 -080041
42
431. Events / state machine
44-------------------------
45
Johan Hovolde70bdd42011-05-11 16:35:30 -070046In half-period mode, state a) and c) above are used to determine the
47rotational direction based on the last stable state. Events are reported in
48states b) and d) given that the new stable state is different from the last
49(i.e. the rotation was not reversed half-way).
50
51Otherwise, the following apply:
52
Daniel Mack73969ff2009-03-04 23:27:14 -080053a) Rising edge on channel A, channel B in low state
54 This state is used to recognize a clockwise turn
55
56b) 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.
59
60c) Falling edge on channel A, channel B in high state
61 This state is used to recognize a counter-clockwise turn
62
63d) Falling edge on channel B, channel A in low state
64 Parking position. If the encoder enters this state, a full transition
Lucas De Marchi25985ed2011-03-30 22:57:33 -030065 should have happened, unless it flipped back on half the way. The
Daniel Mack73969ff2009-03-04 23:27:14 -080066 'armed' state tells us about that.
67
682. Platform requirements
69------------------------
70
71As there is no hardware dependent call in this driver, the platform it is
72used with must support gpiolib. Another requirement is that IRQs must be
73able to fire on both edges.
74
75
763. Board integration
77--------------------
78
79To use this driver in your system, register a platform_device with the
80name 'rotary-encoder' and associate the IRQs and some specific platform
81data with it.
82
83struct rotary_encoder_platform_data is declared in
84include/linux/rotary-encoder.h and needs to be filled with the number of
85steps the encoder has and can carry information about externally inverted
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -070086signals (because of an inverting buffer or other reasons). The encoder
87can be set up to deliver input information as either an absolute or relative
88axes. For relative axes the input event returns +/-1 for each step. For
89absolute axes the position of the encoder can either roll over between zero
90and the number of steps or will clamp at the maximum and zero depending on
91the configuration.
Daniel Mack73969ff2009-03-04 23:27:14 -080092
93Because GPIO to IRQ mapping is platform specific, this information must
Daniel Mack3ad2f3f2010-02-03 08:01:28 +080094be given in separately to the driver. See the example below.
Daniel Mack73969ff2009-03-04 23:27:14 -080095
96---------<snip>---------
97
98/* board support file example */
99
100#include <linux/input.h>
101#include <linux/rotary_encoder.h>
102
103#define GPIO_ROTARY_A 1
104#define GPIO_ROTARY_B 2
105
106static struct rotary_encoder_platform_data my_rotary_encoder_info = {
107 .steps = 24,
108 .axis = ABS_X,
H Hartley Sweetenbd3ce652009-04-17 20:12:35 -0700109 .relative_axis = false,
110 .rollover = false,
Daniel Mack73969ff2009-03-04 23:27:14 -0800111 .gpio_a = GPIO_ROTARY_A,
112 .gpio_b = GPIO_ROTARY_B,
113 .inverted_a = 0,
114 .inverted_b = 0,
Johan Hovolde70bdd42011-05-11 16:35:30 -0700115 .half_period = false,
Sylvain Rochet47ec6e52015-10-13 23:24:36 -0700116 .wakeup_source = false,
Daniel Mack73969ff2009-03-04 23:27:14 -0800117};
118
119static struct platform_device rotary_encoder_device = {
120 .name = "rotary-encoder",
121 .id = 0,
122 .dev = {
123 .platform_data = &my_rotary_encoder_info,
124 }
125};
126