WIP FPC-III support
[linux/fpc-iii.git] / drivers / clk / sunxi-ng / ccu_frac.c
blob44fcded8b3547de7327eca6fa60567d77d572e65
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 */
7 #include <linux/clk-provider.h>
8 #include <linux/io.h>
9 #include <linux/spinlock.h>
11 #include "ccu_frac.h"
13 bool ccu_frac_helper_is_enabled(struct ccu_common *common,
14 struct ccu_frac_internal *cf)
16 if (!(common->features & CCU_FEATURE_FRACTIONAL))
17 return false;
19 return !(readl(common->base + common->reg) & cf->enable);
22 void ccu_frac_helper_enable(struct ccu_common *common,
23 struct ccu_frac_internal *cf)
25 unsigned long flags;
26 u32 reg;
28 if (!(common->features & CCU_FEATURE_FRACTIONAL))
29 return;
31 spin_lock_irqsave(common->lock, flags);
32 reg = readl(common->base + common->reg);
33 writel(reg & ~cf->enable, common->base + common->reg);
34 spin_unlock_irqrestore(common->lock, flags);
37 void ccu_frac_helper_disable(struct ccu_common *common,
38 struct ccu_frac_internal *cf)
40 unsigned long flags;
41 u32 reg;
43 if (!(common->features & CCU_FEATURE_FRACTIONAL))
44 return;
46 spin_lock_irqsave(common->lock, flags);
47 reg = readl(common->base + common->reg);
48 writel(reg | cf->enable, common->base + common->reg);
49 spin_unlock_irqrestore(common->lock, flags);
52 bool ccu_frac_helper_has_rate(struct ccu_common *common,
53 struct ccu_frac_internal *cf,
54 unsigned long rate)
56 if (!(common->features & CCU_FEATURE_FRACTIONAL))
57 return false;
59 return (cf->rates[0] == rate) || (cf->rates[1] == rate);
62 unsigned long ccu_frac_helper_read_rate(struct ccu_common *common,
63 struct ccu_frac_internal *cf)
65 u32 reg;
67 pr_debug("%s: Read fractional\n", clk_hw_get_name(&common->hw));
69 if (!(common->features & CCU_FEATURE_FRACTIONAL))
70 return 0;
72 pr_debug("%s: clock is fractional (rates %lu and %lu)\n",
73 clk_hw_get_name(&common->hw), cf->rates[0], cf->rates[1]);
75 reg = readl(common->base + common->reg);
77 pr_debug("%s: clock reg is 0x%x (select is 0x%x)\n",
78 clk_hw_get_name(&common->hw), reg, cf->select);
80 return (reg & cf->select) ? cf->rates[1] : cf->rates[0];
83 int ccu_frac_helper_set_rate(struct ccu_common *common,
84 struct ccu_frac_internal *cf,
85 unsigned long rate, u32 lock)
87 unsigned long flags;
88 u32 reg, sel;
90 if (!(common->features & CCU_FEATURE_FRACTIONAL))
91 return -EINVAL;
93 if (cf->rates[0] == rate)
94 sel = 0;
95 else if (cf->rates[1] == rate)
96 sel = cf->select;
97 else
98 return -EINVAL;
100 spin_lock_irqsave(common->lock, flags);
101 reg = readl(common->base + common->reg);
102 reg &= ~cf->select;
103 writel(reg | sel, common->base + common->reg);
104 spin_unlock_irqrestore(common->lock, flags);
106 ccu_helper_wait_for_lock(common, lock);
108 return 0;