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