Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <led.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 13 | #include <asm/gpio.h> |
| 14 | #include <dm/lists.h> |
| 15 | |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 16 | struct led_gpio_priv { |
| 17 | struct gpio_desc gpio; |
| 18 | }; |
| 19 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 20 | static int gpio_led_set_state(struct udevice *dev, enum led_state_t state) |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 21 | { |
| 22 | struct led_gpio_priv *priv = dev_get_priv(dev); |
Simon Glass | e445dfd | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 23 | int ret; |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 24 | |
| 25 | if (!dm_gpio_is_valid(&priv->gpio)) |
| 26 | return -EREMOTEIO; |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 27 | switch (state) { |
| 28 | case LEDST_OFF: |
| 29 | case LEDST_ON: |
| 30 | break; |
Simon Glass | e445dfd | 2017-04-10 11:34:56 -0600 | [diff] [blame] | 31 | case LEDST_TOGGLE: |
| 32 | ret = dm_gpio_get_value(&priv->gpio); |
| 33 | if (ret < 0) |
| 34 | return ret; |
| 35 | state = !ret; |
| 36 | break; |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 37 | default: |
| 38 | return -ENOSYS; |
| 39 | } |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 40 | |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 41 | return dm_gpio_set_value(&priv->gpio, state); |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 42 | } |
| 43 | |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 44 | static enum led_state_t gpio_led_get_state(struct udevice *dev) |
| 45 | { |
| 46 | struct led_gpio_priv *priv = dev_get_priv(dev); |
| 47 | int ret; |
| 48 | |
| 49 | if (!dm_gpio_is_valid(&priv->gpio)) |
| 50 | return -EREMOTEIO; |
| 51 | ret = dm_gpio_get_value(&priv->gpio); |
| 52 | if (ret < 0) |
| 53 | return ret; |
| 54 | |
| 55 | return ret ? LEDST_ON : LEDST_OFF; |
| 56 | } |
| 57 | |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 58 | static int led_gpio_probe(struct udevice *dev) |
| 59 | { |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 60 | struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev); |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 61 | struct led_gpio_priv *priv = dev_get_priv(dev); |
Patrick Bruenn | 10ab023 | 2018-04-11 11:16:27 +0200 | [diff] [blame] | 62 | int ret; |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 63 | |
| 64 | /* Ignore the top-level LED node */ |
| 65 | if (!uc_plat->label) |
| 66 | return 0; |
Patrick Bruenn | 10ab023 | 2018-04-11 11:16:27 +0200 | [diff] [blame] | 67 | |
| 68 | ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, GPIOD_IS_OUT); |
| 69 | if (ret) |
| 70 | return ret; |
| 71 | |
Patrick Bruenn | 10ab023 | 2018-04-11 11:16:27 +0200 | [diff] [blame] | 72 | return 0; |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int led_gpio_remove(struct udevice *dev) |
| 76 | { |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 77 | /* |
| 78 | * The GPIO driver may have already been removed. We will need to |
| 79 | * address this more generally. |
| 80 | */ |
| 81 | #ifndef CONFIG_SANDBOX |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 82 | struct led_gpio_priv *priv = dev_get_priv(dev); |
| 83 | |
| 84 | if (dm_gpio_is_valid(&priv->gpio)) |
| 85 | dm_gpio_free(dev, &priv->gpio); |
Simon Glass | d783eb3 | 2015-07-06 12:54:34 -0600 | [diff] [blame] | 86 | #endif |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int led_gpio_bind(struct udevice *parent) |
| 92 | { |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 93 | struct udevice *dev; |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 94 | ofnode node; |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 95 | int ret; |
| 96 | |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 97 | dev_for_each_subnode(node, parent) { |
Simon Glass | da01de9 | 2017-04-10 11:34:53 -0600 | [diff] [blame] | 98 | struct led_uc_plat *uc_plat; |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 99 | const char *label; |
| 100 | |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 101 | label = ofnode_read_string(node, "label"); |
Sean Anderson | 9476963 | 2020-09-14 11:02:02 -0400 | [diff] [blame] | 102 | if (!label) |
| 103 | label = ofnode_get_name(node); |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 104 | ret = device_bind_driver_to_node(parent, "gpio_led", |
Simon Glass | e6dd8da | 2017-05-18 20:09:07 -0600 | [diff] [blame] | 105 | ofnode_get_name(node), |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 106 | node, &dev); |
| 107 | if (ret) |
| 108 | return ret; |
| 109 | uc_plat = dev_get_uclass_platdata(dev); |
| 110 | uc_plat->label = label; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static const struct led_ops gpio_led_ops = { |
Simon Glass | 6ca1977 | 2017-04-10 11:34:54 -0600 | [diff] [blame] | 117 | .set_state = gpio_led_set_state, |
Simon Glass | dc53166 | 2017-04-10 11:34:55 -0600 | [diff] [blame] | 118 | .get_state = gpio_led_get_state, |
Simon Glass | 5a9390b | 2015-06-23 15:38:46 -0600 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static const struct udevice_id led_gpio_ids[] = { |
| 122 | { .compatible = "gpio-leds" }, |
| 123 | { } |
| 124 | }; |
| 125 | |
| 126 | U_BOOT_DRIVER(led_gpio) = { |
| 127 | .name = "gpio_led", |
| 128 | .id = UCLASS_LED, |
| 129 | .of_match = led_gpio_ids, |
| 130 | .ops = &gpio_led_ops, |
| 131 | .priv_auto_alloc_size = sizeof(struct led_gpio_priv), |
| 132 | .bind = led_gpio_bind, |
| 133 | .probe = led_gpio_probe, |
| 134 | .remove = led_gpio_remove, |
| 135 | }; |