clk-divider.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/clk-provider.h>
  21. #include "clk.h"
  22. #define pll_out_override(p) (BIT((p->shift - 6)))
  23. #define div_mask(d) ((1 << (d->width)) - 1)
  24. #define get_mul(d) (1 << d->frac_width)
  25. #define get_max_div(d) div_mask(d)
  26. #define PERIPH_CLK_UART_DIV_ENB BIT(24)
  27. static int get_div(struct tegra_clk_frac_div *divider, unsigned long rate,
  28. unsigned long parent_rate)
  29. {
  30. s64 divider_ux1 = parent_rate;
  31. u8 flags = divider->flags;
  32. int mul;
  33. if (!rate)
  34. return 0;
  35. mul = get_mul(divider);
  36. if (!(flags & TEGRA_DIVIDER_INT))
  37. divider_ux1 *= mul;
  38. if (flags & TEGRA_DIVIDER_ROUND_UP)
  39. divider_ux1 += rate - 1;
  40. do_div(divider_ux1, rate);
  41. if (flags & TEGRA_DIVIDER_INT)
  42. divider_ux1 *= mul;
  43. divider_ux1 -= mul;
  44. if (divider_ux1 < 0)
  45. return 0;
  46. if (divider_ux1 > get_max_div(divider))
  47. return get_max_div(divider);
  48. return divider_ux1;
  49. }
  50. static unsigned long clk_frac_div_recalc_rate(struct clk_hw *hw,
  51. unsigned long parent_rate)
  52. {
  53. struct tegra_clk_frac_div *divider = to_clk_frac_div(hw);
  54. u32 reg;
  55. int div, mul;
  56. u64 rate = parent_rate;
  57. reg = readl_relaxed(divider->reg) >> divider->shift;
  58. div = reg & div_mask(divider);
  59. mul = get_mul(divider);
  60. div += mul;
  61. rate *= mul;
  62. rate += div - 1;
  63. do_div(rate, div);
  64. return rate;
  65. }
  66. static long clk_frac_div_round_rate(struct clk_hw *hw, unsigned long rate,
  67. unsigned long *prate)
  68. {
  69. struct tegra_clk_frac_div *divider = to_clk_frac_div(hw);
  70. int div, mul;
  71. unsigned long output_rate = *prate;
  72. if (!rate)
  73. return output_rate;
  74. div = get_div(divider, rate, output_rate);
  75. if (div < 0)
  76. return *prate;
  77. mul = get_mul(divider);
  78. return DIV_ROUND_UP(output_rate * mul, div + mul);
  79. }
  80. static int clk_frac_div_set_rate(struct clk_hw *hw, unsigned long rate,
  81. unsigned long parent_rate)
  82. {
  83. struct tegra_clk_frac_div *divider = to_clk_frac_div(hw);
  84. int div;
  85. unsigned long flags = 0;
  86. u32 val;
  87. div = get_div(divider, rate, parent_rate);
  88. if (div < 0)
  89. return div;
  90. if (divider->lock)
  91. spin_lock_irqsave(divider->lock, flags);
  92. val = readl_relaxed(divider->reg);
  93. val &= ~(div_mask(divider) << divider->shift);
  94. val |= div << divider->shift;
  95. if (divider->flags & TEGRA_DIVIDER_UART) {
  96. if (div)
  97. val |= PERIPH_CLK_UART_DIV_ENB;
  98. else
  99. val &= ~PERIPH_CLK_UART_DIV_ENB;
  100. }
  101. if (divider->flags & TEGRA_DIVIDER_FIXED)
  102. val |= pll_out_override(divider);
  103. writel_relaxed(val, divider->reg);
  104. if (divider->lock)
  105. spin_unlock_irqrestore(divider->lock, flags);
  106. return 0;
  107. }
  108. const struct clk_ops tegra_clk_frac_div_ops = {
  109. .recalc_rate = clk_frac_div_recalc_rate,
  110. .set_rate = clk_frac_div_set_rate,
  111. .round_rate = clk_frac_div_round_rate,
  112. };
  113. struct clk *tegra_clk_register_divider(const char *name,
  114. const char *parent_name, void __iomem *reg,
  115. unsigned long flags, u8 clk_divider_flags, u8 shift, u8 width,
  116. u8 frac_width, spinlock_t *lock)
  117. {
  118. struct tegra_clk_frac_div *divider;
  119. struct clk *clk;
  120. struct clk_init_data init;
  121. divider = kzalloc(sizeof(*divider), GFP_KERNEL);
  122. if (!divider) {
  123. pr_err("%s: could not allocate fractional divider clk\n",
  124. __func__);
  125. return ERR_PTR(-ENOMEM);
  126. }
  127. init.name = name;
  128. init.ops = &tegra_clk_frac_div_ops;
  129. init.flags = flags;
  130. init.parent_names = parent_name ? &parent_name : NULL;
  131. init.num_parents = parent_name ? 1 : 0;
  132. divider->reg = reg;
  133. divider->shift = shift;
  134. divider->width = width;
  135. divider->frac_width = frac_width;
  136. divider->lock = lock;
  137. divider->flags = clk_divider_flags;
  138. /* Data in .init is copied by clk_register(), so stack variable OK */
  139. divider->hw.init = &init;
  140. clk = clk_register(NULL, &divider->hw);
  141. if (IS_ERR(clk))
  142. kfree(divider);
  143. return clk;
  144. }
  145. static const struct clk_div_table mc_div_table[] = {
  146. { .val = 0, .div = 2 },
  147. { .val = 1, .div = 1 },
  148. { .val = 0, .div = 0 },
  149. };
  150. struct clk *tegra_clk_register_mc(const char *name, const char *parent_name,
  151. void __iomem *reg, spinlock_t *lock)
  152. {
  153. return clk_register_divider_table(NULL, name, parent_name, 0, reg,
  154. 16, 1, 0, mc_div_table, lock);
  155. }