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