GPIO¶
GPIO state¶
The current state of the system's GPIOs can be obtained in user-mode, as shown in the following example: GPIOs state for the DT8MCustomBoard carrier board:
root@imx95-var-dart:~# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 512-543, parent: platform/43810000.gpio, 43810000.gpio:
gpio-516 ( |spi0 CS0 ) out hi ACTIVE LOW
gpio-522 ( |scl ) out hi
gpio-523 ( |sda ) in hi
gpio-534 ( |microchip,rx-int ) in hi ACTIVE LOW
gpio-536 ( |pendown ) in hi IRQ ACTIVE LOW
gpio-539 ( |reset ) out hi ACTIVE LOW
gpio-540 ( |scl ) out hi
gpio-541 ( |sda ) in hi
gpiochip1: GPIOs 544-575, parent: platform/43820000.gpio, 43820000.gpio:
gpio-544 ( |cd ) in lo IRQ ACTIVE LOW
gpio-551 ( |regulator-vmmc-usdhc) out hi
gpio-571 ( |Heartbeat ) out lo
gpiochip2: GPIOs 576-607, parent: platform/43840000.gpio, 43840000.gpio:
gpio-605 ( |reset ) out hi ACTIVE LOW
gpiochip3: GPIOs 608-639, parent: platform/43850000.gpio, 43850000.gpio:
gpio-612 ( |spi0 CS2 ) out hi ACTIVE LOW
gpio-620 ( |interrupt ) in hi IRQ
gpio-621 ( |interrupt ) in hi IRQ
gpio-622 ( |interrupt ) in hi IRQ
gpio-624 ( |PHY reset ) out hi ACTIVE LOW
gpio-625 ( |interrupt ) in hi IRQ
gpiochip4: GPIOs 640-671, parent: platform/47400000.gpio, 47400000.gpio:
gpio-647 ( |spi0 CS1 ) out hi ACTIVE LOW
gpiochip5: GPIOs 672-679, parent: i2c/7-0020, 7-0020, can sleep:
gpio-672 ( |reset ) out hi ACTIVE LOW
gpio-673 ( |reset ) out hi ACTIVE LOW
gpio-674 ( |powerdown ) out hi
gpio-675 ( |powerdown ) out hi
gpio-676 ( |Home ) in hi IRQ ACTIVE LOW
gpio-677 ( |Up ) in hi IRQ ACTIVE LOW
gpio-678 ( |Down ) in hi IRQ ACTIVE LOW
gpio-679 ( |Back ) in hi IRQ ACTIVE LOW
gpiochip6: GPIOs 680-687, parent: i2c/7-0021, 7-0021, can sleep:
gpio-680 ( |regulator-vddio-phy1) out hi
gpio-683 ( |PCIe reset ) out hi ACTIVE LOW
gpio-684 ( |reset ) out hi ACTIVE LOW
GPIOs state for the Sonata carrier board:
root@imx95-var-dart:~# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 512-543, parent: platform/43810000.gpio, 43810000.gpio:
gpio-516 ( |spi0 CS0 ) out hi ACTIVE LOW
gpio-522 ( |sda ) in hi
gpio-523 ( |scl ) out hi
gpio-534 ( |microchip,rx-int ) in hi ACTIVE LOW
gpio-536 ( |pendown ) in hi IRQ ACTIVE LOW
gpio-539 ( |reset ) out hi ACTIVE LOW
gpio-540 ( |sda ) in hi
gpio-541 ( |scl ) out hi
gpiochip1: GPIOs 544-575, parent: platform/43820000.gpio, 43820000.gpio:
gpio-544 ( |cd ) in lo IRQ ACTIVE LOW
gpio-551 ( |regulator-vmmc-usdhc) out hi
gpio-571 ( |Heartbeat ) out lo
gpiochip2: GPIOs 576-607, parent: platform/43840000.gpio, 43840000.gpio:
gpio-605 ( |reset ) out hi ACTIVE LOW
gpiochip3: GPIOs 608-639, parent: platform/43850000.gpio, 43850000.gpio:
gpio-612 ( |spi0 CS2 ) out hi ACTIVE LOW
gpio-620 ( |interrupt ) in hi IRQ
gpio-621 ( |interrupt ) in hi IRQ
gpio-622 ( |interrupt ) in hi IRQ
gpio-624 ( |PHY reset ) out hi ACTIVE LOW
gpio-625 ( |interrupt ) in hi IRQ
gpiochip4: GPIOs 640-671, parent: platform/47400000.gpio, 47400000.gpio:
gpio-647 ( |spi0 CS1 ) out hi ACTIVE LOW
gpiochip5: GPIOs 672-679, parent: i2c/2-0022, 2-0022, can sleep:
gpio-673 ( |los ) in hi IRQ
gpio-677 ( |sfp-sw ) out hi
gpio-678 ( |pcie-clk-sw ) out lo
gpiochip6: GPIOs 680-687, parent: i2c/7-0020, 7-0020, can sleep:
gpio-680 ( |reset ) out hi ACTIVE LOW
gpio-681 ( |reset ) out hi ACTIVE LOW
gpio-682 ( |powerdown ) out hi
gpio-683 ( |powerdown ) out hi
gpio-684 ( |Home ) in hi IRQ ACTIVE LOW
gpio-685 ( |Up ) in hi IRQ ACTIVE LOW
gpio-686 ( |Down ) in hi IRQ ACTIVE LOW
gpio-687 ( |Back ) in hi IRQ ACTIVE LOW
gpiochip7: GPIOs 688-695, parent: i2c/7-0021, 7-0021, can sleep:
gpio-688 ( |regulator-vddio-phy1) out hi
gpio-690 ( |PCIe reset ) out hi ACTIVE LOW
gpio-691 ( |PCIe reset ) out hi ACTIVE LOW
gpio-692 ( |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:
When the SD card is removed, the state will be:
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:
The gpioinfo utility can be used to inspect the lines for a given gpiochip:
# gpioinfo -c gpiochip0
gpiochip0 - 32 lines:
...
line 9: unnamed input
line 10: unnamed input drive=open-drain consumer="sda"
line 11: unnamed output drive=open-drain consumer="scl"
line 12: unnamed input
...
The gpioset and gpioget utilities can be used to manipulate GPIO from the
command-line.
For example, assuming GPIO5_7 is configured in your device tree:
Set GPIO5_7 high:
Set GPIO5_7 low:
Read GPIO5_7:
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):
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¶
The pin function definitions for the i.MX95 processor are located in the Linux kernel source code
under the directory arch/arm64/boot/dts/freescale/imx95-pinfunc.h. Within this header file,
pin multiplexing configurations are grouped by physical pad names. For instance, the macro GPIO5_IO_BIT7
is defined alongside other functions associated with the pad IMX95_PAD_XSPI1_DATA7:
#define IMX95_PAD_XSPI1_DATA7__FLEXSPI1_A_DATA_BIT7 0x018C 0x0390 0x04F0 0x00 0x01
#define IMX95_PAD_XSPI1_DATA7__SAI5_RX_DATA_BIT0 0x018C 0x0390 0x05AC 0x01 0x01
#define IMX95_PAD_XSPI1_DATA7__SAI5_TX_DATA_BIT1 0x018C 0x0390 0x0000 0x02 0x00
#define IMX95_PAD_XSPI1_DATA7__XSPI_DATA_BIT7 0x018C 0x0390 0x0608 0x04 0x01
#define IMX95_PAD_XSPI1_DATA7__GPIO5_IO_BIT7 0x018C 0x0390 0x0000 0x05 0x00
Selecting the GPIO5_IO_BIT7 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 imx95-pinfunc.h as described in
the Pin Function file section above.
Edit arch/arm64/boot/dts/freescale/imx95-var-dart-sonata.dts and add the
GPIO configuration to the IOMUXC node, for example:
&scmi_iomuxc {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_hog>;
pinctrl_hog: hoggrp {
fsl,pins = <
IMX95_PAD_XSPI1_DATA7__GPIO5_IO_BIT7 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 95 Applications Processor Reference Manual for the exact register bit definitions.