v4l2-clk.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * V4L2 clock service
  3. *
  4. * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/atomic.h>
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/errno.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <media/v4l2-clk.h>
  20. #include <media/v4l2-subdev.h>
  21. static DEFINE_MUTEX(clk_lock);
  22. static LIST_HEAD(clk_list);
  23. static struct v4l2_clk *v4l2_clk_find(const char *dev_id)
  24. {
  25. struct v4l2_clk *clk;
  26. list_for_each_entry(clk, &clk_list, list)
  27. if (!strcmp(dev_id, clk->dev_id))
  28. return clk;
  29. return ERR_PTR(-ENODEV);
  30. }
  31. struct v4l2_clk *v4l2_clk_get(struct device *dev, const char *id)
  32. {
  33. struct v4l2_clk *clk;
  34. struct clk *ccf_clk = clk_get(dev, id);
  35. if (PTR_ERR(ccf_clk) == -EPROBE_DEFER)
  36. return ERR_PTR(-EPROBE_DEFER);
  37. if (!IS_ERR_OR_NULL(ccf_clk)) {
  38. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  39. if (!clk) {
  40. clk_put(ccf_clk);
  41. return ERR_PTR(-ENOMEM);
  42. }
  43. clk->clk = ccf_clk;
  44. return clk;
  45. }
  46. mutex_lock(&clk_lock);
  47. clk = v4l2_clk_find(dev_name(dev));
  48. if (!IS_ERR(clk))
  49. atomic_inc(&clk->use_count);
  50. mutex_unlock(&clk_lock);
  51. return clk;
  52. }
  53. EXPORT_SYMBOL(v4l2_clk_get);
  54. void v4l2_clk_put(struct v4l2_clk *clk)
  55. {
  56. struct v4l2_clk *tmp;
  57. if (IS_ERR(clk))
  58. return;
  59. if (clk->clk) {
  60. clk_put(clk->clk);
  61. kfree(clk);
  62. return;
  63. }
  64. mutex_lock(&clk_lock);
  65. list_for_each_entry(tmp, &clk_list, list)
  66. if (tmp == clk)
  67. atomic_dec(&clk->use_count);
  68. mutex_unlock(&clk_lock);
  69. }
  70. EXPORT_SYMBOL(v4l2_clk_put);
  71. static int v4l2_clk_lock_driver(struct v4l2_clk *clk)
  72. {
  73. struct v4l2_clk *tmp;
  74. int ret = -ENODEV;
  75. mutex_lock(&clk_lock);
  76. list_for_each_entry(tmp, &clk_list, list)
  77. if (tmp == clk) {
  78. ret = !try_module_get(clk->ops->owner);
  79. if (ret)
  80. ret = -EFAULT;
  81. break;
  82. }
  83. mutex_unlock(&clk_lock);
  84. return ret;
  85. }
  86. static void v4l2_clk_unlock_driver(struct v4l2_clk *clk)
  87. {
  88. module_put(clk->ops->owner);
  89. }
  90. int v4l2_clk_enable(struct v4l2_clk *clk)
  91. {
  92. int ret;
  93. if (clk->clk)
  94. return clk_prepare_enable(clk->clk);
  95. ret = v4l2_clk_lock_driver(clk);
  96. if (ret < 0)
  97. return ret;
  98. mutex_lock(&clk->lock);
  99. if (++clk->enable == 1 && clk->ops->enable) {
  100. ret = clk->ops->enable(clk);
  101. if (ret < 0)
  102. clk->enable--;
  103. }
  104. mutex_unlock(&clk->lock);
  105. return ret;
  106. }
  107. EXPORT_SYMBOL(v4l2_clk_enable);
  108. /*
  109. * You might Oops if you try to disabled a disabled clock, because then the
  110. * driver isn't locked and could have been unloaded by now, so, don't do that
  111. */
  112. void v4l2_clk_disable(struct v4l2_clk *clk)
  113. {
  114. int enable;
  115. if (clk->clk)
  116. return clk_disable_unprepare(clk->clk);
  117. mutex_lock(&clk->lock);
  118. enable = --clk->enable;
  119. if (WARN(enable < 0, "Unbalanced %s() on %s!\n", __func__,
  120. clk->dev_id))
  121. clk->enable++;
  122. else if (!enable && clk->ops->disable)
  123. clk->ops->disable(clk);
  124. mutex_unlock(&clk->lock);
  125. v4l2_clk_unlock_driver(clk);
  126. }
  127. EXPORT_SYMBOL(v4l2_clk_disable);
  128. unsigned long v4l2_clk_get_rate(struct v4l2_clk *clk)
  129. {
  130. int ret;
  131. if (clk->clk)
  132. return clk_get_rate(clk->clk);
  133. ret = v4l2_clk_lock_driver(clk);
  134. if (ret < 0)
  135. return ret;
  136. mutex_lock(&clk->lock);
  137. if (!clk->ops->get_rate)
  138. ret = -ENOSYS;
  139. else
  140. ret = clk->ops->get_rate(clk);
  141. mutex_unlock(&clk->lock);
  142. v4l2_clk_unlock_driver(clk);
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(v4l2_clk_get_rate);
  146. int v4l2_clk_set_rate(struct v4l2_clk *clk, unsigned long rate)
  147. {
  148. int ret;
  149. if (clk->clk) {
  150. long r = clk_round_rate(clk->clk, rate);
  151. if (r < 0)
  152. return r;
  153. return clk_set_rate(clk->clk, r);
  154. }
  155. ret = v4l2_clk_lock_driver(clk);
  156. if (ret < 0)
  157. return ret;
  158. mutex_lock(&clk->lock);
  159. if (!clk->ops->set_rate)
  160. ret = -ENOSYS;
  161. else
  162. ret = clk->ops->set_rate(clk, rate);
  163. mutex_unlock(&clk->lock);
  164. v4l2_clk_unlock_driver(clk);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(v4l2_clk_set_rate);
  168. struct v4l2_clk *v4l2_clk_register(const struct v4l2_clk_ops *ops,
  169. const char *dev_id,
  170. void *priv)
  171. {
  172. struct v4l2_clk *clk;
  173. int ret;
  174. if (!ops || !dev_id)
  175. return ERR_PTR(-EINVAL);
  176. clk = kzalloc(sizeof(struct v4l2_clk), GFP_KERNEL);
  177. if (!clk)
  178. return ERR_PTR(-ENOMEM);
  179. clk->dev_id = kstrdup(dev_id, GFP_KERNEL);
  180. if (!clk->dev_id) {
  181. ret = -ENOMEM;
  182. goto ealloc;
  183. }
  184. clk->ops = ops;
  185. clk->priv = priv;
  186. atomic_set(&clk->use_count, 0);
  187. mutex_init(&clk->lock);
  188. mutex_lock(&clk_lock);
  189. if (!IS_ERR(v4l2_clk_find(dev_id))) {
  190. mutex_unlock(&clk_lock);
  191. ret = -EEXIST;
  192. goto eexist;
  193. }
  194. list_add_tail(&clk->list, &clk_list);
  195. mutex_unlock(&clk_lock);
  196. return clk;
  197. eexist:
  198. ealloc:
  199. kfree(clk->dev_id);
  200. kfree(clk);
  201. return ERR_PTR(ret);
  202. }
  203. EXPORT_SYMBOL(v4l2_clk_register);
  204. void v4l2_clk_unregister(struct v4l2_clk *clk)
  205. {
  206. if (WARN(atomic_read(&clk->use_count),
  207. "%s(): Refusing to unregister ref-counted %s clock!\n",
  208. __func__, clk->dev_id))
  209. return;
  210. mutex_lock(&clk_lock);
  211. list_del(&clk->list);
  212. mutex_unlock(&clk_lock);
  213. kfree(clk->dev_id);
  214. kfree(clk);
  215. }
  216. EXPORT_SYMBOL(v4l2_clk_unregister);
  217. struct v4l2_clk_fixed {
  218. unsigned long rate;
  219. struct v4l2_clk_ops ops;
  220. };
  221. static unsigned long fixed_get_rate(struct v4l2_clk *clk)
  222. {
  223. struct v4l2_clk_fixed *priv = clk->priv;
  224. return priv->rate;
  225. }
  226. struct v4l2_clk *__v4l2_clk_register_fixed(const char *dev_id,
  227. unsigned long rate, struct module *owner)
  228. {
  229. struct v4l2_clk *clk;
  230. struct v4l2_clk_fixed *priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  231. if (!priv)
  232. return ERR_PTR(-ENOMEM);
  233. priv->rate = rate;
  234. priv->ops.get_rate = fixed_get_rate;
  235. priv->ops.owner = owner;
  236. clk = v4l2_clk_register(&priv->ops, dev_id, priv);
  237. if (IS_ERR(clk))
  238. kfree(priv);
  239. return clk;
  240. }
  241. EXPORT_SYMBOL(__v4l2_clk_register_fixed);
  242. void v4l2_clk_unregister_fixed(struct v4l2_clk *clk)
  243. {
  244. kfree(clk->priv);
  245. v4l2_clk_unregister(clk);
  246. }
  247. EXPORT_SYMBOL(v4l2_clk_unregister_fixed);