1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
6 #define LOG_CATEGORY UCLASS_CLK
7 #include <kendryte/bypass.h>
9 #include <clk-uclass.h>
10 #include <linux/clk-provider.h>
11 #include <linux/err.h>
14 #define CLK_K210_BYPASS "k210_clk_bypass"
17 * This is a small driver to do a software bypass of a clock if hardware bypass
18 * is not working. I have tried to write this in a generic fashion, so that it
19 * could be potentially broken out of the kendryte code at some future date.
21 * Say you have the following clock configuration
36 * But the pll does not have a bypass, so when you configure the pll, the
37 * configuration needs to change to look like
52 * To set this up, create a bypass clock with bypassee=pll and alt=osc. When
53 * creating the child clocks, set their parent to the bypass clock. After
54 * creating all the children, call k210_bypass_setchildren().
57 static int k210_bypass_dobypass(struct k210_bypass *bypass)
62 * If we already have saved parents, then the children are already
65 if (bypass->child_count && bypass->saved_parents[0])
68 for (i = 0; i < bypass->child_count; i++) {
69 struct clk *child = bypass->children[i];
70 struct clk *parent = clk_get_parent(child);
74 bypass->saved_parents[i] = NULL;
75 return PTR_ERR(parent);
77 bypass->saved_parents[i] = parent;
80 for (i = 0; i < bypass->child_count; i++) {
81 struct clk *child = bypass->children[i];
83 ret = clk_set_parent(child, bypass->alt);
86 clk_set_parent(bypass->children[i],
87 bypass->saved_parents[i]);
88 for (i = 0; i < bypass->child_count; i++)
89 bypass->saved_parents[i] = NULL;
97 static int k210_bypass_unbypass(struct k210_bypass *bypass)
101 if (!bypass->child_count && !bypass->saved_parents[0]) {
102 log_warning("Cannot unbypass children; dobypass not called first\n");
107 for (i = 0; i < bypass->child_count; i++) {
108 err = clk_set_parent(bypass->children[i],
109 bypass->saved_parents[i]);
112 bypass->saved_parents[i] = NULL;
117 static ulong k210_bypass_get_rate(struct clk *clk)
119 struct k210_bypass *bypass = to_k210_bypass(clk);
120 const struct clk_ops *ops = bypass->bypassee_ops;
123 return ops->get_rate(bypass->bypassee);
125 return clk_get_parent_rate(bypass->bypassee);
128 static ulong k210_bypass_set_rate(struct clk *clk, unsigned long rate)
131 struct k210_bypass *bypass = to_k210_bypass(clk);
132 const struct clk_ops *ops = bypass->bypassee_ops;
134 /* Don't bother bypassing if we aren't going to set the rate */
136 return k210_bypass_get_rate(clk);
138 ret = k210_bypass_dobypass(bypass);
142 ret = ops->set_rate(bypass->bypassee, rate);
146 return k210_bypass_unbypass(bypass);
149 static int k210_bypass_set_parent(struct clk *clk, struct clk *parent)
151 struct k210_bypass *bypass = to_k210_bypass(clk);
152 const struct clk_ops *ops = bypass->bypassee_ops;
155 return ops->set_parent(bypass->bypassee, parent);
161 * For these next two functions, do the bypassing even if there is no
162 * en-/-disable function, since the bypassing itself can be observed in between
165 static int k210_bypass_enable(struct clk *clk)
168 struct k210_bypass *bypass = to_k210_bypass(clk);
169 const struct clk_ops *ops = bypass->bypassee_ops;
171 ret = k210_bypass_dobypass(bypass);
176 ret = ops->enable(bypass->bypassee);
182 return k210_bypass_unbypass(bypass);
185 static int k210_bypass_disable(struct clk *clk)
188 struct k210_bypass *bypass = to_k210_bypass(clk);
189 const struct clk_ops *ops = bypass->bypassee_ops;
191 ret = k210_bypass_dobypass(bypass);
196 return ops->disable(bypass->bypassee);
201 static const struct clk_ops k210_bypass_ops = {
202 .get_rate = k210_bypass_get_rate,
203 .set_rate = k210_bypass_set_rate,
204 .set_parent = k210_bypass_set_parent,
205 .enable = k210_bypass_enable,
206 .disable = k210_bypass_disable,
209 int k210_bypass_set_children(struct clk *clk, struct clk **children,
212 struct k210_bypass *bypass = to_k210_bypass(clk);
214 kfree(bypass->saved_parents);
216 bypass->saved_parents =
217 kcalloc(child_count, sizeof(struct clk *), GFP_KERNEL);
218 if (!bypass->saved_parents)
221 bypass->child_count = child_count;
222 bypass->children = children;
227 struct clk *k210_register_bypass_struct(const char *name,
228 const char *parent_name,
229 struct k210_bypass *bypass)
236 ret = clk_register(clk, CLK_K210_BYPASS, name, parent_name);
240 bypass->bypassee->dev = clk->dev;
244 struct clk *k210_register_bypass(const char *name, const char *parent_name,
245 struct clk *bypassee,
246 const struct clk_ops *bypassee_ops,
250 struct k210_bypass *bypass;
252 bypass = kzalloc(sizeof(*bypass), GFP_KERNEL);
254 return ERR_PTR(-ENOMEM);
256 bypass->bypassee = bypassee;
257 bypass->bypassee_ops = bypassee_ops;
260 clk = k210_register_bypass_struct(name, parent_name, bypass);
266 U_BOOT_DRIVER(k210_bypass) = {
267 .name = CLK_K210_BYPASS,
269 .ops = &k210_bypass_ops,