gpio-sim.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/valid``
  36. This group represents a single line at the offset Y. The ``valid`` attribute
  37. indicates whether the line can be used as GPIO. The ``name`` attribute allows
  38. to set the line name as represented by the 'gpio-line-names' property.
  39. **Item:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog``
  40. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/name``
  41. **Attribute:** ``/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/direction``
  42. This item makes the gpio-sim module hog the associated line. The ``'name'``
  43. attribute specifies the in-kernel consumer name to use. The ``'direction'``
  44. attribute specifies the hog direction and must be one of: ``'input'``,
  45. ``'output-high'`` and ``'output-low'``.
  46. Inside each bank directory, there's a set of attributes that can be used to
  47. configure the new chip. Additionally the user can ``mkdir()`` subdirectories
  48. inside the chip's directory that allow to pass additional configuration for
  49. specific lines. The name of those subdirectories must take the form of:
  50. ``'line<offset>'`` (e.g. ``'line0'``, ``'line20'``, etc.) as the name will be
  51. used by the module to assign the config to the specific line at given offset.
  52. Once the configuration is complete, the ``'live'`` attribute must be set to 1 in
  53. order to instantiate the chip. It can be set back to 0 to destroy the simulated
  54. chip. The module will synchronously wait for the new simulated device to be
  55. successfully probed and if this doesn't happen, writing to ``'live'`` will
  56. result in an error.
  57. Simulated GPIO chips can also be defined in device-tree. The compatible string
  58. must be: ``"gpio-simulator"``. Supported properties are:
  59. ``"gpio-sim,label"`` - chip label
  60. Other standard GPIO properties (like ``"gpio-line-names"``, ``"ngpios"`` or
  61. ``"gpio-hog"``) are also supported. Please refer to the GPIO documentation for
  62. details.
  63. An example device-tree code defining a GPIO simulator:
  64. .. code-block :: none
  65. gpio-sim {
  66. compatible = "gpio-simulator";
  67. bank0 {
  68. gpio-controller;
  69. #gpio-cells = <2>;
  70. ngpios = <16>;
  71. gpio-sim,label = "dt-bank0";
  72. gpio-line-names = "", "sim-foo", "", "sim-bar";
  73. };
  74. bank1 {
  75. gpio-controller;
  76. #gpio-cells = <2>;
  77. ngpios = <8>;
  78. gpio-sim,label = "dt-bank1";
  79. line3 {
  80. gpio-hog;
  81. gpios = <3 0>;
  82. output-high;
  83. line-name = "sim-hog-from-dt";
  84. };
  85. };
  86. };
  87. Manipulating simulated lines
  88. ----------------------------
  89. Each simulated GPIO chip creates a separate sysfs group under its device
  90. directory for each exposed line
  91. (e.g. ``/sys/devices/platform/gpio-sim.X/gpiochipY/``). The name of each group
  92. is of the form: ``'sim_gpioX'`` where X is the offset of the line. Inside each
  93. group there are two attributes:
  94. ``pull`` - allows to read and set the current simulated pull setting for
  95. every line, when writing the value must be one of: ``'pull-up'``,
  96. ``'pull-down'``
  97. ``value`` - allows to read the current value of the line which may be
  98. different from the pull if the line is being driven from
  99. user-space