1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2012 Freescale Semiconductor, Inc.
4 * Copyright 2012 Linaro Ltd.
7 #include <linux/clk-provider.h>
9 #include <linux/slab.h>
10 #include <linux/err.h>
14 * struct clk_pfd - IMX PFD clock
16 * @reg: PFD register address
17 * @idx: the index of PFD encoded in the register
19 * PFD clock found on i.MX6 series. Each register for PFD has 4 clk_pfd
20 * data encoded, and member idx is used to specify the one. And each
21 * register has SET, CLR and TOG registers at offset 0x4 0x8 and 0xc.
29 #define to_clk_pfd(_hw) container_of(_hw, struct clk_pfd, hw)
35 static int clk_pfd_enable(struct clk_hw
*hw
)
37 struct clk_pfd
*pfd
= to_clk_pfd(hw
);
39 writel_relaxed(1 << ((pfd
->idx
+ 1) * 8 - 1), pfd
->reg
+ CLR
);
44 static void clk_pfd_disable(struct clk_hw
*hw
)
46 struct clk_pfd
*pfd
= to_clk_pfd(hw
);
48 writel_relaxed(1 << ((pfd
->idx
+ 1) * 8 - 1), pfd
->reg
+ SET
);
51 static unsigned long clk_pfd_recalc_rate(struct clk_hw
*hw
,
52 unsigned long parent_rate
)
54 struct clk_pfd
*pfd
= to_clk_pfd(hw
);
55 u64 tmp
= parent_rate
;
56 u8 frac
= (readl_relaxed(pfd
->reg
) >> (pfd
->idx
* 8)) & 0x3f;
64 static long clk_pfd_round_rate(struct clk_hw
*hw
, unsigned long rate
,
70 tmp
= tmp
* 18 + rate
/ 2;
84 static int clk_pfd_set_rate(struct clk_hw
*hw
, unsigned long rate
,
85 unsigned long parent_rate
)
87 struct clk_pfd
*pfd
= to_clk_pfd(hw
);
88 u64 tmp
= parent_rate
;
91 tmp
= tmp
* 18 + rate
/ 2;
99 writel_relaxed(0x3f << (pfd
->idx
* 8), pfd
->reg
+ CLR
);
100 writel_relaxed(frac
<< (pfd
->idx
* 8), pfd
->reg
+ SET
);
105 static int clk_pfd_is_enabled(struct clk_hw
*hw
)
107 struct clk_pfd
*pfd
= to_clk_pfd(hw
);
109 if (readl_relaxed(pfd
->reg
) & (1 << ((pfd
->idx
+ 1) * 8 - 1)))
115 static const struct clk_ops clk_pfd_ops
= {
116 .enable
= clk_pfd_enable
,
117 .disable
= clk_pfd_disable
,
118 .recalc_rate
= clk_pfd_recalc_rate
,
119 .round_rate
= clk_pfd_round_rate
,
120 .set_rate
= clk_pfd_set_rate
,
121 .is_enabled
= clk_pfd_is_enabled
,
124 struct clk_hw
*imx_clk_hw_pfd(const char *name
, const char *parent_name
,
125 void __iomem
*reg
, u8 idx
)
129 struct clk_init_data init
;
132 pfd
= kzalloc(sizeof(*pfd
), GFP_KERNEL
);
134 return ERR_PTR(-ENOMEM
);
140 init
.ops
= &clk_pfd_ops
;
142 init
.parent_names
= &parent_name
;
143 init
.num_parents
= 1;
145 pfd
->hw
.init
= &init
;
148 ret
= clk_hw_register(NULL
, hw
);