gpiolib-devres.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 <[email protected]>
  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_GPL(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_GPL(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. desc = gpiod_get_index(dev, con_id, idx, flags);
  88. if (IS_ERR(desc))
  89. return desc;
  90. /*
  91. * For non-exclusive GPIO descriptors, check if this descriptor is
  92. * already under resource management by this device.
  93. */
  94. if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
  95. struct devres *dres;
  96. dres = devres_find(dev, devm_gpiod_release,
  97. devm_gpiod_match, &desc);
  98. if (dres)
  99. return desc;
  100. }
  101. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  102. GFP_KERNEL);
  103. if (!dr) {
  104. gpiod_put(desc);
  105. return ERR_PTR(-ENOMEM);
  106. }
  107. *dr = desc;
  108. devres_add(dev, dr);
  109. return desc;
  110. }
  111. EXPORT_SYMBOL_GPL(devm_gpiod_get_index);
  112. /**
  113. * devm_gpiod_get_from_of_node() - obtain a GPIO from an OF node
  114. * @dev: device for lifecycle management
  115. * @node: handle of the OF node
  116. * @propname: name of the DT property representing the GPIO
  117. * @index: index of the GPIO to obtain for the consumer
  118. * @dflags: GPIO initialization flags
  119. * @label: label to attach to the requested GPIO
  120. *
  121. * Returns:
  122. * On successful request the GPIO pin is configured in accordance with
  123. * provided @dflags.
  124. *
  125. * In case of error an ERR_PTR() is returned.
  126. */
  127. struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev,
  128. const struct device_node *node,
  129. const char *propname, int index,
  130. enum gpiod_flags dflags,
  131. const char *label)
  132. {
  133. struct gpio_desc **dr;
  134. struct gpio_desc *desc;
  135. desc = gpiod_get_from_of_node(node, propname, index, dflags, label);
  136. if (IS_ERR(desc))
  137. return desc;
  138. /*
  139. * For non-exclusive GPIO descriptors, check if this descriptor is
  140. * already under resource management by this device.
  141. */
  142. if (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
  143. struct devres *dres;
  144. dres = devres_find(dev, devm_gpiod_release,
  145. devm_gpiod_match, &desc);
  146. if (dres)
  147. return desc;
  148. }
  149. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  150. GFP_KERNEL);
  151. if (!dr) {
  152. gpiod_put(desc);
  153. return ERR_PTR(-ENOMEM);
  154. }
  155. *dr = desc;
  156. devres_add(dev, dr);
  157. return desc;
  158. }
  159. EXPORT_SYMBOL_GPL(devm_gpiod_get_from_of_node);
  160. /**
  161. * devm_fwnode_gpiod_get_index - get a GPIO descriptor from a given node
  162. * @dev: GPIO consumer
  163. * @fwnode: firmware node containing GPIO reference
  164. * @con_id: function within the GPIO consumer
  165. * @index: index of the GPIO to obtain in the consumer
  166. * @flags: GPIO initialization flags
  167. * @label: label to attach to the requested GPIO
  168. *
  169. * GPIO descriptors returned from this function are automatically disposed on
  170. * driver detach.
  171. *
  172. * On successful request the GPIO pin is configured in accordance with
  173. * provided @flags.
  174. */
  175. struct gpio_desc *devm_fwnode_gpiod_get_index(struct device *dev,
  176. struct fwnode_handle *fwnode,
  177. const char *con_id, int index,
  178. enum gpiod_flags flags,
  179. const char *label)
  180. {
  181. struct gpio_desc **dr;
  182. struct gpio_desc *desc;
  183. dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),
  184. GFP_KERNEL);
  185. if (!dr)
  186. return ERR_PTR(-ENOMEM);
  187. desc = fwnode_gpiod_get_index(fwnode, con_id, index, flags, label);
  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_GPL(devm_fwnode_gpiod_get_index);
  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 (gpiod_not_found(desc))
  217. return NULL;
  218. return desc;
  219. }
  220. EXPORT_SYMBOL_GPL(devm_gpiod_get_index_optional);
  221. /**
  222. * devm_gpiod_get_array - Resource-managed gpiod_get_array()
  223. * @dev: GPIO consumer
  224. * @con_id: function within the GPIO consumer
  225. * @flags: optional GPIO initialization flags
  226. *
  227. * Managed gpiod_get_array(). GPIO descriptors returned from this function are
  228. * automatically disposed on driver detach. See gpiod_get_array() for detailed
  229. * information about behavior and return values.
  230. */
  231. struct gpio_descs *__must_check devm_gpiod_get_array(struct device *dev,
  232. const char *con_id,
  233. enum gpiod_flags flags)
  234. {
  235. struct gpio_descs **dr;
  236. struct gpio_descs *descs;
  237. dr = devres_alloc(devm_gpiod_release_array,
  238. sizeof(struct gpio_descs *), GFP_KERNEL);
  239. if (!dr)
  240. return ERR_PTR(-ENOMEM);
  241. descs = gpiod_get_array(dev, con_id, flags);
  242. if (IS_ERR(descs)) {
  243. devres_free(dr);
  244. return descs;
  245. }
  246. *dr = descs;
  247. devres_add(dev, dr);
  248. return descs;
  249. }
  250. EXPORT_SYMBOL_GPL(devm_gpiod_get_array);
  251. /**
  252. * devm_gpiod_get_array_optional - Resource-managed gpiod_get_array_optional()
  253. * @dev: GPIO consumer
  254. * @con_id: function within the GPIO consumer
  255. * @flags: optional GPIO initialization flags
  256. *
  257. * Managed gpiod_get_array_optional(). GPIO descriptors returned from this
  258. * function are automatically disposed on driver detach.
  259. * See gpiod_get_array_optional() for detailed information about behavior and
  260. * return values.
  261. */
  262. struct gpio_descs *__must_check
  263. devm_gpiod_get_array_optional(struct device *dev, const char *con_id,
  264. enum gpiod_flags flags)
  265. {
  266. struct gpio_descs *descs;
  267. descs = devm_gpiod_get_array(dev, con_id, flags);
  268. if (gpiod_not_found(descs))
  269. return NULL;
  270. return descs;
  271. }
  272. EXPORT_SYMBOL_GPL(devm_gpiod_get_array_optional);
  273. /**
  274. * devm_gpiod_put - Resource-managed gpiod_put()
  275. * @dev: GPIO consumer
  276. * @desc: GPIO descriptor to dispose of
  277. *
  278. * Dispose of a GPIO descriptor obtained with devm_gpiod_get() or
  279. * devm_gpiod_get_index(). Normally this function will not be called as the GPIO
  280. * will be disposed of by the resource management code.
  281. */
  282. void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
  283. {
  284. WARN_ON(devres_release(dev, devm_gpiod_release, devm_gpiod_match,
  285. &desc));
  286. }
  287. EXPORT_SYMBOL_GPL(devm_gpiod_put);
  288. /**
  289. * devm_gpiod_unhinge - Remove resource management from a gpio descriptor
  290. * @dev: GPIO consumer
  291. * @desc: GPIO descriptor to remove resource management from
  292. *
  293. * Remove resource management from a GPIO descriptor. This is needed when
  294. * you want to hand over lifecycle management of a descriptor to another
  295. * mechanism.
  296. */
  297. void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc)
  298. {
  299. int ret;
  300. if (IS_ERR_OR_NULL(desc))
  301. return;
  302. ret = devres_destroy(dev, devm_gpiod_release,
  303. devm_gpiod_match, &desc);
  304. /*
  305. * If the GPIO descriptor is requested as nonexclusive, we
  306. * may call this function several times on the same descriptor
  307. * so it is OK if devres_destroy() returns -ENOENT.
  308. */
  309. if (ret == -ENOENT)
  310. return;
  311. /* Anything else we should warn about */
  312. WARN_ON(ret);
  313. }
  314. EXPORT_SYMBOL_GPL(devm_gpiod_unhinge);
  315. /**
  316. * devm_gpiod_put_array - Resource-managed gpiod_put_array()
  317. * @dev: GPIO consumer
  318. * @descs: GPIO descriptor array to dispose of
  319. *
  320. * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
  321. * Normally this function will not be called as the GPIOs will be disposed of
  322. * by the resource management code.
  323. */
  324. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
  325. {
  326. WARN_ON(devres_release(dev, devm_gpiod_release_array,
  327. devm_gpiod_match_array, &descs));
  328. }
  329. EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
  330. static void devm_gpio_release(struct device *dev, void *res)
  331. {
  332. unsigned *gpio = res;
  333. gpio_free(*gpio);
  334. }
  335. /**
  336. * devm_gpio_request - request a GPIO for a managed device
  337. * @dev: device to request the GPIO for
  338. * @gpio: GPIO to allocate
  339. * @label: the name of the requested GPIO
  340. *
  341. * Except for the extra @dev argument, this function takes the
  342. * same arguments and performs the same function as
  343. * gpio_request(). GPIOs requested with this function will be
  344. * automatically freed on driver detach.
  345. */
  346. int devm_gpio_request(struct device *dev, unsigned gpio, 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(gpio, 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_GPL(devm_gpio_request);
  363. /**
  364. * devm_gpio_request_one - request a single GPIO with initial setup
  365. * @dev: device to request for
  366. * @gpio: the GPIO number
  367. * @flags: GPIO configuration as specified by GPIOF_*
  368. * @label: a literal description string of this GPIO
  369. */
  370. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  371. unsigned long flags, const char *label)
  372. {
  373. unsigned *dr;
  374. int rc;
  375. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  376. if (!dr)
  377. return -ENOMEM;
  378. rc = gpio_request_one(gpio, flags, label);
  379. if (rc) {
  380. devres_free(dr);
  381. return rc;
  382. }
  383. *dr = gpio;
  384. devres_add(dev, dr);
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(devm_gpio_request_one);
  388. static void devm_gpio_chip_release(void *data)
  389. {
  390. struct gpio_chip *gc = data;
  391. gpiochip_remove(gc);
  392. }
  393. /**
  394. * devm_gpiochip_add_data_with_key() - Resource managed gpiochip_add_data_with_key()
  395. * @dev: pointer to the device that gpio_chip belongs to.
  396. * @gc: the GPIO chip to register
  397. * @data: driver-private data associated with this chip
  398. * @lock_key: lockdep class for IRQ lock
  399. * @request_key: lockdep class for IRQ request
  400. *
  401. * Context: potentially before irqs will work
  402. *
  403. * The gpio chip automatically be released when the device is unbound.
  404. *
  405. * Returns:
  406. * A negative errno if the chip can't be registered, such as because the
  407. * gc->base is invalid or already associated with a different chip.
  408. * Otherwise it returns zero as a success code.
  409. */
  410. int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
  411. struct lock_class_key *lock_key,
  412. struct lock_class_key *request_key)
  413. {
  414. int ret;
  415. ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key);
  416. if (ret < 0)
  417. return ret;
  418. return devm_add_action_or_reset(dev, devm_gpio_chip_release, gc);
  419. }
  420. EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key);