gpiolib-devres.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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_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. 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 (IS_ERR(desc)) {
  217. if (PTR_ERR(desc) == -ENOENT)
  218. return NULL;
  219. }
  220. return desc;
  221. }
  222. EXPORT_SYMBOL_GPL(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_GPL(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 (PTR_ERR(descs) == -ENOENT)
  271. return NULL;
  272. return descs;
  273. }
  274. EXPORT_SYMBOL_GPL(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_GPL(devm_gpiod_put);
  290. /**
  291. * devm_gpiod_unhinge - Remove resource management from a gpio descriptor
  292. * @dev: GPIO consumer
  293. * @desc: GPIO descriptor to remove resource management from
  294. *
  295. * Remove resource management from a GPIO descriptor. This is needed when
  296. * you want to hand over lifecycle management of a descriptor to another
  297. * mechanism.
  298. */
  299. void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc)
  300. {
  301. int ret;
  302. if (IS_ERR_OR_NULL(desc))
  303. return;
  304. ret = devres_destroy(dev, devm_gpiod_release,
  305. devm_gpiod_match, &desc);
  306. /*
  307. * If the GPIO descriptor is requested as nonexclusive, we
  308. * may call this function several times on the same descriptor
  309. * so it is OK if devres_destroy() returns -ENOENT.
  310. */
  311. if (ret == -ENOENT)
  312. return;
  313. /* Anything else we should warn about */
  314. WARN_ON(ret);
  315. }
  316. EXPORT_SYMBOL_GPL(devm_gpiod_unhinge);
  317. /**
  318. * devm_gpiod_put_array - Resource-managed gpiod_put_array()
  319. * @dev: GPIO consumer
  320. * @descs: GPIO descriptor array to dispose of
  321. *
  322. * Dispose of an array of GPIO descriptors obtained with devm_gpiod_get_array().
  323. * Normally this function will not be called as the GPIOs will be disposed of
  324. * by the resource management code.
  325. */
  326. void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
  327. {
  328. WARN_ON(devres_release(dev, devm_gpiod_release_array,
  329. devm_gpiod_match_array, &descs));
  330. }
  331. EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
  332. static void devm_gpio_release(struct device *dev, void *res)
  333. {
  334. unsigned *gpio = res;
  335. gpio_free(*gpio);
  336. }
  337. static int devm_gpio_match(struct device *dev, void *res, void *data)
  338. {
  339. unsigned *this = res, *gpio = data;
  340. return *this == *gpio;
  341. }
  342. /**
  343. * devm_gpio_request - request a GPIO for a managed device
  344. * @dev: device to request the GPIO for
  345. * @gpio: GPIO to allocate
  346. * @label: the name of the requested GPIO
  347. *
  348. * Except for the extra @dev argument, this function takes the
  349. * same arguments and performs the same function as
  350. * gpio_request(). GPIOs requested with this function will be
  351. * automatically freed on driver detach.
  352. *
  353. * If an GPIO allocated with this function needs to be freed
  354. * separately, devm_gpio_free() must be used.
  355. */
  356. int devm_gpio_request(struct device *dev, unsigned gpio, const char *label)
  357. {
  358. unsigned *dr;
  359. int rc;
  360. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  361. if (!dr)
  362. return -ENOMEM;
  363. rc = gpio_request(gpio, label);
  364. if (rc) {
  365. devres_free(dr);
  366. return rc;
  367. }
  368. *dr = gpio;
  369. devres_add(dev, dr);
  370. return 0;
  371. }
  372. EXPORT_SYMBOL_GPL(devm_gpio_request);
  373. /**
  374. * devm_gpio_request_one - request a single GPIO with initial setup
  375. * @dev: device to request for
  376. * @gpio: the GPIO number
  377. * @flags: GPIO configuration as specified by GPIOF_*
  378. * @label: a literal description string of this GPIO
  379. */
  380. int devm_gpio_request_one(struct device *dev, unsigned gpio,
  381. unsigned long flags, const char *label)
  382. {
  383. unsigned *dr;
  384. int rc;
  385. dr = devres_alloc(devm_gpio_release, sizeof(unsigned), GFP_KERNEL);
  386. if (!dr)
  387. return -ENOMEM;
  388. rc = gpio_request_one(gpio, flags, label);
  389. if (rc) {
  390. devres_free(dr);
  391. return rc;
  392. }
  393. *dr = gpio;
  394. devres_add(dev, dr);
  395. return 0;
  396. }
  397. EXPORT_SYMBOL_GPL(devm_gpio_request_one);
  398. /**
  399. * devm_gpio_free - free a GPIO
  400. * @dev: device to free GPIO for
  401. * @gpio: GPIO to free
  402. *
  403. * Except for the extra @dev argument, this function takes the
  404. * same arguments and performs the same function as gpio_free().
  405. * This function instead of gpio_free() should be used to manually
  406. * free GPIOs allocated with devm_gpio_request().
  407. */
  408. void devm_gpio_free(struct device *dev, unsigned int gpio)
  409. {
  410. WARN_ON(devres_release(dev, devm_gpio_release, devm_gpio_match,
  411. &gpio));
  412. }
  413. EXPORT_SYMBOL_GPL(devm_gpio_free);
  414. static void devm_gpio_chip_release(struct device *dev, void *res)
  415. {
  416. struct gpio_chip *gc = *(struct gpio_chip **)res;
  417. gpiochip_remove(gc);
  418. }
  419. /**
  420. * devm_gpiochip_add_data_with_key() - Resource managed gpiochip_add_data_with_key()
  421. * @dev: pointer to the device that gpio_chip belongs to.
  422. * @gc: the GPIO chip to register
  423. * @data: driver-private data associated with this chip
  424. * @lock_key: lockdep class for IRQ lock
  425. * @request_key: lockdep class for IRQ request
  426. *
  427. * Context: potentially before irqs will work
  428. *
  429. * The gpio chip automatically be released when the device is unbound.
  430. *
  431. * Returns:
  432. * A negative errno if the chip can't be registered, such as because the
  433. * gc->base is invalid or already associated with a different chip.
  434. * Otherwise it returns zero as a success code.
  435. */
  436. int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, void *data,
  437. struct lock_class_key *lock_key,
  438. struct lock_class_key *request_key)
  439. {
  440. struct gpio_chip **ptr;
  441. int ret;
  442. ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
  443. GFP_KERNEL);
  444. if (!ptr)
  445. return -ENOMEM;
  446. ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key);
  447. if (ret < 0) {
  448. devres_free(ptr);
  449. return ret;
  450. }
  451. *ptr = gc;
  452. devres_add(dev, ptr);
  453. return 0;
  454. }
  455. EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key);