GPIO customization on Jetson Platform

This is the example for users to find the relationship between GPIO PINs and GPIO num.
The followed information captured from Jetson NX.

From Pinmux to find the right ball name and customer usage.
eg: GPIO00

GPIO00 87 USB_VBUS_EN0 GPIO3_PZ.01 USB_VBUS_EN0 wake61 100k 0 GPIO3_PZ.01 Bidirectional Int PU Yes Disable Disable GPIO0 (USB_VBUS_EN0) Baseboard USB VBUS Detect

From devicetree:

gpios = <&tegra_main_gpio TEGRA194_MAIN_GPIO(Z, 1) GPIO_ACTIVE_LOW>;

GPIO07 in PINMUX:

GPIO07 206 SOC_GPIO44 GPIO3_PR.00 VGP5 GP_PWM8 100k pd GPIO3_PR.00 Input Int PD Disable Disable GPIO07 (PWM) Baseboard 40 Pin GPIO

From devicetree:

gpios = <&tegra_main_gpio TEGRA194_MAIN_GPIO(R, 0) GPIO_ACTIVE_LOW>;

$ grep PORT_R  ../../hardware/nvidia/soc/t19x/kernel-include/dt-bindings/gpio/tegra194-gpio.h 
#define TEGRA194_MAIN_GPIO_PORT_R 17
$ grep PORT_Z  ../../hardware/nvidia/soc/t19x/kernel-include/dt-bindings/gpio/tegra194-gpio.h 
#define TEGRA194_MAIN_GPIO_PORT_Z 25

Get the GPIO number from its port offset.

GPIO3_PR.00: (17 x 8 + 0) = 136
GPIO3_PZ.01: (25 x 8 + 1) = 200

GPIO Chip offset:

nx@nx-desktop:~$ dmesg | grep GPIOs
[    0.754758] gpiochip_setup_dev: registered GPIOs 288 to 511 on device: gpiochip0 (tegra-gpio)
[    0.761503] gpiochip_setup_dev: registered GPIOs 248 to 287 on device: gpiochip1 (tegra-gpio-aon)
[    0.896879] gpiochip_setup_dev: registered GPIOs 240 to 247 on device: gpiochip2 (max77620-gpio)

GPIOchip0 offset from 288.

GPIO07 number in software == 136 + 288 = 424
GPIO00 number in software == 200 + 288 = 488

You can check with debug/gpio.

nx@nx-desktop:~$ sudo cat /sys/kernel/debug/gpio
[sudo] password for nx: 
gpiochip2: GPIOs 240-247, parent: platform/max77620-gpio, max77620-gpio, can sleep:
 gpio-246 (                    |gpio_default        ) out hi    
 gpio-247 (                    |gpio_default        ) out hi    

gpiochip1: GPIOs 248-287, parent: platform/c2f0000.gpio, tegra-gpio-aon:
 gpio-248 (                    )
 gpio-249 (                    )
 gpio-250 (                    )
 gpio-251 (                    )
 gpio-252 (                    )
 gpio-253 (                    |pex-refclk-sel-low  ) out lo    
 gpio-254 (                    )
 gpio-255 (                    )
 gpio-256 (                    )
 gpio-257 (                    )
 gpio-258 (                    )
 gpio-259 (                    )
 gpio-260 (                    )
 gpio-261 (                    )
 gpio-262 (                    )
 gpio-263 (                    )
 gpio-264 (                    |w-disable2          ) out lo    
 gpio-265 (                    )
 gpio-266 (                    |w-disable1          ) out lo    
 gpio-267 (                    |i2c-mux-gpio        ) out hi    
 gpio-268 (GPIO12              )
 gpio-284 (                    |power-key           ) in  hi    
gpiochip0: GPIOs 288-511, parent: platform/2200000.gpio, tegra-gpio:
 gpio-288 (                    )
 gpio-289 (                    )
 gpio-290 (                    )
 gpio-291 (                    )
 gpio-292 (                    )
 gpio-293 (                    )
 gpio-294 (                    )
 gpio-295 (                    )
 gpio-296 (                    )
 gpio-297 (                    )
 gpio-298 (                    )
 gpio-299 (                    )
 gpio-300 (                    )
 gpio-301 (                    )
 gpio-302 (                    )
 gpio-303 (                    )
 gpio-304 (                    )
 gpio-305 (                    )
 gpio-306 (                    )
 gpio-307 (                    )
 gpio-308 (                    )
 gpio-309 (                    )
 gpio-310 (                    )
 gpio-311 (                    )
 gpio-312 (                    )
 gpio-313 (                    )
 gpio-314 (                    )
 gpio-315 (                    )
 gpio-316 (                    )
 gpio-317 (                    )
 gpio-318 (                    )
 gpio-319 (                    )
 gpio-320 (                    )
 gpio-321 (                    )
 gpio-322 (                    )
 gpio-323 (                    )
 gpio-324 (                    )
 gpio-325 (                    )
 gpio-326 (                    )
 gpio-327 (                    )
 gpio-328 (                    )
 gpio-329 (                    )
 gpio-330 (                    )
 gpio-331 (                    )
 gpio-332 (                    )
 gpio-333 (                    )
 gpio-334 (                    )
 gpio-335 (                    )
 gpio-336 (                    |force-recovery      ) in  hi    
 gpio-337 (                    )
 gpio-338 (                    |vdd-sdmmc1-sw       ) out hi    
 gpio-339 (                    |wifi-enable         ) out hi    
 gpio-340 (                    )
 gpio-341 (                    )
 gpio-342 (                    )
 gpio-343 (                    |cd                  ) in  lo    
 gpio-344 (                    )
 gpio-345 (                    )
 gpio-346 (                    )
 gpio-347 (                    )
 gpio-348 (                    )
 gpio-349 (                    )
 gpio-350 (                    )
 gpio-351 (                    )
 gpio-352 (                    )
 gpio-353 (                    )
 gpio-354 (                    )
 gpio-355 (                    )
 gpio-356 (                    )
 gpio-357 (                    )
 gpio-358 (                    )
 gpio-359 (                    )
 gpio-360 (                    )
 gpio-361 (                    )
 gpio-362 (                    )
 gpio-363 (                    )
 gpio-364 (                    )
 gpio-365 (                    )
 gpio-366 (                    )
 gpio-367 (                    )
 gpio-368 (                    )
 gpio-369 (                    )
 gpio-370 (                    )
 gpio-371 (                    )
 gpio-372 (                    )
 gpio-373 (                    )
 gpio-374 (                    )
 gpio-375 (                    )
 gpio-376 (                    )
 gpio-377 (                    )
 gpio-378 (                    |pcie_wake           ) in  hi    
 gpio-379 (                    )
 gpio-380 (                    )
 gpio-381 (                    )
 gpio-382 (                    )
 gpio-383 (                    )
 gpio-384 (                    )
 gpio-385 (                    |hdmi2.0_hpd         ) in  lo    
 gpio-386 (                    )
 gpio-387 (                    )
 gpio-388 (                    )
 gpio-389 (                    )
 gpio-390 (                    )
 gpio-391 (                    )
 gpio-392 (                    |avdd-cam-2v8        ) out lo    
 gpio-393 (GPIO13              )
 gpio-394 (                    )
 gpio-395 (                    )
 gpio-396 (                    )
 gpio-397 (                    )
 gpio-398 (                    )
 gpio-399 (                    )
 gpio-400 (                    )
 gpio-401 (                    )
 gpio-402 (                    )
 gpio-403 (                    )
 gpio-404 (                    )
 gpio-405 (                    )
 gpio-406 (                    )
 gpio-407 (                    )
 gpio-408 (                    )
 gpio-409 (                    )
 gpio-410 (                    )
 gpio-411 (                    )
 gpio-412 (                    |cam_reset_gpio      ) out lo    
 gpio-413 (                    |cam_reset_gpio      ) out lo    
 gpio-414 (                    )
 gpio-415 (                    )
 gpio-416 (                    )
 gpio-417 (                    )
 gpio-418 (                    )
 gpio-419 (                    )
 gpio-420 (                    )
 gpio-421 (GPIO01              )
 gpio-422 (GPIO11              )
 gpio-423 (                    )
 gpio-424 (GPIO07              )
 gpio-425 (                    |eqos_phy_reset      ) out hi    
 gpio-426 (                    )
 gpio-427 (                    )
 gpio-428 (UART1_RTS           )
 gpio-429 (UART1_CTS           )
 gpio-430 (                    )
 gpio-431 (                    )
 gpio-432 (                    )
 gpio-433 (                    )
 gpio-434 (                    )
 gpio-435 (                    )
 gpio-436 (GPIO09              )
 gpio-437 (                    )
 gpio-438 (                    )
 gpio-439 (                    )
 gpio-440 (                    )
 gpio-441 (                    )
 gpio-442 (                    )
 gpio-443 (                    )
 gpio-444 (                    )
 gpio-445 (I2S0_SCLK           )
 gpio-446 (I2S0_DOUT           )
 gpio-447 (I2S0_DIN            )
 gpio-448 (I2S0_FS             )
 gpio-449 (                    )
 gpio-450 (                    )
 gpio-451 (                    )
 gpio-452 (                    )
 gpio-453 (                    )
 gpio-454 (                    )
 gpio-455 (                    )
 gpio-456 (                    )
 gpio-457 (                    )
 gpio-458 (                    )
 gpio-459 (                    )
 gpio-460 (                    )
 gpio-461 (                    )
 gpio-462 (                    )
 gpio-463 (                    )
 gpio-464 (                    )
 gpio-465 (                    )
 gpio-466 (                    )
 gpio-467 (                    )
 gpio-468 (                    )
 gpio-469 (                    )
 gpio-470 (                    )
 gpio-471 (                    )
 gpio-472 (                    )
 gpio-473 (                    )
 gpio-474 (                    )
 gpio-475 (                    )
 gpio-476 (                    )
 gpio-477 (                    )
 gpio-478 (                    )
 gpio-479 (                    )
 gpio-480 (SPI1_SCK            )
 gpio-481 (SPI1_MISO           )
 gpio-482 (SPI1_MOSI           )
 gpio-483 (SPI1_CS0            )
 gpio-484 (SPI1_CS1            )
 gpio-485 (                    )
 gpio-486 (                    )
 gpio-487 (                    )
 gpio-488 (                    )
 gpio-489 (                    |external-connection:) in  hi     GPIO00 PZ01
 gpio-490 (                    )					PZ02
 gpio-491 (SPI0_SCK            )					PZ03
 gpio-492 (SPI0_MISO           )
 gpio-493 (SPI0_MOSI           )
 gpio-494 (SPI0_CS0            )
 gpio-495 (SPI0_CS1            )
 gpio-496 (                    )
 gpio-497 (                    )
 gpio-498 (                    )
 gpio-499 (                    )
 gpio-500 (                    )
 gpio-501 (                    )
 gpio-502 (                    )
 gpio-503 (                    )
 gpio-504 (                    )
 gpio-505 (                    )
 gpio-506 (                    )
 gpio-507 (                    )
 gpio-508 (                    )
 gpio-509 (                    )
 gpio-510 (                    )
 gpio-511 (                    )

You can also check with the tegra_gpio to see configuration.

nx@nx-desktop:~$ sudo cat /sys/kernel/debug/tegra_gpio
Port:Pin:ENB DBC IN OUT_CTRL OUT_VAL INT_CLR
A:0 0x0 0x0 0x0 0x1 0x0 0x0
A:1 0x0 0x0 0x0 0x1 0x0 0x0
A:2 0x0 0x0 0x0 0x1 0x0 0x0
A:3 0x0 0x0 0x0 0x1 0x0 0x0
A:4 0x0 0x0 0x0 0x1 0x0 0x0
A:5 0x0 0x0 0x0 0x1 0x0 0x0
A:6 0x0 0x0 0x0 0x1 0x0 0x0
A:7 0x0 0x0 0x0 0x1 0x0 0x0
B:0 0x3 0x0 0x0 0x0 0x1 0x0
B:1 0x0 0x0 0x0 0x1 0x0 0x0
C:0 0x0 0x0 0x0 0x1 0x0 0x0
C:1 0x0 0x0 0x0 0x1 0x0 0x0
C:2 0x0 0x0 0x0 0x1 0x0 0x0
C:3 0x0 0x0 0x0 0x1 0x0 0x0
C:4 0x0 0x0 0x0 0x1 0x0 0x0
C:5 0x0 0x0 0x0 0x1 0x0 0x0
C:6 0x0 0x0 0x0 0x1 0x0 0x0
C:7 0x0 0x0 0x0 0x1 0x0 0x0
D:0 0x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x0 0x1 0x0 0x0
FF:1 0x0 0x0 0x0 0x1 0x0 0x0
GG:0 0x0 0x0 0x0 0x1 0x0 0x0
GG:1 0x0 0x0 0x0 0x1 0x0 0x0

Linux_for_Tegra/source/public/kernel/kernel-4.9/include/linux/gpio.h

The APIS used to control the GPIO.

gpio_request(num, “name”);
gpio_direction_output(num,GPIOF_DIR_OUT);
gpio_set_value(num,0);
gpio_set_value(num,1);

Useful guide:

40 PIN Header: https://docs.nvidia.com/jetson/l4t/index.html#page/Tegra%20Linux%20Driver%20Package%20Development%20Guide%2Fhw_setup_jetson_io.html
Tegra GPIO:
https://github.com/NVIDIA/jetson-gpio
Platform Adaptation and Bring-Up;
https://docs.nvidia.com/jetson/l4t/index.html#page/Tegra%20Linux%20Driver%20Package%20Development%20Guide%2Fadaptation_and_bringup_xavier_nx.html