clock.c 10.0 KB

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