devres.c 11 KB

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