12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26f9f7f87SMaxime Ripard /*
36f9f7f87SMaxime Ripard * Copyright (C) 2016 Maxime Ripard
46f9f7f87SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com>
56f9f7f87SMaxime Ripard */
66f9f7f87SMaxime Ripard
76f9f7f87SMaxime Ripard #include <linux/clk-provider.h>
862e59c4eSStephen Boyd #include <linux/io.h>
96f9f7f87SMaxime Ripard #include <linux/spinlock.h>
106f9f7f87SMaxime Ripard
116f9f7f87SMaxime Ripard #include "ccu_phase.h"
126f9f7f87SMaxime Ripard
ccu_phase_get_phase(struct clk_hw * hw)136f9f7f87SMaxime Ripard static int ccu_phase_get_phase(struct clk_hw *hw)
146f9f7f87SMaxime Ripard {
156f9f7f87SMaxime Ripard struct ccu_phase *phase = hw_to_ccu_phase(hw);
166f9f7f87SMaxime Ripard struct clk_hw *parent, *grandparent;
176f9f7f87SMaxime Ripard unsigned int parent_rate, grandparent_rate;
186f9f7f87SMaxime Ripard u16 step, parent_div;
196f9f7f87SMaxime Ripard u32 reg;
206f9f7f87SMaxime Ripard u8 delay;
216f9f7f87SMaxime Ripard
226f9f7f87SMaxime Ripard reg = readl(phase->common.base + phase->common.reg);
236f9f7f87SMaxime Ripard delay = (reg >> phase->shift);
246f9f7f87SMaxime Ripard delay &= (1 << phase->width) - 1;
256f9f7f87SMaxime Ripard
266f9f7f87SMaxime Ripard if (!delay)
276f9f7f87SMaxime Ripard return 180;
286f9f7f87SMaxime Ripard
296f9f7f87SMaxime Ripard /* Get our parent clock, it's the one that can adjust its rate */
306f9f7f87SMaxime Ripard parent = clk_hw_get_parent(hw);
316f9f7f87SMaxime Ripard if (!parent)
326f9f7f87SMaxime Ripard return -EINVAL;
336f9f7f87SMaxime Ripard
346f9f7f87SMaxime Ripard /* And its rate */
356f9f7f87SMaxime Ripard parent_rate = clk_hw_get_rate(parent);
366f9f7f87SMaxime Ripard if (!parent_rate)
376f9f7f87SMaxime Ripard return -EINVAL;
386f9f7f87SMaxime Ripard
396f9f7f87SMaxime Ripard /* Now, get our parent's parent (most likely some PLL) */
406f9f7f87SMaxime Ripard grandparent = clk_hw_get_parent(parent);
416f9f7f87SMaxime Ripard if (!grandparent)
426f9f7f87SMaxime Ripard return -EINVAL;
436f9f7f87SMaxime Ripard
446f9f7f87SMaxime Ripard /* And its rate */
456f9f7f87SMaxime Ripard grandparent_rate = clk_hw_get_rate(grandparent);
466f9f7f87SMaxime Ripard if (!grandparent_rate)
476f9f7f87SMaxime Ripard return -EINVAL;
486f9f7f87SMaxime Ripard
496f9f7f87SMaxime Ripard /* Get our parent clock divider */
506f9f7f87SMaxime Ripard parent_div = grandparent_rate / parent_rate;
516f9f7f87SMaxime Ripard
526f9f7f87SMaxime Ripard step = DIV_ROUND_CLOSEST(360, parent_div);
536f9f7f87SMaxime Ripard return delay * step;
546f9f7f87SMaxime Ripard }
556f9f7f87SMaxime Ripard
ccu_phase_set_phase(struct clk_hw * hw,int degrees)566f9f7f87SMaxime Ripard static int ccu_phase_set_phase(struct clk_hw *hw, int degrees)
576f9f7f87SMaxime Ripard {
586f9f7f87SMaxime Ripard struct ccu_phase *phase = hw_to_ccu_phase(hw);
596f9f7f87SMaxime Ripard struct clk_hw *parent, *grandparent;
606f9f7f87SMaxime Ripard unsigned int parent_rate, grandparent_rate;
616f9f7f87SMaxime Ripard unsigned long flags;
626f9f7f87SMaxime Ripard u32 reg;
636f9f7f87SMaxime Ripard u8 delay;
646f9f7f87SMaxime Ripard
656f9f7f87SMaxime Ripard /* Get our parent clock, it's the one that can adjust its rate */
666f9f7f87SMaxime Ripard parent = clk_hw_get_parent(hw);
676f9f7f87SMaxime Ripard if (!parent)
686f9f7f87SMaxime Ripard return -EINVAL;
696f9f7f87SMaxime Ripard
706f9f7f87SMaxime Ripard /* And its rate */
716f9f7f87SMaxime Ripard parent_rate = clk_hw_get_rate(parent);
726f9f7f87SMaxime Ripard if (!parent_rate)
736f9f7f87SMaxime Ripard return -EINVAL;
746f9f7f87SMaxime Ripard
756f9f7f87SMaxime Ripard /* Now, get our parent's parent (most likely some PLL) */
766f9f7f87SMaxime Ripard grandparent = clk_hw_get_parent(parent);
776f9f7f87SMaxime Ripard if (!grandparent)
786f9f7f87SMaxime Ripard return -EINVAL;
796f9f7f87SMaxime Ripard
806f9f7f87SMaxime Ripard /* And its rate */
816f9f7f87SMaxime Ripard grandparent_rate = clk_hw_get_rate(grandparent);
826f9f7f87SMaxime Ripard if (!grandparent_rate)
836f9f7f87SMaxime Ripard return -EINVAL;
846f9f7f87SMaxime Ripard
856f9f7f87SMaxime Ripard if (degrees != 180) {
866f9f7f87SMaxime Ripard u16 step, parent_div;
876f9f7f87SMaxime Ripard
886f9f7f87SMaxime Ripard /* Get our parent divider */
896f9f7f87SMaxime Ripard parent_div = grandparent_rate / parent_rate;
906f9f7f87SMaxime Ripard
916f9f7f87SMaxime Ripard /*
926f9f7f87SMaxime Ripard * We can only outphase the clocks by multiple of the
936f9f7f87SMaxime Ripard * PLL's period.
946f9f7f87SMaxime Ripard *
956f9f7f87SMaxime Ripard * Since our parent clock is only a divider, and the
966f9f7f87SMaxime Ripard * formula to get the outphasing in degrees is deg =
976f9f7f87SMaxime Ripard * 360 * delta / period
986f9f7f87SMaxime Ripard *
996f9f7f87SMaxime Ripard * If we simplify this formula, we can see that the
1006f9f7f87SMaxime Ripard * only thing that we're concerned about is the number
1016f9f7f87SMaxime Ripard * of period we want to outphase our clock from, and
1026f9f7f87SMaxime Ripard * the divider set by our parent clock.
1036f9f7f87SMaxime Ripard */
1046f9f7f87SMaxime Ripard step = DIV_ROUND_CLOSEST(360, parent_div);
1056f9f7f87SMaxime Ripard delay = DIV_ROUND_CLOSEST(degrees, step);
1066f9f7f87SMaxime Ripard } else {
1076f9f7f87SMaxime Ripard delay = 0;
1086f9f7f87SMaxime Ripard }
1096f9f7f87SMaxime Ripard
1106f9f7f87SMaxime Ripard spin_lock_irqsave(phase->common.lock, flags);
1116f9f7f87SMaxime Ripard reg = readl(phase->common.base + phase->common.reg);
1126f9f7f87SMaxime Ripard reg &= ~GENMASK(phase->width + phase->shift - 1, phase->shift);
1136f9f7f87SMaxime Ripard writel(reg | (delay << phase->shift),
1146f9f7f87SMaxime Ripard phase->common.base + phase->common.reg);
1156f9f7f87SMaxime Ripard spin_unlock_irqrestore(phase->common.lock, flags);
1166f9f7f87SMaxime Ripard
1176f9f7f87SMaxime Ripard return 0;
1186f9f7f87SMaxime Ripard }
1196f9f7f87SMaxime Ripard
1206f9f7f87SMaxime Ripard const struct clk_ops ccu_phase_ops = {
1216f9f7f87SMaxime Ripard .get_phase = ccu_phase_get_phase,
1226f9f7f87SMaxime Ripard .set_phase = ccu_phase_set_phase,
1236f9f7f87SMaxime Ripard };
124*551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_phase_ops, SUNXI_CCU);
125