devres.c 13 KB

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