core.c 9.5 KB

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