2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
15 DECLARE_GLOBAL_DATA_PTR;
17 struct led_gpio_priv {
18 struct gpio_desc gpio;
21 static int gpio_led_set_on(struct udevice *dev, int on)
23 struct led_gpio_priv *priv = dev_get_priv(dev);
25 if (!dm_gpio_is_valid(&priv->gpio))
28 return dm_gpio_set_value(&priv->gpio, on);
31 static int led_gpio_probe(struct udevice *dev)
33 struct led_uclass_plat *uc_plat = dev_get_uclass_platdata(dev);
34 struct led_gpio_priv *priv = dev_get_priv(dev);
36 /* Ignore the top-level LED node */
39 return gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
42 static int led_gpio_remove(struct udevice *dev)
45 * The GPIO driver may have already been removed. We will need to
46 * address this more generally.
48 #ifndef CONFIG_SANDBOX
49 struct led_gpio_priv *priv = dev_get_priv(dev);
51 if (dm_gpio_is_valid(&priv->gpio))
52 dm_gpio_free(dev, &priv->gpio);
58 static int led_gpio_bind(struct udevice *parent)
60 const void *blob = gd->fdt_blob;
65 for (node = fdt_first_subnode(blob, parent->of_offset);
67 node = fdt_next_subnode(blob, node)) {
68 struct led_uclass_plat *uc_plat;
71 label = fdt_getprop(blob, node, "label", NULL);
73 debug("%s: node %s has no label\n", __func__,
74 fdt_get_name(blob, node, NULL));
77 ret = device_bind_driver_to_node(parent, "gpio_led",
78 fdt_get_name(blob, node, NULL),
82 uc_plat = dev_get_uclass_platdata(dev);
83 uc_plat->label = label;
89 static const struct led_ops gpio_led_ops = {
90 .set_on = gpio_led_set_on,
93 static const struct udevice_id led_gpio_ids[] = {
94 { .compatible = "gpio-leds" },
98 U_BOOT_DRIVER(led_gpio) = {
101 .of_match = led_gpio_ids,
102 .ops = &gpio_led_ops,
103 .priv_auto_alloc_size = sizeof(struct led_gpio_priv),
104 .bind = led_gpio_bind,
105 .probe = led_gpio_probe,
106 .remove = led_gpio_remove,