core.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * reset_control_status - returns a negative errno if not supported, a
  114. * positive value if the reset line is asserted, or zero if the reset
  115. * line is not asserted.
  116. * @rstc: reset controller
  117. */
  118. int reset_control_status(struct reset_control *rstc)
  119. {
  120. if (rstc->rcdev->ops->status)
  121. return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
  122. return -ENOSYS;
  123. }
  124. EXPORT_SYMBOL_GPL(reset_control_status);
  125. /**
  126. * of_reset_control_get - Lookup and obtain a reference to a reset controller.
  127. * @node: device to be reset by the controller
  128. * @id: reset line name
  129. *
  130. * Returns a struct reset_control or IS_ERR() condition containing errno.
  131. *
  132. * Use of id names is optional.
  133. */
  134. struct reset_control *of_reset_control_get(struct device_node *node,
  135. const char *id)
  136. {
  137. struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
  138. struct reset_controller_dev *r, *rcdev;
  139. struct of_phandle_args args;
  140. int index = 0;
  141. int rstc_id;
  142. int ret;
  143. if (id)
  144. index = of_property_match_string(node,
  145. "reset-names", id);
  146. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  147. index, &args);
  148. if (ret)
  149. return ERR_PTR(ret);
  150. mutex_lock(&reset_controller_list_mutex);
  151. rcdev = NULL;
  152. list_for_each_entry(r, &reset_controller_list, list) {
  153. if (args.np == r->of_node) {
  154. rcdev = r;
  155. break;
  156. }
  157. }
  158. of_node_put(args.np);
  159. if (!rcdev) {
  160. mutex_unlock(&reset_controller_list_mutex);
  161. return ERR_PTR(-EPROBE_DEFER);
  162. }
  163. rstc_id = rcdev->of_xlate(rcdev, &args);
  164. if (rstc_id < 0) {
  165. mutex_unlock(&reset_controller_list_mutex);
  166. return ERR_PTR(rstc_id);
  167. }
  168. try_module_get(rcdev->owner);
  169. mutex_unlock(&reset_controller_list_mutex);
  170. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  171. if (!rstc) {
  172. module_put(rcdev->owner);
  173. return ERR_PTR(-ENOMEM);
  174. }
  175. rstc->rcdev = rcdev;
  176. rstc->id = rstc_id;
  177. return rstc;
  178. }
  179. EXPORT_SYMBOL_GPL(of_reset_control_get);
  180. /**
  181. * reset_control_get - Lookup and obtain a reference to a reset controller.
  182. * @dev: device to be reset by the controller
  183. * @id: reset line name
  184. *
  185. * Returns a struct reset_control or IS_ERR() condition containing errno.
  186. *
  187. * Use of id names is optional.
  188. */
  189. struct reset_control *reset_control_get(struct device *dev, const char *id)
  190. {
  191. struct reset_control *rstc;
  192. if (!dev)
  193. return ERR_PTR(-EINVAL);
  194. rstc = of_reset_control_get(dev->of_node, id);
  195. if (!IS_ERR(rstc))
  196. rstc->dev = dev;
  197. return rstc;
  198. }
  199. EXPORT_SYMBOL_GPL(reset_control_get);
  200. /**
  201. * reset_control_put - free the reset controller
  202. * @rstc: reset controller
  203. */
  204. void reset_control_put(struct reset_control *rstc)
  205. {
  206. if (IS_ERR(rstc))
  207. return;
  208. module_put(rstc->rcdev->owner);
  209. kfree(rstc);
  210. }
  211. EXPORT_SYMBOL_GPL(reset_control_put);
  212. static void devm_reset_control_release(struct device *dev, void *res)
  213. {
  214. reset_control_put(*(struct reset_control **)res);
  215. }
  216. /**
  217. * devm_reset_control_get - resource managed reset_control_get()
  218. * @dev: device to be reset by the controller
  219. * @id: reset line name
  220. *
  221. * Managed reset_control_get(). For reset controllers returned from this
  222. * function, reset_control_put() is called automatically on driver detach.
  223. * See reset_control_get() for more information.
  224. */
  225. struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
  226. {
  227. struct reset_control **ptr, *rstc;
  228. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  229. GFP_KERNEL);
  230. if (!ptr)
  231. return ERR_PTR(-ENOMEM);
  232. rstc = reset_control_get(dev, id);
  233. if (!IS_ERR(rstc)) {
  234. *ptr = rstc;
  235. devres_add(dev, ptr);
  236. } else {
  237. devres_free(ptr);
  238. }
  239. return rstc;
  240. }
  241. EXPORT_SYMBOL_GPL(devm_reset_control_get);
  242. /**
  243. * device_reset - find reset controller associated with the device
  244. * and perform reset
  245. * @dev: device to be reset by the controller
  246. *
  247. * Convenience wrapper for reset_control_get() and reset_control_reset().
  248. * This is useful for the common case of devices with single, dedicated reset
  249. * lines.
  250. */
  251. int device_reset(struct device *dev)
  252. {
  253. struct reset_control *rstc;
  254. int ret;
  255. rstc = reset_control_get(dev, NULL);
  256. if (IS_ERR(rstc))
  257. return PTR_ERR(rstc);
  258. ret = reset_control_reset(rstc);
  259. reset_control_put(rstc);
  260. return ret;
  261. }
  262. EXPORT_SYMBOL_GPL(device_reset);