clock.c 11 KB

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