From: Yao Zi <ziyao@disroot.org>
To: Yinbo Zhu <zhuyinbo@loongson.cn>,
Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>, Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Huacai Chen <chenhuacai@kernel.org>,
WANG Xuerui <kernel@xen0n.name>
Cc: linux-clk@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, loongarch@lists.linux.dev,
Mingcong Bai <jeffbai@aosc.io>,
Kexy Biscuit <kexybiscuit@aosc.io>, Yao Zi <ziyao@disroot.org>
Subject: [PATCH v3 3/8] clk: loongson2: Support scale clocks with an alternative mode
Date: Tue, 5 Aug 2025 15:01:42 +0000 [thread overview]
Message-ID: <20250805150147.25909-4-ziyao@disroot.org> (raw)
In-Reply-To: <20250805150147.25909-1-ziyao@disroot.org>
Loongson 2K0300 and 2K1500 ship scale clocks with an alternative mode.
There's one mode bit in clock configuration register indicating the
operation mode.
When mode bit is unset, the scale clock acts the same as previous
generation of scale clocks. When it's set, a different equation for
calculating result frequency, Fout = Fin / (scale + 1), is used.
This patch adds frequency calculation support for the scale clock
variant. A helper macro, CLK_SCALE_MODE, is added to simplify
definitions.
Signed-off-by: Yao Zi <ziyao@disroot.org>
---
drivers/clk/clk-loongson2.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
diff --git a/drivers/clk/clk-loongson2.c b/drivers/clk/clk-loongson2.c
index cc3fb13e770f..bba97270376c 100644
--- a/drivers/clk/clk-loongson2.c
+++ b/drivers/clk/clk-loongson2.c
@@ -42,6 +42,7 @@ struct loongson2_clk_data {
u8 div_width;
u8 mult_shift;
u8 mult_width;
+ u8 bit_idx;
};
struct loongson2_clk_board_info {
@@ -96,6 +97,19 @@ struct loongson2_clk_board_info {
.div_width = _dwidth, \
}
+#define CLK_SCALE_MODE(_id, _name, _pname, _offset, \
+ _dshift, _dwidth, _midx) \
+ { \
+ .id = _id, \
+ .type = CLK_TYPE_SCALE, \
+ .name = _name, \
+ .parent_name = _pname, \
+ .reg_offset = _offset, \
+ .div_shift = _dshift, \
+ .div_width = _dwidth, \
+ .bit_idx = _midx + 1, \
+ }
+
#define CLK_GATE(_id, _name, _pname, _offset, _bidx) \
{ \
.id = _id, \
@@ -243,13 +257,18 @@ static const struct clk_ops loongson2_pll_recalc_ops = {
static unsigned long loongson2_freqscale_recalc_rate(struct clk_hw *hw,
unsigned long parent_rate)
{
- u64 val, mult;
+ u64 val, scale;
+ u32 mode = 0;
struct loongson2_clk_data *clk = to_loongson2_clk(hw);
val = readq(clk->reg);
- mult = loongson2_rate_part(val, clk->div_shift, clk->div_width) + 1;
+ scale = loongson2_rate_part(val, clk->div_shift, clk->div_width) + 1;
+
+ if (clk->bit_idx)
+ mode = val & BIT(clk->bit_idx - 1);
- return div_u64((u64)parent_rate * mult, 8);
+ return mode == 0 ? div_u64((u64)parent_rate * scale, 8) :
+ div_u64((u64)parent_rate, scale);
}
static const struct clk_ops loongson2_freqscale_recalc_ops = {
@@ -284,6 +303,7 @@ static struct clk_hw *loongson2_clk_register(struct loongson2_clk_provider *clp,
clk->div_width = cld->div_width;
clk->mult_shift = cld->mult_shift;
clk->mult_width = cld->mult_width;
+ clk->bit_idx = cld->bit_idx;
clk->hw.init = &init;
hw = &clk->hw;
--
2.50.1
next prev parent reply other threads:[~2025-08-05 15:04 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-05 15:01 [PATCH v3 0/8] Add clock support for Loongson 2K0300 SoC Yao Zi
2025-08-05 15:01 ` [PATCH v3 1/8] dt-bindings: clock: loongson2: Add Loongson 2K0300 compatible Yao Zi
2025-08-06 8:01 ` Krzysztof Kozlowski
2025-08-06 8:36 ` Huacai Chen
2025-08-06 12:30 ` Yao Zi
2025-08-07 4:44 ` Huacai Chen
2025-08-07 10:04 ` Yanteng Si
2025-08-05 15:01 ` [PATCH v3 2/8] clk: loongson2: Allow specifying clock flags for gate clock Yao Zi
2025-08-05 15:01 ` Yao Zi [this message]
2025-08-07 11:18 ` [PATCH v3 3/8] clk: loongson2: Support scale clocks with an alternative mode Huacai Chen
2025-08-08 3:24 ` Yao Zi
2025-08-08 12:58 ` Huacai Chen
2025-08-05 15:01 ` [PATCH v3 4/8] clk: loongson2: Allow zero divisors for dividers Yao Zi
2025-08-05 15:01 ` [PATCH v3 5/8] clk: loongson2: Avoid hardcoding firmware name of the reference clock Yao Zi
2025-08-05 15:01 ` [PATCH v3 6/8] clk: loongson2: Add clock definitions for Loongson 2K0300 SoC Yao Zi
2025-08-07 11:21 ` Huacai Chen
2025-08-08 3:13 ` Yao Zi
2025-08-05 15:01 ` [PATCH v3 7/8] LoongArch: dts: Add clock tree for Loongson 2K0300 Yao Zi
2025-08-05 15:01 ` [PATCH v3 8/8] LoongArch: dts: Remove clock-frquency from UART0 of CTCISZ Forever Pi Yao Zi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250805150147.25909-4-ziyao@disroot.org \
--to=ziyao@disroot.org \
--cc=chenhuacai@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jeffbai@aosc.io \
--cc=kernel@xen0n.name \
--cc=kexybiscuit@aosc.io \
--cc=krzk+dt@kernel.org \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=loongarch@lists.linux.dev \
--cc=mturquette@baylibre.com \
--cc=robh@kernel.org \
--cc=sboyd@kernel.org \
--cc=zhuyinbo@loongson.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).