blob: 2f4d69eedddc07fc9ff0c6a277a6558352b3c9db [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass59171122015-06-23 15:38:45 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass59171122015-06-23 15:38:45 -06005 */
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
14int 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) {
Simon Glass56e19872017-04-10 11:34:53 -060024 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
Simon Glass59171122015-06-23 15:38:45 -060025
Simon Glassfb8a5ff2015-07-06 12:54:33 -060026 /* Ignore the top-level LED node */
27 if (uc_plat->label && !strcmp(label, uc_plat->label))
Simon Glass59171122015-06-23 15:38:45 -060028 return uclass_get_device_tail(dev, 0, devp);
29 }
30
Simon Glassfb8a5ff2015-07-06 12:54:33 -060031 return -ENODEV;
Simon Glass59171122015-06-23 15:38:45 -060032}
33
Simon Glassddae9fc2017-04-10 11:34:54 -060034int led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glass59171122015-06-23 15:38:45 -060035{
36 struct led_ops *ops = led_get_ops(dev);
37
Simon Glassddae9fc2017-04-10 11:34:54 -060038 if (!ops->set_state)
Simon Glass59171122015-06-23 15:38:45 -060039 return -ENOSYS;
40
Simon Glassddae9fc2017-04-10 11:34:54 -060041 return ops->set_state(dev, state);
Simon Glass59171122015-06-23 15:38:45 -060042}
43
Simon Glass8f4b6122017-04-10 11:34:55 -060044enum 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
Simon Glass53378da2017-04-10 11:34:57 -060054#ifdef CONFIG_LED_BLINK
55int 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
Simon Glass59171122015-06-23 15:38:45 -060066UCLASS_DRIVER(led) = {
67 .id = UCLASS_LED,
68 .name = "led",
Simon Glass56e19872017-04-10 11:34:53 -060069 .per_device_platdata_auto_alloc_size = sizeof(struct led_uc_plat),
Simon Glass59171122015-06-23 15:38:45 -060070};