xref: /linux/drivers/clk/sunxi-ng/ccu_nm.c (revision 0c3c8e135897eb8e896a0bb82a5aff6c9bc158cc)
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);
736174a1e2SMaxime Ripard 	unsigned long n, m;
746174a1e2SMaxime Ripard 	u32 reg;
756174a1e2SMaxime Ripard 
766174a1e2SMaxime Ripard 	if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac))
776174a1e2SMaxime Ripard 		return ccu_frac_helper_read_rate(&nm->common, &nm->frac);
786174a1e2SMaxime Ripard 
796174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
806174a1e2SMaxime Ripard 
816174a1e2SMaxime Ripard 	n = reg >> nm->n.shift;
826174a1e2SMaxime Ripard 	n &= (1 << nm->n.width) - 1;
83e66f81bbSMaxime Ripard 	n += nm->n.offset;
84e66f81bbSMaxime Ripard 	if (!n)
85e66f81bbSMaxime Ripard 		n++;
866174a1e2SMaxime Ripard 
876174a1e2SMaxime Ripard 	m = reg >> nm->m.shift;
886174a1e2SMaxime Ripard 	m &= (1 << nm->m.width) - 1;
89e66f81bbSMaxime Ripard 	m += nm->m.offset;
90e66f81bbSMaxime Ripard 	if (!m)
91e66f81bbSMaxime Ripard 		m++;
926174a1e2SMaxime Ripard 
93e66f81bbSMaxime Ripard 	return parent_rate * n / m;
946174a1e2SMaxime Ripard }
956174a1e2SMaxime Ripard 
966174a1e2SMaxime Ripard static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
976174a1e2SMaxime Ripard 			      unsigned long *parent_rate)
986174a1e2SMaxime Ripard {
996174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
100ee28648cSMaxime Ripard 	struct _ccu_nm _nm;
1016174a1e2SMaxime Ripard 
1022beaa601SMaxime Ripard 	_nm.min_n = nm->n.min;
103*0c3c8e13SMaxime Ripard 	_nm.max_n = nm->n.max ?: 1 << nm->n.width;
1046e0d50daSMaxime Ripard 	_nm.min_m = 1;
105ee28648cSMaxime Ripard 	_nm.max_m = nm->m.max ?: 1 << nm->m.width;
10687ba9e59SMaxime Ripard 
107ee28648cSMaxime Ripard 	ccu_nm_find_best(*parent_rate, rate, &_nm);
1086174a1e2SMaxime Ripard 
109ee28648cSMaxime Ripard 	return *parent_rate * _nm.n / _nm.m;
1106174a1e2SMaxime Ripard }
1116174a1e2SMaxime Ripard 
1126174a1e2SMaxime Ripard static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
1136174a1e2SMaxime Ripard 			   unsigned long parent_rate)
1146174a1e2SMaxime Ripard {
1156174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
116ee28648cSMaxime Ripard 	struct _ccu_nm _nm;
1176174a1e2SMaxime Ripard 	unsigned long flags;
1186174a1e2SMaxime Ripard 	u32 reg;
1196174a1e2SMaxime Ripard 
1206174a1e2SMaxime Ripard 	if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
1216174a1e2SMaxime Ripard 		return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
1226174a1e2SMaxime Ripard 	else
1236174a1e2SMaxime Ripard 		ccu_frac_helper_disable(&nm->common, &nm->frac);
1246174a1e2SMaxime Ripard 
1256e0d50daSMaxime Ripard 	_nm.min_n = 1;
126*0c3c8e13SMaxime Ripard 	_nm.max_n = nm->n.max ?: 1 << nm->n.width;
1276e0d50daSMaxime Ripard 	_nm.min_m = 1;
128ee28648cSMaxime Ripard 	_nm.max_m = nm->m.max ?: 1 << nm->m.width;
12987ba9e59SMaxime Ripard 
130ee28648cSMaxime Ripard 	ccu_nm_find_best(parent_rate, rate, &_nm);
1316174a1e2SMaxime Ripard 
1326174a1e2SMaxime Ripard 	spin_lock_irqsave(nm->common.lock, flags);
1336174a1e2SMaxime Ripard 
1346174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
1356174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
1366174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
1376174a1e2SMaxime Ripard 
138e66f81bbSMaxime Ripard 	reg |= (_nm.n - nm->n.offset) << nm->n.shift;
139e66f81bbSMaxime Ripard 	reg |= (_nm.m - nm->m.offset) << nm->m.shift;
140e66f81bbSMaxime Ripard 	writel(reg, nm->common.base + nm->common.reg);
1416174a1e2SMaxime Ripard 
1426174a1e2SMaxime Ripard 	spin_unlock_irqrestore(nm->common.lock, flags);
1436174a1e2SMaxime Ripard 
1446174a1e2SMaxime Ripard 	ccu_helper_wait_for_lock(&nm->common, nm->lock);
1456174a1e2SMaxime Ripard 
1466174a1e2SMaxime Ripard 	return 0;
1476174a1e2SMaxime Ripard }
1486174a1e2SMaxime Ripard 
1496174a1e2SMaxime Ripard const struct clk_ops ccu_nm_ops = {
1506174a1e2SMaxime Ripard 	.disable	= ccu_nm_disable,
1516174a1e2SMaxime Ripard 	.enable		= ccu_nm_enable,
1526174a1e2SMaxime Ripard 	.is_enabled	= ccu_nm_is_enabled,
1536174a1e2SMaxime Ripard 
1546174a1e2SMaxime Ripard 	.recalc_rate	= ccu_nm_recalc_rate,
1556174a1e2SMaxime Ripard 	.round_rate	= ccu_nm_round_rate,
1566174a1e2SMaxime Ripard 	.set_rate	= ccu_nm_set_rate,
1576174a1e2SMaxime Ripard };
158