12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2df6561e6SMaxime Ripard /* 3df6561e6SMaxime Ripard * Copyright (C) 2016 Maxime Ripard 4df6561e6SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com> 5df6561e6SMaxime Ripard */ 6df6561e6SMaxime Ripard 7df6561e6SMaxime Ripard #include <linux/clk-provider.h> 862e59c4eSStephen Boyd #include <linux/io.h> 9df6561e6SMaxime Ripard 10df6561e6SMaxime Ripard #include "ccu_gate.h" 11df6561e6SMaxime Ripard #include "ccu_nkm.h" 12df6561e6SMaxime Ripard 13df6561e6SMaxime Ripard struct _ccu_nkm { 146e0d50daSMaxime Ripard unsigned long n, min_n, max_n; 156e0d50daSMaxime Ripard unsigned long k, min_k, max_k; 166e0d50daSMaxime Ripard unsigned long m, min_m, max_m; 17df6561e6SMaxime Ripard }; 18df6561e6SMaxime Ripard 19*088cefcaSFrank Oltmanns static unsigned long ccu_nkm_find_best_with_parent_adj(struct ccu_common *common, 20*088cefcaSFrank Oltmanns struct clk_hw *parent_hw, 213492e4f6SFrank Oltmanns unsigned long *parent, unsigned long rate, 223492e4f6SFrank Oltmanns struct _ccu_nkm *nkm) 233492e4f6SFrank Oltmanns { 243492e4f6SFrank Oltmanns unsigned long best_rate = 0, best_parent_rate = *parent, tmp_parent = *parent; 253492e4f6SFrank Oltmanns unsigned long best_n = 0, best_k = 0, best_m = 0; 263492e4f6SFrank Oltmanns unsigned long _n, _k, _m; 273492e4f6SFrank Oltmanns 283492e4f6SFrank Oltmanns for (_k = nkm->min_k; _k <= nkm->max_k; _k++) { 293492e4f6SFrank Oltmanns for (_n = nkm->min_n; _n <= nkm->max_n; _n++) { 303492e4f6SFrank Oltmanns for (_m = nkm->min_m; _m <= nkm->max_m; _m++) { 313492e4f6SFrank Oltmanns unsigned long tmp_rate; 323492e4f6SFrank Oltmanns 333492e4f6SFrank Oltmanns tmp_parent = clk_hw_round_rate(parent_hw, rate * _m / (_n * _k)); 343492e4f6SFrank Oltmanns 353492e4f6SFrank Oltmanns tmp_rate = tmp_parent * _n * _k / _m; 363492e4f6SFrank Oltmanns 37*088cefcaSFrank Oltmanns if (ccu_is_better_rate(common, rate, tmp_rate, best_rate)) { 383492e4f6SFrank Oltmanns best_rate = tmp_rate; 393492e4f6SFrank Oltmanns best_parent_rate = tmp_parent; 403492e4f6SFrank Oltmanns best_n = _n; 413492e4f6SFrank Oltmanns best_k = _k; 423492e4f6SFrank Oltmanns best_m = _m; 433492e4f6SFrank Oltmanns } 443492e4f6SFrank Oltmanns } 453492e4f6SFrank Oltmanns } 463492e4f6SFrank Oltmanns } 473492e4f6SFrank Oltmanns 483492e4f6SFrank Oltmanns nkm->n = best_n; 493492e4f6SFrank Oltmanns nkm->k = best_k; 503492e4f6SFrank Oltmanns nkm->m = best_m; 513492e4f6SFrank Oltmanns 523492e4f6SFrank Oltmanns *parent = best_parent_rate; 533492e4f6SFrank Oltmanns 543492e4f6SFrank Oltmanns return best_rate; 553492e4f6SFrank Oltmanns } 563492e4f6SFrank Oltmanns 57657f477aSSamuel Holland static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, 58*088cefcaSFrank Oltmanns struct _ccu_nkm *nkm, struct ccu_common *common) 59df6561e6SMaxime Ripard { 60df6561e6SMaxime Ripard unsigned long best_rate = 0; 61df6561e6SMaxime Ripard unsigned long best_n = 0, best_k = 0, best_m = 0; 62df6561e6SMaxime Ripard unsigned long _n, _k, _m; 63df6561e6SMaxime Ripard 646e0d50daSMaxime Ripard for (_k = nkm->min_k; _k <= nkm->max_k; _k++) { 656e0d50daSMaxime Ripard for (_n = nkm->min_n; _n <= nkm->max_n; _n++) { 666e0d50daSMaxime Ripard for (_m = nkm->min_m; _m <= nkm->max_m; _m++) { 67df6561e6SMaxime Ripard unsigned long tmp_rate; 68df6561e6SMaxime Ripard 69df6561e6SMaxime Ripard tmp_rate = parent * _n * _k / _m; 70df6561e6SMaxime Ripard 71*088cefcaSFrank Oltmanns if (ccu_is_better_rate(common, rate, tmp_rate, best_rate)) { 72df6561e6SMaxime Ripard best_rate = tmp_rate; 73df6561e6SMaxime Ripard best_n = _n; 74df6561e6SMaxime Ripard best_k = _k; 75df6561e6SMaxime Ripard best_m = _m; 76df6561e6SMaxime Ripard } 77df6561e6SMaxime Ripard } 78ee28648cSMaxime Ripard } 79ee28648cSMaxime Ripard } 80df6561e6SMaxime Ripard 81df6561e6SMaxime Ripard nkm->n = best_n; 82df6561e6SMaxime Ripard nkm->k = best_k; 83df6561e6SMaxime Ripard nkm->m = best_m; 84657f477aSSamuel Holland 85657f477aSSamuel Holland return best_rate; 86df6561e6SMaxime Ripard } 87df6561e6SMaxime Ripard 88df6561e6SMaxime Ripard static void ccu_nkm_disable(struct clk_hw *hw) 89df6561e6SMaxime Ripard { 90df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 91df6561e6SMaxime Ripard 92df6561e6SMaxime Ripard return ccu_gate_helper_disable(&nkm->common, nkm->enable); 93df6561e6SMaxime Ripard } 94df6561e6SMaxime Ripard 95df6561e6SMaxime Ripard static int ccu_nkm_enable(struct clk_hw *hw) 96df6561e6SMaxime Ripard { 97df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 98df6561e6SMaxime Ripard 99df6561e6SMaxime Ripard return ccu_gate_helper_enable(&nkm->common, nkm->enable); 100df6561e6SMaxime Ripard } 101df6561e6SMaxime Ripard 102df6561e6SMaxime Ripard static int ccu_nkm_is_enabled(struct clk_hw *hw) 103df6561e6SMaxime Ripard { 104df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 105df6561e6SMaxime Ripard 106df6561e6SMaxime Ripard return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable); 107df6561e6SMaxime Ripard } 108df6561e6SMaxime Ripard 109df6561e6SMaxime Ripard static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw, 110df6561e6SMaxime Ripard unsigned long parent_rate) 111df6561e6SMaxime Ripard { 112df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 113a6653773SIcenowy Zheng unsigned long n, m, k, rate; 114df6561e6SMaxime Ripard u32 reg; 115df6561e6SMaxime Ripard 116df6561e6SMaxime Ripard reg = readl(nkm->common.base + nkm->common.reg); 117df6561e6SMaxime Ripard 118df6561e6SMaxime Ripard n = reg >> nkm->n.shift; 119df6561e6SMaxime Ripard n &= (1 << nkm->n.width) - 1; 120e66f81bbSMaxime Ripard n += nkm->n.offset; 121e66f81bbSMaxime Ripard if (!n) 122e66f81bbSMaxime Ripard n++; 123df6561e6SMaxime Ripard 124df6561e6SMaxime Ripard k = reg >> nkm->k.shift; 125df6561e6SMaxime Ripard k &= (1 << nkm->k.width) - 1; 126e66f81bbSMaxime Ripard k += nkm->k.offset; 127e66f81bbSMaxime Ripard if (!k) 128e66f81bbSMaxime Ripard k++; 129df6561e6SMaxime Ripard 130df6561e6SMaxime Ripard m = reg >> nkm->m.shift; 131df6561e6SMaxime Ripard m &= (1 << nkm->m.width) - 1; 132e66f81bbSMaxime Ripard m += nkm->m.offset; 133e66f81bbSMaxime Ripard if (!m) 134e66f81bbSMaxime Ripard m++; 135df6561e6SMaxime Ripard 136a6653773SIcenowy Zheng rate = parent_rate * n * k / m; 137a6653773SIcenowy Zheng 138a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) 139a6653773SIcenowy Zheng rate /= nkm->fixed_post_div; 140a6653773SIcenowy Zheng 141a6653773SIcenowy Zheng return rate; 142df6561e6SMaxime Ripard } 143df6561e6SMaxime Ripard 144a3658359SChen-Yu Tsai static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux, 14580c439cdSFrank Oltmanns struct clk_hw *parent_hw, 14610a8d9b9SMaxime Ripard unsigned long *parent_rate, 147a3658359SChen-Yu Tsai unsigned long rate, 148a3658359SChen-Yu Tsai void *data) 149df6561e6SMaxime Ripard { 150a3658359SChen-Yu Tsai struct ccu_nkm *nkm = data; 151df6561e6SMaxime Ripard struct _ccu_nkm _nkm; 152df6561e6SMaxime Ripard 1534162c5ceSChen-Yu Tsai _nkm.min_n = nkm->n.min ?: 1; 1540c3c8e13SMaxime Ripard _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width; 1554162c5ceSChen-Yu Tsai _nkm.min_k = nkm->k.min ?: 1; 1560c3c8e13SMaxime Ripard _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width; 1576e0d50daSMaxime Ripard _nkm.min_m = 1; 15887ba9e59SMaxime Ripard _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width; 159df6561e6SMaxime Ripard 160a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) 161a6653773SIcenowy Zheng rate *= nkm->fixed_post_div; 162a6653773SIcenowy Zheng 1633492e4f6SFrank Oltmanns if (!clk_hw_can_set_rate_parent(&nkm->common.hw)) 164*088cefcaSFrank Oltmanns rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm, &nkm->common); 1653492e4f6SFrank Oltmanns else 166*088cefcaSFrank Oltmanns rate = ccu_nkm_find_best_with_parent_adj(&nkm->common, parent_hw, parent_rate, rate, 167*088cefcaSFrank Oltmanns &_nkm); 168a6653773SIcenowy Zheng 169a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) 170a6653773SIcenowy Zheng rate /= nkm->fixed_post_div; 171a6653773SIcenowy Zheng 172a6653773SIcenowy Zheng return rate; 173a3658359SChen-Yu Tsai } 174a3658359SChen-Yu Tsai 175a3658359SChen-Yu Tsai static int ccu_nkm_determine_rate(struct clk_hw *hw, 176a3658359SChen-Yu Tsai struct clk_rate_request *req) 177a3658359SChen-Yu Tsai { 178a3658359SChen-Yu Tsai struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 179a3658359SChen-Yu Tsai 180a3658359SChen-Yu Tsai return ccu_mux_helper_determine_rate(&nkm->common, &nkm->mux, 181a3658359SChen-Yu Tsai req, ccu_nkm_round_rate, nkm); 182df6561e6SMaxime Ripard } 183df6561e6SMaxime Ripard 184df6561e6SMaxime Ripard static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate, 185df6561e6SMaxime Ripard unsigned long parent_rate) 186df6561e6SMaxime Ripard { 187df6561e6SMaxime Ripard struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 188df6561e6SMaxime Ripard struct _ccu_nkm _nkm; 189df6561e6SMaxime Ripard unsigned long flags; 190df6561e6SMaxime Ripard u32 reg; 191df6561e6SMaxime Ripard 192a6653773SIcenowy Zheng if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) 193a6653773SIcenowy Zheng rate *= nkm->fixed_post_div; 194a6653773SIcenowy Zheng 1954162c5ceSChen-Yu Tsai _nkm.min_n = nkm->n.min ?: 1; 1960c3c8e13SMaxime Ripard _nkm.max_n = nkm->n.max ?: 1 << nkm->n.width; 1974162c5ceSChen-Yu Tsai _nkm.min_k = nkm->k.min ?: 1; 1980c3c8e13SMaxime Ripard _nkm.max_k = nkm->k.max ?: 1 << nkm->k.width; 1996e0d50daSMaxime Ripard _nkm.min_m = 1; 20087ba9e59SMaxime Ripard _nkm.max_m = nkm->m.max ?: 1 << nkm->m.width; 201df6561e6SMaxime Ripard 202*088cefcaSFrank Oltmanns ccu_nkm_find_best(parent_rate, rate, &_nkm, &nkm->common); 203df6561e6SMaxime Ripard 204df6561e6SMaxime Ripard spin_lock_irqsave(nkm->common.lock, flags); 205df6561e6SMaxime Ripard 206df6561e6SMaxime Ripard reg = readl(nkm->common.base + nkm->common.reg); 207df6561e6SMaxime Ripard reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift); 208df6561e6SMaxime Ripard reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift); 209df6561e6SMaxime Ripard reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift); 210df6561e6SMaxime Ripard 211e66f81bbSMaxime Ripard reg |= (_nkm.n - nkm->n.offset) << nkm->n.shift; 212e66f81bbSMaxime Ripard reg |= (_nkm.k - nkm->k.offset) << nkm->k.shift; 213e66f81bbSMaxime Ripard reg |= (_nkm.m - nkm->m.offset) << nkm->m.shift; 214df6561e6SMaxime Ripard writel(reg, nkm->common.base + nkm->common.reg); 215df6561e6SMaxime Ripard 216df6561e6SMaxime Ripard spin_unlock_irqrestore(nkm->common.lock, flags); 217df6561e6SMaxime Ripard 218df6561e6SMaxime Ripard ccu_helper_wait_for_lock(&nkm->common, nkm->lock); 219df6561e6SMaxime Ripard 220df6561e6SMaxime Ripard return 0; 221df6561e6SMaxime Ripard } 222df6561e6SMaxime Ripard 223a3658359SChen-Yu Tsai static u8 ccu_nkm_get_parent(struct clk_hw *hw) 224a3658359SChen-Yu Tsai { 225a3658359SChen-Yu Tsai struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 226a3658359SChen-Yu Tsai 227a3658359SChen-Yu Tsai return ccu_mux_helper_get_parent(&nkm->common, &nkm->mux); 228a3658359SChen-Yu Tsai } 229a3658359SChen-Yu Tsai 230a3658359SChen-Yu Tsai static int ccu_nkm_set_parent(struct clk_hw *hw, u8 index) 231a3658359SChen-Yu Tsai { 232a3658359SChen-Yu Tsai struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 233a3658359SChen-Yu Tsai 234a3658359SChen-Yu Tsai return ccu_mux_helper_set_parent(&nkm->common, &nkm->mux, index); 235a3658359SChen-Yu Tsai } 236a3658359SChen-Yu Tsai 237df6561e6SMaxime Ripard const struct clk_ops ccu_nkm_ops = { 238df6561e6SMaxime Ripard .disable = ccu_nkm_disable, 239df6561e6SMaxime Ripard .enable = ccu_nkm_enable, 240df6561e6SMaxime Ripard .is_enabled = ccu_nkm_is_enabled, 241df6561e6SMaxime Ripard 242a3658359SChen-Yu Tsai .get_parent = ccu_nkm_get_parent, 243a3658359SChen-Yu Tsai .set_parent = ccu_nkm_set_parent, 244a3658359SChen-Yu Tsai 245a3658359SChen-Yu Tsai .determine_rate = ccu_nkm_determine_rate, 246df6561e6SMaxime Ripard .recalc_rate = ccu_nkm_recalc_rate, 247df6561e6SMaxime Ripard .set_rate = ccu_nkm_set_rate, 248df6561e6SMaxime Ripard }; 249551b62b1SSamuel Holland EXPORT_SYMBOL_NS_GPL(ccu_nkm_ops, SUNXI_CCU); 250