clock.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * linux/arch/arm/plat-omap/clock.c
  3. *
  4. * Copyright (C) 2004 - 2005 Nokia corporation
  5. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  6. *
  7. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/version.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/list.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/string.h>
  21. #include <linux/clk.h>
  22. #include <linux/mutex.h>
  23. #include <linux/platform_device.h>
  24. #include <asm/io.h>
  25. #include <asm/semaphore.h>
  26. #include <asm/arch/clock.h>
  27. static LIST_HEAD(clocks);
  28. static DEFINE_MUTEX(clocks_mutex);
  29. static DEFINE_SPINLOCK(clockfw_lock);
  30. static struct clk_functions *arch_clock;
  31. /*-------------------------------------------------------------------------
  32. * Standard clock functions defined in include/linux/clk.h
  33. *-------------------------------------------------------------------------*/
  34. /*
  35. * Returns a clock. Note that we first try to use device id on the bus
  36. * and clock name. If this fails, we try to use clock name only.
  37. */
  38. struct clk * clk_get(struct device *dev, const char *id)
  39. {
  40. struct clk *p, *clk = ERR_PTR(-ENOENT);
  41. int idno;
  42. if (dev == NULL || dev->bus != &platform_bus_type)
  43. idno = -1;
  44. else
  45. idno = to_platform_device(dev)->id;
  46. mutex_lock(&clocks_mutex);
  47. list_for_each_entry(p, &clocks, node) {
  48. if (p->id == idno &&
  49. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  50. clk = p;
  51. goto found;
  52. }
  53. }
  54. list_for_each_entry(p, &clocks, node) {
  55. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  56. clk = p;
  57. break;
  58. }
  59. }
  60. found:
  61. mutex_unlock(&clocks_mutex);
  62. return clk;
  63. }
  64. EXPORT_SYMBOL(clk_get);
  65. int clk_enable(struct clk *clk)
  66. {
  67. unsigned long flags;
  68. int ret = 0;
  69. if (clk == NULL || IS_ERR(clk))
  70. return -EINVAL;
  71. spin_lock_irqsave(&clockfw_lock, flags);
  72. if (arch_clock->clk_enable)
  73. ret = arch_clock->clk_enable(clk);
  74. spin_unlock_irqrestore(&clockfw_lock, flags);
  75. return ret;
  76. }
  77. EXPORT_SYMBOL(clk_enable);
  78. void clk_disable(struct clk *clk)
  79. {
  80. unsigned long flags;
  81. if (clk == NULL || IS_ERR(clk))
  82. return;
  83. spin_lock_irqsave(&clockfw_lock, flags);
  84. if (arch_clock->clk_disable)
  85. arch_clock->clk_disable(clk);
  86. spin_unlock_irqrestore(&clockfw_lock, flags);
  87. }
  88. EXPORT_SYMBOL(clk_disable);
  89. int clk_get_usecount(struct clk *clk)
  90. {
  91. unsigned long flags;
  92. int ret = 0;
  93. if (clk == NULL || IS_ERR(clk))
  94. return 0;
  95. spin_lock_irqsave(&clockfw_lock, flags);
  96. ret = clk->usecount;
  97. spin_unlock_irqrestore(&clockfw_lock, flags);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL(clk_get_usecount);
  101. unsigned long clk_get_rate(struct clk *clk)
  102. {
  103. unsigned long flags;
  104. unsigned long ret = 0;
  105. if (clk == NULL || IS_ERR(clk))
  106. return 0;
  107. spin_lock_irqsave(&clockfw_lock, flags);
  108. ret = clk->rate;
  109. spin_unlock_irqrestore(&clockfw_lock, flags);
  110. return ret;
  111. }
  112. EXPORT_SYMBOL(clk_get_rate);
  113. void clk_put(struct clk *clk)
  114. {
  115. if (clk && !IS_ERR(clk))
  116. module_put(clk->owner);
  117. }
  118. EXPORT_SYMBOL(clk_put);
  119. /*-------------------------------------------------------------------------
  120. * Optional clock functions defined in include/linux/clk.h
  121. *-------------------------------------------------------------------------*/
  122. long clk_round_rate(struct clk *clk, unsigned long rate)
  123. {
  124. unsigned long flags;
  125. long ret = 0;
  126. if (clk == NULL || IS_ERR(clk))
  127. return ret;
  128. spin_lock_irqsave(&clockfw_lock, flags);
  129. if (arch_clock->clk_round_rate)
  130. ret = arch_clock->clk_round_rate(clk, rate);
  131. spin_unlock_irqrestore(&clockfw_lock, flags);
  132. return ret;
  133. }
  134. EXPORT_SYMBOL(clk_round_rate);
  135. int clk_set_rate(struct clk *clk, unsigned long rate)
  136. {
  137. unsigned long flags;
  138. int ret = -EINVAL;
  139. if (clk == NULL || IS_ERR(clk))
  140. return ret;
  141. spin_lock_irqsave(&clockfw_lock, flags);
  142. if (arch_clock->clk_set_rate)
  143. ret = arch_clock->clk_set_rate(clk, rate);
  144. spin_unlock_irqrestore(&clockfw_lock, flags);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL(clk_set_rate);
  148. int clk_set_parent(struct clk *clk, struct clk *parent)
  149. {
  150. unsigned long flags;
  151. int ret = -EINVAL;
  152. if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
  153. return ret;
  154. spin_lock_irqsave(&clockfw_lock, flags);
  155. if (arch_clock->clk_set_parent)
  156. ret = arch_clock->clk_set_parent(clk, parent);
  157. spin_unlock_irqrestore(&clockfw_lock, flags);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL(clk_set_parent);
  161. struct clk *clk_get_parent(struct clk *clk)
  162. {
  163. unsigned long flags;
  164. struct clk * ret = NULL;
  165. if (clk == NULL || IS_ERR(clk))
  166. return ret;
  167. spin_lock_irqsave(&clockfw_lock, flags);
  168. if (arch_clock->clk_get_parent)
  169. ret = arch_clock->clk_get_parent(clk);
  170. spin_unlock_irqrestore(&clockfw_lock, flags);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(clk_get_parent);
  174. /*-------------------------------------------------------------------------
  175. * OMAP specific clock functions shared between omap1 and omap2
  176. *-------------------------------------------------------------------------*/
  177. unsigned int __initdata mpurate;
  178. /*
  179. * By default we use the rate set by the bootloader.
  180. * You can override this with mpurate= cmdline option.
  181. */
  182. static int __init omap_clk_setup(char *str)
  183. {
  184. get_option(&str, &mpurate);
  185. if (!mpurate)
  186. return 1;
  187. if (mpurate < 1000)
  188. mpurate *= 1000000;
  189. return 1;
  190. }
  191. __setup("mpurate=", omap_clk_setup);
  192. /* Used for clocks that always have same value as the parent clock */
  193. void followparent_recalc(struct clk *clk)
  194. {
  195. if (clk == NULL || IS_ERR(clk))
  196. return;
  197. clk->rate = clk->parent->rate;
  198. }
  199. /* Propagate rate to children */
  200. void propagate_rate(struct clk * tclk)
  201. {
  202. struct clk *clkp;
  203. if (tclk == NULL || IS_ERR(tclk))
  204. return;
  205. list_for_each_entry(clkp, &clocks, node) {
  206. if (likely(clkp->parent != tclk))
  207. continue;
  208. if (likely((u32)clkp->recalc))
  209. clkp->recalc(clkp);
  210. }
  211. }
  212. int clk_register(struct clk *clk)
  213. {
  214. if (clk == NULL || IS_ERR(clk))
  215. return -EINVAL;
  216. mutex_lock(&clocks_mutex);
  217. list_add(&clk->node, &clocks);
  218. if (clk->init)
  219. clk->init(clk);
  220. mutex_unlock(&clocks_mutex);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(clk_register);
  224. void clk_unregister(struct clk *clk)
  225. {
  226. if (clk == NULL || IS_ERR(clk))
  227. return;
  228. mutex_lock(&clocks_mutex);
  229. list_del(&clk->node);
  230. mutex_unlock(&clocks_mutex);
  231. }
  232. EXPORT_SYMBOL(clk_unregister);
  233. void clk_deny_idle(struct clk *clk)
  234. {
  235. unsigned long flags;
  236. if (clk == NULL || IS_ERR(clk))
  237. return;
  238. spin_lock_irqsave(&clockfw_lock, flags);
  239. if (arch_clock->clk_deny_idle)
  240. arch_clock->clk_deny_idle(clk);
  241. spin_unlock_irqrestore(&clockfw_lock, flags);
  242. }
  243. EXPORT_SYMBOL(clk_deny_idle);
  244. void clk_allow_idle(struct clk *clk)
  245. {
  246. unsigned long flags;
  247. if (clk == NULL || IS_ERR(clk))
  248. return;
  249. spin_lock_irqsave(&clockfw_lock, flags);
  250. if (arch_clock->clk_allow_idle)
  251. arch_clock->clk_allow_idle(clk);
  252. spin_unlock_irqrestore(&clockfw_lock, flags);
  253. }
  254. EXPORT_SYMBOL(clk_allow_idle);
  255. /*-------------------------------------------------------------------------*/
  256. int __init clk_init(struct clk_functions * custom_clocks)
  257. {
  258. if (!custom_clocks) {
  259. printk(KERN_ERR "No custom clock functions registered\n");
  260. BUG();
  261. }
  262. arch_clock = custom_clocks;
  263. return 0;
  264. }