devres.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. *
  37. * Managed gpiod_get(). GPIO descriptors returned from this function are
  38. * automatically disposed on driver detach. See gpiod_get() for detailed
  39. * information about behavior and return values.
  40. */
  41. struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  42. const char *con_id)
  43. {
  44. return devm_gpiod_get_index(dev, con_id, 0);
  45. }
  46. EXPORT_SYMBOL(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. *
  52. * Managed gpiod_get_optional(). GPIO descriptors returned from this function
  53. * are automatically disposed on driver detach. See gpiod_get_optional() for
  54. * detailed information about behavior and return values.
  55. */
  56. struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
  57. const char *con_id)
  58. {
  59. return devm_gpiod_get_index_optional(dev, con_id, 0);
  60. }
  61. EXPORT_SYMBOL(devm_gpiod_get_optional);
  62. /**
  63. * devm_gpiod_get_index - Resource-managed gpiod_get_index()
  64. * @dev: GPIO consumer
  65. * @con_id: function within the GPIO consumer
  66. * @idx: index of the GPIO to obtain in the consumer
  67. *
  68. * Managed gpiod_get_index(). GPIO descriptors returned from this function are
  69. * automatically disposed on driver detach. See gpiod_get_index() for detailed
  70. * information about behavior and return values.
  71. */
  72. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  73. const char *con_id,
  74. unsigned int idx)
  75. {
  76. struct gpio_desc **dr;
  77. struct gpio_desc *desc;
  78. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpiod_desc *),
  79. GFP_KERNEL);
  80. if (!dr)
  81. return ERR_PTR(-ENOMEM);
  82. desc = gpiod_get_index(dev, con_id, idx);
  83. if (IS_ERR(desc)) {
  84. devres_free(dr);
  85. return desc;
  86. }
  87. *dr = desc;
  88. devres_add(dev, dr);
  89. return desc;
  90. }
  91. EXPORT_SYMBOL(devm_gpiod_get_index);
  92. /**
  93. * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
  94. * @dev: GPIO consumer
  95. * @con_id: function within the GPIO consumer
  96. * @index: index of the GPIO to obtain in the consumer
  97. *
  98. * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
  99. * function are automatically disposed on driver detach. See
  100. * gpiod_get_index_optional() for detailed information about behavior and
  101. * return values.
  102. */
  103. struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
  104. const char *con_id,
  105. unsigned int index)
  106. {
  107. struct gpio_desc *desc;
  108. desc = devm_gpiod_get_index(dev, con_id, index);
  109. if (IS_ERR(desc)) {
  110. if (PTR_ERR(desc) == -ENOENT)
  111. return NULL;
  112. }
  113. return desc;
  114. }
  115. EXPORT_SYMBOL(devm_gpiod_get_index_optional);
  116. /**
  117. * devm_gpiod_put - Resource-managed gpiod_put()
  118. * @desc: GPIO descriptor to dispose of
  119. *
  120. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  121. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  122. * will be disposed of by the resource management code.
  123. */
  124. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  125. {
  126. WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
  127. &desc));
  128. }
  129. EXPORT_SYMBOL(devm_gpiod_put);
  130. static void devm_gpio_release(struct device *dev, void *res)
  131. {
  132. unsigned *gpio = res;
  133. gpio_free(*gpio);
  134. }
  135. static int devm_gpio_match(struct device *dev, void *res, void *data)
  136. {
  137. unsigned *this = res, *gpio = data;
  138. return *this == *gpio;
  139. }
  140. /**
  141. * devm_gpio_request - request a GPIO for a managed device
  142. * @dev: device to request the GPIO for
  143. * @gpio: GPIO to allocate
  144. * @label: the name of the requested GPIO
  145. *
  146. * Except for the extra @dev argument, this function takes the
  147. * same arguments and performs the same function as
  148. * gpio_request(). GPIOs requested with this function will be
  149. * automatically freed on driver detach.
  150. *
  151. * If an GPIO allocated with this function needs to be freed
  152. * separately, devm_gpio_free() must be used.
  153. */
  154. int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
  155. {
  156. unsigned *dr;
  157. int rc;
  158. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  159. if (!dr)
  160. return -ENOMEM;
  161. rc = gpio_request(gpio, label);
  162. if (rc) {
  163. devres_free(dr);
  164. return rc;
  165. }
  166. *dr = gpio;
  167. devres_add(dev, dr);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL(devm_gpio_request);
  171. /**
  172. * devm_gpio_request_one - request a single GPIO with initial setup
  173. * @dev: device to request for
  174. * @gpio: the GPIO number
  175. * @flags: GPIO configuration as specified by GPIOF_*
  176. * @label: a literal description string of this GPIO
  177. */
  178. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  179. unsigned long flags, const char *label)
  180. {
  181. unsigned *dr;
  182. int rc;
  183. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  184. if (!dr)
  185. return -ENOMEM;
  186. rc = gpio_request_one(gpio, flags, label);
  187. if (rc) {
  188. devres_free(dr);
  189. return rc;
  190. }
  191. *dr = gpio;
  192. devres_add(dev, dr);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL(devm_gpio_request_one);
  196. /**
  197. * devm_gpio_free - free a GPIO
  198. * @dev: device to free GPIO for
  199. * @gpio: GPIO to free
  200. *
  201. * Except for the extra @dev argument, this function takes the
  202. * same arguments and performs the same function as gpio_free().
  203. * This function instead of gpio_free() should be used to manually
  204. * free GPIOs allocated with devm_gpio_request().
  205. */
  206. void devm_gpio_free(struct device *dev, unsigned int gpio)
  207. {
  208. WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
  209. &gpio));
  210. }
  211. EXPORT_SYMBOL(devm_gpio_free);