summaryrefslogtreecommitdiffstats
path: root/drivers/clk/kendryte/bypass.c
blob: d1fd28175ba309a5328a543f377b19467237140d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
 */

#define LOG_CATEGORY UCLASS_CLK
#include <kendryte/bypass.h>

#include <clk-uclass.h>
#include <linux/clk-provider.h>
#include <linux/err.h>
#include <log.h>

#define CLK_K210_BYPASS "k210_clk_bypass"

/*
 * This is a small driver to do a software bypass of a clock if hardware bypass
 * is not working. I have tried to write this in a generic fashion, so that it
 * could be potentially broken out of the kendryte code at some future date.
 *
 * Say you have the following clock configuration
 *
 * +---+ +---+
 * |osc| |pll|
 * +---+ +---+
 *         ^
 *        /|
 *       / |
 *      /  |
 *     /   |
 *    /    |
 * +---+ +---+
 * |clk| |clk|
 * +---+ +---+
 *
 * But the pll does not have a bypass, so when you configure the pll, the
 * configuration needs to change to look like
 *
 * +---+ +---+
 * |osc| |pll|
 * +---+ +---+
 *   ^
 *   |\
 *   | \
 *   |  \
 *   |   \
 *   |    \
 * +---+ +---+
 * |clk| |clk|
 * +---+ +---+
 *
 * To set this up, create a bypass clock with bypassee=pll and alt=osc. When
 * creating the child clocks, set their parent to the bypass clock. After
 * creating all the children, call k210_bypass_setchildren().
 */

static int k210_bypass_dobypass(struct k210_bypass *bypass)
{
	int ret, i;

	/*
	 * If we already have saved parents, then the children are already
	 * bypassed
	 */
	if (bypass->child_count && bypass->saved_parents[0])
		return 0;

	for (i = 0; i < bypass->child_count; i++) {
		struct clk *child = bypass->children[i];
		struct clk *parent = clk_get_parent(child);

		if (IS_ERR(parent)) {
			for (; i; i--)
				bypass->saved_parents[i] = NULL;
			return PTR_ERR(parent);
		}
		bypass->saved_parents[i] = parent;
	}

	for (i = 0; i < bypass->child_count; i++) {
		struct clk *child = bypass->children[i];

		ret = clk_set_parent(child, bypass->alt);
		if (ret) {
			for (; i; i--)
				clk_set_parent(bypass->children[i],
					       bypass->saved_parents[i]);
			for (i = 0; i < bypass->child_count; i++)
				bypass->saved_parents[i] = NULL;
			return ret;
		}
	}

	return 0;
}

static int k210_bypass_unbypass(struct k210_bypass *bypass)
{
	int err, ret, i;

	if (!bypass->child_count && !bypass->saved_parents[0]) {
		log_warning("Cannot unbypass children; dobypass not called first\n");
		return 0;
	}

	ret = 0;
	for (i = 0; i < bypass->child_count; i++) {
		err = clk_set_parent(bypass->children[i],
				     bypass->saved_parents[i]);
		if (err)
			ret = err;
		bypass->saved_parents[i] = NULL;
	}
	return ret;
}

static ulong k210_bypass_get_rate(struct clk *clk)
{
	struct k210_bypass *bypass = to_k210_bypass(clk);
	const struct clk_ops *ops = bypass->bypassee_ops;

	if (ops->get_rate)
		return ops->get_rate(bypass->bypassee);
	else
		return clk_get_parent_rate(bypass->bypassee);
}

static ulong k210_bypass_set_rate(struct clk *clk, unsigned long rate)
{
	int ret;
	struct k210_bypass *bypass = to_k210_bypass(clk);
	const struct clk_ops *ops = bypass->bypassee_ops;

	/* Don't bother bypassing if we aren't going to set the rate */
	if (!ops->set_rate)
		return k210_bypass_get_rate(clk);

	ret = k210_bypass_dobypass(bypass);
	if (ret)
		return ret;

	ret = ops->set_rate(bypass->bypassee, rate);
	if (ret < 0)
		return ret;

	return k210_bypass_unbypass(bypass);
}

static int k210_bypass_set_parent(struct clk *clk, struct clk *parent)
{
	struct k210_bypass *bypass = to_k210_bypass(clk);
	const struct clk_ops *ops = bypass->bypassee_ops;

	if (ops->set_parent)
		return ops->set_parent(bypass->bypassee, parent);
	else
		return -ENOTSUPP;
}

/*
 * For these next two functions, do the bypassing even if there is no
 * en-/-disable function, since the bypassing itself can be observed in between
 * calls.
 */
static int k210_bypass_enable(struct clk *clk)
{
	int ret;
	struct k210_bypass *bypass = to_k210_bypass(clk);
	const struct clk_ops *ops = bypass->bypassee_ops;

	ret = k210_bypass_dobypass(bypass);
	if (ret)
		return ret;

	if (ops->enable)
		ret = ops->enable(bypass->bypassee);
	else
		ret = 0;
	if (ret)
		return ret;

	return k210_bypass_unbypass(bypass);
}

static int k210_bypass_disable(struct clk *clk)
{
	int ret;
	struct k210_bypass *bypass = to_k210_bypass(clk);
	const struct clk_ops *ops = bypass->bypassee_ops;

	ret = k210_bypass_dobypass(bypass);
	if (ret)
		return ret;

	if (ops->disable)
		return ops->disable(bypass->bypassee);
	else
		return 0;
}

static const struct clk_ops k210_bypass_ops = {
	.get_rate = k210_bypass_get_rate,
	.set_rate = k210_bypass_set_rate,
	.set_parent = k210_bypass_set_parent,
	.enable = k210_bypass_enable,
	.disable = k210_bypass_disable,
};

int k210_bypass_set_children(struct clk *clk, struct clk **children,
			     size_t child_count)
{
	struct k210_bypass *bypass = to_k210_bypass(clk);

	kfree(bypass->saved_parents);
	if (child_count) {
		bypass->saved_parents =
			kcalloc(child_count, sizeof(struct clk *), GFP_KERNEL);
		if (!bypass->saved_parents)
			return -ENOMEM;
	}
	bypass->child_count = child_count;
	bypass->children = children;

	return 0;
}

struct clk *k210_register_bypass_struct(const char *name,
					const char *parent_name,
					struct k210_bypass *bypass)
{
	int ret;
	struct clk *clk;

	clk = &bypass->clk;

	ret = clk_register(clk, CLK_K210_BYPASS, name, parent_name);
	if (ret)
		return ERR_PTR(ret);

	bypass->bypassee->dev = clk->dev;
	return clk;
}

struct clk *k210_register_bypass(const char *name, const char *parent_name,
				 struct clk *bypassee,
				 const struct clk_ops *bypassee_ops,
				 struct clk *alt)
{
	struct clk *clk;
	struct k210_bypass *bypass;

	bypass = kzalloc(sizeof(*bypass), GFP_KERNEL);
	if (!bypass)
		return ERR_PTR(-ENOMEM);

	bypass->bypassee = bypassee;
	bypass->bypassee_ops = bypassee_ops;
	bypass->alt = alt;

	clk = k210_register_bypass_struct(name, parent_name, bypass);
	if (IS_ERR(clk))
		kfree(bypass);
	return clk;
}

U_BOOT_DRIVER(k210_bypass) = {
	.name	= CLK_K210_BYPASS,
	.id	= UCLASS_CLK,
	.ops	= &k210_bypass_ops,
};