MX8M GPIO: Difference between revisions

From Variscite Wiki
No edit summary
No edit summary
 
(37 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<!-- Set release according to "release" parameter in URL and use RELEASE_SUMO_V1.0_DART-MX8M as default
<!-- Set release according to "release" parameter in URL and use RELEASE_SUMO_V1.0_DART-MX8M as default
--> {{#vardefine:RELEASE_PARAM|{{#urlget:release|RELEASE_SUMO_V1.0_DART-MX8M}}}} <!--
--> {{INIT_RELEASE_PARAM|RELEASE_SUMO_V1.0_DART-MX8M}}<!--
--> {{#lst:Yocto_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:Yocto_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:B2QT_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:B2QT_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:Android_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:Android_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#lst:Debian_Platform_Customization|{{#var:RELEASE_PARAM}}}} <!--
--> {{#vardefine: SD_CD_GPIO | 44}} <!--
--> {{PageHeader|{{#var:HARDWARE_NAME}} GPIO}} {{DocImage|category1=Yocto|category2=Android}}[[Category:Debian]][[Category:{{#var:HARDWARE_NAME}}]] __toc__
--> {{PageHeader|{{#var:HARDWARE_NAME}} GPIO}} {{DocImage|category1=Yocto|category2=Android}}[[Category:Debian]][[Category:{{#var:HARDWARE_NAME}}]] __toc__


= GPIO state =
= GPIO state =
The current state of the system's GPIOs can be obtained in user-mode, as shown in the following example:
The current state of the system's GPIOs can be obtained in user-mode, as shown in the following example:
{{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M |
{{#switch: {{#var:HARDWARE_NAME}} | DART-MX8M=
<pre>
<pre>
# cat /sys/kernel/debug/gpio
# cat /sys/kernel/debug/gpio
Line 29: Line 31:
  gpio-114 (                    |Up                  ) in  hi IRQ
  gpio-114 (                    |Up                  ) in  hi IRQ
</pre>
</pre>
| {{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M-MINI |
| DART-MX8M-MINI=
<pre>
<pre>
# cat /sys/kernel/debug/gpio  
# cat /sys/kernel/debug/gpio  
Line 60: Line 62:
  gpio-156 (                    |ov5640_mipi_reset  ) out hi
  gpio-156 (                    |ov5640_mipi_reset  ) out hi
</pre>
</pre>
| {{#ifeq: {{#var:HARDWARE_NAME}} | VAR-SOM-MX8M-NANO |
| VAR-SOM-MX8M-NANO=
<pre>
<pre>
# cat /sys/kernel/debug/gpio
# cat /sys/kernel/debug/gpio
Line 100: Line 102:
  gpio-511 (                    |enet_sel            ) out lo     
  gpio-511 (                    |enet_sel            ) out lo     
</pre>
</pre>
| {{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M-PLUS |
| DART-MX8M-PLUS=
<pre>
gpiochip0: GPIOs 0-31, parent: platform/30200000.gpio, 30200000.gpio:
gpiochip0: GPIOs 0-31, parent: platform/30200000.gpio, 30200000.gpio:
  gpio-5  (                    |hdmi_on            ) out lo  
  gpio-5  (                    |hdmi_on            ) out lo  
Line 141: Line 144:
  gpio-149 (                    |sda                ) in  lo  
  gpio-149 (                    |sda                ) in  lo  
  gpio-156 (                    |reset              ) out hi ACTIVE LOW
  gpio-156 (                    |reset              ) out hi ACTIVE LOW
|}} }} }} }}
</pre>
| VAR-SOM-MX93=
<pre>
# cat /sys/kernel/debug/gpio
gpiochip3: GPIOs 0-31, parent: platform/47400080.gpio, 47400080.gpio:
gpio-0  (                    |spi0 CS0            ) out hi ACTIVE LOW
gpio-1  (                    |sda                ) in  lo
gpio-7  (                    |regulator-eqos-phy  ) out hi
 
gpiochip0: GPIOs 32-63, parent: platform/43810080.gpio, 43810080.gpio:
gpio-43  (                    |powerdown          ) out hi
gpio-44  (                    |spi1 CS0            ) out hi ACTIVE LOW
gpio-49  (                    |cam_buf_dis        ) in  lo
gpio-50  (                    |regulator-usdhc2    ) out hi
gpio-54  (                    |sda                ) in  lo
gpio-55  (                    |scl                ) out lo
gpio-60  (                    |sda                ) in  lo
gpio-61  (                    |scl                ) out lo
 
gpiochip1: GPIOs 64-95, parent: platform/43820080.gpio, 43820080.gpio:
gpio-64  (                    |cd                  ) in  lo IRQ ACTIVE LOW
gpio-71  (                    |regulator-usdhc3    ) out hi
gpio-91  (                    |reset              ) out hi ACTIVE LOW
 
gpiochip2: GPIOs 96-127, parent: platform/43830080.gpio, 43830080.gpio:
gpio-110 (                    |regulator-wl-on    ) out hi
 
gpiochip4: GPIOs 504-511, parent: i2c/4-0020, 4-0020, can sleep:
gpio-505 (                    |Back                ) in  hi IRQ ACTIVE LOW
gpio-506 (                    |Home                ) in  hi IRQ ACTIVE LOW
gpio-507 (                    |Menu                ) in  hi IRQ ACTIVE LOW
gpio-509 (                    |regulator-fec-phy-rs) out hi
gpio-510 (                    |regulator-fec-phy-vs) out hi
gpio-511 (                    |regulator-fec-phy  ) out hi
</pre>
| DART-MX95=
<pre>
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-621 (                    |spi0 CS1            ) out hi ACTIVE LOW
gpio-622 (                    |int                ) in  hi IRQ
gpio-624 (                    |regulator-enet0-phy ) out hi
 
gpiochip4: GPIOs 640-671, parent: platform/47400000.gpio, 47400000.gpio:
 
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 (                    |regulator-enet1-phy ) out hi
gpio-683 (                    |PCIe reset          ) out hi
gpio-684 (                    |reset              ) out hi ACTIVE LOW
</pre>
{{#vardefine: SD_CD_GPIO | 64}}
}}


Each GPIO is defined as in or out and the state is shown as lo or hi.<br>
Each GPIO is defined as in or out and the state is shown as lo or hi.<br>
For example pin 44 is the SD card card-detect.
For example pin {{#var: SD_CD_GPIO}} is the SD card card-detect.
When an SD card is plugged in, the state will be:
When an SD card is plugged in, the state will be:
<pre>
  gpio-{{#var: SD_CD_GPIO}} (                    |cd                  ) in  lo IRQ
  gpio-44 (                    |cd                  ) in  lo IRQ
</pre>
When the SD card is removed, the state will be:
When the SD card is removed, the state will be:
gpio-{{#var: SD_CD_GPIO}}  (                    |cd                  ) in  hi IRQ
= Manipulating GPIO using libgpiod =
<section begin=libgpiod_section/><!--
-->{{#vardefine:LIBGPIOD_URL|https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/}}<!--
-->The Linux GPIO sysfs interface is being deprecated. Moving forward, user space should use the character device <code>/dev/gpiochip*</code> instead. libgpiod provides bindings and utilities for for manipulating GPIO via user space.
== libgpiod via command line ==
[{{#var:LIBGPIOD_URL}} libgpiod] provides command line utilities for GPIO:
{| class="wikitable"
|-
| '''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 <code>/dev/gpiochip<bank index></code>. The <code>gpiodetect</code> utility can be used to inspect the available gpiochip character devices:
# gpiodetect
gpiochip0 [30200000.gpio] (32 lines)
gpiochip1 [30210000.gpio] (32 lines)
...
The <code>gpioinfo</code> 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 <code>gpioset</code> and <code>gpioget</code> 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 ==
[{{#var:LIBGPIOD_URL}} libgpiod] provides bindings for C/C++ applications. C++ examples are available in the libgpiod [{{#var:LIBGPIOD_URL}}/tree/bindings/cxx/examples /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'''
<pre>
<pre>
gpio-44  (                   |cd                  ) in  hi IRQ
#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;
}
</pre>
</pre>


== libgpiod Python Application ==
libgpiod provides bindings for python applications:
# pip3 install gpiod
Python examples are available in the libgpiod [{{#var:LIBGPIOD_URL}}/tree/bindings/python/examples /tree/bindings/python/examples ] directory.
<section end=libgpiod_section/>
<!-- Start to phase out sysfs by hiding it for imx9 and future SOC_FAMILY. Eventually, drop this for all families
-->{{#switch:{{#var:SOC_FAMILY}}
|imx8m=
= Manipulating a single GPIO via /sys/class/gpio =
= Manipulating a single GPIO via /sys/class/gpio =
<section begin=gpio_sysfs_warning_section/> <!--
-->{{Note|Note: The /sys/class/gpio ABI is being deprecated in the Linux kernel and is being replaced by the GPIO character device and [{{#var:LIBGPIOD_URL}} libgpiod]. Please refer to the kernel [https://github.com/varigit/linux-imx/blob/{{#var:KERNEL_BRANCH}}/Documentation/ABI/obsolete/sysfs-gpio Documentation] for the latest information.|error}}<!--
--><section end=gpio_sysfs_warning_section/>
== Using a command line or a script ==
== Using a command line or a script ==
GPIOs in i.MX are grouped in groups of 32 pins.<br>
GPIOs in i.MX are grouped in groups of 32 pins.<br>
Line 275: Line 498:
</pre>
</pre>
* This is only a short example. If you want to use it in your code remember add error handling to it.
* This is only a short example. If you want to use it in your code remember add error handling to it.
}}<!-- END OF SYSFS -->


= Kernel Device Tree GPIO configuration =
= Kernel Device Tree GPIO configuration =
== Device Tree GPIO files ==
== Device Tree GPIO files ==
=== Pin Func files ===
=== Pin Func files ===
{{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M |
{{#switch: {{#var:HARDWARE_NAME}} | DART-MX8M=
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
The relevant file is imx8mq-pinfunc.h.<br>
The relevant file is imx8mq-pinfunc.h.<br>
Line 291: Line 515:
#define MX8MQ_IOMUXC_SAI1_RXD0_SIM_M_HADDR17                                0x164 0x3CC 0x000 0x7 0x0
#define MX8MQ_IOMUXC_SAI1_RXD0_SIM_M_HADDR17                                0x164 0x3CC 0x000 0x7 0x0
</pre>
</pre>
|{{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M-MINI |
| DART-MX8M-MINI=
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
The relevant file is imx8mm-pinfunc.h.<br>
The relevant file is imx8mm-pinfunc.h.<br>
Line 304: Line 528:
#define MX8MM_IOMUXC_SAI1_RXD0_SIM_M_HADDR17                                0x164 0x3CC 0x000 0x7 0x0
#define MX8MM_IOMUXC_SAI1_RXD0_SIM_M_HADDR17                                0x164 0x3CC 0x000 0x7 0x0
</pre>
</pre>
|{{#ifeq: {{#var:HARDWARE_NAME}} | VAR-SOM-MX8M-NANO |
| VAR-SOM-MX8M-NANO=
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
The relevant file is imx8mn-pinfunc.h.<br>
The relevant file is imx8mn-pinfunc.h.<br>
Line 316: Line 540:
#define MX8MN_IOMUXC_SAI2_RXC_PDM_BIT_STREAM1                              0x01B4 0x041C 0x0538 0x6 0x8
#define MX8MN_IOMUXC_SAI2_RXC_PDM_BIT_STREAM1                              0x01B4 0x041C 0x0538 0x6 0x8
</pre>
</pre>
|{{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M-PLUS |
| DART-MX8M-PLUS=
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
In the directory {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
The relevant file is imx8mp-pinfunc.h.<br>
The relevant file is imx8mp-pinfunc.h.<br>
Line 330: Line 554:


</pre>
</pre>
 
| VAR-SOM-MX93=
|}} }} }} }}
In the directory arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.<br>
Adding only the one with the {{#ifeq: {{#var:HARDWARE_NAME}}|VAR-SOM-MX8MN|GPIO4_IO22|{{#ifeq: {{#var:HARDWARE_NAME}}|DART-MX8M-PLUS|GPIO4_IO22|GPIO4_IO2}}}} suffix (function) to your dts file will let you use the pin as GPIO.
The relevant file is imx93-pinfunc.h.<br>
If you search it for GPIO_IO25, for example, you will see a group of definitions with same prefix (pad name), "MX93_PAD_GPIO_IO25".
<pre>
#define MX93_PAD_GPIO_IO25__GPIO2_IO25                            0x0074 0x0224 0x0000 0x0 0x0
#define MX93_PAD_GPIO_IO25__USDHC3_DATA1                          0x0074 0x0224 0x0464 0x1 0x0
#define MX93_PAD_GPIO_IO25__CAN2_TX                              0x0074 0x0224 0x0000 0x2 0x0
#define MX93_PAD_GPIO_IO25__MEDIAMIX_DISP_DATA21                  0x0074 0x0224 0x0000 0x3 0x0
#define MX93_PAD_GPIO_IO25__TPM4_CH3                              0x0074 0x0224 0x0000 0x4 0x0
#define MX93_PAD_GPIO_IO25__JTAG_MUX_TCK                          0x0074 0x0224 0x03D4 0x5 0x1
#define MX93_PAD_GPIO_IO25__LPSPI7_PCS1                          0x0074 0x0224 0x0000 0x6 0x0
#define MX93_PAD_GPIO_IO25__FLEXIO1_FLEXIO25                      0x0074 0x0224 0x03C4 0x7 0x0
</pre>
}}
Adding only the one with the {{#switch: {{#var:HARDWARE_NAME}}|VAR-SOM-MX8M-NANO|DART-MX8M-PLUS=GPIO4_IO22|VAR-SOM-MX93=GPIO_IO25|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 ==
== 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|Pin Func files]] section above.<br>
You need to add the relevant definitions to your device tree, as explained in the [[#Pin Func files|Pin Func files]] section above.<br>
{{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M |
{{#switch: {{#var:HARDWARE_NAME}} | DART-MX8M=
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/{{#ifeq: {{#var:YOCTO_NAME}} | Morty |variscite/imx8m-var-dart-common.dtsi|freescale/{{#var:DTB_PREFIX|fsl-imx8mq-var-dart}}{{#ifeq: {{#sub:{{#var:KERNEL_VER}}|0|2}} | 4. | -common | }}.dtsi}} and add the definition for the GPIO you need in the iomuxc node.<br>
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/{{#ifeq: {{#var:YOCTO_NAME}} | Morty |variscite/imx8m-var-dart-common.dtsi|freescale/{{#var:DTB_PREFIX|fsl-imx8mq-var-dart}}{{#ifeq: {{#sub:{{#var:KERNEL_VER}}|0|2}} | 4. | -common | }}.dtsi}} and add the definition for the GPIO you need in the iomuxc node.<br>
<pre>
<pre>
Line 353: Line 590:
};
};
</pre>
</pre>
|{{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M-MINI |
| DART-MX8M-MINI=
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale/{{#var:DTB_PREFIX|fsl-imx8mm-var-dart}}.dts and add the definition for the GPIO you need in the iomuxc node.<br>
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale/{{#var:DTB_PREFIX|fsl-imx8mm-var-dart}}.dts and add the definition for the GPIO you need in the iomuxc node.<br>
<pre>
<pre>
Line 370: Line 607:
};
};
</pre>
</pre>
| {{#ifeq: {{#var:HARDWARE_NAME}} | VAR-SOM-MX8M-NANO |
| VAR-SOM-MX8M-NANO=
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale/{{#var:DTB_PREFIX|fsl-imx8mn-var-som}}.dts and add the definition for the GPIO you need in the iomuxc node.<br>
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale/{{#var:DTB_PREFIX|fsl-imx8mn-var-som}}.dts and add the definition for the GPIO you need in the iomuxc node.<br>
<pre>
<pre>
Line 387: Line 624:
};
};
</pre>
</pre>
| {{#ifeq: {{#var:HARDWARE_NAME}} | DART-MX8M-PLUS |
| DART-MX8M-PLUS=
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale/imx8mp-var-dart.dts and add the definition for the GPIO you need in the iomuxc node.<br>
Edit {{#ifeq: {{#var:ANDROID_NAME}} | Pie |{{#var:BUILD_FOLDER}}/{{#var:BUILD_FOLDER_ANDROID}}/variscite/kernel_imx/}}arch/arm64/boot/dts/freescale/imx8mp-var-dart.dts and add the definition for the GPIO you need in the iomuxc node.<br>
<pre>
<pre>
Line 402: Line 639:
};
};
</pre>
</pre>
|}} }} }} }}
| VAR-SOM-MX93=
Edit arch/arm64/boot/dts/freescale/imx93-var-som-symphony.dts and add the definition for the GPIO you need in the iomuxc node.<br>
<pre>
&iomuxc {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_hog>;
 
pinctrl_hog: hoggrp {
fsl,pins = <
/* Add your GPIO definitions here */
>;
};
};
</pre>
}}


=== Device Tree GPIO attribute ===
=== Device Tree GPIO attribute ===

Latest revision as of 12:18, 23 October 2024

Warning: This page is designed to be used with a 'release' URL parameter.

This page is using the default release RELEASE_SUMO_V1.0_DART-MX8M.
To view this page for a specific Variscite SoM and software release, please follow these steps:

  1. Visit variwiki.com
  2. Select your SoM
  3. Select the software release
DART-MX8M GPIO

GPIO state

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

# cat /sys/kernel/debug/gpio
gpiochip0: GPIOs 0-31, parent: platform/30200000.gpio, 30200000.gpio:
 gpio-8   (                    |eth_phy_pwr         ) out hi    
 gpio-9   (                    |phy-reset           ) out hi    
 gpio-10  (                    |connect             ) in  hi IRQ

gpiochip1: GPIOs 32-63, parent: platform/30210000.gpio, 30210000.gpio:
 gpio-44  (                    |cd                  ) in  lo IRQ
 gpio-51  (                    |VSD_3V3             ) out hi    

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

gpiochip3: GPIOs 96-127, parent: platform/30230000.gpio, 30230000.gpio:
 gpio-102 (                    |Back                ) in  hi IRQ
 gpio-109 (                    |Home                ) in  hi IRQ
 gpio-111 (                    |Down                ) in  hi IRQ
 gpio-113 (                    |?                   ) out lo    
 gpio-114 (                    |Up                  ) in  hi IRQ

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

gpio-44  (                    |cd                  ) in  lo IRQ

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

gpio-44  (                    |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.


Manipulating a single GPIO via /sys/class/gpio

Note: The /sys/class/gpio ABI is being deprecated in the Linux kernel and is being replaced by the GPIO character device and libgpiod. Please refer to the kernel Documentation for the latest information.

Using a command line or a script

GPIOs in i.MX are grouped in groups of 32 pins.
For example, GPIO1_3 belong to the first group, pin 3. Its absolute number will be 3.
GPIO4_21 will be (4-1)*32+21=117.
Assuming this GPIO is defined in your device tree, the following is an example of how to use it from userspace.

To export the GPIO for userspace use:

$ echo 117 > /sys/class/gpio/export


To configure as output:

$ echo out > /sys/class/gpio/gpio117/direction

Set GPIO high:

$ echo 1 > /sys/class/gpio/gpio117/value

Set GPIO low:

$ echo 0 > /sys/class/gpio/gpio117/value


To configure as input:

$ echo in > /sys/class/gpio/gpio117/direction

Read the current value:

$ cat /sys/class/gpio/gpio117/value


To free the GPIO after you're done using it:

$ echo 117 > /sys/class/gpio/unexport

Using a C application

All of the command line operations above can be translated to C code:
Reserve (export) the GPIO:

#define IMX_GPIO_NR(port, index)    ((((port)-1)*32)+((index)&31))

int fd;
char buf[MAX_BUF]; 
int gpio = IMX_GPIO_NR(4, 21); /* Just an example */

fd = open("/sys/class/gpio/export", O_WRONLY);

sprintf(buf, "%d", gpio); 

write(fd, buf, strlen(buf));

close(fd);

Set the GPIO direction:

sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);

fd = open(buf, O_WRONLY);

/* Set out direction */
write(fd, "out", 3); 
/* Set in direction */
write(fd, "in", 2); 

close(fd);

In case of out direction set the GPIO value:

sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

fd = open(buf, O_WRONLY);

/* Set GPIO high status */
write(fd, "1", 1); 
/* Set GPIO low status */
write(fd, "0", 1); 

close(fd);

In case of in direction get the current GPIO value:

char value;

sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);

fd = open(buf, O_RDONLY);

read(fd, &value, 1);

if (value == '0') { 
     /* Current GPIO status low */
} else {
     /* Current GPIO status high */
}

close(fd);

Once finished, free (unexport) the GPIO:

fd = open("/sys/class/gpio/unexport", O_WRONLY);

sprintf(buf, "%d", gpio);

write(fd, buf, strlen(buf));

close(fd);

Important notes:

  • Remember that after the first read operation the file pointer will move to the next position in the file, so to get a correct value for each read operation you simply have to set the file pointer at the beginning of the file before read by using the following command:
lseek(fd, 0, SEEK_SET);
  • This is only a short example. If you want to use it in your code remember add error handling to it.

Kernel Device Tree GPIO configuration

Device Tree GPIO files

Pin Func files

In the directory arch/arm64/boot/dts/freescale of the Linux kernel source you will find the pin functions definition files.
The relevant file is imx8mq-pinfunc.h.
If you search it for GPIO4_IO2, for example, you will see a group of definitions with same prefix (pad name), "MX8MQ_IOMUXC_SAI1_RXD0".

#define MX8MQ_IOMUXC_SAI1_RXD0_SAI1_RX_DATA0                                0x164 0x3CC 0x000 0x0 0x0
#define MX8MQ_IOMUXC_SAI1_RXD0_SAI5_RX_DATA0                                0x164 0x3CC 0x4D4 0x1 0x1
#define MX8MQ_IOMUXC_SAI1_RXD0_CORESIGHT_TRACE0                             0x164 0x3CC 0x000 0x4 0x0
#define MX8MQ_IOMUXC_SAI1_RXD0_GPIO4_IO2                                    0x164 0x3CC 0x000 0x5 0x0
#define MX8MQ_IOMUXC_SAI1_RXD0_CCMSRCGPCMIX_BOOT_CFG0                       0x164 0x3CC 0x000 0x6 0x0
#define MX8MQ_IOMUXC_SAI1_RXD0_SIM_M_HADDR17                                0x164 0x3CC 0x000 0x7 0x0

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.
Edit arch/arm64/boot/dts/freescale/fsl-imx8mq-var-dart-common.dtsi and add the definition for the GPIO you need in the iomuxc node.

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

	imx8m-var-dart {
		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-imx8mq-var-dart-common.dtsi 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.