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