1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 #include <linux/clk-provider.h>
12 void ccu_gate_helper_disable(struct ccu_common
*common
, u32 gate
)
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
)
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
);
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
)
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
;
87 static long ccu_gate_round_rate(struct clk_hw
*hw
, unsigned long rate
,
90 struct ccu_gate
*cg
= hw_to_ccu_gate(hw
);
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
)
101 *prate
= clk_hw_round_rate(clk_hw_get_parent(hw
), best_parent
);
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
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
,