Skip to content

Commit cb28ee3

Browse files
Linus Walleijbroonie
authored andcommitted
gpio: devres: Handle nonexclusive GPIOs
When we get a nonexeclusive GPIO descriptor using managed resources, we should only add it to the list of managed resources once: on the first user. Augment the devm_gpiod_get_index() and devm_gpiod_get_from_of_node() calls to account for this by checking if the descriptor is already resource managed before we proceed to allocate a new resource management struct. Fixes: b0ce7b2 ("regulator/gpio: Allow nonexclusive GPIO access") Reported-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Reviewed-by: Marek Szyprowski <m.szyprowski@samsung.com> Tested-by: Marek Szyprowski <m.szyprowski@samsung.com> Reviewed-by: Charles Keepax <ckeepax@opensource.cirrus.com> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent ec75700 commit cb28ee3

1 file changed

Lines changed: 38 additions & 12 deletions

File tree

‎drivers/gpio/gpiolib-devres.c‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,28 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
9898
struct gpio_desc **dr;
9999
struct gpio_desc *desc;
100100

101+
desc = gpiod_get_index(dev, con_id, idx, flags);
102+
if (IS_ERR(desc))
103+
return desc;
104+
105+
/*
106+
* For non-exclusive GPIO descriptors, check if this descriptor is
107+
* already under resource management by this device.
108+
*/
109+
if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
110+
struct devres *dres;
111+
112+
dres = devres_find(dev, devm_gpiod_release,
113+
devm_gpiod_match, &desc);
114+
if (dres)
115+
return desc;
116+
}
117+
101118
dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
102119
GFP_KERNEL);
103-
if (!dr)
120+
if (!dr) {
121+
gpiod_put(desc);
104122
return ERR_PTR(-ENOMEM);
105-
106-
desc = gpiod_get_index(dev, con_id, idx, flags);
107-
if (IS_ERR(desc)) {
108-
devres_free(dr);
109-
return desc;
110123
}
111124

112125
*dr = desc;
@@ -140,15 +153,28 @@ struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
140153
struct gpio_desc **dr;
141154
struct gpio_desc *desc;
142155

156+
desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
157+
if (IS_ERR(desc))
158+
return desc;
159+
160+
/*
161+
* For non-exclusive GPIO descriptors, check if this descriptor is
162+
* already under resource management by this device.
163+
*/
164+
if (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
165+
struct devres *dres;
166+
167+
dres = devres_find(dev, devm_gpiod_release,
168+
devm_gpiod_match, &desc);
169+
if (dres)
170+
return desc;
171+
}
172+
143173
dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
144174
GFP_KERNEL);
145-
if (!dr)
175+
if (!dr) {
176+
gpiod_put(desc);
146177
return ERR_PTR(-ENOMEM);
147-
148-
desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
149-
if (IS_ERR(desc)) {
150-
devres_free(dr);
151-
return desc;
152178
}
153179

154180
*dr = desc;

0 commit comments

Comments
 (0)