core.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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/acpi.h>
  16. #include <linux/reset.h>
  17. #include <linux/reset-controller.h>
  18. #include <linux/slab.h>
  19. static DEFINE_MUTEX(reset_list_mutex);
  20. static LIST_HEAD(reset_controller_list);
  21. static DEFINE_MUTEX(reset_lookup_mutex);
  22. static LIST_HEAD(reset_lookup_list);
  23. /**
  24. * struct reset_control - a reset control
  25. * @rcdev: a pointer to the reset controller device
  26. * this reset control belongs to
  27. * @list: list entry for the rcdev's reset controller list
  28. * @id: ID of the reset controller in the reset
  29. * controller device
  30. * @refcnt: Number of gets of this reset_control
  31. * @acquired: Only one reset_control may be acquired for a given rcdev and id.
  32. * @shared: Is this a shared (1), or an exclusive (0) reset_control?
  33. * @array: Is this an array of reset controls (1)?
  34. * @deassert_count: Number of times this reset line has been deasserted
  35. * @triggered_count: Number of times this reset line has been reset. Currently
  36. * only used for shared resets, which means that the value
  37. * will be either 0 or 1.
  38. */
  39. struct reset_control {
  40. struct reset_controller_dev *rcdev;
  41. struct list_head list;
  42. unsigned int id;
  43. struct kref refcnt;
  44. bool acquired;
  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. static const char *rcdev_name(struct reset_controller_dev *rcdev)
  62. {
  63. if (rcdev->dev)
  64. return dev_name(rcdev->dev);
  65. if (rcdev->of_node)
  66. return rcdev->of_node->full_name;
  67. return NULL;
  68. }
  69. /**
  70. * of_reset_simple_xlate - translate reset_spec to the reset line number
  71. * @rcdev: a pointer to the reset controller device
  72. * @reset_spec: reset line specifier as found in the device tree
  73. *
  74. * This static translation function is used by default if of_xlate in
  75. * :c:type:`reset_controller_dev` is not set. It is useful for all reset
  76. * controllers with 1:1 mapping, where reset lines can be indexed by number
  77. * without gaps.
  78. */
  79. static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  80. const struct of_phandle_args *reset_spec)
  81. {
  82. if (reset_spec->args[0] >= rcdev->nr_resets)
  83. return -EINVAL;
  84. return reset_spec->args[0];
  85. }
  86. /**
  87. * reset_controller_register - register a reset controller device
  88. * @rcdev: a pointer to the initialized reset controller device
  89. */
  90. int reset_controller_register(struct reset_controller_dev *rcdev)
  91. {
  92. if (!rcdev->of_xlate) {
  93. rcdev->of_reset_n_cells = 1;
  94. rcdev->of_xlate = of_reset_simple_xlate;
  95. }
  96. INIT_LIST_HEAD(&rcdev->reset_control_head);
  97. mutex_lock(&reset_list_mutex);
  98. list_add(&rcdev->list, &reset_controller_list);
  99. mutex_unlock(&reset_list_mutex);
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(reset_controller_register);
  103. /**
  104. * reset_controller_unregister - unregister a reset controller device
  105. * @rcdev: a pointer to the reset controller device
  106. */
  107. void reset_controller_unregister(struct reset_controller_dev *rcdev)
  108. {
  109. mutex_lock(&reset_list_mutex);
  110. list_del(&rcdev->list);
  111. mutex_unlock(&reset_list_mutex);
  112. }
  113. EXPORT_SYMBOL_GPL(reset_controller_unregister);
  114. static void devm_reset_controller_release(struct device *dev, void *res)
  115. {
  116. reset_controller_unregister(*(struct reset_controller_dev **)res);
  117. }
  118. /**
  119. * devm_reset_controller_register - resource managed reset_controller_register()
  120. * @dev: device that is registering this reset controller
  121. * @rcdev: a pointer to the initialized reset controller device
  122. *
  123. * Managed reset_controller_register(). For reset controllers registered by
  124. * this function, reset_controller_unregister() is automatically called on
  125. * driver detach. See reset_controller_register() for more information.
  126. */
  127. int devm_reset_controller_register(struct device *dev,
  128. struct reset_controller_dev *rcdev)
  129. {
  130. struct reset_controller_dev **rcdevp;
  131. int ret;
  132. rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
  133. GFP_KERNEL);
  134. if (!rcdevp)
  135. return -ENOMEM;
  136. ret = reset_controller_register(rcdev);
  137. if (ret) {
  138. devres_free(rcdevp);
  139. return ret;
  140. }
  141. *rcdevp = rcdev;
  142. devres_add(dev, rcdevp);
  143. return ret;
  144. }
  145. EXPORT_SYMBOL_GPL(devm_reset_controller_register);
  146. /**
  147. * reset_controller_add_lookup - register a set of lookup entries
  148. * @lookup: array of reset lookup entries
  149. * @num_entries: number of entries in the lookup array
  150. */
  151. void reset_controller_add_lookup(struct reset_control_lookup *lookup,
  152. unsigned int num_entries)
  153. {
  154. struct reset_control_lookup *entry;
  155. unsigned int i;
  156. mutex_lock(&reset_lookup_mutex);
  157. for (i = 0; i < num_entries; i++) {
  158. entry = &lookup[i];
  159. if (!entry->dev_id || !entry->provider) {
  160. pr_warn("%s(): reset lookup entry badly specified, skipping\n",
  161. __func__);
  162. continue;
  163. }
  164. list_add_tail(&entry->list, &reset_lookup_list);
  165. }
  166. mutex_unlock(&reset_lookup_mutex);
  167. }
  168. EXPORT_SYMBOL_GPL(reset_controller_add_lookup);
  169. static inline struct reset_control_array *
  170. rstc_to_array(struct reset_control *rstc) {
  171. return container_of(rstc, struct reset_control_array, base);
  172. }
  173. static int reset_control_array_reset(struct reset_control_array *resets)
  174. {
  175. int ret, i;
  176. for (i = 0; i < resets->num_rstcs; i++) {
  177. ret = reset_control_reset(resets->rstc[i]);
  178. if (ret)
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. static int reset_control_array_rearm(struct reset_control_array *resets)
  184. {
  185. struct reset_control *rstc;
  186. int i;
  187. for (i = 0; i < resets->num_rstcs; i++) {
  188. rstc = resets->rstc[i];
  189. if (!rstc)
  190. continue;
  191. if (WARN_ON(IS_ERR(rstc)))
  192. return -EINVAL;
  193. if (rstc->shared) {
  194. if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
  195. return -EINVAL;
  196. } else {
  197. if (!rstc->acquired)
  198. return -EPERM;
  199. }
  200. }
  201. for (i = 0; i < resets->num_rstcs; i++) {
  202. rstc = resets->rstc[i];
  203. if (rstc && rstc->shared)
  204. WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0);
  205. }
  206. return 0;
  207. }
  208. static int reset_control_array_assert(struct reset_control_array *resets)
  209. {
  210. int ret, i;
  211. for (i = 0; i < resets->num_rstcs; i++) {
  212. ret = reset_control_assert(resets->rstc[i]);
  213. if (ret)
  214. goto err;
  215. }
  216. return 0;
  217. err:
  218. while (i--)
  219. reset_control_deassert(resets->rstc[i]);
  220. return ret;
  221. }
  222. static int reset_control_array_deassert(struct reset_control_array *resets)
  223. {
  224. int ret, i;
  225. for (i = 0; i < resets->num_rstcs; i++) {
  226. ret = reset_control_deassert(resets->rstc[i]);
  227. if (ret)
  228. goto err;
  229. }
  230. return 0;
  231. err:
  232. while (i--)
  233. reset_control_assert(resets->rstc[i]);
  234. return ret;
  235. }
  236. static int reset_control_array_acquire(struct reset_control_array *resets)
  237. {
  238. unsigned int i;
  239. int err;
  240. for (i = 0; i < resets->num_rstcs; i++) {
  241. err = reset_control_acquire(resets->rstc[i]);
  242. if (err < 0)
  243. goto release;
  244. }
  245. return 0;
  246. release:
  247. while (i--)
  248. reset_control_release(resets->rstc[i]);
  249. return err;
  250. }
  251. static void reset_control_array_release(struct reset_control_array *resets)
  252. {
  253. unsigned int i;
  254. for (i = 0; i < resets->num_rstcs; i++)
  255. reset_control_release(resets->rstc[i]);
  256. }
  257. static inline bool reset_control_is_array(struct reset_control *rstc)
  258. {
  259. return rstc->array;
  260. }
  261. /**
  262. * reset_control_reset - reset the controlled device
  263. * @rstc: reset controller
  264. *
  265. * On a shared reset line the actual reset pulse is only triggered once for the
  266. * lifetime of the reset_control instance: for all but the first caller this is
  267. * a no-op.
  268. * Consumers must not use reset_control_(de)assert on shared reset lines when
  269. * reset_control_reset has been used.
  270. *
  271. * If rstc is NULL it is an optional reset and the function will just
  272. * return 0.
  273. */
  274. int reset_control_reset(struct reset_control *rstc)
  275. {
  276. int ret;
  277. if (!rstc)
  278. return 0;
  279. if (WARN_ON(IS_ERR(rstc)))
  280. return -EINVAL;
  281. if (reset_control_is_array(rstc))
  282. return reset_control_array_reset(rstc_to_array(rstc));
  283. if (!rstc->rcdev->ops->reset)
  284. return -ENOTSUPP;
  285. if (rstc->shared) {
  286. if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
  287. return -EINVAL;
  288. if (atomic_inc_return(&rstc->triggered_count) != 1)
  289. return 0;
  290. } else {
  291. if (!rstc->acquired)
  292. return -EPERM;
  293. }
  294. ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  295. if (rstc->shared && ret)
  296. atomic_dec(&rstc->triggered_count);
  297. return ret;
  298. }
  299. EXPORT_SYMBOL_GPL(reset_control_reset);
  300. /**
  301. * reset_control_bulk_reset - reset the controlled devices in order
  302. * @num_rstcs: number of entries in rstcs array
  303. * @rstcs: array of struct reset_control_bulk_data with reset controls set
  304. *
  305. * Issue a reset on all provided reset controls, in order.
  306. *
  307. * See also: reset_control_reset()
  308. */
  309. int reset_control_bulk_reset(int num_rstcs,
  310. struct reset_control_bulk_data *rstcs)
  311. {
  312. int ret, i;
  313. for (i = 0; i < num_rstcs; i++) {
  314. ret = reset_control_reset(rstcs[i].rstc);
  315. if (ret)
  316. return ret;
  317. }
  318. return 0;
  319. }
  320. EXPORT_SYMBOL_GPL(reset_control_bulk_reset);
  321. /**
  322. * reset_control_rearm - allow shared reset line to be re-triggered"
  323. * @rstc: reset controller
  324. *
  325. * On a shared reset line the actual reset pulse is only triggered once for the
  326. * lifetime of the reset_control instance, except if this call is used.
  327. *
  328. * Calls to this function must be balanced with calls to reset_control_reset,
  329. * a warning is thrown in case triggered_count ever dips below 0.
  330. *
  331. * Consumers must not use reset_control_(de)assert on shared reset lines when
  332. * reset_control_reset or reset_control_rearm have been used.
  333. *
  334. * If rstc is NULL the function will just return 0.
  335. */
  336. int reset_control_rearm(struct reset_control *rstc)
  337. {
  338. if (!rstc)
  339. return 0;
  340. if (WARN_ON(IS_ERR(rstc)))
  341. return -EINVAL;
  342. if (reset_control_is_array(rstc))
  343. return reset_control_array_rearm(rstc_to_array(rstc));
  344. if (rstc->shared) {
  345. if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
  346. return -EINVAL;
  347. WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0);
  348. } else {
  349. if (!rstc->acquired)
  350. return -EPERM;
  351. }
  352. return 0;
  353. }
  354. EXPORT_SYMBOL_GPL(reset_control_rearm);
  355. /**
  356. * reset_control_assert - asserts the reset line
  357. * @rstc: reset controller
  358. *
  359. * Calling this on an exclusive reset controller guarantees that the reset
  360. * will be asserted. When called on a shared reset controller the line may
  361. * still be deasserted, as long as other users keep it so.
  362. *
  363. * For shared reset controls a driver cannot expect the hw's registers and
  364. * internal state to be reset, but must be prepared for this to happen.
  365. * Consumers must not use reset_control_reset on shared reset lines when
  366. * reset_control_(de)assert has been used.
  367. *
  368. * If rstc is NULL it is an optional reset and the function will just
  369. * return 0.
  370. */
  371. int reset_control_assert(struct reset_control *rstc)
  372. {
  373. if (!rstc)
  374. return 0;
  375. if (WARN_ON(IS_ERR(rstc)))
  376. return -EINVAL;
  377. if (reset_control_is_array(rstc))
  378. return reset_control_array_assert(rstc_to_array(rstc));
  379. if (rstc->shared) {
  380. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  381. return -EINVAL;
  382. if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
  383. return -EINVAL;
  384. if (atomic_dec_return(&rstc->deassert_count) != 0)
  385. return 0;
  386. /*
  387. * Shared reset controls allow the reset line to be in any state
  388. * after this call, so doing nothing is a valid option.
  389. */
  390. if (!rstc->rcdev->ops->assert)
  391. return 0;
  392. } else {
  393. /*
  394. * If the reset controller does not implement .assert(), there
  395. * is no way to guarantee that the reset line is asserted after
  396. * this call.
  397. */
  398. if (!rstc->rcdev->ops->assert)
  399. return -ENOTSUPP;
  400. if (!rstc->acquired) {
  401. WARN(1, "reset %s (ID: %u) is not acquired\n",
  402. rcdev_name(rstc->rcdev), rstc->id);
  403. return -EPERM;
  404. }
  405. }
  406. return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
  407. }
  408. EXPORT_SYMBOL_GPL(reset_control_assert);
  409. /**
  410. * reset_control_bulk_assert - asserts the reset lines in order
  411. * @num_rstcs: number of entries in rstcs array
  412. * @rstcs: array of struct reset_control_bulk_data with reset controls set
  413. *
  414. * Assert the reset lines for all provided reset controls, in order.
  415. * If an assertion fails, already asserted resets are deasserted again.
  416. *
  417. * See also: reset_control_assert()
  418. */
  419. int reset_control_bulk_assert(int num_rstcs,
  420. struct reset_control_bulk_data *rstcs)
  421. {
  422. int ret, i;
  423. for (i = 0; i < num_rstcs; i++) {
  424. ret = reset_control_assert(rstcs[i].rstc);
  425. if (ret)
  426. goto err;
  427. }
  428. return 0;
  429. err:
  430. while (i--)
  431. reset_control_deassert(rstcs[i].rstc);
  432. return ret;
  433. }
  434. EXPORT_SYMBOL_GPL(reset_control_bulk_assert);
  435. /**
  436. * reset_control_deassert - deasserts the reset line
  437. * @rstc: reset controller
  438. *
  439. * After calling this function, the reset is guaranteed to be deasserted.
  440. * Consumers must not use reset_control_reset on shared reset lines when
  441. * reset_control_(de)assert has been used.
  442. *
  443. * If rstc is NULL it is an optional reset and the function will just
  444. * return 0.
  445. */
  446. int reset_control_deassert(struct reset_control *rstc)
  447. {
  448. if (!rstc)
  449. return 0;
  450. if (WARN_ON(IS_ERR(rstc)))
  451. return -EINVAL;
  452. if (reset_control_is_array(rstc))
  453. return reset_control_array_deassert(rstc_to_array(rstc));
  454. if (rstc->shared) {
  455. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  456. return -EINVAL;
  457. if (atomic_inc_return(&rstc->deassert_count) != 1)
  458. return 0;
  459. } else {
  460. if (!rstc->acquired) {
  461. WARN(1, "reset %s (ID: %u) is not acquired\n",
  462. rcdev_name(rstc->rcdev), rstc->id);
  463. return -EPERM;
  464. }
  465. }
  466. /*
  467. * If the reset controller does not implement .deassert(), we assume
  468. * that it handles self-deasserting reset lines via .reset(). In that
  469. * case, the reset lines are deasserted by default. If that is not the
  470. * case, the reset controller driver should implement .deassert() and
  471. * return -ENOTSUPP.
  472. */
  473. if (!rstc->rcdev->ops->deassert)
  474. return 0;
  475. return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
  476. }
  477. EXPORT_SYMBOL_GPL(reset_control_deassert);
  478. /**
  479. * reset_control_bulk_deassert - deasserts the reset lines in reverse order
  480. * @num_rstcs: number of entries in rstcs array
  481. * @rstcs: array of struct reset_control_bulk_data with reset controls set
  482. *
  483. * Deassert the reset lines for all provided reset controls, in reverse order.
  484. * If a deassertion fails, already deasserted resets are asserted again.
  485. *
  486. * See also: reset_control_deassert()
  487. */
  488. int reset_control_bulk_deassert(int num_rstcs,
  489. struct reset_control_bulk_data *rstcs)
  490. {
  491. int ret, i;
  492. for (i = num_rstcs - 1; i >= 0; i--) {
  493. ret = reset_control_deassert(rstcs[i].rstc);
  494. if (ret)
  495. goto err;
  496. }
  497. return 0;
  498. err:
  499. while (i < num_rstcs)
  500. reset_control_assert(rstcs[i++].rstc);
  501. return ret;
  502. }
  503. EXPORT_SYMBOL_GPL(reset_control_bulk_deassert);
  504. /**
  505. * reset_control_status - returns a negative errno if not supported, a
  506. * positive value if the reset line is asserted, or zero if the reset
  507. * line is not asserted or if the desc is NULL (optional reset).
  508. * @rstc: reset controller
  509. */
  510. int reset_control_status(struct reset_control *rstc)
  511. {
  512. if (!rstc)
  513. return 0;
  514. if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
  515. return -EINVAL;
  516. if (rstc->rcdev->ops->status)
  517. return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
  518. return -ENOTSUPP;
  519. }
  520. EXPORT_SYMBOL_GPL(reset_control_status);
  521. /**
  522. * reset_control_acquire() - acquires a reset control for exclusive use
  523. * @rstc: reset control
  524. *
  525. * This is used to explicitly acquire a reset control for exclusive use. Note
  526. * that exclusive resets are requested as acquired by default. In order for a
  527. * second consumer to be able to control the reset, the first consumer has to
  528. * release it first. Typically the easiest way to achieve this is to call the
  529. * reset_control_get_exclusive_released() to obtain an instance of the reset
  530. * control. Such reset controls are not acquired by default.
  531. *
  532. * Consumers implementing shared access to an exclusive reset need to follow
  533. * a specific protocol in order to work together. Before consumers can change
  534. * a reset they must acquire exclusive access using reset_control_acquire().
  535. * After they are done operating the reset, they must release exclusive access
  536. * with a call to reset_control_release(). Consumers are not granted exclusive
  537. * access to the reset as long as another consumer hasn't released a reset.
  538. *
  539. * See also: reset_control_release()
  540. */
  541. int reset_control_acquire(struct reset_control *rstc)
  542. {
  543. struct reset_control *rc;
  544. if (!rstc)
  545. return 0;
  546. if (WARN_ON(IS_ERR(rstc)))
  547. return -EINVAL;
  548. if (reset_control_is_array(rstc))
  549. return reset_control_array_acquire(rstc_to_array(rstc));
  550. mutex_lock(&reset_list_mutex);
  551. if (rstc->acquired) {
  552. mutex_unlock(&reset_list_mutex);
  553. return 0;
  554. }
  555. list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) {
  556. if (rstc != rc && rstc->id == rc->id) {
  557. if (rc->acquired) {
  558. mutex_unlock(&reset_list_mutex);
  559. return -EBUSY;
  560. }
  561. }
  562. }
  563. rstc->acquired = true;
  564. mutex_unlock(&reset_list_mutex);
  565. return 0;
  566. }
  567. EXPORT_SYMBOL_GPL(reset_control_acquire);
  568. /**
  569. * reset_control_bulk_acquire - acquires reset controls for exclusive use
  570. * @num_rstcs: number of entries in rstcs array
  571. * @rstcs: array of struct reset_control_bulk_data with reset controls set
  572. *
  573. * This is used to explicitly acquire reset controls requested with
  574. * reset_control_bulk_get_exclusive_release() for temporary exclusive use.
  575. *
  576. * See also: reset_control_acquire(), reset_control_bulk_release()
  577. */
  578. int reset_control_bulk_acquire(int num_rstcs,
  579. struct reset_control_bulk_data *rstcs)
  580. {
  581. int ret, i;
  582. for (i = 0; i < num_rstcs; i++) {
  583. ret = reset_control_acquire(rstcs[i].rstc);
  584. if (ret)
  585. goto err;
  586. }
  587. return 0;
  588. err:
  589. while (i--)
  590. reset_control_release(rstcs[i].rstc);
  591. return ret;
  592. }
  593. EXPORT_SYMBOL_GPL(reset_control_bulk_acquire);
  594. /**
  595. * reset_control_release() - releases exclusive access to a reset control
  596. * @rstc: reset control
  597. *
  598. * Releases exclusive access right to a reset control previously obtained by a
  599. * call to reset_control_acquire(). Until a consumer calls this function, no
  600. * other consumers will be granted exclusive access.
  601. *
  602. * See also: reset_control_acquire()
  603. */
  604. void reset_control_release(struct reset_control *rstc)
  605. {
  606. if (!rstc || WARN_ON(IS_ERR(rstc)))
  607. return;
  608. if (reset_control_is_array(rstc))
  609. reset_control_array_release(rstc_to_array(rstc));
  610. else
  611. rstc->acquired = false;
  612. }
  613. EXPORT_SYMBOL_GPL(reset_control_release);
  614. /**
  615. * reset_control_bulk_release() - releases exclusive access to reset controls
  616. * @num_rstcs: number of entries in rstcs array
  617. * @rstcs: array of struct reset_control_bulk_data with reset controls set
  618. *
  619. * Releases exclusive access right to reset controls previously obtained by a
  620. * call to reset_control_bulk_acquire().
  621. *
  622. * See also: reset_control_release(), reset_control_bulk_acquire()
  623. */
  624. void reset_control_bulk_release(int num_rstcs,
  625. struct reset_control_bulk_data *rstcs)
  626. {
  627. int i;
  628. for (i = 0; i < num_rstcs; i++)
  629. reset_control_release(rstcs[i].rstc);
  630. }
  631. EXPORT_SYMBOL_GPL(reset_control_bulk_release);
  632. static struct reset_control *
  633. __reset_control_get_internal(struct reset_controller_dev *rcdev,
  634. unsigned int index, bool shared, bool acquired)
  635. {
  636. struct reset_control *rstc;
  637. lockdep_assert_held(&reset_list_mutex);
  638. list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
  639. if (rstc->id == index) {
  640. /*
  641. * Allow creating a secondary exclusive reset_control
  642. * that is initially not acquired for an already
  643. * controlled reset line.
  644. */
  645. if (!rstc->shared && !shared && !acquired)
  646. break;
  647. if (WARN_ON(!rstc->shared || !shared))
  648. return ERR_PTR(-EBUSY);
  649. kref_get(&rstc->refcnt);
  650. return rstc;
  651. }
  652. }
  653. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  654. if (!rstc)
  655. return ERR_PTR(-ENOMEM);
  656. if (!try_module_get(rcdev->owner)) {
  657. kfree(rstc);
  658. return ERR_PTR(-ENODEV);
  659. }
  660. rstc->rcdev = rcdev;
  661. list_add(&rstc->list, &rcdev->reset_control_head);
  662. rstc->id = index;
  663. kref_init(&rstc->refcnt);
  664. rstc->acquired = acquired;
  665. rstc->shared = shared;
  666. return rstc;
  667. }
  668. static void __reset_control_release(struct kref *kref)
  669. {
  670. struct reset_control *rstc = container_of(kref, struct reset_control,
  671. refcnt);
  672. lockdep_assert_held(&reset_list_mutex);
  673. module_put(rstc->rcdev->owner);
  674. list_del(&rstc->list);
  675. kfree(rstc);
  676. }
  677. static void __reset_control_put_internal(struct reset_control *rstc)
  678. {
  679. lockdep_assert_held(&reset_list_mutex);
  680. kref_put(&rstc->refcnt, __reset_control_release);
  681. }
  682. struct reset_control *
  683. __of_reset_control_get(struct device_node *node, const char *id, int index,
  684. bool shared, bool optional, bool acquired)
  685. {
  686. struct reset_control *rstc;
  687. struct reset_controller_dev *r, *rcdev;
  688. struct of_phandle_args args;
  689. int rstc_id;
  690. int ret;
  691. if (!node)
  692. return ERR_PTR(-EINVAL);
  693. if (id) {
  694. index = of_property_match_string(node,
  695. "reset-names", id);
  696. if (index == -EILSEQ)
  697. return ERR_PTR(index);
  698. if (index < 0)
  699. return optional ? NULL : ERR_PTR(-ENOENT);
  700. }
  701. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  702. index, &args);
  703. if (ret == -EINVAL)
  704. return ERR_PTR(ret);
  705. if (ret)
  706. return optional ? NULL : ERR_PTR(ret);
  707. mutex_lock(&reset_list_mutex);
  708. rcdev = NULL;
  709. list_for_each_entry(r, &reset_controller_list, list) {
  710. if (args.np == r->of_node) {
  711. rcdev = r;
  712. break;
  713. }
  714. }
  715. if (!rcdev) {
  716. rstc = ERR_PTR(-EPROBE_DEFER);
  717. goto out;
  718. }
  719. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  720. rstc = ERR_PTR(-EINVAL);
  721. goto out;
  722. }
  723. rstc_id = rcdev->of_xlate(rcdev, &args);
  724. if (rstc_id < 0) {
  725. rstc = ERR_PTR(rstc_id);
  726. goto out;
  727. }
  728. /* reset_list_mutex also protects the rcdev's reset_control list */
  729. rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired);
  730. out:
  731. mutex_unlock(&reset_list_mutex);
  732. of_node_put(args.np);
  733. return rstc;
  734. }
  735. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  736. static struct reset_controller_dev *
  737. __reset_controller_by_name(const char *name)
  738. {
  739. struct reset_controller_dev *rcdev;
  740. lockdep_assert_held(&reset_list_mutex);
  741. list_for_each_entry(rcdev, &reset_controller_list, list) {
  742. if (!rcdev->dev)
  743. continue;
  744. if (!strcmp(name, dev_name(rcdev->dev)))
  745. return rcdev;
  746. }
  747. return NULL;
  748. }
  749. static struct reset_control *
  750. __reset_control_get_from_lookup(struct device *dev, const char *con_id,
  751. bool shared, bool optional, bool acquired)
  752. {
  753. const struct reset_control_lookup *lookup;
  754. struct reset_controller_dev *rcdev;
  755. const char *dev_id = dev_name(dev);
  756. struct reset_control *rstc = NULL;
  757. mutex_lock(&reset_lookup_mutex);
  758. list_for_each_entry(lookup, &reset_lookup_list, list) {
  759. if (strcmp(lookup->dev_id, dev_id))
  760. continue;
  761. if ((!con_id && !lookup->con_id) ||
  762. ((con_id && lookup->con_id) &&
  763. !strcmp(con_id, lookup->con_id))) {
  764. mutex_lock(&reset_list_mutex);
  765. rcdev = __reset_controller_by_name(lookup->provider);
  766. if (!rcdev) {
  767. mutex_unlock(&reset_list_mutex);
  768. mutex_unlock(&reset_lookup_mutex);
  769. /* Reset provider may not be ready yet. */
  770. return ERR_PTR(-EPROBE_DEFER);
  771. }
  772. rstc = __reset_control_get_internal(rcdev,
  773. lookup->index,
  774. shared, acquired);
  775. mutex_unlock(&reset_list_mutex);
  776. break;
  777. }
  778. }
  779. mutex_unlock(&reset_lookup_mutex);
  780. if (!rstc)
  781. return optional ? NULL : ERR_PTR(-ENOENT);
  782. return rstc;
  783. }
  784. struct reset_control *__reset_control_get(struct device *dev, const char *id,
  785. int index, bool shared, bool optional,
  786. bool acquired)
  787. {
  788. if (WARN_ON(shared && acquired))
  789. return ERR_PTR(-EINVAL);
  790. if (dev->of_node)
  791. return __of_reset_control_get(dev->of_node, id, index, shared,
  792. optional, acquired);
  793. return __reset_control_get_from_lookup(dev, id, shared, optional,
  794. acquired);
  795. }
  796. EXPORT_SYMBOL_GPL(__reset_control_get);
  797. int __reset_control_bulk_get(struct device *dev, int num_rstcs,
  798. struct reset_control_bulk_data *rstcs,
  799. bool shared, bool optional, bool acquired)
  800. {
  801. int ret, i;
  802. for (i = 0; i < num_rstcs; i++) {
  803. rstcs[i].rstc = __reset_control_get(dev, rstcs[i].id, 0,
  804. shared, optional, acquired);
  805. if (IS_ERR(rstcs[i].rstc)) {
  806. ret = PTR_ERR(rstcs[i].rstc);
  807. goto err;
  808. }
  809. }
  810. return 0;
  811. err:
  812. mutex_lock(&reset_list_mutex);
  813. while (i--)
  814. __reset_control_put_internal(rstcs[i].rstc);
  815. mutex_unlock(&reset_list_mutex);
  816. return ret;
  817. }
  818. EXPORT_SYMBOL_GPL(__reset_control_bulk_get);
  819. static void reset_control_array_put(struct reset_control_array *resets)
  820. {
  821. int i;
  822. mutex_lock(&reset_list_mutex);
  823. for (i = 0; i < resets->num_rstcs; i++)
  824. __reset_control_put_internal(resets->rstc[i]);
  825. mutex_unlock(&reset_list_mutex);
  826. kfree(resets);
  827. }
  828. /**
  829. * reset_control_put - free the reset controller
  830. * @rstc: reset controller
  831. */
  832. void reset_control_put(struct reset_control *rstc)
  833. {
  834. if (IS_ERR_OR_NULL(rstc))
  835. return;
  836. if (reset_control_is_array(rstc)) {
  837. reset_control_array_put(rstc_to_array(rstc));
  838. return;
  839. }
  840. mutex_lock(&reset_list_mutex);
  841. __reset_control_put_internal(rstc);
  842. mutex_unlock(&reset_list_mutex);
  843. }
  844. EXPORT_SYMBOL_GPL(reset_control_put);
  845. /**
  846. * reset_control_bulk_put - free the reset controllers
  847. * @num_rstcs: number of entries in rstcs array
  848. * @rstcs: array of struct reset_control_bulk_data with reset controls set
  849. */
  850. void reset_control_bulk_put(int num_rstcs, struct reset_control_bulk_data *rstcs)
  851. {
  852. mutex_lock(&reset_list_mutex);
  853. while (num_rstcs--) {
  854. if (IS_ERR_OR_NULL(rstcs[num_rstcs].rstc))
  855. continue;
  856. __reset_control_put_internal(rstcs[num_rstcs].rstc);
  857. }
  858. mutex_unlock(&reset_list_mutex);
  859. }
  860. EXPORT_SYMBOL_GPL(reset_control_bulk_put);
  861. static void devm_reset_control_release(struct device *dev, void *res)
  862. {
  863. reset_control_put(*(struct reset_control **)res);
  864. }
  865. struct reset_control *
  866. __devm_reset_control_get(struct device *dev, const char *id, int index,
  867. bool shared, bool optional, bool acquired)
  868. {
  869. struct reset_control **ptr, *rstc;
  870. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  871. GFP_KERNEL);
  872. if (!ptr)
  873. return ERR_PTR(-ENOMEM);
  874. rstc = __reset_control_get(dev, id, index, shared, optional, acquired);
  875. if (IS_ERR_OR_NULL(rstc)) {
  876. devres_free(ptr);
  877. return rstc;
  878. }
  879. *ptr = rstc;
  880. devres_add(dev, ptr);
  881. return rstc;
  882. }
  883. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  884. struct reset_control_bulk_devres {
  885. int num_rstcs;
  886. struct reset_control_bulk_data *rstcs;
  887. };
  888. static void devm_reset_control_bulk_release(struct device *dev, void *res)
  889. {
  890. struct reset_control_bulk_devres *devres = res;
  891. reset_control_bulk_put(devres->num_rstcs, devres->rstcs);
  892. }
  893. int __devm_reset_control_bulk_get(struct device *dev, int num_rstcs,
  894. struct reset_control_bulk_data *rstcs,
  895. bool shared, bool optional, bool acquired)
  896. {
  897. struct reset_control_bulk_devres *ptr;
  898. int ret;
  899. ptr = devres_alloc(devm_reset_control_bulk_release, sizeof(*ptr),
  900. GFP_KERNEL);
  901. if (!ptr)
  902. return -ENOMEM;
  903. ret = __reset_control_bulk_get(dev, num_rstcs, rstcs, shared, optional, acquired);
  904. if (ret < 0) {
  905. devres_free(ptr);
  906. return ret;
  907. }
  908. ptr->num_rstcs = num_rstcs;
  909. ptr->rstcs = rstcs;
  910. devres_add(dev, ptr);
  911. return 0;
  912. }
  913. EXPORT_SYMBOL_GPL(__devm_reset_control_bulk_get);
  914. /**
  915. * __device_reset - find reset controller associated with the device
  916. * and perform reset
  917. * @dev: device to be reset by the controller
  918. * @optional: whether it is optional to reset the device
  919. *
  920. * Convenience wrapper for __reset_control_get() and reset_control_reset().
  921. * This is useful for the common case of devices with single, dedicated reset
  922. * lines. _RST firmware method will be called for devices with ACPI.
  923. */
  924. int __device_reset(struct device *dev, bool optional)
  925. {
  926. struct reset_control *rstc;
  927. int ret;
  928. #ifdef CONFIG_ACPI
  929. acpi_handle handle = ACPI_HANDLE(dev);
  930. if (handle) {
  931. if (!acpi_has_method(handle, "_RST"))
  932. return optional ? 0 : -ENOENT;
  933. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST", NULL,
  934. NULL)))
  935. return -EIO;
  936. }
  937. #endif
  938. rstc = __reset_control_get(dev, NULL, 0, 0, optional, true);
  939. if (IS_ERR(rstc))
  940. return PTR_ERR(rstc);
  941. ret = reset_control_reset(rstc);
  942. reset_control_put(rstc);
  943. return ret;
  944. }
  945. EXPORT_SYMBOL_GPL(__device_reset);
  946. /*
  947. * APIs to manage an array of reset controls.
  948. */
  949. /**
  950. * of_reset_control_get_count - Count number of resets available with a device
  951. *
  952. * @node: device node that contains 'resets'.
  953. *
  954. * Returns positive reset count on success, or error number on failure and
  955. * on count being zero.
  956. */
  957. static int of_reset_control_get_count(struct device_node *node)
  958. {
  959. int count;
  960. if (!node)
  961. return -EINVAL;
  962. count = of_count_phandle_with_args(node, "resets", "#reset-cells");
  963. if (count == 0)
  964. count = -ENOENT;
  965. return count;
  966. }
  967. /**
  968. * of_reset_control_array_get - Get a list of reset controls using
  969. * device node.
  970. *
  971. * @np: device node for the device that requests the reset controls array
  972. * @shared: whether reset controls are shared or not
  973. * @optional: whether it is optional to get the reset controls
  974. * @acquired: only one reset control may be acquired for a given controller
  975. * and ID
  976. *
  977. * Returns pointer to allocated reset_control on success or error on failure
  978. */
  979. struct reset_control *
  980. of_reset_control_array_get(struct device_node *np, bool shared, bool optional,
  981. bool acquired)
  982. {
  983. struct reset_control_array *resets;
  984. struct reset_control *rstc;
  985. int num, i;
  986. num = of_reset_control_get_count(np);
  987. if (num < 0)
  988. return optional ? NULL : ERR_PTR(num);
  989. resets = kzalloc(struct_size(resets, rstc, num), GFP_KERNEL);
  990. if (!resets)
  991. return ERR_PTR(-ENOMEM);
  992. for (i = 0; i < num; i++) {
  993. rstc = __of_reset_control_get(np, NULL, i, shared, optional,
  994. acquired);
  995. if (IS_ERR(rstc))
  996. goto err_rst;
  997. resets->rstc[i] = rstc;
  998. }
  999. resets->num_rstcs = num;
  1000. resets->base.array = true;
  1001. return &resets->base;
  1002. err_rst:
  1003. mutex_lock(&reset_list_mutex);
  1004. while (--i >= 0)
  1005. __reset_control_put_internal(resets->rstc[i]);
  1006. mutex_unlock(&reset_list_mutex);
  1007. kfree(resets);
  1008. return rstc;
  1009. }
  1010. EXPORT_SYMBOL_GPL(of_reset_control_array_get);
  1011. /**
  1012. * devm_reset_control_array_get - Resource managed reset control array get
  1013. *
  1014. * @dev: device that requests the list of reset controls
  1015. * @shared: whether reset controls are shared or not
  1016. * @optional: whether it is optional to get the reset controls
  1017. *
  1018. * The reset control array APIs are intended for a list of resets
  1019. * that just have to be asserted or deasserted, without any
  1020. * requirements on the order.
  1021. *
  1022. * Returns pointer to allocated reset_control on success or error on failure
  1023. */
  1024. struct reset_control *
  1025. devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
  1026. {
  1027. struct reset_control **ptr, *rstc;
  1028. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  1029. GFP_KERNEL);
  1030. if (!ptr)
  1031. return ERR_PTR(-ENOMEM);
  1032. rstc = of_reset_control_array_get(dev->of_node, shared, optional, true);
  1033. if (IS_ERR_OR_NULL(rstc)) {
  1034. devres_free(ptr);
  1035. return rstc;
  1036. }
  1037. *ptr = rstc;
  1038. devres_add(dev, ptr);
  1039. return rstc;
  1040. }
  1041. EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
  1042. static int reset_control_get_count_from_lookup(struct device *dev)
  1043. {
  1044. const struct reset_control_lookup *lookup;
  1045. const char *dev_id;
  1046. int count = 0;
  1047. if (!dev)
  1048. return -EINVAL;
  1049. dev_id = dev_name(dev);
  1050. mutex_lock(&reset_lookup_mutex);
  1051. list_for_each_entry(lookup, &reset_lookup_list, list) {
  1052. if (!strcmp(lookup->dev_id, dev_id))
  1053. count++;
  1054. }
  1055. mutex_unlock(&reset_lookup_mutex);
  1056. if (count == 0)
  1057. count = -ENOENT;
  1058. return count;
  1059. }
  1060. /**
  1061. * reset_control_get_count - Count number of resets available with a device
  1062. *
  1063. * @dev: device for which to return the number of resets
  1064. *
  1065. * Returns positive reset count on success, or error number on failure and
  1066. * on count being zero.
  1067. */
  1068. int reset_control_get_count(struct device *dev)
  1069. {
  1070. if (dev->of_node)
  1071. return of_reset_control_get_count(dev->of_node);
  1072. return reset_control_get_count_from_lookup(dev);
  1073. }
  1074. EXPORT_SYMBOL_GPL(reset_control_get_count);