]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - Documentation/input/devices/rotary-encoder.rst
Merge branch 'parisc-4.15-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[mirror_ubuntu-bionic-kernel.git] / Documentation / input / devices / rotary-encoder.rst
CommitLineData
42f2309b 1============================================================
73969ff0 2rotary-encoder - a generic driver for GPIO connected devices
42f2309b 3============================================================
73969ff0 4
42f2309b
MCC
5:Author: Daniel Mack <daniel@caiaq.de>, Feb 2009
6
7Function
8--------
73969ff0
DM
9
10Rotary encoders are devices which are connected to the CPU or other
11peripherals with two wires. The outputs are phase-shifted by 90 degrees
12and by triggering on falling and rising edges, the turn direction can
13be determined.
14
3a341a4c
EG
15Some encoders have both outputs low in stable states, others also have
16a stable state with both outputs high (half-period mode) and some have
17a stable state in all steps (quarter-period mode).
e70bdd41 18
42f2309b 19The phase diagram of these two outputs look like this::
73969ff0
DM
20
21 _____ _____ _____
22 | | | | | |
23 Channel A ____| |_____| |_____| |____
24
25 : : : : : : : : : : : :
26 __ _____ _____ _____
27 | | | | | | |
28 Channel B |_____| |_____| |_____| |__
29
30 : : : : : : : : : : : :
31 Event a b c d a b c d a b c d
32
33 |<-------->|
34 one step
35
e70bdd41
JH
36 |<-->|
37 one step (half-period mode)
73969ff0 38
3a341a4c
EG
39 |<>|
40 one step (quarter-period mode)
41
73969ff0 42For more information, please see
ae13c65b 43 https://en.wikipedia.org/wiki/Rotary_encoder
73969ff0
DM
44
45
42f2309b
MCC
46Events / state machine
47----------------------
73969ff0 48
e70bdd41
JH
49In half-period mode, state a) and c) above are used to determine the
50rotational direction based on the last stable state. Events are reported in
51states b) and d) given that the new stable state is different from the last
52(i.e. the rotation was not reversed half-way).
53
54Otherwise, the following apply:
55
73969ff0
DM
56a) Rising edge on channel A, channel B in low state
57 This state is used to recognize a clockwise turn
58
59b) Rising edge on channel B, channel A in high state
60 When entering this state, the encoder is put into 'armed' state,
61 meaning that there it has seen half the way of a one-step transition.
62
63c) Falling edge on channel A, channel B in high state
64 This state is used to recognize a counter-clockwise turn
65
66d) Falling edge on channel B, channel A in low state
67 Parking position. If the encoder enters this state, a full transition
25985edc 68 should have happened, unless it flipped back on half the way. The
73969ff0
DM
69 'armed' state tells us about that.
70
42f2309b
MCC
71Platform requirements
72---------------------
73969ff0
DM
73
74As there is no hardware dependent call in this driver, the platform it is
75used with must support gpiolib. Another requirement is that IRQs must be
76able to fire on both edges.
77
78
42f2309b
MCC
79Board integration
80-----------------
73969ff0
DM
81
82To use this driver in your system, register a platform_device with the
83name 'rotary-encoder' and associate the IRQs and some specific platform
01ef6601
DT
84data with it. Because the driver uses generic device properties, this can
85be done either via device tree, ACPI, or using static board files, like in
86example below:
73969ff0 87
01ef6601 88::
73969ff0 89
01ef6601 90 /* board support file example */
73969ff0 91
01ef6601
DT
92 #include <linux/input.h>
93 #include <linux/gpio/machine.h>
94 #include <linux/property.h>
95
96 #define GPIO_ROTARY_A 1
97 #define GPIO_ROTARY_B 2
98
99 static struct gpiod_lookup_table rotary_encoder_gpios = {
100 .dev_id = "rotary-encoder.0",
101 .table = {
102 GPIO_LOOKUP_IDX("gpio-0",
103 GPIO_ROTARY_A, NULL, 0, GPIO_ACTIVE_LOW),
104 GPIO_LOOKUP_IDX("gpio-0",
105 GPIO_ROTARY_B, NULL, 1, GPIO_ACTIVE_HIGH),
106 { },
107 },
108 };
109
110 static const struct property_entry rotary_encoder_properties[] __initconst = {
111 PROPERTY_ENTRY_INTEGER("rotary-encoder,steps-per-period", u32, 24),
112 PROPERTY_ENTRY_INTEGER("linux,axis", u32, ABS_X),
113 PROPERTY_ENTRY_INTEGER("rotary-encoder,relative_axis", u32, 0),
114 { },
115 };
116
117 static struct platform_device rotary_encoder_device = {
118 .name = "rotary-encoder",
119 .id = 0,
120 };
121
122 ...
123
124 gpiod_add_lookup_table(&rotary_encoder_gpios);
125 device_add_properties(&rotary_encoder_device, rotary_encoder_properties);
126 platform_device_register(&rotary_encoder_device);
127
128 ...
42f2309b 129
01ef6601
DT
130Please consult device tree binding documentation to see all properties
131supported by the driver.