aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/meson/a1.c
blob: 5220a337a8bbf7600a70fcae711990c58ae353cd (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2023 SberDevices, Inc.
 * Author: Igor Prusov <ivprusov@salutedevices.com>
 */

#include <common.h>
#include <clk-uclass.h>
#include <dm.h>
#include <regmap.h>
#include <asm/arch/clock-a1.h>
#include <dt-bindings/clock/amlogic,a1-pll-clkc.h>
#include <dt-bindings/clock/amlogic,a1-peripherals-clkc.h>
#include "clk_meson.h"

/*
 * This driver supports both PLL and peripherals clock sources.
 * Following operations are supported:
 * - calculating clock frequency on a limited tree
 * - reading muxes and dividers
 * - enabling/disabling gates without propagation
 * - reparenting without rate propagation, only on muxes
 * - setting rates with limited reparenting, only on dividers with mux parent
 */

#define NR_CLKS				154
#define NR_PLL_CLKS			11

/* External clock IDs. Those should not overlap with regular IDs */
#define EXTERNAL_XTAL			(NR_CLKS + 0)
#define EXTERNAL_FCLK_DIV2		(NR_CLKS + 1)
#define EXTERNAL_FCLK_DIV3		(NR_CLKS + 2)
#define EXTERNAL_FCLK_DIV5		(NR_CLKS + 3)
#define EXTERNAL_FCLK_DIV7		(NR_CLKS + 4)

#define EXTERNAL_FIXPLL_IN		(NR_PLL_CLKS + 1)

#define SET_PARM_VALUE(_priv, _parm, _val)				\
	regmap_update_bits((_priv)->map, (_parm)->reg_off,		\
			   SETPMASK((_parm)->width, (_parm)->shift),	\
			   (_val) << (_parm)->shift)

#define GET_PARM_VALUE(_priv, _parm)					\
({									\
	uint _reg;							\
	regmap_read((_priv)->map, (_parm)->reg_off, &_reg);		\
	PARM_GET((_parm)->width, (_parm)->shift, _reg);			\
})

struct meson_clk {
	struct regmap *map;
};

/**
 * enum meson_clk_type - The type of clock
 * @MESON_CLK_ANY: Special value that matches any clock type
 * @MESON_CLK_GATE: This clock is a gate
 * @MESON_CLK_MUX: This clock is a multiplexer
 * @MESON_CLK_DIV: This clock is a configurable divider
 * @MESON_CLK_FIXED_DIV: This clock is a configurable divider
 * @MESON_CLK_EXTERNAL: This is an external clock from different clock provider
 * @MESON_CLK_PLL: This is a PLL
 */
enum meson_clk_type {
	MESON_CLK_ANY = 0,
	MESON_CLK_GATE,
	MESON_CLK_MUX,
	MESON_CLK_DIV,
	MESON_CLK_FIXED_DIV,
	MESON_CLK_EXTERNAL,
	MESON_CLK_PLL,
};

/**
 * struct meson_clk_info - The parameters defining a clock
 * @name: Name of the clock
 * @parm: Register bits description for muxes and dividers
 * @div: Fixed divider value
 * @parents: List of parent clock IDs
 * @type: Clock type
 */
struct meson_clk_info {
	const char *name;
	union {
		const struct parm *parm;
		u8 div;
	};
	const unsigned int *parents;
	const enum meson_clk_type type;
};

/**
 * struct meson_clk_data - Clocks supported by clock provider
 * @num_clocks: Number of clocks
 * @clocks: Array of clock descriptions
 *
 */
struct meson_clk_data {
	const u8 num_clocks;
	const struct meson_clk_info **clocks;
};

/* Clock description initialization macros */

/* A multiplexer */
#define CLK_MUX(_name, _reg, _shift, _width, ...)			\
	(&(struct meson_clk_info){					\
		.parents = (const unsigned int[])__VA_ARGS__,		\
		.parm = &(struct parm) {				\
			.reg_off = (_reg),				\
			.shift = (_shift),				\
			.width = (_width),				\
		},							\
		.name = (_name),					\
		.type = MESON_CLK_MUX,					\
	})

/* A divider with an integral divisor */
#define CLK_DIV(_name, _reg, _shift, _width, _parent)			\
	(&(struct meson_clk_info){					\
		.parents = (const unsigned int[]) { (_parent) },	\
		.parm = &(struct parm) {				\
			.reg_off = (_reg),				\
			.shift = (_shift),				\
			.width = (_width),				\
		},							\
		.name = (_name),					\
		.type = MESON_CLK_DIV,					\
	})

/* A fixed divider */
#define CLK_DIV_FIXED(_name, _div, _parent)				\
	(&(struct meson_clk_info){					\
		.parents = (const unsigned int[]) { (_parent) },	\
		.div = (_div),						\
		.name = (_name),					\
		.type = MESON_CLK_FIXED_DIV,				\
	})

/* An external clock */
#define CLK_EXTERNAL(_name)						\
	(&(struct meson_clk_info){					\
		.name = (_name),					\
		.parents = (const unsigned int[]) { -ENOENT },		\
		.type = MESON_CLK_EXTERNAL,				\
	})

/* A clock gate */
#define CLK_GATE(_name, _reg, _shift, _parent)				\
	(&(struct meson_clk_info){					\
		.parents = (const unsigned int[]) { (_parent) },	\
		.parm = &(struct parm) {				\
			.reg_off = (_reg),				\
			.shift = (_shift),				\
			.width = 1,					\
		},							\
		.name = (_name),					\
		.type = MESON_CLK_GATE,					\
	})

/* A PLL clock */
#define CLK_PLL(_name, _parent, ...)					\
	(&(struct meson_clk_info){					\
		.name = (_name),					\
		.parents = (const unsigned int[]) { (_parent) },	\
		.parm = (const struct parm[])__VA_ARGS__,		\
		.type = MESON_CLK_PLL,					\
	})

/* A1 peripherals clocks */
static const struct meson_clk_info *meson_clocks[] = {
	[CLKID_SPIFC_SEL] = CLK_MUX("spifc_sel", A1_SPIFC_CLK_CTRL, 9, 2, {
		EXTERNAL_FCLK_DIV2,
		EXTERNAL_FCLK_DIV3,
		EXTERNAL_FCLK_DIV5,
		-ENOENT,
	}),
	[CLKID_SPIFC_SEL2] = CLK_MUX("spifc_sel2", A1_SPIFC_CLK_CTRL, 15, 1, {
		CLKID_SPIFC_DIV,
		EXTERNAL_XTAL,
	}),
	[CLKID_USB_BUS_SEL] = CLK_MUX("usb_bus_sel", A1_USB_BUSCLK_CTRL, 9, 2, {
		-ENOENT,
		CLKID_SYS,
		EXTERNAL_FCLK_DIV3,
		EXTERNAL_FCLK_DIV5,
	}),
	[CLKID_SYS] = CLK_MUX("sys", A1_SYS_CLK_CTRL0, 31, 1, {
		CLKID_SYS_A,
		CLKID_SYS_B,
	}),
	[CLKID_SYS_A_SEL] = CLK_MUX("sys_a_sel", A1_SYS_CLK_CTRL0, 10, 3, {
		-ENOENT,
		EXTERNAL_FCLK_DIV2,
		EXTERNAL_FCLK_DIV3,
		EXTERNAL_FCLK_DIV5,
		-ENOENT,
		-ENOENT,
		-ENOENT,
		-ENOENT,
	}),
	[CLKID_SYS_B_SEL] = CLK_MUX("sys_b_sel", A1_SYS_CLK_CTRL0, 26, 3, {
		-ENOENT,
		EXTERNAL_FCLK_DIV2,
		EXTERNAL_FCLK_DIV3,
		EXTERNAL_FCLK_DIV5,
		-ENOENT,
		-ENOENT,
		-ENOENT,
		-ENOENT,
	}),

	[CLKID_SPIFC_DIV] = CLK_DIV("spifc_div", A1_SPIFC_CLK_CTRL, 0, 8,
		CLKID_SPIFC_SEL
	),
	[CLKID_USB_BUS_DIV] = CLK_DIV("usb_bus_div", A1_USB_BUSCLK_CTRL, 0, 8,
		CLKID_USB_BUS_SEL
	),
	[CLKID_SYS_A_DIV] = CLK_DIV("sys_a_div", A1_SYS_CLK_CTRL0, 0, 10,
		CLKID_SYS_A_SEL
	),
	[CLKID_SYS_B_DIV] = CLK_DIV("sys_b_div", A1_SYS_CLK_CTRL0, 16, 10,
		CLKID_SYS_B_SEL
	),

	[CLKID_SPIFC] = CLK_GATE("spifc", A1_SPIFC_CLK_CTRL, 8,
		CLKID_SPIFC_SEL2
	),
	[CLKID_USB_BUS] = CLK_GATE("usb_bus", A1_USB_BUSCLK_CTRL, 8,
		CLKID_USB_BUS_DIV
	),
	[CLKID_SYS_A] = CLK_GATE("sys_a", A1_SYS_CLK_CTRL0, 13,
		CLKID_SYS_A_DIV
	),
	[CLKID_SYS_B] = CLK_GATE("sys_b", A1_SYS_CLK_CTRL0, 29,
		CLKID_SYS_B_DIV
	),
	[CLKID_FIXPLL_IN] = CLK_GATE("fixpll_in", A1_SYS_OSCIN_CTRL, 1,
		EXTERNAL_XTAL
	),
	[CLKID_USB_PHY_IN] = CLK_GATE("usb_phy_in", A1_SYS_OSCIN_CTRL, 2,
		EXTERNAL_XTAL
	),
	[CLKID_USB_CTRL_IN] = CLK_GATE("usb_ctrl_in", A1_SYS_OSCIN_CTRL, 3,
		EXTERNAL_XTAL
	),
	[CLKID_USB_CTRL] = CLK_GATE("usb_ctrl", A1_SYS_CLK_EN0, 28,
		CLKID_SYS
	),
	[CLKID_USB_PHY] = CLK_GATE("usb_phy", A1_SYS_CLK_EN0, 27,
		CLKID_SYS
	),
	[CLKID_SARADC] = CLK_GATE("saradc", A1_SAR_ADC_CLK_CTR, 8,
		-ENOENT
	),
	[CLKID_SARADC_EN] = CLK_GATE("saradc_en", A1_SYS_CLK_EN0, 13,
		CLKID_SYS
	),

	[EXTERNAL_XTAL] = CLK_EXTERNAL("xtal"),
	[EXTERNAL_FCLK_DIV2] = CLK_EXTERNAL("fclk_div2"),
	[EXTERNAL_FCLK_DIV3] = CLK_EXTERNAL("fclk_div3"),
	[EXTERNAL_FCLK_DIV5] = CLK_EXTERNAL("fclk_div5"),
	[EXTERNAL_FCLK_DIV7] = CLK_EXTERNAL("fclk_div7"),
};

/* A1 PLL clocks */
static const struct meson_clk_info *meson_pll_clocks[] = {
	[EXTERNAL_FIXPLL_IN] = CLK_EXTERNAL("fixpll_in"),

	[CLKID_FIXED_PLL_DCO] = CLK_PLL("fixed_pll_dco", EXTERNAL_FIXPLL_IN, {
			{A1_ANACTRL_FIXPLL_CTRL0, 0, 8},
			{A1_ANACTRL_FIXPLL_CTRL0, 10, 5},
	}),

	[CLKID_FCLK_DIV2_DIV] = CLK_DIV_FIXED("fclk_div2_div", 2,
		CLKID_FIXED_PLL
	),
	[CLKID_FCLK_DIV3_DIV] = CLK_DIV_FIXED("fclk_div3_div", 3,
		CLKID_FIXED_PLL
	),
	[CLKID_FCLK_DIV5_DIV] = CLK_DIV_FIXED("fclk_div5_div", 5,
		CLKID_FIXED_PLL
	),
	[CLKID_FCLK_DIV7_DIV] = CLK_DIV_FIXED("fclk_div7_div", 7,
		CLKID_FIXED_PLL
	),

	[CLKID_FIXED_PLL] = CLK_GATE("fixed_pll", A1_ANACTRL_FIXPLL_CTRL0, 20,
		CLKID_FIXED_PLL_DCO
	),
	[CLKID_FCLK_DIV2] = CLK_GATE("fclk_div2", A1_ANACTRL_FIXPLL_CTRL0, 21,
		CLKID_FCLK_DIV2_DIV
	),
	[CLKID_FCLK_DIV3] = CLK_GATE("fclk_div3", A1_ANACTRL_FIXPLL_CTRL0, 22,
		CLKID_FCLK_DIV3_DIV
	),
	[CLKID_FCLK_DIV5] = CLK_GATE("fclk_div5", A1_ANACTRL_FIXPLL_CTRL0, 23,
		CLKID_FCLK_DIV5_DIV
	),
	[CLKID_FCLK_DIV7] = CLK_GATE("fclk_div7", A1_ANACTRL_FIXPLL_CTRL0, 24,
		CLKID_FCLK_DIV7_DIV
	),
};

static const struct meson_clk_info *meson_clk_get_info(struct clk *clk, ulong id,
						       enum meson_clk_type type)
{
	struct meson_clk_data *data;
	const struct meson_clk_info *info;

	data = (struct meson_clk_data *)dev_get_driver_data(clk->dev);
	if (id >= data->num_clocks)
		return ERR_PTR(-EINVAL);

	info = data->clocks[id];
	if (!info)
		return ERR_PTR(-ENOENT);

	if (type != MESON_CLK_ANY && type != info->type)
		return ERR_PTR(-EINVAL);

	return info;
}

static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id);

static int meson_set_gate(struct clk *clk, bool on)
{
	struct meson_clk *priv = dev_get_priv(clk->dev);
	const struct meson_clk_info *info;

	debug("%s: %sabling %lu\n", __func__, on ? "en" : "dis", clk->id);

	info = meson_clk_get_info(clk, clk->id, MESON_CLK_ANY);
	if (IS_ERR(info))
		return PTR_ERR(info);

	SET_PARM_VALUE(priv, info->parm, on);

	return 0;
}

static int meson_clk_enable(struct clk *clk)
{
	return meson_set_gate(clk, true);
}

static int meson_clk_disable(struct clk *clk)
{
	return meson_set_gate(clk, false);
}

static ulong meson_div_get_rate(struct clk *clk, unsigned long id)
{
	struct meson_clk *priv = dev_get_priv(clk->dev);
	u16 n;
	ulong rate;
	const struct meson_clk_info *info;

	info = meson_clk_get_info(clk, id, MESON_CLK_DIV);
	if (IS_ERR(info))
		return PTR_ERR(info);

	/* Actual divider value is (field value + 1), hence the increment */
	n = GET_PARM_VALUE(priv, info->parm) + 1;

	rate = meson_clk_get_rate_by_id(clk, info->parents[0]);

	return rate / n;
}

static int meson_clk_get_parent(struct clk *clk, unsigned long id)
{
	uint reg = 0;
	struct meson_clk *priv = dev_get_priv(clk->dev);
	const struct meson_clk_info *info;

	info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
	if (IS_ERR(info))
		return PTR_ERR(info);

	/* For muxes we read currently selected parent from register,
	 * for other types there is always only one element in parents array.
	 */
	if (info->type == MESON_CLK_MUX) {
		reg = GET_PARM_VALUE(priv, info->parm);
		if (IS_ERR_VALUE(reg))
			return reg;
	}

	return info->parents[reg];
}

static ulong meson_pll_get_rate(struct clk *clk, unsigned long id)
{
	struct meson_clk *priv = dev_get_priv(clk->dev);
	const struct meson_clk_info *info;
	const struct parm *pm, *pn;
	ulong parent_rate_mhz;
	unsigned int parent;
	u16 n, m;

	info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
	if (IS_ERR(info))
		return PTR_ERR(info);

	pm = &info->parm[0];
	pn = &info->parm[1];

	n = GET_PARM_VALUE(priv, pn);
	m = GET_PARM_VALUE(priv, pm);

	if (n == 0)
		return -EINVAL;

	parent = info->parents[0];
	parent_rate_mhz = meson_clk_get_rate_by_id(clk, parent) / 1000000;

	return parent_rate_mhz * m / n * 1000000;
}

static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id)
{
	ulong rate, parent;
	const struct meson_clk_info *info;

	if (IS_ERR_VALUE(id))
		return id;

	info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
	if (IS_ERR(info))
		return PTR_ERR(info);

	switch (info->type) {
	case MESON_CLK_PLL:
		rate = meson_pll_get_rate(clk, id);
		break;
	case MESON_CLK_GATE:
	case MESON_CLK_MUX:
		parent = meson_clk_get_parent(clk, id);
		rate = meson_clk_get_rate_by_id(clk, parent);
		break;
	case MESON_CLK_DIV:
		rate = meson_div_get_rate(clk, id);
		break;
	case MESON_CLK_FIXED_DIV:
		parent = meson_clk_get_parent(clk, id);
		rate = meson_clk_get_rate_by_id(clk, parent) / info->div;
		break;
	case MESON_CLK_EXTERNAL: {
		int ret;
		struct clk external_clk;

		ret = clk_get_by_name(clk->dev, info->name, &external_clk);
		if (ret)
			return ret;

		rate = clk_get_rate(&external_clk);
		break;
	}
	default:
		rate = -EINVAL;
		break;
	}

	return rate;
}

static ulong meson_clk_get_rate(struct clk *clk)
{
	return meson_clk_get_rate_by_id(clk, clk->id);
}

/* This implements rate propagation for dividers placed after multiplexer:
 *  ---------|\
 *     ..... | |---DIV--
 *  ---------|/
 */
static ulong meson_composite_set_rate(struct clk *clk, ulong id, ulong rate)
{
	unsigned int i, best_div_val;
	unsigned long best_delta, best_parent;
	const struct meson_clk_info *div;
	const struct meson_clk_info *mux;
	struct meson_clk *priv = dev_get_priv(clk->dev);

	div = meson_clk_get_info(clk, id, MESON_CLK_DIV);
	if (IS_ERR(div))
		return PTR_ERR(div);

	mux = meson_clk_get_info(clk, div->parents[0], MESON_CLK_MUX);
	if (IS_ERR(mux))
		return PTR_ERR(mux);

	best_parent = -EINVAL;
	best_delta = ULONG_MAX;
	for (i = 0; i < (1 << mux->parm->width); i++) {
		unsigned long parent_rate, delta;
		unsigned int div_val;

		parent_rate = meson_clk_get_rate_by_id(clk, mux->parents[i]);
		if (IS_ERR_VALUE(parent_rate))
			continue;

		/* If overflow, try to use max divider value */
		div_val = min(DIV_ROUND_CLOSEST(parent_rate, rate),
			      (1UL << div->parm->width));

		delta = abs(rate - (parent_rate / div_val));
		if (delta < best_delta) {
			best_delta = delta;
			best_div_val = div_val;
			best_parent = i;
		}
	}

	if (IS_ERR_VALUE(best_parent))
		return best_parent;

	SET_PARM_VALUE(priv, mux->parm, best_parent);
	/* Divider is set to (field value + 1), hence the decrement */
	SET_PARM_VALUE(priv, div->parm, best_div_val - 1);

	return 0;
}

static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned int id, ulong rate);

static ulong meson_mux_set_rate(struct clk *clk, unsigned long id, ulong rate)
{
	int i;
	ulong ret = -EINVAL;
	struct meson_clk *priv = dev_get_priv(clk->dev);
	const struct meson_clk_info *info;

	info = meson_clk_get_info(clk, id, MESON_CLK_MUX);
	if (IS_ERR(info))
		return PTR_ERR(info);

	for (i = 0; i < (1 << info->parm->width); i++) {
		ret = meson_clk_set_rate_by_id(clk, info->parents[i], rate);
		if (!ret) {
			SET_PARM_VALUE(priv, info->parm, i);
			break;
		}
	}

	return ret;
}

/* Rate propagation is implemented for a subcection of a clock tree, that is
 * required at boot stage.
 */
static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned int id, ulong rate)
{
	switch (id) {
	case CLKID_SPIFC_DIV:
	case CLKID_USB_BUS_DIV:
		return meson_composite_set_rate(clk, id, rate);
	case CLKID_SPIFC:
	case CLKID_USB_BUS: {
		unsigned long parent = meson_clk_get_parent(clk, id);

		return meson_clk_set_rate_by_id(clk, parent, rate);
	}
	case CLKID_SPIFC_SEL2:
		return meson_mux_set_rate(clk, id, rate);
	}

	return -EINVAL;
}

static ulong meson_clk_set_rate(struct clk *clk, ulong rate)
{
	return meson_clk_set_rate_by_id(clk, clk->id, rate);
}

static int meson_mux_set_parent_by_id(struct clk *clk, unsigned int parent_id)
{
	unsigned int i, parent_index;
	struct meson_clk *priv = dev_get_priv(clk->dev);
	const struct meson_clk_info *info;

	info = meson_clk_get_info(clk, clk->id, MESON_CLK_MUX);
	if (IS_ERR(info))
		return PTR_ERR(info);

	parent_index = -EINVAL;
	for (i = 0; i < (1 << info->parm->width); i++) {
		if (parent_id == info->parents[i]) {
			parent_index = i;
			break;
		}
	}

	if (IS_ERR_VALUE(parent_index))
		return parent_index;

	SET_PARM_VALUE(priv, info->parm, parent_index);

	return 0;
}

static int meson_clk_set_parent(struct clk *clk, struct clk *parent_clk)
{
	return meson_mux_set_parent_by_id(clk, parent_clk->id);
}

static int meson_clk_probe(struct udevice *dev)
{
	struct meson_clk *priv = dev_get_priv(dev);

	return regmap_init_mem(dev_ofnode(dev), &priv->map);
}

struct meson_clk_data meson_a1_peripherals_info = {
	.clocks = meson_clocks,
	.num_clocks = ARRAY_SIZE(meson_clocks),
};

struct meson_clk_data meson_a1_pll_info = {
	.clocks = meson_pll_clocks,
	.num_clocks = ARRAY_SIZE(meson_pll_clocks),
};

static const struct udevice_id meson_clk_ids[] = {
	{
		.compatible = "amlogic,a1-peripherals-clkc",
		.data = (ulong)&meson_a1_peripherals_info,
	},
	{
		.compatible = "amlogic,a1-pll-clkc",
		.data = (ulong)&meson_a1_pll_info,
	},
	{ }
};

#if IS_ENABLED(CONFIG_CMD_CLK)
static const char *meson_clk_get_name(struct clk *clk, int id)
{
	const struct meson_clk_info *info;

	info = meson_clk_get_info(clk, id, MESON_CLK_ANY);

	return IS_ERR(info) ? "unknown" : info->name;
}

static int meson_clk_dump_single(struct clk *clk)
{
	const struct meson_clk_info *info;
	struct meson_clk *priv;
	unsigned long rate;
	char *state, frequency[80];
	int parent;

	priv = dev_get_priv(clk->dev);

	info = meson_clk_get_info(clk, clk->id, MESON_CLK_ANY);
	if (IS_ERR(info) || !info->name)
		return -EINVAL;

	rate = clk_get_rate(clk);
	if (IS_ERR_VALUE(rate))
		sprintf(frequency, "unknown");
	else
		sprintf(frequency, "%lu", rate);

	if (info->type == MESON_CLK_GATE)
		state = GET_PARM_VALUE(priv, info->parm) ? "enabled" : "disabled";
	else
		state = "N/A";

	parent = meson_clk_get_parent(clk, clk->id);
	printf("%15s%20s%20s%15s\n",
	       info->name,
	       frequency,
	       meson_clk_get_name(clk, parent),
	       state);

	return 0;
}

static void meson_clk_dump(struct udevice *dev)
{
	int i;
	struct meson_clk_data *data;
	const char *sep = "--------------------";

	printf("%s:\n", dev->name);
	printf("%.15s%s%s%.15s\n", sep, sep, sep, sep);
	printf("%15s%20s%20s%15s\n", "clk", "frequency", "parent", "state");
	printf("%.15s%s%s%.15s\n", sep, sep, sep, sep);

	data = (struct meson_clk_data *)dev_get_driver_data(dev);
	for (i = 0; i < data->num_clocks; i++) {
		meson_clk_dump_single(&(struct clk){
			.dev = dev,
			.id = i
		});
	}
}
#endif

static struct clk_ops meson_clk_ops = {
	.disable	= meson_clk_disable,
	.enable		= meson_clk_enable,
	.get_rate	= meson_clk_get_rate,
	.set_rate	= meson_clk_set_rate,
	.set_parent	= meson_clk_set_parent,
#if IS_ENABLED(CONFIG_CMD_CLK)
	.dump		= meson_clk_dump,
#endif
};

U_BOOT_DRIVER(meson_clk) = {
	.name		= "meson-clk-a1",
	.id		= UCLASS_CLK,
	.of_match	= meson_clk_ids,
	.priv_auto	= sizeof(struct meson_clk),
	.ops		= &meson_clk_ops,
	.probe		= meson_clk_probe,
};