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 <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <led.h> |
Patrick Delaunay | a5bf928 | 2018-07-27 16:37:07 +0200 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Rasmus Villemoes | 49ba9f0 | 2023-11-17 12:38:08 +0100 | [diff] [blame] | 14 | #include <dm/lists.h> |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 15 | #include <dm/root.h> |
| 16 | #include <dm/uclass-internal.h> |
| 17 | |
Rasmus Villemoes | 49ba9f0 | 2023-11-17 12:38:08 +0100 | [diff] [blame] | 18 | int led_bind_generic(struct udevice *parent, const char *driver_name) |
| 19 | { |
| 20 | struct udevice *dev; |
| 21 | ofnode node; |
| 22 | int ret; |
| 23 | |
| 24 | dev_for_each_subnode(node, parent) { |
| 25 | ret = device_bind_driver_to_node(parent, driver_name, |
| 26 | ofnode_get_name(node), |
| 27 | node, &dev); |
| 28 | if (ret) |
| 29 | return ret; |
| 30 | } |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 35 | int led_get_by_label(const char *label, struct udevice **devp) |
| 36 | { |
| 37 | struct udevice *dev; |
| 38 | struct uclass *uc; |
| 39 | int ret; |
| 40 | |
| 41 | ret = uclass_get(UCLASS_LED, &uc); |
| 42 | if (ret) |
| 43 | return ret; |
| 44 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 45 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 46 | |
Simon Glass | bf3ac819 | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 47 | /* Ignore the top-level LED node */ |
| 48 | if (uc_plat->label && !strcmp(label, uc_plat->label)) |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 49 | return uclass_get_device_tail(dev, 0, devp); |
| 50 | } |
| 51 | |
Simon Glass | bf3ac819 | 2015-07-06 12:54:33 -0600 | [diff] [blame] | 52 | return -ENODEV; |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 53 | } |
| 54 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 55 | int led_set_state(struct udevice *dev, enum led_state_t state) |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 56 | { |
| 57 | struct led_ops *ops = led_get_ops(dev); |
| 58 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 59 | if (!ops->set_state) |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 60 | return -ENOSYS; |
| 61 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 62 | return ops->set_state(dev, state); |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 65 | enum led_state_t led_get_state(struct udevice *dev) |
| 66 | { |
| 67 | struct led_ops *ops = led_get_ops(dev); |
| 68 | |
| 69 | if (!ops->get_state) |
| 70 | return -ENOSYS; |
| 71 | |
| 72 | return ops->get_state(dev); |
| 73 | } |
| 74 | |
Simon Glass | 3bd0c46 | 2017-04-10 11:34:57 -0600 | [diff] [blame] | 75 | #ifdef CONFIG_LED_BLINK |
| 76 | int led_set_period(struct udevice *dev, int period_ms) |
| 77 | { |
| 78 | struct led_ops *ops = led_get_ops(dev); |
| 79 | |
| 80 | if (!ops->set_period) |
| 81 | return -ENOSYS; |
| 82 | |
| 83 | return ops->set_period(dev, period_ms); |
| 84 | } |
| 85 | #endif |
| 86 | |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 87 | static int led_post_bind(struct udevice *dev) |
| 88 | { |
| 89 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 90 | const char *default_state; |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 91 | |
Rasmus Villemoes | ad3401f | 2023-11-17 12:38:06 +0100 | [diff] [blame] | 92 | if (!uc_plat->label) |
| 93 | uc_plat->label = dev_read_string(dev, "label"); |
| 94 | |
Rasmus Villemoes | 128f87a | 2023-11-17 12:38:07 +0100 | [diff] [blame] | 95 | if (!uc_plat->label && !dev_read_string(dev, "compatible")) |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 96 | uc_plat->label = ofnode_get_name(dev_ofnode(dev)); |
| 97 | |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 98 | uc_plat->default_state = LEDST_COUNT; |
| 99 | |
| 100 | default_state = dev_read_string(dev, "default-state"); |
| 101 | if (!default_state) |
| 102 | return 0; |
| 103 | |
| 104 | if (!strncmp(default_state, "on", 2)) |
| 105 | uc_plat->default_state = LEDST_ON; |
| 106 | else if (!strncmp(default_state, "off", 3)) |
| 107 | uc_plat->default_state = LEDST_OFF; |
| 108 | else |
| 109 | return 0; |
| 110 | |
| 111 | /* |
| 112 | * In case the LED has default-state DT property, trigger |
| 113 | * probe() to configure its default state during startup. |
| 114 | */ |
Marek Vasut | 1c2b81f | 2022-04-22 15:15:54 +0200 | [diff] [blame] | 115 | dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND); |
| 116 | |
| 117 | return 0; |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int led_post_probe(struct udevice *dev) |
| 121 | { |
| 122 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
| 123 | |
| 124 | if (uc_plat->default_state == LEDST_ON || |
| 125 | uc_plat->default_state == LEDST_OFF) |
| 126 | led_set_state(dev, uc_plat->default_state); |
| 127 | |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 131 | UCLASS_DRIVER(led) = { |
| 132 | .id = UCLASS_LED, |
| 133 | .name = "led", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 134 | .per_device_plat_auto = sizeof(struct led_uc_plat), |
Marek Vasut | c2b7e98 | 2022-04-04 01:18:02 +0200 | [diff] [blame] | 135 | .post_bind = led_post_bind, |
Marek Vasut | 03cadb6 | 2022-04-04 01:23:27 +0200 | [diff] [blame] | 136 | .post_probe = led_post_probe, |
Simon Glass | cce3aed | 2015-06-23 15:38:45 -0600 | [diff] [blame] | 137 | }; |