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/of_address.h>
20 #define to_clk_plldiv(hw) container_of(hw, struct clk_plldiv, hw)
27 static unsigned long clk_plldiv_recalc_rate(struct clk_hw
*hw
,
28 unsigned long parent_rate
)
30 struct clk_plldiv
*plldiv
= to_clk_plldiv(hw
);
31 struct at91_pmc
*pmc
= plldiv
->pmc
;
33 if (pmc_read(pmc
, AT91_PMC_MCKR
) & AT91_PMC_PLLADIV2
)
34 return parent_rate
/ 2;
39 static long clk_plldiv_round_rate(struct clk_hw
*hw
, unsigned long rate
,
40 unsigned long *parent_rate
)
44 if (rate
> *parent_rate
)
46 div
= *parent_rate
/ 2;
50 if (rate
- div
< *parent_rate
- rate
)
56 static int clk_plldiv_set_rate(struct clk_hw
*hw
, unsigned long rate
,
57 unsigned long parent_rate
)
59 struct clk_plldiv
*plldiv
= to_clk_plldiv(hw
);
60 struct at91_pmc
*pmc
= plldiv
->pmc
;
63 if (parent_rate
!= rate
&& (parent_rate
/ 2) != rate
)
67 tmp
= pmc_read(pmc
, AT91_PMC_MCKR
) & ~AT91_PMC_PLLADIV2
;
68 if ((parent_rate
/ 2) == rate
)
69 tmp
|= AT91_PMC_PLLADIV2
;
70 pmc_write(pmc
, AT91_PMC_MCKR
, tmp
);
76 static const struct clk_ops plldiv_ops
= {
77 .recalc_rate
= clk_plldiv_recalc_rate
,
78 .round_rate
= clk_plldiv_round_rate
,
79 .set_rate
= clk_plldiv_set_rate
,
82 static struct clk
* __init
83 at91_clk_register_plldiv(struct at91_pmc
*pmc
, const char *name
,
84 const char *parent_name
)
86 struct clk_plldiv
*plldiv
;
87 struct clk
*clk
= NULL
;
88 struct clk_init_data init
;
90 plldiv
= kzalloc(sizeof(*plldiv
), GFP_KERNEL
);
92 return ERR_PTR(-ENOMEM
);
95 init
.ops
= &plldiv_ops
;
96 init
.parent_names
= parent_name
? &parent_name
: NULL
;
97 init
.num_parents
= parent_name
? 1 : 0;
98 init
.flags
= CLK_SET_RATE_GATE
;
100 plldiv
->hw
.init
= &init
;
103 clk
= clk_register(NULL
, &plldiv
->hw
);
112 of_at91_clk_plldiv_setup(struct device_node
*np
, struct at91_pmc
*pmc
)
115 const char *parent_name
;
116 const char *name
= np
->name
;
118 parent_name
= of_clk_get_parent_name(np
, 0);
120 of_property_read_string(np
, "clock-output-names", &name
);
122 clk
= at91_clk_register_plldiv(pmc
, name
, parent_name
);
127 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
131 void __init
of_at91sam9x5_clk_plldiv_setup(struct device_node
*np
,
132 struct at91_pmc
*pmc
)
134 of_at91_clk_plldiv_setup(np
, pmc
);