clock.c 10.0 KB

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