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.
11 #include <linux/clk-provider.h>
17 unsigned long mult, min, max;
20 static void ccu_mult_find_best(unsigned long parent, unsigned long rate,
21 struct _ccu_mult *mult)
25 _mult = rate / parent;
26 if (_mult < mult->min)
29 if (_mult > mult->max)
35 static unsigned long ccu_mult_round_rate(struct ccu_mux_internal *mux,
36 unsigned long parent_rate,
40 struct ccu_mult *cm = data;
43 _cm.min = cm->mult.min;
46 _cm.max = cm->mult.max;
48 _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
50 ccu_mult_find_best(parent_rate, rate, &_cm);
52 return parent_rate * _cm.mult;
55 static void ccu_mult_disable(struct clk_hw *hw)
57 struct ccu_mult *cm = hw_to_ccu_mult(hw);
59 return ccu_gate_helper_disable(&cm->common, cm->enable);
62 static int ccu_mult_enable(struct clk_hw *hw)
64 struct ccu_mult *cm = hw_to_ccu_mult(hw);
66 return ccu_gate_helper_enable(&cm->common, cm->enable);
69 static int ccu_mult_is_enabled(struct clk_hw *hw)
71 struct ccu_mult *cm = hw_to_ccu_mult(hw);
73 return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
76 static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw,
77 unsigned long parent_rate)
79 struct ccu_mult *cm = hw_to_ccu_mult(hw);
83 if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac))
84 return ccu_frac_helper_read_rate(&cm->common, &cm->frac);
86 reg = readl(cm->common.base + cm->common.reg);
87 val = reg >> cm->mult.shift;
88 val &= (1 << cm->mult.width) - 1;
90 ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
93 return parent_rate * (val + cm->mult.offset);
96 static int ccu_mult_determine_rate(struct clk_hw *hw,
97 struct clk_rate_request *req)
99 struct ccu_mult *cm = hw_to_ccu_mult(hw);
101 return ccu_mux_helper_determine_rate(&cm->common, &cm->mux,
102 req, ccu_mult_round_rate, cm);
105 static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate,
106 unsigned long parent_rate)
108 struct ccu_mult *cm = hw_to_ccu_mult(hw);
109 struct _ccu_mult _cm;
113 if (ccu_frac_helper_has_rate(&cm->common, &cm->frac, rate))
114 return ccu_frac_helper_set_rate(&cm->common, &cm->frac, rate);
116 ccu_frac_helper_disable(&cm->common, &cm->frac);
118 ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1,
121 _cm.min = cm->mult.min;
124 _cm.max = cm->mult.max;
126 _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1;
128 ccu_mult_find_best(parent_rate, rate, &_cm);
130 spin_lock_irqsave(cm->common.lock, flags);
132 reg = readl(cm->common.base + cm->common.reg);
133 reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift);
134 reg |= ((_cm.mult - cm->mult.offset) << cm->mult.shift);
136 writel(reg, cm->common.base + cm->common.reg);
138 spin_unlock_irqrestore(cm->common.lock, flags);
143 static u8 ccu_mult_get_parent(struct clk_hw *hw)
145 struct ccu_mult *cm = hw_to_ccu_mult(hw);
147 return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
150 static int ccu_mult_set_parent(struct clk_hw *hw, u8 index)
152 struct ccu_mult *cm = hw_to_ccu_mult(hw);
154 return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
157 const struct clk_ops ccu_mult_ops = {
158 .disable = ccu_mult_disable,
159 .enable = ccu_mult_enable,
160 .is_enabled = ccu_mult_is_enabled,
162 .get_parent = ccu_mult_get_parent,
163 .set_parent = ccu_mult_set_parent,
165 .determine_rate = ccu_mult_determine_rate,
166 .recalc_rate = ccu_mult_recalc_rate,
167 .set_rate = ccu_mult_set_rate,