rotary-encoder.txt 3.8 KB

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