clk-ref.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include "clk.h"
  17. /**
  18. * struct clk_ref - mxs reference clock
  19. * @hw: clk_hw for the reference clock
  20. * @reg: register address
  21. * @idx: the index of the reference clock within the same register
  22. *
  23. * The mxs reference clock sources from pll. Every 4 reference clocks share
  24. * one register space, and @idx is used to identify them. Each reference
  25. * clock has a gate control and a fractional * divider. The rate is calculated
  26. * as pll rate * (18 / FRAC), where FRAC = 18 ~ 35.
  27. */
  28. struct clk_ref {
  29. struct clk_hw hw;
  30. void __iomem *reg;
  31. u8 idx;
  32. };
  33. #define to_clk_ref(_hw) container_of(_hw, struct clk_ref, hw)
  34. static int clk_ref_enable(struct clk_hw *hw)
  35. {
  36. struct clk_ref *ref = to_clk_ref(hw);
  37. writel_relaxed(1 << ((ref->idx + 1) * 8 - 1), ref->reg + CLR);
  38. return 0;
  39. }
  40. static void clk_ref_disable(struct clk_hw *hw)
  41. {
  42. struct clk_ref *ref = to_clk_ref(hw);
  43. writel_relaxed(1 << ((ref->idx + 1) * 8 - 1), ref->reg + SET);
  44. }
  45. static unsigned long clk_ref_recalc_rate(struct clk_hw *hw,
  46. unsigned long parent_rate)
  47. {
  48. struct clk_ref *ref = to_clk_ref(hw);
  49. u64 tmp = parent_rate;
  50. u8 frac = (readl_relaxed(ref->reg) >> (ref->idx * 8)) & 0x3f;
  51. tmp *= 18;
  52. do_div(tmp, frac);
  53. return tmp;
  54. }
  55. static long clk_ref_round_rate(struct clk_hw *hw, unsigned long rate,
  56. unsigned long *prate)
  57. {
  58. unsigned long parent_rate = *prate;
  59. u64 tmp = parent_rate;
  60. u8 frac;
  61. tmp = tmp * 18 + rate / 2;
  62. do_div(tmp, rate);
  63. frac = tmp;
  64. if (frac < 18)
  65. frac = 18;
  66. else if (frac > 35)
  67. frac = 35;
  68. tmp = parent_rate;
  69. tmp *= 18;
  70. do_div(tmp, frac);
  71. return tmp;
  72. }
  73. static int clk_ref_set_rate(struct clk_hw *hw, unsigned long rate,
  74. unsigned long parent_rate)
  75. {
  76. struct clk_ref *ref = to_clk_ref(hw);
  77. unsigned long flags;
  78. u64 tmp = parent_rate;
  79. u32 val;
  80. u8 frac, shift = ref->idx * 8;
  81. tmp = tmp * 18 + rate / 2;
  82. do_div(tmp, rate);
  83. frac = tmp;
  84. if (frac < 18)
  85. frac = 18;
  86. else if (frac > 35)
  87. frac = 35;
  88. spin_lock_irqsave(&mxs_lock, flags);
  89. val = readl_relaxed(ref->reg);
  90. val &= ~(0x3f << shift);
  91. val |= frac << shift;
  92. writel_relaxed(val, ref->reg);
  93. spin_unlock_irqrestore(&mxs_lock, flags);
  94. return 0;
  95. }
  96. static const struct clk_ops clk_ref_ops = {
  97. .enable = clk_ref_enable,
  98. .disable = clk_ref_disable,
  99. .recalc_rate = clk_ref_recalc_rate,
  100. .round_rate = clk_ref_round_rate,
  101. .set_rate = clk_ref_set_rate,
  102. };
  103. struct clk *mxs_clk_ref(const char *name, const char *parent_name,
  104. void __iomem *reg, u8 idx)
  105. {
  106. struct clk_ref *ref;
  107. struct clk *clk;
  108. struct clk_init_data init;
  109. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  110. if (!ref)
  111. return ERR_PTR(-ENOMEM);
  112. init.name = name;
  113. init.ops = &clk_ref_ops;
  114. init.flags = 0;
  115. init.parent_names = (parent_name ? &parent_name: NULL);
  116. init.num_parents = (parent_name ? 1 : 0);
  117. ref->reg = reg;
  118. ref->idx = idx;
  119. ref->hw.init = &init;
  120. clk = clk_register(NULL, &ref->hw);
  121. if (IS_ERR(clk))
  122. kfree(ref);
  123. return clk;
  124. }