core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * Reset Controller framework
  3. *
  4. * Copyright 2013 Philipp Zabel, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kref.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/reset.h>
  20. #include <linux/reset-controller.h>
  21. #include <linux/slab.h>
  22. static DEFINE_MUTEX(reset_list_mutex);
  23. static LIST_HEAD(reset_controller_list);
  24. static DEFINE_MUTEX(reset_lookup_mutex);
  25. static LIST_HEAD(reset_lookup_list);
  26. /**
  27. * struct reset_control - a reset control
  28. * @rcdev: a pointer to the reset controller device
  29. * this reset control belongs to
  30. * @list: list entry for the rcdev's reset controller list
  31. * @id: ID of the reset controller in the reset
  32. * controller device
  33. * @refcnt: Number of gets of this reset_control
  34. * @shared: Is this a shared (1), or an exclusive (0) reset_control?
  35. * @deassert_cnt: Number of times this reset line has been deasserted
  36. * @triggered_count: Number of times this reset line has been reset. Currently
  37. * only used for shared resets, which means that the value
  38. * will be either 0 or 1.
  39. */
  40. struct reset_control {
  41. struct reset_controller_dev *rcdev;
  42. struct list_head list;
  43. unsigned int id;
  44. struct kref refcnt;
  45. bool shared;
  46. bool array;
  47. atomic_t deassert_count;
  48. atomic_t triggered_count;
  49. };
  50. /**
  51. * struct reset_control_array - an array of reset controls
  52. * @base: reset control for compatibility with reset control API functions
  53. * @num_rstcs: number of reset controls
  54. * @rstc: array of reset controls
  55. */
  56. struct reset_control_array {
  57. struct reset_control base;
  58. unsigned int num_rstcs;
  59. struct reset_control *rstc[];
  60. };
  61. /**
  62. * of_reset_simple_xlate - translate reset_spec to the reset line number
  63. * @rcdev: a pointer to the reset controller device
  64. * @reset_spec: reset line specifier as found in the device tree
  65. * @flags: a flags pointer to fill in (optional)
  66. *
  67. * This simple translation function should be used for reset controllers
  68. * with 1:1 mapping, where reset lines can be indexed by number without gaps.
  69. */
  70. static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  71. const struct of_phandle_args *reset_spec)
  72. {
  73. if (reset_spec->args[0] >= rcdev->nr_resets)
  74. return -EINVAL;
  75. return reset_spec->args[0];
  76. }
  77. /**
  78. * reset_controller_register - register a reset controller device
  79. * @rcdev: a pointer to the initialized reset controller device
  80. */
  81. int reset_controller_register(struct reset_controller_dev *rcdev)
  82. {
  83. if (!rcdev->of_xlate) {
  84. rcdev->of_reset_n_cells = 1;
  85. rcdev->of_xlate = of_reset_simple_xlate;
  86. }
  87. INIT_LIST_HEAD(&rcdev->reset_control_head);
  88. mutex_lock(&reset_list_mutex);
  89. list_add(&rcdev->list, &reset_controller_list);
  90. mutex_unlock(&reset_list_mutex);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL_GPL(reset_controller_register);
  94. /**
  95. * reset_controller_unregister - unregister a reset controller device
  96. * @rcdev: a pointer to the reset controller device
  97. */
  98. void reset_controller_unregister(struct reset_controller_dev *rcdev)
  99. {
  100. mutex_lock(&reset_list_mutex);
  101. list_del(&rcdev->list);
  102. mutex_unlock(&reset_list_mutex);
  103. }
  104. EXPORT_SYMBOL_GPL(reset_controller_unregister);
  105. static void devm_reset_controller_release(struct device *dev, void *res)
  106. {
  107. reset_controller_unregister(*(struct reset_controller_dev **)res);
  108. }
  109. /**
  110. * devm_reset_controller_register - resource managed reset_controller_register()
  111. * @dev: device that is registering this reset controller
  112. * @rcdev: a pointer to the initialized reset controller device
  113. *
  114. * Managed reset_controller_register(). For reset controllers registered by
  115. * this function, reset_controller_unregister() is automatically called on
  116. * driver detach. See reset_controller_register() for more information.
  117. */
  118. int devm_reset_controller_register(struct device *dev,
  119. struct reset_controller_dev *rcdev)
  120. {
  121. struct reset_controller_dev **rcdevp;
  122. int ret;
  123. rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
  124. GFP_KERNEL);
  125. if (!rcdevp)
  126. return -ENOMEM;
  127. ret = reset_controller_register(rcdev);
  128. if (!ret) {
  129. *rcdevp = rcdev;
  130. devres_add(dev, rcdevp);
  131. } else {
  132. devres_free(rcdevp);
  133. }
  134. return ret;
  135. }
  136. EXPORT_SYMBOL_GPL(devm_reset_controller_register);
  137. /**
  138. * reset_controller_add_lookup - register a set of lookup entries
  139. * @lookup: array of reset lookup entries
  140. * @num_entries: number of entries in the lookup array
  141. */
  142. void reset_controller_add_lookup(struct reset_control_lookup *lookup,
  143. unsigned int num_entries)
  144. {
  145. struct reset_control_lookup *entry;
  146. unsigned int i;
  147. mutex_lock(&reset_lookup_mutex);
  148. for (i = 0; i < num_entries; i++) {
  149. entry = &lookup[i];
  150. if (!entry->dev_id || !entry->provider) {
  151. pr_warn("%s(): reset lookup entry badly specified, skipping\n",
  152. __func__);
  153. continue;
  154. }
  155. list_add_tail(&entry->list, &reset_lookup_list);
  156. }
  157. mutex_unlock(&reset_lookup_mutex);
  158. }
  159. EXPORT_SYMBOL_GPL(reset_controller_add_lookup);
  160. static inline struct reset_control_array *
  161. rstc_to_array(struct reset_control *rstc) {
  162. return container_of(rstc, struct reset_control_array, base);
  163. }
  164. static int reset_control_array_reset(struct reset_control_array *resets)
  165. {
  166. int ret, i;
  167. for (i = 0; i < resets->num_rstcs; i++) {
  168. ret = reset_control_reset(resets->rstc[i]);
  169. if (ret)
  170. return ret;
  171. }
  172. return 0;
  173. }
  174. static int reset_control_array_assert(struct reset_control_array *resets)
  175. {
  176. int ret, i;
  177. for (i = 0; i < resets->num_rstcs; i++) {
  178. ret = reset_control_assert(resets->rstc[i]);
  179. if (ret)
  180. goto err;
  181. }
  182. return 0;
  183. err:
  184. while (i--)
  185. reset_control_deassert(resets->rstc[i]);
  186. return ret;
  187. }
  188. static int reset_control_array_deassert(struct reset_control_array *resets)
  189. {
  190. int ret, i;
  191. for (i = 0; i < resets->num_rstcs; i++) {
  192. ret = reset_control_deassert(resets->rstc[i]);
  193. if (ret)
  194. goto err;
  195. }
  196. return 0;
  197. err:
  198. while (i--)
  199. reset_control_assert(resets->rstc[i]);
  200. return ret;
  201. }
  202. static inline bool reset_control_is_array(struct reset_control *rstc)
  203. {
  204. return rstc->array;
  205. }
  206. /**
  207. * reset_control_reset - reset the controlled device
  208. * @rstc: reset controller
  209. *
  210. * On a shared reset line the actual reset pulse is only triggered once for the
  211. * lifetime of the reset_control instance: for all but the first caller this is
  212. * a no-op.
  213. * Consumers must not use reset_control_(de)assert on shared reset lines when
  214. * reset_control_reset has been used.
  215. *
  216. * If rstc is NULL it is an optional reset and the function will just
  217. * return 0.
  218. */
  219. int reset_control_reset(struct reset_control *rstc)
  220. {
  221. int ret;
  222. if (!rstc)
  223. return 0;
  224. if (WARN_ON(IS_ERR(rstc)))
  225. return -EINVAL;
  226. if (reset_control_is_array(rstc))
  227. return reset_control_array_reset(rstc_to_array(rstc));
  228. if (!rstc->rcdev->ops->reset)
  229. return -ENOTSUPP;
  230. if (rstc->shared) {
  231. if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
  232. return -EINVAL;
  233. if (atomic_inc_return(&rstc->triggered_count) != 1)
  234. return 0;
  235. }
  236. ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  237. if (rstc->shared && ret)
  238. atomic_dec(&rstc->triggered_count);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL_GPL(reset_control_reset);
  242. /**
  243. * reset_control_assert - asserts the reset line
  244. * @rstc: reset controller
  245. *
  246. * Calling this on an exclusive reset controller guarantees that the reset
  247. * will be asserted. When called on a shared reset controller the line may
  248. * still be deasserted, as long as other users keep it so.
  249. *
  250. * For shared reset controls a driver cannot expect the hw's registers and
  251. * internal state to be reset, but must be prepared for this to happen.
  252. * Consumers must not use reset_control_reset on shared reset lines when
  253. * reset_control_(de)assert has been used.
  254. * return 0.
  255. *
  256. * If rstc is NULL it is an optional reset and the function will just
  257. * return 0.
  258. */
  259. int reset_control_assert(struct reset_control *rstc)
  260. {
  261. if (!rstc)
  262. return 0;
  263. if (WARN_ON(IS_ERR(rstc)))
  264. return -EINVAL;
  265. if (reset_control_is_array(rstc))
  266. return reset_control_array_assert(rstc_to_array(rstc));
  267. if (rstc->shared) {
  268. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  269. return -EINVAL;
  270. if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
  271. return -EINVAL;
  272. if (atomic_dec_return(&rstc->deassert_count) != 0)
  273. return 0;
  274. /*
  275. * Shared reset controls allow the reset line to be in any state
  276. * after this call, so doing nothing is a valid option.
  277. */
  278. if (!rstc->rcdev->ops->assert)
  279. return 0;
  280. } else {
  281. /*
  282. * If the reset controller does not implement .assert(), there
  283. * is no way to guarantee that the reset line is asserted after
  284. * this call.
  285. */
  286. if (!rstc->rcdev->ops->assert)
  287. return -ENOTSUPP;
  288. }
  289. return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
  290. }
  291. EXPORT_SYMBOL_GPL(reset_control_assert);
  292. /**
  293. * reset_control_deassert - deasserts the reset line
  294. * @rstc: reset controller
  295. *
  296. * After calling this function, the reset is guaranteed to be deasserted.
  297. * Consumers must not use reset_control_reset on shared reset lines when
  298. * reset_control_(de)assert has been used.
  299. * return 0.
  300. *
  301. * If rstc is NULL it is an optional reset and the function will just
  302. * return 0.
  303. */
  304. int reset_control_deassert(struct reset_control *rstc)
  305. {
  306. if (!rstc)
  307. return 0;
  308. if (WARN_ON(IS_ERR(rstc)))
  309. return -EINVAL;
  310. if (reset_control_is_array(rstc))
  311. return reset_control_array_deassert(rstc_to_array(rstc));
  312. if (rstc->shared) {
  313. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  314. return -EINVAL;
  315. if (atomic_inc_return(&rstc->deassert_count) != 1)
  316. return 0;
  317. }
  318. /*
  319. * If the reset controller does not implement .deassert(), we assume
  320. * that it handles self-deasserting reset lines via .reset(). In that
  321. * case, the reset lines are deasserted by default. If that is not the
  322. * case, the reset controller driver should implement .deassert() and
  323. * return -ENOTSUPP.
  324. */
  325. if (!rstc->rcdev->ops->deassert)
  326. return 0;
  327. return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
  328. }
  329. EXPORT_SYMBOL_GPL(reset_control_deassert);
  330. /**
  331. * reset_control_status - returns a negative errno if not supported, a
  332. * positive value if the reset line is asserted, or zero if the reset
  333. * line is not asserted or if the desc is NULL (optional reset).
  334. * @rstc: reset controller
  335. */
  336. int reset_control_status(struct reset_control *rstc)
  337. {
  338. if (!rstc)
  339. return 0;
  340. if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
  341. return -EINVAL;
  342. if (rstc->rcdev->ops->status)
  343. return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
  344. return -ENOTSUPP;
  345. }
  346. EXPORT_SYMBOL_GPL(reset_control_status);
  347. static struct reset_control *__reset_control_get_internal(
  348. struct reset_controller_dev *rcdev,
  349. unsigned int index, bool shared)
  350. {
  351. struct reset_control *rstc;
  352. lockdep_assert_held(&reset_list_mutex);
  353. list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
  354. if (rstc->id == index) {
  355. if (WARN_ON(!rstc->shared || !shared))
  356. return ERR_PTR(-EBUSY);
  357. kref_get(&rstc->refcnt);
  358. return rstc;
  359. }
  360. }
  361. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  362. if (!rstc)
  363. return ERR_PTR(-ENOMEM);
  364. try_module_get(rcdev->owner);
  365. rstc->rcdev = rcdev;
  366. list_add(&rstc->list, &rcdev->reset_control_head);
  367. rstc->id = index;
  368. kref_init(&rstc->refcnt);
  369. rstc->shared = shared;
  370. return rstc;
  371. }
  372. static void __reset_control_release(struct kref *kref)
  373. {
  374. struct reset_control *rstc = container_of(kref, struct reset_control,
  375. refcnt);
  376. lockdep_assert_held(&reset_list_mutex);
  377. module_put(rstc->rcdev->owner);
  378. list_del(&rstc->list);
  379. kfree(rstc);
  380. }
  381. static void __reset_control_put_internal(struct reset_control *rstc)
  382. {
  383. lockdep_assert_held(&reset_list_mutex);
  384. kref_put(&rstc->refcnt, __reset_control_release);
  385. }
  386. struct reset_control *__of_reset_control_get(struct device_node *node,
  387. const char *id, int index, bool shared,
  388. bool optional)
  389. {
  390. struct reset_control *rstc;
  391. struct reset_controller_dev *r, *rcdev;
  392. struct of_phandle_args args;
  393. int rstc_id;
  394. int ret;
  395. if (!node)
  396. return ERR_PTR(-EINVAL);
  397. if (id) {
  398. index = of_property_match_string(node,
  399. "reset-names", id);
  400. if (index == -EILSEQ)
  401. return ERR_PTR(index);
  402. if (index < 0)
  403. return optional ? NULL : ERR_PTR(-ENOENT);
  404. }
  405. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  406. index, &args);
  407. if (ret == -EINVAL)
  408. return ERR_PTR(ret);
  409. if (ret)
  410. return optional ? NULL : ERR_PTR(ret);
  411. mutex_lock(&reset_list_mutex);
  412. rcdev = NULL;
  413. list_for_each_entry(r, &reset_controller_list, list) {
  414. if (args.np == r->of_node) {
  415. rcdev = r;
  416. break;
  417. }
  418. }
  419. if (!rcdev) {
  420. rstc = ERR_PTR(-EPROBE_DEFER);
  421. goto out;
  422. }
  423. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  424. rstc = ERR_PTR(-EINVAL);
  425. goto out;
  426. }
  427. rstc_id = rcdev->of_xlate(rcdev, &args);
  428. if (rstc_id < 0) {
  429. rstc = ERR_PTR(rstc_id);
  430. goto out;
  431. }
  432. /* reset_list_mutex also protects the rcdev's reset_control list */
  433. rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
  434. out:
  435. mutex_unlock(&reset_list_mutex);
  436. of_node_put(args.np);
  437. return rstc;
  438. }
  439. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  440. static struct reset_controller_dev *
  441. __reset_controller_by_name(const char *name)
  442. {
  443. struct reset_controller_dev *rcdev;
  444. lockdep_assert_held(&reset_list_mutex);
  445. list_for_each_entry(rcdev, &reset_controller_list, list) {
  446. if (!rcdev->dev)
  447. continue;
  448. if (!strcmp(name, dev_name(rcdev->dev)))
  449. return rcdev;
  450. }
  451. return NULL;
  452. }
  453. static struct reset_control *
  454. __reset_control_get_from_lookup(struct device *dev, const char *con_id,
  455. bool shared, bool optional)
  456. {
  457. const struct reset_control_lookup *lookup;
  458. struct reset_controller_dev *rcdev;
  459. const char *dev_id = dev_name(dev);
  460. struct reset_control *rstc = NULL;
  461. if (!dev)
  462. return ERR_PTR(-EINVAL);
  463. mutex_lock(&reset_lookup_mutex);
  464. list_for_each_entry(lookup, &reset_lookup_list, list) {
  465. if (strcmp(lookup->dev_id, dev_id))
  466. continue;
  467. if ((!con_id && !lookup->con_id) ||
  468. ((con_id && lookup->con_id) &&
  469. !strcmp(con_id, lookup->con_id))) {
  470. mutex_lock(&reset_list_mutex);
  471. rcdev = __reset_controller_by_name(lookup->provider);
  472. if (!rcdev) {
  473. mutex_unlock(&reset_list_mutex);
  474. mutex_unlock(&reset_lookup_mutex);
  475. /* Reset provider may not be ready yet. */
  476. return ERR_PTR(-EPROBE_DEFER);
  477. }
  478. rstc = __reset_control_get_internal(rcdev,
  479. lookup->index,
  480. shared);
  481. mutex_unlock(&reset_list_mutex);
  482. break;
  483. }
  484. }
  485. mutex_unlock(&reset_lookup_mutex);
  486. if (!rstc)
  487. return optional ? NULL : ERR_PTR(-ENOENT);
  488. return rstc;
  489. }
  490. struct reset_control *__reset_control_get(struct device *dev, const char *id,
  491. int index, bool shared, bool optional)
  492. {
  493. if (dev->of_node)
  494. return __of_reset_control_get(dev->of_node, id, index, shared,
  495. optional);
  496. return __reset_control_get_from_lookup(dev, id, shared, optional);
  497. }
  498. EXPORT_SYMBOL_GPL(__reset_control_get);
  499. static void reset_control_array_put(struct reset_control_array *resets)
  500. {
  501. int i;
  502. mutex_lock(&reset_list_mutex);
  503. for (i = 0; i < resets->num_rstcs; i++)
  504. __reset_control_put_internal(resets->rstc[i]);
  505. mutex_unlock(&reset_list_mutex);
  506. kfree(resets);
  507. }
  508. /**
  509. * reset_control_put - free the reset controller
  510. * @rstc: reset controller
  511. */
  512. void reset_control_put(struct reset_control *rstc)
  513. {
  514. if (IS_ERR_OR_NULL(rstc))
  515. return;
  516. if (reset_control_is_array(rstc)) {
  517. reset_control_array_put(rstc_to_array(rstc));
  518. return;
  519. }
  520. mutex_lock(&reset_list_mutex);
  521. __reset_control_put_internal(rstc);
  522. mutex_unlock(&reset_list_mutex);
  523. }
  524. EXPORT_SYMBOL_GPL(reset_control_put);
  525. static void devm_reset_control_release(struct device *dev, void *res)
  526. {
  527. reset_control_put(*(struct reset_control **)res);
  528. }
  529. struct reset_control *__devm_reset_control_get(struct device *dev,
  530. const char *id, int index, bool shared,
  531. bool optional)
  532. {
  533. struct reset_control **ptr, *rstc;
  534. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  535. GFP_KERNEL);
  536. if (!ptr)
  537. return ERR_PTR(-ENOMEM);
  538. rstc = __reset_control_get(dev, id, index, shared, optional);
  539. if (!IS_ERR(rstc)) {
  540. *ptr = rstc;
  541. devres_add(dev, ptr);
  542. } else {
  543. devres_free(ptr);
  544. }
  545. return rstc;
  546. }
  547. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  548. /**
  549. * device_reset - find reset controller associated with the device
  550. * and perform reset
  551. * @dev: device to be reset by the controller
  552. * @optional: whether it is optional to reset the device
  553. *
  554. * Convenience wrapper for __reset_control_get() and reset_control_reset().
  555. * This is useful for the common case of devices with single, dedicated reset
  556. * lines.
  557. */
  558. int __device_reset(struct device *dev, bool optional)
  559. {
  560. struct reset_control *rstc;
  561. int ret;
  562. rstc = __reset_control_get(dev, NULL, 0, 0, optional);
  563. if (IS_ERR(rstc))
  564. return PTR_ERR(rstc);
  565. ret = reset_control_reset(rstc);
  566. reset_control_put(rstc);
  567. return ret;
  568. }
  569. EXPORT_SYMBOL_GPL(__device_reset);
  570. /**
  571. * APIs to manage an array of reset controls.
  572. */
  573. /**
  574. * of_reset_control_get_count - Count number of resets available with a device
  575. *
  576. * @node: device node that contains 'resets'.
  577. *
  578. * Returns positive reset count on success, or error number on failure and
  579. * on count being zero.
  580. */
  581. static int of_reset_control_get_count(struct device_node *node)
  582. {
  583. int count;
  584. if (!node)
  585. return -EINVAL;
  586. count = of_count_phandle_with_args(node, "resets", "#reset-cells");
  587. if (count == 0)
  588. count = -ENOENT;
  589. return count;
  590. }
  591. /**
  592. * of_reset_control_array_get - Get a list of reset controls using
  593. * device node.
  594. *
  595. * @np: device node for the device that requests the reset controls array
  596. * @shared: whether reset controls are shared or not
  597. * @optional: whether it is optional to get the reset controls
  598. *
  599. * Returns pointer to allocated reset_control_array on success or
  600. * error on failure
  601. */
  602. struct reset_control *
  603. of_reset_control_array_get(struct device_node *np, bool shared, bool optional)
  604. {
  605. struct reset_control_array *resets;
  606. struct reset_control *rstc;
  607. int num, i;
  608. num = of_reset_control_get_count(np);
  609. if (num < 0)
  610. return optional ? NULL : ERR_PTR(num);
  611. resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
  612. if (!resets)
  613. return ERR_PTR(-ENOMEM);
  614. for (i = 0; i < num; i++) {
  615. rstc = __of_reset_control_get(np, NULL, i, shared, optional);
  616. if (IS_ERR(rstc))
  617. goto err_rst;
  618. resets->rstc[i] = rstc;
  619. }
  620. resets->num_rstcs = num;
  621. resets->base.array = true;
  622. return &resets->base;
  623. err_rst:
  624. mutex_lock(&reset_list_mutex);
  625. while (--i >= 0)
  626. __reset_control_put_internal(resets->rstc[i]);
  627. mutex_unlock(&reset_list_mutex);
  628. kfree(resets);
  629. return rstc;
  630. }
  631. EXPORT_SYMBOL_GPL(of_reset_control_array_get);
  632. /**
  633. * devm_reset_control_array_get - Resource managed reset control array get
  634. *
  635. * @dev: device that requests the list of reset controls
  636. * @shared: whether reset controls are shared or not
  637. * @optional: whether it is optional to get the reset controls
  638. *
  639. * The reset control array APIs are intended for a list of resets
  640. * that just have to be asserted or deasserted, without any
  641. * requirements on the order.
  642. *
  643. * Returns pointer to allocated reset_control_array on success or
  644. * error on failure
  645. */
  646. struct reset_control *
  647. devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
  648. {
  649. struct reset_control **devres;
  650. struct reset_control *rstc;
  651. devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
  652. GFP_KERNEL);
  653. if (!devres)
  654. return ERR_PTR(-ENOMEM);
  655. rstc = of_reset_control_array_get(dev->of_node, shared, optional);
  656. if (IS_ERR(rstc)) {
  657. devres_free(devres);
  658. return rstc;
  659. }
  660. *devres = rstc;
  661. devres_add(dev, devres);
  662. return rstc;
  663. }
  664. EXPORT_SYMBOL_GPL(devm_reset_control_array_get);