gpio-sim.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. .. SPDX-License-Identifier: GPL-2.0-or-later
  2. Configfs GPIO Simulator
  3. =======================
  4. The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO
  5. chips for testing purposes. The lines exposed by these chips can be accessed
  6. using the standard GPIO character device interface as well as manipulated
  7. using sysfs attributes.
  8. Creating simulated chips
  9. ------------------------
  10. The gpio-sim module registers a configfs subsystem called ``'gpio-sim'``. For
  11. details of the configfs filesystem, please refer to the configfs documentation.
  12. The user can create a hierarchy of configfs groups and items as well as modify
  13. values of exposed attributes. Once the chip is instantiated, this hierarchy
  14. will be translated to appropriate device properties. The general structure is:
  15. **Group:** ``/config/gpio-sim``
  16. This is the top directory of the gpio-sim configfs tree.
  17. **Group:** ``/config/gpio-sim/gpio-device``
  18. **Attribute:** ``/config/gpio-sim/gpio-device/dev_name``
  19. **Attribute:** ``/config/gpio-sim/gpio-device/live``
  20. This is a directory representing a GPIO platform device. The ``'dev_name'``
  21. attribute is read-only and allows the user-space to read the platform device
  22. name (e.g. ``'gpio-sim.0'``). The ``'live'`` attribute allows to trigger the
  23. actual creation of the device once it's fully configured. The accepted values
  24. are: ``'1'`` to enable the simulated device and ``'0'`` to disable and tear
  25. it down.
  26. **Group:** ``/config/gpio-sim/gpio-device/gpio-bankX``
  27. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/chip_name``
  28. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/num_lines``
  29. This group represents a bank of GPIOs under the top platform device. The
  30. ``'chip_name'`` attribute is read-only and allows the user-space to read the
  31. device name of the bank device. The ``'num_lines'`` attribute allows to specify
  32. the number of lines exposed by this bank.
  33. **Group:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY``
  34. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/name``
  35. This group represents a single line at the offset Y. The 'name' attribute
  36. allows to set the line name as represented by the 'gpio-line-names' property.
  37. **Item:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog``
  38. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/name``
  39. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/direction``
  40. This item makes the gpio-sim module hog the associated line. The ``'name'``
  41. attribute specifies the in-kernel consumer name to use. The ``'direction'``
  42. attribute specifies the hog direction and must be one of: ``'input'``,
  43. ``'output-high'`` and ``'output-low'``.
  44. Inside each bank directory, there's a set of attributes that can be used to
  45. configure the new chip. Additionally the user can ``mkdir()`` subdirectories
  46. inside the chip's directory that allow to pass additional configuration for
  47. specific lines. The name of those subdirectories must take the form of:
  48. ``'line<offset>'`` (e.g. ``'line0'``, ``'line20'``, etc.) as the name will be
  49. used by the module to assign the config to the specific line at given offset.
  50. Once the confiuration is complete, the ``'live'`` attribute must be set to 1 in
  51. order to instantiate the chip. It can be set back to 0 to destroy the simulated
  52. chip. The module will synchronously wait for the new simulated device to be
  53. successfully probed and if this doesn't happen, writing to ``'live'`` will
  54. result in an error.
  55. Simulated GPIO chips can also be defined in device-tree. The compatible string
  56. must be: ``"gpio-simulator"``. Supported properties are:
  57. ``"gpio-sim,label"`` - chip label
  58. Other standard GPIO properties (like ``"gpio-line-names"``, ``"ngpios"`` or
  59. ``"gpio-hog"``) are also supported. Please refer to the GPIO documentation for
  60. details.
  61. An example device-tree code defining a GPIO simulator:
  62. .. code-block :: none
  63. gpio-sim {
  64. compatible = "gpio-simulator";
  65. bank0 {
  66. gpio-controller;
  67. #gpio-cells = <2>;
  68. ngpios = <16>;
  69. gpio-sim,label = "dt-bank0";
  70. gpio-line-names = "", "sim-foo", "", "sim-bar";
  71. };
  72. bank1 {
  73. gpio-controller;
  74. #gpio-cells = <2>;
  75. ngpios = <8>;
  76. gpio-sim,label = "dt-bank1";
  77. line3 {
  78. gpio-hog;
  79. gpios = <3 0>;
  80. output-high;
  81. line-name = "sim-hog-from-dt";
  82. };
  83. };
  84. };
  85. Manipulating simulated lines
  86. ----------------------------
  87. Each simulated GPIO chip creates a separate sysfs group under its device
  88. directory for each exposed line
  89. (e.g. ``/sys/devices/platform/gpio-sim.X/gpiochipY/``). The name of each group
  90. is of the form: ``'sim_gpioX'`` where X is the offset of the line. Inside each
  91. group there are two attributes:
  92. ``pull`` - allows to read and set the current simulated pull setting for
  93. every line, when writing the value must be one of: ``'pull-up'``,
  94. ``'pull-down'``
  95. ``value`` - allows to read the current value of the line which may be
  96. different from the pull if the line is being driven from
  97. user-space