MX8M GPIO

From Variscite Wiki


DART-MX95 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 lo 
 gpio-523 (                    |sda                 ) in  lo 
 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 lo 
 gpio-541 (                    |sda                 ) in  lo 

gpiochip1: GPIOs 544-575, parent: platform/43820000.gpio, 43820000.gpio:
 gpio-544 (                    |cd                  ) in  lo IRQ ACTIVE LOW
 gpio-551 (                    |regulator-usdhc2    ) 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-622 (                    |int                 ) in  hi IRQ 
 gpio-624 (                    |PHY reset           ) out hi ACTIVE LOW

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-673 (                    |reset               ) out hi ACTIVE LOW
 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 (                    |PHY reset           ) out hi ACTIVE LOW
 gpio-683 (                    |PCIe reset          ) out hi 
 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 (                    |scl                 ) out lo 
 gpio-523 (                    |sda                 ) in  lo 
 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 lo 
 gpio-541 (                    |sda                 ) in  lo 

gpiochip1: GPIOs 544-575, parent: platform/43820000.gpio, 43820000.gpio:
 gpio-544 (                    |cd                  ) in  lo IRQ ACTIVE LOW
 gpio-551 (                    |regulator-usdhc2    ) 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-622 (                    |int                 ) in  hi IRQ 
 gpio-624 (                    |PHY reset           ) out hi ACTIVE LOW

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 (                    |?                   ) in  hi ACTIVE LOW
 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-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 (                    |PHY reset           ) out hi ACTIVE LOW
 gpio-690 (                    |PCIe reset          ) out hi 
 gpio-691 (                    |PCIe reset          ) out hi 
 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 pin 64 is the SD card card-detect. When an SD card is plugged in, the state will be:

gpio-64  (                    |cd                  ) in  lo IRQ

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

gpio-64  (                    |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 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 [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 21

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:

Makefile:

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

main.c

#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);
        sleep(1);
        val = !val;
    }

release_line:
    gpiod_line_release(line);
close_chip:
    gpiod_chip_close(chip);
end:
    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

Adding only the one with the GPIO4_IO2 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.


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

Device Tree GPIO attribute

If you look at the pin control definitions in arch/arm64/boot/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.