1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
4 * Copyright 2017~2018 NXP
10 #include <linux/clk-provider.h>
11 #include <linux/err.h>
12 #include <linux/iopoll.h>
13 #include <linux/slab.h>
18 * struct clk_pfdv2 - IMX PFD clock
19 * @clk_hw: clock source
20 * @reg: PFD register address
21 * @gate_bit: Gate bit offset
22 * @vld_bit: Valid bit offset
23 * @frac_off: PLL Fractional Divider offset
34 #define to_clk_pfdv2(_hw) container_of(_hw, struct clk_pfdv2, hw)
36 #define CLK_PFDV2_FRAC_MASK 0x3f
38 #define LOCK_TIMEOUT_US USEC_PER_MSEC
40 static DEFINE_SPINLOCK(pfd_lock);
42 static int clk_pfdv2_wait(struct clk_pfdv2 *pfd)
46 return readl_poll_timeout(pfd->reg, val, val & pfd->vld_bit,
50 static int clk_pfdv2_enable(struct clk_hw *hw)
52 struct clk_pfdv2 *pfd = to_clk_pfdv2(hw);
56 spin_lock_irqsave(&pfd_lock, flags);
57 val = readl_relaxed(pfd->reg);
58 val &= ~pfd->gate_bit;
59 writel_relaxed(val, pfd->reg);
60 spin_unlock_irqrestore(&pfd_lock, flags);
62 return clk_pfdv2_wait(pfd);
65 static void clk_pfdv2_disable(struct clk_hw *hw)
67 struct clk_pfdv2 *pfd = to_clk_pfdv2(hw);
71 spin_lock_irqsave(&pfd_lock, flags);
72 val = readl_relaxed(pfd->reg);
74 writel_relaxed(val, pfd->reg);
75 spin_unlock_irqrestore(&pfd_lock, flags);
78 static unsigned long clk_pfdv2_recalc_rate(struct clk_hw *hw,
79 unsigned long parent_rate)
81 struct clk_pfdv2 *pfd = to_clk_pfdv2(hw);
82 u64 tmp = parent_rate;
85 frac = (readl_relaxed(pfd->reg) >> pfd->frac_off)
86 & CLK_PFDV2_FRAC_MASK;
89 pr_debug("clk_pfdv2: %s invalid pfd frac value 0\n",
100 static long clk_pfdv2_round_rate(struct clk_hw *hw, unsigned long rate,
101 unsigned long *prate)
106 tmp = tmp * 18 + rate / 2;
122 static int clk_pfdv2_is_enabled(struct clk_hw *hw)
124 struct clk_pfdv2 *pfd = to_clk_pfdv2(hw);
126 if (readl_relaxed(pfd->reg) & pfd->gate_bit)
132 static int clk_pfdv2_set_rate(struct clk_hw *hw, unsigned long rate,
133 unsigned long parent_rate)
135 struct clk_pfdv2 *pfd = to_clk_pfdv2(hw);
137 u64 tmp = parent_rate;
141 tmp = tmp * 18 + rate / 2;
149 spin_lock_irqsave(&pfd_lock, flags);
150 val = readl_relaxed(pfd->reg);
151 val &= ~(CLK_PFDV2_FRAC_MASK << pfd->frac_off);
152 val |= frac << pfd->frac_off;
153 writel_relaxed(val, pfd->reg);
154 spin_unlock_irqrestore(&pfd_lock, flags);
159 static const struct clk_ops clk_pfdv2_ops = {
160 .enable = clk_pfdv2_enable,
161 .disable = clk_pfdv2_disable,
162 .recalc_rate = clk_pfdv2_recalc_rate,
163 .round_rate = clk_pfdv2_round_rate,
164 .set_rate = clk_pfdv2_set_rate,
165 .is_enabled = clk_pfdv2_is_enabled,
168 struct clk_hw *imx_clk_pfdv2(const char *name, const char *parent_name,
169 void __iomem *reg, u8 idx)
171 struct clk_init_data init;
172 struct clk_pfdv2 *pfd;
178 pfd = kzalloc(sizeof(*pfd), GFP_KERNEL);
180 return ERR_PTR(-ENOMEM);
183 pfd->gate_bit = 1 << ((idx + 1) * 8 - 1);
184 pfd->vld_bit = pfd->gate_bit - 1;
185 pfd->frac_off = idx * 8;
188 init.ops = &clk_pfdv2_ops;
189 init.parent_names = &parent_name;
190 init.num_parents = 1;
191 init.flags = CLK_SET_RATE_GATE;
193 pfd->hw.init = &init;
196 ret = clk_hw_register(NULL, hw);