Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+173 -7
+15 -3
Documentation/pinctrl.txt
··· 972 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 987 Generally it is discouraged to let individual drivers get and enable pin 976 988 control. So if possible, handle the pin control in platform code or some other 977 989 place where you have access to all the affected struct device * pointers. In ··· 1109 1097 mux in and bias pins in a certain way before the GPIO subsystems starts to 1110 1098 deal with them. 1111 1099 1112 - The above can be hidden: using pinctrl hogs, the pin control driver may be 1113 - setting up the config and muxing for the pins when it is probing, 1114 - nevertheless orthogonal to the GPIO subsystem. 1100 + The above can be hidden: using the device core, the pinctrl core may be 1101 + setting up the config and muxing for the pins right before the device is 1102 + probing, nevertheless orthogonal to the GPIO subsystem. 1115 1103 1116 1104 But there are also situations where it makes sense for the GPIO subsystem 1117 1105 to communicate directly with with the pinctrl subsystem, using the latter
+1
drivers/base/Makefile
··· 21 21 obj-$(CONFIG_SYS_HYPERVISOR) += hypervisor.o 22 22 obj-$(CONFIG_REGMAP) += regmap/ 23 23 obj-$(CONFIG_SOC_BUS) += soc.o 24 + obj-$(CONFIG_PINCTRL) += pinctrl.o 24 25 25 26 ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG 26 27
+7
drivers/base/dd.c
··· 24 24 #include <linux/wait.h> 25 25 #include <linux/async.h> 26 26 #include <linux/pm_runtime.h> 27 + #include <linux/pinctrl/devinfo.h> 27 28 28 29 #include "base.h" 29 30 #include "power/power.h" ··· 270 269 WARN_ON(!list_empty(&dev->devres_head)); 271 270 272 271 dev->driver = drv; 272 + 273 + /* If using pinctrl, bind pins now before probing */ 274 + ret = pinctrl_bind_pins(dev); 275 + if (ret) 276 + goto probe_failed; 277 + 273 278 if (driver_sysfs_add(dev)) { 274 279 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n", 275 280 __func__, dev_name(dev));
+69
drivers/base/pinctrl.c
··· 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 + }
+26 -4
drivers/pinctrl/core.c
··· 14 14 #define pr_fmt(fmt) "pinctrl core: " fmt 15 15 16 16 #include <linux/kernel.h> 17 + #include <linux/kref.h> 17 18 #include <linux/export.h> 18 19 #include <linux/init.h> 19 20 #include <linux/device.h> ··· 728 727 return ERR_PTR(ret); 729 728 } 730 729 730 + kref_init(&p->users); 731 + 731 732 /* Add the pinctrl handle to the global list */ 732 733 list_add_tail(&p->node, &pinctrl_list); 733 734 ··· 743 740 if (WARN_ON(!dev)) 744 741 return ERR_PTR(-EINVAL); 745 742 743 + /* 744 + * See if somebody else (such as the device core) has already 745 + * obtained a handle to the pinctrl for this device. In that case, 746 + * return another pointer to it. 747 + */ 746 748 p = find_pinctrl(dev); 747 - if (p != NULL) 748 - return ERR_PTR(-EBUSY); 749 + if (p != NULL) { 750 + dev_dbg(dev, "obtain a copy of previously claimed pinctrl\n"); 751 + kref_get(&p->users); 752 + return p; 753 + } 749 754 750 755 return create_pinctrl(dev); 751 756 } ··· 809 798 } 810 799 811 800 /** 812 - * pinctrl_put() - release a previously claimed pinctrl handle 801 + * pinctrl_release() - release the pinctrl handle 802 + * @kref: the kref in the pinctrl being released 803 + */ 804 + void pinctrl_release(struct kref *kref) 805 + { 806 + struct pinctrl *p = container_of(kref, struct pinctrl, users); 807 + 808 + pinctrl_put_locked(p, true); 809 + } 810 + 811 + /** 812 + * pinctrl_put() - decrease use count on a previously claimed pinctrl handle 813 813 * @p: the pinctrl handle to release 814 814 */ 815 815 void pinctrl_put(struct pinctrl *p) 816 816 { 817 817 mutex_lock(&pinctrl_mutex); 818 - pinctrl_put_locked(p, true); 818 + kref_put(&p->users, pinctrl_release); 819 819 mutex_unlock(&pinctrl_mutex); 820 820 } 821 821 EXPORT_SYMBOL_GPL(pinctrl_put);
+3
drivers/pinctrl/core.h
··· 9 9 * License terms: GNU General Public License (GPL) version 2 10 10 */ 11 11 12 + #include <linux/kref.h> 12 13 #include <linux/mutex.h> 13 14 #include <linux/radix-tree.h> 14 15 #include <linux/pinctrl/pinconf.h> ··· 59 58 * @state: the current state 60 59 * @dt_maps: the mapping table chunks dynamically parsed from device tree for 61 60 * this device, if any 61 + * @users: reference count 62 62 */ 63 63 struct pinctrl { 64 64 struct list_head node; ··· 67 65 struct list_head states; 68 66 struct pinctrl_state *state; 69 67 struct list_head dt_maps; 68 + struct kref users; 70 69 }; 71 70 72 71 /**
+7
include/linux/device.h
··· 21 21 #include <linux/compiler.h> 22 22 #include <linux/types.h> 23 23 #include <linux/mutex.h> 24 + #include <linux/pinctrl/devinfo.h> 24 25 #include <linux/pm.h> 25 26 #include <linux/atomic.h> 26 27 #include <linux/ratelimit.h> ··· 621 620 * @pm_domain: Provide callbacks that are executed during system suspend, 622 621 * hibernation, system resume and during runtime PM transitions 623 622 * along with subsystem-level and driver-level callbacks. 623 + * @pins: For device pin management. 624 + * See Documentation/pinctrl.txt for details. 624 625 * @numa_node: NUMA node this device is close to. 625 626 * @dma_mask: Dma mask (if dma'ble device). 626 627 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all ··· 674 671 core doesn't touch it */ 675 672 struct dev_pm_info power; 676 673 struct dev_pm_domain *pm_domain; 674 + 675 + #ifdef CONFIG_PINCTRL 676 + struct dev_pin_info *pins; 677 + #endif 677 678 678 679 #ifdef CONFIG_NUMA 679 680 int numa_node; /* NUMA node this device is close to */
+45
include/linux/pinctrl/devinfo.h
··· 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 */