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