^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // OWL factor clock driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (c) 2014 Actions Semi Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Author: David Liu <liuwei@actions-semi.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) // Copyright (c) 2018 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) // Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/clk-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "owl-factor.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static unsigned int _get_table_maxval(const struct clk_factor_table *table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned int maxval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) const struct clk_factor_table *clkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) for (clkt = table; clkt->div; clkt++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (clkt->val > maxval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) maxval = clkt->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return maxval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int _get_table_div_mul(const struct clk_factor_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned int val, unsigned int *mul, unsigned int *div)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) const struct clk_factor_table *clkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) for (clkt = table; clkt->div; clkt++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (clkt->val == val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *mul = clkt->mul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *div = clkt->div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static unsigned int _get_table_val(const struct clk_factor_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned long rate, unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const struct clk_factor_table *clkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int val = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u64 calc_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (clkt = table; clkt->div; clkt++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) calc_rate = parent_rate * clkt->mul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) do_div(calc_rate, clkt->div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if ((unsigned long)calc_rate <= rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) val = clkt->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (val == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) val = _get_table_maxval(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int owl_clk_val_best(const struct owl_factor_hw *factor_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct clk_hw *hw, unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned long *best_parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const struct clk_factor_table *clkt = factor_hw->table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned long parent_rate, try_parent_rate, best = 0, cur_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned long parent_rate_saved = *best_parent_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int bestval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) rate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) parent_rate = *best_parent_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) bestval = _get_table_val(clkt, rate, parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return bestval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) for (clkt = factor_hw->table; clkt->div; clkt++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) try_parent_rate = rate * clkt->div / clkt->mul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (try_parent_rate == parent_rate_saved) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) pr_debug("%s: [%d %d %d] found try_parent_rate %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) __func__, clkt->val, clkt->mul, clkt->div,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) try_parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * It's the most ideal case if the requested rate can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * divided from parent clock without any need to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * parent rate, so return the divider immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) *best_parent_rate = parent_rate_saved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return clkt->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) try_parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cur_rate = DIV_ROUND_UP(parent_rate, clkt->div) * clkt->mul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (cur_rate <= rate && cur_rate > best) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) bestval = clkt->val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) best = cur_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *best_parent_rate = parent_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!bestval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) bestval = _get_table_maxval(clkt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *best_parent_rate = clk_hw_round_rate(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) clk_hw_get_parent(hw), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return bestval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) long owl_factor_helper_round_rate(struct owl_clk_common *common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) const struct owl_factor_hw *factor_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned long *parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) const struct clk_factor_table *clkt = factor_hw->table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned int val, mul = 0, div = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) val = owl_clk_val_best(factor_hw, &common->hw, rate, parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) _get_table_div_mul(clkt, val, &mul, &div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return *parent_rate * mul / div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static long owl_factor_round_rate(struct clk_hw *hw, unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned long *parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct owl_factor *factor = hw_to_owl_factor(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct owl_factor_hw *factor_hw = &factor->factor_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return owl_factor_helper_round_rate(&factor->common, factor_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) rate, parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned long owl_factor_helper_recalc_rate(struct owl_clk_common *common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) const struct owl_factor_hw *factor_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const struct clk_factor_table *clkt = factor_hw->table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) unsigned long long int rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) u32 reg, val, mul, div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) div = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) mul = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) regmap_read(common->regmap, factor_hw->reg, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) val = reg >> factor_hw->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) val &= div_mask(factor_hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) _get_table_div_mul(clkt, val, &mul, &div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!div) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) WARN(!(factor_hw->fct_flags & CLK_DIVIDER_ALLOW_ZERO),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) __clk_get_name(common->hw.clk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return parent_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) rate = (unsigned long long int)parent_rate * mul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) do_div(rate, div);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static unsigned long owl_factor_recalc_rate(struct clk_hw *hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct owl_factor *factor = hw_to_owl_factor(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct owl_factor_hw *factor_hw = &factor->factor_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct owl_clk_common *common = &factor->common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return owl_factor_helper_recalc_rate(common, factor_hw, parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int owl_factor_helper_set_rate(const struct owl_clk_common *common,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) const struct owl_factor_hw *factor_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) u32 val, reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) val = _get_table_val(factor_hw->table, rate, parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (val > div_mask(factor_hw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) val = div_mask(factor_hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) regmap_read(common->regmap, factor_hw->reg, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) reg &= ~(div_mask(factor_hw) << factor_hw->shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) reg |= val << factor_hw->shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) regmap_write(common->regmap, factor_hw->reg, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int owl_factor_set_rate(struct clk_hw *hw, unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct owl_factor *factor = hw_to_owl_factor(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct owl_factor_hw *factor_hw = &factor->factor_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct owl_clk_common *common = &factor->common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return owl_factor_helper_set_rate(common, factor_hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) rate, parent_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) const struct clk_ops owl_factor_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .round_rate = owl_factor_round_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) .recalc_rate = owl_factor_recalc_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .set_rate = owl_factor_set_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) };