public inbox for linux-gpio@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional
@ 2026-01-26 14:27 Michael Tretter
  2026-01-26 14:27 ` [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper Michael Tretter
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Michael Tretter @ 2026-01-26 14:27 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Matti Vaittinen,
	Liam Girdwood, Mark Brown
  Cc: linux-gpio, kernel, Michael Tretter, Stefan Kerkmann

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.

Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
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,
-- 
Michael Tretter <m.tretter@pengutronix.de>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper
  2026-01-26 14:27 [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional Michael Tretter
@ 2026-01-26 14:27 ` Michael Tretter
  2026-01-27  6:53   ` Matti Vaittinen
  2026-01-27 19:40   ` Andy Shevchenko
  2026-01-26 14:27 ` [PATCH v2 2/2] regulator: bd71815: switch to devm_fwnode_gpiod_get_optional Michael Tretter
  2026-01-27  9:17 ` [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional Bartosz Golaszewski
  2 siblings, 2 replies; 6+ messages in thread
From: Michael Tretter @ 2026-01-26 14:27 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Matti Vaittinen,
	Liam Girdwood, Mark Brown
  Cc: linux-gpio, kernel, Michael Tretter, Stefan Kerkmann

From: Stefan Kerkmann <s.kerkmann@pengutronix.de>

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>
---
Changes in v2:
- Add kerneldoc for devm_fwnode_gpiod_get_optional
---
 include/linux/gpio/consumer.h | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h
index cafeb7a40ad1..0d8408582918 100644
--- a/include/linux/gpio/consumer.h
+++ b/include/linux/gpio/consumer.h
@@ -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;

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/2] regulator: bd71815: switch to devm_fwnode_gpiod_get_optional
  2026-01-26 14:27 [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional Michael Tretter
  2026-01-26 14:27 ` [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper Michael Tretter
@ 2026-01-26 14:27 ` Michael Tretter
  2026-01-27  9:17 ` [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional Bartosz Golaszewski
  2 siblings, 0 replies; 6+ messages in thread
From: Michael Tretter @ 2026-01-26 14:27 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Matti Vaittinen,
	Liam Girdwood, Mark Brown
  Cc: linux-gpio, kernel, Michael Tretter

Use the devm_fwnode_gpiod_get_optional variant to simplify the error
handling code.

Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>
Acked-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Michael Tretter <m.tretter@pengutronix.de>
---
Changes in v2:
- none
---
 drivers/regulator/bd71815-regulator.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/drivers/regulator/bd71815-regulator.c b/drivers/regulator/bd71815-regulator.c
index 8da57a7bb2f1..668714f35464 100644
--- a/drivers/regulator/bd71815-regulator.c
+++ b/drivers/regulator/bd71815-regulator.c
@@ -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);

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper
  2026-01-26 14:27 ` [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper Michael Tretter
@ 2026-01-27  6:53   ` Matti Vaittinen
  2026-01-27 19:40   ` Andy Shevchenko
  1 sibling, 0 replies; 6+ messages in thread
From: Matti Vaittinen @ 2026-01-27  6:53 UTC (permalink / raw)
  To: Michael Tretter, Linus Walleij, Bartosz Golaszewski,
	Liam Girdwood, Mark Brown
  Cc: linux-gpio, kernel, Stefan Kerkmann

On 26/01/2026 16:27, Michael Tretter wrote:
> From: Stefan Kerkmann <s.kerkmann@pengutronix.de>
> 
> 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>

FWIW:
Reviewed-by: Matti Vaittinen <mazziesaccount@gmail.com>

Yours,
   -- Matti

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

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional
  2026-01-26 14:27 [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional Michael Tretter
  2026-01-26 14:27 ` [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper Michael Tretter
  2026-01-26 14:27 ` [PATCH v2 2/2] regulator: bd71815: switch to devm_fwnode_gpiod_get_optional Michael Tretter
@ 2026-01-27  9:17 ` Bartosz Golaszewski
  2 siblings, 0 replies; 6+ messages in thread
From: Bartosz Golaszewski @ 2026-01-27  9:17 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski, Matti Vaittinen,
	Liam Girdwood, Mark Brown, Michael Tretter
  Cc: Bartosz Golaszewski, linux-gpio, kernel, Stefan Kerkmann


On Mon, 26 Jan 2026 15:27:46 +0100, Michael Tretter wrote:
> 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,
-- 
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper
  2026-01-26 14:27 ` [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper Michael Tretter
  2026-01-27  6:53   ` Matti Vaittinen
@ 2026-01-27 19:40   ` Andy Shevchenko
  1 sibling, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2026-01-27 19:40 UTC (permalink / raw)
  To: Michael Tretter
  Cc: Linus Walleij, Bartosz Golaszewski, Matti Vaittinen,
	Liam Girdwood, Mark Brown, linux-gpio, kernel, Stefan Kerkmann

On Mon, Jan 26, 2026 at 03:27:47PM +0100, Michael Tretter wrote:
> 
> 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.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-01-27 19:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-26 14:27 [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional Michael Tretter
2026-01-26 14:27 ` [PATCH v2 1/2] gpiolib: introduce devm_fwnode_gpiod_get_optional() wrapper Michael Tretter
2026-01-27  6:53   ` Matti Vaittinen
2026-01-27 19:40   ` Andy Shevchenko
2026-01-26 14:27 ` [PATCH v2 2/2] regulator: bd71815: switch to devm_fwnode_gpiod_get_optional Michael Tretter
2026-01-27  9:17 ` [PATCH v2 0/2] gpiolib: introduce devm_fwnode_gpiod_get_optional Bartosz Golaszewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox