2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/err.h>
17 #include <linux/delay.h>
18 #include <linux/export.h>
19 #include <linux/clk-provider.h>
20 #include <linux/regmap.h>
22 #include "clk-branch.h"
24 static bool clk_branch_in_hwcg_mode(const struct clk_branch
*br
)
31 regmap_read(br
->clkr
.regmap
, br
->hwcg_reg
, &val
);
33 return !!(val
& BIT(br
->hwcg_bit
));
36 static bool clk_branch_check_halt(const struct clk_branch
*br
, bool enabling
)
38 bool invert
= (br
->halt_check
== BRANCH_HALT_ENABLE
);
41 regmap_read(br
->clkr
.regmap
, br
->halt_reg
, &val
);
43 val
&= BIT(br
->halt_bit
);
47 return !!val
== !enabling
;
50 #define BRANCH_CLK_OFF BIT(31)
51 #define BRANCH_NOC_FSM_STATUS_SHIFT 28
52 #define BRANCH_NOC_FSM_STATUS_MASK 0x7
53 #define BRANCH_NOC_FSM_STATUS_ON (0x2 << BRANCH_NOC_FSM_STATUS_SHIFT)
55 static bool clk_branch2_check_halt(const struct clk_branch
*br
, bool enabling
)
60 mask
= BRANCH_NOC_FSM_STATUS_MASK
<< BRANCH_NOC_FSM_STATUS_SHIFT
;
61 mask
|= BRANCH_CLK_OFF
;
63 regmap_read(br
->clkr
.regmap
, br
->halt_reg
, &val
);
67 return (val
& BRANCH_CLK_OFF
) == 0 ||
68 val
== BRANCH_NOC_FSM_STATUS_ON
;
70 return val
& BRANCH_CLK_OFF
;
74 static int clk_branch_wait(const struct clk_branch
*br
, bool enabling
,
75 bool (check_halt
)(const struct clk_branch
*, bool))
77 bool voted
= br
->halt_check
& BRANCH_VOTED
;
78 const char *name
= clk_hw_get_name(&br
->clkr
.hw
);
81 * Skip checking halt bit if we're explicitly ignoring the bit or the
82 * clock is in hardware gated mode
84 if (br
->halt_check
== BRANCH_HALT_SKIP
|| clk_branch_in_hwcg_mode(br
))
87 if (br
->halt_check
== BRANCH_HALT_DELAY
|| (!enabling
&& voted
)) {
89 } else if (br
->halt_check
== BRANCH_HALT_ENABLE
||
90 br
->halt_check
== BRANCH_HALT
||
91 (enabling
&& voted
)) {
95 if (check_halt(br
, enabling
))
99 WARN(1, "%s status stuck at 'o%s'", name
,
100 enabling
? "ff" : "n");
106 static int clk_branch_toggle(struct clk_hw
*hw
, bool en
,
107 bool (check_halt
)(const struct clk_branch
*, bool))
109 struct clk_branch
*br
= to_clk_branch(hw
);
113 ret
= clk_enable_regmap(hw
);
117 clk_disable_regmap(hw
);
120 return clk_branch_wait(br
, en
, check_halt
);
123 static int clk_branch_enable(struct clk_hw
*hw
)
125 return clk_branch_toggle(hw
, true, clk_branch_check_halt
);
128 static void clk_branch_disable(struct clk_hw
*hw
)
130 clk_branch_toggle(hw
, false, clk_branch_check_halt
);
133 const struct clk_ops clk_branch_ops
= {
134 .enable
= clk_branch_enable
,
135 .disable
= clk_branch_disable
,
136 .is_enabled
= clk_is_enabled_regmap
,
138 EXPORT_SYMBOL_GPL(clk_branch_ops
);
140 static int clk_branch2_enable(struct clk_hw
*hw
)
142 return clk_branch_toggle(hw
, true, clk_branch2_check_halt
);
145 static void clk_branch2_disable(struct clk_hw
*hw
)
147 clk_branch_toggle(hw
, false, clk_branch2_check_halt
);
150 const struct clk_ops clk_branch2_ops
= {
151 .enable
= clk_branch2_enable
,
152 .disable
= clk_branch2_disable
,
153 .is_enabled
= clk_is_enabled_regmap
,
155 EXPORT_SYMBOL_GPL(clk_branch2_ops
);
157 const struct clk_ops clk_branch_simple_ops
= {
158 .enable
= clk_enable_regmap
,
159 .disable
= clk_disable_regmap
,
160 .is_enabled
= clk_is_enabled_regmap
,
162 EXPORT_SYMBOL_GPL(clk_branch_simple_ops
);