xref: /linux/drivers/clk/sunxi-ng/ccu_nm.c (revision 7d333ef1cc1b8c8951f3a2c41f6406e2295d8be9)
16174a1e2SMaxime Ripard /*
26174a1e2SMaxime Ripard  * Copyright (C) 2016 Maxime Ripard
36174a1e2SMaxime Ripard  * Maxime Ripard <maxime.ripard@free-electrons.com>
46174a1e2SMaxime Ripard  *
56174a1e2SMaxime Ripard  * This program is free software; you can redistribute it and/or
66174a1e2SMaxime Ripard  * modify it under the terms of the GNU General Public License as
76174a1e2SMaxime Ripard  * published by the Free Software Foundation; either version 2 of
86174a1e2SMaxime Ripard  * the License, or (at your option) any later version.
96174a1e2SMaxime Ripard  */
106174a1e2SMaxime Ripard 
116174a1e2SMaxime Ripard #include <linux/clk-provider.h>
126174a1e2SMaxime Ripard 
136174a1e2SMaxime Ripard #include "ccu_frac.h"
146174a1e2SMaxime Ripard #include "ccu_gate.h"
156174a1e2SMaxime Ripard #include "ccu_nm.h"
166174a1e2SMaxime Ripard 
17ee28648cSMaxime Ripard struct _ccu_nm {
186e0d50daSMaxime Ripard 	unsigned long	n, min_n, max_n;
196e0d50daSMaxime Ripard 	unsigned long	m, min_m, max_m;
20ee28648cSMaxime Ripard };
21ee28648cSMaxime Ripard 
22ee28648cSMaxime Ripard static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
23ee28648cSMaxime Ripard 			     struct _ccu_nm *nm)
24ee28648cSMaxime Ripard {
25ee28648cSMaxime Ripard 	unsigned long best_rate = 0;
26ee28648cSMaxime Ripard 	unsigned long best_n = 0, best_m = 0;
27ee28648cSMaxime Ripard 	unsigned long _n, _m;
28ee28648cSMaxime Ripard 
296e0d50daSMaxime Ripard 	for (_n = nm->min_n; _n <= nm->max_n; _n++) {
306e0d50daSMaxime Ripard 		for (_m = nm->min_m; _m <= nm->max_m; _m++) {
31ee28648cSMaxime Ripard 			unsigned long tmp_rate = parent * _n  / _m;
32ee28648cSMaxime Ripard 
33ee28648cSMaxime Ripard 			if (tmp_rate > rate)
34ee28648cSMaxime Ripard 				continue;
35ee28648cSMaxime Ripard 
36ee28648cSMaxime Ripard 			if ((rate - tmp_rate) < (rate - best_rate)) {
37ee28648cSMaxime Ripard 				best_rate = tmp_rate;
38ee28648cSMaxime Ripard 				best_n = _n;
39ee28648cSMaxime Ripard 				best_m = _m;
40ee28648cSMaxime Ripard 			}
41ee28648cSMaxime Ripard 		}
42ee28648cSMaxime Ripard 	}
43ee28648cSMaxime Ripard 
44ee28648cSMaxime Ripard 	nm->n = best_n;
45ee28648cSMaxime Ripard 	nm->m = best_m;
46ee28648cSMaxime Ripard }
47ee28648cSMaxime Ripard 
486174a1e2SMaxime Ripard static void ccu_nm_disable(struct clk_hw *hw)
496174a1e2SMaxime Ripard {
506174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
516174a1e2SMaxime Ripard 
526174a1e2SMaxime Ripard 	return ccu_gate_helper_disable(&nm->common, nm->enable);
536174a1e2SMaxime Ripard }
546174a1e2SMaxime Ripard 
556174a1e2SMaxime Ripard static int ccu_nm_enable(struct clk_hw *hw)
566174a1e2SMaxime Ripard {
576174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
586174a1e2SMaxime Ripard 
596174a1e2SMaxime Ripard 	return ccu_gate_helper_enable(&nm->common, nm->enable);
606174a1e2SMaxime Ripard }
616174a1e2SMaxime Ripard 
626174a1e2SMaxime Ripard static int ccu_nm_is_enabled(struct clk_hw *hw)
636174a1e2SMaxime Ripard {
646174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
656174a1e2SMaxime Ripard 
666174a1e2SMaxime Ripard 	return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
676174a1e2SMaxime Ripard }
686174a1e2SMaxime Ripard 
696174a1e2SMaxime Ripard static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
706174a1e2SMaxime Ripard 					unsigned long parent_rate)
716174a1e2SMaxime Ripard {
726174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
73*7d333ef1SChen-Yu Tsai 	unsigned long rate;
746174a1e2SMaxime Ripard 	unsigned long n, m;
756174a1e2SMaxime Ripard 	u32 reg;
766174a1e2SMaxime Ripard 
77*7d333ef1SChen-Yu Tsai 	if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac)) {
78*7d333ef1SChen-Yu Tsai 		rate = ccu_frac_helper_read_rate(&nm->common, &nm->frac);
79*7d333ef1SChen-Yu Tsai 
80*7d333ef1SChen-Yu Tsai 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
81*7d333ef1SChen-Yu Tsai 			rate /= nm->fixed_post_div;
82*7d333ef1SChen-Yu Tsai 
83*7d333ef1SChen-Yu Tsai 		return rate;
84*7d333ef1SChen-Yu Tsai 	}
856174a1e2SMaxime Ripard 
866174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
876174a1e2SMaxime Ripard 
886174a1e2SMaxime Ripard 	n = reg >> nm->n.shift;
896174a1e2SMaxime Ripard 	n &= (1 << nm->n.width) - 1;
90e66f81bbSMaxime Ripard 	n += nm->n.offset;
91e66f81bbSMaxime Ripard 	if (!n)
92e66f81bbSMaxime Ripard 		n++;
936174a1e2SMaxime Ripard 
946174a1e2SMaxime Ripard 	m = reg >> nm->m.shift;
956174a1e2SMaxime Ripard 	m &= (1 << nm->m.width) - 1;
96e66f81bbSMaxime Ripard 	m += nm->m.offset;
97e66f81bbSMaxime Ripard 	if (!m)
98e66f81bbSMaxime Ripard 		m++;
996174a1e2SMaxime Ripard 
100*7d333ef1SChen-Yu Tsai 	if (ccu_sdm_helper_is_enabled(&nm->common, &nm->sdm))
101*7d333ef1SChen-Yu Tsai 		rate = ccu_sdm_helper_read_rate(&nm->common, &nm->sdm, m, n);
102*7d333ef1SChen-Yu Tsai 	else
103*7d333ef1SChen-Yu Tsai 		rate = parent_rate * n / m;
104392ba5faSChen-Yu Tsai 
105*7d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
106*7d333ef1SChen-Yu Tsai 		rate /= nm->fixed_post_div;
107*7d333ef1SChen-Yu Tsai 
108*7d333ef1SChen-Yu Tsai 	return rate;
1096174a1e2SMaxime Ripard }
1106174a1e2SMaxime Ripard 
1116174a1e2SMaxime Ripard static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
1126174a1e2SMaxime Ripard 			      unsigned long *parent_rate)
1136174a1e2SMaxime Ripard {
1146174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
115ee28648cSMaxime Ripard 	struct _ccu_nm _nm;
1166174a1e2SMaxime Ripard 
117*7d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
118*7d333ef1SChen-Yu Tsai 		rate *= nm->fixed_post_div;
1194cdbc40dSChen-Yu Tsai 
120*7d333ef1SChen-Yu Tsai 	if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
121*7d333ef1SChen-Yu Tsai 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
122*7d333ef1SChen-Yu Tsai 			rate /= nm->fixed_post_div;
123392ba5faSChen-Yu Tsai 		return rate;
124*7d333ef1SChen-Yu Tsai 	}
125*7d333ef1SChen-Yu Tsai 
126*7d333ef1SChen-Yu Tsai 	if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
127*7d333ef1SChen-Yu Tsai 		if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
128*7d333ef1SChen-Yu Tsai 			rate /= nm->fixed_post_div;
129*7d333ef1SChen-Yu Tsai 		return rate;
130*7d333ef1SChen-Yu Tsai 	}
131392ba5faSChen-Yu Tsai 
1324162c5ceSChen-Yu Tsai 	_nm.min_n = nm->n.min ?: 1;
1330c3c8e13SMaxime Ripard 	_nm.max_n = nm->n.max ?: 1 << nm->n.width;
1346e0d50daSMaxime Ripard 	_nm.min_m = 1;
135ee28648cSMaxime Ripard 	_nm.max_m = nm->m.max ?: 1 << nm->m.width;
13687ba9e59SMaxime Ripard 
137ee28648cSMaxime Ripard 	ccu_nm_find_best(*parent_rate, rate, &_nm);
138*7d333ef1SChen-Yu Tsai 	rate = *parent_rate * _nm.n / _nm.m;
1396174a1e2SMaxime Ripard 
140*7d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
141*7d333ef1SChen-Yu Tsai 		rate /= nm->fixed_post_div;
142*7d333ef1SChen-Yu Tsai 
143*7d333ef1SChen-Yu Tsai 	return rate;
1446174a1e2SMaxime Ripard }
1456174a1e2SMaxime Ripard 
1466174a1e2SMaxime Ripard static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
1476174a1e2SMaxime Ripard 			   unsigned long parent_rate)
1486174a1e2SMaxime Ripard {
1496174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
150ee28648cSMaxime Ripard 	struct _ccu_nm _nm;
1516174a1e2SMaxime Ripard 	unsigned long flags;
1526174a1e2SMaxime Ripard 	u32 reg;
1536174a1e2SMaxime Ripard 
154*7d333ef1SChen-Yu Tsai 	/* Adjust target rate according to post-dividers */
155*7d333ef1SChen-Yu Tsai 	if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
156*7d333ef1SChen-Yu Tsai 		rate = rate * nm->fixed_post_div;
157*7d333ef1SChen-Yu Tsai 
158b64dfec0SJernej Škrabec 	if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
159b64dfec0SJernej Škrabec 		spin_lock_irqsave(nm->common.lock, flags);
160b64dfec0SJernej Škrabec 
161b64dfec0SJernej Škrabec 		/* most SoCs require M to be 0 if fractional mode is used */
162b64dfec0SJernej Škrabec 		reg = readl(nm->common.base + nm->common.reg);
163b64dfec0SJernej Škrabec 		reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
164b64dfec0SJernej Škrabec 		writel(reg, nm->common.base + nm->common.reg);
165b64dfec0SJernej Škrabec 
166b64dfec0SJernej Škrabec 		spin_unlock_irqrestore(nm->common.lock, flags);
167b64dfec0SJernej Škrabec 
168b64dfec0SJernej Škrabec 		ccu_frac_helper_enable(&nm->common, &nm->frac);
169b64dfec0SJernej Škrabec 
1701d42460aSJernej Škrabec 		return ccu_frac_helper_set_rate(&nm->common, &nm->frac,
1711d42460aSJernej Škrabec 						rate, nm->lock);
172b64dfec0SJernej Škrabec 	} else {
1736174a1e2SMaxime Ripard 		ccu_frac_helper_disable(&nm->common, &nm->frac);
174b64dfec0SJernej Škrabec 	}
1756174a1e2SMaxime Ripard 
17695ad8ed9SChen-Yu Tsai 	_nm.min_n = nm->n.min ?: 1;
1770c3c8e13SMaxime Ripard 	_nm.max_n = nm->n.max ?: 1 << nm->n.width;
1786e0d50daSMaxime Ripard 	_nm.min_m = 1;
179ee28648cSMaxime Ripard 	_nm.max_m = nm->m.max ?: 1 << nm->m.width;
18087ba9e59SMaxime Ripard 
181392ba5faSChen-Yu Tsai 	if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
182392ba5faSChen-Yu Tsai 		ccu_sdm_helper_enable(&nm->common, &nm->sdm, rate);
183392ba5faSChen-Yu Tsai 
184392ba5faSChen-Yu Tsai 		/* Sigma delta modulation requires specific N and M factors */
185392ba5faSChen-Yu Tsai 		ccu_sdm_helper_get_factors(&nm->common, &nm->sdm, rate,
186392ba5faSChen-Yu Tsai 					   &_nm.m, &_nm.n);
187392ba5faSChen-Yu Tsai 	} else {
188392ba5faSChen-Yu Tsai 		ccu_sdm_helper_disable(&nm->common, &nm->sdm);
189ee28648cSMaxime Ripard 		ccu_nm_find_best(parent_rate, rate, &_nm);
190392ba5faSChen-Yu Tsai 	}
1916174a1e2SMaxime Ripard 
1926174a1e2SMaxime Ripard 	spin_lock_irqsave(nm->common.lock, flags);
1936174a1e2SMaxime Ripard 
1946174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
1956174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
1966174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
1976174a1e2SMaxime Ripard 
198e66f81bbSMaxime Ripard 	reg |= (_nm.n - nm->n.offset) << nm->n.shift;
199e66f81bbSMaxime Ripard 	reg |= (_nm.m - nm->m.offset) << nm->m.shift;
200e66f81bbSMaxime Ripard 	writel(reg, nm->common.base + nm->common.reg);
2016174a1e2SMaxime Ripard 
2026174a1e2SMaxime Ripard 	spin_unlock_irqrestore(nm->common.lock, flags);
2036174a1e2SMaxime Ripard 
2046174a1e2SMaxime Ripard 	ccu_helper_wait_for_lock(&nm->common, nm->lock);
2056174a1e2SMaxime Ripard 
2066174a1e2SMaxime Ripard 	return 0;
2076174a1e2SMaxime Ripard }
2086174a1e2SMaxime Ripard 
2096174a1e2SMaxime Ripard const struct clk_ops ccu_nm_ops = {
2106174a1e2SMaxime Ripard 	.disable	= ccu_nm_disable,
2116174a1e2SMaxime Ripard 	.enable		= ccu_nm_enable,
2126174a1e2SMaxime Ripard 	.is_enabled	= ccu_nm_is_enabled,
2136174a1e2SMaxime Ripard 
2146174a1e2SMaxime Ripard 	.recalc_rate	= ccu_nm_recalc_rate,
2156174a1e2SMaxime Ripard 	.round_rate	= ccu_nm_round_rate,
2166174a1e2SMaxime Ripard 	.set_rate	= ccu_nm_set_rate,
2176174a1e2SMaxime Ripard };
218