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 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 62 | EXPORT_SYMBOL_GPL(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 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 80 | EXPORT_SYMBOL_GPL(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 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(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, |
Krzysztof Kozlowski | e6ae9a8 | 2021-07-28 16:42:28 +0200 | [diff] [blame] | 148 | const struct device_node *node, |
Linus Walleij | 92542ed | 2017-12-29 22:52:02 +0100 | [diff] [blame] | 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 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 185 | EXPORT_SYMBOL_GPL(devm_gpiod_get_from_of_node); |
Linus Walleij | 92542ed | 2017-12-29 22:52:02 +0100 | [diff] [blame] | 186 | |
| 187 | /** |
Dmitry Torokhov | 2d2f116 | 2019-09-12 20:22:38 -0700 | [diff] [blame] | 188 | * devm_fwnode_gpiod_get_index - get a GPIO descriptor from a given node |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 189 | * @dev: GPIO consumer |
Dmitry Torokhov | 2d2f116 | 2019-09-12 20:22:38 -0700 | [diff] [blame] | 190 | * @fwnode: firmware node containing GPIO reference |
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 |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 193 | * @flags: GPIO initialization flags |
Thierry Reding | 2b7c809 | 2017-07-24 16:57:24 +0200 | [diff] [blame] | 194 | * @label: label to attach to the requested GPIO |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 195 | * |
| 196 | * GPIO descriptors returned from this function are automatically disposed on |
| 197 | * driver detach. |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 198 | * |
Andy Shevchenko | ff21378 | 2017-02-28 17:03:12 +0200 | [diff] [blame] | 199 | * On successful request the GPIO pin is configured in accordance with |
Andy Shevchenko | a264d10 | 2017-01-09 16:02:28 +0200 | [diff] [blame] | 200 | * provided @flags. |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 201 | */ |
Dmitry Torokhov | 2d2f116 | 2019-09-12 20:22:38 -0700 | [diff] [blame] | 202 | struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev, |
| 203 | struct fwnode_handle *fwnode, |
| 204 | const char *con_id, int index, |
| 205 | enum gpiod_flags flags, |
| 206 | const char *label) |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 207 | { |
| 208 | struct gpio_desc **dr; |
| 209 | struct gpio_desc *desc; |
| 210 | |
| 211 | dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), |
| 212 | GFP_KERNEL); |
| 213 | if (!dr) |
| 214 | return ERR_PTR(-ENOMEM); |
| 215 | |
Dmitry Torokhov | 13949fa | 2019-09-12 20:22:39 -0700 | [diff] [blame] | 216 | desc = fwnode_gpiod_get_index(fwnode, con_id, index, flags, label); |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 217 | if (IS_ERR(desc)) { |
| 218 | devres_free(dr); |
| 219 | return desc; |
| 220 | } |
| 221 | |
| 222 | *dr = desc; |
| 223 | devres_add(dev, dr); |
| 224 | |
| 225 | return desc; |
| 226 | } |
Dmitry Torokhov | 2d2f116 | 2019-09-12 20:22:38 -0700 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_index); |
Mika Westerberg | 40b7318 | 2014-10-21 13:33:59 +0200 | [diff] [blame] | 228 | |
| 229 | /** |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 230 | * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional() |
| 231 | * @dev: GPIO consumer |
| 232 | * @con_id: function within the GPIO consumer |
| 233 | * @index: index of the GPIO to obtain in the consumer |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 234 | * @flags: optional GPIO initialization flags |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 235 | * |
| 236 | * Managed gpiod_get_index_optional(). GPIO descriptors returned from this |
| 237 | * function are automatically disposed on driver detach. See |
| 238 | * gpiod_get_index_optional() for detailed information about behavior and |
| 239 | * return values. |
| 240 | */ |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 241 | 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] | 242 | const char *con_id, |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 243 | unsigned int index, |
Uwe Kleine-König | b17d1bf | 2015-02-11 11:52:37 +0100 | [diff] [blame] | 244 | enum gpiod_flags flags) |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 245 | { |
| 246 | struct gpio_desc *desc; |
| 247 | |
Alexandre Courbot | 39b2bbe | 2014-07-25 23:38:36 +0900 | [diff] [blame] | 248 | desc = devm_gpiod_get_index(dev, con_id, index, flags); |
Andy Shevchenko | 7b58696 | 2020-10-21 17:25:28 +0300 | [diff] [blame] | 249 | if (gpiod_not_found(desc)) |
| 250 | return NULL; |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 251 | |
| 252 | return desc; |
| 253 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 254 | EXPORT_SYMBOL_GPL(devm_gpiod_get_index_optional); |
Thierry Reding | 29a1f233 | 2014-04-25 17:10:06 +0200 | [diff] [blame] | 255 | |
| 256 | /** |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 257 | * devm_gpiod_get_array - Resource-managed gpiod_get_array() |
| 258 | * @dev: GPIO consumer |
| 259 | * @con_id: function within the GPIO consumer |
| 260 | * @flags: optional GPIO initialization flags |
| 261 | * |
| 262 | * Managed gpiod_get_array(). GPIO descriptors returned from this function are |
| 263 | * automatically disposed on driver detach. See gpiod_get_array() for detailed |
| 264 | * information about behavior and return values. |
| 265 | */ |
| 266 | struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev, |
| 267 | const char *con_id, |
| 268 | enum gpiod_flags flags) |
| 269 | { |
| 270 | struct gpio_descs **dr; |
| 271 | struct gpio_descs *descs; |
| 272 | |
| 273 | dr = devres_alloc(devm_gpiod_release_array, |
| 274 | sizeof(struct gpio_descs *), GFP_KERNEL); |
| 275 | if (!dr) |
| 276 | return ERR_PTR(-ENOMEM); |
| 277 | |
| 278 | descs = gpiod_get_array(dev, con_id, flags); |
| 279 | if (IS_ERR(descs)) { |
| 280 | devres_free(dr); |
| 281 | return descs; |
| 282 | } |
| 283 | |
| 284 | *dr = descs; |
| 285 | devres_add(dev, dr); |
| 286 | |
| 287 | return descs; |
| 288 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 289 | EXPORT_SYMBOL_GPL(devm_gpiod_get_array); |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional() |
| 293 | * @dev: GPIO consumer |
| 294 | * @con_id: function within the GPIO consumer |
| 295 | * @flags: optional GPIO initialization flags |
| 296 | * |
| 297 | * Managed gpiod_get_array_optional(). GPIO descriptors returned from this |
| 298 | * function are automatically disposed on driver detach. |
| 299 | * See gpiod_get_array_optional() for detailed information about behavior and |
| 300 | * return values. |
| 301 | */ |
| 302 | struct gpio_descs *__must_check |
| 303 | devm_gpiod_get_array_optional(struct device *dev, const char *con_id, |
| 304 | enum gpiod_flags flags) |
| 305 | { |
| 306 | struct gpio_descs *descs; |
| 307 | |
| 308 | descs = devm_gpiod_get_array(dev, con_id, flags); |
Andy Shevchenko | 7b58696 | 2020-10-21 17:25:28 +0300 | [diff] [blame] | 309 | if (gpiod_not_found(descs)) |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 310 | return NULL; |
| 311 | |
| 312 | return descs; |
| 313 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 314 | EXPORT_SYMBOL_GPL(devm_gpiod_get_array_optional); |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 315 | |
| 316 | /** |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 317 | * devm_gpiod_put - Resource-managed gpiod_put() |
Thierry Reding | 2b7c809 | 2017-07-24 16:57:24 +0200 | [diff] [blame] | 318 | * @dev: GPIO consumer |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 319 | * @desc: GPIO descriptor to dispose of |
| 320 | * |
| 321 | * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or |
| 322 | * devm_gpiod_get_index(). Normally this function will not be called as the GPIO |
| 323 | * will be disposed of by the resource management code. |
| 324 | */ |
| 325 | void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) |
| 326 | { |
| 327 | WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match, |
| 328 | &desc)); |
| 329 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 330 | EXPORT_SYMBOL_GPL(devm_gpiod_put); |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 331 | |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 332 | /** |
Linus Walleij | 891ddbc | 2018-12-06 13:43:46 +0100 | [diff] [blame] | 333 | * devm_gpiod_unhinge - Remove resource management from a gpio descriptor |
| 334 | * @dev: GPIO consumer |
| 335 | * @desc: GPIO descriptor to remove resource management from |
| 336 | * |
| 337 | * Remove resource management from a GPIO descriptor. This is needed when |
| 338 | * you want to hand over lifecycle management of a descriptor to another |
| 339 | * mechanism. |
| 340 | */ |
| 341 | |
| 342 | void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc) |
| 343 | { |
| 344 | int ret; |
| 345 | |
| 346 | if (IS_ERR_OR_NULL(desc)) |
| 347 | return; |
| 348 | ret = devres_destroy(dev, devm_gpiod_release, |
| 349 | devm_gpiod_match, &desc); |
| 350 | /* |
| 351 | * If the GPIO descriptor is requested as nonexclusive, we |
| 352 | * may call this function several times on the same descriptor |
| 353 | * so it is OK if devres_destroy() returns -ENOENT. |
| 354 | */ |
| 355 | if (ret == -ENOENT) |
| 356 | return; |
| 357 | /* Anything else we should warn about */ |
| 358 | WARN_ON(ret); |
| 359 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 360 | EXPORT_SYMBOL_GPL(devm_gpiod_unhinge); |
Linus Walleij | 891ddbc | 2018-12-06 13:43:46 +0100 | [diff] [blame] | 361 | |
| 362 | /** |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 363 | * devm_gpiod_put_array - Resource-managed gpiod_put_array() |
Thierry Reding | 2b7c809 | 2017-07-24 16:57:24 +0200 | [diff] [blame] | 364 | * @dev: GPIO consumer |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 365 | * @descs: GPIO descriptor array to dispose of |
| 366 | * |
| 367 | * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array(). |
| 368 | * Normally this function will not be called as the GPIOs will be disposed of |
| 369 | * by the resource management code. |
| 370 | */ |
| 371 | void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs) |
| 372 | { |
| 373 | WARN_ON(devres_release(dev, devm_gpiod_release_array, |
| 374 | devm_gpiod_match_array, &descs)); |
| 375 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 376 | EXPORT_SYMBOL_GPL(devm_gpiod_put_array); |
Rojhalat Ibrahim | 331758e | 2015-02-11 17:28:02 +0100 | [diff] [blame] | 377 | |
Alexandre Courbot | bae48da | 2013-10-17 10:21:38 -0700 | [diff] [blame] | 378 | |
| 379 | |
| 380 | |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 381 | static void devm_gpio_release(struct device *dev, void *res) |
| 382 | { |
| 383 | unsigned *gpio = res; |
| 384 | |
| 385 | gpio_free(*gpio); |
| 386 | } |
| 387 | |
| 388 | static int devm_gpio_match(struct device *dev, void *res, void *data) |
| 389 | { |
| 390 | unsigned *this = res, *gpio = data; |
| 391 | |
| 392 | return *this == *gpio; |
| 393 | } |
| 394 | |
| 395 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 396 | * devm_gpio_request - request a GPIO for a managed device |
| 397 | * @dev: device to request the GPIO for |
| 398 | * @gpio: GPIO to allocate |
| 399 | * @label: the name of the requested GPIO |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 400 | * |
| 401 | * Except for the extra @dev argument, this function takes the |
| 402 | * same arguments and performs the same function as |
| 403 | * gpio_request(). GPIOs requested with this function will be |
| 404 | * automatically freed on driver detach. |
| 405 | * |
| 406 | * If an GPIO allocated with this function needs to be freed |
| 407 | * separately, devm_gpio_free() must be used. |
| 408 | */ |
| 409 | |
| 410 | int devm_gpio_request(struct device *dev, unsigned gpio, const char *label) |
| 411 | { |
| 412 | unsigned *dr; |
| 413 | int rc; |
| 414 | |
| 415 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 416 | if (!dr) |
| 417 | return -ENOMEM; |
| 418 | |
| 419 | rc = gpio_request(gpio, label); |
| 420 | if (rc) { |
| 421 | devres_free(dr); |
| 422 | return rc; |
| 423 | } |
| 424 | |
| 425 | *dr = gpio; |
| 426 | devres_add(dev, dr); |
| 427 | |
| 428 | return 0; |
| 429 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 430 | EXPORT_SYMBOL_GPL(devm_gpio_request); |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 431 | |
| 432 | /** |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 433 | * devm_gpio_request_one - request a single GPIO with initial setup |
| 434 | * @dev: device to request for |
| 435 | * @gpio: the GPIO number |
| 436 | * @flags: GPIO configuration as specified by GPIOF_* |
| 437 | * @label: a literal description string of this GPIO |
| 438 | */ |
| 439 | int devm_gpio_request_one(struct device *dev, unsigned gpio, |
| 440 | unsigned long flags, const char *label) |
| 441 | { |
| 442 | unsigned *dr; |
| 443 | int rc; |
| 444 | |
| 445 | dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL); |
| 446 | if (!dr) |
| 447 | return -ENOMEM; |
| 448 | |
| 449 | rc = gpio_request_one(gpio, flags, label); |
| 450 | if (rc) { |
| 451 | devres_free(dr); |
| 452 | return rc; |
| 453 | } |
| 454 | |
| 455 | *dr = gpio; |
| 456 | devres_add(dev, dr); |
| 457 | |
| 458 | return 0; |
| 459 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 460 | EXPORT_SYMBOL_GPL(devm_gpio_request_one); |
Mark Brown | 09d71ff | 2012-05-02 12:46:46 +0100 | [diff] [blame] | 461 | |
| 462 | /** |
Wolfram Sang | 713b7ef | 2013-06-04 17:48:36 +0200 | [diff] [blame] | 463 | * devm_gpio_free - free a GPIO |
| 464 | * @dev: device to free GPIO for |
| 465 | * @gpio: GPIO to free |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 466 | * |
| 467 | * Except for the extra @dev argument, this function takes the |
| 468 | * same arguments and performs the same function as gpio_free(). |
| 469 | * This function instead of gpio_free() should be used to manually |
| 470 | * free GPIOs allocated with devm_gpio_request(). |
| 471 | */ |
| 472 | void devm_gpio_free(struct device *dev, unsigned int gpio) |
| 473 | { |
| 474 | |
Mark Brown | a85990b | 2012-05-03 18:15:14 +0100 | [diff] [blame] | 475 | WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match, |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 476 | &gpio)); |
John Crispin | 1a0703e | 2011-12-20 21:40:21 +0100 | [diff] [blame] | 477 | } |
Geert Uytterhoeven | ac57199 | 2019-09-06 10:45:39 +0200 | [diff] [blame] | 478 | EXPORT_SYMBOL_GPL(devm_gpio_free); |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 479 | |
Bartosz Golaszewski | 3c6e73e | 2020-11-10 10:39:21 +0100 | [diff] [blame] | 480 | static void devm_gpio_chip_release(void *data) |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 481 | { |
Bartosz Golaszewski | 3c6e73e | 2020-11-10 10:39:21 +0100 | [diff] [blame] | 482 | struct gpio_chip *gc = data; |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 483 | |
| 484 | gpiochip_remove(gc); |
| 485 | } |
| 486 | |
| 487 | /** |
Ahmad Fatoum | 5f402bb | 2020-07-31 14:38:36 +0200 | [diff] [blame] | 488 | * devm_gpiochip_add_data_with_key() - Resource managed gpiochip_add_data_with_key() |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 489 | * @dev: pointer to the device that gpio_chip belongs to. |
| 490 | * @gc: the GPIO chip to register |
| 491 | * @data: driver-private data associated with this chip |
Ahmad Fatoum | 5f402bb | 2020-07-31 14:38:36 +0200 | [diff] [blame] | 492 | * @lock_key: lockdep class for IRQ lock |
| 493 | * @request_key: lockdep class for IRQ request |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 494 | * |
| 495 | * Context: potentially before irqs will work |
| 496 | * |
| 497 | * The gpio chip automatically be released when the device is unbound. |
| 498 | * |
| 499 | * Returns: |
| 500 | * A negative errno if the chip can't be registered, such as because the |
| 501 | * gc->base is invalid or already associated with a different chip. |
| 502 | * Otherwise it returns zero as a success code. |
| 503 | */ |
Ahmad Fatoum | 5f402bb | 2020-07-31 14:38:36 +0200 | [diff] [blame] | 504 | int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data, |
| 505 | struct lock_class_key *lock_key, |
| 506 | struct lock_class_key *request_key) |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 507 | { |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 508 | int ret; |
| 509 | |
Ahmad Fatoum | 5f402bb | 2020-07-31 14:38:36 +0200 | [diff] [blame] | 510 | ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key); |
Bartosz Golaszewski | 3c6e73e | 2020-11-10 10:39:21 +0100 | [diff] [blame] | 511 | if (ret < 0) |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 512 | return ret; |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 513 | |
Bartosz Golaszewski | 3c6e73e | 2020-11-10 10:39:21 +0100 | [diff] [blame] | 514 | return devm_add_action_or_reset(dev, devm_gpio_chip_release, gc); |
Linus Walleij | a28e1c0 | 2020-03-13 09:15:22 +0100 | [diff] [blame] | 515 | } |
Ahmad Fatoum | 5f402bb | 2020-07-31 14:38:36 +0200 | [diff] [blame] | 516 | EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key); |