ccu_phase.c 3.2 KB

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