v4l2-clk.c 6.1 KB

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