core.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. int reset_control_reset(struct reset_control *rstc)
  125. {
  126. if (WARN_ON(IS_ERR_OR_NULL(rstc)) ||
  127. WARN_ON(rstc->shared))
  128. return -EINVAL;
  129. if (rstc->rcdev->ops->reset)
  130. return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  131. return -ENOTSUPP;
  132. }
  133. EXPORT_SYMBOL_GPL(reset_control_reset);
  134. /**
  135. * reset_control_assert - asserts the reset line
  136. * @rstc: reset controller
  137. *
  138. * Calling this on an exclusive reset controller guarantees that the reset
  139. * will be asserted. When called on a shared reset controller the line may
  140. * still be deasserted, as long as other users keep it so.
  141. *
  142. * For shared reset controls a driver cannot expect the hw's registers and
  143. * internal state to be reset, but must be prepared for this to happen.
  144. */
  145. int reset_control_assert(struct reset_control *rstc)
  146. {
  147. if (WARN_ON(IS_ERR_OR_NULL(rstc)))
  148. return -EINVAL;
  149. if (!rstc->rcdev->ops->assert)
  150. return -ENOTSUPP;
  151. if (rstc->shared) {
  152. if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
  153. return -EINVAL;
  154. if (atomic_dec_return(&rstc->deassert_count) != 0)
  155. return 0;
  156. }
  157. return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
  158. }
  159. EXPORT_SYMBOL_GPL(reset_control_assert);
  160. /**
  161. * reset_control_deassert - deasserts the reset line
  162. * @rstc: reset controller
  163. *
  164. * After calling this function, the reset is guaranteed to be deasserted.
  165. */
  166. int reset_control_deassert(struct reset_control *rstc)
  167. {
  168. if (WARN_ON(IS_ERR_OR_NULL(rstc)))
  169. return -EINVAL;
  170. if (!rstc->rcdev->ops->deassert)
  171. return -ENOTSUPP;
  172. if (rstc->shared) {
  173. if (atomic_inc_return(&rstc->deassert_count) != 1)
  174. return 0;
  175. }
  176. return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
  177. }
  178. EXPORT_SYMBOL_GPL(reset_control_deassert);
  179. /**
  180. * reset_control_status - returns a negative errno if not supported, a
  181. * positive value if the reset line is asserted, or zero if the reset
  182. * line is not asserted.
  183. * @rstc: reset controller
  184. */
  185. int reset_control_status(struct reset_control *rstc)
  186. {
  187. if (WARN_ON(IS_ERR_OR_NULL(rstc)))
  188. return -EINVAL;
  189. if (rstc->rcdev->ops->status)
  190. return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
  191. return -ENOTSUPP;
  192. }
  193. EXPORT_SYMBOL_GPL(reset_control_status);
  194. static struct reset_control *__reset_control_get(
  195. struct reset_controller_dev *rcdev,
  196. unsigned int index, int shared)
  197. {
  198. struct reset_control *rstc;
  199. lockdep_assert_held(&reset_list_mutex);
  200. list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
  201. if (rstc->id == index) {
  202. if (WARN_ON(!rstc->shared || !shared))
  203. return ERR_PTR(-EBUSY);
  204. rstc->refcnt++;
  205. return rstc;
  206. }
  207. }
  208. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  209. if (!rstc)
  210. return ERR_PTR(-ENOMEM);
  211. try_module_get(rcdev->owner);
  212. rstc->rcdev = rcdev;
  213. list_add(&rstc->list, &rcdev->reset_control_head);
  214. rstc->id = index;
  215. rstc->refcnt = 1;
  216. rstc->shared = shared;
  217. return rstc;
  218. }
  219. static void __reset_control_put(struct reset_control *rstc)
  220. {
  221. lockdep_assert_held(&reset_list_mutex);
  222. if (--rstc->refcnt)
  223. return;
  224. module_put(rstc->rcdev->owner);
  225. list_del(&rstc->list);
  226. kfree(rstc);
  227. }
  228. struct reset_control *__of_reset_control_get(struct device_node *node,
  229. const char *id, int index, int shared)
  230. {
  231. struct reset_control *rstc;
  232. struct reset_controller_dev *r, *rcdev;
  233. struct of_phandle_args args;
  234. int rstc_id;
  235. int ret;
  236. if (!node)
  237. return ERR_PTR(-EINVAL);
  238. if (id) {
  239. index = of_property_match_string(node,
  240. "reset-names", id);
  241. if (index < 0)
  242. return ERR_PTR(-ENOENT);
  243. }
  244. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  245. index, &args);
  246. if (ret)
  247. return ERR_PTR(ret);
  248. mutex_lock(&reset_list_mutex);
  249. rcdev = NULL;
  250. list_for_each_entry(r, &reset_controller_list, list) {
  251. if (args.np == r->of_node) {
  252. rcdev = r;
  253. break;
  254. }
  255. }
  256. of_node_put(args.np);
  257. if (!rcdev) {
  258. mutex_unlock(&reset_list_mutex);
  259. return ERR_PTR(-EPROBE_DEFER);
  260. }
  261. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  262. mutex_unlock(&reset_list_mutex);
  263. return ERR_PTR(-EINVAL);
  264. }
  265. rstc_id = rcdev->of_xlate(rcdev, &args);
  266. if (rstc_id < 0) {
  267. mutex_unlock(&reset_list_mutex);
  268. return ERR_PTR(rstc_id);
  269. }
  270. /* reset_list_mutex also protects the rcdev's reset_control list */
  271. rstc = __reset_control_get(rcdev, rstc_id, shared);
  272. mutex_unlock(&reset_list_mutex);
  273. return rstc;
  274. }
  275. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  276. /**
  277. * reset_control_put - free the reset controller
  278. * @rstc: reset controller
  279. */
  280. void reset_control_put(struct reset_control *rstc)
  281. {
  282. if (IS_ERR(rstc))
  283. return;
  284. mutex_lock(&reset_list_mutex);
  285. __reset_control_put(rstc);
  286. mutex_unlock(&reset_list_mutex);
  287. }
  288. EXPORT_SYMBOL_GPL(reset_control_put);
  289. static void devm_reset_control_release(struct device *dev, void *res)
  290. {
  291. reset_control_put(*(struct reset_control **)res);
  292. }
  293. struct reset_control *__devm_reset_control_get(struct device *dev,
  294. const char *id, int index, int shared)
  295. {
  296. struct reset_control **ptr, *rstc;
  297. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  298. GFP_KERNEL);
  299. if (!ptr)
  300. return ERR_PTR(-ENOMEM);
  301. rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
  302. id, index, shared);
  303. if (!IS_ERR(rstc)) {
  304. *ptr = rstc;
  305. devres_add(dev, ptr);
  306. } else {
  307. devres_free(ptr);
  308. }
  309. return rstc;
  310. }
  311. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  312. /**
  313. * device_reset - find reset controller associated with the device
  314. * and perform reset
  315. * @dev: device to be reset by the controller
  316. *
  317. * Convenience wrapper for reset_control_get() and reset_control_reset().
  318. * This is useful for the common case of devices with single, dedicated reset
  319. * lines.
  320. */
  321. int device_reset(struct device *dev)
  322. {
  323. struct reset_control *rstc;
  324. int ret;
  325. rstc = reset_control_get(dev, NULL);
  326. if (IS_ERR(rstc))
  327. return PTR_ERR(rstc);
  328. ret = reset_control_reset(rstc);
  329. reset_control_put(rstc);
  330. return ret;
  331. }
  332. EXPORT_SYMBOL_GPL(device_reset);