xref: /openbmc/linux/drivers/clk/bcm/clk-bcm2835.c (revision 2eb0f624b709e78ec8e2f4c3412947703db99301)
1 /*
2  * Copyright (C) 2010,2015 Broadcom
3  * Copyright (C) 2012 Stephen Warren
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 
17 /**
18  * DOC: BCM2835 CPRMAN (clock manager for the "audio" domain)
19  *
20  * The clock tree on the 2835 has several levels.  There's a root
21  * oscillator running at 19.2Mhz.  After the oscillator there are 5
22  * PLLs, roughly divided as "camera", "ARM", "core", "DSI displays",
23  * and "HDMI displays".  Those 5 PLLs each can divide their output to
24  * produce up to 4 channels.  Finally, there is the level of clocks to
25  * be consumed by other hardware components (like "H264" or "HDMI
26  * state machine"), which divide off of some subset of the PLL
27  * channels.
28  *
29  * All of the clocks in the tree are exposed in the DT, because the DT
30  * may want to make assignments of the final layer of clocks to the
31  * PLL channels, and some components of the hardware will actually
32  * skip layers of the tree (for example, the pixel clock comes
33  * directly from the PLLH PIX channel without using a CM_*CTL clock
34  * generator).
35  */
36 
37 #include <linux/clk-provider.h>
38 #include <linux/clkdev.h>
39 #include <linux/clk.h>
40 #include <linux/debugfs.h>
41 #include <linux/delay.h>
42 #include <linux/module.h>
43 #include <linux/of.h>
44 #include <linux/platform_device.h>
45 #include <linux/slab.h>
46 #include <dt-bindings/clock/bcm2835.h>
47 
48 #define CM_PASSWORD		0x5a000000
49 
50 #define CM_GNRICCTL		0x000
51 #define CM_GNRICDIV		0x004
52 # define CM_DIV_FRAC_BITS	12
53 # define CM_DIV_FRAC_MASK	GENMASK(CM_DIV_FRAC_BITS - 1, 0)
54 
55 #define CM_VPUCTL		0x008
56 #define CM_VPUDIV		0x00c
57 #define CM_SYSCTL		0x010
58 #define CM_SYSDIV		0x014
59 #define CM_PERIACTL		0x018
60 #define CM_PERIADIV		0x01c
61 #define CM_PERIICTL		0x020
62 #define CM_PERIIDIV		0x024
63 #define CM_H264CTL		0x028
64 #define CM_H264DIV		0x02c
65 #define CM_ISPCTL		0x030
66 #define CM_ISPDIV		0x034
67 #define CM_V3DCTL		0x038
68 #define CM_V3DDIV		0x03c
69 #define CM_CAM0CTL		0x040
70 #define CM_CAM0DIV		0x044
71 #define CM_CAM1CTL		0x048
72 #define CM_CAM1DIV		0x04c
73 #define CM_CCP2CTL		0x050
74 #define CM_CCP2DIV		0x054
75 #define CM_DSI0ECTL		0x058
76 #define CM_DSI0EDIV		0x05c
77 #define CM_DSI0PCTL		0x060
78 #define CM_DSI0PDIV		0x064
79 #define CM_DPICTL		0x068
80 #define CM_DPIDIV		0x06c
81 #define CM_GP0CTL		0x070
82 #define CM_GP0DIV		0x074
83 #define CM_GP1CTL		0x078
84 #define CM_GP1DIV		0x07c
85 #define CM_GP2CTL		0x080
86 #define CM_GP2DIV		0x084
87 #define CM_HSMCTL		0x088
88 #define CM_HSMDIV		0x08c
89 #define CM_OTPCTL		0x090
90 #define CM_OTPDIV		0x094
91 #define CM_PCMCTL		0x098
92 #define CM_PCMDIV		0x09c
93 #define CM_PWMCTL		0x0a0
94 #define CM_PWMDIV		0x0a4
95 #define CM_SLIMCTL		0x0a8
96 #define CM_SLIMDIV		0x0ac
97 #define CM_SMICTL		0x0b0
98 #define CM_SMIDIV		0x0b4
99 /* no definition for 0x0b8  and 0x0bc */
100 #define CM_TCNTCTL		0x0c0
101 # define CM_TCNT_SRC1_SHIFT		12
102 #define CM_TCNTCNT		0x0c4
103 #define CM_TECCTL		0x0c8
104 #define CM_TECDIV		0x0cc
105 #define CM_TD0CTL		0x0d0
106 #define CM_TD0DIV		0x0d4
107 #define CM_TD1CTL		0x0d8
108 #define CM_TD1DIV		0x0dc
109 #define CM_TSENSCTL		0x0e0
110 #define CM_TSENSDIV		0x0e4
111 #define CM_TIMERCTL		0x0e8
112 #define CM_TIMERDIV		0x0ec
113 #define CM_UARTCTL		0x0f0
114 #define CM_UARTDIV		0x0f4
115 #define CM_VECCTL		0x0f8
116 #define CM_VECDIV		0x0fc
117 #define CM_PULSECTL		0x190
118 #define CM_PULSEDIV		0x194
119 #define CM_SDCCTL		0x1a8
120 #define CM_SDCDIV		0x1ac
121 #define CM_ARMCTL		0x1b0
122 #define CM_AVEOCTL		0x1b8
123 #define CM_AVEODIV		0x1bc
124 #define CM_EMMCCTL		0x1c0
125 #define CM_EMMCDIV		0x1c4
126 
127 /* General bits for the CM_*CTL regs */
128 # define CM_ENABLE			BIT(4)
129 # define CM_KILL			BIT(5)
130 # define CM_GATE_BIT			6
131 # define CM_GATE			BIT(CM_GATE_BIT)
132 # define CM_BUSY			BIT(7)
133 # define CM_BUSYD			BIT(8)
134 # define CM_FRAC			BIT(9)
135 # define CM_SRC_SHIFT			0
136 # define CM_SRC_BITS			4
137 # define CM_SRC_MASK			0xf
138 # define CM_SRC_GND			0
139 # define CM_SRC_OSC			1
140 # define CM_SRC_TESTDEBUG0		2
141 # define CM_SRC_TESTDEBUG1		3
142 # define CM_SRC_PLLA_CORE		4
143 # define CM_SRC_PLLA_PER		4
144 # define CM_SRC_PLLC_CORE0		5
145 # define CM_SRC_PLLC_PER		5
146 # define CM_SRC_PLLC_CORE1		8
147 # define CM_SRC_PLLD_CORE		6
148 # define CM_SRC_PLLD_PER		6
149 # define CM_SRC_PLLH_AUX		7
150 # define CM_SRC_PLLC_CORE1		8
151 # define CM_SRC_PLLC_CORE2		9
152 
153 #define CM_OSCCOUNT		0x100
154 
155 #define CM_PLLA			0x104
156 # define CM_PLL_ANARST			BIT(8)
157 # define CM_PLLA_HOLDPER		BIT(7)
158 # define CM_PLLA_LOADPER		BIT(6)
159 # define CM_PLLA_HOLDCORE		BIT(5)
160 # define CM_PLLA_LOADCORE		BIT(4)
161 # define CM_PLLA_HOLDCCP2		BIT(3)
162 # define CM_PLLA_LOADCCP2		BIT(2)
163 # define CM_PLLA_HOLDDSI0		BIT(1)
164 # define CM_PLLA_LOADDSI0		BIT(0)
165 
166 #define CM_PLLC			0x108
167 # define CM_PLLC_HOLDPER		BIT(7)
168 # define CM_PLLC_LOADPER		BIT(6)
169 # define CM_PLLC_HOLDCORE2		BIT(5)
170 # define CM_PLLC_LOADCORE2		BIT(4)
171 # define CM_PLLC_HOLDCORE1		BIT(3)
172 # define CM_PLLC_LOADCORE1		BIT(2)
173 # define CM_PLLC_HOLDCORE0		BIT(1)
174 # define CM_PLLC_LOADCORE0		BIT(0)
175 
176 #define CM_PLLD			0x10c
177 # define CM_PLLD_HOLDPER		BIT(7)
178 # define CM_PLLD_LOADPER		BIT(6)
179 # define CM_PLLD_HOLDCORE		BIT(5)
180 # define CM_PLLD_LOADCORE		BIT(4)
181 # define CM_PLLD_HOLDDSI1		BIT(3)
182 # define CM_PLLD_LOADDSI1		BIT(2)
183 # define CM_PLLD_HOLDDSI0		BIT(1)
184 # define CM_PLLD_LOADDSI0		BIT(0)
185 
186 #define CM_PLLH			0x110
187 # define CM_PLLH_LOADRCAL		BIT(2)
188 # define CM_PLLH_LOADAUX		BIT(1)
189 # define CM_PLLH_LOADPIX		BIT(0)
190 
191 #define CM_LOCK			0x114
192 # define CM_LOCK_FLOCKH			BIT(12)
193 # define CM_LOCK_FLOCKD			BIT(11)
194 # define CM_LOCK_FLOCKC			BIT(10)
195 # define CM_LOCK_FLOCKB			BIT(9)
196 # define CM_LOCK_FLOCKA			BIT(8)
197 
198 #define CM_EVENT		0x118
199 #define CM_DSI1ECTL		0x158
200 #define CM_DSI1EDIV		0x15c
201 #define CM_DSI1PCTL		0x160
202 #define CM_DSI1PDIV		0x164
203 #define CM_DFTCTL		0x168
204 #define CM_DFTDIV		0x16c
205 
206 #define CM_PLLB			0x170
207 # define CM_PLLB_HOLDARM		BIT(1)
208 # define CM_PLLB_LOADARM		BIT(0)
209 
210 #define A2W_PLLA_CTRL		0x1100
211 #define A2W_PLLC_CTRL		0x1120
212 #define A2W_PLLD_CTRL		0x1140
213 #define A2W_PLLH_CTRL		0x1160
214 #define A2W_PLLB_CTRL		0x11e0
215 # define A2W_PLL_CTRL_PRST_DISABLE	BIT(17)
216 # define A2W_PLL_CTRL_PWRDN		BIT(16)
217 # define A2W_PLL_CTRL_PDIV_MASK		0x000007000
218 # define A2W_PLL_CTRL_PDIV_SHIFT	12
219 # define A2W_PLL_CTRL_NDIV_MASK		0x0000003ff
220 # define A2W_PLL_CTRL_NDIV_SHIFT	0
221 
222 #define A2W_PLLA_ANA0		0x1010
223 #define A2W_PLLC_ANA0		0x1030
224 #define A2W_PLLD_ANA0		0x1050
225 #define A2W_PLLH_ANA0		0x1070
226 #define A2W_PLLB_ANA0		0x10f0
227 
228 #define A2W_PLL_KA_SHIFT	7
229 #define A2W_PLL_KA_MASK		GENMASK(9, 7)
230 #define A2W_PLL_KI_SHIFT	19
231 #define A2W_PLL_KI_MASK		GENMASK(21, 19)
232 #define A2W_PLL_KP_SHIFT	15
233 #define A2W_PLL_KP_MASK		GENMASK(18, 15)
234 
235 #define A2W_PLLH_KA_SHIFT	19
236 #define A2W_PLLH_KA_MASK	GENMASK(21, 19)
237 #define A2W_PLLH_KI_LOW_SHIFT	22
238 #define A2W_PLLH_KI_LOW_MASK	GENMASK(23, 22)
239 #define A2W_PLLH_KI_HIGH_SHIFT	0
240 #define A2W_PLLH_KI_HIGH_MASK	GENMASK(0, 0)
241 #define A2W_PLLH_KP_SHIFT	1
242 #define A2W_PLLH_KP_MASK	GENMASK(4, 1)
243 
244 #define A2W_XOSC_CTRL		0x1190
245 # define A2W_XOSC_CTRL_PLLB_ENABLE	BIT(7)
246 # define A2W_XOSC_CTRL_PLLA_ENABLE	BIT(6)
247 # define A2W_XOSC_CTRL_PLLD_ENABLE	BIT(5)
248 # define A2W_XOSC_CTRL_DDR_ENABLE	BIT(4)
249 # define A2W_XOSC_CTRL_CPR1_ENABLE	BIT(3)
250 # define A2W_XOSC_CTRL_USB_ENABLE	BIT(2)
251 # define A2W_XOSC_CTRL_HDMI_ENABLE	BIT(1)
252 # define A2W_XOSC_CTRL_PLLC_ENABLE	BIT(0)
253 
254 #define A2W_PLLA_FRAC		0x1200
255 #define A2W_PLLC_FRAC		0x1220
256 #define A2W_PLLD_FRAC		0x1240
257 #define A2W_PLLH_FRAC		0x1260
258 #define A2W_PLLB_FRAC		0x12e0
259 # define A2W_PLL_FRAC_MASK		((1 << A2W_PLL_FRAC_BITS) - 1)
260 # define A2W_PLL_FRAC_BITS		20
261 
262 #define A2W_PLL_CHANNEL_DISABLE		BIT(8)
263 #define A2W_PLL_DIV_BITS		8
264 #define A2W_PLL_DIV_SHIFT		0
265 
266 #define A2W_PLLA_DSI0		0x1300
267 #define A2W_PLLA_CORE		0x1400
268 #define A2W_PLLA_PER		0x1500
269 #define A2W_PLLA_CCP2		0x1600
270 
271 #define A2W_PLLC_CORE2		0x1320
272 #define A2W_PLLC_CORE1		0x1420
273 #define A2W_PLLC_PER		0x1520
274 #define A2W_PLLC_CORE0		0x1620
275 
276 #define A2W_PLLD_DSI0		0x1340
277 #define A2W_PLLD_CORE		0x1440
278 #define A2W_PLLD_PER		0x1540
279 #define A2W_PLLD_DSI1		0x1640
280 
281 #define A2W_PLLH_AUX		0x1360
282 #define A2W_PLLH_RCAL		0x1460
283 #define A2W_PLLH_PIX		0x1560
284 #define A2W_PLLH_STS		0x1660
285 
286 #define A2W_PLLH_CTRLR		0x1960
287 #define A2W_PLLH_FRACR		0x1a60
288 #define A2W_PLLH_AUXR		0x1b60
289 #define A2W_PLLH_RCALR		0x1c60
290 #define A2W_PLLH_PIXR		0x1d60
291 #define A2W_PLLH_STSR		0x1e60
292 
293 #define A2W_PLLB_ARM		0x13e0
294 #define A2W_PLLB_SP0		0x14e0
295 #define A2W_PLLB_SP1		0x15e0
296 #define A2W_PLLB_SP2		0x16e0
297 
298 #define LOCK_TIMEOUT_NS		100000000
299 #define BCM2835_MAX_FB_RATE	1750000000u
300 
301 /*
302  * Names of clocks used within the driver that need to be replaced
303  * with an external parent's name.  This array is in the order that
304  * the clocks node in the DT references external clocks.
305  */
306 static const char *const cprman_parent_names[] = {
307 	"xosc",
308 	"dsi0_byte",
309 	"dsi0_ddr2",
310 	"dsi0_ddr",
311 	"dsi1_byte",
312 	"dsi1_ddr2",
313 	"dsi1_ddr",
314 };
315 
316 struct bcm2835_cprman {
317 	struct device *dev;
318 	void __iomem *regs;
319 	spinlock_t regs_lock; /* spinlock for all clocks */
320 
321 	/*
322 	 * Real names of cprman clock parents looked up through
323 	 * of_clk_get_parent_name(), which will be used in the
324 	 * parent_names[] arrays for clock registration.
325 	 */
326 	const char *real_parent_names[ARRAY_SIZE(cprman_parent_names)];
327 
328 	/* Must be last */
329 	struct clk_hw_onecell_data onecell;
330 };
331 
332 static inline void cprman_write(struct bcm2835_cprman *cprman, u32 reg, u32 val)
333 {
334 	writel(CM_PASSWORD | val, cprman->regs + reg);
335 }
336 
337 static inline u32 cprman_read(struct bcm2835_cprman *cprman, u32 reg)
338 {
339 	return readl(cprman->regs + reg);
340 }
341 
342 /* Does a cycle of measuring a clock through the TCNT clock, which may
343  * source from many other clocks in the system.
344  */
345 static unsigned long bcm2835_measure_tcnt_mux(struct bcm2835_cprman *cprman,
346 					      u32 tcnt_mux)
347 {
348 	u32 osccount = 19200; /* 1ms */
349 	u32 count;
350 	ktime_t timeout;
351 
352 	spin_lock(&cprman->regs_lock);
353 
354 	cprman_write(cprman, CM_TCNTCTL, CM_KILL);
355 
356 	cprman_write(cprman, CM_TCNTCTL,
357 		     (tcnt_mux & CM_SRC_MASK) |
358 		     (tcnt_mux >> CM_SRC_BITS) << CM_TCNT_SRC1_SHIFT);
359 
360 	cprman_write(cprman, CM_OSCCOUNT, osccount);
361 
362 	/* do a kind delay at the start */
363 	mdelay(1);
364 
365 	/* Finish off whatever is left of OSCCOUNT */
366 	timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
367 	while (cprman_read(cprman, CM_OSCCOUNT)) {
368 		if (ktime_after(ktime_get(), timeout)) {
369 			dev_err(cprman->dev, "timeout waiting for OSCCOUNT\n");
370 			count = 0;
371 			goto out;
372 		}
373 		cpu_relax();
374 	}
375 
376 	/* Wait for BUSY to clear. */
377 	timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
378 	while (cprman_read(cprman, CM_TCNTCTL) & CM_BUSY) {
379 		if (ktime_after(ktime_get(), timeout)) {
380 			dev_err(cprman->dev, "timeout waiting for !BUSY\n");
381 			count = 0;
382 			goto out;
383 		}
384 		cpu_relax();
385 	}
386 
387 	count = cprman_read(cprman, CM_TCNTCNT);
388 
389 	cprman_write(cprman, CM_TCNTCTL, 0);
390 
391 out:
392 	spin_unlock(&cprman->regs_lock);
393 
394 	return count * 1000;
395 }
396 
397 static int bcm2835_debugfs_regset(struct bcm2835_cprman *cprman, u32 base,
398 				  struct debugfs_reg32 *regs, size_t nregs,
399 				  struct dentry *dentry)
400 {
401 	struct dentry *regdump;
402 	struct debugfs_regset32 *regset;
403 
404 	regset = devm_kzalloc(cprman->dev, sizeof(*regset), GFP_KERNEL);
405 	if (!regset)
406 		return -ENOMEM;
407 
408 	regset->regs = regs;
409 	regset->nregs = nregs;
410 	regset->base = cprman->regs + base;
411 
412 	regdump = debugfs_create_regset32("regdump", S_IRUGO, dentry,
413 					  regset);
414 
415 	return regdump ? 0 : -ENOMEM;
416 }
417 
418 struct bcm2835_pll_data {
419 	const char *name;
420 	u32 cm_ctrl_reg;
421 	u32 a2w_ctrl_reg;
422 	u32 frac_reg;
423 	u32 ana_reg_base;
424 	u32 reference_enable_mask;
425 	/* Bit in CM_LOCK to indicate when the PLL has locked. */
426 	u32 lock_mask;
427 
428 	const struct bcm2835_pll_ana_bits *ana;
429 
430 	unsigned long min_rate;
431 	unsigned long max_rate;
432 	/*
433 	 * Highest rate for the VCO before we have to use the
434 	 * pre-divide-by-2.
435 	 */
436 	unsigned long max_fb_rate;
437 };
438 
439 struct bcm2835_pll_ana_bits {
440 	u32 mask0;
441 	u32 set0;
442 	u32 mask1;
443 	u32 set1;
444 	u32 mask3;
445 	u32 set3;
446 	u32 fb_prediv_mask;
447 };
448 
449 static const struct bcm2835_pll_ana_bits bcm2835_ana_default = {
450 	.mask0 = 0,
451 	.set0 = 0,
452 	.mask1 = A2W_PLL_KI_MASK | A2W_PLL_KP_MASK,
453 	.set1 = (2 << A2W_PLL_KI_SHIFT) | (8 << A2W_PLL_KP_SHIFT),
454 	.mask3 = A2W_PLL_KA_MASK,
455 	.set3 = (2 << A2W_PLL_KA_SHIFT),
456 	.fb_prediv_mask = BIT(14),
457 };
458 
459 static const struct bcm2835_pll_ana_bits bcm2835_ana_pllh = {
460 	.mask0 = A2W_PLLH_KA_MASK | A2W_PLLH_KI_LOW_MASK,
461 	.set0 = (2 << A2W_PLLH_KA_SHIFT) | (2 << A2W_PLLH_KI_LOW_SHIFT),
462 	.mask1 = A2W_PLLH_KI_HIGH_MASK | A2W_PLLH_KP_MASK,
463 	.set1 = (6 << A2W_PLLH_KP_SHIFT),
464 	.mask3 = 0,
465 	.set3 = 0,
466 	.fb_prediv_mask = BIT(11),
467 };
468 
469 struct bcm2835_pll_divider_data {
470 	const char *name;
471 	const char *source_pll;
472 
473 	u32 cm_reg;
474 	u32 a2w_reg;
475 
476 	u32 load_mask;
477 	u32 hold_mask;
478 	u32 fixed_divider;
479 	u32 flags;
480 };
481 
482 struct bcm2835_clock_data {
483 	const char *name;
484 
485 	const char *const *parents;
486 	int num_mux_parents;
487 
488 	/* Bitmap encoding which parents accept rate change propagation. */
489 	unsigned int set_rate_parent;
490 
491 	u32 ctl_reg;
492 	u32 div_reg;
493 
494 	/* Number of integer bits in the divider */
495 	u32 int_bits;
496 	/* Number of fractional bits in the divider */
497 	u32 frac_bits;
498 
499 	u32 flags;
500 
501 	bool is_vpu_clock;
502 	bool is_mash_clock;
503 	bool low_jitter;
504 
505 	u32 tcnt_mux;
506 };
507 
508 struct bcm2835_gate_data {
509 	const char *name;
510 	const char *parent;
511 
512 	u32 ctl_reg;
513 };
514 
515 struct bcm2835_pll {
516 	struct clk_hw hw;
517 	struct bcm2835_cprman *cprman;
518 	const struct bcm2835_pll_data *data;
519 };
520 
521 static int bcm2835_pll_is_on(struct clk_hw *hw)
522 {
523 	struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
524 	struct bcm2835_cprman *cprman = pll->cprman;
525 	const struct bcm2835_pll_data *data = pll->data;
526 
527 	return cprman_read(cprman, data->a2w_ctrl_reg) &
528 		A2W_PLL_CTRL_PRST_DISABLE;
529 }
530 
531 static void bcm2835_pll_choose_ndiv_and_fdiv(unsigned long rate,
532 					     unsigned long parent_rate,
533 					     u32 *ndiv, u32 *fdiv)
534 {
535 	u64 div;
536 
537 	div = (u64)rate << A2W_PLL_FRAC_BITS;
538 	do_div(div, parent_rate);
539 
540 	*ndiv = div >> A2W_PLL_FRAC_BITS;
541 	*fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
542 }
543 
544 static long bcm2835_pll_rate_from_divisors(unsigned long parent_rate,
545 					   u32 ndiv, u32 fdiv, u32 pdiv)
546 {
547 	u64 rate;
548 
549 	if (pdiv == 0)
550 		return 0;
551 
552 	rate = (u64)parent_rate * ((ndiv << A2W_PLL_FRAC_BITS) + fdiv);
553 	do_div(rate, pdiv);
554 	return rate >> A2W_PLL_FRAC_BITS;
555 }
556 
557 static long bcm2835_pll_round_rate(struct clk_hw *hw, unsigned long rate,
558 				   unsigned long *parent_rate)
559 {
560 	struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
561 	const struct bcm2835_pll_data *data = pll->data;
562 	u32 ndiv, fdiv;
563 
564 	rate = clamp(rate, data->min_rate, data->max_rate);
565 
566 	bcm2835_pll_choose_ndiv_and_fdiv(rate, *parent_rate, &ndiv, &fdiv);
567 
568 	return bcm2835_pll_rate_from_divisors(*parent_rate, ndiv, fdiv, 1);
569 }
570 
571 static unsigned long bcm2835_pll_get_rate(struct clk_hw *hw,
572 					  unsigned long parent_rate)
573 {
574 	struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
575 	struct bcm2835_cprman *cprman = pll->cprman;
576 	const struct bcm2835_pll_data *data = pll->data;
577 	u32 a2wctrl = cprman_read(cprman, data->a2w_ctrl_reg);
578 	u32 ndiv, pdiv, fdiv;
579 	bool using_prediv;
580 
581 	if (parent_rate == 0)
582 		return 0;
583 
584 	fdiv = cprman_read(cprman, data->frac_reg) & A2W_PLL_FRAC_MASK;
585 	ndiv = (a2wctrl & A2W_PLL_CTRL_NDIV_MASK) >> A2W_PLL_CTRL_NDIV_SHIFT;
586 	pdiv = (a2wctrl & A2W_PLL_CTRL_PDIV_MASK) >> A2W_PLL_CTRL_PDIV_SHIFT;
587 	using_prediv = cprman_read(cprman, data->ana_reg_base + 4) &
588 		data->ana->fb_prediv_mask;
589 
590 	if (using_prediv) {
591 		ndiv *= 2;
592 		fdiv *= 2;
593 	}
594 
595 	return bcm2835_pll_rate_from_divisors(parent_rate, ndiv, fdiv, pdiv);
596 }
597 
598 static void bcm2835_pll_off(struct clk_hw *hw)
599 {
600 	struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
601 	struct bcm2835_cprman *cprman = pll->cprman;
602 	const struct bcm2835_pll_data *data = pll->data;
603 
604 	spin_lock(&cprman->regs_lock);
605 	cprman_write(cprman, data->cm_ctrl_reg,
606 		     cprman_read(cprman, data->cm_ctrl_reg) |
607 		     CM_PLL_ANARST);
608 	cprman_write(cprman, data->a2w_ctrl_reg,
609 		     cprman_read(cprman, data->a2w_ctrl_reg) |
610 		     A2W_PLL_CTRL_PWRDN);
611 	spin_unlock(&cprman->regs_lock);
612 }
613 
614 static int bcm2835_pll_on(struct clk_hw *hw)
615 {
616 	struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
617 	struct bcm2835_cprman *cprman = pll->cprman;
618 	const struct bcm2835_pll_data *data = pll->data;
619 	ktime_t timeout;
620 
621 	cprman_write(cprman, data->a2w_ctrl_reg,
622 		     cprman_read(cprman, data->a2w_ctrl_reg) &
623 		     ~A2W_PLL_CTRL_PWRDN);
624 
625 	/* Take the PLL out of reset. */
626 	spin_lock(&cprman->regs_lock);
627 	cprman_write(cprman, data->cm_ctrl_reg,
628 		     cprman_read(cprman, data->cm_ctrl_reg) & ~CM_PLL_ANARST);
629 	spin_unlock(&cprman->regs_lock);
630 
631 	/* Wait for the PLL to lock. */
632 	timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
633 	while (!(cprman_read(cprman, CM_LOCK) & data->lock_mask)) {
634 		if (ktime_after(ktime_get(), timeout)) {
635 			dev_err(cprman->dev, "%s: couldn't lock PLL\n",
636 				clk_hw_get_name(hw));
637 			return -ETIMEDOUT;
638 		}
639 
640 		cpu_relax();
641 	}
642 
643 	return 0;
644 }
645 
646 static void
647 bcm2835_pll_write_ana(struct bcm2835_cprman *cprman, u32 ana_reg_base, u32 *ana)
648 {
649 	int i;
650 
651 	/*
652 	 * ANA register setup is done as a series of writes to
653 	 * ANA3-ANA0, in that order.  This lets us write all 4
654 	 * registers as a single cycle of the serdes interface (taking
655 	 * 100 xosc clocks), whereas if we were to update ana0, 1, and
656 	 * 3 individually through their partial-write registers, each
657 	 * would be their own serdes cycle.
658 	 */
659 	for (i = 3; i >= 0; i--)
660 		cprman_write(cprman, ana_reg_base + i * 4, ana[i]);
661 }
662 
663 static int bcm2835_pll_set_rate(struct clk_hw *hw,
664 				unsigned long rate, unsigned long parent_rate)
665 {
666 	struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
667 	struct bcm2835_cprman *cprman = pll->cprman;
668 	const struct bcm2835_pll_data *data = pll->data;
669 	bool was_using_prediv, use_fb_prediv, do_ana_setup_first;
670 	u32 ndiv, fdiv, a2w_ctl;
671 	u32 ana[4];
672 	int i;
673 
674 	if (rate > data->max_fb_rate) {
675 		use_fb_prediv = true;
676 		rate /= 2;
677 	} else {
678 		use_fb_prediv = false;
679 	}
680 
681 	bcm2835_pll_choose_ndiv_and_fdiv(rate, parent_rate, &ndiv, &fdiv);
682 
683 	for (i = 3; i >= 0; i--)
684 		ana[i] = cprman_read(cprman, data->ana_reg_base + i * 4);
685 
686 	was_using_prediv = ana[1] & data->ana->fb_prediv_mask;
687 
688 	ana[0] &= ~data->ana->mask0;
689 	ana[0] |= data->ana->set0;
690 	ana[1] &= ~data->ana->mask1;
691 	ana[1] |= data->ana->set1;
692 	ana[3] &= ~data->ana->mask3;
693 	ana[3] |= data->ana->set3;
694 
695 	if (was_using_prediv && !use_fb_prediv) {
696 		ana[1] &= ~data->ana->fb_prediv_mask;
697 		do_ana_setup_first = true;
698 	} else if (!was_using_prediv && use_fb_prediv) {
699 		ana[1] |= data->ana->fb_prediv_mask;
700 		do_ana_setup_first = false;
701 	} else {
702 		do_ana_setup_first = true;
703 	}
704 
705 	/* Unmask the reference clock from the oscillator. */
706 	spin_lock(&cprman->regs_lock);
707 	cprman_write(cprman, A2W_XOSC_CTRL,
708 		     cprman_read(cprman, A2W_XOSC_CTRL) |
709 		     data->reference_enable_mask);
710 	spin_unlock(&cprman->regs_lock);
711 
712 	if (do_ana_setup_first)
713 		bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
714 
715 	/* Set the PLL multiplier from the oscillator. */
716 	cprman_write(cprman, data->frac_reg, fdiv);
717 
718 	a2w_ctl = cprman_read(cprman, data->a2w_ctrl_reg);
719 	a2w_ctl &= ~A2W_PLL_CTRL_NDIV_MASK;
720 	a2w_ctl |= ndiv << A2W_PLL_CTRL_NDIV_SHIFT;
721 	a2w_ctl &= ~A2W_PLL_CTRL_PDIV_MASK;
722 	a2w_ctl |= 1 << A2W_PLL_CTRL_PDIV_SHIFT;
723 	cprman_write(cprman, data->a2w_ctrl_reg, a2w_ctl);
724 
725 	if (!do_ana_setup_first)
726 		bcm2835_pll_write_ana(cprman, data->ana_reg_base, ana);
727 
728 	return 0;
729 }
730 
731 static int bcm2835_pll_debug_init(struct clk_hw *hw,
732 				  struct dentry *dentry)
733 {
734 	struct bcm2835_pll *pll = container_of(hw, struct bcm2835_pll, hw);
735 	struct bcm2835_cprman *cprman = pll->cprman;
736 	const struct bcm2835_pll_data *data = pll->data;
737 	struct debugfs_reg32 *regs;
738 
739 	regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
740 	if (!regs)
741 		return -ENOMEM;
742 
743 	regs[0].name = "cm_ctrl";
744 	regs[0].offset = data->cm_ctrl_reg;
745 	regs[1].name = "a2w_ctrl";
746 	regs[1].offset = data->a2w_ctrl_reg;
747 	regs[2].name = "frac";
748 	regs[2].offset = data->frac_reg;
749 	regs[3].name = "ana0";
750 	regs[3].offset = data->ana_reg_base + 0 * 4;
751 	regs[4].name = "ana1";
752 	regs[4].offset = data->ana_reg_base + 1 * 4;
753 	regs[5].name = "ana2";
754 	regs[5].offset = data->ana_reg_base + 2 * 4;
755 	regs[6].name = "ana3";
756 	regs[6].offset = data->ana_reg_base + 3 * 4;
757 
758 	return bcm2835_debugfs_regset(cprman, 0, regs, 7, dentry);
759 }
760 
761 static const struct clk_ops bcm2835_pll_clk_ops = {
762 	.is_prepared = bcm2835_pll_is_on,
763 	.prepare = bcm2835_pll_on,
764 	.unprepare = bcm2835_pll_off,
765 	.recalc_rate = bcm2835_pll_get_rate,
766 	.set_rate = bcm2835_pll_set_rate,
767 	.round_rate = bcm2835_pll_round_rate,
768 	.debug_init = bcm2835_pll_debug_init,
769 };
770 
771 struct bcm2835_pll_divider {
772 	struct clk_divider div;
773 	struct bcm2835_cprman *cprman;
774 	const struct bcm2835_pll_divider_data *data;
775 };
776 
777 static struct bcm2835_pll_divider *
778 bcm2835_pll_divider_from_hw(struct clk_hw *hw)
779 {
780 	return container_of(hw, struct bcm2835_pll_divider, div.hw);
781 }
782 
783 static int bcm2835_pll_divider_is_on(struct clk_hw *hw)
784 {
785 	struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
786 	struct bcm2835_cprman *cprman = divider->cprman;
787 	const struct bcm2835_pll_divider_data *data = divider->data;
788 
789 	return !(cprman_read(cprman, data->a2w_reg) & A2W_PLL_CHANNEL_DISABLE);
790 }
791 
792 static long bcm2835_pll_divider_round_rate(struct clk_hw *hw,
793 					   unsigned long rate,
794 					   unsigned long *parent_rate)
795 {
796 	return clk_divider_ops.round_rate(hw, rate, parent_rate);
797 }
798 
799 static unsigned long bcm2835_pll_divider_get_rate(struct clk_hw *hw,
800 						  unsigned long parent_rate)
801 {
802 	return clk_divider_ops.recalc_rate(hw, parent_rate);
803 }
804 
805 static void bcm2835_pll_divider_off(struct clk_hw *hw)
806 {
807 	struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
808 	struct bcm2835_cprman *cprman = divider->cprman;
809 	const struct bcm2835_pll_divider_data *data = divider->data;
810 
811 	spin_lock(&cprman->regs_lock);
812 	cprman_write(cprman, data->cm_reg,
813 		     (cprman_read(cprman, data->cm_reg) &
814 		      ~data->load_mask) | data->hold_mask);
815 	cprman_write(cprman, data->a2w_reg,
816 		     cprman_read(cprman, data->a2w_reg) |
817 		     A2W_PLL_CHANNEL_DISABLE);
818 	spin_unlock(&cprman->regs_lock);
819 }
820 
821 static int bcm2835_pll_divider_on(struct clk_hw *hw)
822 {
823 	struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
824 	struct bcm2835_cprman *cprman = divider->cprman;
825 	const struct bcm2835_pll_divider_data *data = divider->data;
826 
827 	spin_lock(&cprman->regs_lock);
828 	cprman_write(cprman, data->a2w_reg,
829 		     cprman_read(cprman, data->a2w_reg) &
830 		     ~A2W_PLL_CHANNEL_DISABLE);
831 
832 	cprman_write(cprman, data->cm_reg,
833 		     cprman_read(cprman, data->cm_reg) & ~data->hold_mask);
834 	spin_unlock(&cprman->regs_lock);
835 
836 	return 0;
837 }
838 
839 static int bcm2835_pll_divider_set_rate(struct clk_hw *hw,
840 					unsigned long rate,
841 					unsigned long parent_rate)
842 {
843 	struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
844 	struct bcm2835_cprman *cprman = divider->cprman;
845 	const struct bcm2835_pll_divider_data *data = divider->data;
846 	u32 cm, div, max_div = 1 << A2W_PLL_DIV_BITS;
847 
848 	div = DIV_ROUND_UP_ULL(parent_rate, rate);
849 
850 	div = min(div, max_div);
851 	if (div == max_div)
852 		div = 0;
853 
854 	cprman_write(cprman, data->a2w_reg, div);
855 	cm = cprman_read(cprman, data->cm_reg);
856 	cprman_write(cprman, data->cm_reg, cm | data->load_mask);
857 	cprman_write(cprman, data->cm_reg, cm & ~data->load_mask);
858 
859 	return 0;
860 }
861 
862 static int bcm2835_pll_divider_debug_init(struct clk_hw *hw,
863 					  struct dentry *dentry)
864 {
865 	struct bcm2835_pll_divider *divider = bcm2835_pll_divider_from_hw(hw);
866 	struct bcm2835_cprman *cprman = divider->cprman;
867 	const struct bcm2835_pll_divider_data *data = divider->data;
868 	struct debugfs_reg32 *regs;
869 
870 	regs = devm_kzalloc(cprman->dev, 7 * sizeof(*regs), GFP_KERNEL);
871 	if (!regs)
872 		return -ENOMEM;
873 
874 	regs[0].name = "cm";
875 	regs[0].offset = data->cm_reg;
876 	regs[1].name = "a2w";
877 	regs[1].offset = data->a2w_reg;
878 
879 	return bcm2835_debugfs_regset(cprman, 0, regs, 2, dentry);
880 }
881 
882 static const struct clk_ops bcm2835_pll_divider_clk_ops = {
883 	.is_prepared = bcm2835_pll_divider_is_on,
884 	.prepare = bcm2835_pll_divider_on,
885 	.unprepare = bcm2835_pll_divider_off,
886 	.recalc_rate = bcm2835_pll_divider_get_rate,
887 	.set_rate = bcm2835_pll_divider_set_rate,
888 	.round_rate = bcm2835_pll_divider_round_rate,
889 	.debug_init = bcm2835_pll_divider_debug_init,
890 };
891 
892 /*
893  * The CM dividers do fixed-point division, so we can't use the
894  * generic integer divider code like the PLL dividers do (and we can't
895  * fake it by having some fixed shifts preceding it in the clock tree,
896  * because we'd run out of bits in a 32-bit unsigned long).
897  */
898 struct bcm2835_clock {
899 	struct clk_hw hw;
900 	struct bcm2835_cprman *cprman;
901 	const struct bcm2835_clock_data *data;
902 };
903 
904 static struct bcm2835_clock *bcm2835_clock_from_hw(struct clk_hw *hw)
905 {
906 	return container_of(hw, struct bcm2835_clock, hw);
907 }
908 
909 static int bcm2835_clock_is_on(struct clk_hw *hw)
910 {
911 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
912 	struct bcm2835_cprman *cprman = clock->cprman;
913 	const struct bcm2835_clock_data *data = clock->data;
914 
915 	return (cprman_read(cprman, data->ctl_reg) & CM_ENABLE) != 0;
916 }
917 
918 static u32 bcm2835_clock_choose_div(struct clk_hw *hw,
919 				    unsigned long rate,
920 				    unsigned long parent_rate,
921 				    bool round_up)
922 {
923 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
924 	const struct bcm2835_clock_data *data = clock->data;
925 	u32 unused_frac_mask =
926 		GENMASK(CM_DIV_FRAC_BITS - data->frac_bits, 0) >> 1;
927 	u64 temp = (u64)parent_rate << CM_DIV_FRAC_BITS;
928 	u64 rem;
929 	u32 div, mindiv, maxdiv;
930 
931 	rem = do_div(temp, rate);
932 	div = temp;
933 
934 	/* Round up and mask off the unused bits */
935 	if (round_up && ((div & unused_frac_mask) != 0 || rem != 0))
936 		div += unused_frac_mask + 1;
937 	div &= ~unused_frac_mask;
938 
939 	/* different clamping limits apply for a mash clock */
940 	if (data->is_mash_clock) {
941 		/* clamp to min divider of 2 */
942 		mindiv = 2 << CM_DIV_FRAC_BITS;
943 		/* clamp to the highest possible integer divider */
944 		maxdiv = (BIT(data->int_bits) - 1) << CM_DIV_FRAC_BITS;
945 	} else {
946 		/* clamp to min divider of 1 */
947 		mindiv = 1 << CM_DIV_FRAC_BITS;
948 		/* clamp to the highest possible fractional divider */
949 		maxdiv = GENMASK(data->int_bits + CM_DIV_FRAC_BITS - 1,
950 				 CM_DIV_FRAC_BITS - data->frac_bits);
951 	}
952 
953 	/* apply the clamping  limits */
954 	div = max_t(u32, div, mindiv);
955 	div = min_t(u32, div, maxdiv);
956 
957 	return div;
958 }
959 
960 static long bcm2835_clock_rate_from_divisor(struct bcm2835_clock *clock,
961 					    unsigned long parent_rate,
962 					    u32 div)
963 {
964 	const struct bcm2835_clock_data *data = clock->data;
965 	u64 temp;
966 
967 	if (data->int_bits == 0 && data->frac_bits == 0)
968 		return parent_rate;
969 
970 	/*
971 	 * The divisor is a 12.12 fixed point field, but only some of
972 	 * the bits are populated in any given clock.
973 	 */
974 	div >>= CM_DIV_FRAC_BITS - data->frac_bits;
975 	div &= (1 << (data->int_bits + data->frac_bits)) - 1;
976 
977 	if (div == 0)
978 		return 0;
979 
980 	temp = (u64)parent_rate << data->frac_bits;
981 
982 	do_div(temp, div);
983 
984 	return temp;
985 }
986 
987 static unsigned long bcm2835_clock_get_rate(struct clk_hw *hw,
988 					    unsigned long parent_rate)
989 {
990 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
991 	struct bcm2835_cprman *cprman = clock->cprman;
992 	const struct bcm2835_clock_data *data = clock->data;
993 	u32 div;
994 
995 	if (data->int_bits == 0 && data->frac_bits == 0)
996 		return parent_rate;
997 
998 	div = cprman_read(cprman, data->div_reg);
999 
1000 	return bcm2835_clock_rate_from_divisor(clock, parent_rate, div);
1001 }
1002 
1003 static void bcm2835_clock_wait_busy(struct bcm2835_clock *clock)
1004 {
1005 	struct bcm2835_cprman *cprman = clock->cprman;
1006 	const struct bcm2835_clock_data *data = clock->data;
1007 	ktime_t timeout = ktime_add_ns(ktime_get(), LOCK_TIMEOUT_NS);
1008 
1009 	while (cprman_read(cprman, data->ctl_reg) & CM_BUSY) {
1010 		if (ktime_after(ktime_get(), timeout)) {
1011 			dev_err(cprman->dev, "%s: couldn't lock PLL\n",
1012 				clk_hw_get_name(&clock->hw));
1013 			return;
1014 		}
1015 		cpu_relax();
1016 	}
1017 }
1018 
1019 static void bcm2835_clock_off(struct clk_hw *hw)
1020 {
1021 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1022 	struct bcm2835_cprman *cprman = clock->cprman;
1023 	const struct bcm2835_clock_data *data = clock->data;
1024 
1025 	spin_lock(&cprman->regs_lock);
1026 	cprman_write(cprman, data->ctl_reg,
1027 		     cprman_read(cprman, data->ctl_reg) & ~CM_ENABLE);
1028 	spin_unlock(&cprman->regs_lock);
1029 
1030 	/* BUSY will remain high until the divider completes its cycle. */
1031 	bcm2835_clock_wait_busy(clock);
1032 }
1033 
1034 static int bcm2835_clock_on(struct clk_hw *hw)
1035 {
1036 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1037 	struct bcm2835_cprman *cprman = clock->cprman;
1038 	const struct bcm2835_clock_data *data = clock->data;
1039 
1040 	spin_lock(&cprman->regs_lock);
1041 	cprman_write(cprman, data->ctl_reg,
1042 		     cprman_read(cprman, data->ctl_reg) |
1043 		     CM_ENABLE |
1044 		     CM_GATE);
1045 	spin_unlock(&cprman->regs_lock);
1046 
1047 	/* Debug code to measure the clock once it's turned on to see
1048 	 * if it's ticking at the rate we expect.
1049 	 */
1050 	if (data->tcnt_mux && false) {
1051 		dev_info(cprman->dev,
1052 			 "clk %s: rate %ld, measure %ld\n",
1053 			 data->name,
1054 			 clk_hw_get_rate(hw),
1055 			 bcm2835_measure_tcnt_mux(cprman, data->tcnt_mux));
1056 	}
1057 
1058 	return 0;
1059 }
1060 
1061 static int bcm2835_clock_set_rate(struct clk_hw *hw,
1062 				  unsigned long rate, unsigned long parent_rate)
1063 {
1064 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1065 	struct bcm2835_cprman *cprman = clock->cprman;
1066 	const struct bcm2835_clock_data *data = clock->data;
1067 	u32 div = bcm2835_clock_choose_div(hw, rate, parent_rate, false);
1068 	u32 ctl;
1069 
1070 	spin_lock(&cprman->regs_lock);
1071 
1072 	/*
1073 	 * Setting up frac support
1074 	 *
1075 	 * In principle it is recommended to stop/start the clock first,
1076 	 * but as we set CLK_SET_RATE_GATE during registration of the
1077 	 * clock this requirement should be take care of by the
1078 	 * clk-framework.
1079 	 */
1080 	ctl = cprman_read(cprman, data->ctl_reg) & ~CM_FRAC;
1081 	ctl |= (div & CM_DIV_FRAC_MASK) ? CM_FRAC : 0;
1082 	cprman_write(cprman, data->ctl_reg, ctl);
1083 
1084 	cprman_write(cprman, data->div_reg, div);
1085 
1086 	spin_unlock(&cprman->regs_lock);
1087 
1088 	return 0;
1089 }
1090 
1091 static bool
1092 bcm2835_clk_is_pllc(struct clk_hw *hw)
1093 {
1094 	if (!hw)
1095 		return false;
1096 
1097 	return strncmp(clk_hw_get_name(hw), "pllc", 4) == 0;
1098 }
1099 
1100 static unsigned long bcm2835_clock_choose_div_and_prate(struct clk_hw *hw,
1101 							int parent_idx,
1102 							unsigned long rate,
1103 							u32 *div,
1104 							unsigned long *prate,
1105 							unsigned long *avgrate)
1106 {
1107 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1108 	struct bcm2835_cprman *cprman = clock->cprman;
1109 	const struct bcm2835_clock_data *data = clock->data;
1110 	unsigned long best_rate = 0;
1111 	u32 curdiv, mindiv, maxdiv;
1112 	struct clk_hw *parent;
1113 
1114 	parent = clk_hw_get_parent_by_index(hw, parent_idx);
1115 
1116 	if (!(BIT(parent_idx) & data->set_rate_parent)) {
1117 		*prate = clk_hw_get_rate(parent);
1118 		*div = bcm2835_clock_choose_div(hw, rate, *prate, true);
1119 
1120 		*avgrate = bcm2835_clock_rate_from_divisor(clock, *prate, *div);
1121 
1122 		if (data->low_jitter && (*div & CM_DIV_FRAC_MASK)) {
1123 			unsigned long high, low;
1124 			u32 int_div = *div & ~CM_DIV_FRAC_MASK;
1125 
1126 			high = bcm2835_clock_rate_from_divisor(clock, *prate,
1127 							       int_div);
1128 			int_div += CM_DIV_FRAC_MASK + 1;
1129 			low = bcm2835_clock_rate_from_divisor(clock, *prate,
1130 							      int_div);
1131 
1132 			/*
1133 			 * Return a value which is the maximum deviation
1134 			 * below the ideal rate, for use as a metric.
1135 			 */
1136 			return *avgrate - max(*avgrate - low, high - *avgrate);
1137 		}
1138 		return *avgrate;
1139 	}
1140 
1141 	if (data->frac_bits)
1142 		dev_warn(cprman->dev,
1143 			"frac bits are not used when propagating rate change");
1144 
1145 	/* clamp to min divider of 2 if we're dealing with a mash clock */
1146 	mindiv = data->is_mash_clock ? 2 : 1;
1147 	maxdiv = BIT(data->int_bits) - 1;
1148 
1149 	/* TODO: Be smart, and only test a subset of the available divisors. */
1150 	for (curdiv = mindiv; curdiv <= maxdiv; curdiv++) {
1151 		unsigned long tmp_rate;
1152 
1153 		tmp_rate = clk_hw_round_rate(parent, rate * curdiv);
1154 		tmp_rate /= curdiv;
1155 		if (curdiv == mindiv ||
1156 		    (tmp_rate > best_rate && tmp_rate <= rate))
1157 			best_rate = tmp_rate;
1158 
1159 		if (best_rate == rate)
1160 			break;
1161 	}
1162 
1163 	*div = curdiv << CM_DIV_FRAC_BITS;
1164 	*prate = curdiv * best_rate;
1165 	*avgrate = best_rate;
1166 
1167 	return best_rate;
1168 }
1169 
1170 static int bcm2835_clock_determine_rate(struct clk_hw *hw,
1171 					struct clk_rate_request *req)
1172 {
1173 	struct clk_hw *parent, *best_parent = NULL;
1174 	bool current_parent_is_pllc;
1175 	unsigned long rate, best_rate = 0;
1176 	unsigned long prate, best_prate = 0;
1177 	unsigned long avgrate, best_avgrate = 0;
1178 	size_t i;
1179 	u32 div;
1180 
1181 	current_parent_is_pllc = bcm2835_clk_is_pllc(clk_hw_get_parent(hw));
1182 
1183 	/*
1184 	 * Select parent clock that results in the closest but lower rate
1185 	 */
1186 	for (i = 0; i < clk_hw_get_num_parents(hw); ++i) {
1187 		parent = clk_hw_get_parent_by_index(hw, i);
1188 		if (!parent)
1189 			continue;
1190 
1191 		/*
1192 		 * Don't choose a PLLC-derived clock as our parent
1193 		 * unless it had been manually set that way.  PLLC's
1194 		 * frequency gets adjusted by the firmware due to
1195 		 * over-temp or under-voltage conditions, without
1196 		 * prior notification to our clock consumer.
1197 		 */
1198 		if (bcm2835_clk_is_pllc(parent) && !current_parent_is_pllc)
1199 			continue;
1200 
1201 		rate = bcm2835_clock_choose_div_and_prate(hw, i, req->rate,
1202 							  &div, &prate,
1203 							  &avgrate);
1204 		if (rate > best_rate && rate <= req->rate) {
1205 			best_parent = parent;
1206 			best_prate = prate;
1207 			best_rate = rate;
1208 			best_avgrate = avgrate;
1209 		}
1210 	}
1211 
1212 	if (!best_parent)
1213 		return -EINVAL;
1214 
1215 	req->best_parent_hw = best_parent;
1216 	req->best_parent_rate = best_prate;
1217 
1218 	req->rate = best_avgrate;
1219 
1220 	return 0;
1221 }
1222 
1223 static int bcm2835_clock_set_parent(struct clk_hw *hw, u8 index)
1224 {
1225 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1226 	struct bcm2835_cprman *cprman = clock->cprman;
1227 	const struct bcm2835_clock_data *data = clock->data;
1228 	u8 src = (index << CM_SRC_SHIFT) & CM_SRC_MASK;
1229 
1230 	cprman_write(cprman, data->ctl_reg, src);
1231 	return 0;
1232 }
1233 
1234 static u8 bcm2835_clock_get_parent(struct clk_hw *hw)
1235 {
1236 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1237 	struct bcm2835_cprman *cprman = clock->cprman;
1238 	const struct bcm2835_clock_data *data = clock->data;
1239 	u32 src = cprman_read(cprman, data->ctl_reg);
1240 
1241 	return (src & CM_SRC_MASK) >> CM_SRC_SHIFT;
1242 }
1243 
1244 static struct debugfs_reg32 bcm2835_debugfs_clock_reg32[] = {
1245 	{
1246 		.name = "ctl",
1247 		.offset = 0,
1248 	},
1249 	{
1250 		.name = "div",
1251 		.offset = 4,
1252 	},
1253 };
1254 
1255 static int bcm2835_clock_debug_init(struct clk_hw *hw,
1256 				    struct dentry *dentry)
1257 {
1258 	struct bcm2835_clock *clock = bcm2835_clock_from_hw(hw);
1259 	struct bcm2835_cprman *cprman = clock->cprman;
1260 	const struct bcm2835_clock_data *data = clock->data;
1261 
1262 	return bcm2835_debugfs_regset(
1263 		cprman, data->ctl_reg,
1264 		bcm2835_debugfs_clock_reg32,
1265 		ARRAY_SIZE(bcm2835_debugfs_clock_reg32),
1266 		dentry);
1267 }
1268 
1269 static const struct clk_ops bcm2835_clock_clk_ops = {
1270 	.is_prepared = bcm2835_clock_is_on,
1271 	.prepare = bcm2835_clock_on,
1272 	.unprepare = bcm2835_clock_off,
1273 	.recalc_rate = bcm2835_clock_get_rate,
1274 	.set_rate = bcm2835_clock_set_rate,
1275 	.determine_rate = bcm2835_clock_determine_rate,
1276 	.set_parent = bcm2835_clock_set_parent,
1277 	.get_parent = bcm2835_clock_get_parent,
1278 	.debug_init = bcm2835_clock_debug_init,
1279 };
1280 
1281 static int bcm2835_vpu_clock_is_on(struct clk_hw *hw)
1282 {
1283 	return true;
1284 }
1285 
1286 /*
1287  * The VPU clock can never be disabled (it doesn't have an ENABLE
1288  * bit), so it gets its own set of clock ops.
1289  */
1290 static const struct clk_ops bcm2835_vpu_clock_clk_ops = {
1291 	.is_prepared = bcm2835_vpu_clock_is_on,
1292 	.recalc_rate = bcm2835_clock_get_rate,
1293 	.set_rate = bcm2835_clock_set_rate,
1294 	.determine_rate = bcm2835_clock_determine_rate,
1295 	.set_parent = bcm2835_clock_set_parent,
1296 	.get_parent = bcm2835_clock_get_parent,
1297 	.debug_init = bcm2835_clock_debug_init,
1298 };
1299 
1300 static struct clk_hw *bcm2835_register_pll(struct bcm2835_cprman *cprman,
1301 					   const struct bcm2835_pll_data *data)
1302 {
1303 	struct bcm2835_pll *pll;
1304 	struct clk_init_data init;
1305 	int ret;
1306 
1307 	memset(&init, 0, sizeof(init));
1308 
1309 	/* All of the PLLs derive from the external oscillator. */
1310 	init.parent_names = &cprman->real_parent_names[0];
1311 	init.num_parents = 1;
1312 	init.name = data->name;
1313 	init.ops = &bcm2835_pll_clk_ops;
1314 	init.flags = CLK_IGNORE_UNUSED;
1315 
1316 	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
1317 	if (!pll)
1318 		return NULL;
1319 
1320 	pll->cprman = cprman;
1321 	pll->data = data;
1322 	pll->hw.init = &init;
1323 
1324 	ret = devm_clk_hw_register(cprman->dev, &pll->hw);
1325 	if (ret)
1326 		return NULL;
1327 	return &pll->hw;
1328 }
1329 
1330 static struct clk_hw *
1331 bcm2835_register_pll_divider(struct bcm2835_cprman *cprman,
1332 			     const struct bcm2835_pll_divider_data *data)
1333 {
1334 	struct bcm2835_pll_divider *divider;
1335 	struct clk_init_data init;
1336 	const char *divider_name;
1337 	int ret;
1338 
1339 	if (data->fixed_divider != 1) {
1340 		divider_name = devm_kasprintf(cprman->dev, GFP_KERNEL,
1341 					      "%s_prediv", data->name);
1342 		if (!divider_name)
1343 			return NULL;
1344 	} else {
1345 		divider_name = data->name;
1346 	}
1347 
1348 	memset(&init, 0, sizeof(init));
1349 
1350 	init.parent_names = &data->source_pll;
1351 	init.num_parents = 1;
1352 	init.name = divider_name;
1353 	init.ops = &bcm2835_pll_divider_clk_ops;
1354 	init.flags = data->flags | CLK_IGNORE_UNUSED;
1355 
1356 	divider = devm_kzalloc(cprman->dev, sizeof(*divider), GFP_KERNEL);
1357 	if (!divider)
1358 		return NULL;
1359 
1360 	divider->div.reg = cprman->regs + data->a2w_reg;
1361 	divider->div.shift = A2W_PLL_DIV_SHIFT;
1362 	divider->div.width = A2W_PLL_DIV_BITS;
1363 	divider->div.flags = CLK_DIVIDER_MAX_AT_ZERO;
1364 	divider->div.lock = &cprman->regs_lock;
1365 	divider->div.hw.init = &init;
1366 	divider->div.table = NULL;
1367 
1368 	divider->cprman = cprman;
1369 	divider->data = data;
1370 
1371 	ret = devm_clk_hw_register(cprman->dev, &divider->div.hw);
1372 	if (ret)
1373 		return ERR_PTR(ret);
1374 
1375 	/*
1376 	 * PLLH's channels have a fixed divide by 10 afterwards, which
1377 	 * is what our consumers are actually using.
1378 	 */
1379 	if (data->fixed_divider != 1) {
1380 		return clk_hw_register_fixed_factor(cprman->dev, data->name,
1381 						    divider_name,
1382 						    CLK_SET_RATE_PARENT,
1383 						    1,
1384 						    data->fixed_divider);
1385 	}
1386 
1387 	return &divider->div.hw;
1388 }
1389 
1390 static struct clk_hw *bcm2835_register_clock(struct bcm2835_cprman *cprman,
1391 					  const struct bcm2835_clock_data *data)
1392 {
1393 	struct bcm2835_clock *clock;
1394 	struct clk_init_data init;
1395 	const char *parents[1 << CM_SRC_BITS];
1396 	size_t i, j;
1397 	int ret;
1398 
1399 	/*
1400 	 * Replace our strings referencing parent clocks with the
1401 	 * actual clock-output-name of the parent.
1402 	 */
1403 	for (i = 0; i < data->num_mux_parents; i++) {
1404 		parents[i] = data->parents[i];
1405 
1406 		for (j = 0; j < ARRAY_SIZE(cprman_parent_names); j++) {
1407 			if (strcmp(parents[i], cprman_parent_names[j]) == 0) {
1408 				parents[i] = cprman->real_parent_names[j];
1409 				break;
1410 			}
1411 		}
1412 	}
1413 
1414 	memset(&init, 0, sizeof(init));
1415 	init.parent_names = parents;
1416 	init.num_parents = data->num_mux_parents;
1417 	init.name = data->name;
1418 	init.flags = data->flags | CLK_IGNORE_UNUSED;
1419 
1420 	/*
1421 	 * Pass the CLK_SET_RATE_PARENT flag if we are allowed to propagate
1422 	 * rate changes on at least of the parents.
1423 	 */
1424 	if (data->set_rate_parent)
1425 		init.flags |= CLK_SET_RATE_PARENT;
1426 
1427 	if (data->is_vpu_clock) {
1428 		init.ops = &bcm2835_vpu_clock_clk_ops;
1429 	} else {
1430 		init.ops = &bcm2835_clock_clk_ops;
1431 		init.flags |= CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
1432 
1433 		/* If the clock wasn't actually enabled at boot, it's not
1434 		 * critical.
1435 		 */
1436 		if (!(cprman_read(cprman, data->ctl_reg) & CM_ENABLE))
1437 			init.flags &= ~CLK_IS_CRITICAL;
1438 	}
1439 
1440 	clock = devm_kzalloc(cprman->dev, sizeof(*clock), GFP_KERNEL);
1441 	if (!clock)
1442 		return NULL;
1443 
1444 	clock->cprman = cprman;
1445 	clock->data = data;
1446 	clock->hw.init = &init;
1447 
1448 	ret = devm_clk_hw_register(cprman->dev, &clock->hw);
1449 	if (ret)
1450 		return ERR_PTR(ret);
1451 	return &clock->hw;
1452 }
1453 
1454 static struct clk *bcm2835_register_gate(struct bcm2835_cprman *cprman,
1455 					 const struct bcm2835_gate_data *data)
1456 {
1457 	return clk_register_gate(cprman->dev, data->name, data->parent,
1458 				 CLK_IGNORE_UNUSED | CLK_SET_RATE_GATE,
1459 				 cprman->regs + data->ctl_reg,
1460 				 CM_GATE_BIT, 0, &cprman->regs_lock);
1461 }
1462 
1463 typedef struct clk_hw *(*bcm2835_clk_register)(struct bcm2835_cprman *cprman,
1464 					       const void *data);
1465 struct bcm2835_clk_desc {
1466 	bcm2835_clk_register clk_register;
1467 	const void *data;
1468 };
1469 
1470 /* assignment helper macros for different clock types */
1471 #define _REGISTER(f, ...) { .clk_register = (bcm2835_clk_register)f, \
1472 			    .data = __VA_ARGS__ }
1473 #define REGISTER_PLL(...)	_REGISTER(&bcm2835_register_pll,	\
1474 					  &(struct bcm2835_pll_data)	\
1475 					  {__VA_ARGS__})
1476 #define REGISTER_PLL_DIV(...)	_REGISTER(&bcm2835_register_pll_divider, \
1477 					  &(struct bcm2835_pll_divider_data) \
1478 					  {__VA_ARGS__})
1479 #define REGISTER_CLK(...)	_REGISTER(&bcm2835_register_clock,	\
1480 					  &(struct bcm2835_clock_data)	\
1481 					  {__VA_ARGS__})
1482 #define REGISTER_GATE(...)	_REGISTER(&bcm2835_register_gate,	\
1483 					  &(struct bcm2835_gate_data)	\
1484 					  {__VA_ARGS__})
1485 
1486 /* parent mux arrays plus helper macros */
1487 
1488 /* main oscillator parent mux */
1489 static const char *const bcm2835_clock_osc_parents[] = {
1490 	"gnd",
1491 	"xosc",
1492 	"testdebug0",
1493 	"testdebug1"
1494 };
1495 
1496 #define REGISTER_OSC_CLK(...)	REGISTER_CLK(				\
1497 	.num_mux_parents = ARRAY_SIZE(bcm2835_clock_osc_parents),	\
1498 	.parents = bcm2835_clock_osc_parents,				\
1499 	__VA_ARGS__)
1500 
1501 /* main peripherial parent mux */
1502 static const char *const bcm2835_clock_per_parents[] = {
1503 	"gnd",
1504 	"xosc",
1505 	"testdebug0",
1506 	"testdebug1",
1507 	"plla_per",
1508 	"pllc_per",
1509 	"plld_per",
1510 	"pllh_aux",
1511 };
1512 
1513 #define REGISTER_PER_CLK(...)	REGISTER_CLK(				\
1514 	.num_mux_parents = ARRAY_SIZE(bcm2835_clock_per_parents),	\
1515 	.parents = bcm2835_clock_per_parents,				\
1516 	__VA_ARGS__)
1517 
1518 /*
1519  * Restrict clock sources for the PCM peripheral to the oscillator and
1520  * PLLD_PER because other source may have varying rates or be switched
1521  * off.
1522  *
1523  * Prevent other sources from being selected by replacing their names in
1524  * the list of potential parents with dummy entries (entry index is
1525  * significant).
1526  */
1527 static const char *const bcm2835_pcm_per_parents[] = {
1528 	"-",
1529 	"xosc",
1530 	"-",
1531 	"-",
1532 	"-",
1533 	"-",
1534 	"plld_per",
1535 	"-",
1536 };
1537 
1538 #define REGISTER_PCM_CLK(...)	REGISTER_CLK(				\
1539 	.num_mux_parents = ARRAY_SIZE(bcm2835_pcm_per_parents),		\
1540 	.parents = bcm2835_pcm_per_parents,				\
1541 	__VA_ARGS__)
1542 
1543 /* main vpu parent mux */
1544 static const char *const bcm2835_clock_vpu_parents[] = {
1545 	"gnd",
1546 	"xosc",
1547 	"testdebug0",
1548 	"testdebug1",
1549 	"plla_core",
1550 	"pllc_core0",
1551 	"plld_core",
1552 	"pllh_aux",
1553 	"pllc_core1",
1554 	"pllc_core2",
1555 };
1556 
1557 #define REGISTER_VPU_CLK(...)	REGISTER_CLK(				\
1558 	.num_mux_parents = ARRAY_SIZE(bcm2835_clock_vpu_parents),	\
1559 	.parents = bcm2835_clock_vpu_parents,				\
1560 	__VA_ARGS__)
1561 
1562 /*
1563  * DSI parent clocks.  The DSI byte/DDR/DDR2 clocks come from the DSI
1564  * analog PHY.  The _inv variants are generated internally to cprman,
1565  * but we don't use them so they aren't hooked up.
1566  */
1567 static const char *const bcm2835_clock_dsi0_parents[] = {
1568 	"gnd",
1569 	"xosc",
1570 	"testdebug0",
1571 	"testdebug1",
1572 	"dsi0_ddr",
1573 	"dsi0_ddr_inv",
1574 	"dsi0_ddr2",
1575 	"dsi0_ddr2_inv",
1576 	"dsi0_byte",
1577 	"dsi0_byte_inv",
1578 };
1579 
1580 static const char *const bcm2835_clock_dsi1_parents[] = {
1581 	"gnd",
1582 	"xosc",
1583 	"testdebug0",
1584 	"testdebug1",
1585 	"dsi1_ddr",
1586 	"dsi1_ddr_inv",
1587 	"dsi1_ddr2",
1588 	"dsi1_ddr2_inv",
1589 	"dsi1_byte",
1590 	"dsi1_byte_inv",
1591 };
1592 
1593 #define REGISTER_DSI0_CLK(...)	REGISTER_CLK(				\
1594 	.num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi0_parents),	\
1595 	.parents = bcm2835_clock_dsi0_parents,				\
1596 	__VA_ARGS__)
1597 
1598 #define REGISTER_DSI1_CLK(...)	REGISTER_CLK(				\
1599 	.num_mux_parents = ARRAY_SIZE(bcm2835_clock_dsi1_parents),	\
1600 	.parents = bcm2835_clock_dsi1_parents,				\
1601 	__VA_ARGS__)
1602 
1603 /*
1604  * the real definition of all the pll, pll_dividers and clocks
1605  * these make use of the above REGISTER_* macros
1606  */
1607 static const struct bcm2835_clk_desc clk_desc_array[] = {
1608 	/* the PLL + PLL dividers */
1609 
1610 	/*
1611 	 * PLLA is the auxiliary PLL, used to drive the CCP2
1612 	 * (Compact Camera Port 2) transmitter clock.
1613 	 *
1614 	 * It is in the PX LDO power domain, which is on when the
1615 	 * AUDIO domain is on.
1616 	 */
1617 	[BCM2835_PLLA]		= REGISTER_PLL(
1618 		.name = "plla",
1619 		.cm_ctrl_reg = CM_PLLA,
1620 		.a2w_ctrl_reg = A2W_PLLA_CTRL,
1621 		.frac_reg = A2W_PLLA_FRAC,
1622 		.ana_reg_base = A2W_PLLA_ANA0,
1623 		.reference_enable_mask = A2W_XOSC_CTRL_PLLA_ENABLE,
1624 		.lock_mask = CM_LOCK_FLOCKA,
1625 
1626 		.ana = &bcm2835_ana_default,
1627 
1628 		.min_rate = 600000000u,
1629 		.max_rate = 2400000000u,
1630 		.max_fb_rate = BCM2835_MAX_FB_RATE),
1631 	[BCM2835_PLLA_CORE]	= REGISTER_PLL_DIV(
1632 		.name = "plla_core",
1633 		.source_pll = "plla",
1634 		.cm_reg = CM_PLLA,
1635 		.a2w_reg = A2W_PLLA_CORE,
1636 		.load_mask = CM_PLLA_LOADCORE,
1637 		.hold_mask = CM_PLLA_HOLDCORE,
1638 		.fixed_divider = 1,
1639 		.flags = CLK_SET_RATE_PARENT),
1640 	[BCM2835_PLLA_PER]	= REGISTER_PLL_DIV(
1641 		.name = "plla_per",
1642 		.source_pll = "plla",
1643 		.cm_reg = CM_PLLA,
1644 		.a2w_reg = A2W_PLLA_PER,
1645 		.load_mask = CM_PLLA_LOADPER,
1646 		.hold_mask = CM_PLLA_HOLDPER,
1647 		.fixed_divider = 1,
1648 		.flags = CLK_SET_RATE_PARENT),
1649 	[BCM2835_PLLA_DSI0]	= REGISTER_PLL_DIV(
1650 		.name = "plla_dsi0",
1651 		.source_pll = "plla",
1652 		.cm_reg = CM_PLLA,
1653 		.a2w_reg = A2W_PLLA_DSI0,
1654 		.load_mask = CM_PLLA_LOADDSI0,
1655 		.hold_mask = CM_PLLA_HOLDDSI0,
1656 		.fixed_divider = 1),
1657 	[BCM2835_PLLA_CCP2]	= REGISTER_PLL_DIV(
1658 		.name = "plla_ccp2",
1659 		.source_pll = "plla",
1660 		.cm_reg = CM_PLLA,
1661 		.a2w_reg = A2W_PLLA_CCP2,
1662 		.load_mask = CM_PLLA_LOADCCP2,
1663 		.hold_mask = CM_PLLA_HOLDCCP2,
1664 		.fixed_divider = 1,
1665 		.flags = CLK_SET_RATE_PARENT),
1666 
1667 	/* PLLB is used for the ARM's clock. */
1668 	[BCM2835_PLLB]		= REGISTER_PLL(
1669 		.name = "pllb",
1670 		.cm_ctrl_reg = CM_PLLB,
1671 		.a2w_ctrl_reg = A2W_PLLB_CTRL,
1672 		.frac_reg = A2W_PLLB_FRAC,
1673 		.ana_reg_base = A2W_PLLB_ANA0,
1674 		.reference_enable_mask = A2W_XOSC_CTRL_PLLB_ENABLE,
1675 		.lock_mask = CM_LOCK_FLOCKB,
1676 
1677 		.ana = &bcm2835_ana_default,
1678 
1679 		.min_rate = 600000000u,
1680 		.max_rate = 3000000000u,
1681 		.max_fb_rate = BCM2835_MAX_FB_RATE),
1682 	[BCM2835_PLLB_ARM]	= REGISTER_PLL_DIV(
1683 		.name = "pllb_arm",
1684 		.source_pll = "pllb",
1685 		.cm_reg = CM_PLLB,
1686 		.a2w_reg = A2W_PLLB_ARM,
1687 		.load_mask = CM_PLLB_LOADARM,
1688 		.hold_mask = CM_PLLB_HOLDARM,
1689 		.fixed_divider = 1,
1690 		.flags = CLK_SET_RATE_PARENT),
1691 
1692 	/*
1693 	 * PLLC is the core PLL, used to drive the core VPU clock.
1694 	 *
1695 	 * It is in the PX LDO power domain, which is on when the
1696 	 * AUDIO domain is on.
1697 	 */
1698 	[BCM2835_PLLC]		= REGISTER_PLL(
1699 		.name = "pllc",
1700 		.cm_ctrl_reg = CM_PLLC,
1701 		.a2w_ctrl_reg = A2W_PLLC_CTRL,
1702 		.frac_reg = A2W_PLLC_FRAC,
1703 		.ana_reg_base = A2W_PLLC_ANA0,
1704 		.reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1705 		.lock_mask = CM_LOCK_FLOCKC,
1706 
1707 		.ana = &bcm2835_ana_default,
1708 
1709 		.min_rate = 600000000u,
1710 		.max_rate = 3000000000u,
1711 		.max_fb_rate = BCM2835_MAX_FB_RATE),
1712 	[BCM2835_PLLC_CORE0]	= REGISTER_PLL_DIV(
1713 		.name = "pllc_core0",
1714 		.source_pll = "pllc",
1715 		.cm_reg = CM_PLLC,
1716 		.a2w_reg = A2W_PLLC_CORE0,
1717 		.load_mask = CM_PLLC_LOADCORE0,
1718 		.hold_mask = CM_PLLC_HOLDCORE0,
1719 		.fixed_divider = 1,
1720 		.flags = CLK_SET_RATE_PARENT),
1721 	[BCM2835_PLLC_CORE1]	= REGISTER_PLL_DIV(
1722 		.name = "pllc_core1",
1723 		.source_pll = "pllc",
1724 		.cm_reg = CM_PLLC,
1725 		.a2w_reg = A2W_PLLC_CORE1,
1726 		.load_mask = CM_PLLC_LOADCORE1,
1727 		.hold_mask = CM_PLLC_HOLDCORE1,
1728 		.fixed_divider = 1,
1729 		.flags = CLK_SET_RATE_PARENT),
1730 	[BCM2835_PLLC_CORE2]	= REGISTER_PLL_DIV(
1731 		.name = "pllc_core2",
1732 		.source_pll = "pllc",
1733 		.cm_reg = CM_PLLC,
1734 		.a2w_reg = A2W_PLLC_CORE2,
1735 		.load_mask = CM_PLLC_LOADCORE2,
1736 		.hold_mask = CM_PLLC_HOLDCORE2,
1737 		.fixed_divider = 1,
1738 		.flags = CLK_SET_RATE_PARENT),
1739 	[BCM2835_PLLC_PER]	= REGISTER_PLL_DIV(
1740 		.name = "pllc_per",
1741 		.source_pll = "pllc",
1742 		.cm_reg = CM_PLLC,
1743 		.a2w_reg = A2W_PLLC_PER,
1744 		.load_mask = CM_PLLC_LOADPER,
1745 		.hold_mask = CM_PLLC_HOLDPER,
1746 		.fixed_divider = 1,
1747 		.flags = CLK_SET_RATE_PARENT),
1748 
1749 	/*
1750 	 * PLLD is the display PLL, used to drive DSI display panels.
1751 	 *
1752 	 * It is in the PX LDO power domain, which is on when the
1753 	 * AUDIO domain is on.
1754 	 */
1755 	[BCM2835_PLLD]		= REGISTER_PLL(
1756 		.name = "plld",
1757 		.cm_ctrl_reg = CM_PLLD,
1758 		.a2w_ctrl_reg = A2W_PLLD_CTRL,
1759 		.frac_reg = A2W_PLLD_FRAC,
1760 		.ana_reg_base = A2W_PLLD_ANA0,
1761 		.reference_enable_mask = A2W_XOSC_CTRL_DDR_ENABLE,
1762 		.lock_mask = CM_LOCK_FLOCKD,
1763 
1764 		.ana = &bcm2835_ana_default,
1765 
1766 		.min_rate = 600000000u,
1767 		.max_rate = 2400000000u,
1768 		.max_fb_rate = BCM2835_MAX_FB_RATE),
1769 	[BCM2835_PLLD_CORE]	= REGISTER_PLL_DIV(
1770 		.name = "plld_core",
1771 		.source_pll = "plld",
1772 		.cm_reg = CM_PLLD,
1773 		.a2w_reg = A2W_PLLD_CORE,
1774 		.load_mask = CM_PLLD_LOADCORE,
1775 		.hold_mask = CM_PLLD_HOLDCORE,
1776 		.fixed_divider = 1,
1777 		.flags = CLK_SET_RATE_PARENT),
1778 	[BCM2835_PLLD_PER]	= REGISTER_PLL_DIV(
1779 		.name = "plld_per",
1780 		.source_pll = "plld",
1781 		.cm_reg = CM_PLLD,
1782 		.a2w_reg = A2W_PLLD_PER,
1783 		.load_mask = CM_PLLD_LOADPER,
1784 		.hold_mask = CM_PLLD_HOLDPER,
1785 		.fixed_divider = 1,
1786 		.flags = CLK_SET_RATE_PARENT),
1787 	[BCM2835_PLLD_DSI0]	= REGISTER_PLL_DIV(
1788 		.name = "plld_dsi0",
1789 		.source_pll = "plld",
1790 		.cm_reg = CM_PLLD,
1791 		.a2w_reg = A2W_PLLD_DSI0,
1792 		.load_mask = CM_PLLD_LOADDSI0,
1793 		.hold_mask = CM_PLLD_HOLDDSI0,
1794 		.fixed_divider = 1),
1795 	[BCM2835_PLLD_DSI1]	= REGISTER_PLL_DIV(
1796 		.name = "plld_dsi1",
1797 		.source_pll = "plld",
1798 		.cm_reg = CM_PLLD,
1799 		.a2w_reg = A2W_PLLD_DSI1,
1800 		.load_mask = CM_PLLD_LOADDSI1,
1801 		.hold_mask = CM_PLLD_HOLDDSI1,
1802 		.fixed_divider = 1),
1803 
1804 	/*
1805 	 * PLLH is used to supply the pixel clock or the AUX clock for the
1806 	 * TV encoder.
1807 	 *
1808 	 * It is in the HDMI power domain.
1809 	 */
1810 	[BCM2835_PLLH]		= REGISTER_PLL(
1811 		"pllh",
1812 		.cm_ctrl_reg = CM_PLLH,
1813 		.a2w_ctrl_reg = A2W_PLLH_CTRL,
1814 		.frac_reg = A2W_PLLH_FRAC,
1815 		.ana_reg_base = A2W_PLLH_ANA0,
1816 		.reference_enable_mask = A2W_XOSC_CTRL_PLLC_ENABLE,
1817 		.lock_mask = CM_LOCK_FLOCKH,
1818 
1819 		.ana = &bcm2835_ana_pllh,
1820 
1821 		.min_rate = 600000000u,
1822 		.max_rate = 3000000000u,
1823 		.max_fb_rate = BCM2835_MAX_FB_RATE),
1824 	[BCM2835_PLLH_RCAL]	= REGISTER_PLL_DIV(
1825 		.name = "pllh_rcal",
1826 		.source_pll = "pllh",
1827 		.cm_reg = CM_PLLH,
1828 		.a2w_reg = A2W_PLLH_RCAL,
1829 		.load_mask = CM_PLLH_LOADRCAL,
1830 		.hold_mask = 0,
1831 		.fixed_divider = 10,
1832 		.flags = CLK_SET_RATE_PARENT),
1833 	[BCM2835_PLLH_AUX]	= REGISTER_PLL_DIV(
1834 		.name = "pllh_aux",
1835 		.source_pll = "pllh",
1836 		.cm_reg = CM_PLLH,
1837 		.a2w_reg = A2W_PLLH_AUX,
1838 		.load_mask = CM_PLLH_LOADAUX,
1839 		.hold_mask = 0,
1840 		.fixed_divider = 1,
1841 		.flags = CLK_SET_RATE_PARENT),
1842 	[BCM2835_PLLH_PIX]	= REGISTER_PLL_DIV(
1843 		.name = "pllh_pix",
1844 		.source_pll = "pllh",
1845 		.cm_reg = CM_PLLH,
1846 		.a2w_reg = A2W_PLLH_PIX,
1847 		.load_mask = CM_PLLH_LOADPIX,
1848 		.hold_mask = 0,
1849 		.fixed_divider = 10,
1850 		.flags = CLK_SET_RATE_PARENT),
1851 
1852 	/* the clocks */
1853 
1854 	/* clocks with oscillator parent mux */
1855 
1856 	/* One Time Programmable Memory clock.  Maximum 10Mhz. */
1857 	[BCM2835_CLOCK_OTP]	= REGISTER_OSC_CLK(
1858 		.name = "otp",
1859 		.ctl_reg = CM_OTPCTL,
1860 		.div_reg = CM_OTPDIV,
1861 		.int_bits = 4,
1862 		.frac_bits = 0,
1863 		.tcnt_mux = 6),
1864 	/*
1865 	 * Used for a 1Mhz clock for the system clocksource, and also used
1866 	 * bythe watchdog timer and the camera pulse generator.
1867 	 */
1868 	[BCM2835_CLOCK_TIMER]	= REGISTER_OSC_CLK(
1869 		.name = "timer",
1870 		.ctl_reg = CM_TIMERCTL,
1871 		.div_reg = CM_TIMERDIV,
1872 		.int_bits = 6,
1873 		.frac_bits = 12),
1874 	/*
1875 	 * Clock for the temperature sensor.
1876 	 * Generally run at 2Mhz, max 5Mhz.
1877 	 */
1878 	[BCM2835_CLOCK_TSENS]	= REGISTER_OSC_CLK(
1879 		.name = "tsens",
1880 		.ctl_reg = CM_TSENSCTL,
1881 		.div_reg = CM_TSENSDIV,
1882 		.int_bits = 5,
1883 		.frac_bits = 0),
1884 	[BCM2835_CLOCK_TEC]	= REGISTER_OSC_CLK(
1885 		.name = "tec",
1886 		.ctl_reg = CM_TECCTL,
1887 		.div_reg = CM_TECDIV,
1888 		.int_bits = 6,
1889 		.frac_bits = 0),
1890 
1891 	/* clocks with vpu parent mux */
1892 	[BCM2835_CLOCK_H264]	= REGISTER_VPU_CLK(
1893 		.name = "h264",
1894 		.ctl_reg = CM_H264CTL,
1895 		.div_reg = CM_H264DIV,
1896 		.int_bits = 4,
1897 		.frac_bits = 8,
1898 		.tcnt_mux = 1),
1899 	[BCM2835_CLOCK_ISP]	= REGISTER_VPU_CLK(
1900 		.name = "isp",
1901 		.ctl_reg = CM_ISPCTL,
1902 		.div_reg = CM_ISPDIV,
1903 		.int_bits = 4,
1904 		.frac_bits = 8,
1905 		.tcnt_mux = 2),
1906 
1907 	/*
1908 	 * Secondary SDRAM clock.  Used for low-voltage modes when the PLL
1909 	 * in the SDRAM controller can't be used.
1910 	 */
1911 	[BCM2835_CLOCK_SDRAM]	= REGISTER_VPU_CLK(
1912 		.name = "sdram",
1913 		.ctl_reg = CM_SDCCTL,
1914 		.div_reg = CM_SDCDIV,
1915 		.int_bits = 6,
1916 		.frac_bits = 0,
1917 		.tcnt_mux = 3),
1918 	[BCM2835_CLOCK_V3D]	= REGISTER_VPU_CLK(
1919 		.name = "v3d",
1920 		.ctl_reg = CM_V3DCTL,
1921 		.div_reg = CM_V3DDIV,
1922 		.int_bits = 4,
1923 		.frac_bits = 8,
1924 		.tcnt_mux = 4),
1925 	/*
1926 	 * VPU clock.  This doesn't have an enable bit, since it drives
1927 	 * the bus for everything else, and is special so it doesn't need
1928 	 * to be gated for rate changes.  It is also known as "clk_audio"
1929 	 * in various hardware documentation.
1930 	 */
1931 	[BCM2835_CLOCK_VPU]	= REGISTER_VPU_CLK(
1932 		.name = "vpu",
1933 		.ctl_reg = CM_VPUCTL,
1934 		.div_reg = CM_VPUDIV,
1935 		.int_bits = 12,
1936 		.frac_bits = 8,
1937 		.flags = CLK_IS_CRITICAL,
1938 		.is_vpu_clock = true,
1939 		.tcnt_mux = 5),
1940 
1941 	/* clocks with per parent mux */
1942 	[BCM2835_CLOCK_AVEO]	= REGISTER_PER_CLK(
1943 		.name = "aveo",
1944 		.ctl_reg = CM_AVEOCTL,
1945 		.div_reg = CM_AVEODIV,
1946 		.int_bits = 4,
1947 		.frac_bits = 0,
1948 		.tcnt_mux = 38),
1949 	[BCM2835_CLOCK_CAM0]	= REGISTER_PER_CLK(
1950 		.name = "cam0",
1951 		.ctl_reg = CM_CAM0CTL,
1952 		.div_reg = CM_CAM0DIV,
1953 		.int_bits = 4,
1954 		.frac_bits = 8,
1955 		.tcnt_mux = 14),
1956 	[BCM2835_CLOCK_CAM1]	= REGISTER_PER_CLK(
1957 		.name = "cam1",
1958 		.ctl_reg = CM_CAM1CTL,
1959 		.div_reg = CM_CAM1DIV,
1960 		.int_bits = 4,
1961 		.frac_bits = 8,
1962 		.tcnt_mux = 15),
1963 	[BCM2835_CLOCK_DFT]	= REGISTER_PER_CLK(
1964 		.name = "dft",
1965 		.ctl_reg = CM_DFTCTL,
1966 		.div_reg = CM_DFTDIV,
1967 		.int_bits = 5,
1968 		.frac_bits = 0),
1969 	[BCM2835_CLOCK_DPI]	= REGISTER_PER_CLK(
1970 		.name = "dpi",
1971 		.ctl_reg = CM_DPICTL,
1972 		.div_reg = CM_DPIDIV,
1973 		.int_bits = 4,
1974 		.frac_bits = 8,
1975 		.tcnt_mux = 17),
1976 
1977 	/* Arasan EMMC clock */
1978 	[BCM2835_CLOCK_EMMC]	= REGISTER_PER_CLK(
1979 		.name = "emmc",
1980 		.ctl_reg = CM_EMMCCTL,
1981 		.div_reg = CM_EMMCDIV,
1982 		.int_bits = 4,
1983 		.frac_bits = 8,
1984 		.tcnt_mux = 39),
1985 
1986 	/* General purpose (GPIO) clocks */
1987 	[BCM2835_CLOCK_GP0]	= REGISTER_PER_CLK(
1988 		.name = "gp0",
1989 		.ctl_reg = CM_GP0CTL,
1990 		.div_reg = CM_GP0DIV,
1991 		.int_bits = 12,
1992 		.frac_bits = 12,
1993 		.is_mash_clock = true,
1994 		.tcnt_mux = 20),
1995 	[BCM2835_CLOCK_GP1]	= REGISTER_PER_CLK(
1996 		.name = "gp1",
1997 		.ctl_reg = CM_GP1CTL,
1998 		.div_reg = CM_GP1DIV,
1999 		.int_bits = 12,
2000 		.frac_bits = 12,
2001 		.flags = CLK_IS_CRITICAL,
2002 		.is_mash_clock = true,
2003 		.tcnt_mux = 21),
2004 	[BCM2835_CLOCK_GP2]	= REGISTER_PER_CLK(
2005 		.name = "gp2",
2006 		.ctl_reg = CM_GP2CTL,
2007 		.div_reg = CM_GP2DIV,
2008 		.int_bits = 12,
2009 		.frac_bits = 12,
2010 		.flags = CLK_IS_CRITICAL),
2011 
2012 	/* HDMI state machine */
2013 	[BCM2835_CLOCK_HSM]	= REGISTER_PER_CLK(
2014 		.name = "hsm",
2015 		.ctl_reg = CM_HSMCTL,
2016 		.div_reg = CM_HSMDIV,
2017 		.int_bits = 4,
2018 		.frac_bits = 8,
2019 		.tcnt_mux = 22),
2020 	[BCM2835_CLOCK_PCM]	= REGISTER_PCM_CLK(
2021 		.name = "pcm",
2022 		.ctl_reg = CM_PCMCTL,
2023 		.div_reg = CM_PCMDIV,
2024 		.int_bits = 12,
2025 		.frac_bits = 12,
2026 		.is_mash_clock = true,
2027 		.low_jitter = true,
2028 		.tcnt_mux = 23),
2029 	[BCM2835_CLOCK_PWM]	= REGISTER_PER_CLK(
2030 		.name = "pwm",
2031 		.ctl_reg = CM_PWMCTL,
2032 		.div_reg = CM_PWMDIV,
2033 		.int_bits = 12,
2034 		.frac_bits = 12,
2035 		.is_mash_clock = true,
2036 		.tcnt_mux = 24),
2037 	[BCM2835_CLOCK_SLIM]	= REGISTER_PER_CLK(
2038 		.name = "slim",
2039 		.ctl_reg = CM_SLIMCTL,
2040 		.div_reg = CM_SLIMDIV,
2041 		.int_bits = 12,
2042 		.frac_bits = 12,
2043 		.is_mash_clock = true,
2044 		.tcnt_mux = 25),
2045 	[BCM2835_CLOCK_SMI]	= REGISTER_PER_CLK(
2046 		.name = "smi",
2047 		.ctl_reg = CM_SMICTL,
2048 		.div_reg = CM_SMIDIV,
2049 		.int_bits = 4,
2050 		.frac_bits = 8,
2051 		.tcnt_mux = 27),
2052 	[BCM2835_CLOCK_UART]	= REGISTER_PER_CLK(
2053 		.name = "uart",
2054 		.ctl_reg = CM_UARTCTL,
2055 		.div_reg = CM_UARTDIV,
2056 		.int_bits = 10,
2057 		.frac_bits = 12,
2058 		.tcnt_mux = 28),
2059 
2060 	/* TV encoder clock.  Only operating frequency is 108Mhz.  */
2061 	[BCM2835_CLOCK_VEC]	= REGISTER_PER_CLK(
2062 		.name = "vec",
2063 		.ctl_reg = CM_VECCTL,
2064 		.div_reg = CM_VECDIV,
2065 		.int_bits = 4,
2066 		.frac_bits = 0,
2067 		/*
2068 		 * Allow rate change propagation only on PLLH_AUX which is
2069 		 * assigned index 7 in the parent array.
2070 		 */
2071 		.set_rate_parent = BIT(7),
2072 		.tcnt_mux = 29),
2073 
2074 	/* dsi clocks */
2075 	[BCM2835_CLOCK_DSI0E]	= REGISTER_PER_CLK(
2076 		.name = "dsi0e",
2077 		.ctl_reg = CM_DSI0ECTL,
2078 		.div_reg = CM_DSI0EDIV,
2079 		.int_bits = 4,
2080 		.frac_bits = 8,
2081 		.tcnt_mux = 18),
2082 	[BCM2835_CLOCK_DSI1E]	= REGISTER_PER_CLK(
2083 		.name = "dsi1e",
2084 		.ctl_reg = CM_DSI1ECTL,
2085 		.div_reg = CM_DSI1EDIV,
2086 		.int_bits = 4,
2087 		.frac_bits = 8,
2088 		.tcnt_mux = 19),
2089 	[BCM2835_CLOCK_DSI0P]	= REGISTER_DSI0_CLK(
2090 		.name = "dsi0p",
2091 		.ctl_reg = CM_DSI0PCTL,
2092 		.div_reg = CM_DSI0PDIV,
2093 		.int_bits = 0,
2094 		.frac_bits = 0,
2095 		.tcnt_mux = 12),
2096 	[BCM2835_CLOCK_DSI1P]	= REGISTER_DSI1_CLK(
2097 		.name = "dsi1p",
2098 		.ctl_reg = CM_DSI1PCTL,
2099 		.div_reg = CM_DSI1PDIV,
2100 		.int_bits = 0,
2101 		.frac_bits = 0,
2102 		.tcnt_mux = 13),
2103 
2104 	/* the gates */
2105 
2106 	/*
2107 	 * CM_PERIICTL (and CM_PERIACTL, CM_SYSCTL and CM_VPUCTL if
2108 	 * you have the debug bit set in the power manager, which we
2109 	 * don't bother exposing) are individual gates off of the
2110 	 * non-stop vpu clock.
2111 	 */
2112 	[BCM2835_CLOCK_PERI_IMAGE] = REGISTER_GATE(
2113 		.name = "peri_image",
2114 		.parent = "vpu",
2115 		.ctl_reg = CM_PERIICTL),
2116 };
2117 
2118 /*
2119  * Permanently take a reference on the parent of the SDRAM clock.
2120  *
2121  * While the SDRAM is being driven by its dedicated PLL most of the
2122  * time, there is a little loop running in the firmware that
2123  * periodically switches the SDRAM to using our CM clock to do PVT
2124  * recalibration, with the assumption that the previously configured
2125  * SDRAM parent is still enabled and running.
2126  */
2127 static int bcm2835_mark_sdc_parent_critical(struct clk *sdc)
2128 {
2129 	struct clk *parent = clk_get_parent(sdc);
2130 
2131 	if (IS_ERR(parent))
2132 		return PTR_ERR(parent);
2133 
2134 	return clk_prepare_enable(parent);
2135 }
2136 
2137 static int bcm2835_clk_probe(struct platform_device *pdev)
2138 {
2139 	struct device *dev = &pdev->dev;
2140 	struct clk_hw **hws;
2141 	struct bcm2835_cprman *cprman;
2142 	struct resource *res;
2143 	const struct bcm2835_clk_desc *desc;
2144 	const size_t asize = ARRAY_SIZE(clk_desc_array);
2145 	size_t i;
2146 	int ret;
2147 
2148 	cprman = devm_kzalloc(dev, sizeof(*cprman) +
2149 			      sizeof(*cprman->onecell.hws) * asize,
2150 			      GFP_KERNEL);
2151 	if (!cprman)
2152 		return -ENOMEM;
2153 
2154 	spin_lock_init(&cprman->regs_lock);
2155 	cprman->dev = dev;
2156 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2157 	cprman->regs = devm_ioremap_resource(dev, res);
2158 	if (IS_ERR(cprman->regs))
2159 		return PTR_ERR(cprman->regs);
2160 
2161 	memcpy(cprman->real_parent_names, cprman_parent_names,
2162 	       sizeof(cprman_parent_names));
2163 	of_clk_parent_fill(dev->of_node, cprman->real_parent_names,
2164 			   ARRAY_SIZE(cprman_parent_names));
2165 
2166 	/*
2167 	 * Make sure the external oscillator has been registered.
2168 	 *
2169 	 * The other (DSI) clocks are not present on older device
2170 	 * trees, which we still need to support for backwards
2171 	 * compatibility.
2172 	 */
2173 	if (!cprman->real_parent_names[0])
2174 		return -ENODEV;
2175 
2176 	platform_set_drvdata(pdev, cprman);
2177 
2178 	cprman->onecell.num = asize;
2179 	hws = cprman->onecell.hws;
2180 
2181 	for (i = 0; i < asize; i++) {
2182 		desc = &clk_desc_array[i];
2183 		if (desc->clk_register && desc->data)
2184 			hws[i] = desc->clk_register(cprman, desc->data);
2185 	}
2186 
2187 	ret = bcm2835_mark_sdc_parent_critical(hws[BCM2835_CLOCK_SDRAM]->clk);
2188 	if (ret)
2189 		return ret;
2190 
2191 	return of_clk_add_hw_provider(dev->of_node, of_clk_hw_onecell_get,
2192 				      &cprman->onecell);
2193 }
2194 
2195 static const struct of_device_id bcm2835_clk_of_match[] = {
2196 	{ .compatible = "brcm,bcm2835-cprman", },
2197 	{}
2198 };
2199 MODULE_DEVICE_TABLE(of, bcm2835_clk_of_match);
2200 
2201 static struct platform_driver bcm2835_clk_driver = {
2202 	.driver = {
2203 		.name = "bcm2835-clk",
2204 		.of_match_table = bcm2835_clk_of_match,
2205 	},
2206 	.probe          = bcm2835_clk_probe,
2207 };
2208 
2209 builtin_platform_driver(bcm2835_clk_driver);
2210 
2211 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
2212 MODULE_DESCRIPTION("BCM2835 clock driver");
2213 MODULE_LICENSE("GPL v2");
2214