gpiod_get_optional(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ¦‹ gpiod_get_optional β€” the optional-GPIO bestie we all needed

okay so this is a page out of the Kernel API Manual (section 9, kernel developer land, very ✨serious✨ business) and honestly it's short and sweet and it ate. Let's get into it πŸ’…


🌸 NAME

gpiod_get_optional β€” grab a GPIO for a given GPIO function, but like… only if there actually is one 🫢


⭐ SYNOPSIS

struct gpio_desc * gpiod_get_optional(struct device *dev, const char *con_id, enum gpiod_flags flags);

πŸŽ€ ARGUMENTS

  • dev β€” the GPIO consumer. and yes bestie, this one can be NULL, which is what you do for system-global GPIOs πŸ‘‘
  • con_id β€” the function within the GPIO consumer. it's giving "which one of my many functions is this pin for" energy
  • flags β€” optional GPIO initialization flags 🌈

πŸ’– DESCRIPTION

so this is literally gpiod_get but with ✨optional✨ energy. the whole vibe: when no GPIO was assigned to the requested function, it returns NULL instead of throwing a fit. no cap, that's it.

it's highkey convenient for drivers that need to handle optional GPIOs β€” like, the driver works with or without the pin, and this function doesn't make it a whole dramatic thing 🧚


πŸ”₯ RETURN

here's the vibe check on what you get back:

  • the GPIO descriptor corresponding to the function con_id of device dev πŸ’œ
  • NULL if no GPIO has been assigned to the requested function (that's the optional part, iconic)
  • or another IS_ERR code if an error occurred while trying to acquire the GPIO β€” so fr fr, check it properly 😭

🌈 SEE ALSO

and now, the moment you've all been waiting for β€” the entire friend group πŸ’€ the kernel file ./drivers/gpio/gpiolib.c and its absolutely enormous circle:

gpio_to_desc(9), gpio_device_get_desc(9), desc_to_gpio(9), gpiod_hwgpio(9), gpiod_to_chip(9), gpiod_to_gpio_device(9), gpio_device_get_base(9), gpio_device_get_label(9), gpio_device_get_chip(9), gpiochip_find_base_unlocked(9), gpiod_is_equal(9), gpiod_get_direction(9), gpiod_is_single_ended(9), gpiochip_query_valid_mask(9), gpiochip_get_data(9), gpiochip_remove(9), gpio_device_find(9), gpio_device_find_by_label(9), gpio_device_find_by_fwnode(9), gpio_device_get(9), gpio_device_put(9), gpio_device_to_device(9), gpiochip_set_hierarchical_irqchip(9), gpiochip_irq_domain_activate(9), gpiochip_irq_domain_deactivate(9), gpiochip_irq_map(9), gpiochip_add_irqchip(9), gpiochip_irqchip_remove(9), gpiochip_irqchip_add_domain(9), gpiochip_generic_request(9), gpiochip_generic_free(9), gpiochip_generic_config(9), gpiochip_add_pingroup_range(9), gpiochip_add_pin_range_with_pins(9), gpiochip_remove_pin_ranges(9), gpiochip_dup_line_label(9), gpiochip_request_own_desc(9), gpiochip_free_own_desc(9), gpio_set_debounce_timeout(9), gpiod_direction_input(9), gpiod_direction_output_raw(9), gpiod_direction_output(9), gpiod_enable_hw_timestamp_ns(9), gpiod_disable_hw_timestamp_ns(9), gpiod_set_config(9), gpiod_set_debounce(9), gpiod_set_transitory(9), gpiod_is_active_low(9), gpiod_toggle_active_low(9), gpiod_get_raw_value(9), gpiod_get_value(9), gpiod_get_raw_array_value(9), gpiod_get_array_value(9), gpiod_set_raw_value(9), gpiod_set_value_nocheck(9), gpiod_set_value(9), gpiod_set_raw_array_value(9), gpiod_set_array_value(9), gpiod_cansleep(9), gpiod_set_consumer_name(9), gpiod_is_shared(9), gpiod_to_irq(9), gpiochip_lock_as_irq(9), gpiochip_unlock_as_irq(9), gpiod_get_raw_value_cansleep(9), gpiod_get_value_cansleep(9), gpiod_get_raw_array_value_cansleep(9), gpiod_get_array_value_cansleep(9), gpiod_set_raw_value_cansleep(9), gpiod_set_value_cansleep(9), gpiod_set_raw_array_value_cansleep(9), gpiod_add_lookup_tables(9), gpiod_set_array_value_cansleep(9), gpiod_add_lookup_table(9), gpiod_remove_lookup_table(9), fwnode_gpiod_get_index(9), gpiod_count(9), gpiod_get(9), gpiod_configure_flags(9), gpiod_get_index(9), gpiod_get_index_optional(9), gpiod_hog(9), gpiochip_free_hogs(9), gpiod_get_array(9), gpiod_get_array_optional(9), gpiod_put(9), gpiod_put_array(9)

like… that's a lot of friends. slay for them πŸ’…


<div align="center">

and that's the page bestie πŸŽ€ it lives in ./drivers/gpio, dated October 2026, and it was brought to you by gpiod_get_optional(9) β€” go read her sisters next, mwah πŸ’–

</div>

πŸ“¦ where this page comes from