clk-frac.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * mmp factor clock operation source file
  3. *
  4. * Copyright (C) 2012 Marvell
  5. * Chao Xie <xiechao.mail@gmail.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include "clk.h"
  16. /*
  17. * It is M/N clock
  18. *
  19. * Fout from synthesizer can be given from two equations:
  20. * numerator/denominator = Fin / (Fout * factor)
  21. */
  22. #define to_clk_factor(hw) container_of(hw, struct mmp_clk_factor, hw)
  23. static long clk_factor_round_rate(struct clk_hw *hw, unsigned long drate,
  24. unsigned long *prate)
  25. {
  26. struct mmp_clk_factor *factor = to_clk_factor(hw);
  27. unsigned long rate = 0, prev_rate;
  28. int i;
  29. for (i = 0; i < factor->ftbl_cnt; i++) {
  30. prev_rate = rate;
  31. rate = (((*prate / 10000) * factor->ftbl[i].den) /
  32. (factor->ftbl[i].num * factor->masks->factor)) * 10000;
  33. if (rate > drate)
  34. break;
  35. }
  36. if ((i == 0) || (i == factor->ftbl_cnt)) {
  37. return rate;
  38. } else {
  39. if ((drate - prev_rate) > (rate - drate))
  40. return rate;
  41. else
  42. return prev_rate;
  43. }
  44. }
  45. static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
  46. unsigned long parent_rate)
  47. {
  48. struct mmp_clk_factor *factor = to_clk_factor(hw);
  49. struct mmp_clk_factor_masks *masks = factor->masks;
  50. unsigned int val, num, den;
  51. val = readl_relaxed(factor->base);
  52. /* calculate numerator */
  53. num = (val >> masks->num_shift) & masks->num_mask;
  54. /* calculate denominator */
  55. den = (val >> masks->den_shift) & masks->den_mask;
  56. if (!den)
  57. return 0;
  58. return (((parent_rate / 10000) * den) /
  59. (num * factor->masks->factor)) * 10000;
  60. }
  61. /* Configures new clock rate*/
  62. static int clk_factor_set_rate(struct clk_hw *hw, unsigned long drate,
  63. unsigned long prate)
  64. {
  65. struct mmp_clk_factor *factor = to_clk_factor(hw);
  66. struct mmp_clk_factor_masks *masks = factor->masks;
  67. int i;
  68. unsigned long val;
  69. unsigned long prev_rate, rate = 0;
  70. unsigned long flags = 0;
  71. for (i = 0; i < factor->ftbl_cnt; i++) {
  72. prev_rate = rate;
  73. rate = (((prate / 10000) * factor->ftbl[i].den) /
  74. (factor->ftbl[i].num * factor->masks->factor)) * 10000;
  75. if (rate > drate)
  76. break;
  77. }
  78. if (i > 0)
  79. i--;
  80. if (factor->lock)
  81. spin_lock_irqsave(factor->lock, flags);
  82. val = readl_relaxed(factor->base);
  83. val &= ~(masks->num_mask << masks->num_shift);
  84. val |= (factor->ftbl[i].num & masks->num_mask) << masks->num_shift;
  85. val &= ~(masks->den_mask << masks->den_shift);
  86. val |= (factor->ftbl[i].den & masks->den_mask) << masks->den_shift;
  87. writel_relaxed(val, factor->base);
  88. if (factor->lock)
  89. spin_unlock_irqrestore(factor->lock, flags);
  90. return 0;
  91. }
  92. static void clk_factor_init(struct clk_hw *hw)
  93. {
  94. struct mmp_clk_factor *factor = to_clk_factor(hw);
  95. struct mmp_clk_factor_masks *masks = factor->masks;
  96. u32 val, num, den;
  97. int i;
  98. unsigned long flags = 0;
  99. if (factor->lock)
  100. spin_lock_irqsave(factor->lock, flags);
  101. val = readl(factor->base);
  102. /* calculate numerator */
  103. num = (val >> masks->num_shift) & masks->num_mask;
  104. /* calculate denominator */
  105. den = (val >> masks->den_shift) & masks->den_mask;
  106. for (i = 0; i < factor->ftbl_cnt; i++)
  107. if (den == factor->ftbl[i].den && num == factor->ftbl[i].num)
  108. break;
  109. if (i >= factor->ftbl_cnt) {
  110. val &= ~(masks->num_mask << masks->num_shift);
  111. val |= (factor->ftbl[0].num & masks->num_mask) <<
  112. masks->num_shift;
  113. val &= ~(masks->den_mask << masks->den_shift);
  114. val |= (factor->ftbl[0].den & masks->den_mask) <<
  115. masks->den_shift;
  116. writel(val, factor->base);
  117. }
  118. if (factor->lock)
  119. spin_unlock_irqrestore(factor->lock, flags);
  120. }
  121. static struct clk_ops clk_factor_ops = {
  122. .recalc_rate = clk_factor_recalc_rate,
  123. .round_rate = clk_factor_round_rate,
  124. .set_rate = clk_factor_set_rate,
  125. .init = clk_factor_init,
  126. };
  127. struct clk *mmp_clk_register_factor(const char *name, const char *parent_name,
  128. unsigned long flags, void __iomem *base,
  129. struct mmp_clk_factor_masks *masks,
  130. struct mmp_clk_factor_tbl *ftbl,
  131. unsigned int ftbl_cnt, spinlock_t *lock)
  132. {
  133. struct mmp_clk_factor *factor;
  134. struct clk_init_data init;
  135. struct clk *clk;
  136. if (!masks) {
  137. pr_err("%s: must pass a clk_factor_mask\n", __func__);
  138. return ERR_PTR(-EINVAL);
  139. }
  140. factor = kzalloc(sizeof(*factor), GFP_KERNEL);
  141. if (!factor) {
  142. pr_err("%s: could not allocate factor clk\n", __func__);
  143. return ERR_PTR(-ENOMEM);
  144. }
  145. /* struct clk_aux assignments */
  146. factor->base = base;
  147. factor->masks = masks;
  148. factor->ftbl = ftbl;
  149. factor->ftbl_cnt = ftbl_cnt;
  150. factor->hw.init = &init;
  151. factor->lock = lock;
  152. init.name = name;
  153. init.ops = &clk_factor_ops;
  154. init.flags = flags;
  155. init.parent_names = &parent_name;
  156. init.num_parents = 1;
  157. clk = clk_register(NULL, &factor->hw);
  158. if (IS_ERR_OR_NULL(clk))
  159. kfree(factor);
  160. return clk;
  161. }