diff mbox

[U-Boot,v2,17/22] LED: provide function to count and get all (DM-)LEDs

Message ID 20160620182727.21075.46894.stgit@obelix.dresden.micronet24.de
State Changes Requested
Delegated to: Simon Glass
Headers show

Commit Message

Benjamin Tietz June 20, 2016, 6:27 p.m. UTC
From: Benjamin Tietz <benjamin@micronet24.de>

This extends the API of the LED-driver to count the number
of configured LEDs and retrieving an array of them.
---
 drivers/led/led-uclass.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++
 include/led.h            |   18 ++++++++++++++++
 2 files changed, 71 insertions(+)
diff mbox

Patch

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