core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Reset Controller framework
  3. *
  4. * Copyright 2013 Philipp Zabel, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/reset.h>
  19. #include <linux/reset-controller.h>
  20. #include <linux/slab.h>
  21. static DEFINE_MUTEX(reset_list_mutex);
  22. static LIST_HEAD(reset_controller_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. * @shared: Is this a shared (1), or an exclusive (0) reset_control?
  32. * @deassert_cnt: Number of times this reset line has been deasserted
  33. */
  34. struct reset_control {
  35. struct reset_controller_dev *rcdev;
  36. struct list_head list;
  37. unsigned int id;
  38. unsigned int refcnt;
  39. int shared;
  40. atomic_t deassert_count;
  41. };
  42. /**
  43. * of_reset_simple_xlate - translate reset_spec to the reset line number
  44. * @rcdev: a pointer to the reset controller device
  45. * @reset_spec: reset line specifier as found in the device tree
  46. * @flags: a flags pointer to fill in (optional)
  47. *
  48. * This simple translation function should be used for reset controllers
  49. * with 1:1 mapping, where reset lines can be indexed by number without gaps.
  50. */
  51. static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  52. const struct of_phandle_args *reset_spec)
  53. {
  54. if (reset_spec->args[0] >= rcdev->nr_resets)
  55. return -EINVAL;
  56. return reset_spec->args[0];
  57. }
  58. /**
  59. * reset_controller_register - register a reset controller device
  60. * @rcdev: a pointer to the initialized reset controller device
  61. */
  62. int reset_controller_register(struct reset_controller_dev *rcdev)
  63. {
  64. if (!rcdev->of_xlate) {
  65. rcdev->of_reset_n_cells = 1;
  66. rcdev->of_xlate = of_reset_simple_xlate;
  67. }
  68. INIT_LIST_HEAD(&rcdev->reset_control_head);
  69. mutex_lock(&reset_list_mutex);
  70. list_add(&rcdev->list, &reset_controller_list);
  71. mutex_unlock(&reset_list_mutex);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(reset_controller_register);
  75. /**
  76. * reset_controller_unregister - unregister a reset controller device
  77. * @rcdev: a pointer to the reset controller device
  78. */
  79. void reset_controller_unregister(struct reset_controller_dev *rcdev)
  80. {
  81. mutex_lock(&reset_list_mutex);
  82. list_del(&rcdev->list);
  83. mutex_unlock(&reset_list_mutex);
  84. }
  85. EXPORT_SYMBOL_GPL(reset_controller_unregister);
  86. static void devm_reset_controller_release(struct device *dev, void *res)
  87. {
  88. reset_controller_unregister(*(struct reset_controller_dev **)res);
  89. }
  90. /**
  91. * devm_reset_controller_register - resource managed reset_controller_register()
  92. * @dev: device that is registering this reset controller
  93. * @rcdev: a pointer to the initialized reset controller device
  94. *
  95. * Managed reset_controller_register(). For reset controllers registered by
  96. * this function, reset_controller_unregister() is automatically called on
  97. * driver detach. See reset_controller_register() for more information.
  98. */
  99. int devm_reset_controller_register(struct device *dev,
  100. struct reset_controller_dev *rcdev)
  101. {
  102. struct reset_controller_dev **rcdevp;
  103. int ret;
  104. rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
  105. GFP_KERNEL);
  106. if (!rcdevp)
  107. return -ENOMEM;
  108. ret = reset_controller_register(rcdev);
  109. if (!ret) {
  110. *rcdevp = rcdev;
  111. devres_add(dev, rcdevp);
  112. } else {
  113. devres_free(rcdevp);
  114. }
  115. return ret;
  116. }
  117. EXPORT_SYMBOL_GPL(devm_reset_controller_register);
  118. /**
  119. * reset_control_reset - reset the controlled device
  120. * @rstc: reset controller
  121. *
  122. * Calling this on a shared reset controller is an error.
  123. *
  124. * If rstc is NULL it is an optional reset and the function will just
  125. * return 0.
  126. */
  127. int reset_control_reset(struct reset_control *rstc)
  128. {
  129. if (!rstc)
  130. return 0;
  131. if (WARN_ON(IS_ERR(rstc)))
  132. return -EINVAL;
  133. if (rstc->rcdev->ops->reset)
  134. return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  135. return -ENOTSUPP;
  136. }
  137. EXPORT_SYMBOL_GPL(reset_control_reset);
  138. /**
  139. * reset_control_assert - asserts the reset line
  140. * @rstc: reset controller
  141. *
  142. * Calling this on an exclusive reset controller guarantees that the reset
  143. * will be asserted. When called on a shared reset controller the line may
  144. * still be deasserted, as long as other users keep it so.
  145. *
  146. * For shared reset controls a driver cannot expect the hw's registers and
  147. * internal state to be reset, but must be prepared for this to happen.
  148. *
  149. * If rstc is NULL it is an optional reset and the function will just
  150. * return 0.
  151. */
  152. int reset_control_assert(struct reset_control *rstc)
  153. {
  154. if (!rstc)
  155. return 0;
  156. if (WARN_ON(IS_ERR(rstc)))
  157. return -EINVAL;
  158. if (!rstc->rcdev->ops->assert)
  159. return -ENOTSUPP;
  160. if (rstc->shared) {
  161. if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
  162. return -EINVAL;
  163. if (atomic_dec_return(&rstc->deassert_count) != 0)
  164. return 0;
  165. }
  166. return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
  167. }
  168. EXPORT_SYMBOL_GPL(reset_control_assert);
  169. /**
  170. * reset_control_deassert - deasserts the reset line
  171. * @rstc: reset controller
  172. *
  173. * After calling this function, the reset is guaranteed to be deasserted.
  174. *
  175. * If rstc is NULL it is an optional reset and the function will just
  176. * return 0.
  177. */
  178. int reset_control_deassert(struct reset_control *rstc)
  179. {
  180. if (!rstc)
  181. return 0;
  182. if (WARN_ON(IS_ERR(rstc)))
  183. return -EINVAL;
  184. if (!rstc->rcdev->ops->deassert)
  185. return -ENOTSUPP;
  186. if (rstc->shared) {
  187. if (atomic_inc_return(&rstc->deassert_count) != 1)
  188. return 0;
  189. }
  190. return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
  191. }
  192. EXPORT_SYMBOL_GPL(reset_control_deassert);
  193. /**
  194. * reset_control_status - returns a negative errno if not supported, a
  195. * positive value if the reset line is asserted, or zero if the reset
  196. * line is not asserted or if the desc is NULL (optional reset).
  197. * @rstc: reset controller
  198. */
  199. int reset_control_status(struct reset_control *rstc)
  200. {
  201. if (!rstc)
  202. return 0;
  203. if (WARN_ON(IS_ERR(rstc)))
  204. return -EINVAL;
  205. if (rstc->rcdev->ops->status)
  206. return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
  207. return -ENOTSUPP;
  208. }
  209. EXPORT_SYMBOL_GPL(reset_control_status);
  210. static struct reset_control *__reset_control_get_internal(
  211. struct reset_controller_dev *rcdev,
  212. unsigned int index, int shared)
  213. {
  214. struct reset_control *rstc;
  215. lockdep_assert_held(&reset_list_mutex);
  216. list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
  217. if (rstc->id == index) {
  218. if (WARN_ON(!rstc->shared || !shared))
  219. return ERR_PTR(-EBUSY);
  220. rstc->refcnt++;
  221. return rstc;
  222. }
  223. }
  224. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  225. if (!rstc)
  226. return ERR_PTR(-ENOMEM);
  227. try_module_get(rcdev->owner);
  228. rstc->rcdev = rcdev;
  229. list_add(&rstc->list, &rcdev->reset_control_head);
  230. rstc->id = index;
  231. rstc->refcnt = 1;
  232. rstc->shared = shared;
  233. return rstc;
  234. }
  235. static void __reset_control_put_internal(struct reset_control *rstc)
  236. {
  237. lockdep_assert_held(&reset_list_mutex);
  238. if (--rstc->refcnt)
  239. return;
  240. module_put(rstc->rcdev->owner);
  241. list_del(&rstc->list);
  242. kfree(rstc);
  243. }
  244. struct reset_control *__of_reset_control_get(struct device_node *node,
  245. const char *id, int index, bool shared,
  246. bool optional)
  247. {
  248. struct reset_control *rstc;
  249. struct reset_controller_dev *r, *rcdev;
  250. struct of_phandle_args args;
  251. int rstc_id;
  252. int ret;
  253. if (!node)
  254. return ERR_PTR(-EINVAL);
  255. if (id) {
  256. index = of_property_match_string(node,
  257. "reset-names", id);
  258. if (index == -EILSEQ)
  259. return ERR_PTR(index);
  260. if (index < 0)
  261. return optional ? NULL : ERR_PTR(-ENOENT);
  262. }
  263. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  264. index, &args);
  265. if (ret == -EINVAL)
  266. return ERR_PTR(ret);
  267. if (ret)
  268. return optional ? NULL : ERR_PTR(ret);
  269. mutex_lock(&reset_list_mutex);
  270. rcdev = NULL;
  271. list_for_each_entry(r, &reset_controller_list, list) {
  272. if (args.np == r->of_node) {
  273. rcdev = r;
  274. break;
  275. }
  276. }
  277. of_node_put(args.np);
  278. if (!rcdev) {
  279. mutex_unlock(&reset_list_mutex);
  280. return ERR_PTR(-EPROBE_DEFER);
  281. }
  282. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  283. mutex_unlock(&reset_list_mutex);
  284. return ERR_PTR(-EINVAL);
  285. }
  286. rstc_id = rcdev->of_xlate(rcdev, &args);
  287. if (rstc_id < 0) {
  288. mutex_unlock(&reset_list_mutex);
  289. return ERR_PTR(rstc_id);
  290. }
  291. /* reset_list_mutex also protects the rcdev's reset_control list */
  292. rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
  293. mutex_unlock(&reset_list_mutex);
  294. return rstc;
  295. }
  296. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  297. struct reset_control *__reset_control_get(struct device *dev, const char *id,
  298. int index, bool shared, bool optional)
  299. {
  300. if (dev->of_node)
  301. return __of_reset_control_get(dev->of_node, id, index, shared,
  302. optional);
  303. return optional ? NULL : ERR_PTR(-EINVAL);
  304. }
  305. EXPORT_SYMBOL_GPL(__reset_control_get);
  306. /**
  307. * reset_control_put - free the reset controller
  308. * @rstc: reset controller
  309. */
  310. void reset_control_put(struct reset_control *rstc)
  311. {
  312. if (IS_ERR_OR_NULL(rstc))
  313. return;
  314. mutex_lock(&reset_list_mutex);
  315. __reset_control_put_internal(rstc);
  316. mutex_unlock(&reset_list_mutex);
  317. }
  318. EXPORT_SYMBOL_GPL(reset_control_put);
  319. static void devm_reset_control_release(struct device *dev, void *res)
  320. {
  321. reset_control_put(*(struct reset_control **)res);
  322. }
  323. struct reset_control *__devm_reset_control_get(struct device *dev,
  324. const char *id, int index, bool shared,
  325. bool optional)
  326. {
  327. struct reset_control **ptr, *rstc;
  328. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  329. GFP_KERNEL);
  330. if (!ptr)
  331. return ERR_PTR(-ENOMEM);
  332. rstc = __reset_control_get(dev, id, index, shared, optional);
  333. if (!IS_ERR(rstc)) {
  334. *ptr = rstc;
  335. devres_add(dev, ptr);
  336. } else {
  337. devres_free(ptr);
  338. }
  339. return rstc;
  340. }
  341. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  342. /**
  343. * device_reset - find reset controller associated with the device
  344. * and perform reset
  345. * @dev: device to be reset by the controller
  346. * @optional: whether it is optional to reset the device
  347. *
  348. * Convenience wrapper for __reset_control_get() and reset_control_reset().
  349. * This is useful for the common case of devices with single, dedicated reset
  350. * lines.
  351. */
  352. int __device_reset(struct device *dev, bool optional)
  353. {
  354. struct reset_control *rstc;
  355. int ret;
  356. rstc = __reset_control_get(dev, NULL, 0, 0, optional);
  357. if (IS_ERR(rstc))
  358. return PTR_ERR(rstc);
  359. ret = reset_control_reset(rstc);
  360. reset_control_put(rstc);
  361. return ret;
  362. }
  363. EXPORT_SYMBOL_GPL(__device_reset);