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