Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5917112 | 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 | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_LED |
| 8 | |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <led.h> |
Patrick Delaunay | d7a435a | 2018-07-27 16:37:07 +0200 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 14 | #include <dm/root.h> |
| 15 | #include <dm/uclass-internal.h> |
| 16 | |
| 17 | int led_get_by_label(const char *label, struct udevice **devp) |
| 18 | { |
| 19 | struct udevice *dev; |
| 20 | struct uclass *uc; |
| 21 | int ret; |
| 22 | |
| 23 | ret = uclass_get(UCLASS_LED, &uc); |
| 24 | if (ret) |
| 25 | return ret; |
| 26 | uclass_foreach_dev(dev, uc) { |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 27 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 28 | |
Simon Glass | fb8a5ff | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 29 | /* Ignore the top-level LED node */ |
| 30 | if (uc_plat->label && !strcmp(label, uc_plat->label)) |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 31 | return uclass_get_device_tail(dev, 0, devp); |
| 32 | } |
| 33 | |
Simon Glass | fb8a5ff | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 34 | return -ENODEV; |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 35 | } |
| 36 | |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 37 | int led_set_state(struct udevice *dev, enum led_state_t state) |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 38 | { |
| 39 | struct led_ops *ops = led_get_ops(dev); |
| 40 | |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 41 | if (!ops->set_state) |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 42 | return -ENOSYS; |
| 43 | |
Simon Glass | ddae9fc | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 44 | return ops->set_state(dev, state); |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 45 | } |
| 46 | |
Simon Glass | 8f4b612 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 47 | enum led_state_t led_get_state(struct udevice *dev) |
| 48 | { |
| 49 | struct led_ops *ops = led_get_ops(dev); |
| 50 | |
| 51 | if (!ops->get_state) |
| 52 | return -ENOSYS; |
| 53 | |
| 54 | return ops->get_state(dev); |
| 55 | } |
| 56 | |
Simon Glass | 53378da | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 57 | #ifdef CONFIG_LED_BLINK |
| 58 | int led_set_period(struct udevice *dev, int period_ms) |
| 59 | { |
| 60 | struct led_ops *ops = led_get_ops(dev); |
| 61 | |
| 62 | if (!ops->set_period) |
| 63 | return -ENOSYS; |
| 64 | |
| 65 | return ops->set_period(dev, period_ms); |
| 66 | } |
| 67 | #endif |
| 68 | |
Patrick Delaunay | d7a435a | 2018-07-27 16:37:07 +0200 | [diff] [blame] | 69 | int led_default_state(void) |
| 70 | { |
| 71 | struct udevice *dev; |
| 72 | struct uclass *uc; |
| 73 | const char *default_state; |
| 74 | int ret; |
| 75 | |
| 76 | ret = uclass_get(UCLASS_LED, &uc); |
| 77 | if (ret) |
| 78 | return ret; |
| 79 | for (uclass_find_first_device(UCLASS_LED, &dev); |
| 80 | dev; |
| 81 | uclass_find_next_device(&dev)) { |
| 82 | default_state = dev_read_string(dev, "default-state"); |
| 83 | if (!default_state) |
| 84 | continue; |
| 85 | ret = device_probe(dev); |
| 86 | if (ret) |
| 87 | return ret; |
| 88 | if (!strncmp(default_state, "on", 2)) |
| 89 | led_set_state(dev, LEDST_ON); |
| 90 | else if (!strncmp(default_state, "off", 3)) |
| 91 | led_set_state(dev, LEDST_OFF); |
| 92 | /* default-state = "keep" : device is only probed */ |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 98 | UCLASS_DRIVER(led) = { |
| 99 | .id = UCLASS_LED, |
| 100 | .name = "led", |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 101 | .per_device_plat_auto = sizeof(struct led_uc_plat), |
Simon Glass | 5917112 | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 102 | }; |