mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Aleksandrs Vinarskis <alex@vinarskis.com>
To: Hans de Goede <hansg@kernel.org>, Lee Jones <lee@kernel.org>,
	 Pavel Machek <pavel@kernel.org>, Rob Herring <robh@kernel.org>,
	 Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>,
	 Bryan O'Donoghue <bryan.odonoghue@linaro.org>,
	 Daniel Thompson <danielt@kernel.org>,
	Jingoo Han <jingoohan1@gmail.com>,
	 Mauro Carvalho Chehab <mchehab@kernel.org>,
	 Jean-Jacques Hiblot <jjhiblot@traphandler.com>,
	 Jacopo Mondi <jacopo@jmondi.org>,
	 Sakari Ailus <sakari.ailus@linux.intel.com>,
	 Bjorn Andersson <andersson@kernel.org>,
	 Konrad Dybcio <konradybcio@kernel.org>
Cc: linux-leds@vger.kernel.org, devicetree@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Daniel Thompson <daniel.thompson@linaro.org>,
	 dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org,
	 linux-arm-msm@vger.kernel.org,
	Aleksandrs Vinarskis <alex@vinarskis.com>,
	 Andy Shevchenko <andy.shevchenko@gmail.com>,
	 Linus Walleij <linus.walleij@linaro.org>
Subject: [PATCH v2 3/4] leds: led-class: Add devicetree support to led_get()
Date: Fri, 05 Sep 2025 09:59:31 +0200	[thread overview]
Message-ID: <20250905-leds-v2-3-ed8f66f56da8@vinarskis.com> (raw)
In-Reply-To: <20250905-leds-v2-0-ed8f66f56da8@vinarskis.com>

From: Hans de Goede <hansg@kernel.org>

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: Lee Jones <lee@kernel.org>
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>
---
 drivers/leds/led-class.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 15633fbf3c166aa4f521774d245f6399a642bced..982eb4c1aac998e85b104d7cba26e786ca62056f 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -252,15 +252,23 @@ static const struct class leds_class = {
  * of_led_get() - request a LED device via the LED framework
  * @np: device node to get the LED device from
  * @index: the index of the LED
+ * @name: the name of the LED used to map it to its function, if present
  *
  * Returns the LED device parsed from the phandle specified in the "leds"
  * property of a device tree node or a negative error-code on failure.
  */
-static struct led_classdev *of_led_get(struct device_node *np, int index)
+static struct led_classdev *of_led_get(struct device_node *np, int index,
+				       const char *name)
 {
 	struct device *led_dev;
 	struct device_node *led_node;
 
+	/*
+	 * For named LEDs, first look up the name in the "led-names" property.
+	 * If it cannot be found, then of_parse_phandle() will propagate the error.
+	 */
+	if (name)
+		index = of_property_match_string(np, "led-names", name);
 	led_node = of_parse_phandle(np, "leds", index);
 	if (!led_node)
 		return ERR_PTR(-ENOENT);
@@ -324,7 +332,7 @@ struct led_classdev *__must_check devm_of_led_get(struct device *dev,
 	if (!dev)
 		return ERR_PTR(-EINVAL);
 
-	led = of_led_get(dev->of_node, index);
+	led = of_led_get(dev->of_node, index, NULL);
 	if (IS_ERR(led))
 		return led;
 
@@ -342,9 +350,16 @@ EXPORT_SYMBOL_GPL(devm_of_led_get);
 struct led_classdev *led_get(struct device *dev, char *con_id)
 {
 	struct led_lookup_data *lookup;
+	struct led_classdev *led_cdev;
 	const char *provider = NULL;
 	struct device *led_dev;
 
+	if (dev->of_node) {
+		led_cdev = of_led_get(dev->of_node, -1, con_id);
+		if (!IS_ERR(led_cdev) || PTR_ERR(led_cdev) != -ENOENT)
+			return led_cdev;
+	}
+
 	mutex_lock(&leds_lookup_lock);
 	list_for_each_entry(lookup, &leds_lookup_list, list) {
 		if (!strcmp(lookup->dev_id, dev_name(dev)) &&

-- 
2.48.1


  parent reply	other threads:[~2025-09-05  8:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-05  7:59 [PATCH v2 0/4] leds: privacy-led support for devicetree Aleksandrs Vinarskis
2025-09-05  7:59 ` [PATCH v2 1/4] dt-bindings: leds: add generic LED consumer documentation Aleksandrs Vinarskis
2025-09-05 10:20   ` Konrad Dybcio
2025-09-05 15:02   ` Rob Herring (Arm)
2025-09-05 15:17   ` Rob Herring
2025-09-05 15:34     ` Rob Herring
2025-09-05  7:59 ` [PATCH v2 2/4] dt-bindings: leds: commonize leds property Aleksandrs Vinarskis
2025-09-05 15:24   ` Rob Herring
2025-09-05 16:48     ` Aleksandrs Vinarskis
2025-09-05 23:02       ` Rob Herring
2025-09-05  7:59 ` Aleksandrs Vinarskis [this message]
2025-09-05  9:58   ` [PATCH v2 3/4] leds: led-class: Add devicetree support to led_get() Andy Shevchenko
2025-09-05  7:59 ` [PATCH v2 4/4] arm64: dts: qcom: sc8280xp-x13s: enable camera privacy indicator Aleksandrs Vinarskis
2025-09-05 10:21   ` Konrad Dybcio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250905-leds-v2-3-ed8f66f56da8@vinarskis.com \
    --to=alex@vinarskis.com \
    --cc=andersson@kernel.org \
    --cc=andy.shevchenko@gmail.com \
    --cc=bryan.odonoghue@linaro.org \
    --cc=conor+dt@kernel.org \
    --cc=daniel.thompson@linaro.org \
    --cc=danielt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hansg@kernel.org \
    --cc=jacopo@jmondi.org \
    --cc=jingoohan1@gmail.com \
    --cc=jjhiblot@traphandler.com \
    --cc=konradybcio@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=pavel@kernel.org \
    --cc=robh@kernel.org \
    --cc=sakari.ailus@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®