reset.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_RESET_H_
  3. #define _LINUX_RESET_H_
  4. #include <linux/err.h>
  5. #include <linux/errno.h>
  6. #include <linux/types.h>
  7. struct device;
  8. struct device_node;
  9. struct reset_control;
  10. #ifdef CONFIG_RESET_CONTROLLER
  11. int reset_control_reset(struct reset_control *rstc);
  12. int reset_control_assert(struct reset_control *rstc);
  13. int reset_control_deassert(struct reset_control *rstc);
  14. int reset_control_status(struct reset_control *rstc);
  15. int reset_control_acquire(struct reset_control *rstc);
  16. void reset_control_release(struct reset_control *rstc);
  17. struct reset_control *__of_reset_control_get(struct device_node *node,
  18. const char *id, int index, bool shared,
  19. bool optional, bool acquired);
  20. struct reset_control *__reset_control_get(struct device *dev, const char *id,
  21. int index, bool shared,
  22. bool optional, bool acquired);
  23. void reset_control_put(struct reset_control *rstc);
  24. int __device_reset(struct device *dev, bool optional);
  25. struct reset_control *__devm_reset_control_get(struct device *dev,
  26. const char *id, int index, bool shared,
  27. bool optional, bool acquired);
  28. struct reset_control *devm_reset_control_array_get(struct device *dev,
  29. bool shared, bool optional);
  30. struct reset_control *of_reset_control_array_get(struct device_node *np,
  31. bool shared, bool optional,
  32. bool acquired);
  33. int reset_control_get_count(struct device *dev);
  34. #else
  35. static inline int reset_control_reset(struct reset_control *rstc)
  36. {
  37. return 0;
  38. }
  39. static inline int reset_control_assert(struct reset_control *rstc)
  40. {
  41. return 0;
  42. }
  43. static inline int reset_control_deassert(struct reset_control *rstc)
  44. {
  45. return 0;
  46. }
  47. static inline int reset_control_status(struct reset_control *rstc)
  48. {
  49. return 0;
  50. }
  51. static inline int reset_control_acquire(struct reset_control *rstc)
  52. {
  53. return 0;
  54. }
  55. static inline void reset_control_release(struct reset_control *rstc)
  56. {
  57. }
  58. static inline void reset_control_put(struct reset_control *rstc)
  59. {
  60. }
  61. static inline int __device_reset(struct device *dev, bool optional)
  62. {
  63. return optional ? 0 : -ENOTSUPP;
  64. }
  65. static inline struct reset_control *__of_reset_control_get(
  66. struct device_node *node,
  67. const char *id, int index, bool shared,
  68. bool optional, bool acquired)
  69. {
  70. return optional ? NULL : ERR_PTR(-ENOTSUPP);
  71. }
  72. static inline struct reset_control *__reset_control_get(
  73. struct device *dev, const char *id,
  74. int index, bool shared, bool optional,
  75. bool acquired)
  76. {
  77. return optional ? NULL : ERR_PTR(-ENOTSUPP);
  78. }
  79. static inline struct reset_control *__devm_reset_control_get(
  80. struct device *dev, const char *id,
  81. int index, bool shared, bool optional,
  82. bool acquired)
  83. {
  84. return optional ? NULL : ERR_PTR(-ENOTSUPP);
  85. }
  86. static inline struct reset_control *
  87. devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
  88. {
  89. return optional ? NULL : ERR_PTR(-ENOTSUPP);
  90. }
  91. static inline struct reset_control *
  92. of_reset_control_array_get(struct device_node *np, bool shared, bool optional,
  93. bool acquired)
  94. {
  95. return optional ? NULL : ERR_PTR(-ENOTSUPP);
  96. }
  97. static inline int reset_control_get_count(struct device *dev)
  98. {
  99. return -ENOENT;
  100. }
  101. #endif /* CONFIG_RESET_CONTROLLER */
  102. static inline int __must_check device_reset(struct device *dev)
  103. {
  104. return __device_reset(dev, false);
  105. }
  106. static inline int device_reset_optional(struct device *dev)
  107. {
  108. return __device_reset(dev, true);
  109. }
  110. /**
  111. * reset_control_get_exclusive - Lookup and obtain an exclusive reference
  112. * to a reset controller.
  113. * @dev: device to be reset by the controller
  114. * @id: reset line name
  115. *
  116. * Returns a struct reset_control or IS_ERR() condition containing errno.
  117. * If this function is called more than once for the same reset_control it will
  118. * return -EBUSY.
  119. *
  120. * See reset_control_get_shared() for details on shared references to
  121. * reset-controls.
  122. *
  123. * Use of id names is optional.
  124. */
  125. static inline struct reset_control *
  126. __must_check reset_control_get_exclusive(struct device *dev, const char *id)
  127. {
  128. return __reset_control_get(dev, id, 0, false, false, true);
  129. }
  130. /**
  131. * reset_control_get_exclusive_released - Lookup and obtain a temoprarily
  132. * exclusive reference to a reset
  133. * controller.
  134. * @dev: device to be reset by the controller
  135. * @id: reset line name
  136. *
  137. * Returns a struct reset_control or IS_ERR() condition containing errno.
  138. * reset-controls returned by this function must be acquired via
  139. * reset_control_acquire() before they can be used and should be released
  140. * via reset_control_release() afterwards.
  141. *
  142. * Use of id names is optional.
  143. */
  144. static inline struct reset_control *
  145. __must_check reset_control_get_exclusive_released(struct device *dev,
  146. const char *id)
  147. {
  148. return __reset_control_get(dev, id, 0, false, false, false);
  149. }
  150. /**
  151. * reset_control_get_shared - Lookup and obtain a shared reference to a
  152. * reset controller.
  153. * @dev: device to be reset by the controller
  154. * @id: reset line name
  155. *
  156. * Returns a struct reset_control or IS_ERR() condition containing errno.
  157. * This function is intended for use with reset-controls which are shared
  158. * between hardware blocks.
  159. *
  160. * When a reset-control is shared, the behavior of reset_control_assert /
  161. * deassert is changed, the reset-core will keep track of a deassert_count
  162. * and only (re-)assert the reset after reset_control_assert has been called
  163. * as many times as reset_control_deassert was called. Also see the remark
  164. * about shared reset-controls in the reset_control_assert docs.
  165. *
  166. * Calling reset_control_assert without first calling reset_control_deassert
  167. * is not allowed on a shared reset control. Calling reset_control_reset is
  168. * also not allowed on a shared reset control.
  169. *
  170. * Use of id names is optional.
  171. */
  172. static inline struct reset_control *reset_control_get_shared(
  173. struct device *dev, const char *id)
  174. {
  175. return __reset_control_get(dev, id, 0, true, false, false);
  176. }
  177. /**
  178. * reset_control_get_optional_exclusive - optional reset_control_get_exclusive()
  179. * @dev: device to be reset by the controller
  180. * @id: reset line name
  181. *
  182. * Optional variant of reset_control_get_exclusive(). If the requested reset
  183. * is not specified in the device tree, this function returns NULL instead of
  184. * an error.
  185. *
  186. * See reset_control_get_exclusive() for more information.
  187. */
  188. static inline struct reset_control *reset_control_get_optional_exclusive(
  189. struct device *dev, const char *id)
  190. {
  191. return __reset_control_get(dev, id, 0, false, true, true);
  192. }
  193. /**
  194. * reset_control_get_optional_shared - optional reset_control_get_shared()
  195. * @dev: device to be reset by the controller
  196. * @id: reset line name
  197. *
  198. * Optional variant of reset_control_get_shared(). If the requested reset
  199. * is not specified in the device tree, this function returns NULL instead of
  200. * an error.
  201. *
  202. * See reset_control_get_shared() for more information.
  203. */
  204. static inline struct reset_control *reset_control_get_optional_shared(
  205. struct device *dev, const char *id)
  206. {
  207. return __reset_control_get(dev, id, 0, true, true, false);
  208. }
  209. /**
  210. * of_reset_control_get_exclusive - Lookup and obtain an exclusive reference
  211. * to a reset controller.
  212. * @node: device to be reset by the controller
  213. * @id: reset line name
  214. *
  215. * Returns a struct reset_control or IS_ERR() condition containing errno.
  216. *
  217. * Use of id names is optional.
  218. */
  219. static inline struct reset_control *of_reset_control_get_exclusive(
  220. struct device_node *node, const char *id)
  221. {
  222. return __of_reset_control_get(node, id, 0, false, false, true);
  223. }
  224. /**
  225. * of_reset_control_get_shared - Lookup and obtain a shared reference
  226. * to a reset controller.
  227. * @node: device to be reset by the controller
  228. * @id: reset line name
  229. *
  230. * When a reset-control is shared, the behavior of reset_control_assert /
  231. * deassert is changed, the reset-core will keep track of a deassert_count
  232. * and only (re-)assert the reset after reset_control_assert has been called
  233. * as many times as reset_control_deassert was called. Also see the remark
  234. * about shared reset-controls in the reset_control_assert docs.
  235. *
  236. * Calling reset_control_assert without first calling reset_control_deassert
  237. * is not allowed on a shared reset control. Calling reset_control_reset is
  238. * also not allowed on a shared reset control.
  239. * Returns a struct reset_control or IS_ERR() condition containing errno.
  240. *
  241. * Use of id names is optional.
  242. */
  243. static inline struct reset_control *of_reset_control_get_shared(
  244. struct device_node *node, const char *id)
  245. {
  246. return __of_reset_control_get(node, id, 0, true, false, false);
  247. }
  248. /**
  249. * of_reset_control_get_exclusive_by_index - Lookup and obtain an exclusive
  250. * reference to a reset controller
  251. * by index.
  252. * @node: device to be reset by the controller
  253. * @index: index of the reset controller
  254. *
  255. * This is to be used to perform a list of resets for a device or power domain
  256. * in whatever order. Returns a struct reset_control or IS_ERR() condition
  257. * containing errno.
  258. */
  259. static inline struct reset_control *of_reset_control_get_exclusive_by_index(
  260. struct device_node *node, int index)
  261. {
  262. return __of_reset_control_get(node, NULL, index, false, false, true);
  263. }
  264. /**
  265. * of_reset_control_get_shared_by_index - Lookup and obtain a shared
  266. * reference to a reset controller
  267. * by index.
  268. * @node: device to be reset by the controller
  269. * @index: index of the reset controller
  270. *
  271. * When a reset-control is shared, the behavior of reset_control_assert /
  272. * deassert is changed, the reset-core will keep track of a deassert_count
  273. * and only (re-)assert the reset after reset_control_assert has been called
  274. * as many times as reset_control_deassert was called. Also see the remark
  275. * about shared reset-controls in the reset_control_assert docs.
  276. *
  277. * Calling reset_control_assert without first calling reset_control_deassert
  278. * is not allowed on a shared reset control. Calling reset_control_reset is
  279. * also not allowed on a shared reset control.
  280. * Returns a struct reset_control or IS_ERR() condition containing errno.
  281. *
  282. * This is to be used to perform a list of resets for a device or power domain
  283. * in whatever order. Returns a struct reset_control or IS_ERR() condition
  284. * containing errno.
  285. */
  286. static inline struct reset_control *of_reset_control_get_shared_by_index(
  287. struct device_node *node, int index)
  288. {
  289. return __of_reset_control_get(node, NULL, index, true, false, false);
  290. }
  291. /**
  292. * devm_reset_control_get_exclusive - resource managed
  293. * reset_control_get_exclusive()
  294. * @dev: device to be reset by the controller
  295. * @id: reset line name
  296. *
  297. * Managed reset_control_get_exclusive(). For reset controllers returned
  298. * from this function, reset_control_put() is called automatically on driver
  299. * detach.
  300. *
  301. * See reset_control_get_exclusive() for more information.
  302. */
  303. static inline struct reset_control *
  304. __must_check devm_reset_control_get_exclusive(struct device *dev,
  305. const char *id)
  306. {
  307. return __devm_reset_control_get(dev, id, 0, false, false, true);
  308. }
  309. /**
  310. * devm_reset_control_get_exclusive_released - resource managed
  311. * reset_control_get_exclusive_released()
  312. * @dev: device to be reset by the controller
  313. * @id: reset line name
  314. *
  315. * Managed reset_control_get_exclusive_released(). For reset controllers
  316. * returned from this function, reset_control_put() is called automatically on
  317. * driver detach.
  318. *
  319. * See reset_control_get_exclusive_released() for more information.
  320. */
  321. static inline struct reset_control *
  322. __must_check devm_reset_control_get_exclusive_released(struct device *dev,
  323. const char *id)
  324. {
  325. return __devm_reset_control_get(dev, id, 0, false, false, false);
  326. }
  327. /**
  328. * devm_reset_control_get_shared - resource managed reset_control_get_shared()
  329. * @dev: device to be reset by the controller
  330. * @id: reset line name
  331. *
  332. * Managed reset_control_get_shared(). For reset controllers returned from
  333. * this function, reset_control_put() is called automatically on driver detach.
  334. * See reset_control_get_shared() for more information.
  335. */
  336. static inline struct reset_control *devm_reset_control_get_shared(
  337. struct device *dev, const char *id)
  338. {
  339. return __devm_reset_control_get(dev, id, 0, true, false, false);
  340. }
  341. /**
  342. * devm_reset_control_get_optional_exclusive - resource managed
  343. * reset_control_get_optional_exclusive()
  344. * @dev: device to be reset by the controller
  345. * @id: reset line name
  346. *
  347. * Managed reset_control_get_optional_exclusive(). For reset controllers
  348. * returned from this function, reset_control_put() is called automatically on
  349. * driver detach.
  350. *
  351. * See reset_control_get_optional_exclusive() for more information.
  352. */
  353. static inline struct reset_control *devm_reset_control_get_optional_exclusive(
  354. struct device *dev, const char *id)
  355. {
  356. return __devm_reset_control_get(dev, id, 0, false, true, true);
  357. }
  358. /**
  359. * devm_reset_control_get_optional_shared - resource managed
  360. * reset_control_get_optional_shared()
  361. * @dev: device to be reset by the controller
  362. * @id: reset line name
  363. *
  364. * Managed reset_control_get_optional_shared(). For reset controllers returned
  365. * from this function, reset_control_put() is called automatically on driver
  366. * detach.
  367. *
  368. * See reset_control_get_optional_shared() for more information.
  369. */
  370. static inline struct reset_control *devm_reset_control_get_optional_shared(
  371. struct device *dev, const char *id)
  372. {
  373. return __devm_reset_control_get(dev, id, 0, true, true, false);
  374. }
  375. /**
  376. * devm_reset_control_get_exclusive_by_index - resource managed
  377. * reset_control_get_exclusive()
  378. * @dev: device to be reset by the controller
  379. * @index: index of the reset controller
  380. *
  381. * Managed reset_control_get_exclusive(). For reset controllers returned from
  382. * this function, reset_control_put() is called automatically on driver
  383. * detach.
  384. *
  385. * See reset_control_get_exclusive() for more information.
  386. */
  387. static inline struct reset_control *
  388. devm_reset_control_get_exclusive_by_index(struct device *dev, int index)
  389. {
  390. return __devm_reset_control_get(dev, NULL, index, false, false, true);
  391. }
  392. /**
  393. * devm_reset_control_get_shared_by_index - resource managed
  394. * reset_control_get_shared
  395. * @dev: device to be reset by the controller
  396. * @index: index of the reset controller
  397. *
  398. * Managed reset_control_get_shared(). For reset controllers returned from
  399. * this function, reset_control_put() is called automatically on driver detach.
  400. * See reset_control_get_shared() for more information.
  401. */
  402. static inline struct reset_control *
  403. devm_reset_control_get_shared_by_index(struct device *dev, int index)
  404. {
  405. return __devm_reset_control_get(dev, NULL, index, true, false, false);
  406. }
  407. /*
  408. * TEMPORARY calls to use during transition:
  409. *
  410. * of_reset_control_get() => of_reset_control_get_exclusive()
  411. *
  412. * These inline function calls will be removed once all consumers
  413. * have been moved over to the new explicit API.
  414. */
  415. static inline struct reset_control *of_reset_control_get(
  416. struct device_node *node, const char *id)
  417. {
  418. return of_reset_control_get_exclusive(node, id);
  419. }
  420. static inline struct reset_control *of_reset_control_get_by_index(
  421. struct device_node *node, int index)
  422. {
  423. return of_reset_control_get_exclusive_by_index(node, index);
  424. }
  425. static inline struct reset_control *devm_reset_control_get(
  426. struct device *dev, const char *id)
  427. {
  428. return devm_reset_control_get_exclusive(dev, id);
  429. }
  430. static inline struct reset_control *devm_reset_control_get_optional(
  431. struct device *dev, const char *id)
  432. {
  433. return devm_reset_control_get_optional_exclusive(dev, id);
  434. }
  435. static inline struct reset_control *devm_reset_control_get_by_index(
  436. struct device *dev, int index)
  437. {
  438. return devm_reset_control_get_exclusive_by_index(dev, index);
  439. }
  440. /*
  441. * APIs to manage a list of reset controllers
  442. */
  443. static inline struct reset_control *
  444. devm_reset_control_array_get_exclusive(struct device *dev)
  445. {
  446. return devm_reset_control_array_get(dev, false, false);
  447. }
  448. static inline struct reset_control *
  449. devm_reset_control_array_get_shared(struct device *dev)
  450. {
  451. return devm_reset_control_array_get(dev, true, false);
  452. }
  453. static inline struct reset_control *
  454. devm_reset_control_array_get_optional_exclusive(struct device *dev)
  455. {
  456. return devm_reset_control_array_get(dev, false, true);
  457. }
  458. static inline struct reset_control *
  459. devm_reset_control_array_get_optional_shared(struct device *dev)
  460. {
  461. return devm_reset_control_array_get(dev, true, true);
  462. }
  463. static inline struct reset_control *
  464. of_reset_control_array_get_exclusive(struct device_node *node)
  465. {
  466. return of_reset_control_array_get(node, false, false, true);
  467. }
  468. static inline struct reset_control *
  469. of_reset_control_array_get_exclusive_released(struct device_node *node)
  470. {
  471. return of_reset_control_array_get(node, false, false, false);
  472. }
  473. static inline struct reset_control *
  474. of_reset_control_array_get_shared(struct device_node *node)
  475. {
  476. return of_reset_control_array_get(node, true, false, true);
  477. }
  478. static inline struct reset_control *
  479. of_reset_control_array_get_optional_exclusive(struct device_node *node)
  480. {
  481. return of_reset_control_array_get(node, false, true, true);
  482. }
  483. static inline struct reset_control *
  484. of_reset_control_array_get_optional_shared(struct device_node *node)
  485. {
  486. return of_reset_control_array_get(node, true, true, true);
  487. }
  488. #endif