aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2012-02-09 13:47:48 -0500
committerLinus Walleij <linus.walleij@linaro.org>2012-02-10 15:33:06 -0500
commitbefe5bdfbb698b3bc57c58d0bd7ca3391c9275ed (patch)
tree71416f2adc515f010f54de2371e29626cd144a5b
parente93bcee00c43e2bd4037291262111016f4c05793 (diff)
pinctrl: factor pin control handles over to the core
This moves the per-devices struct pinctrl handles and device map over from the pinmux part of the subsystem to the core pinctrl part. This makes the device handles core infrastructure with the goal of using these handles also for pin configuration, so that device drivers (or boards etc) will need one and only one handle to the pin control core. Acked-by: Stephen Warren <swarren@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r--drivers/pinctrl/core.c598
-rw-r--r--drivers/pinctrl/core.h30
-rw-r--r--drivers/pinctrl/pinmux.c711
-rw-r--r--drivers/pinctrl/pinmux.h67
-rw-r--r--include/linux/pinctrl/consumer.h8
5 files changed, 779 insertions, 635 deletions
diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
index 75c6a6bb6c0a..ec32c545f07f 100644
--- a/drivers/pinctrl/core.c
+++ b/drivers/pinctrl/core.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Core driver for the pin control subsystem 2 * Core driver for the pin control subsystem
3 * 3 *
4 * Copyright (C) 2011 ST-Ericsson SA 4 * Copyright (C) 2011-2012 ST-Ericsson SA
5 * Written on behalf of Linaro for ST-Ericsson 5 * Written on behalf of Linaro for ST-Ericsson
6 * Based on bits of regulator core, gpio core and clk core 6 * Based on bits of regulator core, gpio core and clk core
7 * 7 *
@@ -30,10 +30,30 @@
30#include "pinmux.h" 30#include "pinmux.h"
31#include "pinconf.h" 31#include "pinconf.h"
32 32
33/**
34 * struct pinctrl_hog - a list item to stash control hogs
35 * @node: pin control hog list node
36 * @map: map entry responsible for this hogging
37 * @pmx: the pin control hogged by this item
38 */
39struct pinctrl_hog {
40 struct list_head node;
41 struct pinctrl_map const *map;
42 struct pinctrl *p;
43};
44
33/* Global list of pin control devices */ 45/* Global list of pin control devices */
34static DEFINE_MUTEX(pinctrldev_list_mutex); 46static DEFINE_MUTEX(pinctrldev_list_mutex);
35static LIST_HEAD(pinctrldev_list); 47static LIST_HEAD(pinctrldev_list);
36 48
49/* List of pin controller handles */
50static DEFINE_MUTEX(pinctrl_list_mutex);
51static LIST_HEAD(pinctrl_list);
52
53/* Global pinctrl maps */
54static struct pinctrl_map *pinctrl_maps;
55static unsigned pinctrl_maps_num;
56
37const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev) 57const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev)
38{ 58{
39 /* We're not allowed to register devices without name */ 59 /* We're not allowed to register devices without name */
@@ -337,6 +357,476 @@ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
337 return -EINVAL; 357 return -EINVAL;
338} 358}
339 359
360/**
361 * pinctrl_request_gpio() - request a single pin to be used in as GPIO
362 * @gpio: the GPIO pin number from the GPIO subsystem number space
363 *
364 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
365 * as part of their gpio_request() semantics, platforms and individual drivers
366 * shall *NOT* request GPIO pins to be muxed in.
367 */
368int pinctrl_request_gpio(unsigned gpio)
369{
370 struct pinctrl_dev *pctldev;
371 struct pinctrl_gpio_range *range;
372 int ret;
373 int pin;
374
375 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
376 if (ret)
377 return -EINVAL;
378
379 /* Convert to the pin controllers number space */
380 pin = gpio - range->base + range->pin_base;
381
382 return pinmux_request_gpio(pctldev, range, pin, gpio);
383}
384EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
385
386/**
387 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO
388 * @gpio: the GPIO pin number from the GPIO subsystem number space
389 *
390 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
391 * as part of their gpio_free() semantics, platforms and individual drivers
392 * shall *NOT* request GPIO pins to be muxed out.
393 */
394void pinctrl_free_gpio(unsigned gpio)
395{
396 struct pinctrl_dev *pctldev;
397 struct pinctrl_gpio_range *range;
398 int ret;
399 int pin;
400
401 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
402 if (ret)
403 return;
404
405 /* Convert to the pin controllers number space */
406 pin = gpio - range->base + range->pin_base;
407
408 return pinmux_free_gpio(pctldev, pin, range);
409}
410EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
411
412static int pinctrl_gpio_direction(unsigned gpio, bool input)
413{
414 struct pinctrl_dev *pctldev;
415 struct pinctrl_gpio_range *range;
416 int ret;
417 int pin;
418
419 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
420 if (ret)
421 return ret;
422
423 /* Convert to the pin controllers number space */
424 pin = gpio - range->base + range->pin_base;
425
426 return pinmux_gpio_direction(pctldev, range, pin, input);
427}
428
429/**
430 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
431 * @gpio: the GPIO pin number from the GPIO subsystem number space
432 *
433 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
434 * as part of their gpio_direction_input() semantics, platforms and individual
435 * drivers shall *NOT* touch pin control GPIO calls.
436 */
437int pinctrl_gpio_direction_input(unsigned gpio)
438{
439 return pinctrl_gpio_direction(gpio, true);
440}
441EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
442
443/**
444 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
445 * @gpio: the GPIO pin number from the GPIO subsystem number space
446 *
447 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
448 * as part of their gpio_direction_output() semantics, platforms and individual
449 * drivers shall *NOT* touch pin control GPIO calls.
450 */
451int pinctrl_gpio_direction_output(unsigned gpio)
452{
453 return pinctrl_gpio_direction(gpio, false);
454}
455EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
456
457/**
458 * pinctrl_get() - retrieves the pin controller handle for a certain device
459 * @dev: the device to get the pin controller handle for
460 * @name: an optional specific control mapping name or NULL, the name is only
461 * needed if you want to have more than one mapping per device, or if you
462 * need an anonymous pin control (not tied to any specific device)
463 */
464struct pinctrl *pinctrl_get(struct device *dev, const char *name)
465{
466 struct pinctrl_map const *map = NULL;
467 struct pinctrl_dev *pctldev = NULL;
468 const char *devname = NULL;
469 struct pinctrl *p;
470 bool found_map;
471 unsigned num_maps = 0;
472 int ret = -ENODEV;
473 int i;
474
475 /* We must have dev or ID or both */
476 if (!dev && !name)
477 return ERR_PTR(-EINVAL);
478
479 if (dev)
480 devname = dev_name(dev);
481
482 pr_debug("get pin control handle %s for device %s\n", name,
483 devname ? devname : "(none)");
484
485 /*
486 * create the state cookie holder struct pinctrl for each
487 * mapping, this is what consumers will get when requesting
488 * a pin control handle with pinctrl_get()
489 */
490 p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
491 if (p == NULL)
492 return ERR_PTR(-ENOMEM);
493 mutex_init(&p->mutex);
494 pinmux_init_pinctrl_handle(p);
495
496 /* Iterate over the pin control maps to locate the right ones */
497 for (i = 0; i < pinctrl_maps_num; i++) {
498 map = &pinctrl_maps[i];
499 found_map = false;
500
501 /*
502 * First, try to find the pctldev given in the map
503 */
504 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
505 if (!pctldev) {
506 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
507 map->function);
508 pr_warning("given pinctrl device name: %s",
509 map->ctrl_dev_name);
510
511 /* Continue to check the other mappings anyway... */
512 continue;
513 }
514
515 pr_debug("in map, found pctldev %s to handle function %s",
516 dev_name(pctldev->dev), map->function);
517
518
519 /*
520 * If we're looking for a specific named map, this must match,
521 * else we loop and look for the next.
522 */
523 if (name != NULL) {
524 if (map->name == NULL)
525 continue;
526 if (strcmp(map->name, name))
527 continue;
528 }
529
530 /*
531 * This is for the case where no device name is given, we
532 * already know that the function name matches from above
533 * code.
534 */
535 if (!map->dev_name && (name != NULL))
536 found_map = true;
537
538 /* If the mapping has a device set up it must match */
539 if (map->dev_name &&
540 (!devname || !strcmp(map->dev_name, devname)))
541 /* MATCH! */
542 found_map = true;
543
544 /* If this map is applicable, then apply it */
545 if (found_map) {
546 ret = pinmux_apply_muxmap(pctldev, p, dev,
547 devname, map);
548 if (ret) {
549 kfree(p);
550 return ERR_PTR(ret);
551 }
552 num_maps++;
553 }
554 }
555
556 /* We should have atleast one map, right */
557 if (!num_maps) {
558 pr_err("could not find any mux maps for device %s, ID %s\n",
559 devname ? devname : "(anonymous)",
560 name ? name : "(undefined)");
561 kfree(p);
562 return ERR_PTR(-EINVAL);
563 }
564
565 pr_debug("found %u mux maps for device %s, UD %s\n",
566 num_maps,
567 devname ? devname : "(anonymous)",
568 name ? name : "(undefined)");
569
570 /* Add the pinmux to the global list */
571 mutex_lock(&pinctrl_list_mutex);
572 list_add(&p->node, &pinctrl_list);
573 mutex_unlock(&pinctrl_list_mutex);
574
575 return p;
576}
577EXPORT_SYMBOL_GPL(pinctrl_get);
578
579/**
580 * pinctrl_put() - release a previously claimed pin control handle
581 * @p: a pin control handle previously claimed by pinctrl_get()
582 */
583void pinctrl_put(struct pinctrl *p)
584{
585 if (p == NULL)
586 return;
587
588 mutex_lock(&p->mutex);
589 if (p->usecount)
590 pr_warn("releasing pin control handle with active users!\n");
591 /* Free the groups and all acquired pins */
592 pinmux_put(p);
593 mutex_unlock(&p->mutex);
594
595 /* Remove from list */
596 mutex_lock(&pinctrl_list_mutex);
597 list_del(&p->node);
598 mutex_unlock(&pinctrl_list_mutex);
599
600 kfree(p);
601}
602EXPORT_SYMBOL_GPL(pinctrl_put);
603
604/**
605 * pinctrl_enable() - enable a certain pin controller setting
606 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
607 */
608int pinctrl_enable(struct pinctrl *p)
609{
610 int ret = 0;
611
612 if (p == NULL)
613 return -EINVAL;
614 mutex_lock(&p->mutex);
615 if (p->usecount++ == 0) {
616 ret = pinmux_enable(p);
617 if (ret)
618 p->usecount--;
619 }
620 mutex_unlock(&p->mutex);
621 return ret;
622}
623EXPORT_SYMBOL_GPL(pinctrl_enable);
624
625/**
626 * pinctrl_disable() - disable a certain pin control setting
627 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
628 */
629void pinctrl_disable(struct pinctrl *p)
630{
631 if (p == NULL)
632 return;
633
634 mutex_lock(&p->mutex);
635 if (--p->usecount == 0) {
636 pinmux_disable(p);
637 }
638 mutex_unlock(&p->mutex);
639}
640EXPORT_SYMBOL_GPL(pinctrl_disable);
641
642/**
643 * pinctrl_register_mappings() - register a set of pin controller mappings
644 * @maps: the pincontrol mappings table to register, this should be marked with
645 * __initdata so it can be discarded after boot, this function will
646 * perform a shallow copy for the mapping entries.
647 * @num_maps: the number of maps in the mapping table
648 *
649 * Only call this once during initialization of your machine, the function is
650 * tagged as __init and won't be callable after init has completed. The map
651 * passed into this function will be owned by the pinmux core and cannot be
652 * freed.
653 */
654int __init pinctrl_register_mappings(struct pinctrl_map const *maps,
655 unsigned num_maps)
656{
657 void *tmp_maps;
658 int i;
659
660 pr_debug("add %d pinmux maps\n", num_maps);
661
662 /* First sanity check the new mapping */
663 for (i = 0; i < num_maps; i++) {
664 if (!maps[i].name) {
665 pr_err("failed to register map %d: no map name given\n",
666 i);
667 return -EINVAL;
668 }
669
670 if (!maps[i].ctrl_dev_name) {
671 pr_err("failed to register map %s (%d): no pin control device given\n",
672 maps[i].name, i);
673 return -EINVAL;
674 }
675
676 if (!maps[i].function) {
677 pr_err("failed to register map %s (%d): no function ID given\n",
678 maps[i].name, i);
679 return -EINVAL;
680 }
681
682 if (!maps[i].dev_name)
683 pr_debug("add system map %s function %s with no device\n",
684 maps[i].name,
685 maps[i].function);
686 else
687 pr_debug("register map %s, function %s\n",
688 maps[i].name,
689 maps[i].function);
690 }
691
692 /*
693 * Make a copy of the map array - string pointers will end up in the
694 * kernel const section anyway so these do not need to be deep copied.
695 */
696 if (!pinctrl_maps_num) {
697 /* On first call, just copy them */
698 tmp_maps = kmemdup(maps,
699 sizeof(struct pinctrl_map) * num_maps,
700 GFP_KERNEL);
701 if (!tmp_maps)
702 return -ENOMEM;
703 } else {
704 /* Subsequent calls, reallocate array to new size */
705 size_t oldsize = sizeof(struct pinctrl_map) * pinctrl_maps_num;
706 size_t newsize = sizeof(struct pinctrl_map) * num_maps;
707
708 tmp_maps = krealloc(pinctrl_maps,
709 oldsize + newsize, GFP_KERNEL);
710 if (!tmp_maps)
711 return -ENOMEM;
712 memcpy((tmp_maps + oldsize), maps, newsize);
713 }
714
715 pinctrl_maps = tmp_maps;
716 pinctrl_maps_num += num_maps;
717 return 0;
718}
719
720/* Hog a single map entry and add to the hoglist */
721static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
722 struct pinctrl_map const *map)
723{
724 struct pinctrl_hog *hog;
725 struct pinctrl *p;
726 int ret;
727
728 if (map->dev_name) {
729 /*
730 * TODO: the day we have device tree support, we can
731 * traverse the device tree and hog to specific device nodes
732 * without any problems, so then we can hog pinmuxes for
733 * all devices that just want a static pin mux at this point.
734 */
735 dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n",
736 map->name);
737 return -EINVAL;
738 }
739
740 hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
741 if (!hog)
742 return -ENOMEM;
743
744 p = pinctrl_get(NULL, map->name);
745 if (IS_ERR(p)) {
746 kfree(hog);
747 dev_err(pctldev->dev,
748 "could not get the %s pin control mapping for hogging\n",
749 map->name);
750 return PTR_ERR(p);
751 }
752
753 ret = pinctrl_enable(p);
754 if (ret) {
755 pinctrl_put(p);
756 kfree(hog);
757 dev_err(pctldev->dev,
758 "could not enable the %s pin control mapping for hogging\n",
759 map->name);
760 return ret;
761 }
762
763 hog->map = map;
764 hog->p = p;
765
766 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
767 map->function);
768 mutex_lock(&pctldev->pinctrl_hogs_lock);
769 list_add(&hog->node, &pctldev->pinctrl_hogs);
770 mutex_unlock(&pctldev->pinctrl_hogs_lock);
771
772 return 0;
773}
774
775/**
776 * pinctrl_hog_maps() - hog specific map entries on controller device
777 * @pctldev: the pin control device to hog entries on
778 *
779 * When the pin controllers are registered, there may be some specific pinmux
780 * map entries that need to be hogged, i.e. get+enabled until the system shuts
781 * down.
782 */
783int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
784{
785 struct device *dev = pctldev->dev;
786 const char *devname = dev_name(dev);
787 int ret;
788 int i;
789
790 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
791 mutex_init(&pctldev->pinctrl_hogs_lock);
792
793 for (i = 0; i < pinctrl_maps_num; i++) {
794 struct pinctrl_map const *map = &pinctrl_maps[i];
795
796 if (!map->hog_on_boot)
797 continue;
798
799 if (map->ctrl_dev_name &&
800 !strcmp(map->ctrl_dev_name, devname)) {
801 /* OK time to hog! */
802 ret = pinctrl_hog_map(pctldev, map);
803 if (ret)
804 return ret;
805 }
806 }
807 return 0;
808}
809
810/**
811 * pinctrl_unhog_maps() - unhog specific map entries on controller device
812 * @pctldev: the pin control device to unhog entries on
813 */
814void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
815{
816 struct list_head *node, *tmp;
817
818 mutex_lock(&pctldev->pinctrl_hogs_lock);
819 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
820 struct pinctrl_hog *hog =
821 list_entry(node, struct pinctrl_hog, node);
822 pinctrl_disable(hog->p);
823 pinctrl_put(hog->p);
824 list_del(node);
825 kfree(hog);
826 }
827 mutex_unlock(&pctldev->pinctrl_hogs_lock);
828}
829
340#ifdef CONFIG_DEBUG_FS 830#ifdef CONFIG_DEBUG_FS
341 831
342static int pinctrl_pins_show(struct seq_file *s, void *what) 832static int pinctrl_pins_show(struct seq_file *s, void *what)
@@ -427,6 +917,43 @@ static int pinctrl_gpioranges_show(struct seq_file *s, void *what)
427 return 0; 917 return 0;
428} 918}
429 919
920static int pinctrl_maps_show(struct seq_file *s, void *what)
921{
922 int i;
923
924 seq_puts(s, "Pinctrl maps:\n");
925
926 for (i = 0; i < pinctrl_maps_num; i++) {
927 struct pinctrl_map const *map = &pinctrl_maps[i];
928
929 seq_printf(s, "%s:\n", map->name);
930 if (map->dev_name)
931 seq_printf(s, " device: %s\n",
932 map->dev_name);
933 else
934 seq_printf(s, " SYSTEM MUX\n");
935 seq_printf(s, " controlling device %s\n",
936 map->ctrl_dev_name);
937 seq_printf(s, " function: %s\n", map->function);
938 seq_printf(s, " group: %s\n", map->group ? map->group :
939 "(default)");
940 }
941 return 0;
942}
943
944static int pinmux_hogs_show(struct seq_file *s, void *what)
945{
946 struct pinctrl_dev *pctldev = s->private;
947 struct pinctrl_hog *hog;
948
949 seq_puts(s, "Pin control map hogs held by device\n");
950
951 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
952 seq_printf(s, "%s\n", hog->map->name);
953
954 return 0;
955}
956
430static int pinctrl_devices_show(struct seq_file *s, void *what) 957static int pinctrl_devices_show(struct seq_file *s, void *what)
431{ 958{
432 struct pinctrl_dev *pctldev; 959 struct pinctrl_dev *pctldev;
@@ -450,6 +977,32 @@ static int pinctrl_devices_show(struct seq_file *s, void *what)
450 return 0; 977 return 0;
451} 978}
452 979
980static int pinctrl_show(struct seq_file *s, void *what)
981{
982 struct pinctrl *p;
983
984 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
985 list_for_each_entry(p, &pinctrl_list, node) {
986 struct pinctrl_dev *pctldev = p->pctldev;
987
988 if (!pctldev) {
989 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
990 continue;
991 }
992
993 seq_printf(s, "device: %s",
994 pinctrl_dev_get_name(p->pctldev));
995
996 pinmux_dbg_show(s, p);
997
998 seq_printf(s, " users: %u map-> %s\n",
999 p->usecount,
1000 p->dev ? dev_name(p->dev) : "(system)");
1001 }
1002
1003 return 0;
1004}
1005
453static int pinctrl_pins_open(struct inode *inode, struct file *file) 1006static int pinctrl_pins_open(struct inode *inode, struct file *file)
454{ 1007{
455 return single_open(file, pinctrl_pins_show, inode->i_private); 1008 return single_open(file, pinctrl_pins_show, inode->i_private);
@@ -465,11 +1018,26 @@ static int pinctrl_gpioranges_open(struct inode *inode, struct file *file)
465 return single_open(file, pinctrl_gpioranges_show, inode->i_private); 1018 return single_open(file, pinctrl_gpioranges_show, inode->i_private);
466} 1019}
467 1020
1021static int pinctrl_maps_open(struct inode *inode, struct file *file)
1022{
1023 return single_open(file, pinctrl_maps_show, inode->i_private);
1024}
1025
1026static int pinmux_hogs_open(struct inode *inode, struct file *file)
1027{
1028 return single_open(file, pinmux_hogs_show, inode->i_private);
1029}
1030
468static int pinctrl_devices_open(struct inode *inode, struct file *file) 1031static int pinctrl_devices_open(struct inode *inode, struct file *file)
469{ 1032{
470 return single_open(file, pinctrl_devices_show, NULL); 1033 return single_open(file, pinctrl_devices_show, NULL);
471} 1034}
472 1035
1036static int pinctrl_open(struct inode *inode, struct file *file)
1037{
1038 return single_open(file, pinctrl_show, NULL);
1039}
1040
473static const struct file_operations pinctrl_pins_ops = { 1041static const struct file_operations pinctrl_pins_ops = {
474 .open = pinctrl_pins_open, 1042 .open = pinctrl_pins_open,
475 .read = seq_read, 1043 .read = seq_read,
@@ -491,6 +1059,20 @@ static const struct file_operations pinctrl_gpioranges_ops = {
491 .release = single_release, 1059 .release = single_release,
492}; 1060};
493 1061
1062static const struct file_operations pinctrl_maps_ops = {
1063 .open = pinctrl_maps_open,
1064 .read = seq_read,
1065 .llseek = seq_lseek,
1066 .release = single_release,
1067};
1068
1069static const struct file_operations pinmux_hogs_ops = {
1070 .open = pinmux_hogs_open,
1071 .read = seq_read,
1072 .llseek = seq_lseek,
1073 .release = single_release,
1074};
1075
494static const struct file_operations pinctrl_devices_ops = { 1076static const struct file_operations pinctrl_devices_ops = {
495 .open = pinctrl_devices_open, 1077 .open = pinctrl_devices_open,
496 .read = seq_read, 1078 .read = seq_read,
@@ -498,6 +1080,13 @@ static const struct file_operations pinctrl_devices_ops = {
498 .release = single_release, 1080 .release = single_release,
499}; 1081};
500 1082
1083static const struct file_operations pinctrl_ops = {
1084 .open = pinctrl_open,
1085 .read = seq_read,
1086 .llseek = seq_lseek,
1087 .release = single_release,
1088};
1089
501static struct dentry *debugfs_root; 1090static struct dentry *debugfs_root;
502 1091
503static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev) 1092static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
@@ -519,6 +1108,10 @@ static void pinctrl_init_device_debugfs(struct pinctrl_dev *pctldev)
519 device_root, pctldev, &pinctrl_groups_ops); 1108 device_root, pctldev, &pinctrl_groups_ops);
520 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO, 1109 debugfs_create_file("gpio-ranges", S_IFREG | S_IRUGO,
521 device_root, pctldev, &pinctrl_gpioranges_ops); 1110 device_root, pctldev, &pinctrl_gpioranges_ops);
1111 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1112 device_root, pctldev, &pinctrl_maps_ops);
1113 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1114 device_root, pctldev, &pinmux_hogs_ops);
522 pinmux_init_device_debugfs(device_root, pctldev); 1115 pinmux_init_device_debugfs(device_root, pctldev);
523 pinconf_init_device_debugfs(device_root, pctldev); 1116 pinconf_init_device_debugfs(device_root, pctldev);
524} 1117}
@@ -539,7 +1132,8 @@ static void pinctrl_init_debugfs(void)
539 1132
540 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO, 1133 debugfs_create_file("pinctrl-devices", S_IFREG | S_IRUGO,
541 debugfs_root, NULL, &pinctrl_devices_ops); 1134 debugfs_root, NULL, &pinctrl_devices_ops);
542 pinmux_init_debugfs(debugfs_root); 1135 debugfs_create_file("pinctrl-handles", S_IFREG | S_IRUGO,
1136 debugfs_root, NULL, &pinctrl_ops);
543} 1137}
544 1138
545#else /* CONFIG_DEBUG_FS */ 1139#else /* CONFIG_DEBUG_FS */
diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h
index 7a89888fce94..a50cdb053c84 100644
--- a/drivers/pinctrl/core.h
+++ b/drivers/pinctrl/core.h
@@ -30,6 +30,7 @@ struct pinctrl_gpio_range;
30 * subsystem 30 * subsystem
31 * @pinctrl_hogs_lock: lock for the pin control hog list 31 * @pinctrl_hogs_lock: lock for the pin control hog list
32 * @pinctrl_hogs: list of pin control maps hogged by this device 32 * @pinctrl_hogs: list of pin control maps hogged by this device
33 * @device_root: debugfs root for this device
33 */ 34 */
34struct pinctrl_dev { 35struct pinctrl_dev {
35 struct list_head node; 36 struct list_head node;
@@ -41,12 +42,37 @@ struct pinctrl_dev {
41 struct device *dev; 42 struct device *dev;
42 struct module *owner; 43 struct module *owner;
43 void *driver_data; 44 void *driver_data;
45 struct mutex pinctrl_hogs_lock;
46 struct list_head pinctrl_hogs;
44#ifdef CONFIG_DEBUG_FS 47#ifdef CONFIG_DEBUG_FS
45 struct dentry *device_root; 48 struct dentry *device_root;
46#endif 49#endif
50};
51
52/**
53 * struct pinctrl - per-device pin control state holder
54 * @node: global list node
55 * @dev: the device using this pin control handle
56 * @usecount: the number of active users of this pin controller setting, used
57 * to keep track of nested use cases
58 * @pctldev: pin control device handling this pin control handle
59 * @mutex: a lock for the pin control state holder
60 * @func_selector: the function selector for the pinmux device handling
61 * this pinmux
62 * @groups: the group selectors for the pinmux device and
63 * selector combination handling this pinmux, this is a list that
64 * will be traversed on all pinmux operations such as
65 * get/put/enable/disable
66 */
67struct pinctrl {
68 struct list_head node;
69 struct device *dev;
70 unsigned usecount;
71 struct pinctrl_dev *pctldev;
72 struct mutex mutex;
47#ifdef CONFIG_PINMUX 73#ifdef CONFIG_PINMUX
48 struct mutex pinctrl_hogs_lock; 74 unsigned func_selector;
49 struct list_head pinctrl_hogs; 75 struct list_head groups;
50#endif 76#endif
51}; 77};
52 78
diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c
index 773835d18f55..fe4a00751c60 100644
--- a/drivers/pinctrl/pinmux.c
+++ b/drivers/pinctrl/pinmux.c
@@ -28,14 +28,7 @@
28#include <linux/pinctrl/machine.h> 28#include <linux/pinctrl/machine.h>
29#include <linux/pinctrl/pinmux.h> 29#include <linux/pinctrl/pinmux.h>
30#include "core.h" 30#include "core.h"
31 31#include "pinmux.h"
32/* List of pin controller handles */
33static DEFINE_MUTEX(pinctrl_list_mutex);
34static LIST_HEAD(pinctrl_list);
35
36/* Global pinctrl maps */
37static struct pinctrl_map *pinctrl_maps;
38static unsigned pinctrl_maps_num;
39 32
40/** 33/**
41 * struct pinmux_group - group list item for pinmux groups 34 * struct pinmux_group - group list item for pinmux groups
@@ -48,43 +41,6 @@ struct pinmux_group {
48}; 41};
49 42
50/** 43/**
51 * struct pinctrl - per-device pin control state holder
52 * @node: global list node
53 * @dev: the device using this pin control handle
54 * @usecount: the number of active users of this pin controller setting, used
55 * to keep track of nested use cases
56 * @pctldev: pin control device handling this pin control handle
57 * @func_selector: the function selector for the pinmux device handling
58 * this pinmux
59 * @groups: the group selectors for the pinmux device and
60 * selector combination handling this pinmux, this is a list that
61 * will be traversed on all pinmux operations such as
62 * get/put/enable/disable
63 * @mutex: a lock for the pinmux state holder
64 */
65struct pinctrl {
66 struct list_head node;
67 struct device *dev;
68 unsigned usecount;
69 struct pinctrl_dev *pctldev;
70 unsigned func_selector;
71 struct list_head groups;
72 struct mutex mutex;
73};
74
75/**
76 * struct pinctrl_hog - a list item to stash control hogs
77 * @node: pin control hog list node
78 * @map: map entry responsible for this hogging
79 * @pmx: the pin control hogged by this item
80 */
81struct pinctrl_hog {
82 struct list_head node;
83 struct pinctrl_map const *map;
84 struct pinctrl *p;
85};
86
87/**
88 * pin_request() - request a single pin to be muxed in, typically for GPIO 44 * pin_request() - request a single pin to be muxed in, typically for GPIO
89 * @pin: the pin number in the global pin space 45 * @pin: the pin number in the global pin space
90 * @function: a functional name to give to this pin, passed to the driver 46 * @function: a functional name to give to this pin, passed to the driver
@@ -207,28 +163,18 @@ static const char *pin_free(struct pinctrl_dev *pctldev, int pin,
207} 163}
208 164
209/** 165/**
210 * pinctrl_request_gpio() - request a single pin to be used in as GPIO 166 * pinmux_request_gpio() - request pinmuxing for a GPIO pin
211 * @gpio: the GPIO pin number from the GPIO subsystem number space 167 * @pctldev: pin controller device affected
212 * 168 * @pin: the pin to mux in for GPIO
213 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 169 * @range: the applicable GPIO range
214 * as part of their gpio_request() semantics, platforms and individual drivers
215 * shall *NOT* request GPIO pins to be muxed in.
216 */ 170 */
217int pinctrl_request_gpio(unsigned gpio) 171int pinmux_request_gpio(struct pinctrl_dev *pctldev,
172 struct pinctrl_gpio_range *range,
173 unsigned pin, unsigned gpio)
218{ 174{
219 char gpiostr[16]; 175 char gpiostr[16];
220 const char *function; 176 const char *function;
221 struct pinctrl_dev *pctldev;
222 struct pinctrl_gpio_range *range;
223 int ret; 177 int ret;
224 int pin;
225
226 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
227 if (ret)
228 return -EINVAL;
229
230 /* Convert to the pin controllers number space */
231 pin = gpio - range->base + range->pin_base;
232 178
233 /* Conjure some name stating what chip and pin this is taken by */ 179 /* Conjure some name stating what chip and pin this is taken by */
234 snprintf(gpiostr, 15, "%s:%d", range->name, gpio); 180 snprintf(gpiostr, 15, "%s:%d", range->name, gpio);
@@ -243,53 +189,38 @@ int pinctrl_request_gpio(unsigned gpio)
243 189
244 return ret; 190 return ret;
245} 191}
246EXPORT_SYMBOL_GPL(pinctrl_request_gpio);
247 192
248/** 193/**
249 * pinctrl_free_gpio() - free control on a single pin, currently used as GPIO 194 * pinmux_free_gpio() - release a pin from GPIO muxing
250 * @gpio: the GPIO pin number from the GPIO subsystem number space 195 * @pctldev: the pin controller device for the pin
251 * 196 * @pin: the affected currently GPIO-muxed in pin
252 * This function should *ONLY* be used from gpiolib-based GPIO drivers, 197 * @range: applicable GPIO range
253 * as part of their gpio_free() semantics, platforms and individual drivers
254 * shall *NOT* request GPIO pins to be muxed out.
255 */ 198 */
256void pinctrl_free_gpio(unsigned gpio) 199void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
200 struct pinctrl_gpio_range *range)
257{ 201{
258 struct pinctrl_dev *pctldev;
259 struct pinctrl_gpio_range *range;
260 int ret;
261 int pin;
262 const char *func; 202 const char *func;
263 203
264 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
265 if (ret)
266 return;
267
268 /* Convert to the pin controllers number space */
269 pin = gpio - range->base + range->pin_base;
270
271 func = pin_free(pctldev, pin, range); 204 func = pin_free(pctldev, pin, range);
272 kfree(func); 205 kfree(func);
273} 206}
274EXPORT_SYMBOL_GPL(pinctrl_free_gpio);
275 207
276static int pinctrl_gpio_direction(unsigned gpio, bool input) 208/**
209 * pinmux_gpio_direction() - set the direction of a single muxed-in GPIO pin
210 * @pctldev: the pin controller handling this pin
211 * @range: applicable GPIO range
212 * @pin: the affected GPIO pin in this controller
213 * @input: true if we set the pin as input, false for output
214 */
215int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
216 struct pinctrl_gpio_range *range,
217 unsigned pin, bool input)
277{ 218{
278 struct pinctrl_dev *pctldev;
279 struct pinctrl_gpio_range *range;
280 const struct pinmux_ops *ops; 219 const struct pinmux_ops *ops;
281 int ret; 220 int ret;
282 int pin;
283
284 ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
285 if (ret)
286 return ret;
287 221
288 ops = pctldev->desc->pmxops; 222 ops = pctldev->desc->pmxops;
289 223
290 /* Convert to the pin controllers number space */
291 pin = gpio - range->base + range->pin_base;
292
293 if (ops->gpio_set_direction) 224 if (ops->gpio_set_direction)
294 ret = ops->gpio_set_direction(pctldev, range, pin, input); 225 ret = ops->gpio_set_direction(pctldev, range, pin, input);
295 else 226 else
@@ -299,112 +230,6 @@ static int pinctrl_gpio_direction(unsigned gpio, bool input)
299} 230}
300 231
301/** 232/**
302 * pinctrl_gpio_direction_input() - request a GPIO pin to go into input mode
303 * @gpio: the GPIO pin number from the GPIO subsystem number space
304 *
305 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
306 * as part of their gpio_direction_input() semantics, platforms and individual
307 * drivers shall *NOT* touch pin control GPIO calls.
308 */
309int pinctrl_gpio_direction_input(unsigned gpio)
310{
311 return pinctrl_gpio_direction(gpio, true);
312}
313EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
314
315/**
316 * pinctrl_gpio_direction_output() - request a GPIO pin to go into output mode
317 * @gpio: the GPIO pin number from the GPIO subsystem number space
318 *
319 * This function should *ONLY* be used from gpiolib-based GPIO drivers,
320 * as part of their gpio_direction_output() semantics, platforms and individual
321 * drivers shall *NOT* touch pin control GPIO calls.
322 */
323int pinctrl_gpio_direction_output(unsigned gpio)
324{
325 return pinctrl_gpio_direction(gpio, false);
326}
327EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
328
329/**
330 * pinctrl_register_mappings() - register a set of pin controller mappings
331 * @maps: the pincontrol mappings table to register, this should be marked with
332 * __initdata so it can be discarded after boot, this function will
333 * perform a shallow copy for the mapping entries.
334 * @num_maps: the number of maps in the mapping table
335 *
336 * Only call this once during initialization of your machine, the function is
337 * tagged as __init and won't be callable after init has completed. The map
338 * passed into this function will be owned by the pinmux core and cannot be
339 * freed.
340 */
341int __init pinctrl_register_mappings(struct pinctrl_map const *maps,
342 unsigned num_maps)
343{
344 void *tmp_maps;
345 int i;
346
347 pr_debug("add %d pinmux maps\n", num_maps);
348
349 /* First sanity check the new mapping */
350 for (i = 0; i < num_maps; i++) {
351 if (!maps[i].name) {
352 pr_err("failed to register map %d: no map name given\n",
353 i);
354 return -EINVAL;
355 }
356
357 if (!maps[i].ctrl_dev_name) {
358 pr_err("failed to register map %s (%d): no pin control device given\n",
359 maps[i].name, i);
360 return -EINVAL;
361 }
362
363 if (!maps[i].function) {
364 pr_err("failed to register map %s (%d): no function ID given\n",
365 maps[i].name, i);
366 return -EINVAL;
367 }
368
369 if (!maps[i].dev_name)
370 pr_debug("add system map %s function %s with no device\n",
371 maps[i].name,
372 maps[i].function);
373 else
374 pr_debug("register map %s, function %s\n",
375 maps[i].name,
376 maps[i].function);
377 }
378
379 /*
380 * Make a copy of the map array - string pointers will end up in the
381 * kernel const section anyway so these do not need to be deep copied.
382 */
383 if (!pinctrl_maps_num) {
384 /* On first call, just copy them */
385 tmp_maps = kmemdup(maps,
386 sizeof(struct pinctrl_map) * num_maps,
387 GFP_KERNEL);
388 if (!tmp_maps)
389 return -ENOMEM;
390 } else {
391 /* Subsequent calls, reallocate array to new size */
392 size_t oldsize = sizeof(struct pinctrl_map) * pinctrl_maps_num;
393 size_t newsize = sizeof(struct pinctrl_map) * num_maps;
394
395 tmp_maps = krealloc(pinctrl_maps,
396 oldsize + newsize, GFP_KERNEL);
397 if (!tmp_maps)
398 return -ENOMEM;
399 memcpy((tmp_maps + oldsize), maps, newsize);
400 }
401
402 pinctrl_maps = tmp_maps;
403 pinctrl_maps_num += num_maps;
404 return 0;
405}
406
407/**
408 * acquire_pins() - acquire all the pins for a certain function on a pinmux 233 * acquire_pins() - acquire all the pins for a certain function on a pinmux
409 * @pctldev: the device to take the pins on 234 * @pctldev: the device to take the pins on
410 * @func_selector: the function selector to acquire the pins for 235 * @func_selector: the function selector to acquire the pins for
@@ -660,227 +485,81 @@ static int pinmux_enable_muxmap(struct pinctrl_dev *pctldev,
660 return 0; 485 return 0;
661} 486}
662 487
663static void pinmux_free_groups(struct pinctrl *p)
664{
665 struct list_head *node, *tmp;
666
667 list_for_each_safe(node, tmp, &p->groups) {
668 struct pinmux_group *grp =
669 list_entry(node, struct pinmux_group, node);
670 /* Release all pins taken by this group */
671 release_pins(p->pctldev, grp->group_selector);
672 list_del(node);
673 kfree(grp);
674 }
675}
676
677/** 488/**
678 * pinctrl_get() - retrieves the pin controller handle for a certain device 489 * pinmux_apply_muxmap() - apply a certain mux mapping entry
679 * @dev: the device to get the pin controller handle for
680 * @name: an optional specific control mapping name or NULL, the name is only
681 * needed if you want to have more than one mapping per device, or if you
682 * need an anonymous pin control (not tied to any specific device)
683 */ 490 */
684struct pinctrl *pinctrl_get(struct device *dev, const char *name) 491int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
492 struct pinctrl *p,
493 struct device *dev,
494 const char *devname,
495 struct pinctrl_map const *map)
685{ 496{
686 struct pinctrl_map const *map = NULL; 497 int ret;
687 struct pinctrl_dev *pctldev = NULL;
688 const char *devname = NULL;
689 struct pinctrl *p;
690 bool found_map;
691 unsigned num_maps = 0;
692 int ret = -ENODEV;
693 int i;
694
695 /* We must have dev or ID or both */
696 if (!dev && !name)
697 return ERR_PTR(-EINVAL);
698
699 if (dev)
700 devname = dev_name(dev);
701
702 pr_debug("get mux %s for device %s\n", name,
703 devname ? devname : "(none)");
704
705 /*
706 * create the state cookie holder struct pinmux for each
707 * mapping, this is what consumers will get when requesting
708 * a pinmux handle with pinmux_get()
709 */
710 p = kzalloc(sizeof(struct pinctrl), GFP_KERNEL);
711 if (p == NULL)
712 return ERR_PTR(-ENOMEM);
713 mutex_init(&p->mutex);
714 p->func_selector = UINT_MAX;
715 INIT_LIST_HEAD(&p->groups);
716
717 /* Iterate over the pin control maps to locate the right ones */
718 for (i = 0; i < pinctrl_maps_num; i++) {
719 map = &pinctrl_maps[i];
720 found_map = false;
721
722 /*
723 * First, try to find the pctldev given in the map
724 */
725 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
726 if (!pctldev) {
727 pr_warning("could not find a pinctrl device for pinmux function %s, fishy, they shall all have one\n",
728 map->function);
729 pr_warning("given pinctrl device name: %s",
730 map->ctrl_dev_name);
731
732 /* Continue to check the other mappings anyway... */
733 continue;
734 }
735
736 pr_debug("in map, found pctldev %s to handle function %s",
737 dev_name(pctldev->dev), map->function);
738
739
740 /*
741 * If we're looking for a specific named map, this must match,
742 * else we loop and look for the next.
743 */
744 if (name != NULL) {
745 if (map->name == NULL)
746 continue;
747 if (strcmp(map->name, name))
748 continue;
749 }
750
751 /*
752 * This is for the case where no device name is given, we
753 * already know that the function name matches from above
754 * code.
755 */
756 if (!map->dev_name && (name != NULL))
757 found_map = true;
758
759 /* If the mapping has a device set up it must match */
760 if (map->dev_name &&
761 (!devname || !strcmp(map->dev_name, devname)))
762 /* MATCH! */
763 found_map = true;
764
765 /* If this map is applicable, then apply it */
766 if (found_map) {
767 ret = pinmux_enable_muxmap(pctldev, p, dev,
768 devname, map);
769 if (ret) {
770 pinmux_free_groups(p);
771 kfree(p);
772 return ERR_PTR(ret);
773 }
774 num_maps++;
775 }
776 }
777
778 498
779 /* We should have atleast one map, right */ 499 ret = pinmux_enable_muxmap(pctldev, p, dev,
780 if (!num_maps) { 500 devname, map);
781 pr_err("could not find any mux maps for device %s, ID %s\n", 501 if (ret) {
782 devname ? devname : "(anonymous)", 502 pinmux_put(p);
783 name ? name : "(undefined)"); 503 return ret;
784 kfree(p);
785 return ERR_PTR(-EINVAL);
786 } 504 }
787 505
788 pr_debug("found %u mux maps for device %s, UD %s\n", 506 return 0;
789 num_maps,
790 devname ? devname : "(anonymous)",
791 name ? name : "(undefined)");
792
793 /* Add the pinmux to the global list */
794 mutex_lock(&pinctrl_list_mutex);
795 list_add(&p->node, &pinctrl_list);
796 mutex_unlock(&pinctrl_list_mutex);
797
798 return p;
799} 507}
800EXPORT_SYMBOL_GPL(pinctrl_get);
801 508
802/** 509/**
803 * pinctrl_put() - release a previously claimed pin control handle 510 * pinmux_put() - free up the pinmux portions of a pin controller handle
804 * @p: a pin control handle previously claimed by pinctrl_get()
805 */ 511 */
806void pinctrl_put(struct pinctrl *p) 512void pinmux_put(struct pinctrl *p)
807{ 513{
808 if (p == NULL) 514 struct list_head *node, *tmp;
809 return;
810
811 mutex_lock(&p->mutex);
812 if (p->usecount)
813 pr_warn("releasing pin control handle with active users!\n");
814 /* Free the groups and all acquired pins */
815 pinmux_free_groups(p);
816 mutex_unlock(&p->mutex);
817
818 /* Remove from list */
819 mutex_lock(&pinctrl_list_mutex);
820 list_del(&p->node);
821 mutex_unlock(&pinctrl_list_mutex);
822 515
823 kfree(p); 516 list_for_each_safe(node, tmp, &p->groups) {
517 struct pinmux_group *grp =
518 list_entry(node, struct pinmux_group, node);
519 /* Release all pins taken by this group */
520 release_pins(p->pctldev, grp->group_selector);
521 list_del(node);
522 kfree(grp);
523 }
824} 524}
825EXPORT_SYMBOL_GPL(pinctrl_put);
826 525
827/** 526/**
828 * pinctrl_enable() - enable a certain pin controller setting 527 * pinmux_enable() - enable the pinmux portion of a pin control handle
829 * @p: the pin control handle to enable, previously claimed by pinctrl_get()
830 */ 528 */
831int pinctrl_enable(struct pinctrl *p) 529int pinmux_enable(struct pinctrl *p)
832{ 530{
833 int ret = 0; 531 struct pinctrl_dev *pctldev = p->pctldev;
532 const struct pinmux_ops *ops = pctldev->desc->pmxops;
533 struct pinmux_group *grp;
534 int ret;
834 535
835 if (p == NULL) 536 list_for_each_entry(grp, &p->groups, node) {
836 return -EINVAL; 537 ret = ops->enable(pctldev, p->func_selector,
837 mutex_lock(&p->mutex); 538 grp->group_selector);
838 if (p->usecount++ == 0) { 539 if (ret)
839 struct pinctrl_dev *pctldev = p->pctldev; 540 /*
840 const struct pinmux_ops *ops = pctldev->desc->pmxops; 541 * TODO: call disable() on all groups we called
841 struct pinmux_group *grp; 542 * enable() on to this point?
842 543 */
843 list_for_each_entry(grp, &p->groups, node) { 544 return ret;
844 ret = ops->enable(pctldev, p->func_selector,
845 grp->group_selector);
846 if (ret) {
847 /*
848 * TODO: call disable() on all groups we called
849 * enable() on to this point?
850 */
851 p->usecount--;
852 break;
853 }
854 }
855 } 545 }
856 mutex_unlock(&p->mutex); 546 return 0;
857 return ret;
858} 547}
859EXPORT_SYMBOL_GPL(pinctrl_enable);
860 548
861/** 549/**
862 * pinctrl_disable() - disable a certain pin control setting 550 * pinmux_disable() - disable the pinmux portions of a pin control handle
863 * @p: the pin control handle to disable, previously claimed by pinctrl_get()
864 */ 551 */
865void pinctrl_disable(struct pinctrl *p) 552void pinmux_disable(struct pinctrl *p)
866{ 553{
867 if (p == NULL) 554 struct pinctrl_dev *pctldev = p->pctldev;
868 return; 555 const struct pinmux_ops *ops = pctldev->desc->pmxops;
869 556 struct pinmux_group *grp;
870 mutex_lock(&p->mutex);
871 if (--p->usecount == 0) {
872 struct pinctrl_dev *pctldev = p->pctldev;
873 const struct pinmux_ops *ops = pctldev->desc->pmxops;
874 struct pinmux_group *grp;
875 557
876 list_for_each_entry(grp, &p->groups, node) { 558 list_for_each_entry(grp, &p->groups, node) {
877 ops->disable(pctldev, p->func_selector, 559 ops->disable(pctldev, p->func_selector,
878 grp->group_selector); 560 grp->group_selector);
879 }
880 } 561 }
881 mutex_unlock(&p->mutex);
882} 562}
883EXPORT_SYMBOL_GPL(pinctrl_disable);
884 563
885int pinmux_check_ops(struct pinctrl_dev *pctldev) 564int pinmux_check_ops(struct pinctrl_dev *pctldev)
886{ 565{
@@ -910,116 +589,6 @@ int pinmux_check_ops(struct pinctrl_dev *pctldev)
910 return 0; 589 return 0;
911} 590}
912 591
913/* Hog a single map entry and add to the hoglist */
914static int pinctrl_hog_map(struct pinctrl_dev *pctldev,
915 struct pinctrl_map const *map)
916{
917 struct pinctrl_hog *hog;
918 struct pinctrl *p;
919 int ret;
920
921 if (map->dev_name) {
922 /*
923 * TODO: the day we have device tree support, we can
924 * traverse the device tree and hog to specific device nodes
925 * without any problems, so then we can hog pinmuxes for
926 * all devices that just want a static pin mux at this point.
927 */
928 dev_err(pctldev->dev, "map %s wants to hog a non-system pinmux, this is not going to work\n",
929 map->name);
930 return -EINVAL;
931 }
932
933 hog = kzalloc(sizeof(struct pinctrl_hog), GFP_KERNEL);
934 if (!hog)
935 return -ENOMEM;
936
937 p = pinctrl_get(NULL, map->name);
938 if (IS_ERR(p)) {
939 kfree(hog);
940 dev_err(pctldev->dev,
941 "could not get the %s pin control mapping for hogging\n",
942 map->name);
943 return PTR_ERR(p);
944 }
945
946 ret = pinctrl_enable(p);
947 if (ret) {
948 pinctrl_put(p);
949 kfree(hog);
950 dev_err(pctldev->dev,
951 "could not enable the %s pin control mapping for hogging\n",
952 map->name);
953 return ret;
954 }
955
956 hog->map = map;
957 hog->p = p;
958
959 dev_info(pctldev->dev, "hogged map %s, function %s\n", map->name,
960 map->function);
961 mutex_lock(&pctldev->pinctrl_hogs_lock);
962 list_add(&hog->node, &pctldev->pinctrl_hogs);
963 mutex_unlock(&pctldev->pinctrl_hogs_lock);
964
965 return 0;
966}
967
968/**
969 * pinctrl_hog_maps() - hog specific map entries on controller device
970 * @pctldev: the pin control device to hog entries on
971 *
972 * When the pin controllers are registered, there may be some specific pinmux
973 * map entries that need to be hogged, i.e. get+enabled until the system shuts
974 * down.
975 */
976int pinctrl_hog_maps(struct pinctrl_dev *pctldev)
977{
978 struct device *dev = pctldev->dev;
979 const char *devname = dev_name(dev);
980 int ret;
981 int i;
982
983 INIT_LIST_HEAD(&pctldev->pinctrl_hogs);
984 mutex_init(&pctldev->pinctrl_hogs_lock);
985
986 for (i = 0; i < pinctrl_maps_num; i++) {
987 struct pinctrl_map const *map = &pinctrl_maps[i];
988
989 if (!map->hog_on_boot)
990 continue;
991
992 if (map->ctrl_dev_name &&
993 !strcmp(map->ctrl_dev_name, devname)) {
994 /* OK time to hog! */
995 ret = pinctrl_hog_map(pctldev, map);
996 if (ret)
997 return ret;
998 }
999 }
1000 return 0;
1001}
1002
1003/**
1004 * pinctrl_unhog_maps() - unhog specific map entries on controller device
1005 * @pctldev: the pin control device to unhog entries on
1006 */
1007void pinctrl_unhog_maps(struct pinctrl_dev *pctldev)
1008{
1009 struct list_head *node, *tmp;
1010
1011 mutex_lock(&pctldev->pinctrl_hogs_lock);
1012 list_for_each_safe(node, tmp, &pctldev->pinctrl_hogs) {
1013 struct pinctrl_hog *hog =
1014 list_entry(node, struct pinctrl_hog, node);
1015 pinctrl_disable(hog->p);
1016 pinctrl_put(hog->p);
1017 list_del(node);
1018 kfree(hog);
1019 }
1020 mutex_unlock(&pctldev->pinctrl_hogs_lock);
1021}
1022
1023#ifdef CONFIG_DEBUG_FS 592#ifdef CONFIG_DEBUG_FS
1024 593
1025/* Called from pincontrol core */ 594/* Called from pincontrol core */
@@ -1083,83 +652,29 @@ static int pinmux_pins_show(struct seq_file *s, void *what)
1083 return 0; 652 return 0;
1084} 653}
1085 654
1086static int pinmux_hogs_show(struct seq_file *s, void *what) 655void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
1087{
1088 struct pinctrl_dev *pctldev = s->private;
1089 struct pinctrl_hog *hog;
1090
1091 seq_puts(s, "Pin control map hogs held by device\n");
1092
1093 list_for_each_entry(hog, &pctldev->pinctrl_hogs, node)
1094 seq_printf(s, "%s\n", hog->map->name);
1095
1096 return 0;
1097}
1098
1099static int pinmux_show(struct seq_file *s, void *what)
1100{ 656{
1101 struct pinctrl *p; 657 struct pinctrl_dev *pctldev = p->pctldev;
1102 658 const struct pinmux_ops *pmxops;
1103 seq_puts(s, "Requested pinmuxes and their maps:\n"); 659 const struct pinctrl_ops *pctlops;
1104 list_for_each_entry(p, &pinctrl_list, node) { 660 struct pinmux_group *grp;
1105 struct pinctrl_dev *pctldev = p->pctldev;
1106 const struct pinmux_ops *pmxops;
1107 const struct pinctrl_ops *pctlops;
1108 struct pinmux_group *grp;
1109
1110 if (!pctldev) {
1111 seq_puts(s, "NO PIN CONTROLLER DEVICE\n");
1112 continue;
1113 }
1114
1115 pmxops = pctldev->desc->pmxops;
1116 pctlops = pctldev->desc->pctlops;
1117
1118 seq_printf(s, "device: %s function: %s (%u),",
1119 pinctrl_dev_get_name(p->pctldev),
1120 pmxops->get_function_name(pctldev,
1121 p->func_selector),
1122 p->func_selector);
1123
1124 seq_printf(s, " groups: [");
1125 list_for_each_entry(grp, &p->groups, node) {
1126 seq_printf(s, " %s (%u)",
1127 pctlops->get_group_name(pctldev,
1128 grp->group_selector),
1129 grp->group_selector);
1130 }
1131 seq_printf(s, " ]");
1132 661
1133 seq_printf(s, " users: %u map-> %s\n", 662 pmxops = pctldev->desc->pmxops;
1134 p->usecount, 663 pctlops = pctldev->desc->pctlops;
1135 p->dev ? dev_name(p->dev) : "(system)");
1136 }
1137 664
1138 return 0; 665 seq_printf(s, " function: %s (%u),",
1139} 666 pmxops->get_function_name(pctldev,
1140 667 p->func_selector),
1141static int pinctrl_maps_show(struct seq_file *s, void *what) 668 p->func_selector);
1142{
1143 int i;
1144 669
1145 seq_puts(s, "Pinctrl maps:\n"); 670 seq_printf(s, " groups: [");
1146 671 list_for_each_entry(grp, &p->groups, node) {
1147 for (i = 0; i < pinctrl_maps_num; i++) { 672 seq_printf(s, " %s (%u)",
1148 struct pinctrl_map const *map = &pinctrl_maps[i]; 673 pctlops->get_group_name(pctldev,
1149 674 grp->group_selector),
1150 seq_printf(s, "%s:\n", map->name); 675 grp->group_selector);
1151 if (map->dev_name)
1152 seq_printf(s, " device: %s\n",
1153 map->dev_name);
1154 else
1155 seq_printf(s, " SYSTEM MUX\n");
1156 seq_printf(s, " controlling device %s\n",
1157 map->ctrl_dev_name);
1158 seq_printf(s, " function: %s\n", map->function);
1159 seq_printf(s, " group: %s\n", map->group ? map->group :
1160 "(default)");
1161 } 676 }
1162 return 0; 677 seq_printf(s, " ]");
1163} 678}
1164 679
1165static int pinmux_functions_open(struct inode *inode, struct file *file) 680static int pinmux_functions_open(struct inode *inode, struct file *file)
@@ -1172,21 +687,6 @@ static int pinmux_pins_open(struct inode *inode, struct file *file)
1172 return single_open(file, pinmux_pins_show, inode->i_private); 687 return single_open(file, pinmux_pins_show, inode->i_private);
1173} 688}
1174 689
1175static int pinmux_hogs_open(struct inode *inode, struct file *file)
1176{
1177 return single_open(file, pinmux_hogs_show, inode->i_private);
1178}
1179
1180static int pinmux_open(struct inode *inode, struct file *file)
1181{
1182 return single_open(file, pinmux_show, NULL);
1183}
1184
1185static int pinctrl_maps_open(struct inode *inode, struct file *file)
1186{
1187 return single_open(file, pinctrl_maps_show, NULL);
1188}
1189
1190static const struct file_operations pinmux_functions_ops = { 690static const struct file_operations pinmux_functions_ops = {
1191 .open = pinmux_functions_open, 691 .open = pinmux_functions_open,
1192 .read = seq_read, 692 .read = seq_read,
@@ -1201,27 +701,6 @@ static const struct file_operations pinmux_pins_ops = {
1201 .release = single_release, 701 .release = single_release,
1202}; 702};
1203 703
1204static const struct file_operations pinmux_hogs_ops = {
1205 .open = pinmux_hogs_open,
1206 .read = seq_read,
1207 .llseek = seq_lseek,
1208 .release = single_release,
1209};
1210
1211static const struct file_operations pinmux_ops = {
1212 .open = pinmux_open,
1213 .read = seq_read,
1214 .llseek = seq_lseek,
1215 .release = single_release,
1216};
1217
1218static const struct file_operations pinctrl_maps_ops = {
1219 .open = pinctrl_maps_open,
1220 .read = seq_read,
1221 .llseek = seq_lseek,
1222 .release = single_release,
1223};
1224
1225void pinmux_init_device_debugfs(struct dentry *devroot, 704void pinmux_init_device_debugfs(struct dentry *devroot,
1226 struct pinctrl_dev *pctldev) 705 struct pinctrl_dev *pctldev)
1227{ 706{
@@ -1229,16 +708,6 @@ void pinmux_init_device_debugfs(struct dentry *devroot,
1229 devroot, pctldev, &pinmux_functions_ops); 708 devroot, pctldev, &pinmux_functions_ops);
1230 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO, 709 debugfs_create_file("pinmux-pins", S_IFREG | S_IRUGO,
1231 devroot, pctldev, &pinmux_pins_ops); 710 devroot, pctldev, &pinmux_pins_ops);
1232 debugfs_create_file("pinmux-hogs", S_IFREG | S_IRUGO,
1233 devroot, pctldev, &pinmux_hogs_ops);
1234}
1235
1236void pinmux_init_debugfs(struct dentry *subsys_root)
1237{
1238 debugfs_create_file("pinmuxes", S_IFREG | S_IRUGO,
1239 subsys_root, NULL, &pinmux_ops);
1240 debugfs_create_file("pinctrl-maps", S_IFREG | S_IRUGO,
1241 subsys_root, NULL, &pinctrl_maps_ops);
1242} 711}
1243 712
1244#endif /* CONFIG_DEBUG_FS */ 713#endif /* CONFIG_DEBUG_FS */
diff --git a/drivers/pinctrl/pinmux.h b/drivers/pinctrl/pinmux.h
index dfe81726965c..7680a1703252 100644
--- a/drivers/pinctrl/pinmux.h
+++ b/drivers/pinctrl/pinmux.h
@@ -15,9 +15,28 @@
15int pinmux_check_ops(struct pinctrl_dev *pctldev); 15int pinmux_check_ops(struct pinctrl_dev *pctldev);
16void pinmux_init_device_debugfs(struct dentry *devroot, 16void pinmux_init_device_debugfs(struct dentry *devroot,
17 struct pinctrl_dev *pctldev); 17 struct pinctrl_dev *pctldev);
18void pinmux_init_debugfs(struct dentry *subsys_root); 18int pinmux_request_gpio(struct pinctrl_dev *pctldev,
19int pinctrl_hog_maps(struct pinctrl_dev *pctldev); 19 struct pinctrl_gpio_range *range,
20void pinctrl_unhog_maps(struct pinctrl_dev *pctldev); 20 unsigned pin, unsigned gpio);
21void pinmux_free_gpio(struct pinctrl_dev *pctldev, unsigned pin,
22 struct pinctrl_gpio_range *range);
23int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
24 struct pinctrl_gpio_range *range,
25 unsigned pin, bool input);
26static inline void pinmux_init_pinctrl_handle(struct pinctrl *p)
27{
28 p->func_selector = UINT_MAX;
29 INIT_LIST_HEAD(&p->groups);
30}
31int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
32 struct pinctrl *p,
33 struct device *dev,
34 const char *devname,
35 struct pinctrl_map const *map);
36void pinmux_put(struct pinctrl *p);
37int pinmux_enable(struct pinctrl *p);
38void pinmux_disable(struct pinctrl *p);
39void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p);
21 40
22#else 41#else
23 42
@@ -31,16 +50,52 @@ static inline void pinmux_init_device_debugfs(struct dentry *devroot,
31{ 50{
32} 51}
33 52
34static inline void pinmux_init_debugfs(struct dentry *subsys_root) 53static inline int pinmux_request_gpio(struct pinctrl_dev *pctldev,
54 struct pinctrl_gpio_range *range,
55 unsigned pin, unsigned gpio)
56{
57 return 0;
58}
59
60static inline void pinmux_free_gpio(struct pinctrl_dev *pctldev,
61 unsigned pin,
62 struct pinctrl_gpio_range *range)
63{
64}
65
66static inline int pinmux_gpio_direction(struct pinctrl_dev *pctldev,
67 struct pinctrl_gpio_range *range,
68 unsigned pin, bool input)
69{
70 return 0;
71}
72
73static inline void pinmux_init_pinctrl_handle(struct pinctrl *p)
35{ 74{
36} 75}
37 76
38static inline int pinctrl_hog_maps(struct pinctrl_dev *pctldev) 77static inline int pinmux_apply_muxmap(struct pinctrl_dev *pctldev,
78 struct pinctrl *p,
79 struct device *dev,
80 const char *devname,
81 struct pinctrl_map const *map)
39{ 82{
40 return 0; 83 return 0;
41} 84}
42 85
43static inline void pinctrl_unhog_maps(struct pinctrl_dev *pctldev) 86static inline void pinmux_put(struct pinctrl *p)
87{
88}
89
90static inline int pinmux_enable(struct pinctrl *p)
91{
92}
93
94static inline void pinmux_disable(struct pinctrl *p)
95{
96}
97
98void pinmux_dbg_show(struct seq_file *s, struct pinctrl *p)
44{ 99{
45} 100}
46 101
diff --git a/include/linux/pinctrl/consumer.h b/include/linux/pinctrl/consumer.h
index c7d061776293..30865947f2d9 100644
--- a/include/linux/pinctrl/consumer.h
+++ b/include/linux/pinctrl/consumer.h
@@ -19,9 +19,9 @@
19/* This struct is private to the core and should be regarded as a cookie */ 19/* This struct is private to the core and should be regarded as a cookie */
20struct pinctrl; 20struct pinctrl;
21 21
22#ifdef CONFIG_PINMUX 22#ifdef CONFIG_PINCTRL
23 23
24/* External interface to pinmux */ 24/* External interface to pin control */
25extern int pinctrl_request_gpio(unsigned gpio); 25extern int pinctrl_request_gpio(unsigned gpio);
26extern void pinctrl_free_gpio(unsigned gpio); 26extern void pinctrl_free_gpio(unsigned gpio);
27extern int pinctrl_gpio_direction_input(unsigned gpio); 27extern int pinctrl_gpio_direction_input(unsigned gpio);
@@ -31,7 +31,7 @@ extern void pinctrl_put(struct pinctrl *p);
31extern int pinctrl_enable(struct pinctrl *p); 31extern int pinctrl_enable(struct pinctrl *p);
32extern void pinctrl_disable(struct pinctrl *p); 32extern void pinctrl_disable(struct pinctrl *p);
33 33
34#else /* !CONFIG_PINMUX */ 34#else /* !CONFIG_PINCTRL */
35 35
36static inline int pinctrl_request_gpio(unsigned gpio) 36static inline int pinctrl_request_gpio(unsigned gpio)
37{ 37{
@@ -70,7 +70,7 @@ static inline void pinctrl_disable(struct pinctrl *p)
70{ 70{
71} 71}
72 72
73#endif /* CONFIG_PINMUX */ 73#endif /* CONFIG_PINCTRL */
74 74
75#ifdef CONFIG_PINCONF 75#ifdef CONFIG_PINCONF
76 76