gpiolib-devres.c 12 KB

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