1 // SPDX-License-Identifier: GPL-2.0
3 * StarFive JH7110 Clock Generator Driver
5 * Copyright (C) 2022 Xingyu Wu <xingyu.wu@starfivetech.com>
8 #include <linux/bits.h>
10 #include <linux/clk-provider.h>
11 #include <linux/device.h>
12 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/of_device.h>
19 #include <linux/pm_runtime.h>
21 #include <dt-bindings/clock/starfive-jh7110-clkgen.h>
22 #include "clk-starfive-jh7110.h"
23 #include "clk-starfive-jh7110-pll.h"
25 static struct jh7110_clk * __init jh7110_clk_from(struct clk_hw *hw)
27 return container_of(hw, struct jh7110_clk, hw);
30 static struct jh7110_clk_priv *jh7110_priv_from(struct jh7110_clk *clk)
32 return container_of(clk, struct jh7110_clk_priv, reg[clk->idx]);
35 void __iomem *jh7110_clk_reg_addr_get(struct jh7110_clk *clk)
38 struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
40 if (clk->reg_flags == JH7110_CLK_SYS_FLAG)
41 reg = priv->sys_base + 4 * clk->idx;
42 else if (clk->reg_flags == JH7110_CLK_STG_FLAG)
43 reg = priv->stg_base + 4 * (clk->idx - JH7110_CLK_SYS_REG_END);
44 else if (clk->reg_flags == JH7110_CLK_AON_FLAG)
45 reg = priv->aon_base + 4 * (clk->idx - JH7110_CLK_STG_REG_END);
46 else if (clk->reg_flags == JH7110_CLK_VOUT_FLAG)
47 reg = priv->vout_base + 4 * clk->idx;
48 else if (clk->reg_flags == JH7110_CLK_ISP_FLAG)
49 reg = priv->isp_base + 4 * clk->idx;
54 static u32 jh7110_clk_reg_get(struct jh7110_clk *clk)
56 void __iomem *reg = jh7110_clk_reg_addr_get(clk);
57 if (clk->reg_flags == JH7110_CLK_ISP_FLAG) {
59 struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
61 if (pm_runtime_suspended(priv->dev)) {
62 ret = pm_runtime_get_sync(priv->dev);
64 dev_err(priv->dev, "cannot resume device :%d.\n", ret);
67 pm_runtime_put(priv->dev);
71 return readl_relaxed(reg);
74 static void jh7110_clk_reg_rmw(struct jh7110_clk *clk, u32 mask, u32 value)
76 struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
77 void __iomem *reg = jh7110_clk_reg_addr_get(clk);
80 spin_lock_irqsave(&priv->rmw_lock, flags);
81 if ((clk->idx == JH7110_UART3_CLK_CORE
82 || clk->idx == JH7110_UART4_CLK_CORE
83 || clk->idx == JH7110_UART5_CLK_CORE)
84 && (value != JH7110_CLK_ENABLE))
86 value |= jh7110_clk_reg_get(clk) & ~mask;
87 writel_relaxed(value, reg);
88 spin_unlock_irqrestore(&priv->rmw_lock, flags);
91 static int jh7110_clk_enable(struct clk_hw *hw)
93 struct jh7110_clk *clk = jh7110_clk_from(hw);
95 jh7110_clk_reg_rmw(clk, JH7110_CLK_ENABLE, JH7110_CLK_ENABLE);
99 static void jh7110_clk_disable(struct clk_hw *hw)
101 struct jh7110_clk *clk = jh7110_clk_from(hw);
103 jh7110_clk_reg_rmw(clk, JH7110_CLK_ENABLE, 0);
106 static int jh7110_clk_is_enabled(struct clk_hw *hw)
108 struct jh7110_clk *clk = jh7110_clk_from(hw);
110 return !!(jh7110_clk_reg_get(clk) & JH7110_CLK_ENABLE);
113 static unsigned long jh7110_clk_recalc_rate(struct clk_hw *hw,
114 unsigned long parent_rate)
116 struct jh7110_clk *clk = jh7110_clk_from(hw);
117 u32 div = jh7110_clk_reg_get(clk) & JH7110_CLK_DIV_MASK;
119 if (clk->idx == JH7110_UART3_CLK_CORE
120 || clk->idx == JH7110_UART4_CLK_CORE
121 || clk->idx == JH7110_UART5_CLK_CORE)
124 return div ? parent_rate / div : 0;
127 static int jh7110_clk_determine_rate(struct clk_hw *hw,
128 struct clk_rate_request *req)
130 struct jh7110_clk *clk = jh7110_clk_from(hw);
131 unsigned long parent = req->best_parent_rate;
132 unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate);
133 unsigned long div = min_t(unsigned long,
134 DIV_ROUND_UP(parent, rate), clk->max_div);
135 unsigned long result = parent / div;
138 * we want the result clamped by min_rate and max_rate if possible:
139 * case 1: div hits the max divider value, which means it's less than
140 * parent / rate, so the result is greater than rate and min_rate in
141 * particular. we can't do anything about result > max_rate because the
142 * divider doesn't go any further.
143 * case 2: div = DIV_ROUND_UP(parent, rate) which means the result is
144 * always lower or equal to rate and max_rate. however the result may
145 * turn out lower than min_rate, but then the next higher rate is fine:
146 * div - 1 = ceil(parent / rate) - 1 < parent / rate
148 * min_rate <= rate < parent / (div - 1)
150 if (result < req->min_rate && div > 1)
151 result = parent / (div - 1);
157 static int jh7110_clk_set_rate(struct clk_hw *hw,
159 unsigned long parent_rate)
161 struct jh7110_clk *clk = jh7110_clk_from(hw);
162 unsigned long div = clamp(DIV_ROUND_CLOSEST(parent_rate, rate),
163 1UL, (unsigned long)clk->max_div);
165 jh7110_clk_reg_rmw(clk, JH7110_CLK_DIV_MASK, div);
169 static u8 jh7110_clk_get_parent(struct clk_hw *hw)
171 struct jh7110_clk *clk = jh7110_clk_from(hw);
172 u32 value = jh7110_clk_reg_get(clk);
174 return (value & JH7110_CLK_MUX_MASK) >> JH7110_CLK_MUX_SHIFT;
177 static int jh7110_clk_set_parent(struct clk_hw *hw, u8 index)
179 struct jh7110_clk *clk = jh7110_clk_from(hw);
180 u32 value = (u32)index << JH7110_CLK_MUX_SHIFT;
182 jh7110_clk_reg_rmw(clk, JH7110_CLK_MUX_MASK, value);
186 static int jh7110_clk_mux_determine_rate(struct clk_hw *hw,
187 struct clk_rate_request *req)
189 return clk_mux_determine_rate_flags(hw, req, 0);
192 static int jh7110_clk_get_phase(struct clk_hw *hw)
194 struct jh7110_clk *clk = jh7110_clk_from(hw);
195 u32 value = jh7110_clk_reg_get(clk);
197 return (value & JH7110_CLK_INVERT) ? 180 : 0;
200 static int jh7110_clk_set_phase(struct clk_hw *hw, int degrees)
202 struct jh7110_clk *clk = jh7110_clk_from(hw);
207 else if (degrees == 180)
208 value = JH7110_CLK_INVERT;
212 jh7110_clk_reg_rmw(clk, JH7110_CLK_INVERT, value);
216 #ifdef CONFIG_DEBUG_FS
217 static void jh7110_clk_debug_init(struct clk_hw *hw, struct dentry *dentry)
219 static const struct debugfs_reg32 jh7110_clk_reg = {
223 struct jh7110_clk *clk = jh7110_clk_from(hw);
224 struct jh7110_clk_priv *priv = jh7110_priv_from(clk);
225 struct debugfs_regset32 *regset;
227 regset = devm_kzalloc(priv->dev, sizeof(*regset), GFP_KERNEL);
231 regset->regs = &jh7110_clk_reg;
233 regset->base = jh7110_clk_reg_addr_get(clk);
235 debugfs_create_regset32("registers", 0400, dentry, regset);
238 #define jh7110_clk_debug_init NULL
241 static const struct clk_ops jh7110_clk_gate_ops = {
242 .enable = jh7110_clk_enable,
243 .disable = jh7110_clk_disable,
244 .is_enabled = jh7110_clk_is_enabled,
245 .debug_init = jh7110_clk_debug_init,
248 static const struct clk_ops jh7110_clk_div_ops = {
249 .recalc_rate = jh7110_clk_recalc_rate,
250 .determine_rate = jh7110_clk_determine_rate,
251 .set_rate = jh7110_clk_set_rate,
252 .debug_init = jh7110_clk_debug_init,
255 static const struct clk_ops jh7110_clk_gdiv_ops = {
256 .enable = jh7110_clk_enable,
257 .disable = jh7110_clk_disable,
258 .is_enabled = jh7110_clk_is_enabled,
259 .recalc_rate = jh7110_clk_recalc_rate,
260 .determine_rate = jh7110_clk_determine_rate,
261 .set_rate = jh7110_clk_set_rate,
262 .debug_init = jh7110_clk_debug_init,
265 static const struct clk_ops jh7110_clk_mux_ops = {
266 .determine_rate = jh7110_clk_mux_determine_rate,
267 .set_parent = jh7110_clk_set_parent,
268 .get_parent = jh7110_clk_get_parent,
269 .debug_init = jh7110_clk_debug_init,
272 static const struct clk_ops jh7110_clk_gmux_ops = {
273 .enable = jh7110_clk_enable,
274 .disable = jh7110_clk_disable,
275 .is_enabled = jh7110_clk_is_enabled,
276 .determine_rate = jh7110_clk_mux_determine_rate,
277 .set_parent = jh7110_clk_set_parent,
278 .get_parent = jh7110_clk_get_parent,
279 .debug_init = jh7110_clk_debug_init,
282 static const struct clk_ops jh7110_clk_mdiv_ops = {
283 .recalc_rate = jh7110_clk_recalc_rate,
284 .determine_rate = jh7110_clk_determine_rate,
285 .get_parent = jh7110_clk_get_parent,
286 .set_parent = jh7110_clk_set_parent,
287 .set_rate = jh7110_clk_set_rate,
288 .debug_init = jh7110_clk_debug_init,
291 static const struct clk_ops jh7110_clk_gmd_ops = {
292 .enable = jh7110_clk_enable,
293 .disable = jh7110_clk_disable,
294 .is_enabled = jh7110_clk_is_enabled,
295 .recalc_rate = jh7110_clk_recalc_rate,
296 .determine_rate = jh7110_clk_determine_rate,
297 .get_parent = jh7110_clk_get_parent,
298 .set_parent = jh7110_clk_set_parent,
299 .set_rate = jh7110_clk_set_rate,
300 .debug_init = jh7110_clk_debug_init,
303 static const struct clk_ops jh7110_clk_inv_ops = {
304 .get_phase = jh7110_clk_get_phase,
305 .set_phase = jh7110_clk_set_phase,
306 .debug_init = jh7110_clk_debug_init,
309 const struct clk_ops *starfive_jh7110_clk_ops(u32 max)
311 const struct clk_ops *ops;
313 if (max & JH7110_CLK_DIV_MASK) {
314 if (max & JH7110_CLK_MUX_MASK) {
315 if (max & JH7110_CLK_ENABLE)
316 ops = &jh7110_clk_gmd_ops;
318 ops = &jh7110_clk_mdiv_ops;
319 } else if (max & JH7110_CLK_ENABLE)
320 ops = &jh7110_clk_gdiv_ops;
322 ops = &jh7110_clk_div_ops;
323 } else if (max & JH7110_CLK_MUX_MASK) {
324 if (max & JH7110_CLK_ENABLE)
325 ops = &jh7110_clk_gmux_ops;
327 ops = &jh7110_clk_mux_ops;
328 } else if (max & JH7110_CLK_ENABLE)
329 ops = &jh7110_clk_gate_ops;
331 ops = &jh7110_clk_inv_ops;
335 EXPORT_SYMBOL_GPL(starfive_jh7110_clk_ops);
337 static struct clk_hw *jh7110_clk_get(struct of_phandle_args *clkspec,
340 struct jh7110_clk_priv *priv = data;
341 unsigned int idx = clkspec->args[0];
343 if (idx < JH7110_PLL0_OUT)
344 return &priv->reg[idx].hw;
346 if (idx < JH7110_CLK_END) {
347 #ifdef CONFIG_CLK_STARFIVE_JH7110_PLL
348 if ((idx == JH7110_PLL0_OUT) || (idx == JH7110_PLL2_OUT))
349 return &priv->pll_priv[PLL_OF(idx)].hw;
351 return priv->pll[PLL_OF(idx)];
354 return ERR_PTR(-EINVAL);
358 static int __init clk_starfive_jh7110_probe(struct platform_device *pdev)
360 struct jh7110_clk_priv *priv;
363 priv = devm_kzalloc(&pdev->dev, struct_size(priv, reg, JH7110_PLL0_OUT),
368 spin_lock_init(&priv->rmw_lock);
369 priv->dev = &pdev->dev;
371 #ifdef CONFIG_CLK_STARFIVE_JH7110_PLL
372 ret = clk_starfive_jh7110_pll_init(pdev, priv->pll_priv);
377 ret = clk_starfive_jh7110_sys_init(pdev, priv);
381 ret = clk_starfive_jh7110_stg_init(pdev, priv);
385 ret = clk_starfive_jh7110_aon_init(pdev, priv);
389 ret = devm_of_clk_add_hw_provider(priv->dev, jh7110_clk_get, priv);
393 dev_info(&pdev->dev, "starfive JH7110 clkgen init successfully.");
397 static const struct of_device_id clk_starfive_jh7110_match[] = {
398 {.compatible = "starfive,jh7110-clkgen"},
402 static struct platform_driver clk_starfive_jh7110_driver = {
404 .name = "clk-starfive-jh7110",
405 .of_match_table = clk_starfive_jh7110_match,
408 builtin_platform_driver_probe(clk_starfive_jh7110_driver,
409 clk_starfive_jh7110_probe);
411 MODULE_AUTHOR("Xingyu Wu <xingyu.wu@starfivetech.com>");
412 MODULE_DESCRIPTION("StarFive JH7110 sysgen clock driver");
413 MODULE_LICENSE("GPL");