[U-Boot] [PATCH v4 3/5] dm: led: move default state support in led uclass

Patrick Delaunay patrick.delaunay at st.com
Fri Jul 27 14:37:07 UTC 2018


This patch save common LED property "default-state" value
in post bind of LED uclass.
The configuration for this default state is only performed when
led_default_state() is called;
It can be called in your board_init()
or it could added in init_sequence_r[] in future.

Signed-off-by: Patrick Delaunay <patrick.delaunay at st.com>
---

Changes in v4:
  - simplify the uclass impact after Patrick Bruenn review

Changes in v3: None
Changes in v2: None

 drivers/led/led-uclass.c | 30 ++++++++++++++++++++++++++++++
 drivers/led/led_gpio.c   |  8 --------
 include/led.h            |  9 +++++++++
 3 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index 2f4d69e..2859475 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -8,6 +8,7 @@
 #include <dm.h>
 #include <errno.h>
 #include <led.h>
+#include <dm/device-internal.h>
 #include <dm/root.h>
 #include <dm/uclass-internal.h>
 
@@ -63,6 +64,35 @@ int led_set_period(struct udevice *dev, int period_ms)
 }
 #endif
 
+int led_default_state(void)
+{
+	struct udevice *dev;
+	struct uclass *uc;
+	const char *default_state;
+	int ret;
+
+	ret = uclass_get(UCLASS_LED, &uc);
+	if (ret)
+		return ret;
+	for (uclass_find_first_device(UCLASS_LED, &dev);
+	     dev;
+	     uclass_find_next_device(&dev)) {
+		default_state = dev_read_string(dev, "default-state");
+		if (!default_state)
+			continue;
+		ret = device_probe(dev);
+		if (ret)
+			return ret;
+		if (!strncmp(default_state, "on", 2))
+			led_set_state(dev, LEDST_ON);
+		else if (!strncmp(default_state, "off", 3))
+			led_set_state(dev, LEDST_OFF);
+		/* default-state = "keep" : device is only probed */
+	}
+
+	return ret;
+}
+
 UCLASS_DRIVER(led) = {
 	.id		= UCLASS_LED,
 	.name		= "led",
diff --git a/drivers/led/led_gpio.c b/drivers/led/led_gpio.c
index 533587d..93f6b91 100644
--- a/drivers/led/led_gpio.c
+++ b/drivers/led/led_gpio.c
@@ -57,7 +57,6 @@ static int led_gpio_probe(struct udevice *dev)
 {
 	struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
 	struct led_gpio_priv *priv = dev_get_priv(dev);
-	const char *default_state;
 	int ret;
 
 	/* Ignore the top-level LED node */
@@ -68,13 +67,6 @@ static int led_gpio_probe(struct udevice *dev)
 	if (ret)
 		return ret;
 
-	default_state = dev_read_string(dev, "default-state");
-	if (default_state) {
-		if (!strncmp(default_state, "on", 2))
-			gpio_led_set_state(dev, LEDST_ON);
-		else if (!strncmp(default_state, "off", 3))
-			gpio_led_set_state(dev, LEDST_OFF);
-	}
 	return 0;
 }
 
diff --git a/include/led.h b/include/led.h
index 940b97f..7bfdddf 100644
--- a/include/led.h
+++ b/include/led.h
@@ -106,4 +106,13 @@ enum led_state_t led_get_state(struct udevice *dev);
  */
 int led_set_period(struct udevice *dev, int period_ms);
 
+/**
+ * led_default_state() - set the default state for all the LED
+ *
+ * This enables all leds which have default state.
+ * see Documentation/devicetree/bindings/leds/common.txt
+ *
+ */
+int led_default_state(void);
+
 #endif
-- 
2.7.4



More information about the U-Boot mailing list