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