clk-fractional-divider.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2014 Intel Corporation
  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. * Adjustable fractional divider clock implementation.
  9. * Output rate = (m / n) * parent_rate.
  10. * Uses rational best approximation algorithm.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/rational.h>
  17. #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
  18. static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
  19. unsigned long parent_rate)
  20. {
  21. struct clk_fractional_divider *fd = to_clk_fd(hw);
  22. unsigned long flags = 0;
  23. unsigned long m, n;
  24. u32 val;
  25. u64 ret;
  26. if (fd->lock)
  27. spin_lock_irqsave(fd->lock, flags);
  28. else
  29. __acquire(fd->lock);
  30. val = clk_readl(fd->reg);
  31. if (fd->lock)
  32. spin_unlock_irqrestore(fd->lock, flags);
  33. else
  34. __release(fd->lock);
  35. m = (val & fd->mmask) >> fd->mshift;
  36. n = (val & fd->nmask) >> fd->nshift;
  37. if (!n || !m)
  38. return parent_rate;
  39. ret = (u64)parent_rate * m;
  40. do_div(ret, n);
  41. return ret;
  42. }
  43. static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
  44. unsigned long *parent_rate)
  45. {
  46. struct clk_fractional_divider *fd = to_clk_fd(hw);
  47. unsigned long scale;
  48. unsigned long m, n;
  49. u64 ret;
  50. if (!rate || rate >= *parent_rate)
  51. return *parent_rate;
  52. /*
  53. * Get rate closer to *parent_rate to guarantee there is no overflow
  54. * for m and n. In the result it will be the nearest rate left shifted
  55. * by (scale - fd->nwidth) bits.
  56. */
  57. scale = fls_long(*parent_rate / rate - 1);
  58. if (scale > fd->nwidth)
  59. rate <<= scale - fd->nwidth;
  60. rational_best_approximation(rate, *parent_rate,
  61. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  62. &m, &n);
  63. ret = (u64)*parent_rate * m;
  64. do_div(ret, n);
  65. return ret;
  66. }
  67. static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
  68. unsigned long parent_rate)
  69. {
  70. struct clk_fractional_divider *fd = to_clk_fd(hw);
  71. unsigned long flags = 0;
  72. unsigned long m, n;
  73. u32 val;
  74. rational_best_approximation(rate, parent_rate,
  75. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  76. &m, &n);
  77. if (fd->lock)
  78. spin_lock_irqsave(fd->lock, flags);
  79. else
  80. __acquire(fd->lock);
  81. val = clk_readl(fd->reg);
  82. val &= ~(fd->mmask | fd->nmask);
  83. val |= (m << fd->mshift) | (n << fd->nshift);
  84. clk_writel(val, fd->reg);
  85. if (fd->lock)
  86. spin_unlock_irqrestore(fd->lock, flags);
  87. else
  88. __release(fd->lock);
  89. return 0;
  90. }
  91. const struct clk_ops clk_fractional_divider_ops = {
  92. .recalc_rate = clk_fd_recalc_rate,
  93. .round_rate = clk_fd_round_rate,
  94. .set_rate = clk_fd_set_rate,
  95. };
  96. EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
  97. struct clk *clk_register_fractional_divider(struct device *dev,
  98. const char *name, const char *parent_name, unsigned long flags,
  99. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  100. u8 clk_divider_flags, spinlock_t *lock)
  101. {
  102. struct clk_fractional_divider *fd;
  103. struct clk_init_data init;
  104. struct clk *clk;
  105. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  106. if (!fd)
  107. return ERR_PTR(-ENOMEM);
  108. init.name = name;
  109. init.ops = &clk_fractional_divider_ops;
  110. init.flags = flags | CLK_IS_BASIC;
  111. init.parent_names = parent_name ? &parent_name : NULL;
  112. init.num_parents = parent_name ? 1 : 0;
  113. fd->reg = reg;
  114. fd->mshift = mshift;
  115. fd->mwidth = mwidth;
  116. fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
  117. fd->nshift = nshift;
  118. fd->nwidth = nwidth;
  119. fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
  120. fd->flags = clk_divider_flags;
  121. fd->lock = lock;
  122. fd->hw.init = &init;
  123. clk = clk_register(dev, &fd->hw);
  124. if (IS_ERR(clk))
  125. kfree(fd);
  126. return clk;
  127. }
  128. EXPORT_SYMBOL_GPL(clk_register_fractional_divider);