devres.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. static void regulator_action_disable(void *d)
  64. {
  65. struct regulator *r = (struct regulator *)d;
  66. regulator_disable(r);
  67. }
  68. static int _devm_regulator_get_enable(struct device *dev, const char *id,
  69. int get_type)
  70. {
  71. struct regulator *r;
  72. int ret;
  73. r = _devm_regulator_get(dev, id, get_type);
  74. if (IS_ERR(r))
  75. return PTR_ERR(r);
  76. ret = regulator_enable(r);
  77. if (!ret)
  78. ret = devm_add_action_or_reset(dev, &regulator_action_disable, r);
  79. if (ret)
  80. devm_regulator_put(r);
  81. return ret;
  82. }
  83. /**
  84. * devm_regulator_get_enable_optional - Resource managed regulator get and enable
  85. * @dev: device to supply
  86. * @id: supply name or regulator ID.
  87. *
  88. * Get and enable regulator for duration of the device life-time.
  89. * regulator_disable() and regulator_put() are automatically called on driver
  90. * detach. See regulator_get_optional() and regulator_enable() for more
  91. * information.
  92. */
  93. int devm_regulator_get_enable_optional(struct device *dev, const char *id)
  94. {
  95. return _devm_regulator_get_enable(dev, id, OPTIONAL_GET);
  96. }
  97. EXPORT_SYMBOL_GPL(devm_regulator_get_enable_optional);
  98. /**
  99. * devm_regulator_get_enable - Resource managed regulator get and enable
  100. * @dev: device to supply
  101. * @id: supply name or regulator ID.
  102. *
  103. * Get and enable regulator for duration of the device life-time.
  104. * regulator_disable() and regulator_put() are automatically called on driver
  105. * detach. See regulator_get() and regulator_enable() for more
  106. * information.
  107. */
  108. int devm_regulator_get_enable(struct device *dev, const char *id)
  109. {
  110. return _devm_regulator_get_enable(dev, id, NORMAL_GET);
  111. }
  112. EXPORT_SYMBOL_GPL(devm_regulator_get_enable);
  113. /**
  114. * devm_regulator_get_optional - Resource managed regulator_get_optional()
  115. * @dev: device to supply
  116. * @id: supply name or regulator ID.
  117. *
  118. * Managed regulator_get_optional(). Regulators returned from this
  119. * function are automatically regulator_put() on driver detach. See
  120. * regulator_get_optional() for more information.
  121. */
  122. struct regulator *devm_regulator_get_optional(struct device *dev,
  123. const char *id)
  124. {
  125. return _devm_regulator_get(dev, id, OPTIONAL_GET);
  126. }
  127. EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
  128. /**
  129. * devm_regulator_get_enable_read_voltage - Resource managed regulator get and
  130. * enable that returns the voltage
  131. * @dev: device to supply
  132. * @id: supply name or regulator ID.
  133. *
  134. * Get and enable regulator for duration of the device life-time.
  135. * regulator_disable() and regulator_put() are automatically called on driver
  136. * detach. See regulator_get_optional(), regulator_enable(), and
  137. * regulator_get_voltage() for more information.
  138. *
  139. * This is a convenience function for supplies that provide a reference voltage
  140. * where the consumer driver just needs to know the voltage and keep the
  141. * regulator enabled.
  142. *
  143. * In cases where the supply is not strictly required, callers can check for
  144. * -ENODEV error and handle it accordingly.
  145. *
  146. * Returns: voltage in microvolts on success, or an negative error number on failure.
  147. */
  148. int devm_regulator_get_enable_read_voltage(struct device *dev, const char *id)
  149. {
  150. struct regulator *r;
  151. int ret;
  152. /*
  153. * Since we need a real voltage, we use devm_regulator_get_optional()
  154. * rather than getting a dummy regulator with devm_regulator_get() and
  155. * then letting regulator_get_voltage() fail with -EINVAL. This way, the
  156. * caller can handle the -ENODEV negative error number if needed instead
  157. * of the ambiguous -EINVAL.
  158. */
  159. r = devm_regulator_get_optional(dev, id);
  160. if (IS_ERR(r))
  161. return PTR_ERR(r);
  162. ret = regulator_enable(r);
  163. if (ret)
  164. goto err_regulator_put;
  165. ret = devm_add_action_or_reset(dev, regulator_action_disable, r);
  166. if (ret)
  167. goto err_regulator_put;
  168. ret = regulator_get_voltage(r);
  169. if (ret < 0)
  170. goto err_release_action;
  171. return ret;
  172. err_release_action:
  173. devm_release_action(dev, regulator_action_disable, r);
  174. err_regulator_put:
  175. devm_regulator_put(r);
  176. return ret;
  177. }
  178. EXPORT_SYMBOL_GPL(devm_regulator_get_enable_read_voltage);
  179. static int devm_regulator_match(struct device *dev, void *res, void *data)
  180. {
  181. struct regulator **r = res;
  182. if (!r || !*r) {
  183. WARN_ON(!r || !*r);
  184. return 0;
  185. }
  186. return *r == data;
  187. }
  188. /**
  189. * devm_regulator_put - Resource managed regulator_put()
  190. * @regulator: regulator to free
  191. *
  192. * Deallocate a regulator allocated with devm_regulator_get(). Normally
  193. * this function will not need to be called and the resource management
  194. * code will ensure that the resource is freed.
  195. */
  196. void devm_regulator_put(struct regulator *regulator)
  197. {
  198. int rc;
  199. rc = devres_release(regulator->dev, devm_regulator_release,
  200. devm_regulator_match, regulator);
  201. if (rc != 0)
  202. WARN_ON(rc);
  203. }
  204. EXPORT_SYMBOL_GPL(devm_regulator_put);
  205. struct regulator_bulk_devres {
  206. struct regulator_bulk_data *consumers;
  207. int num_consumers;
  208. };
  209. static void devm_regulator_bulk_release(struct device *dev, void *res)
  210. {
  211. struct regulator_bulk_devres *devres = res;
  212. regulator_bulk_free(devres->num_consumers, devres->consumers);
  213. }
  214. static int _devm_regulator_bulk_get(struct device *dev, int num_consumers,
  215. struct regulator_bulk_data *consumers,
  216. enum regulator_get_type get_type)
  217. {
  218. struct regulator_bulk_devres *devres;
  219. int ret;
  220. devres = devres_alloc(devm_regulator_bulk_release,
  221. sizeof(*devres), GFP_KERNEL);
  222. if (!devres)
  223. return -ENOMEM;
  224. ret = _regulator_bulk_get(dev, num_consumers, consumers, get_type);
  225. if (!ret) {
  226. devres->consumers = consumers;
  227. devres->num_consumers = num_consumers;
  228. devres_add(dev, devres);
  229. } else {
  230. devres_free(devres);
  231. }
  232. return ret;
  233. }
  234. /**
  235. * devm_regulator_bulk_get - managed get multiple regulator consumers
  236. *
  237. * @dev: device to supply
  238. * @num_consumers: number of consumers to register
  239. * @consumers: configuration of consumers; clients are stored here.
  240. *
  241. * @return 0 on success, a negative error number on failure.
  242. *
  243. * This helper function allows drivers to get several regulator
  244. * consumers in one operation with management, the regulators will
  245. * automatically be freed when the device is unbound. If any of the
  246. * regulators cannot be acquired then any regulators that were
  247. * allocated will be freed before returning to the caller.
  248. */
  249. int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  250. struct regulator_bulk_data *consumers)
  251. {
  252. return _devm_regulator_bulk_get(dev, num_consumers, consumers, NORMAL_GET);
  253. }
  254. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
  255. /**
  256. * devm_regulator_bulk_get_exclusive - managed exclusive get of multiple
  257. * regulator consumers
  258. *
  259. * @dev: device to supply
  260. * @num_consumers: number of consumers to register
  261. * @consumers: configuration of consumers; clients are stored here.
  262. *
  263. * @return 0 on success, a negative error number on failure.
  264. *
  265. * This helper function allows drivers to exclusively get several
  266. * regulator consumers in one operation with management, the regulators
  267. * will automatically be freed when the device is unbound. If any of
  268. * the regulators cannot be acquired then any regulators that were
  269. * allocated will be freed before returning to the caller.
  270. */
  271. int devm_regulator_bulk_get_exclusive(struct device *dev, int num_consumers,
  272. struct regulator_bulk_data *consumers)
  273. {
  274. return _devm_regulator_bulk_get(dev, num_consumers, consumers, EXCLUSIVE_GET);
  275. }
  276. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_exclusive);
  277. /**
  278. * devm_regulator_bulk_get_const - devm_regulator_bulk_get() w/ const data
  279. *
  280. * @dev: device to supply
  281. * @num_consumers: number of consumers to register
  282. * @in_consumers: const configuration of consumers
  283. * @out_consumers: in_consumers is copied here and this is passed to
  284. * devm_regulator_bulk_get().
  285. *
  286. * This is a convenience function to allow bulk regulator configuration
  287. * to be stored "static const" in files.
  288. *
  289. * Return: 0 on success, a negative error number on failure.
  290. */
  291. int devm_regulator_bulk_get_const(struct device *dev, int num_consumers,
  292. const struct regulator_bulk_data *in_consumers,
  293. struct regulator_bulk_data **out_consumers)
  294. {
  295. *out_consumers = devm_kmemdup(dev, in_consumers,
  296. num_consumers * sizeof(*in_consumers),
  297. GFP_KERNEL);
  298. if (*out_consumers == NULL)
  299. return -ENOMEM;
  300. return devm_regulator_bulk_get(dev, num_consumers, *out_consumers);
  301. }
  302. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_const);
  303. static int devm_regulator_bulk_match(struct device *dev, void *res,
  304. void *data)
  305. {
  306. struct regulator_bulk_devres *match = res;
  307. struct regulator_bulk_data *target = data;
  308. /*
  309. * We check the put uses same consumer list as the get did.
  310. * We _could_ scan all entries in consumer array and check the
  311. * regulators match but ATM I don't see the need. We can change this
  312. * later if needed.
  313. */
  314. return match->consumers == target;
  315. }
  316. /**
  317. * devm_regulator_bulk_put - Resource managed regulator_bulk_put()
  318. * @consumers: consumers to free
  319. *
  320. * Deallocate regulators allocated with devm_regulator_bulk_get(). Normally
  321. * this function will not need to be called and the resource management
  322. * code will ensure that the resource is freed.
  323. */
  324. void devm_regulator_bulk_put(struct regulator_bulk_data *consumers)
  325. {
  326. int rc;
  327. struct regulator *regulator = consumers[0].consumer;
  328. rc = devres_release(regulator->dev, devm_regulator_bulk_release,
  329. devm_regulator_bulk_match, consumers);
  330. if (rc != 0)
  331. WARN_ON(rc);
  332. }
  333. EXPORT_SYMBOL_GPL(devm_regulator_bulk_put);
  334. static void devm_regulator_bulk_disable(void *res)
  335. {
  336. struct regulator_bulk_devres *devres = res;
  337. int i;
  338. for (i = 0; i < devres->num_consumers; i++)
  339. regulator_disable(devres->consumers[i].consumer);
  340. }
  341. /**
  342. * devm_regulator_bulk_get_enable - managed get'n enable multiple regulators
  343. *
  344. * @dev: device to supply
  345. * @num_consumers: number of consumers to register
  346. * @id: list of supply names or regulator IDs
  347. *
  348. * @return 0 on success, a negative error number on failure.
  349. *
  350. * This helper function allows drivers to get several regulator
  351. * consumers in one operation with management, the regulators will
  352. * automatically be freed when the device is unbound. If any of the
  353. * regulators cannot be acquired then any regulators that were
  354. * allocated will be freed before returning to the caller.
  355. */
  356. int devm_regulator_bulk_get_enable(struct device *dev, int num_consumers,
  357. const char * const *id)
  358. {
  359. struct regulator_bulk_devres *devres;
  360. struct regulator_bulk_data *consumers;
  361. int i, ret;
  362. devres = devm_kmalloc(dev, sizeof(*devres), GFP_KERNEL);
  363. if (!devres)
  364. return -ENOMEM;
  365. devres->consumers = devm_kcalloc(dev, num_consumers, sizeof(*consumers),
  366. GFP_KERNEL);
  367. consumers = devres->consumers;
  368. if (!consumers)
  369. return -ENOMEM;
  370. devres->num_consumers = num_consumers;
  371. for (i = 0; i < num_consumers; i++)
  372. consumers[i].supply = id[i];
  373. ret = devm_regulator_bulk_get(dev, num_consumers, consumers);
  374. if (ret)
  375. return ret;
  376. for (i = 0; i < num_consumers; i++) {
  377. ret = regulator_enable(consumers[i].consumer);
  378. if (ret)
  379. goto unwind;
  380. }
  381. ret = devm_add_action(dev, devm_regulator_bulk_disable, devres);
  382. if (!ret)
  383. return 0;
  384. unwind:
  385. while (--i >= 0)
  386. regulator_disable(consumers[i].consumer);
  387. devm_regulator_bulk_put(consumers);
  388. return ret;
  389. }
  390. EXPORT_SYMBOL_GPL(devm_regulator_bulk_get_enable);
  391. static void devm_rdev_release(struct device *dev, void *res)
  392. {
  393. regulator_unregister(*(struct regulator_dev **)res);
  394. }
  395. /**
  396. * devm_regulator_register - Resource managed regulator_register()
  397. * @dev: device to supply
  398. * @regulator_desc: regulator to register
  399. * @config: runtime configuration for regulator
  400. *
  401. * Called by regulator drivers to register a regulator. Returns a
  402. * valid pointer to struct regulator_dev on success or an ERR_PTR() on
  403. * error. The regulator will automatically be released when the device
  404. * is unbound.
  405. */
  406. struct regulator_dev *devm_regulator_register(struct device *dev,
  407. const struct regulator_desc *regulator_desc,
  408. const struct regulator_config *config)
  409. {
  410. struct regulator_dev **ptr, *rdev;
  411. ptr = devres_alloc(devm_rdev_release, sizeof(*ptr),
  412. GFP_KERNEL);
  413. if (!ptr)
  414. return ERR_PTR(-ENOMEM);
  415. rdev = regulator_register(dev, regulator_desc, config);
  416. if (!IS_ERR(rdev)) {
  417. *ptr = rdev;
  418. devres_add(dev, ptr);
  419. } else {
  420. devres_free(ptr);
  421. }
  422. return rdev;
  423. }
  424. EXPORT_SYMBOL_GPL(devm_regulator_register);
  425. struct regulator_supply_alias_match {
  426. struct device *dev;
  427. const char *id;
  428. };
  429. static int devm_regulator_match_supply_alias(struct device *dev, void *res,
  430. void *data)
  431. {
  432. struct regulator_supply_alias_match *match = res;
  433. struct regulator_supply_alias_match *target = data;
  434. return match->dev == target->dev && strcmp(match->id, target->id) == 0;
  435. }
  436. static void devm_regulator_destroy_supply_alias(struct device *dev, void *res)
  437. {
  438. struct regulator_supply_alias_match *match = res;
  439. regulator_unregister_supply_alias(match->dev, match->id);
  440. }
  441. /**
  442. * devm_regulator_register_supply_alias - Resource managed
  443. * regulator_register_supply_alias()
  444. *
  445. * @dev: device to supply
  446. * @id: supply name or regulator ID
  447. * @alias_dev: device that should be used to lookup the supply
  448. * @alias_id: supply name or regulator ID that should be used to lookup the
  449. * supply
  450. *
  451. * The supply alias will automatically be unregistered when the source
  452. * device is unbound.
  453. */
  454. int devm_regulator_register_supply_alias(struct device *dev, const char *id,
  455. struct device *alias_dev,
  456. const char *alias_id)
  457. {
  458. struct regulator_supply_alias_match *match;
  459. int ret;
  460. match = devres_alloc(devm_regulator_destroy_supply_alias,
  461. sizeof(struct regulator_supply_alias_match),
  462. GFP_KERNEL);
  463. if (!match)
  464. return -ENOMEM;
  465. match->dev = dev;
  466. match->id = id;
  467. ret = regulator_register_supply_alias(dev, id, alias_dev, alias_id);
  468. if (ret < 0) {
  469. devres_free(match);
  470. return ret;
  471. }
  472. devres_add(dev, match);
  473. return 0;
  474. }
  475. EXPORT_SYMBOL_GPL(devm_regulator_register_supply_alias);
  476. static void devm_regulator_unregister_supply_alias(struct device *dev,
  477. const char *id)
  478. {
  479. struct regulator_supply_alias_match match;
  480. int rc;
  481. match.dev = dev;
  482. match.id = id;
  483. rc = devres_release(dev, devm_regulator_destroy_supply_alias,
  484. devm_regulator_match_supply_alias, &match);
  485. if (rc != 0)
  486. WARN_ON(rc);
  487. }
  488. /**
  489. * devm_regulator_bulk_register_supply_alias - Managed register
  490. * multiple aliases
  491. *
  492. * @dev: device to supply
  493. * @id: list of supply names or regulator IDs
  494. * @alias_dev: device that should be used to lookup the supply
  495. * @alias_id: list of supply names or regulator IDs that should be used to
  496. * lookup the supply
  497. * @num_id: number of aliases to register
  498. *
  499. * @return 0 on success, a negative error number on failure.
  500. *
  501. * This helper function allows drivers to register several supply
  502. * aliases in one operation, the aliases will be automatically
  503. * unregisters when the source device is unbound. If any of the
  504. * aliases cannot be registered any aliases that were registered
  505. * will be removed before returning to the caller.
  506. */
  507. int devm_regulator_bulk_register_supply_alias(struct device *dev,
  508. const char *const *id,
  509. struct device *alias_dev,
  510. const char *const *alias_id,
  511. int num_id)
  512. {
  513. int i;
  514. int ret;
  515. for (i = 0; i < num_id; ++i) {
  516. ret = devm_regulator_register_supply_alias(dev, id[i],
  517. alias_dev,
  518. alias_id[i]);
  519. if (ret < 0)
  520. goto err;
  521. }
  522. return 0;
  523. err:
  524. dev_err(dev,
  525. "Failed to create supply alias %s,%s -> %s,%s\n",
  526. id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
  527. while (--i >= 0)
  528. devm_regulator_unregister_supply_alias(dev, id[i]);
  529. return ret;
  530. }
  531. EXPORT_SYMBOL_GPL(devm_regulator_bulk_register_supply_alias);
  532. struct regulator_notifier_match {
  533. struct regulator *regulator;
  534. struct notifier_block *nb;
  535. };
  536. static int devm_regulator_match_notifier(struct device *dev, void *res,
  537. void *data)
  538. {
  539. struct regulator_notifier_match *match = res;
  540. struct regulator_notifier_match *target = data;
  541. return match->regulator == target->regulator && match->nb == target->nb;
  542. }
  543. static void devm_regulator_destroy_notifier(struct device *dev, void *res)
  544. {
  545. struct regulator_notifier_match *match = res;
  546. regulator_unregister_notifier(match->regulator, match->nb);
  547. }
  548. /**
  549. * devm_regulator_register_notifier - Resource managed
  550. * regulator_register_notifier
  551. *
  552. * @regulator: regulator source
  553. * @nb: notifier block
  554. *
  555. * The notifier will be registers under the consumer device and be
  556. * automatically be unregistered when the source device is unbound.
  557. */
  558. int devm_regulator_register_notifier(struct regulator *regulator,
  559. struct notifier_block *nb)
  560. {
  561. struct regulator_notifier_match *match;
  562. int ret;
  563. match = devres_alloc(devm_regulator_destroy_notifier,
  564. sizeof(struct regulator_notifier_match),
  565. GFP_KERNEL);
  566. if (!match)
  567. return -ENOMEM;
  568. match->regulator = regulator;
  569. match->nb = nb;
  570. ret = regulator_register_notifier(regulator, nb);
  571. if (ret < 0) {
  572. devres_free(match);
  573. return ret;
  574. }
  575. devres_add(regulator->dev, match);
  576. return 0;
  577. }
  578. EXPORT_SYMBOL_GPL(devm_regulator_register_notifier);
  579. /**
  580. * devm_regulator_unregister_notifier - Resource managed
  581. * regulator_unregister_notifier()
  582. *
  583. * @regulator: regulator source
  584. * @nb: notifier block
  585. *
  586. * Unregister a notifier registered with devm_regulator_register_notifier().
  587. * Normally this function will not need to be called and the resource
  588. * management code will ensure that the resource is freed.
  589. */
  590. void devm_regulator_unregister_notifier(struct regulator *regulator,
  591. struct notifier_block *nb)
  592. {
  593. struct regulator_notifier_match match;
  594. int rc;
  595. match.regulator = regulator;
  596. match.nb = nb;
  597. rc = devres_release(regulator->dev, devm_regulator_destroy_notifier,
  598. devm_regulator_match_notifier, &match);
  599. if (rc != 0)
  600. WARN_ON(rc);
  601. }
  602. EXPORT_SYMBOL_GPL(devm_regulator_unregister_notifier);
  603. static void regulator_irq_helper_drop(void *res)
  604. {
  605. regulator_irq_helper_cancel(&res);
  606. }
  607. /**
  608. * devm_regulator_irq_helper - resource managed registration of IRQ based
  609. * regulator event/error notifier
  610. *
  611. * @dev: device to which lifetime the helper's lifetime is
  612. * bound.
  613. * @d: IRQ helper descriptor.
  614. * @irq: IRQ used to inform events/errors to be notified.
  615. * @irq_flags: Extra IRQ flags to be OR'ed with the default
  616. * IRQF_ONESHOT when requesting the (threaded) irq.
  617. * @common_errs: Errors which can be flagged by this IRQ for all rdevs.
  618. * When IRQ is re-enabled these errors will be cleared
  619. * from all associated regulators
  620. * @per_rdev_errs: Optional error flag array describing errors specific
  621. * for only some of the regulators. These errors will be
  622. * or'ed with common errors. If this is given the array
  623. * should contain rdev_amount flags. Can be set to NULL
  624. * if there is no regulator specific error flags for this
  625. * IRQ.
  626. * @rdev: Array of pointers to regulators associated with this
  627. * IRQ.
  628. * @rdev_amount: Amount of regulators associated with this IRQ.
  629. *
  630. * Return: handle to irq_helper or an ERR_PTR() encoded negative error number.
  631. */
  632. void *devm_regulator_irq_helper(struct device *dev,
  633. const struct regulator_irq_desc *d, int irq,
  634. int irq_flags, int common_errs,
  635. int *per_rdev_errs,
  636. struct regulator_dev **rdev, int rdev_amount)
  637. {
  638. void *ptr;
  639. int ret;
  640. ptr = regulator_irq_helper(dev, d, irq, irq_flags, common_errs,
  641. per_rdev_errs, rdev, rdev_amount);
  642. if (IS_ERR(ptr))
  643. return ptr;
  644. ret = devm_add_action_or_reset(dev, regulator_irq_helper_drop, ptr);
  645. if (ret)
  646. return ERR_PTR(ret);
  647. return ptr;
  648. }
  649. EXPORT_SYMBOL_GPL(devm_regulator_irq_helper);