core.c 8.0 KB

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