clk-frac-synth.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2012 ST Microelectronics
  3. * Viresh Kumar <vireshk@kernel.org>
  4. *
  5. * This file is licensed under the terms of the GNU General Public
  6. * License version 2. This program is licensed "as is" without any
  7. * warranty of any kind, whether express or implied.
  8. *
  9. * Fractional Synthesizer clock implementation
  10. */
  11. #define pr_fmt(fmt) "clk-frac-synth: " fmt
  12. #include <linux/clk-provider.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/err.h>
  16. #include "clk.h"
  17. #define DIV_FACTOR_MASK 0x1FFFF
  18. /*
  19. * DOC: Fractional Synthesizer clock
  20. *
  21. * Fout from synthesizer can be given from below equation:
  22. *
  23. * Fout= Fin/2*div (division factor)
  24. * div is 17 bits:-
  25. * 0-13 (fractional part)
  26. * 14-16 (integer part)
  27. * div is (16-14 bits).(13-0 bits) (in binary)
  28. *
  29. * Fout = Fin/(2 * div)
  30. * Fout = ((Fin / 10000)/(2 * div)) * 10000
  31. * Fout = (2^14 * (Fin / 10000)/(2^14 * (2 * div))) * 10000
  32. * Fout = (((Fin / 10000) << 14)/(2 * (div << 14))) * 10000
  33. *
  34. * div << 14 simply 17 bit value written at register.
  35. * Max error due to scaling down by 10000 is 10 KHz
  36. */
  37. #define to_clk_frac(_hw) container_of(_hw, struct clk_frac, hw)
  38. static unsigned long frac_calc_rate(struct clk_hw *hw, unsigned long prate,
  39. int index)
  40. {
  41. struct clk_frac *frac = to_clk_frac(hw);
  42. struct frac_rate_tbl *rtbl = frac->rtbl;
  43. prate /= 10000;
  44. prate <<= 14;
  45. prate /= (2 * rtbl[index].div);
  46. prate *= 10000;
  47. return prate;
  48. }
  49. static long clk_frac_round_rate(struct clk_hw *hw, unsigned long drate,
  50. unsigned long *prate)
  51. {
  52. struct clk_frac *frac = to_clk_frac(hw);
  53. int unused;
  54. return clk_round_rate_index(hw, drate, *prate, frac_calc_rate,
  55. frac->rtbl_cnt, &unused);
  56. }
  57. static unsigned long clk_frac_recalc_rate(struct clk_hw *hw,
  58. unsigned long parent_rate)
  59. {
  60. struct clk_frac *frac = to_clk_frac(hw);
  61. unsigned long flags = 0;
  62. unsigned int div = 1, val;
  63. if (frac->lock)
  64. spin_lock_irqsave(frac->lock, flags);
  65. val = readl_relaxed(frac->reg);
  66. if (frac->lock)
  67. spin_unlock_irqrestore(frac->lock, flags);
  68. div = val & DIV_FACTOR_MASK;
  69. if (!div)
  70. return 0;
  71. parent_rate = parent_rate / 10000;
  72. parent_rate = (parent_rate << 14) / (2 * div);
  73. return parent_rate * 10000;
  74. }
  75. /* Configures new clock rate of frac */
  76. static int clk_frac_set_rate(struct clk_hw *hw, unsigned long drate,
  77. unsigned long prate)
  78. {
  79. struct clk_frac *frac = to_clk_frac(hw);
  80. struct frac_rate_tbl *rtbl = frac->rtbl;
  81. unsigned long flags = 0, val;
  82. int i;
  83. clk_round_rate_index(hw, drate, prate, frac_calc_rate, frac->rtbl_cnt,
  84. &i);
  85. if (frac->lock)
  86. spin_lock_irqsave(frac->lock, flags);
  87. val = readl_relaxed(frac->reg) & ~DIV_FACTOR_MASK;
  88. val |= rtbl[i].div & DIV_FACTOR_MASK;
  89. writel_relaxed(val, frac->reg);
  90. if (frac->lock)
  91. spin_unlock_irqrestore(frac->lock, flags);
  92. return 0;
  93. }
  94. static struct clk_ops clk_frac_ops = {
  95. .recalc_rate = clk_frac_recalc_rate,
  96. .round_rate = clk_frac_round_rate,
  97. .set_rate = clk_frac_set_rate,
  98. };
  99. struct clk *clk_register_frac(const char *name, const char *parent_name,
  100. unsigned long flags, void __iomem *reg,
  101. struct frac_rate_tbl *rtbl, u8 rtbl_cnt, spinlock_t *lock)
  102. {
  103. struct clk_init_data init;
  104. struct clk_frac *frac;
  105. struct clk *clk;
  106. if (!name || !parent_name || !reg || !rtbl || !rtbl_cnt) {
  107. pr_err("Invalid arguments passed");
  108. return ERR_PTR(-EINVAL);
  109. }
  110. frac = kzalloc(sizeof(*frac), GFP_KERNEL);
  111. if (!frac) {
  112. pr_err("could not allocate frac clk\n");
  113. return ERR_PTR(-ENOMEM);
  114. }
  115. /* struct clk_frac assignments */
  116. frac->reg = reg;
  117. frac->rtbl = rtbl;
  118. frac->rtbl_cnt = rtbl_cnt;
  119. frac->lock = lock;
  120. frac->hw.init = &init;
  121. init.name = name;
  122. init.ops = &clk_frac_ops;
  123. init.flags = flags;
  124. init.parent_names = &parent_name;
  125. init.num_parents = 1;
  126. clk = clk_register(NULL, &frac->hw);
  127. if (!IS_ERR_OR_NULL(clk))
  128. return clk;
  129. pr_err("clk register failed\n");
  130. kfree(frac);
  131. return NULL;
  132. }