2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/regmap.h>
20 DEFINE_SPINLOCK(pmc_pcr_lock
);
22 #define PERIPHERAL_MAX 64
24 #define PERIPHERAL_AT91RM9200 0
25 #define PERIPHERAL_AT91SAM9X5 1
27 #define PERIPHERAL_ID_MIN 2
28 #define PERIPHERAL_ID_MAX 31
29 #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
31 #define PERIPHERAL_RSHIFT_MASK 0x3
32 #define PERIPHERAL_RSHIFT(val) (((val) >> 16) & PERIPHERAL_RSHIFT_MASK)
34 #define PERIPHERAL_MAX_SHIFT 3
36 struct clk_peripheral
{
38 struct regmap
*regmap
;
42 #define to_clk_peripheral(hw) container_of(hw, struct clk_peripheral, hw)
44 struct clk_sam9x5_peripheral
{
46 struct regmap
*regmap
;
47 struct clk_range range
;
54 #define to_clk_sam9x5_peripheral(hw) \
55 container_of(hw, struct clk_sam9x5_peripheral, hw)
57 static int clk_peripheral_enable(struct clk_hw
*hw
)
59 struct clk_peripheral
*periph
= to_clk_peripheral(hw
);
60 int offset
= AT91_PMC_PCER
;
63 if (id
< PERIPHERAL_ID_MIN
)
65 if (id
> PERIPHERAL_ID_MAX
)
66 offset
= AT91_PMC_PCER1
;
67 regmap_write(periph
->regmap
, offset
, PERIPHERAL_MASK(id
));
72 static void clk_peripheral_disable(struct clk_hw
*hw
)
74 struct clk_peripheral
*periph
= to_clk_peripheral(hw
);
75 int offset
= AT91_PMC_PCDR
;
78 if (id
< PERIPHERAL_ID_MIN
)
80 if (id
> PERIPHERAL_ID_MAX
)
81 offset
= AT91_PMC_PCDR1
;
82 regmap_write(periph
->regmap
, offset
, PERIPHERAL_MASK(id
));
85 static int clk_peripheral_is_enabled(struct clk_hw
*hw
)
87 struct clk_peripheral
*periph
= to_clk_peripheral(hw
);
88 int offset
= AT91_PMC_PCSR
;
92 if (id
< PERIPHERAL_ID_MIN
)
94 if (id
> PERIPHERAL_ID_MAX
)
95 offset
= AT91_PMC_PCSR1
;
96 regmap_read(periph
->regmap
, offset
, &status
);
98 return status
& PERIPHERAL_MASK(id
) ? 1 : 0;
101 static const struct clk_ops peripheral_ops
= {
102 .enable
= clk_peripheral_enable
,
103 .disable
= clk_peripheral_disable
,
104 .is_enabled
= clk_peripheral_is_enabled
,
107 static struct clk_hw
* __init
108 at91_clk_register_peripheral(struct regmap
*regmap
, const char *name
,
109 const char *parent_name
, u32 id
)
111 struct clk_peripheral
*periph
;
112 struct clk_init_data init
;
116 if (!name
|| !parent_name
|| id
> PERIPHERAL_ID_MAX
)
117 return ERR_PTR(-EINVAL
);
119 periph
= kzalloc(sizeof(*periph
), GFP_KERNEL
);
121 return ERR_PTR(-ENOMEM
);
124 init
.ops
= &peripheral_ops
;
125 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
126 init
.num_parents
= (parent_name
? 1 : 0);
130 periph
->hw
.init
= &init
;
131 periph
->regmap
= regmap
;
134 ret
= clk_hw_register(NULL
, &periph
->hw
);
143 static void clk_sam9x5_peripheral_autodiv(struct clk_sam9x5_peripheral
*periph
)
145 struct clk_hw
*parent
;
146 unsigned long parent_rate
;
149 if (!periph
->auto_div
)
152 if (periph
->range
.max
) {
153 parent
= clk_hw_get_parent_by_index(&periph
->hw
, 0);
154 parent_rate
= clk_hw_get_rate(parent
);
158 for (; shift
< PERIPHERAL_MAX_SHIFT
; shift
++) {
159 if (parent_rate
>> shift
<= periph
->range
.max
)
164 periph
->auto_div
= false;
168 static int clk_sam9x5_peripheral_enable(struct clk_hw
*hw
)
170 struct clk_sam9x5_peripheral
*periph
= to_clk_sam9x5_peripheral(hw
);
173 if (periph
->id
< PERIPHERAL_ID_MIN
)
176 spin_lock_irqsave(periph
->lock
, flags
);
177 regmap_write(periph
->regmap
, AT91_PMC_PCR
,
178 (periph
->id
& AT91_PMC_PCR_PID_MASK
));
179 regmap_update_bits(periph
->regmap
, AT91_PMC_PCR
,
180 AT91_PMC_PCR_DIV_MASK
| AT91_PMC_PCR_CMD
|
182 AT91_PMC_PCR_DIV(periph
->div
) |
185 spin_unlock_irqrestore(periph
->lock
, flags
);
190 static void clk_sam9x5_peripheral_disable(struct clk_hw
*hw
)
192 struct clk_sam9x5_peripheral
*periph
= to_clk_sam9x5_peripheral(hw
);
195 if (periph
->id
< PERIPHERAL_ID_MIN
)
198 spin_lock_irqsave(periph
->lock
, flags
);
199 regmap_write(periph
->regmap
, AT91_PMC_PCR
,
200 (periph
->id
& AT91_PMC_PCR_PID_MASK
));
201 regmap_update_bits(periph
->regmap
, AT91_PMC_PCR
,
202 AT91_PMC_PCR_EN
| AT91_PMC_PCR_CMD
,
204 spin_unlock_irqrestore(periph
->lock
, flags
);
207 static int clk_sam9x5_peripheral_is_enabled(struct clk_hw
*hw
)
209 struct clk_sam9x5_peripheral
*periph
= to_clk_sam9x5_peripheral(hw
);
213 if (periph
->id
< PERIPHERAL_ID_MIN
)
216 spin_lock_irqsave(periph
->lock
, flags
);
217 regmap_write(periph
->regmap
, AT91_PMC_PCR
,
218 (periph
->id
& AT91_PMC_PCR_PID_MASK
));
219 regmap_read(periph
->regmap
, AT91_PMC_PCR
, &status
);
220 spin_unlock_irqrestore(periph
->lock
, flags
);
222 return status
& AT91_PMC_PCR_EN
? 1 : 0;
226 clk_sam9x5_peripheral_recalc_rate(struct clk_hw
*hw
,
227 unsigned long parent_rate
)
229 struct clk_sam9x5_peripheral
*periph
= to_clk_sam9x5_peripheral(hw
);
233 if (periph
->id
< PERIPHERAL_ID_MIN
)
236 spin_lock_irqsave(periph
->lock
, flags
);
237 regmap_write(periph
->regmap
, AT91_PMC_PCR
,
238 (periph
->id
& AT91_PMC_PCR_PID_MASK
));
239 regmap_read(periph
->regmap
, AT91_PMC_PCR
, &status
);
240 spin_unlock_irqrestore(periph
->lock
, flags
);
242 if (status
& AT91_PMC_PCR_EN
) {
243 periph
->div
= PERIPHERAL_RSHIFT(status
);
244 periph
->auto_div
= false;
246 clk_sam9x5_peripheral_autodiv(periph
);
249 return parent_rate
>> periph
->div
;
252 static long clk_sam9x5_peripheral_round_rate(struct clk_hw
*hw
,
254 unsigned long *parent_rate
)
257 unsigned long best_rate
;
258 unsigned long best_diff
;
259 unsigned long cur_rate
= *parent_rate
;
260 unsigned long cur_diff
;
261 struct clk_sam9x5_peripheral
*periph
= to_clk_sam9x5_peripheral(hw
);
263 if (periph
->id
< PERIPHERAL_ID_MIN
|| !periph
->range
.max
)
266 if (periph
->range
.max
) {
267 for (; shift
<= PERIPHERAL_MAX_SHIFT
; shift
++) {
268 cur_rate
= *parent_rate
>> shift
;
269 if (cur_rate
<= periph
->range
.max
)
274 if (rate
>= cur_rate
)
277 best_diff
= cur_rate
- rate
;
278 best_rate
= cur_rate
;
279 for (; shift
<= PERIPHERAL_MAX_SHIFT
; shift
++) {
280 cur_rate
= *parent_rate
>> shift
;
282 cur_diff
= rate
- cur_rate
;
284 cur_diff
= cur_rate
- rate
;
286 if (cur_diff
< best_diff
) {
287 best_diff
= cur_diff
;
288 best_rate
= cur_rate
;
291 if (!best_diff
|| cur_rate
< rate
)
298 static int clk_sam9x5_peripheral_set_rate(struct clk_hw
*hw
,
300 unsigned long parent_rate
)
303 struct clk_sam9x5_peripheral
*periph
= to_clk_sam9x5_peripheral(hw
);
304 if (periph
->id
< PERIPHERAL_ID_MIN
|| !periph
->range
.max
) {
305 if (parent_rate
== rate
)
311 if (periph
->range
.max
&& rate
> periph
->range
.max
)
314 for (shift
= 0; shift
<= PERIPHERAL_MAX_SHIFT
; shift
++) {
315 if (parent_rate
>> shift
== rate
) {
316 periph
->auto_div
= false;
325 static const struct clk_ops sam9x5_peripheral_ops
= {
326 .enable
= clk_sam9x5_peripheral_enable
,
327 .disable
= clk_sam9x5_peripheral_disable
,
328 .is_enabled
= clk_sam9x5_peripheral_is_enabled
,
329 .recalc_rate
= clk_sam9x5_peripheral_recalc_rate
,
330 .round_rate
= clk_sam9x5_peripheral_round_rate
,
331 .set_rate
= clk_sam9x5_peripheral_set_rate
,
334 static struct clk_hw
* __init
335 at91_clk_register_sam9x5_peripheral(struct regmap
*regmap
, spinlock_t
*lock
,
336 const char *name
, const char *parent_name
,
337 u32 id
, const struct clk_range
*range
)
339 struct clk_sam9x5_peripheral
*periph
;
340 struct clk_init_data init
;
344 if (!name
|| !parent_name
)
345 return ERR_PTR(-EINVAL
);
347 periph
= kzalloc(sizeof(*periph
), GFP_KERNEL
);
349 return ERR_PTR(-ENOMEM
);
352 init
.ops
= &sam9x5_peripheral_ops
;
353 init
.parent_names
= (parent_name
? &parent_name
: NULL
);
354 init
.num_parents
= (parent_name
? 1 : 0);
358 periph
->hw
.init
= &init
;
360 periph
->regmap
= regmap
;
362 periph
->auto_div
= true;
363 periph
->range
= *range
;
366 ret
= clk_hw_register(NULL
, &periph
->hw
);
371 clk_sam9x5_peripheral_autodiv(periph
);
377 of_at91_clk_periph_setup(struct device_node
*np
, u8 type
)
382 const char *parent_name
;
384 struct device_node
*periphclknp
;
385 struct regmap
*regmap
;
387 parent_name
= of_clk_get_parent_name(np
, 0);
391 num
= of_get_child_count(np
);
392 if (!num
|| num
> PERIPHERAL_MAX
)
395 regmap
= syscon_node_to_regmap(of_get_parent(np
));
399 for_each_child_of_node(np
, periphclknp
) {
400 if (of_property_read_u32(periphclknp
, "reg", &id
))
403 if (id
>= PERIPHERAL_MAX
)
406 if (of_property_read_string(np
, "clock-output-names", &name
))
407 name
= periphclknp
->name
;
409 if (type
== PERIPHERAL_AT91RM9200
) {
410 hw
= at91_clk_register_peripheral(regmap
, name
,
413 struct clk_range range
= CLK_RANGE(0, 0);
415 of_at91_get_clk_range(periphclknp
,
416 "atmel,clk-output-range",
419 hw
= at91_clk_register_sam9x5_peripheral(regmap
,
429 of_clk_add_hw_provider(periphclknp
, of_clk_hw_simple_get
, hw
);
433 static void __init
of_at91rm9200_clk_periph_setup(struct device_node
*np
)
435 of_at91_clk_periph_setup(np
, PERIPHERAL_AT91RM9200
);
437 CLK_OF_DECLARE(at91rm9200_clk_periph
, "atmel,at91rm9200-clk-peripheral",
438 of_at91rm9200_clk_periph_setup
);
440 static void __init
of_at91sam9x5_clk_periph_setup(struct device_node
*np
)
442 of_at91_clk_periph_setup(np
, PERIPHERAL_AT91SAM9X5
);
444 CLK_OF_DECLARE(at91sam9x5_clk_periph
, "atmel,at91sam9x5-clk-peripheral",
445 of_at91sam9x5_clk_periph_setup
);