clk-ref.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 int clk_ref_determine_rate(struct clk_hw *hw,
  49. struct clk_rate_request *req)
  50. {
  51. unsigned long parent_rate = req->best_parent_rate;
  52. u64 tmp = parent_rate;
  53. u8 frac;
  54. tmp = tmp * 18 + req->rate / 2;
  55. do_div(tmp, req->rate);
  56. frac = clamp(tmp, 18, 35);
  57. tmp = parent_rate;
  58. tmp *= 18;
  59. do_div(tmp, frac);
  60. req->rate = tmp;
  61. return 0;
  62. }
  63. static int clk_ref_set_rate(struct clk_hw *hw, unsigned long rate,
  64. unsigned long parent_rate)
  65. {
  66. struct clk_ref *ref = to_clk_ref(hw);
  67. unsigned long flags;
  68. u64 tmp = parent_rate;
  69. u32 val;
  70. u8 frac, shift = ref->idx * 8;
  71. tmp = tmp * 18 + rate / 2;
  72. do_div(tmp, rate);
  73. frac = clamp(tmp, 18, 35);
  74. spin_lock_irqsave(&mxs_lock, flags);
  75. val = readl_relaxed(ref->reg);
  76. val &= ~(0x3f << shift);
  77. val |= frac << shift;
  78. writel_relaxed(val, ref->reg);
  79. spin_unlock_irqrestore(&mxs_lock, flags);
  80. return 0;
  81. }
  82. static const struct clk_ops clk_ref_ops = {
  83. .enable = clk_ref_enable,
  84. .disable = clk_ref_disable,
  85. .recalc_rate = clk_ref_recalc_rate,
  86. .determine_rate = clk_ref_determine_rate,
  87. .set_rate = clk_ref_set_rate,
  88. };
  89. struct clk *mxs_clk_ref(const char *name, const char *parent_name,
  90. void __iomem *reg, u8 idx)
  91. {
  92. struct clk_ref *ref;
  93. struct clk *clk;
  94. struct clk_init_data init;
  95. ref = kzalloc_obj(*ref);
  96. if (!ref)
  97. return ERR_PTR(-ENOMEM);
  98. init.name = name;
  99. init.ops = &clk_ref_ops;
  100. init.flags = 0;
  101. init.parent_names = (parent_name ? &parent_name: NULL);
  102. init.num_parents = (parent_name ? 1 : 0);
  103. ref->reg = reg;
  104. ref->idx = idx;
  105. ref->hw.init = &init;
  106. clk = clk_register(NULL, &ref->hw);
  107. if (IS_ERR(clk))
  108. kfree(ref);
  109. return clk;
  110. }