clock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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/err.h>
  18. #include <linux/string.h>
  19. #include <linux/clk.h>
  20. #include <linux/mutex.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/debugfs.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 = 0;
  36. if (clk == NULL || IS_ERR(clk))
  37. return -EINVAL;
  38. spin_lock_irqsave(&clockfw_lock, flags);
  39. if (arch_clock->clk_enable)
  40. ret = arch_clock->clk_enable(clk);
  41. spin_unlock_irqrestore(&clockfw_lock, flags);
  42. return ret;
  43. }
  44. EXPORT_SYMBOL(clk_enable);
  45. void clk_disable(struct clk *clk)
  46. {
  47. unsigned long flags;
  48. if (clk == NULL || IS_ERR(clk))
  49. return;
  50. spin_lock_irqsave(&clockfw_lock, flags);
  51. if (clk->usecount == 0) {
  52. pr_err("Trying disable clock %s with 0 usecount\n",
  53. clk->name);
  54. WARN_ON(1);
  55. goto out;
  56. }
  57. if (arch_clock->clk_disable)
  58. arch_clock->clk_disable(clk);
  59. out:
  60. spin_unlock_irqrestore(&clockfw_lock, flags);
  61. }
  62. EXPORT_SYMBOL(clk_disable);
  63. unsigned long clk_get_rate(struct clk *clk)
  64. {
  65. unsigned long flags;
  66. unsigned long ret = 0;
  67. if (clk == NULL || IS_ERR(clk))
  68. return 0;
  69. spin_lock_irqsave(&clockfw_lock, flags);
  70. ret = clk->rate;
  71. spin_unlock_irqrestore(&clockfw_lock, flags);
  72. return ret;
  73. }
  74. EXPORT_SYMBOL(clk_get_rate);
  75. /*
  76. * Optional clock functions defined in include/linux/clk.h
  77. */
  78. long clk_round_rate(struct clk *clk, unsigned long rate)
  79. {
  80. unsigned long flags;
  81. long ret = 0;
  82. if (clk == NULL || IS_ERR(clk))
  83. return ret;
  84. spin_lock_irqsave(&clockfw_lock, flags);
  85. if (arch_clock->clk_round_rate)
  86. ret = arch_clock->clk_round_rate(clk, rate);
  87. spin_unlock_irqrestore(&clockfw_lock, flags);
  88. return ret;
  89. }
  90. EXPORT_SYMBOL(clk_round_rate);
  91. int clk_set_rate(struct clk *clk, unsigned long rate)
  92. {
  93. unsigned long flags;
  94. int ret = -EINVAL;
  95. if (clk == NULL || IS_ERR(clk))
  96. return ret;
  97. spin_lock_irqsave(&clockfw_lock, flags);
  98. if (arch_clock->clk_set_rate)
  99. ret = arch_clock->clk_set_rate(clk, rate);
  100. if (ret == 0) {
  101. if (clk->recalc)
  102. clk->rate = clk->recalc(clk);
  103. propagate_rate(clk);
  104. }
  105. spin_unlock_irqrestore(&clockfw_lock, flags);
  106. return ret;
  107. }
  108. EXPORT_SYMBOL(clk_set_rate);
  109. int clk_set_parent(struct clk *clk, struct clk *parent)
  110. {
  111. unsigned long flags;
  112. int ret = -EINVAL;
  113. if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
  114. return ret;
  115. spin_lock_irqsave(&clockfw_lock, flags);
  116. if (clk->usecount == 0) {
  117. if (arch_clock->clk_set_parent)
  118. ret = arch_clock->clk_set_parent(clk, parent);
  119. if (ret == 0) {
  120. if (clk->recalc)
  121. clk->rate = clk->recalc(clk);
  122. propagate_rate(clk);
  123. }
  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. /*
  276. * Low level helpers
  277. */
  278. static int clkll_enable_null(struct clk *clk)
  279. {
  280. return 0;
  281. }
  282. static void clkll_disable_null(struct clk *clk)
  283. {
  284. }
  285. const struct clkops clkops_null = {
  286. .enable = clkll_enable_null,
  287. .disable = clkll_disable_null,
  288. };
  289. /*
  290. * Dummy clock
  291. *
  292. * Used for clock aliases that are needed on some OMAPs, but not others
  293. */
  294. struct clk dummy_ck = {
  295. .name = "dummy",
  296. .ops = &clkops_null,
  297. };
  298. #ifdef CONFIG_CPU_FREQ
  299. void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
  300. {
  301. unsigned long flags;
  302. spin_lock_irqsave(&clockfw_lock, flags);
  303. if (arch_clock->clk_init_cpufreq_table)
  304. arch_clock->clk_init_cpufreq_table(table);
  305. spin_unlock_irqrestore(&clockfw_lock, flags);
  306. }
  307. void clk_exit_cpufreq_table(struct cpufreq_frequency_table **table)
  308. {
  309. unsigned long flags;
  310. spin_lock_irqsave(&clockfw_lock, flags);
  311. if (arch_clock->clk_exit_cpufreq_table)
  312. arch_clock->clk_exit_cpufreq_table(table);
  313. spin_unlock_irqrestore(&clockfw_lock, flags);
  314. }
  315. #endif
  316. /*
  317. *
  318. */
  319. #ifdef CONFIG_OMAP_RESET_CLOCKS
  320. /*
  321. * Disable any unused clocks left on by the bootloader
  322. */
  323. static int __init clk_disable_unused(void)
  324. {
  325. struct clk *ck;
  326. unsigned long flags;
  327. pr_info("clock: disabling unused clocks to save power\n");
  328. list_for_each_entry(ck, &clocks, node) {
  329. if (ck->ops == &clkops_null)
  330. continue;
  331. if (ck->usecount > 0 || !ck->enable_reg)
  332. continue;
  333. spin_lock_irqsave(&clockfw_lock, flags);
  334. if (arch_clock->clk_disable_unused)
  335. arch_clock->clk_disable_unused(ck);
  336. spin_unlock_irqrestore(&clockfw_lock, flags);
  337. }
  338. return 0;
  339. }
  340. late_initcall(clk_disable_unused);
  341. #endif
  342. int __init clk_init(struct clk_functions * custom_clocks)
  343. {
  344. if (!custom_clocks) {
  345. pr_err("No custom clock functions registered\n");
  346. BUG();
  347. }
  348. arch_clock = custom_clocks;
  349. return 0;
  350. }
  351. #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
  352. /*
  353. * debugfs support to trace clock tree hierarchy and attributes
  354. */
  355. static struct dentry *clk_debugfs_root;
  356. static int clk_debugfs_register_one(struct clk *c)
  357. {
  358. int err;
  359. struct dentry *d, *child, *child_tmp;
  360. struct clk *pa = c->parent;
  361. char s[255];
  362. char *p = s;
  363. p += sprintf(p, "%s", c->name);
  364. d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
  365. if (!d)
  366. return -ENOMEM;
  367. c->dent = d;
  368. d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
  369. if (!d) {
  370. err = -ENOMEM;
  371. goto err_out;
  372. }
  373. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  374. if (!d) {
  375. err = -ENOMEM;
  376. goto err_out;
  377. }
  378. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  379. if (!d) {
  380. err = -ENOMEM;
  381. goto err_out;
  382. }
  383. return 0;
  384. err_out:
  385. d = c->dent;
  386. list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
  387. debugfs_remove(child);
  388. debugfs_remove(c->dent);
  389. return err;
  390. }
  391. static int clk_debugfs_register(struct clk *c)
  392. {
  393. int err;
  394. struct clk *pa = c->parent;
  395. if (pa && !pa->dent) {
  396. err = clk_debugfs_register(pa);
  397. if (err)
  398. return err;
  399. }
  400. if (!c->dent) {
  401. err = clk_debugfs_register_one(c);
  402. if (err)
  403. return err;
  404. }
  405. return 0;
  406. }
  407. static int __init clk_debugfs_init(void)
  408. {
  409. struct clk *c;
  410. struct dentry *d;
  411. int err;
  412. d = debugfs_create_dir("clock", NULL);
  413. if (!d)
  414. return -ENOMEM;
  415. clk_debugfs_root = d;
  416. list_for_each_entry(c, &clocks, node) {
  417. err = clk_debugfs_register(c);
  418. if (err)
  419. goto err_out;
  420. }
  421. return 0;
  422. err_out:
  423. debugfs_remove_recursive(clk_debugfs_root);
  424. return err;
  425. }
  426. late_initcall(clk_debugfs_init);
  427. #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */