1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
14 struct led_gpio_priv {
15 struct gpio_desc gpio;
18 static int gpio_led_set_state(struct udevice *dev, enum led_state_t state)
20 struct led_gpio_priv *priv = dev_get_priv(dev);
23 if (!dm_gpio_is_valid(&priv->gpio))
30 ret = dm_gpio_get_value(&priv->gpio);
39 return dm_gpio_set_value(&priv->gpio, state);
42 static enum led_state_t gpio_led_get_state(struct udevice *dev)
44 struct led_gpio_priv *priv = dev_get_priv(dev);
47 if (!dm_gpio_is_valid(&priv->gpio))
49 ret = dm_gpio_get_value(&priv->gpio);
53 return ret ? LEDST_ON : LEDST_OFF;
56 static int led_gpio_probe(struct udevice *dev)
58 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
59 struct led_gpio_priv *priv = dev_get_priv(dev);
62 /* Ignore the top-level LED node */
66 ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT);
73 static int led_gpio_remove(struct udevice *dev)
76 * The GPIO driver may have already been removed. We will need to
77 * address this more generally.
79 #ifndef CONFIG_SANDBOX
80 struct led_gpio_priv *priv = dev_get_priv(dev);
82 if (dm_gpio_is_valid(&priv->gpio))
83 dm_gpio_free(dev, &priv->gpio);
89 static int led_gpio_bind(struct udevice *parent)
95 dev_for_each_subnode(node, parent) {
96 struct led_uc_plat *uc_plat;
99 label = ofnode_read_string(node, "label");
101 debug("%s: node %s has no label\n", __func__,
102 ofnode_get_name(node));
105 ret = device_bind_driver_to_node(parent, "gpio_led",
106 ofnode_get_name(node),
110 uc_plat = dev_get_uclass_platdata(dev);
111 uc_plat->label = label;
117 static const struct led_ops gpio_led_ops = {
118 .set_state = gpio_led_set_state,
119 .get_state = gpio_led_get_state,
122 static const struct udevice_id led_gpio_ids[] = {
123 { .compatible = "gpio-leds" },
127 U_BOOT_DRIVER(led_gpio) = {
130 .of_match = led_gpio_ids,
131 .ops = &gpio_led_ops,
132 .priv_auto_alloc_size = sizeof(struct led_gpio_priv),
133 .bind = led_gpio_bind,
134 .probe = led_gpio_probe,
135 .remove = led_gpio_remove,