clock.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Based on arch/arm/plat-omap/clock.c
  3. *
  4. * Copyright (C) 2004 - 2005 Nokia corporation
  5. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  6. * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
  7. * Copyright 2007 Freescale Semiconductor, Inc. All Rights Reserved.
  8. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  22. * MA 02110-1301, USA.
  23. */
  24. /* #define DEBUG */
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/kernel.h>
  31. #include <linux/list.h>
  32. #include <linux/module.h>
  33. #include <linux/mutex.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/semaphore.h>
  37. #include <linux/string.h>
  38. #include <mach/clock.h>
  39. static LIST_HEAD(clocks);
  40. static DEFINE_MUTEX(clocks_mutex);
  41. /*-------------------------------------------------------------------------
  42. * Standard clock functions defined in include/linux/clk.h
  43. *-------------------------------------------------------------------------*/
  44. /*
  45. * All the code inside #ifndef CONFIG_COMMON_CLKDEV can be removed once all
  46. * MXC architectures have switched to using clkdev.
  47. */
  48. #ifndef CONFIG_COMMON_CLKDEV
  49. /*
  50. * Retrieve a clock by name.
  51. *
  52. * Note that we first try to use device id on the bus
  53. * and clock name. If this fails, we try to use "<name>.<id>". If this fails,
  54. * we try to use clock name only.
  55. * The reference count to the clock's module owner ref count is incremented.
  56. */
  57. struct clk *clk_get(struct device *dev, const char *id)
  58. {
  59. struct clk *p, *clk = ERR_PTR(-ENOENT);
  60. int idno;
  61. const char *str;
  62. if (id == NULL)
  63. return clk;
  64. if (dev == NULL || dev->bus != &platform_bus_type)
  65. idno = -1;
  66. else
  67. idno = to_platform_device(dev)->id;
  68. mutex_lock(&clocks_mutex);
  69. list_for_each_entry(p, &clocks, node) {
  70. if (p->id == idno &&
  71. strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  72. clk = p;
  73. goto found;
  74. }
  75. }
  76. str = strrchr(id, '.');
  77. if (str) {
  78. int cnt = str - id;
  79. str++;
  80. idno = simple_strtol(str, NULL, 10);
  81. list_for_each_entry(p, &clocks, node) {
  82. if (p->id == idno &&
  83. strlen(p->name) == cnt &&
  84. strncmp(id, p->name, cnt) == 0 &&
  85. try_module_get(p->owner)) {
  86. clk = p;
  87. goto found;
  88. }
  89. }
  90. }
  91. list_for_each_entry(p, &clocks, node) {
  92. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  93. clk = p;
  94. goto found;
  95. }
  96. }
  97. printk(KERN_WARNING "clk: Unable to get requested clock: %s\n", id);
  98. found:
  99. mutex_unlock(&clocks_mutex);
  100. return clk;
  101. }
  102. EXPORT_SYMBOL(clk_get);
  103. #endif
  104. static void __clk_disable(struct clk *clk)
  105. {
  106. if (clk == NULL || IS_ERR(clk))
  107. return;
  108. __clk_disable(clk->parent);
  109. __clk_disable(clk->secondary);
  110. if (!(--clk->usecount) && clk->disable)
  111. clk->disable(clk);
  112. }
  113. static int __clk_enable(struct clk *clk)
  114. {
  115. if (clk == NULL || IS_ERR(clk))
  116. return -EINVAL;
  117. __clk_enable(clk->parent);
  118. __clk_enable(clk->secondary);
  119. if (clk->usecount++ == 0 && clk->enable)
  120. clk->enable(clk);
  121. return 0;
  122. }
  123. /* This function increments the reference count on the clock and enables the
  124. * clock if not already enabled. The parent clock tree is recursively enabled
  125. */
  126. int clk_enable(struct clk *clk)
  127. {
  128. int ret = 0;
  129. if (clk == NULL || IS_ERR(clk))
  130. return -EINVAL;
  131. mutex_lock(&clocks_mutex);
  132. ret = __clk_enable(clk);
  133. mutex_unlock(&clocks_mutex);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL(clk_enable);
  137. /* This function decrements the reference count on the clock and disables
  138. * the clock when reference count is 0. The parent clock tree is
  139. * recursively disabled
  140. */
  141. void clk_disable(struct clk *clk)
  142. {
  143. if (clk == NULL || IS_ERR(clk))
  144. return;
  145. mutex_lock(&clocks_mutex);
  146. __clk_disable(clk);
  147. mutex_unlock(&clocks_mutex);
  148. }
  149. EXPORT_SYMBOL(clk_disable);
  150. /* Retrieve the *current* clock rate. If the clock itself
  151. * does not provide a special calculation routine, ask
  152. * its parent and so on, until one is able to return
  153. * a valid clock rate
  154. */
  155. unsigned long clk_get_rate(struct clk *clk)
  156. {
  157. if (clk == NULL || IS_ERR(clk))
  158. return 0UL;
  159. if (clk->get_rate)
  160. return clk->get_rate(clk);
  161. return clk_get_rate(clk->parent);
  162. }
  163. EXPORT_SYMBOL(clk_get_rate);
  164. #ifndef CONFIG_COMMON_CLKDEV
  165. /* Decrement the clock's module reference count */
  166. void clk_put(struct clk *clk)
  167. {
  168. if (clk && !IS_ERR(clk))
  169. module_put(clk->owner);
  170. }
  171. EXPORT_SYMBOL(clk_put);
  172. #endif
  173. /* Round the requested clock rate to the nearest supported
  174. * rate that is less than or equal to the requested rate.
  175. * This is dependent on the clock's current parent.
  176. */
  177. long clk_round_rate(struct clk *clk, unsigned long rate)
  178. {
  179. if (clk == NULL || IS_ERR(clk) || !clk->round_rate)
  180. return 0;
  181. return clk->round_rate(clk, rate);
  182. }
  183. EXPORT_SYMBOL(clk_round_rate);
  184. /* Set the clock to the requested clock rate. The rate must
  185. * match a supported rate exactly based on what clk_round_rate returns
  186. */
  187. int clk_set_rate(struct clk *clk, unsigned long rate)
  188. {
  189. int ret = -EINVAL;
  190. if (clk == NULL || IS_ERR(clk) || clk->set_rate == NULL || rate == 0)
  191. return ret;
  192. mutex_lock(&clocks_mutex);
  193. ret = clk->set_rate(clk, rate);
  194. mutex_unlock(&clocks_mutex);
  195. return ret;
  196. }
  197. EXPORT_SYMBOL(clk_set_rate);
  198. /* Set the clock's parent to another clock source */
  199. int clk_set_parent(struct clk *clk, struct clk *parent)
  200. {
  201. int ret = -EINVAL;
  202. if (clk == NULL || IS_ERR(clk) || parent == NULL ||
  203. IS_ERR(parent) || clk->set_parent == NULL)
  204. return ret;
  205. mutex_lock(&clocks_mutex);
  206. ret = clk->set_parent(clk, parent);
  207. if (ret == 0)
  208. clk->parent = parent;
  209. mutex_unlock(&clocks_mutex);
  210. return ret;
  211. }
  212. EXPORT_SYMBOL(clk_set_parent);
  213. /* Retrieve the clock's parent clock source */
  214. struct clk *clk_get_parent(struct clk *clk)
  215. {
  216. struct clk *ret = NULL;
  217. if (clk == NULL || IS_ERR(clk))
  218. return ret;
  219. return clk->parent;
  220. }
  221. EXPORT_SYMBOL(clk_get_parent);
  222. #ifndef CONFIG_COMMON_CLKDEV
  223. /*
  224. * Add a new clock to the clock tree.
  225. */
  226. int clk_register(struct clk *clk)
  227. {
  228. if (clk == NULL || IS_ERR(clk))
  229. return -EINVAL;
  230. mutex_lock(&clocks_mutex);
  231. list_add(&clk->node, &clocks);
  232. mutex_unlock(&clocks_mutex);
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(clk_register);
  236. /* Remove a clock from the clock tree */
  237. void clk_unregister(struct clk *clk)
  238. {
  239. if (clk == NULL || IS_ERR(clk))
  240. return;
  241. mutex_lock(&clocks_mutex);
  242. list_del(&clk->node);
  243. mutex_unlock(&clocks_mutex);
  244. }
  245. EXPORT_SYMBOL(clk_unregister);
  246. #ifdef CONFIG_PROC_FS
  247. static int mxc_clock_read_proc(char *page, char **start, off_t off,
  248. int count, int *eof, void *data)
  249. {
  250. struct clk *clkp;
  251. char *p = page;
  252. int len;
  253. list_for_each_entry(clkp, &clocks, node) {
  254. p += sprintf(p, "%s-%d:\t\t%lu, %d", clkp->name, clkp->id,
  255. clk_get_rate(clkp), clkp->usecount);
  256. if (clkp->parent)
  257. p += sprintf(p, ", %s-%d\n", clkp->parent->name,
  258. clkp->parent->id);
  259. else
  260. p += sprintf(p, "\n");
  261. }
  262. len = (p - page) - off;
  263. if (len < 0)
  264. len = 0;
  265. *eof = (len <= count) ? 1 : 0;
  266. *start = page + off;
  267. return len;
  268. }
  269. static int __init mxc_setup_proc_entry(void)
  270. {
  271. struct proc_dir_entry *res;
  272. res = create_proc_read_entry("cpu/clocks", 0, NULL,
  273. mxc_clock_read_proc, NULL);
  274. if (!res) {
  275. printk(KERN_ERR "Failed to create proc/cpu/clocks\n");
  276. return -ENOMEM;
  277. }
  278. return 0;
  279. }
  280. late_initcall(mxc_setup_proc_entry);
  281. #endif /* CONFIG_PROC_FS */
  282. #endif
  283. /*
  284. * Get the resulting clock rate from a PLL register value and the input
  285. * frequency. PLLs with this register layout can at least be found on
  286. * MX1, MX21, MX27 and MX31
  287. *
  288. * mfi + mfn / (mfd + 1)
  289. * f = 2 * f_ref * --------------------
  290. * pd + 1
  291. */
  292. unsigned long mxc_decode_pll(unsigned int reg_val, u32 freq)
  293. {
  294. long long ll;
  295. int mfn_abs;
  296. unsigned int mfi, mfn, mfd, pd;
  297. mfi = (reg_val >> 10) & 0xf;
  298. mfn = reg_val & 0x3ff;
  299. mfd = (reg_val >> 16) & 0x3ff;
  300. pd = (reg_val >> 26) & 0xf;
  301. mfi = mfi <= 5 ? 5 : mfi;
  302. mfn_abs = mfn;
  303. #if !defined CONFIG_ARCH_MX1 && !defined CONFIG_ARCH_MX21
  304. if (mfn >= 0x200) {
  305. mfn |= 0xFFFFFE00;
  306. mfn_abs = -mfn;
  307. }
  308. #endif
  309. freq *= 2;
  310. freq /= pd + 1;
  311. ll = (unsigned long long)freq * mfn_abs;
  312. do_div(ll, mfd + 1);
  313. if (mfn < 0)
  314. ll = -ll;
  315. ll = (freq * mfi) + ll;
  316. return ll;
  317. }