[U-Boot] [PATCH18/23] LED: provide function to count and get all (DM-)LEDs

Benjamin Tietz uboot at dresden.micronet24.de
Sat Jun 18 14:56:57 CEST 2016


From: Benjamin Tietz <benjamin at micronet24.de>


---
 drivers/led/led-uclass.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++
 include/led.h            |   18 ++++++++++++++++
 2 files changed, 71 insertions(+)
-------------- next part --------------
From: Benjamin Tietz <benjamin at micronet24.de>


---
 drivers/led/led-uclass.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++
 include/led.h            |   18 ++++++++++++++++
 2 files changed, 71 insertions(+)

diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index f5fbbcb..ce519d0 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -32,6 +32,59 @@ int led_get_by_label(const char *label, struct udevice **devp)
 	return -ENODEV;
 }
 
+int led_count(void)
+{
+	int cnt = 0;
+	struct udevice *dev;
+	struct uclass *uc;
+	int ret;
+
+	ret = uclass_get(UCLASS_LED, &uc);
+	if (ret)
+		return ret;
+
+	uclass_foreach_dev(dev, uc) {
+		struct led_uclass_plat *uc_plat = dev_get_uclass_platdata(dev);
+
+		/* Ignore the top-level LED node */
+		if (uc_plat->label)
+			cnt++;
+	}
+
+	return cnt;
+}
+
+int led_get_all(struct udevice **devarr, int maxret)
+{
+	int cnt = 0;
+	struct udevice *dev;
+	struct uclass *uc;
+	int ret;
+
+	ret = uclass_get(UCLASS_LED, &uc);
+	if (ret)
+		return ret;
+
+	if(maxret <= cnt)
+		return cnt;
+
+	uclass_foreach_dev(dev, uc) {
+		struct led_uclass_plat *uc_plat = dev_get_uclass_platdata(dev);
+
+		/* Ignore the top-level LED node */
+		if (uc_plat->label) {
+			ret = uclass_get_device_tail(dev, 0, &devarr[cnt]);
+			if(ret)
+				return ret;
+			cnt++;
+			if(cnt >= maxret)
+				return cnt;
+		}
+	}
+
+	return cnt;
+}
+
 const char *led_get_label(struct udevice *dev)
 {
 	struct led_uclass_plat *uc_plat = dev_get_uclass_platdata(dev);
diff --git a/include/led.h b/include/led.h
index 32e0dec..4b1bd56 100644
--- a/include/led.h
+++ b/include/led.h
@@ -48,6 +48,24 @@ int led_get_by_label(const char *label, struct udevice **devp);
 const char *led_get_label(struct udevice *dev);
 
 /**
+ * led_count() - retrieve the number of DM-configured LEDs
+ *
+ * @return the number of leds found
+ */
+int led_count(void);
+
+/**
+ * led_get_all() - retrieve an array of DM-configured LEDs
+ *
+ * Should return at most led_count() leds.
+ *
+ * @devarr:	pointer to an array of led-device, to be filled.
+ * @maxitm:	pre-allocated size of the array
+ * @return number of leds filled into @devarr
+ */
+int led_get_all(struct udevice **devarr, int maxitm);
+
+/**
  * led_set_on() - set the state of an LED
  *
  * @dev:	LED device to change


More information about the U-Boot mailing list