clock.c 10 KB

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