[PATCH v2 2/2] clk: tests: Add tests for fractional divisor approximation

From: Frank Oltmanns
Date: Tue Jun 13 2023 - 04:37:03 EST


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.

Signed-off-by: Frank Oltmanns <frank@xxxxxxxxxxxx>
Link: https://lore.kernel.org/lkml/20230529133433.56215-1-frank@xxxxxxxxxxxx
---
drivers/clk/clk_test.c | 69 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 68 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/clk_test.c b/drivers/clk/clk_test.c
index f9a5c2964c65..b247ba841cbd 100644
--- a/drivers/clk/clk_test.c
+++ b/drivers/clk/clk_test.c
@@ -8,6 +8,9 @@
/* Needed for clk_hw_get_clk() */
#include "clk.h"

+/* Needed for clk_fractional_divider_general_approximation */
+#include "clk-fractional-divider.h"
+
#include <kunit/test.h>

#define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000)
@@ -2394,6 +2397,69 @@ static struct kunit_suite clk_mux_notifier_test_suite = {
.test_cases = clk_mux_notifier_test_cases,
};

+
+/*
+ * Test that clk_fractional_divider_general_approximation will work with the
+ * highest available numerator and denominator.
+ */
+static void clk_fd_test_round_rate_max_mn(struct kunit *test)
+{
+ struct clk_fractional_divider *fd;
+ struct clk_hw *hw;
+ unsigned long rate, parent_rate, m, n;
+
+ fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, fd);
+
+ fd->mwidth = 3;
+ fd->nwidth = 3;
+
+ hw = &fd->hw;
+
+ rate = DUMMY_CLOCK_RATE_1;
+
+ // Highest denominator, no flags
+ parent_rate = 10 * DUMMY_CLOCK_RATE_1;
+ clk_fractional_divider_general_approximation(hw, rate, &parent_rate, &m, &n);
+ KUNIT_EXPECT_EQ(test, m, 1);
+ KUNIT_EXPECT_EQ(test, n, 7);
+
+ // Highest numerator, no flags
+ parent_rate = DUMMY_CLOCK_RATE_1 / 10;
+ clk_fractional_divider_general_approximation(hw, rate, &parent_rate, &m, &n);
+ KUNIT_EXPECT_EQ(test, m, 7);
+ KUNIT_EXPECT_EQ(test, n, 1);
+
+ // Highest denominator, zero based
+ parent_rate = 10 * DUMMY_CLOCK_RATE_1;
+ fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;
+ clk_fractional_divider_general_approximation(hw, rate, &parent_rate, &m, &n);
+ KUNIT_EXPECT_EQ(test, m, 1);
+ KUNIT_EXPECT_EQ(test, n, 8);
+
+ // Highest numerator, zero based
+ parent_rate = DUMMY_CLOCK_RATE_1 / 10;
+ clk_fractional_divider_general_approximation(hw, rate, &parent_rate, &m, &n);
+ KUNIT_EXPECT_EQ(test, m, 8);
+ KUNIT_EXPECT_EQ(test, n, 1);
+}
+
+static struct kunit_case clk_fd_test_cases[] = {
+ KUNIT_CASE(clk_fd_test_round_rate_max_mn),
+ {}
+};
+
+/*
+ * Test suite for a fractional divider clock.
+ *
+ * These tests exercise the fractional divider API: clk_recalc_rate,
+ * clk_set_rate(), clk_round_rate().
+ */
+static struct kunit_suite clk_fd_test_suite = {
+ .name = "clk-fd-test",
+ .test_cases = clk_fd_test_cases,
+};
+
kunit_test_suites(
&clk_leaf_mux_set_rate_parent_test_suite,
&clk_test_suite,
@@ -2406,6 +2472,7 @@ kunit_test_suites(
&clk_range_maximize_test_suite,
&clk_range_minimize_test_suite,
&clk_single_parent_mux_test_suite,
- &clk_uncached_test_suite
+ &clk_uncached_test_suite,
+ &clk_fd_test_suite
);
MODULE_LICENSE("GPL v2");
--
2.41.0