[PATCH 2/4] led: class: Add devm_fwnode_led_get() to get a LED from a firmware node

From: Jean-Jacques Hiblot
Date: Wed Jun 15 2022 - 11:49:52 EST


Same functionality as devm_of_led_get(), but it takes a firmware node
as a parameter.
This mimic devm_fwnode_pwm_get() found in the PWM core.

The ACPI implementation is missing and the function returns -EOPNOTSUPP
when the firmware node is actually an ACPI node.

Signed-off-by: Jean-Jacques Hiblot <jjhiblot@xxxxxxxxxxxxxxx>
---
drivers/leds/led-class.c | 34 ++++++++++++++++++++++++++++++++++
include/linux/leds.h | 4 ++++
2 files changed, 38 insertions(+)

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 72fd6ee7af88..7faa953a3870 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -297,6 +297,40 @@ struct led_classdev *__must_check devm_of_led_get(struct device *dev,
}
EXPORT_SYMBOL_GPL(devm_of_led_get);

+/**
+ * devm_fwnode_led_get() - request a resource managed LED from firmware node
+ * @dev: device for LED consumer
+ * @fwnode: firmware node to get the LED from
+ * @index: index of the LED to obtain in the consumer
+ *
+ * Returns the LED device parsed from the firmware node. See of_led_get().
+ *
+ * Returns: A pointer to the requested LED device or an ERR_PTR()-encoded
+ * error code on failure.
+ */
+struct led_classdev *__must_check devm_fwnode_led_get(struct device *dev,
+ struct fwnode_handle *fwnode,
+ int index)
+{
+ struct led_classdev *led = ERR_PTR(-ENODEV);
+ int ret;
+
+ if (is_of_node(fwnode))
+ led = of_led_get(to_of_node(fwnode), index);
+ else if (is_acpi_node(fwnode))
+ /* ACPI support is not yet implemented */
+ led = ERR_PTR(-EOPNOTSUPP);
+ if (IS_ERR(led))
+ return led;
+
+ ret = devm_add_action_or_reset(dev, devm_led_release, led);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return led;
+}
+EXPORT_SYMBOL_GPL(devm_fwnode_led_get);
+
static int led_classdev_next_name(const char *init_name, char *name,
size_t len)
{
diff --git a/include/linux/leds.h b/include/linux/leds.h
index ba4861ec73d3..ace0130bfcd2 100644
--- a/include/linux/leds.h
+++ b/include/linux/leds.h
@@ -216,6 +216,10 @@ extern void led_put(struct led_classdev *led_cdev);
struct led_classdev *__must_check devm_of_led_get(struct device *dev,
int index);

+struct led_classdev *__must_check devm_fwnode_led_get(struct device *dev,
+ struct fwnode_handle *fwnode,
+ int index);
+
/**
* led_blink_set - set blinking with software fallback
* @led_cdev: the LED to start blinking
--
2.25.1