2 * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
3 * Author: Lin Huang <hl@rock-chips.com>
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.
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.
16 #include <linux/arm-smccc.h>
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
21 #include <linux/slab.h>
22 #include <soc/rockchip/rockchip_sip.h>
23 #include <soc/rockchip/scpi.h>
29 struct rockchip_ddrclk {
31 void __iomem *reg_base;
41 #define to_rockchip_ddrclk_hw(hw) container_of(hw, struct rockchip_ddrclk, hw)
43 static int rockchip_ddrclk_sip_set_rate(struct clk_hw *hw, unsigned long drate,
46 struct rockchip_ddrclk *ddrclk = to_rockchip_ddrclk_hw(hw);
48 struct arm_smccc_res res;
50 spin_lock_irqsave(ddrclk->lock, flags);
51 arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, drate, 0,
52 ROCKCHIP_SIP_CONFIG_DRAM_SET_RATE,
54 spin_unlock_irqrestore(ddrclk->lock, flags);
60 rockchip_ddrclk_sip_recalc_rate(struct clk_hw *hw,
61 unsigned long parent_rate)
63 struct arm_smccc_res res;
65 arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, 0, 0,
66 ROCKCHIP_SIP_CONFIG_DRAM_GET_RATE,
72 static long rockchip_ddrclk_sip_round_rate(struct clk_hw *hw,
76 struct arm_smccc_res res;
78 arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, rate, 0,
79 ROCKCHIP_SIP_CONFIG_DRAM_ROUND_RATE,
85 static u8 rockchip_ddrclk_get_parent(struct clk_hw *hw)
87 struct rockchip_ddrclk *ddrclk = to_rockchip_ddrclk_hw(hw);
88 int num_parents = clk_hw_get_num_parents(hw);
91 val = clk_readl(ddrclk->reg_base +
92 ddrclk->mux_offset) >> ddrclk->mux_shift;
93 val &= GENMASK(ddrclk->mux_width - 1, 0);
95 if (val >= num_parents)
101 static const struct clk_ops rockchip_ddrclk_sip_ops = {
102 .recalc_rate = rockchip_ddrclk_sip_recalc_rate,
103 .set_rate = rockchip_ddrclk_sip_set_rate,
104 .round_rate = rockchip_ddrclk_sip_round_rate,
105 .get_parent = rockchip_ddrclk_get_parent,
108 static int rockchip_ddrclk_scpi_set_rate(struct clk_hw *hw, unsigned long drate,
114 ret = scpi_ddr_set_clk_rate(drate / MHZ, lcdc_type);
119 static unsigned long rockchip_ddrclk_scpi_recalc_rate(struct clk_hw *hw,
120 unsigned long parent_rate)
122 return (MHZ * scpi_ddr_get_clk_rate());
125 static long rockchip_ddrclk_scpi_round_rate(struct clk_hw *hw,
127 unsigned long *prate)
130 rate = (rate / 12) * 12;
135 static const struct clk_ops rockchip_ddrclk_scpi_ops = {
136 .recalc_rate = rockchip_ddrclk_scpi_recalc_rate,
137 .set_rate = rockchip_ddrclk_scpi_set_rate,
138 .round_rate = rockchip_ddrclk_scpi_round_rate,
139 .get_parent = rockchip_ddrclk_get_parent,
142 struct clk *rockchip_clk_register_ddrclk(const char *name, int flags,
143 const char *const *parent_names,
144 u8 num_parents, int mux_offset,
145 int mux_shift, int mux_width,
146 int div_shift, int div_width,
147 int ddr_flag, void __iomem *reg_base,
150 struct rockchip_ddrclk *ddrclk;
151 struct clk_init_data init;
154 ddrclk = kzalloc(sizeof(*ddrclk), GFP_KERNEL);
156 return ERR_PTR(-ENOMEM);
159 init.parent_names = parent_names;
160 init.num_parents = num_parents;
163 init.flags |= CLK_SET_RATE_NO_REPARENT;
164 init.flags |= CLK_GET_RATE_NOCACHE;
167 case ROCKCHIP_DDRCLK_SIP:
168 init.ops = &rockchip_ddrclk_sip_ops;
170 case ROCKCHIP_DDRCLK_SCPI:
171 init.ops = &rockchip_ddrclk_scpi_ops;
174 pr_err("%s: unsupported ddrclk type %d\n", __func__, ddr_flag);
176 return ERR_PTR(-EINVAL);
179 ddrclk->reg_base = reg_base;
181 ddrclk->hw.init = &init;
182 ddrclk->mux_offset = mux_offset;
183 ddrclk->mux_shift = mux_shift;
184 ddrclk->mux_width = mux_width;
185 ddrclk->div_shift = div_shift;
186 ddrclk->div_width = div_width;
187 ddrclk->ddr_flag = ddr_flag;
189 clk = clk_register(NULL, &ddrclk->hw);
191 pr_err("%s: could not register ddrclk %s\n", __func__, name);