xref: /linux/drivers/clk/sunxi-ng/ccu_nm.c (revision 87ba9e5962f3f6e9a9a44cc332d1ad222d1c0302)
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 #include <linux/rational.h>
136174a1e2SMaxime Ripard 
146174a1e2SMaxime Ripard #include "ccu_frac.h"
156174a1e2SMaxime Ripard #include "ccu_gate.h"
166174a1e2SMaxime Ripard #include "ccu_nm.h"
176174a1e2SMaxime Ripard 
186174a1e2SMaxime Ripard static void ccu_nm_disable(struct clk_hw *hw)
196174a1e2SMaxime Ripard {
206174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
216174a1e2SMaxime Ripard 
226174a1e2SMaxime Ripard 	return ccu_gate_helper_disable(&nm->common, nm->enable);
236174a1e2SMaxime Ripard }
246174a1e2SMaxime Ripard 
256174a1e2SMaxime Ripard static int ccu_nm_enable(struct clk_hw *hw)
266174a1e2SMaxime Ripard {
276174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
286174a1e2SMaxime Ripard 
296174a1e2SMaxime Ripard 	return ccu_gate_helper_enable(&nm->common, nm->enable);
306174a1e2SMaxime Ripard }
316174a1e2SMaxime Ripard 
326174a1e2SMaxime Ripard static int ccu_nm_is_enabled(struct clk_hw *hw)
336174a1e2SMaxime Ripard {
346174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
356174a1e2SMaxime Ripard 
366174a1e2SMaxime Ripard 	return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
376174a1e2SMaxime Ripard }
386174a1e2SMaxime Ripard 
396174a1e2SMaxime Ripard static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
406174a1e2SMaxime Ripard 					unsigned long parent_rate)
416174a1e2SMaxime Ripard {
426174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
436174a1e2SMaxime Ripard 	unsigned long n, m;
446174a1e2SMaxime Ripard 	u32 reg;
456174a1e2SMaxime Ripard 
466174a1e2SMaxime Ripard 	if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac))
476174a1e2SMaxime Ripard 		return ccu_frac_helper_read_rate(&nm->common, &nm->frac);
486174a1e2SMaxime Ripard 
496174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
506174a1e2SMaxime Ripard 
516174a1e2SMaxime Ripard 	n = reg >> nm->n.shift;
526174a1e2SMaxime Ripard 	n &= (1 << nm->n.width) - 1;
536174a1e2SMaxime Ripard 
546174a1e2SMaxime Ripard 	m = reg >> nm->m.shift;
556174a1e2SMaxime Ripard 	m &= (1 << nm->m.width) - 1;
566174a1e2SMaxime Ripard 
576174a1e2SMaxime Ripard 	return parent_rate * (n + 1) / (m + 1);
586174a1e2SMaxime Ripard }
596174a1e2SMaxime Ripard 
606174a1e2SMaxime Ripard static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
616174a1e2SMaxime Ripard 			      unsigned long *parent_rate)
626174a1e2SMaxime Ripard {
636174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
64*87ba9e59SMaxime Ripard 	unsigned long max_n, max_m;
656174a1e2SMaxime Ripard 	unsigned long n, m;
666174a1e2SMaxime Ripard 
67*87ba9e59SMaxime Ripard 	max_n = 1 << nm->n.width;
68*87ba9e59SMaxime Ripard 	max_m = nm->m.max ?: 1 << nm->m.width;
69*87ba9e59SMaxime Ripard 
70*87ba9e59SMaxime Ripard 	rational_best_approximation(rate, *parent_rate, max_n, max_m, &n, &m);
716174a1e2SMaxime Ripard 
726174a1e2SMaxime Ripard 	return *parent_rate * n / m;
736174a1e2SMaxime Ripard }
746174a1e2SMaxime Ripard 
756174a1e2SMaxime Ripard static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
766174a1e2SMaxime Ripard 			   unsigned long parent_rate)
776174a1e2SMaxime Ripard {
786174a1e2SMaxime Ripard 	struct ccu_nm *nm = hw_to_ccu_nm(hw);
796174a1e2SMaxime Ripard 	unsigned long flags;
80*87ba9e59SMaxime Ripard 	unsigned long max_n, max_m;
816174a1e2SMaxime Ripard 	unsigned long n, m;
826174a1e2SMaxime Ripard 	u32 reg;
836174a1e2SMaxime Ripard 
846174a1e2SMaxime Ripard 	if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate))
856174a1e2SMaxime Ripard 		return ccu_frac_helper_set_rate(&nm->common, &nm->frac, rate);
866174a1e2SMaxime Ripard 	else
876174a1e2SMaxime Ripard 		ccu_frac_helper_disable(&nm->common, &nm->frac);
886174a1e2SMaxime Ripard 
89*87ba9e59SMaxime Ripard 	max_n = 1 << nm->n.width;
90*87ba9e59SMaxime Ripard 	max_m = nm->m.max ?: 1 << nm->m.width;
91*87ba9e59SMaxime Ripard 
92*87ba9e59SMaxime Ripard 	rational_best_approximation(rate, parent_rate, max_n, max_m, &n, &m);
936174a1e2SMaxime Ripard 
946174a1e2SMaxime Ripard 	spin_lock_irqsave(nm->common.lock, flags);
956174a1e2SMaxime Ripard 
966174a1e2SMaxime Ripard 	reg = readl(nm->common.base + nm->common.reg);
976174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
986174a1e2SMaxime Ripard 	reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
996174a1e2SMaxime Ripard 
1006174a1e2SMaxime Ripard 	writel(reg | ((m - 1) << nm->m.shift) | ((n - 1) << nm->n.shift),
1016174a1e2SMaxime Ripard 	       nm->common.base + nm->common.reg);
1026174a1e2SMaxime Ripard 
1036174a1e2SMaxime Ripard 	spin_unlock_irqrestore(nm->common.lock, flags);
1046174a1e2SMaxime Ripard 
1056174a1e2SMaxime Ripard 	ccu_helper_wait_for_lock(&nm->common, nm->lock);
1066174a1e2SMaxime Ripard 
1076174a1e2SMaxime Ripard 	return 0;
1086174a1e2SMaxime Ripard }
1096174a1e2SMaxime Ripard 
1106174a1e2SMaxime Ripard const struct clk_ops ccu_nm_ops = {
1116174a1e2SMaxime Ripard 	.disable	= ccu_nm_disable,
1126174a1e2SMaxime Ripard 	.enable		= ccu_nm_enable,
1136174a1e2SMaxime Ripard 	.is_enabled	= ccu_nm_is_enabled,
1146174a1e2SMaxime Ripard 
1156174a1e2SMaxime Ripard 	.recalc_rate	= ccu_nm_recalc_rate,
1166174a1e2SMaxime Ripard 	.round_rate	= ccu_nm_round_rate,
1176174a1e2SMaxime Ripard 	.set_rate	= ccu_nm_set_rate,
1186174a1e2SMaxime Ripard };
119