xref: /linux/drivers/clk/sunxi-ng/ccu_nm.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26174a1e2SMaxime Ripard /*
36174a1e2SMaxime Ripard  * Copyright (C) 2016 Maxime Ripard
46174a1e2SMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
56174a1e2SMaxime Ripard  */
66174a1e2SMaxime Ripard 
76174a1e2SMaxime Ripard #include <linux/clk-provider.h>
862e59c4eSStephen Boyd #include <linux/io.h>
96174a1e2SMaxime Ripard 
106174a1e2SMaxime Ripard #include "ccu_frac.h"
116174a1e2SMaxime Ripard #include "ccu_gate.h"
126174a1e2SMaxime Ripard #include "ccu_nm.h"
136174a1e2SMaxime Ripard 
14ee28648cSMaxime Ripard struct _ccu_nm {
156e0d50daSMaxime Ripard 	unsigned long	n, min_n, max_n;
166e0d50daSMaxime Ripard 	unsigned long	m, min_m, max_m;
17ee28648cSMaxime Ripard };
18ee28648cSMaxime Ripard 
ccu_nm_calc_rate(unsigned long parent,unsigned long n,unsigned long m)1965b66576SJernej Skrabec static unsigned long ccu_nm_calc_rate(unsigned long parent,
2065b66576SJernej Skrabec 				      unsigned long n, unsigned long m)
2165b66576SJernej Skrabec {
2265b66576SJernej Skrabec 	u64 rate = parent;
2365b66576SJernej Skrabec 
2465b66576SJernej Skrabec 	rate *= n;
2565b66576SJernej Skrabec 	do_div(rate, m);
2665b66576SJernej Skrabec 
2765b66576SJernej Skrabec 	return rate;
2865b66576SJernej Skrabec }
2965b66576SJernej Skrabec 
ccu_nm_find_best(struct ccu_common * common,unsigned long parent,unsigned long rate,struct _ccu_nm * nm)30*c0380d14SFrank Oltmanns static unsigned long ccu_nm_find_best(struct ccu_common *common, unsigned long parent,
31*c0380d14SFrank Oltmanns 				      unsigned long rate, struct _ccu_nm *nm)
32ee28648cSMaxime Ripard {
33ee28648cSMaxime Ripard 	unsigned long best_rate = 0;
34ee28648cSMaxime Ripard 	unsigned long best_n = 0, best_m = 0;
35ee28648cSMaxime Ripard 	unsigned long _n, _m;
36ee28648cSMaxime Ripard 
376e0d50daSMaxime Ripard 	for (_n = nm->min_n; _n <= nm->max_n; _n++) {
386e0d50daSMaxime Ripard 		for (_m = nm->min_m; _m <= nm->max_m; _m++) {
3965b66576SJernej Skrabec 			unsigned long tmp_rate = ccu_nm_calc_rate(parent,
4065b66576SJernej Skrabec 								  _n, _m);
41ee28648cSMaxime Ripard 
42*c0380d14SFrank Oltmanns 			if (ccu_is_better_rate(common, rate, tmp_rate, best_rate)) {
43ee28648cSMaxime Ripard 				best_rate = tmp_rate;
44ee28648cSMaxime Ripard 				best_n = _n;
45ee28648cSMaxime Ripard 				best_m = _m;
46ee28648cSMaxime Ripard 			}
47ee28648cSMaxime Ripard 		}
48ee28648cSMaxime Ripard 	}
49ee28648cSMaxime Ripard 
50ee28648cSMaxime Ripard 	nm->n = best_n;
51ee28648cSMaxime Ripard 	nm->m = best_m;
52657f477aSSamuel Holland 
53657f477aSSamuel Holland 	return best_rate;
54ee28648cSMaxime Ripard }
55ee28648cSMaxime Ripard 
ccu_nm_disable(struct clk_hw * hw)566174a1e2SMaxime Ripard static void ccu_nm_disable(struct clk_hw *hw)
576174a1e2SMaxime Ripard {
586174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
596174a1e2SMaxime Ripard 
606174a1e2SMaxime Ripard 	return ccu_gate_helper_disable(&nm->common, nm->enable);
616174a1e2SMaxime Ripard }
626174a1e2SMaxime Ripard 
ccu_nm_enable(struct clk_hw * hw)636174a1e2SMaxime Ripard static int ccu_nm_enable(struct clk_hw *hw)
646174a1e2SMaxime Ripard {
656174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
666174a1e2SMaxime Ripard 
676174a1e2SMaxime Ripard 	return ccu_gate_helper_enable(&nm->common, nm->enable);
686174a1e2SMaxime Ripard }
696174a1e2SMaxime Ripard 
ccu_nm_is_enabled(struct clk_hw * hw)706174a1e2SMaxime Ripard static int ccu_nm_is_enabled(struct clk_hw *hw)
716174a1e2SMaxime Ripard {
726174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
736174a1e2SMaxime Ripard 
746174a1e2SMaxime Ripard 	return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
756174a1e2SMaxime Ripard }
766174a1e2SMaxime Ripard 
ccu_nm_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)776174a1e2SMaxime Ripard static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
786174a1e2SMaxime Ripard 					unsigned long parent_rate)
796174a1e2SMaxime Ripard {
806174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
817d333ef1SChen-Yu Tsai 	unsigned long rate;
826174a1e2SMaxime Ripard 	unsigned long n, m;
836174a1e2SMaxime Ripard 	u32 reg;
846174a1e2SMaxime Ripard 
857d333ef1SChen-Yu Tsai 	if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac)) {
867d333ef1SChen-Yu Tsai 		rate = ccu_frac_helper_read_rate(&nm->common, &nm->frac);
877d333ef1SChen-Yu Tsai 
887d333ef1SChen-Yu Tsai 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
897d333ef1SChen-Yu Tsai 			rate /= nm->fixed_post_div;
907d333ef1SChen-Yu Tsai 
917d333ef1SChen-Yu Tsai 		return rate;
927d333ef1SChen-Yu Tsai 	}
936174a1e2SMaxime Ripard 
946174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
956174a1e2SMaxime Ripard 
966174a1e2SMaxime Ripard 	n = reg >> nm->n.shift;
976174a1e2SMaxime Ripard 	n &= (1 << nm->n.width) - 1;
98e66f81bbSMaxime Ripard 	n += nm->n.offset;
99e66f81bbSMaxime Ripard 	if (!n)
100e66f81bbSMaxime Ripard 		n++;
1016174a1e2SMaxime Ripard 
1026174a1e2SMaxime Ripard 	m = reg >> nm->m.shift;
1036174a1e2SMaxime Ripard 	m &= (1 << nm->m.width) - 1;
104e66f81bbSMaxime Ripard 	m += nm->m.offset;
105e66f81bbSMaxime Ripard 	if (!m)
106e66f81bbSMaxime Ripard 		m++;
1076174a1e2SMaxime Ripard 
1087d333ef1SChen-Yu Tsai 	if (ccu_sdm_helper_is_enabled(&nm->common, &nm->sdm))
1097d333ef1SChen-Yu Tsai 		rate = ccu_sdm_helper_read_rate(&nm->common, &nm->sdm, m, n);
1107d333ef1SChen-Yu Tsai 	else
11165b66576SJernej Skrabec 		rate = ccu_nm_calc_rate(parent_rate, n, m);
112392ba5faSChen-Yu Tsai 
1137d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
1147d333ef1SChen-Yu Tsai 		rate /= nm->fixed_post_div;
1157d333ef1SChen-Yu Tsai 
1167d333ef1SChen-Yu Tsai 	return rate;
1176174a1e2SMaxime Ripard }
1186174a1e2SMaxime Ripard 
ccu_nm_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)1196174a1e2SMaxime Ripard static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
1206174a1e2SMaxime Ripard 			      unsigned long *parent_rate)
1216174a1e2SMaxime Ripard {
1226174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
123ee28648cSMaxime Ripard 	struct _ccu_nm _nm;
1246174a1e2SMaxime Ripard 
1257d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
1267d333ef1SChen-Yu Tsai 		rate *= nm->fixed_post_div;
1274cdbc40dSChen-Yu Tsai 
1282d2b61c1SJernej Skrabec 	if (rate < nm->min_rate) {
1292d2b61c1SJernej Skrabec 		rate = nm->min_rate;
1302d2b61c1SJernej Skrabec 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
1312d2b61c1SJernej Skrabec 			rate /= nm->fixed_post_div;
1322d2b61c1SJernej Skrabec 		return rate;
1332d2b61c1SJernej Skrabec 	}
1342d2b61c1SJernej Skrabec 
135cb54fbd2SJernej Skrabec 	if (nm->max_rate && rate > nm->max_rate) {
136cb54fbd2SJernej Skrabec 		rate = nm->max_rate;
137cb54fbd2SJernej Skrabec 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
138cb54fbd2SJernej Skrabec 			rate /= nm->fixed_post_div;
139cb54fbd2SJernej Skrabec 		return rate;
140cb54fbd2SJernej Skrabec 	}
141cb54fbd2SJernej Skrabec 
1427d333ef1SChen-Yu Tsai 	if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
1437d333ef1SChen-Yu Tsai 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
1447d333ef1SChen-Yu Tsai 			rate /= nm->fixed_post_div;
145392ba5faSChen-Yu Tsai 		return rate;
1467d333ef1SChen-Yu Tsai 	}
1477d333ef1SChen-Yu Tsai 
1487d333ef1SChen-Yu Tsai 	if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
1497d333ef1SChen-Yu Tsai 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
1507d333ef1SChen-Yu Tsai 			rate /= nm->fixed_post_div;
1517d333ef1SChen-Yu Tsai 		return rate;
1527d333ef1SChen-Yu Tsai 	}
153392ba5faSChen-Yu Tsai 
1544162c5ceSChen-Yu Tsai 	_nm.min_n = nm->n.min ?: 1;
1550c3c8e13SMaxime Ripard 	_nm.max_n = nm->n.max ?: 1 << nm->n.width;
1566e0d50daSMaxime Ripard 	_nm.min_m = 1;
157ee28648cSMaxime Ripard 	_nm.max_m = nm->m.max ?: 1 << nm->m.width;
15887ba9e59SMaxime Ripard 
159*c0380d14SFrank Oltmanns 	rate = ccu_nm_find_best(&nm->common, *parent_rate, rate, &_nm);
1606174a1e2SMaxime Ripard 
1617d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
1627d333ef1SChen-Yu Tsai 		rate /= nm->fixed_post_div;
1637d333ef1SChen-Yu Tsai 
1647d333ef1SChen-Yu Tsai 	return rate;
1656174a1e2SMaxime Ripard }
1666174a1e2SMaxime Ripard 
ccu_nm_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)1676174a1e2SMaxime Ripard static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
1686174a1e2SMaxime Ripard 			   unsigned long parent_rate)
1696174a1e2SMaxime Ripard {
1706174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
171ee28648cSMaxime Ripard 	struct _ccu_nm _nm;
1726174a1e2SMaxime Ripard 	unsigned long flags;
1736174a1e2SMaxime Ripard 	u32 reg;
1746174a1e2SMaxime Ripard 
1757d333ef1SChen-Yu Tsai 	/* Adjust target rate according to post-dividers */
1767d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
1777d333ef1SChen-Yu Tsai 		rate = rate * nm->fixed_post_div;
1787d333ef1SChen-Yu Tsai 
179b64dfec0SJernej Škrabec 	if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
180b64dfec0SJernej Škrabec 		spin_lock_irqsave(nm->common.lock, flags);
181b64dfec0SJernej Škrabec 
182b64dfec0SJernej Škrabec 		/* most SoCs require M to be 0 if fractional mode is used */
183b64dfec0SJernej Škrabec 		reg = readl(nm->common.base + nm->common.reg);
184b64dfec0SJernej Škrabec 		reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
185b64dfec0SJernej Škrabec 		writel(reg, nm->common.base + nm->common.reg);
186b64dfec0SJernej Škrabec 
187b64dfec0SJernej Škrabec 		spin_unlock_irqrestore(nm->common.lock, flags);
188b64dfec0SJernej Škrabec 
189b64dfec0SJernej Škrabec 		ccu_frac_helper_enable(&nm->common, &nm->frac);
190b64dfec0SJernej Škrabec 
1911d42460aSJernej Škrabec 		return ccu_frac_helper_set_rate(&nm->common, &nm->frac,
1921d42460aSJernej Škrabec 						rate, nm->lock);
193b64dfec0SJernej Škrabec 	} else {
1946174a1e2SMaxime Ripard 		ccu_frac_helper_disable(&nm->common, &nm->frac);
195b64dfec0SJernej Škrabec 	}
1966174a1e2SMaxime Ripard 
19795ad8ed9SChen-Yu Tsai 	_nm.min_n = nm->n.min ?: 1;
1980c3c8e13SMaxime Ripard 	_nm.max_n = nm->n.max ?: 1 << nm->n.width;
1996e0d50daSMaxime Ripard 	_nm.min_m = 1;
200ee28648cSMaxime Ripard 	_nm.max_m = nm->m.max ?: 1 << nm->m.width;
20187ba9e59SMaxime Ripard 
202392ba5faSChen-Yu Tsai 	if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
203392ba5faSChen-Yu Tsai 		ccu_sdm_helper_enable(&nm->common, &nm->sdm, rate);
204392ba5faSChen-Yu Tsai 
205392ba5faSChen-Yu Tsai 		/* Sigma delta modulation requires specific N and M factors */
206392ba5faSChen-Yu Tsai 		ccu_sdm_helper_get_factors(&nm->common, &nm->sdm, rate,
207392ba5faSChen-Yu Tsai 					   &_nm.m, &_nm.n);
208392ba5faSChen-Yu Tsai 	} else {
209392ba5faSChen-Yu Tsai 		ccu_sdm_helper_disable(&nm->common, &nm->sdm);
210*c0380d14SFrank Oltmanns 		ccu_nm_find_best(&nm->common, parent_rate, rate, &_nm);
211392ba5faSChen-Yu Tsai 	}
2126174a1e2SMaxime Ripard 
2136174a1e2SMaxime Ripard 	spin_lock_irqsave(nm->common.lock, flags);
2146174a1e2SMaxime Ripard 
2156174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
2166174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
2176174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
2186174a1e2SMaxime Ripard 
219e66f81bbSMaxime Ripard 	reg |= (_nm.n - nm->n.offset) << nm->n.shift;
220e66f81bbSMaxime Ripard 	reg |= (_nm.m - nm->m.offset) << nm->m.shift;
221e66f81bbSMaxime Ripard 	writel(reg, nm->common.base + nm->common.reg);
2226174a1e2SMaxime Ripard 
2236174a1e2SMaxime Ripard 	spin_unlock_irqrestore(nm->common.lock, flags);
2246174a1e2SMaxime Ripard 
2256174a1e2SMaxime Ripard 	ccu_helper_wait_for_lock(&nm->common, nm->lock);
2266174a1e2SMaxime Ripard 
2276174a1e2SMaxime Ripard 	return 0;
2286174a1e2SMaxime Ripard }
2296174a1e2SMaxime Ripard 
2306174a1e2SMaxime Ripard const struct clk_ops ccu_nm_ops = {
2316174a1e2SMaxime Ripard 	.disable	= ccu_nm_disable,
2326174a1e2SMaxime Ripard 	.enable		= ccu_nm_enable,
2336174a1e2SMaxime Ripard 	.is_enabled	= ccu_nm_is_enabled,
2346174a1e2SMaxime Ripard 
2356174a1e2SMaxime Ripard 	.recalc_rate	= ccu_nm_recalc_rate,
2366174a1e2SMaxime Ripard 	.round_rate	= ccu_nm_round_rate,
2376174a1e2SMaxime Ripard 	.set_rate	= ccu_nm_set_rate,
2386174a1e2SMaxime Ripard };
239551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_nm_ops, SUNXI_CCU);
240