Linux 4.18.10
[linux/fpc-iii.git] / drivers / clk / sunxi-ng / ccu_frac.c
blobd1d168d4c4f00b2de8e30d1a9dc532e7fecc4578
1 /*
2 * Copyright (C) 2016 Maxime Ripard
3 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 */
11 #include <linux/clk-provider.h>
12 #include <linux/spinlock.h>
14 #include "ccu_frac.h"
16 bool ccu_frac_helper_is_enabled(struct ccu_common *common,
17 struct ccu_frac_internal *cf)
19 if (!(common->features & CCU_FEATURE_FRACTIONAL))
20 return false;
22 return !(readl(common->base + common->reg) & cf->enable);
25 void ccu_frac_helper_enable(struct ccu_common *common,
26 struct ccu_frac_internal *cf)
28 unsigned long flags;
29 u32 reg;
31 if (!(common->features & CCU_FEATURE_FRACTIONAL))
32 return;
34 spin_lock_irqsave(common->lock, flags);
35 reg = readl(common->base + common->reg);
36 writel(reg & ~cf->enable, common->base + common->reg);
37 spin_unlock_irqrestore(common->lock, flags);
40 void ccu_frac_helper_disable(struct ccu_common *common,
41 struct ccu_frac_internal *cf)
43 unsigned long flags;
44 u32 reg;
46 if (!(common->features & CCU_FEATURE_FRACTIONAL))
47 return;
49 spin_lock_irqsave(common->lock, flags);
50 reg = readl(common->base + common->reg);
51 writel(reg | cf->enable, common->base + common->reg);
52 spin_unlock_irqrestore(common->lock, flags);
55 bool ccu_frac_helper_has_rate(struct ccu_common *common,
56 struct ccu_frac_internal *cf,
57 unsigned long rate)
59 if (!(common->features & CCU_FEATURE_FRACTIONAL))
60 return false;
62 return (cf->rates[0] == rate) || (cf->rates[1] == rate);
65 unsigned long ccu_frac_helper_read_rate(struct ccu_common *common,
66 struct ccu_frac_internal *cf)
68 u32 reg;
70 pr_debug("%s: Read fractional\n", clk_hw_get_name(&common->hw));
72 if (!(common->features & CCU_FEATURE_FRACTIONAL))
73 return 0;
75 pr_debug("%s: clock is fractional (rates %lu and %lu)\n",
76 clk_hw_get_name(&common->hw), cf->rates[0], cf->rates[1]);
78 reg = readl(common->base + common->reg);
80 pr_debug("%s: clock reg is 0x%x (select is 0x%x)\n",
81 clk_hw_get_name(&common->hw), reg, cf->select);
83 return (reg & cf->select) ? cf->rates[1] : cf->rates[0];
86 int ccu_frac_helper_set_rate(struct ccu_common *common,
87 struct ccu_frac_internal *cf,
88 unsigned long rate, u32 lock)
90 unsigned long flags;
91 u32 reg, sel;
93 if (!(common->features & CCU_FEATURE_FRACTIONAL))
94 return -EINVAL;
96 if (cf->rates[0] == rate)
97 sel = 0;
98 else if (cf->rates[1] == rate)
99 sel = cf->select;
100 else
101 return -EINVAL;
103 spin_lock_irqsave(common->lock, flags);
104 reg = readl(common->base + common->reg);
105 reg &= ~cf->select;
106 writel(reg | sel, common->base + common->reg);
107 spin_unlock_irqrestore(common->lock, flags);
109 ccu_helper_wait_for_lock(common, lock);
111 return 0;