devres.c 5.1 KB

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