Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / sunxi-ng / ccu_sdm.c
blob79581a1c649af3223ac3b73deb3903104a974600
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2017 Chen-Yu Tsai <wens@csie.org>
4 */
6 #include <linux/clk-provider.h>
7 #include <linux/io.h>
8 #include <linux/spinlock.h>
10 #include "ccu_sdm.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))
16 return false;
18 if (sdm->enable && !(readl(common->base + common->reg) & sdm->enable))
19 return false;
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,
26 unsigned long rate)
28 unsigned long flags;
29 unsigned int i;
30 u32 reg;
32 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
33 return;
35 /* Set the pattern */
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)
56 unsigned long flags;
57 u32 reg;
59 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
60 return;
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
84 * pursue this.
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,
92 unsigned long rate)
94 unsigned int i;
96 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
97 return false;
99 for (i = 0; i < sdm->table_size; i++)
100 if (sdm->table[i].rate == rate)
101 return true;
103 return false;
106 unsigned long ccu_sdm_helper_read_rate(struct ccu_common *common,
107 struct ccu_sdm_internal *sdm,
108 u32 m, u32 n)
110 unsigned int i;
111 u32 reg;
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))
117 return 0;
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. */
133 return 0;
136 int ccu_sdm_helper_get_factors(struct ccu_common *common,
137 struct ccu_sdm_internal *sdm,
138 unsigned long rate,
139 unsigned long *m, unsigned long *n)
141 unsigned int i;
143 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
144 return -EINVAL;
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;
150 return 0;
153 /* nothing found */
154 return -EINVAL;