clock.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * arch/sh/kernel/cpu/clock.c - SuperH clock framework
  3. *
  4. * Copyright (C) 2005, 2006, 2007 Paul Mundt
  5. *
  6. * This clock framework is derived from the OMAP version by:
  7. *
  8. * Copyright (C) 2004 - 2005 Nokia Corporation
  9. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  10. *
  11. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  12. *
  13. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/list.h>
  22. #include <linux/kobject.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/err.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/proc_fs.h>
  28. #include <asm/clock.h>
  29. #include <asm/timer.h>
  30. static LIST_HEAD(clock_list);
  31. static DEFINE_SPINLOCK(clock_lock);
  32. static DEFINE_MUTEX(clock_list_sem);
  33. /*
  34. * Each subtype is expected to define the init routines for these clocks,
  35. * as each subtype (or processor family) will have these clocks at the
  36. * very least. These are all provided through the CPG, which even some of
  37. * the more quirky parts (such as ST40, SH4-202, etc.) still have.
  38. *
  39. * The processor-specific code is expected to register any additional
  40. * clock sources that are of interest.
  41. */
  42. static struct clk master_clk = {
  43. .name = "master_clk",
  44. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  45. .rate = CONFIG_SH_PCLK_FREQ,
  46. };
  47. static struct clk module_clk = {
  48. .name = "module_clk",
  49. .parent = &master_clk,
  50. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  51. };
  52. static struct clk bus_clk = {
  53. .name = "bus_clk",
  54. .parent = &master_clk,
  55. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  56. };
  57. static struct clk cpu_clk = {
  58. .name = "cpu_clk",
  59. .parent = &master_clk,
  60. .flags = CLK_ALWAYS_ENABLED,
  61. };
  62. /*
  63. * The ordering of these clocks matters, do not change it.
  64. */
  65. static struct clk *onchip_clocks[] = {
  66. &master_clk,
  67. &module_clk,
  68. &bus_clk,
  69. &cpu_clk,
  70. };
  71. /* Propagate rate to children */
  72. static void propagate_rate(struct clk *clk)
  73. {
  74. struct clk *clkp;
  75. list_for_each_entry(clkp, &clock_list, node) {
  76. if (likely(clkp->parent != clk))
  77. continue;
  78. if (likely(clkp->ops && clkp->ops->recalc))
  79. clkp->rate = clkp->ops->recalc(clkp);
  80. if (unlikely(clkp->flags & CLK_RATE_PROPAGATES))
  81. propagate_rate(clkp);
  82. }
  83. }
  84. static void __clk_init(struct clk *clk)
  85. {
  86. /*
  87. * See if this is the first time we're enabling the clock, some
  88. * clocks that are always enabled still require "special"
  89. * initialization. This is especially true if the clock mode
  90. * changes and the clock needs to hunt for the proper set of
  91. * divisors to use before it can effectively recalc.
  92. */
  93. if (clk->flags & CLK_NEEDS_INIT) {
  94. if (clk->ops && clk->ops->init)
  95. clk->ops->init(clk);
  96. clk->flags &= ~CLK_NEEDS_INIT;
  97. }
  98. }
  99. static int __clk_enable(struct clk *clk)
  100. {
  101. if (!clk)
  102. return -EINVAL;
  103. clk->usecount++;
  104. /* nothing to do if always enabled */
  105. if (clk->flags & CLK_ALWAYS_ENABLED)
  106. return 0;
  107. if (clk->usecount == 1) {
  108. __clk_init(clk);
  109. __clk_enable(clk->parent);
  110. if (clk->ops && clk->ops->enable)
  111. clk->ops->enable(clk);
  112. }
  113. return 0;
  114. }
  115. int clk_enable(struct clk *clk)
  116. {
  117. unsigned long flags;
  118. int ret;
  119. spin_lock_irqsave(&clock_lock, flags);
  120. ret = __clk_enable(clk);
  121. spin_unlock_irqrestore(&clock_lock, flags);
  122. return ret;
  123. }
  124. EXPORT_SYMBOL_GPL(clk_enable);
  125. static void __clk_disable(struct clk *clk)
  126. {
  127. if (!clk)
  128. return;
  129. clk->usecount--;
  130. WARN_ON(clk->usecount < 0);
  131. if (clk->flags & CLK_ALWAYS_ENABLED)
  132. return;
  133. if (clk->usecount == 0) {
  134. if (likely(clk->ops && clk->ops->disable))
  135. clk->ops->disable(clk);
  136. __clk_disable(clk->parent);
  137. }
  138. }
  139. void clk_disable(struct clk *clk)
  140. {
  141. unsigned long flags;
  142. spin_lock_irqsave(&clock_lock, flags);
  143. __clk_disable(clk);
  144. spin_unlock_irqrestore(&clock_lock, flags);
  145. }
  146. EXPORT_SYMBOL_GPL(clk_disable);
  147. int clk_register(struct clk *clk)
  148. {
  149. mutex_lock(&clock_list_sem);
  150. list_add(&clk->node, &clock_list);
  151. clk->usecount = 0;
  152. clk->flags |= CLK_NEEDS_INIT;
  153. mutex_unlock(&clock_list_sem);
  154. if (clk->flags & CLK_ALWAYS_ENABLED) {
  155. __clk_init(clk);
  156. pr_debug( "Clock '%s' is ALWAYS_ENABLED\n", clk->name);
  157. if (clk->ops && clk->ops->enable)
  158. clk->ops->enable(clk);
  159. pr_debug( "Enabled.");
  160. }
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(clk_register);
  164. void clk_unregister(struct clk *clk)
  165. {
  166. mutex_lock(&clock_list_sem);
  167. list_del(&clk->node);
  168. mutex_unlock(&clock_list_sem);
  169. }
  170. EXPORT_SYMBOL_GPL(clk_unregister);
  171. unsigned long clk_get_rate(struct clk *clk)
  172. {
  173. return clk->rate;
  174. }
  175. EXPORT_SYMBOL_GPL(clk_get_rate);
  176. int clk_set_rate(struct clk *clk, unsigned long rate)
  177. {
  178. return clk_set_rate_ex(clk, rate, 0);
  179. }
  180. EXPORT_SYMBOL_GPL(clk_set_rate);
  181. int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
  182. {
  183. int ret = -EOPNOTSUPP;
  184. if (likely(clk->ops && clk->ops->set_rate)) {
  185. unsigned long flags;
  186. spin_lock_irqsave(&clock_lock, flags);
  187. ret = clk->ops->set_rate(clk, rate, algo_id);
  188. spin_unlock_irqrestore(&clock_lock, flags);
  189. }
  190. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  191. propagate_rate(clk);
  192. return ret;
  193. }
  194. EXPORT_SYMBOL_GPL(clk_set_rate_ex);
  195. void clk_recalc_rate(struct clk *clk)
  196. {
  197. if (likely(clk->ops && clk->ops->recalc)) {
  198. unsigned long flags;
  199. spin_lock_irqsave(&clock_lock, flags);
  200. clk->rate = clk->ops->recalc(clk);
  201. spin_unlock_irqrestore(&clock_lock, flags);
  202. }
  203. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  204. propagate_rate(clk);
  205. }
  206. EXPORT_SYMBOL_GPL(clk_recalc_rate);
  207. int clk_set_parent(struct clk *clk, struct clk *parent)
  208. {
  209. int ret = -EINVAL;
  210. struct clk *old;
  211. if (!parent || !clk)
  212. return ret;
  213. old = clk->parent;
  214. if (likely(clk->ops && clk->ops->set_parent)) {
  215. unsigned long flags;
  216. spin_lock_irqsave(&clock_lock, flags);
  217. ret = clk->ops->set_parent(clk, parent);
  218. spin_unlock_irqrestore(&clock_lock, flags);
  219. clk->parent = (ret ? old : parent);
  220. }
  221. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  222. propagate_rate(clk);
  223. return ret;
  224. }
  225. EXPORT_SYMBOL_GPL(clk_set_parent);
  226. struct clk *clk_get_parent(struct clk *clk)
  227. {
  228. return clk->parent;
  229. }
  230. EXPORT_SYMBOL_GPL(clk_get_parent);
  231. long clk_round_rate(struct clk *clk, unsigned long rate)
  232. {
  233. if (likely(clk->ops && clk->ops->round_rate)) {
  234. unsigned long flags, rounded;
  235. spin_lock_irqsave(&clock_lock, flags);
  236. rounded = clk->ops->round_rate(clk, rate);
  237. spin_unlock_irqrestore(&clock_lock, flags);
  238. return rounded;
  239. }
  240. return clk_get_rate(clk);
  241. }
  242. EXPORT_SYMBOL_GPL(clk_round_rate);
  243. /*
  244. * Returns a clock. Note that we first try to use device id on the bus
  245. * and clock name. If this fails, we try to use clock name only.
  246. */
  247. struct clk *clk_get(struct device *dev, const char *id)
  248. {
  249. struct clk *p, *clk = ERR_PTR(-ENOENT);
  250. int idno;
  251. if (dev == NULL || dev->bus != &platform_bus_type)
  252. idno = -1;
  253. else
  254. idno = to_platform_device(dev)->id;
  255. mutex_lock(&clock_list_sem);
  256. list_for_each_entry(p, &clock_list, node) {
  257. if (p->id == idno &&
  258. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  259. clk = p;
  260. goto found;
  261. }
  262. }
  263. list_for_each_entry(p, &clock_list, node) {
  264. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  265. clk = p;
  266. break;
  267. }
  268. }
  269. found:
  270. mutex_unlock(&clock_list_sem);
  271. return clk;
  272. }
  273. EXPORT_SYMBOL_GPL(clk_get);
  274. void clk_put(struct clk *clk)
  275. {
  276. if (clk && !IS_ERR(clk))
  277. module_put(clk->owner);
  278. }
  279. EXPORT_SYMBOL_GPL(clk_put);
  280. void __init __attribute__ ((weak))
  281. arch_init_clk_ops(struct clk_ops **ops, int type)
  282. {
  283. }
  284. int __init __attribute__ ((weak))
  285. arch_clk_init(void)
  286. {
  287. return 0;
  288. }
  289. static int show_clocks(char *buf, char **start, off_t off,
  290. int len, int *eof, void *data)
  291. {
  292. struct clk *clk;
  293. char *p = buf;
  294. list_for_each_entry_reverse(clk, &clock_list, node) {
  295. unsigned long rate = clk_get_rate(clk);
  296. p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
  297. rate / 1000000, (rate % 1000000) / 10000,
  298. ((clk->flags & CLK_ALWAYS_ENABLED) ||
  299. clk->usecount > 0) ?
  300. "enabled" : "disabled");
  301. }
  302. return p - buf;
  303. }
  304. #ifdef CONFIG_PM
  305. static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
  306. {
  307. static pm_message_t prev_state;
  308. struct clk *clkp;
  309. switch (state.event) {
  310. case PM_EVENT_ON:
  311. /* Resumeing from hibernation */
  312. if (prev_state.event != PM_EVENT_FREEZE)
  313. break;
  314. list_for_each_entry(clkp, &clock_list, node) {
  315. if (likely(clkp->ops)) {
  316. unsigned long rate = clkp->rate;
  317. if (likely(clkp->ops->set_parent))
  318. clkp->ops->set_parent(clkp,
  319. clkp->parent);
  320. if (likely(clkp->ops->set_rate))
  321. clkp->ops->set_rate(clkp,
  322. rate, NO_CHANGE);
  323. else if (likely(clkp->ops->recalc))
  324. clkp->rate = clkp->ops->recalc(clkp);
  325. }
  326. }
  327. break;
  328. case PM_EVENT_FREEZE:
  329. break;
  330. case PM_EVENT_SUSPEND:
  331. break;
  332. }
  333. prev_state = state;
  334. return 0;
  335. }
  336. static int clks_sysdev_resume(struct sys_device *dev)
  337. {
  338. return clks_sysdev_suspend(dev, PMSG_ON);
  339. }
  340. static struct sysdev_class clks_sysdev_class = {
  341. .name = "clks",
  342. };
  343. static struct sysdev_driver clks_sysdev_driver = {
  344. .suspend = clks_sysdev_suspend,
  345. .resume = clks_sysdev_resume,
  346. };
  347. static struct sys_device clks_sysdev_dev = {
  348. .cls = &clks_sysdev_class,
  349. };
  350. static int __init clk_sysdev_init(void)
  351. {
  352. sysdev_class_register(&clks_sysdev_class);
  353. sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
  354. sysdev_register(&clks_sysdev_dev);
  355. return 0;
  356. }
  357. subsys_initcall(clk_sysdev_init);
  358. #endif
  359. int __init clk_init(void)
  360. {
  361. int i, ret = 0;
  362. BUG_ON(!master_clk.rate);
  363. for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
  364. struct clk *clk = onchip_clocks[i];
  365. arch_init_clk_ops(&clk->ops, i);
  366. ret |= clk_register(clk);
  367. }
  368. ret |= arch_clk_init();
  369. /* Kick the child clocks.. */
  370. propagate_rate(&master_clk);
  371. propagate_rate(&bus_clk);
  372. return ret;
  373. }
  374. static int __init clk_proc_init(void)
  375. {
  376. struct proc_dir_entry *p;
  377. p = create_proc_read_entry("clocks", S_IRUSR, NULL,
  378. show_clocks, NULL);
  379. if (unlikely(!p))
  380. return -EINVAL;
  381. return 0;
  382. }
  383. subsys_initcall(clk_proc_init);