GPIO KERNEL UBOOT DRAFT

From Variscite Wiki
Revision as of 05:18, 24 April 2023 by Alifer (talk | contribs) (Initial draft of the GPIO Kernel/Uboot wiki page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
VAR-SOM-MX8X GPIO

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/imx8qxp-var-som-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 IOMUXD register of the relevant pad.
Please consult the SOC reference manual for details about the relevant register.

U-Boot GPIO

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.

Installation

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

CONFIG_CMD_GPIO=y

Usage

=> 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.