Linux-ARM-Kernel Archive mirror
 help / color / mirror / Atom feed
* [PATCH] clk: fractional-divider: Improve approximation when zero based
@ 2023-05-29 13:34 Frank Oltmanns
  2023-06-12 21:39 ` Stephen Boyd
  0 siblings, 1 reply; 3+ messages in thread
From: Frank Oltmanns @ 2023-05-29 13:34 UTC (permalink / raw
  To: Michael Turquette, Stephen Boyd
  Cc: Frank Oltmanns, Abel Vesa, Peng Fan, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, NXP Linux Team, Jacky Bai,
	Anson Huang, linux-clk, linux-arm-kernel, linux-kernel

Consider the CLK_FRAC_DIVIDER_ZERO_BASED flag when finding the best
approximation for m and n. By doing so, increase the range of valid
values for the numerator and denominator by 1.

Cc: A.s. Dong <aisheng.dong@nxp.com
Signed-off-by: Frank Oltmanns <frank@oltmanns.dev>
---
I stumpled upon this, when familiarizing myself with clk drivers. Unfortunately,
I have no boards to test this patch. It seems the only user of this flag in
mainline is drivers/clk/imx/clk-composite-7ulp.c, therefore I'm cc-ing
get_maintainers.pl --git-blame -f drivers/clk/imx/clk-composite-7ulp.c
in the hopes of a wider audience.

Thank you for considering this contribution,
  Frank

 drivers/clk/clk-fractional-divider.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c
index 479297763e70..7da21cd2bdb1 100644
--- a/drivers/clk/clk-fractional-divider.c
+++ b/drivers/clk/clk-fractional-divider.c
@@ -123,6 +123,7 @@ void clk_fractional_divider_general_approximation(struct clk_hw *hw,
 						  unsigned long *m, unsigned long *n)
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
+	unsigned long max_m, max_n;
 
 	/*
 	 * Get rate closer to *parent_rate to guarantee there is no overflow
@@ -138,9 +139,15 @@ void clk_fractional_divider_general_approximation(struct clk_hw *hw,
 			rate <<= scale - fd->nwidth;
 	}
 
-	rational_best_approximation(rate, *parent_rate,
-			GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
-			m, n);
+	if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
+		max_m = 1 << fd->mwidth;
+		max_n = 1 << fd->nwidth;
+	} else {
+		max_m = GENMASK(fd->mwidth - 1, 0);
+		max_n = GENMASK(fd->nwidth - 1, 0);
+	}
+
+	rational_best_approximation(rate, *parent_rate, max_m, max_n, m, n);
 }
 
 static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
@@ -169,13 +176,18 @@ static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
 	unsigned long flags = 0;
-	unsigned long m, n;
+	unsigned long m, n, max_m, max_n;
 	u32 mmask, nmask;
 	u32 val;
 
-	rational_best_approximation(rate, parent_rate,
-			GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
-			&m, &n);
+	if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
+		max_m = 1 << fd->mwidth;
+		max_n = 1 << fd->nwidth;
+	} else {
+		max_m = GENMASK(fd->mwidth - 1, 0);
+		max_n = GENMASK(fd->nwidth - 1, 0);
+	}
+	rational_best_approximation(rate, parent_rate, max_m, max_n, &m, &n);
 
 	if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
 		m--;
-- 
2.40.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] clk: fractional-divider: Improve approximation when zero based
  2023-05-29 13:34 [PATCH] clk: fractional-divider: Improve approximation when zero based Frank Oltmanns
@ 2023-06-12 21:39 ` Stephen Boyd
  2023-06-13  8:40   ` Frank Oltmanns
  0 siblings, 1 reply; 3+ messages in thread
From: Stephen Boyd @ 2023-06-12 21:39 UTC (permalink / raw
  To: Frank Oltmanns, Michael Turquette
  Cc: Frank Oltmanns, Abel Vesa, Peng Fan, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, NXP Linux Team, Jacky Bai,
	Anson Huang, linux-clk, linux-arm-kernel, linux-kernel

Quoting Frank Oltmanns (2023-05-29 06:34:33)
> Consider the CLK_FRAC_DIVIDER_ZERO_BASED flag when finding the best
> approximation for m and n. By doing so, increase the range of valid
> values for the numerator and denominator by 1.
> 
> Cc: A.s. Dong <aisheng.dong@nxp.com
> Signed-off-by: Frank Oltmanns <frank@oltmanns.dev>
> ---
> I stumpled upon this, when familiarizing myself with clk drivers. Unfortunately,
> I have no boards to test this patch. It seems the only user of this flag in
> mainline is drivers/clk/imx/clk-composite-7ulp.c, therefore I'm cc-ing
> get_maintainers.pl --git-blame -f drivers/clk/imx/clk-composite-7ulp.c
> in the hopes of a wider audience.
> 
> Thank you for considering this contribution,

Thanks for looking at this. Can you add a kunit test (or a suite of
tests) to confirm that this doesn't break existing functionality and
also improves a case that would have failed or been suboptimal before?

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] clk: fractional-divider: Improve approximation when zero based
  2023-06-12 21:39 ` Stephen Boyd
@ 2023-06-13  8:40   ` Frank Oltmanns
  0 siblings, 0 replies; 3+ messages in thread
From: Frank Oltmanns @ 2023-06-13  8:40 UTC (permalink / raw
  To: Stephen Boyd
  Cc: Michael Turquette, Abel Vesa, Peng Fan, Shawn Guo, Sascha Hauer,
	Pengutronix Kernel Team, Fabio Estevam, NXP Linux Team, Jacky Bai,
	Anson Huang, linux-clk, linux-arm-kernel, linux-kernel

Hi Stephen,

On 2023-06-12 at 14:39:00 -0700, Stephen Boyd <sboyd@kernel.org> wrote:
> Quoting Frank Oltmanns (2023-05-29 06:34:33)
>> Consider the CLK_FRAC_DIVIDER_ZERO_BASED flag when finding the best
>> approximation for m and n. By doing so, increase the range of valid
>> values for the numerator and denominator by 1.
>>
>> Cc: A.s. Dong <aisheng.dong@nxp.com
>> Signed-off-by: Frank Oltmanns <frank@oltmanns.dev>
>> ---
>> I stumpled upon this, when familiarizing myself with clk drivers. Unfortunately,
>> I have no boards to test this patch. It seems the only user of this flag in
>> mainline is drivers/clk/imx/clk-composite-7ulp.c, therefore I'm cc-ing
>> get_maintainers.pl --git-blame -f drivers/clk/imx/clk-composite-7ulp.c
>> in the hopes of a wider audience.
>>
>> Thank you for considering this contribution,
>
> Thanks for looking at this. Can you add a kunit test (or a suite of
> tests) to confirm that this doesn't break existing functionality and
> also improves a case that would have failed or been suboptimal before?

Thank you for your feedback, I've submitted a V2 that contains the
tests:

https://lore.kernel.org/lkml/20230613083626.227476-1-frank@oltmanns.dev/

Thanks,
  Frank

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-13  8:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-29 13:34 [PATCH] clk: fractional-divider: Improve approximation when zero based Frank Oltmanns
2023-06-12 21:39 ` Stephen Boyd
2023-06-13  8:40   ` Frank Oltmanns

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 read-only IMAP folder(s) and NNTP newsgroup(s).