summaryrefslogtreecommitdiff
path: root/drivers/clk/spacemit/clk_mix.c
blob: a1158512a92bcfe490f6eb3da9331b786399d284 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2024 SpacemiT Technology Co. Ltd
 * Copyright (c) 2024-2025 Haylen Chu <[email protected]>
 * Copyright (c) 2025 Junhui Liu <[email protected]>
 *  Authors: Haylen Chu <[email protected]>
 *
 * MIX clock type is the combination of mux, factor or divider, and gate
 */

#include <dm/device.h>
#include <dm/uclass.h>
#include <div64.h>
#include <regmap.h>
#include <linux/clk-provider.h>
#include <linux/kernel.h>

#include "clk_mix.h"

#define UBOOT_DM_SPACEMIT_CLK_GATE		"spacemit_clk_gate"
#define UBOOT_DM_SPACEMIT_CLK_FACTOR		"spacemit_clk_factor"
#define UBOOT_DM_SPACEMIT_CLK_MUX		"spacemit_clk_mux"
#define UBOOT_DM_SPACEMIT_CLK_DIV		"spacemit_clk_div"
#define UBOOT_DM_SPACEMIT_CLK_FACTOR_GATE	"spacemit_clk_factor_gate"
#define UBOOT_DM_SPACEMIT_CLK_MUX_GATE		"spacemit_clk_mux_gate"
#define UBOOT_DM_SPACEMIT_CLK_DIV_GATE		"spacemit_clk_div_gate"
#define UBOOT_DM_SPACEMIT_CLK_MUX_DIV		"spacemit_clk_mux_div"
#define UBOOT_DM_SPACEMIT_CLK_MUX_DIV_GATE	"spacemit_clk_mux_div_gate"

#define MIX_FC_TIMEOUT_US	10000
#define MIX_FC_DELAY_US		5

int ccu_gate_disable(struct clk *clk)
{
	struct ccu_mix *mix = clk_to_ccu_mix(clk);

	ccu_update(&mix->common, ctrl, mix->gate.mask, 0);

	return 0;
}

int ccu_gate_enable(struct clk *clk)
{
	struct ccu_mix *mix = clk_to_ccu_mix(clk);
	struct ccu_gate_config *gate = &mix->gate;

	ccu_update(&mix->common, ctrl, gate->mask, gate->mask);

	return 0;
}

static unsigned long ccu_factor_recalc_rate(struct clk *clk)
{
	struct ccu_mix *mix = clk_to_ccu_mix(clk);

	return clk_get_parent_rate(clk) * mix->factor.mul / mix->factor.div;
}

static unsigned long ccu_div_recalc_rate(struct clk *clk)
{
	struct ccu_mix *mix = clk_to_ccu_mix(clk);
	struct ccu_div_config *div = &mix->div;
	unsigned long val;

	val = ccu_read(&mix->common, ctrl) >> div->shift;
	val &= (1 << div->width) - 1;

	return divider_recalc_rate(clk, clk_get_parent_rate(clk), val, NULL, 0, div->width);
}

/*
 * Some clocks require a "FC" (frequency change) bit to be set after changing
 * their rates or reparenting. This bit will be automatically cleared by
 * hardware in MIX_FC_TIMEOUT_US, which indicates the operation is completed.
 */
static int ccu_mix_trigger_fc(struct clk *clk)
{
	struct ccu_common *common = clk_to_ccu_common(clk);
	unsigned int val;

	if (common->reg_fc)
		return 0;

	ccu_update(common, fc, common->mask_fc, common->mask_fc);

	return regmap_read_poll_timeout(common->regmap, common->reg_fc,
					val, !(val & common->mask_fc),
					MIX_FC_DELAY_US,
					MIX_FC_TIMEOUT_US);
}

static unsigned long
ccu_mix_calc_best_rate(struct clk *clk, unsigned long rate,
		       struct clk **best_parent,
		       unsigned long *best_parent_rate,
		       u32 *div_val)
{
	struct ccu_common *common = clk_to_ccu_common(clk);
	struct ccu_mix *mix = clk_to_ccu_mix(clk);
	unsigned int parent_num = common->num_parents;
	struct ccu_div_config *div = &mix->div;
	u32 div_max = 1 << div->width;
	unsigned long best_rate = 0;

	for (int i = 0; i < parent_num; i++) {
		struct udevice *parent_dev;
		unsigned long parent_rate;
		struct clk *parent;

		if (uclass_get_device_by_name(UCLASS_CLK, common->parents[i],
					      &parent_dev))
			continue;
		parent = dev_get_clk_ptr(parent_dev);
		if (!parent)
			continue;

		parent_rate = clk_get_rate(parent);

		for (int j = 1; j <= div_max; j++) {
			unsigned long tmp = DIV_ROUND_CLOSEST_ULL(parent_rate, j);

			if (abs(tmp - rate) < abs(best_rate - rate)) {
				best_rate = tmp;

				if (div_val)
					*div_val = j - 1;

				if (best_parent) {
					*best_parent      = parent;
					*best_parent_rate = parent_rate;
				}
			}
		}
	}

	return best_rate;
}

static unsigned long ccu_mix_set_rate(struct clk *clk, unsigned long rate)
{
	struct ccu_mix *mix = clk_to_ccu_mix(clk);
	struct ccu_common *common = &mix->common;
	struct ccu_div_config *div = &mix->div;
	u32 current_div, target_div, mask;

	ccu_mix_calc_best_rate(clk, rate, NULL, NULL, &target_div);

	current_div = ccu_read(common, ctrl) >> div->shift;
	current_div &= (1 << div->width) - 1;

	if (current_div == target_div)
		return 0;

	mask = GENMASK(div->width + div->shift - 1, div->shift);

	ccu_update(common, ctrl, mask, target_div << div->shift);

	return ccu_mix_trigger_fc(clk);
}

static u8 ccu_mux_get_parent(struct clk *clk)
{
	struct ccu_mix *mix = clk_to_ccu_mix(clk);
	struct ccu_mux_config *mux = &mix->mux;
	u8 parent;

	parent = ccu_read(&mix->common, ctrl) >> mux->shift;
	parent &= (1 << mux->width) - 1;

	return parent;
}

static int ccu_mux_set_parent(struct clk *clk, struct clk *parent)
{
	struct ccu_common *common = clk_to_ccu_common(clk);
	struct ccu_mix *mix = clk_to_ccu_mix(clk);
	struct ccu_mux_config *mux = &mix->mux;
	u32 mask;
	int i = 0;

	mask = GENMASK(mux->width + mux->shift - 1, mux->shift);

	for (i = 0; i < common->num_parents; i++) {
		if (!strcmp(parent->dev->name, common->parents[i]))
			break;
	}

	if (i == common->num_parents)
		return -EINVAL;

	ccu_update(&mix->common, ctrl, mask, i << mux->shift);

	return ccu_mix_trigger_fc(clk);
}

int spacemit_gate_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_GATE,
			    common->name, common->parents[0]);
}

static const struct clk_ops spacemit_clk_gate_ops = {
	.disable	= ccu_gate_disable,
	.enable		= ccu_gate_enable,
	.get_rate	= clk_generic_get_rate,
};

U_BOOT_DRIVER(spacemit_clk_gate) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_GATE,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_gate_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_factor_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_FACTOR,
			    common->name, common->parents[0]);
}

static const struct clk_ops spacemit_clk_factor_ops = {
	.get_rate	= ccu_factor_recalc_rate,
};

U_BOOT_DRIVER(spacemit_clk_factor) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_FACTOR,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_factor_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_mux_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;
	u8 index;

	index = ccu_mux_get_parent(clk);
	if (index >= common->num_parents)
		index = 0;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_MUX,
			    common->name, common->parents[index]);
}

static const struct clk_ops spacemit_clk_mux_ops = {
	.set_parent	= ccu_mux_set_parent,
	.get_rate	= clk_generic_get_rate,
};

U_BOOT_DRIVER(spacemit_clk_mux) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_MUX,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_mux_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_div_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_DIV,
			    common->name, common->parents[0]);
}

static const struct clk_ops spacemit_clk_div_ops = {
	.get_rate	= ccu_div_recalc_rate,
	.set_rate	= ccu_mix_set_rate,
};

U_BOOT_DRIVER(spacemit_clk_div) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_DIV,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_div_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_factor_gate_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_FACTOR_GATE,
			    common->name, common->parents[0]);
}

static const struct clk_ops spacemit_clk_factor_gate_ops = {
	.disable	= ccu_gate_disable,
	.enable		= ccu_gate_enable,
	.get_rate	= ccu_factor_recalc_rate,
};

U_BOOT_DRIVER(spacemit_clk_factor_gate) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_FACTOR_GATE,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_factor_gate_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_mux_gate_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;
	u8 index;

	index = ccu_mux_get_parent(clk);
	if (index >= common->num_parents)
		index = 0;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_MUX_GATE,
			    common->name, common->parents[index]);
}

static const struct clk_ops spacemit_clk_mux_gate_ops = {
	.disable	= ccu_gate_disable,
	.enable		= ccu_gate_enable,
	.set_parent	= ccu_mux_set_parent,
	.get_rate	= clk_generic_get_rate,
};

U_BOOT_DRIVER(spacemit_clk_mux_gate) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_MUX_GATE,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_mux_gate_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_div_gate_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_DIV_GATE,
			    common->name, common->parents[0]);
}

static const struct clk_ops spacemit_clk_div_gate_ops = {
	.disable	= ccu_gate_disable,
	.enable		= ccu_gate_enable,
	.get_rate	= ccu_div_recalc_rate,
	.set_rate	= ccu_mix_set_rate,
};

U_BOOT_DRIVER(spacemit_clk_div_gate) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_DIV_GATE,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_div_gate_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_mux_div_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;
	u8 index;

	index = ccu_mux_get_parent(clk);
	if (index >= common->num_parents)
		index = 0;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_MUX_DIV,
			    common->name, common->parents[index]);
}

static const struct clk_ops spacemit_clk_mux_div_ops = {
	.set_parent	= ccu_mux_set_parent,
	.get_rate	= ccu_div_recalc_rate,
	.set_rate	= ccu_mix_set_rate,
};

U_BOOT_DRIVER(spacemit_clk_mux_div) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_MUX_DIV,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_mux_div_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

int spacemit_mux_div_gate_init(struct ccu_common *common)
{
	struct clk *clk = &common->clk;
	u8 index;

	index = ccu_mux_get_parent(clk);
	if (index >= common->num_parents)
		index = 0;

	return clk_register(clk, UBOOT_DM_SPACEMIT_CLK_MUX_DIV_GATE,
			    common->name, common->parents[index]);
}

static const struct clk_ops spacemit_clk_mux_div_gate_ops = {
	.disable	= ccu_gate_disable,
	.enable		= ccu_gate_enable,
	.set_parent	= ccu_mux_set_parent,
	.get_rate	= ccu_div_recalc_rate,
	.set_rate	= ccu_mix_set_rate,
};

U_BOOT_DRIVER(spacemit_clk_mux_div_gate) = {
	.name	= UBOOT_DM_SPACEMIT_CLK_MUX_DIV_GATE,
	.id	= UCLASS_CLK,
	.ops	= &spacemit_clk_mux_div_gate_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};