gpiolib-devres.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * devres.c - managed gpio resources
  4. * This file is based on kernel/irq/devres.c
  5. *
  6. * Copyright (c) 2011 John Crispin <john@phrozen.org>
  7. */
  8. #include <linux/device/devres.h>
  9. #include <linux/err.h>
  10. #include <linux/export.h>
  11. #include <linux/gfp.h>
  12. #include <linux/types.h>
  13. #include <linux/gpio/consumer.h>
  14. #include "gpiolib.h"
  15. struct fwnode_handle;
  16. struct lock_class_key;
  17. static void devm_gpiod_release(void *desc)
  18. {
  19. gpiod_put(desc);
  20. }
  21. static void devm_gpiod_release_array(void *descs)
  22. {
  23. gpiod_put_array(descs);
  24. }
  25. /**
  26. * devm_gpiod_get - Resource-managed gpiod_get()
  27. * @dev: GPIO consumer
  28. * @con_id: function within the GPIO consumer
  29. * @flags: optional GPIO initialization flags
  30. *
  31. * Managed gpiod_get(). GPIO descriptors returned from this function are
  32. * automatically disposed on driver detach. See gpiod_get() for detailed
  33. * information about behavior and return values.
  34. *
  35. * Returns:
  36. * The GPIO descriptor corresponding to the function @con_id of device
  37. * dev, %-ENOENT if no GPIO has been assigned to the requested function, or
  38. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  39. */
  40. struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  41. const char *con_id,
  42. enum gpiod_flags flags)
  43. {
  44. return devm_gpiod_get_index(dev, con_id, 0, flags);
  45. }
  46. EXPORT_SYMBOL_GPL(devm_gpiod_get);
  47. /**
  48. * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
  49. * @dev: GPIO consumer
  50. * @con_id: function within the GPIO consumer
  51. * @flags: optional GPIO initialization flags
  52. *
  53. * Managed gpiod_get_optional(). GPIO descriptors returned from this function
  54. * are automatically disposed on driver detach. See gpiod_get_optional() for
  55. * detailed information about behavior and return values.
  56. *
  57. * Returns:
  58. * The GPIO descriptor corresponding to the function @con_id of device
  59. * dev, NULL if no GPIO has been assigned to the requested function, or
  60. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  61. */
  62. struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
  63. const char *con_id,
  64. enum gpiod_flags flags)
  65. {
  66. return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
  67. }
  68. EXPORT_SYMBOL_GPL(devm_gpiod_get_optional);
  69. /**
  70. * devm_gpiod_get_index - Resource-managed gpiod_get_index()
  71. * @dev: GPIO consumer
  72. * @con_id: function within the GPIO consumer
  73. * @idx: index of the GPIO to obtain in the consumer
  74. * @flags: optional GPIO initialization flags
  75. *
  76. * Managed gpiod_get_index(). GPIO descriptors returned from this function are
  77. * automatically disposed on driver detach. See gpiod_get_index() for detailed
  78. * information about behavior and return values.
  79. *
  80. * Returns:
  81. * The GPIO descriptor corresponding to the function @con_id of device
  82. * dev, %-ENOENT if no GPIO has been assigned to the requested function, or
  83. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  84. */
  85. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  86. const char *con_id,
  87. unsigned int idx,
  88. enum gpiod_flags flags)
  89. {
  90. struct gpio_desc *desc;
  91. int ret;
  92. desc = gpiod_get_index(dev, con_id, idx, flags);
  93. if (IS_ERR(desc))
  94. return desc;
  95. /*
  96. * For non-exclusive GPIO descriptors, check if this descriptor is
  97. * already under resource management by this device.
  98. */
  99. if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
  100. bool dres;
  101. dres = devm_is_action_added(dev, devm_gpiod_release, desc);
  102. if (dres)
  103. return desc;
  104. }
  105. ret = devm_add_action_or_reset(dev, devm_gpiod_release, desc);
  106. if (ret)
  107. return ERR_PTR(ret);
  108. return desc;
  109. }
  110. EXPORT_SYMBOL_GPL(devm_gpiod_get_index);
  111. /**
  112. * devm_fwnode_gpiod_get_index - get a GPIO descriptor from a given node
  113. * @dev: GPIO consumer
  114. * @fwnode: firmware node containing GPIO reference
  115. * @con_id: function within the GPIO consumer
  116. * @index: index of the GPIO to obtain in the consumer
  117. * @flags: GPIO initialization flags
  118. * @label: label to attach to the requested GPIO
  119. *
  120. * GPIO descriptors returned from this function are automatically disposed on
  121. * driver detach.
  122. *
  123. * Returns:
  124. * The GPIO descriptor corresponding to the function @con_id of device
  125. * dev, %-ENOENT if no GPIO has been assigned to the requested function, or
  126. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  127. */
  128. struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
  129. struct fwnode_handle *fwnode,
  130. const char *con_id, int index,
  131. enum gpiod_flags flags,
  132. const char *label)
  133. {
  134. struct gpio_desc *desc;
  135. int ret;
  136. desc = gpiod_find_and_request(dev, fwnode, con_id, index, flags, label, false);
  137. if (IS_ERR(desc))
  138. return desc;
  139. ret = devm_add_action_or_reset(dev, devm_gpiod_release, desc);
  140. if (ret)
  141. return ERR_PTR(ret);
  142. return desc;
  143. }
  144. EXPORT_SYMBOL_GPL(devm_fwnode_gpiod_get_index);
  145. /**
  146. * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
  147. * @dev: GPIO consumer
  148. * @con_id: function within the GPIO consumer
  149. * @index: index of the GPIO to obtain in the consumer
  150. * @flags: optional GPIO initialization flags
  151. *
  152. * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
  153. * function are automatically disposed on driver detach. See
  154. * gpiod_get_index_optional() for detailed information about behavior and
  155. * return values.
  156. *
  157. * Returns:
  158. * The GPIO descriptor corresponding to the function @con_id of device
  159. * dev, %NULL if no GPIO has been assigned to the requested function, or
  160. * another IS_ERR() code if an error occurred while trying to acquire the GPIO.
  161. */
  162. struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
  163. const char *con_id,
  164. unsigned int index,
  165. enum gpiod_flags flags)
  166. {
  167. struct gpio_desc *desc;
  168. desc = devm_gpiod_get_index(dev, con_id, index, flags);
  169. if (gpiod_not_found(desc))
  170. return NULL;
  171. return desc;
  172. }
  173. EXPORT_SYMBOL_GPL(devm_gpiod_get_index_optional);
  174. /**
  175. * devm_gpiod_get_array - Resource-managed gpiod_get_array()
  176. * @dev: GPIO consumer
  177. * @con_id: function within the GPIO consumer
  178. * @flags: optional GPIO initialization flags
  179. *
  180. * Managed gpiod_get_array(). GPIO descriptors returned from this function are
  181. * automatically disposed on driver detach. See gpiod_get_array() for detailed
  182. * information about behavior and return values.
  183. *
  184. * Returns:
  185. * The GPIO descriptors corresponding to the function @con_id of device
  186. * dev, %-ENOENT if no GPIO has been assigned to the requested function,
  187. * or another IS_ERR() code if an error occurred while trying to acquire
  188. * the GPIOs.
  189. */
  190. struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
  191. const char *con_id,
  192. enum gpiod_flags flags)
  193. {
  194. struct gpio_descs *descs;
  195. int ret;
  196. descs = gpiod_get_array(dev, con_id, flags);
  197. if (IS_ERR(descs))
  198. return descs;
  199. ret = devm_add_action_or_reset(dev, devm_gpiod_release_array, descs);
  200. if (ret)
  201. return ERR_PTR(ret);
  202. return descs;
  203. }
  204. EXPORT_SYMBOL_GPL(devm_gpiod_get_array);
  205. /**
  206. * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
  207. * @dev: GPIO consumer
  208. * @con_id: function within the GPIO consumer
  209. * @flags: optional GPIO initialization flags
  210. *
  211. * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
  212. * function are automatically disposed on driver detach.
  213. * See gpiod_get_array_optional() for detailed information about behavior and
  214. * return values.
  215. *
  216. * Returns:
  217. * The GPIO descriptors corresponding to the function @con_id of device
  218. * dev, %NULL if no GPIO has been assigned to the requested function,
  219. * or another IS_ERR() code if an error occurred while trying to acquire
  220. * the GPIOs.
  221. */
  222. struct gpio_descs *__must_check
  223. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  224. enum gpiod_flags flags)
  225. {
  226. struct gpio_descs *descs;
  227. descs = devm_gpiod_get_array(dev, con_id, flags);
  228. if (gpiod_not_found(descs))
  229. return NULL;
  230. return descs;
  231. }
  232. EXPORT_SYMBOL_GPL(devm_gpiod_get_array_optional);
  233. /**
  234. * devm_gpiod_put - Resource-managed gpiod_put()
  235. * @dev: GPIO consumer
  236. * @desc: GPIO descriptor to dispose of
  237. *
  238. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  239. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  240. * will be disposed of by the resource management code.
  241. */
  242. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  243. {
  244. devm_release_action(dev, devm_gpiod_release, desc);
  245. }
  246. EXPORT_SYMBOL_GPL(devm_gpiod_put);
  247. /**
  248. * devm_gpiod_unhinge - Remove resource management from a gpio descriptor
  249. * @dev: GPIO consumer
  250. * @desc: GPIO descriptor to remove resource management from
  251. *
  252. * *DEPRECATED*
  253. * This function should not be used. It's been provided as a workaround for
  254. * resource ownership issues in the regulator framework and should be replaced
  255. * with a better solution.
  256. *
  257. * Remove resource management from a GPIO descriptor. This is needed when
  258. * you want to hand over lifecycle management of a descriptor to another
  259. * mechanism.
  260. */
  261. void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc)
  262. {
  263. int ret;
  264. if (IS_ERR_OR_NULL(desc))
  265. return;
  266. /*
  267. * If the GPIO descriptor is requested as nonexclusive, we
  268. * may call this function several times on the same descriptor
  269. * so it is OK if devres_destroy() returns -ENOENT.
  270. */
  271. ret = devm_remove_action_nowarn(dev, devm_gpiod_release, desc);
  272. if (ret == -ENOENT)
  273. return;
  274. /* Anything else we should warn about */
  275. WARN_ON(ret);
  276. }
  277. EXPORT_SYMBOL_GPL(devm_gpiod_unhinge);
  278. /**
  279. * devm_gpiod_put_array - Resource-managed gpiod_put_array()
  280. * @dev: GPIO consumer
  281. * @descs: GPIO descriptor array to dispose of
  282. *
  283. * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
  284. * Normally this function will not be called as the GPIOs will be disposed of
  285. * by the resource management code.
  286. */
  287. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
  288. {
  289. devm_release_action(dev, devm_gpiod_release_array, descs);
  290. }
  291. EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
  292. static void devm_gpio_chip_release(void *data)
  293. {
  294. struct gpio_chip *gc = data;
  295. gpiochip_remove(gc);
  296. }
  297. /**
  298. * devm_gpiochip_add_data_with_key() - Resource managed gpiochip_add_data_with_key()
  299. * @dev: pointer to the device that gpio_chip belongs to.
  300. * @gc: the GPIO chip to register
  301. * @data: driver-private data associated with this chip
  302. * @lock_key: lockdep class for IRQ lock
  303. * @request_key: lockdep class for IRQ request
  304. *
  305. * Context: potentially before irqs will work
  306. *
  307. * The gpio chip automatically be released when the device is unbound.
  308. *
  309. * Returns:
  310. * A negative errno if the chip can't be registered, such as because the
  311. * gc->base is invalid or already associated with a different chip.
  312. * Otherwise it returns zero as a success code.
  313. */
  314. int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
  315. struct lock_class_key *lock_key,
  316. struct lock_class_key *request_key)
  317. {
  318. int ret;
  319. ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key);
  320. if (ret < 0)
  321. return ret;
  322. return devm_add_action_or_reset(dev, devm_gpio_chip_release, gc);
  323. }
  324. EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key);