clock.c 12 KB

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