Skip to content

Commit ef381e1

Browse files
Hans de Goedelag-linaro
authored andcommitted
leds: led-class: Add Device Tree support to led_get()
Add 'name' argument to of_led_get() such that it can lookup LEDs in devicetree by either name or index. And use this modified function to add devicetree support to the generic (non devicetree specific) [devm_]led_get() function. This uses the standard devicetree pattern of adding a -names string array to map names to the indexes for an array of resources. Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Hans de Goede <hansg@kernel.org> Signed-off-by: Aleksandrs Vinarskis <alex@vinarskis.com> Link: https://lore.kernel.org/r/20250910-leds-v5-3-bb90a0f897d5@vinarskis.com Signed-off-by: Lee Jones <lee@kernel.org>
1 parent 22420da commit ef381e1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

drivers/leds/led-class.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,23 @@ static const struct class leds_class = {
252252
* of_led_get() - request a LED device via the LED framework
253253
* @np: device node to get the LED device from
254254
* @index: the index of the LED
255+
* @name: the name of the LED used to map it to its function, if present
255256
*
256257
* Returns the LED device parsed from the phandle specified in the "leds"
257258
* property of a device tree node or a negative error-code on failure.
258259
*/
259-
static struct led_classdev *of_led_get(struct device_node *np, int index)
260+
static struct led_classdev *of_led_get(struct device_node *np, int index,
261+
const char *name)
260262
{
261263
struct device *led_dev;
262264
struct device_node *led_node;
263265

266+
/*
267+
* For named LEDs, first look up the name in the "led-names" property.
268+
* If it cannot be found, then of_parse_phandle() will propagate the error.
269+
*/
270+
if (name)
271+
index = of_property_match_string(np, "led-names", name);
264272
led_node = of_parse_phandle(np, "leds", index);
265273
if (!led_node)
266274
return ERR_PTR(-ENOENT);
@@ -324,7 +332,7 @@ struct led_classdev *__must_check devm_of_led_get(struct device *dev,
324332
if (!dev)
325333
return ERR_PTR(-EINVAL);
326334

327-
led = of_led_get(dev->of_node, index);
335+
led = of_led_get(dev->of_node, index, NULL);
328336
if (IS_ERR(led))
329337
return led;
330338

@@ -342,9 +350,14 @@ EXPORT_SYMBOL_GPL(devm_of_led_get);
342350
struct led_classdev *led_get(struct device *dev, char *con_id)
343351
{
344352
struct led_lookup_data *lookup;
353+
struct led_classdev *led_cdev;
345354
const char *provider = NULL;
346355
struct device *led_dev;
347356

357+
led_cdev = of_led_get(dev->of_node, -1, con_id);
358+
if (!IS_ERR(led_cdev) || PTR_ERR(led_cdev) != -ENOENT)
359+
return led_cdev;
360+
348361
mutex_lock(&leds_lookup_lock);
349362
list_for_each_entry(lookup, &leds_lookup_list, list) {
350363
if (!strcmp(lookup->dev_id, dev_name(dev)) &&

0 commit comments

Comments
 (0)