clock.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * linux/arch/arm/plat-omap/clock.c
  3. *
  4. * Copyright (C) 2004 - 2008 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/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/list.h>
  17. #include <linux/errno.h>
  18. #include <linux/err.h>
  19. #include <linux/string.h>
  20. #include <linux/clk.h>
  21. #include <linux/mutex.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/cpufreq.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/io.h>
  26. #include <mach/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. int clk_enable(struct clk *clk)
  35. {
  36. unsigned long flags;
  37. int ret = 0;
  38. if (clk == NULL || IS_ERR(clk))
  39. return -EINVAL;
  40. spin_lock_irqsave(&clockfw_lock, flags);
  41. if (arch_clock->clk_enable)
  42. ret = arch_clock->clk_enable(clk);
  43. spin_unlock_irqrestore(&clockfw_lock, flags);
  44. return ret;
  45. }
  46. EXPORT_SYMBOL(clk_enable);
  47. void clk_disable(struct clk *clk)
  48. {
  49. unsigned long flags;
  50. if (clk == NULL || IS_ERR(clk))
  51. return;
  52. spin_lock_irqsave(&clockfw_lock, flags);
  53. if (clk->usecount == 0) {
  54. printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
  55. clk->name);
  56. WARN_ON(1);
  57. goto out;
  58. }
  59. if (arch_clock->clk_disable)
  60. arch_clock->clk_disable(clk);
  61. out:
  62. spin_unlock_irqrestore(&clockfw_lock, flags);
  63. }
  64. EXPORT_SYMBOL(clk_disable);
  65. int clk_get_usecount(struct clk *clk)
  66. {
  67. unsigned long flags;
  68. int ret = 0;
  69. if (clk == NULL || IS_ERR(clk))
  70. return 0;
  71. spin_lock_irqsave(&clockfw_lock, flags);
  72. ret = clk->usecount;
  73. spin_unlock_irqrestore(&clockfw_lock, flags);
  74. return ret;
  75. }
  76. EXPORT_SYMBOL(clk_get_usecount);
  77. unsigned long clk_get_rate(struct clk *clk)
  78. {
  79. unsigned long flags;
  80. unsigned long ret = 0;
  81. if (clk == NULL || IS_ERR(clk))
  82. return 0;
  83. spin_lock_irqsave(&clockfw_lock, flags);
  84. ret = clk->rate;
  85. spin_unlock_irqrestore(&clockfw_lock, flags);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(clk_get_rate);
  89. /*-------------------------------------------------------------------------
  90. * Optional clock functions defined in include/linux/clk.h
  91. *-------------------------------------------------------------------------*/
  92. long clk_round_rate(struct clk *clk, unsigned long rate)
  93. {
  94. unsigned long flags;
  95. long ret = 0;
  96. if (clk == NULL || IS_ERR(clk))
  97. return ret;
  98. spin_lock_irqsave(&clockfw_lock, flags);
  99. if (arch_clock->clk_round_rate)
  100. ret = arch_clock->clk_round_rate(clk, rate);
  101. spin_unlock_irqrestore(&clockfw_lock, flags);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(clk_round_rate);
  105. int clk_set_rate(struct clk *clk, unsigned long rate)
  106. {
  107. unsigned long flags;
  108. int ret = -EINVAL;
  109. if (clk == NULL || IS_ERR(clk))
  110. return ret;
  111. spin_lock_irqsave(&clockfw_lock, flags);
  112. if (arch_clock->clk_set_rate)
  113. ret = arch_clock->clk_set_rate(clk, rate);
  114. if (ret == 0) {
  115. if (clk->recalc)
  116. clk->recalc(clk);
  117. propagate_rate(clk);
  118. }
  119. spin_unlock_irqrestore(&clockfw_lock, flags);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL(clk_set_rate);
  123. int clk_set_parent(struct clk *clk, struct clk *parent)
  124. {
  125. unsigned long flags;
  126. int ret = -EINVAL;
  127. if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
  128. return ret;
  129. spin_lock_irqsave(&clockfw_lock, flags);
  130. if (arch_clock->clk_set_parent)
  131. ret = arch_clock->clk_set_parent(clk, parent);
  132. if (ret == 0) {
  133. if (clk->recalc)
  134. clk->recalc(clk);
  135. propagate_rate(clk);
  136. }
  137. spin_unlock_irqrestore(&clockfw_lock, flags);
  138. return ret;
  139. }
  140. EXPORT_SYMBOL(clk_set_parent);
  141. struct clk *clk_get_parent(struct clk *clk)
  142. {
  143. return clk->parent;
  144. }
  145. EXPORT_SYMBOL(clk_get_parent);
  146. /*-------------------------------------------------------------------------
  147. * OMAP specific clock functions shared between omap1 and omap2
  148. *-------------------------------------------------------------------------*/
  149. unsigned int __initdata mpurate;
  150. /*
  151. * By default we use the rate set by the bootloader.
  152. * You can override this with mpurate= cmdline option.
  153. */
  154. static int __init omap_clk_setup(char *str)
  155. {
  156. get_option(&str, &mpurate);
  157. if (!mpurate)
  158. return 1;
  159. if (mpurate < 1000)
  160. mpurate *= 1000000;
  161. return 1;
  162. }
  163. __setup("mpurate=", omap_clk_setup);
  164. /* Used for clocks that always have same value as the parent clock */
  165. void followparent_recalc(struct clk *clk)
  166. {
  167. if (clk == NULL || IS_ERR(clk))
  168. return;
  169. clk->rate = clk->parent->rate;
  170. }
  171. void clk_reparent(struct clk *child, struct clk *parent)
  172. {
  173. list_del_init(&child->sibling);
  174. if (parent)
  175. list_add(&child->sibling, &parent->children);
  176. child->parent = parent;
  177. /* now do the debugfs renaming to reattach the child
  178. to the proper parent */
  179. }
  180. /* Propagate rate to children */
  181. void propagate_rate(struct clk * tclk)
  182. {
  183. struct clk *clkp;
  184. list_for_each_entry(clkp, &tclk->children, sibling) {
  185. if (clkp->recalc)
  186. clkp->recalc(clkp);
  187. propagate_rate(clkp);
  188. }
  189. }
  190. static LIST_HEAD(root_clks);
  191. /**
  192. * recalculate_root_clocks - recalculate and propagate all root clocks
  193. *
  194. * Recalculates all root clocks (clocks with no parent), which if the
  195. * clock's .recalc is set correctly, should also propagate their rates.
  196. * Called at init.
  197. */
  198. void recalculate_root_clocks(void)
  199. {
  200. struct clk *clkp;
  201. list_for_each_entry(clkp, &root_clks, sibling) {
  202. if (clkp->recalc)
  203. clkp->recalc(clkp);
  204. propagate_rate(clkp);
  205. }
  206. }
  207. void clk_init_one(struct clk *clk)
  208. {
  209. INIT_LIST_HEAD(&clk->children);
  210. }
  211. int clk_register(struct clk *clk)
  212. {
  213. if (clk == NULL || IS_ERR(clk))
  214. return -EINVAL;
  215. /*
  216. * trap out already registered clocks
  217. */
  218. if (clk->node.next || clk->node.prev)
  219. return 0;
  220. mutex_lock(&clocks_mutex);
  221. if (clk->parent)
  222. list_add(&clk->sibling, &clk->parent->children);
  223. else
  224. list_add(&clk->sibling, &root_clks);
  225. list_add(&clk->node, &clocks);
  226. if (clk->init)
  227. clk->init(clk);
  228. mutex_unlock(&clocks_mutex);
  229. return 0;
  230. }
  231. EXPORT_SYMBOL(clk_register);
  232. void clk_unregister(struct clk *clk)
  233. {
  234. if (clk == NULL || IS_ERR(clk))
  235. return;
  236. mutex_lock(&clocks_mutex);
  237. list_del(&clk->sibling);
  238. list_del(&clk->node);
  239. mutex_unlock(&clocks_mutex);
  240. }
  241. EXPORT_SYMBOL(clk_unregister);
  242. void clk_enable_init_clocks(void)
  243. {
  244. struct clk *clkp;
  245. list_for_each_entry(clkp, &clocks, node) {
  246. if (clkp->flags & ENABLE_ON_INIT)
  247. clk_enable(clkp);
  248. }
  249. }
  250. EXPORT_SYMBOL(clk_enable_init_clocks);
  251. /*
  252. * Low level helpers
  253. */
  254. static int clkll_enable_null(struct clk *clk)
  255. {
  256. return 0;
  257. }
  258. static void clkll_disable_null(struct clk *clk)
  259. {
  260. }
  261. const struct clkops clkops_null = {
  262. .enable = clkll_enable_null,
  263. .disable = clkll_disable_null,
  264. };
  265. #ifdef CONFIG_CPU_FREQ
  266. void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
  267. {
  268. unsigned long flags;
  269. spin_lock_irqsave(&clockfw_lock, flags);
  270. if (arch_clock->clk_init_cpufreq_table)
  271. arch_clock->clk_init_cpufreq_table(table);
  272. spin_unlock_irqrestore(&clockfw_lock, flags);
  273. }
  274. EXPORT_SYMBOL(clk_init_cpufreq_table);
  275. #endif
  276. /*-------------------------------------------------------------------------*/
  277. #ifdef CONFIG_OMAP_RESET_CLOCKS
  278. /*
  279. * Disable any unused clocks left on by the bootloader
  280. */
  281. static int __init clk_disable_unused(void)
  282. {
  283. struct clk *ck;
  284. unsigned long flags;
  285. list_for_each_entry(ck, &clocks, node) {
  286. if (ck->ops == &clkops_null)
  287. continue;
  288. if (ck->usecount > 0 || ck->enable_reg == 0)
  289. continue;
  290. spin_lock_irqsave(&clockfw_lock, flags);
  291. if (arch_clock->clk_disable_unused)
  292. arch_clock->clk_disable_unused(ck);
  293. spin_unlock_irqrestore(&clockfw_lock, flags);
  294. }
  295. return 0;
  296. }
  297. late_initcall(clk_disable_unused);
  298. #endif
  299. int __init clk_init(struct clk_functions * custom_clocks)
  300. {
  301. if (!custom_clocks) {
  302. printk(KERN_ERR "No custom clock functions registered\n");
  303. BUG();
  304. }
  305. arch_clock = custom_clocks;
  306. return 0;
  307. }
  308. #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
  309. /*
  310. * debugfs support to trace clock tree hierarchy and attributes
  311. */
  312. static struct dentry *clk_debugfs_root;
  313. static int clk_debugfs_register_one(struct clk *c)
  314. {
  315. int err;
  316. struct dentry *d, *child;
  317. struct clk *pa = c->parent;
  318. char s[255];
  319. char *p = s;
  320. p += sprintf(p, "%s", c->name);
  321. if (c->id != 0)
  322. sprintf(p, ":%d", c->id);
  323. d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
  324. if (!d)
  325. return -ENOMEM;
  326. c->dent = d;
  327. d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
  328. if (!d) {
  329. err = -ENOMEM;
  330. goto err_out;
  331. }
  332. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  333. if (!d) {
  334. err = -ENOMEM;
  335. goto err_out;
  336. }
  337. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  338. if (!d) {
  339. err = -ENOMEM;
  340. goto err_out;
  341. }
  342. return 0;
  343. err_out:
  344. d = c->dent;
  345. list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
  346. debugfs_remove(child);
  347. debugfs_remove(c->dent);
  348. return err;
  349. }
  350. static int clk_debugfs_register(struct clk *c)
  351. {
  352. int err;
  353. struct clk *pa = c->parent;
  354. if (pa && !pa->dent) {
  355. err = clk_debugfs_register(pa);
  356. if (err)
  357. return err;
  358. }
  359. if (!c->dent) {
  360. err = clk_debugfs_register_one(c);
  361. if (err)
  362. return err;
  363. }
  364. return 0;
  365. }
  366. static int __init clk_debugfs_init(void)
  367. {
  368. struct clk *c;
  369. struct dentry *d;
  370. int err;
  371. d = debugfs_create_dir("clock", NULL);
  372. if (!d)
  373. return -ENOMEM;
  374. clk_debugfs_root = d;
  375. list_for_each_entry(c, &clocks, node) {
  376. err = clk_debugfs_register(c);
  377. if (err)
  378. goto err_out;
  379. }
  380. return 0;
  381. err_out:
  382. debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
  383. return err;
  384. }
  385. late_initcall(clk_debugfs_init);
  386. #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */