From Variscite Wiki
Revision as of 11:14, 8 May 2023 by Alifer (talk | contribs) (Created page with "<!-- Set release according to the "release" parameter in URL and use mx8mp-yocto-hardknott-5.10.72_2.2.1-v1.0 as default --> {{INIT_RELEASE_PARAM|mx8x-yocto-kirkstone-5.15-2.0...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Warning: This page is designed to be used with a 'release' URL parameter.

This page is using the default release mx8x-yocto-kirkstone-5.15-2.0.x-v1.0.
To view this page for a specific Variscite SoM and software release, please follow these steps:

  1. Visit
  2. Select your SoM
  3. Select the software release

GPIO state

The current state of the system's GPIOs can be obtained in user space, as shown in the following example:

# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 0-31, parent: platform/5d080000.gpio, 5d080000.gpio:
 gpio-12  (                    |ov5640_mipi_reset   ) out hi    
 gpio-16  (                    |fsl_lpspi           ) in  hi    
 gpio-26  (                    |ov5640_mipi_pwdn    ) out lo    

gpiochip1: GPIOs 32-63, parent: platform/5d090000.gpio, 5d090000.gpio:
 gpio-32  (                    |fsl_lpspi           ) in  hi    
 gpio-34  (                    |phy-reset           ) out hi    
 gpio-39  (                    |reg_sd_pwr          ) out hi    
 gpio-60  (                    |reg_ethphy0         ) out hi    

gpiochip2: GPIOs 64-95, parent: platform/5d0a0000.gpio, 5d0a0000.gpio:

gpiochip3: GPIOs 96-127, parent: platform/5d0b0000.gpio, 5d0b0000.gpio:
 gpio-120 (                    |cam_buf_en          ) out hi    

gpiochip4: GPIOs 128-159, parent: platform/5d0c0000.gpio, 5d0c0000.gpio:
 gpio-149 (                    |usb_otg1_vbus       ) out hi    
 gpio-150 (                    |cd                  ) in  lo IRQ

gpiochip5: GPIOs 160-191, parent: platform/5d0d0000.gpio, 5d0d0000.gpio:
 gpio-169 (                    |connect             ) in  hi IRQ

gpiochip6: GPIOs 192-223, parent: platform/5d0e0000.gpio, 5d0e0000.gpio:

gpiochip7: GPIOs 224-255, parent: platform/5d0f0000.gpio, 5d0f0000.gpio:

gpiochip8: GPIOs 504-511, parent: i2c/1-0020, pca9534, can sleep:
 gpio-504 (                    |heartbeat           ) out lo    
 gpio-505 (                    |Back                ) in  hi IRQ
 gpio-506 (                    |Home                ) in  hi IRQ
 gpio-507 (                    |Menu                ) in  hi IRQ
 gpio-508 (                    |usb3_sel            ) out lo    
 gpio-509 (                    |phy-reset           ) out hi    
 gpio-510 (                    |reg_vselect         ) out hi    
 gpio-511 (                    |reg_ethphy1         ) out hi    

Each GPIO is defined as in or out, and the state is shown as lo or hi.
For example, pin 150 is the SD card card-detect. When an SD card is plugged in, the state will be:

gpio-150  (                    |cd                  ) in  lo IRQ

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

gpio-150  (                    |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.
The GPIO sysfs interface is no longer included in the Linux kernel by default. To include it, the following lines should be added to the Linux kernel defconfig:


For a guide on how to manipulate GPIO via sysfs interface see: Manipulating a single GPIO via /sys/class/gpio

libgpiod via command line

libgpiod provides command line utilities for GPIO:

gpiodetect List all gpiochips present on the system, their names, labels, and the number of GPIO lines.
gpioinfo List all lines of specified gpiochips, their names, consumers, direction, active state, and additional flags.
gpioget Read the values of the specified GPIO lines (not valid if the line is already requested). The line will be then configured as an input.
gpioset Set the values of the specified GPIO lines, potentially keeping the lines exported, and wait until timeout, user input, or signal (not valid if the line is already requested). The line will be then configured as output.
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 [30200000.gpio] (32 lines)
gpiochip1 [30210000.gpio] (32 lines)

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

# gpioinfo gpiochip0
gpiochip0 - 32 lines:
        line   0:      unnamed    "spi_imx"  output  active-high [used]
        line   1:      unnamed       unused   input  active-high
        line   2:      unnamed       unused   input  active-high

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

For example, assuming GPIO4_21 is configured as a GPIO in your device tree:

Set GPIO4_21 high:

gpioset gpiochip3 21=1

Set GPIO4_21 low:

gpioset gpiochip3 21=0

Read GPIO4_21:

gpioget gpiochip3 21GPIO_SYSFS_DRAFT

The gpiomon utility is useful for polling the lines to expect incoming input events.
For example, wait for three rising edge events on a given GPIO line:

gpiomon --num-events=3 --rising-edge gpiochip3 21
event:  RISING EDGE offset: 3 timestamp: [    1151.814356387]
event:  RISING EDGE offset: 3 timestamp: [    1151.815449803]
event:  RISING EDGE offset: 3 timestamp: [    1152.091556803]

Porting scripts using sysfs GPIO interface to libgpiod

Migrating from sysfs GPIO interface to libgpiod can be accomplished using the libgpiod package's command-line tools. For bash scripts, instead of writing or reading from /sys/class/gpio, you'll use these tools.
Here are a few examples of how you'd read and write in a GPIO using sysfs interface and the equivalent operations through libgpiod:


GPIOs in i.MX SoCs are grouped in groups of 32 pins. That is relevant when converting the GPIO absolute number used by sysfs GPIO interface.
For example:
GPIO 17 belongs to gpiochip0, pin 17: 32 * 0 + 17 equals 17.
GPIO 83 belongs to gpiochip2, pin 19: 32 * 2 + 19 equals 83.
GPIO 149 belongs to gpiochip4, pin 21: 32 * 4 + 21 equals 149.

# writing in a GPIO using sysfs GPIO interface
echo 149 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio149/direction
echo 1 > /sys/class/gpio/gpio149/value

# writing in a GPIO using libgpiod
gpioset gpiochip4 21=1
# reading a GPIO using sysfs GPIO interface
echo 149 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio149/direction
cat /sys/class/gpio/gpio149/value

# reading a GPIO using libgpiod
gpioget gpiochip4 21

Remember, it is also important to check the return status of these commands in your scripts to ensure they are working correctly. If a command fails, it will return a non-zero status which can be checked in the $? variable, this variable holds the exit status of the last command that was executed. For example:

gpioget gpiochip4 21
if [ $? -ne 0 ]; then
  echo "Failed to get GPIO value"
  exit 1

libgpiod C Application

libgpiod provides bindings for C/C++ 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 GPIO4_IO21:


all: main.cpp
	$(CC) $(CCFLAGS) -Og -lgpiod main.c -g -o hello.bin
	rm -f hello.bin


#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define    CONSUMER    "Variscite Demo"

int main(int argc, char **argv)
    unsigned int i, ret, val;
    struct gpiod_chip *chip;
    struct gpiod_line *line;
    const char * chipname = "gpiochip3";
    const unsigned int line_num = 21;

    chip = gpiod_chip_open_by_name(chipname);
    if (!chip) {
        perror("Open chip failed\n");
        goto end;

    line = gpiod_chip_get_line(chip, line_num);
    if (!line) {
        perror("Get line failed\n");
        goto close_chip;

    ret = gpiod_line_request_output(line, CONSUMER, 0);
    if (ret < 0) {
        perror("Request line as output failed\n");
        goto release_line;

    /* Blink 5 times */
    val = 0;
    for (i = 0; i < 5; i++) {
        ret = gpiod_line_set_value(line, val);
        if (ret < 0) {
            perror("Set line output failed\n");
            goto release_line;
        printf("Output %u on line #%u\n", val, line_num);
        val = !val;

    return 0;

libgpiod Python Application

libgpiod provides bindings for python applications:

# pip3 install gpiod

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

Kernel Device Tree GPIO configuration

Device Tree GPIO files

Pin Func files

In the directory include/dt-bindings/pinctrl/ of the Linux kernel source you will find the pin functions definition files.
The relevant file is pads-imx8qxp.h.
If you search it for GPIO0_IO20, for example, you will see a goup of definitions with the same prefix (pad name) "SC_P_MCLK_OUT0",

#define SC_P_MCLK_OUT0_ADMA_ACM_MCLK_OUT0                       SC_P_MCLK_OUT0                     0
#define SC_P_MCLK_OUT0_ADMA_ESAI0_TX_HF_CLK                     SC_P_MCLK_OUT0                     1
#define SC_P_MCLK_OUT0_ADMA_LCDIF_CLK                           SC_P_MCLK_OUT0                     2
#define SC_P_MCLK_OUT0_ADMA_SPI2_SDO                            SC_P_MCLK_OUT0                     3
#define SC_P_MCLK_OUT0_LSIO_GPIO0_IO20                          SC_P_MCLK_OUT0                     4

Adding only the one with the GPIO0_IO20 suffix (function) to your dts file will let you use the pin as GPIO.

Define a pin as GPIO in the kernel Device Tree

You need to add the relevant definitions to your device tree, as explained in the Pin Func files section above.
Edit arch/arm64/boot/dts/freescale/fsl-imx8qxp-var-som-common.dtsi and add the definition for the GPIO you need in the iomuxc node.

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

	imx8qxp-var-som {
		pinctrl_hog: hoggrp {
			fsl,pins = <
				/* Add your GPIO definitions here */ 

Device Tree GPIO attribute

If you look at the pin control definitions in arch/arm64/boot/dts/freescale/fsl-imx8qxp-var-som.dts in the Linux kernel source tree, the number to the right of the pin mux macro can be used for additional attributes like pull-up, slew rate, open drain, drive strength, etc.
This value is written to the IOMUXC_SW_PAD_CTRL_ register of the relevant pin.
Please consult the SOC reference manual for details about the relevant register.


In U-Boot, GPIO pins can be configured and manipulated using the "gpio" command. This command provides various options to configure the GPIO pins, set their direction (input or output), and toggle their values.


To enable the "gpio" command line tool in U-Boot, the defconfig file must have the following line:



=> gpio help
gpio - query and control gpio pins
Usage: gpio <input|set|clear|toggle> <pin> - input/set/clear/toggle the specified pin gpio status [-a] [<bank> | <pin>] - show [all/claimed] GPIOs

Switch the GPIO pin to input mode:

=> gpio input <pin>

Switch the GPIO pin to output mode and set its value to 1:

=> gpio set <pin>

Switch the GPIO pin to output mode and set its value to 0:

=> gpio clear <pin>

Switch the GPIO pin to output mode and toggle its value:

=> gpio toggle <pin>

Display the status of one or multiple GPIOs:

=> gpio status

By default only claimed GPIOs are displayed. To show unclaimed GPIOs, the -a parameter must be used:

=> gpio status -a

gpio status command output fields are:

<name>: <function>: <value> [x] <label>

function can take the following values:

  • output: pin configured in gpio output, the value indicates the pin’s level
  • input: pin configured in gpio input, the value indicates the pin’s level
  • func: pin configured in alternate function, followed by the label which shows pinmuxing label.
  • unused: pin not configured

[x] or [ ] indicate, respectively, if the gpio is used or not.
label shows the gpio label.