Skip to content

GPIO

GPIO state

The current state of the system's GPIOs can be obtained in user-mode, as shown in the following example for DART-MX93 on Sonata carrier board:

# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 512-543, parent: platform/43810000.gpio, 43810000.gpio:
 gpio-512 (                    |spi0 CS1            ) out hi ACTIVE LOW
 gpio-513 (                    |spi0 CS0            ) out hi ACTIVE LOW
 gpio-518 (                    |sda                 ) in  lo
 gpio-519 (                    |scl                 ) out lo
 gpio-523 (                    |eMMC                ) out lo
 gpio-530 (                    |regulator-usdhc2    ) out hi
 gpio-534 (                    |sda                 ) in  hi
 gpio-535 (                    |scl                 ) out hi
 gpio-540 (                    |sda                 ) in  lo
 gpio-541 (                    |scl                 ) out lo

gpiochip1: GPIOs 544-575, parent: platform/43820000.gpio, 43820000.gpio:
 gpio-544 (                    |cd                  ) in  lo IRQ ACTIVE LOW
 gpio-551 (                    |reset               ) out hi ACTIVE LOW

gpiochip2: GPIOs 576-607, parent: platform/43830000.gpio, 43830000.gpio:
 gpio-590 (                    |reset               ) out hi ACTIVE LOW
 gpio-605 (                    |int                 ) in  hi IRQ

gpiochip3: GPIOs 608-639, parent: platform/47400000.gpio, 47400000.gpio:
 gpio-608 (                    |scl                 ) out lo
 gpio-609 (                    |sda                 ) in  lo
 gpio-615 (                    |PHY reset           ) out hi ACTIVE LOW

gpiochip4: GPIOs 640-647, parent: i2c/0-0022, 0-0022, can sleep:

gpiochip5: GPIOs 648-655, parent: i2c/6-0020, 6-0020, can sleep:
 gpio-649 (                    |reset               ) out hi ACTIVE LOW
 gpio-651 (                    |powerdown           ) out hi
 gpio-652 (                    |Home                ) in  hi IRQ ACTIVE LOW
 gpio-653 (                    |Up                  ) in  hi IRQ ACTIVE LOW
 gpio-654 (                    |Down                ) in  hi IRQ ACTIVE LOW
 gpio-655 (                    |Back                ) in  hi IRQ ACTIVE LOW

gpiochip6: GPIOs 656-663, parent: i2c/6-0021, 6-0021, can sleep:
 gpio-656 (                    |regulator-fec-phy-rs) out hi
 gpio-660 (                    |reset               ) out hi ACTIVE LOW

Each GPIO is defined as in or out and the state is shown as lo or hi. For example gpio-544 is the SD card detect pin.

When an SD card is plugged in, the state will be:

gpio-544  (                    |cd                  ) in  lo IRQ

When the SD card is removed, the state will be:

gpio-544  (                    |cd                  ) in  hi IRQ

Manipulating GPIO using libgpiod

The Linux GPIO sysfs interface is being deprecated. Moving forward, user-space should use the character device /dev/gpiochip* instead. libgpiod provides bindings and utilities for manipulating GPIO via user-space.

libgpiod via command-line

libgpiod provides command-line utilities for GPIO:

gpiodetect List all gpiochips present on the system, their names, labels and number of GPIO lines
gpioinfo List all lines of specified gpiochips, their names, consumers, direction, active state and additional flags
gpioget Read values of specified GPIO lines
gpioset Set values of specified GPIO lines, potentially keep the lines exported and wait until timeout, user input or signal
gpiofind Find the gpiochip name and line offset given the line name
gpiomon Wait for events on GPIO lines, specify which events to watch, how many events to process before exiting or if the events should be reported to the console

i.MX GPIOs are organized in banks of 32 pins. Each bank corresponds to a character device /dev/gpiochip<bank index>. The gpiodetect utility can be used to inspect the available gpiochip character devices:

# gpiodetect
gpiochip0 [43810000.gpio] (32 lines)
gpiochip1 [43820000.gpio] (32 lines)
...

The gpioinfo utility can be used to inspect the lines for a given gpiochip:

# gpioinfo -c gpiochip0
gpiochip0 - 32 lines:
    line   0:   unnamed             output active-low consumer=spi0 CS1
    line   1:   unnamed             output active-low consumer=spi0 CS0
    line   2:   unnamed             input consumer=interrupt
        ...

The gpioset and gpioget utilities can be used to manipulate GPIO from the command-line.

For example, assuming GPIO2_21 is configured in your device tree:

Set GPIO2_21 high:

gpioset -t 0 -c gpiochip0 21=1

Set GPIO2_21 low:

gpioset -t 0 -c gpiochip0 21=0

Read GPIO2_21:

gpioget -c gpiochip0 21

Note: To verify the output value of a GPIO pin after using the gpioset command, you can physically connect (jumper) it to a separate GPIO configured as an input. Then, use the gpioget command on the input pin to read the output state.

libgpiod C++ Application

libgpiod provides bindings for C++, Python and Rust applications. C++ examples are available in the libgpiod /tree/bindings/cxx/examples directory.

Below is a simple C application demonstrating how to use the bindings with GPIO0_IO05:

Makefile:

LDFLAGS = -lgpiodcxx -lgpiod

# Nome do executável e arquivos fonte
TARGET = main
SRCS = main.cpp

all: $(TARGET)

$(TARGET): $(SRCS)
    $(CXX) $(SRCS) $(LDFLAGS) -o $(TARGET)

clean:
    rm -f $(TARGET)

main.cpp

// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>

/* Minimal example of reading a single line. */

#include <cstdlib>
#include <filesystem>
#include <gpiod.hpp>
#include <iostream>

namespace {

/* Example configuration - customize to suit your situation */
const ::std::filesystem::path chip_path("/dev/gpiochip0");
const ::gpiod::line::offset line_offset = 5;

} /* namespace */

int main()
{
    auto request = ::gpiod::chip(chip_path)
                   .prepare_request()
                   .set_consumer("get-line-value")
                   .add_line_settings(
                       line_offset,
                       ::gpiod::line_settings().set_direction(
                           ::gpiod::line::direction::INPUT))
                   .do_request();

    ::std::cout << line_offset << "="
            << (request.get_value(line_offset) ==
                    ::gpiod::line::value::ACTIVE ?
                "Active" :
                "Inactive")
            << ::std::endl;

    return EXIT_SUCCESS;
}

libgpiod Python Application

libgpiod provides bindings for Python applications.

Recommended (Debian/apt):

apt update
apt install python3-gpiod

Alternative (venv + pip):

apt update
apt install python3-venv
python3 -m venv /opt/gpio-venv
. /opt/gpio-venv/bin/activate
pip install gpiod

Python examples are available in the libgpiod /tree/bindings/python/examples directory.

Kernel Device Tree GPIO configuration

Device Tree Source files

Pin Function file

Pin function definitions for the i.MX93 processor are located in the Linux kernel source tree at arch/arm64/boot/dts/freescale/imx93-pinfunc.h. In this header file, pin multiplexing configurations are grouped by physical pad, with all available alternate functions listed for each pad. For example, the MX93_PAD_GPIO_IO21 pad includes the GPIO2_IO21 function alongside its other available functions:

#define MX93_PAD_GPIO_IO21__GPIO2_IO21                            0x0064 0x0214 0x0000 0x0 0x0
#define MX93_PAD_GPIO_IO21__SAI3_TX_DATA00                        0x0064 0x0214 0x0000 0x1 0x0
#define MX93_PAD_GPIO_IO21__PDM_CLK                               0x0064 0x0214 0x0000 0x2 0x0
#define MX93_PAD_GPIO_IO21__MEDIAMIX_DISP_DATA17                  0x0064 0x0214 0x0000 0x3 0x0
#define MX93_PAD_GPIO_IO21__LPSPI5_SCK                            0x0064 0x0214 0x0000 0x4 0x0
#define MX93_PAD_GPIO_IO21__LPSPI4_SCK                            0x0064 0x0214 0x0000 0x5 0x0
#define MX93_PAD_GPIO_IO21__TPM4_CH1                              0x0064 0x0214 0x0000 0x6 0x0
#define MX93_PAD_GPIO_IO21__SAI3_RX_BCLK                          0x0064 0x0214 0x044C 0x7 0x1

Selecting the GPIO2_IO21 function (mux mode) for this pad in your Device Tree enables the pin for GPIO usage.

Define a pin as GPIO in the kernel Device Tree

To configure a pin as a GPIO, identify the pin function macro in imx93-pinfunc.h as described in the Pin Function file section above.

Edit arch/arm64/boot/dts/freescale/imx93-var-dart-sonata.dts and add the GPIO configuration to the IOMUXC node, for example:

&iomuxc {
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_hog>;

    pinctrl_hog: hoggrp {
        fsl,pins = <
            MX93_PAD_GPIO_IO21__GPIO2_IO21          0x31e
        >;
    };
        /* ... */
};

Please consult Variscite's blog post i.MX Device Tree Pinmux Settings Guide for further information.

Device Tree GPIO attribute

In the pinctrl example above, the hexadecimal value to the right of the pinmux macro specifies the PAD settings such as pull-up, pull-down, slew rate, open drain, drive strength, etc. This value is written to the SW_PAD_CTL_PAD_<PAD_NAME> register.

Refer to the i.MX 93 Applications Processor Reference Manual for the exact register bit definitions.