devres.c 13 KB

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