clock.c 7.1 KB

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