diff mbox

[U-Boot,v2,4/9] dm: led: Adjust the LED uclass

Message ID 20170410173459.30461-5-sjg@chromium.org
State Accepted
Commit ddae9fcddc48d1e624c941148d0df5a4fc7d7d5c
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass April 10, 2017, 5:34 p.m. UTC
At present this is very simple, supporting only on and off. We want to
also support toggling and blinking. As a first step, change the name of
the main method and use an enum to indicate the state.

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

Changes in v2: None

 drivers/led/led-uclass.c |  6 +++---
 drivers/led/led_gpio.c   |  6 +++---
 include/led.h            | 19 +++++++++++++------
 test/dm/led.c            |  5 +++--
 4 files changed, 22 insertions(+), 14 deletions(-)

Comments

techping.chan@gmail.com April 12, 2017, 8:52 a.m. UTC | #1
2017-04-11 1:34 GMT+08:00 Simon Glass <sjg@chromium.org>:

> At present this is very simple, supporting only on and off. We want to
> also support toggling and blinking. As a first step, change the name of
> the main method and use an enum to indicate the state.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> Changes in v2: None
>
>  drivers/led/led-uclass.c |  6 +++---
>  drivers/led/led_gpio.c   |  6 +++---
>  include/led.h            | 19 +++++++++++++------
>  test/dm/led.c            |  5 +++--
>  4 files changed, 22 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
> index ca4f98c0b3..b30346913b 100644
> --- a/drivers/led/led-uclass.c
> +++ b/drivers/led/led-uclass.c
> @@ -32,14 +32,14 @@ int led_get_by_label(const char *label, struct udevice
> **devp)
>         return -ENODEV;
>  }
>
> -int led_set_on(struct udevice *dev, int on)
> +int led_set_state(struct udevice *dev, enum led_state_t state)
>  {
>         struct led_ops *ops = led_get_ops(dev);
>
> -       if (!ops->set_on)
> +       if (!ops->set_state)
>                 return -ENOSYS;
>
> -       return ops->set_on(dev, on);
> +       return ops->set_state(dev, state);
>  }
>
>  UCLASS_DRIVER(led) = {
> diff --git a/drivers/led/led_gpio.c b/drivers/led/led_gpio.c
> index 97b5da35cd..af8133d3c7 100644
> --- a/drivers/led/led_gpio.c
> +++ b/drivers/led/led_gpio.c
> @@ -18,14 +18,14 @@ struct led_gpio_priv {
>         struct gpio_desc gpio;
>  };
>
> -static int gpio_led_set_on(struct udevice *dev, int on)
> +static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
>  {
>         struct led_gpio_priv *priv = dev_get_priv(dev);
>
>         if (!dm_gpio_is_valid(&priv->gpio))
>                 return -EREMOTEIO;
>
> -       return dm_gpio_set_value(&priv->gpio, on);
> +       return dm_gpio_set_value(&priv->gpio, state);
>  }
>
>  static int led_gpio_probe(struct udevice *dev)
> @@ -87,7 +87,7 @@ static int led_gpio_bind(struct udevice *parent)
>  }
>
>  static const struct led_ops gpio_led_ops = {
> -       .set_on         = gpio_led_set_on,
> +       .set_state      = gpio_led_set_state,
>  };
>
>  static const struct udevice_id led_gpio_ids[] = {
> diff --git a/include/led.h b/include/led.h
> index a856b3d9ff..8af87ea8ea 100644
> --- a/include/led.h
> +++ b/include/led.h
> @@ -17,15 +17,22 @@ struct led_uc_plat {
>         const char *label;
>  };
>
> +enum led_state_t {
> +       LEDST_OFF = 0,
> +       LEDST_ON = 1,
> +
> +       LEDST_COUNT,
> +};
> +
>  struct led_ops {
>         /**
> -        * set_on() - set the state of an LED
> +        * set_state() - set the state of an LED
>          *
>          * @dev:        LED device to change
> -        * @on:         1 to turn the LED on, 0 to turn it off
> +        * @state:      LED state to set
>          * @return 0 if OK, -ve on error
>          */
> -       int (*set_on)(struct udevice *dev, int on);
> +       int (*set_state)(struct udevice *dev, enum led_state_t state);
>  };
>
>  #define led_get_ops(dev)       ((struct led_ops *)(dev)->driver->ops)
> @@ -40,12 +47,12 @@ struct led_ops {
>  int led_get_by_label(const char *label, struct udevice **devp);
>
>  /**
> - * led_set_on() - set the state of an LED
> + * led_set_state() - set the state of an LED
>   *
>   * @dev:       LED device to change
> - * @on:                1 to turn the LED on, 0 to turn it off
> + * @state:     LED state to set
>   * @return 0 if OK, -ve on error
>   */
> -int led_set_on(struct udevice *dev, int on);
> +int led_set_state(struct udevice *dev, enum led_state_t state);
>
>  #endif
> diff --git a/test/dm/led.c b/test/dm/led.c
> index 8ee075cf1c..ebb9b46584 100644
> --- a/test/dm/led.c
> +++ b/test/dm/led.c
> @@ -41,9 +41,10 @@ static int dm_test_led_gpio(struct unit_test_state *uts)
>         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));
> -       led_set_on(dev, 1);
> +       ut_assertok(led_set_state(dev, LEDST_ON));
>         ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
> -       led_set_on(dev, 0);
> +
> +       ut_assertok(led_set_state(dev, LEDST_OFF));
>         ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
>
>         return 0;
> --
> 2.12.2.715.g7642488e1d-goog
>
>
Reviewed-by: Ziping Chen <techping.chan@gmail.com>
Simon Glass April 15, 2017, 4:07 p.m. UTC | #2
On 12 April 2017 at 02:52, Ziping Chen <techping.chan@gmail.com> wrote:
>
>
> 2017-04-11 1:34 GMT+08:00 Simon Glass <sjg@chromium.org>:
>>
>> At present this is very simple, supporting only on and off. We want to
>> also support toggling and blinking. As a first step, change the name of
>> the main method and use an enum to indicate the state.
>>
>> Signed-off-by: Simon Glass <sjg@chromium.org>
>> ---
>>
>> Changes in v2: None
>>
>>  drivers/led/led-uclass.c |  6 +++---
>>  drivers/led/led_gpio.c   |  6 +++---
>>  include/led.h            | 19 +++++++++++++------
>>  test/dm/led.c            |  5 +++--
>>  4 files changed, 22 insertions(+), 14 deletions(-)
>>
>
> Reviewed-by: Ziping Chen <techping.chan@gmail.com>

Applied to u-boot-dm
diff mbox

Patch

diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
index ca4f98c0b3..b30346913b 100644
--- a/drivers/led/led-uclass.c
+++ b/drivers/led/led-uclass.c
@@ -32,14 +32,14 @@  int led_get_by_label(const char *label, struct udevice **devp)
 	return -ENODEV;
 }
 
-int led_set_on(struct udevice *dev, int on)
+int led_set_state(struct udevice *dev, enum led_state_t state)
 {
 	struct led_ops *ops = led_get_ops(dev);
 
-	if (!ops->set_on)
+	if (!ops->set_state)
 		return -ENOSYS;
 
-	return ops->set_on(dev, on);
+	return ops->set_state(dev, state);
 }
 
 UCLASS_DRIVER(led) = {
diff --git a/drivers/led/led_gpio.c b/drivers/led/led_gpio.c
index 97b5da35cd..af8133d3c7 100644
--- a/drivers/led/led_gpio.c
+++ b/drivers/led/led_gpio.c
@@ -18,14 +18,14 @@  struct led_gpio_priv {
 	struct gpio_desc gpio;
 };
 
-static int gpio_led_set_on(struct udevice *dev, int on)
+static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
 {
 	struct led_gpio_priv *priv = dev_get_priv(dev);
 
 	if (!dm_gpio_is_valid(&priv->gpio))
 		return -EREMOTEIO;
 
-	return dm_gpio_set_value(&priv->gpio, on);
+	return dm_gpio_set_value(&priv->gpio, state);
 }
 
 static int led_gpio_probe(struct udevice *dev)
@@ -87,7 +87,7 @@  static int led_gpio_bind(struct udevice *parent)
 }
 
 static const struct led_ops gpio_led_ops = {
-	.set_on		= gpio_led_set_on,
+	.set_state	= gpio_led_set_state,
 };
 
 static const struct udevice_id led_gpio_ids[] = {
diff --git a/include/led.h b/include/led.h
index a856b3d9ff..8af87ea8ea 100644
--- a/include/led.h
+++ b/include/led.h
@@ -17,15 +17,22 @@  struct led_uc_plat {
 	const char *label;
 };
 
+enum led_state_t {
+	LEDST_OFF = 0,
+	LEDST_ON = 1,
+
+	LEDST_COUNT,
+};
+
 struct led_ops {
 	/**
-	 * set_on() - set the state of an LED
+	 * set_state() - set the state of an LED
 	 *
 	 * @dev:	LED device to change
-	 * @on:		1 to turn the LED on, 0 to turn it off
+	 * @state:	LED state to set
 	 * @return 0 if OK, -ve on error
 	 */
-	int (*set_on)(struct udevice *dev, int on);
+	int (*set_state)(struct udevice *dev, enum led_state_t state);
 };
 
 #define led_get_ops(dev)	((struct led_ops *)(dev)->driver->ops)
@@ -40,12 +47,12 @@  struct led_ops {
 int led_get_by_label(const char *label, struct udevice **devp);
 
 /**
- * led_set_on() - set the state of an LED
+ * led_set_state() - set the state of an LED
  *
  * @dev:	LED device to change
- * @on:		1 to turn the LED on, 0 to turn it off
+ * @state:	LED state to set
  * @return 0 if OK, -ve on error
  */
-int led_set_on(struct udevice *dev, int on);
+int led_set_state(struct udevice *dev, enum led_state_t state);
 
 #endif
diff --git a/test/dm/led.c b/test/dm/led.c
index 8ee075cf1c..ebb9b46584 100644
--- a/test/dm/led.c
+++ b/test/dm/led.c
@@ -41,9 +41,10 @@  static int dm_test_led_gpio(struct unit_test_state *uts)
 	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));
-	led_set_on(dev, 1);
+	ut_assertok(led_set_state(dev, LEDST_ON));
 	ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
-	led_set_on(dev, 0);
+
+	ut_assertok(led_set_state(dev, LEDST_OFF));
 	ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
 
 	return 0;