Blame view

Documentation/input/rotary-encoder.txt 3.85 KB
73969ff0e   Daniel Mack   Input: generic dr...
1
2
3
4
5
6
7
8
9
10
  rotary-encoder - a generic driver for GPIO connected devices
  Daniel Mack <daniel@caiaq.de>, Feb 2009
  
  0. Function
  -----------
  
  Rotary encoders are devices which are connected to the CPU or other
  peripherals with two wires. The outputs are phase-shifted by 90 degrees
  and by triggering on falling and rising edges, the turn direction can
  be determined.
e70bdd41b   Johan Hovold   Input: rotary-enc...
11
12
  Some encoders have both outputs low in stable states, whereas others also have
  a stable state with both outputs high (half-period mode).
73969ff0e   Daniel Mack   Input: generic dr...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  The phase diagram of these two outputs look like this:
  
                    _____       _____       _____
                   |     |     |     |     |     |
    Channel A  ____|     |_____|     |_____|     |____
  
                   :  :  :  :  :  :  :  :  :  :  :  :
              __       _____       _____       _____
                |     |     |     |     |     |     |
    Channel B   |_____|     |_____|     |_____|     |__
  
                   :  :  :  :  :  :  :  :  :  :  :  :
    Event          a  b  c  d  a  b  c  d  a  b  c  d
  
                  |<-------->|
  	          one step
e70bdd41b   Johan Hovold   Input: rotary-enc...
29
30
                  |<-->|
  	          one step (half-period mode)
73969ff0e   Daniel Mack   Input: generic dr...
31
32
33
34
35
36
37
  
  For more information, please see
  	http://en.wikipedia.org/wiki/Rotary_encoder
  
  
  1. Events / state machine
  -------------------------
e70bdd41b   Johan Hovold   Input: rotary-enc...
38
39
40
41
42
43
  In half-period mode, state a) and c) above are used to determine the
  rotational direction based on the last stable state. Events are reported in
  states b) and d) given that the new stable state is different from the last
  (i.e. the rotation was not reversed half-way).
  
  Otherwise, the following apply:
73969ff0e   Daniel Mack   Input: generic dr...
44
45
46
47
48
49
50
51
52
53
54
55
  a) Rising edge on channel A, channel B in low state
  	This state is used to recognize a clockwise turn
  
  b) Rising edge on channel B, channel A in high state
  	When entering this state, the encoder is put into 'armed' state,
  	meaning that there it has seen half the way of a one-step transition.
  
  c) Falling edge on channel A, channel B in high state
  	This state is used to recognize a counter-clockwise turn
  
  d) Falling edge on channel B, channel A in low state
  	Parking position. If the encoder enters this state, a full transition
25985edce   Lucas De Marchi   Fix common misspe...
56
  	should have happened, unless it flipped back on half the way. The
73969ff0e   Daniel Mack   Input: generic dr...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  	'armed' state tells us about that.
  
  2. Platform requirements
  ------------------------
  
  As there is no hardware dependent call in this driver, the platform it is
  used with must support gpiolib. Another requirement is that IRQs must be
  able to fire on both edges.
  
  
  3. Board integration
  --------------------
  
  To use this driver in your system, register a platform_device with the
  name 'rotary-encoder' and associate the IRQs and some specific platform
  data with it.
  
  struct rotary_encoder_platform_data is declared in
  include/linux/rotary-encoder.h and needs to be filled with the number of
  steps the encoder has and can carry information about externally inverted
bd3ce6556   H Hartley Sweeten   Input: rotary_enc...
77
78
79
80
81
82
  signals (because of an inverting buffer or other reasons). The encoder
  can be set up to deliver input information as either an absolute or relative
  axes. For relative axes the input event returns +/-1 for each step. For
  absolute axes the position of the encoder can either roll over between zero
  and the number of steps or will clamp at the maximum and zero depending on
  the configuration.
73969ff0e   Daniel Mack   Input: generic dr...
83
84
  
  Because GPIO to IRQ mapping is platform specific, this information must
3ad2f3fbb   Daniel Mack   tree-wide: Assort...
85
  be given in separately to the driver. See the example below.
73969ff0e   Daniel Mack   Input: generic dr...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  
  ---------<snip>---------
  
  /* board support file example */
  
  #include <linux/input.h>
  #include <linux/rotary_encoder.h>
  
  #define GPIO_ROTARY_A 1
  #define GPIO_ROTARY_B 2
  
  static struct rotary_encoder_platform_data my_rotary_encoder_info = {
  	.steps		= 24,
  	.axis		= ABS_X,
bd3ce6556   H Hartley Sweeten   Input: rotary_enc...
100
101
  	.relative_axis	= false,
  	.rollover	= false,
73969ff0e   Daniel Mack   Input: generic dr...
102
103
104
105
  	.gpio_a		= GPIO_ROTARY_A,
  	.gpio_b		= GPIO_ROTARY_B,
  	.inverted_a	= 0,
  	.inverted_b	= 0,
e70bdd41b   Johan Hovold   Input: rotary-enc...
106
  	.half_period	= false,
73969ff0e   Daniel Mack   Input: generic dr...
107
108
109
110
111
112
113
114
115
  };
  
  static struct platform_device rotary_encoder_device = {
  	.name		= "rotary-encoder",
  	.id		= 0,
  	.dev		= {
  		.platform_data = &my_rotary_encoder_info,
  	}
  };