clk-fractional-divider.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/gcd.h>
  16. #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
  17. static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
  18. unsigned long parent_rate)
  19. {
  20. struct clk_fractional_divider *fd = to_clk_fd(hw);
  21. unsigned long flags = 0;
  22. u32 val, m, n;
  23. u64 ret;
  24. if (fd->lock)
  25. spin_lock_irqsave(fd->lock, flags);
  26. else
  27. __acquire(fd->lock);
  28. val = clk_readl(fd->reg);
  29. if (fd->lock)
  30. spin_unlock_irqrestore(fd->lock, flags);
  31. else
  32. __release(fd->lock);
  33. m = (val & fd->mmask) >> fd->mshift;
  34. n = (val & fd->nmask) >> fd->nshift;
  35. if (!n || !m)
  36. return parent_rate;
  37. ret = (u64)parent_rate * m;
  38. do_div(ret, n);
  39. return ret;
  40. }
  41. static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
  42. unsigned long *prate)
  43. {
  44. struct clk_fractional_divider *fd = to_clk_fd(hw);
  45. unsigned maxn = (fd->nmask >> fd->nshift) + 1;
  46. unsigned div;
  47. if (!rate || rate >= *prate)
  48. return *prate;
  49. div = gcd(*prate, rate);
  50. while ((*prate / div) > maxn) {
  51. div <<= 1;
  52. rate <<= 1;
  53. }
  54. return rate;
  55. }
  56. static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
  57. unsigned long parent_rate)
  58. {
  59. struct clk_fractional_divider *fd = to_clk_fd(hw);
  60. unsigned long flags = 0;
  61. unsigned long div;
  62. unsigned n, m;
  63. u32 val;
  64. div = gcd(parent_rate, rate);
  65. m = rate / div;
  66. n = parent_rate / div;
  67. if (fd->lock)
  68. spin_lock_irqsave(fd->lock, flags);
  69. else
  70. __acquire(fd->lock);
  71. val = clk_readl(fd->reg);
  72. val &= ~(fd->mmask | fd->nmask);
  73. val |= (m << fd->mshift) | (n << fd->nshift);
  74. clk_writel(val, fd->reg);
  75. if (fd->lock)
  76. spin_unlock_irqrestore(fd->lock, flags);
  77. else
  78. __release(fd->lock);
  79. return 0;
  80. }
  81. const struct clk_ops clk_fractional_divider_ops = {
  82. .recalc_rate = clk_fd_recalc_rate,
  83. .round_rate = clk_fd_round_rate,
  84. .set_rate = clk_fd_set_rate,
  85. };
  86. EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
  87. struct clk *clk_register_fractional_divider(struct device *dev,
  88. const char *name, const char *parent_name, unsigned long flags,
  89. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  90. u8 clk_divider_flags, spinlock_t *lock)
  91. {
  92. struct clk_fractional_divider *fd;
  93. struct clk_init_data init;
  94. struct clk *clk;
  95. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  96. if (!fd)
  97. return ERR_PTR(-ENOMEM);
  98. init.name = name;
  99. init.ops = &clk_fractional_divider_ops;
  100. init.flags = flags | CLK_IS_BASIC;
  101. init.parent_names = parent_name ? &parent_name : NULL;
  102. init.num_parents = parent_name ? 1 : 0;
  103. fd->reg = reg;
  104. fd->mshift = mshift;
  105. fd->mmask = (BIT(mwidth) - 1) << mshift;
  106. fd->nshift = nshift;
  107. fd->nmask = (BIT(nwidth) - 1) << nshift;
  108. fd->flags = clk_divider_flags;
  109. fd->lock = lock;
  110. fd->hw.init = &init;
  111. clk = clk_register(dev, &fd->hw);
  112. if (IS_ERR(clk))
  113. kfree(fd);
  114. return clk;
  115. }
  116. EXPORT_SYMBOL_GPL(clk_register_fractional_divider);