xref: /openbmc/linux/drivers/clk/renesas/rzg2l-cpg.c (revision d35ac6ac)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * RZ/G2L Clock Pulse Generator
4  *
5  * Copyright (C) 2021 Renesas Electronics Corp.
6  *
7  * Based on renesas-cpg-mssr.c
8  *
9  * Copyright (C) 2015 Glider bvba
10  * Copyright (C) 2013 Ideas On Board SPRL
11  * Copyright (C) 2015 Renesas Electronics Corp.
12  */
13 
14 #include <linux/clk.h>
15 #include <linux/clk-provider.h>
16 #include <linux/clk/renesas.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/iopoll.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_clock.h>
27 #include <linux/pm_domain.h>
28 #include <linux/reset-controller.h>
29 #include <linux/slab.h>
30 #include <linux/units.h>
31 
32 #include <dt-bindings/clock/renesas-cpg-mssr.h>
33 
34 #include "rzg2l-cpg.h"
35 
36 #ifdef DEBUG
37 #define WARN_DEBUG(x)	WARN_ON(x)
38 #else
39 #define WARN_DEBUG(x)	do { } while (0)
40 #endif
41 
42 #define DIV_RSMASK(v, s, m)	((v >> s) & m)
43 #define GET_SHIFT(val)		((val >> 12) & 0xff)
44 #define GET_WIDTH(val)		((val >> 8) & 0xf)
45 
46 #define KDIV(val)		DIV_RSMASK(val, 16, 0xffff)
47 #define MDIV(val)		DIV_RSMASK(val, 6, 0x3ff)
48 #define PDIV(val)		DIV_RSMASK(val, 0, 0x3f)
49 #define SDIV(val)		DIV_RSMASK(val, 0, 0x7)
50 
51 #define CLK_ON_R(reg)		(reg)
52 #define CLK_MON_R(reg)		(0x180 + (reg))
53 #define CLK_RST_R(reg)		(reg)
54 #define CLK_MRST_R(reg)		(0x180 + (reg))
55 
56 #define GET_REG_OFFSET(val)		((val >> 20) & 0xfff)
57 #define GET_REG_SAMPLL_CLK1(val)	((val >> 22) & 0xfff)
58 #define GET_REG_SAMPLL_CLK2(val)	((val >> 12) & 0xfff)
59 
60 #define MAX_VCLK_FREQ		(148500000)
61 
62 struct sd_hw_data {
63 	struct clk_hw hw;
64 	u32 conf;
65 	struct rzg2l_cpg_priv *priv;
66 };
67 
68 #define to_sd_hw_data(_hw)	container_of(_hw, struct sd_hw_data, hw)
69 
70 struct rzg2l_pll5_param {
71 	u32 pl5_fracin;
72 	u8 pl5_refdiv;
73 	u8 pl5_intin;
74 	u8 pl5_postdiv1;
75 	u8 pl5_postdiv2;
76 	u8 pl5_spread;
77 };
78 
79 struct rzg2l_pll5_mux_dsi_div_param {
80 	u8 clksrc;
81 	u8 dsi_div_a;
82 	u8 dsi_div_b;
83 };
84 
85 /**
86  * struct rzg2l_cpg_priv - Clock Pulse Generator Private Data
87  *
88  * @rcdev: Reset controller entity
89  * @dev: CPG device
90  * @base: CPG register block base address
91  * @rmw_lock: protects register accesses
92  * @clks: Array containing all Core and Module Clocks
93  * @num_core_clks: Number of Core Clocks in clks[]
94  * @num_mod_clks: Number of Module Clocks in clks[]
95  * @num_resets: Number of Module Resets in info->resets[]
96  * @last_dt_core_clk: ID of the last Core Clock exported to DT
97  * @info: Pointer to platform data
98  * @genpd: PM domain
99  * @mux_dsi_div_params: pll5 mux and dsi div parameters
100  */
101 struct rzg2l_cpg_priv {
102 	struct reset_controller_dev rcdev;
103 	struct device *dev;
104 	void __iomem *base;
105 	spinlock_t rmw_lock;
106 
107 	struct clk **clks;
108 	unsigned int num_core_clks;
109 	unsigned int num_mod_clks;
110 	unsigned int num_resets;
111 	unsigned int last_dt_core_clk;
112 
113 	const struct rzg2l_cpg_info *info;
114 
115 	struct generic_pm_domain genpd;
116 
117 	struct rzg2l_pll5_mux_dsi_div_param mux_dsi_div_params;
118 };
119 
120 static void rzg2l_cpg_del_clk_provider(void *data)
121 {
122 	of_clk_del_provider(data);
123 }
124 
125 static struct clk * __init
126 rzg2l_cpg_div_clk_register(const struct cpg_core_clk *core,
127 			   struct clk **clks,
128 			   void __iomem *base,
129 			   struct rzg2l_cpg_priv *priv)
130 {
131 	struct device *dev = priv->dev;
132 	const struct clk *parent;
133 	const char *parent_name;
134 	struct clk_hw *clk_hw;
135 
136 	parent = clks[core->parent & 0xffff];
137 	if (IS_ERR(parent))
138 		return ERR_CAST(parent);
139 
140 	parent_name = __clk_get_name(parent);
141 
142 	if (core->dtable)
143 		clk_hw = clk_hw_register_divider_table(dev, core->name,
144 						       parent_name, 0,
145 						       base + GET_REG_OFFSET(core->conf),
146 						       GET_SHIFT(core->conf),
147 						       GET_WIDTH(core->conf),
148 						       core->flag,
149 						       core->dtable,
150 						       &priv->rmw_lock);
151 	else
152 		clk_hw = clk_hw_register_divider(dev, core->name,
153 						 parent_name, 0,
154 						 base + GET_REG_OFFSET(core->conf),
155 						 GET_SHIFT(core->conf),
156 						 GET_WIDTH(core->conf),
157 						 core->flag, &priv->rmw_lock);
158 
159 	if (IS_ERR(clk_hw))
160 		return ERR_CAST(clk_hw);
161 
162 	return clk_hw->clk;
163 }
164 
165 static struct clk * __init
166 rzg2l_cpg_mux_clk_register(const struct cpg_core_clk *core,
167 			   void __iomem *base,
168 			   struct rzg2l_cpg_priv *priv)
169 {
170 	const struct clk_hw *clk_hw;
171 
172 	clk_hw = devm_clk_hw_register_mux(priv->dev, core->name,
173 					  core->parent_names, core->num_parents,
174 					  core->flag,
175 					  base + GET_REG_OFFSET(core->conf),
176 					  GET_SHIFT(core->conf),
177 					  GET_WIDTH(core->conf),
178 					  core->mux_flags, &priv->rmw_lock);
179 	if (IS_ERR(clk_hw))
180 		return ERR_CAST(clk_hw);
181 
182 	return clk_hw->clk;
183 }
184 
185 static int rzg2l_cpg_sd_clk_mux_determine_rate(struct clk_hw *hw,
186 					       struct clk_rate_request *req)
187 {
188 	return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
189 }
190 
191 static int rzg2l_cpg_sd_clk_mux_set_parent(struct clk_hw *hw, u8 index)
192 {
193 	struct sd_hw_data *hwdata = to_sd_hw_data(hw);
194 	struct rzg2l_cpg_priv *priv = hwdata->priv;
195 	u32 off = GET_REG_OFFSET(hwdata->conf);
196 	u32 shift = GET_SHIFT(hwdata->conf);
197 	const u32 clk_src_266 = 2;
198 	u32 bitmask;
199 
200 	/*
201 	 * As per the HW manual, we should not directly switch from 533 MHz to
202 	 * 400 MHz and vice versa. To change the setting from 2’b01 (533 MHz)
203 	 * to 2’b10 (400 MHz) or vice versa, Switch to 2’b11 (266 MHz) first,
204 	 * and then switch to the target setting (2’b01 (533 MHz) or 2’b10
205 	 * (400 MHz)).
206 	 * Setting a value of '0' to the SEL_SDHI0_SET or SEL_SDHI1_SET clock
207 	 * switching register is prohibited.
208 	 * The clock mux has 3 input clocks(533 MHz, 400 MHz, and 266 MHz), and
209 	 * the index to value mapping is done by adding 1 to the index.
210 	 */
211 	bitmask = (GENMASK(GET_WIDTH(hwdata->conf) - 1, 0) << shift) << 16;
212 	if (index != clk_src_266) {
213 		u32 msk, val;
214 		int ret;
215 
216 		writel(bitmask | ((clk_src_266 + 1) << shift), priv->base + off);
217 
218 		msk = off ? CPG_CLKSTATUS_SELSDHI1_STS : CPG_CLKSTATUS_SELSDHI0_STS;
219 
220 		ret = readl_poll_timeout(priv->base + CPG_CLKSTATUS, val,
221 					 !(val & msk), 100,
222 					 CPG_SDHI_CLK_SWITCH_STATUS_TIMEOUT_US);
223 		if (ret) {
224 			dev_err(priv->dev, "failed to switch clk source\n");
225 			return ret;
226 		}
227 	}
228 
229 	writel(bitmask | ((index + 1) << shift), priv->base + off);
230 
231 	return 0;
232 }
233 
234 static u8 rzg2l_cpg_sd_clk_mux_get_parent(struct clk_hw *hw)
235 {
236 	struct sd_hw_data *hwdata = to_sd_hw_data(hw);
237 	struct rzg2l_cpg_priv *priv = hwdata->priv;
238 	u32 val = readl(priv->base + GET_REG_OFFSET(hwdata->conf));
239 
240 	val >>= GET_SHIFT(hwdata->conf);
241 	val &= GENMASK(GET_WIDTH(hwdata->conf) - 1, 0);
242 	if (val) {
243 		val--;
244 	} else {
245 		/* Prohibited clk source, change it to 533 MHz(reset value) */
246 		rzg2l_cpg_sd_clk_mux_set_parent(hw, 0);
247 	}
248 
249 	return val;
250 }
251 
252 static const struct clk_ops rzg2l_cpg_sd_clk_mux_ops = {
253 	.determine_rate = rzg2l_cpg_sd_clk_mux_determine_rate,
254 	.set_parent	= rzg2l_cpg_sd_clk_mux_set_parent,
255 	.get_parent	= rzg2l_cpg_sd_clk_mux_get_parent,
256 };
257 
258 static struct clk * __init
259 rzg2l_cpg_sd_mux_clk_register(const struct cpg_core_clk *core,
260 			      void __iomem *base,
261 			      struct rzg2l_cpg_priv *priv)
262 {
263 	struct sd_hw_data *clk_hw_data;
264 	struct clk_init_data init;
265 	struct clk_hw *clk_hw;
266 	int ret;
267 
268 	clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL);
269 	if (!clk_hw_data)
270 		return ERR_PTR(-ENOMEM);
271 
272 	clk_hw_data->priv = priv;
273 	clk_hw_data->conf = core->conf;
274 
275 	init.name = GET_SHIFT(core->conf) ? "sd1" : "sd0";
276 	init.ops = &rzg2l_cpg_sd_clk_mux_ops;
277 	init.flags = 0;
278 	init.num_parents = core->num_parents;
279 	init.parent_names = core->parent_names;
280 
281 	clk_hw = &clk_hw_data->hw;
282 	clk_hw->init = &init;
283 
284 	ret = devm_clk_hw_register(priv->dev, clk_hw);
285 	if (ret)
286 		return ERR_PTR(ret);
287 
288 	return clk_hw->clk;
289 }
290 
291 static unsigned long
292 rzg2l_cpg_get_foutpostdiv_rate(struct rzg2l_pll5_param *params,
293 			       unsigned long rate)
294 {
295 	unsigned long foutpostdiv_rate;
296 
297 	params->pl5_intin = rate / MEGA;
298 	params->pl5_fracin = div_u64(((u64)rate % MEGA) << 24, MEGA);
299 	params->pl5_refdiv = 2;
300 	params->pl5_postdiv1 = 1;
301 	params->pl5_postdiv2 = 1;
302 	params->pl5_spread = 0x16;
303 
304 	foutpostdiv_rate =
305 		EXTAL_FREQ_IN_MEGA_HZ * MEGA / params->pl5_refdiv *
306 		((((params->pl5_intin << 24) + params->pl5_fracin)) >> 24) /
307 		(params->pl5_postdiv1 * params->pl5_postdiv2);
308 
309 	return foutpostdiv_rate;
310 }
311 
312 struct dsi_div_hw_data {
313 	struct clk_hw hw;
314 	u32 conf;
315 	unsigned long rate;
316 	struct rzg2l_cpg_priv *priv;
317 };
318 
319 #define to_dsi_div_hw_data(_hw)	container_of(_hw, struct dsi_div_hw_data, hw)
320 
321 static unsigned long rzg2l_cpg_dsi_div_recalc_rate(struct clk_hw *hw,
322 						   unsigned long parent_rate)
323 {
324 	struct dsi_div_hw_data *dsi_div = to_dsi_div_hw_data(hw);
325 	unsigned long rate = dsi_div->rate;
326 
327 	if (!rate)
328 		rate = parent_rate;
329 
330 	return rate;
331 }
332 
333 static unsigned long rzg2l_cpg_get_vclk_parent_rate(struct clk_hw *hw,
334 						    unsigned long rate)
335 {
336 	struct dsi_div_hw_data *dsi_div = to_dsi_div_hw_data(hw);
337 	struct rzg2l_cpg_priv *priv = dsi_div->priv;
338 	struct rzg2l_pll5_param params;
339 	unsigned long parent_rate;
340 
341 	parent_rate = rzg2l_cpg_get_foutpostdiv_rate(&params, rate);
342 
343 	if (priv->mux_dsi_div_params.clksrc)
344 		parent_rate /= 2;
345 
346 	return parent_rate;
347 }
348 
349 static int rzg2l_cpg_dsi_div_determine_rate(struct clk_hw *hw,
350 					    struct clk_rate_request *req)
351 {
352 	if (req->rate > MAX_VCLK_FREQ)
353 		req->rate = MAX_VCLK_FREQ;
354 
355 	req->best_parent_rate = rzg2l_cpg_get_vclk_parent_rate(hw, req->rate);
356 
357 	return 0;
358 }
359 
360 static int rzg2l_cpg_dsi_div_set_rate(struct clk_hw *hw,
361 				      unsigned long rate,
362 				      unsigned long parent_rate)
363 {
364 	struct dsi_div_hw_data *dsi_div = to_dsi_div_hw_data(hw);
365 	struct rzg2l_cpg_priv *priv = dsi_div->priv;
366 
367 	/*
368 	 * MUX -->DIV_DSI_{A,B} -->M3 -->VCLK
369 	 *
370 	 * Based on the dot clock, the DSI divider clock sets the divider value,
371 	 * calculates the pll parameters for generating FOUTPOSTDIV and the clk
372 	 * source for the MUX and propagates that info to the parents.
373 	 */
374 
375 	if (!rate || rate > MAX_VCLK_FREQ)
376 		return -EINVAL;
377 
378 	dsi_div->rate = rate;
379 	writel(CPG_PL5_SDIV_DIV_DSI_A_WEN | CPG_PL5_SDIV_DIV_DSI_B_WEN |
380 	       (priv->mux_dsi_div_params.dsi_div_a << 0) |
381 	       (priv->mux_dsi_div_params.dsi_div_b << 8),
382 	       priv->base + CPG_PL5_SDIV);
383 
384 	return 0;
385 }
386 
387 static const struct clk_ops rzg2l_cpg_dsi_div_ops = {
388 	.recalc_rate = rzg2l_cpg_dsi_div_recalc_rate,
389 	.determine_rate = rzg2l_cpg_dsi_div_determine_rate,
390 	.set_rate = rzg2l_cpg_dsi_div_set_rate,
391 };
392 
393 static struct clk * __init
394 rzg2l_cpg_dsi_div_clk_register(const struct cpg_core_clk *core,
395 			       struct clk **clks,
396 			       struct rzg2l_cpg_priv *priv)
397 {
398 	struct dsi_div_hw_data *clk_hw_data;
399 	const struct clk *parent;
400 	const char *parent_name;
401 	struct clk_init_data init;
402 	struct clk_hw *clk_hw;
403 	int ret;
404 
405 	parent = clks[core->parent & 0xffff];
406 	if (IS_ERR(parent))
407 		return ERR_CAST(parent);
408 
409 	clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL);
410 	if (!clk_hw_data)
411 		return ERR_PTR(-ENOMEM);
412 
413 	clk_hw_data->priv = priv;
414 
415 	parent_name = __clk_get_name(parent);
416 	init.name = core->name;
417 	init.ops = &rzg2l_cpg_dsi_div_ops;
418 	init.flags = CLK_SET_RATE_PARENT;
419 	init.parent_names = &parent_name;
420 	init.num_parents = 1;
421 
422 	clk_hw = &clk_hw_data->hw;
423 	clk_hw->init = &init;
424 
425 	ret = devm_clk_hw_register(priv->dev, clk_hw);
426 	if (ret)
427 		return ERR_PTR(ret);
428 
429 	return clk_hw->clk;
430 }
431 
432 struct pll5_mux_hw_data {
433 	struct clk_hw hw;
434 	u32 conf;
435 	unsigned long rate;
436 	struct rzg2l_cpg_priv *priv;
437 };
438 
439 #define to_pll5_mux_hw_data(_hw)	container_of(_hw, struct pll5_mux_hw_data, hw)
440 
441 static int rzg2l_cpg_pll5_4_clk_mux_determine_rate(struct clk_hw *hw,
442 						   struct clk_rate_request *req)
443 {
444 	struct clk_hw *parent;
445 	struct pll5_mux_hw_data *hwdata = to_pll5_mux_hw_data(hw);
446 	struct rzg2l_cpg_priv *priv = hwdata->priv;
447 
448 	parent = clk_hw_get_parent_by_index(hw, priv->mux_dsi_div_params.clksrc);
449 	req->best_parent_hw = parent;
450 	req->best_parent_rate = req->rate;
451 
452 	return 0;
453 }
454 
455 static int rzg2l_cpg_pll5_4_clk_mux_set_parent(struct clk_hw *hw, u8 index)
456 {
457 	struct pll5_mux_hw_data *hwdata = to_pll5_mux_hw_data(hw);
458 	struct rzg2l_cpg_priv *priv = hwdata->priv;
459 
460 	/*
461 	 * FOUTPOSTDIV--->|
462 	 *  |             | -->MUX -->DIV_DSIA_B -->M3 -->VCLK
463 	 *  |--FOUT1PH0-->|
464 	 *
465 	 * Based on the dot clock, the DSI divider clock calculates the parent
466 	 * rate and clk source for the MUX. It propagates that info to
467 	 * pll5_4_clk_mux which sets the clock source for DSI divider clock.
468 	 */
469 
470 	writel(CPG_OTHERFUNC1_REG_RES0_ON_WEN | index,
471 	       priv->base + CPG_OTHERFUNC1_REG);
472 
473 	return 0;
474 }
475 
476 static u8 rzg2l_cpg_pll5_4_clk_mux_get_parent(struct clk_hw *hw)
477 {
478 	struct pll5_mux_hw_data *hwdata = to_pll5_mux_hw_data(hw);
479 	struct rzg2l_cpg_priv *priv = hwdata->priv;
480 
481 	return readl(priv->base + GET_REG_OFFSET(hwdata->conf));
482 }
483 
484 static const struct clk_ops rzg2l_cpg_pll5_4_clk_mux_ops = {
485 	.determine_rate = rzg2l_cpg_pll5_4_clk_mux_determine_rate,
486 	.set_parent	= rzg2l_cpg_pll5_4_clk_mux_set_parent,
487 	.get_parent	= rzg2l_cpg_pll5_4_clk_mux_get_parent,
488 };
489 
490 static struct clk * __init
491 rzg2l_cpg_pll5_4_mux_clk_register(const struct cpg_core_clk *core,
492 				  struct rzg2l_cpg_priv *priv)
493 {
494 	struct pll5_mux_hw_data *clk_hw_data;
495 	struct clk_init_data init;
496 	struct clk_hw *clk_hw;
497 	int ret;
498 
499 	clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL);
500 	if (!clk_hw_data)
501 		return ERR_PTR(-ENOMEM);
502 
503 	clk_hw_data->priv = priv;
504 	clk_hw_data->conf = core->conf;
505 
506 	init.name = core->name;
507 	init.ops = &rzg2l_cpg_pll5_4_clk_mux_ops;
508 	init.flags = CLK_SET_RATE_PARENT;
509 	init.num_parents = core->num_parents;
510 	init.parent_names = core->parent_names;
511 
512 	clk_hw = &clk_hw_data->hw;
513 	clk_hw->init = &init;
514 
515 	ret = devm_clk_hw_register(priv->dev, clk_hw);
516 	if (ret)
517 		return ERR_PTR(ret);
518 
519 	return clk_hw->clk;
520 }
521 
522 struct sipll5 {
523 	struct clk_hw hw;
524 	u32 conf;
525 	unsigned long foutpostdiv_rate;
526 	struct rzg2l_cpg_priv *priv;
527 };
528 
529 #define to_sipll5(_hw)	container_of(_hw, struct sipll5, hw)
530 
531 static unsigned long rzg2l_cpg_get_vclk_rate(struct clk_hw *hw,
532 					     unsigned long rate)
533 {
534 	struct sipll5 *sipll5 = to_sipll5(hw);
535 	struct rzg2l_cpg_priv *priv = sipll5->priv;
536 	unsigned long vclk;
537 
538 	vclk = rate / ((1 << priv->mux_dsi_div_params.dsi_div_a) *
539 		       (priv->mux_dsi_div_params.dsi_div_b + 1));
540 
541 	if (priv->mux_dsi_div_params.clksrc)
542 		vclk /= 2;
543 
544 	return vclk;
545 }
546 
547 static unsigned long rzg2l_cpg_sipll5_recalc_rate(struct clk_hw *hw,
548 						  unsigned long parent_rate)
549 {
550 	struct sipll5 *sipll5 = to_sipll5(hw);
551 	unsigned long pll5_rate = sipll5->foutpostdiv_rate;
552 
553 	if (!pll5_rate)
554 		pll5_rate = parent_rate;
555 
556 	return pll5_rate;
557 }
558 
559 static long rzg2l_cpg_sipll5_round_rate(struct clk_hw *hw,
560 					unsigned long rate,
561 					unsigned long *parent_rate)
562 {
563 	return rate;
564 }
565 
566 static int rzg2l_cpg_sipll5_set_rate(struct clk_hw *hw,
567 				     unsigned long rate,
568 				     unsigned long parent_rate)
569 {
570 	struct sipll5 *sipll5 = to_sipll5(hw);
571 	struct rzg2l_cpg_priv *priv = sipll5->priv;
572 	struct rzg2l_pll5_param params;
573 	unsigned long vclk_rate;
574 	int ret;
575 	u32 val;
576 
577 	/*
578 	 *  OSC --> PLL5 --> FOUTPOSTDIV-->|
579 	 *                   |             | -->MUX -->DIV_DSIA_B -->M3 -->VCLK
580 	 *                   |--FOUT1PH0-->|
581 	 *
582 	 * Based on the dot clock, the DSI divider clock calculates the parent
583 	 * rate and the pll5 parameters for generating FOUTPOSTDIV. It propagates
584 	 * that info to sipll5 which sets parameters for generating FOUTPOSTDIV.
585 	 *
586 	 * OSC --> PLL5 --> FOUTPOSTDIV
587 	 */
588 
589 	if (!rate)
590 		return -EINVAL;
591 
592 	vclk_rate = rzg2l_cpg_get_vclk_rate(hw, rate);
593 	sipll5->foutpostdiv_rate =
594 		rzg2l_cpg_get_foutpostdiv_rate(&params, vclk_rate);
595 
596 	/* Put PLL5 into standby mode */
597 	writel(CPG_SIPLL5_STBY_RESETB_WEN, priv->base + CPG_SIPLL5_STBY);
598 	ret = readl_poll_timeout(priv->base + CPG_SIPLL5_MON, val,
599 				 !(val & CPG_SIPLL5_MON_PLL5_LOCK), 100, 250000);
600 	if (ret) {
601 		dev_err(priv->dev, "failed to release pll5 lock");
602 		return ret;
603 	}
604 
605 	/* Output clock setting 1 */
606 	writel((params.pl5_postdiv1 << 0) | (params.pl5_postdiv2 << 4) |
607 	       (params.pl5_refdiv << 8), priv->base + CPG_SIPLL5_CLK1);
608 
609 	/* Output clock setting, SSCG modulation value setting 3 */
610 	writel((params.pl5_fracin << 8), priv->base + CPG_SIPLL5_CLK3);
611 
612 	/* Output clock setting 4 */
613 	writel(CPG_SIPLL5_CLK4_RESV_LSB | (params.pl5_intin << 16),
614 	       priv->base + CPG_SIPLL5_CLK4);
615 
616 	/* Output clock setting 5 */
617 	writel(params.pl5_spread, priv->base + CPG_SIPLL5_CLK5);
618 
619 	/* PLL normal mode setting */
620 	writel(CPG_SIPLL5_STBY_DOWNSPREAD_WEN | CPG_SIPLL5_STBY_SSCG_EN_WEN |
621 	       CPG_SIPLL5_STBY_RESETB_WEN | CPG_SIPLL5_STBY_RESETB,
622 	       priv->base + CPG_SIPLL5_STBY);
623 
624 	/* PLL normal mode transition, output clock stability check */
625 	ret = readl_poll_timeout(priv->base + CPG_SIPLL5_MON, val,
626 				 (val & CPG_SIPLL5_MON_PLL5_LOCK), 100, 250000);
627 	if (ret) {
628 		dev_err(priv->dev, "failed to lock pll5");
629 		return ret;
630 	}
631 
632 	return 0;
633 }
634 
635 static const struct clk_ops rzg2l_cpg_sipll5_ops = {
636 	.recalc_rate = rzg2l_cpg_sipll5_recalc_rate,
637 	.round_rate = rzg2l_cpg_sipll5_round_rate,
638 	.set_rate = rzg2l_cpg_sipll5_set_rate,
639 };
640 
641 static struct clk * __init
642 rzg2l_cpg_sipll5_register(const struct cpg_core_clk *core,
643 			  struct clk **clks,
644 			  struct rzg2l_cpg_priv *priv)
645 {
646 	const struct clk *parent;
647 	struct clk_init_data init;
648 	const char *parent_name;
649 	struct sipll5 *sipll5;
650 	struct clk_hw *clk_hw;
651 	int ret;
652 
653 	parent = clks[core->parent & 0xffff];
654 	if (IS_ERR(parent))
655 		return ERR_CAST(parent);
656 
657 	sipll5 = devm_kzalloc(priv->dev, sizeof(*sipll5), GFP_KERNEL);
658 	if (!sipll5)
659 		return ERR_PTR(-ENOMEM);
660 
661 	init.name = core->name;
662 	parent_name = __clk_get_name(parent);
663 	init.ops = &rzg2l_cpg_sipll5_ops;
664 	init.flags = 0;
665 	init.parent_names = &parent_name;
666 	init.num_parents = 1;
667 
668 	sipll5->hw.init = &init;
669 	sipll5->conf = core->conf;
670 	sipll5->priv = priv;
671 
672 	writel(CPG_SIPLL5_STBY_SSCG_EN_WEN | CPG_SIPLL5_STBY_RESETB_WEN |
673 	       CPG_SIPLL5_STBY_RESETB, priv->base + CPG_SIPLL5_STBY);
674 
675 	clk_hw = &sipll5->hw;
676 	clk_hw->init = &init;
677 
678 	ret = devm_clk_hw_register(priv->dev, clk_hw);
679 	if (ret)
680 		return ERR_PTR(ret);
681 
682 	priv->mux_dsi_div_params.clksrc = 1; /* Use clk src 1 for DSI */
683 	priv->mux_dsi_div_params.dsi_div_a = 1; /* Divided by 2 */
684 	priv->mux_dsi_div_params.dsi_div_b = 2; /* Divided by 3 */
685 
686 	return clk_hw->clk;
687 }
688 
689 struct pll_clk {
690 	struct clk_hw hw;
691 	unsigned int conf;
692 	unsigned int type;
693 	void __iomem *base;
694 	struct rzg2l_cpg_priv *priv;
695 };
696 
697 #define to_pll(_hw)	container_of(_hw, struct pll_clk, hw)
698 
699 static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw,
700 						   unsigned long parent_rate)
701 {
702 	struct pll_clk *pll_clk = to_pll(hw);
703 	struct rzg2l_cpg_priv *priv = pll_clk->priv;
704 	unsigned int val1, val2;
705 	unsigned int mult = 1;
706 	unsigned int div = 1;
707 
708 	if (pll_clk->type != CLK_TYPE_SAM_PLL)
709 		return parent_rate;
710 
711 	val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf));
712 	val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf));
713 	mult = MDIV(val1) + KDIV(val1) / 65536;
714 	div = PDIV(val1) << SDIV(val2);
715 
716 	return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div);
717 }
718 
719 static const struct clk_ops rzg2l_cpg_pll_ops = {
720 	.recalc_rate = rzg2l_cpg_pll_clk_recalc_rate,
721 };
722 
723 static struct clk * __init
724 rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core,
725 			   struct clk **clks,
726 			   void __iomem *base,
727 			   struct rzg2l_cpg_priv *priv)
728 {
729 	struct device *dev = priv->dev;
730 	const struct clk *parent;
731 	struct clk_init_data init;
732 	const char *parent_name;
733 	struct pll_clk *pll_clk;
734 
735 	parent = clks[core->parent & 0xffff];
736 	if (IS_ERR(parent))
737 		return ERR_CAST(parent);
738 
739 	pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
740 	if (!pll_clk)
741 		return ERR_PTR(-ENOMEM);
742 
743 	parent_name = __clk_get_name(parent);
744 	init.name = core->name;
745 	init.ops = &rzg2l_cpg_pll_ops;
746 	init.flags = 0;
747 	init.parent_names = &parent_name;
748 	init.num_parents = 1;
749 
750 	pll_clk->hw.init = &init;
751 	pll_clk->conf = core->conf;
752 	pll_clk->base = base;
753 	pll_clk->priv = priv;
754 	pll_clk->type = core->type;
755 
756 	return clk_register(NULL, &pll_clk->hw);
757 }
758 
759 static struct clk
760 *rzg2l_cpg_clk_src_twocell_get(struct of_phandle_args *clkspec,
761 			       void *data)
762 {
763 	unsigned int clkidx = clkspec->args[1];
764 	struct rzg2l_cpg_priv *priv = data;
765 	struct device *dev = priv->dev;
766 	const char *type;
767 	struct clk *clk;
768 
769 	switch (clkspec->args[0]) {
770 	case CPG_CORE:
771 		type = "core";
772 		if (clkidx > priv->last_dt_core_clk) {
773 			dev_err(dev, "Invalid %s clock index %u\n", type, clkidx);
774 			return ERR_PTR(-EINVAL);
775 		}
776 		clk = priv->clks[clkidx];
777 		break;
778 
779 	case CPG_MOD:
780 		type = "module";
781 		if (clkidx >= priv->num_mod_clks) {
782 			dev_err(dev, "Invalid %s clock index %u\n", type,
783 				clkidx);
784 			return ERR_PTR(-EINVAL);
785 		}
786 		clk = priv->clks[priv->num_core_clks + clkidx];
787 		break;
788 
789 	default:
790 		dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]);
791 		return ERR_PTR(-EINVAL);
792 	}
793 
794 	if (IS_ERR(clk))
795 		dev_err(dev, "Cannot get %s clock %u: %ld", type, clkidx,
796 			PTR_ERR(clk));
797 	else
798 		dev_dbg(dev, "clock (%u, %u) is %pC at %lu Hz\n",
799 			clkspec->args[0], clkspec->args[1], clk,
800 			clk_get_rate(clk));
801 	return clk;
802 }
803 
804 static void __init
805 rzg2l_cpg_register_core_clk(const struct cpg_core_clk *core,
806 			    const struct rzg2l_cpg_info *info,
807 			    struct rzg2l_cpg_priv *priv)
808 {
809 	struct clk *clk = ERR_PTR(-EOPNOTSUPP), *parent;
810 	struct device *dev = priv->dev;
811 	unsigned int id = core->id, div = core->div;
812 	const char *parent_name;
813 
814 	WARN_DEBUG(id >= priv->num_core_clks);
815 	WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
816 
817 	if (!core->name) {
818 		/* Skip NULLified clock */
819 		return;
820 	}
821 
822 	switch (core->type) {
823 	case CLK_TYPE_IN:
824 		clk = of_clk_get_by_name(priv->dev->of_node, core->name);
825 		break;
826 	case CLK_TYPE_FF:
827 		WARN_DEBUG(core->parent >= priv->num_core_clks);
828 		parent = priv->clks[core->parent];
829 		if (IS_ERR(parent)) {
830 			clk = parent;
831 			goto fail;
832 		}
833 
834 		parent_name = __clk_get_name(parent);
835 		clk = clk_register_fixed_factor(NULL, core->name,
836 						parent_name, CLK_SET_RATE_PARENT,
837 						core->mult, div);
838 		break;
839 	case CLK_TYPE_SAM_PLL:
840 		clk = rzg2l_cpg_pll_clk_register(core, priv->clks,
841 						 priv->base, priv);
842 		break;
843 	case CLK_TYPE_SIPLL5:
844 		clk = rzg2l_cpg_sipll5_register(core, priv->clks, priv);
845 		break;
846 	case CLK_TYPE_DIV:
847 		clk = rzg2l_cpg_div_clk_register(core, priv->clks,
848 						 priv->base, priv);
849 		break;
850 	case CLK_TYPE_MUX:
851 		clk = rzg2l_cpg_mux_clk_register(core, priv->base, priv);
852 		break;
853 	case CLK_TYPE_SD_MUX:
854 		clk = rzg2l_cpg_sd_mux_clk_register(core, priv->base, priv);
855 		break;
856 	case CLK_TYPE_PLL5_4_MUX:
857 		clk = rzg2l_cpg_pll5_4_mux_clk_register(core, priv);
858 		break;
859 	case CLK_TYPE_DSI_DIV:
860 		clk = rzg2l_cpg_dsi_div_clk_register(core, priv->clks, priv);
861 		break;
862 	default:
863 		goto fail;
864 	}
865 
866 	if (IS_ERR_OR_NULL(clk))
867 		goto fail;
868 
869 	dev_dbg(dev, "Core clock %pC at %lu Hz\n", clk, clk_get_rate(clk));
870 	priv->clks[id] = clk;
871 	return;
872 
873 fail:
874 	dev_err(dev, "Failed to register %s clock %s: %ld\n", "core",
875 		core->name, PTR_ERR(clk));
876 }
877 
878 /**
879  * struct mstp_clock - MSTP gating clock
880  *
881  * @hw: handle between common and hardware-specific interfaces
882  * @off: register offset
883  * @bit: ON/MON bit
884  * @enabled: soft state of the clock, if it is coupled with another clock
885  * @priv: CPG/MSTP private data
886  * @sibling: pointer to the other coupled clock
887  */
888 struct mstp_clock {
889 	struct clk_hw hw;
890 	u16 off;
891 	u8 bit;
892 	bool enabled;
893 	struct rzg2l_cpg_priv *priv;
894 	struct mstp_clock *sibling;
895 };
896 
897 #define to_mod_clock(_hw) container_of(_hw, struct mstp_clock, hw)
898 
899 static int rzg2l_mod_clock_endisable(struct clk_hw *hw, bool enable)
900 {
901 	struct mstp_clock *clock = to_mod_clock(hw);
902 	struct rzg2l_cpg_priv *priv = clock->priv;
903 	unsigned int reg = clock->off;
904 	struct device *dev = priv->dev;
905 	unsigned long flags;
906 	u32 bitmask = BIT(clock->bit);
907 	u32 value;
908 	int error;
909 
910 	if (!clock->off) {
911 		dev_dbg(dev, "%pC does not support ON/OFF\n",  hw->clk);
912 		return 0;
913 	}
914 
915 	dev_dbg(dev, "CLK_ON %u/%pC %s\n", CLK_ON_R(reg), hw->clk,
916 		enable ? "ON" : "OFF");
917 	spin_lock_irqsave(&priv->rmw_lock, flags);
918 
919 	if (enable)
920 		value = (bitmask << 16) | bitmask;
921 	else
922 		value = bitmask << 16;
923 	writel(value, priv->base + CLK_ON_R(reg));
924 
925 	spin_unlock_irqrestore(&priv->rmw_lock, flags);
926 
927 	if (!enable)
928 		return 0;
929 
930 	if (!priv->info->has_clk_mon_regs)
931 		return 0;
932 
933 	error = readl_poll_timeout_atomic(priv->base + CLK_MON_R(reg), value,
934 					  value & bitmask, 0, 10);
935 	if (error)
936 		dev_err(dev, "Failed to enable CLK_ON %p\n",
937 			priv->base + CLK_ON_R(reg));
938 
939 	return error;
940 }
941 
942 static int rzg2l_mod_clock_enable(struct clk_hw *hw)
943 {
944 	struct mstp_clock *clock = to_mod_clock(hw);
945 
946 	if (clock->sibling) {
947 		struct rzg2l_cpg_priv *priv = clock->priv;
948 		unsigned long flags;
949 		bool enabled;
950 
951 		spin_lock_irqsave(&priv->rmw_lock, flags);
952 		enabled = clock->sibling->enabled;
953 		clock->enabled = true;
954 		spin_unlock_irqrestore(&priv->rmw_lock, flags);
955 		if (enabled)
956 			return 0;
957 	}
958 
959 	return rzg2l_mod_clock_endisable(hw, true);
960 }
961 
962 static void rzg2l_mod_clock_disable(struct clk_hw *hw)
963 {
964 	struct mstp_clock *clock = to_mod_clock(hw);
965 
966 	if (clock->sibling) {
967 		struct rzg2l_cpg_priv *priv = clock->priv;
968 		unsigned long flags;
969 		bool enabled;
970 
971 		spin_lock_irqsave(&priv->rmw_lock, flags);
972 		enabled = clock->sibling->enabled;
973 		clock->enabled = false;
974 		spin_unlock_irqrestore(&priv->rmw_lock, flags);
975 		if (enabled)
976 			return;
977 	}
978 
979 	rzg2l_mod_clock_endisable(hw, false);
980 }
981 
982 static int rzg2l_mod_clock_is_enabled(struct clk_hw *hw)
983 {
984 	struct mstp_clock *clock = to_mod_clock(hw);
985 	struct rzg2l_cpg_priv *priv = clock->priv;
986 	u32 bitmask = BIT(clock->bit);
987 	u32 value;
988 
989 	if (!clock->off) {
990 		dev_dbg(priv->dev, "%pC does not support ON/OFF\n",  hw->clk);
991 		return 1;
992 	}
993 
994 	if (clock->sibling)
995 		return clock->enabled;
996 
997 	if (priv->info->has_clk_mon_regs)
998 		value = readl(priv->base + CLK_MON_R(clock->off));
999 	else
1000 		value = readl(priv->base + clock->off);
1001 
1002 	return value & bitmask;
1003 }
1004 
1005 static const struct clk_ops rzg2l_mod_clock_ops = {
1006 	.enable = rzg2l_mod_clock_enable,
1007 	.disable = rzg2l_mod_clock_disable,
1008 	.is_enabled = rzg2l_mod_clock_is_enabled,
1009 };
1010 
1011 static struct mstp_clock
1012 *rzg2l_mod_clock_get_sibling(struct mstp_clock *clock,
1013 			     struct rzg2l_cpg_priv *priv)
1014 {
1015 	struct clk_hw *hw;
1016 	unsigned int i;
1017 
1018 	for (i = 0; i < priv->num_mod_clks; i++) {
1019 		struct mstp_clock *clk;
1020 
1021 		if (priv->clks[priv->num_core_clks + i] == ERR_PTR(-ENOENT))
1022 			continue;
1023 
1024 		hw = __clk_get_hw(priv->clks[priv->num_core_clks + i]);
1025 		clk = to_mod_clock(hw);
1026 		if (clock->off == clk->off && clock->bit == clk->bit)
1027 			return clk;
1028 	}
1029 
1030 	return NULL;
1031 }
1032 
1033 static void __init
1034 rzg2l_cpg_register_mod_clk(const struct rzg2l_mod_clk *mod,
1035 			   const struct rzg2l_cpg_info *info,
1036 			   struct rzg2l_cpg_priv *priv)
1037 {
1038 	struct mstp_clock *clock = NULL;
1039 	struct device *dev = priv->dev;
1040 	unsigned int id = mod->id;
1041 	struct clk_init_data init;
1042 	struct clk *parent, *clk;
1043 	const char *parent_name;
1044 	unsigned int i;
1045 
1046 	WARN_DEBUG(id < priv->num_core_clks);
1047 	WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks);
1048 	WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks);
1049 	WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
1050 
1051 	if (!mod->name) {
1052 		/* Skip NULLified clock */
1053 		return;
1054 	}
1055 
1056 	parent = priv->clks[mod->parent];
1057 	if (IS_ERR(parent)) {
1058 		clk = parent;
1059 		goto fail;
1060 	}
1061 
1062 	clock = devm_kzalloc(dev, sizeof(*clock), GFP_KERNEL);
1063 	if (!clock) {
1064 		clk = ERR_PTR(-ENOMEM);
1065 		goto fail;
1066 	}
1067 
1068 	init.name = mod->name;
1069 	init.ops = &rzg2l_mod_clock_ops;
1070 	init.flags = CLK_SET_RATE_PARENT;
1071 	for (i = 0; i < info->num_crit_mod_clks; i++)
1072 		if (id == info->crit_mod_clks[i]) {
1073 			dev_dbg(dev, "CPG %s setting CLK_IS_CRITICAL\n",
1074 				mod->name);
1075 			init.flags |= CLK_IS_CRITICAL;
1076 			break;
1077 		}
1078 
1079 	parent_name = __clk_get_name(parent);
1080 	init.parent_names = &parent_name;
1081 	init.num_parents = 1;
1082 
1083 	clock->off = mod->off;
1084 	clock->bit = mod->bit;
1085 	clock->priv = priv;
1086 	clock->hw.init = &init;
1087 
1088 	clk = clk_register(NULL, &clock->hw);
1089 	if (IS_ERR(clk))
1090 		goto fail;
1091 
1092 	dev_dbg(dev, "Module clock %pC at %lu Hz\n", clk, clk_get_rate(clk));
1093 	priv->clks[id] = clk;
1094 
1095 	if (mod->is_coupled) {
1096 		struct mstp_clock *sibling;
1097 
1098 		clock->enabled = rzg2l_mod_clock_is_enabled(&clock->hw);
1099 		sibling = rzg2l_mod_clock_get_sibling(clock, priv);
1100 		if (sibling) {
1101 			clock->sibling = sibling;
1102 			sibling->sibling = clock;
1103 		}
1104 	}
1105 
1106 	return;
1107 
1108 fail:
1109 	dev_err(dev, "Failed to register %s clock %s: %ld\n", "module",
1110 		mod->name, PTR_ERR(clk));
1111 }
1112 
1113 #define rcdev_to_priv(x)	container_of(x, struct rzg2l_cpg_priv, rcdev)
1114 
1115 static int rzg2l_cpg_reset(struct reset_controller_dev *rcdev,
1116 			   unsigned long id)
1117 {
1118 	struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev);
1119 	const struct rzg2l_cpg_info *info = priv->info;
1120 	unsigned int reg = info->resets[id].off;
1121 	u32 dis = BIT(info->resets[id].bit);
1122 	u32 we = dis << 16;
1123 
1124 	dev_dbg(rcdev->dev, "reset id:%ld offset:0x%x\n", id, CLK_RST_R(reg));
1125 
1126 	/* Reset module */
1127 	writel(we, priv->base + CLK_RST_R(reg));
1128 
1129 	/* Wait for at least one cycle of the RCLK clock (@ ca. 32 kHz) */
1130 	udelay(35);
1131 
1132 	/* Release module from reset state */
1133 	writel(we | dis, priv->base + CLK_RST_R(reg));
1134 
1135 	return 0;
1136 }
1137 
1138 static int rzg2l_cpg_assert(struct reset_controller_dev *rcdev,
1139 			    unsigned long id)
1140 {
1141 	struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev);
1142 	const struct rzg2l_cpg_info *info = priv->info;
1143 	unsigned int reg = info->resets[id].off;
1144 	u32 value = BIT(info->resets[id].bit) << 16;
1145 
1146 	dev_dbg(rcdev->dev, "assert id:%ld offset:0x%x\n", id, CLK_RST_R(reg));
1147 
1148 	writel(value, priv->base + CLK_RST_R(reg));
1149 	return 0;
1150 }
1151 
1152 static int rzg2l_cpg_deassert(struct reset_controller_dev *rcdev,
1153 			      unsigned long id)
1154 {
1155 	struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev);
1156 	const struct rzg2l_cpg_info *info = priv->info;
1157 	unsigned int reg = info->resets[id].off;
1158 	u32 dis = BIT(info->resets[id].bit);
1159 	u32 value = (dis << 16) | dis;
1160 
1161 	dev_dbg(rcdev->dev, "deassert id:%ld offset:0x%x\n", id,
1162 		CLK_RST_R(reg));
1163 
1164 	writel(value, priv->base + CLK_RST_R(reg));
1165 	return 0;
1166 }
1167 
1168 static int rzg2l_cpg_status(struct reset_controller_dev *rcdev,
1169 			    unsigned long id)
1170 {
1171 	struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev);
1172 	const struct rzg2l_cpg_info *info = priv->info;
1173 	unsigned int reg = info->resets[id].off;
1174 	u32 bitmask = BIT(info->resets[id].bit);
1175 	s8 monbit = info->resets[id].monbit;
1176 
1177 	if (info->has_clk_mon_regs) {
1178 		return !!(readl(priv->base + CLK_MRST_R(reg)) & bitmask);
1179 	} else if (monbit >= 0) {
1180 		u32 monbitmask = BIT(monbit);
1181 
1182 		return !!(readl(priv->base + CPG_RST_MON) & monbitmask);
1183 	}
1184 	return -ENOTSUPP;
1185 }
1186 
1187 static const struct reset_control_ops rzg2l_cpg_reset_ops = {
1188 	.reset = rzg2l_cpg_reset,
1189 	.assert = rzg2l_cpg_assert,
1190 	.deassert = rzg2l_cpg_deassert,
1191 	.status = rzg2l_cpg_status,
1192 };
1193 
1194 static int rzg2l_cpg_reset_xlate(struct reset_controller_dev *rcdev,
1195 				 const struct of_phandle_args *reset_spec)
1196 {
1197 	struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev);
1198 	const struct rzg2l_cpg_info *info = priv->info;
1199 	unsigned int id = reset_spec->args[0];
1200 
1201 	if (id >= rcdev->nr_resets || !info->resets[id].off) {
1202 		dev_err(rcdev->dev, "Invalid reset index %u\n", id);
1203 		return -EINVAL;
1204 	}
1205 
1206 	return id;
1207 }
1208 
1209 static int rzg2l_cpg_reset_controller_register(struct rzg2l_cpg_priv *priv)
1210 {
1211 	priv->rcdev.ops = &rzg2l_cpg_reset_ops;
1212 	priv->rcdev.of_node = priv->dev->of_node;
1213 	priv->rcdev.dev = priv->dev;
1214 	priv->rcdev.of_reset_n_cells = 1;
1215 	priv->rcdev.of_xlate = rzg2l_cpg_reset_xlate;
1216 	priv->rcdev.nr_resets = priv->num_resets;
1217 
1218 	return devm_reset_controller_register(priv->dev, &priv->rcdev);
1219 }
1220 
1221 static bool rzg2l_cpg_is_pm_clk(struct rzg2l_cpg_priv *priv,
1222 				const struct of_phandle_args *clkspec)
1223 {
1224 	const struct rzg2l_cpg_info *info = priv->info;
1225 	unsigned int id;
1226 	unsigned int i;
1227 
1228 	if (clkspec->args_count != 2)
1229 		return false;
1230 
1231 	if (clkspec->args[0] != CPG_MOD)
1232 		return false;
1233 
1234 	id = clkspec->args[1] + info->num_total_core_clks;
1235 	for (i = 0; i < info->num_no_pm_mod_clks; i++) {
1236 		if (info->no_pm_mod_clks[i] == id)
1237 			return false;
1238 	}
1239 
1240 	return true;
1241 }
1242 
1243 static int rzg2l_cpg_attach_dev(struct generic_pm_domain *domain, struct device *dev)
1244 {
1245 	struct rzg2l_cpg_priv *priv = container_of(domain, struct rzg2l_cpg_priv, genpd);
1246 	struct device_node *np = dev->of_node;
1247 	struct of_phandle_args clkspec;
1248 	bool once = true;
1249 	struct clk *clk;
1250 	int error;
1251 	int i = 0;
1252 
1253 	while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
1254 					   &clkspec)) {
1255 		if (rzg2l_cpg_is_pm_clk(priv, &clkspec)) {
1256 			if (once) {
1257 				once = false;
1258 				error = pm_clk_create(dev);
1259 				if (error) {
1260 					of_node_put(clkspec.np);
1261 					goto err;
1262 				}
1263 			}
1264 			clk = of_clk_get_from_provider(&clkspec);
1265 			of_node_put(clkspec.np);
1266 			if (IS_ERR(clk)) {
1267 				error = PTR_ERR(clk);
1268 				goto fail_destroy;
1269 			}
1270 
1271 			error = pm_clk_add_clk(dev, clk);
1272 			if (error) {
1273 				dev_err(dev, "pm_clk_add_clk failed %d\n",
1274 					error);
1275 				goto fail_put;
1276 			}
1277 		} else {
1278 			of_node_put(clkspec.np);
1279 		}
1280 		i++;
1281 	}
1282 
1283 	return 0;
1284 
1285 fail_put:
1286 	clk_put(clk);
1287 
1288 fail_destroy:
1289 	pm_clk_destroy(dev);
1290 err:
1291 	return error;
1292 }
1293 
1294 static void rzg2l_cpg_detach_dev(struct generic_pm_domain *unused, struct device *dev)
1295 {
1296 	if (!pm_clk_no_clocks(dev))
1297 		pm_clk_destroy(dev);
1298 }
1299 
1300 static void rzg2l_cpg_genpd_remove(void *data)
1301 {
1302 	pm_genpd_remove(data);
1303 }
1304 
1305 static int __init rzg2l_cpg_add_clk_domain(struct rzg2l_cpg_priv *priv)
1306 {
1307 	struct device *dev = priv->dev;
1308 	struct device_node *np = dev->of_node;
1309 	struct generic_pm_domain *genpd = &priv->genpd;
1310 	int ret;
1311 
1312 	genpd->name = np->name;
1313 	genpd->flags = GENPD_FLAG_PM_CLK | GENPD_FLAG_ALWAYS_ON |
1314 		       GENPD_FLAG_ACTIVE_WAKEUP;
1315 	genpd->attach_dev = rzg2l_cpg_attach_dev;
1316 	genpd->detach_dev = rzg2l_cpg_detach_dev;
1317 	ret = pm_genpd_init(genpd, &pm_domain_always_on_gov, false);
1318 	if (ret)
1319 		return ret;
1320 
1321 	ret = devm_add_action_or_reset(dev, rzg2l_cpg_genpd_remove, genpd);
1322 	if (ret)
1323 		return ret;
1324 
1325 	return of_genpd_add_provider_simple(np, genpd);
1326 }
1327 
1328 static int __init rzg2l_cpg_probe(struct platform_device *pdev)
1329 {
1330 	struct device *dev = &pdev->dev;
1331 	struct device_node *np = dev->of_node;
1332 	const struct rzg2l_cpg_info *info;
1333 	struct rzg2l_cpg_priv *priv;
1334 	unsigned int nclks, i;
1335 	struct clk **clks;
1336 	int error;
1337 
1338 	info = of_device_get_match_data(dev);
1339 
1340 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1341 	if (!priv)
1342 		return -ENOMEM;
1343 
1344 	priv->dev = dev;
1345 	priv->info = info;
1346 	spin_lock_init(&priv->rmw_lock);
1347 
1348 	priv->base = devm_platform_ioremap_resource(pdev, 0);
1349 	if (IS_ERR(priv->base))
1350 		return PTR_ERR(priv->base);
1351 
1352 	nclks = info->num_total_core_clks + info->num_hw_mod_clks;
1353 	clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL);
1354 	if (!clks)
1355 		return -ENOMEM;
1356 
1357 	dev_set_drvdata(dev, priv);
1358 	priv->clks = clks;
1359 	priv->num_core_clks = info->num_total_core_clks;
1360 	priv->num_mod_clks = info->num_hw_mod_clks;
1361 	priv->num_resets = info->num_resets;
1362 	priv->last_dt_core_clk = info->last_dt_core_clk;
1363 
1364 	for (i = 0; i < nclks; i++)
1365 		clks[i] = ERR_PTR(-ENOENT);
1366 
1367 	for (i = 0; i < info->num_core_clks; i++)
1368 		rzg2l_cpg_register_core_clk(&info->core_clks[i], info, priv);
1369 
1370 	for (i = 0; i < info->num_mod_clks; i++)
1371 		rzg2l_cpg_register_mod_clk(&info->mod_clks[i], info, priv);
1372 
1373 	error = of_clk_add_provider(np, rzg2l_cpg_clk_src_twocell_get, priv);
1374 	if (error)
1375 		return error;
1376 
1377 	error = devm_add_action_or_reset(dev, rzg2l_cpg_del_clk_provider, np);
1378 	if (error)
1379 		return error;
1380 
1381 	error = rzg2l_cpg_add_clk_domain(priv);
1382 	if (error)
1383 		return error;
1384 
1385 	error = rzg2l_cpg_reset_controller_register(priv);
1386 	if (error)
1387 		return error;
1388 
1389 	return 0;
1390 }
1391 
1392 static const struct of_device_id rzg2l_cpg_match[] = {
1393 #ifdef CONFIG_CLK_R9A07G043
1394 	{
1395 		.compatible = "renesas,r9a07g043-cpg",
1396 		.data = &r9a07g043_cpg_info,
1397 	},
1398 #endif
1399 #ifdef CONFIG_CLK_R9A07G044
1400 	{
1401 		.compatible = "renesas,r9a07g044-cpg",
1402 		.data = &r9a07g044_cpg_info,
1403 	},
1404 #endif
1405 #ifdef CONFIG_CLK_R9A07G054
1406 	{
1407 		.compatible = "renesas,r9a07g054-cpg",
1408 		.data = &r9a07g054_cpg_info,
1409 	},
1410 #endif
1411 #ifdef CONFIG_CLK_R9A09G011
1412 	{
1413 		.compatible = "renesas,r9a09g011-cpg",
1414 		.data = &r9a09g011_cpg_info,
1415 	},
1416 #endif
1417 	{ /* sentinel */ }
1418 };
1419 
1420 static struct platform_driver rzg2l_cpg_driver = {
1421 	.driver		= {
1422 		.name	= "rzg2l-cpg",
1423 		.of_match_table = rzg2l_cpg_match,
1424 	},
1425 };
1426 
1427 static int __init rzg2l_cpg_init(void)
1428 {
1429 	return platform_driver_probe(&rzg2l_cpg_driver, rzg2l_cpg_probe);
1430 }
1431 
1432 subsys_initcall(rzg2l_cpg_init);
1433 
1434 MODULE_DESCRIPTION("Renesas RZ/G2L CPG Driver");
1435