devres.c 5.1 KB

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