devm_gpiod_get_optional(9)
π devm_gpiod_get_optional β the kernel API manual, but make it make sense
okay so bestie, welcome to the devm_gpiod_get_optional(9) page, from the Kernel API Manual β¨ this one is literally about grabbing a GPIO descriptor in a resource-managed way β and the _optional part is doing SO much work, iykyk π
β¨ NAME β¨
devm_gpiod_get_optional β it's the resource-managed glow-up of gpiod_get_optional(). that's it, that's the whole name, slay π«Ά
πΈ SYNOPSIS πΈ
struct gpio_desc * devm_gpiod_get_optional (struct device *dev , const
char *con_id , enum gpiod_flags flags );
like, look at that signature. it's giving "i return a pointer to a struct gpio_desc" and honestly? iconic π
π¦ ARGUMENTS π¦
devβ the GPIO consumer. it's the device that's gonna be consuming the GPIO, bestie, don't overthink it πcon_idβ the function within the GPIO consumer. that's the name of the thing you're asking for, basically πΈflagsβ optional GPIO initialization flags. this one is literally optional-optional, it's just flags for init β¨
π DESCRIPTION π
so this is the managed gpiod_get_optional. and when we say managed, we mean: GPIO descriptors returned from this function are automatically disposed on driver detach β no cap, the kernel cleans up after you so you don't have to ππ«Ά that's the main character energy of devm_.
for the detailed tea on behavior and return values, go read gpiod_get_optional β this one is basically the same vibe, just resource-managed. periodt π
β RETURN β
here's what you get back (vibe check them all, they matter):
- the GPIO descriptor corresponding to the function
con_idof devicedevβ the happy path, we love her π NULLif no GPIO has been assigned to the requested function β not an error btw, that's the_optionalbit doing its thing π¦- or another
IS_ERRcode if an error occurred while trying to acquire the GPIO β that's your "something actually went wrong" signal π
ngl bestie, always check for IS_ERR before you start using the pointer. just because it's managed doesn't mean it's guaranteed π
π SEE ALSO π
the whole friend group, all of them, in order, no one left out π
- Kernel file
./drivers/gpio/gpiolib-devres.c devm_gpiod_get(9)devm_gpiod_get_index(9)devm_fwnode_gpiod_get_index(9)devm_gpiod_get_index_optional(9)devm_gpiod_get_array(9)devm_gpiod_get_array_optional(9)devm_gpiod_put(9)devm_gpiod_unhinge(9)devm_gpiod_put_array(9)devm_gpiochip_add_data_with_key(9)
and that's the page! π§ it lives over at ./drivers/gpio, dated October 2026, and it's devm_gpiod_get_optional(9) β go forth and acquire those GPIOs responsibly, bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms