[U-Boot] [PATCH v2 18/22] cmd: LED: be aware of DTS-configured leds

Benjamin Tietz uboot at dresden.micronet24.de
Mon Jun 20 20:27:32 CEST 2016


From: Benjamin Tietz <benjamin at micronet24.de>

Make the LED command aware of device-tree configured LEDs, using the new
LED API calls.
---
 cmd/led.c |   53 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/cmd/led.c b/cmd/led.c
index 5295a6e..7632074 100644
--- a/cmd/led.c
+++ b/cmd/led.c
@@ -186,6 +186,54 @@ LED_TBL_STATUS(5, STATUS_LED_BIT5);
 #define LED_TEST_5(name)	while(0)
 #endif
 
+#ifdef CONFIG_LED
+#include <led.h>
+
+static void _led_dm_onoff(const led_tbl_t *led, enum led_cmd cmd, char *arg)
+{
+	struct udevice *dev = NULL;
+	if(led_get_by_label(led->string, &dev))
+		return;
+	if(dev)
+		led_set_on(dev, cmd == LED_ON);
+}
+
+// this isn't const, as the string will be replaced by the current led's label, always.
+static led_tbl_t _led_dm = {
+	.op = {
+		[LED_ON] = _led_dm_onoff,
+		[LED_OFF] = _led_dm_onoff,
+		[LED_LIST] = _led_list_name,
+	},
+};
+
+static const led_tbl_t *_led_dm_get(const char *name) {
+	struct udevice *dev = NULL;
+	if(led_get_by_label(name, &dev))
+		return NULL;
+	if(!dev)
+		return NULL;
+	_led_dm.string = name;
+	return &_led_dm;
+}
+
+#define LED_DM_COUNT()	led_count()
+static int led_dm_list(const char **tbl, int size) {
+	struct udevice *devs[size];
+	int cnt = led_get_all(devs, size);
+	int i;
+	for(i = 0; i < cnt; i++)
+		tbl[i] = led_get_label(devs[i]);
+	return cnt;
+}
+#define LED_TEST_DM(name) return _led_dm_get(name)
+
+#else
+#define LED_TEST_DM(name)	return NULL
+#define LED_DM_COUNT()		0
+#define led_dm_list(tbl, size)	0
+#endif
+
 static int _led_count(void) {
 	int i = 0;
 	if(LED_NAME_GREEN) i++;
@@ -198,6 +246,7 @@ static int _led_count(void) {
 	if(LED_NAME_3) i++;
 	if(LED_NAME_4) i++;
 	if(LED_NAME_5) i++;
+	i += LED_DM_COUNT();
 	return i;
 }
 
@@ -214,6 +263,7 @@ static int _led_list(const char **tbl, int size) {
 	TEST_ADD_NAME(LED_NAME_3, tbl, size);
 	TEST_ADD_NAME(LED_NAME_4, tbl, size);
 	TEST_ADD_NAME(LED_NAME_5, tbl, size);
+	size -= led_dm_list(tbl, size);
 	return init_size - size;
 }
 
@@ -251,7 +301,8 @@ static const led_tbl_t *get_led(const char *name) {
 	LED_TEST_3(name);
 	LED_TEST_4(name);
 	LED_TEST_5(name);
-	return NULL;
+	// must be last, returns always
+	LED_TEST_DM(name);
 }
 
 enum led_cmd get_led_cmd(char *var)



More information about the U-Boot mailing list