core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. of_node_put(args.np);
  420. if (!rcdev) {
  421. mutex_unlock(&reset_list_mutex);
  422. return ERR_PTR(-EPROBE_DEFER);
  423. }
  424. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  425. mutex_unlock(&reset_list_mutex);
  426. return ERR_PTR(-EINVAL);
  427. }
  428. rstc_id = rcdev->of_xlate(rcdev, &args);
  429. if (rstc_id < 0) {
  430. mutex_unlock(&reset_list_mutex);
  431. return ERR_PTR(rstc_id);
  432. }
  433. /* reset_list_mutex also protects the rcdev's reset_control list */
  434. rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
  435. mutex_unlock(&reset_list_mutex);
  436. return rstc;
  437. }
  438. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  439. static struct reset_controller_dev *
  440. __reset_controller_by_name(const char *name)
  441. {
  442. struct reset_controller_dev *rcdev;
  443. lockdep_assert_held(&reset_list_mutex);
  444. list_for_each_entry(rcdev, &reset_controller_list, list) {
  445. if (!rcdev->dev)
  446. continue;
  447. if (!strcmp(name, dev_name(rcdev->dev)))
  448. return rcdev;
  449. }
  450. return NULL;
  451. }
  452. static struct reset_control *
  453. __reset_control_get_from_lookup(struct device *dev, const char *con_id,
  454. bool shared, bool optional)
  455. {
  456. const struct reset_control_lookup *lookup;
  457. struct reset_controller_dev *rcdev;
  458. const char *dev_id = dev_name(dev);
  459. struct reset_control *rstc = NULL;
  460. if (!dev)
  461. return ERR_PTR(-EINVAL);
  462. mutex_lock(&reset_lookup_mutex);
  463. list_for_each_entry(lookup, &reset_lookup_list, list) {
  464. if (strcmp(lookup->dev_id, dev_id))
  465. continue;
  466. if ((!con_id && !lookup->con_id) ||
  467. ((con_id && lookup->con_id) &&
  468. !strcmp(con_id, lookup->con_id))) {
  469. mutex_lock(&reset_list_mutex);
  470. rcdev = __reset_controller_by_name(lookup->provider);
  471. if (!rcdev) {
  472. mutex_unlock(&reset_list_mutex);
  473. mutex_unlock(&reset_lookup_mutex);
  474. /* Reset provider may not be ready yet. */
  475. return ERR_PTR(-EPROBE_DEFER);
  476. }
  477. rstc = __reset_control_get_internal(rcdev,
  478. lookup->index,
  479. shared);
  480. mutex_unlock(&reset_list_mutex);
  481. break;
  482. }
  483. }
  484. mutex_unlock(&reset_lookup_mutex);
  485. if (!rstc)
  486. return optional ? NULL : ERR_PTR(-ENOENT);
  487. return rstc;
  488. }
  489. struct reset_control *__reset_control_get(struct device *dev, const char *id,
  490. int index, bool shared, bool optional)
  491. {
  492. if (dev->of_node)
  493. return __of_reset_control_get(dev->of_node, id, index, shared,
  494. optional);
  495. return __reset_control_get_from_lookup(dev, id, shared, optional);
  496. }
  497. EXPORT_SYMBOL_GPL(__reset_control_get);
  498. static void reset_control_array_put(struct reset_control_array *resets)
  499. {
  500. int i;
  501. mutex_lock(&reset_list_mutex);
  502. for (i = 0; i < resets->num_rstcs; i++)
  503. __reset_control_put_internal(resets->rstc[i]);
  504. mutex_unlock(&reset_list_mutex);
  505. }
  506. /**
  507. * reset_control_put - free the reset controller
  508. * @rstc: reset controller
  509. */
  510. void reset_control_put(struct reset_control *rstc)
  511. {
  512. if (IS_ERR_OR_NULL(rstc))
  513. return;
  514. if (reset_control_is_array(rstc)) {
  515. reset_control_array_put(rstc_to_array(rstc));
  516. return;
  517. }
  518. mutex_lock(&reset_list_mutex);
  519. __reset_control_put_internal(rstc);
  520. mutex_unlock(&reset_list_mutex);
  521. }
  522. EXPORT_SYMBOL_GPL(reset_control_put);
  523. static void devm_reset_control_release(struct device *dev, void *res)
  524. {
  525. reset_control_put(*(struct reset_control **)res);
  526. }
  527. struct reset_control *__devm_reset_control_get(struct device *dev,
  528. const char *id, int index, bool shared,
  529. bool optional)
  530. {
  531. struct reset_control **ptr, *rstc;
  532. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  533. GFP_KERNEL);
  534. if (!ptr)
  535. return ERR_PTR(-ENOMEM);
  536. rstc = __reset_control_get(dev, id, index, shared, optional);
  537. if (!IS_ERR(rstc)) {
  538. *ptr = rstc;
  539. devres_add(dev, ptr);
  540. } else {
  541. devres_free(ptr);
  542. }
  543. return rstc;
  544. }
  545. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  546. /**
  547. * device_reset - find reset controller associated with the device
  548. * and perform reset
  549. * @dev: device to be reset by the controller
  550. * @optional: whether it is optional to reset the device
  551. *
  552. * Convenience wrapper for __reset_control_get() and reset_control_reset().
  553. * This is useful for the common case of devices with single, dedicated reset
  554. * lines.
  555. */
  556. int __device_reset(struct device *dev, bool optional)
  557. {
  558. struct reset_control *rstc;
  559. int ret;
  560. rstc = __reset_control_get(dev, NULL, 0, 0, optional);
  561. if (IS_ERR(rstc))
  562. return PTR_ERR(rstc);
  563. ret = reset_control_reset(rstc);
  564. reset_control_put(rstc);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL_GPL(__device_reset);
  568. /**
  569. * APIs to manage an array of reset controls.
  570. */
  571. /**
  572. * of_reset_control_get_count - Count number of resets available with a device
  573. *
  574. * @node: device node that contains 'resets'.
  575. *
  576. * Returns positive reset count on success, or error number on failure and
  577. * on count being zero.
  578. */
  579. static int of_reset_control_get_count(struct device_node *node)
  580. {
  581. int count;
  582. if (!node)
  583. return -EINVAL;
  584. count = of_count_phandle_with_args(node, "resets", "#reset-cells");
  585. if (count == 0)
  586. count = -ENOENT;
  587. return count;
  588. }
  589. /**
  590. * of_reset_control_array_get - Get a list of reset controls using
  591. * device node.
  592. *
  593. * @np: device node for the device that requests the reset controls array
  594. * @shared: whether reset controls are shared or not
  595. * @optional: whether it is optional to get the reset controls
  596. *
  597. * Returns pointer to allocated reset_control_array on success or
  598. * error on failure
  599. */
  600. struct reset_control *
  601. of_reset_control_array_get(struct device_node *np, bool shared, bool optional)
  602. {
  603. struct reset_control_array *resets;
  604. struct reset_control *rstc;
  605. int num, i;
  606. num = of_reset_control_get_count(np);
  607. if (num < 0)
  608. return optional ? NULL : ERR_PTR(num);
  609. resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
  610. if (!resets)
  611. return ERR_PTR(-ENOMEM);
  612. for (i = 0; i < num; i++) {
  613. rstc = __of_reset_control_get(np, NULL, i, shared, optional);
  614. if (IS_ERR(rstc))
  615. goto err_rst;
  616. resets->rstc[i] = rstc;
  617. }
  618. resets->num_rstcs = num;
  619. resets->base.array = true;
  620. return &resets->base;
  621. err_rst:
  622. mutex_lock(&reset_list_mutex);
  623. while (--i >= 0)
  624. __reset_control_put_internal(resets->rstc[i]);
  625. mutex_unlock(&reset_list_mutex);
  626. kfree(resets);
  627. return rstc;
  628. }
  629. EXPORT_SYMBOL_GPL(of_reset_control_array_get);
  630. /**
  631. * devm_reset_control_array_get - Resource managed reset control array get
  632. *
  633. * @dev: device that requests the list of reset controls
  634. * @shared: whether reset controls are shared or not
  635. * @optional: whether it is optional to get the reset controls
  636. *
  637. * The reset control array APIs are intended for a list of resets
  638. * that just have to be asserted or deasserted, without any
  639. * requirements on the order.
  640. *
  641. * Returns pointer to allocated reset_control_array on success or
  642. * error on failure
  643. */
  644. struct reset_control *
  645. devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
  646. {
  647. struct reset_control **devres;
  648. struct reset_control *rstc;
  649. devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
  650. GFP_KERNEL);
  651. if (!devres)
  652. return ERR_PTR(-ENOMEM);
  653. rstc = of_reset_control_array_get(dev->of_node, shared, optional);
  654. if (IS_ERR(rstc)) {
  655. devres_free(devres);
  656. return rstc;
  657. }
  658. *devres = rstc;
  659. devres_add(dev, devres);
  660. return rstc;
  661. }
  662. EXPORT_SYMBOL_GPL(devm_reset_control_array_get);