devres.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include <linux/module.h>
  17. #include <linux/err.h>
  18. #include <linux/gpio.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/device.h>
  21. #include <linux/gfp.h>
  22. static void devm_gpiod_release(struct device *dev, void *res)
  23. {
  24. struct gpio_desc **desc = res;
  25. gpiod_put(*desc);
  26. }
  27. static int devm_gpiod_match(struct device *dev, void *res, void *data)
  28. {
  29. struct gpio_desc **this = res, **gpio = data;
  30. return *this == *gpio;
  31. }
  32. /**
  33. * devm_gpiod_get - Resource-managed gpiod_get()
  34. * @dev: GPIO consumer
  35. * @con_id: function within the GPIO consumer
  36. * @flags: optional GPIO initialization flags
  37. *
  38. * Managed gpiod_get(). GPIO descriptors returned from this function are
  39. * automatically disposed on driver detach. See gpiod_get() for detailed
  40. * information about behavior and return values.
  41. */
  42. struct gpio_desc *__must_check __devm_gpiod_get(struct device *dev,
  43. const char *con_id,
  44. enum gpiod_flags flags)
  45. {
  46. return devm_gpiod_get_index(dev, con_id, 0, flags);
  47. }
  48. EXPORT_SYMBOL(__devm_gpiod_get);
  49. /**
  50. * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
  51. * @dev: GPIO consumer
  52. * @con_id: function within the GPIO consumer
  53. * @flags: optional GPIO initialization flags
  54. *
  55. * Managed gpiod_get_optional(). GPIO descriptors returned from this function
  56. * are automatically disposed on driver detach. See gpiod_get_optional() for
  57. * detailed information about behavior and return values.
  58. */
  59. struct gpio_desc *__must_check __devm_gpiod_get_optional(struct device *dev,
  60. const char *con_id,
  61. enum gpiod_flags flags)
  62. {
  63. return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
  64. }
  65. EXPORT_SYMBOL(__devm_gpiod_get_optional);
  66. /**
  67. * devm_gpiod_get_index - Resource-managed gpiod_get_index()
  68. * @dev: GPIO consumer
  69. * @con_id: function within the GPIO consumer
  70. * @idx: index of the GPIO to obtain in the consumer
  71. * @flags: optional GPIO initialization flags
  72. *
  73. * Managed gpiod_get_index(). GPIO descriptors returned from this function are
  74. * automatically disposed on driver detach. See gpiod_get_index() for detailed
  75. * information about behavior and return values.
  76. */
  77. struct gpio_desc *__must_check __devm_gpiod_get_index(struct device *dev,
  78. const char *con_id,
  79. unsigned int idx,
  80. enum gpiod_flags flags)
  81. {
  82. struct gpio_desc **dr;
  83. struct gpio_desc *desc;
  84. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  85. GFP_KERNEL);
  86. if (!dr)
  87. return ERR_PTR(-ENOMEM);
  88. desc = gpiod_get_index(dev, con_id, idx, flags);
  89. if (IS_ERR(desc)) {
  90. devres_free(dr);
  91. return desc;
  92. }
  93. *dr = desc;
  94. devres_add(dev, dr);
  95. return desc;
  96. }
  97. EXPORT_SYMBOL(__devm_gpiod_get_index);
  98. /**
  99. * devm_get_gpiod_from_child - get a GPIO descriptor from a device's child node
  100. * @dev: GPIO consumer
  101. * @child: firmware node (child of @dev)
  102. *
  103. * GPIO descriptors returned from this function are automatically disposed on
  104. * driver detach.
  105. */
  106. struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
  107. struct fwnode_handle *child)
  108. {
  109. struct gpio_desc **dr;
  110. struct gpio_desc *desc;
  111. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  112. GFP_KERNEL);
  113. if (!dr)
  114. return ERR_PTR(-ENOMEM);
  115. desc = fwnode_get_named_gpiod(child, "gpios");
  116. if (IS_ERR(desc)) {
  117. devres_free(dr);
  118. return desc;
  119. }
  120. *dr = desc;
  121. devres_add(dev, dr);
  122. return desc;
  123. }
  124. EXPORT_SYMBOL(devm_get_gpiod_from_child);
  125. /**
  126. * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
  127. * @dev: GPIO consumer
  128. * @con_id: function within the GPIO consumer
  129. * @index: index of the GPIO to obtain in the consumer
  130. * @flags: optional GPIO initialization flags
  131. *
  132. * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
  133. * function are automatically disposed on driver detach. See
  134. * gpiod_get_index_optional() for detailed information about behavior and
  135. * return values.
  136. */
  137. struct gpio_desc *__must_check __devm_gpiod_get_index_optional(struct device *dev,
  138. const char *con_id,
  139. unsigned int index,
  140. enum gpiod_flags flags)
  141. {
  142. struct gpio_desc *desc;
  143. desc = devm_gpiod_get_index(dev, con_id, index, flags);
  144. if (IS_ERR(desc)) {
  145. if (PTR_ERR(desc) == -ENOENT)
  146. return NULL;
  147. }
  148. return desc;
  149. }
  150. EXPORT_SYMBOL(__devm_gpiod_get_index_optional);
  151. /**
  152. * devm_gpiod_put - Resource-managed gpiod_put()
  153. * @desc: GPIO descriptor to dispose of
  154. *
  155. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  156. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  157. * will be disposed of by the resource management code.
  158. */
  159. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  160. {
  161. WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
  162. &desc));
  163. }
  164. EXPORT_SYMBOL(devm_gpiod_put);
  165. static void devm_gpio_release(struct device *dev, void *res)
  166. {
  167. unsigned *gpio = res;
  168. gpio_free(*gpio);
  169. }
  170. static int devm_gpio_match(struct device *dev, void *res, void *data)
  171. {
  172. unsigned *this = res, *gpio = data;
  173. return *this == *gpio;
  174. }
  175. /**
  176. * devm_gpio_request - request a GPIO for a managed device
  177. * @dev: device to request the GPIO for
  178. * @gpio: GPIO to allocate
  179. * @label: the name of the requested GPIO
  180. *
  181. * Except for the extra @dev argument, this function takes the
  182. * same arguments and performs the same function as
  183. * gpio_request(). GPIOs requested with this function will be
  184. * automatically freed on driver detach.
  185. *
  186. * If an GPIO allocated with this function needs to be freed
  187. * separately, devm_gpio_free() must be used.
  188. */
  189. int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
  190. {
  191. unsigned *dr;
  192. int rc;
  193. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  194. if (!dr)
  195. return -ENOMEM;
  196. rc = gpio_request(gpio, label);
  197. if (rc) {
  198. devres_free(dr);
  199. return rc;
  200. }
  201. *dr = gpio;
  202. devres_add(dev, dr);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(devm_gpio_request);
  206. /**
  207. * devm_gpio_request_one - request a single GPIO with initial setup
  208. * @dev: device to request for
  209. * @gpio: the GPIO number
  210. * @flags: GPIO configuration as specified by GPIOF_*
  211. * @label: a literal description string of this GPIO
  212. */
  213. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  214. unsigned long flags, const char *label)
  215. {
  216. unsigned *dr;
  217. int rc;
  218. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  219. if (!dr)
  220. return -ENOMEM;
  221. rc = gpio_request_one(gpio, flags, label);
  222. if (rc) {
  223. devres_free(dr);
  224. return rc;
  225. }
  226. *dr = gpio;
  227. devres_add(dev, dr);
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(devm_gpio_request_one);
  231. /**
  232. * devm_gpio_free - free a GPIO
  233. * @dev: device to free GPIO for
  234. * @gpio: GPIO to free
  235. *
  236. * Except for the extra @dev argument, this function takes the
  237. * same arguments and performs the same function as gpio_free().
  238. * This function instead of gpio_free() should be used to manually
  239. * free GPIOs allocated with devm_gpio_request().
  240. */
  241. void devm_gpio_free(struct device *dev, unsigned int gpio)
  242. {
  243. WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
  244. &gpio));
  245. }
  246. EXPORT_SYMBOL(devm_gpio_free);