blob: dfe5e3e32d28f050388f05cb0247f101eb69dc74 [file] [log] [blame]
Emilio Lópeze874a662013-02-25 11:44:26 -03001/*
2 * Copyright (C) 2013 Emilio López <emilio@elopez.com.ar>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Adjustable factor-based clock implementation
9 */
10
11#include <linux/clk-provider.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030012#include <linux/delay.h>
Maxime Ripard601da9d2014-07-04 22:24:52 +020013#include <linux/err.h>
14#include <linux/io.h>
Maxime Ripard601da9d2014-07-04 22:24:52 +020015#include <linux/of_address.h>
16#include <linux/slab.h>
17#include <linux/string.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030018
19#include "clk-factors.h"
20
21/*
Maxime Ripard601da9d2014-07-04 22:24:52 +020022 * DOC: basic adjustable factor-based clock
Emilio Lópeze874a662013-02-25 11:44:26 -030023 *
24 * Traits of this clock:
25 * prepare - clk_prepare only ensures that parents are prepared
26 * enable - clk_enable only ensures that parents are enabled
27 * rate - rate is adjustable.
28 * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
29 * parent - fixed parent. No clk_set_parent support
30 */
31
Emilio Lópeze874a662013-02-25 11:44:26 -030032#define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
33
Maxime Ripard601da9d2014-07-04 22:24:52 +020034#define FACTORS_MAX_PARENTS 5
35
Emilio Lópezc518e842013-09-20 22:03:10 -030036#define SETMASK(len, pos) (((1U << (len)) - 1) << (pos))
Emilio Lópeze874a662013-02-25 11:44:26 -030037#define CLRMASK(len, pos) (~(SETMASK(len, pos)))
38#define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit))
39
40#define FACTOR_SET(bit, len, reg, val) \
41 (((reg) & CLRMASK(len, bit)) | (val << (bit)))
42
43static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
44 unsigned long parent_rate)
45{
46 u8 n = 1, k = 0, p = 0, m = 0;
47 u32 reg;
48 unsigned long rate;
49 struct clk_factors *factors = to_clk_factors(hw);
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +080050 const struct clk_factors_config *config = factors->config;
Emilio Lópeze874a662013-02-25 11:44:26 -030051
52 /* Fetch the register value */
53 reg = readl(factors->reg);
54
55 /* Get each individual factor if applicable */
56 if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
57 n = FACTOR_GET(config->nshift, config->nwidth, reg);
58 if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
59 k = FACTOR_GET(config->kshift, config->kwidth, reg);
60 if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
61 m = FACTOR_GET(config->mshift, config->mwidth, reg);
62 if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
63 p = FACTOR_GET(config->pshift, config->pwidth, reg);
64
Chen-Yu Tsai435b7be2016-01-25 21:15:43 +080065 if (factors->recalc) {
66 struct factors_request factors_req = {
67 .parent_rate = parent_rate,
68 .n = n,
69 .k = k,
70 .m = m,
71 .p = p,
72 };
73
74 /* get mux details from mux clk structure */
75 if (factors->mux)
76 factors_req.parent_index =
77 (reg >> factors->mux->shift) &
78 factors->mux->mask;
79
80 factors->recalc(&factors_req);
81
82 return factors_req.rate;
83 }
84
Emilio Lópeze874a662013-02-25 11:44:26 -030085 /* Calculate the rate */
Chen-Yu Tsai9a5e6c72014-06-26 23:55:41 +080086 rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1);
Emilio Lópeze874a662013-02-25 11:44:26 -030087
88 return rate;
89}
90
Boris Brezillon0817b622015-07-07 20:48:08 +020091static int clk_factors_determine_rate(struct clk_hw *hw,
92 struct clk_rate_request *req)
Emilio López862b7282014-05-02 17:57:15 +020093{
Chen-Yu Tsai435b7be2016-01-25 21:15:43 +080094 struct clk_factors *factors = to_clk_factors(hw);
Stephen Boyd1b14afa2015-07-30 17:20:57 -070095 struct clk_hw *parent, *best_parent = NULL;
Emilio López862b7282014-05-02 17:57:15 +020096 int i, num_parents;
97 unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
98
99 /* find the parent that can help provide the fastest rate <= rate */
Stephen Boyd497295a2015-06-25 16:53:23 -0700100 num_parents = clk_hw_get_num_parents(hw);
Emilio López862b7282014-05-02 17:57:15 +0200101 for (i = 0; i < num_parents; i++) {
Chen-Yu Tsai435b7be2016-01-25 21:15:43 +0800102 struct factors_request factors_req = {
103 .rate = req->rate,
104 .parent_index = i,
105 };
Stephen Boyd1b14afa2015-07-30 17:20:57 -0700106 parent = clk_hw_get_parent_by_index(hw, i);
Emilio López862b7282014-05-02 17:57:15 +0200107 if (!parent)
108 continue;
Stephen Boyd98d8a602015-06-29 16:56:30 -0700109 if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
Stephen Boyd1b14afa2015-07-30 17:20:57 -0700110 parent_rate = clk_hw_round_rate(parent, req->rate);
Emilio López862b7282014-05-02 17:57:15 +0200111 else
Stephen Boyd1b14afa2015-07-30 17:20:57 -0700112 parent_rate = clk_hw_get_rate(parent);
Emilio López862b7282014-05-02 17:57:15 +0200113
Chen-Yu Tsai435b7be2016-01-25 21:15:43 +0800114 factors_req.parent_rate = parent_rate;
115 factors->get_factors(&factors_req);
116 child_rate = factors_req.rate;
Emilio López862b7282014-05-02 17:57:15 +0200117
Boris Brezillon0817b622015-07-07 20:48:08 +0200118 if (child_rate <= req->rate && child_rate > best_child_rate) {
Emilio López862b7282014-05-02 17:57:15 +0200119 best_parent = parent;
120 best = parent_rate;
121 best_child_rate = child_rate;
122 }
123 }
124
Boris Brezillon57d866e2015-07-09 22:39:38 +0200125 if (!best_parent)
126 return -EINVAL;
127
Stephen Boyd1b14afa2015-07-30 17:20:57 -0700128 req->best_parent_hw = best_parent;
Boris Brezillon0817b622015-07-07 20:48:08 +0200129 req->best_parent_rate = best;
130 req->rate = best_child_rate;
Emilio López862b7282014-05-02 17:57:15 +0200131
Boris Brezillon0817b622015-07-07 20:48:08 +0200132 return 0;
Emilio López862b7282014-05-02 17:57:15 +0200133}
134
Emilio Lópeze874a662013-02-25 11:44:26 -0300135static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
136 unsigned long parent_rate)
137{
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800138 struct factors_request req = {
139 .rate = rate,
140 .parent_rate = parent_rate,
141 };
Emilio Lópeze874a662013-02-25 11:44:26 -0300142 u32 reg;
143 struct clk_factors *factors = to_clk_factors(hw);
Chen-Yu Tsaib3e919e2016-01-25 21:15:38 +0800144 const struct clk_factors_config *config = factors->config;
Emilio Lópeze874a662013-02-25 11:44:26 -0300145 unsigned long flags = 0;
146
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800147 factors->get_factors(&req);
Emilio Lópeze874a662013-02-25 11:44:26 -0300148
149 if (factors->lock)
150 spin_lock_irqsave(factors->lock, flags);
151
152 /* Fetch the register value */
153 reg = readl(factors->reg);
154
155 /* Set up the new factors - macros do not do anything if width is 0 */
Chen-Yu Tsaicfa636882016-01-25 21:15:42 +0800156 reg = FACTOR_SET(config->nshift, config->nwidth, reg, req.n);
157 reg = FACTOR_SET(config->kshift, config->kwidth, reg, req.k);
158 reg = FACTOR_SET(config->mshift, config->mwidth, reg, req.m);
159 reg = FACTOR_SET(config->pshift, config->pwidth, reg, req.p);
Emilio Lópeze874a662013-02-25 11:44:26 -0300160
161 /* Apply them now */
162 writel(reg, factors->reg);
163
164 /* delay 500us so pll stabilizes */
165 __delay((rate >> 20) * 500 / 2);
166
167 if (factors->lock)
168 spin_unlock_irqrestore(factors->lock, flags);
169
170 return 0;
171}
172
Maxime Ripard601da9d2014-07-04 22:24:52 +0200173static const struct clk_ops clk_factors_ops = {
Emilio López862b7282014-05-02 17:57:15 +0200174 .determine_rate = clk_factors_determine_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300175 .recalc_rate = clk_factors_recalc_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300176 .set_rate = clk_factors_set_rate,
177};
Maxime Ripard601da9d2014-07-04 22:24:52 +0200178
Hans de Goede7c74c222014-11-23 14:38:07 +0100179struct clk *sunxi_factors_register(struct device_node *node,
180 const struct factors_data *data,
181 spinlock_t *lock,
182 void __iomem *reg)
Maxime Ripard601da9d2014-07-04 22:24:52 +0200183{
184 struct clk *clk;
185 struct clk_factors *factors;
186 struct clk_gate *gate = NULL;
187 struct clk_mux *mux = NULL;
188 struct clk_hw *gate_hw = NULL;
189 struct clk_hw *mux_hw = NULL;
190 const char *clk_name = node->name;
191 const char *parents[FACTORS_MAX_PARENTS];
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800192 int ret, i = 0;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200193
Maxime Ripard601da9d2014-07-04 22:24:52 +0200194 /* if we have a mux, we will have >1 parents */
Dinh Nguyen8a53fb22015-07-06 22:59:05 -0500195 i = of_clk_parent_fill(node, parents, FACTORS_MAX_PARENTS);
Maxime Ripard601da9d2014-07-04 22:24:52 +0200196
197 /*
198 * some factor clocks, such as pll5 and pll6, may have multiple
199 * outputs, and have their name designated in factors_data
200 */
201 if (data->name)
202 clk_name = data->name;
203 else
204 of_property_read_string(node, "clock-output-names", &clk_name);
205
206 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
207 if (!factors)
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800208 goto err_factors;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200209
210 /* set up factors properties */
211 factors->reg = reg;
212 factors->config = data->table;
213 factors->get_factors = data->getter;
Chen-Yu Tsai435b7be2016-01-25 21:15:43 +0800214 factors->recalc = data->recalc;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200215 factors->lock = lock;
216
217 /* Add a gate if this factor clock can be gated */
218 if (data->enable) {
219 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800220 if (!gate)
221 goto err_gate;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200222
Chen-Yu Tsai4cbeaeb2016-01-25 21:15:40 +0800223 factors->gate = gate;
224
Maxime Ripard601da9d2014-07-04 22:24:52 +0200225 /* set up gate properties */
226 gate->reg = reg;
227 gate->bit_idx = data->enable;
228 gate->lock = factors->lock;
229 gate_hw = &gate->hw;
230 }
231
232 /* Add a mux if this factor clock can be muxed */
233 if (data->mux) {
234 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800235 if (!mux)
236 goto err_mux;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200237
Chen-Yu Tsai4cbeaeb2016-01-25 21:15:40 +0800238 factors->mux = mux;
239
Maxime Ripard601da9d2014-07-04 22:24:52 +0200240 /* set up gate properties */
241 mux->reg = reg;
242 mux->shift = data->mux;
Chen-Yu Tsaie94f8cb32014-10-20 22:10:26 +0800243 mux->mask = data->muxmask;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200244 mux->lock = factors->lock;
245 mux_hw = &mux->hw;
246 }
247
248 clk = clk_register_composite(NULL, clk_name,
249 parents, i,
250 mux_hw, &clk_mux_ops,
251 &factors->hw, &clk_factors_ops,
252 gate_hw, &clk_gate_ops, 0);
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800253 if (IS_ERR(clk))
254 goto err_register;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200255
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800256 ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
257 if (ret)
258 goto err_provider;
259
Maxime Ripard601da9d2014-07-04 22:24:52 +0200260 return clk;
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800261
Chen-Yu Tsai78ca95c2016-01-25 21:15:39 +0800262err_provider:
263 /* TODO: The composite clock stuff will leak a bit here. */
264 clk_unregister(clk);
265err_register:
266 kfree(mux);
267err_mux:
268 kfree(gate);
269err_gate:
270 kfree(factors);
271err_factors:
272 return NULL;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200273}
Chen-Yu Tsai4cbeaeb2016-01-25 21:15:40 +0800274
275void sunxi_factors_unregister(struct device_node *node, struct clk *clk)
276{
277 struct clk_hw *hw = __clk_get_hw(clk);
278 struct clk_factors *factors;
279 const char *name;
280
281 if (!hw)
282 return;
283
284 factors = to_clk_factors(hw);
285 name = clk_hw_get_name(hw);
286
Chen-Yu Tsai4cbeaeb2016-01-25 21:15:40 +0800287 of_clk_del_provider(node);
288 /* TODO: The composite clock stuff will leak a bit here. */
289 clk_unregister(clk);
290 kfree(factors->mux);
291 kfree(factors->gate);
292 kfree(factors);
293}