core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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/atomic.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/reset.h>
  19. #include <linux/reset-controller.h>
  20. #include <linux/slab.h>
  21. static DEFINE_MUTEX(reset_list_mutex);
  22. static LIST_HEAD(reset_controller_list);
  23. /**
  24. * struct reset_control - a reset control
  25. * @rcdev: a pointer to the reset controller device
  26. * this reset control belongs to
  27. * @list: list entry for the rcdev's reset controller list
  28. * @id: ID of the reset controller in the reset
  29. * controller device
  30. * @refcnt: Number of gets of this reset_control
  31. * @shared: Is this a shared (1), or an exclusive (0) reset_control?
  32. * @deassert_cnt: Number of times this reset line has been deasserted
  33. * @triggered_count: Number of times this reset line has been reset. Currently
  34. * only used for shared resets, which means that the value
  35. * will be either 0 or 1.
  36. */
  37. struct reset_control {
  38. struct reset_controller_dev *rcdev;
  39. struct list_head list;
  40. unsigned int id;
  41. unsigned int refcnt;
  42. int shared;
  43. atomic_t deassert_count;
  44. atomic_t triggered_count;
  45. };
  46. /**
  47. * of_reset_simple_xlate - translate reset_spec to the reset line number
  48. * @rcdev: a pointer to the reset controller device
  49. * @reset_spec: reset line specifier as found in the device tree
  50. * @flags: a flags pointer to fill in (optional)
  51. *
  52. * This simple translation function should be used for reset controllers
  53. * with 1:1 mapping, where reset lines can be indexed by number without gaps.
  54. */
  55. static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  56. const struct of_phandle_args *reset_spec)
  57. {
  58. if (reset_spec->args[0] >= rcdev->nr_resets)
  59. return -EINVAL;
  60. return reset_spec->args[0];
  61. }
  62. /**
  63. * reset_controller_register - register a reset controller device
  64. * @rcdev: a pointer to the initialized reset controller device
  65. */
  66. int reset_controller_register(struct reset_controller_dev *rcdev)
  67. {
  68. if (!rcdev->of_xlate) {
  69. rcdev->of_reset_n_cells = 1;
  70. rcdev->of_xlate = of_reset_simple_xlate;
  71. }
  72. INIT_LIST_HEAD(&rcdev->reset_control_head);
  73. mutex_lock(&reset_list_mutex);
  74. list_add(&rcdev->list, &reset_controller_list);
  75. mutex_unlock(&reset_list_mutex);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(reset_controller_register);
  79. /**
  80. * reset_controller_unregister - unregister a reset controller device
  81. * @rcdev: a pointer to the reset controller device
  82. */
  83. void reset_controller_unregister(struct reset_controller_dev *rcdev)
  84. {
  85. mutex_lock(&reset_list_mutex);
  86. list_del(&rcdev->list);
  87. mutex_unlock(&reset_list_mutex);
  88. }
  89. EXPORT_SYMBOL_GPL(reset_controller_unregister);
  90. static void devm_reset_controller_release(struct device *dev, void *res)
  91. {
  92. reset_controller_unregister(*(struct reset_controller_dev **)res);
  93. }
  94. /**
  95. * devm_reset_controller_register - resource managed reset_controller_register()
  96. * @dev: device that is registering this reset controller
  97. * @rcdev: a pointer to the initialized reset controller device
  98. *
  99. * Managed reset_controller_register(). For reset controllers registered by
  100. * this function, reset_controller_unregister() is automatically called on
  101. * driver detach. See reset_controller_register() for more information.
  102. */
  103. int devm_reset_controller_register(struct device *dev,
  104. struct reset_controller_dev *rcdev)
  105. {
  106. struct reset_controller_dev **rcdevp;
  107. int ret;
  108. rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
  109. GFP_KERNEL);
  110. if (!rcdevp)
  111. return -ENOMEM;
  112. ret = reset_controller_register(rcdev);
  113. if (!ret) {
  114. *rcdevp = rcdev;
  115. devres_add(dev, rcdevp);
  116. } else {
  117. devres_free(rcdevp);
  118. }
  119. return ret;
  120. }
  121. EXPORT_SYMBOL_GPL(devm_reset_controller_register);
  122. /**
  123. * reset_control_reset - reset the controlled device
  124. * @rstc: reset controller
  125. *
  126. * On a shared reset line the actual reset pulse is only triggered once for the
  127. * lifetime of the reset_control instance: for all but the first caller this is
  128. * a no-op.
  129. * Consumers must not use reset_control_(de)assert on shared reset lines when
  130. * reset_control_reset has been used.
  131. */
  132. int reset_control_reset(struct reset_control *rstc)
  133. {
  134. int ret;
  135. if (WARN_ON(IS_ERR_OR_NULL(rstc)))
  136. return -EINVAL;
  137. if (!rstc->rcdev->ops->reset)
  138. return -ENOTSUPP;
  139. if (rstc->shared) {
  140. if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
  141. return -EINVAL;
  142. if (atomic_inc_return(&rstc->triggered_count) != 1)
  143. return 0;
  144. }
  145. ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  146. if (rstc->shared && !ret)
  147. atomic_dec(&rstc->triggered_count);
  148. return ret;
  149. }
  150. EXPORT_SYMBOL_GPL(reset_control_reset);
  151. /**
  152. * reset_control_assert - asserts the reset line
  153. * @rstc: reset controller
  154. *
  155. * Calling this on an exclusive reset controller guarantees that the reset
  156. * will be asserted. When called on a shared reset controller the line may
  157. * still be deasserted, as long as other users keep it so.
  158. *
  159. * For shared reset controls a driver cannot expect the hw's registers and
  160. * internal state to be reset, but must be prepared for this to happen.
  161. * Consumers must not use reset_control_reset on shared reset lines when
  162. * reset_control_(de)assert has been used.
  163. */
  164. int reset_control_assert(struct reset_control *rstc)
  165. {
  166. if (WARN_ON(IS_ERR_OR_NULL(rstc)))
  167. return -EINVAL;
  168. if (!rstc->rcdev->ops->assert)
  169. return -ENOTSUPP;
  170. if (rstc->shared) {
  171. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  172. return -EINVAL;
  173. if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
  174. return -EINVAL;
  175. if (atomic_dec_return(&rstc->deassert_count) != 0)
  176. return 0;
  177. }
  178. return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
  179. }
  180. EXPORT_SYMBOL_GPL(reset_control_assert);
  181. /**
  182. * reset_control_deassert - deasserts the reset line
  183. * @rstc: reset controller
  184. *
  185. * After calling this function, the reset is guaranteed to be deasserted.
  186. * Consumers must not use reset_control_reset on shared reset lines when
  187. * reset_control_(de)assert has been used.
  188. */
  189. int reset_control_deassert(struct reset_control *rstc)
  190. {
  191. if (WARN_ON(IS_ERR_OR_NULL(rstc)))
  192. return -EINVAL;
  193. if (!rstc->rcdev->ops->deassert)
  194. return -ENOTSUPP;
  195. if (rstc->shared) {
  196. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  197. return -EINVAL;
  198. if (atomic_inc_return(&rstc->deassert_count) != 1)
  199. return 0;
  200. }
  201. return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
  202. }
  203. EXPORT_SYMBOL_GPL(reset_control_deassert);
  204. /**
  205. * reset_control_status - returns a negative errno if not supported, a
  206. * positive value if the reset line is asserted, or zero if the reset
  207. * line is not asserted.
  208. * @rstc: reset controller
  209. */
  210. int reset_control_status(struct reset_control *rstc)
  211. {
  212. if (WARN_ON(IS_ERR_OR_NULL(rstc)))
  213. return -EINVAL;
  214. if (rstc->rcdev->ops->status)
  215. return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
  216. return -ENOTSUPP;
  217. }
  218. EXPORT_SYMBOL_GPL(reset_control_status);
  219. static struct reset_control *__reset_control_get(
  220. struct reset_controller_dev *rcdev,
  221. unsigned int index, int shared)
  222. {
  223. struct reset_control *rstc;
  224. lockdep_assert_held(&reset_list_mutex);
  225. list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
  226. if (rstc->id == index) {
  227. if (WARN_ON(!rstc->shared || !shared))
  228. return ERR_PTR(-EBUSY);
  229. rstc->refcnt++;
  230. return rstc;
  231. }
  232. }
  233. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  234. if (!rstc)
  235. return ERR_PTR(-ENOMEM);
  236. try_module_get(rcdev->owner);
  237. rstc->rcdev = rcdev;
  238. list_add(&rstc->list, &rcdev->reset_control_head);
  239. rstc->id = index;
  240. rstc->refcnt = 1;
  241. rstc->shared = shared;
  242. return rstc;
  243. }
  244. static void __reset_control_put(struct reset_control *rstc)
  245. {
  246. lockdep_assert_held(&reset_list_mutex);
  247. if (--rstc->refcnt)
  248. return;
  249. module_put(rstc->rcdev->owner);
  250. list_del(&rstc->list);
  251. kfree(rstc);
  252. }
  253. struct reset_control *__of_reset_control_get(struct device_node *node,
  254. const char *id, int index, int shared)
  255. {
  256. struct reset_control *rstc;
  257. struct reset_controller_dev *r, *rcdev;
  258. struct of_phandle_args args;
  259. int rstc_id;
  260. int ret;
  261. if (!node)
  262. return ERR_PTR(-EINVAL);
  263. if (id) {
  264. index = of_property_match_string(node,
  265. "reset-names", id);
  266. if (index < 0)
  267. return ERR_PTR(-ENOENT);
  268. }
  269. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  270. index, &args);
  271. if (ret)
  272. return ERR_PTR(ret);
  273. mutex_lock(&reset_list_mutex);
  274. rcdev = NULL;
  275. list_for_each_entry(r, &reset_controller_list, list) {
  276. if (args.np == r->of_node) {
  277. rcdev = r;
  278. break;
  279. }
  280. }
  281. of_node_put(args.np);
  282. if (!rcdev) {
  283. mutex_unlock(&reset_list_mutex);
  284. return ERR_PTR(-EPROBE_DEFER);
  285. }
  286. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  287. mutex_unlock(&reset_list_mutex);
  288. return ERR_PTR(-EINVAL);
  289. }
  290. rstc_id = rcdev->of_xlate(rcdev, &args);
  291. if (rstc_id < 0) {
  292. mutex_unlock(&reset_list_mutex);
  293. return ERR_PTR(rstc_id);
  294. }
  295. /* reset_list_mutex also protects the rcdev's reset_control list */
  296. rstc = __reset_control_get(rcdev, rstc_id, shared);
  297. mutex_unlock(&reset_list_mutex);
  298. return rstc;
  299. }
  300. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  301. /**
  302. * reset_control_put - free the reset controller
  303. * @rstc: reset controller
  304. */
  305. void reset_control_put(struct reset_control *rstc)
  306. {
  307. if (IS_ERR(rstc))
  308. return;
  309. mutex_lock(&reset_list_mutex);
  310. __reset_control_put(rstc);
  311. mutex_unlock(&reset_list_mutex);
  312. }
  313. EXPORT_SYMBOL_GPL(reset_control_put);
  314. static void devm_reset_control_release(struct device *dev, void *res)
  315. {
  316. reset_control_put(*(struct reset_control **)res);
  317. }
  318. struct reset_control *__devm_reset_control_get(struct device *dev,
  319. const char *id, int index, int shared)
  320. {
  321. struct reset_control **ptr, *rstc;
  322. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  323. GFP_KERNEL);
  324. if (!ptr)
  325. return ERR_PTR(-ENOMEM);
  326. rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
  327. id, index, shared);
  328. if (!IS_ERR(rstc)) {
  329. *ptr = rstc;
  330. devres_add(dev, ptr);
  331. } else {
  332. devres_free(ptr);
  333. }
  334. return rstc;
  335. }
  336. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  337. /**
  338. * device_reset - find reset controller associated with the device
  339. * and perform reset
  340. * @dev: device to be reset by the controller
  341. *
  342. * Convenience wrapper for reset_control_get() and reset_control_reset().
  343. * This is useful for the common case of devices with single, dedicated reset
  344. * lines.
  345. */
  346. int device_reset(struct device *dev)
  347. {
  348. struct reset_control *rstc;
  349. int ret;
  350. rstc = reset_control_get(dev, NULL);
  351. if (IS_ERR(rstc))
  352. return PTR_ERR(rstc);
  353. ret = reset_control_reset(rstc);
  354. reset_control_put(rstc);
  355. return ret;
  356. }
  357. EXPORT_SYMBOL_GPL(device_reset);