1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Clock driver for the ARM Integrator/AP, Integrator/CP, Versatile AB and
4  * Versatile PB boards.
5  * Copyright (C) 2012 Linus Walleij
6  */
7 #include <linux/clk-provider.h>
8 #include <linux/err.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 
12 #include "icst.h"
13 #include "clk-icst.h"
14 
15 #define INTEGRATOR_HDR_LOCK_OFFSET	0x14
16 
17 #define VERSATILE_SYS_OSCCLCD_OFFSET	0x1c
18 #define VERSATILE_SYS_LOCK_OFFSET	0x20
19 
20 /* Base offset for the core module */
21 static void __iomem *cm_base;
22 
23 static const struct icst_params cp_auxosc_params = {
24 	.vco_max	= ICST525_VCO_MAX_5V,
25 	.vco_min	= ICST525_VCO_MIN,
26 	.vd_min 	= 8,
27 	.vd_max 	= 263,
28 	.rd_min 	= 3,
29 	.rd_max 	= 65,
30 	.s2div		= icst525_s2div,
31 	.idx2s		= icst525_idx2s,
32 };
33 
34 static const struct clk_icst_desc cm_auxosc_desc __initconst = {
35 	.params = &cp_auxosc_params,
36 	.vco_offset = 0x1c,
37 	.lock_offset = INTEGRATOR_HDR_LOCK_OFFSET,
38 };
39 
40 static const struct icst_params versatile_auxosc_params = {
41 	.vco_max	= ICST307_VCO_MAX,
42 	.vco_min	= ICST307_VCO_MIN,
43 	.vd_min		= 4 + 8,
44 	.vd_max		= 511 + 8,
45 	.rd_min		= 1 + 2,
46 	.rd_max		= 127 + 2,
47 	.s2div		= icst307_s2div,
48 	.idx2s		= icst307_idx2s,
49 };
50 
51 static const struct clk_icst_desc versatile_auxosc_desc __initconst = {
52 	.params = &versatile_auxosc_params,
53 	.vco_offset = VERSATILE_SYS_OSCCLCD_OFFSET,
54 	.lock_offset = VERSATILE_SYS_LOCK_OFFSET,
55 };
56 static void __init cm_osc_setup(struct device_node *np,
57 				const struct clk_icst_desc *desc)
58 {
59 	struct clk *clk;
60 	const char *clk_name = np->name;
61 	const char *parent_name;
62 
63 	if (!cm_base) {
64 		/* Remap the core module base if not done yet */
65 		struct device_node *parent;
66 
67 		parent = of_get_parent(np);
68 		if (!parent) {
69 			pr_err("no parent on core module clock\n");
70 			return;
71 		}
72 		cm_base = of_iomap(parent, 0);
73 		of_node_put(parent);
74 		if (!cm_base) {
75 			pr_err("could not remap core module base\n");
76 			return;
77 		}
78 	}
79 
80 	parent_name = of_clk_get_parent_name(np, 0);
81 	clk = icst_clk_register(NULL, desc, clk_name, parent_name, cm_base);
82 	if (!IS_ERR(clk))
83 		of_clk_add_provider(np, of_clk_src_simple_get, clk);
84 }
85 
86 static void __init of_integrator_cm_osc_setup(struct device_node *np)
87 {
88 	cm_osc_setup(np, &cm_auxosc_desc);
89 }
90 CLK_OF_DECLARE(integrator_cm_auxosc_clk,
91 	"arm,integrator-cm-auxosc", of_integrator_cm_osc_setup);
92 
93 static void __init of_versatile_cm_osc_setup(struct device_node *np)
94 {
95 	cm_osc_setup(np, &versatile_auxosc_desc);
96 }
97 CLK_OF_DECLARE(versatile_cm_auxosc_clk,
98 	       "arm,versatile-cm-auxosc", of_versatile_cm_osc_setup);
99