[U-Boot] [PATCH 6/8] dm: led: Add support for blinking LEDs

Simon Glass sjg at chromium.org
Fri Mar 31 17:55:03 UTC 2017


Allow LEDs to be blinked if the driver supports it.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 drivers/led/led-uclass.c | 10 ++++++++++
 include/led.h            | 22 ++++++++++++++++++++++
 test/dm/led.c            | 22 ++++++++++++++++++++++
 3 files changed, 54 insertions(+)

diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index ea5fbabadf..95b3be425a 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -52,6 +52,16 @@ enum led_state_t led_get_state(struct udevice *dev)
 	return ops->get_state(dev);
 }
 
+int led_set_period(struct udevice *dev, int period_ms)
+{
+	struct led_ops *ops = led_get_ops(dev);
+
+	if (!ops->set_period)
+		return -ENOSYS;
+
+	return ops->set_period(dev, period_ms);
+}
+
 UCLASS_DRIVER(led) = {
 	.id		= UCLASS_LED,
 	.name		= "led",
diff --git a/include/led.h b/include/led.h
index 84a365228f..ebb6faa34b 100644
--- a/include/led.h
+++ b/include/led.h
@@ -21,6 +21,7 @@ enum led_state_t {
 	LEDST_OFF = 0,
 	LEDST_ON = 1,
 	LEDST_TOGGLE = 2,
+	LEDST_BLINK = 3,
 
 	LEDST_COUNT,
 };
@@ -42,6 +43,18 @@ struct led_ops {
 	 * @return LED state led_state_t, or -ve on error
 	 */
 	enum led_state_t (*get_state)(struct udevice *dev);
+
+	/**
+	 * led_set_period() - set the blink period of an LED
+	 *
+	 * Thie records the period if supported, or returns -ENOSYS if not.
+	 * To start the LED blinking, use set_state().
+	 *
+	 * @dev:	LED device to change
+	 * @period_ms:	LED blink period in milliseconds
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*set_period)(struct udevice *dev, int period_ms);
 };
 
 #define led_get_ops(dev)	((struct led_ops *)(dev)->driver->ops)
@@ -72,4 +85,13 @@ int led_set_state(struct udevice *dev, enum led_state_t state);
  */
 enum led_state_t led_get_state(struct udevice *dev);
 
+/**
+ * led_set_period() - set the blink period of an LED
+ *
+ * @dev:	LED device to change
+ * @period_ms:	LED blink period in milliseconds
+ * @return 0 if OK, -ve on error
+ */
+int led_set_period(struct udevice *dev, int period_ms);
+
 #endif
diff --git a/test/dm/led.c b/test/dm/led.c
index 2cc24127e2..79575bd565 100644
--- a/test/dm/led.c
+++ b/test/dm/led.c
@@ -98,3 +98,25 @@ static int dm_test_led_label(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_led_label, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test LED blinking */
+static int dm_test_led_blink(struct unit_test_state *uts)
+{
+	const int offset = 1;
+	struct udevice *dev, *gpio;
+
+	/*
+	 * Check that we get an error when trying to blink an LED, since it is
+	 * not supported by the GPIO LED driver.
+	 */
+	ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
+	ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
+	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
+	ut_asserteq(-ENOSYS, led_set_state(dev, LEDST_BLINK));
+	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
+	ut_asserteq(LEDST_OFF, led_get_state(dev));
+	ut_asserteq(-ENOSYS, led_set_period(dev, 100));
+
+	return 0;
+}
+DM_TEST(dm_test_led_blink, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.12.2.564.g063fe858b8-goog



More information about the U-Boot mailing list