Commit ddae9fcddc48d1e624c941148d0df5a4fc7d7d5c

Authored by Simon Glass
1 parent 56e19871dc

dm: led: Adjust the LED uclass

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>
Reviewed-by: Ziping Chen <techping.chan@gmail.com>

Showing 4 changed files with 22 additions and 14 deletions Side-by-side Diff

drivers/led/led-uclass.c
... ... @@ -32,14 +32,14 @@
32 32 return -ENODEV;
33 33 }
34 34  
35   -int led_set_on(struct udevice *dev, int on)
  35 +int led_set_state(struct udevice *dev, enum led_state_t state)
36 36 {
37 37 struct led_ops *ops = led_get_ops(dev);
38 38  
39   - if (!ops->set_on)
  39 + if (!ops->set_state)
40 40 return -ENOSYS;
41 41  
42   - return ops->set_on(dev, on);
  42 + return ops->set_state(dev, state);
43 43 }
44 44  
45 45 UCLASS_DRIVER(led) = {
drivers/led/led_gpio.c
... ... @@ -18,14 +18,14 @@
18 18 struct gpio_desc gpio;
19 19 };
20 20  
21   -static int gpio_led_set_on(struct udevice *dev, int on)
  21 +static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
22 22 {
23 23 struct led_gpio_priv *priv = dev_get_priv(dev);
24 24  
25 25 if (!dm_gpio_is_valid(&priv->gpio))
26 26 return -EREMOTEIO;
27 27  
28   - return dm_gpio_set_value(&priv->gpio, on);
  28 + return dm_gpio_set_value(&priv->gpio, state);
29 29 }
30 30  
31 31 static int led_gpio_probe(struct udevice *dev)
... ... @@ -87,7 +87,7 @@
87 87 }
88 88  
89 89 static const struct led_ops gpio_led_ops = {
90   - .set_on = gpio_led_set_on,
  90 + .set_state = gpio_led_set_state,
91 91 };
92 92  
93 93 static const struct udevice_id led_gpio_ids[] = {
... ... @@ -17,15 +17,22 @@
17 17 const char *label;
18 18 };
19 19  
  20 +enum led_state_t {
  21 + LEDST_OFF = 0,
  22 + LEDST_ON = 1,
  23 +
  24 + LEDST_COUNT,
  25 +};
  26 +
20 27 struct led_ops {
21 28 /**
22   - * set_on() - set the state of an LED
  29 + * set_state() - set the state of an LED
23 30 *
24 31 * @dev: LED device to change
25   - * @on: 1 to turn the LED on, 0 to turn it off
  32 + * @state: LED state to set
26 33 * @return 0 if OK, -ve on error
27 34 */
28   - int (*set_on)(struct udevice *dev, int on);
  35 + int (*set_state)(struct udevice *dev, enum led_state_t state);
29 36 };
30 37  
31 38 #define led_get_ops(dev) ((struct led_ops *)(dev)->driver->ops)
32 39  
33 40  
... ... @@ -40,13 +47,13 @@
40 47 int led_get_by_label(const char *label, struct udevice **devp);
41 48  
42 49 /**
43   - * led_set_on() - set the state of an LED
  50 + * led_set_state() - set the state of an LED
44 51 *
45 52 * @dev: LED device to change
46   - * @on: 1 to turn the LED on, 0 to turn it off
  53 + * @state: LED state to set
47 54 * @return 0 if OK, -ve on error
48 55 */
49   -int led_set_on(struct udevice *dev, int on);
  56 +int led_set_state(struct udevice *dev, enum led_state_t state);
50 57  
51 58 #endif
... ... @@ -41,9 +41,10 @@
41 41 ut_assertok(uclass_get_device(UCLASS_LED, 1, &dev));
42 42 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
43 43 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
44   - led_set_on(dev, 1);
  44 + ut_assertok(led_set_state(dev, LEDST_ON));
45 45 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
46   - led_set_on(dev, 0);
  46 +
  47 + ut_assertok(led_set_state(dev, LEDST_OFF));
47 48 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
48 49  
49 50 return 0;