diff options
author | Alexandre Courbot <acourbot@nvidia.com> | 2013-10-17 13:21:38 -0400 |
---|---|---|
committer | Linus Walleij <linus.walleij@linaro.org> | 2013-10-19 17:24:56 -0400 |
commit | bae48da237fcedd7ad09569025483b988635efb7 (patch) | |
tree | 51d82d78acc5e248623dd73d9e5deb2ce4f0e869 /drivers/gpio/devres.c | |
parent | af8b6375a8291fe2cf77707f3edec86b98a999cc (diff) |
gpiolib: add gpiod_get() and gpiod_put() functions
Add gpiod_get(), gpiod_get_index() and gpiod_put() functions that
provide safer management of GPIOs.
These functions put the GPIO framework in line with the conventions of
other frameworks in the kernel, and help ensure every GPIO is declared
properly and valid while it is used.
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/gpio/devres.c')
-rw-r--r-- | drivers/gpio/devres.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/drivers/gpio/devres.c b/drivers/gpio/devres.c index 3e7812f0405e..2caa2571734e 100644 --- a/drivers/gpio/devres.c +++ b/drivers/gpio/devres.c | |||
@@ -19,6 +19,89 @@ | |||
19 | #include <linux/device.h> | 19 | #include <linux/device.h> |
20 | #include <linux/gfp.h> | 20 | #include <linux/gfp.h> |
21 | 21 | ||
22 | static void devm_gpiod_release(struct device *dev, void *res) | ||
23 | { | ||
24 | struct gpio_desc **desc = res; | ||
25 | |||
26 | gpiod_put(*desc); | ||
27 | } | ||
28 | |||
29 | static int devm_gpiod_match(struct device *dev, void *res, void *data) | ||
30 | { | ||
31 | struct gpio_desc **this = res, **gpio = data; | ||
32 | |||
33 | return *this == *gpio; | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * devm_gpiod_get - Resource-managed gpiod_get() | ||
38 | * @dev: GPIO consumer | ||
39 | * @con_id: function within the GPIO consumer | ||
40 | * | ||
41 | * Managed gpiod_get(). GPIO descriptors returned from this function are | ||
42 | * automatically disposed on driver detach. See gpiod_get() for detailed | ||
43 | * information about behavior and return values. | ||
44 | */ | ||
45 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, | ||
46 | const char *con_id) | ||
47 | { | ||
48 | return devm_gpiod_get_index(dev, con_id, 0); | ||
49 | } | ||
50 | EXPORT_SYMBOL(devm_gpiod_get); | ||
51 | |||
52 | /** | ||
53 | * devm_gpiod_get_index - Resource-managed gpiod_get_index() | ||
54 | * @dev: GPIO consumer | ||
55 | * @con_id: function within the GPIO consumer | ||
56 | * @idx: index of the GPIO to obtain in the consumer | ||
57 | * | ||
58 | * Managed gpiod_get_index(). GPIO descriptors returned from this function are | ||
59 | * automatically disposed on driver detach. See gpiod_get_index() for detailed | ||
60 | * information about behavior and return values. | ||
61 | */ | ||
62 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, | ||
63 | const char *con_id, | ||
64 | unsigned int idx) | ||
65 | { | ||
66 | struct gpio_desc **dr; | ||
67 | struct gpio_desc *desc; | ||
68 | |||
69 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpiod_desc *), | ||
70 | GFP_KERNEL); | ||
71 | if (!dr) | ||
72 | return ERR_PTR(-ENOMEM); | ||
73 | |||
74 | desc = gpiod_get_index(dev, con_id, idx); | ||
75 | if (IS_ERR(desc)) { | ||
76 | devres_free(dr); | ||
77 | return desc; | ||
78 | } | ||
79 | |||
80 | *dr = desc; | ||
81 | devres_add(dev, dr); | ||
82 | |||
83 | return 0; | ||
84 | } | ||
85 | EXPORT_SYMBOL(devm_gpiod_get_index); | ||
86 | |||
87 | /** | ||
88 | * devm_gpiod_put - Resource-managed gpiod_put() | ||
89 | * @desc: GPIO descriptor to dispose of | ||
90 | * | ||
91 | * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or | ||
92 | * devm_gpiod_get_index(). Normally this function will not be called as the GPIO | ||
93 | * will be disposed of by the resource management code. | ||
94 | */ | ||
95 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) | ||
96 | { | ||
97 | WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match, | ||
98 | &desc)); | ||
99 | } | ||
100 | EXPORT_SYMBOL(devm_gpiod_put); | ||
101 | |||
102 | |||
103 | |||
104 | |||
22 | static void devm_gpio_release(struct device *dev, void *res) | 105 | static void devm_gpio_release(struct device *dev, void *res) |
23 | { | 106 | { |
24 | unsigned *gpio = res; | 107 | unsigned *gpio = res; |