clock.c 9.9 KB

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