Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / clk / axs10x / pll_clock.c
blob500345d99adb98a2141c234beb68bf54bbd78dfd
1 /*
2 * Synopsys AXS10X SDP Generic PLL clock driver
4 * Copyright (C) 2017 Synopsys
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
9 */
11 #include <linux/platform_device.h>
12 #include <linux/module.h>
13 #include <linux/clk-provider.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
17 #include <linux/io.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/slab.h>
21 #include <linux/of.h>
23 /* PLL registers addresses */
24 #define PLL_REG_IDIV 0x0
25 #define PLL_REG_FBDIV 0x4
26 #define PLL_REG_ODIV 0x8
29 * Bit fields of the PLL IDIV/FBDIV/ODIV registers:
30 * ________________________________________________________________________
31 * |31 15| 14 | 13 | 12 |11 6|5 0|
32 * |-------RESRVED------|-NOUPDATE-|-BYPASS-|-EDGE-|--HIGHTIME--|--LOWTIME--|
33 * |____________________|__________|________|______|____________|___________|
35 * Following macros determine the way of access to these registers
36 * They should be set up only using the macros.
37 * reg should be an u32 variable.
40 #define PLL_REG_GET_LOW(reg) \
41 (((reg) & (0x3F << 0)) >> 0)
42 #define PLL_REG_GET_HIGH(reg) \
43 (((reg) & (0x3F << 6)) >> 6)
44 #define PLL_REG_GET_EDGE(reg) \
45 (((reg) & (BIT(12))) ? 1 : 0)
46 #define PLL_REG_GET_BYPASS(reg) \
47 (((reg) & (BIT(13))) ? 1 : 0)
48 #define PLL_REG_GET_NOUPD(reg) \
49 (((reg) & (BIT(14))) ? 1 : 0)
50 #define PLL_REG_GET_PAD(reg) \
51 (((reg) & (0x1FFFF << 15)) >> 15)
53 #define PLL_REG_SET_LOW(reg, value) \
54 { reg |= (((value) & 0x3F) << 0); }
55 #define PLL_REG_SET_HIGH(reg, value) \
56 { reg |= (((value) & 0x3F) << 6); }
57 #define PLL_REG_SET_EDGE(reg, value) \
58 { reg |= (((value) & 0x01) << 12); }
59 #define PLL_REG_SET_BYPASS(reg, value) \
60 { reg |= (((value) & 0x01) << 13); }
61 #define PLL_REG_SET_NOUPD(reg, value) \
62 { reg |= (((value) & 0x01) << 14); }
63 #define PLL_REG_SET_PAD(reg, value) \
64 { reg |= (((value) & 0x1FFFF) << 15); }
66 #define PLL_LOCK BIT(0)
67 #define PLL_ERROR BIT(1)
68 #define PLL_MAX_LOCK_TIME 100 /* 100 us */
70 struct axs10x_pll_cfg {
71 u32 rate;
72 u32 idiv;
73 u32 fbdiv;
74 u32 odiv;
77 static const struct axs10x_pll_cfg arc_pll_cfg[] = {
78 { 33333333, 1, 1, 1 },
79 { 50000000, 1, 30, 20 },
80 { 75000000, 2, 45, 10 },
81 { 90000000, 2, 54, 10 },
82 { 100000000, 1, 30, 10 },
83 { 125000000, 2, 45, 6 },
87 static const struct axs10x_pll_cfg pgu_pll_cfg[] = {
88 { 25200000, 1, 84, 90 },
89 { 50000000, 1, 100, 54 },
90 { 74250000, 1, 44, 16 },
94 struct axs10x_pll_clk {
95 struct clk_hw hw;
96 void __iomem *base;
97 void __iomem *lock;
98 const struct axs10x_pll_cfg *pll_cfg;
99 struct device *dev;
102 static inline void axs10x_pll_write(struct axs10x_pll_clk *clk, u32 reg,
103 u32 val)
105 iowrite32(val, clk->base + reg);
108 static inline u32 axs10x_pll_read(struct axs10x_pll_clk *clk, u32 reg)
110 return ioread32(clk->base + reg);
113 static inline struct axs10x_pll_clk *to_axs10x_pll_clk(struct clk_hw *hw)
115 return container_of(hw, struct axs10x_pll_clk, hw);
118 static inline u32 axs10x_div_get_value(u32 reg)
120 if (PLL_REG_GET_BYPASS(reg))
121 return 1;
123 return PLL_REG_GET_HIGH(reg) + PLL_REG_GET_LOW(reg);
126 static inline u32 axs10x_encode_div(unsigned int id, int upd)
128 u32 div = 0;
130 PLL_REG_SET_LOW(div, (id % 2 == 0) ? id >> 1 : (id >> 1) + 1);
131 PLL_REG_SET_HIGH(div, id >> 1);
132 PLL_REG_SET_EDGE(div, id % 2);
133 PLL_REG_SET_BYPASS(div, id == 1 ? 1 : 0);
134 PLL_REG_SET_NOUPD(div, upd == 0 ? 1 : 0);
136 return div;
139 static unsigned long axs10x_pll_recalc_rate(struct clk_hw *hw,
140 unsigned long parent_rate)
142 u64 rate;
143 u32 idiv, fbdiv, odiv;
144 struct axs10x_pll_clk *clk = to_axs10x_pll_clk(hw);
146 idiv = axs10x_div_get_value(axs10x_pll_read(clk, PLL_REG_IDIV));
147 fbdiv = axs10x_div_get_value(axs10x_pll_read(clk, PLL_REG_FBDIV));
148 odiv = axs10x_div_get_value(axs10x_pll_read(clk, PLL_REG_ODIV));
150 rate = (u64)parent_rate * fbdiv;
151 do_div(rate, idiv * odiv);
153 return rate;
156 static long axs10x_pll_round_rate(struct clk_hw *hw, unsigned long rate,
157 unsigned long *prate)
159 int i;
160 long best_rate;
161 struct axs10x_pll_clk *clk = to_axs10x_pll_clk(hw);
162 const struct axs10x_pll_cfg *pll_cfg = clk->pll_cfg;
164 if (pll_cfg[0].rate == 0)
165 return -EINVAL;
167 best_rate = pll_cfg[0].rate;
169 for (i = 1; pll_cfg[i].rate != 0; i++) {
170 if (abs(rate - pll_cfg[i].rate) < abs(rate - best_rate))
171 best_rate = pll_cfg[i].rate;
174 return best_rate;
177 static int axs10x_pll_set_rate(struct clk_hw *hw, unsigned long rate,
178 unsigned long parent_rate)
180 int i;
181 struct axs10x_pll_clk *clk = to_axs10x_pll_clk(hw);
182 const struct axs10x_pll_cfg *pll_cfg = clk->pll_cfg;
184 for (i = 0; pll_cfg[i].rate != 0; i++) {
185 if (pll_cfg[i].rate == rate) {
186 axs10x_pll_write(clk, PLL_REG_IDIV,
187 axs10x_encode_div(pll_cfg[i].idiv, 0));
188 axs10x_pll_write(clk, PLL_REG_FBDIV,
189 axs10x_encode_div(pll_cfg[i].fbdiv, 0));
190 axs10x_pll_write(clk, PLL_REG_ODIV,
191 axs10x_encode_div(pll_cfg[i].odiv, 1));
194 * Wait until CGU relocks and check error status.
195 * If after timeout CGU is unlocked yet return error
197 udelay(PLL_MAX_LOCK_TIME);
198 if (!(ioread32(clk->lock) & PLL_LOCK))
199 return -ETIMEDOUT;
201 if (ioread32(clk->lock) & PLL_ERROR)
202 return -EINVAL;
204 return 0;
208 dev_err(clk->dev, "invalid rate=%ld, parent_rate=%ld\n", rate,
209 parent_rate);
210 return -EINVAL;
213 static const struct clk_ops axs10x_pll_ops = {
214 .recalc_rate = axs10x_pll_recalc_rate,
215 .round_rate = axs10x_pll_round_rate,
216 .set_rate = axs10x_pll_set_rate,
219 static int axs10x_pll_clk_probe(struct platform_device *pdev)
221 struct device *dev = &pdev->dev;
222 const char *parent_name;
223 struct axs10x_pll_clk *pll_clk;
224 struct clk_init_data init = { };
225 int ret;
227 pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
228 if (!pll_clk)
229 return -ENOMEM;
231 pll_clk->base = devm_platform_ioremap_resource(pdev, 0);
232 if (IS_ERR(pll_clk->base))
233 return PTR_ERR(pll_clk->base);
235 pll_clk->lock = devm_platform_ioremap_resource(pdev, 1);
236 if (IS_ERR(pll_clk->lock))
237 return PTR_ERR(pll_clk->lock);
239 init.name = dev->of_node->name;
240 init.ops = &axs10x_pll_ops;
241 parent_name = of_clk_get_parent_name(dev->of_node, 0);
242 init.parent_names = &parent_name;
243 init.num_parents = 1;
244 pll_clk->hw.init = &init;
245 pll_clk->dev = dev;
246 pll_clk->pll_cfg = of_device_get_match_data(dev);
248 if (!pll_clk->pll_cfg) {
249 dev_err(dev, "No OF match data provided\n");
250 return -EINVAL;
253 ret = devm_clk_hw_register(dev, &pll_clk->hw);
254 if (ret) {
255 dev_err(dev, "failed to register %s clock\n", init.name);
256 return ret;
259 return of_clk_add_hw_provider(dev->of_node, of_clk_hw_simple_get,
260 &pll_clk->hw);
263 static int axs10x_pll_clk_remove(struct platform_device *pdev)
265 of_clk_del_provider(pdev->dev.of_node);
266 return 0;
269 static void __init of_axs10x_pll_clk_setup(struct device_node *node)
271 const char *parent_name;
272 struct axs10x_pll_clk *pll_clk;
273 struct clk_init_data init = { };
274 int ret;
276 pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
277 if (!pll_clk)
278 return;
280 pll_clk->base = of_iomap(node, 0);
281 if (!pll_clk->base) {
282 pr_err("failed to map pll div registers\n");
283 goto err_free_pll_clk;
286 pll_clk->lock = of_iomap(node, 1);
287 if (!pll_clk->lock) {
288 pr_err("failed to map pll lock register\n");
289 goto err_unmap_base;
292 init.name = node->name;
293 init.ops = &axs10x_pll_ops;
294 parent_name = of_clk_get_parent_name(node, 0);
295 init.parent_names = &parent_name;
296 init.num_parents = parent_name ? 1 : 0;
297 pll_clk->hw.init = &init;
298 pll_clk->pll_cfg = arc_pll_cfg;
300 ret = clk_hw_register(NULL, &pll_clk->hw);
301 if (ret) {
302 pr_err("failed to register %pOFn clock\n", node);
303 goto err_unmap_lock;
306 ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, &pll_clk->hw);
307 if (ret) {
308 pr_err("failed to add hw provider for %pOFn clock\n", node);
309 goto err_unregister_clk;
312 return;
314 err_unregister_clk:
315 clk_hw_unregister(&pll_clk->hw);
316 err_unmap_lock:
317 iounmap(pll_clk->lock);
318 err_unmap_base:
319 iounmap(pll_clk->base);
320 err_free_pll_clk:
321 kfree(pll_clk);
323 CLK_OF_DECLARE(axs10x_pll_clock, "snps,axs10x-arc-pll-clock",
324 of_axs10x_pll_clk_setup);
326 static const struct of_device_id axs10x_pll_clk_id[] = {
327 { .compatible = "snps,axs10x-pgu-pll-clock", .data = &pgu_pll_cfg},
330 MODULE_DEVICE_TABLE(of, axs10x_pll_clk_id);
332 static struct platform_driver axs10x_pll_clk_driver = {
333 .driver = {
334 .name = "axs10x-pll-clock",
335 .of_match_table = axs10x_pll_clk_id,
337 .probe = axs10x_pll_clk_probe,
338 .remove = axs10x_pll_clk_remove,
340 builtin_platform_driver(axs10x_pll_clk_driver);
342 MODULE_AUTHOR("Vlad Zakharov <vzakhar@synopsys.com>");
343 MODULE_DESCRIPTION("Synopsys AXS10X SDP Generic PLL Clock Driver");
344 MODULE_LICENSE("GPL v2");