clock.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 asm/hardware/clock.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 (clk->enable)
  54. ret = clk->enable(clk);
  55. else if (arch_clock->clk_enable)
  56. ret = arch_clock->clk_enable(clk);
  57. else
  58. printk(KERN_ERR "Could not enable clock %s\n", clk->name);
  59. spin_unlock_irqrestore(&clockfw_lock, flags);
  60. return ret;
  61. }
  62. EXPORT_SYMBOL(clk_enable);
  63. void clk_disable(struct clk *clk)
  64. {
  65. unsigned long flags;
  66. spin_lock_irqsave(&clockfw_lock, flags);
  67. if (clk->disable)
  68. clk->disable(clk);
  69. else if (arch_clock->clk_disable)
  70. arch_clock->clk_disable(clk);
  71. else
  72. printk(KERN_ERR "Could not disable clock %s\n", clk->name);
  73. spin_unlock_irqrestore(&clockfw_lock, flags);
  74. }
  75. EXPORT_SYMBOL(clk_disable);
  76. int clk_use(struct clk *clk)
  77. {
  78. unsigned long flags;
  79. int ret = 0;
  80. spin_lock_irqsave(&clockfw_lock, flags);
  81. if (arch_clock->clk_use)
  82. ret = arch_clock->clk_use(clk);
  83. spin_unlock_irqrestore(&clockfw_lock, flags);
  84. return ret;
  85. }
  86. EXPORT_SYMBOL(clk_use);
  87. void clk_unuse(struct clk *clk)
  88. {
  89. unsigned long flags;
  90. spin_lock_irqsave(&clockfw_lock, flags);
  91. if (arch_clock->clk_unuse)
  92. arch_clock->clk_unuse(clk);
  93. spin_unlock_irqrestore(&clockfw_lock, flags);
  94. }
  95. EXPORT_SYMBOL(clk_unuse);
  96. int clk_get_usecount(struct clk *clk)
  97. {
  98. unsigned long flags;
  99. int ret = 0;
  100. spin_lock_irqsave(&clockfw_lock, flags);
  101. ret = clk->usecount;
  102. spin_unlock_irqrestore(&clockfw_lock, flags);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL(clk_get_usecount);
  106. unsigned long clk_get_rate(struct clk *clk)
  107. {
  108. unsigned long flags;
  109. unsigned long ret = 0;
  110. spin_lock_irqsave(&clockfw_lock, flags);
  111. ret = clk->rate;
  112. spin_unlock_irqrestore(&clockfw_lock, flags);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL(clk_get_rate);
  116. void clk_put(struct clk *clk)
  117. {
  118. if (clk && !IS_ERR(clk))
  119. module_put(clk->owner);
  120. }
  121. EXPORT_SYMBOL(clk_put);
  122. /*-------------------------------------------------------------------------
  123. * Optional clock functions defined in asm/hardware/clock.h
  124. *-------------------------------------------------------------------------*/
  125. long clk_round_rate(struct clk *clk, unsigned long rate)
  126. {
  127. unsigned long flags;
  128. long ret = 0;
  129. spin_lock_irqsave(&clockfw_lock, flags);
  130. if (arch_clock->clk_round_rate)
  131. ret = arch_clock->clk_round_rate(clk, rate);
  132. spin_unlock_irqrestore(&clockfw_lock, flags);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL(clk_round_rate);
  136. int clk_set_rate(struct clk *clk, unsigned long rate)
  137. {
  138. unsigned long flags;
  139. int ret = 0;
  140. spin_lock_irqsave(&clockfw_lock, flags);
  141. if (arch_clock->clk_set_rate)
  142. ret = arch_clock->clk_set_rate(clk, rate);
  143. spin_unlock_irqrestore(&clockfw_lock, flags);
  144. return ret;
  145. }
  146. EXPORT_SYMBOL(clk_set_rate);
  147. int clk_set_parent(struct clk *clk, struct clk *parent)
  148. {
  149. unsigned long flags;
  150. int ret = 0;
  151. spin_lock_irqsave(&clockfw_lock, flags);
  152. if (arch_clock->clk_set_parent)
  153. ret = arch_clock->clk_set_parent(clk, parent);
  154. spin_unlock_irqrestore(&clockfw_lock, flags);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL(clk_set_parent);
  158. struct clk *clk_get_parent(struct clk *clk)
  159. {
  160. unsigned long flags;
  161. struct clk * ret = NULL;
  162. spin_lock_irqsave(&clockfw_lock, flags);
  163. if (arch_clock->clk_get_parent)
  164. ret = arch_clock->clk_get_parent(clk);
  165. spin_unlock_irqrestore(&clockfw_lock, flags);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL(clk_get_parent);
  169. /*-------------------------------------------------------------------------
  170. * OMAP specific clock functions shared between omap1 and omap2
  171. *-------------------------------------------------------------------------*/
  172. unsigned int __initdata mpurate;
  173. /*
  174. * By default we use the rate set by the bootloader.
  175. * You can override this with mpurate= cmdline option.
  176. */
  177. static int __init omap_clk_setup(char *str)
  178. {
  179. get_option(&str, &mpurate);
  180. if (!mpurate)
  181. return 1;
  182. if (mpurate < 1000)
  183. mpurate *= 1000000;
  184. return 1;
  185. }
  186. __setup("mpurate=", omap_clk_setup);
  187. /* Used for clocks that always have same value as the parent clock */
  188. void followparent_recalc(struct clk *clk)
  189. {
  190. clk->rate = clk->parent->rate;
  191. }
  192. /* Propagate rate to children */
  193. void propagate_rate(struct clk * tclk)
  194. {
  195. struct clk *clkp;
  196. list_for_each_entry(clkp, &clocks, node) {
  197. if (likely(clkp->parent != tclk))
  198. continue;
  199. if (likely((u32)clkp->recalc))
  200. clkp->recalc(clkp);
  201. }
  202. }
  203. int clk_register(struct clk *clk)
  204. {
  205. mutex_lock(&clocks_mutex);
  206. list_add(&clk->node, &clocks);
  207. if (clk->init)
  208. clk->init(clk);
  209. mutex_unlock(&clocks_mutex);
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(clk_register);
  213. void clk_unregister(struct clk *clk)
  214. {
  215. mutex_lock(&clocks_mutex);
  216. list_del(&clk->node);
  217. mutex_unlock(&clocks_mutex);
  218. }
  219. EXPORT_SYMBOL(clk_unregister);
  220. void clk_deny_idle(struct clk *clk)
  221. {
  222. unsigned long flags;
  223. spin_lock_irqsave(&clockfw_lock, flags);
  224. if (arch_clock->clk_deny_idle)
  225. arch_clock->clk_deny_idle(clk);
  226. spin_unlock_irqrestore(&clockfw_lock, flags);
  227. }
  228. EXPORT_SYMBOL(clk_deny_idle);
  229. void clk_allow_idle(struct clk *clk)
  230. {
  231. unsigned long flags;
  232. spin_lock_irqsave(&clockfw_lock, flags);
  233. if (arch_clock->clk_allow_idle)
  234. arch_clock->clk_allow_idle(clk);
  235. spin_unlock_irqrestore(&clockfw_lock, flags);
  236. }
  237. EXPORT_SYMBOL(clk_allow_idle);
  238. /*-------------------------------------------------------------------------*/
  239. int __init clk_init(struct clk_functions * custom_clocks)
  240. {
  241. if (!custom_clocks) {
  242. printk(KERN_ERR "No custom clock functions registered\n");
  243. BUG();
  244. }
  245. arch_clock = custom_clocks;
  246. return 0;
  247. }