1 /* 2 * Copyright (C) 2016 Maxime Ripard 3 * Maxime Ripard <maxime.ripard@free-electrons.com> 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation; either version 2 of 8 * the License, or (at your option) any later version. 9 */ 10 11 #include <linux/clk-provider.h> 12 #include <linux/rational.h> 13 14 #include "ccu_gate.h" 15 #include "ccu_nkm.h" 16 17 struct _ccu_nkm { 18 unsigned long n, max_n; 19 unsigned long k, max_k; 20 unsigned long m, max_m; 21 }; 22 23 static void ccu_nkm_find_best(unsigned long parent, unsigned long rate, 24 struct _ccu_nkm *nkm) 25 { 26 unsigned long best_rate = 0; 27 unsigned long best_n = 0, best_k = 0, best_m = 0; 28 unsigned long _n, _k, _m; 29 30 for (_k = 1; _k <= nkm->max_k; _k++) { 31 unsigned long tmp_rate; 32 33 rational_best_approximation(rate / _k, parent, 34 nkm->max_n, nkm->max_m, &_n, &_m); 35 36 tmp_rate = parent * _n * _k / _m; 37 38 if (tmp_rate > rate) 39 continue; 40 41 if ((rate - tmp_rate) < (rate - best_rate)) { 42 best_rate = tmp_rate; 43 best_n = _n; 44 best_k = _k; 45 best_m = _m; 46 } 47 } 48 49 nkm->n = best_n; 50 nkm->k = best_k; 51 nkm->m = best_m; 52 } 53 54 static void ccu_nkm_disable(struct clk_hw *hw) 55 { 56 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 57 58 return ccu_gate_helper_disable(&nkm->common, nkm->enable); 59 } 60 61 static int ccu_nkm_enable(struct clk_hw *hw) 62 { 63 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 64 65 return ccu_gate_helper_enable(&nkm->common, nkm->enable); 66 } 67 68 static int ccu_nkm_is_enabled(struct clk_hw *hw) 69 { 70 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 71 72 return ccu_gate_helper_is_enabled(&nkm->common, nkm->enable); 73 } 74 75 static unsigned long ccu_nkm_recalc_rate(struct clk_hw *hw, 76 unsigned long parent_rate) 77 { 78 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 79 unsigned long n, m, k; 80 u32 reg; 81 82 reg = readl(nkm->common.base + nkm->common.reg); 83 84 n = reg >> nkm->n.shift; 85 n &= (1 << nkm->n.width) - 1; 86 87 k = reg >> nkm->k.shift; 88 k &= (1 << nkm->k.width) - 1; 89 90 m = reg >> nkm->m.shift; 91 m &= (1 << nkm->m.width) - 1; 92 93 return parent_rate * (n + 1) * (k + 1) / (m + 1); 94 } 95 96 static long ccu_nkm_round_rate(struct clk_hw *hw, unsigned long rate, 97 unsigned long *parent_rate) 98 { 99 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 100 struct _ccu_nkm _nkm; 101 102 _nkm.max_n = 1 << nkm->n.width; 103 _nkm.max_k = 1 << nkm->k.width; 104 _nkm.max_m = 1 << nkm->m.width; 105 106 ccu_nkm_find_best(*parent_rate, rate, &_nkm); 107 108 return *parent_rate * _nkm.n * _nkm.k / _nkm.m; 109 } 110 111 static int ccu_nkm_set_rate(struct clk_hw *hw, unsigned long rate, 112 unsigned long parent_rate) 113 { 114 struct ccu_nkm *nkm = hw_to_ccu_nkm(hw); 115 struct _ccu_nkm _nkm; 116 unsigned long flags; 117 u32 reg; 118 119 _nkm.max_n = 1 << nkm->n.width; 120 _nkm.max_k = 1 << nkm->k.width; 121 _nkm.max_m = 1 << nkm->m.width; 122 123 ccu_nkm_find_best(parent_rate, rate, &_nkm); 124 125 spin_lock_irqsave(nkm->common.lock, flags); 126 127 reg = readl(nkm->common.base + nkm->common.reg); 128 reg &= ~GENMASK(nkm->n.width + nkm->n.shift - 1, nkm->n.shift); 129 reg &= ~GENMASK(nkm->k.width + nkm->k.shift - 1, nkm->k.shift); 130 reg &= ~GENMASK(nkm->m.width + nkm->m.shift - 1, nkm->m.shift); 131 132 reg |= (_nkm.n - 1) << nkm->n.shift; 133 reg |= (_nkm.k - 1) << nkm->k.shift; 134 reg |= (_nkm.m - 1) << nkm->m.shift; 135 136 writel(reg, nkm->common.base + nkm->common.reg); 137 138 spin_unlock_irqrestore(nkm->common.lock, flags); 139 140 ccu_helper_wait_for_lock(&nkm->common, nkm->lock); 141 142 return 0; 143 } 144 145 const struct clk_ops ccu_nkm_ops = { 146 .disable = ccu_nkm_disable, 147 .enable = ccu_nkm_enable, 148 .is_enabled = ccu_nkm_is_enabled, 149 150 .recalc_rate = ccu_nkm_recalc_rate, 151 .round_rate = ccu_nkm_round_rate, 152 .set_rate = ccu_nkm_set_rate, 153 }; 154