1 /* 2 * Core driver for the pin control subsystem 3 * 4 * Copyright (C) 2011-2012 ST-Ericsson SA 5 * Written on behalf of Linaro for ST-Ericsson 6 * Based on bits of regulator core, gpio core and clk core 7 * 8 * Author: Linus Walleij <linus.walleij@linaro.org> 9 * 10 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. 11 * 12 * License terms: GNU General Public License (GPL) version 2 13 */ 14 #define pr_fmt(fmt) "pinctrl core: " fmt 15 16 #include <linux/kernel.h> 17 #include <linux/kref.h> 18 #include <linux/export.h> 19 #include <linux/init.h> 20 #include <linux/device.h> 21 #include <linux/slab.h> 22 #include <linux/err.h> 23 #include <linux/list.h> 24 #include <linux/sysfs.h> 25 #include <linux/debugfs.h> 26 #include <linux/seq_file.h> 27 #include <linux/pinctrl/consumer.h> 28 #include <linux/pinctrl/pinctrl.h> 29 #include <linux/pinctrl/machine.h> 30 31 #ifdef CONFIG_GPIOLIB 32 #include <asm-generic/gpio.h> 33 #endif 34 35 #include "core.h" 36 #include "devicetree.h" 37 #include "pinmux.h" 38 #include "pinconf.h" 39 40 41 static bool pinctrl_dummy_state; 42 43 /* Mutex taken to protect pinctrl_list */ 44 static DEFINE_MUTEX(pinctrl_list_mutex); 45 46 /* Mutex taken to protect pinctrl_maps */ 47 DEFINE_MUTEX(pinctrl_maps_mutex); 48 49 /* Mutex taken to protect pinctrldev_list */ 50 static DEFINE_MUTEX(pinctrldev_list_mutex); 51 52 /* Global list of pin control devices (struct pinctrl_dev) */ 53 static LIST_HEAD(pinctrldev_list); 54 55 /* List of pin controller handles (struct pinctrl) */ 56 static LIST_HEAD(pinctrl_list); 57 58 /* List of pinctrl maps (struct pinctrl_maps) */ 59 LIST_HEAD(pinctrl_maps); 60 61 62 /** 63 * pinctrl_provide_dummies() - indicate if pinctrl provides dummy state support 64 * 65 * Usually this function is called by platforms without pinctrl driver support 66 * but run with some shared drivers using pinctrl APIs. 67 * After calling this function, the pinctrl core will return successfully 68 * with creating a dummy state for the driver to keep going smoothly. 69 */ 70 void pinctrl_provide_dummies(void) 71 { 72 pinctrl_dummy_state = true; 73 } 74 75 const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev) 76 { 77 /* We're not allowed to register devices without name */ 78 return pctldev->desc->name; 79 } 80 EXPORT_SYMBOL_GPL(pinctrl_dev_get_name); 81 82 const char *pinctrl_dev_get_devname(struct pinctrl_dev *pctldev) 83 { 84 return dev_name(pctldev->dev); 85 } 86 EXPORT_SYMBOL_GPL(pinctrl_dev_get_devname); 87 88 void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev) 89 { 90 return pctldev->driver_data; 91 } 92 EXPORT_SYMBOL_GPL(pinctrl_dev_get_drvdata); 93 94 /** 95 * get_pinctrl_dev_from_devname() - look up pin controller device 96 * @devname: the name of a device instance, as returned by dev_name() 97 * 98 * Looks up a pin control device matching a certain device name or pure device 99 * pointer, the pure device pointer will take precedence. 100 */ 101 struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname) 102 { 103 struct pinctrl_dev *pctldev = NULL; 104 105 if (!devname) 106 return NULL; 107 108 mutex_lock(&pinctrldev_list_mutex); 109 110 list_for_each_entry(pctldev, &pinctrldev_list, node) { 111 if (!strcmp(dev_name(pctldev->dev), devname)) { 112 /* Matched on device name */ 113 mutex_unlock(&pinctrldev_list_mutex); 114 return pctldev; 115 } 116 } 117 118 mutex_unlock(&pinctrldev_list_mutex); 119 120 return NULL; 121 } 122 123 struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np) 124 { 125 struct pinctrl_dev *pctldev; 126 127 mutex_lock(&pinctrldev_list_mutex); 128 129 list_for_each_entry(pctldev, &pinctrldev_list, node) 130 if (pctldev->dev->of_node == np) { 131 mutex_unlock(&pinctrldev_list_mutex); 132 return pctldev; 133 } 134 135 mutex_unlock(&pinctrldev_list_mutex); 136 137 return NULL; 138 } 139 140 /** 141 * pin_get_from_name() - look up a pin number from a name 142 * @pctldev: the pin control device to lookup the pin on 143 * @name: the name of the pin to look up 144 */ 145 int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name) 146 { 147 unsigned i, pin; 148 149 /* The pin number can be retrived from the pin controller descriptor */ 150 for (i = 0; i < pctldev->desc->npins; i++) { 151 struct pin_desc *desc; 152 153 pin = pctldev->desc->pins[i].number; 154 desc = pin_desc_get(pctldev, pin); 155 /* Pin space may be sparse */ 156 if (desc && !strcmp(name, desc->name)) 157 return pin; 158 } 159 160 return -EINVAL; 161 } 162 163 /** 164 * pin_get_name_from_id() - look up a pin name from a pin id 165 * @pctldev: the pin control device to lookup the pin on 166 * @name: the name of the pin to look up 167 */ 168 const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned pin) 169 { 170 const struct pin_desc *desc; 171 172 desc = pin_desc_get(pctldev, pin); 173 if (desc == NULL) { 174 dev_err(pctldev->dev, "failed to get pin(%d) name\n", 175 pin); 176 return NULL; 177 } 178 179 return desc->name; 180 } 181 182 /** 183 * pin_is_valid() - check if pin exists on controller 184 * @pctldev: the pin control device to check the pin on 185 * @pin: pin to check, use the local pin controller index number 186 * 187 * This tells us whether a certain pin exist on a certain pin controller or 188 * not. Pin lists may be sparse, so some pins may not exist. 189 */ 190 bool pin_is_valid(struct pinctrl_dev *pctldev, int pin) 191 { 192 struct pin_desc *pindesc; 193 194 if (pin < 0) 195 return false; 196 197 mutex_lock(&pctldev->mutex); 198 pindesc = pin_desc_get(pctldev, pin); 199 mutex_unlock(&pctldev->mutex); 200 201 return pindesc != NULL; 202 } 203 EXPORT_SYMBOL_GPL(pin_is_valid); 204 205 /* Deletes a range of pin descriptors */ 206 static void pinctrl_free_pindescs(struct pinctrl_dev *pctldev, 207 const struct pinctrl_pin_desc *pins, 208 unsigned num_pins) 209 { 210 int i; 211 212 for (i = 0; i < num_pins; i++) { 213 struct pin_desc *pindesc; 214 215 pindesc = radix_tree_lookup(&pctldev->pin_desc_tree, 216 pins[i].number); 217 if (pindesc != NULL) { 218 radix_tree_delete(&pctldev->pin_desc_tree, 219 pins[i].number); 220 if (pindesc->dynamic_name) 221 kfree(pindesc->name); 222 } 223 kfree(pindesc); 224 } 225 } 226 227 static int pinctrl_register_one_pin(struct pinctrl_dev *pctldev, 228 unsigned number, const char *name) 229 { 230 struct pin_desc *pindesc; 231 232 pindesc = pin_desc_get(pctldev, number); 233 if (pindesc != NULL) { 234 pr_err("pin %d already registered on %s\n", number, 235 pctldev->desc->name); 236 return -EINVAL; 237 } 238 239 pindesc = kzalloc(sizeof(*pindesc), GFP_KERNEL); 240 if (pindesc == NULL) { 241 dev_err(pctldev->dev, "failed to alloc struct pin_desc\n"); 242 return -ENOMEM; 243 } 244 245 /* Set owner */ 246 pindesc->pctldev = pctldev; 247 248 /* Copy basic pin info */ 249 if (name) { 250 pindesc->name = name; 251 } else { 252 pindesc->name = kasprintf(GFP_KERNEL, "PIN%u", number); 253 if (pindesc->name == NULL) { 254 kfree(pindesc); 255 return -ENOMEM; 256 } 257 pindesc->dynamic_name = true; 258 } 259 260 radix_tree_insert(&pctldev->pin_desc_tree, number, pindesc); 261 pr_debug("registered pin %d (%s) on %s\n", 262 number, pindesc->name, pctldev->desc->name); 263 return 0; 264 } 265 266 static int pinctrl_register_pins(struct pinctrl_dev *pctldev, 267 struct pinctrl_pin_desc const *pins, 268 unsigned num_descs) 269 { 270 unsigned i; 271 int ret = 0; 272 273 for (i = 0; i < num_descs; i++) { 274 ret = pinctrl_register_one_pin(pctldev, 275 pins[i].number, pins[i].name); 276 if (ret) 277 return ret; 278 } 279 280 return 0; 281 } 282 283 /** 284 * gpio_to_pin() - GPIO range GPIO number to pin number translation 285 * @range: GPIO range used for the translation 286 * @gpio: gpio pin to translate to a pin number 287 * 288 * Finds the pin number for a given GPIO using the specified GPIO range 289 * as a base for translation. The distinction between linear GPIO ranges 290 * and pin list based GPIO ranges is managed correctly by this function. 291 * 292 * This function assumes the gpio is part of the specified GPIO range, use 293 * only after making sure this is the case (e.g. by calling it on the 294 * result of successful pinctrl_get_device_gpio_range calls)! 295 */ 296 static inline int gpio_to_pin(struct pinctrl_gpio_range *range, 297 unsigned int gpio) 298 { 299 unsigned int offset = gpio - range->base; 300 if (range->pins) 301 return range->pins[offset]; 302 else 303 return range->pin_base + offset; 304 } 305 306 /** 307 * pinctrl_match_gpio_range() - check if a certain GPIO pin is in range 308 * @pctldev: pin controller device to check 309 * @gpio: gpio pin to check taken from the global GPIO pin space 310 * 311 * Tries to match a GPIO pin number to the ranges handled by a certain pin 312 * controller, return the range or NULL 313 */ 314 static struct pinctrl_gpio_range * 315 pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio) 316 { 317 struct pinctrl_gpio_range *range = NULL; 318 319 mutex_lock(&pctldev->mutex); 320 /* Loop over the ranges */ 321 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 322 /* Check if we're in the valid range */ 323 if (gpio >= range->base && 324 gpio < range->base + range->npins) { 325 mutex_unlock(&pctldev->mutex); 326 return range; 327 } 328 } 329 mutex_unlock(&pctldev->mutex); 330 return NULL; 331 } 332 333 /** 334 * pinctrl_ready_for_gpio_range() - check if other GPIO pins of 335 * the same GPIO chip are in range 336 * @gpio: gpio pin to check taken from the global GPIO pin space 337 * 338 * This function is complement of pinctrl_match_gpio_range(). If the return 339 * value of pinctrl_match_gpio_range() is NULL, this function could be used 340 * to check whether pinctrl device is ready or not. Maybe some GPIO pins 341 * of the same GPIO chip don't have back-end pinctrl interface. 342 * If the return value is true, it means that pinctrl device is ready & the 343 * certain GPIO pin doesn't have back-end pinctrl device. If the return value 344 * is false, it means that pinctrl device may not be ready. 345 */ 346 #ifdef CONFIG_GPIOLIB 347 static bool pinctrl_ready_for_gpio_range(unsigned gpio) 348 { 349 struct pinctrl_dev *pctldev; 350 struct pinctrl_gpio_range *range = NULL; 351 struct gpio_chip *chip = gpio_to_chip(gpio); 352 353 mutex_lock(&pinctrldev_list_mutex); 354 355 /* Loop over the pin controllers */ 356 list_for_each_entry(pctldev, &pinctrldev_list, node) { 357 /* Loop over the ranges */ 358 mutex_lock(&pctldev->mutex); 359 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 360 /* Check if any gpio range overlapped with gpio chip */ 361 if (range->base + range->npins - 1 < chip->base || 362 range->base > chip->base + chip->ngpio - 1) 363 continue; 364 mutex_unlock(&pctldev->mutex); 365 mutex_unlock(&pinctrldev_list_mutex); 366 return true; 367 } 368 mutex_unlock(&pctldev->mutex); 369 } 370 371 mutex_unlock(&pinctrldev_list_mutex); 372 373 return false; 374 } 375 #else 376 static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; } 377 #endif 378 379 /** 380 * pinctrl_get_device_gpio_range() - find device for GPIO range 381 * @gpio: the pin to locate the pin controller for 382 * @outdev: the pin control device if found 383 * @outrange: the GPIO range if found 384 * 385 * Find the pin controller handling a certain GPIO pin from the pinspace of 386 * the GPIO subsystem, return the device and the matching GPIO range. Returns 387 * -EPROBE_DEFER if the GPIO range could not be found in any device since it 388 * may still have not been registered. 389 */ 390 static int pinctrl_get_device_gpio_range(unsigned gpio, 391 struct pinctrl_dev **outdev, 392 struct pinctrl_gpio_range **outrange) 393 { 394 struct pinctrl_dev *pctldev = NULL; 395 396 mutex_lock(&pinctrldev_list_mutex); 397 398 /* Loop over the pin controllers */ 399 list_for_each_entry(pctldev, &pinctrldev_list, node) { 400 struct pinctrl_gpio_range *range; 401 402 range = pinctrl_match_gpio_range(pctldev, gpio); 403 if (range != NULL) { 404 *outdev = pctldev; 405 *outrange = range; 406 mutex_unlock(&pinctrldev_list_mutex); 407 return 0; 408 } 409 } 410 411 mutex_unlock(&pinctrldev_list_mutex); 412 413 return -EPROBE_DEFER; 414 } 415 416 /** 417 * pinctrl_add_gpio_range() - register a GPIO range for a controller 418 * @pctldev: pin controller device to add the range to 419 * @range: the GPIO range to add 420 * 421 * This adds a range of GPIOs to be handled by a certain pin controller. Call 422 * this to register handled ranges after registering your pin controller. 423 */ 424 void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev, 425 struct pinctrl_gpio_range *range) 426 { 427 mutex_lock(&pctldev->mutex); 428 list_add_tail(&range->node, &pctldev->gpio_ranges); 429 mutex_unlock(&pctldev->mutex); 430 } 431 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_range); 432 433 void pinctrl_add_gpio_ranges(struct pinctrl_dev *pctldev, 434 struct pinctrl_gpio_range *ranges, 435 unsigned nranges) 436 { 437 int i; 438 439 for (i = 0; i < nranges; i++) 440 pinctrl_add_gpio_range(pctldev, &ranges[i]); 441 } 442 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges); 443 444 struct pinctrl_dev *pinctrl_find_and_add_gpio_range(const char *devname, 445 struct pinctrl_gpio_range *range) 446 { 447 struct pinctrl_dev *pctldev; 448 449 pctldev = get_pinctrl_dev_from_devname(devname); 450 451 /* 452 * If we can't find this device, let's assume that is because 453 * it has not probed yet, so the driver trying to register this 454 * range need to defer probing. 455 */ 456 if (!pctldev) { 457 return ERR_PTR(-EPROBE_DEFER); 458 } 459 pinctrl_add_gpio_range(pctldev, range); 460 461 return pctldev; 462 } 463 EXPORT_SYMBOL_GPL(pinctrl_find_and_add_gpio_range); 464 465 int pinctrl_get_group_pins(struct pinctrl_dev *pctldev, const char *pin_group, 466 const unsigned **pins, unsigned *num_pins) 467 { 468 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; 469 int gs; 470 471 if (!pctlops->get_group_pins) 472 return -EINVAL; 473 474 gs = pinctrl_get_group_selector(pctldev, pin_group); 475 if (gs < 0) 476 return gs; 477 478 return pctlops->get_group_pins(pctldev, gs, pins, num_pins); 479 } 480 EXPORT_SYMBOL_GPL(pinctrl_get_group_pins); 481 482 /** 483 * pinctrl_find_gpio_range_from_pin() - locate the GPIO range for a pin 484 * @pctldev: the pin controller device to look in 485 * @pin: a controller-local number to find the range for 486 */ 487 struct pinctrl_gpio_range * 488 pinctrl_find_gpio_range_from_pin(struct pinctrl_dev *pctldev, 489 unsigned int pin) 490 { 491 struct pinctrl_gpio_range *range; 492 493 mutex_lock(&pctldev->mutex); 494 /* Loop over the ranges */ 495 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 496 /* Check if we're in the valid range */ 497 if (range->pins) { 498 int a; 499 for (a = 0; a < range->npins; a++) { 500 if (range->pins[a] == pin) 501 goto out; 502 } 503 } else if (pin >= range->pin_base && 504 pin < range->pin_base + range->npins) 505 goto out; 506 } 507 range = NULL; 508 out: 509 mutex_unlock(&pctldev->mutex); 510 return range; 511 } 512 EXPORT_SYMBOL_GPL(pinctrl_find_gpio_range_from_pin); 513 514 /** 515 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller 516 * @pctldev: pin controller device to remove the range from 517 * @range: the GPIO range to remove 518 */ 519 void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, 520 struct pinctrl_gpio_range *range) 521 { 522 mutex_lock(&pctldev->mutex); 523 list_del(&range->node); 524 mutex_unlock(&pctldev->mutex); 525 } 526 EXPORT_SYMBOL_GPL(pinctrl_remove_gpio_range); 527 528 /** 529 * pinctrl_get_group_selector() - returns the group selector for a group 530 * @pctldev: the pin controller handling the group 531 * @pin_group: the pin group to look up 532 */ 533 int pinctrl_get_group_selector(struct pinctrl_dev *pctldev, 534 const char *pin_group) 535 { 536 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; 537 unsigned ngroups = pctlops->get_groups_count(pctldev); 538 unsigned group_selector = 0; 539 540 while (group_selector < ngroups) { 541 const char *gname = pctlops->get_group_name(pctldev, 542 group_selector); 543 if (!strcmp(gname, pin_group)) { 544 dev_dbg(pctldev->dev, 545 "found group selector %u for %s\n", 546 group_selector, 547 pin_group); 548 return group_selector; 549 } 550 551 group_selector++; 552 } 553 554 dev_err(pctldev->dev, "does not have pin group %s\n", 555 pin_group); 556 557 return -EINVAL; 558 } 559 560 /** 561 * pinctrl_request_gpio() - request a single pin to be used in as GPIO 562 * @gpio: the GPIO pin number from the GPIO subsystem number space 563 * 564 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 565 * as part of their gpio_request() semantics, platforms and individual drivers 566 * shall *NOT* request GPIO pins to be muxed in. 567 */ 568 int pinctrl_request_gpio(unsigned gpio) 569 { 570 struct pinctrl_dev *pctldev; 571 struct pinctrl_gpio_range *range; 572 int ret; 573 int pin; 574 575 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 576 if (ret) { 577 if (pinctrl_ready_for_gpio_range(gpio)) 578 ret = 0; 579 return ret; 580 } 581 582 mutex_lock(&pctldev->mutex); 583 584 /* Convert to the pin controllers number space */ 585 pin = gpio_to_pin(range, gpio); 586 587 ret = pinmux_request_gpio(pctldev, range, pin, gpio); 588 589 mutex_unlock(&pctldev->mutex); 590 591 return ret; 592 } 593 EXPORT_SYMBOL_GPL(pinctrl_request_gpio); 594 595 /** 596 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO 597 * @gpio: the GPIO pin number from the GPIO subsystem number space 598 * 599 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 600 * as part of their gpio_free() semantics, platforms and individual drivers 601 * shall *NOT* request GPIO pins to be muxed out. 602 */ 603 void pinctrl_free_gpio(unsigned gpio) 604 { 605 struct pinctrl_dev *pctldev; 606 struct pinctrl_gpio_range *range; 607 int ret; 608 int pin; 609 610 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 611 if (ret) { 612 return; 613 } 614 mutex_lock(&pctldev->mutex); 615 616 /* Convert to the pin controllers number space */ 617 pin = gpio_to_pin(range, gpio); 618 619 pinmux_free_gpio(pctldev, pin, range); 620 621 mutex_unlock(&pctldev->mutex); 622 } 623 EXPORT_SYMBOL_GPL(pinctrl_free_gpio); 624 625 static int pinctrl_gpio_direction(unsigned gpio, bool input) 626 { 627 struct pinctrl_dev *pctldev; 628 struct pinctrl_gpio_range *range; 629 int ret; 630 int pin; 631 632 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 633 if (ret) { 634 return ret; 635 } 636 637 mutex_lock(&pctldev->mutex); 638 639 /* Convert to the pin controllers number space */ 640 pin = gpio_to_pin(range, gpio); 641 ret = pinmux_gpio_direction(pctldev, range, pin, input); 642 643 mutex_unlock(&pctldev->mutex); 644 645 return ret; 646 } 647 648 /** 649 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode 650 * @gpio: the GPIO pin number from the GPIO subsystem number space 651 * 652 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 653 * as part of their gpio_direction_input() semantics, platforms and individual 654 * drivers shall *NOT* touch pin control GPIO calls. 655 */ 656 int pinctrl_gpio_direction_input(unsigned gpio) 657 { 658 return pinctrl_gpio_direction(gpio, true); 659 } 660 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input); 661 662 /** 663 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode 664 * @gpio: the GPIO pin number from the GPIO subsystem number space 665 * 666 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 667 * as part of their gpio_direction_output() semantics, platforms and individual 668 * drivers shall *NOT* touch pin control GPIO calls. 669 */ 670 int pinctrl_gpio_direction_output(unsigned gpio) 671 { 672 return pinctrl_gpio_direction(gpio, false); 673 } 674 EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); 675 676 static struct pinctrl_state *find_state(struct pinctrl *p, 677 const char *name) 678 { 679 struct pinctrl_state *state; 680 681 list_for_each_entry(state, &p->states, node) 682 if (!strcmp(state->name, name)) 683 return state; 684 685 return NULL; 686 } 687 688 static struct pinctrl_state *create_state(struct pinctrl *p, 689 const char *name) 690 { 691 struct pinctrl_state *state; 692 693 state = kzalloc(sizeof(*state), GFP_KERNEL); 694 if (state == NULL) { 695 dev_err(p->dev, 696 "failed to alloc struct pinctrl_state\n"); 697 return ERR_PTR(-ENOMEM); 698 } 699 700 state->name = name; 701 INIT_LIST_HEAD(&state->settings); 702 703 list_add_tail(&state->node, &p->states); 704 705 return state; 706 } 707 708 static int add_setting(struct pinctrl *p, struct pinctrl_map const *map) 709 { 710 struct pinctrl_state *state; 711 struct pinctrl_setting *setting; 712 int ret; 713 714 state = find_state(p, map->name); 715 if (!state) 716 state = create_state(p, map->name); 717 if (IS_ERR(state)) 718 return PTR_ERR(state); 719 720 if (map->type == PIN_MAP_TYPE_DUMMY_STATE) 721 return 0; 722 723 setting = kzalloc(sizeof(*setting), GFP_KERNEL); 724 if (setting == NULL) { 725 dev_err(p->dev, 726 "failed to alloc struct pinctrl_setting\n"); 727 return -ENOMEM; 728 } 729 730 setting->type = map->type; 731 732 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name); 733 if (setting->pctldev == NULL) { 734 kfree(setting); 735 /* Do not defer probing of hogs (circular loop) */ 736 if (!strcmp(map->ctrl_dev_name, map->dev_name)) 737 return -ENODEV; 738 /* 739 * OK let us guess that the driver is not there yet, and 740 * let's defer obtaining this pinctrl handle to later... 741 */ 742 dev_info(p->dev, "unknown pinctrl device %s in map entry, deferring probe", 743 map->ctrl_dev_name); 744 return -EPROBE_DEFER; 745 } 746 747 setting->dev_name = map->dev_name; 748 749 switch (map->type) { 750 case PIN_MAP_TYPE_MUX_GROUP: 751 ret = pinmux_map_to_setting(map, setting); 752 break; 753 case PIN_MAP_TYPE_CONFIGS_PIN: 754 case PIN_MAP_TYPE_CONFIGS_GROUP: 755 ret = pinconf_map_to_setting(map, setting); 756 break; 757 default: 758 ret = -EINVAL; 759 break; 760 } 761 if (ret < 0) { 762 kfree(setting); 763 return ret; 764 } 765 766 list_add_tail(&setting->node, &state->settings); 767 768 return 0; 769 } 770 771 static struct pinctrl *find_pinctrl(struct device *dev) 772 { 773 struct pinctrl *p; 774 775 mutex_lock(&pinctrl_list_mutex); 776 list_for_each_entry(p, &pinctrl_list, node) 777 if (p->dev == dev) { 778 mutex_unlock(&pinctrl_list_mutex); 779 return p; 780 } 781 782 mutex_unlock(&pinctrl_list_mutex); 783 return NULL; 784 } 785 786 static void pinctrl_free(struct pinctrl *p, bool inlist); 787 788 static struct pinctrl *create_pinctrl(struct device *dev) 789 { 790 struct pinctrl *p; 791 const char *devname; 792 struct pinctrl_maps *maps_node; 793 int i; 794 struct pinctrl_map const *map; 795 int ret; 796 797 /* 798 * create the state cookie holder struct pinctrl for each 799 * mapping, this is what consumers will get when requesting 800 * a pin control handle with pinctrl_get() 801 */ 802 p = kzalloc(sizeof(*p), GFP_KERNEL); 803 if (p == NULL) { 804 dev_err(dev, "failed to alloc struct pinctrl\n"); 805 return ERR_PTR(-ENOMEM); 806 } 807 p->dev = dev; 808 INIT_LIST_HEAD(&p->states); 809 INIT_LIST_HEAD(&p->dt_maps); 810 811 ret = pinctrl_dt_to_map(p); 812 if (ret < 0) { 813 kfree(p); 814 return ERR_PTR(ret); 815 } 816 817 devname = dev_name(dev); 818 819 mutex_lock(&pinctrl_maps_mutex); 820 /* Iterate over the pin control maps to locate the right ones */ 821 for_each_maps(maps_node, i, map) { 822 /* Map must be for this device */ 823 if (strcmp(map->dev_name, devname)) 824 continue; 825 826 ret = add_setting(p, map); 827 /* 828 * At this point the adding of a setting may: 829 * 830 * - Defer, if the pinctrl device is not yet available 831 * - Fail, if the pinctrl device is not yet available, 832 * AND the setting is a hog. We cannot defer that, since 833 * the hog will kick in immediately after the device 834 * is registered. 835 * 836 * If the error returned was not -EPROBE_DEFER then we 837 * accumulate the errors to see if we end up with 838 * an -EPROBE_DEFER later, as that is the worst case. 839 */ 840 if (ret == -EPROBE_DEFER) { 841 pinctrl_free(p, false); 842 mutex_unlock(&pinctrl_maps_mutex); 843 return ERR_PTR(ret); 844 } 845 } 846 mutex_unlock(&pinctrl_maps_mutex); 847 848 if (ret < 0) { 849 /* If some other error than deferral occured, return here */ 850 pinctrl_free(p, false); 851 return ERR_PTR(ret); 852 } 853 854 kref_init(&p->users); 855 856 /* Add the pinctrl handle to the global list */ 857 mutex_lock(&pinctrl_list_mutex); 858 list_add_tail(&p->node, &pinctrl_list); 859 mutex_unlock(&pinctrl_list_mutex); 860 861 return p; 862 } 863 864 /** 865 * pinctrl_get() - retrieves the pinctrl handle for a device 866 * @dev: the device to obtain the handle for 867 */ 868 struct pinctrl *pinctrl_get(struct device *dev) 869 { 870 struct pinctrl *p; 871 872 if (WARN_ON(!dev)) 873 return ERR_PTR(-EINVAL); 874 875 /* 876 * See if somebody else (such as the device core) has already 877 * obtained a handle to the pinctrl for this device. In that case, 878 * return another pointer to it. 879 */ 880 p = find_pinctrl(dev); 881 if (p != NULL) { 882 dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n"); 883 kref_get(&p->users); 884 return p; 885 } 886 887 return create_pinctrl(dev); 888 } 889 EXPORT_SYMBOL_GPL(pinctrl_get); 890 891 static void pinctrl_free_setting(bool disable_setting, 892 struct pinctrl_setting *setting) 893 { 894 switch (setting->type) { 895 case PIN_MAP_TYPE_MUX_GROUP: 896 if (disable_setting) 897 pinmux_disable_setting(setting); 898 pinmux_free_setting(setting); 899 break; 900 case PIN_MAP_TYPE_CONFIGS_PIN: 901 case PIN_MAP_TYPE_CONFIGS_GROUP: 902 pinconf_free_setting(setting); 903 break; 904 default: 905 break; 906 } 907 } 908 909 static void pinctrl_free(struct pinctrl *p, bool inlist) 910 { 911 struct pinctrl_state *state, *n1; 912 struct pinctrl_setting *setting, *n2; 913 914 mutex_lock(&pinctrl_list_mutex); 915 list_for_each_entry_safe(state, n1, &p->states, node) { 916 list_for_each_entry_safe(setting, n2, &state->settings, node) { 917 pinctrl_free_setting(state == p->state, setting); 918 list_del(&setting->node); 919 kfree(setting); 920 } 921 list_del(&state->node); 922 kfree(state); 923 } 924 925 pinctrl_dt_free_maps(p); 926 927 if (inlist) 928 list_del(&p->node); 929 kfree(p); 930 mutex_unlock(&pinctrl_list_mutex); 931 } 932 933 /** 934 * pinctrl_release() - release the pinctrl handle 935 * @kref: the kref in the pinctrl being released 936 */ 937 static void pinctrl_release(struct kref *kref) 938 { 939 struct pinctrl *p = container_of(kref, struct pinctrl, users); 940 941 pinctrl_free(p, true); 942 } 943 944 /** 945 * pinctrl_put() - decrease use count on a previously claimed pinctrl handle 946 * @p: the pinctrl handle to release 947 */ 948 void pinctrl_put(struct pinctrl *p) 949 { 950 kref_put(&p->users, pinctrl_release); 951 } 952 EXPORT_SYMBOL_GPL(pinctrl_put); 953 954 /** 955 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle 956 * @p: the pinctrl handle to retrieve the state from 957 * @name: the state name to retrieve 958 */ 959 struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, 960 const char *name) 961 { 962 struct pinctrl_state *state; 963 964 state = find_state(p, name); 965 if (!state) { 966 if (pinctrl_dummy_state) { 967 /* create dummy state */ 968 dev_dbg(p->dev, "using pinctrl dummy state (%s)\n", 969 name); 970 state = create_state(p, name); 971 } else 972 state = ERR_PTR(-ENODEV); 973 } 974 975 return state; 976 } 977 EXPORT_SYMBOL_GPL(pinctrl_lookup_state); 978 979 /** 980 * pinctrl_select_state() - select/activate/program a pinctrl state to HW 981 * @p: the pinctrl handle for the device that requests configuration 982 * @state: the state handle to select/activate/program 983 */ 984 int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state) 985 { 986 struct pinctrl_setting *setting, *setting2; 987 struct pinctrl_state *old_state = p->state; 988 int ret; 989 990 if (p->state == state) 991 return 0; 992 993 if (p->state) { 994 /* 995 * The set of groups with a mux configuration in the old state 996 * may not be identical to the set of groups with a mux setting 997 * in the new state. While this might be unusual, it's entirely 998 * possible for the "user"-supplied mapping table to be written 999 * that way. For each group that was configured in the old state 1000 * but not in the new state, this code puts that group into a 1001 * safe/disabled state. 1002 */ 1003 list_for_each_entry(setting, &p->state->settings, node) { 1004 bool found = false; 1005 if (setting->type != PIN_MAP_TYPE_MUX_GROUP) 1006 continue; 1007 list_for_each_entry(setting2, &state->settings, node) { 1008 if (setting2->type != PIN_MAP_TYPE_MUX_GROUP) 1009 continue; 1010 if (setting2->data.mux.group == 1011 setting->data.mux.group) { 1012 found = true; 1013 break; 1014 } 1015 } 1016 if (!found) 1017 pinmux_disable_setting(setting); 1018 } 1019 } 1020 1021 p->state = NULL; 1022 1023 /* Apply all the settings for the new state */ 1024 list_for_each_entry(setting, &state->settings, node) { 1025 switch (setting->type) { 1026 case PIN_MAP_TYPE_MUX_GROUP: 1027 ret = pinmux_enable_setting(setting); 1028 break; 1029 case PIN_MAP_TYPE_CONFIGS_PIN: 1030 case PIN_MAP_TYPE_CONFIGS_GROUP: 1031 ret = pinconf_apply_setting(setting); 1032 break; 1033 default: 1034 ret = -EINVAL; 1035 break; 1036 } 1037 1038 if (ret < 0) { 1039 goto unapply_new_state; 1040 } 1041 } 1042 1043 p->state = state; 1044 1045 return 0; 1046 1047 unapply_new_state: 1048 dev_err(p->dev, "Error applying setting, reverse things back\n"); 1049 1050 list_for_each_entry(setting2, &state->settings, node) { 1051 if (&setting2->node == &setting->node) 1052 break; 1053 /* 1054 * All we can do here is pinmux_disable_setting. 1055 * That means that some pins are muxed differently now 1056 * than they were before applying the setting (We can't 1057 * "unmux a pin"!), but it's not a big deal since the pins 1058 * are free to be muxed by another apply_setting. 1059 */ 1060 if (setting2->type == PIN_MAP_TYPE_MUX_GROUP) 1061 pinmux_disable_setting(setting2); 1062 } 1063 1064 /* There's no infinite recursive loop here because p->state is NULL */ 1065 if (old_state) 1066 pinctrl_select_state(p, old_state); 1067 1068 return ret; 1069 } 1070 EXPORT_SYMBOL_GPL(pinctrl_select_state); 1071 1072 static void devm_pinctrl_release(struct device *dev, void *res) 1073 { 1074 pinctrl_put(*(struct pinctrl **)res); 1075 } 1076 1077 /** 1078 * struct devm_pinctrl_get() - Resource managed pinctrl_get() 1079 * @dev: the device to obtain the handle for 1080 * 1081 * If there is a need to explicitly destroy the returned struct pinctrl, 1082 * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). 1083 */ 1084 struct pinctrl *devm_pinctrl_get(struct device *dev) 1085 { 1086 struct pinctrl **ptr, *p; 1087 1088 ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL); 1089 if (!ptr) 1090 return ERR_PTR(-ENOMEM); 1091 1092 p = pinctrl_get(dev); 1093 if (!IS_ERR(p)) { 1094 *ptr = p; 1095 devres_add(dev, ptr); 1096 } else { 1097 devres_free(ptr); 1098 } 1099 1100 return p; 1101 } 1102 EXPORT_SYMBOL_GPL(devm_pinctrl_get); 1103 1104 static int devm_pinctrl_match(struct device *dev, void *res, void *data) 1105 { 1106 struct pinctrl **p = res; 1107 1108 return *p == data; 1109 } 1110 1111 /** 1112 * devm_pinctrl_put() - Resource managed pinctrl_put() 1113 * @p: the pinctrl handle to release 1114 * 1115 * Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally 1116 * this function will not need to be called and the resource management 1117 * code will ensure that the resource is freed. 1118 */ 1119 void devm_pinctrl_put(struct pinctrl *p) 1120 { 1121 WARN_ON(devres_release(p->dev, devm_pinctrl_release, 1122 devm_pinctrl_match, p)); 1123 } 1124 EXPORT_SYMBOL_GPL(devm_pinctrl_put); 1125 1126 int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps, 1127 bool dup, bool locked) 1128 { 1129 int i, ret; 1130 struct pinctrl_maps *maps_node; 1131 1132 pr_debug("add %d pinmux maps\n", num_maps); 1133 1134 /* First sanity check the new mapping */ 1135 for (i = 0; i < num_maps; i++) { 1136 if (!maps[i].dev_name) { 1137 pr_err("failed to register map %s (%d): no device given\n", 1138 maps[i].name, i); 1139 return -EINVAL; 1140 } 1141 1142 if (!maps[i].name) { 1143 pr_err("failed to register map %d: no map name given\n", 1144 i); 1145 return -EINVAL; 1146 } 1147 1148 if (maps[i].type != PIN_MAP_TYPE_DUMMY_STATE && 1149 !maps[i].ctrl_dev_name) { 1150 pr_err("failed to register map %s (%d): no pin control device given\n", 1151 maps[i].name, i); 1152 return -EINVAL; 1153 } 1154 1155 switch (maps[i].type) { 1156 case PIN_MAP_TYPE_DUMMY_STATE: 1157 break; 1158 case PIN_MAP_TYPE_MUX_GROUP: 1159 ret = pinmux_validate_map(&maps[i], i); 1160 if (ret < 0) 1161 return ret; 1162 break; 1163 case PIN_MAP_TYPE_CONFIGS_PIN: 1164 case PIN_MAP_TYPE_CONFIGS_GROUP: 1165 ret = pinconf_validate_map(&maps[i], i); 1166 if (ret < 0) 1167 return ret; 1168 break; 1169 default: 1170 pr_err("failed to register map %s (%d): invalid type given\n", 1171 maps[i].name, i); 1172 return -EINVAL; 1173 } 1174 } 1175 1176 maps_node = kzalloc(sizeof(*maps_node), GFP_KERNEL); 1177 if (!maps_node) { 1178 pr_err("failed to alloc struct pinctrl_maps\n"); 1179 return -ENOMEM; 1180 } 1181 1182 maps_node->num_maps = num_maps; 1183 if (dup) { 1184 maps_node->maps = kmemdup(maps, sizeof(*maps) * num_maps, 1185 GFP_KERNEL); 1186 if (!maps_node->maps) { 1187 pr_err("failed to duplicate mapping table\n"); 1188 kfree(maps_node); 1189 return -ENOMEM; 1190 } 1191 } else { 1192 maps_node->maps = maps; 1193 } 1194 1195 if (!locked) 1196 mutex_lock(&pinctrl_maps_mutex); 1197 list_add_tail(&maps_node->node, &pinctrl_maps); 1198 if (!locked) 1199 mutex_unlock(&pinctrl_maps_mutex); 1200 1201 return 0; 1202 } 1203 1204 /** 1205 * pinctrl_register_mappings() - register a set of pin controller mappings 1206 * @maps: the pincontrol mappings table to register. This should probably be 1207 * marked with __initdata so it can be discarded after boot. This 1208 * function will perform a shallow copy for the mapping entries. 1209 * @num_maps: the number of maps in the mapping table 1210 */ 1211 int pinctrl_register_mappings(struct pinctrl_map const *maps, 1212 unsigned num_maps) 1213 { 1214 return pinctrl_register_map(maps, num_maps, true, false); 1215 } 1216 1217 void pinctrl_unregister_map(struct pinctrl_map const *map) 1218 { 1219 struct pinctrl_maps *maps_node; 1220 1221 mutex_lock(&pinctrl_maps_mutex); 1222 list_for_each_entry(maps_node, &pinctrl_maps, node) { 1223 if (maps_node->maps == map) { 1224 list_del(&maps_node->node); 1225 kfree(maps_node); 1226 mutex_unlock(&pinctrl_maps_mutex); 1227 return; 1228 } 1229 } 1230 mutex_unlock(&pinctrl_maps_mutex); 1231 } 1232 1233 /** 1234 * pinctrl_force_sleep() - turn a given controller device into sleep state 1235 * @pctldev: pin controller device 1236 */ 1237 int pinctrl_force_sleep(struct pinctrl_dev *pctldev) 1238 { 1239 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_sleep)) 1240 return pinctrl_select_state(pctldev->p, pctldev->hog_sleep); 1241 return 0; 1242 } 1243 EXPORT_SYMBOL_GPL(pinctrl_force_sleep); 1244 1245 /** 1246 * pinctrl_force_default() - turn a given controller device into default state 1247 * @pctldev: pin controller device 1248 */ 1249 int pinctrl_force_default(struct pinctrl_dev *pctldev) 1250 { 1251 if (!IS_ERR(pctldev->p) && !IS_ERR(pctldev->hog_default)) 1252 return pinctrl_select_state(pctldev->p, pctldev->hog_default); 1253 return 0; 1254 } 1255 EXPORT_SYMBOL_GPL(pinctrl_force_default); 1256 1257 #ifdef CONFIG_PM 1258 1259 /** 1260 * pinctrl_pm_select_state() - select pinctrl state for PM 1261 * @dev: device to select default state for 1262 * @state: state to set 1263 */ 1264 static int pinctrl_pm_select_state(struct device *dev, 1265 struct pinctrl_state *state) 1266 { 1267 struct dev_pin_info *pins = dev->pins; 1268 int ret; 1269 1270 if (IS_ERR(state)) 1271 return 0; /* No such state */ 1272 ret = pinctrl_select_state(pins->p, state); 1273 if (ret) 1274 dev_err(dev, "failed to activate pinctrl state %s\n", 1275 state->name); 1276 return ret; 1277 } 1278 1279 /** 1280 * pinctrl_pm_select_default_state() - select default pinctrl state for PM 1281 * @dev: device to select default state for 1282 */ 1283 int pinctrl_pm_select_default_state(struct device *dev) 1284 { 1285 if (!dev->pins) 1286 return 0; 1287 1288 return pinctrl_pm_select_state(dev, dev->pins->default_state); 1289 } 1290 EXPORT_SYMBOL_GPL(pinctrl_pm_select_default_state); 1291 1292 /** 1293 * pinctrl_pm_select_sleep_state() - select sleep pinctrl state for PM 1294 * @dev: device to select sleep state for 1295 */ 1296 int pinctrl_pm_select_sleep_state(struct device *dev) 1297 { 1298 if (!dev->pins) 1299 return 0; 1300 1301 return pinctrl_pm_select_state(dev, dev->pins->sleep_state); 1302 } 1303 EXPORT_SYMBOL_GPL(pinctrl_pm_select_sleep_state); 1304 1305 /** 1306 * pinctrl_pm_select_idle_state() - select idle pinctrl state for PM 1307 * @dev: device to select idle state for 1308 */ 1309 int pinctrl_pm_select_idle_state(struct device *dev) 1310 { 1311 if (!dev->pins) 1312 return 0; 1313 1314 return pinctrl_pm_select_state(dev, dev->pins->idle_state); 1315 } 1316 EXPORT_SYMBOL_GPL(pinctrl_pm_select_idle_state); 1317 #endif 1318 1319 #ifdef CONFIG_DEBUG_FS 1320 1321 static int pinctrl_pins_show(struct seq_file *s, void *what) 1322 { 1323 struct pinctrl_dev *pctldev = s->private; 1324 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1325 unsigned i, pin; 1326 1327 seq_printf(s, "registered pins: %d\n", pctldev->desc->npins); 1328 1329 mutex_lock(&pctldev->mutex); 1330 1331 /* The pin number can be retrived from the pin controller descriptor */ 1332 for (i = 0; i < pctldev->desc->npins; i++) { 1333 struct pin_desc *desc; 1334 1335 pin = pctldev->desc->pins[i].number; 1336 desc = pin_desc_get(pctldev, pin); 1337 /* Pin space may be sparse */ 1338 if (desc == NULL) 1339 continue; 1340 1341 seq_printf(s, "pin %d (%s) ", pin, 1342 desc->name ? desc->name : "unnamed"); 1343 1344 /* Driver-specific info per pin */ 1345 if (ops->pin_dbg_show) 1346 ops->pin_dbg_show(pctldev, s, pin); 1347 1348 seq_puts(s, "\n"); 1349 } 1350 1351 mutex_unlock(&pctldev->mutex); 1352 1353 return 0; 1354 } 1355 1356 static int pinctrl_groups_show(struct seq_file *s, void *what) 1357 { 1358 struct pinctrl_dev *pctldev = s->private; 1359 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1360 unsigned ngroups, selector = 0; 1361 1362 mutex_lock(&pctldev->mutex); 1363 1364 ngroups = ops->get_groups_count(pctldev); 1365 1366 seq_puts(s, "registered pin groups:\n"); 1367 while (selector < ngroups) { 1368 const unsigned *pins = NULL; 1369 unsigned num_pins = 0; 1370 const char *gname = ops->get_group_name(pctldev, selector); 1371 const char *pname; 1372 int ret = 0; 1373 int i; 1374 1375 if (ops->get_group_pins) 1376 ret = ops->get_group_pins(pctldev, selector, 1377 &pins, &num_pins); 1378 if (ret) 1379 seq_printf(s, "%s [ERROR GETTING PINS]\n", 1380 gname); 1381 else { 1382 seq_printf(s, "group: %s\n", gname); 1383 for (i = 0; i < num_pins; i++) { 1384 pname = pin_get_name(pctldev, pins[i]); 1385 if (WARN_ON(!pname)) { 1386 mutex_unlock(&pctldev->mutex); 1387 return -EINVAL; 1388 } 1389 seq_printf(s, "pin %d (%s)\n", pins[i], pname); 1390 } 1391 seq_puts(s, "\n"); 1392 } 1393 selector++; 1394 } 1395 1396 mutex_unlock(&pctldev->mutex); 1397 1398 return 0; 1399 } 1400 1401 static int pinctrl_gpioranges_show(struct seq_file *s, void *what) 1402 { 1403 struct pinctrl_dev *pctldev = s->private; 1404 struct pinctrl_gpio_range *range = NULL; 1405 1406 seq_puts(s, "GPIO ranges handled:\n"); 1407 1408 mutex_lock(&pctldev->mutex); 1409 1410 /* Loop over the ranges */ 1411 list_for_each_entry(range, &pctldev->gpio_ranges, node) { 1412 if (range->pins) { 1413 int a; 1414 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS {", 1415 range->id, range->name, 1416 range->base, (range->base + range->npins - 1)); 1417 for (a = 0; a < range->npins - 1; a++) 1418 seq_printf(s, "%u, ", range->pins[a]); 1419 seq_printf(s, "%u}\n", range->pins[a]); 1420 } 1421 else 1422 seq_printf(s, "%u: %s GPIOS [%u - %u] PINS [%u - %u]\n", 1423 range->id, range->name, 1424 range->base, (range->base + range->npins - 1), 1425 range->pin_base, 1426 (range->pin_base + range->npins - 1)); 1427 } 1428 1429 mutex_unlock(&pctldev->mutex); 1430 1431 return 0; 1432 } 1433 1434 static int pinctrl_devices_show(struct seq_file *s, void *what) 1435 { 1436 struct pinctrl_dev *pctldev; 1437 1438 seq_puts(s, "name [pinmux] [pinconf]\n"); 1439 1440 mutex_lock(&pinctrldev_list_mutex); 1441 1442 list_for_each_entry(pctldev, &pinctrldev_list, node) { 1443 seq_printf(s, "%s ", pctldev->desc->name); 1444 if (pctldev->desc->pmxops) 1445 seq_puts(s, "yes "); 1446 else 1447 seq_puts(s, "no "); 1448 if (pctldev->desc->confops) 1449 seq_puts(s, "yes"); 1450 else 1451 seq_puts(s, "no"); 1452 seq_puts(s, "\n"); 1453 } 1454 1455 mutex_unlock(&pinctrldev_list_mutex); 1456 1457 return 0; 1458 } 1459 1460 static inline const char *map_type(enum pinctrl_map_type type) 1461 { 1462 static const char * const names[] = { 1463 "INVALID", 1464 "DUMMY_STATE", 1465 "MUX_GROUP", 1466 "CONFIGS_PIN", 1467 "CONFIGS_GROUP", 1468 }; 1469 1470 if (type >= ARRAY_SIZE(names)) 1471 return "UNKNOWN"; 1472 1473 return names[type]; 1474 } 1475 1476 static int pinctrl_maps_show(struct seq_file *s, void *what) 1477 { 1478 struct pinctrl_maps *maps_node; 1479 int i; 1480 struct pinctrl_map const *map; 1481 1482 seq_puts(s, "Pinctrl maps:\n"); 1483 1484 mutex_lock(&pinctrl_maps_mutex); 1485 for_each_maps(maps_node, i, map) { 1486 seq_printf(s, "device %s\nstate %s\ntype %s (%d)\n", 1487 map->dev_name, map->name, map_type(map->type), 1488 map->type); 1489 1490 if (map->type != PIN_MAP_TYPE_DUMMY_STATE) 1491 seq_printf(s, "controlling device %s\n", 1492 map->ctrl_dev_name); 1493 1494 switch (map->type) { 1495 case PIN_MAP_TYPE_MUX_GROUP: 1496 pinmux_show_map(s, map); 1497 break; 1498 case PIN_MAP_TYPE_CONFIGS_PIN: 1499 case PIN_MAP_TYPE_CONFIGS_GROUP: 1500 pinconf_show_map(s, map); 1501 break; 1502 default: 1503 break; 1504 } 1505 1506 seq_printf(s, "\n"); 1507 } 1508 mutex_unlock(&pinctrl_maps_mutex); 1509 1510 return 0; 1511 } 1512 1513 static int pinctrl_show(struct seq_file *s, void *what) 1514 { 1515 struct pinctrl *p; 1516 struct pinctrl_state *state; 1517 struct pinctrl_setting *setting; 1518 1519 seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); 1520 1521 mutex_lock(&pinctrl_list_mutex); 1522 1523 list_for_each_entry(p, &pinctrl_list, node) { 1524 seq_printf(s, "device: %s current state: %s\n", 1525 dev_name(p->dev), 1526 p->state ? p->state->name : "none"); 1527 1528 list_for_each_entry(state, &p->states, node) { 1529 seq_printf(s, " state: %s\n", state->name); 1530 1531 list_for_each_entry(setting, &state->settings, node) { 1532 struct pinctrl_dev *pctldev = setting->pctldev; 1533 1534 seq_printf(s, " type: %s controller %s ", 1535 map_type(setting->type), 1536 pinctrl_dev_get_name(pctldev)); 1537 1538 switch (setting->type) { 1539 case PIN_MAP_TYPE_MUX_GROUP: 1540 pinmux_show_setting(s, setting); 1541 break; 1542 case PIN_MAP_TYPE_CONFIGS_PIN: 1543 case PIN_MAP_TYPE_CONFIGS_GROUP: 1544 pinconf_show_setting(s, setting); 1545 break; 1546 default: 1547 break; 1548 } 1549 } 1550 } 1551 } 1552 1553 mutex_unlock(&pinctrl_list_mutex); 1554 1555 return 0; 1556 } 1557 1558 static int pinctrl_pins_open(struct inode *inode, struct file *file) 1559 { 1560 return single_open(file, pinctrl_pins_show, inode->i_private); 1561 } 1562 1563 static int pinctrl_groups_open(struct inode *inode, struct file *file) 1564 { 1565 return single_open(file, pinctrl_groups_show, inode->i_private); 1566 } 1567 1568 static int pinctrl_gpioranges_open(struct inode *inode, struct file *file) 1569 { 1570 return single_open(file, pinctrl_gpioranges_show, inode->i_private); 1571 } 1572 1573 static int pinctrl_devices_open(struct inode *inode, struct file *file) 1574 { 1575 return single_open(file, pinctrl_devices_show, NULL); 1576 } 1577 1578 static int pinctrl_maps_open(struct inode *inode, struct file *file) 1579 { 1580 return single_open(file, pinctrl_maps_show, NULL); 1581 } 1582 1583 static int pinctrl_open(struct inode *inode, struct file *file) 1584 { 1585 return single_open(file, pinctrl_show, NULL); 1586 } 1587 1588 static const struct file_operations pinctrl_pins_ops = { 1589 .open = pinctrl_pins_open, 1590 .read = seq_read, 1591 .llseek = seq_lseek, 1592 .release = single_release, 1593 }; 1594 1595 static const struct file_operations pinctrl_groups_ops = { 1596 .open = pinctrl_groups_open, 1597 .read = seq_read, 1598 .llseek = seq_lseek, 1599 .release = single_release, 1600 }; 1601 1602 static const struct file_operations pinctrl_gpioranges_ops = { 1603 .open = pinctrl_gpioranges_open, 1604 .read = seq_read, 1605 .llseek = seq_lseek, 1606 .release = single_release, 1607 }; 1608 1609 static const struct file_operations pinctrl_devices_ops = { 1610 .open = pinctrl_devices_open, 1611 .read = seq_read, 1612 .llseek = seq_lseek, 1613 .release = single_release, 1614 }; 1615 1616 static const struct file_operations pinctrl_maps_ops = { 1617 .open = pinctrl_maps_open, 1618 .read = seq_read, 1619 .llseek = seq_lseek, 1620 .release = single_release, 1621 }; 1622 1623 static const struct file_operations pinctrl_ops = { 1624 .open = pinctrl_open, 1625 .read = seq_read, 1626 .llseek = seq_lseek, 1627 .release = single_release, 1628 }; 1629 1630 static struct dentry *debugfs_root; 1631 1632 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) 1633 { 1634 struct dentry *device_root; 1635 1636 device_root = debugfs_create_dir(dev_name(pctldev->dev), 1637 debugfs_root); 1638 pctldev->device_root = device_root; 1639 1640 if (IS_ERR(device_root) || !device_root) { 1641 pr_warn("failed to create debugfs directory for %s\n", 1642 dev_name(pctldev->dev)); 1643 return; 1644 } 1645 debugfs_create_file("pins", S_IFREG | S_IRUGO, 1646 device_root, pctldev, &pinctrl_pins_ops); 1647 debugfs_create_file("pingroups", S_IFREG | S_IRUGO, 1648 device_root, pctldev, &pinctrl_groups_ops); 1649 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, 1650 device_root, pctldev, &pinctrl_gpioranges_ops); 1651 if (pctldev->desc->pmxops) 1652 pinmux_init_device_debugfs(device_root, pctldev); 1653 if (pctldev->desc->confops) 1654 pinconf_init_device_debugfs(device_root, pctldev); 1655 } 1656 1657 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) 1658 { 1659 debugfs_remove_recursive(pctldev->device_root); 1660 } 1661 1662 static void pinctrl_init_debugfs(void) 1663 { 1664 debugfs_root = debugfs_create_dir("pinctrl", NULL); 1665 if (IS_ERR(debugfs_root) || !debugfs_root) { 1666 pr_warn("failed to create debugfs directory\n"); 1667 debugfs_root = NULL; 1668 return; 1669 } 1670 1671 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, 1672 debugfs_root, NULL, &pinctrl_devices_ops); 1673 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO, 1674 debugfs_root, NULL, &pinctrl_maps_ops); 1675 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO, 1676 debugfs_root, NULL, &pinctrl_ops); 1677 } 1678 1679 #else /* CONFIG_DEBUG_FS */ 1680 1681 static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) 1682 { 1683 } 1684 1685 static void pinctrl_init_debugfs(void) 1686 { 1687 } 1688 1689 static void pinctrl_remove_device_debugfs(struct pinctrl_dev *pctldev) 1690 { 1691 } 1692 1693 #endif 1694 1695 static int pinctrl_check_ops(struct pinctrl_dev *pctldev) 1696 { 1697 const struct pinctrl_ops *ops = pctldev->desc->pctlops; 1698 1699 if (!ops || 1700 !ops->get_groups_count || 1701 !ops->get_group_name) 1702 return -EINVAL; 1703 1704 if (ops->dt_node_to_map && !ops->dt_free_map) 1705 return -EINVAL; 1706 1707 return 0; 1708 } 1709 1710 /** 1711 * pinctrl_register() - register a pin controller device 1712 * @pctldesc: descriptor for this pin controller 1713 * @dev: parent device for this pin controller 1714 * @driver_data: private pin controller data for this pin controller 1715 */ 1716 struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc, 1717 struct device *dev, void *driver_data) 1718 { 1719 struct pinctrl_dev *pctldev; 1720 int ret; 1721 1722 if (!pctldesc) 1723 return NULL; 1724 if (!pctldesc->name) 1725 return NULL; 1726 1727 pctldev = kzalloc(sizeof(*pctldev), GFP_KERNEL); 1728 if (pctldev == NULL) { 1729 dev_err(dev, "failed to alloc struct pinctrl_dev\n"); 1730 return NULL; 1731 } 1732 1733 /* Initialize pin control device struct */ 1734 pctldev->owner = pctldesc->owner; 1735 pctldev->desc = pctldesc; 1736 pctldev->driver_data = driver_data; 1737 INIT_RADIX_TREE(&pctldev->pin_desc_tree, GFP_KERNEL); 1738 INIT_LIST_HEAD(&pctldev->gpio_ranges); 1739 pctldev->dev = dev; 1740 mutex_init(&pctldev->mutex); 1741 1742 /* check core ops for sanity */ 1743 if (pinctrl_check_ops(pctldev)) { 1744 dev_err(dev, "pinctrl ops lacks necessary functions\n"); 1745 goto out_err; 1746 } 1747 1748 /* If we're implementing pinmuxing, check the ops for sanity */ 1749 if (pctldesc->pmxops) { 1750 if (pinmux_check_ops(pctldev)) 1751 goto out_err; 1752 } 1753 1754 /* If we're implementing pinconfig, check the ops for sanity */ 1755 if (pctldesc->confops) { 1756 if (pinconf_check_ops(pctldev)) 1757 goto out_err; 1758 } 1759 1760 /* Register all the pins */ 1761 dev_dbg(dev, "try to register %d pins ...\n", pctldesc->npins); 1762 ret = pinctrl_register_pins(pctldev, pctldesc->pins, pctldesc->npins); 1763 if (ret) { 1764 dev_err(dev, "error during pin registration\n"); 1765 pinctrl_free_pindescs(pctldev, pctldesc->pins, 1766 pctldesc->npins); 1767 goto out_err; 1768 } 1769 1770 mutex_lock(&pinctrldev_list_mutex); 1771 list_add_tail(&pctldev->node, &pinctrldev_list); 1772 mutex_unlock(&pinctrldev_list_mutex); 1773 1774 pctldev->p = pinctrl_get(pctldev->dev); 1775 1776 if (!IS_ERR(pctldev->p)) { 1777 pctldev->hog_default = 1778 pinctrl_lookup_state(pctldev->p, PINCTRL_STATE_DEFAULT); 1779 if (IS_ERR(pctldev->hog_default)) { 1780 dev_dbg(dev, "failed to lookup the default state\n"); 1781 } else { 1782 if (pinctrl_select_state(pctldev->p, 1783 pctldev->hog_default)) 1784 dev_err(dev, 1785 "failed to select default state\n"); 1786 } 1787 1788 pctldev->hog_sleep = 1789 pinctrl_lookup_state(pctldev->p, 1790 PINCTRL_STATE_SLEEP); 1791 if (IS_ERR(pctldev->hog_sleep)) 1792 dev_dbg(dev, "failed to lookup the sleep state\n"); 1793 } 1794 1795 pinctrl_init_device_debugfs(pctldev); 1796 1797 return pctldev; 1798 1799 out_err: 1800 mutex_destroy(&pctldev->mutex); 1801 kfree(pctldev); 1802 return NULL; 1803 } 1804 EXPORT_SYMBOL_GPL(pinctrl_register); 1805 1806 /** 1807 * pinctrl_unregister() - unregister pinmux 1808 * @pctldev: pin controller to unregister 1809 * 1810 * Called by pinmux drivers to unregister a pinmux. 1811 */ 1812 void pinctrl_unregister(struct pinctrl_dev *pctldev) 1813 { 1814 struct pinctrl_gpio_range *range, *n; 1815 if (pctldev == NULL) 1816 return; 1817 1818 mutex_lock(&pinctrldev_list_mutex); 1819 mutex_lock(&pctldev->mutex); 1820 1821 pinctrl_remove_device_debugfs(pctldev); 1822 1823 if (!IS_ERR(pctldev->p)) 1824 pinctrl_put(pctldev->p); 1825 1826 /* TODO: check that no pinmuxes are still active? */ 1827 list_del(&pctldev->node); 1828 /* Destroy descriptor tree */ 1829 pinctrl_free_pindescs(pctldev, pctldev->desc->pins, 1830 pctldev->desc->npins); 1831 /* remove gpio ranges map */ 1832 list_for_each_entry_safe(range, n, &pctldev->gpio_ranges, node) 1833 list_del(&range->node); 1834 1835 mutex_unlock(&pctldev->mutex); 1836 mutex_destroy(&pctldev->mutex); 1837 kfree(pctldev); 1838 mutex_unlock(&pinctrldev_list_mutex); 1839 } 1840 EXPORT_SYMBOL_GPL(pinctrl_unregister); 1841 1842 static int __init pinctrl_init(void) 1843 { 1844 pr_info("initialized pinctrl subsystem\n"); 1845 pinctrl_init_debugfs(); 1846 return 0; 1847 } 1848 1849 /* init early since many drivers really need to initialized pinmux early */ 1850 core_initcall(pinctrl_init); 1851