2 * Copyright (C) 2015 Atmel Corporation,
3 * Nicolas Ferre <nicolas.ferre@atmel.com>
5 * Based on clk-programmable & clk-peripheral drivers by Boris BREZILLON.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <linux/clk-provider.h>
15 #include <linux/clkdev.h>
16 #include <linux/clk/at91_pmc.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/regmap.h>
23 #define PERIPHERAL_MAX 64
24 #define PERIPHERAL_ID_MIN 2
26 #define GENERATED_SOURCE_MAX 6
27 #define GENERATED_MAX_DIV 255
29 struct clk_generated
{
31 struct regmap
*regmap
;
32 struct clk_range range
;
39 #define to_clk_generated(hw) \
40 container_of(hw, struct clk_generated, hw)
42 static int clk_generated_enable(struct clk_hw
*hw
)
44 struct clk_generated
*gck
= to_clk_generated(hw
);
47 pr_debug("GCLK: %s, gckdiv = %d, parent id = %d\n",
48 __func__
, gck
->gckdiv
, gck
->parent_id
);
50 spin_lock_irqsave(gck
->lock
, flags
);
51 regmap_write(gck
->regmap
, AT91_PMC_PCR
,
52 (gck
->id
& AT91_PMC_PCR_PID_MASK
));
53 regmap_update_bits(gck
->regmap
, AT91_PMC_PCR
,
54 AT91_PMC_PCR_GCKDIV_MASK
| AT91_PMC_PCR_GCKCSS_MASK
|
55 AT91_PMC_PCR_CMD
| AT91_PMC_PCR_GCKEN
,
56 AT91_PMC_PCR_GCKCSS(gck
->parent_id
) |
58 AT91_PMC_PCR_GCKDIV(gck
->gckdiv
) |
60 spin_unlock_irqrestore(gck
->lock
, flags
);
64 static void clk_generated_disable(struct clk_hw
*hw
)
66 struct clk_generated
*gck
= to_clk_generated(hw
);
69 spin_lock_irqsave(gck
->lock
, flags
);
70 regmap_write(gck
->regmap
, AT91_PMC_PCR
,
71 (gck
->id
& AT91_PMC_PCR_PID_MASK
));
72 regmap_update_bits(gck
->regmap
, AT91_PMC_PCR
,
73 AT91_PMC_PCR_CMD
| AT91_PMC_PCR_GCKEN
,
75 spin_unlock_irqrestore(gck
->lock
, flags
);
78 static int clk_generated_is_enabled(struct clk_hw
*hw
)
80 struct clk_generated
*gck
= to_clk_generated(hw
);
84 spin_lock_irqsave(gck
->lock
, flags
);
85 regmap_write(gck
->regmap
, AT91_PMC_PCR
,
86 (gck
->id
& AT91_PMC_PCR_PID_MASK
));
87 regmap_read(gck
->regmap
, AT91_PMC_PCR
, &status
);
88 spin_unlock_irqrestore(gck
->lock
, flags
);
90 return status
& AT91_PMC_PCR_GCKEN
? 1 : 0;
94 clk_generated_recalc_rate(struct clk_hw
*hw
,
95 unsigned long parent_rate
)
97 struct clk_generated
*gck
= to_clk_generated(hw
);
99 return DIV_ROUND_CLOSEST(parent_rate
, gck
->gckdiv
+ 1);
102 static int clk_generated_determine_rate(struct clk_hw
*hw
,
103 struct clk_rate_request
*req
)
105 struct clk_generated
*gck
= to_clk_generated(hw
);
106 struct clk_hw
*parent
= NULL
;
107 long best_rate
= -EINVAL
;
108 unsigned long tmp_rate
, min_rate
;
113 for (i
= 0; i
< clk_hw_get_num_parents(hw
); i
++) {
115 unsigned long parent_rate
;
117 parent
= clk_hw_get_parent_by_index(hw
, i
);
121 parent_rate
= clk_hw_get_rate(parent
);
122 min_rate
= DIV_ROUND_CLOSEST(parent_rate
, GENERATED_MAX_DIV
+ 1);
124 (gck
->range
.max
&& min_rate
> gck
->range
.max
))
127 for (div
= 1; div
< GENERATED_MAX_DIV
+ 2; div
++) {
128 tmp_rate
= DIV_ROUND_CLOSEST(parent_rate
, div
);
129 tmp_diff
= abs(req
->rate
- tmp_rate
);
131 if (best_diff
< 0 || best_diff
> tmp_diff
) {
132 best_rate
= tmp_rate
;
133 best_diff
= tmp_diff
;
134 req
->best_parent_rate
= parent_rate
;
135 req
->best_parent_hw
= parent
;
138 if (!best_diff
|| tmp_rate
< req
->rate
)
146 pr_debug("GCLK: %s, best_rate = %ld, parent clk: %s @ %ld\n",
148 __clk_get_name((req
->best_parent_hw
)->clk
),
149 req
->best_parent_rate
);
154 req
->rate
= best_rate
;
158 /* No modification of hardware as we have the flag CLK_SET_PARENT_GATE set */
159 static int clk_generated_set_parent(struct clk_hw
*hw
, u8 index
)
161 struct clk_generated
*gck
= to_clk_generated(hw
);
163 if (index
>= clk_hw_get_num_parents(hw
))
166 gck
->parent_id
= index
;
170 static u8
clk_generated_get_parent(struct clk_hw
*hw
)
172 struct clk_generated
*gck
= to_clk_generated(hw
);
174 return gck
->parent_id
;
177 /* No modification of hardware as we have the flag CLK_SET_RATE_GATE set */
178 static int clk_generated_set_rate(struct clk_hw
*hw
,
180 unsigned long parent_rate
)
182 struct clk_generated
*gck
= to_clk_generated(hw
);
188 if (gck
->range
.max
&& rate
> gck
->range
.max
)
191 div
= DIV_ROUND_CLOSEST(parent_rate
, rate
);
192 if (div
> GENERATED_MAX_DIV
+ 1 || !div
)
195 gck
->gckdiv
= div
- 1;
199 static const struct clk_ops generated_ops
= {
200 .enable
= clk_generated_enable
,
201 .disable
= clk_generated_disable
,
202 .is_enabled
= clk_generated_is_enabled
,
203 .recalc_rate
= clk_generated_recalc_rate
,
204 .determine_rate
= clk_generated_determine_rate
,
205 .get_parent
= clk_generated_get_parent
,
206 .set_parent
= clk_generated_set_parent
,
207 .set_rate
= clk_generated_set_rate
,
211 * clk_generated_startup - Initialize a given clock to its default parent and
214 * @gck: Generated clock to set the startup parameters for.
216 * Take parameters from the hardware and update local clock configuration
219 static void clk_generated_startup(struct clk_generated
*gck
)
224 spin_lock_irqsave(gck
->lock
, flags
);
225 regmap_write(gck
->regmap
, AT91_PMC_PCR
,
226 (gck
->id
& AT91_PMC_PCR_PID_MASK
));
227 regmap_read(gck
->regmap
, AT91_PMC_PCR
, &tmp
);
228 spin_unlock_irqrestore(gck
->lock
, flags
);
230 gck
->parent_id
= (tmp
& AT91_PMC_PCR_GCKCSS_MASK
)
231 >> AT91_PMC_PCR_GCKCSS_OFFSET
;
232 gck
->gckdiv
= (tmp
& AT91_PMC_PCR_GCKDIV_MASK
)
233 >> AT91_PMC_PCR_GCKDIV_OFFSET
;
236 static struct clk
* __init
237 at91_clk_register_generated(struct regmap
*regmap
, spinlock_t
*lock
, const char
238 *name
, const char **parent_names
, u8 num_parents
,
239 u8 id
, const struct clk_range
*range
)
241 struct clk_generated
*gck
;
242 struct clk
*clk
= NULL
;
243 struct clk_init_data init
;
245 gck
= kzalloc(sizeof(*gck
), GFP_KERNEL
);
247 return ERR_PTR(-ENOMEM
);
250 init
.ops
= &generated_ops
;
251 init
.parent_names
= parent_names
;
252 init
.num_parents
= num_parents
;
253 init
.flags
= CLK_SET_RATE_GATE
| CLK_SET_PARENT_GATE
;
256 gck
->hw
.init
= &init
;
257 gck
->regmap
= regmap
;
261 clk
= clk_register(NULL
, &gck
->hw
);
265 clk_generated_startup(gck
);
270 void __init
of_sama5d2_clk_generated_setup(struct device_node
*np
)
276 unsigned int num_parents
;
277 const char *parent_names
[GENERATED_SOURCE_MAX
];
278 struct device_node
*gcknp
;
279 struct clk_range range
= CLK_RANGE(0, 0);
280 struct regmap
*regmap
;
282 num_parents
= of_clk_get_parent_count(np
);
283 if (num_parents
== 0 || num_parents
> GENERATED_SOURCE_MAX
)
286 of_clk_parent_fill(np
, parent_names
, num_parents
);
288 num
= of_get_child_count(np
);
289 if (!num
|| num
> PERIPHERAL_MAX
)
292 regmap
= syscon_node_to_regmap(of_get_parent(np
));
296 for_each_child_of_node(np
, gcknp
) {
297 if (of_property_read_u32(gcknp
, "reg", &id
))
300 if (id
< PERIPHERAL_ID_MIN
|| id
>= PERIPHERAL_MAX
)
303 if (of_property_read_string(np
, "clock-output-names", &name
))
306 of_at91_get_clk_range(gcknp
, "atmel,clk-output-range",
309 clk
= at91_clk_register_generated(regmap
, &pmc_pcr_lock
, name
,
310 parent_names
, num_parents
,
315 of_clk_add_provider(gcknp
, of_clk_src_simple_get
, clk
);
318 CLK_OF_DECLARE(of_sama5d2_clk_generated_setup
, "atmel,sama5d2-clk-generated",
319 of_sama5d2_clk_generated_setup
);