clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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/io.h>
  24. #include <plat/clock.h>
  25. static LIST_HEAD(clocks);
  26. static DEFINE_MUTEX(clocks_mutex);
  27. static DEFINE_SPINLOCK(clockfw_lock);
  28. static struct clk_functions *arch_clock;
  29. /*
  30. * Standard clock functions defined in include/linux/clk.h
  31. */
  32. int clk_enable(struct clk *clk)
  33. {
  34. unsigned long flags;
  35. int ret;
  36. if (clk == NULL || IS_ERR(clk))
  37. return -EINVAL;
  38. if (!arch_clock || !arch_clock->clk_enable)
  39. return -EINVAL;
  40. spin_lock_irqsave(&clockfw_lock, flags);
  41. ret = arch_clock->clk_enable(clk);
  42. spin_unlock_irqrestore(&clockfw_lock, flags);
  43. return ret;
  44. }
  45. EXPORT_SYMBOL(clk_enable);
  46. void clk_disable(struct clk *clk)
  47. {
  48. unsigned long flags;
  49. if (clk == NULL || IS_ERR(clk))
  50. return;
  51. if (!arch_clock || !arch_clock->clk_disable)
  52. return;
  53. spin_lock_irqsave(&clockfw_lock, flags);
  54. if (clk->usecount == 0) {
  55. pr_err("Trying disable clock %s with 0 usecount\n",
  56. clk->name);
  57. WARN_ON(1);
  58. goto out;
  59. }
  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;
  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;
  84. if (clk == NULL || IS_ERR(clk))
  85. return 0;
  86. if (!arch_clock || !arch_clock->clk_round_rate)
  87. return 0;
  88. spin_lock_irqsave(&clockfw_lock, flags);
  89. ret = arch_clock->clk_round_rate(clk, rate);
  90. spin_unlock_irqrestore(&clockfw_lock, flags);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(clk_round_rate);
  94. int clk_set_rate(struct clk *clk, unsigned long rate)
  95. {
  96. unsigned long flags;
  97. int ret = -EINVAL;
  98. if (clk == NULL || IS_ERR(clk))
  99. return ret;
  100. if (!arch_clock || !arch_clock->clk_set_rate)
  101. return ret;
  102. spin_lock_irqsave(&clockfw_lock, flags);
  103. ret = arch_clock->clk_set_rate(clk, rate);
  104. if (ret == 0)
  105. propagate_rate(clk);
  106. spin_unlock_irqrestore(&clockfw_lock, flags);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL(clk_set_rate);
  110. int clk_set_parent(struct clk *clk, struct clk *parent)
  111. {
  112. unsigned long flags;
  113. int ret = -EINVAL;
  114. if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
  115. return ret;
  116. if (!arch_clock || !arch_clock->clk_set_parent)
  117. return ret;
  118. spin_lock_irqsave(&clockfw_lock, flags);
  119. if (clk->usecount == 0) {
  120. ret = arch_clock->clk_set_parent(clk, parent);
  121. if (ret == 0)
  122. propagate_rate(clk);
  123. } else
  124. ret = -EBUSY;
  125. spin_unlock_irqrestore(&clockfw_lock, flags);
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(clk_set_parent);
  129. struct clk *clk_get_parent(struct clk *clk)
  130. {
  131. return clk->parent;
  132. }
  133. EXPORT_SYMBOL(clk_get_parent);
  134. /*
  135. * OMAP specific clock functions shared between omap1 and omap2
  136. */
  137. int __initdata mpurate;
  138. /*
  139. * By default we use the rate set by the bootloader.
  140. * You can override this with mpurate= cmdline option.
  141. */
  142. static int __init omap_clk_setup(char *str)
  143. {
  144. get_option(&str, &mpurate);
  145. if (!mpurate)
  146. return 1;
  147. if (mpurate < 1000)
  148. mpurate *= 1000000;
  149. return 1;
  150. }
  151. __setup("mpurate=", omap_clk_setup);
  152. /* Used for clocks that always have same value as the parent clock */
  153. unsigned long followparent_recalc(struct clk *clk)
  154. {
  155. return clk->parent->rate;
  156. }
  157. /*
  158. * Used for clocks that have the same value as the parent clock,
  159. * divided by some factor
  160. */
  161. unsigned long omap_fixed_divisor_recalc(struct clk *clk)
  162. {
  163. WARN_ON(!clk->fixed_div);
  164. return clk->parent->rate / clk->fixed_div;
  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. /**
  253. * omap_clk_get_by_name - locate OMAP struct clk by its name
  254. * @name: name of the struct clk to locate
  255. *
  256. * Locate an OMAP struct clk by its name. Assumes that struct clk
  257. * names are unique. Returns NULL if not found or a pointer to the
  258. * struct clk if found.
  259. */
  260. struct clk *omap_clk_get_by_name(const char *name)
  261. {
  262. struct clk *c;
  263. struct clk *ret = NULL;
  264. mutex_lock(&clocks_mutex);
  265. list_for_each_entry(c, &clocks, node) {
  266. if (!strcmp(c->name, name)) {
  267. ret = c;
  268. break;
  269. }
  270. }
  271. mutex_unlock(&clocks_mutex);
  272. return ret;
  273. }
  274. int omap_clk_enable_autoidle_all(void)
  275. {
  276. struct clk *c;
  277. unsigned long flags;
  278. spin_lock_irqsave(&clockfw_lock, flags);
  279. list_for_each_entry(c, &clocks, node)
  280. if (c->ops->allow_idle)
  281. c->ops->allow_idle(c);
  282. spin_unlock_irqrestore(&clockfw_lock, flags);
  283. return 0;
  284. }
  285. int omap_clk_disable_autoidle_all(void)
  286. {
  287. struct clk *c;
  288. unsigned long flags;
  289. spin_lock_irqsave(&clockfw_lock, flags);
  290. list_for_each_entry(c, &clocks, node)
  291. if (c->ops->deny_idle)
  292. c->ops->deny_idle(c);
  293. spin_unlock_irqrestore(&clockfw_lock, flags);
  294. return 0;
  295. }
  296. /*
  297. * Low level helpers
  298. */
  299. static int clkll_enable_null(struct clk *clk)
  300. {
  301. return 0;
  302. }
  303. static void clkll_disable_null(struct clk *clk)
  304. {
  305. }
  306. const struct clkops clkops_null = {
  307. .enable = clkll_enable_null,
  308. .disable = clkll_disable_null,
  309. };
  310. /*
  311. * Dummy clock
  312. *
  313. * Used for clock aliases that are needed on some OMAPs, but not others
  314. */
  315. struct clk dummy_ck = {
  316. .name = "dummy",
  317. .ops = &clkops_null,
  318. };
  319. /*
  320. *
  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. if (!arch_clock || !arch_clock->clk_disable_unused)
  331. return 0;
  332. pr_info("clock: disabling unused clocks to save power\n");
  333. spin_lock_irqsave(&clockfw_lock, flags);
  334. list_for_each_entry(ck, &clocks, node) {
  335. if (ck->ops == &clkops_null)
  336. continue;
  337. if (ck->usecount > 0 || !ck->enable_reg)
  338. continue;
  339. arch_clock->clk_disable_unused(ck);
  340. }
  341. spin_unlock_irqrestore(&clockfw_lock, flags);
  342. return 0;
  343. }
  344. late_initcall(clk_disable_unused);
  345. late_initcall(omap_clk_enable_autoidle_all);
  346. #endif
  347. int __init clk_init(struct clk_functions * custom_clocks)
  348. {
  349. if (!custom_clocks) {
  350. pr_err("No custom clock functions registered\n");
  351. BUG();
  352. }
  353. arch_clock = custom_clocks;
  354. return 0;
  355. }
  356. #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
  357. /*
  358. * debugfs support to trace clock tree hierarchy and attributes
  359. */
  360. #include <linux/debugfs.h>
  361. #include <linux/seq_file.h>
  362. static struct dentry *clk_debugfs_root;
  363. static int clk_dbg_show_summary(struct seq_file *s, void *unused)
  364. {
  365. struct clk *c;
  366. struct clk *pa;
  367. seq_printf(s, "%-30s %-30s %-10s %s\n",
  368. "clock-name", "parent-name", "rate", "use-count");
  369. list_for_each_entry(c, &clocks, node) {
  370. pa = c->parent;
  371. seq_printf(s, "%-30s %-30s %-10lu %d\n",
  372. c->name, pa ? pa->name : "none", c->rate, c->usecount);
  373. }
  374. return 0;
  375. }
  376. static int clk_dbg_open(struct inode *inode, struct file *file)
  377. {
  378. return single_open(file, clk_dbg_show_summary, inode->i_private);
  379. }
  380. static const struct file_operations debug_clock_fops = {
  381. .open = clk_dbg_open,
  382. .read = seq_read,
  383. .llseek = seq_lseek,
  384. .release = single_release,
  385. };
  386. static int clk_debugfs_register_one(struct clk *c)
  387. {
  388. int err;
  389. struct dentry *d;
  390. struct clk *pa = c->parent;
  391. d = debugfs_create_dir(c->name, pa ? pa->dent : clk_debugfs_root);
  392. if (!d)
  393. return -ENOMEM;
  394. c->dent = d;
  395. d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
  396. if (!d) {
  397. err = -ENOMEM;
  398. goto err_out;
  399. }
  400. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  401. if (!d) {
  402. err = -ENOMEM;
  403. goto err_out;
  404. }
  405. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  406. if (!d) {
  407. err = -ENOMEM;
  408. goto err_out;
  409. }
  410. return 0;
  411. err_out:
  412. debugfs_remove_recursive(c->dent);
  413. return err;
  414. }
  415. static int clk_debugfs_register(struct clk *c)
  416. {
  417. int err;
  418. struct clk *pa = c->parent;
  419. if (pa && !pa->dent) {
  420. err = clk_debugfs_register(pa);
  421. if (err)
  422. return err;
  423. }
  424. if (!c->dent) {
  425. err = clk_debugfs_register_one(c);
  426. if (err)
  427. return err;
  428. }
  429. return 0;
  430. }
  431. static int __init clk_debugfs_init(void)
  432. {
  433. struct clk *c;
  434. struct dentry *d;
  435. int err;
  436. d = debugfs_create_dir("clock", NULL);
  437. if (!d)
  438. return -ENOMEM;
  439. clk_debugfs_root = d;
  440. list_for_each_entry(c, &clocks, node) {
  441. err = clk_debugfs_register(c);
  442. if (err)
  443. goto err_out;
  444. }
  445. d = debugfs_create_file("summary", S_IRUGO,
  446. d, NULL, &debug_clock_fops);
  447. if (!d)
  448. return -ENOMEM;
  449. return 0;
  450. err_out:
  451. debugfs_remove_recursive(clk_debugfs_root);
  452. return err;
  453. }
  454. late_initcall(clk_debugfs_init);
  455. #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */