core.c 24 KB

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