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