aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/pinctrl.txt120
-rw-r--r--arch/arm/mach-u300/core.c16
-rw-r--r--drivers/pinctrl/core.c363
-rw-r--r--drivers/pinctrl/core.h23
-rw-r--r--drivers/tty/serial/sirfsoc_uart.c12
-rw-r--r--include/linux/pinctrl/consumer.h55
-rw-r--r--include/linux/pinctrl/machine.h11
7 files changed, 375 insertions, 225 deletions
diff --git a/Documentation/pinctrl.txt b/Documentation/pinctrl.txt
index 558aac554d09..23426c7bc8dc 100644
--- a/Documentation/pinctrl.txt
+++ b/Documentation/pinctrl.txt
@@ -847,8 +847,8 @@ As it is possible to map a function to different groups of pins an optional
847This example mapping is used to switch between two positions for spi0 at 847This example mapping is used to switch between two positions for spi0 at
848runtime, as described further below under the heading "Runtime pinmuxing". 848runtime, as described further below under the heading "Runtime pinmuxing".
849 849
850Further it is possible to match several groups of pins to the same function 850Further it is possible for one named state to affect the muxing of several
851for a single device, say for example in the mmc0 example above, where you can 851groups of pins, say for example in the mmc0 example above, where you can
852additively expand the mmc0 bus from 2 to 4 to 8 pins. If we want to use all 852additively expand the mmc0 bus from 2 to 4 to 8 pins. If we want to use all
853three groups for a total of 2+2+4 = 8 pins (for an 8-bit MMC bus as is the 853three groups for a total of 2+2+4 = 8 pins (for an 8-bit MMC bus as is the
854case), we define a mapping like this: 854case), we define a mapping like this:
@@ -879,6 +879,7 @@ case), we define a mapping like this:
879 .dev_name = "foo-mmc.0", 879 .dev_name = "foo-mmc.0",
880 .name = "8bit" 880 .name = "8bit"
881 .ctrl_dev_name = "pinctrl-foo", 881 .ctrl_dev_name = "pinctrl-foo",
882 .function = "mmc0",
882 .group = "mmc0_1_grp", 883 .group = "mmc0_1_grp",
883}, 884},
884{ 885{
@@ -900,10 +901,16 @@ case), we define a mapping like this:
900The result of grabbing this mapping from the device with something like 901The result of grabbing this mapping from the device with something like
901this (see next paragraph): 902this (see next paragraph):
902 903
903 p = pinctrl_get(&device, "8bit"); 904 p = pinctrl_get(dev);
905 s = pinctrl_lookup_state(p, "8bit");
906 ret = pinctrl_select_state(p, s);
907
908or more simply:
909
910 p = pinctrl_get_select(dev, "8bit");
904 911
905Will be that you activate all the three bottom records in the mapping at 912Will be that you activate all the three bottom records in the mapping at
906once. Since they share the same name, pin controller device, funcion and 913once. Since they share the same name, pin controller device, function and
907device, and since we allow multiple groups to match to a single device, they 914device, and since we allow multiple groups to match to a single device, they
908all get selected, and they all get enabled and disable simultaneously by the 915all get selected, and they all get enabled and disable simultaneously by the
909pinmux core. 916pinmux core.
@@ -925,45 +932,63 @@ default state like this:
925 932
926struct foo_state { 933struct foo_state {
927 struct pinctrl *p; 934 struct pinctrl *p;
935 struct pinctrl_state *s;
928 ... 936 ...
929}; 937};
930 938
931foo_probe() 939foo_probe()
932{ 940{
933 /* Allocate a state holder named "state" etc */ 941 /* Allocate a state holder named "foo" etc */
934 struct pinctrl p; 942 struct foo_state *foo = ...;
943
944 foo->p = pinctrl_get(&device);
945 if (IS_ERR(foo->p)) {
946 /* FIXME: clean up "foo" here */
947 return PTR_ERR(foo->p);
948 }
935 949
936 p = pinctrl_get(&device, PINCTRL_STATE_DEFAULT); 950 foo->s = pinctrl_lookup_state(foo->p, PINCTRL_STATE_DEFAULT);
937 if IS_ERR(p) 951 if (IS_ERR(foo->s)) {
938 return PTR_ERR(p); 952 pinctrl_put(foo->p);
939 pinctrl_enable(p); 953 /* FIXME: clean up "foo" here */
954 return PTR_ERR(s);
955 }
940 956
941 state->p = p; 957 ret = pinctrl_select_state(foo->s);
958 if (ret < 0) {
959 pinctrl_put(foo->p);
960 /* FIXME: clean up "foo" here */
961 return ret;
962 }
942} 963}
943 964
944foo_remove() 965foo_remove()
945{ 966{
946 pinctrl_disable(state->p);
947 pinctrl_put(state->p); 967 pinctrl_put(state->p);
948} 968}
949 969
950This get/enable/disable/put sequence can just as well be handled by bus drivers 970This get/lookup/select/put sequence can just as well be handled by bus drivers
951if you don't want each and every driver to handle it and you know the 971if you don't want each and every driver to handle it and you know the
952arrangement on your bus. 972arrangement on your bus.
953 973
954The semantics of the get/enable respective disable/put is as follows: 974The semantics of the pinctrl APIs are:
975
976- pinctrl_get() is called in process context to obtain a handle to all pinctrl
977 information for a given client device. It will allocate a struct from the
978 kernel memory to hold the pinmux state. All mapping table parsing or similar
979 slow operations take place within this API.
955 980
956- pinctrl_get() is called in process context to reserve the pins affected with 981- pinctrl_lookup_state() is called in process context to obtain a handle to a
957 a certain mapping and set up the pinmux core and the driver. It will allocate 982 specific state for a the client device. This operation may be slow too.
958 a struct from the kernel memory to hold the pinmux state.
959 983
960- pinctrl_enable()/pinctrl_disable() is quick and can be called from fastpath 984- pinctrl_select_state() programs pin controller hardware according to the
961 (irq context) when you quickly want to set up/tear down the hardware muxing 985 definition of the state as given by the mapping table. In theory this is a
962 when running a device driver. Usually it will just poke some values into a 986 fast-path operation, since it only involved blasting some register settings
963 register. 987 into hardware. However, note that some pin controllers may have their
988 registers on a slow/IRQ-based bus, so client devices should not assume they
989 can call pinctrl_select_state() from non-blocking contexts.
964 990
965- pinctrl_disable() is called in process context to tear down the pin requests 991- pinctrl_put() frees all information associated with a pinctrl handle.
966 and release the state holder struct for the mux setting etc.
967 992
968Usually the pin control core handled the get/put pair and call out to the 993Usually the pin control core handled the get/put pair and call out to the
969device drivers bookkeeping operations, like checking available functions and 994device drivers bookkeeping operations, like checking available functions and
@@ -979,12 +1004,12 @@ System pin control hogging
979========================== 1004==========================
980 1005
981Pin control map entries can be hogged by the core when the pin controller 1006Pin control map entries can be hogged by the core when the pin controller
982is registered. This means that the core will attempt to call pinctrl_get() and 1007is registered. This means that the core will attempt to call pinctrl_get(),
983pinctrl_enable() on it immediately after the pin control device has been 1008lookup_state() and select_state() on it immediately after the pin control
984registered. 1009device has been registered.
985 1010
986This is enabled by simply setting the .dev_name field in the map to the name 1011This occurs for mapping table entries where the client device name is equal
987of the pin controller itself, like this: 1012to the pin controller device name, and the state name is PINCTRL_STATE_DEFAULT.
988 1013
989{ 1014{
990 .dev_name = "pinctrl-foo", 1015 .dev_name = "pinctrl-foo",
@@ -1009,8 +1034,8 @@ It is possible to mux a certain function in and out at runtime, say to move
1009an SPI port from one set of pins to another set of pins. Say for example for 1034an SPI port from one set of pins to another set of pins. Say for example for
1010spi0 in the example above, we expose two different groups of pins for the same 1035spi0 in the example above, we expose two different groups of pins for the same
1011function, but with different named in the mapping as described under 1036function, but with different named in the mapping as described under
1012"Advanced mapping" above. So we have two mappings named "spi0-pos-A" and 1037"Advanced mapping" above. So that for an SPI device, we have two states named
1013"spi0-pos-B". 1038"pos-A" and "pos-B".
1014 1039
1015This snippet first muxes the function in the pins defined by group A, enables 1040This snippet first muxes the function in the pins defined by group A, enables
1016it, disables and releases it, and muxes it in on the pins defined by group B: 1041it, disables and releases it, and muxes it in on the pins defined by group B:
@@ -1020,23 +1045,36 @@ it, disables and releases it, and muxes it in on the pins defined by group B:
1020foo_switch() 1045foo_switch()
1021{ 1046{
1022 struct pinctrl *p; 1047 struct pinctrl *p;
1048 struct pinctrl_state *s1, *s2;
1049
1050 /* Setup */
1051 p = pinctrl_get(&device);
1052 if (IS_ERR(p))
1053 ...
1054
1055 s1 = pinctrl_lookup_state(foo->p, "pos-A");
1056 if (IS_ERR(s1))
1057 ...
1058
1059 s2 = pinctrl_lookup_state(foo->p, "pos-B");
1060 if (IS_ERR(s2))
1061 ...
1023 1062
1024 /* Enable on position A */ 1063 /* Enable on position A */
1025 p = pinctrl_get(&device, "spi0-pos-A"); 1064 ret = pinctrl_select_state(s1);
1026 if IS_ERR(p) 1065 if (ret < 0)
1027 return PTR_ERR(p); 1066 ...
1028 pinctrl_enable(p);
1029 1067
1030 /* This releases the pins again */ 1068 ...
1031 pinctrl_disable(p);
1032 pinctrl_put(p);
1033 1069
1034 /* Enable on position B */ 1070 /* Enable on position B */
1035 p = pinctrl_get(&device, "spi0-pos-B"); 1071 ret = pinctrl_select_state(s2);
1036 if IS_ERR(p) 1072 if (ret < 0)
1037 return PTR_ERR(p); 1073 ...
1038 pinctrl_enable(p); 1074
1039 ... 1075 ...
1076
1077 pinctrl_put(p);
1040} 1078}
1041 1079
1042The above has to be done from process context. 1080The above has to be done from process context.
diff --git a/arch/arm/mach-u300/core.c b/arch/arm/mach-u300/core.c
index f29565a10e2e..c092cf92e8ea 100644
--- a/arch/arm/mach-u300/core.c
+++ b/arch/arm/mach-u300/core.c
@@ -1618,22 +1618,18 @@ static struct pinctrl_map __initdata u300_pinmux_map[] = {
1618}; 1618};
1619 1619
1620struct u300_mux_hog { 1620struct u300_mux_hog {
1621 const char *name;
1622 struct device *dev; 1621 struct device *dev;
1623 struct pinctrl *p; 1622 struct pinctrl *p;
1624}; 1623};
1625 1624
1626static struct u300_mux_hog u300_mux_hogs[] = { 1625static struct u300_mux_hog u300_mux_hogs[] = {
1627 { 1626 {
1628 .name = "uart0",
1629 .dev = &uart0_device.dev, 1627 .dev = &uart0_device.dev,
1630 }, 1628 },
1631 { 1629 {
1632 .name = "spi0",
1633 .dev = &pl022_device.dev, 1630 .dev = &pl022_device.dev,
1634 }, 1631 },
1635 { 1632 {
1636 .name = "mmc0",
1637 .dev = &mmcsd_device.dev, 1633 .dev = &mmcsd_device.dev,
1638 }, 1634 },
1639}; 1635};
@@ -1646,16 +1642,10 @@ static int __init u300_pinctrl_fetch(void)
1646 struct pinctrl *p; 1642 struct pinctrl *p;
1647 int ret; 1643 int ret;
1648 1644
1649 p = pinctrl_get(u300_mux_hogs[i].dev, PINCTRL_STATE_DEFAULT); 1645 p = pinctrl_get_select_default(u300_mux_hogs[i].dev);
1650 if (IS_ERR(p)) { 1646 if (IS_ERR(p)) {
1651 pr_err("u300: could not get pinmux hog %s\n", 1647 pr_err("u300: could not get pinmux hog for dev %s\n",
1652 u300_mux_hogs[i].name); 1648 dev_name(u300_mux_hogs[i].dev));
1653 continue;
1654 }
1655 ret = pinctrl_enable(p);
1656 if (ret) {
1657 pr_err("u300: could enable pinmux hog %s\n",
1658 u300_mux_hogs[i].name);
1659 continue; 1649 continue;
1660 } 1650 }
1661 u300_mux_hogs[i].p = p; 1651 u300_mux_hogs[i].p = p;
diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
index 535f8d53c289..c6f3ca32189e 100644
--- a/drivers/pinctrl/core.c
+++ b/drivers/pinctrl/core.c
@@ -458,25 +458,98 @@ int pinctrl_gpio_direction_output(unsigned gpio)
458} 458}
459EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output); 459EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
460 460
461static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name) 461static struct pinctrl_state *find_state(struct pinctrl *p,
462 const char *name)
462{ 463{
463 struct pinctrl_dev *pctldev; 464 struct pinctrl_state *state;
464 const char *devname; 465
465 struct pinctrl *p; 466 list_for_each_entry(state, &p->states, node)
466 unsigned num_maps = 0; 467 if (!strcmp(state->name, name))
467 int ret; 468 return state;
468 struct pinctrl_maps *maps_node; 469
469 int i; 470 return NULL;
470 struct pinctrl_map const *map; 471}
472
473static struct pinctrl_state *create_state(struct pinctrl *p,
474 const char *name)
475{
476 struct pinctrl_state *state;
477
478 state = kzalloc(sizeof(*state), GFP_KERNEL);
479 if (state == NULL) {
480 dev_err(p->dev,
481 "failed to alloc struct pinctrl_state\n");
482 return ERR_PTR(-ENOMEM);
483 }
484
485 state->name = name;
486 INIT_LIST_HEAD(&state->settings);
487
488 list_add_tail(&state->node, &p->states);
489
490 return state;
491}
492
493static int add_setting(struct pinctrl *p, struct pinctrl_map const *map)
494{
495 struct pinctrl_state *state;
471 struct pinctrl_setting *setting; 496 struct pinctrl_setting *setting;
497 int ret;
472 498
473 /* We must have both a dev and state name */ 499 state = find_state(p, map->name);
474 if (WARN_ON(!dev || !name)) 500 if (!state)
475 return ERR_PTR(-EINVAL); 501 state = create_state(p, map->name);
502 if (IS_ERR(state))
503 return PTR_ERR(state);
476 504
477 devname = dev_name(dev); 505 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
506 if (setting == NULL) {
507 dev_err(p->dev,
508 "failed to alloc struct pinctrl_setting\n");
509 return -ENOMEM;
510 }
478 511
479 dev_dbg(dev, "pinctrl_get() for device %s state %s\n", devname, name); 512 setting->pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
513 if (setting->pctldev == NULL) {
514 dev_err(p->dev, "unknown pinctrl device %s in map entry",
515 map->ctrl_dev_name);
516 kfree(setting);
517 /* Eventually, this should trigger deferred probe */
518 return -ENODEV;
519 }
520
521 ret = pinmux_map_to_setting(map, setting);
522 if (ret < 0) {
523 kfree(setting);
524 return ret;
525 }
526
527 list_add_tail(&setting->node, &state->settings);
528
529 return 0;
530}
531
532static struct pinctrl *find_pinctrl(struct device *dev)
533{
534 struct pinctrl *p;
535
536 list_for_each_entry(p, &pinctrldev_list, node)
537 if (p->dev == dev)
538 return p;
539
540 return NULL;
541}
542
543static void pinctrl_put_locked(struct pinctrl *p, bool inlist);
544
545static struct pinctrl *create_pinctrl(struct device *dev)
546{
547 struct pinctrl *p;
548 const char *devname;
549 struct pinctrl_maps *maps_node;
550 int i;
551 struct pinctrl_map const *map;
552 int ret;
480 553
481 /* 554 /*
482 * create the state cookie holder struct pinctrl for each 555 * create the state cookie holder struct pinctrl for each
@@ -489,8 +562,9 @@ static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
489 return ERR_PTR(-ENOMEM); 562 return ERR_PTR(-ENOMEM);
490 } 563 }
491 p->dev = dev; 564 p->dev = dev;
492 p->state = name; 565 INIT_LIST_HEAD(&p->states);
493 INIT_LIST_HEAD(&p->settings); 566
567 devname = dev_name(dev);
494 568
495 /* Iterate over the pin control maps to locate the right ones */ 569 /* Iterate over the pin control maps to locate the right ones */
496 for_each_maps(maps_node, i, map) { 570 for_each_maps(maps_node, i, map) {
@@ -498,183 +572,179 @@ static struct pinctrl *pinctrl_get_locked(struct device *dev, const char *name)
498 if (strcmp(map->dev_name, devname)) 572 if (strcmp(map->dev_name, devname))
499 continue; 573 continue;
500 574
501 /* State name must be the one we're looking for */ 575 ret = add_setting(p, map);
502 if (strcmp(map->name, name)) 576 if (ret < 0) {
503 continue; 577 pinctrl_put_locked(p, false);
504 578 return ERR_PTR(ret);
505 /*
506 * Try to find the pctldev given in the map
507 */
508 pctldev = get_pinctrl_dev_from_devname(map->ctrl_dev_name);
509 if (!pctldev) {
510 dev_err(dev, "unknown pinctrl device %s in map entry",
511 map->ctrl_dev_name);
512 /* Eventually, this should trigger deferred probe */
513 ret = -ENODEV;
514 goto error;
515 }
516
517 dev_dbg(dev, "in map, found pctldev %s to handle function %s",
518 dev_name(pctldev->dev), map->function);
519
520 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
521 if (setting == NULL) {
522 dev_err(dev,
523 "failed to alloc struct pinctrl_setting\n");
524 ret = -ENOMEM;
525 goto error;
526 } 579 }
527
528 setting->pctldev = pctldev;
529 ret = pinmux_map_to_setting(map, setting);
530 if (ret < 0)
531 goto error;
532
533 list_add_tail(&setting->node, &p->settings);
534
535 num_maps++;
536 } 580 }
537 581
538 /*
539 * This may be perfectly legitimate. An IP block may get re-used
540 * across SoCs. Not all of those SoCs may need pinmux settings for the
541 * IP block, e.g. if one SoC dedicates pins to that function but
542 * another doesn't. The driver won't know this, and will always
543 * attempt to set up the pinmux. The mapping table defines whether any
544 * HW programming is actually needed.
545 */
546 if (!num_maps)
547 dev_info(dev, "zero maps found for mapping %s\n", name);
548
549 dev_dbg(dev, "found %u maps for device %s state %s\n",
550 num_maps, devname, name ? name : "(undefined)");
551
552 /* Add the pinmux to the global list */ 582 /* Add the pinmux to the global list */
553 list_add_tail(&p->node, &pinctrl_list); 583 list_add_tail(&p->node, &pinctrl_list);
554 584
555 return p; 585 return p;
586}
556 587
557error: 588static struct pinctrl *pinctrl_get_locked(struct device *dev)
558 list_for_each_entry(setting, &p->settings, node) 589{
559 pinmux_free_setting(setting); 590 struct pinctrl *p;
560 591
561 kfree(p); 592 if (WARN_ON(!dev))
593 return ERR_PTR(-EINVAL);
594
595 p = find_pinctrl(dev);
596 if (p != NULL)
597 return ERR_PTR(-EBUSY);
562 598
563 return ERR_PTR(ret); 599 p = create_pinctrl(dev);
600 if (IS_ERR(p))
601 return p;
602
603 return p;
564} 604}
565 605
566/** 606/**
567 * pinctrl_get() - retrieves the pin controller handle for a certain device 607 * pinctrl_get() - retrieves the pinctrl handle for a device
568 * @dev: the device to get the pin controller handle for 608 * @dev: the device to obtain the handle for
569 * @name: an optional specific control mapping name or NULL, the name is only
570 * needed if you want to have more than one mapping per device, or if you
571 * need an anonymous pin control (not tied to any specific device)
572 */ 609 */
573struct pinctrl *pinctrl_get(struct device *dev, const char *name) 610struct pinctrl *pinctrl_get(struct device *dev)
574{ 611{
575 struct pinctrl *p; 612 struct pinctrl *p;
576 613
577 mutex_lock(&pinctrl_mutex); 614 mutex_lock(&pinctrl_mutex);
578 p = pinctrl_get_locked(dev, name); 615 p = pinctrl_get_locked(dev);
579 mutex_unlock(&pinctrl_mutex); 616 mutex_unlock(&pinctrl_mutex);
580 617
581 return p; 618 return p;
582} 619}
583EXPORT_SYMBOL_GPL(pinctrl_get); 620EXPORT_SYMBOL_GPL(pinctrl_get);
584 621
585static void pinctrl_put_locked(struct pinctrl *p) 622static void pinctrl_put_locked(struct pinctrl *p, bool inlist)
586{ 623{
587 struct pinctrl_setting *setting, *n; 624 struct pinctrl_state *state, *n1;
588 625 struct pinctrl_setting *setting, *n2;
589 if (p == NULL) 626
590 return; 627 list_for_each_entry_safe(state, n1, &p->states, node) {
591 628 list_for_each_entry_safe(setting, n2, &state->settings, node) {
592 if (p->usecount) 629 if (state == p->state)
593 pr_warn("releasing pin control handle with active users!\n"); 630 pinmux_disable_setting(setting);
594 list_for_each_entry_safe(setting, n, &p->settings, node) { 631 pinmux_free_setting(setting);
595 pinmux_free_setting(setting); 632 list_del(&setting->node);
596 list_del(&setting->node); 633 kfree(setting);
597 kfree(setting); 634 }
635 list_del(&state->node);
636 kfree(state);
598 } 637 }
599 638
600 /* Remove from list */ 639 if (inlist)
601 list_del(&p->node); 640 list_del(&p->node);
602
603 kfree(p); 641 kfree(p);
604} 642}
605 643
606/** 644/**
607 * pinctrl_put() - release a previously claimed pin control handle 645 * pinctrl_put() - release a previously claimed pinctrl handle
608 * @p: a pin control handle previously claimed by pinctrl_get() 646 * @p: the pinctrl handle to release
609 */ 647 */
610void pinctrl_put(struct pinctrl *p) 648void pinctrl_put(struct pinctrl *p)
611{ 649{
612 mutex_lock(&pinctrl_mutex); 650 mutex_lock(&pinctrl_mutex);
613 pinctrl_put(p); 651 pinctrl_put_locked(p, true);
614 mutex_unlock(&pinctrl_mutex); 652 mutex_unlock(&pinctrl_mutex);
615} 653}
616EXPORT_SYMBOL_GPL(pinctrl_put); 654EXPORT_SYMBOL_GPL(pinctrl_put);
617 655
618static int pinctrl_enable_locked(struct pinctrl *p) 656static struct pinctrl_state *pinctrl_lookup_state_locked(struct pinctrl *p,
657 const char *name)
619{ 658{
620 struct pinctrl_setting *setting; 659 struct pinctrl_state *state;
621 int ret;
622 660
623 if (p == NULL) 661 state = find_state(p, name);
624 return -EINVAL; 662 if (!state)
663 return ERR_PTR(-ENODEV);
625 664
626 if (p->usecount++ == 0) { 665 return state;
627 list_for_each_entry(setting, &p->settings, node) {
628 ret = pinmux_enable_setting(setting);
629 if (ret < 0) {
630 /* FIXME: Difficult to return to prev state */
631 p->usecount--;
632 return ret;
633 }
634 }
635 }
636
637 return 0;
638} 666}
639 667
640/** 668/**
641 * pinctrl_enable() - enable a certain pin controller setting 669 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle
642 * @p: the pin control handle to enable, previously claimed by pinctrl_get() 670 * @p: the pinctrl handle to retrieve the state from
671 * @name: the state name to retrieve
643 */ 672 */
644int pinctrl_enable(struct pinctrl *p) 673struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)
645{ 674{
646 int ret; 675 struct pinctrl_state *s;
676
647 mutex_lock(&pinctrl_mutex); 677 mutex_lock(&pinctrl_mutex);
648 ret = pinctrl_enable_locked(p); 678 s = pinctrl_lookup_state_locked(p, name);
649 mutex_unlock(&pinctrl_mutex); 679 mutex_unlock(&pinctrl_mutex);
650 return ret; 680
681 return s;
651} 682}
652EXPORT_SYMBOL_GPL(pinctrl_enable); 683EXPORT_SYMBOL_GPL(pinctrl_lookup_state);
653 684
654static void pinctrl_disable_locked(struct pinctrl *p) 685static int pinctrl_select_state_locked(struct pinctrl *p,
686 struct pinctrl_state *state)
655{ 687{
656 struct pinctrl_setting *setting; 688 struct pinctrl_setting *setting, *setting2;
689 int ret;
657 690
658 if (p == NULL) 691 if (p->state == state)
659 return; 692 return 0;
660 693
661 if (--p->usecount == 0) { 694 if (p->state) {
662 list_for_each_entry(setting, &p->settings, node) 695 /*
663 pinmux_disable_setting(setting); 696 * The set of groups with a mux configuration in the old state
697 * may not be identical to the set of groups with a mux setting
698 * in the new state. While this might be unusual, it's entirely
699 * possible for the "user"-supplied mapping table to be written
700 * that way. For each group that was configured in the old state
701 * but not in the new state, this code puts that group into a
702 * safe/disabled state.
703 */
704 list_for_each_entry(setting, &p->state->settings, node) {
705 bool found = false;
706 list_for_each_entry(setting2, &state->settings, node) {
707 if (setting2->group_selector ==
708 setting->group_selector) {
709 found = true;
710 break;
711 }
712 }
713 if (!found)
714 pinmux_disable_setting(setting);
715 }
716 }
717
718 p->state = state;
719
720 /* Apply all the settings for the new state */
721 list_for_each_entry(setting, &state->settings, node) {
722 ret = pinmux_enable_setting(setting);
723 if (ret < 0) {
724 /* FIXME: Difficult to return to prev state */
725 return ret;
726 }
664 } 727 }
728
729 return 0;
665} 730}
666 731
667/** 732/**
668 * pinctrl_disable() - disable a certain pin control setting 733 * pinctrl_select() - select/activate/program a pinctrl state to HW
669 * @p: the pin control handle to disable, previously claimed by pinctrl_get() 734 * @p: the pinctrl handle for the device that requests configuratio
735 * @state: the state handle to select/activate/program
670 */ 736 */
671void pinctrl_disable(struct pinctrl *p) 737int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
672{ 738{
739 int ret;
740
673 mutex_lock(&pinctrl_mutex); 741 mutex_lock(&pinctrl_mutex);
674 pinctrl_disable_locked(p); 742 ret = pinctrl_select_state_locked(p, state);
675 mutex_unlock(&pinctrl_mutex); 743 mutex_unlock(&pinctrl_mutex);
744
745 return ret;
676} 746}
677EXPORT_SYMBOL_GPL(pinctrl_disable); 747EXPORT_SYMBOL_GPL(pinctrl_select_state);
678 748
679/** 749/**
680 * pinctrl_register_mappings() - register a set of pin controller mappings 750 * pinctrl_register_mappings() - register a set of pin controller mappings
@@ -891,6 +961,7 @@ static int pinctrl_maps_show(struct seq_file *s, void *what)
891static int pinctrl_show(struct seq_file *s, void *what) 961static int pinctrl_show(struct seq_file *s, void *what)
892{ 962{
893 struct pinctrl *p; 963 struct pinctrl *p;
964 struct pinctrl_state *state;
894 struct pinctrl_setting *setting; 965 struct pinctrl_setting *setting;
895 966
896 seq_puts(s, "Requested pin control handlers their pinmux maps:\n"); 967 seq_puts(s, "Requested pin control handlers their pinmux maps:\n");
@@ -898,12 +969,17 @@ static int pinctrl_show(struct seq_file *s, void *what)
898 mutex_lock(&pinctrl_mutex); 969 mutex_lock(&pinctrl_mutex);
899 970
900 list_for_each_entry(p, &pinctrl_list, node) { 971 list_for_each_entry(p, &pinctrl_list, node) {
901 seq_printf(s, "device: %s state: %s users: %u\n", 972 seq_printf(s, "device: %s current state: %s\n",
902 dev_name(p->dev), p->state, p->usecount); 973 dev_name(p->dev),
974 p->state ? p->state->name : "none");
975
976 list_for_each_entry(state, &p->states, node) {
977 seq_printf(s, " state: %s\n", state->name);
903 978
904 list_for_each_entry(setting, &p->settings, node) { 979 list_for_each_entry(setting, &state->settings, node) {
905 seq_printf(s, " "); 980 seq_printf(s, " ");
906 pinmux_dbg_show(s, setting); 981 pinmux_dbg_show(s, setting);
982 }
907 } 983 }
908 } 984 }
909 985
@@ -1113,9 +1189,14 @@ struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
1113 1189
1114 list_add_tail(&pctldev->node, &pinctrldev_list); 1190 list_add_tail(&pctldev->node, &pinctrldev_list);
1115 1191
1116 pctldev->p = pinctrl_get_locked(pctldev->dev, PINCTRL_STATE_DEFAULT); 1192 pctldev->p = pinctrl_get_locked(pctldev->dev);
1117 if (!IS_ERR(pctldev->p)) 1193 if (!IS_ERR(pctldev->p)) {
1118 pinctrl_enable_locked(pctldev->p); 1194 struct pinctrl_state *s =
1195 pinctrl_lookup_state_locked(pctldev->p,
1196 PINCTRL_STATE_DEFAULT);
1197 if (!IS_ERR(s))
1198 pinctrl_select_state_locked(pctldev->p, s);
1199 }
1119 1200
1120 mutex_unlock(&pinctrl_mutex); 1201 mutex_unlock(&pinctrl_mutex);
1121 1202
@@ -1144,10 +1225,8 @@ void pinctrl_unregister(struct pinctrl_dev *pctldev)
1144 1225
1145 mutex_lock(&pinctrl_mutex); 1226 mutex_lock(&pinctrl_mutex);
1146 1227
1147 if (!IS_ERR(pctldev->p)) { 1228 if (!IS_ERR(pctldev->p))
1148 pinctrl_disable_locked(pctldev->p); 1229 pinctrl_put_locked(pctldev->p, true);
1149 pinctrl_put_locked(pctldev->p);
1150 }
1151 1230
1152 /* TODO: check that no pinmuxes are still active? */ 1231 /* TODO: check that no pinmuxes are still active? */
1153 list_del(&pctldev->node); 1232 list_del(&pctldev->node);
diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h
index 0bc52ecaf710..5691d312e15a 100644
--- a/drivers/pinctrl/core.h
+++ b/drivers/pinctrl/core.h
@@ -49,22 +49,31 @@ struct pinctrl_dev {
49 * struct pinctrl - per-device pin control state holder 49 * struct pinctrl - per-device pin control state holder
50 * @node: global list node 50 * @node: global list node
51 * @dev: the device using this pin control handle 51 * @dev: the device using this pin control handle
52 * @state: the state name passed to pinctrl_get() 52 * @states: a list of states for this device
53 * @usecount: the number of active users of this pin controller setting, used 53 * @state: the current state
54 * to keep track of nested use cases
55 * @settings: a list of settings for this device/state
56 */ 54 */
57struct pinctrl { 55struct pinctrl {
58 struct list_head node; 56 struct list_head node;
59 struct device *dev; 57 struct device *dev;
60 const char *state; 58 struct list_head states;
61 unsigned usecount; 59 struct pinctrl_state *state;
60};
61
62/**
63 * struct pinctrl_state - a pinctrl state for a device
64 * @node: list not for struct pinctrl's @states field
65 * @name: the name of this state
66 * @settings: a list of settings for this state
67 */
68struct pinctrl_state {
69 struct list_head node;
70 const char *name;
62 struct list_head settings; 71 struct list_head settings;
63}; 72};
64 73
65/** 74/**
66 * struct pinctrl_setting - an individual mux setting 75 * struct pinctrl_setting - an individual mux setting
67 * @node: list node for struct pinctrl's @settings field 76 * @node: list node for struct pinctrl_settings's @settings field
68 * @pctldev: pin control device handling to be programmed 77 * @pctldev: pin control device handling to be programmed
69 * @group_selector: the group selector to program 78 * @group_selector: the group selector to program
70 * @func_selector: the function selector to program 79 * @func_selector: the function selector to program
diff --git a/drivers/tty/serial/sirfsoc_uart.c b/drivers/tty/serial/sirfsoc_uart.c
index 3cabb650a1c1..5b3eda2024fe 100644
--- a/drivers/tty/serial/sirfsoc_uart.c
+++ b/drivers/tty/serial/sirfsoc_uart.c
@@ -673,12 +673,10 @@ int sirfsoc_uart_probe(struct platform_device *pdev)
673 port->irq = res->start; 673 port->irq = res->start;
674 674
675 if (sirfport->hw_flow_ctrl) { 675 if (sirfport->hw_flow_ctrl) {
676 sirfport->p = pinctrl_get(&pdev->dev, PINCTRL_STATE_DEFAULT); 676 sirfport->p = pinctrl_get_select_default(&pdev->dev);
677 ret = IS_ERR(sirfport->p); 677 ret = IS_ERR(sirfport->p);
678 if (ret) 678 if (ret)
679 goto pin_err; 679 goto pin_err;
680
681 pinctrl_enable(sirfport->p);
682 } 680 }
683 681
684 port->ops = &sirfsoc_uart_ops; 682 port->ops = &sirfsoc_uart_ops;
@@ -695,10 +693,8 @@ int sirfsoc_uart_probe(struct platform_device *pdev)
695 693
696port_err: 694port_err:
697 platform_set_drvdata(pdev, NULL); 695 platform_set_drvdata(pdev, NULL);
698 if (sirfport->hw_flow_ctrl) { 696 if (sirfport->hw_flow_ctrl)
699 pinctrl_disable(sirfport->p);
700 pinctrl_put(sirfport->p); 697 pinctrl_put(sirfport->p);
701 }
702pin_err: 698pin_err:
703irq_err: 699irq_err:
704 devm_iounmap(&pdev->dev, port->membase); 700 devm_iounmap(&pdev->dev, port->membase);
@@ -711,10 +707,8 @@ static int sirfsoc_uart_remove(struct platform_device *pdev)
711 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev); 707 struct sirfsoc_uart_port *sirfport = platform_get_drvdata(pdev);
712 struct uart_port *port = &sirfport->port; 708 struct uart_port *port = &sirfport->port;
713 platform_set_drvdata(pdev, NULL); 709 platform_set_drvdata(pdev, NULL);
714 if (sirfport->hw_flow_ctrl) { 710 if (sirfport->hw_flow_ctrl)
715 pinctrl_disable(sirfport->p);
716 pinctrl_put(sirfport->p); 711 pinctrl_put(sirfport->p);
717 }
718 devm_iounmap(&pdev->dev, port->membase); 712 devm_iounmap(&pdev->dev, port->membase);
719 uart_remove_one_port(&sirfsoc_uart_drv, port); 713 uart_remove_one_port(&sirfsoc_uart_drv, port);
720 return 0; 714 return 0;
diff --git a/include/linux/pinctrl/consumer.h b/include/linux/pinctrl/consumer.h
index 30865947f2d9..9ad5896cfa0e 100644
--- a/include/linux/pinctrl/consumer.h
+++ b/include/linux/pinctrl/consumer.h
@@ -12,12 +12,14 @@
12#ifndef __LINUX_PINCTRL_CONSUMER_H 12#ifndef __LINUX_PINCTRL_CONSUMER_H
13#define __LINUX_PINCTRL_CONSUMER_H 13#define __LINUX_PINCTRL_CONSUMER_H
14 14
15#include <linux/err.h>
15#include <linux/list.h> 16#include <linux/list.h>
16#include <linux/seq_file.h> 17#include <linux/seq_file.h>
17#include "pinctrl.h" 18#include "pinctrl.h"
18 19
19/* This struct is private to the core and should be regarded as a cookie */ 20/* This struct is private to the core and should be regarded as a cookie */
20struct pinctrl; 21struct pinctrl;
22struct pinctrl_state;
21 23
22#ifdef CONFIG_PINCTRL 24#ifdef CONFIG_PINCTRL
23 25
@@ -26,10 +28,13 @@ extern int pinctrl_request_gpio(unsigned gpio);
26extern void pinctrl_free_gpio(unsigned gpio); 28extern void pinctrl_free_gpio(unsigned gpio);
27extern int pinctrl_gpio_direction_input(unsigned gpio); 29extern int pinctrl_gpio_direction_input(unsigned gpio);
28extern int pinctrl_gpio_direction_output(unsigned gpio); 30extern int pinctrl_gpio_direction_output(unsigned gpio);
29extern struct pinctrl * __must_check pinctrl_get(struct device *dev, const char *name); 31
32extern struct pinctrl * __must_check pinctrl_get(struct device *dev);
30extern void pinctrl_put(struct pinctrl *p); 33extern void pinctrl_put(struct pinctrl *p);
31extern int pinctrl_enable(struct pinctrl *p); 34extern struct pinctrl_state * __must_check pinctrl_lookup_state(
32extern void pinctrl_disable(struct pinctrl *p); 35 struct pinctrl *p,
36 const char *name);
37extern int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *s);
33 38
34#else /* !CONFIG_PINCTRL */ 39#else /* !CONFIG_PINCTRL */
35 40
@@ -52,7 +57,7 @@ static inline int pinctrl_gpio_direction_output(unsigned gpio)
52 return 0; 57 return 0;
53} 58}
54 59
55static inline struct pinctrl * __must_check pinctrl_get(struct device *dev, const char *name) 60static inline struct pinctrl * __must_check pinctrl_get(struct device *dev)
56{ 61{
57 return NULL; 62 return NULL;
58} 63}
@@ -61,17 +66,53 @@ static inline void pinctrl_put(struct pinctrl *p)
61{ 66{
62} 67}
63 68
64static inline int pinctrl_enable(struct pinctrl *p) 69static inline struct pinctrl_state * __must_check pinctrl_lookup_state(
70 struct pinctrl *p,
71 const char *name)
65{ 72{
66 return 0; 73 return NULL;
67} 74}
68 75
69static inline void pinctrl_disable(struct pinctrl *p) 76static inline int pinctrl_select_state(struct pinctrl *p,
77 struct pinctrl_state *s)
70{ 78{
79 return 0;
71} 80}
72 81
73#endif /* CONFIG_PINCTRL */ 82#endif /* CONFIG_PINCTRL */
74 83
84static inline struct pinctrl * __must_check pinctrl_get_select(
85 struct device *dev, const char *name)
86{
87 struct pinctrl *p;
88 struct pinctrl_state *s;
89 int ret;
90
91 p = pinctrl_get(dev);
92 if (IS_ERR(p))
93 return p;
94
95 s = pinctrl_lookup_state(p, name);
96 if (IS_ERR(s)) {
97 pinctrl_put(p);
98 return ERR_PTR(PTR_ERR(s));
99 }
100
101 ret = pinctrl_select_state(p, s);
102 if (ret < 0) {
103 pinctrl_put(p);
104 return ERR_PTR(ret);
105 }
106
107 return p;
108}
109
110static inline struct pinctrl * __must_check pinctrl_get_select_default(
111 struct device *dev)
112{
113 return pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
114}
115
75#ifdef CONFIG_PINCONF 116#ifdef CONFIG_PINCONF
76 117
77extern int pin_config_get(const char *dev_name, const char *name, 118extern int pin_config_get(const char *dev_name, const char *name,
diff --git a/include/linux/pinctrl/machine.h b/include/linux/pinctrl/machine.h
index 20e97353d5f9..05d25c8adbaf 100644
--- a/include/linux/pinctrl/machine.h
+++ b/include/linux/pinctrl/machine.h
@@ -21,23 +21,22 @@
21 * same name as the pin controllers own dev_name(), the map entry will be 21 * same name as the pin controllers own dev_name(), the map entry will be
22 * hogged by the driver itself upon registration 22 * hogged by the driver itself upon registration
23 * @name: the name of this specific map entry for the particular machine. 23 * @name: the name of this specific map entry for the particular machine.
24 * This is the second parameter passed to pinmux_get() when you want 24 * This is the parameter passed to pinmux_lookup_state()
25 * to have several mappings to the same device
26 * @ctrl_dev_name: the name of the device controlling this specific mapping, 25 * @ctrl_dev_name: the name of the device controlling this specific mapping,
27 * the name must be the same as in your struct device* 26 * the name must be the same as in your struct device*
28 * @function: a function in the driver to use for this mapping, the driver
29 * will lookup the function referenced by this ID on the specified
30 * pin control device
31 * @group: sometimes a function can map to different pin groups, so this 27 * @group: sometimes a function can map to different pin groups, so this
32 * selects a certain specific pin group to activate for the function, if 28 * selects a certain specific pin group to activate for the function, if
33 * left as NULL, the first applicable group will be used 29 * left as NULL, the first applicable group will be used
30 * @function: a function in the driver to use for this mapping, the driver
31 * will lookup the function referenced by this ID on the specified
32 * pin control device
34 */ 33 */
35struct pinctrl_map { 34struct pinctrl_map {
36 const char *dev_name; 35 const char *dev_name;
37 const char *name; 36 const char *name;
38 const char *ctrl_dev_name; 37 const char *ctrl_dev_name;
39 const char *function;
40 const char *group; 38 const char *group;
39 const char *function;
41}; 40};
42 41
43/* 42/*