devres.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. *
  118. * GPIO descriptors returned from this function are automatically disposed on
  119. * driver detach.
  120. *
  121. * On successful request the GPIO pin is configured in accordance with
  122. * provided @flags.
  123. */
  124. struct gpio_desc *devm_fwnode_get_index_gpiod_from_child(struct device *dev,
  125. const char *con_id, int index,
  126. struct fwnode_handle *child,
  127. enum gpiod_flags flags,
  128. const char *label)
  129. {
  130. char prop_name[32]; /* 32 is max size of property name */
  131. struct gpio_desc **dr;
  132. struct gpio_desc *desc;
  133. unsigned int i;
  134. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  135. GFP_KERNEL);
  136. if (!dr)
  137. return ERR_PTR(-ENOMEM);
  138. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  139. if (con_id)
  140. snprintf(prop_name, sizeof(prop_name), "%s-%s",
  141. con_id, gpio_suffixes[i]);
  142. else
  143. snprintf(prop_name, sizeof(prop_name), "%s",
  144. gpio_suffixes[i]);
  145. desc = fwnode_get_named_gpiod(child, prop_name, index, flags,
  146. label);
  147. if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
  148. break;
  149. }
  150. if (IS_ERR(desc)) {
  151. devres_free(dr);
  152. return desc;
  153. }
  154. *dr = desc;
  155. devres_add(dev, dr);
  156. return desc;
  157. }
  158. EXPORT_SYMBOL(devm_fwnode_get_index_gpiod_from_child);
  159. /**
  160. * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()
  161. * @dev: GPIO consumer
  162. * @con_id: function within the GPIO consumer
  163. * @index: index of the GPIO to obtain in the consumer
  164. * @flags: optional GPIO initialization flags
  165. *
  166. * Managed gpiod_get_index_optional(). GPIO descriptors returned from this
  167. * function are automatically disposed on driver detach. See
  168. * gpiod_get_index_optional() for detailed information about behavior and
  169. * return values.
  170. */
  171. struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,
  172. const char *con_id,
  173. unsigned int index,
  174. enum gpiod_flags flags)
  175. {
  176. struct gpio_desc *desc;
  177. desc = devm_gpiod_get_index(dev, con_id, index, flags);
  178. if (IS_ERR(desc)) {
  179. if (PTR_ERR(desc) == -ENOENT)
  180. return NULL;
  181. }
  182. return desc;
  183. }
  184. EXPORT_SYMBOL(devm_gpiod_get_index_optional);
  185. /**
  186. * devm_gpiod_get_array - Resource-managed gpiod_get_array()
  187. * @dev: GPIO consumer
  188. * @con_id: function within the GPIO consumer
  189. * @flags: optional GPIO initialization flags
  190. *
  191. * Managed gpiod_get_array(). GPIO descriptors returned from this function are
  192. * automatically disposed on driver detach. See gpiod_get_array() for detailed
  193. * information about behavior and return values.
  194. */
  195. struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
  196. const char *con_id,
  197. enum gpiod_flags flags)
  198. {
  199. struct gpio_descs **dr;
  200. struct gpio_descs *descs;
  201. dr = devres_alloc(devm_gpiod_release_array,
  202. sizeof(struct gpio_descs *), GFP_KERNEL);
  203. if (!dr)
  204. return ERR_PTR(-ENOMEM);
  205. descs = gpiod_get_array(dev, con_id, flags);
  206. if (IS_ERR(descs)) {
  207. devres_free(dr);
  208. return descs;
  209. }
  210. *dr = descs;
  211. devres_add(dev, dr);
  212. return descs;
  213. }
  214. EXPORT_SYMBOL(devm_gpiod_get_array);
  215. /**
  216. * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
  217. * @dev: GPIO consumer
  218. * @con_id: function within the GPIO consumer
  219. * @flags: optional GPIO initialization flags
  220. *
  221. * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
  222. * function are automatically disposed on driver detach.
  223. * See gpiod_get_array_optional() for detailed information about behavior and
  224. * return values.
  225. */
  226. struct gpio_descs *__must_check
  227. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  228. enum gpiod_flags flags)
  229. {
  230. struct gpio_descs *descs;
  231. descs = devm_gpiod_get_array(dev, con_id, flags);
  232. if (IS_ERR(descs) && (PTR_ERR(descs) == -ENOENT))
  233. return NULL;
  234. return descs;
  235. }
  236. EXPORT_SYMBOL(devm_gpiod_get_array_optional);
  237. /**
  238. * devm_gpiod_put - Resource-managed gpiod_put()
  239. * @desc: GPIO descriptor to dispose of
  240. *
  241. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  242. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  243. * will be disposed of by the resource management code.
  244. */
  245. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  246. {
  247. WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
  248. &desc));
  249. }
  250. EXPORT_SYMBOL(devm_gpiod_put);
  251. /**
  252. * devm_gpiod_put_array - Resource-managed gpiod_put_array()
  253. * @descs: GPIO descriptor array to dispose of
  254. *
  255. * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
  256. * Normally this function will not be called as the GPIOs will be disposed of
  257. * by the resource management code.
  258. */
  259. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
  260. {
  261. WARN_ON(devres_release(dev, devm_gpiod_release_array,
  262. devm_gpiod_match_array, &descs));
  263. }
  264. EXPORT_SYMBOL(devm_gpiod_put_array);
  265. static void devm_gpio_release(struct device *dev, void *res)
  266. {
  267. unsigned *gpio = res;
  268. gpio_free(*gpio);
  269. }
  270. static int devm_gpio_match(struct device *dev, void *res, void *data)
  271. {
  272. unsigned *this = res, *gpio = data;
  273. return *this == *gpio;
  274. }
  275. /**
  276. * devm_gpio_request - request a GPIO for a managed device
  277. * @dev: device to request the GPIO for
  278. * @gpio: GPIO to allocate
  279. * @label: the name of the requested GPIO
  280. *
  281. * Except for the extra @dev argument, this function takes the
  282. * same arguments and performs the same function as
  283. * gpio_request(). GPIOs requested with this function will be
  284. * automatically freed on driver detach.
  285. *
  286. * If an GPIO allocated with this function needs to be freed
  287. * separately, devm_gpio_free() must be used.
  288. */
  289. int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
  290. {
  291. unsigned *dr;
  292. int rc;
  293. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  294. if (!dr)
  295. return -ENOMEM;
  296. rc = gpio_request(gpio, label);
  297. if (rc) {
  298. devres_free(dr);
  299. return rc;
  300. }
  301. *dr = gpio;
  302. devres_add(dev, dr);
  303. return 0;
  304. }
  305. EXPORT_SYMBOL(devm_gpio_request);
  306. /**
  307. * devm_gpio_request_one - request a single GPIO with initial setup
  308. * @dev: device to request for
  309. * @gpio: the GPIO number
  310. * @flags: GPIO configuration as specified by GPIOF_*
  311. * @label: a literal description string of this GPIO
  312. */
  313. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  314. unsigned long flags, const char *label)
  315. {
  316. unsigned *dr;
  317. int rc;
  318. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  319. if (!dr)
  320. return -ENOMEM;
  321. rc = gpio_request_one(gpio, flags, label);
  322. if (rc) {
  323. devres_free(dr);
  324. return rc;
  325. }
  326. *dr = gpio;
  327. devres_add(dev, dr);
  328. return 0;
  329. }
  330. EXPORT_SYMBOL(devm_gpio_request_one);
  331. /**
  332. * devm_gpio_free - free a GPIO
  333. * @dev: device to free GPIO for
  334. * @gpio: GPIO to free
  335. *
  336. * Except for the extra @dev argument, this function takes the
  337. * same arguments and performs the same function as gpio_free().
  338. * This function instead of gpio_free() should be used to manually
  339. * free GPIOs allocated with devm_gpio_request().
  340. */
  341. void devm_gpio_free(struct device *dev, unsigned int gpio)
  342. {
  343. WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
  344. &gpio));
  345. }
  346. EXPORT_SYMBOL(devm_gpio_free);