devres.c 12 KB

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