clock.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. BUG_ON(clk->usecount == 0);
  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. #ifdef CONFIG_OMAP_RESET_CLOCKS
  258. /*
  259. * Disable any unused clocks left on by the bootloader
  260. */
  261. static int __init clk_disable_unused(void)
  262. {
  263. struct clk *ck;
  264. unsigned long flags;
  265. list_for_each_entry(ck, &clocks, node) {
  266. if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
  267. ck->enable_reg == 0)
  268. continue;
  269. spin_lock_irqsave(&clockfw_lock, flags);
  270. if (arch_clock->clk_disable_unused)
  271. arch_clock->clk_disable_unused(ck);
  272. spin_unlock_irqrestore(&clockfw_lock, flags);
  273. }
  274. return 0;
  275. }
  276. late_initcall(clk_disable_unused);
  277. #endif
  278. int __init clk_init(struct clk_functions * custom_clocks)
  279. {
  280. if (!custom_clocks) {
  281. printk(KERN_ERR "No custom clock functions registered\n");
  282. BUG();
  283. }
  284. arch_clock = custom_clocks;
  285. return 0;
  286. }