Skip to contentSkip to conversation

gpiolib: introduce devm_fwnode_gpiod_get_optional

PATCH Applied Michael Tretter 4 participants 6 messages Jan 26 – Jan 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover gpiolib: introduce devm_fwnode_gpiod_get_optional

There are various helpers to simplify the handling of optional gpios. The devm_fwnode_gpiod_get() lacks the _optional variant, and drivers have to explicitly handle the error for optional gpios.

Introduce a devm_fwnode_gpiod_get_optional helper and simplify the BD71815 voltage regulator driver by using this helper.

---
Changes in v2:
- Add kerneldoc for devm_fwnode_gpiod_get_optional
- Link to v1: https://patch.msgid.link/20260123-gpio-devm_fwnode_gpiod_get_optional-v1-0-fb49905452a6@pengutronix.de

---
Michael Tretter (1):
regulator: bd71815: switch to devm_fwnode_gpiod_get_optional

Stefan Kerkmann (1):
gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper

 drivers/regulator/bd71815-regulator.c | 15 ++++++---------
 include/linux/gpio/consumer.h         | 36 +++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 9 deletions(-)

---
base-commit: c072629f05d7bca1148ab17690d7922a31423984
change-id: 20260123-gpio-devm_fwnode_gpiod_get_optional-e96227cd393b

Best regards,

Signature · 1 line
Michael Tretter <[email protected]>
#1 lore raw

PATCH v2 1/2 gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper

From: Stefan Kerkmann <[email protected]>

The helper makes it easier to handle optional GPIOs and simplifies the error handling code.

---
Changes in v2:
- Add kerneldoc for devm_fwnode_gpiod_get_optional

---
 include/linux/gpio/consumer.h | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
include/linux/gpio/consumer.h +36 -0
index cafeb7a40ad1..0d8408582918 100644
@@ -607,6 +607,42 @@ struct gpio_desc *devm_fwnode_gpiod_get(struct device *dev,
 					   flags, label);
 }
 
+/**
+ * devm_fwnode_gpiod_get_optional - obtain an optional GPIO from firmware node
+ * @dev:	GPIO consumer
+ * @fwnode:	handle of the firmware node
+ * @con_id:	function within the GPIO consumer
+ * @flags:	GPIO initialization flags
+ * @label:	label to attach to the requested GPIO
+ *
+ * This function can be used for drivers that get their configuration
+ * from opaque firmware.
+ *
+ * GPIO descriptors returned from this function are automatically disposed on
+ * driver detach.
+ *
+ * Returns:
+ * The GPIO descriptor corresponding to the optional function @con_id of device
+ * dev, NULL if no GPIO has been assigned to the requested function, or
+ * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
+ */
+static inline
+struct gpio_desc *devm_fwnode_gpiod_get_optional(struct device *dev,
+						 struct fwnode_handle *fwnode,
+						 const char *con_id,
+						 enum gpiod_flags flags,
+						 const char *label)
+{
+	struct gpio_desc *desc;
+
+	desc = devm_fwnode_gpiod_get_index(dev, fwnode, con_id, 0,
+					   flags, label);
+	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
+		return NULL;
+
+	return desc;
+}
+
 struct acpi_gpio_params {
 	unsigned int crs_entry_index;
 	unsigned short line_index;
Signature · 1 line
2.47.3
maintainer Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper

Quoted 7 lines · Michael Tretter

From: Stefan Kerkmann <[email protected]>

The helper makes it easier to handle optional GPIOs and simplifies the error handling code.

FWIW:

Yours,
-- Matti

---
Matti Vaittinen
Linux kernel developer at ROHM Semiconductors
Oulu Finland

~~ When things go utterly wrong vim users can always type :help! ~~

#3 lore raw

Re: [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper

In reply to Michael Tretter
Quoted 3 lines · Michael Tretter

The helper makes it easier to handle optional GPIOs and simplifies the error handling code.

This helper should be exported function which may use the existing internal API, id est gpiod_not_found(). Another possibility to move the latter to the tree wide header, but I'm not sure we can make the good choice for that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

PATCH v2 2/2 regulator: bd71815: switch to devm_fwnode_gpiod_get_optional

In reply to Michael Tretter

Use the devm_fwnode_gpiod_get_optional variant to simplify the error handling code.

---
Changes in v2:
- none

---
 drivers/regulator/bd71815-regulator.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/regulator/bd71815-regulator.c +6 -9
index 8da57a7bb2f1..668714f35464 100644
@@ -571,15 +571,12 @@ static int bd7181x_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	ldo4_en = devm_fwnode_gpiod_get(&pdev->dev,
-					dev_fwnode(pdev->dev.parent),
-					"rohm,vsel", GPIOD_ASIS, "ldo4-en");
-	if (IS_ERR(ldo4_en)) {
-		ret = PTR_ERR(ldo4_en);
-		if (ret != -ENOENT)
-			return ret;
-		ldo4_en = NULL;
-	}
+	ldo4_en = devm_fwnode_gpiod_get_optional(&pdev->dev,
+						 dev_fwnode(pdev->dev.parent),
+						 "rohm,vsel", GPIOD_ASIS,
+						 "ldo4-en");
+	if (IS_ERR(ldo4_en))
+		return PTR_ERR(ldo4_en);
 
 	/* Disable to go to ship-mode */
 	ret = regmap_update_bits(regmap, BD71815_REG_PWRCTRL, RESTARTEN, 0);
Signature · 1 line
2.47.3
#5 lore raw
In reply to Michael Tretter
Quoted 8 lines · Michael Tretter

There are various helpers to simplify the handling of optional gpios. The devm_fwnode_gpiod_get() lacks the _optional variant, and drivers have to explicitly handle the error for optional gpios.

Introduce a devm_fwnode_gpiod_get_optional helper and simplify the BD71815 voltage regulator driver by using this helper.

[...]

Applied, thanks!

[1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper

      364713741ca19b8f6a506d073af1deff5b2d124a
[2/2] regulator: bd71815: switch to devm_fwnode_gpiod_get_optional
      09b174f1a554d69b9f69f3c6c115db12c1f6d29d

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>