rotary-encoder.txt 4.0 KB

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