devres.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 for regulator "consumer"
  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 for regulator "consumer"
  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. /**
  64. * devm_regulator_get_optional - Resource managed regulator_get_optional()
  65. * @dev: device for regulator "consumer"
  66. * @id: Supply name or regulator ID.
  67. *
  68. * Managed regulator_get_optional(). Regulators returned from this
  69. * function are automatically regulator_put() on driver detach. See
  70. * regulator_get_optional() for more information.
  71. */
  72. struct regulator *devm_regulator_get_optional(struct device *dev,
  73. const char *id)
  74. {
  75. return _devm_regulator_get(dev, id, OPTIONAL_GET);
  76. }
  77. EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
  78. static int devm_regulator_match(struct device *dev, void *res, void *data)
  79. {
  80. struct regulator **r = res;
  81. if (!r || !*r) {
  82. WARN_ON(!r || !*r);
  83. return 0;
  84. }
  85. return *r == data;
  86. }
  87. /**
  88. * devm_regulator_put - Resource managed regulator_put()
  89. * @regulator: regulator to free
  90. *
  91. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  92. * this function will not need to be called and the resource management
  93. * code will ensure that the resource is freed.
  94. */
  95. void devm_regulator_put(struct regulator *regulator)
  96. {
  97. int rc;
  98. rc = devres_release(regulator->dev, devm_regulator_release,
  99. devm_regulator_match, regulator);
  100. if (rc != 0)
  101. WARN_ON(rc);
  102. }
  103. EXPORT_SYMBOL_GPL(devm_regulator_put);
  104. struct regulator_bulk_devres {
  105. struct regulator_bulk_data *consumers;
  106. int num_consumers;
  107. };
  108. static void devm_regulator_bulk_release(struct device *dev, void *res)
  109. {
  110. struct regulator_bulk_devres *devres = res;
  111. regulator_bulk_free(devres->num_consumers, devres->consumers);
  112. }
  113. /**
  114. * devm_regulator_bulk_get - managed get multiple regulator consumers
  115. *
  116. * @dev: Device to supply
  117. * @num_consumers: Number of consumers to register
  118. * @consumers: Configuration of consumers; clients are stored here.
  119. *
  120. * @return 0 on success, an errno on failure.
  121. *
  122. * This helper function allows drivers to get several regulator
  123. * consumers in one operation with management, the regulators will
  124. * automatically be freed when the device is unbound. If any of the
  125. * regulators cannot be acquired then any regulators that were
  126. * allocated will be freed before returning to the caller.
  127. */
  128. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  129. struct regulator_bulk_data *consumers)
  130. {
  131. struct regulator_bulk_devres *devres;
  132. int ret;
  133. devres = devres_alloc(devm_regulator_bulk_release,
  134. sizeof(*devres), GFP_KERNEL);
  135. if (!devres)
  136. return -ENOMEM;
  137. ret = regulator_bulk_get(dev, num_consumers, consumers);
  138. if (!ret) {
  139. devres->consumers = consumers;
  140. devres->num_consumers = num_consumers;
  141. devres_add(dev, devres);
  142. } else {
  143. devres_free(devres);
  144. }
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  148. static void devm_rdev_release(struct device *dev, void *res)
  149. {
  150. regulator_unregister(*(struct regulator_dev **)res);
  151. }
  152. /**
  153. * devm_regulator_register - Resource managed regulator_register()
  154. * @regulator_desc: regulator to register
  155. * @config: runtime configuration for regulator
  156. *
  157. * Called by regulator drivers to register a regulator. Returns a
  158. * valid pointer to struct regulator_dev on success or an ERR_PTR() on
  159. * error. The regulator will automatically be released when the device
  160. * is unbound.
  161. */
  162. struct regulator_dev *devm_regulator_register(struct device *dev,
  163. const struct regulator_desc *regulator_desc,
  164. const struct regulator_config *config)
  165. {
  166. struct regulator_dev **ptr, *rdev;
  167. ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
  168. GFP_KERNEL);
  169. if (!ptr)
  170. return ERR_PTR(-ENOMEM);
  171. rdev = regulator_register(regulator_desc, config);
  172. if (!IS_ERR(rdev)) {
  173. *ptr = rdev;
  174. devres_add(dev, ptr);
  175. } else {
  176. devres_free(ptr);
  177. }
  178. return rdev;
  179. }
  180. EXPORT_SYMBOL_GPL(devm_regulator_register);
  181. static int devm_rdev_match(struct device *dev, void *res, void *data)
  182. {
  183. struct regulator_dev **r = res;
  184. if (!r || !*r) {
  185. WARN_ON(!r || !*r);
  186. return 0;
  187. }
  188. return *r == data;
  189. }
  190. /**
  191. * devm_regulator_unregister - Resource managed regulator_unregister()
  192. * @regulator: regulator to free
  193. *
  194. * Unregister a regulator registered with devm_regulator_register().
  195. * Normally this function will not need to be called and the resource
  196. * management code will ensure that the resource is freed.
  197. */
  198. void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
  199. {
  200. int rc;
  201. rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
  202. if (rc != 0)
  203. WARN_ON(rc);
  204. }
  205. EXPORT_SYMBOL_GPL(devm_regulator_unregister);
  206. struct regulator_supply_alias_match {
  207. struct device *dev;
  208. const char *id;
  209. };
  210. static int devm_regulator_match_supply_alias(struct device *dev, void *res,
  211. void *data)
  212. {
  213. struct regulator_supply_alias_match *match = res;
  214. struct regulator_supply_alias_match *target = data;
  215. return match->dev == target->dev && strcmp(match->id, target->id) == 0;
  216. }
  217. static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
  218. {
  219. struct regulator_supply_alias_match *match = res;
  220. regulator_unregister_supply_alias(match->dev, match->id);
  221. }
  222. /**
  223. * devm_regulator_register_supply_alias - Resource managed
  224. * regulator_register_supply_alias()
  225. *
  226. * @dev: device that will be given as the regulator "consumer"
  227. * @id: Supply name or regulator ID
  228. * @alias_dev: device that should be used to lookup the supply
  229. * @alias_id: Supply name or regulator ID that should be used to lookup the
  230. * supply
  231. *
  232. * The supply alias will automatically be unregistered when the source
  233. * device is unbound.
  234. */
  235. int devm_regulator_register_supply_alias(struct device *dev, const char *id,
  236. struct device *alias_dev,
  237. const char *alias_id)
  238. {
  239. struct regulator_supply_alias_match *match;
  240. int ret;
  241. match = devres_alloc(devm_regulator_destroy_supply_alias,
  242. sizeof(struct regulator_supply_alias_match),
  243. GFP_KERNEL);
  244. if (!match)
  245. return -ENOMEM;
  246. match->dev = dev;
  247. match->id = id;
  248. ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
  249. if (ret < 0) {
  250. devres_free(match);
  251. return ret;
  252. }
  253. devres_add(dev, match);
  254. return 0;
  255. }
  256. EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
  257. /**
  258. * devm_regulator_unregister_supply_alias - Resource managed
  259. * regulator_unregister_supply_alias()
  260. *
  261. * @dev: device that will be given as the regulator "consumer"
  262. * @id: Supply name or regulator ID
  263. *
  264. * Unregister an alias registered with
  265. * devm_regulator_register_supply_alias(). Normally this function
  266. * will not need to be called and the resource management code
  267. * will ensure that the resource is freed.
  268. */
  269. void devm_regulator_unregister_supply_alias(struct device *dev, const char *id)
  270. {
  271. struct regulator_supply_alias_match match;
  272. int rc;
  273. match.dev = dev;
  274. match.id = id;
  275. rc = devres_release(dev, devm_regulator_destroy_supply_alias,
  276. devm_regulator_match_supply_alias, &match);
  277. if (rc != 0)
  278. WARN_ON(rc);
  279. }
  280. EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
  281. /**
  282. * devm_regulator_bulk_register_supply_alias - Managed register
  283. * multiple aliases
  284. *
  285. * @dev: device that will be given as the regulator "consumer"
  286. * @id: List of supply names or regulator IDs
  287. * @alias_dev: device that should be used to lookup the supply
  288. * @alias_id: List of supply names or regulator IDs that should be used to
  289. * lookup the supply
  290. * @num_id: Number of aliases to register
  291. *
  292. * @return 0 on success, an errno on failure.
  293. *
  294. * This helper function allows drivers to register several supply
  295. * aliases in one operation, the aliases will be automatically
  296. * unregisters when the source device is unbound. If any of the
  297. * aliases cannot be registered any aliases that were registered
  298. * will be removed before returning to the caller.
  299. */
  300. int devm_regulator_bulk_register_supply_alias(struct device *dev,
  301. const char *const *id,
  302. struct device *alias_dev,
  303. const char *const *alias_id,
  304. int num_id)
  305. {
  306. int i;
  307. int ret;
  308. for (i = 0; i < num_id; ++i) {
  309. ret = devm_regulator_register_supply_alias(dev, id[i],
  310. alias_dev,
  311. alias_id[i]);
  312. if (ret < 0)
  313. goto err;
  314. }
  315. return 0;
  316. err:
  317. dev_err(dev,
  318. "Failed to create supply alias %s,%s -> %s,%s\n",
  319. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  320. while (--i >= 0)
  321. devm_regulator_unregister_supply_alias(dev, id[i]);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
  325. /**
  326. * devm_regulator_bulk_unregister_supply_alias - Managed unregister
  327. * multiple aliases
  328. *
  329. * @dev: device that will be given as the regulator "consumer"
  330. * @id: List of supply names or regulator IDs
  331. * @num_id: Number of aliases to unregister
  332. *
  333. * Unregister aliases registered with
  334. * devm_regulator_bulk_register_supply_alias(). Normally this function
  335. * will not need to be called and the resource management code
  336. * will ensure that the resource is freed.
  337. */
  338. void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
  339. const char *const *id,
  340. int num_id)
  341. {
  342. int i;
  343. for (i = 0; i < num_id; ++i)
  344. devm_regulator_unregister_supply_alias(dev, id[i]);
  345. }
  346. EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias);
  347. struct regulator_notifier_match {
  348. struct regulator *regulator;
  349. struct notifier_block *nb;
  350. };
  351. static int devm_regulator_match_notifier(struct device *dev, void *res,
  352. void *data)
  353. {
  354. struct regulator_notifier_match *match = res;
  355. struct regulator_notifier_match *target = data;
  356. return match->regulator == target->regulator && match->nb == target->nb;
  357. }
  358. static void devm_regulator_destroy_notifier(struct device *dev, void *res)
  359. {
  360. struct regulator_notifier_match *match = res;
  361. regulator_unregister_notifier(match->regulator, match->nb);
  362. }
  363. /**
  364. * devm_regulator_register_notifier - Resource managed
  365. * regulator_register_notifier
  366. *
  367. * @regulator: regulator source
  368. * @nb: notifier block
  369. *
  370. * The notifier will be registers under the consumer device and be
  371. * automatically be unregistered when the source device is unbound.
  372. */
  373. int devm_regulator_register_notifier(struct regulator *regulator,
  374. struct notifier_block *nb)
  375. {
  376. struct regulator_notifier_match *match;
  377. int ret;
  378. match = devres_alloc(devm_regulator_destroy_notifier,
  379. sizeof(struct regulator_notifier_match),
  380. GFP_KERNEL);
  381. if (!match)
  382. return -ENOMEM;
  383. match->regulator = regulator;
  384. match->nb = nb;
  385. ret = regulator_register_notifier(regulator, nb);
  386. if (ret < 0) {
  387. devres_free(match);
  388. return ret;
  389. }
  390. devres_add(regulator->dev, match);
  391. return 0;
  392. }
  393. EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
  394. /**
  395. * devm_regulator_unregister_notifier - Resource managed
  396. * regulator_unregister_notifier()
  397. *
  398. * @regulator: regulator source
  399. * @nb: notifier block
  400. *
  401. * Unregister a notifier registered with devm_regulator_register_notifier().
  402. * Normally this function will not need to be called and the resource
  403. * management code will ensure that the resource is freed.
  404. */
  405. void devm_regulator_unregister_notifier(struct regulator *regulator,
  406. struct notifier_block *nb)
  407. {
  408. struct regulator_notifier_match match;
  409. int rc;
  410. match.regulator = regulator;
  411. match.nb = nb;
  412. rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
  413. devm_regulator_match_notifier, &match);
  414. if (rc != 0)
  415. WARN_ON(rc);
  416. }
  417. EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);