I'm using the Rust gpio_cdev library. This uses the GPIO character device, rather than the deprecated sysfs.

With the character device, you need to tell it the proper gpio chip to use.

My code might be running on a Raspberry Pi 2, 3, 4, or potentially 5 and 6 one day.

The docs say that instead of using a hardcoded value like /dev/gpiochip0, you should use their chips function that returns a list of chips, and look for the one you need.

What's the best thing to look for when searching for the right chip?

  • The chip with the most lines?
  • The chip with a specific name?
  • The chip with a specific number of lines?
  • Something else?

For all Raspberry Pis I would assume the expansion header GPIO are always on /dev/gpiochip0.

It would be an extremely perverse decision to place them on any other gpiochip.

For reference the Pi4B has two gpiochips.


lines=58 name=gpiochip0 label=pinctrl-bcm2711
offset=0 flags=0 name=ID_SDA user=
offset=1 flags=0 name=ID_SCL user=
offset=2 flags=0 name=SDA1 user=
offset=3 flags=0 name=SCL1 user=
offset=4 flags=0 name=GPIO_GCLK user=
offset=5 flags=0 name=GPIO5 user=
offset=6 flags=0 name=GPIO6 user=
offset=7 flags=0 name=SPI_CE1_N user=
offset=8 flags=0 name=SPI_CE0_N user=
offset=9 flags=0 name=SPI_MISO user=
offset=10 flags=0 name=SPI_MOSI user=
offset=11 flags=0 name=SPI_SCLK user=
offset=12 flags=0 name=GPIO12 user=
offset=13 flags=0 name=GPIO13 user=
offset=14 flags=0 name=TXD1 user=
offset=15 flags=0 name=RXD1 user=
offset=16 flags=0 name=GPIO16 user=
offset=17 flags=0 name=GPIO17 user=
offset=18 flags=0 name=GPIO18 user=
offset=19 flags=0 name=GPIO19 user=
offset=20 flags=0 name=GPIO20 user=
offset=21 flags=0 name=GPIO21 user=
offset=22 flags=0 name=GPIO22 user=
offset=23 flags=0 name=GPIO23 user=
offset=24 flags=0 name=GPIO24 user=
offset=25 flags=0 name=GPIO25 user=
offset=26 flags=0 name=GPIO26 user=
offset=27 flags=0 name=GPIO27 user=
offset=28 flags=0 name=RGMII_MDIO user=
offset=29 flags=0 name=RGMIO_MDC user=
offset=30 flags=0 name=CTS0 user=
offset=31 flags=0 name=RTS0 user=
offset=32 flags=0 name=TXD0 user=
offset=33 flags=0 name=RXD0 user=
offset=34 flags=0 name=SD1_CLK user=
offset=35 flags=0 name=SD1_CMD user=
offset=36 flags=0 name=SD1_DATA0 user=
offset=37 flags=0 name=SD1_DATA1 user=
offset=38 flags=0 name=SD1_DATA2 user=
offset=39 flags=0 name=SD1_DATA3 user=
offset=40 flags=0 name=PWM0_MISO user=
offset=41 flags=0 name=PWM1_MOSI user=
offset=42 flags=3 name=STATUS_LED_G_CLK user=led0
offset=43 flags=0 name=SPIFLASH_CE_N user=
offset=44 flags=7 name=SDA0 user=spi0 CS0
offset=45 flags=7 name=SCL0 user=spi0 CS1
offset=46 flags=0 name=RGMII_RXCLK user=
offset=47 flags=0 name=RGMII_RXCTL user=
offset=48 flags=0 name=RGMII_RXD0 user=
offset=49 flags=0 name=RGMII_RXD1 user=
offset=50 flags=0 name=RGMII_RXD2 user=
offset=51 flags=0 name=RGMII_RXD3 user=
offset=52 flags=0 name=RGMII_TXCLK user=
offset=53 flags=0 name=RGMII_TXCTL user=
offset=54 flags=0 name=RGMII_TXD0 user=
offset=55 flags=0 name=RGMII_TXD1 user=
offset=56 flags=0 name=RGMII_TXD2 user=
offset=57 flags=0 name=RGMII_TXD3 user=


lines=8 name=gpiochip1 label=raspberrypi-exp-gpio
offset=0 flags=2 name=BT_ON user=
offset=1 flags=2 name=WL_ON user=
offset=2 flags=7 name=PWR_LED_OFF user=led1
offset=3 flags=2 name=GLOBAL_RESET user=
offset=4 flags=3 name=VDD_SD_IO_SEL user=vdd-sd-io
offset=5 flags=2 name=CAM_GPIO user=
offset=6 flags=3 name=SD_PWR_ON user=sd_vcc_reg
offset=7 flags=0 name=SD_OC_N user=

My laptop has four gpiochips. I only show the lines field as the name and user fields are blank.

lines=80 name=gpiochip0 label=INT3453:00
lines=80 name=gpiochip1 label=INT3453:01
lines=20 name=gpiochip2 label=INT3453:02
lines=35 name=gpiochip3 label=INT3453:03
  • From the sound of it, the system numbers the chips on boot, and the fact that the Pi's main GPIO chip is /dev/gpiochip0 is a coincidence, but one that can be more-or-less relied upon always being the case. There's no better way to ensure we're always dealing with the right chip? Doesn't seem like it would be particularly hard to code. Just a function that finds the right chip. Just need to know what to look for that would match on different Pi hardware. – John Feb 26 at 16:21

gpiodetect – list all gpiochips present on the system, their names, labels and number of GPIO lines

gpiochip0 [pinctrl-bcm2711] (58 lines)
gpiochip1 [raspberrypi-exp-gpio] (8 lines)
  • I'm aware of this tool. Looking at that exact output, I can tell that gpiochip0 is probably the right one because the only other option is a chip with only 8 lines, and the Pi has more than 8 gpio pins. probably the right one isn't great when you're telling a computer how to find something. Is there a smart way to tell it exactly which chip I'm looking for? I could say "look for the chip with 58 lines", but will all Pis have a chip with exactly 58 lines? Is the gpiochip1 some internal chip? Or are some of the GPIO pins on the main header attached to gpiochip1? – John Feb 27 at 18:04
  • I would have thought bcm2711 was a dead giveaway. Hardly worth worrying about as a new GPIO implementation is unlikely, even if the CPU is updated. The previous implementation was unchanged for 8 years. – Milliways Feb 27 at 20:42
  • So it's safe to just look for the chip named bcm2711 then? Or the chip with the string bcm in the name? – John Feb 28 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.