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/consumer.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_probe(p->dev, ret, 1385 "Error applying setting, reverse things back\n"); 1386 1387 /* 1388 * All we can do here is pinmux_disable_setting. 1389 * That means that some pins are muxed differently now 1390 * than they were before applying the setting (We can't 1391 * "unmux a pin"!), but it's not a big deal since the pins 1392 * are free to be muxed by another apply_setting. 1393 */ 1394 pinctrl_cond_disable_mux_setting(state, setting); 1395 1396 restore_old_state: 1397 /* There's no infinite recursive loop here because p->state is NULL */ 1398 if (old_state) 1399 pinctrl_select_state(p, old_state); 1400 1401 return ret; 1402 } 1403 1404 /** 1405 * pinctrl_select_state() - select/activate/program a pinctrl state to HW 1406 * @p: the pinctrl handle for the device that requests configuration 1407 * @state: the state handle to select/activate/program 1408 */ 1409 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) 1410 { 1411 if (p->state == state) 1412 return 0; 1413 1414 return pinctrl_commit_state(p, state); 1415 } 1416 EXPORT_SYMBOL_GPL(pinctrl_select_state); 1417 1418 static void devm_pinctrl_release(void *p) 1419 { 1420 pinctrl_put(p); 1421 } 1422 1423 /** 1424 * devm_pinctrl_get() - Resource managed pinctrl_get() 1425 * @dev: the device to obtain the handle for 1426 * 1427 * If there is a need to explicitly destroy the returned struct pinctrl, 1428 * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). 1429 */ 1430 struct pinctrl *devm_pinctrl_get(struct device *dev) 1431 { 1432 struct pinctrl *p; 1433 int ret; 1434 1435 p = pinctrl_get(dev); 1436 if (IS_ERR(p)) 1437 return p; 1438 1439 ret = devm_add_action_or_reset(dev, devm_pinctrl_release, p); 1440 if (ret) 1441 return ERR_PTR(ret); 1442 1443 return p; 1444 } 1445 EXPORT_SYMBOL_GPL(devm_pinctrl_get); 1446 1447 /** 1448 * devm_pinctrl_put() - Resource managed pinctrl_put() 1449 * @p: the pinctrl handle to release 1450 * 1451 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally 1452 * this function will not need to be called and the resource management 1453 * code will ensure that the resource is freed. 1454 */ 1455 void devm_pinctrl_put(struct pinctrl *p) 1456 { 1457 devm_release_action(p->dev, devm_pinctrl_release, p); 1458 } 1459 EXPORT_SYMBOL_GPL(devm_pinctrl_put); 1460 1461 /** 1462 * pinctrl_register_mappings() - register a set of pin controller mappings 1463 * @maps: the pincontrol mappings table to register. Note the pinctrl-core 1464 * keeps a reference to the passed in maps, so they should _not_ be 1465 * marked with __initdata. 1466 * @num_maps: the number of maps in the mapping table 1467 */ 1468 int pinctrl_register_mappings(const struct pinctrl_map *maps, 1469 unsigned int num_maps) 1470 { 1471 int i, ret; 1472 struct pinctrl_maps *maps_node; 1473 1474 pr_debug("add %u pinctrl maps\n", num_maps); 1475 1476 /* First sanity check the new mapping */ 1477 for (i = 0; i < num_maps; i++) { 1478 if (!maps[i].dev_name) { 1479 pr_err("failed to register map %s (%d): no device given\n", 1480 maps[i].name, i); 1481 return -EINVAL; 1482 } 1483 1484 if (!maps[i].name) { 1485 pr_err("failed to register map %d: no map name given\n", 1486 i); 1487 return -EINVAL; 1488 } 1489 1490 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE && 1491 !maps[i].ctrl_dev_name) { 1492 pr_err("failed to register map %s (%d): no pin control device given\n", 1493 maps[i].name, i); 1494 return -EINVAL; 1495 } 1496 1497 switch (maps[i].type) { 1498 case PIN_MAP_TYPE_DUMMY_STATE: 1499 break; 1500 case PIN_MAP_TYPE_MUX_GROUP: 1501 ret = pinmux_validate_map(&maps[i], i); 1502 if (ret < 0) 1503 return ret; 1504 break; 1505 case PIN_MAP_TYPE_CONFIGS_PIN: 1506 case PIN_MAP_TYPE_CONFIGS_GROUP: 1507 ret = pinconf_validate_map(&maps[i], i); 1508 if (ret < 0) 1509 return ret; 1510 break; 1511 default: 1512 pr_err("failed to register map %s (%d): invalid type given\n", 1513 maps[i].name, i); 1514 return -EINVAL; 1515 } 1516 } 1517 1518 maps_node = kzalloc_obj(*maps_node); 1519 if (!maps_node) 1520 return -ENOMEM; 1521 1522 maps_node->maps = maps; 1523 maps_node->num_maps = num_maps; 1524 1525 mutex_lock(&pinctrl_maps_mutex); 1526 list_add_tail(&maps_node->node, &pinctrl_maps); 1527 mutex_unlock(&pinctrl_maps_mutex); 1528 1529 return 0; 1530 } 1531 EXPORT_SYMBOL_GPL(pinctrl_register_mappings); 1532 1533 /** 1534 * pinctrl_unregister_mappings() - unregister a set of pin controller mappings 1535 * @map: the pincontrol mappings table passed to pinctrl_register_mappings() 1536 * when registering the mappings. 1537 */ 1538 void pinctrl_unregister_mappings(const struct pinctrl_map *map) 1539 { 1540 struct pinctrl_maps *maps_node; 1541 1542 mutex_lock(&pinctrl_maps_mutex); 1543 list_for_each_entry(maps_node, &pinctrl_maps, node) { 1544 if (maps_node->maps == map) { 1545 list_del(&maps_node->node); 1546 kfree(maps_node); 1547 mutex_unlock(&pinctrl_maps_mutex); 1548 return; 1549 } 1550 } 1551 mutex_unlock(&pinctrl_maps_mutex); 1552 } 1553 EXPORT_SYMBOL_GPL(pinctrl_unregister_mappings); 1554 1555 static void devm_pinctrl_unregister_mappings(void *maps) 1556 { 1557 pinctrl_unregister_mappings(maps); 1558 } 1559 1560 /** 1561 * devm_pinctrl_register_mappings() - Resource managed pinctrl_register_mappings() 1562 * @dev: device for which mappings are registered 1563 * @maps: the pincontrol mappings table to register. Note the pinctrl-core 1564 * keeps a reference to the passed in maps, so they should _not_ be 1565 * marked with __initdata. 1566 * @num_maps: the number of maps in the mapping table 1567 * 1568 * Returns: 0 on success, or negative errno on failure. 1569 */ 1570 int devm_pinctrl_register_mappings(struct device *dev, 1571 const struct pinctrl_map *maps, 1572 unsigned int num_maps) 1573 { 1574 int ret; 1575 1576 ret = pinctrl_register_mappings(maps, num_maps); 1577 if (ret) 1578 return ret; 1579 1580 return devm_add_action_or_reset(dev, devm_pinctrl_unregister_mappings, (void *)maps); 1581 } 1582 EXPORT_SYMBOL_GPL(devm_pinctrl_register_mappings); 1583 1584 /** 1585 * pinctrl_force_sleep() - turn a given controller device into sleep state 1586 * @pctldev: pin controller device 1587 */ 1588 int pinctrl_force_sleep(struct pinctrl_dev *pctldev) 1589 { 1590 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep)) 1591 return pinctrl_commit_state(pctldev->p, pctldev->hog_sleep); 1592 return 0; 1593 } 1594 EXPORT_SYMBOL_GPL(pinctrl_force_sleep); 1595 1596 /** 1597 * pinctrl_force_default() - turn a given controller device into default state 1598 * @pctldev: pin controller device 1599 */ 1600 int pinctrl_force_default(struct pinctrl_dev *pctldev) 1601 { 1602 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default)) 1603 return pinctrl_commit_state(pctldev->p, pctldev->hog_default); 1604 return 0; 1605 } 1606 EXPORT_SYMBOL_GPL(pinctrl_force_default); 1607 1608 /** 1609 * pinctrl_init_done() - tell pinctrl probe is done 1610 * 1611 * We'll use this time to switch the pins from "init" to "default" unless the 1612 * driver selected some other state. 1613 * 1614 * @dev: device to that's done probing 1615 */ 1616 int pinctrl_init_done(struct device *dev) 1617 { 1618 struct dev_pin_info *pins = dev->pins; 1619 int ret; 1620 1621 if (!pins) 1622 return 0; 1623 1624 if (IS_ERR(pins->init_state)) 1625 return 0; /* No such state */ 1626 1627 if (pins->p->state != pins->init_state) 1628 return 0; /* Not at init anyway */ 1629 1630 if (IS_ERR(pins->default_state)) 1631 return 0; /* No default state */ 1632 1633 ret = pinctrl_select_state(pins->p, pins->default_state); 1634 if (ret) 1635 dev_err(dev, "failed to activate default pinctrl state\n"); 1636 1637 return ret; 1638 } 1639 1640 static int pinctrl_select_bound_state(struct device *dev, 1641 struct pinctrl_state *state) 1642 { 1643 struct dev_pin_info *pins = dev->pins; 1644 int ret; 1645 1646 if (IS_ERR(state)) 1647 return 0; /* No such state */ 1648 ret = pinctrl_select_state(pins->p, state); 1649 if (ret) 1650 dev_err(dev, "failed to activate pinctrl state %s\n", 1651 state->name); 1652 return ret; 1653 } 1654 1655 /** 1656 * pinctrl_select_default_state() - select default pinctrl state 1657 * @dev: device to select default state for 1658 */ 1659 int pinctrl_select_default_state(struct device *dev) 1660 { 1661 if (!dev->pins) 1662 return 0; 1663 1664 return pinctrl_select_bound_state(dev, dev->pins->default_state); 1665 } 1666 EXPORT_SYMBOL_GPL(pinctrl_select_default_state); 1667 1668 #ifdef CONFIG_PM 1669 1670 /** 1671 * pinctrl_pm_select_default_state() - select default pinctrl state for PM 1672 * @dev: device to select default state for 1673 */ 1674 int pinctrl_pm_select_default_state(struct device *dev) 1675 { 1676 return pinctrl_select_default_state(dev); 1677 } 1678 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state); 1679 1680 /** 1681 * pinctrl_pm_select_init_state() - select init pinctrl state for PM 1682 * @dev: device to select init state for 1683 */ 1684 int pinctrl_pm_select_init_state(struct device *dev) 1685 { 1686 if (!dev->pins) 1687 return 0; 1688 1689 return pinctrl_select_bound_state(dev, dev->pins->init_state); 1690 } 1691 EXPORT_SYMBOL_GPL(pinctrl_pm_select_init_state); 1692 1693 /** 1694 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM 1695 * @dev: device to select sleep state for 1696 */ 1697 int pinctrl_pm_select_sleep_state(struct device *dev) 1698 { 1699 if (!dev->pins) 1700 return 0; 1701 1702 return pinctrl_select_bound_state(dev, dev->pins->sleep_state); 1703 } 1704 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state); 1705 1706 /** 1707 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM 1708 * @dev: device to select idle state for 1709 */ 1710 int pinctrl_pm_select_idle_state(struct device *dev) 1711 { 1712 if (!dev->pins) 1713 return 0; 1714 1715 return pinctrl_select_bound_state(dev, dev->pins->idle_state); 1716 } 1717 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state); 1718 #endif 1719 1720 #ifdef CONFIG_DEBUG_FS 1721 1722 static int pinctrl_pins_show(struct seq_file *s, void *what) 1723 { 1724 struct pinctrl_dev *pctldev = s->private; 1725 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1726 unsigned int i, pin; 1727 #ifdef CONFIG_GPIOLIB 1728 struct gpio_device *gdev = NULL; 1729 struct pinctrl_gpio_range *range; 1730 int gpio_num; 1731 #endif 1732 1733 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); 1734 1735 mutex_lock(&pctldev->mutex); 1736 1737 /* The pin number can be retrived from the pin controller descriptor */ 1738 for (i = 0; i < pctldev->desc->npins; i++) { 1739 struct pin_desc *desc; 1740 1741 pin = pctldev->desc->pins[i].number; 1742 desc = pin_desc_get(pctldev, pin); 1743 /* Pin space may be sparse */ 1744 if (!desc) 1745 continue; 1746 1747 seq_printf(s, "pin %d (%s) ", pin, desc->name); 1748 1749 #ifdef CONFIG_GPIOLIB 1750 gdev = NULL; 1751 gpio_num = -1; 1752 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 1753 if (range->pins != NULL) { 1754 for (int i = 0; i < range->npins; ++i) { 1755 if (range->pins[i] == pin) { 1756 gpio_num = range->base + i; 1757 break; 1758 } 1759 } 1760 } else if ((pin >= range->pin_base) && 1761 (pin < (range->pin_base + range->npins))) { 1762 gpio_num = 1763 range->base + (pin - range->pin_base); 1764 } 1765 if (gpio_num != -1) 1766 break; 1767 } 1768 if (gpio_num >= 0) 1769 /* 1770 * FIXME: gpio_num comes from the global GPIO numberspace. 1771 * we need to get rid of the range->base eventually and 1772 * get the descriptor directly from the gpio_chip. 1773 */ 1774 gdev = gpiod_to_gpio_device(gpio_to_desc(gpio_num)); 1775 if (gdev) 1776 seq_printf(s, "%u:%s ", 1777 gpio_num - gpio_device_get_base(gdev), 1778 gpio_device_get_label(gdev)); 1779 else 1780 seq_puts(s, "0:? "); 1781 #endif 1782 1783 /* Driver-specific info per pin */ 1784 if (ops->pin_dbg_show) 1785 ops->pin_dbg_show(pctldev, s, pin); 1786 1787 seq_puts(s, "\n"); 1788 } 1789 1790 mutex_unlock(&pctldev->mutex); 1791 1792 return 0; 1793 } 1794 DEFINE_SHOW_ATTRIBUTE(pinctrl_pins); 1795 1796 static int pinctrl_groups_show(struct seq_file *s, void *what) 1797 { 1798 struct pinctrl_dev *pctldev = s->private; 1799 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1800 unsigned int ngroups, selector = 0; 1801 1802 mutex_lock(&pctldev->mutex); 1803 1804 ngroups = ops->get_groups_count(pctldev); 1805 1806 seq_puts(s, "registered pin groups:\n"); 1807 while (selector < ngroups) { 1808 const unsigned int *pins = NULL; 1809 unsigned int num_pins = 0; 1810 const char *gname = ops->get_group_name(pctldev, selector); 1811 const char *pname; 1812 int ret = 0; 1813 int i; 1814 1815 if (ops->get_group_pins) 1816 ret = ops->get_group_pins(pctldev, selector, 1817 &pins, &num_pins); 1818 if (ret) 1819 seq_printf(s, "%s [ERROR GETTING PINS]\n", 1820 gname); 1821 else { 1822 seq_printf(s, "group: %s\n", gname); 1823 for (i = 0; i < num_pins; i++) { 1824 pname = pin_get_name(pctldev, pins[i]); 1825 if (WARN_ON(!pname)) { 1826 mutex_unlock(&pctldev->mutex); 1827 return -EINVAL; 1828 } 1829 seq_printf(s, "pin %d (%s)\n", pins[i], pname); 1830 } 1831 seq_puts(s, "\n"); 1832 } 1833 selector++; 1834 } 1835 1836 mutex_unlock(&pctldev->mutex); 1837 1838 return 0; 1839 } 1840 DEFINE_SHOW_ATTRIBUTE(pinctrl_groups); 1841 1842 static int pinctrl_gpioranges_show(struct seq_file *s, void *what) 1843 { 1844 struct pinctrl_dev *pctldev = s->private; 1845 struct pinctrl_gpio_range *range; 1846 1847 seq_puts(s, "GPIO ranges handled:\n"); 1848 1849 mutex_lock(&pctldev->mutex); 1850 1851 /* Loop over the ranges */ 1852 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 1853 if (range->pins) { 1854 int a; 1855 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {", 1856 range->id, range->name, 1857 range->base, (range->base + range->npins - 1)); 1858 for (a = 0; a < range->npins - 1; a++) 1859 seq_printf(s, "%u, ", range->pins[a]); 1860 seq_printf(s, "%u}\n", range->pins[a]); 1861 } 1862 else 1863 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", 1864 range->id, range->name, 1865 range->base, (range->base + range->npins - 1), 1866 range->pin_base, 1867 (range->pin_base + range->npins - 1)); 1868 } 1869 1870 mutex_unlock(&pctldev->mutex); 1871 1872 return 0; 1873 } 1874 DEFINE_SHOW_ATTRIBUTE(pinctrl_gpioranges); 1875 1876 static int pinctrl_devices_show(struct seq_file *s, void *what) 1877 { 1878 struct pinctrl_dev *pctldev; 1879 1880 seq_puts(s, "name [pinmux] [pinconf]\n"); 1881 1882 mutex_lock(&pinctrldev_list_mutex); 1883 1884 list_for_each_entry(pctldev, &pinctrldev_list, node) { 1885 seq_printf(s, "%s ", pctldev->desc->name); 1886 if (pctldev->desc->pmxops) 1887 seq_puts(s, "yes "); 1888 else 1889 seq_puts(s, "no "); 1890 if (pctldev->desc->confops) 1891 seq_puts(s, "yes"); 1892 else 1893 seq_puts(s, "no"); 1894 seq_puts(s, "\n"); 1895 } 1896 1897 mutex_unlock(&pinctrldev_list_mutex); 1898 1899 return 0; 1900 } 1901 DEFINE_SHOW_ATTRIBUTE(pinctrl_devices); 1902 1903 static inline const char *map_type(enum pinctrl_map_type type) 1904 { 1905 static const char * const names[] = { 1906 "INVALID", 1907 "DUMMY_STATE", 1908 "MUX_GROUP", 1909 "CONFIGS_PIN", 1910 "CONFIGS_GROUP", 1911 }; 1912 1913 if (type >= ARRAY_SIZE(names)) 1914 return "UNKNOWN"; 1915 1916 return names[type]; 1917 } 1918 1919 static int pinctrl_maps_show(struct seq_file *s, void *what) 1920 { 1921 struct pinctrl_maps *maps_node; 1922 const struct pinctrl_map *map; 1923 1924 seq_puts(s, "Pinctrl maps:\n"); 1925 1926 mutex_lock(&pinctrl_maps_mutex); 1927 for_each_pin_map(maps_node, map) { 1928 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", 1929 map->dev_name, map->name, map_type(map->type), 1930 map->type); 1931 1932 if (map->type != PIN_MAP_TYPE_DUMMY_STATE) 1933 seq_printf(s, "controlling device %s\n", 1934 map->ctrl_dev_name); 1935 1936 switch (map->type) { 1937 case PIN_MAP_TYPE_MUX_GROUP: 1938 pinmux_show_map(s, map); 1939 break; 1940 case PIN_MAP_TYPE_CONFIGS_PIN: 1941 case PIN_MAP_TYPE_CONFIGS_GROUP: 1942 pinconf_show_map(s, map); 1943 break; 1944 default: 1945 break; 1946 } 1947 1948 seq_putc(s, '\n'); 1949 } 1950 mutex_unlock(&pinctrl_maps_mutex); 1951 1952 return 0; 1953 } 1954 DEFINE_SHOW_ATTRIBUTE(pinctrl_maps); 1955 1956 static int pinctrl_show(struct seq_file *s, void *what) 1957 { 1958 struct pinctrl *p; 1959 struct pinctrl_state *state; 1960 struct pinctrl_setting *setting; 1961 1962 seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); 1963 1964 mutex_lock(&pinctrl_list_mutex); 1965 1966 list_for_each_entry(p, &pinctrl_list, node) { 1967 seq_printf(s, "device: %s current state: %s\n", 1968 dev_name(p->dev), 1969 p->state ? p->state->name : "none"); 1970 1971 list_for_each_entry(state, &p->states, node) { 1972 seq_printf(s, " state: %s\n", state->name); 1973 1974 list_for_each_entry(setting, &state->settings, node) { 1975 struct pinctrl_dev *pctldev = setting->pctldev; 1976 1977 seq_printf(s, " type: %s controller %s ", 1978 map_type(setting->type), 1979 pinctrl_dev_get_name(pctldev)); 1980 1981 switch (setting->type) { 1982 case PIN_MAP_TYPE_MUX_GROUP: 1983 pinmux_show_setting(s, setting); 1984 break; 1985 case PIN_MAP_TYPE_CONFIGS_PIN: 1986 case PIN_MAP_TYPE_CONFIGS_GROUP: 1987 pinconf_show_setting(s, setting); 1988 break; 1989 default: 1990 break; 1991 } 1992 } 1993 } 1994 } 1995 1996 mutex_unlock(&pinctrl_list_mutex); 1997 1998 return 0; 1999 } 2000 DEFINE_SHOW_ATTRIBUTE(pinctrl); 2001 2002 static struct dentry *debugfs_root; 2003 2004 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) 2005 { 2006 struct dentry *device_root; 2007 const char *debugfs_name; 2008 2009 if (pctldev->desc->name && 2010 strcmp(dev_name(pctldev->dev), pctldev->desc->name)) { 2011 debugfs_name = devm_kasprintf(pctldev->dev, GFP_KERNEL, 2012 "%s-%s", dev_name(pctldev->dev), 2013 pctldev->desc->name); 2014 if (!debugfs_name) { 2015 pr_warn("failed to determine debugfs dir name for %s\n", 2016 dev_name(pctldev->dev)); 2017 return; 2018 } 2019 } else { 2020 debugfs_name = dev_name(pctldev->dev); 2021 } 2022 2023 device_root = debugfs_create_dir(debugfs_name, debugfs_root); 2024 pctldev->device_root = device_root; 2025 2026 if (IS_ERR_OR_NULL(device_root)) { 2027 pr_warn("failed to create debugfs directory for %s\n", 2028 dev_name(pctldev->dev)); 2029 return; 2030 } 2031 debugfs_create_file("pins", 0444, 2032 device_root, pctldev, &pinctrl_pins_fops); 2033 debugfs_create_file("pingroups", 0444, 2034 device_root, pctldev, &pinctrl_groups_fops); 2035 debugfs_create_file("gpio-ranges", 0444, 2036 device_root, pctldev, &pinctrl_gpioranges_fops); 2037 if (pctldev->desc->pmxops) 2038 pinmux_init_device_debugfs(device_root, pctldev); 2039 if (pctldev->desc->confops) 2040 pinconf_init_device_debugfs(device_root, pctldev); 2041 } 2042 2043 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) 2044 { 2045 debugfs_remove_recursive(pctldev->device_root); 2046 } 2047 2048 static void pinctrl_init_debugfs(void) 2049 { 2050 debugfs_root = debugfs_create_dir("pinctrl", NULL); 2051 if (IS_ERR(debugfs_root)) { 2052 pr_warn("failed to create debugfs directory\n"); 2053 debugfs_root = NULL; 2054 return; 2055 } 2056 2057 debugfs_create_file("pinctrl-devices", 0444, 2058 debugfs_root, NULL, &pinctrl_devices_fops); 2059 debugfs_create_file("pinctrl-maps", 0444, 2060 debugfs_root, NULL, &pinctrl_maps_fops); 2061 debugfs_create_file("pinctrl-handles", 0444, 2062 debugfs_root, NULL, &pinctrl_fops); 2063 } 2064 2065 #else /* CONFIG_DEBUG_FS */ 2066 2067 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) 2068 { 2069 } 2070 2071 static void pinctrl_init_debugfs(void) 2072 { 2073 } 2074 2075 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) 2076 { 2077 } 2078 2079 #endif 2080 2081 static int pinctrl_check_ops(struct pinctrl_dev *pctldev) 2082 { 2083 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 2084 2085 if (!ops || 2086 !ops->get_groups_count || 2087 !ops->get_group_name) 2088 return -EINVAL; 2089 2090 return 0; 2091 } 2092 2093 /** 2094 * pinctrl_init_controller() - init a pin controller device 2095 * @pctldesc: descriptor for this pin controller 2096 * @dev: parent device for this pin controller 2097 * @driver_data: private pin controller data for this pin controller 2098 */ 2099 static struct pinctrl_dev * 2100 pinctrl_init_controller(const struct pinctrl_desc *pctldesc, struct device *dev, 2101 void *driver_data) 2102 { 2103 struct pinctrl_dev *pctldev; 2104 int ret; 2105 2106 if (!pctldesc) 2107 return ERR_PTR(-EINVAL); 2108 if (!pctldesc->name) 2109 return ERR_PTR(-EINVAL); 2110 2111 pctldev = kzalloc_obj(*pctldev); 2112 if (!pctldev) 2113 return ERR_PTR(-ENOMEM); 2114 2115 /* Initialize pin control device struct */ 2116 pctldev->owner = pctldesc->owner; 2117 pctldev->desc = pctldesc; 2118 pctldev->driver_data = driver_data; 2119 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); 2120 #ifdef CONFIG_GENERIC_PINCTRL_GROUPS 2121 INIT_RADIX_TREE(&pctldev->pin_group_tree, GFP_KERNEL); 2122 #endif 2123 #ifdef CONFIG_GENERIC_PINMUX_FUNCTIONS 2124 INIT_RADIX_TREE(&pctldev->pin_function_tree, GFP_KERNEL); 2125 #endif 2126 INIT_LIST_HEAD(&pctldev->gpio_ranges); 2127 INIT_LIST_HEAD(&pctldev->node); 2128 pctldev->dev = dev; 2129 mutex_init(&pctldev->mutex); 2130 2131 /* check core ops for sanity */ 2132 ret = pinctrl_check_ops(pctldev); 2133 if (ret) { 2134 dev_err(dev, "pinctrl ops lacks necessary functions\n"); 2135 goto out_err; 2136 } 2137 2138 /* If we're implementing pinmuxing, check the ops for sanity */ 2139 if (pctldesc->pmxops) { 2140 ret = pinmux_check_ops(pctldev); 2141 if (ret) 2142 goto out_err; 2143 } 2144 2145 /* If we're implementing pinconfig, check the ops for sanity */ 2146 if (pctldesc->confops) { 2147 ret = pinconf_check_ops(pctldev); 2148 if (ret) 2149 goto out_err; 2150 } 2151 2152 /* Register all the pins */ 2153 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); 2154 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); 2155 if (ret) { 2156 dev_err(dev, "error during pin registration\n"); 2157 pinctrl_free_pindescs(pctldev, pctldesc->pins, 2158 pctldesc->npins); 2159 goto out_err; 2160 } 2161 2162 return pctldev; 2163 2164 out_err: 2165 mutex_destroy(&pctldev->mutex); 2166 kfree(pctldev); 2167 return ERR_PTR(ret); 2168 } 2169 2170 static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev, 2171 const struct pinctrl_desc *pctldesc) 2172 { 2173 pinctrl_free_pindescs(pctldev, pctldesc->pins, 2174 pctldesc->npins); 2175 mutex_destroy(&pctldev->mutex); 2176 kfree(pctldev); 2177 } 2178 2179 static int pinctrl_claim_hogs(struct pinctrl_dev *pctldev) 2180 { 2181 pctldev->p = create_pinctrl(pctldev->dev, pctldev); 2182 if (PTR_ERR(pctldev->p) == -ENODEV) { 2183 dev_dbg(pctldev->dev, "no hogs found\n"); 2184 2185 return 0; 2186 } 2187 2188 if (IS_ERR(pctldev->p)) { 2189 dev_err(pctldev->dev, "error claiming hogs: %li\n", 2190 PTR_ERR(pctldev->p)); 2191 2192 return PTR_ERR(pctldev->p); 2193 } 2194 2195 pctldev->hog_default = 2196 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT); 2197 if (IS_ERR(pctldev->hog_default)) { 2198 dev_dbg(pctldev->dev, 2199 "failed to lookup the default state\n"); 2200 } else { 2201 if (pinctrl_select_state(pctldev->p, 2202 pctldev->hog_default)) 2203 dev_err(pctldev->dev, 2204 "failed to select default state\n"); 2205 } 2206 2207 pctldev->hog_sleep = 2208 pinctrl_lookup_state(pctldev->p, 2209 PINCTRL_STATE_SLEEP); 2210 if (IS_ERR(pctldev->hog_sleep)) 2211 dev_dbg(pctldev->dev, 2212 "failed to lookup the sleep state\n"); 2213 2214 return 0; 2215 } 2216 2217 int pinctrl_enable(struct pinctrl_dev *pctldev) 2218 { 2219 int error; 2220 2221 error = pinctrl_claim_hogs(pctldev); 2222 if (error) 2223 return error; 2224 2225 mutex_lock(&pinctrldev_list_mutex); 2226 list_add_tail(&pctldev->node, &pinctrldev_list); 2227 mutex_unlock(&pinctrldev_list_mutex); 2228 2229 pinctrl_init_device_debugfs(pctldev); 2230 2231 return 0; 2232 } 2233 EXPORT_SYMBOL_GPL(pinctrl_enable); 2234 2235 /** 2236 * pinctrl_register() - register a pin controller device 2237 * @pctldesc: descriptor for this pin controller 2238 * @dev: parent device for this pin controller 2239 * @driver_data: private pin controller data for this pin controller 2240 * 2241 * Note that pinctrl_register() is known to have problems as the pin 2242 * controller driver functions are called before the driver has a 2243 * struct pinctrl_dev handle. To avoid issues later on, please use the 2244 * new pinctrl_register_and_init() below instead. 2245 */ 2246 struct pinctrl_dev *pinctrl_register(const struct pinctrl_desc *pctldesc, 2247 struct device *dev, void *driver_data) 2248 { 2249 struct pinctrl_dev *pctldev; 2250 int error; 2251 2252 pctldev = pinctrl_init_controller(pctldesc, dev, driver_data); 2253 if (IS_ERR(pctldev)) 2254 return pctldev; 2255 2256 error = pinctrl_enable(pctldev); 2257 if (error) { 2258 pinctrl_uninit_controller(pctldev, pctldesc); 2259 return ERR_PTR(error); 2260 } 2261 2262 return pctldev; 2263 } 2264 EXPORT_SYMBOL_GPL(pinctrl_register); 2265 2266 /** 2267 * pinctrl_register_and_init() - register and init pin controller device 2268 * @pctldesc: descriptor for this pin controller 2269 * @dev: parent device for this pin controller 2270 * @driver_data: private pin controller data for this pin controller 2271 * @pctldev: pin controller device 2272 * 2273 * Note that pinctrl_enable() still needs to be manually called after 2274 * this once the driver is ready. 2275 */ 2276 int pinctrl_register_and_init(const struct pinctrl_desc *pctldesc, 2277 struct device *dev, void *driver_data, 2278 struct pinctrl_dev **pctldev) 2279 { 2280 struct pinctrl_dev *p; 2281 2282 p = pinctrl_init_controller(pctldesc, dev, driver_data); 2283 if (IS_ERR(p)) 2284 return PTR_ERR(p); 2285 2286 /* 2287 * We have pinctrl_start() call functions in the pin controller 2288 * driver with create_pinctrl() for at least dt_node_to_map(). So 2289 * let's make sure pctldev is properly initialized for the 2290 * pin controller driver before we do anything. 2291 */ 2292 *pctldev = p; 2293 2294 return 0; 2295 } 2296 EXPORT_SYMBOL_GPL(pinctrl_register_and_init); 2297 2298 /** 2299 * pinctrl_unregister() - unregister pinmux 2300 * @pctldev: pin controller to unregister 2301 * 2302 * Called by pinmux drivers to unregister a pinmux. 2303 */ 2304 void pinctrl_unregister(struct pinctrl_dev *pctldev) 2305 { 2306 struct pinctrl_gpio_range *range, *n; 2307 2308 if (!pctldev) 2309 return; 2310 2311 mutex_lock(&pctldev->mutex); 2312 pinctrl_remove_device_debugfs(pctldev); 2313 mutex_unlock(&pctldev->mutex); 2314 2315 if (!IS_ERR_OR_NULL(pctldev->p)) 2316 pinctrl_put(pctldev->p); 2317 2318 mutex_lock(&pinctrldev_list_mutex); 2319 mutex_lock(&pctldev->mutex); 2320 /* TODO: check that no pinmuxes are still active? */ 2321 list_del(&pctldev->node); 2322 pinmux_generic_free_functions(pctldev); 2323 pinctrl_generic_free_groups(pctldev); 2324 /* Destroy descriptor tree */ 2325 pinctrl_free_pindescs(pctldev, pctldev->desc->pins, 2326 pctldev->desc->npins); 2327 /* remove gpio ranges map */ 2328 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) 2329 list_del(&range->node); 2330 2331 mutex_unlock(&pctldev->mutex); 2332 mutex_destroy(&pctldev->mutex); 2333 kfree(pctldev); 2334 mutex_unlock(&pinctrldev_list_mutex); 2335 } 2336 EXPORT_SYMBOL_GPL(pinctrl_unregister); 2337 2338 static void devm_pinctrl_dev_release(void *pctldev) 2339 { 2340 pinctrl_unregister(pctldev); 2341 } 2342 2343 /** 2344 * devm_pinctrl_register() - Resource managed version of pinctrl_register(). 2345 * @dev: parent device for this pin controller 2346 * @pctldesc: descriptor for this pin controller 2347 * @driver_data: private pin controller data for this pin controller 2348 * 2349 * Returns an error pointer if pincontrol register failed. Otherwise 2350 * it returns valid pinctrl handle. 2351 * 2352 * The pinctrl device will be automatically released when the device is unbound. 2353 */ 2354 struct pinctrl_dev *devm_pinctrl_register(struct device *dev, 2355 const struct pinctrl_desc *pctldesc, 2356 void *driver_data) 2357 { 2358 struct pinctrl_dev *pctldev; 2359 int ret; 2360 2361 pctldev = pinctrl_register(pctldesc, dev, driver_data); 2362 if (IS_ERR(pctldev)) 2363 return pctldev; 2364 2365 ret = devm_add_action_or_reset(dev, devm_pinctrl_dev_release, pctldev); 2366 if (ret) 2367 return ERR_PTR(ret); 2368 2369 return pctldev; 2370 } 2371 EXPORT_SYMBOL_GPL(devm_pinctrl_register); 2372 2373 /** 2374 * devm_pinctrl_register_and_init() - Resource managed pinctrl register and init 2375 * @dev: parent device for this pin controller 2376 * @pctldesc: descriptor for this pin controller 2377 * @driver_data: private pin controller data for this pin controller 2378 * @pctldev: pin controller device 2379 * 2380 * Returns zero on success or an error number on failure. 2381 * 2382 * The pinctrl device will be automatically released when the device is unbound. 2383 */ 2384 int devm_pinctrl_register_and_init(struct device *dev, 2385 const struct pinctrl_desc *pctldesc, 2386 void *driver_data, 2387 struct pinctrl_dev **pctldev) 2388 { 2389 int error; 2390 2391 error = pinctrl_register_and_init(pctldesc, dev, driver_data, pctldev); 2392 if (error) 2393 return error; 2394 2395 return devm_add_action_or_reset(dev, devm_pinctrl_dev_release, *pctldev); 2396 } 2397 EXPORT_SYMBOL_GPL(devm_pinctrl_register_and_init); 2398 2399 static int __init pinctrl_init(void) 2400 { 2401 pr_debug("initialized pinctrl subsystem\n"); 2402 pinctrl_init_debugfs(); 2403 return 0; 2404 } 2405 2406 /* init early since many drivers really need to initialized pinmux early */ 2407 core_initcall(pinctrl_init); 2408