Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 8 | #include <dm/device-internal.h> |
| 9 | #include <dm/lists.h> |
| 10 | #include <dm/uclass-internal.h> |
Eric Nelson | 6c880b7 | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 11 | #include <dt-bindings/gpio/gpio.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 0dac4d5 | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 13 | #include <fdtdec.h> |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Masahiro Yamada | 84b8bf6 | 2016-01-24 23:27:48 +0900 | [diff] [blame] | 16 | #include <linux/bug.h> |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 17 | #include <linux/ctype.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 18 | |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 21 | /** |
| 22 | * gpio_to_device() - Convert global GPIO number to device, number |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 23 | * |
| 24 | * Convert the GPIO number to an entry in the list of GPIOs |
| 25 | * or GPIO blocks registered with the GPIO controller. Returns |
| 26 | * entry on success, NULL on error. |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 27 | * |
| 28 | * @gpio: The numeric representation of the GPIO |
| 29 | * @desc: Returns description (desc->flags will always be 0) |
| 30 | * @return 0 if found, -ENOENT if not found |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 31 | */ |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 32 | static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 33 | { |
| 34 | struct gpio_dev_priv *uc_priv; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 35 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 36 | int ret; |
| 37 | |
| 38 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); |
| 39 | dev; |
| 40 | ret = uclass_next_device(&dev)) { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 41 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 42 | if (gpio >= uc_priv->gpio_base && |
| 43 | gpio < uc_priv->gpio_base + uc_priv->gpio_count) { |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 44 | desc->dev = dev; |
| 45 | desc->offset = gpio - uc_priv->gpio_base; |
| 46 | desc->flags = 0; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /* No such GPIO */ |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 52 | return ret ? ret : -ENOENT; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Simon Glass | 32ec159 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 55 | int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 56 | { |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 57 | struct gpio_dev_priv *uc_priv = NULL; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 58 | struct udevice *dev; |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 59 | ulong offset; |
| 60 | int numeric; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 61 | int ret; |
| 62 | |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 63 | numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 64 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); |
| 65 | dev; |
| 66 | ret = uclass_next_device(&dev)) { |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 67 | int len; |
| 68 | |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 69 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 70 | if (numeric != -1) { |
| 71 | offset = numeric - uc_priv->gpio_base; |
| 72 | /* Allow GPIOs to be numbered from 0 */ |
Tom Rini | 7589791 | 2017-05-10 15:20:15 -0400 | [diff] [blame] | 73 | if (offset < uc_priv->gpio_count) |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 77 | len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0; |
| 78 | |
Simon Glass | 939cda5 | 2014-06-11 23:29:47 -0600 | [diff] [blame] | 79 | if (!strncasecmp(name, uc_priv->bank_name, len)) { |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 80 | if (!strict_strtoul(name + len, 10, &offset)) |
| 81 | break; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 85 | if (!dev) |
| 86 | return ret ? ret : -EINVAL; |
| 87 | |
Simon Glass | 32ec159 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 88 | desc->dev = dev; |
| 89 | desc->offset = offset; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int gpio_lookup_name(const char *name, struct udevice **devp, |
| 95 | unsigned int *offsetp, unsigned int *gpiop) |
| 96 | { |
| 97 | struct gpio_desc desc; |
| 98 | int ret; |
| 99 | |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 100 | if (devp) |
Simon Glass | 32ec159 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 101 | *devp = NULL; |
| 102 | ret = dm_gpio_lookup_name(name, &desc); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
| 106 | if (devp) |
| 107 | *devp = desc.dev; |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 108 | if (offsetp) |
Simon Glass | 32ec159 | 2015-06-23 15:38:40 -0600 | [diff] [blame] | 109 | *offsetp = desc.offset; |
| 110 | if (gpiop) { |
| 111 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev); |
| 112 | |
| 113 | *gpiop = uc_priv->gpio_base + desc.offset; |
| 114 | } |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 115 | |
| 116 | return 0; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 119 | int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc, |
| 120 | struct ofnode_phandle_args *args) |
Eric Nelson | 6c880b7 | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 121 | { |
| 122 | if (args->args_count < 1) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | desc->offset = args->args[0]; |
| 126 | |
| 127 | if (args->args_count < 2) |
| 128 | return 0; |
| 129 | |
| 130 | if (args->args[1] & GPIO_ACTIVE_LOW) |
| 131 | desc->flags = GPIOD_ACTIVE_LOW; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 136 | static int gpio_find_and_xlate(struct gpio_desc *desc, |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 137 | struct ofnode_phandle_args *args) |
Simon Glass | 0dac4d5 | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 138 | { |
| 139 | struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); |
| 140 | |
Eric Nelson | 6c880b7 | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 141 | if (ops->xlate) |
| 142 | return ops->xlate(desc->dev, desc, args); |
Simon Glass | 0dac4d5 | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 143 | else |
Eric Nelson | 6c880b7 | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 144 | return gpio_xlate_offs_flags(desc->dev, desc, args); |
Simon Glass | 0dac4d5 | 2015-01-05 20:05:28 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 147 | #if defined(CONFIG_GPIO_HOG) |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 148 | |
| 149 | struct gpio_hog_priv { |
| 150 | struct gpio_desc gpiod; |
| 151 | }; |
| 152 | |
| 153 | struct gpio_hog_data { |
| 154 | int gpiod_flags; |
| 155 | int value; |
| 156 | u32 val[2]; |
| 157 | }; |
| 158 | |
| 159 | static int gpio_hog_ofdata_to_platdata(struct udevice *dev) |
| 160 | { |
| 161 | struct gpio_hog_data *plat = dev_get_platdata(dev); |
| 162 | const char *nodename; |
| 163 | int ret; |
| 164 | |
| 165 | plat->value = 0; |
| 166 | if (dev_read_bool(dev, "input")) { |
| 167 | plat->gpiod_flags = GPIOD_IS_IN; |
| 168 | } else if (dev_read_bool(dev, "output-high")) { |
| 169 | plat->value = 1; |
| 170 | plat->gpiod_flags = GPIOD_IS_OUT; |
| 171 | } else if (dev_read_bool(dev, "output-low")) { |
| 172 | plat->gpiod_flags = GPIOD_IS_OUT; |
| 173 | } else { |
| 174 | printf("%s: missing gpio-hog state.\n", __func__); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | ret = dev_read_u32_array(dev, "gpios", plat->val, 2); |
| 178 | if (ret) { |
| 179 | printf("%s: wrong gpios property, 2 values needed %d\n", |
| 180 | __func__, ret); |
| 181 | return ret; |
| 182 | } |
| 183 | nodename = dev_read_string(dev, "line-name"); |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 184 | if (nodename) |
| 185 | device_set_name(dev, nodename); |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int gpio_hog_probe(struct udevice *dev) |
| 191 | { |
| 192 | struct gpio_hog_data *plat = dev_get_platdata(dev); |
| 193 | struct gpio_hog_priv *priv = dev_get_priv(dev); |
| 194 | int ret; |
| 195 | |
| 196 | ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog", |
| 197 | plat->val[0], plat->gpiod_flags, |
| 198 | plat->val[1], &priv->gpiod); |
| 199 | if (ret < 0) { |
| 200 | debug("%s: node %s could not get gpio.\n", __func__, |
| 201 | dev->name); |
| 202 | return ret; |
| 203 | } |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 204 | |
| 205 | if (plat->gpiod_flags == GPIOD_IS_OUT) { |
| 206 | ret = dm_gpio_set_value(&priv->gpiod, plat->value); |
| 207 | if (ret < 0) { |
| 208 | debug("%s: node %s could not set gpio.\n", __func__, |
| 209 | dev->name); |
| 210 | return ret; |
| 211 | } |
| 212 | } |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int gpio_hog_probe_all(void) |
| 218 | { |
| 219 | struct udevice *dev; |
| 220 | int ret; |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 221 | int retval = 0; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 222 | |
| 223 | for (uclass_first_device(UCLASS_NOP, &dev); |
| 224 | dev; |
| 225 | uclass_find_next_device(&dev)) { |
| 226 | if (dev->driver == DM_GET_DRIVER(gpio_hog)) { |
| 227 | ret = device_probe(dev); |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 228 | if (ret) { |
| 229 | printf("Failed to probe device %s err: %d\n", |
| 230 | dev->name, ret); |
| 231 | retval = ret; |
| 232 | } |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 236 | return retval; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 239 | int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 240 | { |
| 241 | struct udevice *dev; |
| 242 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 243 | *desc = NULL; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 244 | gpio_hog_probe_all(); |
| 245 | if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) { |
| 246 | struct gpio_hog_priv *priv = dev_get_priv(dev); |
| 247 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 248 | *desc = &priv->gpiod; |
| 249 | return 0; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 252 | return -ENODEV; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | U_BOOT_DRIVER(gpio_hog) = { |
| 256 | .name = "gpio_hog", |
| 257 | .id = UCLASS_NOP, |
| 258 | .ofdata_to_platdata = gpio_hog_ofdata_to_platdata, |
| 259 | .probe = gpio_hog_probe, |
| 260 | .priv_auto_alloc_size = sizeof(struct gpio_hog_priv), |
| 261 | .platdata_auto_alloc_size = sizeof(struct gpio_hog_data), |
| 262 | }; |
| 263 | #else |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 264 | int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc) |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 265 | { |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 266 | return 0; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 267 | } |
| 268 | #endif |
| 269 | |
Simon Glass | efa677f | 2015-06-23 15:38:41 -0600 | [diff] [blame] | 270 | int dm_gpio_request(struct gpio_desc *desc, const char *label) |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 271 | { |
| 272 | struct udevice *dev = desc->dev; |
| 273 | struct gpio_dev_priv *uc_priv; |
| 274 | char *str; |
| 275 | int ret; |
| 276 | |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 277 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 278 | if (uc_priv->name[desc->offset]) |
| 279 | return -EBUSY; |
| 280 | str = strdup(label); |
| 281 | if (!str) |
| 282 | return -ENOMEM; |
| 283 | if (gpio_get_ops(dev)->request) { |
| 284 | ret = gpio_get_ops(dev)->request(dev, desc->offset, label); |
| 285 | if (ret) { |
| 286 | free(str); |
| 287 | return ret; |
| 288 | } |
| 289 | } |
| 290 | uc_priv->name[desc->offset] = str; |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 295 | static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...) |
| 296 | { |
Simon Glass | 27084c0 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 297 | #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 298 | va_list args; |
| 299 | char buf[40]; |
| 300 | |
| 301 | va_start(args, fmt); |
| 302 | vscnprintf(buf, sizeof(buf), fmt, args); |
| 303 | va_end(args); |
| 304 | return dm_gpio_request(desc, buf); |
Simon Glass | 4dc5259 | 2015-12-29 05:22:48 -0700 | [diff] [blame] | 305 | #else |
| 306 | return dm_gpio_request(desc, fmt); |
| 307 | #endif |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 310 | /** |
| 311 | * gpio_request() - [COMPAT] Request GPIO |
| 312 | * gpio: GPIO number |
| 313 | * label: Name for the requested GPIO |
| 314 | * |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 315 | * The label is copied and allocated so the caller does not need to keep |
| 316 | * the pointer around. |
| 317 | * |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 318 | * This function implements the API that's compatible with current |
| 319 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 320 | * GPIO driver. Returns 0 on success, negative value on error. |
| 321 | */ |
| 322 | int gpio_request(unsigned gpio, const char *label) |
| 323 | { |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 324 | struct gpio_desc desc; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 325 | int ret; |
| 326 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 327 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 328 | if (ret) |
| 329 | return ret; |
| 330 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 331 | return dm_gpio_request(&desc, label); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 335 | * gpio_requestf() - [COMPAT] Request GPIO |
| 336 | * @gpio: GPIO number |
| 337 | * @fmt: Format string for the requested GPIO |
| 338 | * @...: Arguments for the printf() format string |
| 339 | * |
| 340 | * This function implements the API that's compatible with current |
| 341 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 342 | * GPIO driver. Returns 0 on success, negative value on error. |
| 343 | */ |
| 344 | int gpio_requestf(unsigned gpio, const char *fmt, ...) |
| 345 | { |
Simon Glass | 27084c0 | 2019-09-25 08:56:27 -0600 | [diff] [blame] | 346 | #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF) |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 347 | va_list args; |
| 348 | char buf[40]; |
| 349 | |
| 350 | va_start(args, fmt); |
| 351 | vscnprintf(buf, sizeof(buf), fmt, args); |
| 352 | va_end(args); |
| 353 | return gpio_request(gpio, buf); |
Simon Glass | 4dc5259 | 2015-12-29 05:22:48 -0700 | [diff] [blame] | 354 | #else |
| 355 | return gpio_request(gpio, fmt); |
| 356 | #endif |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 357 | } |
| 358 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 359 | int _dm_gpio_free(struct udevice *dev, uint offset) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 360 | { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 361 | struct gpio_dev_priv *uc_priv; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 362 | int ret; |
| 363 | |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 364 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 365 | if (!uc_priv->name[offset]) |
| 366 | return -ENXIO; |
Simon Glass | 093152f | 2020-02-04 20:15:17 -0700 | [diff] [blame] | 367 | if (gpio_get_ops(dev)->rfree) { |
| 368 | ret = gpio_get_ops(dev)->rfree(dev, offset); |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 369 | if (ret) |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | free(uc_priv->name[offset]); |
| 374 | uc_priv->name[offset] = NULL; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 379 | /** |
| 380 | * gpio_free() - [COMPAT] Relinquish GPIO |
| 381 | * gpio: GPIO number |
| 382 | * |
| 383 | * This function implements the API that's compatible with current |
| 384 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 385 | * GPIO driver. Returns 0 on success, negative value on error. |
| 386 | */ |
| 387 | int gpio_free(unsigned gpio) |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 388 | { |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 389 | struct gpio_desc desc; |
| 390 | int ret; |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 391 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 392 | ret = gpio_to_device(gpio, &desc); |
| 393 | if (ret) |
| 394 | return ret; |
| 395 | |
| 396 | return _dm_gpio_free(desc.dev, desc.offset); |
| 397 | } |
| 398 | |
Simon Glass | 17c43f1 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 399 | static int check_reserved(const struct gpio_desc *desc, const char *func) |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 400 | { |
Simon Glass | eca4866 | 2015-07-02 18:16:16 -0600 | [diff] [blame] | 401 | struct gpio_dev_priv *uc_priv; |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 402 | |
Simon Glass | eca4866 | 2015-07-02 18:16:16 -0600 | [diff] [blame] | 403 | if (!dm_gpio_is_valid(desc)) |
| 404 | return -ENOENT; |
| 405 | |
| 406 | uc_priv = dev_get_uclass_priv(desc->dev); |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 407 | if (!uc_priv->name[desc->offset]) { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 408 | printf("%s: %s: error: gpio %s%d not reserved\n", |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 409 | desc->dev->name, func, |
| 410 | uc_priv->bank_name ? uc_priv->bank_name : "", |
| 411 | desc->offset); |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 412 | return -EBUSY; |
| 413 | } |
| 414 | |
| 415 | return 0; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /** |
| 419 | * gpio_direction_input() - [COMPAT] Set GPIO direction to input |
| 420 | * gpio: GPIO number |
| 421 | * |
| 422 | * This function implements the API that's compatible with current |
| 423 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 424 | * GPIO driver. Returns 0 on success, negative value on error. |
| 425 | */ |
| 426 | int gpio_direction_input(unsigned gpio) |
| 427 | { |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 428 | struct gpio_desc desc; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 429 | int ret; |
| 430 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 431 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 432 | if (ret) |
| 433 | return ret; |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 434 | ret = check_reserved(&desc, "dir_input"); |
| 435 | if (ret) |
| 436 | return ret; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 437 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 438 | return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /** |
| 442 | * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value |
| 443 | * gpio: GPIO number |
| 444 | * value: Logical value to be set on the GPIO pin |
| 445 | * |
| 446 | * This function implements the API that's compatible with current |
| 447 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 448 | * GPIO driver. Returns 0 on success, negative value on error. |
| 449 | */ |
| 450 | int gpio_direction_output(unsigned gpio, int value) |
| 451 | { |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 452 | struct gpio_desc desc; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 453 | int ret; |
| 454 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 455 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 456 | if (ret) |
| 457 | return ret; |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 458 | ret = check_reserved(&desc, "dir_output"); |
| 459 | if (ret) |
| 460 | return ret; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 461 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 462 | return gpio_get_ops(desc.dev)->direction_output(desc.dev, |
| 463 | desc.offset, value); |
| 464 | } |
| 465 | |
Simon Glass | 17c43f1 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 466 | int dm_gpio_get_value(const struct gpio_desc *desc) |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 467 | { |
| 468 | int value; |
| 469 | int ret; |
| 470 | |
| 471 | ret = check_reserved(desc, "get_value"); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
| 475 | value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset); |
| 476 | |
| 477 | return desc->flags & GPIOD_ACTIVE_LOW ? !value : value; |
| 478 | } |
| 479 | |
Simon Glass | 17c43f1 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 480 | int dm_gpio_set_value(const struct gpio_desc *desc, int value) |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 481 | { |
| 482 | int ret; |
| 483 | |
| 484 | ret = check_reserved(desc, "set_value"); |
| 485 | if (ret) |
| 486 | return ret; |
| 487 | |
| 488 | if (desc->flags & GPIOD_ACTIVE_LOW) |
| 489 | value = !value; |
| 490 | gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value); |
| 491 | return 0; |
| 492 | } |
| 493 | |
mario.six@gdsys.cc | 53ecdfb | 2016-05-25 15:15:21 +0200 | [diff] [blame] | 494 | int dm_gpio_get_open_drain(struct gpio_desc *desc) |
| 495 | { |
| 496 | struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); |
| 497 | int ret; |
| 498 | |
| 499 | ret = check_reserved(desc, "get_open_drain"); |
| 500 | if (ret) |
| 501 | return ret; |
| 502 | |
| 503 | if (ops->set_open_drain) |
| 504 | return ops->get_open_drain(desc->dev, desc->offset); |
| 505 | else |
| 506 | return -ENOSYS; |
| 507 | } |
| 508 | |
| 509 | int dm_gpio_set_open_drain(struct gpio_desc *desc, int value) |
| 510 | { |
| 511 | struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); |
| 512 | int ret; |
| 513 | |
| 514 | ret = check_reserved(desc, "set_open_drain"); |
| 515 | if (ret) |
| 516 | return ret; |
| 517 | |
| 518 | if (ops->set_open_drain) |
| 519 | ret = ops->set_open_drain(desc->dev, desc->offset, value); |
| 520 | else |
| 521 | return 0; /* feature not supported -> ignore setting */ |
| 522 | |
| 523 | return ret; |
| 524 | } |
| 525 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 526 | int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags) |
| 527 | { |
| 528 | struct udevice *dev = desc->dev; |
| 529 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
| 530 | int ret; |
| 531 | |
| 532 | ret = check_reserved(desc, "set_dir"); |
| 533 | if (ret) |
| 534 | return ret; |
| 535 | |
| 536 | if (flags & GPIOD_IS_OUT) { |
| 537 | int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0; |
| 538 | |
| 539 | if (flags & GPIOD_ACTIVE_LOW) |
| 540 | value = !value; |
| 541 | ret = ops->direction_output(dev, desc->offset, value); |
| 542 | } else if (flags & GPIOD_IS_IN) { |
| 543 | ret = ops->direction_input(dev, desc->offset); |
| 544 | } |
| 545 | if (ret) |
| 546 | return ret; |
| 547 | /* |
| 548 | * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in |
| 549 | * futures |
| 550 | */ |
| 551 | desc->flags = flags; |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | int dm_gpio_set_dir(struct gpio_desc *desc) |
| 557 | { |
| 558 | return dm_gpio_set_dir_flags(desc, desc->flags); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** |
| 562 | * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value |
| 563 | * gpio: GPIO number |
| 564 | * |
| 565 | * This function implements the API that's compatible with current |
| 566 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 567 | * GPIO driver. Returns the value of the GPIO pin, or negative value |
| 568 | * on error. |
| 569 | */ |
| 570 | int gpio_get_value(unsigned gpio) |
| 571 | { |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 572 | int ret; |
| 573 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 574 | struct gpio_desc desc; |
| 575 | |
| 576 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 577 | if (ret) |
| 578 | return ret; |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 579 | return dm_gpio_get_value(&desc); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
| 583 | * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin |
| 584 | * gpio: GPIO number |
| 585 | * value: Logical value to be set on the GPIO pin. |
| 586 | * |
| 587 | * This function implements the API that's compatible with current |
| 588 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 589 | * GPIO driver. Returns 0 on success, negative value on error. |
| 590 | */ |
| 591 | int gpio_set_value(unsigned gpio, int value) |
| 592 | { |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 593 | struct gpio_desc desc; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 594 | int ret; |
| 595 | |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 596 | ret = gpio_to_device(gpio, &desc); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 597 | if (ret) |
| 598 | return ret; |
Simon Glass | ae7123f | 2015-01-05 20:05:27 -0700 | [diff] [blame] | 599 | return dm_gpio_set_value(&desc, value); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 602 | const char *gpio_get_bank_info(struct udevice *dev, int *bit_count) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 603 | { |
| 604 | struct gpio_dev_priv *priv; |
| 605 | |
| 606 | /* Must be called on an active device */ |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 607 | priv = dev_get_uclass_priv(dev); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 608 | assert(priv); |
| 609 | |
| 610 | *bit_count = priv->gpio_count; |
| 611 | return priv->bank_name; |
| 612 | } |
| 613 | |
Simon Glass | 6449a50 | 2014-10-04 11:29:43 -0600 | [diff] [blame] | 614 | static const char * const gpio_function[GPIOF_COUNT] = { |
| 615 | "input", |
| 616 | "output", |
| 617 | "unused", |
| 618 | "unknown", |
| 619 | "func", |
| 620 | }; |
| 621 | |
Masahiro Yamada | fb07f97 | 2017-06-22 16:50:25 +0900 | [diff] [blame] | 622 | static int get_function(struct udevice *dev, int offset, bool skip_unused, |
| 623 | const char **namep) |
Simon Glass | 6449a50 | 2014-10-04 11:29:43 -0600 | [diff] [blame] | 624 | { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 625 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 6449a50 | 2014-10-04 11:29:43 -0600 | [diff] [blame] | 626 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
| 627 | |
| 628 | BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); |
| 629 | if (!device_active(dev)) |
| 630 | return -ENODEV; |
| 631 | if (offset < 0 || offset >= uc_priv->gpio_count) |
| 632 | return -EINVAL; |
| 633 | if (namep) |
| 634 | *namep = uc_priv->name[offset]; |
| 635 | if (skip_unused && !uc_priv->name[offset]) |
| 636 | return GPIOF_UNUSED; |
| 637 | if (ops->get_function) { |
| 638 | int ret; |
| 639 | |
| 640 | ret = ops->get_function(dev, offset); |
| 641 | if (ret < 0) |
| 642 | return ret; |
| 643 | if (ret >= ARRAY_SIZE(gpio_function)) |
| 644 | return -ENODATA; |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | return GPIOF_UNKNOWN; |
| 649 | } |
| 650 | |
| 651 | int gpio_get_function(struct udevice *dev, int offset, const char **namep) |
| 652 | { |
| 653 | return get_function(dev, offset, true, namep); |
| 654 | } |
| 655 | |
| 656 | int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep) |
| 657 | { |
| 658 | return get_function(dev, offset, false, namep); |
| 659 | } |
| 660 | |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 661 | int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize) |
| 662 | { |
| 663 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
| 664 | struct gpio_dev_priv *priv; |
| 665 | char *str = buf; |
| 666 | int func; |
| 667 | int ret; |
| 668 | int len; |
| 669 | |
| 670 | BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); |
| 671 | |
| 672 | *buf = 0; |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 673 | priv = dev_get_uclass_priv(dev); |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 674 | ret = gpio_get_raw_function(dev, offset, NULL); |
| 675 | if (ret < 0) |
| 676 | return ret; |
| 677 | func = ret; |
| 678 | len = snprintf(str, buffsize, "%s%d: %s", |
| 679 | priv->bank_name ? priv->bank_name : "", |
| 680 | offset, gpio_function[func]); |
| 681 | if (func == GPIOF_INPUT || func == GPIOF_OUTPUT || |
| 682 | func == GPIOF_UNUSED) { |
| 683 | const char *label; |
| 684 | bool used; |
| 685 | |
| 686 | ret = ops->get_value(dev, offset); |
| 687 | if (ret < 0) |
| 688 | return ret; |
| 689 | used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED; |
| 690 | snprintf(str + len, buffsize - len, ": %d [%c]%s%s", |
| 691 | ret, |
| 692 | used ? 'x' : ' ', |
| 693 | used ? " " : "", |
| 694 | label ? label : ""); |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
Simon Glass | 962f5ca | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 700 | int gpio_claim_vector(const int *gpio_num_array, const char *fmt) |
| 701 | { |
| 702 | int i, ret; |
| 703 | int gpio; |
| 704 | |
| 705 | for (i = 0; i < 32; i++) { |
| 706 | gpio = gpio_num_array[i]; |
| 707 | if (gpio == -1) |
| 708 | break; |
| 709 | ret = gpio_requestf(gpio, fmt, i); |
| 710 | if (ret) |
| 711 | goto err; |
| 712 | ret = gpio_direction_input(gpio); |
| 713 | if (ret) { |
| 714 | gpio_free(gpio); |
| 715 | goto err; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | err: |
| 721 | for (i--; i >= 0; i--) |
| 722 | gpio_free(gpio_num_array[i]); |
| 723 | |
| 724 | return ret; |
| 725 | } |
| 726 | |
Simon Glass | e5901c9 | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 727 | /* |
| 728 | * get a number comprised of multiple GPIO values. gpio_num_array points to |
| 729 | * the array of gpio pin numbers to scan, terminated by -1. |
| 730 | */ |
Simon Glass | 962f5ca | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 731 | int gpio_get_values_as_int(const int *gpio_list) |
Simon Glass | e5901c9 | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 732 | { |
| 733 | int gpio; |
| 734 | unsigned bitmask = 1; |
| 735 | unsigned vector = 0; |
Simon Glass | 962f5ca | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 736 | int ret; |
Simon Glass | e5901c9 | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 737 | |
| 738 | while (bitmask && |
Simon Glass | 962f5ca | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 739 | ((gpio = *gpio_list++) != -1)) { |
| 740 | ret = gpio_get_value(gpio); |
| 741 | if (ret < 0) |
| 742 | return ret; |
| 743 | else if (ret) |
Simon Glass | e5901c9 | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 744 | vector |= bitmask; |
| 745 | bitmask <<= 1; |
| 746 | } |
Simon Glass | 962f5ca | 2015-04-14 21:03:20 -0600 | [diff] [blame] | 747 | |
Simon Glass | e5901c9 | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 748 | return vector; |
| 749 | } |
| 750 | |
Simon Glass | 17c43f1 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 751 | int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count) |
Simon Glass | bbf2478 | 2016-03-06 19:27:50 -0700 | [diff] [blame] | 752 | { |
| 753 | unsigned bitmask = 1; |
| 754 | unsigned vector = 0; |
| 755 | int ret, i; |
| 756 | |
| 757 | for (i = 0; i < count; i++) { |
| 758 | ret = dm_gpio_get_value(&desc_list[i]); |
| 759 | if (ret < 0) |
| 760 | return ret; |
| 761 | else if (ret) |
| 762 | vector |= bitmask; |
| 763 | bitmask <<= 1; |
| 764 | } |
| 765 | |
| 766 | return vector; |
| 767 | } |
| 768 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 769 | /** |
| 770 | * gpio_request_tail: common work for requesting a gpio. |
| 771 | * |
| 772 | * ret: return value from previous work in function which calls |
| 773 | * this function. |
| 774 | * This seems bogus (why calling this function instead not |
| 775 | * calling it and end caller function instead?). |
| 776 | * Because on error in caller function we want to set some |
| 777 | * default values in gpio desc and have a common error |
| 778 | * debug message, which provides this function. |
| 779 | * nodename: Name of node for which gpio gets requested |
| 780 | * used for gpio label name. |
| 781 | * args: pointer to output arguments structure |
| 782 | * list_name: Name of GPIO list |
| 783 | * used for gpio label name. |
| 784 | * index: gpio index in gpio list |
| 785 | * used for gpio label name. |
| 786 | * desc: pointer to gpio descriptor, filled from this |
| 787 | * function. |
| 788 | * flags: gpio flags to use. |
| 789 | * add_index: should index added to gpio label name |
| 790 | * gpio_dev: pointer to gpio device from which the gpio |
| 791 | * will be requested. If NULL try to get the |
| 792 | * gpio device with uclass_get_device_by_ofnode() |
| 793 | * |
| 794 | * return: In error case this function sets default values in |
| 795 | * gpio descriptor, also emmits a debug message. |
| 796 | * On success it returns 0 else the error code from |
| 797 | * function calls, or the error code passed through |
| 798 | * ret to this function. |
| 799 | * |
| 800 | */ |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 801 | static int gpio_request_tail(int ret, const char *nodename, |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 802 | struct ofnode_phandle_args *args, |
| 803 | const char *list_name, int index, |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 804 | struct gpio_desc *desc, int flags, |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 805 | bool add_index, struct udevice *gpio_dev) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 806 | { |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 807 | desc->dev = gpio_dev; |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 808 | desc->offset = 0; |
Eric Nelson | 6c880b7 | 2016-04-24 16:32:40 -0700 | [diff] [blame] | 809 | desc->flags = 0; |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 810 | if (ret) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 811 | goto err; |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 812 | |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 813 | if (!desc->dev) { |
| 814 | ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node, |
| 815 | &desc->dev); |
| 816 | if (ret) { |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 817 | debug("%s: uclass_get_device_by_ofnode failed\n", |
| 818 | __func__); |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 819 | goto err; |
| 820 | } |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 821 | } |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 822 | ret = gpio_find_and_xlate(desc, args); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 823 | if (ret) { |
| 824 | debug("%s: gpio_find_and_xlate failed\n", __func__); |
| 825 | goto err; |
| 826 | } |
| 827 | ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s", |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 828 | nodename, list_name, index); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 829 | if (ret) { |
| 830 | debug("%s: dm_gpio_requestf failed\n", __func__); |
| 831 | goto err; |
| 832 | } |
| 833 | ret = dm_gpio_set_dir_flags(desc, flags | desc->flags); |
| 834 | if (ret) { |
| 835 | debug("%s: dm_gpio_set_dir failed\n", __func__); |
| 836 | goto err; |
| 837 | } |
| 838 | |
| 839 | return 0; |
| 840 | err: |
| 841 | debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n", |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 842 | __func__, nodename, list_name, index, ret); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 843 | return ret; |
| 844 | } |
| 845 | |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 846 | static int _gpio_request_by_name_nodev(ofnode node, const char *list_name, |
| 847 | int index, struct gpio_desc *desc, |
| 848 | int flags, bool add_index) |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 849 | { |
| 850 | struct ofnode_phandle_args args; |
| 851 | int ret; |
| 852 | |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 853 | ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0, |
| 854 | index, &args); |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 855 | |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 856 | return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, |
| 857 | index, desc, flags, add_index, NULL); |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 858 | } |
| 859 | |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 860 | int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index, |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 861 | struct gpio_desc *desc, int flags) |
| 862 | { |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 863 | return _gpio_request_by_name_nodev(node, list_name, index, desc, flags, |
| 864 | index > 0); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 867 | int gpio_request_by_name(struct udevice *dev, const char *list_name, int index, |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 868 | struct gpio_desc *desc, int flags) |
| 869 | { |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 870 | struct ofnode_phandle_args args; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 871 | ofnode node; |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 872 | int ret; |
| 873 | |
| 874 | ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, |
| 875 | index, &args); |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 876 | node = dev_ofnode(dev); |
| 877 | return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, |
| 878 | index, desc, flags, index > 0, NULL); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 881 | int gpio_request_list_by_name_nodev(ofnode node, const char *list_name, |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 882 | struct gpio_desc *desc, int max_count, |
| 883 | int flags) |
| 884 | { |
| 885 | int count; |
| 886 | int ret; |
| 887 | |
Przemyslaw Marczak | 2984e7a | 2015-03-31 18:57:16 +0200 | [diff] [blame] | 888 | for (count = 0; count < max_count; count++) { |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 889 | ret = _gpio_request_by_name_nodev(node, list_name, count, |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 890 | &desc[count], flags, true); |
| 891 | if (ret == -ENOENT) |
| 892 | break; |
| 893 | else if (ret) |
| 894 | goto err; |
| 895 | } |
| 896 | |
| 897 | /* We ran out of GPIOs in the list */ |
| 898 | return count; |
| 899 | |
| 900 | err: |
| 901 | gpio_free_list_nodev(desc, count - 1); |
| 902 | |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | int gpio_request_list_by_name(struct udevice *dev, const char *list_name, |
| 907 | struct gpio_desc *desc, int max_count, |
| 908 | int flags) |
| 909 | { |
| 910 | /* |
| 911 | * This isn't ideal since we don't use dev->name in the debug() |
| 912 | * calls in gpio_request_by_name(), but we can do this until |
| 913 | * gpio_request_list_by_name_nodev() can be dropped. |
| 914 | */ |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 915 | return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc, |
| 916 | max_count, flags); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | int gpio_get_list_count(struct udevice *dev, const char *list_name) |
| 920 | { |
| 921 | int ret; |
| 922 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 923 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 924 | list_name, "#gpio-cells", 0, -1, |
| 925 | NULL); |
| 926 | if (ret) { |
| 927 | debug("%s: Node '%s', property '%s', GPIO count failed: %d\n", |
| 928 | __func__, dev->name, list_name, ret); |
| 929 | } |
| 930 | |
| 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc) |
| 935 | { |
| 936 | /* For now, we don't do any checking of dev */ |
| 937 | return _dm_gpio_free(desc->dev, desc->offset); |
| 938 | } |
| 939 | |
| 940 | int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count) |
| 941 | { |
| 942 | int i; |
| 943 | |
| 944 | /* For now, we don't do any checking of dev */ |
| 945 | for (i = 0; i < count; i++) |
| 946 | dm_gpio_free(dev, &desc[i]); |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | int gpio_free_list_nodev(struct gpio_desc *desc, int count) |
| 952 | { |
| 953 | return gpio_free_list(NULL, desc, count); |
| 954 | } |
| 955 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 956 | /* We need to renumber the GPIOs when any driver is probed/removed */ |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 957 | static int gpio_renumber(struct udevice *removed_dev) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 958 | { |
| 959 | struct gpio_dev_priv *uc_priv; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 960 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 961 | struct uclass *uc; |
| 962 | unsigned base; |
| 963 | int ret; |
| 964 | |
| 965 | ret = uclass_get(UCLASS_GPIO, &uc); |
| 966 | if (ret) |
| 967 | return ret; |
| 968 | |
| 969 | /* Ensure that we have a base for each bank */ |
| 970 | base = 0; |
| 971 | uclass_foreach_dev(dev, uc) { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 972 | if (device_active(dev) && dev != removed_dev) { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 973 | uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 974 | uc_priv->gpio_base = base; |
| 975 | base += uc_priv->gpio_count; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
Simon Glass | 17c43f1 | 2016-03-06 19:27:51 -0700 | [diff] [blame] | 982 | int gpio_get_number(const struct gpio_desc *desc) |
Simon Glass | 56a71f8 | 2015-03-25 12:21:58 -0600 | [diff] [blame] | 983 | { |
| 984 | struct udevice *dev = desc->dev; |
| 985 | struct gpio_dev_priv *uc_priv; |
| 986 | |
| 987 | if (!dev) |
| 988 | return -1; |
| 989 | uc_priv = dev->uclass_priv; |
| 990 | |
| 991 | return uc_priv->gpio_base + desc->offset; |
| 992 | } |
| 993 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 994 | static int gpio_post_probe(struct udevice *dev) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 995 | { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 996 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 997 | |
| 998 | uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *)); |
| 999 | if (!uc_priv->name) |
| 1000 | return -ENOMEM; |
| 1001 | |
| 1002 | return gpio_renumber(NULL); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 1005 | static int gpio_pre_remove(struct udevice *dev) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1006 | { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 1007 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 1008 | int i; |
| 1009 | |
| 1010 | for (i = 0; i < uc_priv->gpio_count; i++) { |
| 1011 | if (uc_priv->name[i]) |
| 1012 | free(uc_priv->name[i]); |
| 1013 | } |
| 1014 | free(uc_priv->name); |
| 1015 | |
| 1016 | return gpio_renumber(dev); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1019 | int gpio_dev_request_index(struct udevice *dev, const char *nodename, |
| 1020 | char *list_name, int index, int flags, |
| 1021 | int dtflags, struct gpio_desc *desc) |
| 1022 | { |
| 1023 | struct ofnode_phandle_args args; |
| 1024 | |
| 1025 | args.node = ofnode_null(); |
| 1026 | args.args_count = 2; |
| 1027 | args.args[0] = index; |
| 1028 | args.args[1] = dtflags; |
| 1029 | |
| 1030 | return gpio_request_tail(0, nodename, &args, list_name, index, desc, |
| 1031 | flags, 0, dev); |
| 1032 | } |
| 1033 | |
Michal Simek | 1b4c2aa | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1034 | static int gpio_post_bind(struct udevice *dev) |
| 1035 | { |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1036 | struct udevice *child; |
| 1037 | ofnode node; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1038 | |
Michal Simek | 1b4c2aa | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1039 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 1040 | struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev); |
| 1041 | static int reloc_done; |
| 1042 | |
| 1043 | if (!reloc_done) { |
| 1044 | if (ops->request) |
| 1045 | ops->request += gd->reloc_off; |
Simon Glass | 093152f | 2020-02-04 20:15:17 -0700 | [diff] [blame] | 1046 | if (ops->rfree) |
| 1047 | ops->rfree += gd->reloc_off; |
Michal Simek | 1b4c2aa | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1048 | if (ops->direction_input) |
| 1049 | ops->direction_input += gd->reloc_off; |
| 1050 | if (ops->direction_output) |
| 1051 | ops->direction_output += gd->reloc_off; |
| 1052 | if (ops->get_value) |
| 1053 | ops->get_value += gd->reloc_off; |
| 1054 | if (ops->set_value) |
| 1055 | ops->set_value += gd->reloc_off; |
| 1056 | if (ops->get_open_drain) |
| 1057 | ops->get_open_drain += gd->reloc_off; |
| 1058 | if (ops->set_open_drain) |
| 1059 | ops->set_open_drain += gd->reloc_off; |
| 1060 | if (ops->get_function) |
| 1061 | ops->get_function += gd->reloc_off; |
| 1062 | if (ops->xlate) |
| 1063 | ops->xlate += gd->reloc_off; |
| 1064 | |
| 1065 | reloc_done++; |
| 1066 | } |
| 1067 | #endif |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1068 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1069 | if (IS_ENABLED(CONFIG_GPIO_HOG)) { |
| 1070 | dev_for_each_subnode(node, dev) { |
| 1071 | if (ofnode_read_bool(node, "gpio-hog")) { |
| 1072 | const char *name = ofnode_get_name(node); |
| 1073 | int ret; |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1074 | |
Heiko Schocher | 49b10cb | 2019-07-17 06:59:51 +0200 | [diff] [blame] | 1075 | ret = device_bind_driver_to_node(dev, |
| 1076 | "gpio_hog", |
| 1077 | name, node, |
| 1078 | &child); |
| 1079 | if (ret) |
| 1080 | return ret; |
| 1081 | } |
Heiko Schocher | 5fc7cf8 | 2019-06-12 06:11:46 +0200 | [diff] [blame] | 1082 | } |
| 1083 | } |
Michal Simek | 1b4c2aa | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1084 | return 0; |
| 1085 | } |
| 1086 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1087 | UCLASS_DRIVER(gpio) = { |
| 1088 | .id = UCLASS_GPIO, |
| 1089 | .name = "gpio", |
Bhuvanchandra DV | ae89bb0 | 2015-06-01 18:37:15 +0530 | [diff] [blame] | 1090 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1091 | .post_probe = gpio_post_probe, |
Michal Simek | 1b4c2aa | 2018-07-12 12:42:27 +0200 | [diff] [blame] | 1092 | .post_bind = gpio_post_bind, |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1093 | .pre_remove = gpio_pre_remove, |
| 1094 | .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv), |
| 1095 | }; |