blob: b31f3ad946d6adeb694ef4e82d729f8c7570c5f8 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Maxime Ripard89a3dfb2016-06-29 21:05:24 +02002/*
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
Maxime Ripard89a3dfb2016-06-29 21:05:24 +02005 */
6
7#include <linux/clk-provider.h>
Stephen Boyd62e59c42019-04-18 15:20:22 -07008#include <linux/io.h>
Maxime Ripard89a3dfb2016-06-29 21:05:24 +02009#include <linux/spinlock.h>
10
11#include "ccu_frac.h"
12
13bool ccu_frac_helper_is_enabled(struct ccu_common *common,
Maxime Riparda501a142016-09-30 10:05:32 +020014 struct ccu_frac_internal *cf)
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020015{
16 if (!(common->features & CCU_FEATURE_FRACTIONAL))
17 return false;
18
19 return !(readl(common->base + common->reg) & cf->enable);
20}
Samuel Holland551b62b2021-11-18 21:33:34 -060021EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_is_enabled, SUNXI_CCU);
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020022
23void ccu_frac_helper_enable(struct ccu_common *common,
Maxime Riparda501a142016-09-30 10:05:32 +020024 struct ccu_frac_internal *cf)
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020025{
26 unsigned long flags;
27 u32 reg;
28
29 if (!(common->features & CCU_FEATURE_FRACTIONAL))
30 return;
31
32 spin_lock_irqsave(common->lock, flags);
33 reg = readl(common->base + common->reg);
34 writel(reg & ~cf->enable, common->base + common->reg);
35 spin_unlock_irqrestore(common->lock, flags);
36}
Samuel Holland551b62b2021-11-18 21:33:34 -060037EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_enable, SUNXI_CCU);
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020038
39void ccu_frac_helper_disable(struct ccu_common *common,
Maxime Riparda501a142016-09-30 10:05:32 +020040 struct ccu_frac_internal *cf)
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020041{
42 unsigned long flags;
43 u32 reg;
44
45 if (!(common->features & CCU_FEATURE_FRACTIONAL))
46 return;
47
48 spin_lock_irqsave(common->lock, flags);
49 reg = readl(common->base + common->reg);
50 writel(reg | cf->enable, common->base + common->reg);
51 spin_unlock_irqrestore(common->lock, flags);
52}
Samuel Holland551b62b2021-11-18 21:33:34 -060053EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_disable, SUNXI_CCU);
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020054
55bool ccu_frac_helper_has_rate(struct ccu_common *common,
Maxime Riparda501a142016-09-30 10:05:32 +020056 struct ccu_frac_internal *cf,
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020057 unsigned long rate)
58{
59 if (!(common->features & CCU_FEATURE_FRACTIONAL))
60 return false;
61
62 return (cf->rates[0] == rate) || (cf->rates[1] == rate);
63}
Samuel Holland551b62b2021-11-18 21:33:34 -060064EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_has_rate, SUNXI_CCU);
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020065
66unsigned long ccu_frac_helper_read_rate(struct ccu_common *common,
Maxime Riparda501a142016-09-30 10:05:32 +020067 struct ccu_frac_internal *cf)
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020068{
69 u32 reg;
70
Jernej Škrabecb655f362017-07-30 18:41:49 +020071 pr_debug("%s: Read fractional\n", clk_hw_get_name(&common->hw));
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020072
73 if (!(common->features & CCU_FEATURE_FRACTIONAL))
74 return 0;
75
Jernej Škrabecb655f362017-07-30 18:41:49 +020076 pr_debug("%s: clock is fractional (rates %lu and %lu)\n",
77 clk_hw_get_name(&common->hw), cf->rates[0], cf->rates[1]);
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020078
79 reg = readl(common->base + common->reg);
80
Jernej Škrabecb655f362017-07-30 18:41:49 +020081 pr_debug("%s: clock reg is 0x%x (select is 0x%x)\n",
82 clk_hw_get_name(&common->hw), reg, cf->select);
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020083
84 return (reg & cf->select) ? cf->rates[1] : cf->rates[0];
85}
Samuel Holland551b62b2021-11-18 21:33:34 -060086EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_read_rate, SUNXI_CCU);
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020087
88int ccu_frac_helper_set_rate(struct ccu_common *common,
Maxime Riparda501a142016-09-30 10:05:32 +020089 struct ccu_frac_internal *cf,
Jernej Škrabec1d424602017-07-30 18:41:50 +020090 unsigned long rate, u32 lock)
Maxime Ripard89a3dfb2016-06-29 21:05:24 +020091{
92 unsigned long flags;
93 u32 reg, sel;
94
95 if (!(common->features & CCU_FEATURE_FRACTIONAL))
96 return -EINVAL;
97
98 if (cf->rates[0] == rate)
99 sel = 0;
100 else if (cf->rates[1] == rate)
101 sel = cf->select;
102 else
103 return -EINVAL;
104
105 spin_lock_irqsave(common->lock, flags);
106 reg = readl(common->base + common->reg);
107 reg &= ~cf->select;
108 writel(reg | sel, common->base + common->reg);
109 spin_unlock_irqrestore(common->lock, flags);
110
Jernej Škrabec1d424602017-07-30 18:41:50 +0200111 ccu_helper_wait_for_lock(common, lock);
112
Maxime Ripard89a3dfb2016-06-29 21:05:24 +0200113 return 0;
114}
Samuel Holland551b62b2021-11-18 21:33:34 -0600115EXPORT_SYMBOL_NS_GPL(ccu_frac_helper_set_rate, SUNXI_CCU);