core.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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/acpi.h>
  8. #include <linux/atomic.h>
  9. #include <linux/auxiliary_bus.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/export.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/gpio/machine.h>
  16. #include <linux/gpio/property.h>
  17. #include <linux/idr.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kref.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/reset.h>
  23. #include <linux/reset-controller.h>
  24. #include <linux/slab.h>
  25. static DEFINE_MUTEX(reset_list_mutex);
  26. static LIST_HEAD(reset_controller_list);
  27. /* Protects reset_gpio_lookup_list */
  28. static DEFINE_MUTEX(reset_gpio_lookup_mutex);
  29. static LIST_HEAD(reset_gpio_lookup_list);
  30. static DEFINE_IDA(reset_gpio_ida);
  31. /**
  32. * struct reset_control - a reset control
  33. * @rcdev: a pointer to the reset controller device
  34. * this reset control belongs to
  35. * @list: list entry for the rcdev's reset controller list
  36. * @id: ID of the reset controller in the reset
  37. * controller device
  38. * @refcnt: Number of gets of this reset_control
  39. * @acquired: Only one reset_control may be acquired for a given rcdev and id.
  40. * @shared: Is this a shared (1), or an exclusive (0) reset_control?
  41. * @array: Is this an array of reset controls (1)?
  42. * @deassert_count: Number of times this reset line has been deasserted
  43. * @triggered_count: Number of times this reset line has been reset. Currently
  44. * only used for shared resets, which means that the value
  45. * will be either 0 or 1.
  46. */
  47. struct reset_control {
  48. struct reset_controller_dev *rcdev;
  49. struct list_head list;
  50. unsigned int id;
  51. struct kref refcnt;
  52. bool acquired;
  53. bool shared;
  54. bool array;
  55. atomic_t deassert_count;
  56. atomic_t triggered_count;
  57. };
  58. /**
  59. * struct reset_control_array - an array of reset controls
  60. * @base: reset control for compatibility with reset control API functions
  61. * @num_rstcs: number of reset controls
  62. * @rstc: array of reset controls
  63. */
  64. struct reset_control_array {
  65. struct reset_control base;
  66. unsigned int num_rstcs;
  67. struct reset_control *rstc[] __counted_by(num_rstcs);
  68. };
  69. /**
  70. * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices
  71. * @of_args: phandle to the reset controller with all the args like GPIO number
  72. * @swnode: Software node containing the reference to the GPIO provider
  73. * @list: list entry for the reset_gpio_lookup_list
  74. */
  75. struct reset_gpio_lookup {
  76. struct of_phandle_args of_args;
  77. struct fwnode_handle *swnode;
  78. struct list_head list;
  79. };
  80. static const char *rcdev_name(struct reset_controller_dev *rcdev)
  81. {
  82. if (rcdev->dev)
  83. return dev_name(rcdev->dev);
  84. if (rcdev->of_node)
  85. return rcdev->of_node->full_name;
  86. if (rcdev->of_args)
  87. return rcdev->of_args->np->full_name;
  88. return NULL;
  89. }
  90. /**
  91. * of_reset_simple_xlate - translate reset_spec to the reset line number
  92. * @rcdev: a pointer to the reset controller device
  93. * @reset_spec: reset line specifier as found in the device tree
  94. *
  95. * This static translation function is used by default if of_xlate in
  96. * :c:type:`reset_controller_dev` is not set. It is useful for all reset
  97. * controllers with 1:1 mapping, where reset lines can be indexed by number
  98. * without gaps.
  99. */
  100. static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  101. const struct of_phandle_args *reset_spec)
  102. {
  103. if (reset_spec->args[0] >= rcdev->nr_resets)
  104. return -EINVAL;
  105. return reset_spec->args[0];
  106. }
  107. /**
  108. * reset_controller_register - register a reset controller device
  109. * @rcdev: a pointer to the initialized reset controller device
  110. */
  111. int reset_controller_register(struct reset_controller_dev *rcdev)
  112. {
  113. if (rcdev->of_node && rcdev->of_args)
  114. return -EINVAL;
  115. if (!rcdev->of_xlate) {
  116. rcdev->of_reset_n_cells = 1;
  117. rcdev->of_xlate = of_reset_simple_xlate;
  118. }
  119. INIT_LIST_HEAD(&rcdev->reset_control_head);
  120. mutex_lock(&reset_list_mutex);
  121. list_add(&rcdev->list, &reset_controller_list);
  122. mutex_unlock(&reset_list_mutex);
  123. return 0;
  124. }
  125. EXPORT_SYMBOL_GPL(reset_controller_register);
  126. /**
  127. * reset_controller_unregister - unregister a reset controller device
  128. * @rcdev: a pointer to the reset controller device
  129. */
  130. void reset_controller_unregister(struct reset_controller_dev *rcdev)
  131. {
  132. mutex_lock(&reset_list_mutex);
  133. list_del(&rcdev->list);
  134. mutex_unlock(&reset_list_mutex);
  135. }
  136. EXPORT_SYMBOL_GPL(reset_controller_unregister);
  137. static void devm_reset_controller_release(struct device *dev, void *res)
  138. {
  139. reset_controller_unregister(*(struct reset_controller_dev **)res);
  140. }
  141. /**
  142. * devm_reset_controller_register - resource managed reset_controller_register()
  143. * @dev: device that is registering this reset controller
  144. * @rcdev: a pointer to the initialized reset controller device
  145. *
  146. * Managed reset_controller_register(). For reset controllers registered by
  147. * this function, reset_controller_unregister() is automatically called on
  148. * driver detach. See reset_controller_register() for more information.
  149. */
  150. int devm_reset_controller_register(struct device *dev,
  151. struct reset_controller_dev *rcdev)
  152. {
  153. struct reset_controller_dev **rcdevp;
  154. int ret;
  155. rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
  156. GFP_KERNEL);
  157. if (!rcdevp)
  158. return -ENOMEM;
  159. ret = reset_controller_register(rcdev);
  160. if (ret) {
  161. devres_free(rcdevp);
  162. return ret;
  163. }
  164. *rcdevp = rcdev;
  165. devres_add(dev, rcdevp);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL_GPL(devm_reset_controller_register);
  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, enum reset_control_flags flags)
  635. {
  636. bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED;
  637. bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED;
  638. struct reset_control *rstc;
  639. lockdep_assert_held(&reset_list_mutex);
  640. /* Expect callers to filter out OPTIONAL and DEASSERTED bits */
  641. if (WARN_ON(flags & ~(RESET_CONTROL_FLAGS_BIT_SHARED |
  642. RESET_CONTROL_FLAGS_BIT_ACQUIRED)))
  643. return ERR_PTR(-EINVAL);
  644. list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
  645. if (rstc->id == index) {
  646. /*
  647. * Allow creating a secondary exclusive reset_control
  648. * that is initially not acquired for an already
  649. * controlled reset line.
  650. */
  651. if (!rstc->shared && !shared && !acquired)
  652. break;
  653. if (WARN_ON(!rstc->shared || !shared))
  654. return ERR_PTR(-EBUSY);
  655. kref_get(&rstc->refcnt);
  656. return rstc;
  657. }
  658. }
  659. rstc = kzalloc_obj(*rstc);
  660. if (!rstc)
  661. return ERR_PTR(-ENOMEM);
  662. if (!try_module_get(rcdev->owner)) {
  663. kfree(rstc);
  664. return ERR_PTR(-ENODEV);
  665. }
  666. rstc->rcdev = rcdev;
  667. list_add(&rstc->list, &rcdev->reset_control_head);
  668. rstc->id = index;
  669. kref_init(&rstc->refcnt);
  670. rstc->acquired = acquired;
  671. rstc->shared = shared;
  672. get_device(rcdev->dev);
  673. return rstc;
  674. }
  675. static void __reset_control_release(struct kref *kref)
  676. {
  677. struct reset_control *rstc = container_of(kref, struct reset_control,
  678. refcnt);
  679. lockdep_assert_held(&reset_list_mutex);
  680. module_put(rstc->rcdev->owner);
  681. list_del(&rstc->list);
  682. put_device(rstc->rcdev->dev);
  683. kfree(rstc);
  684. }
  685. static void __reset_control_put_internal(struct reset_control *rstc)
  686. {
  687. lockdep_assert_held(&reset_list_mutex);
  688. if (IS_ERR_OR_NULL(rstc))
  689. return;
  690. kref_put(&rstc->refcnt, __reset_control_release);
  691. }
  692. static void reset_gpio_aux_device_release(struct device *dev)
  693. {
  694. struct auxiliary_device *adev = to_auxiliary_dev(dev);
  695. kfree(adev);
  696. }
  697. static int reset_add_gpio_aux_device(struct device *parent,
  698. struct fwnode_handle *swnode,
  699. int id, void *pdata)
  700. {
  701. struct auxiliary_device *adev;
  702. int ret;
  703. adev = kzalloc_obj(*adev);
  704. if (!adev)
  705. return -ENOMEM;
  706. adev->id = id;
  707. adev->name = "gpio";
  708. adev->dev.parent = parent;
  709. adev->dev.platform_data = pdata;
  710. adev->dev.release = reset_gpio_aux_device_release;
  711. device_set_node(&adev->dev, swnode);
  712. ret = auxiliary_device_init(adev);
  713. if (ret) {
  714. kfree(adev);
  715. return ret;
  716. }
  717. ret = __auxiliary_device_add(adev, "reset");
  718. if (ret) {
  719. auxiliary_device_uninit(adev);
  720. return ret;
  721. }
  722. return ret;
  723. }
  724. /*
  725. * @args: phandle to the GPIO provider with all the args like GPIO number
  726. */
  727. static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
  728. {
  729. struct property_entry properties[3] = { };
  730. unsigned int offset, of_flags, lflags;
  731. struct reset_gpio_lookup *rgpio_dev;
  732. struct device *parent;
  733. int id, ret, prop = 0;
  734. /*
  735. * Currently only #gpio-cells=2 is supported with the meaning of:
  736. * args[0]: GPIO number
  737. * args[1]: GPIO flags
  738. * TODO: Handle other cases.
  739. */
  740. if (args->args_count != 2)
  741. return -ENOENT;
  742. /*
  743. * Registering reset-gpio device might cause immediate
  744. * bind, resulting in its probe() registering new reset controller thus
  745. * taking reset_list_mutex lock via reset_controller_register().
  746. */
  747. lockdep_assert_not_held(&reset_list_mutex);
  748. offset = args->args[0];
  749. of_flags = args->args[1];
  750. /*
  751. * Later we map GPIO flags between OF and Linux, however not all
  752. * constants from include/dt-bindings/gpio/gpio.h and
  753. * include/linux/gpio/machine.h match each other.
  754. *
  755. * FIXME: Find a better way of translating OF flags to GPIO lookup
  756. * flags.
  757. */
  758. if (of_flags > GPIO_ACTIVE_LOW) {
  759. pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
  760. of_flags, offset);
  761. return -EINVAL;
  762. }
  763. struct gpio_device *gdev __free(gpio_device_put) =
  764. gpio_device_find_by_fwnode(of_fwnode_handle(args->np));
  765. if (!gdev)
  766. return -EPROBE_DEFER;
  767. guard(mutex)(&reset_gpio_lookup_mutex);
  768. list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
  769. if (args->np == rgpio_dev->of_args.np) {
  770. if (of_phandle_args_equal(args, &rgpio_dev->of_args))
  771. return 0; /* Already on the list, done */
  772. }
  773. }
  774. lflags = GPIO_PERSISTENT | (of_flags & GPIO_ACTIVE_LOW);
  775. parent = gpio_device_to_device(gdev);
  776. properties[prop++] = PROPERTY_ENTRY_STRING("compatible", "reset-gpio");
  777. properties[prop++] = PROPERTY_ENTRY_GPIO("reset-gpios", parent->fwnode, offset, lflags);
  778. id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
  779. if (id < 0)
  780. return id;
  781. /* Not freed on success, because it is persisent subsystem data. */
  782. rgpio_dev = kzalloc_obj(*rgpio_dev);
  783. if (!rgpio_dev) {
  784. ret = -ENOMEM;
  785. goto err_ida_free;
  786. }
  787. rgpio_dev->of_args = *args;
  788. /*
  789. * We keep the device_node reference, but of_args.np is put at the end
  790. * of __of_reset_control_get(), so get it one more time.
  791. * Hold reference as long as rgpio_dev memory is valid.
  792. */
  793. of_node_get(rgpio_dev->of_args.np);
  794. rgpio_dev->swnode = fwnode_create_software_node(properties, NULL);
  795. if (IS_ERR(rgpio_dev->swnode)) {
  796. ret = PTR_ERR(rgpio_dev->swnode);
  797. goto err_put_of_node;
  798. }
  799. ret = reset_add_gpio_aux_device(parent, rgpio_dev->swnode, id,
  800. &rgpio_dev->of_args);
  801. if (ret)
  802. goto err_del_swnode;
  803. list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
  804. return 0;
  805. err_del_swnode:
  806. fwnode_remove_software_node(rgpio_dev->swnode);
  807. err_put_of_node:
  808. of_node_put(rgpio_dev->of_args.np);
  809. kfree(rgpio_dev);
  810. err_ida_free:
  811. ida_free(&reset_gpio_ida, id);
  812. return ret;
  813. }
  814. static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args,
  815. bool gpio_fallback)
  816. {
  817. struct reset_controller_dev *rcdev;
  818. lockdep_assert_held(&reset_list_mutex);
  819. list_for_each_entry(rcdev, &reset_controller_list, list) {
  820. if (gpio_fallback) {
  821. if (rcdev->of_args && of_phandle_args_equal(args,
  822. rcdev->of_args))
  823. return rcdev;
  824. } else {
  825. if (args->np == rcdev->of_node)
  826. return rcdev;
  827. }
  828. }
  829. return NULL;
  830. }
  831. struct reset_control *
  832. __of_reset_control_get(struct device_node *node, const char *id, int index,
  833. enum reset_control_flags flags)
  834. {
  835. bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
  836. bool gpio_fallback = false;
  837. struct reset_control *rstc;
  838. struct reset_controller_dev *rcdev;
  839. struct of_phandle_args args;
  840. int rstc_id;
  841. int ret;
  842. if (!node)
  843. return ERR_PTR(-EINVAL);
  844. if (id) {
  845. index = of_property_match_string(node,
  846. "reset-names", id);
  847. if (index == -EILSEQ)
  848. return ERR_PTR(index);
  849. if (index < 0)
  850. return optional ? NULL : ERR_PTR(-ENOENT);
  851. }
  852. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  853. index, &args);
  854. if (ret == -EINVAL)
  855. return ERR_PTR(ret);
  856. if (ret) {
  857. if (!IS_ENABLED(CONFIG_RESET_GPIO))
  858. return optional ? NULL : ERR_PTR(ret);
  859. /*
  860. * There can be only one reset-gpio for regular devices, so
  861. * don't bother with the "reset-gpios" phandle index.
  862. */
  863. ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
  864. 0, &args);
  865. if (ret)
  866. return optional ? NULL : ERR_PTR(ret);
  867. gpio_fallback = true;
  868. ret = __reset_add_reset_gpio_device(&args);
  869. if (ret) {
  870. rstc = ERR_PTR(ret);
  871. goto out_put;
  872. }
  873. }
  874. mutex_lock(&reset_list_mutex);
  875. rcdev = __reset_find_rcdev(&args, gpio_fallback);
  876. if (!rcdev) {
  877. rstc = ERR_PTR(-EPROBE_DEFER);
  878. goto out_unlock;
  879. }
  880. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  881. rstc = ERR_PTR(-EINVAL);
  882. goto out_unlock;
  883. }
  884. rstc_id = rcdev->of_xlate(rcdev, &args);
  885. if (rstc_id < 0) {
  886. rstc = ERR_PTR(rstc_id);
  887. goto out_unlock;
  888. }
  889. flags &= ~RESET_CONTROL_FLAGS_BIT_OPTIONAL;
  890. /* reset_list_mutex also protects the rcdev's reset_control list */
  891. rstc = __reset_control_get_internal(rcdev, rstc_id, flags);
  892. out_unlock:
  893. mutex_unlock(&reset_list_mutex);
  894. out_put:
  895. of_node_put(args.np);
  896. return rstc;
  897. }
  898. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  899. struct reset_control *__reset_control_get(struct device *dev, const char *id,
  900. int index, enum reset_control_flags flags)
  901. {
  902. bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED;
  903. bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED;
  904. bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
  905. if (WARN_ON(shared && acquired))
  906. return ERR_PTR(-EINVAL);
  907. if (dev->of_node)
  908. return __of_reset_control_get(dev->of_node, id, index, flags);
  909. return optional ? NULL : ERR_PTR(-ENOENT);
  910. }
  911. EXPORT_SYMBOL_GPL(__reset_control_get);
  912. int __reset_control_bulk_get(struct device *dev, int num_rstcs,
  913. struct reset_control_bulk_data *rstcs,
  914. enum reset_control_flags flags)
  915. {
  916. int ret, i;
  917. for (i = 0; i < num_rstcs; i++) {
  918. rstcs[i].rstc = __reset_control_get(dev, rstcs[i].id, 0, flags);
  919. if (IS_ERR(rstcs[i].rstc)) {
  920. ret = PTR_ERR(rstcs[i].rstc);
  921. goto err;
  922. }
  923. }
  924. return 0;
  925. err:
  926. mutex_lock(&reset_list_mutex);
  927. while (i--)
  928. __reset_control_put_internal(rstcs[i].rstc);
  929. mutex_unlock(&reset_list_mutex);
  930. return ret;
  931. }
  932. EXPORT_SYMBOL_GPL(__reset_control_bulk_get);
  933. static void reset_control_array_put(struct reset_control_array *resets)
  934. {
  935. int i;
  936. mutex_lock(&reset_list_mutex);
  937. for (i = 0; i < resets->num_rstcs; i++)
  938. __reset_control_put_internal(resets->rstc[i]);
  939. mutex_unlock(&reset_list_mutex);
  940. kfree(resets);
  941. }
  942. /**
  943. * reset_control_put - free the reset controller
  944. * @rstc: reset controller
  945. */
  946. void reset_control_put(struct reset_control *rstc)
  947. {
  948. if (IS_ERR_OR_NULL(rstc))
  949. return;
  950. if (reset_control_is_array(rstc)) {
  951. reset_control_array_put(rstc_to_array(rstc));
  952. return;
  953. }
  954. mutex_lock(&reset_list_mutex);
  955. __reset_control_put_internal(rstc);
  956. mutex_unlock(&reset_list_mutex);
  957. }
  958. EXPORT_SYMBOL_GPL(reset_control_put);
  959. /**
  960. * reset_control_bulk_put - free the reset controllers
  961. * @num_rstcs: number of entries in rstcs array
  962. * @rstcs: array of struct reset_control_bulk_data with reset controls set
  963. */
  964. void reset_control_bulk_put(int num_rstcs, struct reset_control_bulk_data *rstcs)
  965. {
  966. mutex_lock(&reset_list_mutex);
  967. while (num_rstcs--)
  968. __reset_control_put_internal(rstcs[num_rstcs].rstc);
  969. mutex_unlock(&reset_list_mutex);
  970. }
  971. EXPORT_SYMBOL_GPL(reset_control_bulk_put);
  972. static void devm_reset_control_release(struct device *dev, void *res)
  973. {
  974. reset_control_put(*(struct reset_control **)res);
  975. }
  976. static void devm_reset_control_release_deasserted(struct device *dev, void *res)
  977. {
  978. struct reset_control *rstc = *(struct reset_control **)res;
  979. reset_control_assert(rstc);
  980. reset_control_put(rstc);
  981. }
  982. struct reset_control *
  983. __devm_reset_control_get(struct device *dev, const char *id, int index,
  984. enum reset_control_flags flags)
  985. {
  986. struct reset_control **ptr, *rstc;
  987. bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED;
  988. ptr = devres_alloc(deasserted ? devm_reset_control_release_deasserted :
  989. devm_reset_control_release, sizeof(*ptr),
  990. GFP_KERNEL);
  991. if (!ptr)
  992. return ERR_PTR(-ENOMEM);
  993. flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED;
  994. rstc = __reset_control_get(dev, id, index, flags);
  995. if (IS_ERR_OR_NULL(rstc)) {
  996. devres_free(ptr);
  997. return rstc;
  998. }
  999. if (deasserted) {
  1000. int ret;
  1001. ret = reset_control_deassert(rstc);
  1002. if (ret) {
  1003. reset_control_put(rstc);
  1004. devres_free(ptr);
  1005. return ERR_PTR(ret);
  1006. }
  1007. }
  1008. *ptr = rstc;
  1009. devres_add(dev, ptr);
  1010. return rstc;
  1011. }
  1012. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  1013. struct reset_control_bulk_devres {
  1014. int num_rstcs;
  1015. struct reset_control_bulk_data *rstcs;
  1016. };
  1017. static void devm_reset_control_bulk_release(struct device *dev, void *res)
  1018. {
  1019. struct reset_control_bulk_devres *devres = res;
  1020. reset_control_bulk_put(devres->num_rstcs, devres->rstcs);
  1021. }
  1022. static void devm_reset_control_bulk_release_deasserted(struct device *dev, void *res)
  1023. {
  1024. struct reset_control_bulk_devres *devres = res;
  1025. reset_control_bulk_assert(devres->num_rstcs, devres->rstcs);
  1026. reset_control_bulk_put(devres->num_rstcs, devres->rstcs);
  1027. }
  1028. int __devm_reset_control_bulk_get(struct device *dev, int num_rstcs,
  1029. struct reset_control_bulk_data *rstcs,
  1030. enum reset_control_flags flags)
  1031. {
  1032. struct reset_control_bulk_devres *ptr;
  1033. bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED;
  1034. int ret;
  1035. ptr = devres_alloc(deasserted ? devm_reset_control_bulk_release_deasserted :
  1036. devm_reset_control_bulk_release, sizeof(*ptr),
  1037. GFP_KERNEL);
  1038. if (!ptr)
  1039. return -ENOMEM;
  1040. flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED;
  1041. ret = __reset_control_bulk_get(dev, num_rstcs, rstcs, flags);
  1042. if (ret < 0) {
  1043. devres_free(ptr);
  1044. return ret;
  1045. }
  1046. if (deasserted) {
  1047. ret = reset_control_bulk_deassert(num_rstcs, rstcs);
  1048. if (ret) {
  1049. reset_control_bulk_put(num_rstcs, rstcs);
  1050. devres_free(ptr);
  1051. return ret;
  1052. }
  1053. }
  1054. ptr->num_rstcs = num_rstcs;
  1055. ptr->rstcs = rstcs;
  1056. devres_add(dev, ptr);
  1057. return 0;
  1058. }
  1059. EXPORT_SYMBOL_GPL(__devm_reset_control_bulk_get);
  1060. /**
  1061. * __device_reset - find reset controller associated with the device
  1062. * and perform reset
  1063. * @dev: device to be reset by the controller
  1064. * @optional: whether it is optional to reset the device
  1065. *
  1066. * Convenience wrapper for __reset_control_get() and reset_control_reset().
  1067. * This is useful for the common case of devices with single, dedicated reset
  1068. * lines. _RST firmware method will be called for devices with ACPI.
  1069. */
  1070. int __device_reset(struct device *dev, bool optional)
  1071. {
  1072. enum reset_control_flags flags;
  1073. struct reset_control *rstc;
  1074. int ret;
  1075. #ifdef CONFIG_ACPI
  1076. acpi_handle handle = ACPI_HANDLE(dev);
  1077. if (handle) {
  1078. if (!acpi_has_method(handle, "_RST"))
  1079. return optional ? 0 : -ENOENT;
  1080. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST", NULL,
  1081. NULL)))
  1082. return -EIO;
  1083. }
  1084. #endif
  1085. flags = optional ? RESET_CONTROL_OPTIONAL_EXCLUSIVE : RESET_CONTROL_EXCLUSIVE;
  1086. rstc = __reset_control_get(dev, NULL, 0, flags);
  1087. if (IS_ERR(rstc))
  1088. return PTR_ERR(rstc);
  1089. ret = reset_control_reset(rstc);
  1090. reset_control_put(rstc);
  1091. return ret;
  1092. }
  1093. EXPORT_SYMBOL_GPL(__device_reset);
  1094. /*
  1095. * APIs to manage an array of reset controls.
  1096. */
  1097. /**
  1098. * of_reset_control_get_count - Count number of resets available with a device
  1099. *
  1100. * @node: device node that contains 'resets'.
  1101. *
  1102. * Returns positive reset count on success, or error number on failure and
  1103. * on count being zero.
  1104. */
  1105. static int of_reset_control_get_count(struct device_node *node)
  1106. {
  1107. int count;
  1108. if (!node)
  1109. return -EINVAL;
  1110. count = of_count_phandle_with_args(node, "resets", "#reset-cells");
  1111. if (count == 0)
  1112. count = -ENOENT;
  1113. return count;
  1114. }
  1115. /**
  1116. * of_reset_control_array_get - Get a list of reset controls using
  1117. * device node.
  1118. *
  1119. * @np: device node for the device that requests the reset controls array
  1120. * @flags: whether reset controls are shared, optional, acquired
  1121. *
  1122. * Returns pointer to allocated reset_control on success or error on failure
  1123. */
  1124. struct reset_control *
  1125. of_reset_control_array_get(struct device_node *np, enum reset_control_flags flags)
  1126. {
  1127. bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
  1128. struct reset_control_array *resets;
  1129. struct reset_control *rstc;
  1130. int num, i;
  1131. num = of_reset_control_get_count(np);
  1132. if (num < 0)
  1133. return optional ? NULL : ERR_PTR(num);
  1134. resets = kzalloc_flex(*resets, rstc, num);
  1135. if (!resets)
  1136. return ERR_PTR(-ENOMEM);
  1137. resets->num_rstcs = num;
  1138. for (i = 0; i < num; i++) {
  1139. rstc = __of_reset_control_get(np, NULL, i, flags);
  1140. if (IS_ERR(rstc))
  1141. goto err_rst;
  1142. resets->rstc[i] = rstc;
  1143. }
  1144. resets->base.array = true;
  1145. return &resets->base;
  1146. err_rst:
  1147. mutex_lock(&reset_list_mutex);
  1148. while (--i >= 0)
  1149. __reset_control_put_internal(resets->rstc[i]);
  1150. mutex_unlock(&reset_list_mutex);
  1151. kfree(resets);
  1152. return rstc;
  1153. }
  1154. EXPORT_SYMBOL_GPL(of_reset_control_array_get);
  1155. /**
  1156. * devm_reset_control_array_get - Resource managed reset control array get
  1157. *
  1158. * @dev: device that requests the list of reset controls
  1159. * @flags: whether reset controls are shared, optional, acquired
  1160. *
  1161. * The reset control array APIs are intended for a list of resets
  1162. * that just have to be asserted or deasserted, without any
  1163. * requirements on the order.
  1164. *
  1165. * Returns pointer to allocated reset_control on success or error on failure
  1166. */
  1167. struct reset_control *
  1168. devm_reset_control_array_get(struct device *dev, enum reset_control_flags flags)
  1169. {
  1170. struct reset_control **ptr, *rstc;
  1171. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  1172. GFP_KERNEL);
  1173. if (!ptr)
  1174. return ERR_PTR(-ENOMEM);
  1175. rstc = of_reset_control_array_get(dev->of_node, flags);
  1176. if (IS_ERR_OR_NULL(rstc)) {
  1177. devres_free(ptr);
  1178. return rstc;
  1179. }
  1180. *ptr = rstc;
  1181. devres_add(dev, ptr);
  1182. return rstc;
  1183. }
  1184. EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
  1185. /**
  1186. * reset_control_get_count - Count number of resets available with a device
  1187. *
  1188. * @dev: device for which to return the number of resets
  1189. *
  1190. * Returns positive reset count on success, or error number on failure and
  1191. * on count being zero.
  1192. */
  1193. int reset_control_get_count(struct device *dev)
  1194. {
  1195. if (dev->of_node)
  1196. return of_reset_control_get_count(dev->of_node);
  1197. return -ENOENT;
  1198. }
  1199. EXPORT_SYMBOL_GPL(reset_control_get_count);