Linus Walleij | dae5f0a | 2018-09-25 09:08:48 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 2 | /* |
Linus Walleij | dae5f0a | 2018-09-25 09:08:48 +0200 | [diff] [blame] | 3 | * devres.c - managed gpio resources |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 4 | * This file is based on kernel/irq/devres.c |
| 5 | * |
John Crispin | baddc7c | 2016-12-20 19:57:55 +0100 | [diff] [blame] | 6 | * Copyright (c) 2011 John Crispin <john@phrozen.org> |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
Alexandre Courbot | 3c2c628 | 2013-10-20 15:14:58 -0700 | [diff] [blame] | 10 | #include <linux/err.h> |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 11 | #include <linux/gpio.h> |
Alexandre Courbot | 3c2c628 | 2013-10-20 15:14:58 -0700 | [diff] [blame] | 12 | #include <linux/gpio/consumer.h> |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/gfp.h> |
| 15 | |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 16 | #include "gpiolib.h" |
| 17 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 18 | static void devm_gpiod_release(struct device *dev, void *res) |
| 19 | { |
| 20 | struct gpio_desc **desc = res; |
| 21 | |
| 22 | gpiod_put(*desc); |
| 23 | } |
| 24 | |
| 25 | static int devm_gpiod_match(struct device *dev, void *res, void *data) |
| 26 | { |
| 27 | struct gpio_desc **this = res, **gpio = data; |
| 28 | |
| 29 | return *this == *gpio; |
| 30 | } |
| 31 | |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 32 | static void devm_gpiod_release_array(struct device *dev, void *res) |
| 33 | { |
| 34 | struct gpio_descs **descs = res; |
| 35 | |
| 36 | gpiod_put_array(*descs); |
| 37 | } |
| 38 | |
| 39 | static int devm_gpiod_match_array(struct device *dev, void *res, void *data) |
| 40 | { |
| 41 | struct gpio_descs **this = res, **gpios = data; |
| 42 | |
| 43 | return *this == *gpios; |
| 44 | } |
| 45 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 46 | /** |
| 47 | * devm_gpiod_get - Resource-managed gpiod_get() |
| 48 | * @dev: GPIO consumer |
| 49 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 50 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 51 | * |
| 52 | * Managed gpiod_get(). GPIO descriptors returned from this function are |
| 53 | * automatically disposed on driver detach. See gpiod_get() for detailed |
| 54 | * information about behavior and return values. |
| 55 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 56 | struct gpio_desc *__must_check devm_gpiod_get(struct device *dev, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 57 | const char *con_id, |
| 58 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 59 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 60 | return devm_gpiod_get_index(dev, con_id, 0, flags); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 61 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 62 | EXPORT_SYMBOL(devm_gpiod_get); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 65 | * devm_gpiod_get_optional - Resource-managed gpiod_get_optional() |
| 66 | * @dev: GPIO consumer |
| 67 | * @con_id: function within the GPIO consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 68 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 69 | * |
| 70 | * Managed gpiod_get_optional(). GPIO descriptors returned from this function |
| 71 | * are automatically disposed on driver detach. See gpiod_get_optional() for |
| 72 | * detailed information about behavior and return values. |
| 73 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 74 | struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 75 | const char *con_id, |
| 76 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 77 | { |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 78 | return devm_gpiod_get_index_optional(dev, con_id, 0, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 79 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 80 | EXPORT_SYMBOL(devm_gpiod_get_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 81 | |
| 82 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 83 | * devm_gpiod_get_index - Resource-managed gpiod_get_index() |
| 84 | * @dev: GPIO consumer |
| 85 | * @con_id: function within the GPIO consumer |
| 86 | * @idx: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 87 | * @flags: optional GPIO initialization flags |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 88 | * |
| 89 | * Managed gpiod_get_index(). GPIO descriptors returned from this function are |
| 90 | * automatically disposed on driver detach. See gpiod_get_index() for detailed |
| 91 | * information about behavior and return values. |
| 92 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 93 | struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 94 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 95 | unsigned int idx, |
| 96 | enum gpiod_flags flags) |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 97 | { |
| 98 | struct gpio_desc **dr; |
| 99 | struct gpio_desc *desc; |
| 100 | |
Linus Walleij | cb28ee3 | 2018-12-06 13:43:45 +0100 | [diff] [blame] | 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 | |
Julia Lawall | 0f05a3a | 2014-07-29 17:16:48 +0200 | [diff] [blame] | 118 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 119 | GFP_KERNEL); |
Linus Walleij | cb28ee3 | 2018-12-06 13:43:45 +0100 | [diff] [blame] | 120 | if (!dr) { |
| 121 | gpiod_put(desc); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 122 | return ERR_PTR(-ENOMEM); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | *dr = desc; |
| 126 | devres_add(dev, dr); |
| 127 | |
Alexandre Courbot | 5fcdb9d | 2013-10-20 15:14:57 -0700 | [diff] [blame] | 128 | return desc; |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 129 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 130 | EXPORT_SYMBOL(devm_gpiod_get_index); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
Linus Walleij | 92542ed | 2017-12-29 22:52:02 +0100 | [diff] [blame] | 133 | * devm_gpiod_get_from_of_node() - obtain a GPIO from an OF node |
| 134 | * @dev: device for lifecycle management |
| 135 | * @node: handle of the OF node |
| 136 | * @propname: name of the DT property representing the GPIO |
| 137 | * @index: index of the GPIO to obtain for the consumer |
| 138 | * @dflags: GPIO initialization flags |
| 139 | * @label: label to attach to the requested GPIO |
| 140 | * |
| 141 | * Returns: |
| 142 | * On successful request the GPIO pin is configured in accordance with |
| 143 | * provided @dflags. |
| 144 | * |
| 145 | * In case of error an ERR_PTR() is returned. |
| 146 | */ |
| 147 | struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, |
| 148 | struct device_node *node, |
| 149 | const char *propname, int index, |
| 150 | enum gpiod_flags dflags, |
| 151 | const char *label) |
| 152 | { |
| 153 | struct gpio_desc **dr; |
| 154 | struct gpio_desc *desc; |
| 155 | |
Linus Walleij | cb28ee3 | 2018-12-06 13:43:45 +0100 | [diff] [blame] | 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 | |
Linus Walleij | 92542ed | 2017-12-29 22:52:02 +0100 | [diff] [blame] | 173 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
| 174 | GFP_KERNEL); |
Linus Walleij | cb28ee3 | 2018-12-06 13:43:45 +0100 | [diff] [blame] | 175 | if (!dr) { |
| 176 | gpiod_put(desc); |
Linus Walleij | 92542ed | 2017-12-29 22:52:02 +0100 | [diff] [blame] | 177 | return ERR_PTR(-ENOMEM); |
Linus Walleij | 92542ed | 2017-12-29 22:52:02 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | *dr = desc; |
| 181 | devres_add(dev, dr); |
| 182 | |
| 183 | return desc; |
| 184 | } |
| 185 | EXPORT_SYMBOL(devm_gpiod_get_from_of_node); |
| 186 | |
| 187 | /** |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 188 | * devm_fwnode_get_index_gpiod_from_child - get a GPIO descriptor from a |
| 189 | * device's child node |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 190 | * @dev: GPIO consumer |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 191 | * @con_id: function within the GPIO consumer |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 192 | * @index: index of the GPIO to obtain in the consumer |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 193 | * @child: firmware node (child of @dev) |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 194 | * @flags: GPIO initialization flags |
Thierry Reding | 2b7c809 | 2017-07-24 16:57:24 +0200 | [diff] [blame] | 195 | * @label: label to attach to the requested GPIO |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 196 | * |
| 197 | * GPIO descriptors returned from this function are automatically disposed on |
| 198 | * driver detach. |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 199 | * |
Andy Shevchenko | ff21378 | 2017-02-28 17:03:12 +0200 | [diff] [blame] | 200 | * On successful request the GPIO pin is configured in accordance with |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 201 | * provided @flags. |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 202 | */ |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 203 | struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev, |
| 204 | const char *con_id, int index, |
| 205 | struct fwnode_handle *child, |
| 206 | enum gpiod_flags flags, |
| 207 | const char *label) |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 208 | { |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 209 | char prop_name[32]; /* 32 is max size of property name */ |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 210 | struct gpio_desc **dr; |
| 211 | struct gpio_desc *desc; |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 212 | unsigned int i; |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 213 | |
| 214 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
| 215 | GFP_KERNEL); |
| 216 | if (!dr) |
| 217 | return ERR_PTR(-ENOMEM); |
| 218 | |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 219 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 220 | if (con_id) |
| 221 | snprintf(prop_name, sizeof(prop_name), "%s-%s", |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 222 | con_id, gpio_suffixes[i]); |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 223 | else |
| 224 | snprintf(prop_name, sizeof(prop_name), "%s", |
Andy Shevchenko | 200d017 | 2017-01-03 19:00:20 +0200 | [diff] [blame] | 225 | gpio_suffixes[i]); |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 226 | |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 227 | desc = fwnode_get_named_gpiod(child, prop_name, index, flags, |
| 228 | label); |
Geert Uytterhoeven | 40c8eab | 2016-02-19 11:00:50 +0100 | [diff] [blame] | 229 | if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) |
Olliver Schinagl | 1feb57a | 2015-01-21 22:33:46 +0100 | [diff] [blame] | 230 | break; |
| 231 | } |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 232 | if (IS_ERR(desc)) { |
| 233 | devres_free(dr); |
| 234 | return desc; |
| 235 | } |
| 236 | |
| 237 | *dr = desc; |
| 238 | devres_add(dev, dr); |
| 239 | |
| 240 | return desc; |
| 241 | } |
Boris Brezillon | 537b94d | 2017-02-02 14:53:11 +0100 | [diff] [blame] | 242 | EXPORT_SYMBOL(devm_fwnode_get_index_gpiod_from_child); |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 243 | |
| 244 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 245 | * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional() |
| 246 | * @dev: GPIO consumer |
| 247 | * @con_id: function within the GPIO consumer |
| 248 | * @index: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 249 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 250 | * |
| 251 | * Managed gpiod_get_index_optional(). GPIO descriptors returned from this |
| 252 | * function are automatically disposed on driver detach. See |
| 253 | * gpiod_get_index_optional() for detailed information about behavior and |
| 254 | * return values. |
| 255 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 256 | 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] | 257 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 258 | unsigned int index, |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 259 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 260 | { |
| 261 | struct gpio_desc *desc; |
| 262 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 263 | desc = devm_gpiod_get_index(dev, con_id, index, flags); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 264 | if (IS_ERR(desc)) { |
| 265 | if (PTR_ERR(desc) == -ENOENT) |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | return desc; |
| 270 | } |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 271 | EXPORT_SYMBOL(devm_gpiod_get_index_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 272 | |
| 273 | /** |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 274 | * devm_gpiod_get_array - Resource-managed gpiod_get_array() |
| 275 | * @dev: GPIO consumer |
| 276 | * @con_id: function within the GPIO consumer |
| 277 | * @flags: optional GPIO initialization flags |
| 278 | * |
| 279 | * Managed gpiod_get_array(). GPIO descriptors returned from this function are |
| 280 | * automatically disposed on driver detach. See gpiod_get_array() for detailed |
| 281 | * information about behavior and return values. |
| 282 | */ |
| 283 | struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, |
| 284 | const char *con_id, |
| 285 | enum gpiod_flags flags) |
| 286 | { |
| 287 | struct gpio_descs **dr; |
| 288 | struct gpio_descs *descs; |
| 289 | |
| 290 | dr = devres_alloc(devm_gpiod_release_array, |
| 291 | sizeof(struct gpio_descs *), GFP_KERNEL); |
| 292 | if (!dr) |
| 293 | return ERR_PTR(-ENOMEM); |
| 294 | |
| 295 | descs = gpiod_get_array(dev, con_id, flags); |
| 296 | if (IS_ERR(descs)) { |
| 297 | devres_free(dr); |
| 298 | return descs; |
| 299 | } |
| 300 | |
| 301 | *dr = descs; |
| 302 | devres_add(dev, dr); |
| 303 | |
| 304 | return descs; |
| 305 | } |
| 306 | EXPORT_SYMBOL(devm_gpiod_get_array); |
| 307 | |
| 308 | /** |
| 309 | * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional() |
| 310 | * @dev: GPIO consumer |
| 311 | * @con_id: function within the GPIO consumer |
| 312 | * @flags: optional GPIO initialization flags |
| 313 | * |
| 314 | * Managed gpiod_get_array_optional(). GPIO descriptors returned from this |
| 315 | * function are automatically disposed on driver detach. |
| 316 | * See gpiod_get_array_optional() for detailed information about behavior and |
| 317 | * return values. |
| 318 | */ |
| 319 | struct gpio_descs *__must_check |
| 320 | devm_gpiod_get_array_optional(struct device *dev, const char *con_id, |
| 321 | enum gpiod_flags flags) |
| 322 | { |
| 323 | struct gpio_descs *descs; |
| 324 | |
| 325 | descs = devm_gpiod_get_array(dev, con_id, flags); |
| 326 | if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT)) |
| 327 | return NULL; |
| 328 | |
| 329 | return descs; |
| 330 | } |
| 331 | EXPORT_SYMBOL(devm_gpiod_get_array_optional); |
| 332 | |
| 333 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 334 | * devm_gpiod_put - Resource-managed gpiod_put() |
Thierry Reding | 2b7c809 | 2017-07-24 16:57:24 +0200 | [diff] [blame] | 335 | * @dev: GPIO consumer |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 336 | * @desc: GPIO descriptor to dispose of |
| 337 | * |
| 338 | * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or |
| 339 | * devm_gpiod_get_index(). Normally this function will not be called as the GPIO |
| 340 | * will be disposed of by the resource management code. |
| 341 | */ |
| 342 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 343 | { |
| 344 | WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match, |
| 345 | &desc)); |
| 346 | } |
| 347 | EXPORT_SYMBOL(devm_gpiod_put); |
| 348 | |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 349 | /** |
Linus Walleij | 891ddbc | 2018-12-06 13:43:46 +0100 | [diff] [blame] | 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 | |
| 379 | /** |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 380 | * devm_gpiod_put_array - Resource-managed gpiod_put_array() |
Thierry Reding | 2b7c809 | 2017-07-24 16:57:24 +0200 | [diff] [blame] | 381 | * @dev: GPIO consumer |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 382 | * @descs: GPIO descriptor array to dispose of |
| 383 | * |
| 384 | * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array(). |
| 385 | * Normally this function will not be called as the GPIOs will be disposed of |
| 386 | * by the resource management code. |
| 387 | */ |
| 388 | void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs) |
| 389 | { |
| 390 | WARN_ON(devres_release(dev, devm_gpiod_release_array, |
| 391 | devm_gpiod_match_array, &descs)); |
| 392 | } |
| 393 | EXPORT_SYMBOL(devm_gpiod_put_array); |
| 394 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 395 | |
| 396 | |
| 397 | |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 398 | static void devm_gpio_release(struct device *dev, void *res) |
| 399 | { |
| 400 | unsigned *gpio = res; |
| 401 | |
| 402 | gpio_free(*gpio); |
| 403 | } |
| 404 | |
| 405 | static int devm_gpio_match(struct device *dev, void *res, void *data) |
| 406 | { |
| 407 | unsigned *this = res, *gpio = data; |
| 408 | |
| 409 | return *this == *gpio; |
| 410 | } |
| 411 | |
| 412 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 413 | * devm_gpio_request - request a GPIO for a managed device |
| 414 | * @dev: device to request the GPIO for |
| 415 | * @gpio: GPIO to allocate |
| 416 | * @label: the name of the requested GPIO |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 417 | * |
| 418 | * Except for the extra @dev argument, this function takes the |
| 419 | * same arguments and performs the same function as |
| 420 | * gpio_request(). GPIOs requested with this function will be |
| 421 | * automatically freed on driver detach. |
| 422 | * |
| 423 | * If an GPIO allocated with this function needs to be freed |
| 424 | * separately, devm_gpio_free() must be used. |
| 425 | */ |
| 426 | |
| 427 | int devm_gpio_request(struct device *dev, unsigned gpio, const char *label) |
| 428 | { |
| 429 | unsigned *dr; |
| 430 | int rc; |
| 431 | |
| 432 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 433 | if (!dr) |
| 434 | return -ENOMEM; |
| 435 | |
| 436 | rc = gpio_request(gpio, label); |
| 437 | if (rc) { |
| 438 | devres_free(dr); |
| 439 | return rc; |
| 440 | } |
| 441 | |
| 442 | *dr = gpio; |
| 443 | devres_add(dev, dr); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | EXPORT_SYMBOL(devm_gpio_request); |
| 448 | |
| 449 | /** |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 450 | * devm_gpio_request_one - request a single GPIO with initial setup |
| 451 | * @dev: device to request for |
| 452 | * @gpio: the GPIO number |
| 453 | * @flags: GPIO configuration as specified by GPIOF_* |
| 454 | * @label: a literal description string of this GPIO |
| 455 | */ |
| 456 | int devm_gpio_request_one(struct device *dev, unsigned gpio, |
| 457 | unsigned long flags, const char *label) |
| 458 | { |
| 459 | unsigned *dr; |
| 460 | int rc; |
| 461 | |
| 462 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 463 | if (!dr) |
| 464 | return -ENOMEM; |
| 465 | |
| 466 | rc = gpio_request_one(gpio, flags, label); |
| 467 | if (rc) { |
| 468 | devres_free(dr); |
| 469 | return rc; |
| 470 | } |
| 471 | |
| 472 | *dr = gpio; |
| 473 | devres_add(dev, dr); |
| 474 | |
| 475 | return 0; |
| 476 | } |
Stephen Warren | 3996bfc | 2012-06-07 17:56:09 -0600 | [diff] [blame] | 477 | EXPORT_SYMBOL(devm_gpio_request_one); |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 478 | |
| 479 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 480 | * devm_gpio_free - free a GPIO |
| 481 | * @dev: device to free GPIO for |
| 482 | * @gpio: GPIO to free |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 483 | * |
| 484 | * Except for the extra @dev argument, this function takes the |
| 485 | * same arguments and performs the same function as gpio_free(). |
| 486 | * This function instead of gpio_free() should be used to manually |
| 487 | * free GPIOs allocated with devm_gpio_request(). |
| 488 | */ |
| 489 | void devm_gpio_free(struct device *dev, unsigned int gpio) |
| 490 | { |
| 491 | |
Mark Brown | a85990b | 2012-05-03 18:15:14 +0100 | [diff] [blame] | 492 | WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match, |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 493 | &gpio)); |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 494 | } |
| 495 | EXPORT_SYMBOL(devm_gpio_free); |