John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpio/devres.c - managed gpio resources |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * You should have received a copy of the GNU General Public License |
| 9 | * along with this program; if not, write to the Free Software |
| 10 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 11 | * |
| 12 | * This file is based on kernel/irq/devres.c |
| 13 | * |
John Crispin | baddc7c | 2016-12-20 19:57:55 +0100 | [diff] [blame] | 14 | * Copyright (c) 2011 John Crispin <john@phrozen.org> |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
Alexandre Courbot | 3c2c628 | 2013-10-20 15:14:58 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 19 | #include <linux/gpio.h> |
Alexandre Courbot | 3c2c628 | 2013-10-20 15:14:58 -0700 | [diff] [blame] | 20 | #include <linux/gpio/consumer.h> |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 21 | #include <linux/device.h> |
| 22 | #include <linux/gfp.h> |
| 23 | |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 24 | #include "gpiolib.h" |
| 25 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 26 | static void devm_gpiod_release(struct device *dev, void *res) |
| 27 | { |
| 28 | struct gpio_desc **desc = res; |
| 29 | |
| 30 | gpiod_put(*desc); |
| 31 | } |
| 32 | |
| 33 | static int devm_gpiod_match(struct device *dev, void *res, void *data) |
| 34 | { |
| 35 | struct gpio_desc **this = res, **gpio = data; |
| 36 | |
| 37 | return *this == *gpio; |
| 38 | } |
| 39 | |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 40 | static void devm_gpiod_release_array(struct device *dev, void *res) |
| 41 | { |
| 42 | struct gpio_descs **descs = res; |
| 43 | |
| 44 | gpiod_put_array(*descs); |
| 45 | } |
| 46 | |
| 47 | static int devm_gpiod_match_array(struct device *dev, void *res, void *data) |
| 48 | { |
| 49 | struct gpio_descs **this = res, **gpios = data; |
| 50 | |
| 51 | return *this == *gpios; |
| 52 | } |
| 53 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 54 | /** |
| 55 | * devm_gpiod_get - Resource-managed gpiod_get() |
| 56 | * @dev: GPIO consumer |
| 57 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 58 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 59 | * |
| 60 | * Managed gpiod_get(). GPIO descriptors returned from this function are |
| 61 | * automatically disposed on driver detach. See gpiod_get() for detailed |
| 62 | * information about behavior and return values. |
| 63 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 64 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 65 | const char *con_id, |
| 66 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 67 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 68 | return devm_gpiod_get_index(dev, con_id, 0, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 69 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 70 | EXPORT_SYMBOL(devm_gpiod_get); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 73 | * devm_gpiod_get_optional - Resource-managed gpiod_get_optional() |
| 74 | * @dev: GPIO consumer |
| 75 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 76 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 77 | * |
| 78 | * Managed gpiod_get_optional(). GPIO descriptors returned from this function |
| 79 | * are automatically disposed on driver detach. See gpiod_get_optional() for |
| 80 | * detailed information about behavior and return values. |
| 81 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 82 | struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 83 | const char *con_id, |
| 84 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 85 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 86 | return devm_gpiod_get_index_optional(dev, con_id, 0, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 87 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 88 | EXPORT_SYMBOL(devm_gpiod_get_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 89 | |
| 90 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 91 | * devm_gpiod_get_index - Resource-managed gpiod_get_index() |
| 92 | * @dev: GPIO consumer |
| 93 | * @con_id: function within the GPIO consumer |
| 94 | * @idx: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 95 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 96 | * |
| 97 | * Managed gpiod_get_index(). GPIO descriptors returned from this function are |
| 98 | * automatically disposed on driver detach. See gpiod_get_index() for detailed |
| 99 | * information about behavior and return values. |
| 100 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 101 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 102 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 103 | unsigned int idx, |
| 104 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 105 | { |
| 106 | struct gpio_desc **dr; |
| 107 | struct gpio_desc *desc; |
| 108 | |
Julia Lawall | 0f05a3a | 2014-07-29 17:16:48 +0200 | [diff] [blame] | 109 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 110 | GFP_KERNEL); |
| 111 | if (!dr) |
| 112 | return ERR_PTR(-ENOMEM); |
| 113 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 114 | desc = gpiod_get_index(dev, con_id, idx, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 115 | if (IS_ERR(desc)) { |
| 116 | devres_free(dr); |
| 117 | return desc; |
| 118 | } |
| 119 | |
| 120 | *dr = desc; |
| 121 | devres_add(dev, dr); |
| 122 | |
Alexandre Courbot | 5fcdb9d | 2013-10-20 15:14:57 -0700 | [diff] [blame] | 123 | return desc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 124 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 125 | EXPORT_SYMBOL(devm_gpiod_get_index); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 128 | * devm_fwnode_get_index_gpiod_from_child - get a GPIO descriptor from a |
| 129 | * device's child node |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 130 | * @dev: GPIO consumer |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 131 | * @con_id: function within the GPIO consumer |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 132 | * @index: index of the GPIO to obtain in the consumer |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 133 | * @child: firmware node (child of @dev) |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 134 | * @flags: GPIO initialization flags |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 135 | * |
| 136 | * GPIO descriptors returned from this function are automatically disposed on |
| 137 | * driver detach. |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 138 | * |
Andy Shevchenko | ff21378 | 2017-02-28 17:03:12 +0200 | [diff] [blame] | 139 | * On successful request the GPIO pin is configured in accordance with |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 140 | * provided @flags. |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 141 | */ |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 142 | struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, |
| 143 | const char *con_id, int index, |
| 144 | struct fwnode_handle *child, |
| 145 | enum gpiod_flags flags, |
| 146 | const char *label) |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 147 | { |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 148 | char prop_name[32]; /* 32 is max size of property name */ |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 149 | struct gpio_desc **dr; |
| 150 | struct gpio_desc *desc; |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 151 | unsigned int i; |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 152 | |
| 153 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
| 154 | GFP_KERNEL); |
| 155 | if (!dr) |
| 156 | return ERR_PTR(-ENOMEM); |
| 157 | |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 158 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 159 | if (con_id) |
| 160 | snprintf(prop_name, sizeof(prop_name), "%s-%s", |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 161 | con_id, gpio_suffixes[i]); |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 162 | else |
| 163 | snprintf(prop_name, sizeof(prop_name), "%s", |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 164 | gpio_suffixes[i]); |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 165 | |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 166 | desc = fwnode_get_named_gpiod(child, prop_name, index, flags, |
| 167 | label); |
Geert Uytterhoeven | 40c8eab | 2016-02-19 11:00:50 +0100 | [diff] [blame] | 168 | if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 169 | break; |
| 170 | } |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 171 | if (IS_ERR(desc)) { |
| 172 | devres_free(dr); |
| 173 | return desc; |
| 174 | } |
| 175 | |
| 176 | *dr = desc; |
| 177 | devres_add(dev, dr); |
| 178 | |
| 179 | return desc; |
| 180 | } |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 181 | EXPORT_SYMBOL(devm_fwnode_get_index_gpiod_from_child); |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 182 | |
| 183 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 184 | * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional() |
| 185 | * @dev: GPIO consumer |
| 186 | * @con_id: function within the GPIO consumer |
| 187 | * @index: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 188 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 189 | * |
| 190 | * Managed gpiod_get_index_optional(). GPIO descriptors returned from this |
| 191 | * function are automatically disposed on driver detach. See |
| 192 | * gpiod_get_index_optional() for detailed information about behavior and |
| 193 | * return values. |
| 194 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 195 | struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev, |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 196 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 197 | unsigned int index, |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 198 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 199 | { |
| 200 | struct gpio_desc *desc; |
| 201 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 202 | desc = devm_gpiod_get_index(dev, con_id, index, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 203 | if (IS_ERR(desc)) { |
| 204 | if (PTR_ERR(desc) == -ENOENT) |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | return desc; |
| 209 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 210 | EXPORT_SYMBOL(devm_gpiod_get_index_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 211 | |
| 212 | /** |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 213 | * devm_gpiod_get_array - Resource-managed gpiod_get_array() |
| 214 | * @dev: GPIO consumer |
| 215 | * @con_id: function within the GPIO consumer |
| 216 | * @flags: optional GPIO initialization flags |
| 217 | * |
| 218 | * Managed gpiod_get_array(). GPIO descriptors returned from this function are |
| 219 | * automatically disposed on driver detach. See gpiod_get_array() for detailed |
| 220 | * information about behavior and return values. |
| 221 | */ |
| 222 | struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, |
| 223 | const char *con_id, |
| 224 | enum gpiod_flags flags) |
| 225 | { |
| 226 | struct gpio_descs **dr; |
| 227 | struct gpio_descs *descs; |
| 228 | |
| 229 | dr = devres_alloc(devm_gpiod_release_array, |
| 230 | sizeof(struct gpio_descs *), GFP_KERNEL); |
| 231 | if (!dr) |
| 232 | return ERR_PTR(-ENOMEM); |
| 233 | |
| 234 | descs = gpiod_get_array(dev, con_id, flags); |
| 235 | if (IS_ERR(descs)) { |
| 236 | devres_free(dr); |
| 237 | return descs; |
| 238 | } |
| 239 | |
| 240 | *dr = descs; |
| 241 | devres_add(dev, dr); |
| 242 | |
| 243 | return descs; |
| 244 | } |
| 245 | EXPORT_SYMBOL(devm_gpiod_get_array); |
| 246 | |
| 247 | /** |
| 248 | * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional() |
| 249 | * @dev: GPIO consumer |
| 250 | * @con_id: function within the GPIO consumer |
| 251 | * @flags: optional GPIO initialization flags |
| 252 | * |
| 253 | * Managed gpiod_get_array_optional(). GPIO descriptors returned from this |
| 254 | * function are automatically disposed on driver detach. |
| 255 | * See gpiod_get_array_optional() for detailed information about behavior and |
| 256 | * return values. |
| 257 | */ |
| 258 | struct gpio_descs *__must_check |
| 259 | devm_gpiod_get_array_optional(struct device *dev, const char *con_id, |
| 260 | enum gpiod_flags flags) |
| 261 | { |
| 262 | struct gpio_descs *descs; |
| 263 | |
| 264 | descs = devm_gpiod_get_array(dev, con_id, flags); |
| 265 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) |
| 266 | return NULL; |
| 267 | |
| 268 | return descs; |
| 269 | } |
| 270 | EXPORT_SYMBOL(devm_gpiod_get_array_optional); |
| 271 | |
| 272 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 273 | * devm_gpiod_put - Resource-managed gpiod_put() |
| 274 | * @desc: GPIO descriptor to dispose of |
| 275 | * |
| 276 | * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or |
| 277 | * devm_gpiod_get_index(). Normally this function will not be called as the GPIO |
| 278 | * will be disposed of by the resource management code. |
| 279 | */ |
| 280 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 281 | { |
| 282 | WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match, |
| 283 | &desc)); |
| 284 | } |
| 285 | EXPORT_SYMBOL(devm_gpiod_put); |
| 286 | |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 287 | /** |
| 288 | * devm_gpiod_put_array - Resource-managed gpiod_put_array() |
| 289 | * @descs: GPIO descriptor array to dispose of |
| 290 | * |
| 291 | * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array(). |
| 292 | * Normally this function will not be called as the GPIOs will be disposed of |
| 293 | * by the resource management code. |
| 294 | */ |
| 295 | void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs) |
| 296 | { |
| 297 | WARN_ON(devres_release(dev, devm_gpiod_release_array, |
| 298 | devm_gpiod_match_array, &descs)); |
| 299 | } |
| 300 | EXPORT_SYMBOL(devm_gpiod_put_array); |
| 301 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 302 | |
| 303 | |
| 304 | |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 305 | static void devm_gpio_release(struct device *dev, void *res) |
| 306 | { |
| 307 | unsigned *gpio = res; |
| 308 | |
| 309 | gpio_free(*gpio); |
| 310 | } |
| 311 | |
| 312 | static int devm_gpio_match(struct device *dev, void *res, void *data) |
| 313 | { |
| 314 | unsigned *this = res, *gpio = data; |
| 315 | |
| 316 | return *this == *gpio; |
| 317 | } |
| 318 | |
| 319 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 320 | * devm_gpio_request - request a GPIO for a managed device |
| 321 | * @dev: device to request the GPIO for |
| 322 | * @gpio: GPIO to allocate |
| 323 | * @label: the name of the requested GPIO |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 324 | * |
| 325 | * Except for the extra @dev argument, this function takes the |
| 326 | * same arguments and performs the same function as |
| 327 | * gpio_request(). GPIOs requested with this function will be |
| 328 | * automatically freed on driver detach. |
| 329 | * |
| 330 | * If an GPIO allocated with this function needs to be freed |
| 331 | * separately, devm_gpio_free() must be used. |
| 332 | */ |
| 333 | |
| 334 | int devm_gpio_request(struct device *dev, unsigned gpio, const char *label) |
| 335 | { |
| 336 | unsigned *dr; |
| 337 | int rc; |
| 338 | |
| 339 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 340 | if (!dr) |
| 341 | return -ENOMEM; |
| 342 | |
| 343 | rc = gpio_request(gpio, label); |
| 344 | if (rc) { |
| 345 | devres_free(dr); |
| 346 | return rc; |
| 347 | } |
| 348 | |
| 349 | *dr = gpio; |
| 350 | devres_add(dev, dr); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | EXPORT_SYMBOL(devm_gpio_request); |
| 355 | |
| 356 | /** |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 357 | * devm_gpio_request_one - request a single GPIO with initial setup |
| 358 | * @dev: device to request for |
| 359 | * @gpio: the GPIO number |
| 360 | * @flags: GPIO configuration as specified by GPIOF_* |
| 361 | * @label: a literal description string of this GPIO |
| 362 | */ |
| 363 | int devm_gpio_request_one(struct device *dev, unsigned gpio, |
| 364 | unsigned long flags, const char *label) |
| 365 | { |
| 366 | unsigned *dr; |
| 367 | int rc; |
| 368 | |
| 369 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 370 | if (!dr) |
| 371 | return -ENOMEM; |
| 372 | |
| 373 | rc = gpio_request_one(gpio, flags, label); |
| 374 | if (rc) { |
| 375 | devres_free(dr); |
| 376 | return rc; |
| 377 | } |
| 378 | |
| 379 | *dr = gpio; |
| 380 | devres_add(dev, dr); |
| 381 | |
| 382 | return 0; |
| 383 | } |
Stephen Warren | 3996bfc | 2012-06-07 17:56:09 -0600 | [diff] [blame] | 384 | EXPORT_SYMBOL(devm_gpio_request_one); |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 385 | |
| 386 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 387 | * devm_gpio_free - free a GPIO |
| 388 | * @dev: device to free GPIO for |
| 389 | * @gpio: GPIO to free |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 390 | * |
| 391 | * Except for the extra @dev argument, this function takes the |
| 392 | * same arguments and performs the same function as gpio_free(). |
| 393 | * This function instead of gpio_free() should be used to manually |
| 394 | * free GPIOs allocated with devm_gpio_request(). |
| 395 | */ |
| 396 | void devm_gpio_free(struct device *dev, unsigned int gpio) |
| 397 | { |
| 398 | |
Mark Brown | a85990b | 2012-05-03 18:15:14 +0100 | [diff] [blame] | 399 | WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match, |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 400 | &gpio)); |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 401 | } |
| 402 | EXPORT_SYMBOL(devm_gpio_free); |