2 * Copyright (C) 2015 Altera Corporation. All rights reserved
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
16 #include <linux/clk-provider.h>
18 #include <linux/mfd/syscon.h>
20 #include <linux/regmap.h>
24 #define streq(a, b) (strcmp((a), (b)) == 0)
26 #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
28 /* SDMMC Group for System Manager defines */
29 #define SYSMGR_SDMMCGRP_CTRL_OFFSET 0x28
31 static unsigned long socfpga_gate_clk_recalc_rate(struct clk_hw
*hwclk
,
32 unsigned long parent_rate
)
34 struct socfpga_gate_clk
*socfpgaclk
= to_socfpga_gate_clk(hwclk
);
37 if (socfpgaclk
->fixed_div
)
38 div
= socfpgaclk
->fixed_div
;
39 else if (socfpgaclk
->div_reg
) {
40 val
= readl(socfpgaclk
->div_reg
) >> socfpgaclk
->shift
;
41 val
&= div_mask(socfpgaclk
->width
);
45 return parent_rate
/ div
;
48 static int socfpga_clk_prepare(struct clk_hw
*hwclk
)
50 struct socfpga_gate_clk
*socfpgaclk
= to_socfpga_gate_clk(hwclk
);
55 if (socfpgaclk
->clk_phase
[0] || socfpgaclk
->clk_phase
[1]) {
56 for (i
= 0; i
< ARRAY_SIZE(clk_phase
); i
++) {
57 switch (socfpgaclk
->clk_phase
[i
]) {
88 hs_timing
= SYSMGR_SDMMC_CTRL_SET(clk_phase
[0], clk_phase
[1]);
89 if (!IS_ERR(socfpgaclk
->sys_mgr_base_addr
))
90 regmap_write(socfpgaclk
->sys_mgr_base_addr
,
91 SYSMGR_SDMMCGRP_CTRL_OFFSET
, hs_timing
);
93 pr_err("%s: cannot set clk_phase because sys_mgr_base_addr is not available!\n",
99 static struct clk_ops gateclk_ops
= {
100 .prepare
= socfpga_clk_prepare
,
101 .recalc_rate
= socfpga_gate_clk_recalc_rate
,
104 static void __init
__socfpga_gate_init(struct device_node
*node
,
105 const struct clk_ops
*ops
)
112 struct socfpga_gate_clk
*socfpga_clk
;
113 const char *clk_name
= node
->name
;
114 const char *parent_name
[SOCFPGA_MAX_PARENTS
];
115 struct clk_init_data init
;
119 socfpga_clk
= kzalloc(sizeof(*socfpga_clk
), GFP_KERNEL
);
120 if (WARN_ON(!socfpga_clk
))
123 rc
= of_property_read_u32_array(node
, "clk-gate", clk_gate
, 2);
128 socfpga_clk
->hw
.reg
= clk_mgr_a10_base_addr
+ clk_gate
[0];
129 socfpga_clk
->hw
.bit_idx
= clk_gate
[1];
131 gateclk_ops
.enable
= clk_gate_ops
.enable
;
132 gateclk_ops
.disable
= clk_gate_ops
.disable
;
135 rc
= of_property_read_u32(node
, "fixed-divider", &fixed_div
);
137 socfpga_clk
->fixed_div
= 0;
139 socfpga_clk
->fixed_div
= fixed_div
;
141 rc
= of_property_read_u32_array(node
, "div-reg", div_reg
, 3);
143 socfpga_clk
->div_reg
= clk_mgr_a10_base_addr
+ div_reg
[0];
144 socfpga_clk
->shift
= div_reg
[1];
145 socfpga_clk
->width
= div_reg
[2];
147 socfpga_clk
->div_reg
= NULL
;
150 rc
= of_property_read_u32_array(node
, "clk-phase", clk_phase
, 2);
152 socfpga_clk
->clk_phase
[0] = clk_phase
[0];
153 socfpga_clk
->clk_phase
[1] = clk_phase
[1];
155 socfpga_clk
->sys_mgr_base_addr
=
156 syscon_regmap_lookup_by_compatible("altr,sys-mgr");
157 if (IS_ERR(socfpga_clk
->sys_mgr_base_addr
)) {
158 pr_err("%s: failed to find altr,sys-mgr regmap!\n",
164 of_property_read_string(node
, "clock-output-names", &clk_name
);
166 init
.name
= clk_name
;
169 while (i
< SOCFPGA_MAX_PARENTS
&& (parent_name
[i
] =
170 of_clk_get_parent_name(node
, i
)) != NULL
)
173 init
.parent_names
= parent_name
;
174 init
.num_parents
= i
;
175 socfpga_clk
->hw
.hw
.init
= &init
;
177 clk
= clk_register(NULL
, &socfpga_clk
->hw
.hw
);
178 if (WARN_ON(IS_ERR(clk
))) {
182 rc
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
187 void __init
socfpga_a10_gate_init(struct device_node
*node
)
189 __socfpga_gate_init(node
, &gateclk_ops
);