[U-Boot] [PATCH v2 21/22] LED: provide toggling interface

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


From: Benjamin Tietz <benjamin at micronet24.de>

Extend the LED API to support toggling an LED. If the device provides a
direct way for this, it can implement the corresponding op.

If not supported directly by the driver, the led will be toggled by calling
led_set_on(!led_get_on()), transparently.
---
 drivers/led/led-uclass.c |   14 ++++++++++++++
 include/led.h            |   21 +++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index 7d7dec3..22d2264 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -111,6 +111,20 @@ int led_get_on(struct udevice *dev)
 	return ops->get_on(dev);
 }
 
+int led_toggle(struct udevice *dev)
+{
+	struct led_ops *ops = led_get_ops(dev);
+	int on;
+
+	if (ops->toggle)
+		return ops->toggle(dev);
+
+	on = led_get_on(dev);
+	if(on < 0)
+		return -ENOSYS;
+	return led_set_on(dev, !on);
+}
+
 UCLASS_DRIVER(led) = {
 	.id		= UCLASS_LED,
 	.name		= "led",
diff --git a/include/led.h b/include/led.h
index 4709013..f70db00 100644
--- a/include/led.h
+++ b/include/led.h
@@ -36,6 +36,17 @@ struct led_ops {
 	 * @return 0 if OFF, 1 if ON, -ve on error
 	 */
 	int (*get_on)(struct udevice *dev);
+
+	/**
+	 * toggle() - toggle the state of an LED
+	 *
+	 * will turn off the LED if it's on and vice versa.
+	 * Optional. If not given, a fallback using get_on/set_on is provided.
+	 *
+	 * @dev:	LED device to change
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*toggle)(struct udevice *dev);
 };
 
 #define led_get_ops(dev)	((struct led_ops *)(dev)->driver->ops)
@@ -92,4 +103,14 @@ int led_set_on(struct udevice *dev, int on);
  */
 int led_get_on(struct udevice *dev);
 
+/**
+ * led_toggle() - toggle the state of an LED
+ *
+ * will turn off the LED if it's on and vice versa.
+ *
+ * @dev:	LED device to change
+ * @return 0 if OK, -ve on error
+ */
+int led_toggle(struct udevice *dev);
+
 #endif



More information about the U-Boot mailing list