1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2015 Google, Inc
12 #include <dm/ofnode.h>
17 * Generic LED API provided when a supported compatible is defined in DeviceTree.
19 * To enable support for LEDs, enable the `CONFIG_LED` Kconfig option.
21 * The most common implementation is for GPIO-connected LEDs. If using GPIO-connected LEDs,
22 * enable the `LED_GPIO` Kconfig option.
24 * `LED_BLINK` support requires LED driver support and is therefore optional. If LED blink
25 * functionality is needed, enable the `LED_BLINK` Kconfig option. If LED driver doesn't
26 * support HW Blink, SW Blink can be used with the Cyclic framework by enabling the
27 * CONFIG_LED_SW_BLINK.
29 * Boot and Activity LEDs are also supported. These LEDs can signal various system operations
30 * during runtime, such as boot initialization, file transfers, and flash write/erase operations.
32 * To enable a Boot LED, enable `CONFIG_LED_BOOT` and define in `/options/u-boot` root node the
33 * property `boot-led`. This will enable the specified LED to blink and turn ON when
34 * the bootloader initializes correctly.
36 * To enable an Activity LED, enable `CONFIG_LED_ACTIVITY` and define in `/options/u-boot` root
37 * node the property `activity-led`.
38 * This will enable the specified LED to blink and turn ON during file transfers or flash
39 * write/erase operations.
41 * Both Boot and Activity LEDs provide a simple API to turn the LED ON or OFF:
42 * `led_boot_on()`, `led_boot_off()`, `led_activity_on()`, and `led_activity_off()`.
44 * Both configurations can optionally define a `boot/activity-led-period` property
45 * if `CONFIG_LED_BLINK` or `CONFIG_LED_SW_BLINK` is enabled for LED blink operations, which
46 * is usually used by the Activity LED. If not defined the default value of 250 (ms) is used.
48 * When `CONFIG_LED_BLINK` or `CONFIG_LED_SW_BLINK` is enabled, additional APIs are exposed:
49 * `led_boot_blink()` and `led_activity_blink()`. Note that if `CONFIG_LED_BLINK` or
50 * `CONFIG_LED_SW_BLINK` is disabled, these APIs will behave like the `led_boot_on()` and
51 * `led_activity_on()` APIs, respectively.
65 enum led_sw_blink_state_t {
66 LED_SW_BLINK_ST_DISABLED,
67 LED_SW_BLINK_ST_NOT_READY,
73 enum led_sw_blink_state_t state;
75 struct cyclic_info cyclic;
76 const char cyclic_name[0];
80 * struct led_uc_plat - Platform data the uclass stores about each device
83 * @default_state: LED default state
84 * @sw_blink: LED software blink struct
88 enum led_state_t default_state;
89 #ifdef CONFIG_LED_SW_BLINK
90 struct led_sw_blink *sw_blink;
95 * struct led_uc_priv - Private data the uclass stores about each device
97 * @boot_led_label: Boot LED label
98 * @activity_led_label: Activity LED label
99 * @boot_led_dev: Boot LED dev
100 * @activity_led_dev: Activity LED dev
101 * @boot_led_period: Boot LED blink period
102 * @activity_led_period: Activity LED blink period
105 #ifdef CONFIG_LED_BOOT
106 const char *boot_led_label;
109 #ifdef CONFIG_LED_ACTIVITY
110 const char *activity_led_label;
111 int activity_led_period;
117 * set_state() - set the state of an LED
119 * @dev: LED device to change
120 * @state: LED state to set
121 * @return 0 if OK, -ve on error
123 int (*set_state)(struct udevice *dev, enum led_state_t state);
126 * led_get_state() - get the state of an LED
128 * @dev: LED device to change
129 * @return LED state led_state_t, or -ve on error
131 enum led_state_t (*get_state)(struct udevice *dev);
133 #ifdef CONFIG_LED_BLINK
135 * led_set_period() - set the blink period of an LED
137 * Thie records the period if supported, or returns -ENOSYS if not.
138 * To start the LED blinking, use set_state().
140 * @dev: LED device to change
141 * @period_ms: LED blink period in milliseconds
142 * @return 0 if OK, -ve on error
144 int (*set_period)(struct udevice *dev, int period_ms);
148 #define led_get_ops(dev) ((struct led_ops *)(dev)->driver->ops)
151 * led_get_by_label() - Find an LED device by label
153 * @label: LED label to look up
154 * @devp: Returns the associated device, if found
155 * Return: 0 if found, -ENODEV if not found, other -ve on error
157 int led_get_by_label(const char *label, struct udevice **devp);
160 * led_set_state() - set the state of an LED
162 * @dev: LED device to change
163 * @state: LED state to set
164 * Return: 0 if OK, -ve on error
166 int led_set_state(struct udevice *dev, enum led_state_t state);
169 * led_get_state() - get the state of an LED
171 * @dev: LED device to change
172 * Return: LED state led_state_t, or -ve on error
174 enum led_state_t led_get_state(struct udevice *dev);
177 * led_set_period() - set the blink period of an LED
179 * @dev: LED device to change
180 * @period_ms: LED blink period in milliseconds
181 * Return: 0 if OK, -ve on error
183 int led_set_period(struct udevice *dev, int period_ms);
186 * led_bind_generic() - bind children of parent to given driver
188 * @parent: Top-level LED device
189 * @driver_name: Driver for handling individual child nodes
191 int led_bind_generic(struct udevice *parent, const char *driver_name);
193 /* Internal functions for software blinking. Do not use them in your code */
194 int led_sw_set_period(struct udevice *dev, int period_ms);
195 bool led_sw_is_blinking(struct udevice *dev);
196 bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state);
198 #ifdef CONFIG_LED_BOOT
201 * led_boot_on() - turn ON the designated LED for booting
203 * Return: 0 if OK, -ve on error
205 int led_boot_on(void);
208 * led_boot_off() - turn OFF the designated LED for booting
210 * Return: 0 if OK, -ve on error
212 int led_boot_off(void);
214 #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK)
216 * led_boot_blink() - turn ON the designated LED for booting
218 * Return: 0 if OK, -ve on error
220 int led_boot_blink(void);
223 /* If LED BLINK is not supported/enabled, fallback to LED ON */
224 #define led_boot_blink led_boot_on
227 static inline int led_boot_on(void)
232 static inline int led_boot_off(void)
237 static inline int led_boot_blink(void)
243 #ifdef CONFIG_LED_ACTIVITY
246 * led_activity_on() - turn ON the designated LED for activity
248 * Return: 0 if OK, -ve on error
250 int led_activity_on(void);
253 * led_activity_off() - turn OFF the designated LED for activity
255 * Return: 0 if OK, -ve on error
257 int led_activity_off(void);
259 #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK)
261 * led_activity_blink() - turn ON the designated LED for activity
263 * Return: 0 if OK, -ve on error
265 int led_activity_blink(void);
267 /* If LED BLINK is not supported/enabled, fallback to LED ON */
268 #define led_activity_blink led_activity_on
271 static inline int led_activity_on(void)
276 static inline int led_activity_off(void)
281 static inline int led_activity_blink(void)