blob: 4735de0660cc912f1bafea1e31388bcf9843543d [file] [log] [blame]
Prashant Gaikwadece70092013-03-20 17:30:34 +05301/*
2 * Copyright (c) 2013 NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include <linux/clk.h>
18#include <linux/clk-provider.h>
19#include <linux/err.h>
20#include <linux/slab.h>
21
22#define to_clk_composite(_hw) container_of(_hw, struct clk_composite, hw)
23
24static u8 clk_composite_get_parent(struct clk_hw *hw)
25{
26 struct clk_composite *composite = to_clk_composite(hw);
27 const struct clk_ops *mux_ops = composite->mux_ops;
28 struct clk_hw *mux_hw = composite->mux_hw;
29
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010030 __clk_hw_set_clk(mux_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +053031
32 return mux_ops->get_parent(mux_hw);
33}
34
35static int clk_composite_set_parent(struct clk_hw *hw, u8 index)
36{
37 struct clk_composite *composite = to_clk_composite(hw);
38 const struct clk_ops *mux_ops = composite->mux_ops;
39 struct clk_hw *mux_hw = composite->mux_hw;
40
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010041 __clk_hw_set_clk(mux_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +053042
43 return mux_ops->set_parent(mux_hw, index);
44}
45
46static unsigned long clk_composite_recalc_rate(struct clk_hw *hw,
47 unsigned long parent_rate)
48{
49 struct clk_composite *composite = to_clk_composite(hw);
Mike Turquetted3a1c7b2013-04-11 11:31:36 -070050 const struct clk_ops *rate_ops = composite->rate_ops;
51 struct clk_hw *rate_hw = composite->rate_hw;
Prashant Gaikwadece70092013-03-20 17:30:34 +053052
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010053 __clk_hw_set_clk(rate_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +053054
Mike Turquetted3a1c7b2013-04-11 11:31:36 -070055 return rate_ops->recalc_rate(rate_hw, parent_rate);
Prashant Gaikwadece70092013-03-20 17:30:34 +053056}
57
Boris Brezillon0817b622015-07-07 20:48:08 +020058static int clk_composite_determine_rate(struct clk_hw *hw,
59 struct clk_rate_request *req)
Emilio López107f3192013-09-14 21:37:59 -030060{
61 struct clk_composite *composite = to_clk_composite(hw);
62 const struct clk_ops *rate_ops = composite->rate_ops;
63 const struct clk_ops *mux_ops = composite->mux_ops;
64 struct clk_hw *rate_hw = composite->rate_hw;
65 struct clk_hw *mux_hw = composite->mux_hw;
Stephen Boyd2f508a92015-07-30 17:20:57 -070066 struct clk_hw *parent;
Boris BREZILLON3eb635f2014-07-03 01:56:45 +020067 unsigned long parent_rate;
68 long tmp_rate, best_rate = 0;
69 unsigned long rate_diff;
70 unsigned long best_rate_diff = ULONG_MAX;
Boris Brezillon0817b622015-07-07 20:48:08 +020071 long rate;
Boris BREZILLON3eb635f2014-07-03 01:56:45 +020072 int i;
Emilio López107f3192013-09-14 21:37:59 -030073
74 if (rate_hw && rate_ops && rate_ops->determine_rate) {
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +010075 __clk_hw_set_clk(rate_hw, hw);
Boris Brezillon0817b622015-07-07 20:48:08 +020076 return rate_ops->determine_rate(rate_hw, req);
Boris BREZILLON3eb635f2014-07-03 01:56:45 +020077 } else if (rate_hw && rate_ops && rate_ops->round_rate &&
78 mux_hw && mux_ops && mux_ops->set_parent) {
Boris Brezillon0817b622015-07-07 20:48:08 +020079 req->best_parent_hw = NULL;
Boris BREZILLON3eb635f2014-07-03 01:56:45 +020080
Stephen Boyd98d8a602015-06-29 16:56:30 -070081 if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) {
Stephen Boyd2f508a92015-07-30 17:20:57 -070082 parent = clk_hw_get_parent(mux_hw);
83 req->best_parent_hw = parent;
84 req->best_parent_rate = clk_hw_get_rate(parent);
Boris BREZILLON3eb635f2014-07-03 01:56:45 +020085
Boris Brezillon0817b622015-07-07 20:48:08 +020086 rate = rate_ops->round_rate(rate_hw, req->rate,
87 &req->best_parent_rate);
88 if (rate < 0)
89 return rate;
90
91 req->rate = rate;
92 return 0;
Boris BREZILLON3eb635f2014-07-03 01:56:45 +020093 }
94
Stephen Boyd497295a2015-06-25 16:53:23 -070095 for (i = 0; i < clk_hw_get_num_parents(mux_hw); i++) {
Stephen Boyd2f508a92015-07-30 17:20:57 -070096 parent = clk_hw_get_parent_by_index(mux_hw, i);
Boris BREZILLON3eb635f2014-07-03 01:56:45 +020097 if (!parent)
98 continue;
99
Stephen Boyd2f508a92015-07-30 17:20:57 -0700100 parent_rate = clk_hw_get_rate(parent);
Boris BREZILLON3eb635f2014-07-03 01:56:45 +0200101
Boris Brezillon0817b622015-07-07 20:48:08 +0200102 tmp_rate = rate_ops->round_rate(rate_hw, req->rate,
Boris BREZILLON3eb635f2014-07-03 01:56:45 +0200103 &parent_rate);
104 if (tmp_rate < 0)
105 continue;
106
Boris Brezillon0817b622015-07-07 20:48:08 +0200107 rate_diff = abs(req->rate - tmp_rate);
Boris BREZILLON3eb635f2014-07-03 01:56:45 +0200108
Boris Brezillon0817b622015-07-07 20:48:08 +0200109 if (!rate_diff || !req->best_parent_hw
Boris BREZILLON3eb635f2014-07-03 01:56:45 +0200110 || best_rate_diff > rate_diff) {
Stephen Boyd2f508a92015-07-30 17:20:57 -0700111 req->best_parent_hw = parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200112 req->best_parent_rate = parent_rate;
Boris BREZILLON3eb635f2014-07-03 01:56:45 +0200113 best_rate_diff = rate_diff;
114 best_rate = tmp_rate;
115 }
116
117 if (!rate_diff)
Boris Brezillon0817b622015-07-07 20:48:08 +0200118 return 0;
Boris BREZILLON3eb635f2014-07-03 01:56:45 +0200119 }
120
Boris Brezillon0817b622015-07-07 20:48:08 +0200121 req->rate = best_rate;
122 return 0;
Emilio López107f3192013-09-14 21:37:59 -0300123 } else if (mux_hw && mux_ops && mux_ops->determine_rate) {
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100124 __clk_hw_set_clk(mux_hw, hw);
Boris Brezillon0817b622015-07-07 20:48:08 +0200125 return mux_ops->determine_rate(mux_hw, req);
Emilio López107f3192013-09-14 21:37:59 -0300126 } else {
127 pr_err("clk: clk_composite_determine_rate function called, but no mux or rate callback set!\n");
Boris Brezillon57d866e2015-07-09 22:39:38 +0200128 return -EINVAL;
Emilio López107f3192013-09-14 21:37:59 -0300129 }
130}
131
Prashant Gaikwadece70092013-03-20 17:30:34 +0530132static long clk_composite_round_rate(struct clk_hw *hw, unsigned long rate,
133 unsigned long *prate)
134{
135 struct clk_composite *composite = to_clk_composite(hw);
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700136 const struct clk_ops *rate_ops = composite->rate_ops;
137 struct clk_hw *rate_hw = composite->rate_hw;
Prashant Gaikwadece70092013-03-20 17:30:34 +0530138
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100139 __clk_hw_set_clk(rate_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530140
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700141 return rate_ops->round_rate(rate_hw, rate, prate);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530142}
143
144static int clk_composite_set_rate(struct clk_hw *hw, unsigned long rate,
145 unsigned long parent_rate)
146{
147 struct clk_composite *composite = to_clk_composite(hw);
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700148 const struct clk_ops *rate_ops = composite->rate_ops;
149 struct clk_hw *rate_hw = composite->rate_hw;
Prashant Gaikwadece70092013-03-20 17:30:34 +0530150
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100151 __clk_hw_set_clk(rate_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530152
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700153 return rate_ops->set_rate(rate_hw, rate, parent_rate);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530154}
155
156static int clk_composite_is_enabled(struct clk_hw *hw)
157{
158 struct clk_composite *composite = to_clk_composite(hw);
159 const struct clk_ops *gate_ops = composite->gate_ops;
160 struct clk_hw *gate_hw = composite->gate_hw;
161
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100162 __clk_hw_set_clk(gate_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530163
164 return gate_ops->is_enabled(gate_hw);
165}
166
167static int clk_composite_enable(struct clk_hw *hw)
168{
169 struct clk_composite *composite = to_clk_composite(hw);
170 const struct clk_ops *gate_ops = composite->gate_ops;
171 struct clk_hw *gate_hw = composite->gate_hw;
172
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100173 __clk_hw_set_clk(gate_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530174
175 return gate_ops->enable(gate_hw);
176}
177
178static void clk_composite_disable(struct clk_hw *hw)
179{
180 struct clk_composite *composite = to_clk_composite(hw);
181 const struct clk_ops *gate_ops = composite->gate_ops;
182 struct clk_hw *gate_hw = composite->gate_hw;
183
Javier Martinez Canillas4e907ef2015-02-12 14:58:30 +0100184 __clk_hw_set_clk(gate_hw, hw);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530185
186 gate_ops->disable(gate_hw);
187}
188
189struct clk *clk_register_composite(struct device *dev, const char *name,
Sascha Hauer2893c372015-03-31 20:16:52 +0200190 const char * const *parent_names, int num_parents,
Prashant Gaikwadece70092013-03-20 17:30:34 +0530191 struct clk_hw *mux_hw, const struct clk_ops *mux_ops,
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700192 struct clk_hw *rate_hw, const struct clk_ops *rate_ops,
Prashant Gaikwadece70092013-03-20 17:30:34 +0530193 struct clk_hw *gate_hw, const struct clk_ops *gate_ops,
194 unsigned long flags)
195{
196 struct clk *clk;
197 struct clk_init_data init;
198 struct clk_composite *composite;
199 struct clk_ops *clk_composite_ops;
200
201 composite = kzalloc(sizeof(*composite), GFP_KERNEL);
Stephen Boydd122db72015-05-14 16:47:10 -0700202 if (!composite)
Prashant Gaikwadece70092013-03-20 17:30:34 +0530203 return ERR_PTR(-ENOMEM);
Prashant Gaikwadece70092013-03-20 17:30:34 +0530204
205 init.name = name;
206 init.flags = flags | CLK_IS_BASIC;
207 init.parent_names = parent_names;
208 init.num_parents = num_parents;
209
210 clk_composite_ops = &composite->ops;
211
212 if (mux_hw && mux_ops) {
Heiko Stübner0c02cf22014-07-03 01:57:30 +0200213 if (!mux_ops->get_parent) {
Prashant Gaikwadece70092013-03-20 17:30:34 +0530214 clk = ERR_PTR(-EINVAL);
215 goto err;
216 }
217
218 composite->mux_hw = mux_hw;
219 composite->mux_ops = mux_ops;
220 clk_composite_ops->get_parent = clk_composite_get_parent;
Heiko Stübner0c02cf22014-07-03 01:57:30 +0200221 if (mux_ops->set_parent)
222 clk_composite_ops->set_parent = clk_composite_set_parent;
Emilio López107f3192013-09-14 21:37:59 -0300223 if (mux_ops->determine_rate)
224 clk_composite_ops->determine_rate = clk_composite_determine_rate;
Prashant Gaikwadece70092013-03-20 17:30:34 +0530225 }
226
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700227 if (rate_hw && rate_ops) {
Mike Turquettef363e212013-04-11 11:31:37 -0700228 if (!rate_ops->recalc_rate) {
Prashant Gaikwadece70092013-03-20 17:30:34 +0530229 clk = ERR_PTR(-EINVAL);
230 goto err;
231 }
Mike Turquette5a994e12014-07-03 01:58:14 +0200232 clk_composite_ops->recalc_rate = clk_composite_recalc_rate;
Prashant Gaikwadece70092013-03-20 17:30:34 +0530233
Mike Turquette5a994e12014-07-03 01:58:14 +0200234 if (rate_ops->determine_rate)
235 clk_composite_ops->determine_rate =
236 clk_composite_determine_rate;
237 else if (rate_ops->round_rate)
238 clk_composite_ops->round_rate =
239 clk_composite_round_rate;
240
241 /* .set_rate requires either .round_rate or .determine_rate */
242 if (rate_ops->set_rate) {
243 if (rate_ops->determine_rate || rate_ops->round_rate)
244 clk_composite_ops->set_rate =
245 clk_composite_set_rate;
246 else
247 WARN(1, "%s: missing round_rate op is required\n",
248 __func__);
Mike Turquettef363e212013-04-11 11:31:37 -0700249 }
250
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700251 composite->rate_hw = rate_hw;
252 composite->rate_ops = rate_ops;
Prashant Gaikwadece70092013-03-20 17:30:34 +0530253 }
254
255 if (gate_hw && gate_ops) {
256 if (!gate_ops->is_enabled || !gate_ops->enable ||
257 !gate_ops->disable) {
258 clk = ERR_PTR(-EINVAL);
259 goto err;
260 }
261
262 composite->gate_hw = gate_hw;
263 composite->gate_ops = gate_ops;
264 clk_composite_ops->is_enabled = clk_composite_is_enabled;
265 clk_composite_ops->enable = clk_composite_enable;
266 clk_composite_ops->disable = clk_composite_disable;
267 }
268
269 init.ops = clk_composite_ops;
270 composite->hw.init = &init;
271
272 clk = clk_register(dev, &composite->hw);
273 if (IS_ERR(clk))
274 goto err;
275
276 if (composite->mux_hw)
277 composite->mux_hw->clk = clk;
278
Mike Turquetted3a1c7b2013-04-11 11:31:36 -0700279 if (composite->rate_hw)
280 composite->rate_hw->clk = clk;
Prashant Gaikwadece70092013-03-20 17:30:34 +0530281
282 if (composite->gate_hw)
283 composite->gate_hw->clk = clk;
284
285 return clk;
286
287err:
288 kfree(composite);
289 return clk;
290}