pinmux.c revision 3cc70ed32cd6c5cb57de17bde615148ed7eda88f
1/* 2 * Core driver for the pin muxing portions of 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 * License terms: GNU General Public License (GPL) version 2 11 */ 12#define pr_fmt(fmt) "pinmux core: " fmt 13 14#include <linux/kernel.h> 15#include <linux/module.h> 16#include <linux/init.h> 17#include <linux/device.h> 18#include <linux/slab.h> 19#include <linux/radix-tree.h> 20#include <linux/err.h> 21#include <linux/list.h> 22#include <linux/mutex.h> 23#include <linux/spinlock.h> 24#include <linux/string.h> 25#include <linux/sysfs.h> 26#include <linux/debugfs.h> 27#include <linux/seq_file.h> 28#include <linux/pinctrl/machine.h> 29#include <linux/pinctrl/pinmux.h> 30#include "core.h" 31#include "pinmux.h" 32 33/** 34 * struct pinmux_group - group list item for pinmux groups 35 * @node: pinmux group list node 36 * @group_selector: the group selector for this group 37 */ 38struct pinmux_group { 39 struct list_head node; 40 unsigned group_selector; 41}; 42 43int pinmux_check_ops(struct pinctrl_dev *pctldev) 44{ 45 const struct pinmux_ops *ops = pctldev->desc->pmxops; 46 unsigned selector = 0; 47 48 /* Check that we implement required operations */ 49 if (!ops->list_functions || 50 !ops->get_function_name || 51 !ops->get_function_groups || 52 !ops->enable || 53 !ops->disable) 54 return -EINVAL; 55 56 /* Check that all functions registered have names */ 57 while (ops->list_functions(pctldev, selector) >= 0) { 58 const char *fname = ops->get_function_name(pctldev, 59 selector); 60 if (!fname) { 61 pr_err("pinmux ops has no name for function%u\n", 62 selector); 63 return -EINVAL; 64 } 65 selector++; 66 } 67 68 return 0; 69} 70 71/** 72 * pin_request() - request a single pin to be muxed in, typically for GPIO 73 * @pin: the pin number in the global pin space 74 * @owner: a representation of the owner of this pin; typically the device 75 * name that controls its mux function, or the requested GPIO name 76 * @gpio_range: the range matching the GPIO pin if this is a request for a 77 * single GPIO pin 78 */ 79static int pin_request(struct pinctrl_dev *pctldev, 80 int pin, const char *owner, 81 struct pinctrl_gpio_range *gpio_range) 82{ 83 struct pin_desc *desc; 84 const struct pinmux_ops *ops = pctldev->desc->pmxops; 85 int status = -EINVAL; 86 87 dev_dbg(pctldev->dev, "request pin %d for %s\n", pin, owner); 88 89 desc = pin_desc_get(pctldev, pin); 90 if (desc == NULL) { 91 dev_err(pctldev->dev, 92 "pin is not registered so it cannot be requested\n"); 93 goto out; 94 } 95 96 spin_lock(&desc->lock); 97 if (desc->owner && strcmp(desc->owner, owner)) { 98 spin_unlock(&desc->lock); 99 dev_err(pctldev->dev, 100 "pin already requested\n"); 101 goto out; 102 } 103 desc->owner = owner; 104 spin_unlock(&desc->lock); 105 106 /* Let each pin increase references to this module */ 107 if (!try_module_get(pctldev->owner)) { 108 dev_err(pctldev->dev, 109 "could not increase module refcount for pin %d\n", 110 pin); 111 status = -EINVAL; 112 goto out_free_pin; 113 } 114 115 /* 116 * If there is no kind of request function for the pin we just assume 117 * we got it by default and proceed. 118 */ 119 if (gpio_range && ops->gpio_request_enable) 120 /* This requests and enables a single GPIO pin */ 121 status = ops->gpio_request_enable(pctldev, gpio_range, pin); 122 else if (ops->request) 123 status = ops->request(pctldev, pin); 124 else 125 status = 0; 126 127 if (status) 128 dev_err(pctldev->dev, "->request on device %s failed for pin %d\n", 129 pctldev->desc->name, pin); 130out_free_pin: 131 if (status) { 132 spin_lock(&desc->lock); 133 desc->owner = NULL; 134 spin_unlock(&desc->lock); 135 } 136out: 137 if (status) 138 dev_err(pctldev->dev, "pin-%d (%s) status %d\n", 139 pin, owner, status); 140 141 return status; 142} 143 144/** 145 * pin_free() - release a single muxed in pin so something else can be muxed 146 * @pctldev: pin controller device handling this pin 147 * @pin: the pin to free 148 * @gpio_range: the range matching the GPIO pin if this is a request for a 149 * single GPIO pin 150 * 151 * This function returns a pointer to the previous owner. This is used 152 * for callers that dynamically allocate an owner name so it can be freed 153 * once the pin is free. This is done for GPIO request functions. 154 */ 155static const char *pin_free(struct pinctrl_dev *pctldev, int pin, 156 struct pinctrl_gpio_range *gpio_range) 157{ 158 const struct pinmux_ops *ops = pctldev->desc->pmxops; 159 struct pin_desc *desc; 160 const char *owner; 161 162 desc = pin_desc_get(pctldev, pin); 163 if (desc == NULL) { 164 dev_err(pctldev->dev, 165 "pin is not registered so it cannot be freed\n"); 166 return NULL; 167 } 168 169 /* 170 * If there is no kind of request function for the pin we just assume 171 * we got it by default and proceed. 172 */ 173 if (gpio_range && ops->gpio_disable_free) 174 ops->gpio_disable_free(pctldev, gpio_range, pin); 175 else if (ops->free) 176 ops->free(pctldev, pin); 177 178 spin_lock(&desc->lock); 179 owner = desc->owner; 180 desc->owner = NULL; 181 spin_unlock(&desc->lock); 182 module_put(pctldev->owner); 183 184 return owner; 185} 186 187/** 188 * pinmux_request_gpio() - request pinmuxing for a GPIO pin 189 * @pctldev: pin controller device affected 190 * @pin: the pin to mux in for GPIO 191 * @range: the applicable GPIO range 192 */ 193int pinmux_request_gpio(struct pinctrl_dev *pctldev, 194 struct pinctrl_gpio_range *range, 195 unsigned pin, unsigned gpio) 196{ 197 char gpiostr[16]; 198 const char *owner; 199 int ret; 200 201 /* Conjure some name stating what chip and pin this is taken by */ 202 snprintf(gpiostr, 15, "%s:%d", range->name, gpio); 203 204 owner = kstrdup(gpiostr, GFP_KERNEL); 205 if (!owner) 206 return -EINVAL; 207 208 ret = pin_request(pctldev, pin, owner, range); 209 if (ret < 0) 210 kfree(owner); 211 212 return ret; 213} 214 215/** 216 * pinmux_free_gpio() - release a pin from GPIO muxing 217 * @pctldev: the pin controller device for the pin 218 * @pin: the affected currently GPIO-muxed in pin 219 * @range: applicable GPIO range 220 */ 221void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin, 222 struct pinctrl_gpio_range *range) 223{ 224 const char *owner; 225 226 owner = pin_free(pctldev, pin, range); 227 kfree(owner); 228} 229 230/** 231 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin 232 * @pctldev: the pin controller handling this pin 233 * @range: applicable GPIO range 234 * @pin: the affected GPIO pin in this controller 235 * @input: true if we set the pin as input, false for output 236 */ 237int pinmux_gpio_direction(struct pinctrl_dev *pctldev, 238 struct pinctrl_gpio_range *range, 239 unsigned pin, bool input) 240{ 241 const struct pinmux_ops *ops; 242 int ret; 243 244 ops = pctldev->desc->pmxops; 245 246 if (ops->gpio_set_direction) 247 ret = ops->gpio_set_direction(pctldev, range, pin, input); 248 else 249 ret = 0; 250 251 return ret; 252} 253 254/** 255 * acquire_pins() - acquire all the pins for a certain function on a pinmux 256 * @pctldev: the device to take the pins on 257 * @owner: a representation of the owner of this pin; typically the device 258 * name that controls its mux function 259 * @group_selector: the group selector containing the pins to acquire 260 */ 261static int acquire_pins(struct pinctrl_dev *pctldev, 262 const char *owner, 263 unsigned group_selector) 264{ 265 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; 266 const unsigned *pins; 267 unsigned num_pins; 268 int ret; 269 int i; 270 271 ret = pctlops->get_group_pins(pctldev, group_selector, 272 &pins, &num_pins); 273 if (ret) 274 return ret; 275 276 dev_dbg(pctldev->dev, "requesting the %u pins from group %u\n", 277 num_pins, group_selector); 278 279 /* Try to allocate all pins in this group, one by one */ 280 for (i = 0; i < num_pins; i++) { 281 ret = pin_request(pctldev, pins[i], owner, NULL); 282 if (ret) { 283 dev_err(pctldev->dev, 284 "could not get request pin %d on device %s - conflicting mux mappings?\n", 285 pins[i], 286 pinctrl_dev_get_name(pctldev)); 287 /* On error release all taken pins */ 288 i--; /* this pin just failed */ 289 for (; i >= 0; i--) 290 pin_free(pctldev, pins[i], NULL); 291 return -ENODEV; 292 } 293 } 294 return 0; 295} 296 297/** 298 * release_pins() - release pins taken by earlier acquirement 299 * @pctldev: the device to free the pins on 300 * @group_selector: the group selector containing the pins to free 301 */ 302static void release_pins(struct pinctrl_dev *pctldev, 303 unsigned group_selector) 304{ 305 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; 306 const unsigned *pins; 307 unsigned num_pins; 308 int ret; 309 int i; 310 311 ret = pctlops->get_group_pins(pctldev, group_selector, 312 &pins, &num_pins); 313 if (ret) { 314 dev_err(pctldev->dev, "could not get pins to release for group selector %d\n", 315 group_selector); 316 return; 317 } 318 for (i = 0; i < num_pins; i++) 319 pin_free(pctldev, pins[i], NULL); 320} 321 322/** 323 * pinmux_check_pin_group() - check function and pin group combo 324 * @pctldev: device to check the pin group vs function for 325 * @func_selector: the function selector to check the pin group for, we have 326 * already looked this up in the calling function 327 * @pin_group: the pin group to match to the function 328 * 329 * This function will check that the pinmux driver can supply the 330 * selected pin group for a certain function, returns the group selector if 331 * the group and function selector will work fine together, else returns 332 * negative 333 */ 334static int pinmux_check_pin_group(struct pinctrl_dev *pctldev, 335 unsigned func_selector, 336 const char *pin_group) 337{ 338 const struct pinmux_ops *pmxops = pctldev->desc->pmxops; 339 const struct pinctrl_ops *pctlops = pctldev->desc->pctlops; 340 int ret; 341 342 /* 343 * If the driver does not support different pin groups for the 344 * functions, we only support group 0, and assume this exists. 345 */ 346 if (!pctlops || !pctlops->list_groups) 347 return 0; 348 349 /* 350 * Passing NULL (no specific group) will select the first and 351 * hopefully only group of pins available for this function. 352 */ 353 if (!pin_group) { 354 char const * const *groups; 355 unsigned num_groups; 356 357 ret = pmxops->get_function_groups(pctldev, func_selector, 358 &groups, &num_groups); 359 if (ret) 360 return ret; 361 if (num_groups < 1) 362 return -EINVAL; 363 ret = pinctrl_get_group_selector(pctldev, groups[0]); 364 if (ret < 0) { 365 dev_err(pctldev->dev, 366 "function %s wants group %s but the pin controller does not seem to have that group\n", 367 pmxops->get_function_name(pctldev, func_selector), 368 groups[0]); 369 return ret; 370 } 371 372 if (num_groups > 1) 373 dev_dbg(pctldev->dev, 374 "function %s support more than one group, default-selecting first group %s (%d)\n", 375 pmxops->get_function_name(pctldev, func_selector), 376 groups[0], 377 ret); 378 379 return ret; 380 } 381 382 dev_dbg(pctldev->dev, 383 "check if we have pin group %s on controller %s\n", 384 pin_group, pinctrl_dev_get_name(pctldev)); 385 386 ret = pinctrl_get_group_selector(pctldev, pin_group); 387 if (ret < 0) { 388 dev_dbg(pctldev->dev, 389 "%s does not support pin group %s with function %s\n", 390 pinctrl_dev_get_name(pctldev), 391 pin_group, 392 pmxops->get_function_name(pctldev, func_selector)); 393 } 394 return ret; 395} 396 397/** 398 * pinmux_search_function() - check pin control driver for a certain function 399 * @pctldev: device to check for function and position 400 * @map: function map containing the function and position to look for 401 * @func_selector: returns the applicable function selector if found 402 * @group_selector: returns the applicable group selector if found 403 * 404 * This will search the pinmux driver for an applicable 405 * function with a specific pin group, returns 0 if these can be mapped 406 * negative otherwise 407 */ 408static int pinmux_search_function(struct pinctrl_dev *pctldev, 409 struct pinctrl_map const *map, 410 unsigned *func_selector, 411 unsigned *group_selector) 412{ 413 const struct pinmux_ops *ops = pctldev->desc->pmxops; 414 unsigned selector = 0; 415 416 /* See if this pctldev has this function */ 417 while (ops->list_functions(pctldev, selector) >= 0) { 418 const char *fname = ops->get_function_name(pctldev, 419 selector); 420 int ret; 421 422 if (!strcmp(map->function, fname)) { 423 /* Found the function, check pin group */ 424 ret = pinmux_check_pin_group(pctldev, selector, 425 map->group); 426 if (ret < 0) 427 return ret; 428 429 /* This function and group selector can be used */ 430 *func_selector = selector; 431 *group_selector = ret; 432 return 0; 433 434 } 435 selector++; 436 } 437 438 pr_err("%s does not support function %s\n", 439 pinctrl_dev_get_name(pctldev), map->function); 440 return -EINVAL; 441} 442 443/** 444 * pinmux_enable_muxmap() - enable a map entry for a certain pinmux 445 */ 446static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev, 447 struct pinctrl *p, 448 struct device *dev, 449 const char *devname, 450 struct pinctrl_map const *map) 451{ 452 unsigned func_selector; 453 unsigned group_selector; 454 struct pinmux_group *grp; 455 int ret; 456 457 /* 458 * Note that we're not locking the pinmux mutex here, because 459 * this is only called at pinmux initialization time when it 460 * has not been added to any list and thus is not reachable 461 * by anyone else. 462 */ 463 464 if (p->pctldev && p->pctldev != pctldev) { 465 dev_err(pctldev->dev, 466 "different pin control devices given for device %s, function %s\n", 467 devname, map->function); 468 return -EINVAL; 469 } 470 p->dev = dev; 471 p->pctldev = pctldev; 472 473 /* Now go into the driver and try to match a function and group */ 474 ret = pinmux_search_function(pctldev, map, &func_selector, 475 &group_selector); 476 if (ret < 0) 477 return ret; 478 479 /* 480 * If the function selector is already set, it needs to be identical, 481 * we support several groups with one function but not several 482 * functions with one or several groups in the same pinmux. 483 */ 484 if (p->func_selector != UINT_MAX && 485 p->func_selector != func_selector) { 486 dev_err(pctldev->dev, 487 "dual function defines in the map for device %s\n", 488 devname); 489 return -EINVAL; 490 } 491 p->func_selector = func_selector; 492 493 /* Now add this group selector, we may have many of them */ 494 grp = kmalloc(sizeof(struct pinmux_group), GFP_KERNEL); 495 if (!grp) 496 return -ENOMEM; 497 grp->group_selector = group_selector; 498 ret = acquire_pins(pctldev, devname, group_selector); 499 if (ret) { 500 kfree(grp); 501 return ret; 502 } 503 list_add_tail(&grp->node, &p->groups); 504 505 return 0; 506} 507 508/** 509 * pinmux_apply_muxmap() - apply a certain mux mapping entry 510 */ 511int pinmux_apply_muxmap(struct pinctrl_dev *pctldev, 512 struct pinctrl *p, 513 struct device *dev, 514 const char *devname, 515 struct pinctrl_map const *map) 516{ 517 int ret; 518 519 ret = pinmux_enable_muxmap(pctldev, p, dev, 520 devname, map); 521 if (ret) { 522 pinmux_put(p); 523 return ret; 524 } 525 526 return 0; 527} 528 529/** 530 * pinmux_put() - free up the pinmux portions of a pin controller handle 531 */ 532void pinmux_put(struct pinctrl *p) 533{ 534 struct list_head *node, *tmp; 535 536 list_for_each_safe(node, tmp, &p->groups) { 537 struct pinmux_group *grp = 538 list_entry(node, struct pinmux_group, node); 539 /* Release all pins taken by this group */ 540 release_pins(p->pctldev, grp->group_selector); 541 list_del(node); 542 kfree(grp); 543 } 544} 545 546/** 547 * pinmux_enable() - enable the pinmux portion of a pin control handle 548 */ 549int pinmux_enable(struct pinctrl *p) 550{ 551 struct pinctrl_dev *pctldev = p->pctldev; 552 const struct pinmux_ops *ops = pctldev->desc->pmxops; 553 struct pinmux_group *grp; 554 int ret; 555 556 list_for_each_entry(grp, &p->groups, node) { 557 ret = ops->enable(pctldev, p->func_selector, 558 grp->group_selector); 559 if (ret) 560 /* 561 * TODO: call disable() on all groups we called 562 * enable() on to this point? 563 */ 564 return ret; 565 } 566 return 0; 567} 568 569/** 570 * pinmux_disable() - disable the pinmux portions of a pin control handle 571 */ 572void pinmux_disable(struct pinctrl *p) 573{ 574 struct pinctrl_dev *pctldev = p->pctldev; 575 const struct pinmux_ops *ops = pctldev->desc->pmxops; 576 struct pinmux_group *grp; 577 578 list_for_each_entry(grp, &p->groups, node) { 579 ops->disable(pctldev, p->func_selector, 580 grp->group_selector); 581 } 582} 583 584#ifdef CONFIG_DEBUG_FS 585 586/* Called from pincontrol core */ 587static int pinmux_functions_show(struct seq_file *s, void *what) 588{ 589 struct pinctrl_dev *pctldev = s->private; 590 const struct pinmux_ops *pmxops = pctldev->desc->pmxops; 591 unsigned func_selector = 0; 592 593 while (pmxops->list_functions(pctldev, func_selector) >= 0) { 594 const char *func = pmxops->get_function_name(pctldev, 595 func_selector); 596 const char * const *groups; 597 unsigned num_groups; 598 int ret; 599 int i; 600 601 ret = pmxops->get_function_groups(pctldev, func_selector, 602 &groups, &num_groups); 603 if (ret) 604 seq_printf(s, "function %s: COULD NOT GET GROUPS\n", 605 func); 606 607 seq_printf(s, "function: %s, groups = [ ", func); 608 for (i = 0; i < num_groups; i++) 609 seq_printf(s, "%s ", groups[i]); 610 seq_puts(s, "]\n"); 611 612 func_selector++; 613 614 } 615 616 return 0; 617} 618 619static int pinmux_pins_show(struct seq_file *s, void *what) 620{ 621 struct pinctrl_dev *pctldev = s->private; 622 unsigned i, pin; 623 624 seq_puts(s, "Pinmux settings per pin\n"); 625 seq_puts(s, "Format: pin (name): owner\n"); 626 627 /* The pin number can be retrived from the pin controller descriptor */ 628 for (i = 0; i < pctldev->desc->npins; i++) { 629 630 struct pin_desc *desc; 631 632 pin = pctldev->desc->pins[i].number; 633 desc = pin_desc_get(pctldev, pin); 634 /* Skip if we cannot search the pin */ 635 if (desc == NULL) 636 continue; 637 638 seq_printf(s, "pin %d (%s): %s\n", pin, 639 desc->name ? desc->name : "unnamed", 640 desc->owner ? desc->owner : "UNCLAIMED"); 641 } 642 643 return 0; 644} 645 646void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p) 647{ 648 struct pinctrl_dev *pctldev = p->pctldev; 649 const struct pinmux_ops *pmxops; 650 const struct pinctrl_ops *pctlops; 651 struct pinmux_group *grp; 652 653 pmxops = pctldev->desc->pmxops; 654 pctlops = pctldev->desc->pctlops; 655 656 seq_printf(s, " function: %s (%u),", 657 pmxops->get_function_name(pctldev, 658 p->func_selector), 659 p->func_selector); 660 661 seq_printf(s, " groups: ["); 662 list_for_each_entry(grp, &p->groups, node) { 663 seq_printf(s, " %s (%u)", 664 pctlops->get_group_name(pctldev, 665 grp->group_selector), 666 grp->group_selector); 667 } 668 seq_printf(s, " ]"); 669} 670 671static int pinmux_functions_open(struct inode *inode, struct file *file) 672{ 673 return single_open(file, pinmux_functions_show, inode->i_private); 674} 675 676static int pinmux_pins_open(struct inode *inode, struct file *file) 677{ 678 return single_open(file, pinmux_pins_show, inode->i_private); 679} 680 681static const struct file_operations pinmux_functions_ops = { 682 .open = pinmux_functions_open, 683 .read = seq_read, 684 .llseek = seq_lseek, 685 .release = single_release, 686}; 687 688static const struct file_operations pinmux_pins_ops = { 689 .open = pinmux_pins_open, 690 .read = seq_read, 691 .llseek = seq_lseek, 692 .release = single_release, 693}; 694 695void pinmux_init_device_debugfs(struct dentry *devroot, 696 struct pinctrl_dev *pctldev) 697{ 698 debugfs_create_file("pinmux-functions", S_IFREG | S_IRUGO, 699 devroot, pctldev, &pinmux_functions_ops); 700 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, 701 devroot, pctldev, &pinmux_pins_ops); 702} 703 704#endif /* CONFIG_DEBUG_FS */ 705