1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Core driver for the pin control subsystem 4 * 5 * Copyright (C) 2011-2012 ST-Ericsson SA 6 * Written on behalf of Linaro for ST-Ericsson 7 * Based on bits of regulator core, gpio core and clk core 8 * 9 * Author: Linus Walleij <linus.walleij@linaro.org> 10 * 11 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. 12 */ 13 #define pr_fmt(fmt) "pinctrl core: " fmt 14 15 #include <linux/debugfs.h> 16 #include <linux/device.h> 17 #include <linux/err.h> 18 #include <linux/export.h> 19 #include <linux/init.h> 20 #include <linux/kernel.h> 21 #include <linux/kref.h> 22 #include <linux/list.h> 23 #include <linux/seq_file.h> 24 #include <linux/slab.h> 25 26 #include <linux/pinctrl/consumer.h> 27 #include <linux/pinctrl/devinfo.h> 28 #include <linux/pinctrl/machine.h> 29 #include <linux/pinctrl/pinctrl.h> 30 31 #ifdef CONFIG_GPIOLIB 32 #include "../gpio/gpiolib.h" 33 #include <asm-generic/gpio.h> 34 #endif 35 36 #include "core.h" 37 #include "devicetree.h" 38 #include "pinconf.h" 39 #include "pinmux.h" 40 41 static bool pinctrl_dummy_state; 42 43 /* Mutex taken to protect pinctrl_list */ 44 static DEFINE_MUTEX(pinctrl_list_mutex); 45 46 /* Mutex taken to protect pinctrl_maps */ 47 DEFINE_MUTEX(pinctrl_maps_mutex); 48 49 /* Mutex taken to protect pinctrldev_list */ 50 static DEFINE_MUTEX(pinctrldev_list_mutex); 51 52 /* Global list of pin control devices (struct pinctrl_dev) */ 53 static LIST_HEAD(pinctrldev_list); 54 55 /* List of pin controller handles (struct pinctrl) */ 56 static LIST_HEAD(pinctrl_list); 57 58 /* List of pinctrl maps (struct pinctrl_maps) */ 59 LIST_HEAD(pinctrl_maps); 60 61 62 /** 63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support 64 * 65 * Usually this function is called by platforms without pinctrl driver support 66 * but run with some shared drivers using pinctrl APIs. 67 * After calling this function, the pinctrl core will return successfully 68 * with creating a dummy state for the driver to keep going smoothly. 69 */ 70 void pinctrl_provide_dummies(void) 71 { 72 pinctrl_dummy_state = true; 73 } 74 75 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev) 76 { 77 /* We're not allowed to register devices without name */ 78 return pctldev->desc->name; 79 } 80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name); 81 82 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev) 83 { 84 return dev_name(pctldev->dev); 85 } 86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname); 87 88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) 89 { 90 return pctldev->driver_data; 91 } 92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata); 93 94 /** 95 * get_pinctrl_dev_from_devname() - look up pin controller device 96 * @devname: the name of a device instance, as returned by dev_name() 97 * 98 * Looks up a pin control device matching a certain device name or pure device 99 * pointer, the pure device pointer will take precedence. 100 */ 101 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname) 102 { 103 struct pinctrl_dev *pctldev; 104 105 if (!devname) 106 return NULL; 107 108 mutex_lock(&pinctrldev_list_mutex); 109 110 list_for_each_entry(pctldev, &pinctrldev_list, node) { 111 if (!strcmp(dev_name(pctldev->dev), devname)) { 112 /* Matched on device name */ 113 mutex_unlock(&pinctrldev_list_mutex); 114 return pctldev; 115 } 116 } 117 118 mutex_unlock(&pinctrldev_list_mutex); 119 120 return NULL; 121 } 122 123 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np) 124 { 125 struct pinctrl_dev *pctldev; 126 127 mutex_lock(&pinctrldev_list_mutex); 128 129 list_for_each_entry(pctldev, &pinctrldev_list, node) 130 if (device_match_of_node(pctldev->dev, np)) { 131 mutex_unlock(&pinctrldev_list_mutex); 132 return pctldev; 133 } 134 135 mutex_unlock(&pinctrldev_list_mutex); 136 137 return NULL; 138 } 139 140 /** 141 * pin_get_from_name() - look up a pin number from a name 142 * @pctldev: the pin control device to lookup the pin on 143 * @name: the name of the pin to look up 144 */ 145 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name) 146 { 147 unsigned i, pin; 148 149 /* The pin number can be retrived from the pin controller descriptor */ 150 for (i = 0; i < pctldev->desc->npins; i++) { 151 struct pin_desc *desc; 152 153 pin = pctldev->desc->pins[i].number; 154 desc = pin_desc_get(pctldev, pin); 155 /* Pin space may be sparse */ 156 if (desc && !strcmp(name, desc->name)) 157 return pin; 158 } 159 160 return -EINVAL; 161 } 162 163 /** 164 * pin_get_name() - look up a pin name from a pin id 165 * @pctldev: the pin control device to lookup the pin on 166 * @pin: pin number/id to look up 167 */ 168 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin) 169 { 170 const struct pin_desc *desc; 171 172 desc = pin_desc_get(pctldev, pin); 173 if (!desc) { 174 dev_err(pctldev->dev, "failed to get pin(%d) name\n", 175 pin); 176 return NULL; 177 } 178 179 return desc->name; 180 } 181 EXPORT_SYMBOL_GPL(pin_get_name); 182 183 /* Deletes a range of pin descriptors */ 184 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev, 185 const struct pinctrl_pin_desc *pins, 186 unsigned num_pins) 187 { 188 int i; 189 190 for (i = 0; i < num_pins; i++) { 191 struct pin_desc *pindesc; 192 193 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, 194 pins[i].number); 195 if (pindesc) { 196 radix_tree_delete(&pctldev->pin_desc_tree, 197 pins[i].number); 198 if (pindesc->dynamic_name) 199 kfree(pindesc->name); 200 } 201 kfree(pindesc); 202 } 203 } 204 205 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev, 206 const struct pinctrl_pin_desc *pin) 207 { 208 struct pin_desc *pindesc; 209 210 pindesc = pin_desc_get(pctldev, pin->number); 211 if (pindesc) { 212 dev_err(pctldev->dev, "pin %d already registered\n", 213 pin->number); 214 return -EINVAL; 215 } 216 217 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL); 218 if (!pindesc) 219 return -ENOMEM; 220 221 /* Set owner */ 222 pindesc->pctldev = pctldev; 223 224 /* Copy basic pin info */ 225 if (pin->name) { 226 pindesc->name = pin->name; 227 } else { 228 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", pin->number); 229 if (!pindesc->name) { 230 kfree(pindesc); 231 return -ENOMEM; 232 } 233 pindesc->dynamic_name = true; 234 } 235 236 pindesc->drv_data = pin->drv_data; 237 238 radix_tree_insert(&pctldev->pin_desc_tree, pin->number, pindesc); 239 pr_debug("registered pin %d (%s) on %s\n", 240 pin->number, pindesc->name, pctldev->desc->name); 241 return 0; 242 } 243 244 static int pinctrl_register_pins(struct pinctrl_dev *pctldev, 245 const struct pinctrl_pin_desc *pins, 246 unsigned num_descs) 247 { 248 unsigned i; 249 int ret = 0; 250 251 for (i = 0; i < num_descs; i++) { 252 ret = pinctrl_register_one_pin(pctldev, &pins[i]); 253 if (ret) 254 return ret; 255 } 256 257 return 0; 258 } 259 260 /** 261 * gpio_to_pin() - GPIO range GPIO number to pin number translation 262 * @range: GPIO range used for the translation 263 * @gpio: gpio pin to translate to a pin number 264 * 265 * Finds the pin number for a given GPIO using the specified GPIO range 266 * as a base for translation. The distinction between linear GPIO ranges 267 * and pin list based GPIO ranges is managed correctly by this function. 268 * 269 * This function assumes the gpio is part of the specified GPIO range, use 270 * only after making sure this is the case (e.g. by calling it on the 271 * result of successful pinctrl_get_device_gpio_range calls)! 272 */ 273 static inline int gpio_to_pin(struct pinctrl_gpio_range *range, 274 unsigned int gpio) 275 { 276 unsigned int offset = gpio - range->base; 277 if (range->pins) 278 return range->pins[offset]; 279 else 280 return range->pin_base + offset; 281 } 282 283 /** 284 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range 285 * @pctldev: pin controller device to check 286 * @gpio: gpio pin to check taken from the global GPIO pin space 287 * 288 * Tries to match a GPIO pin number to the ranges handled by a certain pin 289 * controller, return the range or NULL 290 */ 291 static struct pinctrl_gpio_range * 292 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio) 293 { 294 struct pinctrl_gpio_range *range; 295 296 mutex_lock(&pctldev->mutex); 297 /* Loop over the ranges */ 298 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 299 /* Check if we're in the valid range */ 300 if (gpio >= range->base && 301 gpio < range->base + range->npins) { 302 mutex_unlock(&pctldev->mutex); 303 return range; 304 } 305 } 306 mutex_unlock(&pctldev->mutex); 307 return NULL; 308 } 309 310 /** 311 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of 312 * the same GPIO chip are in range 313 * @gpio: gpio pin to check taken from the global GPIO pin space 314 * 315 * This function is complement of pinctrl_match_gpio_range(). If the return 316 * value of pinctrl_match_gpio_range() is NULL, this function could be used 317 * to check whether pinctrl device is ready or not. Maybe some GPIO pins 318 * of the same GPIO chip don't have back-end pinctrl interface. 319 * If the return value is true, it means that pinctrl device is ready & the 320 * certain GPIO pin doesn't have back-end pinctrl device. If the return value 321 * is false, it means that pinctrl device may not be ready. 322 */ 323 #ifdef CONFIG_GPIOLIB 324 static bool pinctrl_ready_for_gpio_range(unsigned gpio) 325 { 326 struct pinctrl_dev *pctldev; 327 struct pinctrl_gpio_range *range = NULL; 328 struct gpio_chip *chip = gpio_to_chip(gpio); 329 330 if (WARN(!chip, "no gpio_chip for gpio%i?", gpio)) 331 return false; 332 333 mutex_lock(&pinctrldev_list_mutex); 334 335 /* Loop over the pin controllers */ 336 list_for_each_entry(pctldev, &pinctrldev_list, node) { 337 /* Loop over the ranges */ 338 mutex_lock(&pctldev->mutex); 339 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 340 /* Check if any gpio range overlapped with gpio chip */ 341 if (range->base + range->npins - 1 < chip->base || 342 range->base > chip->base + chip->ngpio - 1) 343 continue; 344 mutex_unlock(&pctldev->mutex); 345 mutex_unlock(&pinctrldev_list_mutex); 346 return true; 347 } 348 mutex_unlock(&pctldev->mutex); 349 } 350 351 mutex_unlock(&pinctrldev_list_mutex); 352 353 return false; 354 } 355 #else 356 static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; } 357 #endif 358 359 /** 360 * pinctrl_get_device_gpio_range() - find device for GPIO range 361 * @gpio: the pin to locate the pin controller for 362 * @outdev: the pin control device if found 363 * @outrange: the GPIO range if found 364 * 365 * Find the pin controller handling a certain GPIO pin from the pinspace of 366 * the GPIO subsystem, return the device and the matching GPIO range. Returns 367 * -EPROBE_DEFER if the GPIO range could not be found in any device since it 368 * may still have not been registered. 369 */ 370 static int pinctrl_get_device_gpio_range(unsigned gpio, 371 struct pinctrl_dev **outdev, 372 struct pinctrl_gpio_range **outrange) 373 { 374 struct pinctrl_dev *pctldev; 375 376 mutex_lock(&pinctrldev_list_mutex); 377 378 /* Loop over the pin controllers */ 379 list_for_each_entry(pctldev, &pinctrldev_list, node) { 380 struct pinctrl_gpio_range *range; 381 382 range = pinctrl_match_gpio_range(pctldev, gpio); 383 if (range) { 384 *outdev = pctldev; 385 *outrange = range; 386 mutex_unlock(&pinctrldev_list_mutex); 387 return 0; 388 } 389 } 390 391 mutex_unlock(&pinctrldev_list_mutex); 392 393 return -EPROBE_DEFER; 394 } 395 396 /** 397 * pinctrl_add_gpio_range() - register a GPIO range for a controller 398 * @pctldev: pin controller device to add the range to 399 * @range: the GPIO range to add 400 * 401 * This adds a range of GPIOs to be handled by a certain pin controller. Call 402 * this to register handled ranges after registering your pin controller. 403 */ 404 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, 405 struct pinctrl_gpio_range *range) 406 { 407 mutex_lock(&pctldev->mutex); 408 list_add_tail(&range->node, &pctldev->gpio_ranges); 409 mutex_unlock(&pctldev->mutex); 410 } 411 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range); 412 413 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev, 414 struct pinctrl_gpio_range *ranges, 415 unsigned nranges) 416 { 417 int i; 418 419 for (i = 0; i < nranges; i++) 420 pinctrl_add_gpio_range(pctldev, &ranges[i]); 421 } 422 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges); 423 424 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname, 425 struct pinctrl_gpio_range *range) 426 { 427 struct pinctrl_dev *pctldev; 428 429 pctldev = get_pinctrl_dev_from_devname(devname); 430 431 /* 432 * If we can't find this device, let's assume that is because 433 * it has not probed yet, so the driver trying to register this 434 * range need to defer probing. 435 */ 436 if (!pctldev) { 437 return ERR_PTR(-EPROBE_DEFER); 438 } 439 pinctrl_add_gpio_range(pctldev, range); 440 441 return pctldev; 442 } 443 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range); 444 445 int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group, 446 const unsigned **pins, unsigned *num_pins) 447 { 448 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; 449 int gs; 450 451 if (!pctlops->get_group_pins) 452 return -EINVAL; 453 454 gs = pinctrl_get_group_selector(pctldev, pin_group); 455 if (gs < 0) 456 return gs; 457 458 return pctlops->get_group_pins(pctldev, gs, pins, num_pins); 459 } 460 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins); 461 462 struct pinctrl_gpio_range * 463 pinctrl_find_gpio_range_from_pin_nolock(struct pinctrl_dev *pctldev, 464 unsigned int pin) 465 { 466 struct pinctrl_gpio_range *range; 467 468 /* Loop over the ranges */ 469 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 470 /* Check if we're in the valid range */ 471 if (range->pins) { 472 int a; 473 for (a = 0; a < range->npins; a++) { 474 if (range->pins[a] == pin) 475 return range; 476 } 477 } else if (pin >= range->pin_base && 478 pin < range->pin_base + range->npins) 479 return range; 480 } 481 482 return NULL; 483 } 484 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin_nolock); 485 486 /** 487 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin 488 * @pctldev: the pin controller device to look in 489 * @pin: a controller-local number to find the range for 490 */ 491 struct pinctrl_gpio_range * 492 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev, 493 unsigned int pin) 494 { 495 struct pinctrl_gpio_range *range; 496 497 mutex_lock(&pctldev->mutex); 498 range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin); 499 mutex_unlock(&pctldev->mutex); 500 501 return range; 502 } 503 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin); 504 505 /** 506 * pinctrl_remove_gpio_range() - remove a range of GPIOs from a pin controller 507 * @pctldev: pin controller device to remove the range from 508 * @range: the GPIO range to remove 509 */ 510 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, 511 struct pinctrl_gpio_range *range) 512 { 513 mutex_lock(&pctldev->mutex); 514 list_del(&range->node); 515 mutex_unlock(&pctldev->mutex); 516 } 517 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range); 518 519 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS 520 521 /** 522 * pinctrl_generic_get_group_count() - returns the number of pin groups 523 * @pctldev: pin controller device 524 */ 525 int pinctrl_generic_get_group_count(struct pinctrl_dev *pctldev) 526 { 527 return pctldev->num_groups; 528 } 529 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_count); 530 531 /** 532 * pinctrl_generic_get_group_name() - returns the name of a pin group 533 * @pctldev: pin controller device 534 * @selector: group number 535 */ 536 const char *pinctrl_generic_get_group_name(struct pinctrl_dev *pctldev, 537 unsigned int selector) 538 { 539 struct group_desc *group; 540 541 group = radix_tree_lookup(&pctldev->pin_group_tree, 542 selector); 543 if (!group) 544 return NULL; 545 546 return group->name; 547 } 548 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_name); 549 550 /** 551 * pinctrl_generic_get_group_pins() - gets the pin group pins 552 * @pctldev: pin controller device 553 * @selector: group number 554 * @pins: pins in the group 555 * @num_pins: number of pins in the group 556 */ 557 int pinctrl_generic_get_group_pins(struct pinctrl_dev *pctldev, 558 unsigned int selector, 559 const unsigned int **pins, 560 unsigned int *num_pins) 561 { 562 struct group_desc *group; 563 564 group = radix_tree_lookup(&pctldev->pin_group_tree, 565 selector); 566 if (!group) { 567 dev_err(pctldev->dev, "%s could not find pingroup%i\n", 568 __func__, selector); 569 return -EINVAL; 570 } 571 572 *pins = group->pins; 573 *num_pins = group->num_pins; 574 575 return 0; 576 } 577 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group_pins); 578 579 /** 580 * pinctrl_generic_get_group() - returns a pin group based on the number 581 * @pctldev: pin controller device 582 * @selector: group number 583 */ 584 struct group_desc *pinctrl_generic_get_group(struct pinctrl_dev *pctldev, 585 unsigned int selector) 586 { 587 struct group_desc *group; 588 589 group = radix_tree_lookup(&pctldev->pin_group_tree, 590 selector); 591 if (!group) 592 return NULL; 593 594 return group; 595 } 596 EXPORT_SYMBOL_GPL(pinctrl_generic_get_group); 597 598 static int pinctrl_generic_group_name_to_selector(struct pinctrl_dev *pctldev, 599 const char *function) 600 { 601 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 602 int ngroups = ops->get_groups_count(pctldev); 603 int selector = 0; 604 605 /* See if this pctldev has this group */ 606 while (selector < ngroups) { 607 const char *gname = ops->get_group_name(pctldev, selector); 608 609 if (gname && !strcmp(function, gname)) 610 return selector; 611 612 selector++; 613 } 614 615 return -EINVAL; 616 } 617 618 /** 619 * pinctrl_generic_add_group() - adds a new pin group 620 * @pctldev: pin controller device 621 * @name: name of the pin group 622 * @pins: pins in the pin group 623 * @num_pins: number of pins in the pin group 624 * @data: pin controller driver specific data 625 * 626 * Note that the caller must take care of locking. 627 */ 628 int pinctrl_generic_add_group(struct pinctrl_dev *pctldev, const char *name, 629 int *pins, int num_pins, void *data) 630 { 631 struct group_desc *group; 632 int selector; 633 634 if (!name) 635 return -EINVAL; 636 637 selector = pinctrl_generic_group_name_to_selector(pctldev, name); 638 if (selector >= 0) 639 return selector; 640 641 selector = pctldev->num_groups; 642 643 group = devm_kzalloc(pctldev->dev, sizeof(*group), GFP_KERNEL); 644 if (!group) 645 return -ENOMEM; 646 647 group->name = name; 648 group->pins = pins; 649 group->num_pins = num_pins; 650 group->data = data; 651 652 radix_tree_insert(&pctldev->pin_group_tree, selector, group); 653 654 pctldev->num_groups++; 655 656 return selector; 657 } 658 EXPORT_SYMBOL_GPL(pinctrl_generic_add_group); 659 660 /** 661 * pinctrl_generic_remove_group() - removes a numbered pin group 662 * @pctldev: pin controller device 663 * @selector: group number 664 * 665 * Note that the caller must take care of locking. 666 */ 667 int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev, 668 unsigned int selector) 669 { 670 struct group_desc *group; 671 672 group = radix_tree_lookup(&pctldev->pin_group_tree, 673 selector); 674 if (!group) 675 return -ENOENT; 676 677 radix_tree_delete(&pctldev->pin_group_tree, selector); 678 devm_kfree(pctldev->dev, group); 679 680 pctldev->num_groups--; 681 682 return 0; 683 } 684 EXPORT_SYMBOL_GPL(pinctrl_generic_remove_group); 685 686 /** 687 * pinctrl_generic_free_groups() - removes all pin groups 688 * @pctldev: pin controller device 689 * 690 * Note that the caller must take care of locking. The pinctrl groups 691 * are allocated with devm_kzalloc() so no need to free them here. 692 */ 693 static void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) 694 { 695 struct radix_tree_iter iter; 696 void __rcu **slot; 697 698 radix_tree_for_each_slot(slot, &pctldev->pin_group_tree, &iter, 0) 699 radix_tree_delete(&pctldev->pin_group_tree, iter.index); 700 701 pctldev->num_groups = 0; 702 } 703 704 #else 705 static inline void pinctrl_generic_free_groups(struct pinctrl_dev *pctldev) 706 { 707 } 708 #endif /* CONFIG_GENERIC_PINCTRL_GROUPS */ 709 710 /** 711 * pinctrl_get_group_selector() - returns the group selector for a group 712 * @pctldev: the pin controller handling the group 713 * @pin_group: the pin group to look up 714 */ 715 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, 716 const char *pin_group) 717 { 718 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; 719 unsigned ngroups = pctlops->get_groups_count(pctldev); 720 unsigned group_selector = 0; 721 722 while (group_selector < ngroups) { 723 const char *gname = pctlops->get_group_name(pctldev, 724 group_selector); 725 if (gname && !strcmp(gname, pin_group)) { 726 dev_dbg(pctldev->dev, 727 "found group selector %u for %s\n", 728 group_selector, 729 pin_group); 730 return group_selector; 731 } 732 733 group_selector++; 734 } 735 736 dev_err(pctldev->dev, "does not have pin group %s\n", 737 pin_group); 738 739 return -EINVAL; 740 } 741 742 bool pinctrl_gpio_can_use_line(unsigned gpio) 743 { 744 struct pinctrl_dev *pctldev; 745 struct pinctrl_gpio_range *range; 746 bool result; 747 int pin; 748 749 /* 750 * Try to obtain GPIO range, if it fails 751 * we're probably dealing with GPIO driver 752 * without a backing pin controller - bail out. 753 */ 754 if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range)) 755 return true; 756 757 mutex_lock(&pctldev->mutex); 758 759 /* Convert to the pin controllers number space */ 760 pin = gpio_to_pin(range, gpio); 761 762 result = pinmux_can_be_used_for_gpio(pctldev, pin); 763 764 mutex_unlock(&pctldev->mutex); 765 766 return result; 767 } 768 EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line); 769 770 /** 771 * pinctrl_gpio_request() - request a single pin to be used as GPIO 772 * @gpio: the GPIO pin number from the GPIO subsystem number space 773 * 774 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 775 * as part of their gpio_request() semantics, platforms and individual drivers 776 * shall *NOT* request GPIO pins to be muxed in. 777 */ 778 int pinctrl_gpio_request(unsigned gpio) 779 { 780 struct pinctrl_dev *pctldev; 781 struct pinctrl_gpio_range *range; 782 int ret; 783 int pin; 784 785 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 786 if (ret) { 787 if (pinctrl_ready_for_gpio_range(gpio)) 788 ret = 0; 789 return ret; 790 } 791 792 mutex_lock(&pctldev->mutex); 793 794 /* Convert to the pin controllers number space */ 795 pin = gpio_to_pin(range, gpio); 796 797 ret = pinmux_request_gpio(pctldev, range, pin, gpio); 798 799 mutex_unlock(&pctldev->mutex); 800 801 return ret; 802 } 803 EXPORT_SYMBOL_GPL(pinctrl_gpio_request); 804 805 /** 806 * pinctrl_gpio_free() - free control on a single pin, currently used as GPIO 807 * @gpio: the GPIO pin number from the GPIO subsystem number space 808 * 809 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 810 * as part of their gpio_free() semantics, platforms and individual drivers 811 * shall *NOT* request GPIO pins to be muxed out. 812 */ 813 void pinctrl_gpio_free(unsigned gpio) 814 { 815 struct pinctrl_dev *pctldev; 816 struct pinctrl_gpio_range *range; 817 int ret; 818 int pin; 819 820 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 821 if (ret) { 822 return; 823 } 824 mutex_lock(&pctldev->mutex); 825 826 /* Convert to the pin controllers number space */ 827 pin = gpio_to_pin(range, gpio); 828 829 pinmux_free_gpio(pctldev, pin, range); 830 831 mutex_unlock(&pctldev->mutex); 832 } 833 EXPORT_SYMBOL_GPL(pinctrl_gpio_free); 834 835 static int pinctrl_gpio_direction(unsigned gpio, bool input) 836 { 837 struct pinctrl_dev *pctldev; 838 struct pinctrl_gpio_range *range; 839 int ret; 840 int pin; 841 842 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 843 if (ret) { 844 return ret; 845 } 846 847 mutex_lock(&pctldev->mutex); 848 849 /* Convert to the pin controllers number space */ 850 pin = gpio_to_pin(range, gpio); 851 ret = pinmux_gpio_direction(pctldev, range, pin, input); 852 853 mutex_unlock(&pctldev->mutex); 854 855 return ret; 856 } 857 858 /** 859 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode 860 * @gpio: the GPIO pin number from the GPIO subsystem number space 861 * 862 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 863 * as part of their gpio_direction_input() semantics, platforms and individual 864 * drivers shall *NOT* touch pin control GPIO calls. 865 */ 866 int pinctrl_gpio_direction_input(unsigned gpio) 867 { 868 return pinctrl_gpio_direction(gpio, true); 869 } 870 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); 871 872 /** 873 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode 874 * @gpio: the GPIO pin number from the GPIO subsystem number space 875 * 876 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 877 * as part of their gpio_direction_output() semantics, platforms and individual 878 * drivers shall *NOT* touch pin control GPIO calls. 879 */ 880 int pinctrl_gpio_direction_output(unsigned gpio) 881 { 882 return pinctrl_gpio_direction(gpio, false); 883 } 884 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); 885 886 /** 887 * pinctrl_gpio_set_config() - Apply config to given GPIO pin 888 * @gpio: the GPIO pin number from the GPIO subsystem number space 889 * @config: the configuration to apply to the GPIO 890 * 891 * This function should *ONLY* be used from gpiolib-based GPIO drivers, if 892 * they need to call the underlying pin controller to change GPIO config 893 * (for example set debounce time). 894 */ 895 int pinctrl_gpio_set_config(unsigned gpio, unsigned long config) 896 { 897 unsigned long configs[] = { config }; 898 struct pinctrl_gpio_range *range; 899 struct pinctrl_dev *pctldev; 900 int ret, pin; 901 902 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 903 if (ret) 904 return ret; 905 906 mutex_lock(&pctldev->mutex); 907 pin = gpio_to_pin(range, gpio); 908 ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs)); 909 mutex_unlock(&pctldev->mutex); 910 911 return ret; 912 } 913 EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config); 914 915 static struct pinctrl_state *find_state(struct pinctrl *p, 916 const char *name) 917 { 918 struct pinctrl_state *state; 919 920 list_for_each_entry(state, &p->states, node) 921 if (!strcmp(state->name, name)) 922 return state; 923 924 return NULL; 925 } 926 927 static struct pinctrl_state *create_state(struct pinctrl *p, 928 const char *name) 929 { 930 struct pinctrl_state *state; 931 932 state = kzalloc(sizeof(*state), GFP_KERNEL); 933 if (!state) 934 return ERR_PTR(-ENOMEM); 935 936 state->name = name; 937 INIT_LIST_HEAD(&state->settings); 938 939 list_add_tail(&state->node, &p->states); 940 941 return state; 942 } 943 944 static int add_setting(struct pinctrl *p, struct pinctrl_dev *pctldev, 945 const struct pinctrl_map *map) 946 { 947 struct pinctrl_state *state; 948 struct pinctrl_setting *setting; 949 int ret; 950 951 state = find_state(p, map->name); 952 if (!state) 953 state = create_state(p, map->name); 954 if (IS_ERR(state)) 955 return PTR_ERR(state); 956 957 if (map->type == PIN_MAP_TYPE_DUMMY_STATE) 958 return 0; 959 960 setting = kzalloc(sizeof(*setting), GFP_KERNEL); 961 if (!setting) 962 return -ENOMEM; 963 964 setting->type = map->type; 965 966 if (pctldev) 967 setting->pctldev = pctldev; 968 else 969 setting->pctldev = 970 get_pinctrl_dev_from_devname(map->ctrl_dev_name); 971 if (!setting->pctldev) { 972 kfree(setting); 973 /* Do not defer probing of hogs (circular loop) */ 974 if (!strcmp(map->ctrl_dev_name, map->dev_name)) 975 return -ENODEV; 976 /* 977 * OK let us guess that the driver is not there yet, and 978 * let's defer obtaining this pinctrl handle to later... 979 */ 980 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe", 981 map->ctrl_dev_name); 982 return -EPROBE_DEFER; 983 } 984 985 setting->dev_name = map->dev_name; 986 987 switch (map->type) { 988 case PIN_MAP_TYPE_MUX_GROUP: 989 ret = pinmux_map_to_setting(map, setting); 990 break; 991 case PIN_MAP_TYPE_CONFIGS_PIN: 992 case PIN_MAP_TYPE_CONFIGS_GROUP: 993 ret = pinconf_map_to_setting(map, setting); 994 break; 995 default: 996 ret = -EINVAL; 997 break; 998 } 999 if (ret < 0) { 1000 kfree(setting); 1001 return ret; 1002 } 1003 1004 list_add_tail(&setting->node, &state->settings); 1005 1006 return 0; 1007 } 1008 1009 static struct pinctrl *find_pinctrl(struct device *dev) 1010 { 1011 struct pinctrl *p; 1012 1013 mutex_lock(&pinctrl_list_mutex); 1014 list_for_each_entry(p, &pinctrl_list, node) 1015 if (p->dev == dev) { 1016 mutex_unlock(&pinctrl_list_mutex); 1017 return p; 1018 } 1019 1020 mutex_unlock(&pinctrl_list_mutex); 1021 return NULL; 1022 } 1023 1024 static void pinctrl_free(struct pinctrl *p, bool inlist); 1025 1026 static struct pinctrl *create_pinctrl(struct device *dev, 1027 struct pinctrl_dev *pctldev) 1028 { 1029 struct pinctrl *p; 1030 const char *devname; 1031 struct pinctrl_maps *maps_node; 1032 const struct pinctrl_map *map; 1033 int ret; 1034 1035 /* 1036 * create the state cookie holder struct pinctrl for each 1037 * mapping, this is what consumers will get when requesting 1038 * a pin control handle with pinctrl_get() 1039 */ 1040 p = kzalloc(sizeof(*p), GFP_KERNEL); 1041 if (!p) 1042 return ERR_PTR(-ENOMEM); 1043 p->dev = dev; 1044 INIT_LIST_HEAD(&p->states); 1045 INIT_LIST_HEAD(&p->dt_maps); 1046 1047 ret = pinctrl_dt_to_map(p, pctldev); 1048 if (ret < 0) { 1049 kfree(p); 1050 return ERR_PTR(ret); 1051 } 1052 1053 devname = dev_name(dev); 1054 1055 mutex_lock(&pinctrl_maps_mutex); 1056 /* Iterate over the pin control maps to locate the right ones */ 1057 for_each_pin_map(maps_node, map) { 1058 /* Map must be for this device */ 1059 if (strcmp(map->dev_name, devname)) 1060 continue; 1061 /* 1062 * If pctldev is not null, we are claiming hog for it, 1063 * that means, setting that is served by pctldev by itself. 1064 * 1065 * Thus we must skip map that is for this device but is served 1066 * by other device. 1067 */ 1068 if (pctldev && 1069 strcmp(dev_name(pctldev->dev), map->ctrl_dev_name)) 1070 continue; 1071 1072 ret = add_setting(p, pctldev, map); 1073 /* 1074 * At this point the adding of a setting may: 1075 * 1076 * - Defer, if the pinctrl device is not yet available 1077 * - Fail, if the pinctrl device is not yet available, 1078 * AND the setting is a hog. We cannot defer that, since 1079 * the hog will kick in immediately after the device 1080 * is registered. 1081 * 1082 * If the error returned was not -EPROBE_DEFER then we 1083 * accumulate the errors to see if we end up with 1084 * an -EPROBE_DEFER later, as that is the worst case. 1085 */ 1086 if (ret == -EPROBE_DEFER) { 1087 pinctrl_free(p, false); 1088 mutex_unlock(&pinctrl_maps_mutex); 1089 return ERR_PTR(ret); 1090 } 1091 } 1092 mutex_unlock(&pinctrl_maps_mutex); 1093 1094 if (ret < 0) { 1095 /* If some other error than deferral occurred, return here */ 1096 pinctrl_free(p, false); 1097 return ERR_PTR(ret); 1098 } 1099 1100 kref_init(&p->users); 1101 1102 /* Add the pinctrl handle to the global list */ 1103 mutex_lock(&pinctrl_list_mutex); 1104 list_add_tail(&p->node, &pinctrl_list); 1105 mutex_unlock(&pinctrl_list_mutex); 1106 1107 return p; 1108 } 1109 1110 /** 1111 * pinctrl_get() - retrieves the pinctrl handle for a device 1112 * @dev: the device to obtain the handle for 1113 */ 1114 struct pinctrl *pinctrl_get(struct device *dev) 1115 { 1116 struct pinctrl *p; 1117 1118 if (WARN_ON(!dev)) 1119 return ERR_PTR(-EINVAL); 1120 1121 /* 1122 * See if somebody else (such as the device core) has already 1123 * obtained a handle to the pinctrl for this device. In that case, 1124 * return another pointer to it. 1125 */ 1126 p = find_pinctrl(dev); 1127 if (p) { 1128 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n"); 1129 kref_get(&p->users); 1130 return p; 1131 } 1132 1133 return create_pinctrl(dev, NULL); 1134 } 1135 EXPORT_SYMBOL_GPL(pinctrl_get); 1136 1137 static void pinctrl_free_setting(bool disable_setting, 1138 struct pinctrl_setting *setting) 1139 { 1140 switch (setting->type) { 1141 case PIN_MAP_TYPE_MUX_GROUP: 1142 if (disable_setting) 1143 pinmux_disable_setting(setting); 1144 pinmux_free_setting(setting); 1145 break; 1146 case PIN_MAP_TYPE_CONFIGS_PIN: 1147 case PIN_MAP_TYPE_CONFIGS_GROUP: 1148 pinconf_free_setting(setting); 1149 break; 1150 default: 1151 break; 1152 } 1153 } 1154 1155 static void pinctrl_free(struct pinctrl *p, bool inlist) 1156 { 1157 struct pinctrl_state *state, *n1; 1158 struct pinctrl_setting *setting, *n2; 1159 1160 mutex_lock(&pinctrl_list_mutex); 1161 list_for_each_entry_safe(state, n1, &p->states, node) { 1162 list_for_each_entry_safe(setting, n2, &state->settings, node) { 1163 pinctrl_free_setting(state == p->state, setting); 1164 list_del(&setting->node); 1165 kfree(setting); 1166 } 1167 list_del(&state->node); 1168 kfree(state); 1169 } 1170 1171 pinctrl_dt_free_maps(p); 1172 1173 if (inlist) 1174 list_del(&p->node); 1175 kfree(p); 1176 mutex_unlock(&pinctrl_list_mutex); 1177 } 1178 1179 /** 1180 * pinctrl_release() - release the pinctrl handle 1181 * @kref: the kref in the pinctrl being released 1182 */ 1183 static void pinctrl_release(struct kref *kref) 1184 { 1185 struct pinctrl *p = container_of(kref, struct pinctrl, users); 1186 1187 pinctrl_free(p, true); 1188 } 1189 1190 /** 1191 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle 1192 * @p: the pinctrl handle to release 1193 */ 1194 void pinctrl_put(struct pinctrl *p) 1195 { 1196 kref_put(&p->users, pinctrl_release); 1197 } 1198 EXPORT_SYMBOL_GPL(pinctrl_put); 1199 1200 /** 1201 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle 1202 * @p: the pinctrl handle to retrieve the state from 1203 * @name: the state name to retrieve 1204 */ 1205 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, 1206 const char *name) 1207 { 1208 struct pinctrl_state *state; 1209 1210 state = find_state(p, name); 1211 if (!state) { 1212 if (pinctrl_dummy_state) { 1213 /* create dummy state */ 1214 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n", 1215 name); 1216 state = create_state(p, name); 1217 } else 1218 state = ERR_PTR(-ENODEV); 1219 } 1220 1221 return state; 1222 } 1223 EXPORT_SYMBOL_GPL(pinctrl_lookup_state); 1224 1225 static void pinctrl_link_add(struct pinctrl_dev *pctldev, 1226 struct device *consumer) 1227 { 1228 if (pctldev->desc->link_consumers) 1229 device_link_add(consumer, pctldev->dev, 1230 DL_FLAG_PM_RUNTIME | 1231 DL_FLAG_AUTOREMOVE_CONSUMER); 1232 } 1233 1234 /** 1235 * pinctrl_commit_state() - select/activate/program a pinctrl state to HW 1236 * @p: the pinctrl handle for the device that requests configuration 1237 * @state: the state handle to select/activate/program 1238 */ 1239 static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state) 1240 { 1241 struct pinctrl_setting *setting, *setting2; 1242 struct pinctrl_state *old_state = p->state; 1243 int ret; 1244 1245 if (p->state) { 1246 /* 1247 * For each pinmux setting in the old state, forget SW's record 1248 * of mux owner for that pingroup. Any pingroups which are 1249 * still owned by the new state will be re-acquired by the call 1250 * to pinmux_enable_setting() in the loop below. 1251 */ 1252 list_for_each_entry(setting, &p->state->settings, node) { 1253 if (setting->type != PIN_MAP_TYPE_MUX_GROUP) 1254 continue; 1255 pinmux_disable_setting(setting); 1256 } 1257 } 1258 1259 p->state = NULL; 1260 1261 /* Apply all the settings for the new state - pinmux first */ 1262 list_for_each_entry(setting, &state->settings, node) { 1263 switch (setting->type) { 1264 case PIN_MAP_TYPE_MUX_GROUP: 1265 ret = pinmux_enable_setting(setting); 1266 break; 1267 case PIN_MAP_TYPE_CONFIGS_PIN: 1268 case PIN_MAP_TYPE_CONFIGS_GROUP: 1269 ret = 0; 1270 break; 1271 default: 1272 ret = -EINVAL; 1273 break; 1274 } 1275 1276 if (ret < 0) 1277 goto unapply_new_state; 1278 1279 /* Do not link hogs (circular dependency) */ 1280 if (p != setting->pctldev->p) 1281 pinctrl_link_add(setting->pctldev, p->dev); 1282 } 1283 1284 /* Apply all the settings for the new state - pinconf after */ 1285 list_for_each_entry(setting, &state->settings, node) { 1286 switch (setting->type) { 1287 case PIN_MAP_TYPE_MUX_GROUP: 1288 ret = 0; 1289 break; 1290 case PIN_MAP_TYPE_CONFIGS_PIN: 1291 case PIN_MAP_TYPE_CONFIGS_GROUP: 1292 ret = pinconf_apply_setting(setting); 1293 break; 1294 default: 1295 ret = -EINVAL; 1296 break; 1297 } 1298 1299 if (ret < 0) { 1300 goto unapply_new_state; 1301 } 1302 1303 /* Do not link hogs (circular dependency) */ 1304 if (p != setting->pctldev->p) 1305 pinctrl_link_add(setting->pctldev, p->dev); 1306 } 1307 1308 p->state = state; 1309 1310 return 0; 1311 1312 unapply_new_state: 1313 dev_err(p->dev, "Error applying setting, reverse things back\n"); 1314 1315 list_for_each_entry(setting2, &state->settings, node) { 1316 if (&setting2->node == &setting->node) 1317 break; 1318 /* 1319 * All we can do here is pinmux_disable_setting. 1320 * That means that some pins are muxed differently now 1321 * than they were before applying the setting (We can't 1322 * "unmux a pin"!), but it's not a big deal since the pins 1323 * are free to be muxed by another apply_setting. 1324 */ 1325 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP) 1326 pinmux_disable_setting(setting2); 1327 } 1328 1329 /* There's no infinite recursive loop here because p->state is NULL */ 1330 if (old_state) 1331 pinctrl_select_state(p, old_state); 1332 1333 return ret; 1334 } 1335 1336 /** 1337 * pinctrl_select_state() - select/activate/program a pinctrl state to HW 1338 * @p: the pinctrl handle for the device that requests configuration 1339 * @state: the state handle to select/activate/program 1340 */ 1341 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) 1342 { 1343 if (p->state == state) 1344 return 0; 1345 1346 return pinctrl_commit_state(p, state); 1347 } 1348 EXPORT_SYMBOL_GPL(pinctrl_select_state); 1349 1350 static void devm_pinctrl_release(struct device *dev, void *res) 1351 { 1352 pinctrl_put(*(struct pinctrl **)res); 1353 } 1354 1355 /** 1356 * devm_pinctrl_get() - Resource managed pinctrl_get() 1357 * @dev: the device to obtain the handle for 1358 * 1359 * If there is a need to explicitly destroy the returned struct pinctrl, 1360 * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). 1361 */ 1362 struct pinctrl *devm_pinctrl_get(struct device *dev) 1363 { 1364 struct pinctrl **ptr, *p; 1365 1366 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); 1367 if (!ptr) 1368 return ERR_PTR(-ENOMEM); 1369 1370 p = pinctrl_get(dev); 1371 if (!IS_ERR(p)) { 1372 *ptr = p; 1373 devres_add(dev, ptr); 1374 } else { 1375 devres_free(ptr); 1376 } 1377 1378 return p; 1379 } 1380 EXPORT_SYMBOL_GPL(devm_pinctrl_get); 1381 1382 static int devm_pinctrl_match(struct device *dev, void *res, void *data) 1383 { 1384 struct pinctrl **p = res; 1385 1386 return *p == data; 1387 } 1388 1389 /** 1390 * devm_pinctrl_put() - Resource managed pinctrl_put() 1391 * @p: the pinctrl handle to release 1392 * 1393 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally 1394 * this function will not need to be called and the resource management 1395 * code will ensure that the resource is freed. 1396 */ 1397 void devm_pinctrl_put(struct pinctrl *p) 1398 { 1399 WARN_ON(devres_release(p->dev, devm_pinctrl_release, 1400 devm_pinctrl_match, p)); 1401 } 1402 EXPORT_SYMBOL_GPL(devm_pinctrl_put); 1403 1404 /** 1405 * pinctrl_register_mappings() - register a set of pin controller mappings 1406 * @maps: the pincontrol mappings table to register. Note the pinctrl-core 1407 * keeps a reference to the passed in maps, so they should _not_ be 1408 * marked with __initdata. 1409 * @num_maps: the number of maps in the mapping table 1410 */ 1411 int pinctrl_register_mappings(const struct pinctrl_map *maps, 1412 unsigned num_maps) 1413 { 1414 int i, ret; 1415 struct pinctrl_maps *maps_node; 1416 1417 pr_debug("add %u pinctrl maps\n", num_maps); 1418 1419 /* First sanity check the new mapping */ 1420 for (i = 0; i < num_maps; i++) { 1421 if (!maps[i].dev_name) { 1422 pr_err("failed to register map %s (%d): no device given\n", 1423 maps[i].name, i); 1424 return -EINVAL; 1425 } 1426 1427 if (!maps[i].name) { 1428 pr_err("failed to register map %d: no map name given\n", 1429 i); 1430 return -EINVAL; 1431 } 1432 1433 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE && 1434 !maps[i].ctrl_dev_name) { 1435 pr_err("failed to register map %s (%d): no pin control device given\n", 1436 maps[i].name, i); 1437 return -EINVAL; 1438 } 1439 1440 switch (maps[i].type) { 1441 case PIN_MAP_TYPE_DUMMY_STATE: 1442 break; 1443 case PIN_MAP_TYPE_MUX_GROUP: 1444 ret = pinmux_validate_map(&maps[i], i); 1445 if (ret < 0) 1446 return ret; 1447 break; 1448 case PIN_MAP_TYPE_CONFIGS_PIN: 1449 case PIN_MAP_TYPE_CONFIGS_GROUP: 1450 ret = pinconf_validate_map(&maps[i], i); 1451 if (ret < 0) 1452 return ret; 1453 break; 1454 default: 1455 pr_err("failed to register map %s (%d): invalid type given\n", 1456 maps[i].name, i); 1457 return -EINVAL; 1458 } 1459 } 1460 1461 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL); 1462 if (!maps_node) 1463 return -ENOMEM; 1464 1465 maps_node->maps = maps; 1466 maps_node->num_maps = num_maps; 1467 1468 mutex_lock(&pinctrl_maps_mutex); 1469 list_add_tail(&maps_node->node, &pinctrl_maps); 1470 mutex_unlock(&pinctrl_maps_mutex); 1471 1472 return 0; 1473 } 1474 EXPORT_SYMBOL_GPL(pinctrl_register_mappings); 1475 1476 /** 1477 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings 1478 * @map: the pincontrol mappings table passed to pinctrl_register_mappings() 1479 * when registering the mappings. 1480 */ 1481 void pinctrl_unregister_mappings(const struct pinctrl_map *map) 1482 { 1483 struct pinctrl_maps *maps_node; 1484 1485 mutex_lock(&pinctrl_maps_mutex); 1486 list_for_each_entry(maps_node, &pinctrl_maps, node) { 1487 if (maps_node->maps == map) { 1488 list_del(&maps_node->node); 1489 kfree(maps_node); 1490 mutex_unlock(&pinctrl_maps_mutex); 1491 return; 1492 } 1493 } 1494 mutex_unlock(&pinctrl_maps_mutex); 1495 } 1496 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings); 1497 1498 /** 1499 * pinctrl_force_sleep() - turn a given controller device into sleep state 1500 * @pctldev: pin controller device 1501 */ 1502 int pinctrl_force_sleep(struct pinctrl_dev *pctldev) 1503 { 1504 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep)) 1505 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep); 1506 return 0; 1507 } 1508 EXPORT_SYMBOL_GPL(pinctrl_force_sleep); 1509 1510 /** 1511 * pinctrl_force_default() - turn a given controller device into default state 1512 * @pctldev: pin controller device 1513 */ 1514 int pinctrl_force_default(struct pinctrl_dev *pctldev) 1515 { 1516 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default)) 1517 return pinctrl_commit_state(pctldev->p, pctldev->hog_default); 1518 return 0; 1519 } 1520 EXPORT_SYMBOL_GPL(pinctrl_force_default); 1521 1522 /** 1523 * pinctrl_init_done() - tell pinctrl probe is done 1524 * 1525 * We'll use this time to switch the pins from "init" to "default" unless the 1526 * driver selected some other state. 1527 * 1528 * @dev: device to that's done probing 1529 */ 1530 int pinctrl_init_done(struct device *dev) 1531 { 1532 struct dev_pin_info *pins = dev->pins; 1533 int ret; 1534 1535 if (!pins) 1536 return 0; 1537 1538 if (IS_ERR(pins->init_state)) 1539 return 0; /* No such state */ 1540 1541 if (pins->p->state != pins->init_state) 1542 return 0; /* Not at init anyway */ 1543 1544 if (IS_ERR(pins->default_state)) 1545 return 0; /* No default state */ 1546 1547 ret = pinctrl_select_state(pins->p, pins->default_state); 1548 if (ret) 1549 dev_err(dev, "failed to activate default pinctrl state\n"); 1550 1551 return ret; 1552 } 1553 1554 static int pinctrl_select_bound_state(struct device *dev, 1555 struct pinctrl_state *state) 1556 { 1557 struct dev_pin_info *pins = dev->pins; 1558 int ret; 1559 1560 if (IS_ERR(state)) 1561 return 0; /* No such state */ 1562 ret = pinctrl_select_state(pins->p, state); 1563 if (ret) 1564 dev_err(dev, "failed to activate pinctrl state %s\n", 1565 state->name); 1566 return ret; 1567 } 1568 1569 /** 1570 * pinctrl_select_default_state() - select default pinctrl state 1571 * @dev: device to select default state for 1572 */ 1573 int pinctrl_select_default_state(struct device *dev) 1574 { 1575 if (!dev->pins) 1576 return 0; 1577 1578 return pinctrl_select_bound_state(dev, dev->pins->default_state); 1579 } 1580 EXPORT_SYMBOL_GPL(pinctrl_select_default_state); 1581 1582 #ifdef CONFIG_PM 1583 1584 /** 1585 * pinctrl_pm_select_default_state() - select default pinctrl state for PM 1586 * @dev: device to select default state for 1587 */ 1588 int pinctrl_pm_select_default_state(struct device *dev) 1589 { 1590 return pinctrl_select_default_state(dev); 1591 } 1592 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state); 1593 1594 /** 1595 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM 1596 * @dev: device to select sleep state for 1597 */ 1598 int pinctrl_pm_select_sleep_state(struct device *dev) 1599 { 1600 if (!dev->pins) 1601 return 0; 1602 1603 return pinctrl_select_bound_state(dev, dev->pins->sleep_state); 1604 } 1605 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state); 1606 1607 /** 1608 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM 1609 * @dev: device to select idle state for 1610 */ 1611 int pinctrl_pm_select_idle_state(struct device *dev) 1612 { 1613 if (!dev->pins) 1614 return 0; 1615 1616 return pinctrl_select_bound_state(dev, dev->pins->idle_state); 1617 } 1618 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state); 1619 #endif 1620 1621 #ifdef CONFIG_DEBUG_FS 1622 1623 static int pinctrl_pins_show(struct seq_file *s, void *what) 1624 { 1625 struct pinctrl_dev *pctldev = s->private; 1626 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1627 unsigned i, pin; 1628 #ifdef CONFIG_GPIOLIB 1629 struct pinctrl_gpio_range *range; 1630 struct gpio_chip *chip; 1631 int gpio_num; 1632 #endif 1633 1634 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); 1635 1636 mutex_lock(&pctldev->mutex); 1637 1638 /* The pin number can be retrived from the pin controller descriptor */ 1639 for (i = 0; i < pctldev->desc->npins; i++) { 1640 struct pin_desc *desc; 1641 1642 pin = pctldev->desc->pins[i].number; 1643 desc = pin_desc_get(pctldev, pin); 1644 /* Pin space may be sparse */ 1645 if (!desc) 1646 continue; 1647 1648 seq_printf(s, "pin %d (%s) ", pin, desc->name); 1649 1650 #ifdef CONFIG_GPIOLIB 1651 gpio_num = -1; 1652 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 1653 if ((pin >= range->pin_base) && 1654 (pin < (range->pin_base + range->npins))) { 1655 gpio_num = range->base + (pin - range->pin_base); 1656 break; 1657 } 1658 } 1659 if (gpio_num >= 0) 1660 chip = gpio_to_chip(gpio_num); 1661 else 1662 chip = NULL; 1663 if (chip) 1664 seq_printf(s, "%u:%s ", gpio_num - chip->gpiodev->base, chip->label); 1665 else 1666 seq_puts(s, "0:? "); 1667 #endif 1668 1669 /* Driver-specific info per pin */ 1670 if (ops->pin_dbg_show) 1671 ops->pin_dbg_show(pctldev, s, pin); 1672 1673 seq_puts(s, "\n"); 1674 } 1675 1676 mutex_unlock(&pctldev->mutex); 1677 1678 return 0; 1679 } 1680 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins); 1681 1682 static int pinctrl_groups_show(struct seq_file *s, void *what) 1683 { 1684 struct pinctrl_dev *pctldev = s->private; 1685 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1686 unsigned ngroups, selector = 0; 1687 1688 mutex_lock(&pctldev->mutex); 1689 1690 ngroups = ops->get_groups_count(pctldev); 1691 1692 seq_puts(s, "registered pin groups:\n"); 1693 while (selector < ngroups) { 1694 const unsigned *pins = NULL; 1695 unsigned num_pins = 0; 1696 const char *gname = ops->get_group_name(pctldev, selector); 1697 const char *pname; 1698 int ret = 0; 1699 int i; 1700 1701 if (ops->get_group_pins) 1702 ret = ops->get_group_pins(pctldev, selector, 1703 &pins, &num_pins); 1704 if (ret) 1705 seq_printf(s, "%s [ERROR GETTING PINS]\n", 1706 gname); 1707 else { 1708 seq_printf(s, "group: %s\n", gname); 1709 for (i = 0; i < num_pins; i++) { 1710 pname = pin_get_name(pctldev, pins[i]); 1711 if (WARN_ON(!pname)) { 1712 mutex_unlock(&pctldev->mutex); 1713 return -EINVAL; 1714 } 1715 seq_printf(s, "pin %d (%s)\n", pins[i], pname); 1716 } 1717 seq_puts(s, "\n"); 1718 } 1719 selector++; 1720 } 1721 1722 mutex_unlock(&pctldev->mutex); 1723 1724 return 0; 1725 } 1726 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups); 1727 1728 static int pinctrl_gpioranges_show(struct seq_file *s, void *what) 1729 { 1730 struct pinctrl_dev *pctldev = s->private; 1731 struct pinctrl_gpio_range *range; 1732 1733 seq_puts(s, "GPIO ranges handled:\n"); 1734 1735 mutex_lock(&pctldev->mutex); 1736 1737 /* Loop over the ranges */ 1738 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 1739 if (range->pins) { 1740 int a; 1741 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {", 1742 range->id, range->name, 1743 range->base, (range->base + range->npins - 1)); 1744 for (a = 0; a < range->npins - 1; a++) 1745 seq_printf(s, "%u, ", range->pins[a]); 1746 seq_printf(s, "%u}\n", range->pins[a]); 1747 } 1748 else 1749 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", 1750 range->id, range->name, 1751 range->base, (range->base + range->npins - 1), 1752 range->pin_base, 1753 (range->pin_base + range->npins - 1)); 1754 } 1755 1756 mutex_unlock(&pctldev->mutex); 1757 1758 return 0; 1759 } 1760 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges); 1761 1762 static int pinctrl_devices_show(struct seq_file *s, void *what) 1763 { 1764 struct pinctrl_dev *pctldev; 1765 1766 seq_puts(s, "name [pinmux] [pinconf]\n"); 1767 1768 mutex_lock(&pinctrldev_list_mutex); 1769 1770 list_for_each_entry(pctldev, &pinctrldev_list, node) { 1771 seq_printf(s, "%s ", pctldev->desc->name); 1772 if (pctldev->desc->pmxops) 1773 seq_puts(s, "yes "); 1774 else 1775 seq_puts(s, "no "); 1776 if (pctldev->desc->confops) 1777 seq_puts(s, "yes"); 1778 else 1779 seq_puts(s, "no"); 1780 seq_puts(s, "\n"); 1781 } 1782 1783 mutex_unlock(&pinctrldev_list_mutex); 1784 1785 return 0; 1786 } 1787 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices); 1788 1789 static inline const char *map_type(enum pinctrl_map_type type) 1790 { 1791 static const char * const names[] = { 1792 "INVALID", 1793 "DUMMY_STATE", 1794 "MUX_GROUP", 1795 "CONFIGS_PIN", 1796 "CONFIGS_GROUP", 1797 }; 1798 1799 if (type >= ARRAY_SIZE(names)) 1800 return "UNKNOWN"; 1801 1802 return names[type]; 1803 } 1804 1805 static int pinctrl_maps_show(struct seq_file *s, void *what) 1806 { 1807 struct pinctrl_maps *maps_node; 1808 const struct pinctrl_map *map; 1809 1810 seq_puts(s, "Pinctrl maps:\n"); 1811 1812 mutex_lock(&pinctrl_maps_mutex); 1813 for_each_pin_map(maps_node, map) { 1814 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", 1815 map->dev_name, map->name, map_type(map->type), 1816 map->type); 1817 1818 if (map->type != PIN_MAP_TYPE_DUMMY_STATE) 1819 seq_printf(s, "controlling device %s\n", 1820 map->ctrl_dev_name); 1821 1822 switch (map->type) { 1823 case PIN_MAP_TYPE_MUX_GROUP: 1824 pinmux_show_map(s, map); 1825 break; 1826 case PIN_MAP_TYPE_CONFIGS_PIN: 1827 case PIN_MAP_TYPE_CONFIGS_GROUP: 1828 pinconf_show_map(s, map); 1829 break; 1830 default: 1831 break; 1832 } 1833 1834 seq_putc(s, '\n'); 1835 } 1836 mutex_unlock(&pinctrl_maps_mutex); 1837 1838 return 0; 1839 } 1840 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps); 1841 1842 static int pinctrl_show(struct seq_file *s, void *what) 1843 { 1844 struct pinctrl *p; 1845 struct pinctrl_state *state; 1846 struct pinctrl_setting *setting; 1847 1848 seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); 1849 1850 mutex_lock(&pinctrl_list_mutex); 1851 1852 list_for_each_entry(p, &pinctrl_list, node) { 1853 seq_printf(s, "device: %s current state: %s\n", 1854 dev_name(p->dev), 1855 p->state ? p->state->name : "none"); 1856 1857 list_for_each_entry(state, &p->states, node) { 1858 seq_printf(s, " state: %s\n", state->name); 1859 1860 list_for_each_entry(setting, &state->settings, node) { 1861 struct pinctrl_dev *pctldev = setting->pctldev; 1862 1863 seq_printf(s, " type: %s controller %s ", 1864 map_type(setting->type), 1865 pinctrl_dev_get_name(pctldev)); 1866 1867 switch (setting->type) { 1868 case PIN_MAP_TYPE_MUX_GROUP: 1869 pinmux_show_setting(s, setting); 1870 break; 1871 case PIN_MAP_TYPE_CONFIGS_PIN: 1872 case PIN_MAP_TYPE_CONFIGS_GROUP: 1873 pinconf_show_setting(s, setting); 1874 break; 1875 default: 1876 break; 1877 } 1878 } 1879 } 1880 } 1881 1882 mutex_unlock(&pinctrl_list_mutex); 1883 1884 return 0; 1885 } 1886 DEFINE_SHOW_ATTRIBUTE(pinctrl); 1887 1888 static struct dentry *debugfs_root; 1889 1890 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) 1891 { 1892 struct dentry *device_root; 1893 const char *debugfs_name; 1894 1895 if (pctldev->desc->name && 1896 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) { 1897 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL, 1898 "%s-%s", dev_name(pctldev->dev), 1899 pctldev->desc->name); 1900 if (!debugfs_name) { 1901 pr_warn("failed to determine debugfs dir name for %s\n", 1902 dev_name(pctldev->dev)); 1903 return; 1904 } 1905 } else { 1906 debugfs_name = dev_name(pctldev->dev); 1907 } 1908 1909 device_root = debugfs_create_dir(debugfs_name, debugfs_root); 1910 pctldev->device_root = device_root; 1911 1912 if (IS_ERR(device_root) || !device_root) { 1913 pr_warn("failed to create debugfs directory for %s\n", 1914 dev_name(pctldev->dev)); 1915 return; 1916 } 1917 debugfs_create_file("pins", 0444, 1918 device_root, pctldev, &pinctrl_pins_fops); 1919 debugfs_create_file("pingroups", 0444, 1920 device_root, pctldev, &pinctrl_groups_fops); 1921 debugfs_create_file("gpio-ranges", 0444, 1922 device_root, pctldev, &pinctrl_gpioranges_fops); 1923 if (pctldev->desc->pmxops) 1924 pinmux_init_device_debugfs(device_root, pctldev); 1925 if (pctldev->desc->confops) 1926 pinconf_init_device_debugfs(device_root, pctldev); 1927 } 1928 1929 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) 1930 { 1931 debugfs_remove_recursive(pctldev->device_root); 1932 } 1933 1934 static void pinctrl_init_debugfs(void) 1935 { 1936 debugfs_root = debugfs_create_dir("pinctrl", NULL); 1937 if (IS_ERR(debugfs_root) || !debugfs_root) { 1938 pr_warn("failed to create debugfs directory\n"); 1939 debugfs_root = NULL; 1940 return; 1941 } 1942 1943 debugfs_create_file("pinctrl-devices", 0444, 1944 debugfs_root, NULL, &pinctrl_devices_fops); 1945 debugfs_create_file("pinctrl-maps", 0444, 1946 debugfs_root, NULL, &pinctrl_maps_fops); 1947 debugfs_create_file("pinctrl-handles", 0444, 1948 debugfs_root, NULL, &pinctrl_fops); 1949 } 1950 1951 #else /* CONFIG_DEBUG_FS */ 1952 1953 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) 1954 { 1955 } 1956 1957 static void pinctrl_init_debugfs(void) 1958 { 1959 } 1960 1961 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) 1962 { 1963 } 1964 1965 #endif 1966 1967 static int pinctrl_check_ops(struct pinctrl_dev *pctldev) 1968 { 1969 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1970 1971 if (!ops || 1972 !ops->get_groups_count || 1973 !ops->get_group_name) 1974 return -EINVAL; 1975 1976 return 0; 1977 } 1978 1979 /** 1980 * pinctrl_init_controller() - init a pin controller device 1981 * @pctldesc: descriptor for this pin controller 1982 * @dev: parent device for this pin controller 1983 * @driver_data: private pin controller data for this pin controller 1984 */ 1985 static struct pinctrl_dev * 1986 pinctrl_init_controller(struct pinctrl_desc *pctldesc, struct device *dev, 1987 void *driver_data) 1988 { 1989 struct pinctrl_dev *pctldev; 1990 int ret; 1991 1992 if (!pctldesc) 1993 return ERR_PTR(-EINVAL); 1994 if (!pctldesc->name) 1995 return ERR_PTR(-EINVAL); 1996 1997 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL); 1998 if (!pctldev) 1999 return ERR_PTR(-ENOMEM); 2000 2001 /* Initialize pin control device struct */ 2002 pctldev->owner = pctldesc->owner; 2003 pctldev->desc = pctldesc; 2004 pctldev->driver_data = driver_data; 2005 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); 2006 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS 2007 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL); 2008 #endif 2009 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS 2010 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL); 2011 #endif 2012 INIT_LIST_HEAD(&pctldev->gpio_ranges); 2013 INIT_LIST_HEAD(&pctldev->node); 2014 pctldev->dev = dev; 2015 mutex_init(&pctldev->mutex); 2016 2017 /* check core ops for sanity */ 2018 ret = pinctrl_check_ops(pctldev); 2019 if (ret) { 2020 dev_err(dev, "pinctrl ops lacks necessary functions\n"); 2021 goto out_err; 2022 } 2023 2024 /* If we're implementing pinmuxing, check the ops for sanity */ 2025 if (pctldesc->pmxops) { 2026 ret = pinmux_check_ops(pctldev); 2027 if (ret) 2028 goto out_err; 2029 } 2030 2031 /* If we're implementing pinconfig, check the ops for sanity */ 2032 if (pctldesc->confops) { 2033 ret = pinconf_check_ops(pctldev); 2034 if (ret) 2035 goto out_err; 2036 } 2037 2038 /* Register all the pins */ 2039 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); 2040 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); 2041 if (ret) { 2042 dev_err(dev, "error during pin registration\n"); 2043 pinctrl_free_pindescs(pctldev, pctldesc->pins, 2044 pctldesc->npins); 2045 goto out_err; 2046 } 2047 2048 return pctldev; 2049 2050 out_err: 2051 mutex_destroy(&pctldev->mutex); 2052 kfree(pctldev); 2053 return ERR_PTR(ret); 2054 } 2055 2056 static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev) 2057 { 2058 pctldev->p = create_pinctrl(pctldev->dev, pctldev); 2059 if (PTR_ERR(pctldev->p) == -ENODEV) { 2060 dev_dbg(pctldev->dev, "no hogs found\n"); 2061 2062 return 0; 2063 } 2064 2065 if (IS_ERR(pctldev->p)) { 2066 dev_err(pctldev->dev, "error claiming hogs: %li\n", 2067 PTR_ERR(pctldev->p)); 2068 2069 return PTR_ERR(pctldev->p); 2070 } 2071 2072 pctldev->hog_default = 2073 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT); 2074 if (IS_ERR(pctldev->hog_default)) { 2075 dev_dbg(pctldev->dev, 2076 "failed to lookup the default state\n"); 2077 } else { 2078 if (pinctrl_select_state(pctldev->p, 2079 pctldev->hog_default)) 2080 dev_err(pctldev->dev, 2081 "failed to select default state\n"); 2082 } 2083 2084 pctldev->hog_sleep = 2085 pinctrl_lookup_state(pctldev->p, 2086 PINCTRL_STATE_SLEEP); 2087 if (IS_ERR(pctldev->hog_sleep)) 2088 dev_dbg(pctldev->dev, 2089 "failed to lookup the sleep state\n"); 2090 2091 return 0; 2092 } 2093 2094 int pinctrl_enable(struct pinctrl_dev *pctldev) 2095 { 2096 int error; 2097 2098 error = pinctrl_claim_hogs(pctldev); 2099 if (error) { 2100 dev_err(pctldev->dev, "could not claim hogs: %i\n", 2101 error); 2102 pinctrl_free_pindescs(pctldev, pctldev->desc->pins, 2103 pctldev->desc->npins); 2104 mutex_destroy(&pctldev->mutex); 2105 kfree(pctldev); 2106 2107 return error; 2108 } 2109 2110 mutex_lock(&pinctrldev_list_mutex); 2111 list_add_tail(&pctldev->node, &pinctrldev_list); 2112 mutex_unlock(&pinctrldev_list_mutex); 2113 2114 pinctrl_init_device_debugfs(pctldev); 2115 2116 return 0; 2117 } 2118 EXPORT_SYMBOL_GPL(pinctrl_enable); 2119 2120 /** 2121 * pinctrl_register() - register a pin controller device 2122 * @pctldesc: descriptor for this pin controller 2123 * @dev: parent device for this pin controller 2124 * @driver_data: private pin controller data for this pin controller 2125 * 2126 * Note that pinctrl_register() is known to have problems as the pin 2127 * controller driver functions are called before the driver has a 2128 * struct pinctrl_dev handle. To avoid issues later on, please use the 2129 * new pinctrl_register_and_init() below instead. 2130 */ 2131 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, 2132 struct device *dev, void *driver_data) 2133 { 2134 struct pinctrl_dev *pctldev; 2135 int error; 2136 2137 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data); 2138 if (IS_ERR(pctldev)) 2139 return pctldev; 2140 2141 error = pinctrl_enable(pctldev); 2142 if (error) 2143 return ERR_PTR(error); 2144 2145 return pctldev; 2146 } 2147 EXPORT_SYMBOL_GPL(pinctrl_register); 2148 2149 /** 2150 * pinctrl_register_and_init() - register and init pin controller device 2151 * @pctldesc: descriptor for this pin controller 2152 * @dev: parent device for this pin controller 2153 * @driver_data: private pin controller data for this pin controller 2154 * @pctldev: pin controller device 2155 * 2156 * Note that pinctrl_enable() still needs to be manually called after 2157 * this once the driver is ready. 2158 */ 2159 int pinctrl_register_and_init(struct pinctrl_desc *pctldesc, 2160 struct device *dev, void *driver_data, 2161 struct pinctrl_dev **pctldev) 2162 { 2163 struct pinctrl_dev *p; 2164 2165 p = pinctrl_init_controller(pctldesc, dev, driver_data); 2166 if (IS_ERR(p)) 2167 return PTR_ERR(p); 2168 2169 /* 2170 * We have pinctrl_start() call functions in the pin controller 2171 * driver with create_pinctrl() for at least dt_node_to_map(). So 2172 * let's make sure pctldev is properly initialized for the 2173 * pin controller driver before we do anything. 2174 */ 2175 *pctldev = p; 2176 2177 return 0; 2178 } 2179 EXPORT_SYMBOL_GPL(pinctrl_register_and_init); 2180 2181 /** 2182 * pinctrl_unregister() - unregister pinmux 2183 * @pctldev: pin controller to unregister 2184 * 2185 * Called by pinmux drivers to unregister a pinmux. 2186 */ 2187 void pinctrl_unregister(struct pinctrl_dev *pctldev) 2188 { 2189 struct pinctrl_gpio_range *range, *n; 2190 2191 if (!pctldev) 2192 return; 2193 2194 mutex_lock(&pctldev->mutex); 2195 pinctrl_remove_device_debugfs(pctldev); 2196 mutex_unlock(&pctldev->mutex); 2197 2198 if (!IS_ERR_OR_NULL(pctldev->p)) 2199 pinctrl_put(pctldev->p); 2200 2201 mutex_lock(&pinctrldev_list_mutex); 2202 mutex_lock(&pctldev->mutex); 2203 /* TODO: check that no pinmuxes are still active? */ 2204 list_del(&pctldev->node); 2205 pinmux_generic_free_functions(pctldev); 2206 pinctrl_generic_free_groups(pctldev); 2207 /* Destroy descriptor tree */ 2208 pinctrl_free_pindescs(pctldev, pctldev->desc->pins, 2209 pctldev->desc->npins); 2210 /* remove gpio ranges map */ 2211 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) 2212 list_del(&range->node); 2213 2214 mutex_unlock(&pctldev->mutex); 2215 mutex_destroy(&pctldev->mutex); 2216 kfree(pctldev); 2217 mutex_unlock(&pinctrldev_list_mutex); 2218 } 2219 EXPORT_SYMBOL_GPL(pinctrl_unregister); 2220 2221 static void devm_pinctrl_dev_release(struct device *dev, void *res) 2222 { 2223 struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res; 2224 2225 pinctrl_unregister(pctldev); 2226 } 2227 2228 static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data) 2229 { 2230 struct pctldev **r = res; 2231 2232 if (WARN_ON(!r || !*r)) 2233 return 0; 2234 2235 return *r == data; 2236 } 2237 2238 /** 2239 * devm_pinctrl_register() - Resource managed version of pinctrl_register(). 2240 * @dev: parent device for this pin controller 2241 * @pctldesc: descriptor for this pin controller 2242 * @driver_data: private pin controller data for this pin controller 2243 * 2244 * Returns an error pointer if pincontrol register failed. Otherwise 2245 * it returns valid pinctrl handle. 2246 * 2247 * The pinctrl device will be automatically released when the device is unbound. 2248 */ 2249 struct pinctrl_dev *devm_pinctrl_register(struct device *dev, 2250 struct pinctrl_desc *pctldesc, 2251 void *driver_data) 2252 { 2253 struct pinctrl_dev **ptr, *pctldev; 2254 2255 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL); 2256 if (!ptr) 2257 return ERR_PTR(-ENOMEM); 2258 2259 pctldev = pinctrl_register(pctldesc, dev, driver_data); 2260 if (IS_ERR(pctldev)) { 2261 devres_free(ptr); 2262 return pctldev; 2263 } 2264 2265 *ptr = pctldev; 2266 devres_add(dev, ptr); 2267 2268 return pctldev; 2269 } 2270 EXPORT_SYMBOL_GPL(devm_pinctrl_register); 2271 2272 /** 2273 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init 2274 * @dev: parent device for this pin controller 2275 * @pctldesc: descriptor for this pin controller 2276 * @driver_data: private pin controller data for this pin controller 2277 * @pctldev: pin controller device 2278 * 2279 * Returns zero on success or an error number on failure. 2280 * 2281 * The pinctrl device will be automatically released when the device is unbound. 2282 */ 2283 int devm_pinctrl_register_and_init(struct device *dev, 2284 struct pinctrl_desc *pctldesc, 2285 void *driver_data, 2286 struct pinctrl_dev **pctldev) 2287 { 2288 struct pinctrl_dev **ptr; 2289 int error; 2290 2291 ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL); 2292 if (!ptr) 2293 return -ENOMEM; 2294 2295 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev); 2296 if (error) { 2297 devres_free(ptr); 2298 return error; 2299 } 2300 2301 *ptr = *pctldev; 2302 devres_add(dev, ptr); 2303 2304 return 0; 2305 } 2306 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init); 2307 2308 /** 2309 * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister(). 2310 * @dev: device for which resource was allocated 2311 * @pctldev: the pinctrl device to unregister. 2312 */ 2313 void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev) 2314 { 2315 WARN_ON(devres_release(dev, devm_pinctrl_dev_release, 2316 devm_pinctrl_dev_match, pctldev)); 2317 } 2318 EXPORT_SYMBOL_GPL(devm_pinctrl_unregister); 2319 2320 static int __init pinctrl_init(void) 2321 { 2322 pr_info("initialized pinctrl subsystem\n"); 2323 pinctrl_init_debugfs(); 2324 return 0; 2325 } 2326 2327 /* init early since many drivers really need to initialized pinmux early */ 2328 core_initcall(pinctrl_init); 2329