dm: led: Support toggling LEDs
authorSimon Glass <sjg@chromium.org>
Mon, 10 Apr 2017 17:34:56 +0000 (11:34 -0600)
committerSimon Glass <sjg@chromium.org>
Sat, 15 Apr 2017 01:38:57 +0000 (19:38 -0600)
Add support for toggling an LED into the uclass interface. This can be
efficiently implemented by the driver.

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

index 789d15600fdc1092e66c8279e54b8d6288ee4543..4106ecb6799b163bd8eec2b6dc560c1ebeb89cec 100644 (file)
@@ -21,6 +21,7 @@ struct led_gpio_priv {
 static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
 {
        struct led_gpio_priv *priv = dev_get_priv(dev);
+       int ret;
 
        if (!dm_gpio_is_valid(&priv->gpio))
                return -EREMOTEIO;
@@ -28,6 +29,12 @@ static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
        case LEDST_OFF:
        case LEDST_ON:
                break;
+       case LEDST_TOGGLE:
+               ret = dm_gpio_get_value(&priv->gpio);
+               if (ret < 0)
+                       return ret;
+               state = !ret;
+               break;
        default:
                return -ENOSYS;
        }
index bbab4d14c9856025cd06a0fa1dcd9ca6357064d6..8c107e28e78e89d98c3a5cdc786016d4628bb118 100644 (file)
@@ -20,6 +20,7 @@ struct led_uc_plat {
 enum led_state_t {
        LEDST_OFF = 0,
        LEDST_ON = 1,
+       LEDST_TOGGLE,
 
        LEDST_COUNT,
 };
index 68aa39bd4d0809ae08761f2dd4a0b80d4306545c..2cc24127e2ad7fe288a20456b090ab3e97489acb 100644 (file)
@@ -53,6 +53,31 @@ static int dm_test_led_gpio(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_led_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
 
+/* Test that we can toggle LEDs */
+static int dm_test_led_toggle(struct unit_test_state *uts)
+{
+       const int offset = 1;
+       struct udevice *dev, *gpio;
+
+       /*
+        * Check that we can manipulate an LED. LED 1 is connected to GPIO
+        * bank gpio_a, offset 1.
+        */
+       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_assertok(led_set_state(dev, LEDST_TOGGLE));
+       ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
+       ut_asserteq(LEDST_ON, led_get_state(dev));
+
+       ut_assertok(led_set_state(dev, LEDST_TOGGLE));
+       ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
+       ut_asserteq(LEDST_OFF, led_get_state(dev));
+
+       return 0;
+}
+DM_TEST(dm_test_led_toggle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
 /* Test obtaining an LED by label */
 static int dm_test_led_label(struct unit_test_state *uts)
 {