2 * Copyright (C) 2016 Maxime Ripard
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.
11 #include <linux/clk-provider.h>
17 static void ccu_mp_find_best(unsigned long parent, unsigned long rate,
18 unsigned int max_m, unsigned int max_p,
19 unsigned int *m, unsigned int *p)
21 unsigned long best_rate = 0;
22 unsigned int best_m = 0, best_p = 0;
25 for (_p = 1; _p <= max_p; _p <<= 1) {
26 for (_m = 1; _m <= max_m; _m++) {
27 unsigned long tmp_rate = parent / _p / _m;
32 if ((rate - tmp_rate) < (rate - best_rate)) {
44 static unsigned long ccu_mp_find_best_with_parent_adj(struct clk_hw *hw,
45 unsigned long *parent,
50 unsigned long parent_rate_saved;
51 unsigned long parent_rate, now;
52 unsigned long best_rate = 0;
53 unsigned int _m, _p, div;
56 parent_rate_saved = *parent;
59 * The maximum divider we can use without overflowing
60 * unsigned long in rate * m * p below
62 maxdiv = max_m * max_p;
63 maxdiv = min(ULONG_MAX / rate, maxdiv);
65 for (_p = 1; _p <= max_p; _p <<= 1) {
66 for (_m = 1; _m <= max_m; _m++) {
72 if (rate * div == parent_rate_saved) {
74 * It's the most ideal case if the requested
75 * rate can be divided from parent clock without
76 * needing to change parent rate, so return the
77 * divider immediately.
79 *parent = parent_rate_saved;
83 parent_rate = clk_hw_round_rate(hw, rate * div);
84 now = parent_rate / div;
86 if (now <= rate && now > best_rate) {
88 *parent = parent_rate;
99 static unsigned long ccu_mp_round_rate(struct ccu_mux_internal *mux,
101 unsigned long *parent_rate,
105 struct ccu_mp *cmp = data;
106 unsigned int max_m, max_p;
109 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
110 rate *= cmp->fixed_post_div;
112 max_m = cmp->m.max ?: 1 << cmp->m.width;
113 max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
115 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
116 ccu_mp_find_best(*parent_rate, rate, max_m, max_p, &m, &p);
117 rate = *parent_rate / p / m;
119 rate = ccu_mp_find_best_with_parent_adj(hw, parent_rate, rate,
123 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
124 rate /= cmp->fixed_post_div;
129 static void ccu_mp_disable(struct clk_hw *hw)
131 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
133 return ccu_gate_helper_disable(&cmp->common, cmp->enable);
136 static int ccu_mp_enable(struct clk_hw *hw)
138 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
140 return ccu_gate_helper_enable(&cmp->common, cmp->enable);
143 static int ccu_mp_is_enabled(struct clk_hw *hw)
145 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
147 return ccu_gate_helper_is_enabled(&cmp->common, cmp->enable);
150 static unsigned long ccu_mp_recalc_rate(struct clk_hw *hw,
151 unsigned long parent_rate)
153 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
158 /* Adjust parent_rate according to pre-dividers */
159 parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
162 reg = readl(cmp->common.base + cmp->common.reg);
164 m = reg >> cmp->m.shift;
165 m &= (1 << cmp->m.width) - 1;
170 p = reg >> cmp->p.shift;
171 p &= (1 << cmp->p.width) - 1;
173 rate = (parent_rate >> p) / m;
174 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
175 rate /= cmp->fixed_post_div;
180 static int ccu_mp_determine_rate(struct clk_hw *hw,
181 struct clk_rate_request *req)
183 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
185 return ccu_mux_helper_determine_rate(&cmp->common, &cmp->mux,
186 req, ccu_mp_round_rate, cmp);
189 static int ccu_mp_set_rate(struct clk_hw *hw, unsigned long rate,
190 unsigned long parent_rate)
192 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
194 unsigned int max_m, max_p;
198 /* Adjust parent_rate according to pre-dividers */
199 parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
202 max_m = cmp->m.max ?: 1 << cmp->m.width;
203 max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
205 /* Adjust target rate according to post-dividers */
206 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
207 rate = rate * cmp->fixed_post_div;
209 ccu_mp_find_best(parent_rate, rate, max_m, max_p, &m, &p);
211 spin_lock_irqsave(cmp->common.lock, flags);
213 reg = readl(cmp->common.base + cmp->common.reg);
214 reg &= ~GENMASK(cmp->m.width + cmp->m.shift - 1, cmp->m.shift);
215 reg &= ~GENMASK(cmp->p.width + cmp->p.shift - 1, cmp->p.shift);
216 reg |= (m - cmp->m.offset) << cmp->m.shift;
217 reg |= ilog2(p) << cmp->p.shift;
219 writel(reg, cmp->common.base + cmp->common.reg);
221 spin_unlock_irqrestore(cmp->common.lock, flags);
226 static u8 ccu_mp_get_parent(struct clk_hw *hw)
228 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
230 return ccu_mux_helper_get_parent(&cmp->common, &cmp->mux);
233 static int ccu_mp_set_parent(struct clk_hw *hw, u8 index)
235 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
237 return ccu_mux_helper_set_parent(&cmp->common, &cmp->mux, index);
240 const struct clk_ops ccu_mp_ops = {
241 .disable = ccu_mp_disable,
242 .enable = ccu_mp_enable,
243 .is_enabled = ccu_mp_is_enabled,
245 .get_parent = ccu_mp_get_parent,
246 .set_parent = ccu_mp_set_parent,
248 .determine_rate = ccu_mp_determine_rate,
249 .recalc_rate = ccu_mp_recalc_rate,
250 .set_rate = ccu_mp_set_rate,
254 * Support for MMC timing mode switching
256 * The MMC clocks on some SoCs support switching between old and
257 * new timing modes. A platform specific API is provided to query
258 * and set the timing mode on supported SoCs.
260 * In addition, a special class of ccu_mp_ops is provided, which
261 * takes in to account the timing mode switch. When the new timing
262 * mode is active, the clock output rate is halved. This new class
263 * is a wrapper around the generic ccu_mp_ops. When clock rates
264 * are passed through to ccu_mp_ops callbacks, they are doubled
265 * if the new timing mode bit is set, to account for the post
266 * divider. Conversely, when clock rates are passed back, they
267 * are halved if the mode bit is set.
270 static unsigned long ccu_mp_mmc_recalc_rate(struct clk_hw *hw,
271 unsigned long parent_rate)
273 unsigned long rate = ccu_mp_recalc_rate(hw, parent_rate);
274 struct ccu_common *cm = hw_to_ccu_common(hw);
275 u32 val = readl(cm->base + cm->reg);
277 if (val & CCU_MMC_NEW_TIMING_MODE)
282 static int ccu_mp_mmc_determine_rate(struct clk_hw *hw,
283 struct clk_rate_request *req)
285 struct ccu_common *cm = hw_to_ccu_common(hw);
286 u32 val = readl(cm->base + cm->reg);
289 /* adjust the requested clock rate */
290 if (val & CCU_MMC_NEW_TIMING_MODE) {
296 ret = ccu_mp_determine_rate(hw, req);
298 /* re-adjust the requested clock rate back */
299 if (val & CCU_MMC_NEW_TIMING_MODE) {
308 static int ccu_mp_mmc_set_rate(struct clk_hw *hw, unsigned long rate,
309 unsigned long parent_rate)
311 struct ccu_common *cm = hw_to_ccu_common(hw);
312 u32 val = readl(cm->base + cm->reg);
314 if (val & CCU_MMC_NEW_TIMING_MODE)
317 return ccu_mp_set_rate(hw, rate, parent_rate);
320 const struct clk_ops ccu_mp_mmc_ops = {
321 .disable = ccu_mp_disable,
322 .enable = ccu_mp_enable,
323 .is_enabled = ccu_mp_is_enabled,
325 .get_parent = ccu_mp_get_parent,
326 .set_parent = ccu_mp_set_parent,
328 .determine_rate = ccu_mp_mmc_determine_rate,
329 .recalc_rate = ccu_mp_mmc_recalc_rate,
330 .set_rate = ccu_mp_mmc_set_rate,