clock.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include <mach/hardware.h>
  40. #ifndef CONFIG_COMMON_CLK
  41. static LIST_HEAD(clocks);
  42. static DEFINE_MUTEX(clocks_mutex);
  43. /*-------------------------------------------------------------------------
  44. * Standard clock functions defined in include/linux/clk.h
  45. *-------------------------------------------------------------------------*/
  46. static void __clk_disable(struct clk *clk)
  47. {
  48. if (clk == NULL || IS_ERR(clk))
  49. return;
  50. WARN_ON(!clk->usecount);
  51. if (!(--clk->usecount)) {
  52. if (clk->disable)
  53. clk->disable(clk);
  54. __clk_disable(clk->parent);
  55. __clk_disable(clk->secondary);
  56. }
  57. }
  58. static int __clk_enable(struct clk *clk)
  59. {
  60. if (clk == NULL || IS_ERR(clk))
  61. return -EINVAL;
  62. if (clk->usecount++ == 0) {
  63. __clk_enable(clk->parent);
  64. __clk_enable(clk->secondary);
  65. if (clk->enable)
  66. clk->enable(clk);
  67. }
  68. return 0;
  69. }
  70. /* This function increments the reference count on the clock and enables the
  71. * clock if not already enabled. The parent clock tree is recursively enabled
  72. */
  73. int clk_enable(struct clk *clk)
  74. {
  75. int ret = 0;
  76. if (clk == NULL || IS_ERR(clk))
  77. return -EINVAL;
  78. mutex_lock(&clocks_mutex);
  79. ret = __clk_enable(clk);
  80. mutex_unlock(&clocks_mutex);
  81. return ret;
  82. }
  83. EXPORT_SYMBOL(clk_enable);
  84. /* This function decrements the reference count on the clock and disables
  85. * the clock when reference count is 0. The parent clock tree is
  86. * recursively disabled
  87. */
  88. void clk_disable(struct clk *clk)
  89. {
  90. if (clk == NULL || IS_ERR(clk))
  91. return;
  92. mutex_lock(&clocks_mutex);
  93. __clk_disable(clk);
  94. mutex_unlock(&clocks_mutex);
  95. }
  96. EXPORT_SYMBOL(clk_disable);
  97. /* Retrieve the *current* clock rate. If the clock itself
  98. * does not provide a special calculation routine, ask
  99. * its parent and so on, until one is able to return
  100. * a valid clock rate
  101. */
  102. unsigned long clk_get_rate(struct clk *clk)
  103. {
  104. if (clk == NULL || IS_ERR(clk))
  105. return 0UL;
  106. if (clk->get_rate)
  107. return clk->get_rate(clk);
  108. return clk_get_rate(clk->parent);
  109. }
  110. EXPORT_SYMBOL(clk_get_rate);
  111. /* Round the requested clock rate to the nearest supported
  112. * rate that is less than or equal to the requested rate.
  113. * This is dependent on the clock's current parent.
  114. */
  115. long clk_round_rate(struct clk *clk, unsigned long rate)
  116. {
  117. if (clk == NULL || IS_ERR(clk) || !clk->round_rate)
  118. return 0;
  119. return clk->round_rate(clk, rate);
  120. }
  121. EXPORT_SYMBOL(clk_round_rate);
  122. /* Set the clock to the requested clock rate. The rate must
  123. * match a supported rate exactly based on what clk_round_rate returns
  124. */
  125. int clk_set_rate(struct clk *clk, unsigned long rate)
  126. {
  127. int ret = -EINVAL;
  128. if (clk == NULL || IS_ERR(clk) || clk->set_rate == NULL || rate == 0)
  129. return ret;
  130. mutex_lock(&clocks_mutex);
  131. ret = clk->set_rate(clk, rate);
  132. mutex_unlock(&clocks_mutex);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL(clk_set_rate);
  136. /* Set the clock's parent to another clock source */
  137. int clk_set_parent(struct clk *clk, struct clk *parent)
  138. {
  139. int ret = -EINVAL;
  140. struct clk *old;
  141. if (clk == NULL || IS_ERR(clk) || parent == NULL ||
  142. IS_ERR(parent) || clk->set_parent == NULL)
  143. return ret;
  144. if (clk->usecount)
  145. clk_enable(parent);
  146. mutex_lock(&clocks_mutex);
  147. ret = clk->set_parent(clk, parent);
  148. if (ret == 0) {
  149. old = clk->parent;
  150. clk->parent = parent;
  151. } else {
  152. old = parent;
  153. }
  154. mutex_unlock(&clocks_mutex);
  155. if (clk->usecount)
  156. clk_disable(old);
  157. return ret;
  158. }
  159. EXPORT_SYMBOL(clk_set_parent);
  160. /* Retrieve the clock's parent clock source */
  161. struct clk *clk_get_parent(struct clk *clk)
  162. {
  163. struct clk *ret = NULL;
  164. if (clk == NULL || IS_ERR(clk))
  165. return ret;
  166. return clk->parent;
  167. }
  168. EXPORT_SYMBOL(clk_get_parent);
  169. #else
  170. /*
  171. * Lock to protect the clock module (ccm) registers. Used
  172. * on all i.MXs
  173. */
  174. DEFINE_SPINLOCK(imx_ccm_lock);
  175. #endif /* CONFIG_COMMON_CLK */
  176. /*
  177. * Get the resulting clock rate from a PLL register value and the input
  178. * frequency. PLLs with this register layout can at least be found on
  179. * MX1, MX21, MX27 and MX31
  180. *
  181. * mfi + mfn / (mfd + 1)
  182. * f = 2 * f_ref * --------------------
  183. * pd + 1
  184. */
  185. unsigned long mxc_decode_pll(unsigned int reg_val, u32 freq)
  186. {
  187. long long ll;
  188. int mfn_abs;
  189. unsigned int mfi, mfn, mfd, pd;
  190. mfi = (reg_val >> 10) & 0xf;
  191. mfn = reg_val & 0x3ff;
  192. mfd = (reg_val >> 16) & 0x3ff;
  193. pd = (reg_val >> 26) & 0xf;
  194. mfi = mfi <= 5 ? 5 : mfi;
  195. mfn_abs = mfn;
  196. /* On all i.MXs except i.MX1 and i.MX21 mfn is a 10bit
  197. * 2's complements number
  198. */
  199. if (!cpu_is_mx1() && !cpu_is_mx21() && mfn >= 0x200)
  200. mfn_abs = 0x400 - mfn;
  201. freq *= 2;
  202. freq /= pd + 1;
  203. ll = (unsigned long long)freq * mfn_abs;
  204. do_div(ll, mfd + 1);
  205. if (!cpu_is_mx1() && !cpu_is_mx21() && mfn >= 0x200)
  206. ll = -ll;
  207. ll = (freq * mfi) + ll;
  208. return ll;
  209. }