clock.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <linux/cpufreq.h>
  25. #include <asm/io.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. if (clk->usecount == 0) {
  113. printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
  114. clk->name);
  115. WARN_ON(1);
  116. goto out;
  117. }
  118. if (arch_clock->clk_disable)
  119. arch_clock->clk_disable(clk);
  120. out:
  121. spin_unlock_irqrestore(&clockfw_lock, flags);
  122. }
  123. EXPORT_SYMBOL(clk_disable);
  124. int clk_get_usecount(struct clk *clk)
  125. {
  126. unsigned long flags;
  127. int ret = 0;
  128. if (clk == NULL || IS_ERR(clk))
  129. return 0;
  130. spin_lock_irqsave(&clockfw_lock, flags);
  131. ret = clk->usecount;
  132. spin_unlock_irqrestore(&clockfw_lock, flags);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL(clk_get_usecount);
  136. unsigned long clk_get_rate(struct clk *clk)
  137. {
  138. unsigned long flags;
  139. unsigned long ret = 0;
  140. if (clk == NULL || IS_ERR(clk))
  141. return 0;
  142. spin_lock_irqsave(&clockfw_lock, flags);
  143. ret = clk->rate;
  144. spin_unlock_irqrestore(&clockfw_lock, flags);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL(clk_get_rate);
  148. void clk_put(struct clk *clk)
  149. {
  150. if (clk && !IS_ERR(clk))
  151. module_put(clk->owner);
  152. }
  153. EXPORT_SYMBOL(clk_put);
  154. /*-------------------------------------------------------------------------
  155. * Optional clock functions defined in include/linux/clk.h
  156. *-------------------------------------------------------------------------*/
  157. long clk_round_rate(struct clk *clk, unsigned long rate)
  158. {
  159. unsigned long flags;
  160. long ret = 0;
  161. if (clk == NULL || IS_ERR(clk))
  162. return ret;
  163. spin_lock_irqsave(&clockfw_lock, flags);
  164. if (arch_clock->clk_round_rate)
  165. ret = arch_clock->clk_round_rate(clk, rate);
  166. spin_unlock_irqrestore(&clockfw_lock, flags);
  167. return ret;
  168. }
  169. EXPORT_SYMBOL(clk_round_rate);
  170. int clk_set_rate(struct clk *clk, unsigned long rate)
  171. {
  172. unsigned long flags;
  173. int ret = -EINVAL;
  174. if (clk == NULL || IS_ERR(clk))
  175. return ret;
  176. spin_lock_irqsave(&clockfw_lock, flags);
  177. if (arch_clock->clk_set_rate)
  178. ret = arch_clock->clk_set_rate(clk, rate);
  179. spin_unlock_irqrestore(&clockfw_lock, flags);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(clk_set_rate);
  183. int clk_set_parent(struct clk *clk, struct clk *parent)
  184. {
  185. unsigned long flags;
  186. int ret = -EINVAL;
  187. if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
  188. return ret;
  189. spin_lock_irqsave(&clockfw_lock, flags);
  190. if (arch_clock->clk_set_parent)
  191. ret = arch_clock->clk_set_parent(clk, parent);
  192. spin_unlock_irqrestore(&clockfw_lock, flags);
  193. return ret;
  194. }
  195. EXPORT_SYMBOL(clk_set_parent);
  196. struct clk *clk_get_parent(struct clk *clk)
  197. {
  198. unsigned long flags;
  199. struct clk * ret = NULL;
  200. if (clk == NULL || IS_ERR(clk))
  201. return ret;
  202. spin_lock_irqsave(&clockfw_lock, flags);
  203. if (arch_clock->clk_get_parent)
  204. ret = arch_clock->clk_get_parent(clk);
  205. spin_unlock_irqrestore(&clockfw_lock, flags);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL(clk_get_parent);
  209. /*-------------------------------------------------------------------------
  210. * OMAP specific clock functions shared between omap1 and omap2
  211. *-------------------------------------------------------------------------*/
  212. unsigned int __initdata mpurate;
  213. /*
  214. * By default we use the rate set by the bootloader.
  215. * You can override this with mpurate= cmdline option.
  216. */
  217. static int __init omap_clk_setup(char *str)
  218. {
  219. get_option(&str, &mpurate);
  220. if (!mpurate)
  221. return 1;
  222. if (mpurate < 1000)
  223. mpurate *= 1000000;
  224. return 1;
  225. }
  226. __setup("mpurate=", omap_clk_setup);
  227. /* Used for clocks that always have same value as the parent clock */
  228. void followparent_recalc(struct clk *clk)
  229. {
  230. if (clk == NULL || IS_ERR(clk))
  231. return;
  232. clk->rate = clk->parent->rate;
  233. if (unlikely(clk->flags & RATE_PROPAGATES))
  234. propagate_rate(clk);
  235. }
  236. /* Propagate rate to children */
  237. void propagate_rate(struct clk * tclk)
  238. {
  239. struct clk *clkp;
  240. if (tclk == NULL || IS_ERR(tclk))
  241. return;
  242. list_for_each_entry(clkp, &clocks, node) {
  243. if (likely(clkp->parent != tclk))
  244. continue;
  245. if (likely((u32)clkp->recalc))
  246. clkp->recalc(clkp);
  247. }
  248. }
  249. /**
  250. * recalculate_root_clocks - recalculate and propagate all root clocks
  251. *
  252. * Recalculates all root clocks (clocks with no parent), which if the
  253. * clock's .recalc is set correctly, should also propagate their rates.
  254. * Called at init.
  255. */
  256. void recalculate_root_clocks(void)
  257. {
  258. struct clk *clkp;
  259. list_for_each_entry(clkp, &clocks, node) {
  260. if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
  261. clkp->recalc(clkp);
  262. }
  263. }
  264. int clk_register(struct clk *clk)
  265. {
  266. if (clk == NULL || IS_ERR(clk))
  267. return -EINVAL;
  268. mutex_lock(&clocks_mutex);
  269. list_add(&clk->node, &clocks);
  270. if (clk->init)
  271. clk->init(clk);
  272. mutex_unlock(&clocks_mutex);
  273. return 0;
  274. }
  275. EXPORT_SYMBOL(clk_register);
  276. void clk_unregister(struct clk *clk)
  277. {
  278. if (clk == NULL || IS_ERR(clk))
  279. return;
  280. mutex_lock(&clocks_mutex);
  281. list_del(&clk->node);
  282. mutex_unlock(&clocks_mutex);
  283. }
  284. EXPORT_SYMBOL(clk_unregister);
  285. void clk_deny_idle(struct clk *clk)
  286. {
  287. unsigned long flags;
  288. if (clk == NULL || IS_ERR(clk))
  289. return;
  290. spin_lock_irqsave(&clockfw_lock, flags);
  291. if (arch_clock->clk_deny_idle)
  292. arch_clock->clk_deny_idle(clk);
  293. spin_unlock_irqrestore(&clockfw_lock, flags);
  294. }
  295. EXPORT_SYMBOL(clk_deny_idle);
  296. void clk_allow_idle(struct clk *clk)
  297. {
  298. unsigned long flags;
  299. if (clk == NULL || IS_ERR(clk))
  300. return;
  301. spin_lock_irqsave(&clockfw_lock, flags);
  302. if (arch_clock->clk_allow_idle)
  303. arch_clock->clk_allow_idle(clk);
  304. spin_unlock_irqrestore(&clockfw_lock, flags);
  305. }
  306. EXPORT_SYMBOL(clk_allow_idle);
  307. void clk_enable_init_clocks(void)
  308. {
  309. struct clk *clkp;
  310. list_for_each_entry(clkp, &clocks, node) {
  311. if (clkp->flags & ENABLE_ON_INIT)
  312. clk_enable(clkp);
  313. }
  314. }
  315. EXPORT_SYMBOL(clk_enable_init_clocks);
  316. #ifdef CONFIG_CPU_FREQ
  317. void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
  318. {
  319. unsigned long flags;
  320. spin_lock_irqsave(&clockfw_lock, flags);
  321. if (arch_clock->clk_init_cpufreq_table)
  322. arch_clock->clk_init_cpufreq_table(table);
  323. spin_unlock_irqrestore(&clockfw_lock, flags);
  324. }
  325. EXPORT_SYMBOL(clk_init_cpufreq_table);
  326. #endif
  327. /*-------------------------------------------------------------------------*/
  328. #ifdef CONFIG_OMAP_RESET_CLOCKS
  329. /*
  330. * Disable any unused clocks left on by the bootloader
  331. */
  332. static int __init clk_disable_unused(void)
  333. {
  334. struct clk *ck;
  335. unsigned long flags;
  336. list_for_each_entry(ck, &clocks, node) {
  337. if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
  338. ck->enable_reg == 0)
  339. continue;
  340. spin_lock_irqsave(&clockfw_lock, flags);
  341. if (arch_clock->clk_disable_unused)
  342. arch_clock->clk_disable_unused(ck);
  343. spin_unlock_irqrestore(&clockfw_lock, flags);
  344. }
  345. return 0;
  346. }
  347. late_initcall(clk_disable_unused);
  348. #endif
  349. int __init clk_init(struct clk_functions * custom_clocks)
  350. {
  351. if (!custom_clocks) {
  352. printk(KERN_ERR "No custom clock functions registered\n");
  353. BUG();
  354. }
  355. arch_clock = custom_clocks;
  356. return 0;
  357. }