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>
8 #include <linux/export.h>
10 #include <linux/slab.h>
11 #include <linux/err.h>
15 * struct clk_pfd - IMX PFD clock
17 * @reg: PFD register address
18 * @idx: the index of PFD encoded in the register
20 * PFD clock found on i.MX6 series. Each register for PFD has 4 clk_pfd
21 * data encoded, and member idx is used to specify the one. And each
22 * register has SET, CLR and TOG registers at offset 0x4 0x8 and 0xc.
30 #define to_clk_pfd(_hw) container_of(_hw, struct clk_pfd, hw)
36 static int clk_pfd_enable(struct clk_hw *hw)
38 struct clk_pfd *pfd = to_clk_pfd(hw);
40 writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + CLR);
45 static void clk_pfd_disable(struct clk_hw *hw)
47 struct clk_pfd *pfd = to_clk_pfd(hw);
49 writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + SET);
52 static unsigned long clk_pfd_recalc_rate(struct clk_hw *hw,
53 unsigned long parent_rate)
55 struct clk_pfd *pfd = to_clk_pfd(hw);
56 u64 tmp = parent_rate;
57 u8 frac = (readl_relaxed(pfd->reg) >> (pfd->idx * 8)) & 0x3f;
65 static long clk_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
71 tmp = tmp * 18 + rate / 2;
85 static int clk_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
86 unsigned long parent_rate)
88 struct clk_pfd *pfd = to_clk_pfd(hw);
89 u64 tmp = parent_rate;
92 tmp = tmp * 18 + rate / 2;
100 writel_relaxed(0x3f << (pfd->idx * 8), pfd->reg + CLR);
101 writel_relaxed(frac << (pfd->idx * 8), pfd->reg + SET);
106 static int clk_pfd_is_enabled(struct clk_hw *hw)
108 struct clk_pfd *pfd = to_clk_pfd(hw);
110 if (readl_relaxed(pfd->reg) & (1 << ((pfd->idx + 1) * 8 - 1)))
116 static const struct clk_ops clk_pfd_ops = {
117 .enable = clk_pfd_enable,
118 .disable = clk_pfd_disable,
119 .recalc_rate = clk_pfd_recalc_rate,
120 .round_rate = clk_pfd_round_rate,
121 .set_rate = clk_pfd_set_rate,
122 .is_enabled = clk_pfd_is_enabled,
125 struct clk_hw *imx_clk_hw_pfd(const char *name, const char *parent_name,
126 void __iomem *reg, u8 idx)
130 struct clk_init_data init;
133 pfd = kzalloc(sizeof(*pfd), GFP_KERNEL);
135 return ERR_PTR(-ENOMEM);
141 init.ops = &clk_pfd_ops;
143 init.parent_names = &parent_name;
144 init.num_parents = 1;
146 pfd->hw.init = &init;
149 ret = clk_hw_register(NULL, hw);
157 EXPORT_SYMBOL_GPL(imx_clk_hw_pfd);