clk-frac.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/slab.h>
  15. #include "clk.h"
  16. /**
  17. * struct clk_frac - mxs fractional divider clock
  18. * @hw: clk_hw for the fractional divider clock
  19. * @reg: register address
  20. * @shift: the divider bit shift
  21. * @width: the divider bit width
  22. * @busy: busy bit shift
  23. *
  24. * The clock is an adjustable fractional divider with a busy bit to wait
  25. * when the divider is adjusted.
  26. */
  27. struct clk_frac {
  28. struct clk_hw hw;
  29. void __iomem *reg;
  30. u8 shift;
  31. u8 width;
  32. u8 busy;
  33. };
  34. #define to_clk_frac(_hw) container_of(_hw, struct clk_frac, hw)
  35. static unsigned long clk_frac_recalc_rate(struct clk_hw *hw,
  36. unsigned long parent_rate)
  37. {
  38. struct clk_frac *frac = to_clk_frac(hw);
  39. u32 div;
  40. u64 tmp_rate;
  41. div = readl_relaxed(frac->reg) >> frac->shift;
  42. div &= (1 << frac->width) - 1;
  43. tmp_rate = (u64)parent_rate * div;
  44. return tmp_rate >> frac->width;
  45. }
  46. static long clk_frac_round_rate(struct clk_hw *hw, unsigned long rate,
  47. unsigned long *prate)
  48. {
  49. struct clk_frac *frac = to_clk_frac(hw);
  50. unsigned long parent_rate = *prate;
  51. u32 div;
  52. u64 tmp, tmp_rate, result;
  53. if (rate > parent_rate)
  54. return -EINVAL;
  55. tmp = rate;
  56. tmp <<= frac->width;
  57. do_div(tmp, parent_rate);
  58. div = tmp;
  59. if (!div)
  60. return -EINVAL;
  61. tmp_rate = (u64)parent_rate * div;
  62. result = tmp_rate >> frac->width;
  63. if ((result << frac->width) < tmp_rate)
  64. result += 1;
  65. return result;
  66. }
  67. static int clk_frac_set_rate(struct clk_hw *hw, unsigned long rate,
  68. unsigned long parent_rate)
  69. {
  70. struct clk_frac *frac = to_clk_frac(hw);
  71. unsigned long flags;
  72. u32 div, val;
  73. u64 tmp;
  74. if (rate > parent_rate)
  75. return -EINVAL;
  76. tmp = rate;
  77. tmp <<= frac->width;
  78. do_div(tmp, parent_rate);
  79. div = tmp;
  80. if (!div)
  81. return -EINVAL;
  82. spin_lock_irqsave(&mxs_lock, flags);
  83. val = readl_relaxed(frac->reg);
  84. val &= ~(((1 << frac->width) - 1) << frac->shift);
  85. val |= div << frac->shift;
  86. writel_relaxed(val, frac->reg);
  87. spin_unlock_irqrestore(&mxs_lock, flags);
  88. return mxs_clk_wait(frac->reg, frac->busy);
  89. }
  90. static struct clk_ops clk_frac_ops = {
  91. .recalc_rate = clk_frac_recalc_rate,
  92. .round_rate = clk_frac_round_rate,
  93. .set_rate = clk_frac_set_rate,
  94. };
  95. struct clk *mxs_clk_frac(const char *name, const char *parent_name,
  96. void __iomem *reg, u8 shift, u8 width, u8 busy)
  97. {
  98. struct clk_frac *frac;
  99. struct clk *clk;
  100. struct clk_init_data init;
  101. frac = kzalloc(sizeof(*frac), GFP_KERNEL);
  102. if (!frac)
  103. return ERR_PTR(-ENOMEM);
  104. init.name = name;
  105. init.ops = &clk_frac_ops;
  106. init.flags = CLK_SET_RATE_PARENT;
  107. init.parent_names = (parent_name ? &parent_name: NULL);
  108. init.num_parents = (parent_name ? 1 : 0);
  109. frac->reg = reg;
  110. frac->shift = shift;
  111. frac->width = width;
  112. frac->busy = busy;
  113. frac->hw.init = &init;
  114. clk = clk_register(NULL, &frac->hw);
  115. if (IS_ERR(clk))
  116. kfree(frac);
  117. return clk;
  118. }