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/slab.h>
17 #include <linux/clk-provider.h>
19 #include <linux/mfd/syscon.h>
21 #include <linux/regmap.h>
25 #define streq(a, b) (strcmp((a), (b)) == 0)
27 #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
29 /* SDMMC Group for System Manager defines */
30 #define SYSMGR_SDMMCGRP_CTRL_OFFSET 0x28
32 static unsigned long socfpga_gate_clk_recalc_rate(struct clk_hw
*hwclk
,
33 unsigned long parent_rate
)
35 struct socfpga_gate_clk
*socfpgaclk
= to_socfpga_gate_clk(hwclk
);
38 if (socfpgaclk
->fixed_div
)
39 div
= socfpgaclk
->fixed_div
;
40 else if (socfpgaclk
->div_reg
) {
41 val
= readl(socfpgaclk
->div_reg
) >> socfpgaclk
->shift
;
42 val
&= GENMASK(socfpgaclk
->width
- 1, 0);
46 return parent_rate
/ div
;
49 static int socfpga_clk_prepare(struct clk_hw
*hwclk
)
51 struct socfpga_gate_clk
*socfpgaclk
= to_socfpga_gate_clk(hwclk
);
56 if (socfpgaclk
->clk_phase
[0] || socfpgaclk
->clk_phase
[1]) {
57 for (i
= 0; i
< ARRAY_SIZE(clk_phase
); i
++) {
58 switch (socfpgaclk
->clk_phase
[i
]) {
89 hs_timing
= SYSMGR_SDMMC_CTRL_SET_AS10(clk_phase
[0], clk_phase
[1]);
90 if (!IS_ERR(socfpgaclk
->sys_mgr_base_addr
))
91 regmap_write(socfpgaclk
->sys_mgr_base_addr
,
92 SYSMGR_SDMMCGRP_CTRL_OFFSET
, hs_timing
);
94 pr_err("%s: cannot set clk_phase because sys_mgr_base_addr is not available!\n",
100 static struct clk_ops gateclk_ops
= {
101 .prepare
= socfpga_clk_prepare
,
102 .recalc_rate
= socfpga_gate_clk_recalc_rate
,
105 static void __init
__socfpga_gate_init(struct device_node
*node
,
106 const struct clk_ops
*ops
)
113 struct socfpga_gate_clk
*socfpga_clk
;
114 const char *clk_name
= node
->name
;
115 const char *parent_name
[SOCFPGA_MAX_PARENTS
];
116 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
;
170 init
.num_parents
= of_clk_parent_fill(node
, parent_name
, SOCFPGA_MAX_PARENTS
);
171 init
.parent_names
= parent_name
;
172 socfpga_clk
->hw
.hw
.init
= &init
;
174 clk
= clk_register(NULL
, &socfpga_clk
->hw
.hw
);
175 if (WARN_ON(IS_ERR(clk
))) {
179 rc
= of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
184 void __init
socfpga_a10_gate_init(struct device_node
*node
)
186 __socfpga_gate_init(node
, &gateclk_ops
);