blob: 7d75da9a1f2e547cbd48c1bf432473564244e412 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Maxime Ripard2a65ed42016-06-29 21:05:26 +02002/*
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
Maxime Ripard2a65ed42016-06-29 21:05:26 +02005 */
6
Chen-Yu Tsai8adfb082016-08-25 14:21:58 +08007#include <linux/clk.h>
Maxime Ripard2a65ed42016-06-29 21:05:26 +02008#include <linux/clk-provider.h>
Chen-Yu Tsai8adfb082016-08-25 14:21:58 +08009#include <linux/delay.h>
Stephen Boyd62e59c42019-04-18 15:20:22 -070010#include <linux/io.h>
Maxime Ripard2a65ed42016-06-29 21:05:26 +020011
12#include "ccu_gate.h"
13#include "ccu_mux.h"
14
Maxime Ripardea8edcd2017-05-17 09:40:34 +020015static u16 ccu_mux_get_prediv(struct ccu_common *common,
16 struct ccu_mux_internal *cm,
17 int parent_index)
Maxime Ripard2a65ed42016-06-29 21:05:26 +020018{
Chen-Yu Tsai178ca532016-07-26 15:04:25 +080019 u16 prediv = 1;
Maxime Ripard2a65ed42016-06-29 21:05:26 +020020 u32 reg;
21
22 if (!((common->features & CCU_FEATURE_FIXED_PREDIV) ||
Maxime Ripard7c09b852017-01-19 22:49:26 +010023 (common->features & CCU_FEATURE_VARIABLE_PREDIV) ||
24 (common->features & CCU_FEATURE_ALL_PREDIV)))
Maxime Ripardea8edcd2017-05-17 09:40:34 +020025 return 1;
Maxime Ripard2a65ed42016-06-29 21:05:26 +020026
Maxime Ripardea8edcd2017-05-17 09:40:34 +020027 if (common->features & CCU_FEATURE_ALL_PREDIV)
28 return common->prediv;
Maxime Ripard7c09b852017-01-19 22:49:26 +010029
Maxime Ripard2a65ed42016-06-29 21:05:26 +020030 reg = readl(common->base + common->reg);
31 if (parent_index < 0) {
32 parent_index = reg >> cm->shift;
33 parent_index &= (1 << cm->width) - 1;
34 }
35
Maxime Ripardea8edcd2017-05-17 09:40:34 +020036 if (common->features & CCU_FEATURE_FIXED_PREDIV) {
37 int i;
38
Chen-Yu Tsaiff5294d2016-08-25 14:21:57 +080039 for (i = 0; i < cm->n_predivs; i++)
40 if (parent_index == cm->fixed_predivs[i].index)
41 prediv = cm->fixed_predivs[i].div;
Maxime Ripardea8edcd2017-05-17 09:40:34 +020042 }
Maxime Ripard2a65ed42016-06-29 21:05:26 +020043
Chen-Yu Tsai13e0dde2017-05-19 15:06:08 +080044 if (common->features & CCU_FEATURE_VARIABLE_PREDIV) {
45 int i;
Maxime Ripard2a65ed42016-06-29 21:05:26 +020046
Chen-Yu Tsai13e0dde2017-05-19 15:06:08 +080047 for (i = 0; i < cm->n_var_predivs; i++)
48 if (parent_index == cm->var_predivs[i].index) {
49 u8 div;
50
51 div = reg >> cm->var_predivs[i].shift;
52 div &= (1 << cm->var_predivs[i].width) - 1;
53 prediv = div + 1;
54 }
55 }
Maxime Ripard2a65ed42016-06-29 21:05:26 +020056
Maxime Ripardea8edcd2017-05-17 09:40:34 +020057 return prediv;
58}
59
Maxime Ripardd754b152017-05-17 09:40:35 +020060unsigned long ccu_mux_helper_apply_prediv(struct ccu_common *common,
61 struct ccu_mux_internal *cm,
62 int parent_index,
63 unsigned long parent_rate)
Maxime Ripardea8edcd2017-05-17 09:40:34 +020064{
Maxime Ripardd754b152017-05-17 09:40:35 +020065 return parent_rate / ccu_mux_get_prediv(common, cm, parent_index);
Maxime Ripard2a65ed42016-06-29 21:05:26 +020066}
67
Stephen Boyd4dc6e1a2017-06-16 14:50:41 -070068static unsigned long ccu_mux_helper_unapply_prediv(struct ccu_common *common,
Maxime Ripardabea2422017-05-17 09:40:36 +020069 struct ccu_mux_internal *cm,
70 int parent_index,
71 unsigned long parent_rate)
72{
73 return parent_rate * ccu_mux_get_prediv(common, cm, parent_index);
74}
75
Maxime Ripard2a65ed42016-06-29 21:05:26 +020076int ccu_mux_helper_determine_rate(struct ccu_common *common,
77 struct ccu_mux_internal *cm,
78 struct clk_rate_request *req,
79 unsigned long (*round)(struct ccu_mux_internal *,
Maxime Ripard10a8d9b2017-05-17 09:40:31 +020080 struct clk_hw *,
81 unsigned long *,
Maxime Ripard2a65ed42016-06-29 21:05:26 +020082 unsigned long,
83 void *),
84 void *data)
85{
86 unsigned long best_parent_rate = 0, best_rate = 0;
87 struct clk_hw *best_parent, *hw = &common->hw;
88 unsigned int i;
89
Chen-Yu Tsai216abdc2017-01-28 20:22:31 +080090 if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) {
91 unsigned long adj_parent_rate;
92
93 best_parent = clk_hw_get_parent(hw);
94 best_parent_rate = clk_hw_get_rate(best_parent);
Maxime Ripardd754b152017-05-17 09:40:35 +020095 adj_parent_rate = ccu_mux_helper_apply_prediv(common, cm, -1,
96 best_parent_rate);
Chen-Yu Tsai216abdc2017-01-28 20:22:31 +080097
Maxime Ripard10a8d9b2017-05-17 09:40:31 +020098 best_rate = round(cm, best_parent, &adj_parent_rate,
99 req->rate, data);
Chen-Yu Tsai216abdc2017-01-28 20:22:31 +0800100
Maxime Ripardabea2422017-05-17 09:40:36 +0200101 /*
102 * adj_parent_rate might have been modified by our clock.
103 * Unapply the pre-divider if there's one, and give
104 * the actual frequency the parent needs to run at.
105 */
106 best_parent_rate = ccu_mux_helper_unapply_prediv(common, cm, -1,
107 adj_parent_rate);
108
Chen-Yu Tsai216abdc2017-01-28 20:22:31 +0800109 goto out;
110 }
111
Maxime Ripard2a65ed42016-06-29 21:05:26 +0200112 for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
Maxime Ripardabea2422017-05-17 09:40:36 +0200113 unsigned long tmp_rate, parent_rate;
Maxime Ripard2a65ed42016-06-29 21:05:26 +0200114 struct clk_hw *parent;
115
116 parent = clk_hw_get_parent_by_index(hw, i);
117 if (!parent)
118 continue;
119
Maxime Ripardabea2422017-05-17 09:40:36 +0200120 parent_rate = ccu_mux_helper_apply_prediv(common, cm, i,
121 clk_hw_get_rate(parent));
Maxime Ripard2a65ed42016-06-29 21:05:26 +0200122
Maxime Ripardabea2422017-05-17 09:40:36 +0200123 tmp_rate = round(cm, parent, &parent_rate, req->rate, data);
124
125 /*
126 * parent_rate might have been modified by our clock.
127 * Unapply the pre-divider if there's one, and give
128 * the actual frequency the parent needs to run at.
129 */
130 parent_rate = ccu_mux_helper_unapply_prediv(common, cm, i,
131 parent_rate);
Maxime Ripard2a65ed42016-06-29 21:05:26 +0200132 if (tmp_rate == req->rate) {
133 best_parent = parent;
134 best_parent_rate = parent_rate;
135 best_rate = tmp_rate;
136 goto out;
137 }
138
139 if ((req->rate - tmp_rate) < (req->rate - best_rate)) {
140 best_rate = tmp_rate;
141 best_parent_rate = parent_rate;
142 best_parent = parent;
143 }
144 }
145
146 if (best_rate == 0)
147 return -EINVAL;
148
149out:
150 req->best_parent_hw = best_parent;
151 req->best_parent_rate = best_parent_rate;
152 req->rate = best_rate;
153 return 0;
154}
155
156u8 ccu_mux_helper_get_parent(struct ccu_common *common,
157 struct ccu_mux_internal *cm)
158{
159 u32 reg;
160 u8 parent;
161
162 reg = readl(common->base + common->reg);
163 parent = reg >> cm->shift;
164 parent &= (1 << cm->width) - 1;
165
Chen-Yu Tsai2b9c8752016-08-25 14:21:56 +0800166 if (cm->table) {
167 int num_parents = clk_hw_get_num_parents(&common->hw);
168 int i;
169
170 for (i = 0; i < num_parents; i++)
171 if (cm->table[i] == parent)
172 return i;
173 }
174
Maxime Ripard2a65ed42016-06-29 21:05:26 +0200175 return parent;
176}
177
178int ccu_mux_helper_set_parent(struct ccu_common *common,
179 struct ccu_mux_internal *cm,
180 u8 index)
181{
182 unsigned long flags;
183 u32 reg;
184
Chen-Yu Tsai2b9c8752016-08-25 14:21:56 +0800185 if (cm->table)
186 index = cm->table[index];
187
Maxime Ripard2a65ed42016-06-29 21:05:26 +0200188 spin_lock_irqsave(common->lock, flags);
189
190 reg = readl(common->base + common->reg);
191 reg &= ~GENMASK(cm->width + cm->shift - 1, cm->shift);
192 writel(reg | (index << cm->shift), common->base + common->reg);
193
194 spin_unlock_irqrestore(common->lock, flags);
195
196 return 0;
197}
198
199static void ccu_mux_disable(struct clk_hw *hw)
200{
201 struct ccu_mux *cm = hw_to_ccu_mux(hw);
202
203 return ccu_gate_helper_disable(&cm->common, cm->enable);
204}
205
206static int ccu_mux_enable(struct clk_hw *hw)
207{
208 struct ccu_mux *cm = hw_to_ccu_mux(hw);
209
210 return ccu_gate_helper_enable(&cm->common, cm->enable);
211}
212
213static int ccu_mux_is_enabled(struct clk_hw *hw)
214{
215 struct ccu_mux *cm = hw_to_ccu_mux(hw);
216
217 return ccu_gate_helper_is_enabled(&cm->common, cm->enable);
218}
219
220static u8 ccu_mux_get_parent(struct clk_hw *hw)
221{
222 struct ccu_mux *cm = hw_to_ccu_mux(hw);
223
224 return ccu_mux_helper_get_parent(&cm->common, &cm->mux);
225}
226
227static int ccu_mux_set_parent(struct clk_hw *hw, u8 index)
228{
229 struct ccu_mux *cm = hw_to_ccu_mux(hw);
230
231 return ccu_mux_helper_set_parent(&cm->common, &cm->mux, index);
232}
233
234static unsigned long ccu_mux_recalc_rate(struct clk_hw *hw,
235 unsigned long parent_rate)
236{
237 struct ccu_mux *cm = hw_to_ccu_mux(hw);
238
Maxime Ripardd754b152017-05-17 09:40:35 +0200239 return ccu_mux_helper_apply_prediv(&cm->common, &cm->mux, -1,
240 parent_rate);
Maxime Ripard2a65ed42016-06-29 21:05:26 +0200241}
242
243const struct clk_ops ccu_mux_ops = {
244 .disable = ccu_mux_disable,
245 .enable = ccu_mux_enable,
246 .is_enabled = ccu_mux_is_enabled,
247
248 .get_parent = ccu_mux_get_parent,
249 .set_parent = ccu_mux_set_parent,
250
251 .determine_rate = __clk_mux_determine_rate,
252 .recalc_rate = ccu_mux_recalc_rate,
253};
Chen-Yu Tsai8adfb082016-08-25 14:21:58 +0800254
255/*
256 * This clock notifier is called when the frequency of the of the parent
257 * PLL clock is to be changed. The idea is to switch the parent to a
258 * stable clock, such as the main oscillator, while the PLL frequency
259 * stabilizes.
260 */
261static int ccu_mux_notifier_cb(struct notifier_block *nb,
262 unsigned long event, void *data)
263{
264 struct ccu_mux_nb *mux = to_ccu_mux_nb(nb);
265 int ret = 0;
266
267 if (event == PRE_RATE_CHANGE) {
268 mux->original_index = ccu_mux_helper_get_parent(mux->common,
269 mux->cm);
270 ret = ccu_mux_helper_set_parent(mux->common, mux->cm,
271 mux->bypass_index);
272 } else if (event == POST_RATE_CHANGE) {
273 ret = ccu_mux_helper_set_parent(mux->common, mux->cm,
274 mux->original_index);
275 }
276
277 udelay(mux->delay_us);
278
279 return notifier_from_errno(ret);
280}
281
282int ccu_mux_notifier_register(struct clk *clk, struct ccu_mux_nb *mux_nb)
283{
284 mux_nb->clk_nb.notifier_call = ccu_mux_notifier_cb;
285
286 return clk_notifier_register(clk, &mux_nb->clk_nb);
287}