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