Skip to content

Commit 6d4ca1f

Browse files
Stephen WarrenLinus Walleij
authored andcommitted
pinctrl: implement devm_pinctrl_get()/put()
These functions allow the driver core to automatically clean up any allocations made by drivers, thus leading to simplified drivers. Signed-off-by: Stephen Warren <swarren@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 2aeefe0 commit 6d4ca1f

4 files changed

Lines changed: 133 additions & 19 deletions

File tree

‎Documentation/driver-model/devres.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,7 @@ REGULATOR
276276
devm_regulator_get()
277277
devm_regulator_put()
278278
devm_regulator_bulk_get()
279+
280+
PINCTRL
281+
devm_pinctrl_get()
282+
devm_pinctrl_put()

‎Documentation/pinctrl.txt‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -945,13 +945,13 @@ case), we define a mapping like this:
945945
The result of grabbing this mapping from the device with something like
946946
this (see next paragraph):
947947

948-
p = pinctrl_get(dev);
948+
p = devm_pinctrl_get(dev);
949949
s = pinctrl_lookup_state(p, "8bit");
950950
ret = pinctrl_select_state(p, s);
951951

952952
or more simply:
953953

954-
p = pinctrl_get_select(dev, "8bit");
954+
p = devm_pinctrl_get_select(dev, "8bit");
955955

956956
Will be that you activate all the three bottom records in the mapping at
957957
once. Since they share the same name, pin controller device, function and
@@ -985,32 +985,25 @@ foo_probe()
985985
/* Allocate a state holder named "foo" etc */
986986
struct foo_state *foo = ...;
987987

988-
foo->p = pinctrl_get(&device);
988+
foo->p = devm_pinctrl_get(&device);
989989
if (IS_ERR(foo->p)) {
990990
/* FIXME: clean up "foo" here */
991991
return PTR_ERR(foo->p);
992992
}
993993

994994
foo->s = pinctrl_lookup_state(foo->p, PINCTRL_STATE_DEFAULT);
995995
if (IS_ERR(foo->s)) {
996-
pinctrl_put(foo->p);
997996
/* FIXME: clean up "foo" here */
998997
return PTR_ERR(s);
999998
}
1000999

10011000
ret = pinctrl_select_state(foo->s);
10021001
if (ret < 0) {
1003-
pinctrl_put(foo->p);
10041002
/* FIXME: clean up "foo" here */
10051003
return ret;
10061004
}
10071005
}
10081006

1009-
foo_remove()
1010-
{
1011-
pinctrl_put(state->p);
1012-
}
1013-
10141007
This get/lookup/select/put sequence can just as well be handled by bus drivers
10151008
if you don't want each and every driver to handle it and you know the
10161009
arrangement on your bus.
@@ -1022,6 +1015,11 @@ The semantics of the pinctrl APIs are:
10221015
kernel memory to hold the pinmux state. All mapping table parsing or similar
10231016
slow operations take place within this API.
10241017

1018+
- devm_pinctrl_get() is a variant of pinctrl_get() that causes pinctrl_put()
1019+
to be called automatically on the retrieved pointer when the associated
1020+
device is removed. It is recommended to use this function over plain
1021+
pinctrl_get().
1022+
10251023
- pinctrl_lookup_state() is called in process context to obtain a handle to a
10261024
specific state for a the client device. This operation may be slow too.
10271025

@@ -1034,14 +1032,25 @@ The semantics of the pinctrl APIs are:
10341032

10351033
- pinctrl_put() frees all information associated with a pinctrl handle.
10361034

1035+
- devm_pinctrl_put() is a variant of pinctrl_put() that may be used to
1036+
explicitly destroy a pinctrl object returned by devm_pinctrl_get().
1037+
However, use of this function will be rare, due to the automatic cleanup
1038+
that will occur even without calling it.
1039+
1040+
pinctrl_get() must be paired with a plain pinctrl_put().
1041+
pinctrl_get() may not be paired with devm_pinctrl_put().
1042+
devm_pinctrl_get() can optionally be paired with devm_pinctrl_put().
1043+
devm_pinctrl_get() may not be paired with plain pinctrl_put().
1044+
10371045
Usually the pin control core handled the get/put pair and call out to the
10381046
device drivers bookkeeping operations, like checking available functions and
10391047
the associated pins, whereas the enable/disable pass on to the pin controller
10401048
driver which takes care of activating and/or deactivating the mux setting by
10411049
quickly poking some registers.
10421050

1043-
The pins are allocated for your device when you issue the pinctrl_get() call,
1044-
after this you should be able to see this in the debugfs listing of all pins.
1051+
The pins are allocated for your device when you issue the devm_pinctrl_get()
1052+
call, after this you should be able to see this in the debugfs listing of all
1053+
pins.
10451054

10461055
NOTE: the pinctrl system will return -EPROBE_DEFER if it cannot find the
10471056
requested pinctrl handles, for example if the pinctrl driver has not yet
@@ -1092,13 +1101,13 @@ it, disables and releases it, and muxes it in on the pins defined by group B:
10921101

10931102
#include <linux/pinctrl/consumer.h>
10941103

1095-
foo_switch()
1096-
{
1097-
struct pinctrl *p;
1098-
struct pinctrl_state *s1, *s2;
1104+
struct pinctrl *p;
1105+
struct pinctrl_state *s1, *s2;
10991106

1107+
foo_probe()
1108+
{
11001109
/* Setup */
1101-
p = pinctrl_get(&device);
1110+
p = devm_pinctrl_get(&device);
11021111
if (IS_ERR(p))
11031112
...
11041113

@@ -1109,7 +1118,10 @@ foo_switch()
11091118
s2 = pinctrl_lookup_state(foo->p, "pos-B");
11101119
if (IS_ERR(s2))
11111120
...
1121+
}
11121122

1123+
foo_switch()
1124+
{
11131125
/* Enable on position A */
11141126
ret = pinctrl_select_state(s1);
11151127
if (ret < 0)
@@ -1123,8 +1135,6 @@ foo_switch()
11231135
...
11241136

11251137
...
1126-
1127-
pinctrl_put(p);
11281138
}
11291139

11301140
The above has to be done from process context.

‎drivers/pinctrl/core.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/sysfs.h>
2424
#include <linux/debugfs.h>
2525
#include <linux/seq_file.h>
26+
#include <linux/pinctrl/consumer.h>
2627
#include <linux/pinctrl/pinctrl.h>
2728
#include <linux/pinctrl/machine.h>
2829
#include "core.h"
@@ -801,6 +802,61 @@ int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)
801802
}
802803
EXPORT_SYMBOL_GPL(pinctrl_select_state);
803804

805+
static void devm_pinctrl_release(struct device *dev, void *res)
806+
{
807+
pinctrl_put(*(struct pinctrl **)res);
808+
}
809+
810+
/**
811+
* struct devm_pinctrl_get() - Resource managed pinctrl_get()
812+
* @dev: the device to obtain the handle for
813+
*
814+
* If there is a need to explicitly destroy the returned struct pinctrl,
815+
* devm_pinctrl_put() should be used, rather than plain pinctrl_put().
816+
*/
817+
struct pinctrl *devm_pinctrl_get(struct device *dev)
818+
{
819+
struct pinctrl **ptr, *p;
820+
821+
ptr = devres_alloc(devm_pinctrl_release, sizeof(*ptr), GFP_KERNEL);
822+
if (!ptr)
823+
return ERR_PTR(-ENOMEM);
824+
825+
p = pinctrl_get(dev);
826+
if (!IS_ERR(p)) {
827+
*ptr = p;
828+
devres_add(dev, ptr);
829+
} else {
830+
devres_free(ptr);
831+
}
832+
833+
return p;
834+
}
835+
EXPORT_SYMBOL_GPL(devm_pinctrl_get);
836+
837+
static int devm_pinctrl_match(struct device *dev, void *res, void *data)
838+
{
839+
struct pinctrl **p = res;
840+
841+
return *p == data;
842+
}
843+
844+
/**
845+
* devm_pinctrl_put() - Resource managed pinctrl_put()
846+
* @p: the pinctrl handle to release
847+
*
848+
* Deallocate a struct pinctrl obtained via devm_pinctrl_get(). Normally
849+
* this function will not need to be called and the resource management
850+
* code will ensure that the resource is freed.
851+
*/
852+
void devm_pinctrl_put(struct pinctrl *p)
853+
{
854+
WARN_ON(devres_destroy(p->dev, devm_pinctrl_release,
855+
devm_pinctrl_match, p));
856+
pinctrl_put(p);
857+
}
858+
EXPORT_SYMBOL_GPL(devm_pinctrl_put);
859+
804860
int pinctrl_register_map(struct pinctrl_map const *maps, unsigned num_maps,
805861
bool dup, bool locked)
806862
{

‎include/linux/pinctrl/consumer.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ extern struct pinctrl_state * __must_check pinctrl_lookup_state(
3636
const char *name);
3737
extern int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *s);
3838

39+
extern struct pinctrl * __must_check devm_pinctrl_get(struct device *dev);
40+
extern void devm_pinctrl_put(struct pinctrl *p);
41+
3942
#else /* !CONFIG_PINCTRL */
4043

4144
static inline int pinctrl_request_gpio(unsigned gpio)
@@ -79,6 +82,15 @@ static inline int pinctrl_select_state(struct pinctrl *p,
7982
return 0;
8083
}
8184

85+
static inline struct pinctrl * __must_check devm_pinctrl_get(struct device *dev)
86+
{
87+
return NULL;
88+
}
89+
90+
static inline void devm_pinctrl_put(struct pinctrl *p)
91+
{
92+
}
93+
8294
#endif /* CONFIG_PINCTRL */
8395

8496
static inline struct pinctrl * __must_check pinctrl_get_select(
@@ -113,6 +125,38 @@ static inline struct pinctrl * __must_check pinctrl_get_select_default(
113125
return pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
114126
}
115127

128+
static inline struct pinctrl * __must_check devm_pinctrl_get_select(
129+
struct device *dev, const char *name)
130+
{
131+
struct pinctrl *p;
132+
struct pinctrl_state *s;
133+
int ret;
134+
135+
p = devm_pinctrl_get(dev);
136+
if (IS_ERR(p))
137+
return p;
138+
139+
s = pinctrl_lookup_state(p, name);
140+
if (IS_ERR(s)) {
141+
devm_pinctrl_put(p);
142+
return ERR_PTR(PTR_ERR(s));
143+
}
144+
145+
ret = pinctrl_select_state(p, s);
146+
if (ret < 0) {
147+
devm_pinctrl_put(p);
148+
return ERR_PTR(ret);
149+
}
150+
151+
return p;
152+
}
153+
154+
static inline struct pinctrl * __must_check devm_pinctrl_get_select_default(
155+
struct device *dev)
156+
{
157+
return devm_pinctrl_get_select(dev, PINCTRL_STATE_DEFAULT);
158+
}
159+
116160
#ifdef CONFIG_PINCONF
117161

118162
extern int pin_config_get(const char *dev_name, const char *name,

0 commit comments

Comments
 (0)