# GPIO

---

# GPIO

_Peios Kernel / Administration / System administration / GPIO_

> Reference for GPIO.

<a id="rst-gpio"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

- [Character Device Userspace API](</kernel/userspace/userspace-api/gpio/chardev.md>)
- [GPIO Aggregator](</kernel/administration/admin-guide/gpio/gpio-aggregator.md>)
- [Configfs GPIO Simulator](</kernel/administration/admin-guide/gpio/gpio-sim.md>)
- [Virtual GPIO Consumer](</kernel/administration/admin-guide/gpio/gpio-virtuser.md>)
- [Obsolete APIs](</kernel/administration/admin-guide/gpio/obsolete.md>)

---

# GPIO Aggregator

_Peios Kernel / Administration / System administration / GPIO_

> The GPIO Aggregator provides a mechanism to aggregate GPIOs, and expose them as a new gpio_chip. This supports the following use cases.

<a id="rst-gpio-aggregator"></a>

<!-- SPDX-License-Identifier: GPL-2.0-only -->

The GPIO Aggregator provides a mechanism to aggregate GPIOs, and expose them as a new gpio_chip.  This supports the following use cases.

<a id="rst-aggregating-gpios-using-sysfs"></a>

## Aggregating GPIOs using Sysfs

GPIO controllers are exported to userspace using /dev/gpiochip\* character devices.  Access control to these devices is provided by standard UNIX file system permissions, on an all-or-nothing basis: either a GPIO controller is accessible for a user, or it is not.

The GPIO Aggregator provides access control for a set of one or more GPIOs, by aggregating them into a new gpio_chip, which can be assigned to a group or user using standard UNIX file ownership and permissions.  Furthermore, this simplifies and hardens exporting GPIOs to a virtual machine, as the VM can just grab the full GPIO controller, and no longer needs to care about which GPIOs to grab and which not, reducing the attack surface.

Aggregated GPIO controllers are instantiated and destroyed by writing to write-only attribute files in sysfs.

> /sys/bus/platform/drivers/gpio-aggregator/
>
> > - **"new_device" ...**
> >
> >   Userspace may ask the kernel to instantiate an aggregated GPIO controller by writing a string describing the GPIOs to aggregate to the "new_device" file, using the format
> >
> >
> >
> >   ```none
> >   [<gpioA>] [<gpiochipB> <offsets>] ...
> >   ```
> >
> >   Where:
> >
> >   > - **"&lt;gpioA&gt;" ...**
> >   >
> >   >   is a GPIO line name,
> >   >
> >   > - **"&lt;gpiochipB&gt;" ...**
> >   >
> >   >   is a GPIO chip label, and
> >   >
> >   > - **"&lt;offsets&gt;" ...**
> >   >
> >   >   is a comma-separated list of GPIO offsets and/or GPIO offset ranges denoted by dashes.
> >
> >   Example: Instantiate a new GPIO aggregator by aggregating GPIO line 19 of "e6052000.gpio" and GPIO lines 20-21 of "e6050000.gpio" into a new gpio_chip:
> >
> >
> >
> >   ```sh
> >   $ echo 'e6052000.gpio 19 e6050000.gpio 20-21' > new_device
> >   ```
> >
> > - **"delete_device" ...**
> >
> >   Userspace may ask the kernel to destroy an aggregated GPIO controller after use by writing its device name to the "delete_device" file.
> >
> >   Example: Destroy the previously-created aggregated GPIO controller, assumed to be "gpio-aggregator.0":
> >
> >
> >
> >   ```sh
> >   $ echo gpio-aggregator.0 > delete_device
> >   ```

<a id="rst-aggregating-gpios-using-configfs"></a>

## Aggregating GPIOs using Configfs

**Group:** `/config/gpio-aggregator`

> This is the root directory of the gpio-aggregator configfs tree.

**Group:** `/config/gpio-aggregator/<example-name>`

> This directory represents a GPIO aggregator device. You can assign any name to `<example-name>` (e.g. `agg0`), except names starting with `_sysfs` prefix, which are reserved for auto-generated configfs entries corresponding to devices created via Sysfs.

**Attribute:** `/config/gpio-aggregator/<example-name>/live`

> The `live` attribute allows to trigger the actual creation of the device once it's fully configured. Accepted values are:
>
> - `1`, `yes`, `true` : enable the virtual device
>
> - `0`, `no`, `false` : disable the virtual device

**Attribute:** `/config/gpio-aggregator/<example-name>/dev_name`

> The read-only `dev_name` attribute exposes the name of the device as it will appear in the system on the platform bus (e.g. `gpio-aggregator.0`). This is useful for identifying a character device for the newly created aggregator. If it's `gpio-aggregator.0`, `/sys/devices/platform/gpio-aggregator.0/gpiochipX` path tells you that the GPIO device id is `X`.

You must create subdirectories for each virtual line you want to instantiate, named exactly as `line0`, `line1`, ..., `lineY`, when you want to instantiate `Y+1` (Y &gt;= 0) lines.  Configure all lines before activating the device by setting `live` to 1.

**Group:** `/config/gpio-aggregator/<example-name>/<lineY>/`

> This directory represents a GPIO line to include in the aggregator.

**Attribute:** `/config/gpio-aggregator/<example-name>/<lineY>/key`

**Attribute:** `/config/gpio-aggregator/<example-name>/<lineY>/offset`

> The default values after creating the `<lineY>` directory are:
>
> - `key` : &lt;empty&gt;
>
> - `offset` : -1
>
> `key` must always be explicitly configured, while `offset` depends. Two configuration patterns exist for each `<lineY>`:
>
> (a). For lookup by GPIO line name:
>
> > - Set `key` to the line name.
> >
> > - Ensure `offset` remains -1 (the default).
>
> (b). For lookup by GPIO chip name and the line offset within the chip:
>
> > - Set `key` to the chip name.
> >
> > - Set `offset` to the line offset (0 &lt;= `offset` &lt; 65535).

**Attribute:** `/config/gpio-aggregator/<example-name>/<lineY>/name`

> The `name` attribute sets a custom name for lineY. If left unset, the line will remain unnamed.

Once the configuration is done, the `'live'` attribute must be set to 1 in order to instantiate the aggregator device. It can be set back to 0 to destroy the virtual device. The module will synchronously wait for the new aggregator device to be successfully probed and if this doesn't happen, writing to `'live'` will result in an error. This is a different behaviour from the case when you create it using sysfs `new_device` interface.

> [!NOTE]
>
> For aggregators created via Sysfs, the configfs entries are auto-generated and appear as `/config/gpio-aggregator/_sysfs.<N>/`. You cannot add or remove line directories with mkdir(2)/rmdir(2). To modify lines, you must use the "delete_device" interface to tear down the existing device and reconfigure it from scratch. However, you can still toggle the aggregator with the `live` attribute and adjust the `key`, `offset`, and `name` attributes for each line when `live` is set to 0 by hand (i.e. it's not waiting for deferred probe).

<a id="rst-sample-configuration-commands"></a>

### Sample configuration commands



```sh
# Create a directory for an aggregator device
$ mkdir /sys/kernel/config/gpio-aggregator/agg0

# Configure each line
$ mkdir /sys/kernel/config/gpio-aggregator/agg0/line0
$ echo gpiochip0 > /sys/kernel/config/gpio-aggregator/agg0/line0/key
$ echo 6         > /sys/kernel/config/gpio-aggregator/agg0/line0/offset
$ echo test0     > /sys/kernel/config/gpio-aggregator/agg0/line0/name
$ mkdir /sys/kernel/config/gpio-aggregator/agg0/line1
$ echo gpiochip0 > /sys/kernel/config/gpio-aggregator/agg0/line1/key
$ echo 7         > /sys/kernel/config/gpio-aggregator/agg0/line1/offset
$ echo test1     > /sys/kernel/config/gpio-aggregator/agg0/line1/name

# Activate the aggregator device
$ echo 1         > /sys/kernel/config/gpio-aggregator/agg0/live
```

<a id="rst-generic-gpio-driver"></a>

## Generic GPIO Driver

The GPIO Aggregator can also be used as a generic driver for a simple GPIO-operated device described in DT, without a dedicated in-kernel driver. This is useful in industrial control, and is not unlike e.g. spidev, which allows the user to communicate with an SPI device from userspace.

Binding a device to the GPIO Aggregator is performed either by modifying the gpio-aggregator driver, or by writing to the "driver_override" file in Sysfs.

Example: If "door" is a GPIO-operated device described in DT, using its own compatible value:



```text
door {
        compatible = "myvendor,mydoor";

        gpios = <&gpio2 19 GPIO_ACTIVE_HIGH>,
                <&gpio2 20 GPIO_ACTIVE_LOW>;
        gpio-line-names = "open", "lock";
};
```

it can be bound to the GPIO Aggregator by either:

1. Adding its compatible value to `gpio_aggregator_dt_ids[]`,

2. Binding manually using "driver_override":



```sh
$ echo gpio-aggregator > /sys/bus/platform/devices/door/driver_override
$ echo door > /sys/bus/platform/drivers/gpio-aggregator/bind
```

After that, a new gpiochip "door" has been created:



```sh
$ gpioinfo door
gpiochip12 - 2 lines:
        line   0:       "open"       unused   input  active-high
        line   1:       "lock"       unused   input  active-high
```

---

# GPIO Testing Driver

_Peios Kernel / Administration / System administration / GPIO_

> The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO chips for testing purposes. The lines exposed by these chips can be accessed using the standard GPIO character device interface as well as manipulated using the…

<a id="rst-gpio-testing-driver"></a>

<!-- SPDX-License-Identifier: GPL-2.0-only -->

> [!NOTE]
>
> This module has been obsoleted by the more flexible [gpio-sim.rst](/kernel/administration/admin-guide/gpio/gpio-sim.md). New developments should use that API and existing developments are encouraged to migrate as soon as possible. This module will continue to be maintained but no new features will be added.

The GPIO Testing Driver (gpio-mockup) provides a way to create simulated GPIO chips for testing purposes. The lines exposed by these chips can be accessed using the standard GPIO character device interface as well as manipulated using the dedicated debugfs directory structure.

<a id="rst-creating-simulated-chips-using-module-params"></a>

## Creating simulated chips using module params

When loading the gpio-mockup driver a number of parameters can be passed to the module.

> gpio_mockup_ranges
>
> > This parameter takes an argument in the form of an array of integer pairs. Each pair defines the base GPIO number (non-negative integer) and the first number after the last of this chip. If the base GPIO is -1, the gpiolib will assign it automatically. while the following parameter is the number of lines exposed by the chip.
> >
> > Example: gpio_mockup_ranges=-1,8,-1,16,405,409
> >
> > The line above creates three chips. The first one will expose 8 lines, the second 16 and the third 4. The base GPIO for the third chip is set to 405 while for two first chips it will be assigned automatically.
>
> gpio_mockup_named_lines
>
> > This parameter doesn't take any arguments. It lets the driver know that GPIO lines exposed by it should be named.
> >
> > The name format is: gpio-mockup-X-Y where X is mockup chip's ID and Y is the line offset.

<a id="rst-manipulating-simulated-lines"></a>

## Manipulating simulated lines

Each mockup chip creates its own subdirectory in /sys/kernel/debug/gpio-mockup/. The directory is named after the chip's label. A symlink is also created, named after the chip's name, which points to the label directory.

Inside each subdirectory, there's a separate attribute for each GPIO line. The name of the attribute represents the line's offset in the chip.

Reading from a line attribute returns the current value. Writing to it (0 or 1) changes the configuration of the simulated pull-up/pull-down resistor (1 - pull-up, 0 - pull-down).

---

# Configfs GPIO Simulator

_Peios Kernel / Administration / System administration / GPIO_

> The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO chips for testing purposes. The lines exposed by these chips can be accessed using the standard GPIO character device interface as well as manipulated using…

<a id="rst-configfs-gpio-simulator"></a>

<!-- SPDX-License-Identifier: GPL-2.0-or-later -->

The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO chips for testing purposes. The lines exposed by these chips can be accessed using the standard GPIO character device interface as well as manipulated using sysfs attributes.

<a id="rst-creating-simulated-chips"></a>

## Creating simulated chips

The gpio-sim module registers a configfs subsystem called `'gpio-sim'`. For details of the configfs filesystem, please refer to the configfs documentation.

The user can create a hierarchy of configfs groups and items as well as modify values of exposed attributes. Once the chip is instantiated, this hierarchy will be translated to appropriate device properties. The general structure is:

**Group:** `/config/gpio-sim`

This is the top directory of the gpio-sim configfs tree.

**Group:** `/config/gpio-sim/gpio-device`

**Attribute:** `/config/gpio-sim/gpio-device/dev_name`

**Attribute:** `/config/gpio-sim/gpio-device/live`

This is a directory representing a GPIO platform device. The `'dev_name'` attribute is read-only and allows the user-space to read the platform device name (e.g. `'gpio-sim.0'`). The `'live'` attribute allows to trigger the actual creation of the device once it's fully configured. The accepted values are: `'1'` to enable the simulated device and `'0'` to disable and tear it down.

**Group:** `/config/gpio-sim/gpio-device/gpio-bankX`

**Attribute:** `/config/gpio-sim/gpio-device/gpio-bankX/chip_name`

**Attribute:** `/config/gpio-sim/gpio-device/gpio-bankX/num_lines`

This group represents a bank of GPIOs under the top platform device. The `'chip_name'` attribute is read-only and allows the user-space to read the device name of the bank device. The `'num_lines'` attribute allows to specify the number of lines exposed by this bank.

**Group:** `/config/gpio-sim/gpio-device/gpio-bankX/lineY`

**Attribute:** `/config/gpio-sim/gpio-device/gpio-bankX/lineY/name`

**Attribute:** `/config/gpio-sim/gpio-device/gpio-bankX/lineY/valid`

This group represents a single line at the offset Y. The `valid` attribute indicates whether the line can be used as GPIO. The `name` attribute allows to set the line name as represented by the 'gpio-line-names' property.

**Item:** `/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog`

**Attribute:** `/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/name`

**Attribute:** `/config/gpio-sim/gpio-device/gpio-bankX/lineY/hog/direction`

This item makes the gpio-sim module hog the associated line. The `'name'` attribute specifies the in-kernel consumer name to use. The `'direction'` attribute specifies the hog direction and must be one of: `'input'`, `'output-high'` and `'output-low'`.

Inside each bank directory, there's a set of attributes that can be used to configure the new chip. Additionally the user can `mkdir()` subdirectories inside the chip's directory that allow to pass additional configuration for specific lines. The name of those subdirectories must take the form of: `'line<offset>'` (e.g. `'line0'`, `'line20'`, etc.) as the name will be used by the module to assign the config to the specific line at given offset.

Once the configuration is complete, the `'live'` attribute must be set to 1 in order to instantiate the chip. It can be set back to 0 to destroy the simulated chip. The module will synchronously wait for the new simulated device to be successfully probed and if this doesn't happen, writing to `'live'` will result in an error.

Simulated GPIO chips can also be defined in device-tree. The compatible string must be: `"gpio-simulator"`. Supported properties are:

> `"gpio-sim,label"` - chip label

Other standard GPIO properties (like `"gpio-line-names"`, `"ngpios"` or `"gpio-hog"`) are also supported. Please refer to the GPIO documentation for details.

An example device-tree code defining a GPIO simulator:



```none
gpio-sim {
    compatible = "gpio-simulator";

    bank0 {
        gpio-controller;
        #gpio-cells = <2>;
        ngpios = <16>;
        gpio-sim,label = "dt-bank0";
        gpio-line-names = "", "sim-foo", "", "sim-bar";
    };

    bank1 {
        gpio-controller;
        #gpio-cells = <2>;
        ngpios = <8>;
        gpio-sim,label = "dt-bank1";

        line3 {
            gpio-hog;
            gpios = <3 0>;
            output-high;
            line-name = "sim-hog-from-dt";
        };
    };
};
```

<a id="rst-manipulating-simulated-lines"></a>

## Manipulating simulated lines

Each simulated GPIO chip creates a separate sysfs group under its device directory for each exposed line (e.g. `/sys/devices/platform/gpio-sim.X/gpiochipY/`). The name of each group is of the form: `'sim_gpioX'` where X is the offset of the line. Inside each group there are two attributes:

> - **`pull` - allows to read and set the current simulated pull setting for**
>
>   every line, when writing the value must be one of: `'pull-up'`, `'pull-down'`
>
> - **`value` - allows to read the current value of the line which may be**
>
>   different from the pull if the line is being driven from user-space

---

# Virtual GPIO Consumer

_Peios Kernel / Administration / System administration / GPIO_

> The virtual GPIO Consumer module allows users to instantiate virtual devices that request GPIOs and then control their behavior over debugfs. Virtual consumer devices can be instantiated from device-tree or over configfs.

<a id="rst-virtual-gpio-consumer"></a>

<!-- SPDX-License-Identifier: GPL-2.0-only -->

The virtual GPIO Consumer module allows users to instantiate virtual devices that request GPIOs and then control their behavior over debugfs. Virtual consumer devices can be instantiated from device-tree or over configfs.

A virtual consumer uses the driver-facing GPIO APIs and allows to cover it with automated tests driven by user-space. The GPIOs are requested using `gpiod_get_array()` and so we support multiple GPIOs per connector ID.

<a id="rst-creating-gpio-consumers"></a>

## Creating GPIO consumers

The gpio-consumer module registers a configfs subsystem called `'gpio-virtuser'`. For details of the configfs filesystem, please refer to the configfs documentation.

The user can create a hierarchy of configfs groups and items as well as modify values of exposed attributes. Once the consumer is instantiated, this hierarchy will be translated to appropriate device properties. The general structure is:

**Group:** `/config/gpio-virtuser`

This is the top directory of the gpio-consumer configfs tree.

**Group:** `/config/gpio-consumer/example-name`

**Attribute:** `/config/gpio-consumer/example-name/live`

**Attribute:** `/config/gpio-consumer/example-name/dev_name`

This is a directory representing a GPIO consumer device.

The read-only `dev_name` attribute exposes the name of the device as it will appear in the system on the platform bus. This is useful for locating the associated debugfs directory under `/sys/kernel/debug/gpio-virtuser/$dev_name`.

The `'live'` attribute allows to trigger the actual creation of the device once it's fully configured. The accepted values are: `'1'` to enable the virtual device and `'0'` to disable and tear it down.

<a id="rst-creating-gpio-lookup-tables"></a>

## Creating GPIO lookup tables

Users can create a number of configfs groups under the device group:

**Group:** `/config/gpio-consumer/example-name/con_id`

The `'con_id'` directory represents a single GPIO lookup and its value maps to the `'con_id'` argument of the `gpiod_get()` function. For example: `con_id` == `'reset'` maps to the `reset-gpios` device property.

Users can assign a number of GPIOs to each lookup. Each GPIO is a sub-directory with a user-defined name under the `'con_id'` group.

**Attribute:** `/config/gpio-consumer/example-name/con_id/0/key`

**Attribute:** `/config/gpio-consumer/example-name/con_id/0/offset`

**Attribute:** `/config/gpio-consumer/example-name/con_id/0/drive`

**Attribute:** `/config/gpio-consumer/example-name/con_id/0/pull`

**Attribute:** `/config/gpio-consumer/example-name/con_id/0/active_low`

**Attribute:** `/config/gpio-consumer/example-name/con_id/0/transitory`

This is a group describing a single GPIO in the `con_id-gpios` property.

For virtual consumers created using configfs we use machine lookup tables so this group can be considered as a mapping between the filesystem and the fields of a single entry in `'struct gpiod_lookup'`.

The `'key'` attribute represents either the name of the chip this GPIO belongs to or the GPIO line name. This depends on the value of the `'offset'` attribute: if its value is &gt;= 0, then `'key'` represents the label of the chip to lookup while `'offset'` represents the offset of the line in that chip. If `'offset'` is &lt; 0, then `'key'` represents the name of the line.

The remaining attributes map to the `'flags'` field of the GPIO lookup struct. The first two take string values as arguments:

**\`\`'drive'\`\`:** `'push-pull'`, `'open-drain'`, `'open-source'` **\`\`'pull'\`\`:** `'pull-up'`, `'pull-down'`, `'pull-disabled'`, `'as-is'`

`'active_low'` and `'transitory'` are boolean attributes.

<a id="rst-activating-gpio-consumers"></a>

## Activating GPIO consumers

Once the configuration is complete, the `'live'` attribute must be set to 1 in order to instantiate the consumer. It can be set back to 0 to destroy the virtual device. The module will synchronously wait for the new simulated device to be successfully probed and if this doesn't happen, writing to `'live'` will result in an error.

<a id="rst-device-tree"></a>

## Device-tree

Virtual GPIO consumers can also be defined in device-tree. The compatible string must be: `"gpio-virtuser"` with at least one property following the standardized GPIO pattern.

An example device-tree code defining a virtual GPIO consumer:



```none
gpio-virt-consumer {
    compatible = "gpio-virtuser";

    foo-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>, <&gpio1 2 0>;
    bar-gpios = <&gpio0 6 0>;
};
```

<a id="rst-controlling-virtual-gpio-consumers"></a>

## Controlling virtual GPIO consumers

Once active, the device will export debugfs attributes for controlling GPIO arrays as well as each requested GPIO line separately. Let's consider the following device property: `foo-gpios = <&gpio0 0 0>, <&gpio0 4 0>;`.

The following debugfs attribute groups will be created:

**Group:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo/`

This is the group that will contain the attributes for the entire GPIO array.

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo/values`

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo/values_atomic`

Both attributes allow to read and set arrays of GPIO values. User must pass exactly the number of values that the array contains in the form of a string containing zeroes and ones representing inactive and active GPIO states respectively. In this example: `echo 11 > values`.

The `values_atomic` attribute works the same as `values` but the kernel will execute the GPIO driver callbacks in interrupt context.

**Group:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/`

This is a group that represents a single GPIO with `$index` being its offset in the array.

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/consumer`

Allows to set and read the consumer label of the GPIO line.

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/debounce`

Allows to set and read the debounce period of the GPIO line.

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/direction`

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/direction_atomic`

These two attributes allow to set the direction of the GPIO line. They accept "input" and "output" as values. The atomic variant executes the driver callback in interrupt context.

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/interrupts`

If the line is requested in input mode, writing `1` to this attribute will make the module listen for edge interrupts on the GPIO. Writing `0` disables the monitoring. Reading this attribute returns the current number of registered interrupts (both edges).

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/value`

**Attribute:** `/sys/kernel/debug/gpio-virtuser/$dev_name/gpiod:foo:$index/value_atomic`

Both attributes allow to read and set values of individual requested GPIO lines. They accept the following values: `1` and `0`.

---

# Obsolete GPIO APIs

_Peios Kernel / Administration / System administration / GPIO_

> Reference for Obsolete GPIO APIs.

<a id="rst-obsolete-gpio-apis"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

- [Character Device Userspace API (v1)](</kernel/userspace/userspace-api/gpio/chardev-v1.md>)
- [Sysfs Interface](</kernel/userspace/userspace-api/gpio/sysfs.md>)
- [Mockup Testing Module](</kernel/administration/admin-guide/gpio/gpio-mockup.md>)
