blob: a9ebbd207d58deedfa058691b011bf8cc66c877e [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>
15#include <linux/module.h>
16#include <linux/of_address.h>
17#include <linux/slab.h>
18#include <linux/string.h>
Emilio Lópeze874a662013-02-25 11:44:26 -030019
20#include "clk-factors.h"
21
22/*
Maxime Ripard601da9d2014-07-04 22:24:52 +020023 * DOC: basic adjustable factor-based clock
Emilio Lópeze874a662013-02-25 11:44:26 -030024 *
25 * Traits of this clock:
26 * prepare - clk_prepare only ensures that parents are prepared
27 * enable - clk_enable only ensures that parents are enabled
28 * rate - rate is adjustable.
29 * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
30 * parent - fixed parent. No clk_set_parent support
31 */
32
Emilio Lópeze874a662013-02-25 11:44:26 -030033#define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
34
Maxime Ripard601da9d2014-07-04 22:24:52 +020035#define FACTORS_MAX_PARENTS 5
36
Emilio Lópezc518e842013-09-20 22:03:10 -030037#define SETMASK(len, pos) (((1U << (len)) - 1) << (pos))
Emilio Lópeze874a662013-02-25 11:44:26 -030038#define CLRMASK(len, pos) (~(SETMASK(len, pos)))
39#define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit))
40
41#define FACTOR_SET(bit, len, reg, val) \
42 (((reg) & CLRMASK(len, bit)) | (val << (bit)))
43
44static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
45 unsigned long parent_rate)
46{
47 u8 n = 1, k = 0, p = 0, m = 0;
48 u32 reg;
49 unsigned long rate;
50 struct clk_factors *factors = to_clk_factors(hw);
51 struct clk_factors_config *config = factors->config;
52
53 /* Fetch the register value */
54 reg = readl(factors->reg);
55
56 /* Get each individual factor if applicable */
57 if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
58 n = FACTOR_GET(config->nshift, config->nwidth, reg);
59 if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
60 k = FACTOR_GET(config->kshift, config->kwidth, reg);
61 if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
62 m = FACTOR_GET(config->mshift, config->mwidth, reg);
63 if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
64 p = FACTOR_GET(config->pshift, config->pwidth, reg);
65
66 /* Calculate the rate */
Chen-Yu Tsai9a5e6c72014-06-26 23:55:41 +080067 rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1);
Emilio Lópeze874a662013-02-25 11:44:26 -030068
69 return rate;
70}
71
72static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate,
73 unsigned long *parent_rate)
74{
75 struct clk_factors *factors = to_clk_factors(hw);
76 factors->get_factors((u32 *)&rate, (u32)*parent_rate,
77 NULL, NULL, NULL, NULL);
78
79 return rate;
80}
81
Emilio López862b7282014-05-02 17:57:15 +020082static long clk_factors_determine_rate(struct clk_hw *hw, unsigned long rate,
83 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +010084 struct clk_hw **best_parent_p)
Emilio López862b7282014-05-02 17:57:15 +020085{
86 struct clk *clk = hw->clk, *parent, *best_parent = NULL;
87 int i, num_parents;
88 unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
89
90 /* find the parent that can help provide the fastest rate <= rate */
91 num_parents = __clk_get_num_parents(clk);
92 for (i = 0; i < num_parents; i++) {
93 parent = clk_get_parent_by_index(clk, i);
94 if (!parent)
95 continue;
96 if (__clk_get_flags(clk) & CLK_SET_RATE_PARENT)
97 parent_rate = __clk_round_rate(parent, rate);
98 else
99 parent_rate = __clk_get_rate(parent);
100
101 child_rate = clk_factors_round_rate(hw, rate, &parent_rate);
102
103 if (child_rate <= rate && child_rate > best_child_rate) {
104 best_parent = parent;
105 best = parent_rate;
106 best_child_rate = child_rate;
107 }
108 }
109
110 if (best_parent)
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100111 *best_parent_p = __clk_get_hw(best_parent);
Emilio López862b7282014-05-02 17:57:15 +0200112 *best_parent_rate = best;
113
114 return best_child_rate;
115}
116
Emilio Lópeze874a662013-02-25 11:44:26 -0300117static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
118 unsigned long parent_rate)
119{
Emilio López12ef06a2013-09-20 22:03:11 -0300120 u8 n = 0, k = 0, m = 0, p = 0;
Emilio Lópeze874a662013-02-25 11:44:26 -0300121 u32 reg;
122 struct clk_factors *factors = to_clk_factors(hw);
123 struct clk_factors_config *config = factors->config;
124 unsigned long flags = 0;
125
126 factors->get_factors((u32 *)&rate, (u32)parent_rate, &n, &k, &m, &p);
127
128 if (factors->lock)
129 spin_lock_irqsave(factors->lock, flags);
130
131 /* Fetch the register value */
132 reg = readl(factors->reg);
133
134 /* Set up the new factors - macros do not do anything if width is 0 */
135 reg = FACTOR_SET(config->nshift, config->nwidth, reg, n);
136 reg = FACTOR_SET(config->kshift, config->kwidth, reg, k);
137 reg = FACTOR_SET(config->mshift, config->mwidth, reg, m);
138 reg = FACTOR_SET(config->pshift, config->pwidth, reg, p);
139
140 /* Apply them now */
141 writel(reg, factors->reg);
142
143 /* delay 500us so pll stabilizes */
144 __delay((rate >> 20) * 500 / 2);
145
146 if (factors->lock)
147 spin_unlock_irqrestore(factors->lock, flags);
148
149 return 0;
150}
151
Maxime Ripard601da9d2014-07-04 22:24:52 +0200152static const struct clk_ops clk_factors_ops = {
Emilio López862b7282014-05-02 17:57:15 +0200153 .determine_rate = clk_factors_determine_rate,
Emilio Lópeze874a662013-02-25 11:44:26 -0300154 .recalc_rate = clk_factors_recalc_rate,
155 .round_rate = clk_factors_round_rate,
156 .set_rate = clk_factors_set_rate,
157};
Maxime Ripard601da9d2014-07-04 22:24:52 +0200158
Hans de Goede7c74c222014-11-23 14:38:07 +0100159struct clk *sunxi_factors_register(struct device_node *node,
160 const struct factors_data *data,
161 spinlock_t *lock,
162 void __iomem *reg)
Maxime Ripard601da9d2014-07-04 22:24:52 +0200163{
164 struct clk *clk;
165 struct clk_factors *factors;
166 struct clk_gate *gate = NULL;
167 struct clk_mux *mux = NULL;
168 struct clk_hw *gate_hw = NULL;
169 struct clk_hw *mux_hw = NULL;
170 const char *clk_name = node->name;
171 const char *parents[FACTORS_MAX_PARENTS];
Maxime Ripard601da9d2014-07-04 22:24:52 +0200172 int i = 0;
173
Maxime Ripard601da9d2014-07-04 22:24:52 +0200174 /* if we have a mux, we will have >1 parents */
175 while (i < FACTORS_MAX_PARENTS &&
176 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
177 i++;
178
179 /*
180 * some factor clocks, such as pll5 and pll6, may have multiple
181 * outputs, and have their name designated in factors_data
182 */
183 if (data->name)
184 clk_name = data->name;
185 else
186 of_property_read_string(node, "clock-output-names", &clk_name);
187
188 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
189 if (!factors)
190 return NULL;
191
192 /* set up factors properties */
193 factors->reg = reg;
194 factors->config = data->table;
195 factors->get_factors = data->getter;
196 factors->lock = lock;
197
198 /* Add a gate if this factor clock can be gated */
199 if (data->enable) {
200 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
201 if (!gate) {
202 kfree(factors);
203 return NULL;
204 }
205
206 /* set up gate properties */
207 gate->reg = reg;
208 gate->bit_idx = data->enable;
209 gate->lock = factors->lock;
210 gate_hw = &gate->hw;
211 }
212
213 /* Add a mux if this factor clock can be muxed */
214 if (data->mux) {
215 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
216 if (!mux) {
217 kfree(factors);
218 kfree(gate);
219 return NULL;
220 }
221
222 /* set up gate properties */
223 mux->reg = reg;
224 mux->shift = data->mux;
Chen-Yu Tsaie94f8cb32014-10-20 22:10:26 +0800225 mux->mask = data->muxmask;
Maxime Ripard601da9d2014-07-04 22:24:52 +0200226 mux->lock = factors->lock;
227 mux_hw = &mux->hw;
228 }
229
230 clk = clk_register_composite(NULL, clk_name,
231 parents, i,
232 mux_hw, &clk_mux_ops,
233 &factors->hw, &clk_factors_ops,
234 gate_hw, &clk_gate_ops, 0);
235
236 if (!IS_ERR(clk)) {
237 of_clk_add_provider(node, of_clk_src_simple_get, clk);
238 clk_register_clkdev(clk, clk_name, NULL);
239 }
240
241 return clk;
242}