dm: led: Add support for getting the state of an LED
authorSimon Glass <sjg@chromium.org>
Mon, 10 Apr 2017 17:34:55 +0000 (11:34 -0600)
committerSimon Glass <sjg@chromium.org>
Sat, 15 Apr 2017 01:38:57 +0000 (19:38 -0600)
It is useful to be able to read the LED as well as write it. Add this to
the uclass and update the GPIO driver.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ziping Chen <techping.chan@gmail.com>
drivers/led/led-uclass.c
drivers/led/led_gpio.c
include/led.h
test/dm/led.c

index b30346913b9fa5bf6b2f179c43f526263a6fcc7e..ea5fbabadf3b78aaa31f202c86d907cdf8622da5 100644 (file)
@@ -42,6 +42,16 @@ int led_set_state(struct udevice *dev, enum led_state_t state)
        return ops->set_state(dev, state);
 }
 
+enum led_state_t led_get_state(struct udevice *dev)
+{
+       struct led_ops *ops = led_get_ops(dev);
+
+       if (!ops->get_state)
+               return -ENOSYS;
+
+       return ops->get_state(dev);
+}
+
 UCLASS_DRIVER(led) = {
        .id             = UCLASS_LED,
        .name           = "led",
index af8133d3c700b26c752def58b02543ac62002202..789d15600fdc1092e66c8279e54b8d6288ee4543 100644 (file)
@@ -24,10 +24,31 @@ static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
 
        if (!dm_gpio_is_valid(&priv->gpio))
                return -EREMOTEIO;
+       switch (state) {
+       case LEDST_OFF:
+       case LEDST_ON:
+               break;
+       default:
+               return -ENOSYS;
+       }
 
        return dm_gpio_set_value(&priv->gpio, state);
 }
 
+static enum led_state_t gpio_led_get_state(struct udevice *dev)
+{
+       struct led_gpio_priv *priv = dev_get_priv(dev);
+       int ret;
+
+       if (!dm_gpio_is_valid(&priv->gpio))
+               return -EREMOTEIO;
+       ret = dm_gpio_get_value(&priv->gpio);
+       if (ret < 0)
+               return ret;
+
+       return ret ? LEDST_ON : LEDST_OFF;
+}
+
 static int led_gpio_probe(struct udevice *dev)
 {
        struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
@@ -88,6 +109,7 @@ static int led_gpio_bind(struct udevice *parent)
 
 static const struct led_ops gpio_led_ops = {
        .set_state      = gpio_led_set_state,
+       .get_state      = gpio_led_get_state,
 };
 
 static const struct udevice_id led_gpio_ids[] = {
index 8af87ea8ea8c68a45b420dec1d18643724dd3087..bbab4d14c9856025cd06a0fa1dcd9ca6357064d6 100644 (file)
@@ -33,6 +33,14 @@ struct led_ops {
         * @return 0 if OK, -ve on error
         */
        int (*set_state)(struct udevice *dev, enum led_state_t state);
+
+       /**
+        * led_get_state() - get the state of an LED
+        *
+        * @dev:        LED device to change
+        * @return LED state led_state_t, or -ve on error
+        */
+       enum led_state_t (*get_state)(struct udevice *dev);
 };
 
 #define led_get_ops(dev)       ((struct led_ops *)(dev)->driver->ops)
@@ -55,4 +63,12 @@ int led_get_by_label(const char *label, struct udevice **devp);
  */
 int led_set_state(struct udevice *dev, enum led_state_t state);
 
+/**
+ * led_get_state() - get the state of an LED
+ *
+ * @dev:       LED device to change
+ * @return LED state led_state_t, or -ve on error
+ */
+enum led_state_t led_get_state(struct udevice *dev);
+
 #endif
index ebb9b46584865cacc9e72cbf1b5843ef7958753a..68aa39bd4d0809ae08761f2dd4a0b80d4306545c 100644 (file)
@@ -43,9 +43,11 @@ static int dm_test_led_gpio(struct unit_test_state *uts)
        ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
        ut_assertok(led_set_state(dev, LEDST_ON));
        ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
+       ut_asserteq(LEDST_ON, led_get_state(dev));
 
        ut_assertok(led_set_state(dev, LEDST_OFF));
        ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
+       ut_asserteq(LEDST_OFF, led_get_state(dev));
 
        return 0;
 }