core.c 7.3 KB

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