core.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/device.h>
  12. #include <linux/err.h>
  13. #include <linux/export.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/reset.h>
  18. #include <linux/reset-controller.h>
  19. #include <linux/slab.h>
  20. static DEFINE_MUTEX(reset_controller_list_mutex);
  21. static LIST_HEAD(reset_controller_list);
  22. /**
  23. * struct reset_control - a reset control
  24. * @rcdev: a pointer to the reset controller device
  25. * this reset control belongs to
  26. * @id: ID of the reset controller in the reset
  27. * controller device
  28. */
  29. struct reset_control {
  30. struct reset_controller_dev *rcdev;
  31. struct device *dev;
  32. unsigned int id;
  33. };
  34. /**
  35. * of_reset_simple_xlate - translate reset_spec to the reset line number
  36. * @rcdev: a pointer to the reset controller device
  37. * @reset_spec: reset line specifier as found in the device tree
  38. * @flags: a flags pointer to fill in (optional)
  39. *
  40. * This simple translation function should be used for reset controllers
  41. * with 1:1 mapping, where reset lines can be indexed by number without gaps.
  42. */
  43. static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  44. const struct of_phandle_args *reset_spec)
  45. {
  46. if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
  47. return -EINVAL;
  48. if (reset_spec->args[0] >= rcdev->nr_resets)
  49. return -EINVAL;
  50. return reset_spec->args[0];
  51. }
  52. /**
  53. * reset_controller_register - register a reset controller device
  54. * @rcdev: a pointer to the initialized reset controller device
  55. */
  56. int reset_controller_register(struct reset_controller_dev *rcdev)
  57. {
  58. if (!rcdev->of_xlate) {
  59. rcdev->of_reset_n_cells = 1;
  60. rcdev->of_xlate = of_reset_simple_xlate;
  61. }
  62. mutex_lock(&reset_controller_list_mutex);
  63. list_add(&rcdev->list, &reset_controller_list);
  64. mutex_unlock(&reset_controller_list_mutex);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL_GPL(reset_controller_register);
  68. /**
  69. * reset_controller_unregister - unregister a reset controller device
  70. * @rcdev: a pointer to the reset controller device
  71. */
  72. void reset_controller_unregister(struct reset_controller_dev *rcdev)
  73. {
  74. mutex_lock(&reset_controller_list_mutex);
  75. list_del(&rcdev->list);
  76. mutex_unlock(&reset_controller_list_mutex);
  77. }
  78. EXPORT_SYMBOL_GPL(reset_controller_unregister);
  79. /**
  80. * reset_control_reset - reset the controlled device
  81. * @rstc: reset controller
  82. */
  83. int reset_control_reset(struct reset_control *rstc)
  84. {
  85. if (rstc->rcdev->ops->reset)
  86. return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  87. return -ENOSYS;
  88. }
  89. EXPORT_SYMBOL_GPL(reset_control_reset);
  90. /**
  91. * reset_control_assert - asserts the reset line
  92. * @rstc: reset controller
  93. */
  94. int reset_control_assert(struct reset_control *rstc)
  95. {
  96. if (rstc->rcdev->ops->assert)
  97. return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
  98. return -ENOSYS;
  99. }
  100. EXPORT_SYMBOL_GPL(reset_control_assert);
  101. /**
  102. * reset_control_deassert - deasserts the reset line
  103. * @rstc: reset controller
  104. */
  105. int reset_control_deassert(struct reset_control *rstc)
  106. {
  107. if (rstc->rcdev->ops->deassert)
  108. return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
  109. return -ENOSYS;
  110. }
  111. EXPORT_SYMBOL_GPL(reset_control_deassert);
  112. /**
  113. * of_reset_control_get - Lookup and obtain a reference to a reset controller.
  114. * @node: device to be reset by the controller
  115. * @id: reset line name
  116. *
  117. * Returns a struct reset_control or IS_ERR() condition containing errno.
  118. *
  119. * Use of id names is optional.
  120. */
  121. struct reset_control *of_reset_control_get(struct device_node *node,
  122. const char *id)
  123. {
  124. struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
  125. struct reset_controller_dev *r, *rcdev;
  126. struct of_phandle_args args;
  127. int index = 0;
  128. int rstc_id;
  129. int ret;
  130. if (id)
  131. index = of_property_match_string(node,
  132. "reset-names", id);
  133. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  134. index, &args);
  135. if (ret)
  136. return ERR_PTR(ret);
  137. mutex_lock(&reset_controller_list_mutex);
  138. rcdev = NULL;
  139. list_for_each_entry(r, &reset_controller_list, list) {
  140. if (args.np == r->of_node) {
  141. rcdev = r;
  142. break;
  143. }
  144. }
  145. of_node_put(args.np);
  146. if (!rcdev) {
  147. mutex_unlock(&reset_controller_list_mutex);
  148. return ERR_PTR(-EPROBE_DEFER);
  149. }
  150. rstc_id = rcdev->of_xlate(rcdev, &args);
  151. if (rstc_id < 0) {
  152. mutex_unlock(&reset_controller_list_mutex);
  153. return ERR_PTR(rstc_id);
  154. }
  155. try_module_get(rcdev->owner);
  156. mutex_unlock(&reset_controller_list_mutex);
  157. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  158. if (!rstc) {
  159. module_put(rcdev->owner);
  160. return ERR_PTR(-ENOMEM);
  161. }
  162. rstc->rcdev = rcdev;
  163. rstc->id = rstc_id;
  164. return rstc;
  165. }
  166. EXPORT_SYMBOL_GPL(of_reset_control_get);
  167. /**
  168. * reset_control_get - Lookup and obtain a reference to a reset controller.
  169. * @dev: device to be reset by the controller
  170. * @id: reset line name
  171. *
  172. * Returns a struct reset_control or IS_ERR() condition containing errno.
  173. *
  174. * Use of id names is optional.
  175. */
  176. struct reset_control *reset_control_get(struct device *dev, const char *id)
  177. {
  178. struct reset_control *rstc;
  179. if (!dev)
  180. return ERR_PTR(-EINVAL);
  181. rstc = of_reset_control_get(dev->of_node, id);
  182. if (!IS_ERR(rstc))
  183. rstc->dev = dev;
  184. return rstc;
  185. }
  186. EXPORT_SYMBOL_GPL(reset_control_get);
  187. /**
  188. * reset_control_put - free the reset controller
  189. * @rstc: reset controller
  190. */
  191. void reset_control_put(struct reset_control *rstc)
  192. {
  193. if (IS_ERR(rstc))
  194. return;
  195. module_put(rstc->rcdev->owner);
  196. kfree(rstc);
  197. }
  198. EXPORT_SYMBOL_GPL(reset_control_put);
  199. static void devm_reset_control_release(struct device *dev, void *res)
  200. {
  201. reset_control_put(*(struct reset_control **)res);
  202. }
  203. /**
  204. * devm_reset_control_get - resource managed reset_control_get()
  205. * @dev: device to be reset by the controller
  206. * @id: reset line name
  207. *
  208. * Managed reset_control_get(). For reset controllers returned from this
  209. * function, reset_control_put() is called automatically on driver detach.
  210. * See reset_control_get() for more information.
  211. */
  212. struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
  213. {
  214. struct reset_control **ptr, *rstc;
  215. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  216. GFP_KERNEL);
  217. if (!ptr)
  218. return ERR_PTR(-ENOMEM);
  219. rstc = reset_control_get(dev, id);
  220. if (!IS_ERR(rstc)) {
  221. *ptr = rstc;
  222. devres_add(dev, ptr);
  223. } else {
  224. devres_free(ptr);
  225. }
  226. return rstc;
  227. }
  228. EXPORT_SYMBOL_GPL(devm_reset_control_get);
  229. /**
  230. * device_reset - find reset controller associated with the device
  231. * and perform reset
  232. * @dev: device to be reset by the controller
  233. *
  234. * Convenience wrapper for reset_control_get() and reset_control_reset().
  235. * This is useful for the common case of devices with single, dedicated reset
  236. * lines.
  237. */
  238. int device_reset(struct device *dev)
  239. {
  240. struct reset_control *rstc;
  241. int ret;
  242. rstc = reset_control_get(dev, NULL);
  243. if (IS_ERR(rstc))
  244. return PTR_ERR(rstc);
  245. ret = reset_control_reset(rstc);
  246. reset_control_put(rstc);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL_GPL(device_reset);