diff options
author | Linus Walleij <linus.walleij@linaro.org> | 2013-01-22 12:56:14 -0500 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2013-01-23 10:39:51 -0500 |
commit | ab78029ecc347debbd737f06688d788bd9d60c1d (patch) | |
tree | affc1f843abc1897ccf2723cf077be343c2845e2 | |
parent | 684697cbbcd076b8fde78d8863e341700533b542 (diff) |
drivers/pinctrl: grab default handles from device core
This makes the device core auto-grab the pinctrl handle and set
the "default" (PINCTRL_STATE_DEFAULT) state for every device
that is present in the device model right before probe. This will
account for the lion's share of embedded silicon devcies.
A modification of the semantics for pinctrl_get() is also done:
previously if the pinctrl handle for a certain device was already
taken, the pinctrl core would return an error. Now, since the
core may have already default-grabbed the handle and set its
state to "default", if the handle was already taken, this will
be disregarded and the located, previously instanitated handle
will be returned to the caller.
This way all code in drivers explicitly requesting their pinctrl
handlers will still be functional, and drivers that want to
explicitly retrieve and switch their handles can still do that.
But if the desired functionality is just boilerplate of this
type in the probe() function:
struct pinctrl *p;
p = devm_pinctrl_get_select_default(&dev);
if (IS_ERR(p)) {
if (PTR_ERR(p) == -EPROBE_DEFER)
return -EPROBE_DEFER;
dev_warn(&dev, "no pinctrl handle\n");
}
The discussion began with the addition of such boilerplate
to the omap4 keypad driver:
http://marc.info/?l=linux-input&m=135091157719300&w=2
A previous approach using notifiers was discussed:
http://marc.info/?l=linux-kernel&m=135263661110528&w=2
This failed because it could not handle deferred probes.
This patch alone does not solve the entire dilemma faced:
whether code should be distributed into the drivers or
if it should be centralized to e.g. a PM domain. But it
solves the immediate issue of the addition of boilerplate
to a lot of drivers that just want to grab the default
state. As mentioned, they can later explicitly retrieve
the handle and set different states, and this could as
well be done by e.g. PM domains as it is only related
to a certain struct device * pointer.
ChangeLog v4->v5 (Stephen):
- Simplified the devicecore grab code.
- Deleted a piece of documentation recommending that pins
be mapped to a device rather than hogged.
ChangeLog v3->v4 (Linus):
- Drop overzealous NULL checks.
- Move kref initialization to pinctrl_create().
- Seeking Tested-by from Stephen Warren so we do not disturb
the Tegra platform.
- Seeking ACK on this from Greg (and others who like it) so I
can merge it through the pinctrl subsystem.
ChangeLog v2->v3 (Linus):
- Abstain from using IS_ERR_OR_NULL() in the driver core,
Russell recently sent a patch to remove it. Handle the
NULL case explicitly even though it's a bogus case.
- Make sure we handle probe deferral correctly in the device
core file. devm_kfree() the container on error so we don't
waste memory for devices without pinctrl handles.
- Introduce reference counting into the pinctrl core using
<linux/kref.h> so that we don't release pinctrl handles
that have been obtained for two or more places.
ChangeLog v1->v2 (Linus):
- Only store a pointer in the device struct, and only allocate
this if it's really used by the device.
Cc: Felipe Balbi <balbi@ti.com>
Cc: Benoit Cousson <b-cousson@ti.com>
Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Cc: Mitch Bradley <wmb@firmworks.com>
Cc: Ulf Hansson <ulf.hansson@linaro.org>
Cc: Rafael J. Wysocki <rjw@sisk.pl>
Cc: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Cc: Rickard Andersson <rickard.andersson@stericsson.com>
Cc: Russell King <linux@arm.linux.org.uk>
Reviewed-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
[swarren: fixed and simplified error-handling in pinctrl_bind_pins(), to
correctly handle deferred probe. Removed admonition from docs not to use
pinctrl hogs for devices]
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r-- | Documentation/pinctrl.txt | 18 | ||||
-rw-r--r-- | drivers/base/Makefile | 1 | ||||
-rw-r--r-- | drivers/base/dd.c | 7 | ||||
-rw-r--r-- | drivers/base/pinctrl.c | 69 | ||||
-rw-r--r-- | drivers/pinctrl/core.c | 30 | ||||
-rw-r--r-- | drivers/pinctrl/core.h | 3 | ||||
-rw-r--r-- | include/linux/device.h | 7 | ||||
-rw-r--r-- | include/linux/pinctrl/devinfo.h | 45 |
8 files changed, 173 insertions, 7 deletions
diff --git a/Documentation/pinctrl.txt b/Documentation/pinctrl.txt index da40efbef6ec..a2b57e0a1db0 100644 --- a/Documentation/pinctrl.txt +++ b/Documentation/pinctrl.txt | |||
@@ -972,6 +972,18 @@ pinmux core. | |||
972 | Pin control requests from drivers | 972 | Pin control requests from drivers |
973 | ================================= | 973 | ================================= |
974 | 974 | ||
975 | When a device driver is about to probe the device core will automatically | ||
976 | attempt to issue pinctrl_get_select_default() on these devices. | ||
977 | This way driver writers do not need to add any of the boilerplate code | ||
978 | of the type found below. However when doing fine-grained state selection | ||
979 | and not using the "default" state, you may have to do some device driver | ||
980 | handling of the pinctrl handles and states. | ||
981 | |||
982 | So if you just want to put the pins for a certain device into the default | ||
983 | state and be done with it, there is nothing you need to do besides | ||
984 | providing the proper mapping table. The device core will take care of | ||
985 | the rest. | ||
986 | |||
975 | Generally it is discouraged to let individual drivers get and enable pin | 987 | Generally it is discouraged to let individual drivers get and enable pin |
976 | control. So if possible, handle the pin control in platform code or some other | 988 | control. So if possible, handle the pin control in platform code or some other |
977 | place where you have access to all the affected struct device * pointers. In | 989 | place where you have access to all the affected struct device * pointers. In |
@@ -1097,9 +1109,9 @@ situations that can be electrically unpleasant, you will certainly want to | |||
1097 | mux in and bias pins in a certain way before the GPIO subsystems starts to | 1109 | mux in and bias pins in a certain way before the GPIO subsystems starts to |
1098 | deal with them. | 1110 | deal with them. |
1099 | 1111 | ||
1100 | The above can be hidden: using pinctrl hogs, the pin control driver may be | 1112 | The above can be hidden: using the device core, the pinctrl core may be |
1101 | setting up the config and muxing for the pins when it is probing, | 1113 | setting up the config and muxing for the pins right before the device is |
1102 | nevertheless orthogonal to the GPIO subsystem. | 1114 | probing, nevertheless orthogonal to the GPIO subsystem. |
1103 | 1115 | ||
1104 | But there are also situations where it makes sense for the GPIO subsystem | 1116 | But there are also situations where it makes sense for the GPIO subsystem |
1105 | to communicate directly with with the pinctrl subsystem, using the latter | 1117 | to communicate directly with with the pinctrl subsystem, using the latter |
diff --git a/drivers/base/Makefile b/drivers/base/Makefile index 5aa2d703d19f..4e22ce3ed73d 100644 --- a/drivers/base/Makefile +++ b/drivers/base/Makefile | |||
@@ -21,6 +21,7 @@ endif | |||
21 | obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o | 21 | obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o |
22 | obj-$(CONFIG_REGMAP) += regmap/ | 22 | obj-$(CONFIG_REGMAP) += regmap/ |
23 | obj-$(CONFIG_SOC_BUS) += soc.o | 23 | obj-$(CONFIG_SOC_BUS) += soc.o |
24 | obj-$(CONFIG_PINCTRL) += pinctrl.o | ||
24 | 25 | ||
25 | ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG | 26 | ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG |
26 | 27 | ||
diff --git a/drivers/base/dd.c b/drivers/base/dd.c index e3bbed8a617c..656310156dde 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <linux/wait.h> | 24 | #include <linux/wait.h> |
25 | #include <linux/async.h> | 25 | #include <linux/async.h> |
26 | #include <linux/pm_runtime.h> | 26 | #include <linux/pm_runtime.h> |
27 | #include <linux/pinctrl/devinfo.h> | ||
27 | 28 | ||
28 | #include "base.h" | 29 | #include "base.h" |
29 | #include "power/power.h" | 30 | #include "power/power.h" |
@@ -269,6 +270,12 @@ static int really_probe(struct device *dev, struct device_driver *drv) | |||
269 | WARN_ON(!list_empty(&dev->devres_head)); | 270 | WARN_ON(!list_empty(&dev->devres_head)); |
270 | 271 | ||
271 | dev->driver = drv; | 272 | dev->driver = drv; |
273 | |||
274 | /* If using pinctrl, bind pins now before probing */ | ||
275 | ret = pinctrl_bind_pins(dev); | ||
276 | if (ret) | ||
277 | goto probe_failed; | ||
278 | |||
272 | if (driver_sysfs_add(dev)) { | 279 | if (driver_sysfs_add(dev)) { |
273 | printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n", | 280 | printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n", |
274 | __func__, dev_name(dev)); | 281 | __func__, dev_name(dev)); |
diff --git a/drivers/base/pinctrl.c b/drivers/base/pinctrl.c new file mode 100644 index 000000000000..67a274e86727 --- /dev/null +++ b/drivers/base/pinctrl.c | |||
@@ -0,0 +1,69 @@ | |||
1 | /* | ||
2 | * Driver core interface to the pinctrl subsystem. | ||
3 | * | ||
4 | * Copyright (C) 2012 ST-Ericsson SA | ||
5 | * Written on behalf of Linaro for ST-Ericsson | ||
6 | * Based on bits of regulator core, gpio core and clk core | ||
7 | * | ||
8 | * Author: Linus Walleij <linus.walleij@linaro.org> | ||
9 | * | ||
10 | * License terms: GNU General Public License (GPL) version 2 | ||
11 | */ | ||
12 | |||
13 | #include <linux/device.h> | ||
14 | #include <linux/pinctrl/devinfo.h> | ||
15 | #include <linux/pinctrl/consumer.h> | ||
16 | #include <linux/slab.h> | ||
17 | |||
18 | /** | ||
19 | * pinctrl_bind_pins() - called by the device core before probe | ||
20 | * @dev: the device that is just about to probe | ||
21 | */ | ||
22 | int pinctrl_bind_pins(struct device *dev) | ||
23 | { | ||
24 | int ret; | ||
25 | |||
26 | dev->pins = devm_kzalloc(dev, sizeof(*(dev->pins)), GFP_KERNEL); | ||
27 | if (!dev->pins) | ||
28 | return -ENOMEM; | ||
29 | |||
30 | dev->pins->p = devm_pinctrl_get(dev); | ||
31 | if (IS_ERR(dev->pins->p)) { | ||
32 | dev_dbg(dev, "no pinctrl handle\n"); | ||
33 | ret = PTR_ERR(dev->pins->p); | ||
34 | goto cleanup_alloc; | ||
35 | } | ||
36 | |||
37 | dev->pins->default_state = pinctrl_lookup_state(dev->pins->p, | ||
38 | PINCTRL_STATE_DEFAULT); | ||
39 | if (IS_ERR(dev->pins->default_state)) { | ||
40 | dev_dbg(dev, "no default pinctrl state\n"); | ||
41 | ret = 0; | ||
42 | goto cleanup_get; | ||
43 | } | ||
44 | |||
45 | ret = pinctrl_select_state(dev->pins->p, dev->pins->default_state); | ||
46 | if (ret) { | ||
47 | dev_dbg(dev, "failed to activate default pinctrl state\n"); | ||
48 | goto cleanup_get; | ||
49 | } | ||
50 | |||
51 | return 0; | ||
52 | |||
53 | /* | ||
54 | * If no pinctrl handle or default state was found for this device, | ||
55 | * let's explicitly free the pin container in the device, there is | ||
56 | * no point in keeping it around. | ||
57 | */ | ||
58 | cleanup_get: | ||
59 | devm_pinctrl_put(dev->pins->p); | ||
60 | cleanup_alloc: | ||
61 | devm_kfree(dev, dev->pins); | ||
62 | dev->pins = NULL; | ||
63 | |||
64 | /* Only return deferrals */ | ||
65 | if (ret != -EPROBE_DEFER) | ||
66 | ret = 0; | ||
67 | |||
68 | return ret; | ||
69 | } | ||
diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c index e6373f30aeeb..5a2fe9aae20f 100644 --- a/drivers/pinctrl/core.c +++ b/drivers/pinctrl/core.c | |||
@@ -14,6 +14,7 @@ | |||
14 | #define pr_fmt(fmt) "pinctrl core: " fmt | 14 | #define pr_fmt(fmt) "pinctrl core: " fmt |
15 | 15 | ||
16 | #include <linux/kernel.h> | 16 | #include <linux/kernel.h> |
17 | #include <linux/kref.h> | ||
17 | #include <linux/export.h> | 18 | #include <linux/export.h> |
18 | #include <linux/init.h> | 19 | #include <linux/init.h> |
19 | #include <linux/device.h> | 20 | #include <linux/device.h> |
@@ -727,6 +728,8 @@ static struct pinctrl *create_pinctrl(struct device *dev) | |||
727 | return ERR_PTR(ret); | 728 | return ERR_PTR(ret); |
728 | } | 729 | } |
729 | 730 | ||
731 | kref_init(&p->users); | ||
732 | |||
730 | /* Add the pinctrl handle to the global list */ | 733 | /* Add the pinctrl handle to the global list */ |
731 | list_add_tail(&p->node, &pinctrl_list); | 734 | list_add_tail(&p->node, &pinctrl_list); |
732 | 735 | ||
@@ -740,9 +743,17 @@ static struct pinctrl *pinctrl_get_locked(struct device *dev) | |||
740 | if (WARN_ON(!dev)) | 743 | if (WARN_ON(!dev)) |
741 | return ERR_PTR(-EINVAL); | 744 | return ERR_PTR(-EINVAL); |
742 | 745 | ||
746 | /* | ||
747 | * See if somebody else (such as the device core) has already | ||
748 | * obtained a handle to the pinctrl for this device. In that case, | ||
749 | * return another pointer to it. | ||
750 | */ | ||
743 | p = find_pinctrl(dev); | 751 | p = find_pinctrl(dev); |
744 | if (p != NULL) | 752 | if (p != NULL) { |
745 | return ERR_PTR(-EBUSY); | 753 | dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n"); |
754 | kref_get(&p->users); | ||
755 | return p; | ||
756 | } | ||
746 | 757 | ||
747 | return create_pinctrl(dev); | 758 | return create_pinctrl(dev); |
748 | } | 759 | } |
@@ -798,13 +809,24 @@ static void pinctrl_put_locked(struct pinctrl *p, bool inlist) | |||
798 | } | 809 | } |
799 | 810 | ||
800 | /** | 811 | /** |
801 | * pinctrl_put() - release a previously claimed pinctrl handle | 812 | * pinctrl_release() - release the pinctrl handle |
813 | * @kref: the kref in the pinctrl being released | ||
814 | */ | ||
815 | void pinctrl_release(struct kref *kref) | ||
816 | { | ||
817 | struct pinctrl *p = container_of(kref, struct pinctrl, users); | ||
818 | |||
819 | pinctrl_put_locked(p, true); | ||
820 | } | ||
821 | |||
822 | /** | ||
823 | * pinctrl_put() - decrease use count on a previously claimed pinctrl handle | ||
802 | * @p: the pinctrl handle to release | 824 | * @p: the pinctrl handle to release |
803 | */ | 825 | */ |
804 | void pinctrl_put(struct pinctrl *p) | 826 | void pinctrl_put(struct pinctrl *p) |
805 | { | 827 | { |
806 | mutex_lock(&pinctrl_mutex); | 828 | mutex_lock(&pinctrl_mutex); |
807 | pinctrl_put_locked(p, true); | 829 | kref_put(&p->users, pinctrl_release); |
808 | mutex_unlock(&pinctrl_mutex); | 830 | mutex_unlock(&pinctrl_mutex); |
809 | } | 831 | } |
810 | EXPORT_SYMBOL_GPL(pinctrl_put); | 832 | EXPORT_SYMBOL_GPL(pinctrl_put); |
diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h index 232a9f6db4aa..fdd350d639f6 100644 --- a/drivers/pinctrl/core.h +++ b/drivers/pinctrl/core.h | |||
@@ -9,6 +9,7 @@ | |||
9 | * License terms: GNU General Public License (GPL) version 2 | 9 | * License terms: GNU General Public License (GPL) version 2 |
10 | */ | 10 | */ |
11 | 11 | ||
12 | #include <linux/kref.h> | ||
12 | #include <linux/mutex.h> | 13 | #include <linux/mutex.h> |
13 | #include <linux/radix-tree.h> | 14 | #include <linux/radix-tree.h> |
14 | #include <linux/pinctrl/pinconf.h> | 15 | #include <linux/pinctrl/pinconf.h> |
@@ -58,6 +59,7 @@ struct pinctrl_dev { | |||
58 | * @state: the current state | 59 | * @state: the current state |
59 | * @dt_maps: the mapping table chunks dynamically parsed from device tree for | 60 | * @dt_maps: the mapping table chunks dynamically parsed from device tree for |
60 | * this device, if any | 61 | * this device, if any |
62 | * @users: reference count | ||
61 | */ | 63 | */ |
62 | struct pinctrl { | 64 | struct pinctrl { |
63 | struct list_head node; | 65 | struct list_head node; |
@@ -65,6 +67,7 @@ struct pinctrl { | |||
65 | struct list_head states; | 67 | struct list_head states; |
66 | struct pinctrl_state *state; | 68 | struct pinctrl_state *state; |
67 | struct list_head dt_maps; | 69 | struct list_head dt_maps; |
70 | struct kref users; | ||
68 | }; | 71 | }; |
69 | 72 | ||
70 | /** | 73 | /** |
diff --git a/include/linux/device.h b/include/linux/device.h index 43dcda937ddf..001f6637aa47 100644 --- a/include/linux/device.h +++ b/include/linux/device.h | |||
@@ -21,6 +21,7 @@ | |||
21 | #include <linux/compiler.h> | 21 | #include <linux/compiler.h> |
22 | #include <linux/types.h> | 22 | #include <linux/types.h> |
23 | #include <linux/mutex.h> | 23 | #include <linux/mutex.h> |
24 | #include <linux/pinctrl/devinfo.h> | ||
24 | #include <linux/pm.h> | 25 | #include <linux/pm.h> |
25 | #include <linux/atomic.h> | 26 | #include <linux/atomic.h> |
26 | #include <linux/ratelimit.h> | 27 | #include <linux/ratelimit.h> |
@@ -620,6 +621,8 @@ struct acpi_dev_node { | |||
620 | * @pm_domain: Provide callbacks that are executed during system suspend, | 621 | * @pm_domain: Provide callbacks that are executed during system suspend, |
621 | * hibernation, system resume and during runtime PM transitions | 622 | * hibernation, system resume and during runtime PM transitions |
622 | * along with subsystem-level and driver-level callbacks. | 623 | * along with subsystem-level and driver-level callbacks. |
624 | * @pins: For device pin management. | ||
625 | * See Documentation/pinctrl.txt for details. | ||
623 | * @numa_node: NUMA node this device is close to. | 626 | * @numa_node: NUMA node this device is close to. |
624 | * @dma_mask: Dma mask (if dma'ble device). | 627 | * @dma_mask: Dma mask (if dma'ble device). |
625 | * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all | 628 | * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all |
@@ -672,6 +675,10 @@ struct device { | |||
672 | struct dev_pm_info power; | 675 | struct dev_pm_info power; |
673 | struct dev_pm_domain *pm_domain; | 676 | struct dev_pm_domain *pm_domain; |
674 | 677 | ||
678 | #ifdef CONFIG_PINCTRL | ||
679 | struct dev_pin_info *pins; | ||
680 | #endif | ||
681 | |||
675 | #ifdef CONFIG_NUMA | 682 | #ifdef CONFIG_NUMA |
676 | int numa_node; /* NUMA node this device is close to */ | 683 | int numa_node; /* NUMA node this device is close to */ |
677 | #endif | 684 | #endif |
diff --git a/include/linux/pinctrl/devinfo.h b/include/linux/pinctrl/devinfo.h new file mode 100644 index 000000000000..6e5f8a985ea7 --- /dev/null +++ b/include/linux/pinctrl/devinfo.h | |||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | * Per-device information from the pin control system. | ||
3 | * This is the stuff that get included into the device | ||
4 | * core. | ||
5 | * | ||
6 | * Copyright (C) 2012 ST-Ericsson SA | ||
7 | * Written on behalf of Linaro for ST-Ericsson | ||
8 | * This interface is used in the core to keep track of pins. | ||
9 | * | ||
10 | * Author: Linus Walleij <linus.walleij@linaro.org> | ||
11 | * | ||
12 | * License terms: GNU General Public License (GPL) version 2 | ||
13 | */ | ||
14 | |||
15 | #ifndef PINCTRL_DEVINFO_H | ||
16 | #define PINCTRL_DEVINFO_H | ||
17 | |||
18 | #ifdef CONFIG_PINCTRL | ||
19 | |||
20 | /* The device core acts as a consumer toward pinctrl */ | ||
21 | #include <linux/pinctrl/consumer.h> | ||
22 | |||
23 | /** | ||
24 | * struct dev_pin_info - pin state container for devices | ||
25 | * @p: pinctrl handle for the containing device | ||
26 | * @default_state: the default state for the handle, if found | ||
27 | */ | ||
28 | struct dev_pin_info { | ||
29 | struct pinctrl *p; | ||
30 | struct pinctrl_state *default_state; | ||
31 | }; | ||
32 | |||
33 | extern int pinctrl_bind_pins(struct device *dev); | ||
34 | |||
35 | #else | ||
36 | |||
37 | /* Stubs if we're not using pinctrl */ | ||
38 | |||
39 | static inline int pinctrl_bind_pins(struct device *dev) | ||
40 | { | ||
41 | return 0; | ||
42 | } | ||
43 | |||
44 | #endif /* CONFIG_PINCTRL */ | ||
45 | #endif /* PINCTRL_DEVINFO_H */ | ||