devres.c 13 KB

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