^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2016 Maxime Ripard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Maxime Ripard <maxime.ripard@free-electrons.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/clk-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "ccu_gate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "ccu_nk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct _ccu_nk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) unsigned long n, min_n, max_n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) unsigned long k, min_k, max_k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static void ccu_nk_find_best(unsigned long parent, unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct _ccu_nk *nk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long best_rate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int best_k = 0, best_n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int _k, _n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) for (_k = nk->min_k; _k <= nk->max_k; _k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) for (_n = nk->min_n; _n <= nk->max_n; _n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long tmp_rate = parent * _n * _k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (tmp_rate > rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if ((rate - tmp_rate) < (rate - best_rate)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) best_rate = tmp_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) best_k = _k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) best_n = _n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^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) nk->k = best_k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) nk->n = best_n;
^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 void ccu_nk_disable(struct clk_hw *hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct ccu_nk *nk = hw_to_ccu_nk(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return ccu_gate_helper_disable(&nk->common, nk->enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int ccu_nk_enable(struct clk_hw *hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct ccu_nk *nk = hw_to_ccu_nk(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return ccu_gate_helper_enable(&nk->common, nk->enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int ccu_nk_is_enabled(struct clk_hw *hw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct ccu_nk *nk = hw_to_ccu_nk(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return ccu_gate_helper_is_enabled(&nk->common, nk->enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static unsigned long ccu_nk_recalc_rate(struct clk_hw *hw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ccu_nk *nk = hw_to_ccu_nk(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned long rate, n, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) reg = readl(nk->common.base + nk->common.reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) n = reg >> nk->n.shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) n &= (1 << nk->n.width) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) n += nk->n.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) k = reg >> nk->k.shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) k &= (1 << nk->k.width) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) k += nk->k.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) k++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) rate = parent_rate * n * k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) rate /= nk->fixed_post_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static long ccu_nk_round_rate(struct clk_hw *hw, unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned long *parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct ccu_nk *nk = hw_to_ccu_nk(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct _ccu_nk _nk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) rate *= nk->fixed_post_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) _nk.min_n = nk->n.min ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) _nk.max_n = nk->n.max ?: 1 << nk->n.width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) _nk.min_k = nk->k.min ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) _nk.max_k = nk->k.max ?: 1 << nk->k.width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ccu_nk_find_best(*parent_rate, rate, &_nk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) rate = *parent_rate * _nk.n * _nk.k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) rate = rate / nk->fixed_post_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int ccu_nk_set_rate(struct clk_hw *hw, unsigned long rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) unsigned long parent_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct ccu_nk *nk = hw_to_ccu_nk(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct _ccu_nk _nk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (nk->common.features & CCU_FEATURE_FIXED_POSTDIV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) rate = rate * nk->fixed_post_div;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) _nk.min_n = nk->n.min ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) _nk.max_n = nk->n.max ?: 1 << nk->n.width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) _nk.min_k = nk->k.min ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) _nk.max_k = nk->k.max ?: 1 << nk->k.width;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ccu_nk_find_best(parent_rate, rate, &_nk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) spin_lock_irqsave(nk->common.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) reg = readl(nk->common.base + nk->common.reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) reg &= ~GENMASK(nk->n.width + nk->n.shift - 1, nk->n.shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) reg &= ~GENMASK(nk->k.width + nk->k.shift - 1, nk->k.shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) reg |= (_nk.k - nk->k.offset) << nk->k.shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) reg |= (_nk.n - nk->n.offset) << nk->n.shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) writel(reg, nk->common.base + nk->common.reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) spin_unlock_irqrestore(nk->common.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ccu_helper_wait_for_lock(&nk->common, nk->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) const struct clk_ops ccu_nk_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .disable = ccu_nk_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .enable = ccu_nk_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .is_enabled = ccu_nk_is_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .recalc_rate = ccu_nk_recalc_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .round_rate = ccu_nk_round_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .set_rate = ccu_nk_set_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) };