gpio-virtuser.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. Virtual GPIO Consumer
  3. =====================
  4. The virtual GPIO Consumer module allows users to instantiate virtual devices
  5. that request GPIOs and then control their behavior over debugfs. Virtual
  6. consumer devices can be instantiated from device-tree or over configfs.
  7. A virtual consumer uses the driver-facing GPIO APIs and allows to cover it with
  8. automated tests driven by user-space. The GPIOs are requested using
  9. ``gpiod_get_array()`` and so we support multiple GPIOs per connector ID.
  10. Creating GPIO consumers
  11. -----------------------
  12. The gpio-consumer module registers a configfs subsystem called
  13. ``'gpio-virtuser'``. For details of the configfs filesystem, please refer to
  14. the configfs documentation.
  15. The user can create a hierarchy of configfs groups and items as well as modify
  16. values of exposed attributes. Once the consumer is instantiated, this hierarchy
  17. will be translated to appropriate device properties. The general structure is:
  18. **Group:** ``/config/gpio-virtuser``
  19. This is the top directory of the gpio-consumer configfs tree.
  20. **Group:** ``/config/gpio-consumer/example-name``
  21. **Attribute:** ``/config/gpio-consumer/example-name/live``
  22. **Attribute:** ``/config/gpio-consumer/example-name/dev_name``
  23. This is a directory representing a GPIO consumer device.
  24. The read-only ``dev_name`` attribute exposes the name of the device as it will
  25. appear in the system on the platform bus. This is useful for locating the
  26. associated debugfs directory under
  27. ``/sys/kernel/debug/gpio-virtuser/$dev_name``.
  28. The ``'live'`` attribute allows to trigger the actual creation of the device
  29. once it's fully configured. The accepted values are: ``'1'`` to enable the
  30. virtual device and ``'0'`` to disable and tear it down.
  31. Creating GPIO lookup tables
  32. ---------------------------
  33. Users can create a number of configfs groups under the device group:
  34. **Group:** ``/config/gpio-consumer/example-name/con_id``
  35. The ``'con_id'`` directory represents a single GPIO lookup and its value maps
  36. to the ``'con_id'`` argument of the ``gpiod_get()`` function. For example:
  37. ``con_id`` == ``'reset'`` maps to the ``reset-gpios`` device property.
  38. Users can assign a number of GPIOs to each lookup. Each GPIO is a sub-directory
  39. with a user-defined name under the ``'con_id'`` group.
  40. **Attribute:** ``/config/gpio-consumer/example-name/con_id/0/key``
  41. **Attribute:** ``/config/gpio-consumer/example-name/con_id/0/offset``
  42. **Attribute:** ``/config/gpio-consumer/example-name/con_id/0/drive``
  43. **Attribute:** ``/config/gpio-consumer/example-name/con_id/0/pull``
  44. **Attribute:** ``/config/gpio-consumer/example-name/con_id/0/active_low``
  45. **Attribute:** ``/config/gpio-consumer/example-name/con_id/0/transitory``
  46. This is a group describing a single GPIO in the ``con_id-gpios`` property.
  47. For virtual consumers created using configfs we use machine lookup tables so
  48. this group can be considered as a mapping between the filesystem and the fields
  49. of a single entry in ``'struct gpiod_lookup'``.
  50. The ``'key'`` attribute represents either the name of the chip this GPIO
  51. belongs to or the GPIO line name. This depends on the value of the ``'offset'``
  52. attribute: if its value is >= 0, then ``'key'`` represents the label of the
  53. chip to lookup while ``'offset'`` represents the offset of the line in that
  54. chip. If ``'offset'`` is < 0, then ``'key'`` represents the name of the line.
  55. The remaining attributes map to the ``'flags'`` field of the GPIO lookup
  56. struct. The first two take string values as arguments:
  57. **``'drive'``:** ``'push-pull'``, ``'open-drain'``, ``'open-source'``
  58. **``'pull'``:** ``'pull-up'``, ``'pull-down'``, ``'pull-disabled'``, ``'as-is'``
  59. ``'active_low'`` and ``'transitory'`` are boolean attributes.
  60. Activating GPIO consumers
  61. -------------------------
  62. Once the configuration is complete, the ``'live'`` attribute must be set to 1 in
  63. order to instantiate the consumer. It can be set back to 0 to destroy the
  64. virtual device. The module will synchronously wait for the new simulated device
  65. to be successfully probed and if this doesn't happen, writing to ``'live'`` will
  66. result in an error.
  67. Device-tree
  68. -----------
  69. Virtual GPIO consumers can also be defined in device-tree. The compatible string
  70. must be: ``"gpio-virtuser"`` with at least one property following the
  71. standardized GPIO pattern.
  72. An example device-tree code defining a virtual GPIO consumer:
  73. .. code-block :: none
  74. gpio-virt-consumer {
  75. compatible = "gpio-virtuser";
  76. foo-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>, <&gpio1 2 0>;
  77. bar-gpios = <&gpio0 6 0>;
  78. };
  79. Controlling virtual GPIO consumers
  80. ----------------------------------
  81. Once active, the device will export debugfs attributes for controlling GPIO
  82. arrays as well as each requested GPIO line separately. Let's consider the
  83. following device property: ``foo-gpios = <&gpio0 0 0>, <&gpio0 4 0>;``.
  84. The following debugfs attribute groups will be created:
  85. **Group:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo/``
  86. This is the group that will contain the attributes for the entire GPIO array.
  87. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo/values``
  88. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo/values_atomic``
  89. Both attributes allow to read and set arrays of GPIO values. User must pass
  90. exactly the number of values that the array contains in the form of a string
  91. containing zeroes and ones representing inactive and active GPIO states
  92. respectively. In this example: ``echo 11 > values``.
  93. The ``values_atomic`` attribute works the same as ``values`` but the kernel
  94. will execute the GPIO driver callbacks in interrupt context.
  95. **Group:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/``
  96. This is a group that represents a single GPIO with ``$index`` being its offset
  97. in the array.
  98. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/consumer``
  99. Allows to set and read the consumer label of the GPIO line.
  100. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/debounce``
  101. Allows to set and read the debounce period of the GPIO line.
  102. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/direction``
  103. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/direction_atomic``
  104. These two attributes allow to set the direction of the GPIO line. They accept
  105. "input" and "output" as values. The atomic variant executes the driver callback
  106. in interrupt context.
  107. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/interrupts``
  108. If the line is requested in input mode, writing ``1`` to this attribute will
  109. make the module listen for edge interrupts on the GPIO. Writing ``0`` disables
  110. the monitoring. Reading this attribute returns the current number of registered
  111. interrupts (both edges).
  112. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/value``
  113. **Attribute:** ``/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/value_atomic``
  114. Both attributes allow to read and set values of individual requested GPIO lines.
  115. They accept the following values: ``1`` and ``0``.