clk-ref.c 3.0 KB

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