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 <dm/device-internal.h> 10 #include <dm/lists.h> 11 #include <dm/uclass-internal.h> 12 #include <dt-bindings/gpio/gpio.h> 13 #include <errno.h> 14 #include <fdtdec.h> 15 #include <malloc.h> 16 #include <asm/gpio.h> 17 #include <linux/bug.h> 18 #include <linux/ctype.h> 19 20 DECLARE_GLOBAL_DATA_PTR; 21 22 /** 23 * gpio_to_device() - Convert global GPIO number to device, number 24 * 25 * Convert the GPIO number to an entry in the list of GPIOs 26 * or GPIO blocks registered with the GPIO controller. Returns 27 * entry on success, NULL on error. 28 * 29 * @gpio: The numeric representation of the GPIO 30 * @desc: Returns description (desc->flags will always be 0) 31 * @return 0 if found, -ENOENT if not found 32 */ 33 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc) 34 { 35 struct gpio_dev_priv *uc_priv; 36 struct udevice *dev; 37 int ret; 38 39 for (ret = uclass_first_device(UCLASS_GPIO, &dev); 40 dev; 41 ret = uclass_next_device(&dev)) { 42 uc_priv = dev_get_uclass_priv(dev); 43 if (gpio >= uc_priv->gpio_base && 44 gpio < uc_priv->gpio_base + uc_priv->gpio_count) { 45 desc->dev = dev; 46 desc->offset = gpio - uc_priv->gpio_base; 47 desc->flags = 0; 48 return 0; 49 } 50 } 51 52 /* No such GPIO */ 53 return ret ? ret : -ENOENT; 54 } 55 56 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc) 57 { 58 struct gpio_dev_priv *uc_priv = NULL; 59 struct udevice *dev; 60 ulong offset; 61 int numeric; 62 int ret; 63 64 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1; 65 for (ret = uclass_first_device(UCLASS_GPIO, &dev); 66 dev; 67 ret = uclass_next_device(&dev)) { 68 int len; 69 70 uc_priv = dev_get_uclass_priv(dev); 71 if (numeric != -1) { 72 offset = numeric - uc_priv->gpio_base; 73 /* Allow GPIOs to be numbered from 0 */ 74 if (offset < uc_priv->gpio_count) 75 break; 76 } 77 78 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0; 79 80 if (!strncasecmp(name, uc_priv->bank_name, len)) { 81 if (!strict_strtoul(name + len, 10, &offset)) 82 break; 83 } 84 } 85 86 if (!dev) 87 return ret ? ret : -EINVAL; 88 89 desc->dev = dev; 90 desc->offset = offset; 91 92 return 0; 93 } 94 95 int gpio_lookup_name(const char *name, struct udevice **devp, 96 unsigned int *offsetp, unsigned int *gpiop) 97 { 98 struct gpio_desc desc; 99 int ret; 100 101 if (devp) 102 *devp = NULL; 103 ret = dm_gpio_lookup_name(name, &desc); 104 if (ret) 105 return ret; 106 107 if (devp) 108 *devp = desc.dev; 109 if (offsetp) 110 *offsetp = desc.offset; 111 if (gpiop) { 112 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev); 113 114 *gpiop = uc_priv->gpio_base + desc.offset; 115 } 116 117 return 0; 118 } 119 120 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc, 121 struct ofnode_phandle_args *args) 122 { 123 if (args->args_count < 1) 124 return -EINVAL; 125 126 desc->offset = args->args[0]; 127 128 if (args->args_count < 2) 129 return 0; 130 131 if (args->args[1] & GPIO_ACTIVE_LOW) 132 desc->flags = GPIOD_ACTIVE_LOW; 133 134 return 0; 135 } 136 137 static int gpio_find_and_xlate(struct gpio_desc *desc, 138 struct ofnode_phandle_args *args) 139 { 140 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); 141 142 if (ops->xlate) 143 return ops->xlate(desc->dev, desc, args); 144 else 145 return gpio_xlate_offs_flags(desc->dev, desc, args); 146 } 147 148 #if defined(CONFIG_DM_GPIO_HOG) 149 150 struct gpio_hog_priv { 151 struct gpio_desc gpiod; 152 }; 153 154 struct gpio_hog_data { 155 int gpiod_flags; 156 int value; 157 u32 val[2]; 158 }; 159 160 static int gpio_hog_ofdata_to_platdata(struct udevice *dev) 161 { 162 struct gpio_hog_data *plat = dev_get_platdata(dev); 163 const char *nodename; 164 int ret; 165 166 plat->value = 0; 167 if (dev_read_bool(dev, "input")) { 168 plat->gpiod_flags = GPIOD_IS_IN; 169 } else if (dev_read_bool(dev, "output-high")) { 170 plat->value = 1; 171 plat->gpiod_flags = GPIOD_IS_OUT; 172 } else if (dev_read_bool(dev, "output-low")) { 173 plat->gpiod_flags = GPIOD_IS_OUT; 174 } else { 175 printf("%s: missing gpio-hog state.\n", __func__); 176 return -EINVAL; 177 } 178 ret = dev_read_u32_array(dev, "gpios", plat->val, 2); 179 if (ret) { 180 printf("%s: wrong gpios property, 2 values needed %d\n", 181 __func__, ret); 182 return ret; 183 } 184 nodename = dev_read_string(dev, "line-name"); 185 if (!nodename) 186 nodename = dev_read_name(dev); 187 device_set_name(dev, nodename); 188 189 return 0; 190 } 191 192 static int gpio_hog_probe(struct udevice *dev) 193 { 194 struct gpio_hog_data *plat = dev_get_platdata(dev); 195 struct gpio_hog_priv *priv = dev_get_priv(dev); 196 int ret; 197 198 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog", 199 plat->val[0], plat->gpiod_flags, 200 plat->val[1], &priv->gpiod); 201 if (ret < 0) { 202 debug("%s: node %s could not get gpio.\n", __func__, 203 dev->name); 204 return ret; 205 } 206 dm_gpio_set_dir(&priv->gpiod); 207 if (plat->gpiod_flags == GPIOD_IS_OUT) 208 dm_gpio_set_value(&priv->gpiod, plat->value); 209 210 return 0; 211 } 212 213 int gpio_hog_probe_all(void) 214 { 215 struct udevice *dev; 216 int ret; 217 218 for (uclass_find_first_device(UCLASS_NOP, &dev); 219 dev; 220 uclass_find_next_device(&dev)) { 221 if (dev->driver == DM_GET_DRIVER(gpio_hog)) { 222 ret = device_probe(dev); 223 if (ret) 224 return ret; 225 } 226 } 227 228 return 0; 229 } 230 231 struct gpio_desc *gpio_hog_lookup_name(const char *name) 232 { 233 struct udevice *dev; 234 235 gpio_hog_probe_all(); 236 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) { 237 struct gpio_hog_priv *priv = dev_get_priv(dev); 238 239 return &priv->gpiod; 240 } 241 242 return NULL; 243 } 244 245 U_BOOT_DRIVER(gpio_hog) = { 246 .name = "gpio_hog", 247 .id = UCLASS_NOP, 248 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata, 249 .probe = gpio_hog_probe, 250 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv), 251 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data), 252 }; 253 #else 254 struct gpio_desc *gpio_hog_lookup_name(const char *name) 255 { 256 return NULL; 257 } 258 #endif 259 260 int dm_gpio_request(struct gpio_desc *desc, const char *label) 261 { 262 struct udevice *dev = desc->dev; 263 struct gpio_dev_priv *uc_priv; 264 char *str; 265 int ret; 266 267 uc_priv = dev_get_uclass_priv(dev); 268 if (uc_priv->name[desc->offset]) 269 return -EBUSY; 270 str = strdup(label); 271 if (!str) 272 return -ENOMEM; 273 if (gpio_get_ops(dev)->request) { 274 ret = gpio_get_ops(dev)->request(dev, desc->offset, label); 275 if (ret) { 276 free(str); 277 return ret; 278 } 279 } 280 uc_priv->name[desc->offset] = str; 281 282 return 0; 283 } 284 285 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...) 286 { 287 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF) 288 va_list args; 289 char buf[40]; 290 291 va_start(args, fmt); 292 vscnprintf(buf, sizeof(buf), fmt, args); 293 va_end(args); 294 return dm_gpio_request(desc, buf); 295 #else 296 return dm_gpio_request(desc, fmt); 297 #endif 298 } 299 300 /** 301 * gpio_request() - [COMPAT] Request GPIO 302 * gpio: GPIO number 303 * label: Name for the requested GPIO 304 * 305 * The label is copied and allocated so the caller does not need to keep 306 * the pointer around. 307 * 308 * This function implements the API that's compatible with current 309 * GPIO API used in U-Boot. The request is forwarded to particular 310 * GPIO driver. Returns 0 on success, negative value on error. 311 */ 312 int gpio_request(unsigned gpio, const char *label) 313 { 314 struct gpio_desc desc; 315 int ret; 316 317 ret = gpio_to_device(gpio, &desc); 318 if (ret) 319 return ret; 320 321 return dm_gpio_request(&desc, label); 322 } 323 324 /** 325 * gpio_requestf() - [COMPAT] Request GPIO 326 * @gpio: GPIO number 327 * @fmt: Format string for the requested GPIO 328 * @...: Arguments for the printf() format string 329 * 330 * This function implements the API that's compatible with current 331 * GPIO API used in U-Boot. The request is forwarded to particular 332 * GPIO driver. Returns 0 on success, negative value on error. 333 */ 334 int gpio_requestf(unsigned gpio, const char *fmt, ...) 335 { 336 #if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF) 337 va_list args; 338 char buf[40]; 339 340 va_start(args, fmt); 341 vscnprintf(buf, sizeof(buf), fmt, args); 342 va_end(args); 343 return gpio_request(gpio, buf); 344 #else 345 return gpio_request(gpio, fmt); 346 #endif 347 } 348 349 int _dm_gpio_free(struct udevice *dev, uint offset) 350 { 351 struct gpio_dev_priv *uc_priv; 352 int ret; 353 354 uc_priv = dev_get_uclass_priv(dev); 355 if (!uc_priv->name[offset]) 356 return -ENXIO; 357 if (gpio_get_ops(dev)->free) { 358 ret = gpio_get_ops(dev)->free(dev, offset); 359 if (ret) 360 return ret; 361 } 362 363 free(uc_priv->name[offset]); 364 uc_priv->name[offset] = NULL; 365 366 return 0; 367 } 368 369 /** 370 * gpio_free() - [COMPAT] Relinquish GPIO 371 * gpio: GPIO number 372 * 373 * This function implements the API that's compatible with current 374 * GPIO API used in U-Boot. The request is forwarded to particular 375 * GPIO driver. Returns 0 on success, negative value on error. 376 */ 377 int gpio_free(unsigned gpio) 378 { 379 struct gpio_desc desc; 380 int ret; 381 382 ret = gpio_to_device(gpio, &desc); 383 if (ret) 384 return ret; 385 386 return _dm_gpio_free(desc.dev, desc.offset); 387 } 388 389 static int check_reserved(const struct gpio_desc *desc, const char *func) 390 { 391 struct gpio_dev_priv *uc_priv; 392 393 if (!dm_gpio_is_valid(desc)) 394 return -ENOENT; 395 396 uc_priv = dev_get_uclass_priv(desc->dev); 397 if (!uc_priv->name[desc->offset]) { 398 printf("%s: %s: error: gpio %s%d not reserved\n", 399 desc->dev->name, func, 400 uc_priv->bank_name ? uc_priv->bank_name : "", 401 desc->offset); 402 return -EBUSY; 403 } 404 405 return 0; 406 } 407 408 /** 409 * gpio_direction_input() - [COMPAT] Set GPIO direction to input 410 * gpio: GPIO number 411 * 412 * This function implements the API that's compatible with current 413 * GPIO API used in U-Boot. The request is forwarded to particular 414 * GPIO driver. Returns 0 on success, negative value on error. 415 */ 416 int gpio_direction_input(unsigned gpio) 417 { 418 struct gpio_desc desc; 419 int ret; 420 421 ret = gpio_to_device(gpio, &desc); 422 if (ret) 423 return ret; 424 ret = check_reserved(&desc, "dir_input"); 425 if (ret) 426 return ret; 427 428 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset); 429 } 430 431 /** 432 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value 433 * gpio: GPIO number 434 * value: Logical value to be set on the GPIO pin 435 * 436 * This function implements the API that's compatible with current 437 * GPIO API used in U-Boot. The request is forwarded to particular 438 * GPIO driver. Returns 0 on success, negative value on error. 439 */ 440 int gpio_direction_output(unsigned gpio, int value) 441 { 442 struct gpio_desc desc; 443 int ret; 444 445 ret = gpio_to_device(gpio, &desc); 446 if (ret) 447 return ret; 448 ret = check_reserved(&desc, "dir_output"); 449 if (ret) 450 return ret; 451 452 return gpio_get_ops(desc.dev)->direction_output(desc.dev, 453 desc.offset, value); 454 } 455 456 int dm_gpio_get_value(const struct gpio_desc *desc) 457 { 458 int value; 459 int ret; 460 461 ret = check_reserved(desc, "get_value"); 462 if (ret) 463 return ret; 464 465 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset); 466 467 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value; 468 } 469 470 int dm_gpio_set_value(const struct gpio_desc *desc, int value) 471 { 472 int ret; 473 474 ret = check_reserved(desc, "set_value"); 475 if (ret) 476 return ret; 477 478 if (desc->flags & GPIOD_ACTIVE_LOW) 479 value = !value; 480 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value); 481 return 0; 482 } 483 484 int dm_gpio_get_open_drain(struct gpio_desc *desc) 485 { 486 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); 487 int ret; 488 489 ret = check_reserved(desc, "get_open_drain"); 490 if (ret) 491 return ret; 492 493 if (ops->set_open_drain) 494 return ops->get_open_drain(desc->dev, desc->offset); 495 else 496 return -ENOSYS; 497 } 498 499 int dm_gpio_set_open_drain(struct gpio_desc *desc, int value) 500 { 501 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev); 502 int ret; 503 504 ret = check_reserved(desc, "set_open_drain"); 505 if (ret) 506 return ret; 507 508 if (ops->set_open_drain) 509 ret = ops->set_open_drain(desc->dev, desc->offset, value); 510 else 511 return 0; /* feature not supported -> ignore setting */ 512 513 return ret; 514 } 515 516 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags) 517 { 518 struct udevice *dev = desc->dev; 519 struct dm_gpio_ops *ops = gpio_get_ops(dev); 520 int ret; 521 522 ret = check_reserved(desc, "set_dir"); 523 if (ret) 524 return ret; 525 526 if (flags & GPIOD_IS_OUT) { 527 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0; 528 529 if (flags & GPIOD_ACTIVE_LOW) 530 value = !value; 531 ret = ops->direction_output(dev, desc->offset, value); 532 } else if (flags & GPIOD_IS_IN) { 533 ret = ops->direction_input(dev, desc->offset); 534 } 535 if (ret) 536 return ret; 537 /* 538 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in 539 * futures 540 */ 541 desc->flags = flags; 542 543 return 0; 544 } 545 546 int dm_gpio_set_dir(struct gpio_desc *desc) 547 { 548 return dm_gpio_set_dir_flags(desc, desc->flags); 549 } 550 551 /** 552 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value 553 * gpio: GPIO number 554 * 555 * This function implements the API that's compatible with current 556 * GPIO API used in U-Boot. The request is forwarded to particular 557 * GPIO driver. Returns the value of the GPIO pin, or negative value 558 * on error. 559 */ 560 int gpio_get_value(unsigned gpio) 561 { 562 int ret; 563 564 struct gpio_desc desc; 565 566 ret = gpio_to_device(gpio, &desc); 567 if (ret) 568 return ret; 569 return dm_gpio_get_value(&desc); 570 } 571 572 /** 573 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin 574 * gpio: GPIO number 575 * value: Logical value to be set on the GPIO pin. 576 * 577 * This function implements the API that's compatible with current 578 * GPIO API used in U-Boot. The request is forwarded to particular 579 * GPIO driver. Returns 0 on success, negative value on error. 580 */ 581 int gpio_set_value(unsigned gpio, int value) 582 { 583 struct gpio_desc desc; 584 int ret; 585 586 ret = gpio_to_device(gpio, &desc); 587 if (ret) 588 return ret; 589 return dm_gpio_set_value(&desc, value); 590 } 591 592 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count) 593 { 594 struct gpio_dev_priv *priv; 595 596 /* Must be called on an active device */ 597 priv = dev_get_uclass_priv(dev); 598 assert(priv); 599 600 *bit_count = priv->gpio_count; 601 return priv->bank_name; 602 } 603 604 static const char * const gpio_function[GPIOF_COUNT] = { 605 "input", 606 "output", 607 "unused", 608 "unknown", 609 "func", 610 }; 611 612 static int get_function(struct udevice *dev, int offset, bool skip_unused, 613 const char **namep) 614 { 615 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); 616 struct dm_gpio_ops *ops = gpio_get_ops(dev); 617 618 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); 619 if (!device_active(dev)) 620 return -ENODEV; 621 if (offset < 0 || offset >= uc_priv->gpio_count) 622 return -EINVAL; 623 if (namep) 624 *namep = uc_priv->name[offset]; 625 if (skip_unused && !uc_priv->name[offset]) 626 return GPIOF_UNUSED; 627 if (ops->get_function) { 628 int ret; 629 630 ret = ops->get_function(dev, offset); 631 if (ret < 0) 632 return ret; 633 if (ret >= ARRAY_SIZE(gpio_function)) 634 return -ENODATA; 635 return ret; 636 } 637 638 return GPIOF_UNKNOWN; 639 } 640 641 int gpio_get_function(struct udevice *dev, int offset, const char **namep) 642 { 643 return get_function(dev, offset, true, namep); 644 } 645 646 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep) 647 { 648 return get_function(dev, offset, false, namep); 649 } 650 651 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize) 652 { 653 struct dm_gpio_ops *ops = gpio_get_ops(dev); 654 struct gpio_dev_priv *priv; 655 char *str = buf; 656 int func; 657 int ret; 658 int len; 659 660 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function)); 661 662 *buf = 0; 663 priv = dev_get_uclass_priv(dev); 664 ret = gpio_get_raw_function(dev, offset, NULL); 665 if (ret < 0) 666 return ret; 667 func = ret; 668 len = snprintf(str, buffsize, "%s%d: %s", 669 priv->bank_name ? priv->bank_name : "", 670 offset, gpio_function[func]); 671 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT || 672 func == GPIOF_UNUSED) { 673 const char *label; 674 bool used; 675 676 ret = ops->get_value(dev, offset); 677 if (ret < 0) 678 return ret; 679 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED; 680 snprintf(str + len, buffsize - len, ": %d [%c]%s%s", 681 ret, 682 used ? 'x' : ' ', 683 used ? " " : "", 684 label ? label : ""); 685 } 686 687 return 0; 688 } 689 690 int gpio_claim_vector(const int *gpio_num_array, const char *fmt) 691 { 692 int i, ret; 693 int gpio; 694 695 for (i = 0; i < 32; i++) { 696 gpio = gpio_num_array[i]; 697 if (gpio == -1) 698 break; 699 ret = gpio_requestf(gpio, fmt, i); 700 if (ret) 701 goto err; 702 ret = gpio_direction_input(gpio); 703 if (ret) { 704 gpio_free(gpio); 705 goto err; 706 } 707 } 708 709 return 0; 710 err: 711 for (i--; i >= 0; i--) 712 gpio_free(gpio_num_array[i]); 713 714 return ret; 715 } 716 717 /* 718 * get a number comprised of multiple GPIO values. gpio_num_array points to 719 * the array of gpio pin numbers to scan, terminated by -1. 720 */ 721 int gpio_get_values_as_int(const int *gpio_list) 722 { 723 int gpio; 724 unsigned bitmask = 1; 725 unsigned vector = 0; 726 int ret; 727 728 while (bitmask && 729 ((gpio = *gpio_list++) != -1)) { 730 ret = gpio_get_value(gpio); 731 if (ret < 0) 732 return ret; 733 else if (ret) 734 vector |= bitmask; 735 bitmask <<= 1; 736 } 737 738 return vector; 739 } 740 741 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count) 742 { 743 unsigned bitmask = 1; 744 unsigned vector = 0; 745 int ret, i; 746 747 for (i = 0; i < count; i++) { 748 ret = dm_gpio_get_value(&desc_list[i]); 749 if (ret < 0) 750 return ret; 751 else if (ret) 752 vector |= bitmask; 753 bitmask <<= 1; 754 } 755 756 return vector; 757 } 758 759 static int gpio_request_tail(int ret, const char *nodename, 760 struct ofnode_phandle_args *args, 761 const char *list_name, int index, 762 struct gpio_desc *desc, int flags, 763 bool add_index, struct udevice *dev) 764 { 765 desc->dev = dev; 766 desc->offset = 0; 767 desc->flags = 0; 768 if (ret) 769 goto err; 770 771 if (!desc->dev) { 772 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node, 773 &desc->dev); 774 if (ret) { 775 debug("%s: uclass_get_device_by_ofnode failed\n", __func__); 776 goto err; 777 } 778 } 779 ret = gpio_find_and_xlate(desc, args); 780 if (ret) { 781 debug("%s: gpio_find_and_xlate failed\n", __func__); 782 goto err; 783 } 784 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s", 785 nodename, list_name, index); 786 if (ret) { 787 debug("%s: dm_gpio_requestf failed\n", __func__); 788 goto err; 789 } 790 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags); 791 if (ret) { 792 debug("%s: dm_gpio_set_dir failed\n", __func__); 793 goto err; 794 } 795 796 return 0; 797 err: 798 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n", 799 __func__, nodename, list_name, index, ret); 800 return ret; 801 } 802 803 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name, 804 int index, struct gpio_desc *desc, 805 int flags, bool add_index) 806 { 807 struct ofnode_phandle_args args; 808 int ret; 809 810 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0, 811 index, &args); 812 813 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, 814 index, desc, flags, add_index, NULL); 815 } 816 817 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index, 818 struct gpio_desc *desc, int flags) 819 { 820 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags, 821 index > 0); 822 } 823 824 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index, 825 struct gpio_desc *desc, int flags) 826 { 827 struct ofnode_phandle_args args; 828 ofnode node; 829 int ret; 830 831 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, 832 index, &args); 833 node = dev_ofnode(dev); 834 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name, 835 index, desc, flags, index > 0, NULL); 836 } 837 838 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name, 839 struct gpio_desc *desc, int max_count, 840 int flags) 841 { 842 int count; 843 int ret; 844 845 for (count = 0; count < max_count; count++) { 846 ret = _gpio_request_by_name_nodev(node, list_name, count, 847 &desc[count], flags, true); 848 if (ret == -ENOENT) 849 break; 850 else if (ret) 851 goto err; 852 } 853 854 /* We ran out of GPIOs in the list */ 855 return count; 856 857 err: 858 gpio_free_list_nodev(desc, count - 1); 859 860 return ret; 861 } 862 863 int gpio_request_list_by_name(struct udevice *dev, const char *list_name, 864 struct gpio_desc *desc, int max_count, 865 int flags) 866 { 867 /* 868 * This isn't ideal since we don't use dev->name in the debug() 869 * calls in gpio_request_by_name(), but we can do this until 870 * gpio_request_list_by_name_nodev() can be dropped. 871 */ 872 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc, 873 max_count, flags); 874 } 875 876 int gpio_get_list_count(struct udevice *dev, const char *list_name) 877 { 878 int ret; 879 880 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev), 881 list_name, "#gpio-cells", 0, -1, 882 NULL); 883 if (ret) { 884 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n", 885 __func__, dev->name, list_name, ret); 886 } 887 888 return ret; 889 } 890 891 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc) 892 { 893 /* For now, we don't do any checking of dev */ 894 return _dm_gpio_free(desc->dev, desc->offset); 895 } 896 897 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count) 898 { 899 int i; 900 901 /* For now, we don't do any checking of dev */ 902 for (i = 0; i < count; i++) 903 dm_gpio_free(dev, &desc[i]); 904 905 return 0; 906 } 907 908 int gpio_free_list_nodev(struct gpio_desc *desc, int count) 909 { 910 return gpio_free_list(NULL, desc, count); 911 } 912 913 /* We need to renumber the GPIOs when any driver is probed/removed */ 914 static int gpio_renumber(struct udevice *removed_dev) 915 { 916 struct gpio_dev_priv *uc_priv; 917 struct udevice *dev; 918 struct uclass *uc; 919 unsigned base; 920 int ret; 921 922 ret = uclass_get(UCLASS_GPIO, &uc); 923 if (ret) 924 return ret; 925 926 /* Ensure that we have a base for each bank */ 927 base = 0; 928 uclass_foreach_dev(dev, uc) { 929 if (device_active(dev) && dev != removed_dev) { 930 uc_priv = dev_get_uclass_priv(dev); 931 uc_priv->gpio_base = base; 932 base += uc_priv->gpio_count; 933 } 934 } 935 936 return 0; 937 } 938 939 int gpio_get_number(const struct gpio_desc *desc) 940 { 941 struct udevice *dev = desc->dev; 942 struct gpio_dev_priv *uc_priv; 943 944 if (!dev) 945 return -1; 946 uc_priv = dev->uclass_priv; 947 948 return uc_priv->gpio_base + desc->offset; 949 } 950 951 static int gpio_post_probe(struct udevice *dev) 952 { 953 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); 954 955 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *)); 956 if (!uc_priv->name) 957 return -ENOMEM; 958 959 return gpio_renumber(NULL); 960 } 961 962 static int gpio_pre_remove(struct udevice *dev) 963 { 964 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); 965 int i; 966 967 for (i = 0; i < uc_priv->gpio_count; i++) { 968 if (uc_priv->name[i]) 969 free(uc_priv->name[i]); 970 } 971 free(uc_priv->name); 972 973 return gpio_renumber(dev); 974 } 975 976 int gpio_dev_request_index(struct udevice *dev, const char *nodename, 977 char *list_name, int index, int flags, 978 int dtflags, struct gpio_desc *desc) 979 { 980 struct ofnode_phandle_args args; 981 982 args.node = ofnode_null(); 983 args.args_count = 2; 984 args.args[0] = index; 985 args.args[1] = dtflags; 986 987 return gpio_request_tail(0, nodename, &args, list_name, index, desc, 988 flags, 0, dev); 989 } 990 991 static int gpio_post_bind(struct udevice *dev) 992 { 993 #if defined(CONFIG_DM_GPIO_HOG) 994 struct udevice *child; 995 ofnode node; 996 #endif 997 998 #if defined(CONFIG_DM_GPIO_HOG) 999 dev_for_each_subnode(node, dev) { 1000 if (ofnode_read_bool(node, "gpio-hog")) { 1001 const char *name = ofnode_get_name(node); 1002 1003 device_bind_driver_to_node(dev, "gpio_hog", name, 1004 node, &child); 1005 } 1006 } 1007 #endif 1008 return 0; 1009 } 1010 1011 UCLASS_DRIVER(gpio) = { 1012 .id = UCLASS_GPIO, 1013 .name = "gpio", 1014 .flags = DM_UC_FLAG_SEQ_ALIAS, 1015 .post_probe = gpio_post_probe, 1016 .post_bind = gpio_post_bind, 1017 .pre_remove = gpio_pre_remove, 1018 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv), 1019 }; 1020