clk-frac-synth.c 3.6 KB

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