Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __LED_H |
| 8 | #define __LED_H |
| 9 | |
Tom Rini | c88e90a | 2024-07-30 12:36:22 -0600 | [diff] [blame] | 10 | #include <stdbool.h> |
| 11 | #include <cyclic.h> |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 12 | #include <dm/ofnode.h> |
Tom Rini | c88e90a | 2024-07-30 12:36:22 -0600 | [diff] [blame] | 13 | |
Christian Marangi | 82b3245 | 2024-10-01 14:24:42 +0200 | [diff] [blame] | 14 | /** |
| 15 | * DOC: Overview |
| 16 | * |
| 17 | * Generic LED API provided when a supported compatible is defined in DeviceTree. |
| 18 | * |
| 19 | * To enable support for LEDs, enable the `CONFIG_LED` Kconfig option. |
| 20 | * |
| 21 | * The most common implementation is for GPIO-connected LEDs. If using GPIO-connected LEDs, |
| 22 | * enable the `LED_GPIO` Kconfig option. |
| 23 | * |
| 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. |
| 28 | * |
| 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. |
| 31 | * |
| 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. |
| 35 | * |
| 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. |
| 40 | * |
| 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()`. |
| 43 | * |
| 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. |
| 47 | * |
| 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. |
| 52 | */ |
| 53 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 54 | struct udevice; |
| 55 | |
Heiko Schocher | 350d252 | 2025-01-28 14:52:46 +0100 | [diff] [blame^] | 56 | /* |
| 57 | * value imported from linux:include/linux/uapi/linux/uleds.h |
| 58 | */ |
| 59 | #define LED_MAX_NAME_SIZE 64 |
| 60 | |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 61 | enum led_state_t { |
| 62 | LEDST_OFF = 0, |
| 63 | LEDST_ON = 1, |
| 64 | LEDST_TOGGLE, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 65 | LEDST_BLINK, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 66 | |
| 67 | LEDST_COUNT, |
| 68 | }; |
| 69 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 70 | enum led_sw_blink_state_t { |
| 71 | LED_SW_BLINK_ST_DISABLED, |
| 72 | LED_SW_BLINK_ST_NOT_READY, |
| 73 | LED_SW_BLINK_ST_OFF, |
| 74 | LED_SW_BLINK_ST_ON, |
| 75 | }; |
| 76 | |
| 77 | struct led_sw_blink { |
| 78 | enum led_sw_blink_state_t state; |
| 79 | struct udevice *dev; |
| 80 | struct cyclic_info cyclic; |
| 81 | const char cyclic_name[0]; |
| 82 | }; |
| 83 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 84 | /** |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 85 | * struct led_uc_plat - Platform data the uclass stores about each device |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 86 | * |
| 87 | * @label: LED label |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 88 | * @default_state: LED default state |
Christian Marangi | 82b3245 | 2024-10-01 14:24:42 +0200 | [diff] [blame] | 89 | * @sw_blink: LED software blink struct |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 90 | */ |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 91 | struct led_uc_plat { |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 92 | const char *label; |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 93 | enum led_state_t default_state; |
Heiko Schocher | 350d252 | 2025-01-28 14:52:46 +0100 | [diff] [blame^] | 94 | char name[LED_MAX_NAME_SIZE]; |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 95 | #ifdef CONFIG_LED_SW_BLINK |
| 96 | struct led_sw_blink *sw_blink; |
| 97 | #endif |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 98 | }; |
| 99 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 100 | /** |
| 101 | * struct led_uc_priv - Private data the uclass stores about each device |
| 102 | * |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 103 | * @boot_led_label: Boot LED label |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 104 | * @activity_led_label: Activity LED label |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 105 | * @boot_led_dev: Boot LED dev |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 106 | * @activity_led_dev: Activity LED dev |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 107 | * @boot_led_period: Boot LED blink period |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 108 | * @activity_led_period: Activity LED blink period |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 109 | */ |
| 110 | struct led_uc_priv { |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 111 | #ifdef CONFIG_LED_BOOT |
| 112 | const char *boot_led_label; |
| 113 | int boot_led_period; |
| 114 | #endif |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 115 | #ifdef CONFIG_LED_ACTIVITY |
| 116 | const char *activity_led_label; |
| 117 | int activity_led_period; |
| 118 | #endif |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 119 | }; |
| 120 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 121 | struct led_ops { |
| 122 | /** |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 123 | * set_state() - set the state of an LED |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 124 | * |
| 125 | * @dev: LED device to change |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 126 | * @state: LED state to set |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 127 | * @return 0 if OK, -ve on error |
| 128 | */ |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 129 | int (*set_state)(struct udevice *dev, enum led_state_t state); |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * led_get_state() - get the state of an LED |
| 133 | * |
| 134 | * @dev: LED device to change |
| 135 | * @return LED state led_state_t, or -ve on error |
| 136 | */ |
| 137 | enum led_state_t (*get_state)(struct udevice *dev); |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 138 | |
| 139 | #ifdef CONFIG_LED_BLINK |
| 140 | /** |
| 141 | * led_set_period() - set the blink period of an LED |
| 142 | * |
| 143 | * Thie records the period if supported, or returns -ENOSYS if not. |
| 144 | * To start the LED blinking, use set_state(). |
| 145 | * |
| 146 | * @dev: LED device to change |
| 147 | * @period_ms: LED blink period in milliseconds |
| 148 | * @return 0 if OK, -ve on error |
| 149 | */ |
| 150 | int (*set_period)(struct udevice *dev, int period_ms); |
| 151 | #endif |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | #define led_get_ops(dev) ((struct led_ops *)(dev)->driver->ops) |
| 155 | |
| 156 | /** |
| 157 | * led_get_by_label() - Find an LED device by label |
| 158 | * |
| 159 | * @label: LED label to look up |
| 160 | * @devp: Returns the associated device, if found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 161 | * Return: 0 if found, -ENODEV if not found, other -ve on error |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 162 | */ |
| 163 | int led_get_by_label(const char *label, struct udevice **devp); |
| 164 | |
| 165 | /** |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 166 | * led_set_state() - set the state of an LED |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 167 | * |
| 168 | * @dev: LED device to change |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 169 | * @state: LED state to set |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 170 | * Return: 0 if OK, -ve on error |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 171 | */ |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 172 | int led_set_state(struct udevice *dev, enum led_state_t state); |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 173 | |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 174 | /** |
| 175 | * led_get_state() - get the state of an LED |
| 176 | * |
| 177 | * @dev: LED device to change |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 178 | * Return: LED state led_state_t, or -ve on error |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 179 | */ |
| 180 | enum led_state_t led_get_state(struct udevice *dev); |
| 181 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 182 | /** |
| 183 | * led_set_period() - set the blink period of an LED |
| 184 | * |
| 185 | * @dev: LED device to change |
| 186 | * @period_ms: LED blink period in milliseconds |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 187 | * Return: 0 if OK, -ve on error |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 188 | */ |
| 189 | int led_set_period(struct udevice *dev, int period_ms); |
| 190 | |
Rasmus Villemoes | 49ba9f0 | 2023-11-17 12:38:08 +0100 | [diff] [blame] | 191 | /** |
| 192 | * led_bind_generic() - bind children of parent to given driver |
| 193 | * |
| 194 | * @parent: Top-level LED device |
| 195 | * @driver_name: Driver for handling individual child nodes |
| 196 | */ |
| 197 | int led_bind_generic(struct udevice *parent, const char *driver_name); |
| 198 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 199 | /* Internal functions for software blinking. Do not use them in your code */ |
| 200 | int led_sw_set_period(struct udevice *dev, int period_ms); |
| 201 | bool led_sw_is_blinking(struct udevice *dev); |
| 202 | bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state); |
| 203 | |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 204 | #ifdef CONFIG_LED_BOOT |
| 205 | |
| 206 | /** |
| 207 | * led_boot_on() - turn ON the designated LED for booting |
| 208 | * |
| 209 | * Return: 0 if OK, -ve on error |
| 210 | */ |
| 211 | int led_boot_on(void); |
| 212 | |
| 213 | /** |
| 214 | * led_boot_off() - turn OFF the designated LED for booting |
| 215 | * |
| 216 | * Return: 0 if OK, -ve on error |
| 217 | */ |
| 218 | int led_boot_off(void); |
| 219 | |
| 220 | #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK) |
| 221 | /** |
| 222 | * led_boot_blink() - turn ON the designated LED for booting |
| 223 | * |
| 224 | * Return: 0 if OK, -ve on error |
| 225 | */ |
| 226 | int led_boot_blink(void); |
| 227 | |
| 228 | #else |
| 229 | /* If LED BLINK is not supported/enabled, fallback to LED ON */ |
| 230 | #define led_boot_blink led_boot_on |
| 231 | #endif |
| 232 | #else |
| 233 | static inline int led_boot_on(void) |
| 234 | { |
| 235 | return -ENOSYS; |
| 236 | } |
| 237 | |
| 238 | static inline int led_boot_off(void) |
| 239 | { |
| 240 | return -ENOSYS; |
| 241 | } |
| 242 | |
| 243 | static inline int led_boot_blink(void) |
| 244 | { |
| 245 | return -ENOSYS; |
| 246 | } |
| 247 | #endif |
| 248 | |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 249 | #ifdef CONFIG_LED_ACTIVITY |
| 250 | |
| 251 | /** |
| 252 | * led_activity_on() - turn ON the designated LED for activity |
| 253 | * |
| 254 | * Return: 0 if OK, -ve on error |
| 255 | */ |
| 256 | int led_activity_on(void); |
| 257 | |
| 258 | /** |
| 259 | * led_activity_off() - turn OFF the designated LED for activity |
| 260 | * |
| 261 | * Return: 0 if OK, -ve on error |
| 262 | */ |
| 263 | int led_activity_off(void); |
| 264 | |
| 265 | #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK) |
| 266 | /** |
| 267 | * led_activity_blink() - turn ON the designated LED for activity |
| 268 | * |
| 269 | * Return: 0 if OK, -ve on error |
| 270 | */ |
| 271 | int led_activity_blink(void); |
| 272 | #else |
| 273 | /* If LED BLINK is not supported/enabled, fallback to LED ON */ |
| 274 | #define led_activity_blink led_activity_on |
| 275 | #endif |
| 276 | #else |
| 277 | static inline int led_activity_on(void) |
| 278 | { |
| 279 | return -ENOSYS; |
| 280 | } |
| 281 | |
| 282 | static inline int led_activity_off(void) |
| 283 | { |
| 284 | return -ENOSYS; |
| 285 | } |
| 286 | |
| 287 | static inline int led_activity_blink(void) |
| 288 | { |
| 289 | return -ENOSYS; |
| 290 | } |
| 291 | #endif |
| 292 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 293 | #endif |