2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright 2012 Linaro Ltd.
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
13 #include <linux/clk-provider.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
20 * struct clk_pfd - IMX PFD clock
21 * @clk_hw: clock source
22 * @reg: PFD register address
23 * @idx: the index of PFD encoded in the register
25 * PFD clock found on i.MX6 series. Each register for PFD has 4 clk_pfd
26 * data encoded, and member idx is used to specify the one. And each
27 * register has SET, CLR and TOG registers at offset 0x4 0x8 and 0xc.
35 #define to_clk_pfd(_hw) container_of(_hw, struct clk_pfd, hw)
41 static int clk_pfd_enable(struct clk_hw *hw)
43 struct clk_pfd *pfd = to_clk_pfd(hw);
45 writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + CLR);
50 static void clk_pfd_disable(struct clk_hw *hw)
52 struct clk_pfd *pfd = to_clk_pfd(hw);
54 writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + SET);
57 static unsigned long clk_pfd_recalc_rate(struct clk_hw *hw,
58 unsigned long parent_rate)
60 struct clk_pfd *pfd = to_clk_pfd(hw);
61 u64 tmp = parent_rate;
62 u8 frac = (readl_relaxed(pfd->reg) >> (pfd->idx * 8)) & 0x3f;
70 static long clk_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
76 tmp = tmp * 18 + rate / 2;
90 static int clk_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
91 unsigned long parent_rate)
93 struct clk_pfd *pfd = to_clk_pfd(hw);
94 u64 tmp = parent_rate;
97 tmp = tmp * 18 + rate / 2;
105 writel_relaxed(0x3f << (pfd->idx * 8), pfd->reg + CLR);
106 writel_relaxed(frac << (pfd->idx * 8), pfd->reg + SET);
111 static int clk_pfd_is_enabled(struct clk_hw *hw)
113 struct clk_pfd *pfd = to_clk_pfd(hw);
115 if (readl_relaxed(pfd->reg) & (1 << ((pfd->idx + 1) * 8 - 1)))
121 static const struct clk_ops clk_pfd_ops = {
122 .enable = clk_pfd_enable,
123 .disable = clk_pfd_disable,
124 .recalc_rate = clk_pfd_recalc_rate,
125 .round_rate = clk_pfd_round_rate,
126 .set_rate = clk_pfd_set_rate,
127 .is_enabled = clk_pfd_is_enabled,
130 struct clk *imx_clk_pfd(const char *name, const char *parent_name,
131 void __iomem *reg, u8 idx)
135 struct clk_init_data init;
137 pfd = kzalloc(sizeof(*pfd), GFP_KERNEL);
139 return ERR_PTR(-ENOMEM);
145 init.ops = &clk_pfd_ops;
147 init.parent_names = &parent_name;
148 init.num_parents = 1;
150 pfd->hw.init = &init;
152 clk = clk_register(NULL, &pfd->hw);