blob: 199d68bc25a83f21544d40fd00cd874efebdfd78 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasscce3aed2015-06-23 15:38:45 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasscce3aed2015-06-23 15:38:45 -06005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_LED
8
Simon Glasscce3aed2015-06-23 15:38:45 -06009#include <dm.h>
10#include <errno.h>
11#include <led.h>
Patrick Delaunaya5bf9282018-07-27 16:37:07 +020012#include <dm/device-internal.h>
Rasmus Villemoes49ba9f02023-11-17 12:38:08 +010013#include <dm/lists.h>
Simon Glasscce3aed2015-06-23 15:38:45 -060014#include <dm/root.h>
15#include <dm/uclass-internal.h>
16
Rasmus Villemoes49ba9f02023-11-17 12:38:08 +010017int 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 Glasscce3aed2015-06-23 15:38:45 -060034int 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 Glass71fa5b42020-12-03 16:55:18 -070044 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glasscce3aed2015-06-23 15:38:45 -060045
Simon Glassbf3ac8192015-07-06 12:54:33 -060046 /* Ignore the top-level LED node */
47 if (uc_plat->label && !strcmp(label, uc_plat->label))
Simon Glasscce3aed2015-06-23 15:38:45 -060048 return uclass_get_device_tail(dev, 0, devp);
49 }
50
Simon Glassbf3ac8192015-07-06 12:54:33 -060051 return -ENODEV;
Simon Glasscce3aed2015-06-23 15:38:45 -060052}
53
Simon Glass6ca19772017-04-10 11:34:54 -060054int led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glasscce3aed2015-06-23 15:38:45 -060055{
56 struct led_ops *ops = led_get_ops(dev);
57
Simon Glass6ca19772017-04-10 11:34:54 -060058 if (!ops->set_state)
Simon Glasscce3aed2015-06-23 15:38:45 -060059 return -ENOSYS;
60
Michael Polyntsov353deb92024-07-19 13:12:12 +040061 if (IS_ENABLED(CONFIG_LED_SW_BLINK) &&
62 led_sw_on_state_change(dev, state))
63 return 0;
64
Simon Glass6ca19772017-04-10 11:34:54 -060065 return ops->set_state(dev, state);
Simon Glasscce3aed2015-06-23 15:38:45 -060066}
67
Simon Glassdc531662017-04-10 11:34:55 -060068enum 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 Polyntsov353deb92024-07-19 13:12:12 +040075 if (IS_ENABLED(CONFIG_LED_SW_BLINK) &&
76 led_sw_is_blinking(dev))
77 return LEDST_BLINK;
78
Simon Glassdc531662017-04-10 11:34:55 -060079 return ops->get_state(dev);
80}
81
Simon Glass3bd0c462017-04-10 11:34:57 -060082int led_set_period(struct udevice *dev, int period_ms)
83{
Mikhail Kshevetskiy8224f162024-07-19 13:12:11 +040084#ifdef CONFIG_LED_BLINK
Simon Glass3bd0c462017-04-10 11:34:57 -060085 struct led_ops *ops = led_get_ops(dev);
86
Mikhail Kshevetskiy8224f162024-07-19 13:12:11 +040087 if (ops->set_period)
88 return ops->set_period(dev, period_ms);
89#endif
Simon Glass3bd0c462017-04-10 11:34:57 -060090
Michael Polyntsov353deb92024-07-19 13:12:12 +040091 if (IS_ENABLED(CONFIG_LED_SW_BLINK))
92 return led_sw_set_period(dev, period_ms);
93
Mikhail Kshevetskiy8224f162024-07-19 13:12:11 +040094 return -ENOSYS;
Simon Glass3bd0c462017-04-10 11:34:57 -060095}
Simon Glass3bd0c462017-04-10 11:34:57 -060096
Marek Vasutc2b7e982022-04-04 01:18:02 +020097static int led_post_bind(struct udevice *dev)
98{
99 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Marek Vasut03cadb62022-04-04 01:23:27 +0200100 const char *default_state;
Marek Vasutc2b7e982022-04-04 01:18:02 +0200101
Rasmus Villemoesad3401f2023-11-17 12:38:06 +0100102 if (!uc_plat->label)
103 uc_plat->label = dev_read_string(dev, "label");
104
Rasmus Villemoes128f87a2023-11-17 12:38:07 +0100105 if (!uc_plat->label && !dev_read_string(dev, "compatible"))
Marek Vasutc2b7e982022-04-04 01:18:02 +0200106 uc_plat->label = ofnode_get_name(dev_ofnode(dev));
107
Marek Vasut03cadb62022-04-04 01:23:27 +0200108 uc_plat->default_state = LEDST_COUNT;
109
110 default_state = dev_read_string(dev, "default-state");
111 if (!default_state)
112 return 0;
113
114 if (!strncmp(default_state, "on", 2))
115 uc_plat->default_state = LEDST_ON;
116 else if (!strncmp(default_state, "off", 3))
117 uc_plat->default_state = LEDST_OFF;
118 else
119 return 0;
120
Michael Polyntsov56250a62024-07-19 13:12:13 +0400121 if (IS_ENABLED(CONFIG_LED_BLINK)) {
122 const char *trigger;
123
124 trigger = dev_read_string(dev, "linux,default-trigger");
125 if (trigger && !strncmp(trigger, "pattern", 7))
126 uc_plat->default_state = LEDST_BLINK;
127 }
128
Marek Vasut03cadb62022-04-04 01:23:27 +0200129 /*
130 * In case the LED has default-state DT property, trigger
131 * probe() to configure its default state during startup.
132 */
Marek Vasut1c2b81f2022-04-22 15:15:54 +0200133 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
134
135 return 0;
Marek Vasut03cadb62022-04-04 01:23:27 +0200136}
137
138static int led_post_probe(struct udevice *dev)
139{
140 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Michael Polyntsov56250a62024-07-19 13:12:13 +0400141 int default_period_ms = 1000;
142 int ret = 0;
Marek Vasut03cadb62022-04-04 01:23:27 +0200143
Michael Polyntsov56250a62024-07-19 13:12:13 +0400144 switch (uc_plat->default_state) {
145 case LEDST_ON:
146 case LEDST_OFF:
147 ret = led_set_state(dev, uc_plat->default_state);
148 break;
149 case LEDST_BLINK:
150 ret = led_set_period(dev, default_period_ms);
151 if (!ret)
152 ret = led_set_state(dev, uc_plat->default_state);
153 break;
154 default:
155 break;
156 }
Marek Vasut03cadb62022-04-04 01:23:27 +0200157
Michael Polyntsov56250a62024-07-19 13:12:13 +0400158 return ret;
Marek Vasutc2b7e982022-04-04 01:18:02 +0200159}
160
Simon Glasscce3aed2015-06-23 15:38:45 -0600161UCLASS_DRIVER(led) = {
162 .id = UCLASS_LED,
163 .name = "led",
Simon Glass71fa5b42020-12-03 16:55:18 -0700164 .per_device_plat_auto = sizeof(struct led_uc_plat),
Marek Vasutc2b7e982022-04-04 01:18:02 +0200165 .post_bind = led_post_bind,
Marek Vasut03cadb62022-04-04 01:23:27 +0200166 .post_probe = led_post_probe,
Simon Glasscce3aed2015-06-23 15:38:45 -0600167};