clock.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/string.h>
  22. #include <asm/io.h>
  23. #include <asm/semaphore.h>
  24. #include <asm/hardware/clock.h>
  25. #include <asm/arch/clock.h>
  26. LIST_HEAD(clocks);
  27. static DECLARE_MUTEX(clocks_sem);
  28. DEFINE_SPINLOCK(clockfw_lock);
  29. static struct clk_functions *arch_clock;
  30. /*-------------------------------------------------------------------------
  31. * Standard clock functions defined in asm/hardware/clock.h
  32. *-------------------------------------------------------------------------*/
  33. struct clk * clk_get(struct device *dev, const char *id)
  34. {
  35. struct clk *p, *clk = ERR_PTR(-ENOENT);
  36. down(&clocks_sem);
  37. list_for_each_entry(p, &clocks, node) {
  38. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  39. clk = p;
  40. break;
  41. }
  42. }
  43. up(&clocks_sem);
  44. return clk;
  45. }
  46. EXPORT_SYMBOL(clk_get);
  47. int clk_enable(struct clk *clk)
  48. {
  49. unsigned long flags;
  50. int ret = 0;
  51. spin_lock_irqsave(&clockfw_lock, flags);
  52. if (clk->enable)
  53. ret = clk->enable(clk);
  54. else if (arch_clock->clk_enable)
  55. ret = arch_clock->clk_enable(clk);
  56. else
  57. printk(KERN_ERR "Could not enable clock %s\n", clk->name);
  58. spin_unlock_irqrestore(&clockfw_lock, flags);
  59. return ret;
  60. }
  61. EXPORT_SYMBOL(clk_enable);
  62. void clk_disable(struct clk *clk)
  63. {
  64. unsigned long flags;
  65. spin_lock_irqsave(&clockfw_lock, flags);
  66. if (clk->disable)
  67. clk->disable(clk);
  68. else if (arch_clock->clk_disable)
  69. arch_clock->clk_disable(clk);
  70. else
  71. printk(KERN_ERR "Could not disable clock %s\n", clk->name);
  72. spin_unlock_irqrestore(&clockfw_lock, flags);
  73. }
  74. EXPORT_SYMBOL(clk_disable);
  75. int clk_use(struct clk *clk)
  76. {
  77. unsigned long flags;
  78. int ret = 0;
  79. spin_lock_irqsave(&clockfw_lock, flags);
  80. if (arch_clock->clk_use)
  81. ret = arch_clock->clk_use(clk);
  82. spin_unlock_irqrestore(&clockfw_lock, flags);
  83. return ret;
  84. }
  85. EXPORT_SYMBOL(clk_use);
  86. void clk_unuse(struct clk *clk)
  87. {
  88. unsigned long flags;
  89. spin_lock_irqsave(&clockfw_lock, flags);
  90. if (arch_clock->clk_unuse)
  91. arch_clock->clk_unuse(clk);
  92. spin_unlock_irqrestore(&clockfw_lock, flags);
  93. }
  94. EXPORT_SYMBOL(clk_unuse);
  95. int clk_get_usecount(struct clk *clk)
  96. {
  97. unsigned long flags;
  98. int ret = 0;
  99. spin_lock_irqsave(&clockfw_lock, flags);
  100. ret = clk->usecount;
  101. spin_unlock_irqrestore(&clockfw_lock, flags);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(clk_get_usecount);
  105. unsigned long clk_get_rate(struct clk *clk)
  106. {
  107. unsigned long flags;
  108. unsigned long ret = 0;
  109. spin_lock_irqsave(&clockfw_lock, flags);
  110. ret = clk->rate;
  111. spin_unlock_irqrestore(&clockfw_lock, flags);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL(clk_get_rate);
  115. void clk_put(struct clk *clk)
  116. {
  117. if (clk && !IS_ERR(clk))
  118. module_put(clk->owner);
  119. }
  120. EXPORT_SYMBOL(clk_put);
  121. /*-------------------------------------------------------------------------
  122. * Optional clock functions defined in asm/hardware/clock.h
  123. *-------------------------------------------------------------------------*/
  124. long clk_round_rate(struct clk *clk, unsigned long rate)
  125. {
  126. unsigned long flags;
  127. long ret = 0;
  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 = 0;
  139. spin_lock_irqsave(&clockfw_lock, flags);
  140. if (arch_clock->clk_set_rate)
  141. ret = arch_clock->clk_set_rate(clk, rate);
  142. spin_unlock_irqrestore(&clockfw_lock, flags);
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(clk_set_rate);
  146. int clk_set_parent(struct clk *clk, struct clk *parent)
  147. {
  148. unsigned long flags;
  149. int ret = 0;
  150. spin_lock_irqsave(&clockfw_lock, flags);
  151. if (arch_clock->clk_set_parent)
  152. ret = arch_clock->clk_set_parent(clk, parent);
  153. spin_unlock_irqrestore(&clockfw_lock, flags);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL(clk_set_parent);
  157. struct clk *clk_get_parent(struct clk *clk)
  158. {
  159. unsigned long flags;
  160. struct clk * ret = NULL;
  161. spin_lock_irqsave(&clockfw_lock, flags);
  162. if (arch_clock->clk_get_parent)
  163. ret = arch_clock->clk_get_parent(clk);
  164. spin_unlock_irqrestore(&clockfw_lock, flags);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(clk_get_parent);
  168. /*-------------------------------------------------------------------------
  169. * OMAP specific clock functions shared between omap1 and omap2
  170. *-------------------------------------------------------------------------*/
  171. unsigned int __initdata mpurate;
  172. /*
  173. * By default we use the rate set by the bootloader.
  174. * You can override this with mpurate= cmdline option.
  175. */
  176. static int __init omap_clk_setup(char *str)
  177. {
  178. get_option(&str, &mpurate);
  179. if (!mpurate)
  180. return 1;
  181. if (mpurate < 1000)
  182. mpurate *= 1000000;
  183. return 1;
  184. }
  185. __setup("mpurate=", omap_clk_setup);
  186. /* Used for clocks that always have same value as the parent clock */
  187. void followparent_recalc(struct clk *clk)
  188. {
  189. clk->rate = clk->parent->rate;
  190. }
  191. /* Propagate rate to children */
  192. void propagate_rate(struct clk * tclk)
  193. {
  194. struct clk *clkp;
  195. list_for_each_entry(clkp, &clocks, node) {
  196. if (likely(clkp->parent != tclk))
  197. continue;
  198. if (likely((u32)clkp->recalc))
  199. clkp->recalc(clkp);
  200. }
  201. }
  202. int clk_register(struct clk *clk)
  203. {
  204. down(&clocks_sem);
  205. list_add(&clk->node, &clocks);
  206. if (clk->init)
  207. clk->init(clk);
  208. up(&clocks_sem);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(clk_register);
  212. void clk_unregister(struct clk *clk)
  213. {
  214. down(&clocks_sem);
  215. list_del(&clk->node);
  216. up(&clocks_sem);
  217. }
  218. EXPORT_SYMBOL(clk_unregister);
  219. void clk_deny_idle(struct clk *clk)
  220. {
  221. unsigned long flags;
  222. spin_lock_irqsave(&clockfw_lock, flags);
  223. if (arch_clock->clk_deny_idle)
  224. arch_clock->clk_deny_idle(clk);
  225. spin_unlock_irqrestore(&clockfw_lock, flags);
  226. }
  227. EXPORT_SYMBOL(clk_deny_idle);
  228. void clk_allow_idle(struct clk *clk)
  229. {
  230. unsigned long flags;
  231. spin_lock_irqsave(&clockfw_lock, flags);
  232. if (arch_clock->clk_allow_idle)
  233. arch_clock->clk_allow_idle(clk);
  234. spin_unlock_irqrestore(&clockfw_lock, flags);
  235. }
  236. EXPORT_SYMBOL(clk_allow_idle);
  237. /*-------------------------------------------------------------------------*/
  238. int __init clk_init(struct clk_functions * custom_clocks)
  239. {
  240. if (!custom_clocks) {
  241. printk(KERN_ERR "No custom clock functions registered\n");
  242. BUG();
  243. }
  244. arch_clock = custom_clocks;
  245. return 0;
  246. }