1 // SPDX-License-Identifier: GPL-2.0-or-later
6 #include <linux/clk-provider.h>
8 #include <linux/spinlock.h>
12 bool ccu_sdm_helper_is_enabled(struct ccu_common *common,
13 struct ccu_sdm_internal *sdm)
15 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
18 if (sdm->enable && !(readl(common->base + common->reg) & sdm->enable))
21 return !!(readl(common->base + sdm->tuning_reg) & sdm->tuning_enable);
24 void ccu_sdm_helper_enable(struct ccu_common *common,
25 struct ccu_sdm_internal *sdm,
32 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
36 for (i = 0; i < sdm->table_size; i++)
37 if (sdm->table[i].rate == rate)
38 writel(sdm->table[i].pattern,
39 common->base + sdm->tuning_reg);
41 /* Make sure SDM is enabled */
42 spin_lock_irqsave(common->lock, flags);
43 reg = readl(common->base + sdm->tuning_reg);
44 writel(reg | sdm->tuning_enable, common->base + sdm->tuning_reg);
45 spin_unlock_irqrestore(common->lock, flags);
47 spin_lock_irqsave(common->lock, flags);
48 reg = readl(common->base + common->reg);
49 writel(reg | sdm->enable, common->base + common->reg);
50 spin_unlock_irqrestore(common->lock, flags);
53 void ccu_sdm_helper_disable(struct ccu_common *common,
54 struct ccu_sdm_internal *sdm)
59 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
62 spin_lock_irqsave(common->lock, flags);
63 reg = readl(common->base + common->reg);
64 writel(reg & ~sdm->enable, common->base + common->reg);
65 spin_unlock_irqrestore(common->lock, flags);
67 spin_lock_irqsave(common->lock, flags);
68 reg = readl(common->base + sdm->tuning_reg);
69 writel(reg & ~sdm->tuning_enable, common->base + sdm->tuning_reg);
70 spin_unlock_irqrestore(common->lock, flags);
74 * Sigma delta modulation provides a way to do fractional-N frequency
75 * synthesis, in essence allowing the PLL to output any frequency
76 * within its operational range. On earlier SoCs such as the A10/A20,
77 * some PLLs support this. On later SoCs, all PLLs support this.
79 * The datasheets do not explain what the "wave top" and "wave bottom"
80 * parameters mean or do, nor how to calculate the effective output
81 * frequency. The only examples (and real world usage) are for the audio
82 * PLL to generate 24.576 and 22.5792 MHz clock rates used by the audio
83 * peripherals. The author lacks the underlying domain knowledge to
86 * The goal and function of the following code is to support the two
87 * clock rates used by the audio subsystem, allowing for proper audio
88 * playback and capture without any pitch or speed changes.
90 bool ccu_sdm_helper_has_rate(struct ccu_common *common,
91 struct ccu_sdm_internal *sdm,
96 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
99 for (i = 0; i < sdm->table_size; i++)
100 if (sdm->table[i].rate == rate)
106 unsigned long ccu_sdm_helper_read_rate(struct ccu_common *common,
107 struct ccu_sdm_internal *sdm,
113 pr_debug("%s: Read sigma-delta modulation setting\n",
114 clk_hw_get_name(&common->hw));
116 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
119 pr_debug("%s: clock is sigma-delta modulated\n",
120 clk_hw_get_name(&common->hw));
122 reg = readl(common->base + sdm->tuning_reg);
124 pr_debug("%s: pattern reg is 0x%x",
125 clk_hw_get_name(&common->hw), reg);
127 for (i = 0; i < sdm->table_size; i++)
128 if (sdm->table[i].pattern == reg &&
129 sdm->table[i].m == m && sdm->table[i].n == n)
130 return sdm->table[i].rate;
132 /* We can't calculate the effective clock rate, so just fail. */
136 int ccu_sdm_helper_get_factors(struct ccu_common *common,
137 struct ccu_sdm_internal *sdm,
139 unsigned long *m, unsigned long *n)
143 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
146 for (i = 0; i < sdm->table_size; i++)
147 if (sdm->table[i].rate == rate) {
148 *m = sdm->table[i].m;
149 *n = sdm->table[i].n;