
Guide to Using GPIO on the Forlinx OKMX93xx Platform with Linux 6.1.36
It introduces how to use the GPIO on the OKMX93xx series development boards, mainly covering the use of native GPIO and the extended GPIO solution. Operate accordingly.
Scope of Application
Mainly applicable to the Linux 6.1.36 operating system on the Forlinx OKMX93xx series platforms. Other platforms can refer, but differences may exist and self - modifications are required to suit usage.
Note: Please refer to and confirm the device tree you are using.
Using Native GPIO
1.1 Reusing GPIO
(1) Currently, SD2_DATA01 is used as GPIO;
(2) First, search in OKMX93-linux-kernel/arch/arm64/boot/dts/freescale/imx93-pinfunc.h
The specific parameters here will not be described. Refer to the corresponding registers in the imx93RM_RevD_alpha_customer.pdf manual;
(3) Add the following reused GPIO pins to the iomux section in the device tree:
Device tree:
OKMX93-linux-sdk/OKMX93-linux-kernel/arch/arm64/boot/dts/freescale/OK-MX93-C.dts
(4) Add the pin multiplexing configuration for pinctrl_gpio1.
Meanwhile, search for the pin name MX93_PAD_SD2_DATA1 in the device tree to see if it is reused for other functions elsewhere.
It can be seen that this pin is reused for the USDHC2 function. Search for pinctrl_usdhc2 and disable the USDHC2 function.
(5) Compile the device tree. Replace and update the device tree.
1.2 Operating GPIO
1.2.1 Using the gpiod Tool
(1) View all GPIO devices
root@ok-mx93:~# gpiodetect gpiochip0 [43810080.gpio] (32 lines) gpiochip1 [43820080.gpio] (32 lines) gpiochip2 [43830080.gpio] (32 lines) gpiochip3 [47400080.gpio] (32 lines)
(2) From the pin configuration in the device tree, the corresponding GPIO is gpio@43820080 in the device tree, which is gpiochip1.
The specific correspondence is as follows:
(3) Use the following command to view which GPIOs are occupied in the current system.
root@ok-mx93:~# gpioinfo 1 gpiochip1 - 32 lines: line 0: unnamed unused input active-high line 1: unnamed unused input active-high line 2: unnamed unused input active-high line 3: unnamed unused input active-high line 4: unnamed unused output active-high line 5: unnamed unused input active-high line 6: unnamed unused input active-high line 7: unnamed "regulator-usdhc2" output active-high [used] line 8: unnamed unused input active-high line 9: unnamed unused input active-high line 10: unnamed unused input active-high line 11: unnamed unused input active-high line 12: unnamed unused input active-high line 13: unnamed unused input active-high line 14: unnamed unused input active-high line 15: unnamed unused input active-high line 16: unnamed unused input active-high line 17: unnamed unused input active-high line 18: unnamed unused input active-high line 19: unnamed unused input active-high line 20: unnamed unused input active-high line 21: unnamed unused input active-high line 22: unnamed unused input active-high line 23: unnamed unused input active-high line 24: unnamed unused output active-high line 25: unnamed unused output active-high line 26: unnamed unused input active-high line 27: unnamed "key1" input active-low [used] line 28: unnamed unused input active-high line 29: unnamed unused input active-high line 30: unnamed unused input active-high line 31: unnamed unused input active-high
(4) Output Mode
Use the following command to set the GPIO to high level. Measure with a multimeter. If it shows a high level, it indicates that the GPIO test is successful.
root@ok-mx93:~# gpioset gpiochip1 4=1
Use the following command to set the GPIO to low level. Measure with a multimeter. If it shows a low level, it indicates that the GPIO test is successful.
root@ok-mx93:~# gpioset gpiochip1 4=0
By using gpioinfo, it can be found that line 4 is in the output state.
root@ok-mx93:~# gpioinfo 1 gpiochip1 - 32 lines: ........ line 4: unnamed unused output active-high .......
(5) Input Mode
Use the following commands to set the GPIO to input mode and read the current GPIO level status.
root@ok-mx93:~# gpioget gpiochip1 4 0
Through gpioinfo, see that line4 is in the input state.
root@ok-mx93:~# gpioinfo 1 gpiochip1 - 32 lines: ........ line 4: unnamed unused input active-high .......
(6) Interrupt mode
Use the following command to set up GPIO interrupt monitoring
root@ok-mx93:~# gpiomon gpiochip1 4
Pin ground
root@ok-mx93:~# event: FALLING EDGE offset: 4 timestamp: [ 1506.329421425]
Disconnect the ground
root@ok-mx93:~# event: RISING EDGE offset: 4 timestamp: [ 1506.329434716]
Through gpioinfo, see that line4 is in the input state.
root@ok-mx93:~# gpioinfo 1 gpiochip1 - 32 lines: ........ line 4: unnamed unused input active-high .......
1.2.2 Using C Language Routines
Currently, the imx93 uses the Linux 5.15.52 kernel system. The previous sysfs GPIO interface (/sys/class/gpio) for operating GPIO has been deprecated, and its replacement is the GPIO character device API Libgpiod.
The following takes the operation of the button (K1) and the LED (D6) on the development board as an example for illustration.
(1) Modify the Device Tree
Path: OKMX93-linux-sdk/OKMX93-linux-kernel/arch/arm64/boot/dts/freescale/OK-MX93-C.dts
First, mask the default led-1 and keys nodes to avoid conflicts. The format is as follows:
Add the gpio_key node below:
Modify pinctrl_gpio_key and add the multiplexing of the LED pin:
The pin used for the button is: MX93_PAD_CCM_CLKO2__GPIO3_IO27
The pin used for the LED is: MX93_PAD_CCM_CLKO3__GPIO4_IO28
Save the changes and exit, then recompile the device tree.
After compilation is complete, reprogram the OK-MX93-C.dtb.
(2) Compile the Test Source Code
Copy gpiotest.c, gpio-toggle.c, and lib.tar.bz2 from the Libgpiod test source code directory to the development environment.
Extract lib.tar.bz2 to the current directory. The gpiod.h file and the libgpiod library files inside will be used during compilation.
Example 1: Control the LED to turn on and off in a loop with a 1-second interval
Cross-compile gpio-toggle.c
(1) Set the environment variables (note that there is a space after the dot);
(2) Perform cross-compilation;
(3) Copy the executable file gpio-toggle to the development board;
(4) Run the program, and see the LED (D6) turn on for 1 second and then turn off for 1 second.
The input parameters 2 and 28 represent: gpiochip2 line28
Example 2: Control the LED to turn on and off using the button. The state flips each time the button is pressed.
Cross-compile gpio-test.c
(1) Set the environment variables (note that there is a space after the dot);
(2) Perform cross-compilation;
(3) Copy the executable file gpio-test to the development board;
(4) Run the program, and you will see the state of the LED flip each time the button is pressed.
The input parameters 1 and 27 represent: gpiochip1 line27.
The input parameters 2 and 28 represent: gpiochip2 line28.
Extended GPIO Solution (PCAL6524)
The PCAL6524 is a 24-bit general-purpose I/O expander with an I2C interface. For detailed descriptions, please refer to the ''NXP1 - PCAL6524HE.pdf''. By default, the PCAL6524 is not soldered on the development board. If implementing this solution, you need to solder this module on the hardware. In the following test, the PCAL6524 is connected to I2C3, and the interrupt pin uses GPIO_IO1 (if you choose other pins as the interrupt, corresponding adjustments are required). The configuration and testing methods are as follows: Path: OKMX93-linux-sdk/OKMX93-linux-kernel/arch/arm64/boot/dts/freescale/OK-MX93-C.dts
1. Add the pcal6524 node under i2c3:
Add pinctrl_pcal6524 under the &iomuxc node:
2. Mask the previous pin multiplexing to avoid reference conflicts:
3. Compile the device tree. Replace the device tree and reprogram it. 5.Use the gpiodetect command to view the GPIO devices.
See that there is an additional gpiochip4 with 24 lines. For the testing method, refer to the ''01_Reusing GPIO'' section.