Linux 4.19.133
[linux/fpc-iii.git] / drivers / clk / sunxi-ng / ccu_nm.c
blob9e3944f868ffffb471f4d6f99dc3589b382f6f8d
1 /*
2 * Copyright (C) 2016 Maxime Ripard
3 * Maxime Ripard <maxime.ripard@free-electrons.com>
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 */
11 #include <linux/clk-provider.h>
13 #include "ccu_frac.h"
14 #include "ccu_gate.h"
15 #include "ccu_nm.h"
17 struct _ccu_nm {
18 unsigned long n, min_n, max_n;
19 unsigned long m, min_m, max_m;
22 static unsigned long ccu_nm_calc_rate(unsigned long parent,
23 unsigned long n, unsigned long m)
25 u64 rate = parent;
27 rate *= n;
28 do_div(rate, m);
30 return rate;
33 static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
34 struct _ccu_nm *nm)
36 unsigned long best_rate = 0;
37 unsigned long best_n = 0, best_m = 0;
38 unsigned long _n, _m;
40 for (_n = nm->min_n; _n <= nm->max_n; _n++) {
41 for (_m = nm->min_m; _m <= nm->max_m; _m++) {
42 unsigned long tmp_rate = ccu_nm_calc_rate(parent,
43 _n, _m);
45 if (tmp_rate > rate)
46 continue;
48 if ((rate - tmp_rate) < (rate - best_rate)) {
49 best_rate = tmp_rate;
50 best_n = _n;
51 best_m = _m;
56 nm->n = best_n;
57 nm->m = best_m;
60 static void ccu_nm_disable(struct clk_hw *hw)
62 struct ccu_nm *nm = hw_to_ccu_nm(hw);
64 return ccu_gate_helper_disable(&nm->common, nm->enable);
67 static int ccu_nm_enable(struct clk_hw *hw)
69 struct ccu_nm *nm = hw_to_ccu_nm(hw);
71 return ccu_gate_helper_enable(&nm->common, nm->enable);
74 static int ccu_nm_is_enabled(struct clk_hw *hw)
76 struct ccu_nm *nm = hw_to_ccu_nm(hw);
78 return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
81 static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
82 unsigned long parent_rate)
84 struct ccu_nm *nm = hw_to_ccu_nm(hw);
85 unsigned long rate;
86 unsigned long n, m;
87 u32 reg;
89 if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac)) {
90 rate = ccu_frac_helper_read_rate(&nm->common, &nm->frac);
92 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
93 rate /= nm->fixed_post_div;
95 return rate;
98 reg = readl(nm->common.base + nm->common.reg);
100 n = reg >> nm->n.shift;
101 n &= (1 << nm->n.width) - 1;
102 n += nm->n.offset;
103 if (!n)
104 n++;
106 m = reg >> nm->m.shift;
107 m &= (1 << nm->m.width) - 1;
108 m += nm->m.offset;
109 if (!m)
110 m++;
112 if (ccu_sdm_helper_is_enabled(&nm->common, &nm->sdm))
113 rate = ccu_sdm_helper_read_rate(&nm->common, &nm->sdm, m, n);
114 else
115 rate = ccu_nm_calc_rate(parent_rate, n, m);
117 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
118 rate /= nm->fixed_post_div;
120 return rate;
123 static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
124 unsigned long *parent_rate)
126 struct ccu_nm *nm = hw_to_ccu_nm(hw);
127 struct _ccu_nm _nm;
129 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
130 rate *= nm->fixed_post_div;
132 if (rate < nm->min_rate) {
133 rate = nm->min_rate;
134 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
135 rate /= nm->fixed_post_div;
136 return rate;
139 if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
140 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
141 rate /= nm->fixed_post_div;
142 return rate;
145 if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
146 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
147 rate /= nm->fixed_post_div;
148 return rate;
151 _nm.min_n = nm->n.min ?: 1;
152 _nm.max_n = nm->n.max ?: 1 << nm->n.width;
153 _nm.min_m = 1;
154 _nm.max_m = nm->m.max ?: 1 << nm->m.width;
156 ccu_nm_find_best(*parent_rate, rate, &_nm);
157 rate = ccu_nm_calc_rate(*parent_rate, _nm.n, _nm.m);
159 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
160 rate /= nm->fixed_post_div;
162 return rate;
165 static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
166 unsigned long parent_rate)
168 struct ccu_nm *nm = hw_to_ccu_nm(hw);
169 struct _ccu_nm _nm;
170 unsigned long flags;
171 u32 reg;
173 /* Adjust target rate according to post-dividers */
174 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
175 rate = rate * nm->fixed_post_div;
177 if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
178 spin_lock_irqsave(nm->common.lock, flags);
180 /* most SoCs require M to be 0 if fractional mode is used */
181 reg = readl(nm->common.base + nm->common.reg);
182 reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
183 writel(reg, nm->common.base + nm->common.reg);
185 spin_unlock_irqrestore(nm->common.lock, flags);
187 ccu_frac_helper_enable(&nm->common, &nm->frac);
189 return ccu_frac_helper_set_rate(&nm->common, &nm->frac,
190 rate, nm->lock);
191 } else {
192 ccu_frac_helper_disable(&nm->common, &nm->frac);
195 _nm.min_n = nm->n.min ?: 1;
196 _nm.max_n = nm->n.max ?: 1 << nm->n.width;
197 _nm.min_m = 1;
198 _nm.max_m = nm->m.max ?: 1 << nm->m.width;
200 if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
201 ccu_sdm_helper_enable(&nm->common, &nm->sdm, rate);
203 /* Sigma delta modulation requires specific N and M factors */
204 ccu_sdm_helper_get_factors(&nm->common, &nm->sdm, rate,
205 &_nm.m, &_nm.n);
206 } else {
207 ccu_sdm_helper_disable(&nm->common, &nm->sdm);
208 ccu_nm_find_best(parent_rate, rate, &_nm);
211 spin_lock_irqsave(nm->common.lock, flags);
213 reg = readl(nm->common.base + nm->common.reg);
214 reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
215 reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
217 reg |= (_nm.n - nm->n.offset) << nm->n.shift;
218 reg |= (_nm.m - nm->m.offset) << nm->m.shift;
219 writel(reg, nm->common.base + nm->common.reg);
221 spin_unlock_irqrestore(nm->common.lock, flags);
223 ccu_helper_wait_for_lock(&nm->common, nm->lock);
225 return 0;
228 const struct clk_ops ccu_nm_ops = {
229 .disable = ccu_nm_disable,
230 .enable = ccu_nm_enable,
231 .is_enabled = ccu_nm_is_enabled,
233 .recalc_rate = ccu_nm_recalc_rate,
234 .round_rate = ccu_nm_round_rate,
235 .set_rate = ccu_nm_set_rate,