Skip to contentSkip to conversation

clk: fractional-divider: Improve approximation when zero based

PATCH Superseded Frank Oltmanns 1 participant 3 messages Jun 14, 2023 Common clk

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover clk: fractional-divider: Improve approximation when zero based

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.

V2: https://lore.kernel.org/lkml/[email protected]/
V1: https://lore.kernel.org/lkml/[email protected]/

Changes since V2:
- Completely reworked the test cases
- Moved tests to separate file as per Stephen's request
- Move each edge case into their individual test case as per Stephen's request
- Test clk_fd_round_rate instead of clk_fractional_divider_general_approximation as testing the latter broke builds

Changes since V1:
- Added test case as requested by Stephen Boyd
- Fixed commit message as the Cc: was missing a closing bracket, so that the original mail unfortunately did not go out to A. s. Dong.

Thank you for considering this contribution,
Frank

Frank Oltmanns (2):
clk: fractional-divider: Improve approximation when zero based
clk: fractional-divider: tests: Add test suite for edge cases

 drivers/clk/.kunitconfig                  |   1 +
 drivers/clk/Kconfig                       |   7 +
 drivers/clk/Makefile                      |   1 +
 drivers/clk/clk-fractional-divider.c      |  26 ++-
 drivers/clk/clk-fractional-divider_test.c | 196 ++++++++++++++++++++++
 5 files changed, 224 insertions(+), 7 deletions(-)
 create mode 100644 drivers/clk/clk-fractional-divider_test.c
Signature · 1 line
2.41.0
#1 lore raw

PATCH v3 1/2 clk: fractional-divider: Improve approximation when zero based

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.

---
 drivers/clk/clk-fractional-divider.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/clk/clk-fractional-divider.c +19 -7
index 479297763e70..7da21cd2bdb1 100644
@@ -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--;
Signature · 1 line
2.41.0
#2 lore raw

PATCH v3 2/2 clk: fractional-divider: tests: Add test suite for edge cases

In reply to Frank Oltmanns

In light of the recent discovery that the fractional divisor approximation does not utilize the full available range for clocks that are flagged CLK_FRAC_DIVIDER_ZERO_BASED, implement tests for the edge cases of this clock type.

---

Please note: I get two checkpatch warnings for this patch:
- Concerning the help text in Kconfig.
- Regarding the file being added, asking if MAINTAINERS needs updating.

Both the help text as well as the MAINTAINERS file seem fine to me.

As expected, when the tests are run *without* PATCH 1, the two "zero based" test cases fail:

================= clk-fd-test (4 subtests) =================
[PASSED] clk_fd_test_round_rate_max_div
[PASSED] clk_fd_test_round_rate_max_mul
# clk_fd_test_round_rate_max_div_zero_based: EXPECTATION FAILED at drivers/clk/clk-fractional-divider_test.c:139 Expected rounded_rate == parent_rate / 8, but

    rounded_rate == 342857142 (0x146f95b6)
    parent_rate / 8 == 300000000 (0x11e1a300)
[FAILED] clk_fd_test_round_rate_max_div_zero_based
# clk_fd_test_round_rate_max_mul_zero_based: EXPECTATION FAILED at drivers/clk/clk-fractional-divider_test.c:172
Expected rounded_rate == rate, but
    rounded_rate == 252000000 (0xf053700)
    rate == 240000000 (0xe4e1c00)

[FAILED] clk_fd_test_round_rate_max_mul_zero_based # clk-fd-test: pass:2 fail:2 skip:0 total:4
# Totals: pass:2 fail:2 skip:0 total:4 =================== [FAILED] clk-fd-test ===================

Best regards,
Frank

 drivers/clk/.kunitconfig                  |   1 +
 drivers/clk/Kconfig                       |   7 +
 drivers/clk/Makefile                      |   1 +
 drivers/clk/clk-fractional-divider_test.c | 196 ++++++++++++++++++++++
 4 files changed, 205 insertions(+)
 create mode 100644 drivers/clk/clk-fractional-divider_test.c
drivers/clk/.kunitconfig +0 -0
index 2fbeb71316f8..efa12ac2b3f2 100644

*** a/drivers/clk/.kunitconfig
--- b/drivers/clk/.kunitconfig
***************
*** 2,5 ****
--- 2,6 ----
CONFIG_COMMON_CLK=y
CONFIG_CLK_KUNIT_TEST=y
CONFIG_CLK_GATE_KUNIT_TEST=y
+ CONFIG_CLK_FD_KUNIT_TEST=y
CONFIG_UML_PCI_OVER_VIRTIO=n

drivers/clk/Kconfig +0 -0
index 016814e15536..3fbb40cb5551 100644

*** a/drivers/clk/Kconfig
--- b/drivers/clk/Kconfig
***************
*** 513,516 ****
--- 513,523 ----
help
Kunit test for the basic clk gate type.

+ config CLK_FD_KUNIT_TEST
+ tristate "Basic fractional divider type Kunit test" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ Kunit test for the clk-fractional-divider type.
+
endif

drivers/clk/Makefile +0 -0
index 0aebef17edc6..9d2337c12dd1 100644

*** a/drivers/clk/Makefile
--- b/drivers/clk/Makefile
@@ -12,6 +12,7 @@
obj-$(CONFIG_COMMON_CLK) += clk-mux.o
obj-$(CONFIG_COMMON_CLK) += clk-composite.o
obj-$(CONFIG_COMMON_CLK) += clk-fractional-divider.o
+obj-$(CONFIG_CLK_FD_KUNIT_TEST) += clk-fractional-divider_test.o
obj-$(CONFIG_COMMON_CLK) += clk-gpio.o
ifeq ($(CONFIG_OF), y)
obj-$(CONFIG_COMMON_CLK) += clk-conf.o

drivers/clk/clk-fractional-divider_test.c new file +196 -0

196 lines not on this page · raw message

Signature · 1 line
2.41.0