1
2
3
4
5
6
7#define LOG_CATEGORY UCLASS_LED
8
9#include <common.h>
10#include <dm.h>
11#include <errno.h>
12#include <led.h>
13#include <dm/device-internal.h>
14#include <dm/root.h>
15#include <dm/uclass-internal.h>
16
17int 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) {
27 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
28
29
30 if (uc_plat->label && !strcmp(label, uc_plat->label))
31 return uclass_get_device_tail(dev, 0, devp);
32 }
33
34 return -ENODEV;
35}
36
37int led_set_state(struct udevice *dev, enum led_state_t state)
38{
39 struct led_ops *ops = led_get_ops(dev);
40
41 if (!ops->set_state)
42 return -ENOSYS;
43
44 return ops->set_state(dev, state);
45}
46
47enum 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
57#ifdef CONFIG_LED_BLINK
58int 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
69static int led_post_bind(struct udevice *dev)
70{
71 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
72 const char *default_state;
73
74 uc_plat->label = dev_read_string(dev, "label");
75 if (!uc_plat->label)
76 uc_plat->label = ofnode_get_name(dev_ofnode(dev));
77
78 uc_plat->default_state = LEDST_COUNT;
79
80 default_state = dev_read_string(dev, "default-state");
81 if (!default_state)
82 return 0;
83
84 if (!strncmp(default_state, "on", 2))
85 uc_plat->default_state = LEDST_ON;
86 else if (!strncmp(default_state, "off", 3))
87 uc_plat->default_state = LEDST_OFF;
88 else
89 return 0;
90
91
92
93
94
95 dev_or_flags(dev, DM_FLAG_PROBE_AFTER_BIND);
96
97 return 0;
98}
99
100static int led_post_probe(struct udevice *dev)
101{
102 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
103
104 if (uc_plat->default_state == LEDST_ON ||
105 uc_plat->default_state == LEDST_OFF)
106 led_set_state(dev, uc_plat->default_state);
107
108 return 0;
109}
110
111UCLASS_DRIVER(led) = {
112 .id = UCLASS_LED,
113 .name = "led",
114 .per_device_plat_auto = sizeof(struct led_uc_plat),
115 .post_bind = led_post_bind,
116 .post_probe = led_post_probe,
117};
118