clock.c 6.0 KB

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