Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 1 | rotary-encoder - a generic driver for GPIO connected devices |
| 2 | Daniel Mack <daniel@caiaq.de>, Feb 2009 |
| 3 | |
| 4 | 0. Function |
| 5 | ----------- |
| 6 | |
| 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 |
| 10 | be determined. |
| 11 | |
| 12 | The phase diagram of these two outputs look like this: |
| 13 | |
| 14 | _____ _____ _____ |
| 15 | | | | | | | |
| 16 | Channel A ____| |_____| |_____| |____ |
| 17 | |
| 18 | : : : : : : : : : : : : |
| 19 | __ _____ _____ _____ |
| 20 | | | | | | | | |
| 21 | Channel B |_____| |_____| |_____| |__ |
| 22 | |
| 23 | : : : : : : : : : : : : |
| 24 | Event a b c d a b c d a b c d |
| 25 | |
| 26 | |<-------->| |
| 27 | one step |
| 28 | |
| 29 | |
| 30 | For more information, please see |
| 31 | http://en.wikipedia.org/wiki/Rotary_encoder |
| 32 | |
| 33 | |
| 34 | 1. Events / state machine |
| 35 | ------------------------- |
| 36 | |
| 37 | a) Rising edge on channel A, channel B in low state |
| 38 | This state is used to recognize a clockwise turn |
| 39 | |
| 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. |
| 43 | |
| 44 | c) Falling edge on channel A, channel B in high state |
| 45 | This state is used to recognize a counter-clockwise turn |
| 46 | |
| 47 | d) Falling edge on channel B, channel A in low state |
| 48 | Parking position. If the encoder enters this state, a full transition |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 49 | should have happened, unless it flipped back on half the way. The |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 50 | 'armed' state tells us about that. |
| 51 | |
| 52 | 2. Platform requirements |
| 53 | ------------------------ |
| 54 | |
| 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. |
| 58 | |
| 59 | |
| 60 | 3. Board integration |
| 61 | -------------------- |
| 62 | |
| 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 |
| 65 | data with it. |
| 66 | |
| 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 |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 70 | signals (because of an inverting buffer or other reasons). The encoder |
| 71 | can be set up to deliver input information as either an absolute or relative |
| 72 | axes. For relative axes the input event returns +/-1 for each step. For |
| 73 | absolute axes the position of the encoder can either roll over between zero |
| 74 | and the number of steps or will clamp at the maximum and zero depending on |
| 75 | the configuration. |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 76 | |
| 77 | Because GPIO to IRQ mapping is platform specific, this information must |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 78 | be given in separately to the driver. See the example below. |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 79 | |
| 80 | ---------<snip>--------- |
| 81 | |
| 82 | /* board support file example */ |
| 83 | |
| 84 | #include <linux/input.h> |
| 85 | #include <linux/rotary_encoder.h> |
| 86 | |
| 87 | #define GPIO_ROTARY_A 1 |
| 88 | #define GPIO_ROTARY_B 2 |
| 89 | |
| 90 | static struct rotary_encoder_platform_data my_rotary_encoder_info = { |
| 91 | .steps = 24, |
| 92 | .axis = ABS_X, |
H Hartley Sweeten | bd3ce65 | 2009-04-17 20:12:35 -0700 | [diff] [blame] | 93 | .relative_axis = false, |
| 94 | .rollover = false, |
Daniel Mack | 73969ff | 2009-03-04 23:27:14 -0800 | [diff] [blame] | 95 | .gpio_a = GPIO_ROTARY_A, |
| 96 | .gpio_b = GPIO_ROTARY_B, |
| 97 | .inverted_a = 0, |
| 98 | .inverted_b = 0, |
| 99 | }; |
| 100 | |
| 101 | static struct platform_device rotary_encoder_device = { |
| 102 | .name = "rotary-encoder", |
| 103 | .id = 0, |
| 104 | .dev = { |
| 105 | .platform_data = &my_rotary_encoder_info, |
| 106 | } |
| 107 | }; |
| 108 | |