clk-super.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <linux/clk.h>
  23. #include "clk.h"
  24. #define SUPER_STATE_IDLE 0
  25. #define SUPER_STATE_RUN 1
  26. #define SUPER_STATE_IRQ 2
  27. #define SUPER_STATE_FIQ 3
  28. #define SUPER_STATE_SHIFT 28
  29. #define SUPER_STATE_MASK ((BIT(SUPER_STATE_IDLE) | BIT(SUPER_STATE_RUN) | \
  30. BIT(SUPER_STATE_IRQ) | BIT(SUPER_STATE_FIQ)) \
  31. << SUPER_STATE_SHIFT)
  32. #define SUPER_LP_DIV2_BYPASS (1 << 16)
  33. #define super_state(s) (BIT(s) << SUPER_STATE_SHIFT)
  34. #define super_state_to_src_shift(m, s) ((m->width * s))
  35. #define super_state_to_src_mask(m) (((1 << m->width) - 1))
  36. static u8 clk_super_get_parent(struct clk_hw *hw)
  37. {
  38. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  39. u32 val, state;
  40. u8 source, shift;
  41. val = readl_relaxed(mux->reg);
  42. state = val & SUPER_STATE_MASK;
  43. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  44. (state != super_state(SUPER_STATE_IDLE)));
  45. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  46. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  47. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  48. source = (val >> shift) & super_state_to_src_mask(mux);
  49. /*
  50. * If LP_DIV2_BYPASS is not set and PLLX is current parent then
  51. * PLLX/2 is the input source to CCLKLP.
  52. */
  53. if ((mux->flags & TEGRA_DIVIDER_2) && !(val & SUPER_LP_DIV2_BYPASS) &&
  54. (source == mux->pllx_index))
  55. source = mux->div2_index;
  56. return source;
  57. }
  58. static int clk_super_set_parent(struct clk_hw *hw, u8 index)
  59. {
  60. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  61. u32 val, state;
  62. u8 parent_index, shift;
  63. val = readl_relaxed(mux->reg);
  64. state = val & SUPER_STATE_MASK;
  65. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  66. (state != super_state(SUPER_STATE_IDLE)));
  67. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  68. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  69. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  70. /*
  71. * For LP mode super-clock switch between PLLX direct
  72. * and divided-by-2 outputs is allowed only when other
  73. * than PLLX clock source is current parent.
  74. */
  75. if ((mux->flags & TEGRA_DIVIDER_2) && ((index == mux->div2_index) ||
  76. (index == mux->pllx_index))) {
  77. parent_index = clk_super_get_parent(hw);
  78. if ((parent_index == mux->div2_index) ||
  79. (parent_index == mux->pllx_index))
  80. return -EINVAL;
  81. val ^= SUPER_LP_DIV2_BYPASS;
  82. writel_relaxed(val, mux->reg);
  83. udelay(2);
  84. if (index == mux->div2_index)
  85. index = mux->pllx_index;
  86. }
  87. val &= ~((super_state_to_src_mask(mux)) << shift);
  88. val |= (index & (super_state_to_src_mask(mux))) << shift;
  89. writel_relaxed(val, mux->reg);
  90. udelay(2);
  91. return 0;
  92. }
  93. const struct clk_ops tegra_clk_super_ops = {
  94. .get_parent = clk_super_get_parent,
  95. .set_parent = clk_super_set_parent,
  96. };
  97. struct clk *tegra_clk_register_super_mux(const char *name,
  98. const char **parent_names, u8 num_parents,
  99. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  100. u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock)
  101. {
  102. struct tegra_clk_super_mux *super;
  103. struct clk *clk;
  104. struct clk_init_data init;
  105. super = kzalloc(sizeof(*super), GFP_KERNEL);
  106. if (!super) {
  107. pr_err("%s: could not allocate super clk\n", __func__);
  108. return ERR_PTR(-ENOMEM);
  109. }
  110. init.name = name;
  111. init.ops = &tegra_clk_super_ops;
  112. init.flags = flags;
  113. init.parent_names = parent_names;
  114. init.num_parents = num_parents;
  115. super->reg = reg;
  116. super->pllx_index = pllx_index;
  117. super->div2_index = div2_index;
  118. super->lock = lock;
  119. super->width = width;
  120. super->flags = clk_super_flags;
  121. /* Data in .init is copied by clk_register(), so stack variable OK */
  122. super->hw.init = &init;
  123. clk = clk_register(NULL, &super->hw);
  124. if (IS_ERR(clk))
  125. kfree(super);
  126. return clk;
  127. }