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