clock.c 11 KB

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