1 /*
2  * Copyright (c) 2018 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public LED driver APIs
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_DRIVERS_LED_H_
13 #define ZEPHYR_INCLUDE_DRIVERS_LED_H_
14 
15 /**
16  * @brief LED Interface
17  * @defgroup led_interface LED Interface
18  * @since 1.12
19  * @version 1.0.0
20  * @ingroup io_interfaces
21  * @{
22  */
23 
24 #include <errno.h>
25 
26 #include <zephyr/types.h>
27 #include <zephyr/device.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34  * @brief LED information structure
35  *
36  * This structure gathers useful information about LED controller.
37  */
38 struct led_info {
39 	/** LED label */
40 	const char *label;
41 	/** Index of the LED on the controller */
42 	uint32_t index;
43 	/** Number of colors per LED */
44 	uint8_t num_colors;
45 	/** Mapping of the LED colors */
46 	const uint8_t *color_mapping;
47 };
48 
49 /**
50  * @typedef led_api_blink()
51  * @brief Callback API for blinking an LED
52  *
53  * @see led_blink() for argument descriptions.
54  */
55 typedef int (*led_api_blink)(const struct device *dev, uint32_t led,
56 			     uint32_t delay_on, uint32_t delay_off);
57 
58 /**
59  * @typedef led_api_get_info()
60  * @brief Optional API callback to get LED information
61  *
62  * @see led_get_info() for argument descriptions.
63  */
64 typedef int (*led_api_get_info)(const struct device *dev, uint32_t led,
65 				const struct led_info **info);
66 
67 /**
68  * @typedef led_api_set_brightness()
69  * @brief Callback API for setting brightness of an LED
70  *
71  * @see led_set_brightness() for argument descriptions.
72  */
73 typedef int (*led_api_set_brightness)(const struct device *dev, uint32_t led,
74 				      uint8_t value);
75 /**
76  * @typedef led_api_set_color()
77  * @brief Optional API callback to set the colors of a LED.
78  *
79  * @see led_set_color() for argument descriptions.
80  */
81 typedef int (*led_api_set_color)(const struct device *dev, uint32_t led,
82 				 uint8_t num_colors, const uint8_t *color);
83 
84 /**
85  * @typedef led_api_on()
86  * @brief Callback API for turning on an LED
87  *
88  * @see led_on() for argument descriptions.
89  */
90 typedef int (*led_api_on)(const struct device *dev, uint32_t led);
91 
92 /**
93  * @typedef led_api_off()
94  * @brief Callback API for turning off an LED
95  *
96  * @see led_off() for argument descriptions.
97  */
98 typedef int (*led_api_off)(const struct device *dev, uint32_t led);
99 
100 /**
101  * @typedef led_api_write_channels()
102  * @brief Callback API for writing a strip of LED channels
103  *
104  * @see led_api_write_channels() for arguments descriptions.
105  */
106 typedef int (*led_api_write_channels)(const struct device *dev,
107 				      uint32_t start_channel,
108 				      uint32_t num_channels,
109 				      const uint8_t *buf);
110 
111 /**
112  * @brief LED driver API
113  */
114 __subsystem struct led_driver_api {
115 	/* Mandatory callbacks. */
116 	led_api_on on;
117 	led_api_off off;
118 	/* Optional callbacks. */
119 	led_api_blink blink;
120 	led_api_get_info get_info;
121 	led_api_set_brightness set_brightness;
122 	led_api_set_color set_color;
123 	led_api_write_channels write_channels;
124 };
125 
126 /**
127  * @brief Blink an LED
128  *
129  * This optional routine starts blinking a LED forever with the given time
130  * period.
131  *
132  * @param dev LED device
133  * @param led LED number
134  * @param delay_on Time period (in milliseconds) an LED should be ON
135  * @param delay_off Time period (in milliseconds) an LED should be OFF
136  * @return 0 on success, negative on error
137  */
138 __syscall int led_blink(const struct device *dev, uint32_t led,
139 			    uint32_t delay_on, uint32_t delay_off);
140 
z_impl_led_blink(const struct device * dev,uint32_t led,uint32_t delay_on,uint32_t delay_off)141 static inline int z_impl_led_blink(const struct device *dev, uint32_t led,
142 				   uint32_t delay_on, uint32_t delay_off)
143 {
144 	const struct led_driver_api *api =
145 		(const struct led_driver_api *)dev->api;
146 
147 	if (api->blink == NULL) {
148 		return -ENOSYS;
149 	}
150 	return api->blink(dev, led, delay_on, delay_off);
151 }
152 
153 /**
154  * @brief Get LED information
155  *
156  * This optional routine provides information about a LED.
157  *
158  * @param dev LED device
159  * @param led LED number
160  * @param info Pointer to a pointer filled with LED information
161  * @return 0 on success, negative on error
162  */
163 __syscall int led_get_info(const struct device *dev, uint32_t led,
164 			   const struct led_info **info);
165 
z_impl_led_get_info(const struct device * dev,uint32_t led,const struct led_info ** info)166 static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
167 				      const struct led_info **info)
168 {
169 	const struct led_driver_api *api =
170 		(const struct led_driver_api *)dev->api;
171 
172 	if (api->get_info == NULL) {
173 		*info = NULL;
174 		return -ENOSYS;
175 	}
176 	return api->get_info(dev, led, info);
177 }
178 
179 /**
180  * @brief Set LED brightness
181  *
182  * This optional routine sets the brightness of a LED to the given value.
183  * Calling this function after led_blink() won't affect blinking.
184  *
185  * LEDs which can only be turned on or off may provide this function.
186  * These should simply turn the LED on if @p value is nonzero, and off
187  * if @p value is zero.
188  *
189  * @param dev LED device
190  * @param led LED number
191  * @param value Brightness value to set in percent
192  * @return 0 on success, negative on error
193  */
194 __syscall int led_set_brightness(const struct device *dev, uint32_t led,
195 				     uint8_t value);
196 
z_impl_led_set_brightness(const struct device * dev,uint32_t led,uint8_t value)197 static inline int z_impl_led_set_brightness(const struct device *dev,
198 					    uint32_t led,
199 					    uint8_t value)
200 {
201 	const struct led_driver_api *api =
202 		(const struct led_driver_api *)dev->api;
203 
204 	if (api->set_brightness == NULL) {
205 		return -ENOSYS;
206 	}
207 	return api->set_brightness(dev, led, value);
208 }
209 
210 /**
211  * @brief Write/update a strip of LED channels
212  *
213  * This optional routine writes a strip of LED channels to the given array of
214  * levels. Therefore it can be used to configure several LEDs at the same time.
215  *
216  * Calling this function after led_blink() won't affect blinking.
217  *
218  * @param dev LED device
219  * @param start_channel Absolute number (i.e. not relative to a LED) of the
220  *        first channel to update.
221  * @param num_channels The number of channels to write/update.
222  * @param buf array of values to configure the channels with. num_channels
223  *        entries must be provided.
224  * @return 0 on success, negative on error
225  */
226 __syscall int led_write_channels(const struct device *dev,
227 				 uint32_t start_channel,
228 				 uint32_t num_channels, const uint8_t *buf);
229 
230 static inline int
z_impl_led_write_channels(const struct device * dev,uint32_t start_channel,uint32_t num_channels,const uint8_t * buf)231 z_impl_led_write_channels(const struct device *dev, uint32_t start_channel,
232 			  uint32_t num_channels, const uint8_t *buf)
233 {
234 	const struct led_driver_api *api =
235 		(const struct led_driver_api *)dev->api;
236 
237 	if (api->write_channels == NULL) {
238 		return -ENOSYS;
239 	}
240 	return api->write_channels(dev, start_channel, num_channels, buf);
241 }
242 
243 /**
244  * @brief Set a single LED channel
245  *
246  * This optional routine sets a single LED channel to the given value.
247  *
248  * Calling this function after led_blink() won't affect blinking.
249  *
250  * @param dev LED device
251  * @param channel Absolute channel number (i.e. not relative to a LED)
252  * @param value Value to configure the channel with
253  * @return 0 on success, negative on error
254  */
255 __syscall int led_set_channel(const struct device *dev,
256 			      uint32_t channel, uint8_t value);
257 
z_impl_led_set_channel(const struct device * dev,uint32_t channel,uint8_t value)258 static inline int z_impl_led_set_channel(const struct device *dev,
259 					 uint32_t channel, uint8_t value)
260 {
261 	return z_impl_led_write_channels(dev, channel, 1, &value);
262 }
263 
264 /**
265  * @brief Set LED color
266  *
267  * This routine configures all the color channels of a LED with the given
268  * color array.
269  *
270  * Calling this function after led_blink() won't affect blinking.
271  *
272  * @param dev LED device
273  * @param led LED number
274  * @param num_colors Number of colors in the array.
275  * @param color Array of colors. It must be ordered following the color
276  *        mapping of the LED controller. See the color_mapping member
277  *        in struct led_info.
278  * @return 0 on success, negative on error
279  */
280 __syscall int led_set_color(const struct device *dev, uint32_t led,
281 			    uint8_t num_colors, const uint8_t *color);
282 
z_impl_led_set_color(const struct device * dev,uint32_t led,uint8_t num_colors,const uint8_t * color)283 static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
284 				       uint8_t num_colors, const uint8_t *color)
285 {
286 	const struct led_driver_api *api =
287 		(const struct led_driver_api *)dev->api;
288 
289 	if (api->set_color == NULL) {
290 		return -ENOSYS;
291 	}
292 	return api->set_color(dev, led, num_colors, color);
293 }
294 
295 /**
296  * @brief Turn on an LED
297  *
298  * This routine turns on an LED
299  *
300  * @param dev LED device
301  * @param led LED number
302  * @return 0 on success, negative on error
303  */
304 __syscall int led_on(const struct device *dev, uint32_t led);
305 
z_impl_led_on(const struct device * dev,uint32_t led)306 static inline int z_impl_led_on(const struct device *dev, uint32_t led)
307 {
308 	const struct led_driver_api *api =
309 		(const struct led_driver_api *)dev->api;
310 
311 	return api->on(dev, led);
312 }
313 
314 /**
315  * @brief Turn off an LED
316  *
317  * This routine turns off an LED
318  *
319  * @param dev LED device
320  * @param led LED number
321  * @return 0 on success, negative on error
322  */
323 __syscall int led_off(const struct device *dev, uint32_t led);
324 
z_impl_led_off(const struct device * dev,uint32_t led)325 static inline int z_impl_led_off(const struct device *dev, uint32_t led)
326 {
327 	const struct led_driver_api *api =
328 		(const struct led_driver_api *)dev->api;
329 
330 	return api->off(dev, led);
331 }
332 
333 /**
334  * @}
335  */
336 
337 #ifdef __cplusplus
338 }
339 #endif
340 
341 #include <zephyr/syscalls/led.h>
342 
343 #endif	/* ZEPHYR_INCLUDE_DRIVERS_LED_H_ */
344