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