^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * mmp factor clock operation source file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2012 Marvell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Chao Xie <xiechao.mail@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file is licensed under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * License version 2. This program is licensed "as is" without any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * warranty of any kind, whether express or implied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/clk-provider.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) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "clk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * It is M/N clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Fout from synthesizer can be given from two equations:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * numerator/denominator = Fin / (Fout * factor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define to_clk_factor(hw) container_of(hw, struct mmp_clk_factor, hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static long clk_factor_round_rate(struct clk_hw *hw, unsigned long drate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long *prate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct mmp_clk_factor *factor = to_clk_factor(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u64 rate = 0, prev_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) for (i = 0; i < factor->ftbl_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) prev_rate = rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) rate = *prate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) rate *= factor->ftbl[i].den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) do_div(rate, factor->ftbl[i].num * factor->masks->factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (rate > drate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if ((i == 0) || (i == factor->ftbl_cnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if ((drate - prev_rate) > (rate - drate))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return prev_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static unsigned long clk_factor_recalc_rate(struct clk_hw *hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct mmp_clk_factor *factor = to_clk_factor(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct mmp_clk_factor_masks *masks = factor->masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned int val, num, den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u64 rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) val = readl_relaxed(factor->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* calculate numerator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) num = (val >> masks->num_shift) & masks->num_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* calculate denominator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) den = (val >> masks->den_shift) & masks->den_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!den)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) rate = parent_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) rate *= den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) do_div(rate, num * factor->masks->factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* Configures new clock rate*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int clk_factor_set_rate(struct clk_hw *hw, unsigned long drate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned long prate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct mmp_clk_factor *factor = to_clk_factor(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct mmp_clk_factor_masks *masks = factor->masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u64 rate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) for (i = 0; i < factor->ftbl_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) rate = prate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) rate *= factor->ftbl[i].den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) do_div(rate, factor->ftbl[i].num * factor->masks->factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (rate > drate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (i > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (factor->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) spin_lock_irqsave(factor->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) val = readl_relaxed(factor->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) val &= ~(masks->num_mask << masks->num_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) val |= (factor->ftbl[i].num & masks->num_mask) << masks->num_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) val &= ~(masks->den_mask << masks->den_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) val |= (factor->ftbl[i].den & masks->den_mask) << masks->den_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) writel_relaxed(val, factor->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (factor->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) spin_unlock_irqrestore(factor->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^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) static int clk_factor_init(struct clk_hw *hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct mmp_clk_factor *factor = to_clk_factor(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct mmp_clk_factor_masks *masks = factor->masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) u32 val, num, den;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (factor->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) spin_lock_irqsave(factor->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) val = readl(factor->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* calculate numerator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) num = (val >> masks->num_shift) & masks->num_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* calculate denominator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) den = (val >> masks->den_shift) & masks->den_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) for (i = 0; i < factor->ftbl_cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (den == factor->ftbl[i].den && num == factor->ftbl[i].num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (i >= factor->ftbl_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) val &= ~(masks->num_mask << masks->num_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) val |= (factor->ftbl[0].num & masks->num_mask) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) masks->num_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) val &= ~(masks->den_mask << masks->den_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) val |= (factor->ftbl[0].den & masks->den_mask) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) masks->den_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!(val & masks->enable_mask) || i >= factor->ftbl_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) val |= masks->enable_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) writel(val, factor->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (factor->lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) spin_unlock_irqrestore(factor->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static const struct clk_ops clk_factor_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .recalc_rate = clk_factor_recalc_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .round_rate = clk_factor_round_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .set_rate = clk_factor_set_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .init = clk_factor_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct clk *mmp_clk_register_factor(const char *name, const char *parent_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) unsigned long flags, void __iomem *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct mmp_clk_factor_masks *masks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct mmp_clk_factor_tbl *ftbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) unsigned int ftbl_cnt, spinlock_t *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct mmp_clk_factor *factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct clk_init_data init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!masks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) pr_err("%s: must pass a clk_factor_mask\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) factor = kzalloc(sizeof(*factor), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!factor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* struct clk_aux assignments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) factor->base = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) factor->masks = masks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) factor->ftbl = ftbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) factor->ftbl_cnt = ftbl_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) factor->hw.init = &init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) factor->lock = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) init.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) init.ops = &clk_factor_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) init.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) init.parent_names = &parent_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) init.num_parents = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) clk = clk_register(NULL, &factor->hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (IS_ERR_OR_NULL(clk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) kfree(factor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }