core.c 8.0 KB

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