Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / clk / sunxi-ng / ccu_gate.c
blob3d5ca092b08f2d0eb2021dfdf23904f4d590d3a1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 */
7 #include <linux/clk-provider.h>
8 #include <linux/io.h>
10 #include "ccu_gate.h"
12 void ccu_gate_helper_disable(struct ccu_common *common, u32 gate)
14 unsigned long flags;
15 u32 reg;
17 if (!gate)
18 return;
20 spin_lock_irqsave(common->lock, flags);
22 reg = readl(common->base + common->reg);
23 writel(reg & ~gate, common->base + common->reg);
25 spin_unlock_irqrestore(common->lock, flags);
28 static void ccu_gate_disable(struct clk_hw *hw)
30 struct ccu_gate *cg = hw_to_ccu_gate(hw);
32 return ccu_gate_helper_disable(&cg->common, cg->enable);
35 int ccu_gate_helper_enable(struct ccu_common *common, u32 gate)
37 unsigned long flags;
38 u32 reg;
40 if (!gate)
41 return 0;
43 spin_lock_irqsave(common->lock, flags);
45 reg = readl(common->base + common->reg);
46 writel(reg | gate, common->base + common->reg);
48 spin_unlock_irqrestore(common->lock, flags);
50 return 0;
53 static int ccu_gate_enable(struct clk_hw *hw)
55 struct ccu_gate *cg = hw_to_ccu_gate(hw);
57 return ccu_gate_helper_enable(&cg->common, cg->enable);
60 int ccu_gate_helper_is_enabled(struct ccu_common *common, u32 gate)
62 if (!gate)
63 return 1;
65 return readl(common->base + common->reg) & gate;
68 static int ccu_gate_is_enabled(struct clk_hw *hw)
70 struct ccu_gate *cg = hw_to_ccu_gate(hw);
72 return ccu_gate_helper_is_enabled(&cg->common, cg->enable);
75 static unsigned long ccu_gate_recalc_rate(struct clk_hw *hw,
76 unsigned long parent_rate)
78 struct ccu_gate *cg = hw_to_ccu_gate(hw);
79 unsigned long rate = parent_rate;
81 if (cg->common.features & CCU_FEATURE_ALL_PREDIV)
82 rate /= cg->common.prediv;
84 return rate;
87 static long ccu_gate_round_rate(struct clk_hw *hw, unsigned long rate,
88 unsigned long *prate)
90 struct ccu_gate *cg = hw_to_ccu_gate(hw);
91 int div = 1;
93 if (cg->common.features & CCU_FEATURE_ALL_PREDIV)
94 div = cg->common.prediv;
96 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
97 unsigned long best_parent = rate;
99 if (cg->common.features & CCU_FEATURE_ALL_PREDIV)
100 best_parent *= div;
101 *prate = clk_hw_round_rate(clk_hw_get_parent(hw), best_parent);
104 return *prate / div;
107 static int ccu_gate_set_rate(struct clk_hw *hw, unsigned long rate,
108 unsigned long parent_rate)
111 * We must report success but we can do so unconditionally because
112 * clk_factor_round_rate returns values that ensure this call is a
113 * nop.
116 return 0;
119 const struct clk_ops ccu_gate_ops = {
120 .disable = ccu_gate_disable,
121 .enable = ccu_gate_enable,
122 .is_enabled = ccu_gate_is_enabled,
123 .round_rate = ccu_gate_round_rate,
124 .set_rate = ccu_gate_set_rate,
125 .recalc_rate = ccu_gate_recalc_rate,