devres.c 11 KB

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