GPIO Linux offset mapping

How can I get the gpio offset in linux ?

Thanks

The gpio number attributed to a gpio by Linux is not something really stable and can change across kernel versions (and sometime even across reboots). When you want to control a pin as a gpio you need to identify which gpio-controller (gpiochip) is in charge of that pin and what is the index/offset inside this gpio-controller. Most of the systems have only one gpio-controller part of the SoC, but some boards like the dragonboards have additional hardware(s) acting as gpio controller (e.g. the pmic).

The content of /sys/kernel/debug/gpio should give you the connection between of the gpio numbers and their gpiochip/offset.

Also from user-space I strongly suggest to use gpiolib get info or control a gpio:

To avoid looking at the schematics to find the right SoC gpio connected to the expension connectors, 96boards usually have their gpio ‘labelled’ so that you can easily find which gpio you need to use. For example on a dragonboard-410c:

$ gpiodetect
gpiochip0 [1000000.pinctrl] (122 lines)
gpiochip1 [200f000.spmi:pm8916@0:gpios@c00] (4 lines)
gpiochip2 [200f000.spmi:pm8916@0:mpps@a000] (4 lines)

$ gpioinfo
gpiochip0 - 122 lines:
line   0: "[UART0_TX]"       unused   input  active-high 
line   1: "[UART0_RX]"       unused   input  active-high 
line   2: "[UART0_CTS_N]" unused input active-high 
line   3: "[UART0_RTS_N]" unused input active-high 
line   4: "[UART1_TX]"       unused  output  active-high 
line   5: "[UART1_RX]"       unused  output  active-high 
line   6: "[I2C0_SDA]"       unused   input  active-high 
line   7: "[I2C0_SCL]"       unused   input  active-high 
line   8: "[SPI1_DOUT]" unused input active-high 
line   9: "[SPI1_DIN]"       unused   input  active-high 
line  10:  "[SPI1_CS]"       unused   input  active-high 
line  11: "[SPI1_SCLK]" unused input active-high 
line  12:     "GPIO-B"       unused   input  active-high 
line  13:     "GPIO-C"       unused   input  active-high 
    ...

So here you can set GPIO-B (Low speed connectot[1]) to high with

gpioset gpiochip0 12=1

or

gpioset 1000000.pinctrl 12=1

[1] 96Boards GPIO Pinout

1 Like

Hi thanks for the information.

I work with android shell, any other way ?

Thanks :grinning::grinning::grinning::grinning::grinning::grinning:

Either you build gpiolib for android, or you use the ‘legacy’ method via /sys/class/gpio: GPIO - eLinux.org

You can use cat /sys/kernel/debug/gpio to get the mapping.

Thanks! …
This is the solution…