4 * Copyright (C) 2014 Atmel
6 * Alexandre Belloni <alexandre.belloni@free-electrons.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
15 #include <linux/clk-provider.h>
16 #include <linux/clkdev.h>
17 #include <linux/clk/at91_pmc.h>
19 #include <linux/regmap.h>
20 #include <linux/mfd/syscon.h>
24 #define H32MX_MAX_FREQ 90000000
26 struct clk_sama5d4_h32mx
{
28 struct regmap
*regmap
;
31 #define to_clk_sama5d4_h32mx(hw) container_of(hw, struct clk_sama5d4_h32mx, hw)
33 static unsigned long clk_sama5d4_h32mx_recalc_rate(struct clk_hw
*hw
,
34 unsigned long parent_rate
)
36 struct clk_sama5d4_h32mx
*h32mxclk
= to_clk_sama5d4_h32mx(hw
);
39 regmap_read(h32mxclk
->regmap
, AT91_PMC_MCKR
, &mckr
);
40 if (mckr
& AT91_PMC_H32MXDIV
)
41 return parent_rate
/ 2;
43 if (parent_rate
> H32MX_MAX_FREQ
)
44 pr_warn("H32MX clock is too fast\n");
48 static long clk_sama5d4_h32mx_round_rate(struct clk_hw
*hw
, unsigned long rate
,
49 unsigned long *parent_rate
)
53 if (rate
> *parent_rate
)
55 div
= *parent_rate
/ 2;
59 if (rate
- div
< *parent_rate
- rate
)
65 static int clk_sama5d4_h32mx_set_rate(struct clk_hw
*hw
, unsigned long rate
,
66 unsigned long parent_rate
)
68 struct clk_sama5d4_h32mx
*h32mxclk
= to_clk_sama5d4_h32mx(hw
);
71 if (parent_rate
!= rate
&& (parent_rate
/ 2) != rate
)
74 if ((parent_rate
/ 2) == rate
)
75 mckr
= AT91_PMC_H32MXDIV
;
77 regmap_update_bits(h32mxclk
->regmap
, AT91_PMC_MCKR
,
78 AT91_PMC_H32MXDIV
, mckr
);
83 static const struct clk_ops h32mx_ops
= {
84 .recalc_rate
= clk_sama5d4_h32mx_recalc_rate
,
85 .round_rate
= clk_sama5d4_h32mx_round_rate
,
86 .set_rate
= clk_sama5d4_h32mx_set_rate
,
89 struct clk_hw
* __init
90 at91_clk_register_h32mx(struct regmap
*regmap
, const char *name
,
91 const char *parent_name
)
93 struct clk_sama5d4_h32mx
*h32mxclk
;
94 struct clk_init_data init
;
97 h32mxclk
= kzalloc(sizeof(*h32mxclk
), GFP_KERNEL
);
99 return ERR_PTR(-ENOMEM
);
102 init
.ops
= &h32mx_ops
;
103 init
.parent_names
= parent_name
? &parent_name
: NULL
;
104 init
.num_parents
= parent_name
? 1 : 0;
105 init
.flags
= CLK_SET_RATE_GATE
;
107 h32mxclk
->hw
.init
= &init
;
108 h32mxclk
->regmap
= regmap
;
110 ret
= clk_hw_register(NULL
, &h32mxclk
->hw
);
116 return &h32mxclk
->hw
;