devres.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. static void devm_gpiod_release_array(struct device *dev, void *res)
  33. {
  34. struct gpio_descs **descs = res;
  35. gpiod_put_array(*descs);
  36. }
  37. static int devm_gpiod_match_array(struct device *dev, void *res, void *data)
  38. {
  39. struct gpio_descs **this = res, **gpios = data;
  40. return *this == *gpios;
  41. }
  42. /**
  43. * devm_gpiod_get - Resource-managed gpiod_get()
  44. * @dev: GPIO consumer
  45. * @con_id: function within the GPIO consumer
  46. * @flags: optional GPIO initialization flags
  47. *
  48. * Managed gpiod_get(). GPIO descriptors returned from this function are
  49. * automatically disposed on driver detach. See gpiod_get() for detailed
  50. * information about behavior and return values.
  51. */
  52. struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
  53. const char *con_id,
  54. enum gpiod_flags flags)
  55. {
  56. return devm_gpiod_get_index(dev, con_id, 0, flags);
  57. }
  58. EXPORT_SYMBOL(devm_gpiod_get);
  59. /**
  60. * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()
  61. * @dev: GPIO consumer
  62. * @con_id: function within the GPIO consumer
  63. * @flags: optional GPIO initialization flags
  64. *
  65. * Managed gpiod_get_optional(). GPIO descriptors returned from this function
  66. * are automatically disposed on driver detach. See gpiod_get_optional() for
  67. * detailed information about behavior and return values.
  68. */
  69. struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
  70. const char *con_id,
  71. enum gpiod_flags flags)
  72. {
  73. return devm_gpiod_get_index_optional(dev, con_id, 0, flags);
  74. }
  75. EXPORT_SYMBOL(devm_gpiod_get_optional);
  76. /**
  77. * devm_gpiod_get_index - Resource-managed gpiod_get_index()
  78. * @dev: GPIO consumer
  79. * @con_id: function within the GPIO consumer
  80. * @idx: index of the GPIO to obtain in the consumer
  81. * @flags: optional GPIO initialization flags
  82. *
  83. * Managed gpiod_get_index(). GPIO descriptors returned from this function are
  84. * automatically disposed on driver detach. See gpiod_get_index() for detailed
  85. * information about behavior and return values.
  86. */
  87. struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
  88. const char *con_id,
  89. unsigned int idx,
  90. enum gpiod_flags flags)
  91. {
  92. struct gpio_desc **dr;
  93. struct gpio_desc *desc;
  94. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  95. GFP_KERNEL);
  96. if (!dr)
  97. return ERR_PTR(-ENOMEM);
  98. desc = gpiod_get_index(dev, con_id, idx, flags);
  99. if (IS_ERR(desc)) {
  100. devres_free(dr);
  101. return desc;
  102. }
  103. *dr = desc;
  104. devres_add(dev, dr);
  105. return desc;
  106. }
  107. EXPORT_SYMBOL(devm_gpiod_get_index);
  108. /**
  109. * devm_get_gpiod_from_child - get a GPIO descriptor from a device's child node
  110. * @dev: GPIO consumer
  111. * @con_id: function within the GPIO consumer
  112. * @child: firmware node (child of @dev)
  113. *
  114. * GPIO descriptors returned from this function are automatically disposed on
  115. * driver detach.
  116. */
  117. struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
  118. const char *con_id,
  119. struct fwnode_handle *child)
  120. {
  121. static const char * const suffixes[] = { "gpios", "gpio" };
  122. char prop_name[32]; /* 32 is max size of property name */
  123. struct gpio_desc **dr;
  124. struct gpio_desc *desc;
  125. unsigned int i;
  126. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  127. GFP_KERNEL);
  128. if (!dr)
  129. return ERR_PTR(-ENOMEM);
  130. for (i = 0; i < ARRAY_SIZE(suffixes); i++) {
  131. if (con_id)
  132. snprintf(prop_name, sizeof(prop_name), "%s-%s",
  133. con_id, suffixes[i]);
  134. else
  135. snprintf(prop_name, sizeof(prop_name), "%s",
  136. suffixes[i]);
  137. desc = fwnode_get_named_gpiod(child, prop_name);
  138. if (!IS_ERR(desc) || (PTR_ERR(desc) == -EPROBE_DEFER))
  139. break;
  140. }
  141. if (IS_ERR(desc)) {
  142. devres_free(dr);
  143. return desc;
  144. }
  145. *dr = desc;
  146. devres_add(dev, dr);
  147. return desc;
  148. }
  149. EXPORT_SYMBOL(devm_get_gpiod_from_child);
  150. /**
  151. * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
  152. * @dev: GPIO consumer
  153. * @con_id: function within the GPIO consumer
  154. * @index: index of the GPIO to obtain in the consumer
  155. * @flags: optional GPIO initialization flags
  156. *
  157. * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
  158. * function are automatically disposed on driver detach. See
  159. * gpiod_get_index_optional() for detailed information about behavior and
  160. * return values.
  161. */
  162. struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
  163. const char *con_id,
  164. unsigned int index,
  165. enum gpiod_flags flags)
  166. {
  167. struct gpio_desc *desc;
  168. desc = devm_gpiod_get_index(dev, con_id, index, flags);
  169. if (IS_ERR(desc)) {
  170. if (PTR_ERR(desc) == -ENOENT)
  171. return NULL;
  172. }
  173. return desc;
  174. }
  175. EXPORT_SYMBOL(devm_gpiod_get_index_optional);
  176. /**
  177. * devm_gpiod_get_array - Resource-managed gpiod_get_array()
  178. * @dev: GPIO consumer
  179. * @con_id: function within the GPIO consumer
  180. * @flags: optional GPIO initialization flags
  181. *
  182. * Managed gpiod_get_array(). GPIO descriptors returned from this function are
  183. * automatically disposed on driver detach. See gpiod_get_array() for detailed
  184. * information about behavior and return values.
  185. */
  186. struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
  187. const char *con_id,
  188. enum gpiod_flags flags)
  189. {
  190. struct gpio_descs **dr;
  191. struct gpio_descs *descs;
  192. dr = devres_alloc(devm_gpiod_release_array,
  193. sizeof(struct gpio_descs *), GFP_KERNEL);
  194. if (!dr)
  195. return ERR_PTR(-ENOMEM);
  196. descs = gpiod_get_array(dev, con_id, flags);
  197. if (IS_ERR(descs)) {
  198. devres_free(dr);
  199. return descs;
  200. }
  201. *dr = descs;
  202. devres_add(dev, dr);
  203. return descs;
  204. }
  205. EXPORT_SYMBOL(devm_gpiod_get_array);
  206. /**
  207. * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
  208. * @dev: GPIO consumer
  209. * @con_id: function within the GPIO consumer
  210. * @flags: optional GPIO initialization flags
  211. *
  212. * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
  213. * function are automatically disposed on driver detach.
  214. * See gpiod_get_array_optional() for detailed information about behavior and
  215. * return values.
  216. */
  217. struct gpio_descs *__must_check
  218. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  219. enum gpiod_flags flags)
  220. {
  221. struct gpio_descs *descs;
  222. descs = devm_gpiod_get_array(dev, con_id, flags);
  223. if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
  224. return NULL;
  225. return descs;
  226. }
  227. EXPORT_SYMBOL(devm_gpiod_get_array_optional);
  228. /**
  229. * devm_gpiod_put - Resource-managed gpiod_put()
  230. * @desc: GPIO descriptor to dispose of
  231. *
  232. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  233. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  234. * will be disposed of by the resource management code.
  235. */
  236. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  237. {
  238. WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
  239. &desc));
  240. }
  241. EXPORT_SYMBOL(devm_gpiod_put);
  242. /**
  243. * devm_gpiod_put_array - Resource-managed gpiod_put_array()
  244. * @descs: GPIO descriptor array to dispose of
  245. *
  246. * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
  247. * Normally this function will not be called as the GPIOs will be disposed of
  248. * by the resource management code.
  249. */
  250. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
  251. {
  252. WARN_ON(devres_release(dev, devm_gpiod_release_array,
  253. devm_gpiod_match_array, &descs));
  254. }
  255. EXPORT_SYMBOL(devm_gpiod_put_array);
  256. static void devm_gpio_release(struct device *dev, void *res)
  257. {
  258. unsigned *gpio = res;
  259. gpio_free(*gpio);
  260. }
  261. static int devm_gpio_match(struct device *dev, void *res, void *data)
  262. {
  263. unsigned *this = res, *gpio = data;
  264. return *this == *gpio;
  265. }
  266. /**
  267. * devm_gpio_request - request a GPIO for a managed device
  268. * @dev: device to request the GPIO for
  269. * @gpio: GPIO to allocate
  270. * @label: the name of the requested GPIO
  271. *
  272. * Except for the extra @dev argument, this function takes the
  273. * same arguments and performs the same function as
  274. * gpio_request(). GPIOs requested with this function will be
  275. * automatically freed on driver detach.
  276. *
  277. * If an GPIO allocated with this function needs to be freed
  278. * separately, devm_gpio_free() must be used.
  279. */
  280. int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
  281. {
  282. unsigned *dr;
  283. int rc;
  284. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  285. if (!dr)
  286. return -ENOMEM;
  287. rc = gpio_request(gpio, label);
  288. if (rc) {
  289. devres_free(dr);
  290. return rc;
  291. }
  292. *dr = gpio;
  293. devres_add(dev, dr);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(devm_gpio_request);
  297. /**
  298. * devm_gpio_request_one - request a single GPIO with initial setup
  299. * @dev: device to request for
  300. * @gpio: the GPIO number
  301. * @flags: GPIO configuration as specified by GPIOF_*
  302. * @label: a literal description string of this GPIO
  303. */
  304. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  305. unsigned long flags, const char *label)
  306. {
  307. unsigned *dr;
  308. int rc;
  309. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  310. if (!dr)
  311. return -ENOMEM;
  312. rc = gpio_request_one(gpio, flags, label);
  313. if (rc) {
  314. devres_free(dr);
  315. return rc;
  316. }
  317. *dr = gpio;
  318. devres_add(dev, dr);
  319. return 0;
  320. }
  321. EXPORT_SYMBOL(devm_gpio_request_one);
  322. /**
  323. * devm_gpio_free - free a GPIO
  324. * @dev: device to free GPIO for
  325. * @gpio: GPIO to free
  326. *
  327. * Except for the extra @dev argument, this function takes the
  328. * same arguments and performs the same function as gpio_free().
  329. * This function instead of gpio_free() should be used to manually
  330. * free GPIOs allocated with devm_gpio_request().
  331. */
  332. void devm_gpio_free(struct device *dev, unsigned int gpio)
  333. {
  334. WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
  335. &gpio));
  336. }
  337. EXPORT_SYMBOL(devm_gpio_free);