clock.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * TI DaVinci clock config file
  3. *
  4. * Copyright (C) 2006 Texas Instruments.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. #include <linux/platform_device.h>
  18. #include <asm/hardware.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/psc.h>
  21. #include "clock.h"
  22. /* PLL/Reset register offsets */
  23. #define PLLM 0x110
  24. static LIST_HEAD(clocks);
  25. static DEFINE_MUTEX(clocks_mutex);
  26. static DEFINE_SPINLOCK(clockfw_lock);
  27. static unsigned int commonrate;
  28. static unsigned int armrate;
  29. static unsigned int fixedrate = 27000000; /* 27 MHZ */
  30. extern void davinci_psc_config(unsigned int domain, unsigned int id, char enable);
  31. /*
  32. * Returns a clock. Note that we first try to use device id on the bus
  33. * and clock name. If this fails, we try to use clock name only.
  34. */
  35. struct clk *clk_get(struct device *dev, const char *id)
  36. {
  37. struct clk *p, *clk = ERR_PTR(-ENOENT);
  38. int idno;
  39. if (dev == NULL || dev->bus != &platform_bus_type)
  40. idno = -1;
  41. else
  42. idno = to_platform_device(dev)->id;
  43. mutex_lock(&clocks_mutex);
  44. list_for_each_entry(p, &clocks, node) {
  45. if (p->id == idno &&
  46. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  47. clk = p;
  48. goto found;
  49. }
  50. }
  51. list_for_each_entry(p, &clocks, node) {
  52. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  53. clk = p;
  54. break;
  55. }
  56. }
  57. found:
  58. mutex_unlock(&clocks_mutex);
  59. return clk;
  60. }
  61. EXPORT_SYMBOL(clk_get);
  62. void clk_put(struct clk *clk)
  63. {
  64. if (clk && !IS_ERR(clk))
  65. module_put(clk->owner);
  66. }
  67. EXPORT_SYMBOL(clk_put);
  68. static int __clk_enable(struct clk *clk)
  69. {
  70. if (clk->flags & ALWAYS_ENABLED)
  71. return 0;
  72. davinci_psc_config(DAVINCI_GPSC_ARMDOMAIN, clk->lpsc, 1);
  73. return 0;
  74. }
  75. static void __clk_disable(struct clk *clk)
  76. {
  77. if (clk->usecount)
  78. return;
  79. davinci_psc_config(DAVINCI_GPSC_ARMDOMAIN, clk->lpsc, 0);
  80. }
  81. int clk_enable(struct clk *clk)
  82. {
  83. unsigned long flags;
  84. int ret = 0;
  85. if (clk == NULL || IS_ERR(clk))
  86. return -EINVAL;
  87. if (clk->usecount++ == 0) {
  88. spin_lock_irqsave(&clockfw_lock, flags);
  89. ret = __clk_enable(clk);
  90. spin_unlock_irqrestore(&clockfw_lock, flags);
  91. }
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(clk_enable);
  95. void clk_disable(struct clk *clk)
  96. {
  97. unsigned long flags;
  98. if (clk == NULL || IS_ERR(clk))
  99. return;
  100. if (clk->usecount > 0 && !(--clk->usecount)) {
  101. spin_lock_irqsave(&clockfw_lock, flags);
  102. __clk_disable(clk);
  103. spin_unlock_irqrestore(&clockfw_lock, flags);
  104. }
  105. }
  106. EXPORT_SYMBOL(clk_disable);
  107. unsigned long clk_get_rate(struct clk *clk)
  108. {
  109. if (clk == NULL || IS_ERR(clk))
  110. return -EINVAL;
  111. return *(clk->rate);
  112. }
  113. EXPORT_SYMBOL(clk_get_rate);
  114. long clk_round_rate(struct clk *clk, unsigned long rate)
  115. {
  116. if (clk == NULL || IS_ERR(clk))
  117. return -EINVAL;
  118. return *(clk->rate);
  119. }
  120. EXPORT_SYMBOL(clk_round_rate);
  121. int clk_set_rate(struct clk *clk, unsigned long rate)
  122. {
  123. if (clk == NULL || IS_ERR(clk))
  124. return -EINVAL;
  125. /* changing the clk rate is not supported */
  126. return -EINVAL;
  127. }
  128. EXPORT_SYMBOL(clk_set_rate);
  129. int clk_register(struct clk *clk)
  130. {
  131. if (clk == NULL || IS_ERR(clk))
  132. return -EINVAL;
  133. mutex_lock(&clocks_mutex);
  134. list_add(&clk->node, &clocks);
  135. mutex_unlock(&clocks_mutex);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL(clk_register);
  139. void clk_unregister(struct clk *clk)
  140. {
  141. if (clk == NULL || IS_ERR(clk))
  142. return;
  143. mutex_lock(&clocks_mutex);
  144. list_del(&clk->node);
  145. mutex_unlock(&clocks_mutex);
  146. }
  147. EXPORT_SYMBOL(clk_unregister);
  148. static struct clk davinci_clks[] = {
  149. {
  150. .name = "ARMCLK",
  151. .rate = &armrate,
  152. .lpsc = -1,
  153. .flags = ALWAYS_ENABLED,
  154. },
  155. {
  156. .name = "UART",
  157. .rate = &fixedrate,
  158. .lpsc = DAVINCI_LPSC_UART0,
  159. },
  160. {
  161. .name = "EMACCLK",
  162. .rate = &commonrate,
  163. .lpsc = DAVINCI_LPSC_EMAC_WRAPPER,
  164. },
  165. {
  166. .name = "I2CCLK",
  167. .rate = &fixedrate,
  168. .lpsc = DAVINCI_LPSC_I2C,
  169. },
  170. {
  171. .name = "IDECLK",
  172. .rate = &commonrate,
  173. .lpsc = DAVINCI_LPSC_ATA,
  174. },
  175. {
  176. .name = "McBSPCLK",
  177. .rate = &commonrate,
  178. .lpsc = DAVINCI_LPSC_McBSP,
  179. },
  180. {
  181. .name = "MMCSDCLK",
  182. .rate = &commonrate,
  183. .lpsc = DAVINCI_LPSC_MMC_SD,
  184. },
  185. {
  186. .name = "SPICLK",
  187. .rate = &commonrate,
  188. .lpsc = DAVINCI_LPSC_SPI,
  189. },
  190. {
  191. .name = "gpio",
  192. .rate = &commonrate,
  193. .lpsc = DAVINCI_LPSC_GPIO,
  194. },
  195. {
  196. .name = "AEMIFCLK",
  197. .rate = &commonrate,
  198. .lpsc = DAVINCI_LPSC_AEMIF,
  199. .usecount = 1,
  200. }
  201. };
  202. int __init davinci_clk_init(void)
  203. {
  204. struct clk *clkp;
  205. int count = 0;
  206. u32 pll_mult;
  207. pll_mult = davinci_readl(DAVINCI_PLL_CNTRL0_BASE + PLLM);
  208. commonrate = ((pll_mult + 1) * 27000000) / 6;
  209. armrate = ((pll_mult + 1) * 27000000) / 2;
  210. for (clkp = davinci_clks; count < ARRAY_SIZE(davinci_clks);
  211. count++, clkp++) {
  212. clk_register(clkp);
  213. /* Turn on clocks that have been enabled in the
  214. * table above */
  215. if (clkp->usecount)
  216. clk_enable(clkp);
  217. }
  218. return 0;
  219. }
  220. #ifdef CONFIG_PROC_FS
  221. #include <linux/proc_fs.h>
  222. #include <linux/seq_file.h>
  223. static void *davinci_ck_start(struct seq_file *m, loff_t *pos)
  224. {
  225. return *pos < 1 ? (void *)1 : NULL;
  226. }
  227. static void *davinci_ck_next(struct seq_file *m, void *v, loff_t *pos)
  228. {
  229. ++*pos;
  230. return NULL;
  231. }
  232. static void davinci_ck_stop(struct seq_file *m, void *v)
  233. {
  234. }
  235. static int davinci_ck_show(struct seq_file *m, void *v)
  236. {
  237. struct clk *cp;
  238. list_for_each_entry(cp, &clocks, node)
  239. seq_printf(m,"%s %d %d\n", cp->name, *(cp->rate), cp->usecount);
  240. return 0;
  241. }
  242. static struct seq_operations davinci_ck_op = {
  243. .start = davinci_ck_start,
  244. .next = davinci_ck_next,
  245. .stop = davinci_ck_stop,
  246. .show = davinci_ck_show
  247. };
  248. static int davinci_ck_open(struct inode *inode, struct file *file)
  249. {
  250. return seq_open(file, &davinci_ck_op);
  251. }
  252. static struct file_operations proc_davinci_ck_operations = {
  253. .open = davinci_ck_open,
  254. .read = seq_read,
  255. .llseek = seq_lseek,
  256. .release = seq_release,
  257. };
  258. static int __init davinci_ck_proc_init(void)
  259. {
  260. struct proc_dir_entry *entry;
  261. entry = create_proc_entry("davinci_clocks", 0, NULL);
  262. if (entry)
  263. entry->proc_fops = &proc_davinci_ck_operations;
  264. return 0;
  265. }
  266. __initcall(davinci_ck_proc_init);
  267. #endif /* CONFIG_DEBUG_PROC_FS */