xref: /openbmc/u-boot/drivers/led/led-uclass.c (revision 63e22517)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <led.h>
11 #include <dm/root.h>
12 #include <dm/uclass-internal.h>
13 
14 int led_get_by_label(const char *label, struct udevice **devp)
15 {
16 	struct udevice *dev;
17 	struct uclass *uc;
18 	int ret;
19 
20 	ret = uclass_get(UCLASS_LED, &uc);
21 	if (ret)
22 		return ret;
23 	uclass_foreach_dev(dev, uc) {
24 		struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
25 
26 		/* Ignore the top-level LED node */
27 		if (uc_plat->label && !strcmp(label, uc_plat->label))
28 			return uclass_get_device_tail(dev, 0, devp);
29 	}
30 
31 	return -ENODEV;
32 }
33 
34 int led_set_state(struct udevice *dev, enum led_state_t state)
35 {
36 	struct led_ops *ops = led_get_ops(dev);
37 
38 	if (!ops->set_state)
39 		return -ENOSYS;
40 
41 	return ops->set_state(dev, state);
42 }
43 
44 enum led_state_t led_get_state(struct udevice *dev)
45 {
46 	struct led_ops *ops = led_get_ops(dev);
47 
48 	if (!ops->get_state)
49 		return -ENOSYS;
50 
51 	return ops->get_state(dev);
52 }
53 
54 #ifdef CONFIG_LED_BLINK
55 int led_set_period(struct udevice *dev, int period_ms)
56 {
57 	struct led_ops *ops = led_get_ops(dev);
58 
59 	if (!ops->set_period)
60 		return -ENOSYS;
61 
62 	return ops->set_period(dev, period_ms);
63 }
64 #endif
65 
66 UCLASS_DRIVER(led) = {
67 	.id		= UCLASS_LED,
68 	.name		= "led",
69 	.per_device_platdata_auto_alloc_size = sizeof(struct led_uc_plat),
70 };
71