devres.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * devres.c -- Voltage/Current Regulator framework devres implementation.
  4. *
  5. * Copyright 2013 Linaro Ltd
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/err.h>
  9. #include <linux/regmap.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/regulator/driver.h>
  12. #include <linux/module.h>
  13. #include "internal.h"
  14. static void devm_regulator_release(struct device *dev, void *res)
  15. {
  16. regulator_put(*(struct regulator **)res);
  17. }
  18. static struct regulator *_devm_regulator_get(struct device *dev, const char *id,
  19. int get_type)
  20. {
  21. struct regulator **ptr, *regulator;
  22. ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
  23. if (!ptr)
  24. return ERR_PTR(-ENOMEM);
  25. regulator = _regulator_get(dev, id, get_type);
  26. if (!IS_ERR(regulator)) {
  27. *ptr = regulator;
  28. devres_add(dev, ptr);
  29. } else {
  30. devres_free(ptr);
  31. }
  32. return regulator;
  33. }
  34. /**
  35. * devm_regulator_get - Resource managed regulator_get()
  36. * @dev: device to supply
  37. * @id: supply name or regulator ID.
  38. *
  39. * Managed regulator_get(). Regulators returned from this function are
  40. * automatically regulator_put() on driver detach. See regulator_get() for more
  41. * information.
  42. */
  43. struct regulator *devm_regulator_get(struct device *dev, const char *id)
  44. {
  45. return _devm_regulator_get(dev, id, NORMAL_GET);
  46. }
  47. EXPORT_SYMBOL_GPL(devm_regulator_get);
  48. /**
  49. * devm_regulator_get_exclusive - Resource managed regulator_get_exclusive()
  50. * @dev: device to supply
  51. * @id: supply name or regulator ID.
  52. *
  53. * Managed regulator_get_exclusive(). Regulators returned from this function
  54. * are automatically regulator_put() on driver detach. See regulator_get() for
  55. * more information.
  56. */
  57. struct regulator *devm_regulator_get_exclusive(struct device *dev,
  58. const char *id)
  59. {
  60. return _devm_regulator_get(dev, id, EXCLUSIVE_GET);
  61. }
  62. EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive);
  63. static void regulator_action_disable(void *d)
  64. {
  65. struct regulator *r = (struct regulator *)d;
  66. regulator_disable(r);
  67. }
  68. static int _devm_regulator_get_enable(struct device *dev, const char *id,
  69. int get_type)
  70. {
  71. struct regulator *r;
  72. int ret;
  73. r = _devm_regulator_get(dev, id, get_type);
  74. if (IS_ERR(r))
  75. return PTR_ERR(r);
  76. ret = regulator_enable(r);
  77. if (!ret)
  78. ret = devm_add_action_or_reset(dev, &regulator_action_disable, r);
  79. if (ret)
  80. devm_regulator_put(r);
  81. return ret;
  82. }
  83. /**
  84. * devm_regulator_get_enable_optional - Resource managed regulator get and enable
  85. * @dev: device to supply
  86. * @id: supply name or regulator ID.
  87. *
  88. * Get and enable regulator for duration of the device life-time.
  89. * regulator_disable() and regulator_put() are automatically called on driver
  90. * detach. See regulator_get_optional() and regulator_enable() for more
  91. * information.
  92. */
  93. int devm_regulator_get_enable_optional(struct device *dev, const char *id)
  94. {
  95. return _devm_regulator_get_enable(dev, id, OPTIONAL_GET);
  96. }
  97. EXPORT_SYMBOL_GPL(devm_regulator_get_enable_optional);
  98. /**
  99. * devm_regulator_get_enable - Resource managed regulator get and enable
  100. * @dev: device to supply
  101. * @id: supply name or regulator ID.
  102. *
  103. * Get and enable regulator for duration of the device life-time.
  104. * regulator_disable() and regulator_put() are automatically called on driver
  105. * detach. See regulator_get() and regulator_enable() for more
  106. * information.
  107. */
  108. int devm_regulator_get_enable(struct device *dev, const char *id)
  109. {
  110. return _devm_regulator_get_enable(dev, id, NORMAL_GET);
  111. }
  112. EXPORT_SYMBOL_GPL(devm_regulator_get_enable);
  113. /**
  114. * devm_regulator_get_optional - Resource managed regulator_get_optional()
  115. * @dev: device to supply
  116. * @id: supply name or regulator ID.
  117. *
  118. * Managed regulator_get_optional(). Regulators returned from this
  119. * function are automatically regulator_put() on driver detach. See
  120. * regulator_get_optional() for more information.
  121. */
  122. struct regulator *devm_regulator_get_optional(struct device *dev,
  123. const char *id)
  124. {
  125. return _devm_regulator_get(dev, id, OPTIONAL_GET);
  126. }
  127. EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
  128. static int devm_regulator_match(struct device *dev, void *res, void *data)
  129. {
  130. struct regulator **r = res;
  131. if (!r || !*r) {
  132. WARN_ON(!r || !*r);
  133. return 0;
  134. }
  135. return *r == data;
  136. }
  137. /**
  138. * devm_regulator_put - Resource managed regulator_put()
  139. * @regulator: regulator to free
  140. *
  141. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  142. * this function will not need to be called and the resource management
  143. * code will ensure that the resource is freed.
  144. */
  145. void devm_regulator_put(struct regulator *regulator)
  146. {
  147. int rc;
  148. rc = devres_release(regulator->dev, devm_regulator_release,
  149. devm_regulator_match, regulator);
  150. if (rc != 0)
  151. WARN_ON(rc);
  152. }
  153. EXPORT_SYMBOL_GPL(devm_regulator_put);
  154. struct regulator_bulk_devres {
  155. struct regulator_bulk_data *consumers;
  156. int num_consumers;
  157. };
  158. static void devm_regulator_bulk_release(struct device *dev, void *res)
  159. {
  160. struct regulator_bulk_devres *devres = res;
  161. regulator_bulk_free(devres->num_consumers, devres->consumers);
  162. }
  163. /**
  164. * devm_regulator_bulk_get - managed get multiple regulator consumers
  165. *
  166. * @dev: device to supply
  167. * @num_consumers: number of consumers to register
  168. * @consumers: configuration of consumers; clients are stored here.
  169. *
  170. * @return 0 on success, an errno on failure.
  171. *
  172. * This helper function allows drivers to get several regulator
  173. * consumers in one operation with management, the regulators will
  174. * automatically be freed when the device is unbound. If any of the
  175. * regulators cannot be acquired then any regulators that were
  176. * allocated will be freed before returning to the caller.
  177. */
  178. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  179. struct regulator_bulk_data *consumers)
  180. {
  181. struct regulator_bulk_devres *devres;
  182. int ret;
  183. devres = devres_alloc(devm_regulator_bulk_release,
  184. sizeof(*devres), GFP_KERNEL);
  185. if (!devres)
  186. return -ENOMEM;
  187. ret = regulator_bulk_get(dev, num_consumers, consumers);
  188. if (!ret) {
  189. devres->consumers = consumers;
  190. devres->num_consumers = num_consumers;
  191. devres_add(dev, devres);
  192. } else {
  193. devres_free(devres);
  194. }
  195. return ret;
  196. }
  197. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  198. /**
  199. * devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data
  200. *
  201. * @dev: device to supply
  202. * @num_consumers: number of consumers to register
  203. * @in_consumers: const configuration of consumers
  204. * @out_consumers: in_consumers is copied here and this is passed to
  205. * devm_regulator_bulk_get().
  206. *
  207. * This is a convenience function to allow bulk regulator configuration
  208. * to be stored "static const" in files.
  209. *
  210. * Return: 0 on success, an errno on failure.
  211. */
  212. int devm_regulator_bulk_get_const(struct device *dev, int num_consumers,
  213. const struct regulator_bulk_data *in_consumers,
  214. struct regulator_bulk_data **out_consumers)
  215. {
  216. *out_consumers = devm_kmemdup(dev, in_consumers,
  217. num_consumers * sizeof(*in_consumers),
  218. GFP_KERNEL);
  219. if (*out_consumers == NULL)
  220. return -ENOMEM;
  221. return devm_regulator_bulk_get(dev, num_consumers, *out_consumers);
  222. }
  223. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const);
  224. static int devm_regulator_bulk_match(struct device *dev, void *res,
  225. void *data)
  226. {
  227. struct regulator_bulk_devres *match = res;
  228. struct regulator_bulk_data *target = data;
  229. /*
  230. * We check the put uses same consumer list as the get did.
  231. * We _could_ scan all entries in consumer array and check the
  232. * regulators match but ATM I don't see the need. We can change this
  233. * later if needed.
  234. */
  235. return match->consumers == target;
  236. }
  237. /**
  238. * devm_regulator_bulk_put - Resource managed regulator_bulk_put()
  239. * @consumers: consumers to free
  240. *
  241. * Deallocate regulators allocated with devm_regulator_bulk_get(). Normally
  242. * this function will not need to be called and the resource management
  243. * code will ensure that the resource is freed.
  244. */
  245. void devm_regulator_bulk_put(struct regulator_bulk_data *consumers)
  246. {
  247. int rc;
  248. struct regulator *regulator = consumers[0].consumer;
  249. rc = devres_release(regulator->dev, devm_regulator_bulk_release,
  250. devm_regulator_bulk_match, consumers);
  251. if (rc != 0)
  252. WARN_ON(rc);
  253. }
  254. EXPORT_SYMBOL_GPL(devm_regulator_bulk_put);
  255. static void devm_regulator_bulk_disable(void *res)
  256. {
  257. struct regulator_bulk_devres *devres = res;
  258. int i;
  259. for (i = 0; i < devres->num_consumers; i++)
  260. regulator_disable(devres->consumers[i].consumer);
  261. }
  262. /**
  263. * devm_regulator_bulk_get_enable - managed get'n enable multiple regulators
  264. *
  265. * @dev: device to supply
  266. * @num_consumers: number of consumers to register
  267. * @id: list of supply names or regulator IDs
  268. *
  269. * @return 0 on success, an errno on failure.
  270. *
  271. * This helper function allows drivers to get several regulator
  272. * consumers in one operation with management, the regulators will
  273. * automatically be freed when the device is unbound. If any of the
  274. * regulators cannot be acquired then any regulators that were
  275. * allocated will be freed before returning to the caller.
  276. */
  277. int devm_regulator_bulk_get_enable(struct device *dev, int num_consumers,
  278. const char * const *id)
  279. {
  280. struct regulator_bulk_devres *devres;
  281. struct regulator_bulk_data *consumers;
  282. int i, ret;
  283. devres = devm_kmalloc(dev, sizeof(*devres), GFP_KERNEL);
  284. if (!devres)
  285. return -ENOMEM;
  286. devres->consumers = devm_kcalloc(dev, num_consumers, sizeof(*consumers),
  287. GFP_KERNEL);
  288. consumers = devres->consumers;
  289. if (!consumers)
  290. return -ENOMEM;
  291. devres->num_consumers = num_consumers;
  292. for (i = 0; i < num_consumers; i++)
  293. consumers[i].supply = id[i];
  294. ret = devm_regulator_bulk_get(dev, num_consumers, consumers);
  295. if (ret)
  296. return ret;
  297. for (i = 0; i < num_consumers; i++) {
  298. ret = regulator_enable(consumers[i].consumer);
  299. if (ret)
  300. goto unwind;
  301. }
  302. ret = devm_add_action(dev, devm_regulator_bulk_disable, devres);
  303. if (!ret)
  304. return 0;
  305. unwind:
  306. while (--i >= 0)
  307. regulator_disable(consumers[i].consumer);
  308. devm_regulator_bulk_put(consumers);
  309. return ret;
  310. }
  311. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_enable);
  312. static void devm_rdev_release(struct device *dev, void *res)
  313. {
  314. regulator_unregister(*(struct regulator_dev **)res);
  315. }
  316. /**
  317. * devm_regulator_register - Resource managed regulator_register()
  318. * @dev: device to supply
  319. * @regulator_desc: regulator to register
  320. * @config: runtime configuration for regulator
  321. *
  322. * Called by regulator drivers to register a regulator. Returns a
  323. * valid pointer to struct regulator_dev on success or an ERR_PTR() on
  324. * error. The regulator will automatically be released when the device
  325. * is unbound.
  326. */
  327. struct regulator_dev *devm_regulator_register(struct device *dev,
  328. const struct regulator_desc *regulator_desc,
  329. const struct regulator_config *config)
  330. {
  331. struct regulator_dev **ptr, *rdev;
  332. ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
  333. GFP_KERNEL);
  334. if (!ptr)
  335. return ERR_PTR(-ENOMEM);
  336. rdev = regulator_register(dev, regulator_desc, config);
  337. if (!IS_ERR(rdev)) {
  338. *ptr = rdev;
  339. devres_add(dev, ptr);
  340. } else {
  341. devres_free(ptr);
  342. }
  343. return rdev;
  344. }
  345. EXPORT_SYMBOL_GPL(devm_regulator_register);
  346. struct regulator_supply_alias_match {
  347. struct device *dev;
  348. const char *id;
  349. };
  350. static int devm_regulator_match_supply_alias(struct device *dev, void *res,
  351. void *data)
  352. {
  353. struct regulator_supply_alias_match *match = res;
  354. struct regulator_supply_alias_match *target = data;
  355. return match->dev == target->dev && strcmp(match->id, target->id) == 0;
  356. }
  357. static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
  358. {
  359. struct regulator_supply_alias_match *match = res;
  360. regulator_unregister_supply_alias(match->dev, match->id);
  361. }
  362. /**
  363. * devm_regulator_register_supply_alias - Resource managed
  364. * regulator_register_supply_alias()
  365. *
  366. * @dev: device to supply
  367. * @id: supply name or regulator ID
  368. * @alias_dev: device that should be used to lookup the supply
  369. * @alias_id: supply name or regulator ID that should be used to lookup the
  370. * supply
  371. *
  372. * The supply alias will automatically be unregistered when the source
  373. * device is unbound.
  374. */
  375. int devm_regulator_register_supply_alias(struct device *dev, const char *id,
  376. struct device *alias_dev,
  377. const char *alias_id)
  378. {
  379. struct regulator_supply_alias_match *match;
  380. int ret;
  381. match = devres_alloc(devm_regulator_destroy_supply_alias,
  382. sizeof(struct regulator_supply_alias_match),
  383. GFP_KERNEL);
  384. if (!match)
  385. return -ENOMEM;
  386. match->dev = dev;
  387. match->id = id;
  388. ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
  389. if (ret < 0) {
  390. devres_free(match);
  391. return ret;
  392. }
  393. devres_add(dev, match);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
  397. static void devm_regulator_unregister_supply_alias(struct device *dev,
  398. const char *id)
  399. {
  400. struct regulator_supply_alias_match match;
  401. int rc;
  402. match.dev = dev;
  403. match.id = id;
  404. rc = devres_release(dev, devm_regulator_destroy_supply_alias,
  405. devm_regulator_match_supply_alias, &match);
  406. if (rc != 0)
  407. WARN_ON(rc);
  408. }
  409. /**
  410. * devm_regulator_bulk_register_supply_alias - Managed register
  411. * multiple aliases
  412. *
  413. * @dev: device to supply
  414. * @id: list of supply names or regulator IDs
  415. * @alias_dev: device that should be used to lookup the supply
  416. * @alias_id: list of supply names or regulator IDs that should be used to
  417. * lookup the supply
  418. * @num_id: number of aliases to register
  419. *
  420. * @return 0 on success, an errno on failure.
  421. *
  422. * This helper function allows drivers to register several supply
  423. * aliases in one operation, the aliases will be automatically
  424. * unregisters when the source device is unbound. If any of the
  425. * aliases cannot be registered any aliases that were registered
  426. * will be removed before returning to the caller.
  427. */
  428. int devm_regulator_bulk_register_supply_alias(struct device *dev,
  429. const char *const *id,
  430. struct device *alias_dev,
  431. const char *const *alias_id,
  432. int num_id)
  433. {
  434. int i;
  435. int ret;
  436. for (i = 0; i < num_id; ++i) {
  437. ret = devm_regulator_register_supply_alias(dev, id[i],
  438. alias_dev,
  439. alias_id[i]);
  440. if (ret < 0)
  441. goto err;
  442. }
  443. return 0;
  444. err:
  445. dev_err(dev,
  446. "Failed to create supply alias %s,%s -> %s,%s\n",
  447. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  448. while (--i >= 0)
  449. devm_regulator_unregister_supply_alias(dev, id[i]);
  450. return ret;
  451. }
  452. EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
  453. struct regulator_notifier_match {
  454. struct regulator *regulator;
  455. struct notifier_block *nb;
  456. };
  457. static int devm_regulator_match_notifier(struct device *dev, void *res,
  458. void *data)
  459. {
  460. struct regulator_notifier_match *match = res;
  461. struct regulator_notifier_match *target = data;
  462. return match->regulator == target->regulator && match->nb == target->nb;
  463. }
  464. static void devm_regulator_destroy_notifier(struct device *dev, void *res)
  465. {
  466. struct regulator_notifier_match *match = res;
  467. regulator_unregister_notifier(match->regulator, match->nb);
  468. }
  469. /**
  470. * devm_regulator_register_notifier - Resource managed
  471. * regulator_register_notifier
  472. *
  473. * @regulator: regulator source
  474. * @nb: notifier block
  475. *
  476. * The notifier will be registers under the consumer device and be
  477. * automatically be unregistered when the source device is unbound.
  478. */
  479. int devm_regulator_register_notifier(struct regulator *regulator,
  480. struct notifier_block *nb)
  481. {
  482. struct regulator_notifier_match *match;
  483. int ret;
  484. match = devres_alloc(devm_regulator_destroy_notifier,
  485. sizeof(struct regulator_notifier_match),
  486. GFP_KERNEL);
  487. if (!match)
  488. return -ENOMEM;
  489. match->regulator = regulator;
  490. match->nb = nb;
  491. ret = regulator_register_notifier(regulator, nb);
  492. if (ret < 0) {
  493. devres_free(match);
  494. return ret;
  495. }
  496. devres_add(regulator->dev, match);
  497. return 0;
  498. }
  499. EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
  500. /**
  501. * devm_regulator_unregister_notifier - Resource managed
  502. * regulator_unregister_notifier()
  503. *
  504. * @regulator: regulator source
  505. * @nb: notifier block
  506. *
  507. * Unregister a notifier registered with devm_regulator_register_notifier().
  508. * Normally this function will not need to be called and the resource
  509. * management code will ensure that the resource is freed.
  510. */
  511. void devm_regulator_unregister_notifier(struct regulator *regulator,
  512. struct notifier_block *nb)
  513. {
  514. struct regulator_notifier_match match;
  515. int rc;
  516. match.regulator = regulator;
  517. match.nb = nb;
  518. rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
  519. devm_regulator_match_notifier, &match);
  520. if (rc != 0)
  521. WARN_ON(rc);
  522. }
  523. EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);
  524. static void regulator_irq_helper_drop(void *res)
  525. {
  526. regulator_irq_helper_cancel(&res);
  527. }
  528. /**
  529. * devm_regulator_irq_helper - resource managed registration of IRQ based
  530. * regulator event/error notifier
  531. *
  532. * @dev: device to which lifetime the helper's lifetime is
  533. * bound.
  534. * @d: IRQ helper descriptor.
  535. * @irq: IRQ used to inform events/errors to be notified.
  536. * @irq_flags: Extra IRQ flags to be OR'ed with the default
  537. * IRQF_ONESHOT when requesting the (threaded) irq.
  538. * @common_errs: Errors which can be flagged by this IRQ for all rdevs.
  539. * When IRQ is re-enabled these errors will be cleared
  540. * from all associated regulators
  541. * @per_rdev_errs: Optional error flag array describing errors specific
  542. * for only some of the regulators. These errors will be
  543. * or'ed with common errors. If this is given the array
  544. * should contain rdev_amount flags. Can be set to NULL
  545. * if there is no regulator specific error flags for this
  546. * IRQ.
  547. * @rdev: Array of pointers to regulators associated with this
  548. * IRQ.
  549. * @rdev_amount: Amount of regulators associated with this IRQ.
  550. *
  551. * Return: handle to irq_helper or an ERR_PTR() encoded error code.
  552. */
  553. void *devm_regulator_irq_helper(struct device *dev,
  554. const struct regulator_irq_desc *d, int irq,
  555. int irq_flags, int common_errs,
  556. int *per_rdev_errs,
  557. struct regulator_dev **rdev, int rdev_amount)
  558. {
  559. void *ptr;
  560. int ret;
  561. ptr = regulator_irq_helper(dev, d, irq, irq_flags, common_errs,
  562. per_rdev_errs, rdev, rdev_amount);
  563. if (IS_ERR(ptr))
  564. return ptr;
  565. ret = devm_add_action_or_reset(dev, regulator_irq_helper_drop, ptr);
  566. if (ret)
  567. return ERR_PTR(ret);
  568. return ptr;
  569. }
  570. EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);