Skip to content

Linux GPIO Consumer Driver

Overview

Linux GPIO subsystem separates GPIO providers and GPIO consumers.

GPIO Consumer Driver
    gpio_desc
      gpiolib
    gpio_chip
GPIO Controller Driver
 Hardware Registers

The GPIO controller driver exposes GPIO lines through gpio_chip.

Consumer drivers acquire GPIO resources from Device Tree and access them through GPIO descriptors.


GPIO Controller vs GPIO Consumer

GPIO Controller Driver

Responsible for:

  • Registering GPIO lines
  • Managing GPIO hardware
  • Implementing GPIO operations

Typical structure:

struct gpio_chip {
    int (*direction_input)(...);
    int (*direction_output)(...);
    int (*get)(...);
    void (*set)(...);
};

Examples:

  • SoC GPIO controller driver
  • I2C GPIO expander driver
  • SPI GPIO expander driver

GPIO Consumer Driver

Responsible for:

  • Requesting GPIO resources
  • Configuring GPIO direction
  • Reading GPIO inputs
  • Controlling GPIO outputs

Consumer drivers never access GPIO controller hardware directly.

Instead they use GPIO descriptor APIs.


Device Tree GPIO Binding

GPIO resources are described in Device Tree.

Example:

my-device {
    reset-gpios = <&gpio0 7 GPIO_ACTIVE_LOW>;
    enable-gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>;
};

A GPIO specifier contains:

<&gpio_controller offset flags>

Meaning:

Field Description
GPIO controller GPIO provider
offset GPIO line number within controller
flags Active-high or active-low configuration

GPIO Descriptor Model

Consumer drivers do not operate GPIO numbers directly.

Instead:

Device Tree
gpio_desc
gpiod API

Each GPIO line is represented by a descriptor.

Conceptually:

struct gpio_desc {
    struct gpio_chip *chip;
    unsigned int offset;
    bool active_low;
};

The descriptor layer provides:

  • Ownership tracking
  • Active-low translation
  • GPIO abstraction

GPIO Consumer APIs

Request GPIO

Mandatory GPIO:

reset_gpio =
    devm_gpiod_get(dev, "reset");

Looks up:

reset-gpios

from Device Tree.


Optional GPIO

Optional GPIO:

enable_gpio =
    devm_gpiod_get_optional(dev, "enable");

Looks up:

enable-gpios

If the property does not exist:

NULL

is returned.


Configure Direction

Output:

gpiod_direction_output(desc, value);

Input:

gpiod_direction_input(desc);

Set Output Value

gpiod_set_value(desc, value);

Logical values are used.

For active-low GPIOs:

logical 1
physical 0

logical 0
physical 1

Read Input Value

value = gpiod_get_value(desc);

Returned value is a logical value.


GPIO Consumer Driver Flow

Typical probe flow:

probe()
devm_gpiod_get()
gpiod_direction_output()
gpiod_set_value()
device ready

Example:

reset_gpio =
    devm_gpiod_get(dev, "reset");

enable_gpio =
    devm_gpiod_get_optional(dev, "enable");

gpiod_direction_output(reset_gpio, 1);
gpiod_direction_output(enable_gpio, 1);

gpiod_set_value(reset_gpio, 0);

Remove Flow

Typical remove flow:

remove()
disable device
assert reset
release resources

Example:

gpiod_set_value(enable_gpio, 0);
gpiod_set_value(reset_gpio, 1);

Relationship with Platform Drivers

GPIO consumer APIs are commonly used inside platform drivers.

Device Tree
Platform Driver
probe()
devm_gpiod_get()
gpio_desc
gpiod_set_value()

This is the standard GPIO access model used throughout the Linux kernel.