2 * Copyright (C) 2016 Maxime Ripard
3 * Maxime Ripard <maxime.ripard@free-electrons.com>
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>
18 unsigned long n, min_n, max_n;
19 unsigned long m, min_m, max_m;
22 static unsigned long ccu_nm_calc_rate(unsigned long parent,
23 unsigned long n, unsigned long m)
33 static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
36 unsigned long best_rate = 0;
37 unsigned long best_n = 0, best_m = 0;
40 for (_n = nm->min_n; _n <= nm->max_n; _n++) {
41 for (_m = nm->min_m; _m <= nm->max_m; _m++) {
42 unsigned long tmp_rate = ccu_nm_calc_rate(parent,
48 if ((rate - tmp_rate) < (rate - best_rate)) {
60 static void ccu_nm_disable(struct clk_hw *hw)
62 struct ccu_nm *nm = hw_to_ccu_nm(hw);
64 return ccu_gate_helper_disable(&nm->common, nm->enable);
67 static int ccu_nm_enable(struct clk_hw *hw)
69 struct ccu_nm *nm = hw_to_ccu_nm(hw);
71 return ccu_gate_helper_enable(&nm->common, nm->enable);
74 static int ccu_nm_is_enabled(struct clk_hw *hw)
76 struct ccu_nm *nm = hw_to_ccu_nm(hw);
78 return ccu_gate_helper_is_enabled(&nm->common, nm->enable);
81 static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
82 unsigned long parent_rate)
84 struct ccu_nm *nm = hw_to_ccu_nm(hw);
89 if (ccu_frac_helper_is_enabled(&nm->common, &nm->frac)) {
90 rate = ccu_frac_helper_read_rate(&nm->common, &nm->frac);
92 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
93 rate /= nm->fixed_post_div;
98 reg = readl(nm->common.base + nm->common.reg);
100 n = reg >> nm->n.shift;
101 n &= (1 << nm->n.width) - 1;
106 m = reg >> nm->m.shift;
107 m &= (1 << nm->m.width) - 1;
112 if (ccu_sdm_helper_is_enabled(&nm->common, &nm->sdm))
113 rate = ccu_sdm_helper_read_rate(&nm->common, &nm->sdm, m, n);
115 rate = ccu_nm_calc_rate(parent_rate, n, m);
117 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
118 rate /= nm->fixed_post_div;
123 static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
124 unsigned long *parent_rate)
126 struct ccu_nm *nm = hw_to_ccu_nm(hw);
129 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
130 rate *= nm->fixed_post_div;
132 if (rate < nm->min_rate) {
134 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
135 rate /= nm->fixed_post_div;
139 if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
140 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
141 rate /= nm->fixed_post_div;
145 if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
146 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
147 rate /= nm->fixed_post_div;
151 _nm.min_n = nm->n.min ?: 1;
152 _nm.max_n = nm->n.max ?: 1 << nm->n.width;
154 _nm.max_m = nm->m.max ?: 1 << nm->m.width;
156 ccu_nm_find_best(*parent_rate, rate, &_nm);
157 rate = ccu_nm_calc_rate(*parent_rate, _nm.n, _nm.m);
159 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
160 rate /= nm->fixed_post_div;
165 static int ccu_nm_set_rate(struct clk_hw *hw, unsigned long rate,
166 unsigned long parent_rate)
168 struct ccu_nm *nm = hw_to_ccu_nm(hw);
173 /* Adjust target rate according to post-dividers */
174 if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
175 rate = rate * nm->fixed_post_div;
177 if (ccu_frac_helper_has_rate(&nm->common, &nm->frac, rate)) {
178 spin_lock_irqsave(nm->common.lock, flags);
180 /* most SoCs require M to be 0 if fractional mode is used */
181 reg = readl(nm->common.base + nm->common.reg);
182 reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
183 writel(reg, nm->common.base + nm->common.reg);
185 spin_unlock_irqrestore(nm->common.lock, flags);
187 ccu_frac_helper_enable(&nm->common, &nm->frac);
189 return ccu_frac_helper_set_rate(&nm->common, &nm->frac,
192 ccu_frac_helper_disable(&nm->common, &nm->frac);
195 _nm.min_n = nm->n.min ?: 1;
196 _nm.max_n = nm->n.max ?: 1 << nm->n.width;
198 _nm.max_m = nm->m.max ?: 1 << nm->m.width;
200 if (ccu_sdm_helper_has_rate(&nm->common, &nm->sdm, rate)) {
201 ccu_sdm_helper_enable(&nm->common, &nm->sdm, rate);
203 /* Sigma delta modulation requires specific N and M factors */
204 ccu_sdm_helper_get_factors(&nm->common, &nm->sdm, rate,
207 ccu_sdm_helper_disable(&nm->common, &nm->sdm);
208 ccu_nm_find_best(parent_rate, rate, &_nm);
211 spin_lock_irqsave(nm->common.lock, flags);
213 reg = readl(nm->common.base + nm->common.reg);
214 reg &= ~GENMASK(nm->n.width + nm->n.shift - 1, nm->n.shift);
215 reg &= ~GENMASK(nm->m.width + nm->m.shift - 1, nm->m.shift);
217 reg |= (_nm.n - nm->n.offset) << nm->n.shift;
218 reg |= (_nm.m - nm->m.offset) << nm->m.shift;
219 writel(reg, nm->common.base + nm->common.reg);
221 spin_unlock_irqrestore(nm->common.lock, flags);
223 ccu_helper_wait_for_lock(&nm->common, nm->lock);
228 const struct clk_ops ccu_nm_ops = {
229 .disable = ccu_nm_disable,
230 .enable = ccu_nm_enable,
231 .is_enabled = ccu_nm_is_enabled,
233 .recalc_rate = ccu_nm_recalc_rate,
234 .round_rate = ccu_nm_round_rate,
235 .set_rate = ccu_nm_set_rate,