clk-super.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_ops = {
  102. .get_parent = clk_super_get_parent,
  103. .set_parent = clk_super_set_parent,
  104. };
  105. struct clk *tegra_clk_register_super_mux(const char *name,
  106. const char **parent_names, u8 num_parents,
  107. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  108. u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock)
  109. {
  110. struct tegra_clk_super_mux *super;
  111. struct clk *clk;
  112. struct clk_init_data init;
  113. super = kzalloc(sizeof(*super), GFP_KERNEL);
  114. if (!super) {
  115. pr_err("%s: could not allocate super clk\n", __func__);
  116. return ERR_PTR(-ENOMEM);
  117. }
  118. init.name = name;
  119. init.ops = &tegra_clk_super_ops;
  120. init.flags = flags;
  121. init.parent_names = parent_names;
  122. init.num_parents = num_parents;
  123. super->reg = reg;
  124. super->pllx_index = pllx_index;
  125. super->div2_index = div2_index;
  126. super->lock = lock;
  127. super->width = width;
  128. super->flags = clk_super_flags;
  129. /* Data in .init is copied by clk_register(), so stack variable OK */
  130. super->hw.init = &init;
  131. clk = clk_register(NULL, &super->hw);
  132. if (IS_ERR(clk))
  133. kfree(super);
  134. return clk;
  135. }