clock.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*****************************************************************************
  2. * Copyright 2001 - 2009 Broadcom Corporation. All rights reserved.
  3. *
  4. * Unless you and Broadcom execute a separate written software license
  5. * agreement governing use of this software, this software is licensed to you
  6. * under the terms of the GNU General Public License version 2, available at
  7. * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8. *
  9. * Notwithstanding the above, under no circumstances may you combine this
  10. * software in any way with any other Broadcom software provided under a
  11. * license other than the GPL, without Broadcom's express prior written
  12. * consent.
  13. *****************************************************************************/
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/device.h>
  17. #include <linux/list.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/string.h>
  21. #include <linux/clk.h>
  22. #include <linux/spinlock.h>
  23. #include <mach/csp/hw_cfg.h>
  24. #include <mach/csp/chipcHw_def.h>
  25. #include <mach/csp/chipcHw_reg.h>
  26. #include <mach/csp/chipcHw_inline.h>
  27. #include <asm/clkdev.h>
  28. #include "clock.h"
  29. #define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY)
  30. #define clk_is_pll1(x) ((x)->type & CLK_TYPE_PLL1)
  31. #define clk_is_pll2(x) ((x)->type & CLK_TYPE_PLL2)
  32. #define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE)
  33. #define clk_is_bypassable(x) ((x)->type & CLK_TYPE_BYPASSABLE)
  34. #define clk_is_using_xtal(x) ((x)->mode & CLK_MODE_XTAL)
  35. static DEFINE_SPINLOCK(clk_lock);
  36. static void __clk_enable(struct clk *clk)
  37. {
  38. if (!clk)
  39. return;
  40. /* enable parent clock first */
  41. if (clk->parent)
  42. __clk_enable(clk->parent);
  43. if (clk->use_cnt++ == 0) {
  44. if (clk_is_pll1(clk)) { /* PLL1 */
  45. chipcHw_pll1Enable(clk->rate_hz, 0);
  46. } else if (clk_is_pll2(clk)) { /* PLL2 */
  47. chipcHw_pll2Enable(clk->rate_hz);
  48. } else if (clk_is_using_xtal(clk)) { /* source is crystal */
  49. if (!clk_is_primary(clk))
  50. chipcHw_bypassClockEnable(clk->csp_id);
  51. } else { /* source is PLL */
  52. chipcHw_setClockEnable(clk->csp_id);
  53. }
  54. }
  55. }
  56. int clk_enable(struct clk *clk)
  57. {
  58. unsigned long flags;
  59. if (!clk)
  60. return -EINVAL;
  61. spin_lock_irqsave(&clk_lock, flags);
  62. __clk_enable(clk);
  63. spin_unlock_irqrestore(&clk_lock, flags);
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(clk_enable);
  67. static void __clk_disable(struct clk *clk)
  68. {
  69. if (!clk)
  70. return;
  71. BUG_ON(clk->use_cnt == 0);
  72. if (--clk->use_cnt == 0) {
  73. if (clk_is_pll1(clk)) { /* PLL1 */
  74. chipcHw_pll1Disable();
  75. } else if (clk_is_pll2(clk)) { /* PLL2 */
  76. chipcHw_pll2Disable();
  77. } else if (clk_is_using_xtal(clk)) { /* source is crystal */
  78. if (!clk_is_primary(clk))
  79. chipcHw_bypassClockDisable(clk->csp_id);
  80. } else { /* source is PLL */
  81. chipcHw_setClockDisable(clk->csp_id);
  82. }
  83. }
  84. if (clk->parent)
  85. __clk_disable(clk->parent);
  86. }
  87. void clk_disable(struct clk *clk)
  88. {
  89. unsigned long flags;
  90. if (!clk)
  91. return;
  92. spin_lock_irqsave(&clk_lock, flags);
  93. __clk_disable(clk);
  94. spin_unlock_irqrestore(&clk_lock, flags);
  95. }
  96. EXPORT_SYMBOL(clk_disable);
  97. unsigned long clk_get_rate(struct clk *clk)
  98. {
  99. if (!clk)
  100. return 0;
  101. return clk->rate_hz;
  102. }
  103. EXPORT_SYMBOL(clk_get_rate);
  104. long clk_round_rate(struct clk *clk, unsigned long rate)
  105. {
  106. unsigned long flags;
  107. unsigned long actual;
  108. unsigned long rate_hz;
  109. if (!clk)
  110. return -EINVAL;
  111. if (!clk_is_programmable(clk))
  112. return -EINVAL;
  113. if (clk->use_cnt)
  114. return -EBUSY;
  115. spin_lock_irqsave(&clk_lock, flags);
  116. actual = clk->parent->rate_hz;
  117. rate_hz = min(actual, rate);
  118. spin_unlock_irqrestore(&clk_lock, flags);
  119. return rate_hz;
  120. }
  121. EXPORT_SYMBOL(clk_round_rate);
  122. int clk_set_rate(struct clk *clk, unsigned long rate)
  123. {
  124. unsigned long flags;
  125. unsigned long actual;
  126. unsigned long rate_hz;
  127. if (!clk)
  128. return -EINVAL;
  129. if (!clk_is_programmable(clk))
  130. return -EINVAL;
  131. if (clk->use_cnt)
  132. return -EBUSY;
  133. spin_lock_irqsave(&clk_lock, flags);
  134. actual = clk->parent->rate_hz;
  135. rate_hz = min(actual, rate);
  136. rate_hz = chipcHw_setClockFrequency(clk->csp_id, rate_hz);
  137. clk->rate_hz = rate_hz;
  138. spin_unlock_irqrestore(&clk_lock, flags);
  139. return 0;
  140. }
  141. EXPORT_SYMBOL(clk_set_rate);
  142. struct clk *clk_get_parent(struct clk *clk)
  143. {
  144. if (!clk)
  145. return NULL;
  146. return clk->parent;
  147. }
  148. EXPORT_SYMBOL(clk_get_parent);
  149. int clk_set_parent(struct clk *clk, struct clk *parent)
  150. {
  151. unsigned long flags;
  152. struct clk *old_parent;
  153. if (!clk || !parent)
  154. return -EINVAL;
  155. if (!clk_is_primary(parent) || !clk_is_bypassable(clk))
  156. return -EINVAL;
  157. /* if more than one user, parent is not allowed */
  158. if (clk->use_cnt > 1)
  159. return -EBUSY;
  160. if (clk->parent == parent)
  161. return 0;
  162. spin_lock_irqsave(&clk_lock, flags);
  163. old_parent = clk->parent;
  164. clk->parent = parent;
  165. if (clk_is_using_xtal(parent))
  166. clk->mode |= CLK_MODE_XTAL;
  167. else
  168. clk->mode &= (~CLK_MODE_XTAL);
  169. /* if clock is active */
  170. if (clk->use_cnt != 0) {
  171. clk->use_cnt--;
  172. /* enable clock with the new parent */
  173. __clk_enable(clk);
  174. /* disable the old parent */
  175. __clk_disable(old_parent);
  176. }
  177. spin_unlock_irqrestore(&clk_lock, flags);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL(clk_set_parent);