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 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_LED |
| 8 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <led.h> |
Patrick Delaunay | a5bf928 | 2018-07-27 16:37:07 +0200 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Rasmus Villemoes | 49ba9f0 | 2023-11-17 12:38:08 +0100 | [diff] [blame] | 13 | #include <dm/lists.h> |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 14 | #include <dm/root.h> |
| 15 | #include <dm/uclass-internal.h> |
| 16 | |
Rasmus Villemoes | 49ba9f0 | 2023-11-17 12:38:08 +0100 | [diff] [blame] | 17 | int led_bind_generic(struct udevice *parent, const char *driver_name) |
| 18 | { |
| 19 | struct udevice *dev; |
| 20 | ofnode node; |
| 21 | int ret; |
| 22 | |
| 23 | dev_for_each_subnode(node, parent) { |
| 24 | ret = device_bind_driver_to_node(parent, driver_name, |
| 25 | ofnode_get_name(node), |
| 26 | node, &dev); |
| 27 | if (ret) |
| 28 | return ret; |
| 29 | } |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 34 | int led_get_by_label(const char *label, struct udevice **devp) |
| 35 | { |
| 36 | struct udevice *dev; |
| 37 | struct uclass *uc; |
| 38 | int ret; |
| 39 | |
| 40 | ret = uclass_get(UCLASS_LED, &uc); |
| 41 | if (ret) |
| 42 | return ret; |
| 43 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 44 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 45 | |
Simon Glass | bf3ac819 | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 46 | /* Ignore the top-level LED node */ |
| 47 | if (uc_plat->label && !strcmp(label, uc_plat->label)) |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 48 | return uclass_get_device_tail(dev, 0, devp); |
| 49 | } |
| 50 | |
Simon Glass | bf3ac819 | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 51 | return -ENODEV; |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 52 | } |
| 53 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 54 | int led_set_state(struct udevice *dev, enum led_state_t state) |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 55 | { |
| 56 | struct led_ops *ops = led_get_ops(dev); |
| 57 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 58 | if (!ops->set_state) |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 59 | return -ENOSYS; |
| 60 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 61 | if (IS_ENABLED(CONFIG_LED_SW_BLINK) && |
| 62 | led_sw_on_state_change(dev, state)) |
| 63 | return 0; |
| 64 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 65 | return ops->set_state(dev, state); |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 68 | enum led_state_t led_get_state(struct udevice *dev) |
| 69 | { |
| 70 | struct led_ops *ops = led_get_ops(dev); |
| 71 | |
| 72 | if (!ops->get_state) |
| 73 | return -ENOSYS; |
| 74 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 75 | if (IS_ENABLED(CONFIG_LED_SW_BLINK) && |
| 76 | led_sw_is_blinking(dev)) |
| 77 | return LEDST_BLINK; |
| 78 | |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 79 | return ops->get_state(dev); |
| 80 | } |
| 81 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 82 | int led_set_period(struct udevice *dev, int period_ms) |
| 83 | { |
Mikhail Kshevetskiy | 8224f16 | 2024-07-19 13:12:11 +0400 | [diff] [blame] | 84 | #ifdef CONFIG_LED_BLINK |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 85 | struct led_ops *ops = led_get_ops(dev); |
| 86 | |
Mikhail Kshevetskiy | 8224f16 | 2024-07-19 13:12:11 +0400 | [diff] [blame] | 87 | if (ops->set_period) |
| 88 | return ops->set_period(dev, period_ms); |
| 89 | #endif |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 90 | |
Michael Polyntsov | 353deb9 | 2024-07-19 13:12:12 +0400 | [diff] [blame] | 91 | if (IS_ENABLED(CONFIG_LED_SW_BLINK)) |
| 92 | return led_sw_set_period(dev, period_ms); |
| 93 | |
Mikhail Kshevetskiy | 8224f16 | 2024-07-19 13:12:11 +0400 | [diff] [blame] | 94 | return -ENOSYS; |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 95 | } |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 96 | |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 97 | #ifdef CONFIG_LED_BOOT |
| 98 | static int led_boot_get(struct udevice **devp, int *period_ms) |
| 99 | { |
| 100 | struct led_uc_priv *priv; |
| 101 | struct uclass *uc; |
| 102 | int ret; |
| 103 | |
| 104 | ret = uclass_get(UCLASS_LED, &uc); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | priv = uclass_get_priv(uc); |
| 109 | if (!priv->boot_led_label) |
| 110 | return -ENOENT; |
| 111 | |
| 112 | if (period_ms) |
| 113 | *period_ms = priv->boot_led_period; |
| 114 | |
| 115 | return led_get_by_label(priv->boot_led_label, devp); |
| 116 | } |
| 117 | |
| 118 | int led_boot_on(void) |
| 119 | { |
| 120 | struct udevice *dev; |
| 121 | int ret; |
| 122 | |
| 123 | ret = led_boot_get(&dev, NULL); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | |
| 127 | return led_set_state(dev, LEDST_ON); |
| 128 | } |
| 129 | |
| 130 | int led_boot_off(void) |
| 131 | { |
| 132 | struct udevice *dev; |
| 133 | int ret; |
| 134 | |
| 135 | ret = led_boot_get(&dev, NULL); |
| 136 | if (ret) |
| 137 | return ret; |
| 138 | |
| 139 | return led_set_state(dev, LEDST_OFF); |
| 140 | } |
| 141 | |
| 142 | #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK) |
| 143 | int led_boot_blink(void) |
| 144 | { |
| 145 | struct udevice *dev; |
| 146 | int period_ms, ret; |
| 147 | |
| 148 | ret = led_boot_get(&dev, &period_ms); |
| 149 | if (ret) |
| 150 | return ret; |
| 151 | |
| 152 | ret = led_set_period(dev, period_ms); |
| 153 | if (ret) { |
| 154 | if (ret != -ENOSYS) |
| 155 | return ret; |
| 156 | |
| 157 | /* fallback to ON with no set_period and no SW_BLINK */ |
| 158 | return led_set_state(dev, LEDST_ON); |
| 159 | } |
| 160 | |
| 161 | return led_set_state(dev, LEDST_BLINK); |
| 162 | } |
| 163 | #endif |
| 164 | #endif |
| 165 | |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_LED_ACTIVITY |
| 167 | static int led_activity_get(struct udevice **devp, int *period_ms) |
| 168 | { |
| 169 | struct led_uc_priv *priv; |
| 170 | struct uclass *uc; |
| 171 | int ret; |
| 172 | |
| 173 | ret = uclass_get(UCLASS_LED, &uc); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | |
| 177 | priv = uclass_get_priv(uc); |
| 178 | if (!priv->activity_led_label) |
| 179 | return -ENOENT; |
| 180 | |
| 181 | if (period_ms) |
| 182 | *period_ms = priv->activity_led_period; |
| 183 | |
| 184 | return led_get_by_label(priv->activity_led_label, devp); |
| 185 | } |
| 186 | |
| 187 | int led_activity_on(void) |
| 188 | { |
| 189 | struct udevice *dev; |
| 190 | int ret; |
| 191 | |
| 192 | ret = led_activity_get(&dev, NULL); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | return led_set_state(dev, LEDST_ON); |
| 197 | } |
| 198 | |
| 199 | int led_activity_off(void) |
| 200 | { |
| 201 | struct udevice *dev; |
| 202 | int ret; |
| 203 | |
| 204 | ret = led_activity_get(&dev, NULL); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
| 208 | return led_set_state(dev, LEDST_OFF); |
| 209 | } |
| 210 | |
| 211 | #if defined(CONFIG_LED_BLINK) || defined(CONFIG_LED_SW_BLINK) |
| 212 | int led_activity_blink(void) |
| 213 | { |
| 214 | struct udevice *dev; |
| 215 | int period_ms, ret; |
| 216 | |
| 217 | ret = led_activity_get(&dev, &period_ms); |
| 218 | if (ret) |
| 219 | return ret; |
| 220 | |
| 221 | ret = led_set_period(dev, period_ms); |
| 222 | if (ret) { |
| 223 | if (ret != -ENOSYS) |
| 224 | return ret; |
| 225 | |
| 226 | /* fallback to ON with no set_period and no SW_BLINK */ |
| 227 | return led_set_state(dev, LEDST_ON); |
| 228 | } |
| 229 | |
| 230 | return led_set_state(dev, LEDST_BLINK); |
| 231 | } |
| 232 | #endif |
| 233 | #endif |
| 234 | |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 235 | static int led_post_bind(struct udevice *dev) |
| 236 | { |
| 237 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 238 | const char *default_state; |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 239 | |
Rasmus Villemoes | ad3401f | 2023-11-17 12:38:06 +0100 | [diff] [blame] | 240 | if (!uc_plat->label) |
| 241 | uc_plat->label = dev_read_string(dev, "label"); |
| 242 | |
Rasmus Villemoes | 128f87a | 2023-11-17 12:38:07 +0100 | [diff] [blame] | 243 | if (!uc_plat->label && !dev_read_string(dev, "compatible")) |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 244 | uc_plat->label = ofnode_get_name(dev_ofnode(dev)); |
| 245 | |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 246 | uc_plat->default_state = LEDST_COUNT; |
| 247 | |
| 248 | default_state = dev_read_string(dev, "default-state"); |
| 249 | if (!default_state) |
| 250 | return 0; |
| 251 | |
| 252 | if (!strncmp(default_state, "on", 2)) |
| 253 | uc_plat->default_state = LEDST_ON; |
| 254 | else if (!strncmp(default_state, "off", 3)) |
| 255 | uc_plat->default_state = LEDST_OFF; |
| 256 | else |
| 257 | return 0; |
| 258 | |
Michael Polyntsov | 56250a6 | 2024-07-19 13:12:13 +0400 | [diff] [blame] | 259 | if (IS_ENABLED(CONFIG_LED_BLINK)) { |
| 260 | const char *trigger; |
| 261 | |
| 262 | trigger = dev_read_string(dev, "linux,default-trigger"); |
| 263 | if (trigger && !strncmp(trigger, "pattern", 7)) |
| 264 | uc_plat->default_state = LEDST_BLINK; |
| 265 | } |
| 266 | |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 267 | /* |
| 268 | * In case the LED has default-state DT property, trigger |
| 269 | * probe() to configure its default state during startup. |
| 270 | */ |
Marek Vasut | 1c2b81f | 2022-04-22 15:15:54 +0200 | [diff] [blame] | 271 | dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND); |
| 272 | |
| 273 | return 0; |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static int led_post_probe(struct udevice *dev) |
| 277 | { |
| 278 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Michael Polyntsov | 56250a6 | 2024-07-19 13:12:13 +0400 | [diff] [blame] | 279 | int default_period_ms = 1000; |
| 280 | int ret = 0; |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 281 | |
Michael Polyntsov | 56250a6 | 2024-07-19 13:12:13 +0400 | [diff] [blame] | 282 | switch (uc_plat->default_state) { |
| 283 | case LEDST_ON: |
| 284 | case LEDST_OFF: |
| 285 | ret = led_set_state(dev, uc_plat->default_state); |
| 286 | break; |
| 287 | case LEDST_BLINK: |
| 288 | ret = led_set_period(dev, default_period_ms); |
| 289 | if (!ret) |
| 290 | ret = led_set_state(dev, uc_plat->default_state); |
| 291 | break; |
| 292 | default: |
| 293 | break; |
| 294 | } |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 295 | |
Michael Polyntsov | 56250a6 | 2024-07-19 13:12:13 +0400 | [diff] [blame] | 296 | return ret; |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 297 | } |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 298 | |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 299 | #if defined(CONFIG_LED_BOOT) || defined(CONFIG_LED_ACTIVITY) |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 300 | static int led_init(struct uclass *uc) |
| 301 | { |
| 302 | struct led_uc_priv *priv = uclass_get_priv(uc); |
| 303 | |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 304 | #ifdef CONFIG_LED_BOOT |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 305 | priv->boot_led_label = ofnode_options_read_str("boot-led"); |
| 306 | priv->boot_led_period = ofnode_options_read_int("boot-led-period", 250); |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 307 | #endif |
| 308 | |
| 309 | #ifdef CONFIG_LED_ACTIVITY |
| 310 | priv->activity_led_label = ofnode_options_read_str("activity-led"); |
| 311 | priv->activity_led_period = ofnode_options_read_int("activity-led-period", |
| 312 | 250); |
| 313 | #endif |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | #endif |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 318 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 319 | UCLASS_DRIVER(led) = { |
| 320 | .id = UCLASS_LED, |
| 321 | .name = "led", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 322 | .per_device_plat_auto = sizeof(struct led_uc_plat), |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 323 | .post_bind = led_post_bind, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 324 | .post_probe = led_post_probe, |
Christian Marangi | 8be34e8 | 2024-10-01 14:24:38 +0200 | [diff] [blame] | 325 | #if defined(CONFIG_LED_BOOT) || defined(CONFIG_LED_ACTIVITY) |
Christian Marangi | ea7387e | 2024-10-01 14:24:36 +0200 | [diff] [blame] | 326 | .init = led_init, |
| 327 | .priv_auto = sizeof(struct led_uc_priv), |
| 328 | #endif |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 329 | }; |