Skip to content

Commit 891ddbc

Browse files
Linus Walleijbroonie
authored andcommitted
gpio: Add devm_gpiod_unhinge()
This adds a function named devm_gpiod_unhinge() that removes the resource management from a GPIO descriptor. I am not sure if this is the best anglosaxon name for the function, no other managed resources have an equivalent currently, but I chose "unhinge" as the closest intuitive thing I could imagine that fits Rusty Russell's API design criterions "the obvious use is the correct one" and "the name tells you how to use it". The idea came out of a remark from Mark Brown that it should be possible to handle over management of a resource from devres to the regulator core, and indeed we can do that. 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 cb28ee3 commit 891ddbc

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎Documentation/driver-model/devres.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ GPIO
254254
devm_gpiod_get_index_optional()
255255
devm_gpiod_get_optional()
256256
devm_gpiod_put()
257+
devm_gpiod_unhinge()
257258
devm_gpiochip_add_data()
258259
devm_gpiochip_remove()
259260
devm_gpio_request()

‎drivers/gpio/gpiolib-devres.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,36 @@ void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
346346
}
347347
EXPORT_SYMBOL(devm_gpiod_put);
348348

349+
/**
350+
* devm_gpiod_unhinge - Remove resource management from a gpio descriptor
351+
* @dev: GPIO consumer
352+
* @desc: GPIO descriptor to remove resource management from
353+
*
354+
* Remove resource management from a GPIO descriptor. This is needed when
355+
* you want to hand over lifecycle management of a descriptor to another
356+
* mechanism.
357+
*/
358+
359+
void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc)
360+
{
361+
int ret;
362+
363+
if (IS_ERR_OR_NULL(desc))
364+
return;
365+
ret = devres_destroy(dev, devm_gpiod_release,
366+
devm_gpiod_match, &desc);
367+
/*
368+
* If the GPIO descriptor is requested as nonexclusive, we
369+
* may call this function several times on the same descriptor
370+
* so it is OK if devres_destroy() returns -ENOENT.
371+
*/
372+
if (ret == -ENOENT)
373+
return;
374+
/* Anything else we should warn about */
375+
WARN_ON(ret);
376+
}
377+
EXPORT_SYMBOL(devm_gpiod_unhinge);
378+
349379
/**
350380
* devm_gpiod_put_array - Resource-managed gpiod_put_array()
351381
* @dev: GPIO consumer

‎include/linux/gpio/consumer.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct gpio_descs *__must_check
104104
devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
105105
enum gpiod_flags flags);
106106
void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
107+
void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc);
107108
void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs);
108109

109110
int gpiod_get_direction(struct gpio_desc *desc);
@@ -249,6 +250,15 @@ static inline void gpiod_put(struct gpio_desc *desc)
249250
WARN_ON(1);
250251
}
251252

253+
static inline void devm_gpiod_unhinge(struct device *dev,
254+
struct gpio_desc *desc)
255+
{
256+
might_sleep();
257+
258+
/* GPIO can never have been requested */
259+
WARN_ON(1);
260+
}
261+
252262
static inline void gpiod_put_array(struct gpio_descs *descs)
253263
{
254264
might_sleep();

0 commit comments

Comments
 (0)