blob: 78ab76050d64953e19b9f767021689263689444d [file] [log] [blame]
Simon Glasscce3aed2015-06-23 15:38:45 -06001/*
2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <led.h>
12#include <dm/root.h>
13#include <dm/uclass-internal.h>
14
15int led_get_by_label(const char *label, struct udevice **devp)
16{
17 struct udevice *dev;
18 struct uclass *uc;
19 int ret;
20
21 ret = uclass_get(UCLASS_LED, &uc);
22 if (ret)
23 return ret;
24 uclass_foreach_dev(dev, uc) {
Simon Glassda01de92017-04-10 11:34:53 -060025 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
Simon Glasscce3aed2015-06-23 15:38:45 -060026
Simon Glassbf3ac8192015-07-06 12:54:33 -060027 /* Ignore the top-level LED node */
28 if (uc_plat->label && !strcmp(label, uc_plat->label))
Simon Glasscce3aed2015-06-23 15:38:45 -060029 return uclass_get_device_tail(dev, 0, devp);
30 }
31
Simon Glassbf3ac8192015-07-06 12:54:33 -060032 return -ENODEV;
Simon Glasscce3aed2015-06-23 15:38:45 -060033}
34
Simon Glass6ca19772017-04-10 11:34:54 -060035int led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glasscce3aed2015-06-23 15:38:45 -060036{
37 struct led_ops *ops = led_get_ops(dev);
38
Simon Glass6ca19772017-04-10 11:34:54 -060039 if (!ops->set_state)
Simon Glasscce3aed2015-06-23 15:38:45 -060040 return -ENOSYS;
41
Simon Glass6ca19772017-04-10 11:34:54 -060042 return ops->set_state(dev, state);
Simon Glasscce3aed2015-06-23 15:38:45 -060043}
44
Simon Glassdc531662017-04-10 11:34:55 -060045enum led_state_t led_get_state(struct udevice *dev)
46{
47 struct led_ops *ops = led_get_ops(dev);
48
49 if (!ops->get_state)
50 return -ENOSYS;
51
52 return ops->get_state(dev);
53}
54
Simon Glass3bd0c462017-04-10 11:34:57 -060055#ifdef CONFIG_LED_BLINK
56int led_set_period(struct udevice *dev, int period_ms)
57{
58 struct led_ops *ops = led_get_ops(dev);
59
60 if (!ops->set_period)
61 return -ENOSYS;
62
63 return ops->set_period(dev, period_ms);
64}
65#endif
66
Simon Glasscce3aed2015-06-23 15:38:45 -060067UCLASS_DRIVER(led) = {
68 .id = UCLASS_LED,
69 .name = "led",
Simon Glassda01de92017-04-10 11:34:53 -060070 .per_device_platdata_auto_alloc_size = sizeof(struct led_uc_plat),
Simon Glasscce3aed2015-06-23 15:38:45 -060071};