devres.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
  100. * @dev: GPIO consumer
  101. * @con_id: function within the GPIO consumer
  102. * @index: index of the GPIO to obtain in the consumer
  103. * @flags: optional GPIO initialization flags
  104. *
  105. * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
  106. * function are automatically disposed on driver detach. See
  107. * gpiod_get_index_optional() for detailed information about behavior and
  108. * return values.
  109. */
  110. struct gpio_desc *__must_check __devm_gpiod_get_index_optional(struct device *dev,
  111. const char *con_id,
  112. unsigned int index,
  113. enum gpiod_flags flags)
  114. {
  115. struct gpio_desc *desc;
  116. desc = devm_gpiod_get_index(dev, con_id, index, flags);
  117. if (IS_ERR(desc)) {
  118. if (PTR_ERR(desc) == -ENOENT)
  119. return NULL;
  120. }
  121. return desc;
  122. }
  123. EXPORT_SYMBOL(__devm_gpiod_get_index_optional);
  124. /**
  125. * devm_gpiod_put - Resource-managed gpiod_put()
  126. * @desc: GPIO descriptor to dispose of
  127. *
  128. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  129. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  130. * will be disposed of by the resource management code.
  131. */
  132. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  133. {
  134. WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
  135. &desc));
  136. }
  137. EXPORT_SYMBOL(devm_gpiod_put);
  138. static void devm_gpio_release(struct device *dev, void *res)
  139. {
  140. unsigned *gpio = res;
  141. gpio_free(*gpio);
  142. }
  143. static int devm_gpio_match(struct device *dev, void *res, void *data)
  144. {
  145. unsigned *this = res, *gpio = data;
  146. return *this == *gpio;
  147. }
  148. /**
  149. * devm_gpio_request - request a GPIO for a managed device
  150. * @dev: device to request the GPIO for
  151. * @gpio: GPIO to allocate
  152. * @label: the name of the requested GPIO
  153. *
  154. * Except for the extra @dev argument, this function takes the
  155. * same arguments and performs the same function as
  156. * gpio_request(). GPIOs requested with this function will be
  157. * automatically freed on driver detach.
  158. *
  159. * If an GPIO allocated with this function needs to be freed
  160. * separately, devm_gpio_free() must be used.
  161. */
  162. int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
  163. {
  164. unsigned *dr;
  165. int rc;
  166. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  167. if (!dr)
  168. return -ENOMEM;
  169. rc = gpio_request(gpio, label);
  170. if (rc) {
  171. devres_free(dr);
  172. return rc;
  173. }
  174. *dr = gpio;
  175. devres_add(dev, dr);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(devm_gpio_request);
  179. /**
  180. * devm_gpio_request_one - request a single GPIO with initial setup
  181. * @dev: device to request for
  182. * @gpio: the GPIO number
  183. * @flags: GPIO configuration as specified by GPIOF_*
  184. * @label: a literal description string of this GPIO
  185. */
  186. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  187. unsigned long flags, const char *label)
  188. {
  189. unsigned *dr;
  190. int rc;
  191. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  192. if (!dr)
  193. return -ENOMEM;
  194. rc = gpio_request_one(gpio, flags, label);
  195. if (rc) {
  196. devres_free(dr);
  197. return rc;
  198. }
  199. *dr = gpio;
  200. devres_add(dev, dr);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(devm_gpio_request_one);
  204. /**
  205. * devm_gpio_free - free a GPIO
  206. * @dev: device to free GPIO for
  207. * @gpio: GPIO to free
  208. *
  209. * Except for the extra @dev argument, this function takes the
  210. * same arguments and performs the same function as gpio_free().
  211. * This function instead of gpio_free() should be used to manually
  212. * free GPIOs allocated with devm_gpio_request().
  213. */
  214. void devm_gpio_free(struct device *dev, unsigned int gpio)
  215. {
  216. WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
  217. &gpio));
  218. }
  219. EXPORT_SYMBOL(devm_gpio_free);