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