clock.c 9.3 KB

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