clk-factor.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2015 Maxime Ripard <maxime.ripard@free-electrons.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/of.h>
  13. #define to_clk_factor(_hw) container_of(_hw, struct clk_factor, hw)
  14. static unsigned long __get_mult(struct clk_factor *factor,
  15. unsigned long rate,
  16. unsigned long parent_rate)
  17. {
  18. if (factor->flags & CLK_FACTOR_ROUND_CLOSEST)
  19. return DIV_ROUND_CLOSEST(rate, parent_rate);
  20. return rate / parent_rate;
  21. }
  22. static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
  23. unsigned long parent_rate)
  24. {
  25. struct clk_factor *factor = to_clk_factor(hw);
  26. unsigned long val;
  27. val = clk_readl(factor->reg) >> factor->shift;
  28. val &= GENMASK(factor->width, 0);
  29. if (!val && factor->flags & CLK_FACTOR_ZERO_BYPASS)
  30. val = 1;
  31. return parent_rate * val;
  32. }
  33. static bool __is_best_rate(unsigned long rate, unsigned long new,
  34. unsigned long best, unsigned long flags)
  35. {
  36. if (flags & CLK_FACTOR_ROUND_CLOSEST)
  37. return abs(rate - new) < abs(rate - best);
  38. return new >= rate && new < best;
  39. }
  40. static unsigned long clk_factor_bestmult(struct clk_hw *hw, unsigned long rate,
  41. unsigned long *best_parent_rate,
  42. u8 width, unsigned long flags)
  43. {
  44. unsigned long orig_parent_rate = *best_parent_rate;
  45. unsigned long parent_rate, current_rate, best_rate = ~0;
  46. unsigned int i, bestmult = 0;
  47. if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT))
  48. return rate / *best_parent_rate;
  49. for (i = 1; i < ((1 << width) - 1); i++) {
  50. if (rate * i == orig_parent_rate) {
  51. /*
  52. * This is the best case for us if we have a
  53. * perfect match without changing the parent
  54. * rate.
  55. */
  56. *best_parent_rate = orig_parent_rate;
  57. return i;
  58. }
  59. parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
  60. rate / i);
  61. current_rate = parent_rate * i;
  62. if (__is_best_rate(rate, current_rate, best_rate, flags)) {
  63. bestmult = i;
  64. best_rate = current_rate;
  65. *best_parent_rate = parent_rate;
  66. }
  67. }
  68. return bestmult;
  69. }
  70. static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate,
  71. unsigned long *parent_rate)
  72. {
  73. struct clk_factor *factor = to_clk_factor(hw);
  74. unsigned long mult = clk_factor_bestmult(hw, rate, parent_rate,
  75. factor->width, factor->flags);
  76. return *parent_rate * mult;
  77. }
  78. static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate,
  79. unsigned long parent_rate)
  80. {
  81. struct clk_factor *factor = to_clk_factor(hw);
  82. unsigned long mult = __get_mult(factor, rate, parent_rate);
  83. unsigned long uninitialized_var(flags);
  84. struct clk *clk = hw->clk;
  85. unsigned long val;
  86. if (factor->lock)
  87. spin_lock_irqsave(factor->lock, flags);
  88. val = clk_readl(factor->reg);
  89. val &= ~GENMASK(factor->width + factor->shift, factor->shift);
  90. val |= mult << factor->shift;
  91. clk_writel(val, factor->reg);
  92. if (factor->lock)
  93. spin_unlock_irqrestore(factor->lock, flags);
  94. return 0;
  95. }
  96. const struct clk_ops clk_factor_ops = {
  97. .recalc_rate = clk_factor_recalc_rate,
  98. .round_rate = clk_factor_round_rate,
  99. .set_rate = clk_factor_set_rate,
  100. };
  101. EXPORT_SYMBOL_GPL(clk_factor_ops);
  102. struct clk *clk_register_factor(struct device *dev, const char *name,
  103. const char *parent_name, unsigned long flags,
  104. void __iomem *reg, u8 shift, u8 width,
  105. u8 clk_factor_flags, spinlock_t *lock)
  106. {
  107. struct clk_init_data init;
  108. struct clk_factor *factor;
  109. struct clk *clk;
  110. factor = kmalloc(sizeof(*factor), GFP_KERNEL);
  111. if (!factor) {
  112. pr_err("%s: could not allocate fixed factor clk\n", __func__);
  113. return ERR_PTR(-ENOMEM);
  114. }
  115. init.name = name;
  116. init.ops = &clk_factor_ops;
  117. init.flags = flags | CLK_IS_BASIC;
  118. init.parent_names = &parent_name;
  119. init.num_parents = 1;
  120. factor->reg = reg;
  121. factor->shift = shift;
  122. factor->width = width;
  123. factor->flags = clk_factor_flags;
  124. factor->lock = lock;
  125. factor->hw.init = &init;
  126. clk = clk_register(dev, &factor->hw);
  127. if (IS_ERR(clk))
  128. kfree(factor);
  129. return clk;
  130. }
  131. EXPORT_SYMBOL_GPL(clk_register_factor);
  132. void clk_unregister_factor(struct clk *clk)
  133. {
  134. struct clk_factor *factor;
  135. struct clk_hw *hw;
  136. hw = __clk_get_hw(clk);
  137. if (!hw)
  138. return;
  139. factor = to_clk_factor(hw);
  140. clk_unregister(clk);
  141. kfree(factor);
  142. }
  143. EXPORT_SYMBOL_GPL(clk_unregister_factor);