rotary-encoder.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ============================================================
  2. rotary-encoder - a generic driver for GPIO connected devices
  3. ============================================================
  4. :Author: Daniel Mack <daniel@caiaq.de>, Feb 2009
  5. Function
  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. Some encoders have both outputs low in stable states, others also have
  12. a stable state with both outputs high (half-period mode) and some have
  13. a stable state in all steps (quarter-period mode).
  14. The phase diagram of these two outputs look like this::
  15. _____ _____ _____
  16. | | | | | |
  17. Channel A ____| |_____| |_____| |____
  18. : : : : : : : : : : : :
  19. __ _____ _____ _____
  20. | | | | | | |
  21. Channel B |_____| |_____| |_____| |__
  22. : : : : : : : : : : : :
  23. Event a b c d a b c d a b c d
  24. |<-------->|
  25. one step
  26. |<-->|
  27. one step (half-period mode)
  28. |<>|
  29. one step (quarter-period mode)
  30. For more information, please see
  31. https://en.wikipedia.org/wiki/Rotary_encoder
  32. Events / state machine
  33. ----------------------
  34. In half-period mode, state a) and c) above are used to determine the
  35. rotational direction based on the last stable state. Events are reported in
  36. states b) and d) given that the new stable state is different from the last
  37. (i.e. the rotation was not reversed half-way).
  38. Otherwise, the following apply:
  39. a) Rising edge on channel A, channel B in low state
  40. This state is used to recognize a clockwise turn
  41. b) Rising edge on channel B, channel A in high state
  42. When entering this state, the encoder is put into 'armed' state,
  43. meaning that there it has seen half the way of a one-step transition.
  44. c) Falling edge on channel A, channel B in high state
  45. This state is used to recognize a counter-clockwise turn
  46. d) Falling edge on channel B, channel A in low state
  47. Parking position. If the encoder enters this state, a full transition
  48. should have happened, unless it flipped back on half the way. The
  49. 'armed' state tells us about that.
  50. Platform requirements
  51. ---------------------
  52. As there is no hardware dependent call in this driver, the platform it is
  53. used with must support gpiolib. Another requirement is that IRQs must be
  54. able to fire on both edges.
  55. Board integration
  56. -----------------
  57. To use this driver in your system, register a platform_device with the
  58. name 'rotary-encoder' and associate the IRQs and some specific platform
  59. data with it. Because the driver uses generic device properties, this can
  60. be done either via device tree, ACPI, or using static board files, like in
  61. example below:
  62. ::
  63. /* board support file example */
  64. #include <linux/input.h>
  65. #include <linux/gpio/machine.h>
  66. #include <linux/property.h>
  67. #define GPIO_ROTARY_A 1
  68. #define GPIO_ROTARY_B 2
  69. static struct gpiod_lookup_table rotary_encoder_gpios = {
  70. .dev_id = "rotary-encoder.0",
  71. .table = {
  72. GPIO_LOOKUP_IDX("gpio-0",
  73. GPIO_ROTARY_A, NULL, 0, GPIO_ACTIVE_LOW),
  74. GPIO_LOOKUP_IDX("gpio-0",
  75. GPIO_ROTARY_B, NULL, 1, GPIO_ACTIVE_HIGH),
  76. { },
  77. },
  78. };
  79. static const struct property_entry rotary_encoder_properties[] __initconst = {
  80. PROPERTY_ENTRY_INTEGER("rotary-encoder,steps-per-period", u32, 24),
  81. PROPERTY_ENTRY_INTEGER("linux,axis", u32, ABS_X),
  82. PROPERTY_ENTRY_INTEGER("rotary-encoder,relative_axis", u32, 0),
  83. { },
  84. };
  85. static struct platform_device rotary_encoder_device = {
  86. .name = "rotary-encoder",
  87. .id = 0,
  88. };
  89. ...
  90. gpiod_add_lookup_table(&rotary_encoder_gpios);
  91. device_add_properties(&rotary_encoder_device, rotary_encoder_properties);
  92. platform_device_register(&rotary_encoder_device);
  93. ...
  94. Please consult device tree binding documentation to see all properties
  95. supported by the driver.