clock.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #ifdef CONFIG_PM_DEBUG
  32. static void print_parents(struct clk *clk)
  33. {
  34. struct clk *p;
  35. int printed = 0;
  36. list_for_each_entry(p, &clocks, node) {
  37. if (p->parent == clk && p->usecount) {
  38. if (!clk->usecount && !printed) {
  39. printk("MISMATCH: %s\n", clk->name);
  40. printed = 1;
  41. }
  42. printk("\t%-15s\n", p->name);
  43. }
  44. }
  45. }
  46. void clk_print_usecounts(void)
  47. {
  48. unsigned long flags;
  49. struct clk *p;
  50. spin_lock_irqsave(&clockfw_lock, flags);
  51. list_for_each_entry(p, &clocks, node) {
  52. if (p->usecount)
  53. printk("%-15s: %d\n", p->name, p->usecount);
  54. print_parents(p);
  55. }
  56. spin_unlock_irqrestore(&clockfw_lock, flags);
  57. }
  58. #endif
  59. /*-------------------------------------------------------------------------
  60. * Standard clock functions defined in include/linux/clk.h
  61. *-------------------------------------------------------------------------*/
  62. /*
  63. * Returns a clock. Note that we first try to use device id on the bus
  64. * and clock name. If this fails, we try to use clock name only.
  65. */
  66. struct clk * clk_get(struct device *dev, const char *id)
  67. {
  68. struct clk *p, *clk = ERR_PTR(-ENOENT);
  69. int idno;
  70. if (dev == NULL || dev->bus != &platform_bus_type)
  71. idno = -1;
  72. else
  73. idno = to_platform_device(dev)->id;
  74. mutex_lock(&clocks_mutex);
  75. list_for_each_entry(p, &clocks, node) {
  76. if (p->id == idno &&
  77. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  78. clk = p;
  79. goto found;
  80. }
  81. }
  82. list_for_each_entry(p, &clocks, node) {
  83. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  84. clk = p;
  85. break;
  86. }
  87. }
  88. found:
  89. mutex_unlock(&clocks_mutex);
  90. return clk;
  91. }
  92. EXPORT_SYMBOL(clk_get);
  93. int clk_enable(struct clk *clk)
  94. {
  95. unsigned long flags;
  96. int ret = 0;
  97. if (clk == NULL || IS_ERR(clk))
  98. return -EINVAL;
  99. spin_lock_irqsave(&clockfw_lock, flags);
  100. if (arch_clock->clk_enable)
  101. ret = arch_clock->clk_enable(clk);
  102. spin_unlock_irqrestore(&clockfw_lock, flags);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL(clk_enable);
  106. void clk_disable(struct clk *clk)
  107. {
  108. unsigned long flags;
  109. if (clk == NULL || IS_ERR(clk))
  110. return;
  111. spin_lock_irqsave(&clockfw_lock, flags);
  112. BUG_ON(clk->usecount == 0);
  113. if (arch_clock->clk_disable)
  114. arch_clock->clk_disable(clk);
  115. spin_unlock_irqrestore(&clockfw_lock, flags);
  116. }
  117. EXPORT_SYMBOL(clk_disable);
  118. int clk_get_usecount(struct clk *clk)
  119. {
  120. unsigned long flags;
  121. int ret = 0;
  122. if (clk == NULL || IS_ERR(clk))
  123. return 0;
  124. spin_lock_irqsave(&clockfw_lock, flags);
  125. ret = clk->usecount;
  126. spin_unlock_irqrestore(&clockfw_lock, flags);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL(clk_get_usecount);
  130. unsigned long clk_get_rate(struct clk *clk)
  131. {
  132. unsigned long flags;
  133. unsigned long ret = 0;
  134. if (clk == NULL || IS_ERR(clk))
  135. return 0;
  136. spin_lock_irqsave(&clockfw_lock, flags);
  137. ret = clk->rate;
  138. spin_unlock_irqrestore(&clockfw_lock, flags);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL(clk_get_rate);
  142. void clk_put(struct clk *clk)
  143. {
  144. if (clk && !IS_ERR(clk))
  145. module_put(clk->owner);
  146. }
  147. EXPORT_SYMBOL(clk_put);
  148. /*-------------------------------------------------------------------------
  149. * Optional clock functions defined in include/linux/clk.h
  150. *-------------------------------------------------------------------------*/
  151. long clk_round_rate(struct clk *clk, unsigned long rate)
  152. {
  153. unsigned long flags;
  154. long ret = 0;
  155. if (clk == NULL || IS_ERR(clk))
  156. return ret;
  157. spin_lock_irqsave(&clockfw_lock, flags);
  158. if (arch_clock->clk_round_rate)
  159. ret = arch_clock->clk_round_rate(clk, rate);
  160. spin_unlock_irqrestore(&clockfw_lock, flags);
  161. return ret;
  162. }
  163. EXPORT_SYMBOL(clk_round_rate);
  164. int clk_set_rate(struct clk *clk, unsigned long rate)
  165. {
  166. unsigned long flags;
  167. int ret = -EINVAL;
  168. if (clk == NULL || IS_ERR(clk))
  169. return ret;
  170. spin_lock_irqsave(&clockfw_lock, flags);
  171. if (arch_clock->clk_set_rate)
  172. ret = arch_clock->clk_set_rate(clk, rate);
  173. spin_unlock_irqrestore(&clockfw_lock, flags);
  174. return ret;
  175. }
  176. EXPORT_SYMBOL(clk_set_rate);
  177. int clk_set_parent(struct clk *clk, struct clk *parent)
  178. {
  179. unsigned long flags;
  180. int ret = -EINVAL;
  181. if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
  182. return ret;
  183. spin_lock_irqsave(&clockfw_lock, flags);
  184. if (arch_clock->clk_set_parent)
  185. ret = arch_clock->clk_set_parent(clk, parent);
  186. spin_unlock_irqrestore(&clockfw_lock, flags);
  187. return ret;
  188. }
  189. EXPORT_SYMBOL(clk_set_parent);
  190. struct clk *clk_get_parent(struct clk *clk)
  191. {
  192. unsigned long flags;
  193. struct clk * ret = NULL;
  194. if (clk == NULL || IS_ERR(clk))
  195. return ret;
  196. spin_lock_irqsave(&clockfw_lock, flags);
  197. if (arch_clock->clk_get_parent)
  198. ret = arch_clock->clk_get_parent(clk);
  199. spin_unlock_irqrestore(&clockfw_lock, flags);
  200. return ret;
  201. }
  202. EXPORT_SYMBOL(clk_get_parent);
  203. /*-------------------------------------------------------------------------
  204. * OMAP specific clock functions shared between omap1 and omap2
  205. *-------------------------------------------------------------------------*/
  206. unsigned int __initdata mpurate;
  207. /*
  208. * By default we use the rate set by the bootloader.
  209. * You can override this with mpurate= cmdline option.
  210. */
  211. static int __init omap_clk_setup(char *str)
  212. {
  213. get_option(&str, &mpurate);
  214. if (!mpurate)
  215. return 1;
  216. if (mpurate < 1000)
  217. mpurate *= 1000000;
  218. return 1;
  219. }
  220. __setup("mpurate=", omap_clk_setup);
  221. /* Used for clocks that always have same value as the parent clock */
  222. void followparent_recalc(struct clk *clk)
  223. {
  224. if (clk == NULL || IS_ERR(clk))
  225. return;
  226. clk->rate = clk->parent->rate;
  227. if (unlikely(clk->flags & RATE_PROPAGATES))
  228. propagate_rate(clk);
  229. }
  230. /* Propagate rate to children */
  231. void propagate_rate(struct clk * tclk)
  232. {
  233. struct clk *clkp;
  234. if (tclk == NULL || IS_ERR(tclk))
  235. return;
  236. list_for_each_entry(clkp, &clocks, node) {
  237. if (likely(clkp->parent != tclk))
  238. continue;
  239. if (likely((u32)clkp->recalc))
  240. clkp->recalc(clkp);
  241. }
  242. }
  243. int clk_register(struct clk *clk)
  244. {
  245. if (clk == NULL || IS_ERR(clk))
  246. return -EINVAL;
  247. mutex_lock(&clocks_mutex);
  248. list_add(&clk->node, &clocks);
  249. if (clk->init)
  250. clk->init(clk);
  251. mutex_unlock(&clocks_mutex);
  252. return 0;
  253. }
  254. EXPORT_SYMBOL(clk_register);
  255. void clk_unregister(struct clk *clk)
  256. {
  257. if (clk == NULL || IS_ERR(clk))
  258. return;
  259. mutex_lock(&clocks_mutex);
  260. list_del(&clk->node);
  261. mutex_unlock(&clocks_mutex);
  262. }
  263. EXPORT_SYMBOL(clk_unregister);
  264. void clk_deny_idle(struct clk *clk)
  265. {
  266. unsigned long flags;
  267. if (clk == NULL || IS_ERR(clk))
  268. return;
  269. spin_lock_irqsave(&clockfw_lock, flags);
  270. if (arch_clock->clk_deny_idle)
  271. arch_clock->clk_deny_idle(clk);
  272. spin_unlock_irqrestore(&clockfw_lock, flags);
  273. }
  274. EXPORT_SYMBOL(clk_deny_idle);
  275. void clk_allow_idle(struct clk *clk)
  276. {
  277. unsigned long flags;
  278. if (clk == NULL || IS_ERR(clk))
  279. return;
  280. spin_lock_irqsave(&clockfw_lock, flags);
  281. if (arch_clock->clk_allow_idle)
  282. arch_clock->clk_allow_idle(clk);
  283. spin_unlock_irqrestore(&clockfw_lock, flags);
  284. }
  285. EXPORT_SYMBOL(clk_allow_idle);
  286. /*-------------------------------------------------------------------------*/
  287. #ifdef CONFIG_OMAP_RESET_CLOCKS
  288. /*
  289. * Disable any unused clocks left on by the bootloader
  290. */
  291. static int __init clk_disable_unused(void)
  292. {
  293. struct clk *ck;
  294. unsigned long flags;
  295. list_for_each_entry(ck, &clocks, node) {
  296. if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
  297. ck->enable_reg == 0)
  298. continue;
  299. spin_lock_irqsave(&clockfw_lock, flags);
  300. if (arch_clock->clk_disable_unused)
  301. arch_clock->clk_disable_unused(ck);
  302. spin_unlock_irqrestore(&clockfw_lock, flags);
  303. }
  304. return 0;
  305. }
  306. late_initcall(clk_disable_unused);
  307. #endif
  308. int __init clk_init(struct clk_functions * custom_clocks)
  309. {
  310. if (!custom_clocks) {
  311. printk(KERN_ERR "No custom clock functions registered\n");
  312. BUG();
  313. }
  314. arch_clock = custom_clocks;
  315. return 0;
  316. }