Skip to content

Commit 3647137

Browse files
KarlK90Bartosz Golaszewski
authored andcommitted
gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper
The helper makes it easier to handle optional GPIOs and simplifies the error handling code. Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de> Signed-off-by: Michael Tretter <m.tretter@pengutronix.de> Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com> Link: https://lore.kernel.org/r/20260126-gpio-devm_fwnode_gpiod_get_optional-v2-1-ec34f8e35077@pengutronix.de Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
1 parent 3a6a36a commit 3647137

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

include/linux/gpio/consumer.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,42 @@ struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
607607
flags, label);
608608
}
609609

610+
/**
611+
* devm_fwnode_gpiod_get_optional - obtain an optional GPIO from firmware node
612+
* @dev: GPIO consumer
613+
* @fwnode: handle of the firmware node
614+
* @con_id: function within the GPIO consumer
615+
* @flags: GPIO initialization flags
616+
* @label: label to attach to the requested GPIO
617+
*
618+
* This function can be used for drivers that get their configuration
619+
* from opaque firmware.
620+
*
621+
* GPIO descriptors returned from this function are automatically disposed on
622+
* driver detach.
623+
*
624+
* Returns:
625+
* The GPIO descriptor corresponding to the optional function @con_id of device
626+
* dev, NULL if no GPIO has been assigned to the requested function, or
627+
* another IS_ERR() code if an error occurred while trying to acquire the GPIO.
628+
*/
629+
static inline
630+
struct gpio_desc *devm_fwnode_gpiod_get_optional(struct device *dev,
631+
struct fwnode_handle *fwnode,
632+
const char *con_id,
633+
enum gpiod_flags flags,
634+
const char *label)
635+
{
636+
struct gpio_desc *desc;
637+
638+
desc = devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
639+
flags, label);
640+
if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
641+
return NULL;
642+
643+
return desc;
644+
}
645+
610646
struct acpi_gpio_params {
611647
unsigned int crs_entry_index;
612648
unsigned short line_index;

0 commit comments

Comments
 (0)