Linux 4.18.10
[linux/fpc-iii.git] / drivers / clk / sunxi-ng / ccu_sdm.c
blob3b3dc9bdf2b00ee06b72d6be09fbbe3b064e0e7a
1 /*
2 * Copyright (C) 2017 Chen-Yu Tsai <wens@csie.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 */
10 #include <linux/clk-provider.h>
11 #include <linux/spinlock.h>
13 #include "ccu_sdm.h"
15 bool ccu_sdm_helper_is_enabled(struct ccu_common *common,
16 struct ccu_sdm_internal *sdm)
18 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
19 return false;
21 if (sdm->enable && !(readl(common->base + common->reg) & sdm->enable))
22 return false;
24 return !!(readl(common->base + sdm->tuning_reg) & sdm->tuning_enable);
27 void ccu_sdm_helper_enable(struct ccu_common *common,
28 struct ccu_sdm_internal *sdm,
29 unsigned long rate)
31 unsigned long flags;
32 unsigned int i;
33 u32 reg;
35 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
36 return;
38 /* Set the pattern */
39 for (i = 0; i < sdm->table_size; i++)
40 if (sdm->table[i].rate == rate)
41 writel(sdm->table[i].pattern,
42 common->base + sdm->tuning_reg);
44 /* Make sure SDM is enabled */
45 spin_lock_irqsave(common->lock, flags);
46 reg = readl(common->base + sdm->tuning_reg);
47 writel(reg | sdm->tuning_enable, common->base + sdm->tuning_reg);
48 spin_unlock_irqrestore(common->lock, flags);
50 spin_lock_irqsave(common->lock, flags);
51 reg = readl(common->base + common->reg);
52 writel(reg | sdm->enable, common->base + common->reg);
53 spin_unlock_irqrestore(common->lock, flags);
56 void ccu_sdm_helper_disable(struct ccu_common *common,
57 struct ccu_sdm_internal *sdm)
59 unsigned long flags;
60 u32 reg;
62 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
63 return;
65 spin_lock_irqsave(common->lock, flags);
66 reg = readl(common->base + common->reg);
67 writel(reg & ~sdm->enable, common->base + common->reg);
68 spin_unlock_irqrestore(common->lock, flags);
70 spin_lock_irqsave(common->lock, flags);
71 reg = readl(common->base + sdm->tuning_reg);
72 writel(reg & ~sdm->tuning_enable, common->base + sdm->tuning_reg);
73 spin_unlock_irqrestore(common->lock, flags);
77 * Sigma delta modulation provides a way to do fractional-N frequency
78 * synthesis, in essence allowing the PLL to output any frequency
79 * within its operational range. On earlier SoCs such as the A10/A20,
80 * some PLLs support this. On later SoCs, all PLLs support this.
82 * The datasheets do not explain what the "wave top" and "wave bottom"
83 * parameters mean or do, nor how to calculate the effective output
84 * frequency. The only examples (and real world usage) are for the audio
85 * PLL to generate 24.576 and 22.5792 MHz clock rates used by the audio
86 * peripherals. The author lacks the underlying domain knowledge to
87 * pursue this.
89 * The goal and function of the following code is to support the two
90 * clock rates used by the audio subsystem, allowing for proper audio
91 * playback and capture without any pitch or speed changes.
93 bool ccu_sdm_helper_has_rate(struct ccu_common *common,
94 struct ccu_sdm_internal *sdm,
95 unsigned long rate)
97 unsigned int i;
99 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
100 return false;
102 for (i = 0; i < sdm->table_size; i++)
103 if (sdm->table[i].rate == rate)
104 return true;
106 return false;
109 unsigned long ccu_sdm_helper_read_rate(struct ccu_common *common,
110 struct ccu_sdm_internal *sdm,
111 u32 m, u32 n)
113 unsigned int i;
114 u32 reg;
116 pr_debug("%s: Read sigma-delta modulation setting\n",
117 clk_hw_get_name(&common->hw));
119 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
120 return 0;
122 pr_debug("%s: clock is sigma-delta modulated\n",
123 clk_hw_get_name(&common->hw));
125 reg = readl(common->base + sdm->tuning_reg);
127 pr_debug("%s: pattern reg is 0x%x",
128 clk_hw_get_name(&common->hw), reg);
130 for (i = 0; i < sdm->table_size; i++)
131 if (sdm->table[i].pattern == reg &&
132 sdm->table[i].m == m && sdm->table[i].n == n)
133 return sdm->table[i].rate;
135 /* We can't calculate the effective clock rate, so just fail. */
136 return 0;
139 int ccu_sdm_helper_get_factors(struct ccu_common *common,
140 struct ccu_sdm_internal *sdm,
141 unsigned long rate,
142 unsigned long *m, unsigned long *n)
144 unsigned int i;
146 if (!(common->features & CCU_FEATURE_SIGMA_DELTA_MOD))
147 return -EINVAL;
149 for (i = 0; i < sdm->table_size; i++)
150 if (sdm->table[i].rate == rate) {
151 *m = sdm->table[i].m;
152 *n = sdm->table[i].n;
153 return 0;
156 /* nothing found */
157 return -EINVAL;