clock.c 11 KB

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