clk-super.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/io.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/clk-provider.h>
  22. #include "clk.h"
  23. #define SUPER_STATE_IDLE 0
  24. #define SUPER_STATE_RUN 1
  25. #define SUPER_STATE_IRQ 2
  26. #define SUPER_STATE_FIQ 3
  27. #define SUPER_STATE_SHIFT 28
  28. #define SUPER_STATE_MASK ((BIT(SUPER_STATE_IDLE) | BIT(SUPER_STATE_RUN) | \
  29. BIT(SUPER_STATE_IRQ) | BIT(SUPER_STATE_FIQ)) \
  30. << SUPER_STATE_SHIFT)
  31. #define SUPER_LP_DIV2_BYPASS (1 << 16)
  32. #define super_state(s) (BIT(s) << SUPER_STATE_SHIFT)
  33. #define super_state_to_src_shift(m, s) ((m->width * s))
  34. #define super_state_to_src_mask(m) (((1 << m->width) - 1))
  35. static u8 clk_super_get_parent(struct clk_hw *hw)
  36. {
  37. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  38. u32 val, state;
  39. u8 source, shift;
  40. val = readl_relaxed(mux->reg);
  41. state = val & SUPER_STATE_MASK;
  42. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  43. (state != super_state(SUPER_STATE_IDLE)));
  44. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  45. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  46. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  47. source = (val >> shift) & super_state_to_src_mask(mux);
  48. /*
  49. * If LP_DIV2_BYPASS is not set and PLLX is current parent then
  50. * PLLX/2 is the input source to CCLKLP.
  51. */
  52. if ((mux->flags & TEGRA_DIVIDER_2) && !(val & SUPER_LP_DIV2_BYPASS) &&
  53. (source == mux->pllx_index))
  54. source = mux->div2_index;
  55. return source;
  56. }
  57. static int clk_super_set_parent(struct clk_hw *hw, u8 index)
  58. {
  59. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  60. u32 val, state;
  61. int err = 0;
  62. u8 parent_index, shift;
  63. unsigned long flags = 0;
  64. if (mux->lock)
  65. spin_lock_irqsave(mux->lock, flags);
  66. val = readl_relaxed(mux->reg);
  67. state = val & SUPER_STATE_MASK;
  68. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  69. (state != super_state(SUPER_STATE_IDLE)));
  70. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  71. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  72. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  73. /*
  74. * For LP mode super-clock switch between PLLX direct
  75. * and divided-by-2 outputs is allowed only when other
  76. * than PLLX clock source is current parent.
  77. */
  78. if ((mux->flags & TEGRA_DIVIDER_2) && ((index == mux->div2_index) ||
  79. (index == mux->pllx_index))) {
  80. parent_index = clk_super_get_parent(hw);
  81. if ((parent_index == mux->div2_index) ||
  82. (parent_index == mux->pllx_index)) {
  83. err = -EINVAL;
  84. goto out;
  85. }
  86. val ^= SUPER_LP_DIV2_BYPASS;
  87. writel_relaxed(val, mux->reg);
  88. udelay(2);
  89. if (index == mux->div2_index)
  90. index = mux->pllx_index;
  91. }
  92. val &= ~((super_state_to_src_mask(mux)) << shift);
  93. val |= (index & (super_state_to_src_mask(mux))) << shift;
  94. writel_relaxed(val, mux->reg);
  95. udelay(2);
  96. out:
  97. if (mux->lock)
  98. spin_unlock_irqrestore(mux->lock, flags);
  99. return err;
  100. }
  101. const struct clk_ops tegra_clk_super_mux_ops = {
  102. .get_parent = clk_super_get_parent,
  103. .set_parent = clk_super_set_parent,
  104. };
  105. static long clk_super_round_rate(struct clk_hw *hw, unsigned long rate,
  106. unsigned long *parent_rate)
  107. {
  108. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  109. struct clk_hw *div_hw = &super->frac_div.hw;
  110. __clk_hw_set_clk(div_hw, hw);
  111. return super->div_ops->round_rate(div_hw, rate, parent_rate);
  112. }
  113. static unsigned long clk_super_recalc_rate(struct clk_hw *hw,
  114. unsigned long parent_rate)
  115. {
  116. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  117. struct clk_hw *div_hw = &super->frac_div.hw;
  118. __clk_hw_set_clk(div_hw, hw);
  119. return super->div_ops->recalc_rate(div_hw, parent_rate);
  120. }
  121. static int clk_super_set_rate(struct clk_hw *hw, unsigned long rate,
  122. unsigned long parent_rate)
  123. {
  124. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  125. struct clk_hw *div_hw = &super->frac_div.hw;
  126. __clk_hw_set_clk(div_hw, hw);
  127. return super->div_ops->set_rate(div_hw, rate, parent_rate);
  128. }
  129. const struct clk_ops tegra_clk_super_ops = {
  130. .get_parent = clk_super_get_parent,
  131. .set_parent = clk_super_set_parent,
  132. .set_rate = clk_super_set_rate,
  133. .round_rate = clk_super_round_rate,
  134. .recalc_rate = clk_super_recalc_rate,
  135. };
  136. struct clk *tegra_clk_register_super_mux(const char *name,
  137. const char **parent_names, u8 num_parents,
  138. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  139. u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock)
  140. {
  141. struct tegra_clk_super_mux *super;
  142. struct clk *clk;
  143. struct clk_init_data init;
  144. super = kzalloc(sizeof(*super), GFP_KERNEL);
  145. if (!super)
  146. return ERR_PTR(-ENOMEM);
  147. init.name = name;
  148. init.ops = &tegra_clk_super_mux_ops;
  149. init.flags = flags;
  150. init.parent_names = parent_names;
  151. init.num_parents = num_parents;
  152. super->reg = reg;
  153. super->pllx_index = pllx_index;
  154. super->div2_index = div2_index;
  155. super->lock = lock;
  156. super->width = width;
  157. super->flags = clk_super_flags;
  158. /* Data in .init is copied by clk_register(), so stack variable OK */
  159. super->hw.init = &init;
  160. clk = clk_register(NULL, &super->hw);
  161. if (IS_ERR(clk))
  162. kfree(super);
  163. return clk;
  164. }
  165. struct clk *tegra_clk_register_super_clk(const char *name,
  166. const char * const *parent_names, u8 num_parents,
  167. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  168. spinlock_t *lock)
  169. {
  170. struct tegra_clk_super_mux *super;
  171. struct clk *clk;
  172. struct clk_init_data init;
  173. super = kzalloc(sizeof(*super), GFP_KERNEL);
  174. if (!super)
  175. return ERR_PTR(-ENOMEM);
  176. init.name = name;
  177. init.ops = &tegra_clk_super_ops;
  178. init.flags = flags;
  179. init.parent_names = parent_names;
  180. init.num_parents = num_parents;
  181. super->reg = reg;
  182. super->lock = lock;
  183. super->width = 4;
  184. super->flags = clk_super_flags;
  185. super->frac_div.reg = reg + 4;
  186. super->frac_div.shift = 16;
  187. super->frac_div.width = 8;
  188. super->frac_div.frac_width = 1;
  189. super->frac_div.lock = lock;
  190. super->div_ops = &tegra_clk_frac_div_ops;
  191. /* Data in .init is copied by clk_register(), so stack variable OK */
  192. super->hw.init = &init;
  193. clk = clk_register(NULL, &super->hw);
  194. if (IS_ERR(clk))
  195. kfree(super);
  196. return clk;
  197. }