ccu_phase.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2016 Maxime Ripard
  4. * Maxime Ripard <[email protected]>
  5. */
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include <linux/spinlock.h>
  9. #include "ccu_phase.h"
  10. static int ccu_phase_get_phase(struct clk_hw *hw)
  11. {
  12. struct ccu_phase *phase = hw_to_ccu_phase(hw);
  13. struct clk_hw *parent, *grandparent;
  14. unsigned int parent_rate, grandparent_rate;
  15. u16 step, parent_div;
  16. u32 reg;
  17. u8 delay;
  18. reg = readl(phase->common.base + phase->common.reg);
  19. delay = (reg >> phase->shift);
  20. delay &= (1 << phase->width) - 1;
  21. if (!delay)
  22. return 180;
  23. /* Get our parent clock, it's the one that can adjust its rate */
  24. parent = clk_hw_get_parent(hw);
  25. if (!parent)
  26. return -EINVAL;
  27. /* And its rate */
  28. parent_rate = clk_hw_get_rate(parent);
  29. if (!parent_rate)
  30. return -EINVAL;
  31. /* Now, get our parent's parent (most likely some PLL) */
  32. grandparent = clk_hw_get_parent(parent);
  33. if (!grandparent)
  34. return -EINVAL;
  35. /* And its rate */
  36. grandparent_rate = clk_hw_get_rate(grandparent);
  37. if (!grandparent_rate)
  38. return -EINVAL;
  39. /* Get our parent clock divider */
  40. parent_div = grandparent_rate / parent_rate;
  41. step = DIV_ROUND_CLOSEST(360, parent_div);
  42. return delay * step;
  43. }
  44. static int ccu_phase_set_phase(struct clk_hw *hw, int degrees)
  45. {
  46. struct ccu_phase *phase = hw_to_ccu_phase(hw);
  47. struct clk_hw *parent, *grandparent;
  48. unsigned int parent_rate, grandparent_rate;
  49. unsigned long flags;
  50. u32 reg;
  51. u8 delay;
  52. /* Get our parent clock, it's the one that can adjust its rate */
  53. parent = clk_hw_get_parent(hw);
  54. if (!parent)
  55. return -EINVAL;
  56. /* And its rate */
  57. parent_rate = clk_hw_get_rate(parent);
  58. if (!parent_rate)
  59. return -EINVAL;
  60. /* Now, get our parent's parent (most likely some PLL) */
  61. grandparent = clk_hw_get_parent(parent);
  62. if (!grandparent)
  63. return -EINVAL;
  64. /* And its rate */
  65. grandparent_rate = clk_hw_get_rate(grandparent);
  66. if (!grandparent_rate)
  67. return -EINVAL;
  68. if (degrees != 180) {
  69. u16 step, parent_div;
  70. /* Get our parent divider */
  71. parent_div = grandparent_rate / parent_rate;
  72. /*
  73. * We can only outphase the clocks by multiple of the
  74. * PLL's period.
  75. *
  76. * Since our parent clock is only a divider, and the
  77. * formula to get the outphasing in degrees is deg =
  78. * 360 * delta / period
  79. *
  80. * If we simplify this formula, we can see that the
  81. * only thing that we're concerned about is the number
  82. * of period we want to outphase our clock from, and
  83. * the divider set by our parent clock.
  84. */
  85. step = DIV_ROUND_CLOSEST(360, parent_div);
  86. delay = DIV_ROUND_CLOSEST(degrees, step);
  87. } else {
  88. delay = 0;
  89. }
  90. spin_lock_irqsave(phase->common.lock, flags);
  91. reg = readl(phase->common.base + phase->common.reg);
  92. reg &= ~GENMASK(phase->width + phase->shift - 1, phase->shift);
  93. writel(reg | (delay << phase->shift),
  94. phase->common.base + phase->common.reg);
  95. spin_unlock_irqrestore(phase->common.lock, flags);
  96. return 0;
  97. }
  98. const struct clk_ops ccu_phase_ops = {
  99. .get_phase = ccu_phase_get_phase,
  100. .set_phase = ccu_phase_set_phase,
  101. };
  102. EXPORT_SYMBOL_NS_GPL(ccu_phase_ops, SUNXI_CCU);