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 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | struct udevice; |
| 15 | |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 16 | enum led_state_t { |
| 17 | LEDST_OFF = 0, |
| 18 | LEDST_ON = 1, |
| 19 | LEDST_TOGGLE, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 20 | LEDST_BLINK, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 21 | |
| 22 | LEDST_COUNT, |
| 23 | }; |
| 24 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 25 | enum led_sw_blink_state_t { |
| 26 | LED_SW_BLINK_ST_DISABLED, |
| 27 | LED_SW_BLINK_ST_NOT_READY, |
| 28 | LED_SW_BLINK_ST_OFF, |
| 29 | LED_SW_BLINK_ST_ON, |
| 30 | }; |
| 31 | |
| 32 | struct led_sw_blink { |
| 33 | enum led_sw_blink_state_t state; |
| 34 | struct udevice *dev; |
| 35 | struct cyclic_info cyclic; |
| 36 | const char cyclic_name[0]; |
| 37 | }; |
| 38 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 39 | /** |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 40 | * struct led_uc_plat - Platform data the uclass stores about each device |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 41 | * |
| 42 | * @label: LED label |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 43 | * @default_state: LED default state |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 44 | */ |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 45 | struct led_uc_plat { |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 46 | const char *label; |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 47 | enum led_state_t default_state; |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 48 | #ifdef CONFIG_LED_SW_BLINK |
| 49 | struct led_sw_blink *sw_blink; |
| 50 | #endif |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 51 | }; |
| 52 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 53 | /** |
| 54 | * struct led_uc_priv - Private data the uclass stores about each device |
| 55 | * |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 56 | * @boot_led_label: Boot LED label |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame^] | 57 | * @activity_led_label: Activity LED label |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 58 | * @boot_led_dev: Boot LED dev |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame^] | 59 | * @activity_led_dev: Activity LED dev |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 60 | * @boot_led_period: Boot LED blink period |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame^] | 61 | * @activity_led_period: Activity LED blink period |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 62 | */ |
| 63 | struct led_uc_priv { |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 64 | #ifdef CONFIG_LED_BOOT |
| 65 | const char *boot_led_label; |
| 66 | int boot_led_period; |
| 67 | #endif |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame^] | 68 | #ifdef CONFIG_LED_ACTIVITY |
| 69 | const char *activity_led_label; |
| 70 | int activity_led_period; |
| 71 | #endif |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 72 | }; |
| 73 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 74 | struct led_ops { |
| 75 | /** |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 76 | * set_state() - set the state of an LED |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 77 | * |
| 78 | * @dev: LED device to change |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 79 | * @state: LED state to set |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 80 | * @return 0 if OK, -ve on error |
| 81 | */ |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 82 | int (*set_state)(struct udevice *dev, enum led_state_t state); |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * led_get_state() - get the state of an LED |
| 86 | * |
| 87 | * @dev: LED device to change |
| 88 | * @return LED state led_state_t, or -ve on error |
| 89 | */ |
| 90 | enum led_state_t (*get_state)(struct udevice *dev); |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 91 | |
| 92 | #ifdef CONFIG_LED_BLINK |
| 93 | /** |
| 94 | * led_set_period() - set the blink period of an LED |
| 95 | * |
| 96 | * Thie records the period if supported, or returns -ENOSYS if not. |
| 97 | * To start the LED blinking, use set_state(). |
| 98 | * |
| 99 | * @dev: LED device to change |
| 100 | * @period_ms: LED blink period in milliseconds |
| 101 | * @return 0 if OK, -ve on error |
| 102 | */ |
| 103 | int (*set_period)(struct udevice *dev, int period_ms); |
| 104 | #endif |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #define led_get_ops(dev) ((struct led_ops *)(dev)->driver->ops) |
| 108 | |
| 109 | /** |
| 110 | * led_get_by_label() - Find an LED device by label |
| 111 | * |
| 112 | * @label: LED label to look up |
| 113 | * @devp: Returns the associated device, if found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 114 | * Return: 0 if found, -ENODEV if not found, other -ve on error |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 115 | */ |
| 116 | int led_get_by_label(const char *label, struct udevice **devp); |
| 117 | |
| 118 | /** |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 119 | * led_set_state() - set the state of an LED |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 120 | * |
| 121 | * @dev: LED device to change |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 122 | * @state: LED state to set |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 123 | * Return: 0 if OK, -ve on error |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 124 | */ |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 125 | int led_set_state(struct udevice *dev, enum led_state_t state); |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 126 | |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 127 | /** |
| 128 | * led_get_state() - get the state of an LED |
| 129 | * |
| 130 | * @dev: LED device to change |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 131 | * Return: LED state led_state_t, or -ve on error |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 132 | */ |
| 133 | enum led_state_t led_get_state(struct udevice *dev); |
| 134 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 135 | /** |
| 136 | * led_set_period() - set the blink period of an LED |
| 137 | * |
| 138 | * @dev: LED device to change |
| 139 | * @period_ms: LED blink period in milliseconds |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 140 | * Return: 0 if OK, -ve on error |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 141 | */ |
| 142 | int led_set_period(struct udevice *dev, int period_ms); |
| 143 | |
Rasmus Villemoes | 49ba9f0 | 2023-11-17 12:38:08 +0100 | [diff] [blame] | 144 | /** |
| 145 | * led_bind_generic() - bind children of parent to given driver |
| 146 | * |
| 147 | * @parent: Top-level LED device |
| 148 | * @driver_name: Driver for handling individual child nodes |
| 149 | */ |
| 150 | int led_bind_generic(struct udevice *parent, const char *driver_name); |
| 151 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 152 | /* Internal functions for software blinking. Do not use them in your code */ |
| 153 | int led_sw_set_period(struct udevice *dev, int period_ms); |
| 154 | bool led_sw_is_blinking(struct udevice *dev); |
| 155 | bool led_sw_on_state_change(struct udevice *dev, enum led_state_t state); |
| 156 | |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 157 | #ifdef CONFIG_LED_BOOT |
| 158 | |
| 159 | /** |
| 160 | * led_boot_on() - turn ON the designated LED for booting |
| 161 | * |
| 162 | * Return: 0 if OK, -ve on error |
| 163 | */ |
| 164 | int led_boot_on(void); |
| 165 | |
| 166 | /** |
| 167 | * led_boot_off() - turn OFF the designated LED for booting |
| 168 | * |
| 169 | * Return: 0 if OK, -ve on error |
| 170 | */ |
| 171 | int led_boot_off(void); |
| 172 | |
| 173 | #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK) |
| 174 | /** |
| 175 | * led_boot_blink() - turn ON the designated LED for booting |
| 176 | * |
| 177 | * Return: 0 if OK, -ve on error |
| 178 | */ |
| 179 | int led_boot_blink(void); |
| 180 | |
| 181 | #else |
| 182 | /* If LED BLINK is not supported/enabled, fallback to LED ON */ |
| 183 | #define led_boot_blink led_boot_on |
| 184 | #endif |
| 185 | #else |
| 186 | static inline int led_boot_on(void) |
| 187 | { |
| 188 | return -ENOSYS; |
| 189 | } |
| 190 | |
| 191 | static inline int led_boot_off(void) |
| 192 | { |
| 193 | return -ENOSYS; |
| 194 | } |
| 195 | |
| 196 | static inline int led_boot_blink(void) |
| 197 | { |
| 198 | return -ENOSYS; |
| 199 | } |
| 200 | #endif |
| 201 | |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame^] | 202 | #ifdef CONFIG_LED_ACTIVITY |
| 203 | |
| 204 | /** |
| 205 | * led_activity_on() - turn ON the designated LED for activity |
| 206 | * |
| 207 | * Return: 0 if OK, -ve on error |
| 208 | */ |
| 209 | int led_activity_on(void); |
| 210 | |
| 211 | /** |
| 212 | * led_activity_off() - turn OFF the designated LED for activity |
| 213 | * |
| 214 | * Return: 0 if OK, -ve on error |
| 215 | */ |
| 216 | int led_activity_off(void); |
| 217 | |
| 218 | #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK) |
| 219 | /** |
| 220 | * led_activity_blink() - turn ON the designated LED for activity |
| 221 | * |
| 222 | * Return: 0 if OK, -ve on error |
| 223 | */ |
| 224 | int led_activity_blink(void); |
| 225 | #else |
| 226 | /* If LED BLINK is not supported/enabled, fallback to LED ON */ |
| 227 | #define led_activity_blink led_activity_on |
| 228 | #endif |
| 229 | #else |
| 230 | static inline int led_activity_on(void) |
| 231 | { |
| 232 | return -ENOSYS; |
| 233 | } |
| 234 | |
| 235 | static inline int led_activity_off(void) |
| 236 | { |
| 237 | return -ENOSYS; |
| 238 | } |
| 239 | |
| 240 | static inline int led_activity_blink(void) |
| 241 | { |
| 242 | return -ENOSYS; |
| 243 | } |
| 244 | #endif |
| 245 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 246 | #endif |