blob: 19e001eaada9de952206a7faa11ec2fe3368ae47 [file] [log] [blame]
Stephen Boyd6e0ad1b2014-01-15 10:47:26 -08001/*
Taniya Das0192f782016-07-14 11:57:53 +05302 * Copyright (c) 2013, 2016, The Linux Foundation. All rights reserved.
Stephen Boyd6e0ad1b2014-01-15 10:47:26 -08003 *
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.
7 *
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.
12 */
13
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>
21
22#include "clk-branch.h"
23
24static bool clk_branch_in_hwcg_mode(const struct clk_branch *br)
25{
26 u32 val;
27
28 if (!br->hwcg_reg)
29 return 0;
30
31 regmap_read(br->clkr.regmap, br->hwcg_reg, &val);
32
33 return !!(val & BIT(br->hwcg_bit));
34}
35
36static bool clk_branch_check_halt(const struct clk_branch *br, bool enabling)
37{
38 bool invert = (br->halt_check == BRANCH_HALT_ENABLE);
39 u32 val;
40
41 regmap_read(br->clkr.regmap, br->halt_reg, &val);
42
43 val &= BIT(br->halt_bit);
44 if (invert)
45 val = !val;
46
47 return !!val == !enabling;
48}
49
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)
54
55static bool clk_branch2_check_halt(const struct clk_branch *br, bool enabling)
56{
57 u32 val;
58 u32 mask;
59
60 mask = BRANCH_NOC_FSM_STATUS_MASK << BRANCH_NOC_FSM_STATUS_SHIFT;
61 mask |= BRANCH_CLK_OFF;
62
63 regmap_read(br->clkr.regmap, br->halt_reg, &val);
64
65 if (enabling) {
66 val &= mask;
67 return (val & BRANCH_CLK_OFF) == 0 ||
68 val == BRANCH_NOC_FSM_STATUS_ON;
69 } else {
70 return val & BRANCH_CLK_OFF;
71 }
72}
73
74static int clk_branch_wait(const struct clk_branch *br, bool enabling,
75 bool (check_halt)(const struct clk_branch *, bool))
76{
77 bool voted = br->halt_check & BRANCH_VOTED;
Stephen Boyd836ee0f2015-08-12 11:42:23 -070078 const char *name = clk_hw_get_name(&br->clkr.hw);
Stephen Boyd6e0ad1b2014-01-15 10:47:26 -080079
80 /* Skip checking halt bit if the clock is in hardware gated mode */
81 if (clk_branch_in_hwcg_mode(br))
82 return 0;
83
84 if (br->halt_check == BRANCH_HALT_DELAY || (!enabling && voted)) {
85 udelay(10);
86 } else if (br->halt_check == BRANCH_HALT_ENABLE ||
87 br->halt_check == BRANCH_HALT ||
88 (enabling && voted)) {
89 int count = 200;
90
91 while (count-- > 0) {
92 if (check_halt(br, enabling))
93 return 0;
94 udelay(1);
95 }
96 WARN(1, "%s status stuck at 'o%s'", name,
97 enabling ? "ff" : "n");
98 return -EBUSY;
99 }
100 return 0;
101}
102
103static int clk_branch_toggle(struct clk_hw *hw, bool en,
104 bool (check_halt)(const struct clk_branch *, bool))
105{
106 struct clk_branch *br = to_clk_branch(hw);
107 int ret;
108
109 if (en) {
110 ret = clk_enable_regmap(hw);
111 if (ret)
112 return ret;
113 } else {
114 clk_disable_regmap(hw);
115 }
116
117 return clk_branch_wait(br, en, check_halt);
118}
119
120static int clk_branch_enable(struct clk_hw *hw)
121{
122 return clk_branch_toggle(hw, true, clk_branch_check_halt);
123}
124
Taniya Das0192f782016-07-14 11:57:53 +0530125static int clk_cbcr_set_flags(struct regmap *regmap, unsigned int reg,
126 unsigned long flags)
127{
128 u32 cbcr_val;
129
130 regmap_read(regmap, reg, &cbcr_val);
131
132 switch (flags) {
133 case CLKFLAG_PERIPH_OFF_SET:
134 cbcr_val |= BIT(12);
135 break;
136 case CLKFLAG_PERIPH_OFF_CLEAR:
137 cbcr_val &= ~BIT(12);
138 break;
139 case CLKFLAG_RETAIN_PERIPH:
140 cbcr_val |= BIT(13);
141 break;
142 case CLKFLAG_NORETAIN_PERIPH:
143 cbcr_val &= ~BIT(13);
144 break;
145 case CLKFLAG_RETAIN_MEM:
146 cbcr_val |= BIT(14);
147 break;
148 case CLKFLAG_NORETAIN_MEM:
149 cbcr_val &= ~BIT(14);
150 break;
151 default:
152 return -EINVAL;
153 }
154
155 regmap_write(regmap, reg, cbcr_val);
156
157 /* Make sure power is enabled/disabled before returning. */
158 mb();
159 udelay(1);
160
161 return 0;
162}
163
Stephen Boyd6e0ad1b2014-01-15 10:47:26 -0800164static void clk_branch_disable(struct clk_hw *hw)
165{
166 clk_branch_toggle(hw, false, clk_branch_check_halt);
167}
168
Taniya Das0192f782016-07-14 11:57:53 +0530169static int clk_branch_set_flags(struct clk_hw *hw, unsigned int flags)
170{
171 struct clk_branch *br = to_clk_branch(hw);
172
173 return clk_cbcr_set_flags(br->clkr.regmap, br->halt_reg, flags);
174}
175
Stephen Boyd6e0ad1b2014-01-15 10:47:26 -0800176const struct clk_ops clk_branch_ops = {
177 .enable = clk_branch_enable,
178 .disable = clk_branch_disable,
179 .is_enabled = clk_is_enabled_regmap,
Taniya Das0192f782016-07-14 11:57:53 +0530180 .set_flags = clk_branch_set_flags,
Stephen Boyd6e0ad1b2014-01-15 10:47:26 -0800181};
182EXPORT_SYMBOL_GPL(clk_branch_ops);
183
184static int clk_branch2_enable(struct clk_hw *hw)
185{
186 return clk_branch_toggle(hw, true, clk_branch2_check_halt);
187}
188
189static void clk_branch2_disable(struct clk_hw *hw)
190{
191 clk_branch_toggle(hw, false, clk_branch2_check_halt);
192}
193
194const struct clk_ops clk_branch2_ops = {
195 .enable = clk_branch2_enable,
196 .disable = clk_branch2_disable,
197 .is_enabled = clk_is_enabled_regmap,
Taniya Das0192f782016-07-14 11:57:53 +0530198 .set_flags = clk_branch_set_flags,
Stephen Boyd6e0ad1b2014-01-15 10:47:26 -0800199};
200EXPORT_SYMBOL_GPL(clk_branch2_ops);
201
202const struct clk_ops clk_branch_simple_ops = {
203 .enable = clk_enable_regmap,
204 .disable = clk_disable_regmap,
205 .is_enabled = clk_is_enabled_regmap,
206};
207EXPORT_SYMBOL_GPL(clk_branch_simple_ops);