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 13 #include "ccu_gate.h" 14 #include "ccu_mult.h" 15 16 struct _ccu_mult { 17 unsigned long mult, min, max; 18 }; 19 20 static void ccu_mult_find_best(unsigned long parent, unsigned long rate, 21 struct _ccu_mult *mult) 22 { 23 int _mult; 24 25 _mult = rate / parent; 26 if (_mult < mult->min) 27 _mult = mult->min; 28 29 if (_mult > mult->max) 30 _mult = mult->max; 31 32 mult->mult = _mult; 33 } 34 35 static unsigned long ccu_mult_round_rate(struct ccu_mux_internal *mux, 36 unsigned long parent_rate, 37 unsigned long rate, 38 void *data) 39 { 40 struct ccu_mult *cm = data; 41 struct _ccu_mult _cm; 42 43 _cm.min = cm->mult.min; 44 45 if (cm->mult.max) 46 _cm.max = cm->mult.max; 47 else 48 _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1; 49 50 ccu_mult_find_best(parent_rate, rate, &_cm); 51 52 return parent_rate * _cm.mult; 53 } 54 55 static void ccu_mult_disable(struct clk_hw *hw) 56 { 57 struct ccu_mult *cm = hw_to_ccu_mult(hw); 58 59 return ccu_gate_helper_disable(&cm->common, cm->enable); 60 } 61 62 static int ccu_mult_enable(struct clk_hw *hw) 63 { 64 struct ccu_mult *cm = hw_to_ccu_mult(hw); 65 66 return ccu_gate_helper_enable(&cm->common, cm->enable); 67 } 68 69 static int ccu_mult_is_enabled(struct clk_hw *hw) 70 { 71 struct ccu_mult *cm = hw_to_ccu_mult(hw); 72 73 return ccu_gate_helper_is_enabled(&cm->common, cm->enable); 74 } 75 76 static unsigned long ccu_mult_recalc_rate(struct clk_hw *hw, 77 unsigned long parent_rate) 78 { 79 struct ccu_mult *cm = hw_to_ccu_mult(hw); 80 unsigned long val; 81 u32 reg; 82 83 if (ccu_frac_helper_is_enabled(&cm->common, &cm->frac)) 84 return ccu_frac_helper_read_rate(&cm->common, &cm->frac); 85 86 reg = readl(cm->common.base + cm->common.reg); 87 val = reg >> cm->mult.shift; 88 val &= (1 << cm->mult.width) - 1; 89 90 ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1, 91 &parent_rate); 92 93 return parent_rate * (val + cm->mult.offset); 94 } 95 96 static int ccu_mult_determine_rate(struct clk_hw *hw, 97 struct clk_rate_request *req) 98 { 99 struct ccu_mult *cm = hw_to_ccu_mult(hw); 100 101 return ccu_mux_helper_determine_rate(&cm->common, &cm->mux, 102 req, ccu_mult_round_rate, cm); 103 } 104 105 static int ccu_mult_set_rate(struct clk_hw *hw, unsigned long rate, 106 unsigned long parent_rate) 107 { 108 struct ccu_mult *cm = hw_to_ccu_mult(hw); 109 struct _ccu_mult _cm; 110 unsigned long flags; 111 u32 reg; 112 113 if (ccu_frac_helper_has_rate(&cm->common, &cm->frac, rate)) 114 return ccu_frac_helper_set_rate(&cm->common, &cm->frac, rate); 115 else 116 ccu_frac_helper_disable(&cm->common, &cm->frac); 117 118 ccu_mux_helper_adjust_parent_for_prediv(&cm->common, &cm->mux, -1, 119 &parent_rate); 120 121 _cm.min = cm->mult.min; 122 123 if (cm->mult.max) 124 _cm.max = cm->mult.max; 125 else 126 _cm.max = (1 << cm->mult.width) + cm->mult.offset - 1; 127 128 ccu_mult_find_best(parent_rate, rate, &_cm); 129 130 spin_lock_irqsave(cm->common.lock, flags); 131 132 reg = readl(cm->common.base + cm->common.reg); 133 reg &= ~GENMASK(cm->mult.width + cm->mult.shift - 1, cm->mult.shift); 134 reg |= ((_cm.mult - cm->mult.offset) << cm->mult.shift); 135 136 writel(reg, cm->common.base + cm->common.reg); 137 138 spin_unlock_irqrestore(cm->common.lock, flags); 139 140 return 0; 141 } 142 143 static u8 ccu_mult_get_parent(struct clk_hw *hw) 144 { 145 struct ccu_mult *cm = hw_to_ccu_mult(hw); 146 147 return ccu_mux_helper_get_parent(&cm->common, &cm->mux); 148 } 149 150 static int ccu_mult_set_parent(struct clk_hw *hw, u8 index) 151 { 152 struct ccu_mult *cm = hw_to_ccu_mult(hw); 153 154 return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index); 155 } 156 157 const struct clk_ops ccu_mult_ops = { 158 .disable = ccu_mult_disable, 159 .enable = ccu_mult_enable, 160 .is_enabled = ccu_mult_is_enabled, 161 162 .get_parent = ccu_mult_get_parent, 163 .set_parent = ccu_mult_set_parent, 164 165 .determine_rate = ccu_mult_determine_rate, 166 .recalc_rate = ccu_mult_recalc_rate, 167 .set_rate = ccu_mult_set_rate, 168 }; 169