@@ -187,9 +187,10 @@ static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
187187 * This optional routine sets the brightness of a LED to the given value.
188188 * Calling this function after led_blink() won't affect blinking.
189189 *
190- * LEDs which can only be turned on or off may provide this function.
190+ * LEDs which can only be turned on or off do not need to provide this
191+ * function.
191192 * These should simply turn the LED on if @p value is nonzero, and off
192- * if @p value is zero.
193+ * if @p value is zero using the on/off APIs automatically .
193194 *
194195 * @param dev LED device
195196 * @param led LED number
@@ -206,14 +207,23 @@ static inline int z_impl_led_set_brightness(const struct device *dev,
206207 const struct led_driver_api * api =
207208 (const struct led_driver_api * )dev -> api ;
208209
209- if (api -> set_brightness == NULL ) {
210+ if (api -> set_brightness == NULL &&
211+ api -> on == NULL && api -> off == NULL ) {
210212 return - ENOSYS ;
211213 }
212214
213215 if (value > LED_BRIGTHNESS_MAX ) {
214216 return - EINVAL ;
215217 }
216218
219+ if (api -> set_brightness == NULL ) {
220+ if (value ) {
221+ return api -> on (dev , led );
222+ } else {
223+ return api -> off (dev , led );
224+ }
225+ }
226+
217227 return api -> set_brightness (dev , led , value );
218228}
219229
@@ -307,6 +317,9 @@ static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
307317 *
308318 * This routine turns on an LED
309319 *
320+ * LEDs which implements brightness control do not need to implement this, the
321+ * set_brightness API is used automatically.
322+ *
310323 * @param dev LED device
311324 * @param led LED number
312325 * @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)
318331 const struct led_driver_api * api =
319332 (const struct led_driver_api * )dev -> api ;
320333
334+ if (api -> set_brightness == NULL && api -> on == NULL ) {
335+ return - ENOSYS ;
336+ }
337+
338+ if (api -> on == NULL ) {
339+ return api -> set_brightness (dev , led , LED_BRIGTHNESS_MAX );
340+ }
341+
321342 return api -> on (dev , led );
322343}
323344
@@ -326,6 +347,9 @@ static inline int z_impl_led_on(const struct device *dev, uint32_t led)
326347 *
327348 * This routine turns off an LED
328349 *
350+ * LEDs which implements brightness control do not need to implement this, the
351+ * set_brightness API is used automatically.
352+ *
329353 * @param dev LED device
330354 * @param led LED number
331355 * @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)
337361 const struct led_driver_api * api =
338362 (const struct led_driver_api * )dev -> api ;
339363
364+ if (api -> set_brightness == NULL && api -> off == NULL ) {
365+ return - ENOSYS ;
366+ }
367+
368+ if (api -> off == NULL ) {
369+ return api -> set_brightness (dev , led , 0 );
370+ }
371+
340372 return api -> off (dev , led );
341373}
342374
0 commit comments