diff options
author | Simon Glass <sjg@chromium.org> | 2017-04-10 11:34:55 -0600 |
---|---|---|
committer | Simon Glass <sjg@chromium.org> | 2017-04-14 19:38:57 -0600 |
commit | 8f4b612333ee0381eedf767c1c005a830886df27 (patch) | |
tree | c687de5e960d0bb4c1de07f1b322119e46859349 /drivers/led | |
parent | ddae9fcddc48d1e624c941148d0df5a4fc7d7d5c (diff) |
dm: led: Add support for getting the state of an LED
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>
Diffstat (limited to 'drivers/led')
-rw-r--r-- | drivers/led/led-uclass.c | 10 | ||||
-rw-r--r-- | drivers/led/led_gpio.c | 22 |
2 files changed, 32 insertions, 0 deletions
diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c index b30346913b..ea5fbabadf 100644 --- a/drivers/led/led-uclass.c +++ b/drivers/led/led-uclass.c @@ -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", diff --git a/drivers/led/led_gpio.c b/drivers/led/led_gpio.c index af8133d3c7..789d15600f 100644 --- a/drivers/led/led_gpio.c +++ b/drivers/led/led_gpio.c @@ -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[] = { |