devres.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. /**
  64. * devm_regulator_get_optional - Resource managed regulator_get_optional()
  65. * @dev: device to supply
  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. * @dev: device to supply
  155. * @regulator_desc: regulator to register
  156. * @config: runtime configuration for regulator
  157. *
  158. * Called by regulator drivers to register a regulator. Returns a
  159. * valid pointer to struct regulator_dev on success or an ERR_PTR() on
  160. * error. The regulator will automatically be released when the device
  161. * is unbound.
  162. */
  163. struct regulator_dev *devm_regulator_register(struct device *dev,
  164. const struct regulator_desc *regulator_desc,
  165. const struct regulator_config *config)
  166. {
  167. struct regulator_dev **ptr, *rdev;
  168. ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
  169. GFP_KERNEL);
  170. if (!ptr)
  171. return ERR_PTR(-ENOMEM);
  172. rdev = regulator_register(regulator_desc, config);
  173. if (!IS_ERR(rdev)) {
  174. *ptr = rdev;
  175. devres_add(dev, ptr);
  176. } else {
  177. devres_free(ptr);
  178. }
  179. return rdev;
  180. }
  181. EXPORT_SYMBOL_GPL(devm_regulator_register);
  182. static int devm_rdev_match(struct device *dev, void *res, void *data)
  183. {
  184. struct regulator_dev **r = res;
  185. if (!r || !*r) {
  186. WARN_ON(!r || !*r);
  187. return 0;
  188. }
  189. return *r == data;
  190. }
  191. /**
  192. * devm_regulator_unregister - Resource managed regulator_unregister()
  193. * @dev: device to supply
  194. * @rdev: regulator to free
  195. *
  196. * Unregister a regulator registered with devm_regulator_register().
  197. * Normally this function will not need to be called and the resource
  198. * management code will ensure that the resource is freed.
  199. */
  200. void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev)
  201. {
  202. int rc;
  203. rc = devres_release(dev, devm_rdev_release, devm_rdev_match, rdev);
  204. if (rc != 0)
  205. WARN_ON(rc);
  206. }
  207. EXPORT_SYMBOL_GPL(devm_regulator_unregister);
  208. struct regulator_supply_alias_match {
  209. struct device *dev;
  210. const char *id;
  211. };
  212. static int devm_regulator_match_supply_alias(struct device *dev, void *res,
  213. void *data)
  214. {
  215. struct regulator_supply_alias_match *match = res;
  216. struct regulator_supply_alias_match *target = data;
  217. return match->dev == target->dev && strcmp(match->id, target->id) == 0;
  218. }
  219. static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
  220. {
  221. struct regulator_supply_alias_match *match = res;
  222. regulator_unregister_supply_alias(match->dev, match->id);
  223. }
  224. /**
  225. * devm_regulator_register_supply_alias - Resource managed
  226. * regulator_register_supply_alias()
  227. *
  228. * @dev: device to supply
  229. * @id: supply name or regulator ID
  230. * @alias_dev: device that should be used to lookup the supply
  231. * @alias_id: supply name or regulator ID that should be used to lookup the
  232. * supply
  233. *
  234. * The supply alias will automatically be unregistered when the source
  235. * device is unbound.
  236. */
  237. int devm_regulator_register_supply_alias(struct device *dev, const char *id,
  238. struct device *alias_dev,
  239. const char *alias_id)
  240. {
  241. struct regulator_supply_alias_match *match;
  242. int ret;
  243. match = devres_alloc(devm_regulator_destroy_supply_alias,
  244. sizeof(struct regulator_supply_alias_match),
  245. GFP_KERNEL);
  246. if (!match)
  247. return -ENOMEM;
  248. match->dev = dev;
  249. match->id = id;
  250. ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
  251. if (ret < 0) {
  252. devres_free(match);
  253. return ret;
  254. }
  255. devres_add(dev, match);
  256. return 0;
  257. }
  258. EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
  259. /**
  260. * devm_regulator_unregister_supply_alias - Resource managed
  261. * regulator_unregister_supply_alias()
  262. *
  263. * @dev: device to supply
  264. * @id: supply name or regulator ID
  265. *
  266. * Unregister an alias registered with
  267. * devm_regulator_register_supply_alias(). Normally this function
  268. * will not need to be called and the resource management code
  269. * will ensure that the resource is freed.
  270. */
  271. void devm_regulator_unregister_supply_alias(struct device *dev, const char *id)
  272. {
  273. struct regulator_supply_alias_match match;
  274. int rc;
  275. match.dev = dev;
  276. match.id = id;
  277. rc = devres_release(dev, devm_regulator_destroy_supply_alias,
  278. devm_regulator_match_supply_alias, &match);
  279. if (rc != 0)
  280. WARN_ON(rc);
  281. }
  282. EXPORT_SYMBOL_GPL(devm_regulator_unregister_supply_alias);
  283. /**
  284. * devm_regulator_bulk_register_supply_alias - Managed register
  285. * multiple aliases
  286. *
  287. * @dev: device to supply
  288. * @id: list of supply names or regulator IDs
  289. * @alias_dev: device that should be used to lookup the supply
  290. * @alias_id: list of supply names or regulator IDs that should be used to
  291. * lookup the supply
  292. * @num_id: number of aliases to register
  293. *
  294. * @return 0 on success, an errno on failure.
  295. *
  296. * This helper function allows drivers to register several supply
  297. * aliases in one operation, the aliases will be automatically
  298. * unregisters when the source device is unbound. If any of the
  299. * aliases cannot be registered any aliases that were registered
  300. * will be removed before returning to the caller.
  301. */
  302. int devm_regulator_bulk_register_supply_alias(struct device *dev,
  303. const char *const *id,
  304. struct device *alias_dev,
  305. const char *const *alias_id,
  306. int num_id)
  307. {
  308. int i;
  309. int ret;
  310. for (i = 0; i < num_id; ++i) {
  311. ret = devm_regulator_register_supply_alias(dev, id[i],
  312. alias_dev,
  313. alias_id[i]);
  314. if (ret < 0)
  315. goto err;
  316. }
  317. return 0;
  318. err:
  319. dev_err(dev,
  320. "Failed to create supply alias %s,%s -> %s,%s\n",
  321. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  322. while (--i >= 0)
  323. devm_regulator_unregister_supply_alias(dev, id[i]);
  324. return ret;
  325. }
  326. EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
  327. /**
  328. * devm_regulator_bulk_unregister_supply_alias - Managed unregister
  329. * multiple aliases
  330. *
  331. * @dev: device to supply
  332. * @id: list of supply names or regulator IDs
  333. * @num_id: number of aliases to unregister
  334. *
  335. * Unregister aliases registered with
  336. * devm_regulator_bulk_register_supply_alias(). Normally this function
  337. * will not need to be called and the resource management code
  338. * will ensure that the resource is freed.
  339. */
  340. void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
  341. const char *const *id,
  342. int num_id)
  343. {
  344. int i;
  345. for (i = 0; i < num_id; ++i)
  346. devm_regulator_unregister_supply_alias(dev, id[i]);
  347. }
  348. EXPORT_SYMBOL_GPL(devm_regulator_bulk_unregister_supply_alias);
  349. struct regulator_notifier_match {
  350. struct regulator *regulator;
  351. struct notifier_block *nb;
  352. };
  353. static int devm_regulator_match_notifier(struct device *dev, void *res,
  354. void *data)
  355. {
  356. struct regulator_notifier_match *match = res;
  357. struct regulator_notifier_match *target = data;
  358. return match->regulator == target->regulator && match->nb == target->nb;
  359. }
  360. static void devm_regulator_destroy_notifier(struct device *dev, void *res)
  361. {
  362. struct regulator_notifier_match *match = res;
  363. regulator_unregister_notifier(match->regulator, match->nb);
  364. }
  365. /**
  366. * devm_regulator_register_notifier - Resource managed
  367. * regulator_register_notifier
  368. *
  369. * @regulator: regulator source
  370. * @nb: notifier block
  371. *
  372. * The notifier will be registers under the consumer device and be
  373. * automatically be unregistered when the source device is unbound.
  374. */
  375. int devm_regulator_register_notifier(struct regulator *regulator,
  376. struct notifier_block *nb)
  377. {
  378. struct regulator_notifier_match *match;
  379. int ret;
  380. match = devres_alloc(devm_regulator_destroy_notifier,
  381. sizeof(struct regulator_notifier_match),
  382. GFP_KERNEL);
  383. if (!match)
  384. return -ENOMEM;
  385. match->regulator = regulator;
  386. match->nb = nb;
  387. ret = regulator_register_notifier(regulator, nb);
  388. if (ret < 0) {
  389. devres_free(match);
  390. return ret;
  391. }
  392. devres_add(regulator->dev, match);
  393. return 0;
  394. }
  395. EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
  396. /**
  397. * devm_regulator_unregister_notifier - Resource managed
  398. * regulator_unregister_notifier()
  399. *
  400. * @regulator: regulator source
  401. * @nb: notifier block
  402. *
  403. * Unregister a notifier registered with devm_regulator_register_notifier().
  404. * Normally this function will not need to be called and the resource
  405. * management code will ensure that the resource is freed.
  406. */
  407. void devm_regulator_unregister_notifier(struct regulator *regulator,
  408. struct notifier_block *nb)
  409. {
  410. struct regulator_notifier_match match;
  411. int rc;
  412. match.regulator = regulator;
  413. match.nb = nb;
  414. rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
  415. devm_regulator_match_notifier, &match);
  416. if (rc != 0)
  417. WARN_ON(rc);
  418. }
  419. EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);