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