How to use GPIOs

There’s any documentation about GPIO usage on Apalis iMX8?

Toradex brazilian support informed by email that the math needed to acess gpios using sysfs on linux is:

480 - 32 * (controller - 1) + gpio

So i made this small table where the MXM pinout can be converted to the linux equivalent number.

MXM - Contr - GPIO - Linux
1 - 0 - 8 - 520
3 - 0 - 9 - 521
5 - 0 - 12 - 524
7 - 0 - 13 - 525

Following tutorials from another toradex modules I tryed to:

root@apalis-imx8:~# echo 524 > /sys/class/gpio/export
-sh: echo: write error: Invalid argument

Also the brazilian office told me that maybe a device tree patch would be needed, but many errors ocurs when bitbake the modified image.

The bblayer file modification:

diff --git a/imx/meta-bsp/recipes-kernel/linux/linux-toradex_4.9.%.bbappend b/imx/meta-bsp/recipes-kernel/linux/linux-toradex_4.9.%.bbappend
index b95faa5af..a37eef7ee 100644
--- a/imx/meta-bsp/recipes-kernel/linux/linux-toradex_4.9.%.bbappend
+++ b/imx/meta-bsp/recipes-kernel/linux/linux-toradex_4.9.%.bbappend
@@ -4,7 +4,9 @@
 
 FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
 
-SRC_URI_append = " file://Install-dma-buf-h.patch"
+SRC_URI_append = "  file://Install-dma-buf-h.patch \
+                    file://0001-Mux-GPIOs-for-Apalis-iMX8.patch \
+"

The patch that they sent me:

diff --git a/arch/arm64/boot/dts/freescale/fsl-imx8qm-apalis.dts b/arch/arm64/boot/dts/freescale/fsl-imx8qm-apalis.dts
index 49df7ce716d4..0c74a124c5ca 100644
--- a/arch/arm64/boot/dts/freescale/fsl-imx8qm-apalis.dts
+++ b/arch/arm64/boot/dts/freescale/fsl-imx8qm-apalis.dts
@@ -472,6 +472,13 @@
             >;
         };
 
+        pinctrl_gpio_sysfs: gpio_sysfs {
+            fsl,pins = <
+                SC_P_M41_GPIO0_00_LSIO_GPIO0_IO12       0x00000021
+                SC_P_M41_GPIO0_01_LSIO_GPIO0_IO13       0x00000021
+            >;
+        };
+
         pinctrl_usb3503a: usb3503agrp {
             fsl,pins = <
                 SC_P_SCU_GPIO0_03_LSIO_GPIO0_IO31               0x00000021
@@ -490,6 +497,12 @@
     };
 };
 
+&iomuxc {
+    pinctrl-names = "default";
+    pinctrl-0 = <&pinctrl_gpio_sysfs>;
+    status = "okay";
+};
+
 &gpio2 {
     status = "okay";
 };

But that didnt worked.

HI @Rodrigo

Have you done any other changes to the Software then the ones you already shared?

Could you share the dmesg log and the output of cat /sys/kernel/debug/gpio?

Thanks and best regards,
Jaski

Hi!

Here it is:

root@apalis-imx8:~# cat /sys/kernel/debug/gpio gpiochip7: GPIOs 256-287, parent: platform/5d0f0000.gpio, 5d0f0000.gpio:

gpiochip6: GPIOs 288-319, parent: platform/5d0e0000.gpio, 5d0e0000.gpio:

gpiochip5: GPIOs 320-351, parent: platform/5d0d0000.gpio, 5d0d0000.gpio: gpio-320 ( |PCIe reset ) out hi

gpiochip4: GPIOs 352-383, parent: platform/5d0c0000.gpio, 5d0c0000.gpio: gpio-356 ( |usb_host_vbus ) out hi gpio-379 ( |ref-clock ) out hi

gpiochip3: GPIOs 384-415, parent: platform/5d0b0000.gpio, 5d0b0000.gpio: gpio-412 ( |GPIO fan control ) out hi

gpiochip2: GPIOs 416-447, parent: platform/5d0a0000.gpio, 5d0a0000.gpio: gpio-425 ( |cd ) in lo IRQ

gpiochip1: GPIOs 448-479, parent: platform/5d090000.gpio, 5d090000.gpio: gpio-449 ( |usb3503 intn ) out lo gpio-450 ( |usb3503 reset ) out hi gpio-452 ( |backlight ) out hi gpio-478 ( |HDMI_CTRL ) out hi

gpiochip0: GPIOs 480-511, parent: platform/5d080000.gpio, 5d080000.gpio: gpio-511 ( |usb3503 connect ) out hi

Hi

Have you tried to calculate without the ‘-1’?

480 - 32 * controller + gpio

MXM - Contr - GPIO - Linux
1 - 0 - 8 - 488
3 - 0 - 9 - 489
5 - 0 - 12 - 492
7 - 0 - 13 - 493

Max

Hi Max!

Without the ‘-1’ I can export, and echo ‘out’ to direction and echo ‘0’ and ‘1’ to value, but there’s no electrical diference on all 4 gpios. All are 0 volts.

Thanks

Hi @Rodrigo

Could you check if the Pins are defined as pull up or pull down?

Hi @jaski.tx

How can I check this?

hi @Rodrigo

I am sorry for the delayed answer. I did the test with toggling gpio488 (after exporting) using the bring up image and measuring the Pin 13 of Connector X27 Carrier Board Ixora and it works fine.

Hi @jaski.tx

I was testing the board with the bring up image that was sent on SD card and that didnt work. So I downloaded the bring up image that is posted on this forum and the GPIO 488 an 489 are working!

Now I’m trying to make other gpios work, for example on x27 pin 8 (mxm 221) its a default SPI pin, how can I use it as a regular GPIO?

Another example is x27 pin 33 (mxm 2), wich is a PWM, how could it be used as a gpio?

Perfect that the GPIOS are working. You need to mux these pins as gpio. You can look for an example here.

Hi @jaski.tx

On Apalis iMX8 Datasheet, 4.2 Pin control section the Sytem Controller API is mentioned. I didnt found any documentation or examples on that.

I don’t think the SCU is something you directly want to mess with. Unless, of course, I got your requirements completely wrong which would mandate you asking a new question about that.