1 // SPDX-License-Identifier: GPL-2.0+
3 // OWL factor clock driver
5 // Copyright (c) 2014 Actions Semi Inc.
8 // Copyright (c) 2018 Linaro Ltd.
11 #include <linux/clk-provider.h>
12 #include <linux/regmap.h>
14 #include "owl-factor.h"
16 static unsigned int _get_table_maxval(const struct clk_factor_table *table)
18 unsigned int maxval = 0;
19 const struct clk_factor_table *clkt;
21 for (clkt = table; clkt->div; clkt++)
22 if (clkt->val > maxval)
27 static int _get_table_div_mul(const struct clk_factor_table *table,
28 unsigned int val, unsigned int *mul, unsigned int *div)
30 const struct clk_factor_table *clkt;
32 for (clkt = table; clkt->div; clkt++) {
33 if (clkt->val == val) {
43 static unsigned int _get_table_val(const struct clk_factor_table *table,
44 unsigned long rate, unsigned long parent_rate)
46 const struct clk_factor_table *clkt;
50 for (clkt = table; clkt->div; clkt++) {
51 calc_rate = parent_rate * clkt->mul;
52 do_div(calc_rate, clkt->div);
54 if ((unsigned long)calc_rate <= rate) {
61 val = _get_table_maxval(table);
66 static int owl_clk_val_best(const struct owl_factor_hw *factor_hw,
67 struct clk_hw *hw, unsigned long rate,
68 unsigned long *best_parent_rate)
70 const struct clk_factor_table *clkt = factor_hw->table;
71 unsigned long parent_rate, try_parent_rate, best = 0, cur_rate;
72 unsigned long parent_rate_saved = *best_parent_rate;
78 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
79 parent_rate = *best_parent_rate;
80 bestval = _get_table_val(clkt, rate, parent_rate);
84 for (clkt = factor_hw->table; clkt->div; clkt++) {
85 try_parent_rate = rate * clkt->div / clkt->mul;
87 if (try_parent_rate == parent_rate_saved) {
88 pr_debug("%s: [%d %d %d] found try_parent_rate %ld\n",
89 __func__, clkt->val, clkt->mul, clkt->div,
92 * It's the most ideal case if the requested rate can be
93 * divided from parent clock without any need to change
94 * parent rate, so return the divider immediately.
96 *best_parent_rate = parent_rate_saved;
100 parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw),
102 cur_rate = DIV_ROUND_UP(parent_rate, clkt->div) * clkt->mul;
103 if (cur_rate <= rate && cur_rate > best) {
106 *best_parent_rate = parent_rate;
111 bestval = _get_table_maxval(clkt);
112 *best_parent_rate = clk_hw_round_rate(
113 clk_hw_get_parent(hw), 1);
119 long owl_factor_helper_round_rate(struct owl_clk_common *common,
120 const struct owl_factor_hw *factor_hw,
122 unsigned long *parent_rate)
124 const struct clk_factor_table *clkt = factor_hw->table;
125 unsigned int val, mul = 0, div = 1;
127 val = owl_clk_val_best(factor_hw, &common->hw, rate, parent_rate);
128 _get_table_div_mul(clkt, val, &mul, &div);
130 return *parent_rate * mul / div;
133 static long owl_factor_round_rate(struct clk_hw *hw, unsigned long rate,
134 unsigned long *parent_rate)
136 struct owl_factor *factor = hw_to_owl_factor(hw);
137 struct owl_factor_hw *factor_hw = &factor->factor_hw;
139 return owl_factor_helper_round_rate(&factor->common, factor_hw,
143 unsigned long owl_factor_helper_recalc_rate(struct owl_clk_common *common,
144 const struct owl_factor_hw *factor_hw,
145 unsigned long parent_rate)
147 const struct clk_factor_table *clkt = factor_hw->table;
148 unsigned long long int rate;
149 u32 reg, val, mul, div;
154 regmap_read(common->regmap, factor_hw->reg, ®);
156 val = reg >> factor_hw->shift;
157 val &= div_mask(factor_hw);
159 _get_table_div_mul(clkt, val, &mul, &div);
161 WARN(!(factor_hw->fct_flags & CLK_DIVIDER_ALLOW_ZERO),
162 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
163 __clk_get_name(common->hw.clk));
167 rate = (unsigned long long int)parent_rate * mul;
173 static unsigned long owl_factor_recalc_rate(struct clk_hw *hw,
174 unsigned long parent_rate)
176 struct owl_factor *factor = hw_to_owl_factor(hw);
177 struct owl_factor_hw *factor_hw = &factor->factor_hw;
178 struct owl_clk_common *common = &factor->common;
180 return owl_factor_helper_recalc_rate(common, factor_hw, parent_rate);
183 int owl_factor_helper_set_rate(const struct owl_clk_common *common,
184 const struct owl_factor_hw *factor_hw,
186 unsigned long parent_rate)
190 val = _get_table_val(factor_hw->table, rate, parent_rate);
192 if (val > div_mask(factor_hw))
193 val = div_mask(factor_hw);
195 regmap_read(common->regmap, factor_hw->reg, ®);
197 reg &= ~(div_mask(factor_hw) << factor_hw->shift);
198 reg |= val << factor_hw->shift;
200 regmap_write(common->regmap, factor_hw->reg, reg);
205 static int owl_factor_set_rate(struct clk_hw *hw, unsigned long rate,
206 unsigned long parent_rate)
208 struct owl_factor *factor = hw_to_owl_factor(hw);
209 struct owl_factor_hw *factor_hw = &factor->factor_hw;
210 struct owl_clk_common *common = &factor->common;
212 return owl_factor_helper_set_rate(common, factor_hw,
216 const struct clk_ops owl_factor_ops = {
217 .round_rate = owl_factor_round_rate,
218 .recalc_rate = owl_factor_recalc_rate,
219 .set_rate = owl_factor_set_rate,