clock.c 9.4 KB

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