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 | * |
| 14 | * Copyright (c) 2011 John Crispin <blogic@openwrt.org> |
| 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 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 24 | static void devm_gpiod_release(struct device *dev, void *res) |
| 25 | { |
| 26 | struct gpio_desc **desc = res; |
| 27 | |
| 28 | gpiod_put(*desc); |
| 29 | } |
| 30 | |
| 31 | static int devm_gpiod_match(struct device *dev, void *res, void *data) |
| 32 | { |
| 33 | struct gpio_desc **this = res, **gpio = data; |
| 34 | |
| 35 | return *this == *gpio; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * devm_gpiod_get - Resource-managed gpiod_get() |
| 40 | * @dev: GPIO consumer |
| 41 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 42 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 43 | * |
| 44 | * Managed gpiod_get(). GPIO descriptors returned from this function are |
| 45 | * automatically disposed on driver detach. See gpiod_get() for detailed |
| 46 | * information about behavior and return values. |
| 47 | */ |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 48 | struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev, |
| 49 | const char *con_id, |
| 50 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 51 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 52 | return devm_gpiod_get_index(dev, con_id, 0, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 53 | } |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 54 | EXPORT_SYMBOL(__devm_gpiod_get); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 57 | * devm_gpiod_get_optional - Resource-managed gpiod_get_optional() |
| 58 | * @dev: GPIO consumer |
| 59 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 60 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 61 | * |
| 62 | * Managed gpiod_get_optional(). GPIO descriptors returned from this function |
| 63 | * are automatically disposed on driver detach. See gpiod_get_optional() for |
| 64 | * detailed information about behavior and return values. |
| 65 | */ |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 66 | struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev, |
| 67 | const char *con_id, |
| 68 | enum gpiod_flags flags) |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 69 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 70 | return devm_gpiod_get_index_optional(dev, con_id, 0, flags); |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 71 | } |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 72 | EXPORT_SYMBOL(__devm_gpiod_get_optional); |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 75 | * devm_gpiod_get_index - Resource-managed gpiod_get_index() |
| 76 | * @dev: GPIO consumer |
| 77 | * @con_id: function within the GPIO consumer |
| 78 | * @idx: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 79 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 80 | * |
| 81 | * Managed gpiod_get_index(). GPIO descriptors returned from this function are |
| 82 | * automatically disposed on driver detach. See gpiod_get_index() for detailed |
| 83 | * information about behavior and return values. |
| 84 | */ |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 85 | struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 86 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 87 | unsigned int idx, |
| 88 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 89 | { |
| 90 | struct gpio_desc **dr; |
| 91 | struct gpio_desc *desc; |
| 92 | |
Julia Lawall | 0f05a3a | 2014-07-29 17:16:48 +0200 | [diff] [blame] | 93 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 94 | GFP_KERNEL); |
| 95 | if (!dr) |
| 96 | return ERR_PTR(-ENOMEM); |
| 97 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 98 | desc = gpiod_get_index(dev, con_id, idx, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 99 | if (IS_ERR(desc)) { |
| 100 | devres_free(dr); |
| 101 | return desc; |
| 102 | } |
| 103 | |
| 104 | *dr = desc; |
| 105 | devres_add(dev, dr); |
| 106 | |
Alexandre Courbot | 5fcdb9d | 2013-10-20 15:14:57 -0700 | [diff] [blame] | 107 | return desc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 108 | } |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 109 | EXPORT_SYMBOL(__devm_gpiod_get_index); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 112 | * devm_get_gpiod_from_child - get a GPIO descriptor from a device's child node |
| 113 | * @dev: GPIO consumer |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame^] | 114 | * @con_id: function within the GPIO consumer |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 115 | * @child: firmware node (child of @dev) |
| 116 | * |
| 117 | * GPIO descriptors returned from this function are automatically disposed on |
| 118 | * driver detach. |
| 119 | */ |
| 120 | struct gpio_desc *devm_get_gpiod_from_child(struct device *dev, |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame^] | 121 | const char *con_id, |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 122 | struct fwnode_handle *child) |
| 123 | { |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame^] | 124 | static const char const *suffixes[] = { "gpios", "gpio" }; |
| 125 | char prop_name[32]; /* 32 is max size of property name */ |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 126 | struct gpio_desc **dr; |
| 127 | struct gpio_desc *desc; |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame^] | 128 | unsigned int i; |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 129 | |
| 130 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
| 131 | GFP_KERNEL); |
| 132 | if (!dr) |
| 133 | return ERR_PTR(-ENOMEM); |
| 134 | |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame^] | 135 | for (i = 0; i < ARRAY_SIZE(suffixes); i++) { |
| 136 | if (con_id) |
| 137 | snprintf(prop_name, sizeof(prop_name), "%s-%s", |
| 138 | con_id, suffixes[i]); |
| 139 | else |
| 140 | snprintf(prop_name, sizeof(prop_name), "%s", |
| 141 | suffixes[i]); |
| 142 | |
| 143 | desc = fwnode_get_named_gpiod(child, prop_name); |
| 144 | if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER)) |
| 145 | break; |
| 146 | } |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 147 | if (IS_ERR(desc)) { |
| 148 | devres_free(dr); |
| 149 | return desc; |
| 150 | } |
| 151 | |
| 152 | *dr = desc; |
| 153 | devres_add(dev, dr); |
| 154 | |
| 155 | return desc; |
| 156 | } |
| 157 | EXPORT_SYMBOL(devm_get_gpiod_from_child); |
| 158 | |
| 159 | /** |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 160 | * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional() |
| 161 | * @dev: GPIO consumer |
| 162 | * @con_id: function within the GPIO consumer |
| 163 | * @index: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 164 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 165 | * |
| 166 | * Managed gpiod_get_index_optional(). GPIO descriptors returned from this |
| 167 | * function are automatically disposed on driver detach. See |
| 168 | * gpiod_get_index_optional() for detailed information about behavior and |
| 169 | * return values. |
| 170 | */ |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 171 | struct gpio_desc *__must_check __devm_gpiod_get_index_optional(struct device *dev, |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 172 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 173 | unsigned int index, |
| 174 | enum gpiod_flags flags) |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 175 | { |
| 176 | struct gpio_desc *desc; |
| 177 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 178 | desc = devm_gpiod_get_index(dev, con_id, index, flags); |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 179 | if (IS_ERR(desc)) { |
| 180 | if (PTR_ERR(desc) == -ENOENT) |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | return desc; |
| 185 | } |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 186 | EXPORT_SYMBOL(__devm_gpiod_get_index_optional); |
Thierry Reding | 29a1f23 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 187 | |
| 188 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 189 | * devm_gpiod_put - Resource-managed gpiod_put() |
| 190 | * @desc: GPIO descriptor to dispose of |
| 191 | * |
| 192 | * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or |
| 193 | * devm_gpiod_get_index(). Normally this function will not be called as the GPIO |
| 194 | * will be disposed of by the resource management code. |
| 195 | */ |
| 196 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 197 | { |
| 198 | WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match, |
| 199 | &desc)); |
| 200 | } |
| 201 | EXPORT_SYMBOL(devm_gpiod_put); |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 206 | static void devm_gpio_release(struct device *dev, void *res) |
| 207 | { |
| 208 | unsigned *gpio = res; |
| 209 | |
| 210 | gpio_free(*gpio); |
| 211 | } |
| 212 | |
| 213 | static int devm_gpio_match(struct device *dev, void *res, void *data) |
| 214 | { |
| 215 | unsigned *this = res, *gpio = data; |
| 216 | |
| 217 | return *this == *gpio; |
| 218 | } |
| 219 | |
| 220 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 221 | * devm_gpio_request - request a GPIO for a managed device |
| 222 | * @dev: device to request the GPIO for |
| 223 | * @gpio: GPIO to allocate |
| 224 | * @label: the name of the requested GPIO |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 225 | * |
| 226 | * Except for the extra @dev argument, this function takes the |
| 227 | * same arguments and performs the same function as |
| 228 | * gpio_request(). GPIOs requested with this function will be |
| 229 | * automatically freed on driver detach. |
| 230 | * |
| 231 | * If an GPIO allocated with this function needs to be freed |
| 232 | * separately, devm_gpio_free() must be used. |
| 233 | */ |
| 234 | |
| 235 | int devm_gpio_request(struct device *dev, unsigned gpio, const char *label) |
| 236 | { |
| 237 | unsigned *dr; |
| 238 | int rc; |
| 239 | |
| 240 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 241 | if (!dr) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | rc = gpio_request(gpio, label); |
| 245 | if (rc) { |
| 246 | devres_free(dr); |
| 247 | return rc; |
| 248 | } |
| 249 | |
| 250 | *dr = gpio; |
| 251 | devres_add(dev, dr); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | EXPORT_SYMBOL(devm_gpio_request); |
| 256 | |
| 257 | /** |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 258 | * devm_gpio_request_one - request a single GPIO with initial setup |
| 259 | * @dev: device to request for |
| 260 | * @gpio: the GPIO number |
| 261 | * @flags: GPIO configuration as specified by GPIOF_* |
| 262 | * @label: a literal description string of this GPIO |
| 263 | */ |
| 264 | int devm_gpio_request_one(struct device *dev, unsigned gpio, |
| 265 | unsigned long flags, const char *label) |
| 266 | { |
| 267 | unsigned *dr; |
| 268 | int rc; |
| 269 | |
| 270 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 271 | if (!dr) |
| 272 | return -ENOMEM; |
| 273 | |
| 274 | rc = gpio_request_one(gpio, flags, label); |
| 275 | if (rc) { |
| 276 | devres_free(dr); |
| 277 | return rc; |
| 278 | } |
| 279 | |
| 280 | *dr = gpio; |
| 281 | devres_add(dev, dr); |
| 282 | |
| 283 | return 0; |
| 284 | } |
Stephen Warren | 3996bfc | 2012-06-07 17:56:09 -0600 | [diff] [blame] | 285 | EXPORT_SYMBOL(devm_gpio_request_one); |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 286 | |
| 287 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 288 | * devm_gpio_free - free a GPIO |
| 289 | * @dev: device to free GPIO for |
| 290 | * @gpio: GPIO to free |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 291 | * |
| 292 | * Except for the extra @dev argument, this function takes the |
| 293 | * same arguments and performs the same function as gpio_free(). |
| 294 | * This function instead of gpio_free() should be used to manually |
| 295 | * free GPIOs allocated with devm_gpio_request(). |
| 296 | */ |
| 297 | void devm_gpio_free(struct device *dev, unsigned int gpio) |
| 298 | { |
| 299 | |
Mark Brown | a85990b | 2012-05-03 18:15:14 +0100 | [diff] [blame] | 300 | WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match, |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 301 | &gpio)); |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 302 | } |
| 303 | EXPORT_SYMBOL(devm_gpio_free); |