blob: 5d7bf40896b4d7e0e61737092d3a275fdd08638e [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 <common.h>
10#include <dm.h>
11#include <errno.h>
12#include <led.h>
Patrick Delaunaya5bf9282018-07-27 16:37:07 +020013#include <dm/device-internal.h>
Simon Glasscce3aed2015-06-23 15:38:45 -060014#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) {
Simon Glass71fa5b42020-12-03 16:55:18 -070027 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glasscce3aed2015-06-23 15:38:45 -060028
Simon Glassbf3ac8192015-07-06 12:54:33 -060029 /* Ignore the top-level LED node */
30 if (uc_plat->label && !strcmp(label, uc_plat->label))
Simon Glasscce3aed2015-06-23 15:38:45 -060031 return uclass_get_device_tail(dev, 0, devp);
32 }
33
Simon Glassbf3ac8192015-07-06 12:54:33 -060034 return -ENODEV;
Simon Glasscce3aed2015-06-23 15:38:45 -060035}
36
Simon Glass6ca19772017-04-10 11:34:54 -060037int led_set_state(struct udevice *dev, enum led_state_t state)
Simon Glasscce3aed2015-06-23 15:38:45 -060038{
39 struct led_ops *ops = led_get_ops(dev);
40
Simon Glass6ca19772017-04-10 11:34:54 -060041 if (!ops->set_state)
Simon Glasscce3aed2015-06-23 15:38:45 -060042 return -ENOSYS;
43
Simon Glass6ca19772017-04-10 11:34:54 -060044 return ops->set_state(dev, state);
Simon Glasscce3aed2015-06-23 15:38:45 -060045}
46
Simon Glassdc531662017-04-10 11:34:55 -060047enum 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
Simon Glass3bd0c462017-04-10 11:34:57 -060057#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
Marek Vasut03cadb62022-04-04 01:23:27 +020069/* This is superseded by led_post_bind()/led_post_probe() below. */
Patrick Delaunaya5bf9282018-07-27 16:37:07 +020070int led_default_state(void)
71{
Marek Vasut03cadb62022-04-04 01:23:27 +020072 return 0;
Patrick Delaunaya5bf9282018-07-27 16:37:07 +020073}
74
Marek Vasutc2b7e982022-04-04 01:18:02 +020075static int led_post_bind(struct udevice *dev)
76{
77 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Marek Vasut03cadb62022-04-04 01:23:27 +020078 const char *default_state;
Marek Vasutc2b7e982022-04-04 01:18:02 +020079
80 uc_plat->label = dev_read_string(dev, "label");
81 if (!uc_plat->label)
82 uc_plat->label = ofnode_get_name(dev_ofnode(dev));
83
Marek Vasut03cadb62022-04-04 01:23:27 +020084 uc_plat->default_state = LEDST_COUNT;
85
86 default_state = dev_read_string(dev, "default-state");
87 if (!default_state)
88 return 0;
89
90 if (!strncmp(default_state, "on", 2))
91 uc_plat->default_state = LEDST_ON;
92 else if (!strncmp(default_state, "off", 3))
93 uc_plat->default_state = LEDST_OFF;
94 else
95 return 0;
96
97 /*
98 * In case the LED has default-state DT property, trigger
99 * probe() to configure its default state during startup.
100 */
101 return device_probe(dev);
102}
103
104static int led_post_probe(struct udevice *dev)
105{
106 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
107
108 if (uc_plat->default_state == LEDST_ON ||
109 uc_plat->default_state == LEDST_OFF)
110 led_set_state(dev, uc_plat->default_state);
111
Marek Vasutc2b7e982022-04-04 01:18:02 +0200112 return 0;
113}
114
Simon Glasscce3aed2015-06-23 15:38:45 -0600115UCLASS_DRIVER(led) = {
116 .id = UCLASS_LED,
117 .name = "led",
Simon Glass71fa5b42020-12-03 16:55:18 -0700118 .per_device_plat_auto = sizeof(struct led_uc_plat),
Marek Vasutc2b7e982022-04-04 01:18:02 +0200119 .post_bind = led_post_bind,
Marek Vasut03cadb62022-04-04 01:23:27 +0200120 .post_probe = led_post_probe,
Simon Glasscce3aed2015-06-23 15:38:45 -0600121};