Simon Glass | 96495d9 | 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> |
| 9 | #include <errno.h> |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 11 | #include <asm/gpio.h> |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 12 | #include <linux/ctype.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * gpio_to_device() - Convert global GPIO number to device, number |
| 16 | * gpio: The numeric representation of the GPIO |
| 17 | * |
| 18 | * Convert the GPIO number to an entry in the list of GPIOs |
| 19 | * or GPIO blocks registered with the GPIO controller. Returns |
| 20 | * entry on success, NULL on error. |
| 21 | */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 22 | static int gpio_to_device(unsigned int gpio, struct udevice **devp, |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 23 | unsigned int *offset) |
| 24 | { |
| 25 | struct gpio_dev_priv *uc_priv; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 26 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 27 | int ret; |
| 28 | |
| 29 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); |
| 30 | dev; |
| 31 | ret = uclass_next_device(&dev)) { |
| 32 | uc_priv = dev->uclass_priv; |
| 33 | if (gpio >= uc_priv->gpio_base && |
| 34 | gpio < uc_priv->gpio_base + uc_priv->gpio_count) { |
| 35 | *devp = dev; |
| 36 | *offset = gpio - uc_priv->gpio_base; |
| 37 | return 0; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /* No such GPIO */ |
| 42 | return ret ? ret : -EINVAL; |
| 43 | } |
| 44 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 45 | int gpio_lookup_name(const char *name, struct udevice **devp, |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 46 | unsigned int *offsetp, unsigned int *gpiop) |
| 47 | { |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 48 | struct gpio_dev_priv *uc_priv = NULL; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 49 | struct udevice *dev; |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 50 | ulong offset; |
| 51 | int numeric; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 52 | int ret; |
| 53 | |
| 54 | if (devp) |
| 55 | *devp = NULL; |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 56 | numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 57 | for (ret = uclass_first_device(UCLASS_GPIO, &dev); |
| 58 | dev; |
| 59 | ret = uclass_next_device(&dev)) { |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 60 | int len; |
| 61 | |
| 62 | uc_priv = dev->uclass_priv; |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 63 | if (numeric != -1) { |
| 64 | offset = numeric - uc_priv->gpio_base; |
| 65 | /* Allow GPIOs to be numbered from 0 */ |
| 66 | if (offset >= 0 && offset < uc_priv->gpio_count) |
| 67 | break; |
| 68 | } |
| 69 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 70 | len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0; |
| 71 | |
Simon Glass | 939cda5 | 2014-06-11 23:29:47 -0600 | [diff] [blame] | 72 | if (!strncasecmp(name, uc_priv->bank_name, len)) { |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 73 | if (!strict_strtoul(name + len, 10, &offset)) |
| 74 | break; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Simon Glass | fe1ef50 | 2014-10-22 21:37:01 -0600 | [diff] [blame] | 78 | if (!dev) |
| 79 | return ret ? ret : -EINVAL; |
| 80 | |
| 81 | if (devp) |
| 82 | *devp = dev; |
| 83 | if (offsetp) |
| 84 | *offsetp = offset; |
| 85 | if (gpiop) |
| 86 | *gpiop = uc_priv->gpio_base + offset; |
| 87 | |
| 88 | return 0; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * gpio_request() - [COMPAT] Request GPIO |
| 93 | * gpio: GPIO number |
| 94 | * label: Name for the requested GPIO |
| 95 | * |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 96 | * The label is copied and allocated so the caller does not need to keep |
| 97 | * the pointer around. |
| 98 | * |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 99 | * This function implements the API that's compatible with current |
| 100 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 101 | * GPIO driver. Returns 0 on success, negative value on error. |
| 102 | */ |
| 103 | int gpio_request(unsigned gpio, const char *label) |
| 104 | { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 105 | struct gpio_dev_priv *uc_priv; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 106 | unsigned int offset; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 107 | struct udevice *dev; |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 108 | char *str; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 109 | int ret; |
| 110 | |
| 111 | ret = gpio_to_device(gpio, &dev, &offset); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 115 | uc_priv = dev->uclass_priv; |
| 116 | if (uc_priv->name[offset]) |
| 117 | return -EBUSY; |
| 118 | str = strdup(label); |
| 119 | if (!str) |
| 120 | return -ENOMEM; |
| 121 | if (gpio_get_ops(dev)->request) { |
| 122 | ret = gpio_get_ops(dev)->request(dev, offset, label); |
| 123 | if (ret) { |
| 124 | free(str); |
| 125 | return ret; |
| 126 | } |
| 127 | } |
| 128 | uc_priv->name[offset] = str; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 129 | |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 130 | return 0; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 134 | * gpio_requestf() - [COMPAT] Request GPIO |
| 135 | * @gpio: GPIO number |
| 136 | * @fmt: Format string for the requested GPIO |
| 137 | * @...: Arguments for the printf() format string |
| 138 | * |
| 139 | * This function implements the API that's compatible with current |
| 140 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 141 | * GPIO driver. Returns 0 on success, negative value on error. |
| 142 | */ |
| 143 | int gpio_requestf(unsigned gpio, const char *fmt, ...) |
| 144 | { |
| 145 | va_list args; |
| 146 | char buf[40]; |
| 147 | |
| 148 | va_start(args, fmt); |
| 149 | vscnprintf(buf, sizeof(buf), fmt, args); |
| 150 | va_end(args); |
| 151 | return gpio_request(gpio, buf); |
| 152 | } |
| 153 | |
| 154 | /** |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 155 | * gpio_free() - [COMPAT] Relinquish GPIO |
| 156 | * gpio: GPIO number |
| 157 | * |
| 158 | * This function implements the API that's compatible with current |
| 159 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 160 | * GPIO driver. Returns 0 on success, negative value on error. |
| 161 | */ |
| 162 | int gpio_free(unsigned gpio) |
| 163 | { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 164 | struct gpio_dev_priv *uc_priv; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 165 | unsigned int offset; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 166 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 167 | int ret; |
| 168 | |
| 169 | ret = gpio_to_device(gpio, &dev, &offset); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 173 | uc_priv = dev->uclass_priv; |
| 174 | if (!uc_priv->name[offset]) |
| 175 | return -ENXIO; |
| 176 | if (gpio_get_ops(dev)->free) { |
| 177 | ret = gpio_get_ops(dev)->free(dev, offset); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | free(uc_priv->name[offset]); |
| 183 | uc_priv->name[offset] = NULL; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int check_reserved(struct udevice *dev, unsigned offset, |
| 189 | const char *func) |
| 190 | { |
| 191 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 192 | |
| 193 | if (!uc_priv->name[offset]) { |
| 194 | printf("%s: %s: error: gpio %s%d not reserved\n", |
| 195 | dev->name, func, |
| 196 | uc_priv->bank_name ? uc_priv->bank_name : "", offset); |
| 197 | return -EBUSY; |
| 198 | } |
| 199 | |
| 200 | return 0; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * gpio_direction_input() - [COMPAT] Set GPIO direction to input |
| 205 | * gpio: GPIO number |
| 206 | * |
| 207 | * This function implements the API that's compatible with current |
| 208 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 209 | * GPIO driver. Returns 0 on success, negative value on error. |
| 210 | */ |
| 211 | int gpio_direction_input(unsigned gpio) |
| 212 | { |
| 213 | unsigned int offset; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 214 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 215 | int ret; |
| 216 | |
| 217 | ret = gpio_to_device(gpio, &dev, &offset); |
| 218 | if (ret) |
| 219 | return ret; |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 220 | ret = check_reserved(dev, offset, "dir_input"); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 221 | |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 222 | return ret ? ret : gpio_get_ops(dev)->direction_input(dev, offset); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
| 226 | * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value |
| 227 | * gpio: GPIO number |
| 228 | * value: Logical value to be set on the GPIO pin |
| 229 | * |
| 230 | * This function implements the API that's compatible with current |
| 231 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 232 | * GPIO driver. Returns 0 on success, negative value on error. |
| 233 | */ |
| 234 | int gpio_direction_output(unsigned gpio, int value) |
| 235 | { |
| 236 | unsigned int offset; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 237 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 238 | int ret; |
| 239 | |
| 240 | ret = gpio_to_device(gpio, &dev, &offset); |
| 241 | if (ret) |
| 242 | return ret; |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 243 | ret = check_reserved(dev, offset, "dir_output"); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 244 | |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 245 | return ret ? ret : |
| 246 | gpio_get_ops(dev)->direction_output(dev, offset, value); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value |
| 251 | * gpio: GPIO number |
| 252 | * |
| 253 | * This function implements the API that's compatible with current |
| 254 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 255 | * GPIO driver. Returns the value of the GPIO pin, or negative value |
| 256 | * on error. |
| 257 | */ |
| 258 | int gpio_get_value(unsigned gpio) |
| 259 | { |
| 260 | unsigned int offset; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 261 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 262 | int ret; |
| 263 | |
| 264 | ret = gpio_to_device(gpio, &dev, &offset); |
| 265 | if (ret) |
| 266 | return ret; |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 267 | ret = check_reserved(dev, offset, "get_value"); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 268 | |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 269 | return ret ? ret : gpio_get_ops(dev)->get_value(dev, offset); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
| 273 | * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin |
| 274 | * gpio: GPIO number |
| 275 | * value: Logical value to be set on the GPIO pin. |
| 276 | * |
| 277 | * This function implements the API that's compatible with current |
| 278 | * GPIO API used in U-Boot. The request is forwarded to particular |
| 279 | * GPIO driver. Returns 0 on success, negative value on error. |
| 280 | */ |
| 281 | int gpio_set_value(unsigned gpio, int value) |
| 282 | { |
| 283 | unsigned int offset; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 284 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 285 | int ret; |
| 286 | |
| 287 | ret = gpio_to_device(gpio, &dev, &offset); |
| 288 | if (ret) |
| 289 | return ret; |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 290 | ret = check_reserved(dev, offset, "set_value"); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 291 | |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 292 | return ret ? ret : gpio_get_ops(dev)->set_value(dev, offset, value); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 295 | const char *gpio_get_bank_info(struct udevice *dev, int *bit_count) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 296 | { |
| 297 | struct gpio_dev_priv *priv; |
| 298 | |
| 299 | /* Must be called on an active device */ |
| 300 | priv = dev->uclass_priv; |
| 301 | assert(priv); |
| 302 | |
| 303 | *bit_count = priv->gpio_count; |
| 304 | return priv->bank_name; |
| 305 | } |
| 306 | |
Simon Glass | 6449a50 | 2014-10-04 11:29:43 -0600 | [diff] [blame] | 307 | static const char * const gpio_function[GPIOF_COUNT] = { |
| 308 | "input", |
| 309 | "output", |
| 310 | "unused", |
| 311 | "unknown", |
| 312 | "func", |
| 313 | }; |
| 314 | |
| 315 | int get_function(struct udevice *dev, int offset, bool skip_unused, |
| 316 | const char **namep) |
| 317 | { |
| 318 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 319 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
| 320 | |
| 321 | BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); |
| 322 | if (!device_active(dev)) |
| 323 | return -ENODEV; |
| 324 | if (offset < 0 || offset >= uc_priv->gpio_count) |
| 325 | return -EINVAL; |
| 326 | if (namep) |
| 327 | *namep = uc_priv->name[offset]; |
| 328 | if (skip_unused && !uc_priv->name[offset]) |
| 329 | return GPIOF_UNUSED; |
| 330 | if (ops->get_function) { |
| 331 | int ret; |
| 332 | |
| 333 | ret = ops->get_function(dev, offset); |
| 334 | if (ret < 0) |
| 335 | return ret; |
| 336 | if (ret >= ARRAY_SIZE(gpio_function)) |
| 337 | return -ENODATA; |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | return GPIOF_UNKNOWN; |
| 342 | } |
| 343 | |
| 344 | int gpio_get_function(struct udevice *dev, int offset, const char **namep) |
| 345 | { |
| 346 | return get_function(dev, offset, true, namep); |
| 347 | } |
| 348 | |
| 349 | int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep) |
| 350 | { |
| 351 | return get_function(dev, offset, false, namep); |
| 352 | } |
| 353 | |
Simon Glass | 0757535 | 2014-10-04 11:29:44 -0600 | [diff] [blame] | 354 | int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize) |
| 355 | { |
| 356 | struct dm_gpio_ops *ops = gpio_get_ops(dev); |
| 357 | struct gpio_dev_priv *priv; |
| 358 | char *str = buf; |
| 359 | int func; |
| 360 | int ret; |
| 361 | int len; |
| 362 | |
| 363 | BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); |
| 364 | |
| 365 | *buf = 0; |
| 366 | priv = dev->uclass_priv; |
| 367 | ret = gpio_get_raw_function(dev, offset, NULL); |
| 368 | if (ret < 0) |
| 369 | return ret; |
| 370 | func = ret; |
| 371 | len = snprintf(str, buffsize, "%s%d: %s", |
| 372 | priv->bank_name ? priv->bank_name : "", |
| 373 | offset, gpio_function[func]); |
| 374 | if (func == GPIOF_INPUT || func == GPIOF_OUTPUT || |
| 375 | func == GPIOF_UNUSED) { |
| 376 | const char *label; |
| 377 | bool used; |
| 378 | |
| 379 | ret = ops->get_value(dev, offset); |
| 380 | if (ret < 0) |
| 381 | return ret; |
| 382 | used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED; |
| 383 | snprintf(str + len, buffsize - len, ": %d [%c]%s%s", |
| 384 | ret, |
| 385 | used ? 'x' : ' ', |
| 386 | used ? " " : "", |
| 387 | label ? label : ""); |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Simon Glass | e5901c9 | 2014-11-10 18:00:21 -0700 | [diff] [blame] | 393 | /* |
| 394 | * get a number comprised of multiple GPIO values. gpio_num_array points to |
| 395 | * the array of gpio pin numbers to scan, terminated by -1. |
| 396 | */ |
| 397 | unsigned gpio_get_values_as_int(const int *gpio_num_array) |
| 398 | { |
| 399 | int gpio; |
| 400 | unsigned bitmask = 1; |
| 401 | unsigned vector = 0; |
| 402 | |
| 403 | while (bitmask && |
| 404 | ((gpio = *gpio_num_array++) != -1)) { |
| 405 | if (gpio_get_value(gpio)) |
| 406 | vector |= bitmask; |
| 407 | bitmask <<= 1; |
| 408 | } |
| 409 | return vector; |
| 410 | } |
| 411 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 412 | /* We need to renumber the GPIOs when any driver is probed/removed */ |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 413 | static int gpio_renumber(struct udevice *removed_dev) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 414 | { |
| 415 | struct gpio_dev_priv *uc_priv; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 416 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 417 | struct uclass *uc; |
| 418 | unsigned base; |
| 419 | int ret; |
| 420 | |
| 421 | ret = uclass_get(UCLASS_GPIO, &uc); |
| 422 | if (ret) |
| 423 | return ret; |
| 424 | |
| 425 | /* Ensure that we have a base for each bank */ |
| 426 | base = 0; |
| 427 | uclass_foreach_dev(dev, uc) { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 428 | if (device_active(dev) && dev != removed_dev) { |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 429 | uc_priv = dev->uclass_priv; |
| 430 | uc_priv->gpio_base = base; |
| 431 | base += uc_priv->gpio_count; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 438 | static int gpio_post_probe(struct udevice *dev) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 439 | { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 440 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 441 | |
| 442 | uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *)); |
| 443 | if (!uc_priv->name) |
| 444 | return -ENOMEM; |
| 445 | |
| 446 | return gpio_renumber(NULL); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 449 | static int gpio_pre_remove(struct udevice *dev) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 450 | { |
Simon Glass | b892d12 | 2014-10-04 11:29:42 -0600 | [diff] [blame] | 451 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 452 | int i; |
| 453 | |
| 454 | for (i = 0; i < uc_priv->gpio_count; i++) { |
| 455 | if (uc_priv->name[i]) |
| 456 | free(uc_priv->name[i]); |
| 457 | } |
| 458 | free(uc_priv->name); |
| 459 | |
| 460 | return gpio_renumber(dev); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | UCLASS_DRIVER(gpio) = { |
| 464 | .id = UCLASS_GPIO, |
| 465 | .name = "gpio", |
| 466 | .post_probe = gpio_post_probe, |
| 467 | .pre_remove = gpio_pre_remove, |
| 468 | .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv), |
| 469 | }; |