Commit 2f0d1417 authored by Fabio Baltieri's avatar Fabio Baltieri Committed by Benjamin Cabé
Browse files

led: fallback from set_brightness to on/off and vice versa automatically

Change the on/off/set_brightness API to automatically use whatever
function is available, this allows dimmable drivers to not implement the
on/off wrapper and vice versa.

Signed-off-by: default avatarFabio Baltieri <fabiobaltieri@google.com>
parent 62c1fb20
Loading
Loading
Loading
Loading
+35 −3
Changes for include/zephyr/drivers/led.h: 35 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -187,9 +187,10 @@ static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
 * This optional routine sets the brightness of a LED to the given value.
 * Calling this function after led_blink() won't affect blinking.
 *
 * LEDs which can only be turned on or off may provide this function.
 * LEDs which can only be turned on or off do not need to provide this
 * function.
 * These should simply turn the LED on if @p value is nonzero, and off
 * if @p value is zero.
 * if @p value is zero using the on/off APIs automatically.
 *
 * @param dev LED device
 * @param led LED number
@@ -206,7 +207,8 @@ static inline int z_impl_led_set_brightness(const struct device *dev,
	const struct led_driver_api *api =
		(const struct led_driver_api *)dev->api;

	if (api->set_brightness == NULL) {
	if (api->set_brightness == NULL &&
	    api->on == NULL && api->off == NULL) {
		return -ENOSYS;
	}

@@ -214,6 +216,14 @@ static inline int z_impl_led_set_brightness(const struct device *dev,
		return -EINVAL;
	}

	if (api->set_brightness == NULL) {
		if (value) {
			return api->on(dev, led);
		} else {
			return api->off(dev, led);
		}
	}

	return api->set_brightness(dev, led, value);
}

@@ -307,6 +317,9 @@ static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
 *
 * This routine turns on an LED
 *
 * LEDs which implements brightness control do not need to implement this, the
 * set_brightness API is used automatically.
 *
 * @param dev LED device
 * @param led LED number
 * @return 0 on success, negative on error
@@ -318,6 +331,14 @@ static inline int z_impl_led_on(const struct device *dev, uint32_t led)
	const struct led_driver_api *api =
		(const struct led_driver_api *)dev->api;

	if (api->set_brightness == NULL && api->on == NULL) {
		return -ENOSYS;
	}

	if (api->on == NULL) {
		return api->set_brightness(dev, led, LED_BRIGTHNESS_MAX);
	}

	return api->on(dev, led);
}

@@ -326,6 +347,9 @@ static inline int z_impl_led_on(const struct device *dev, uint32_t led)
 *
 * This routine turns off an LED
 *
 * LEDs which implements brightness control do not need to implement this, the
 * set_brightness API is used automatically.
 *
 * @param dev LED device
 * @param led LED number
 * @return 0 on success, negative on error
@@ -337,6 +361,14 @@ static inline int z_impl_led_off(const struct device *dev, uint32_t led)
	const struct led_driver_api *api =
		(const struct led_driver_api *)dev->api;

	if (api->set_brightness == NULL && api->off == NULL) {
		return -ENOSYS;
	}

	if (api->off == NULL) {
		return api->set_brightness(dev, led, 0);
	}

	return api->off(dev, led);
}