1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Atmel Corporation,
4 * Nicolas Ferre <nicolas.ferre@atmel.com>
6 * Based on clk-programmable & clk-peripheral drivers by Boris BREZILLON.
9 #include <linux/bitfield.h>
10 #include <linux/clk-provider.h>
11 #include <linux/clkdev.h>
12 #include <linux/clk/at91_pmc.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/regmap.h>
19 #define GENERATED_MAX_DIV 255
21 struct clk_generated
{
23 struct regmap
*regmap
;
24 struct clk_range range
;
29 const struct clk_pcr_layout
*layout
;
34 #define to_clk_generated(hw) \
35 container_of(hw, struct clk_generated, hw)
37 static int clk_generated_enable(struct clk_hw
*hw
)
39 struct clk_generated
*gck
= to_clk_generated(hw
);
42 pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
43 __func__
, gck
->gckdiv
, gck
->parent_id
);
45 spin_lock_irqsave(gck
->lock
, flags
);
46 regmap_write(gck
->regmap
, gck
->layout
->offset
,
47 (gck
->id
& gck
->layout
->pid_mask
));
48 regmap_update_bits(gck
->regmap
, gck
->layout
->offset
,
49 AT91_PMC_PCR_GCKDIV_MASK
| gck
->layout
->gckcss_mask
|
50 gck
->layout
->cmd
| AT91_PMC_PCR_GCKEN
,
51 field_prep(gck
->layout
->gckcss_mask
, gck
->parent_id
) |
53 FIELD_PREP(AT91_PMC_PCR_GCKDIV_MASK
, gck
->gckdiv
) |
55 spin_unlock_irqrestore(gck
->lock
, flags
);
59 static void clk_generated_disable(struct clk_hw
*hw
)
61 struct clk_generated
*gck
= to_clk_generated(hw
);
64 spin_lock_irqsave(gck
->lock
, flags
);
65 regmap_write(gck
->regmap
, gck
->layout
->offset
,
66 (gck
->id
& gck
->layout
->pid_mask
));
67 regmap_update_bits(gck
->regmap
, gck
->layout
->offset
,
68 gck
->layout
->cmd
| AT91_PMC_PCR_GCKEN
,
70 spin_unlock_irqrestore(gck
->lock
, flags
);
73 static int clk_generated_is_enabled(struct clk_hw
*hw
)
75 struct clk_generated
*gck
= to_clk_generated(hw
);
79 spin_lock_irqsave(gck
->lock
, flags
);
80 regmap_write(gck
->regmap
, gck
->layout
->offset
,
81 (gck
->id
& gck
->layout
->pid_mask
));
82 regmap_read(gck
->regmap
, gck
->layout
->offset
, &status
);
83 spin_unlock_irqrestore(gck
->lock
, flags
);
85 return !!(status
& AT91_PMC_PCR_GCKEN
);
89 clk_generated_recalc_rate(struct clk_hw
*hw
,
90 unsigned long parent_rate
)
92 struct clk_generated
*gck
= to_clk_generated(hw
);
94 return DIV_ROUND_CLOSEST(parent_rate
, gck
->gckdiv
+ 1);
97 static void clk_generated_best_diff(struct clk_rate_request
*req
,
98 struct clk_hw
*parent
,
99 unsigned long parent_rate
, u32 div
,
100 int *best_diff
, long *best_rate
)
102 unsigned long tmp_rate
;
106 tmp_rate
= parent_rate
;
108 tmp_rate
= parent_rate
/ div
;
109 tmp_diff
= abs(req
->rate
- tmp_rate
);
111 if (*best_diff
< 0 || *best_diff
>= tmp_diff
) {
112 *best_rate
= tmp_rate
;
113 *best_diff
= tmp_diff
;
114 req
->best_parent_rate
= parent_rate
;
115 req
->best_parent_hw
= parent
;
119 static int clk_generated_determine_rate(struct clk_hw
*hw
,
120 struct clk_rate_request
*req
)
122 struct clk_generated
*gck
= to_clk_generated(hw
);
123 struct clk_hw
*parent
= NULL
;
124 struct clk_rate_request req_parent
= *req
;
125 long best_rate
= -EINVAL
;
126 unsigned long min_rate
, parent_rate
;
131 for (i
= 0; i
< clk_hw_get_num_parents(hw
); i
++) {
132 if (gck
->chg_pid
== i
)
135 parent
= clk_hw_get_parent_by_index(hw
, i
);
139 parent_rate
= clk_hw_get_rate(parent
);
140 min_rate
= DIV_ROUND_CLOSEST(parent_rate
, GENERATED_MAX_DIV
+ 1);
142 (gck
->range
.max
&& min_rate
> gck
->range
.max
))
145 div
= DIV_ROUND_CLOSEST(parent_rate
, req
->rate
);
146 if (div
> GENERATED_MAX_DIV
+ 1)
147 div
= GENERATED_MAX_DIV
+ 1;
149 clk_generated_best_diff(req
, parent
, parent_rate
, div
,
150 &best_diff
, &best_rate
);
157 * The audio_pll rate can be modified, unlike the five others clocks
158 * that should never be altered.
159 * The audio_pll can technically be used by multiple consumers. However,
160 * with the rate locking, the first consumer to enable to clock will be
161 * the one definitely setting the rate of the clock.
162 * Since audio IPs are most likely to request the same rate, we enforce
163 * that the only clks able to modify gck rate are those of audio IPs.
166 if (gck
->chg_pid
< 0)
169 parent
= clk_hw_get_parent_by_index(hw
, gck
->chg_pid
);
173 for (div
= 1; div
< GENERATED_MAX_DIV
+ 2; div
++) {
174 req_parent
.rate
= req
->rate
* div
;
175 if (__clk_determine_rate(parent
, &req_parent
))
177 clk_generated_best_diff(req
, parent
, req_parent
.rate
, div
,
178 &best_diff
, &best_rate
);
185 pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
187 __clk_get_name((req
->best_parent_hw
)->clk
),
188 req
->best_parent_rate
);
190 if (best_rate
< 0 || (gck
->range
.max
&& best_rate
> gck
->range
.max
))
193 req
->rate
= best_rate
;
197 /* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */
198 static int clk_generated_set_parent(struct clk_hw
*hw
, u8 index
)
200 struct clk_generated
*gck
= to_clk_generated(hw
);
202 if (index
>= clk_hw_get_num_parents(hw
))
206 gck
->parent_id
= clk_mux_index_to_val(gck
->mux_table
, 0, index
);
208 gck
->parent_id
= index
;
213 static u8
clk_generated_get_parent(struct clk_hw
*hw
)
215 struct clk_generated
*gck
= to_clk_generated(hw
);
217 return gck
->parent_id
;
220 /* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */
221 static int clk_generated_set_rate(struct clk_hw
*hw
,
223 unsigned long parent_rate
)
225 struct clk_generated
*gck
= to_clk_generated(hw
);
231 if (gck
->range
.max
&& rate
> gck
->range
.max
)
234 div
= DIV_ROUND_CLOSEST(parent_rate
, rate
);
235 if (div
> GENERATED_MAX_DIV
+ 1 || !div
)
238 gck
->gckdiv
= div
- 1;
242 static const struct clk_ops generated_ops
= {
243 .enable
= clk_generated_enable
,
244 .disable
= clk_generated_disable
,
245 .is_enabled
= clk_generated_is_enabled
,
246 .recalc_rate
= clk_generated_recalc_rate
,
247 .determine_rate
= clk_generated_determine_rate
,
248 .get_parent
= clk_generated_get_parent
,
249 .set_parent
= clk_generated_set_parent
,
250 .set_rate
= clk_generated_set_rate
,
254 * clk_generated_startup - Initialize a given clock to its default parent and
257 * @gck: Generated clock to set the startup parameters for.
259 * Take parameters from the hardware and update local clock configuration
262 static void clk_generated_startup(struct clk_generated
*gck
)
267 spin_lock_irqsave(gck
->lock
, flags
);
268 regmap_write(gck
->regmap
, gck
->layout
->offset
,
269 (gck
->id
& gck
->layout
->pid_mask
));
270 regmap_read(gck
->regmap
, gck
->layout
->offset
, &tmp
);
271 spin_unlock_irqrestore(gck
->lock
, flags
);
273 gck
->parent_id
= field_get(gck
->layout
->gckcss_mask
, tmp
);
274 gck
->gckdiv
= FIELD_GET(AT91_PMC_PCR_GCKDIV_MASK
, tmp
);
277 struct clk_hw
* __init
278 at91_clk_register_generated(struct regmap
*regmap
, spinlock_t
*lock
,
279 const struct clk_pcr_layout
*layout
,
280 const char *name
, const char **parent_names
,
281 u32
*mux_table
, u8 num_parents
, u8 id
,
282 const struct clk_range
*range
,
285 struct clk_generated
*gck
;
286 struct clk_init_data init
;
290 gck
= kzalloc(sizeof(*gck
), GFP_KERNEL
);
292 return ERR_PTR(-ENOMEM
);
295 init
.ops
= &generated_ops
;
296 init
.parent_names
= parent_names
;
297 init
.num_parents
= num_parents
;
298 init
.flags
= CLK_SET_RATE_GATE
| CLK_SET_PARENT_GATE
;
300 init
.flags
|= CLK_SET_RATE_PARENT
;
303 gck
->hw
.init
= &init
;
304 gck
->regmap
= regmap
;
307 gck
->chg_pid
= chg_pid
;
308 gck
->layout
= layout
;
309 gck
->mux_table
= mux_table
;
311 clk_generated_startup(gck
);
313 ret
= clk_hw_register(NULL
, &gck
->hw
);