clock.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/seq_file.h>
  24. #include <linux/err.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/proc_fs.h>
  27. #include <asm/clock.h>
  28. #include <asm/timer.h>
  29. static LIST_HEAD(clock_list);
  30. static DEFINE_SPINLOCK(clock_lock);
  31. static DEFINE_MUTEX(clock_list_sem);
  32. /*
  33. * Each subtype is expected to define the init routines for these clocks,
  34. * as each subtype (or processor family) will have these clocks at the
  35. * very least. These are all provided through the CPG, which even some of
  36. * the more quirky parts (such as ST40, SH4-202, etc.) still have.
  37. *
  38. * The processor-specific code is expected to register any additional
  39. * clock sources that are of interest.
  40. */
  41. static struct clk master_clk = {
  42. .name = "master_clk",
  43. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  44. .rate = CONFIG_SH_PCLK_FREQ,
  45. };
  46. static struct clk module_clk = {
  47. .name = "module_clk",
  48. .parent = &master_clk,
  49. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  50. };
  51. static struct clk bus_clk = {
  52. .name = "bus_clk",
  53. .parent = &master_clk,
  54. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  55. };
  56. static struct clk cpu_clk = {
  57. .name = "cpu_clk",
  58. .parent = &master_clk,
  59. .flags = CLK_ALWAYS_ENABLED,
  60. };
  61. /*
  62. * The ordering of these clocks matters, do not change it.
  63. */
  64. static struct clk *onchip_clocks[] = {
  65. &master_clk,
  66. &module_clk,
  67. &bus_clk,
  68. &cpu_clk,
  69. };
  70. static void propagate_rate(struct clk *clk)
  71. {
  72. struct clk *clkp;
  73. list_for_each_entry(clkp, &clock_list, node) {
  74. if (likely(clkp->parent != clk))
  75. continue;
  76. if (likely(clkp->ops && clkp->ops->recalc))
  77. clkp->ops->recalc(clkp);
  78. }
  79. }
  80. int __clk_enable(struct clk *clk)
  81. {
  82. /*
  83. * See if this is the first time we're enabling the clock, some
  84. * clocks that are always enabled still require "special"
  85. * initialization. This is especially true if the clock mode
  86. * changes and the clock needs to hunt for the proper set of
  87. * divisors to use before it can effectively recalc.
  88. */
  89. if (unlikely(atomic_read(&clk->kref.refcount) == 1))
  90. if (clk->ops && clk->ops->init)
  91. clk->ops->init(clk);
  92. kref_get(&clk->kref);
  93. if (clk->flags & CLK_ALWAYS_ENABLED)
  94. return 0;
  95. if (likely(clk->ops && clk->ops->enable))
  96. clk->ops->enable(clk);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL_GPL(__clk_enable);
  100. int clk_enable(struct clk *clk)
  101. {
  102. unsigned long flags;
  103. int ret;
  104. spin_lock_irqsave(&clock_lock, flags);
  105. ret = __clk_enable(clk);
  106. spin_unlock_irqrestore(&clock_lock, flags);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL_GPL(clk_enable);
  110. static void clk_kref_release(struct kref *kref)
  111. {
  112. /* Nothing to do */
  113. }
  114. void __clk_disable(struct clk *clk)
  115. {
  116. int count = kref_put(&clk->kref, clk_kref_release);
  117. if (clk->flags & CLK_ALWAYS_ENABLED)
  118. return;
  119. if (!count) { /* count reaches zero, disable the clock */
  120. if (likely(clk->ops && clk->ops->disable))
  121. clk->ops->disable(clk);
  122. }
  123. }
  124. EXPORT_SYMBOL_GPL(__clk_disable);
  125. void clk_disable(struct clk *clk)
  126. {
  127. unsigned long flags;
  128. spin_lock_irqsave(&clock_lock, flags);
  129. __clk_disable(clk);
  130. spin_unlock_irqrestore(&clock_lock, flags);
  131. }
  132. EXPORT_SYMBOL_GPL(clk_disable);
  133. int clk_register(struct clk *clk)
  134. {
  135. mutex_lock(&clock_list_sem);
  136. list_add(&clk->node, &clock_list);
  137. kref_init(&clk->kref);
  138. mutex_unlock(&clock_list_sem);
  139. if (clk->flags & CLK_ALWAYS_ENABLED) {
  140. pr_debug( "Clock '%s' is ALWAYS_ENABLED\n", clk->name);
  141. if (clk->ops && clk->ops->init)
  142. clk->ops->init(clk);
  143. if (clk->ops && clk->ops->enable)
  144. clk->ops->enable(clk);
  145. pr_debug( "Enabled.");
  146. }
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(clk_register);
  150. void clk_unregister(struct clk *clk)
  151. {
  152. mutex_lock(&clock_list_sem);
  153. list_del(&clk->node);
  154. mutex_unlock(&clock_list_sem);
  155. }
  156. EXPORT_SYMBOL_GPL(clk_unregister);
  157. unsigned long clk_get_rate(struct clk *clk)
  158. {
  159. return clk->rate;
  160. }
  161. EXPORT_SYMBOL_GPL(clk_get_rate);
  162. int clk_set_rate(struct clk *clk, unsigned long rate)
  163. {
  164. return clk_set_rate_ex(clk, rate, 0);
  165. }
  166. EXPORT_SYMBOL_GPL(clk_set_rate);
  167. int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
  168. {
  169. int ret = -EOPNOTSUPP;
  170. if (likely(clk->ops && clk->ops->set_rate)) {
  171. unsigned long flags;
  172. spin_lock_irqsave(&clock_lock, flags);
  173. ret = clk->ops->set_rate(clk, rate, algo_id);
  174. spin_unlock_irqrestore(&clock_lock, flags);
  175. }
  176. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  177. propagate_rate(clk);
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(clk_set_rate_ex);
  181. void clk_recalc_rate(struct clk *clk)
  182. {
  183. if (likely(clk->ops && clk->ops->recalc)) {
  184. unsigned long flags;
  185. spin_lock_irqsave(&clock_lock, flags);
  186. clk->ops->recalc(clk);
  187. spin_unlock_irqrestore(&clock_lock, flags);
  188. }
  189. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  190. propagate_rate(clk);
  191. }
  192. EXPORT_SYMBOL_GPL(clk_recalc_rate);
  193. /*
  194. * Returns a clock. Note that we first try to use device id on the bus
  195. * and clock name. If this fails, we try to use clock name only.
  196. */
  197. struct clk *clk_get(struct device *dev, const char *id)
  198. {
  199. struct clk *p, *clk = ERR_PTR(-ENOENT);
  200. int idno;
  201. if (dev == NULL || dev->bus != &platform_bus_type)
  202. idno = -1;
  203. else
  204. idno = to_platform_device(dev)->id;
  205. mutex_lock(&clock_list_sem);
  206. list_for_each_entry(p, &clock_list, node) {
  207. if (p->id == idno &&
  208. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  209. clk = p;
  210. goto found;
  211. }
  212. }
  213. list_for_each_entry(p, &clock_list, node) {
  214. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  215. clk = p;
  216. break;
  217. }
  218. }
  219. found:
  220. mutex_unlock(&clock_list_sem);
  221. return clk;
  222. }
  223. EXPORT_SYMBOL_GPL(clk_get);
  224. void clk_put(struct clk *clk)
  225. {
  226. if (clk && !IS_ERR(clk))
  227. module_put(clk->owner);
  228. }
  229. EXPORT_SYMBOL_GPL(clk_put);
  230. void __init __attribute__ ((weak))
  231. arch_init_clk_ops(struct clk_ops **ops, int type)
  232. {
  233. }
  234. static int show_clocks(char *buf, char **start, off_t off,
  235. int len, int *eof, void *data)
  236. {
  237. struct clk *clk;
  238. char *p = buf;
  239. list_for_each_entry_reverse(clk, &clock_list, node) {
  240. unsigned long rate = clk_get_rate(clk);
  241. /*
  242. * Don't bother listing dummy clocks with no ancestry
  243. * that only support enable and disable ops.
  244. */
  245. if (unlikely(!rate && !clk->parent))
  246. continue;
  247. p += sprintf(p, "%-12s\t: %ld.%02ldMHz\n", clk->name,
  248. rate / 1000000, (rate % 1000000) / 10000);
  249. }
  250. return p - buf;
  251. }
  252. int __init clk_init(void)
  253. {
  254. int i, ret = 0;
  255. BUG_ON(!master_clk.rate);
  256. for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
  257. struct clk *clk = onchip_clocks[i];
  258. arch_init_clk_ops(&clk->ops, i);
  259. ret |= clk_register(clk);
  260. }
  261. /* Kick the child clocks.. */
  262. propagate_rate(&master_clk);
  263. propagate_rate(&bus_clk);
  264. return ret;
  265. }
  266. static int __init clk_proc_init(void)
  267. {
  268. struct proc_dir_entry *p;
  269. p = create_proc_read_entry("clocks", S_IRUSR, NULL,
  270. show_clocks, NULL);
  271. if (unlikely(!p))
  272. return -EINVAL;
  273. return 0;
  274. }
  275. subsys_initcall(clk_proc_init);