Path: blob/master/drivers/clk/clk-fractional-divider_test.c
26278 views
// SPDX-License-Identifier: GPL-2.01/*2* Kunit tests for clk fractional divider3*/4#include <linux/clk-provider.h>5#include <kunit/test.h>67#include "clk-fractional-divider.h"89/*10* Test the maximum denominator case for fd clock without flags.11*12* Expect the highest possible denominator to be used in order to get as close as possible to the13* requested rate.14*/15static void clk_fd_test_approximation_max_denominator(struct kunit *test)16{17struct clk_fractional_divider *fd;18unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;1920fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);21KUNIT_ASSERT_NOT_NULL(test, fd);2223fd->mwidth = 3;24fd->nwidth = 3;25max_n = 7;2627rate = 240000000;28parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */29parent_rate_before = parent_rate;3031clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);32KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);3334KUNIT_EXPECT_EQ(test, m, 1);35KUNIT_EXPECT_EQ(test, n, max_n);36}3738/*39* Test the maximum numerator case for fd clock without flags.40*41* Expect the highest possible numerator to be used in order to get as close as possible to the42* requested rate.43*/44static void clk_fd_test_approximation_max_numerator(struct kunit *test)45{46struct clk_fractional_divider *fd;47unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;4849fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);50KUNIT_ASSERT_NOT_NULL(test, fd);5152fd->mwidth = 3;53max_m = 7;54fd->nwidth = 3;5556rate = 240000000;57parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */58parent_rate_before = parent_rate;5960clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);61KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);6263KUNIT_EXPECT_EQ(test, m, max_m);64KUNIT_EXPECT_EQ(test, n, 1);65}6667/*68* Test the maximum denominator case for zero based fd clock.69*70* Expect the highest possible denominator to be used in order to get as close as possible to the71* requested rate.72*/73static void clk_fd_test_approximation_max_denominator_zero_based(struct kunit *test)74{75struct clk_fractional_divider *fd;76unsigned long rate, parent_rate, parent_rate_before, m, n, max_n;7778fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);79KUNIT_ASSERT_NOT_NULL(test, fd);8081fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;82fd->mwidth = 3;83fd->nwidth = 3;84max_n = 8;8586rate = 240000000;87parent_rate = (max_n + 1) * rate; /* so that it exceeds the maximum divisor */88parent_rate_before = parent_rate;8990clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);91KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);9293KUNIT_EXPECT_EQ(test, m, 1);94KUNIT_EXPECT_EQ(test, n, max_n);95}9697/*98* Test the maximum numerator case for zero based fd clock.99*100* Expect the highest possible numerator to be used in order to get as close as possible to the101* requested rate.102*/103static void clk_fd_test_approximation_max_numerator_zero_based(struct kunit *test)104{105struct clk_fractional_divider *fd;106unsigned long rate, parent_rate, parent_rate_before, m, n, max_m;107108fd = kunit_kzalloc(test, sizeof(*fd), GFP_KERNEL);109KUNIT_ASSERT_NOT_NULL(test, fd);110111fd->flags = CLK_FRAC_DIVIDER_ZERO_BASED;112fd->mwidth = 3;113max_m = 8;114fd->nwidth = 3;115116rate = 240000000;117parent_rate = rate / (max_m + 1); /* so that it exceeds the maximum numerator */118parent_rate_before = parent_rate;119120clk_fractional_divider_general_approximation(&fd->hw, rate, &parent_rate, &m, &n);121KUNIT_ASSERT_EQ(test, parent_rate, parent_rate_before);122123KUNIT_EXPECT_EQ(test, m, max_m);124KUNIT_EXPECT_EQ(test, n, 1);125}126127static struct kunit_case clk_fd_approximation_test_cases[] = {128KUNIT_CASE(clk_fd_test_approximation_max_denominator),129KUNIT_CASE(clk_fd_test_approximation_max_numerator),130KUNIT_CASE(clk_fd_test_approximation_max_denominator_zero_based),131KUNIT_CASE(clk_fd_test_approximation_max_numerator_zero_based),132{}133};134135/*136* Test suite for clk_fractional_divider_general_approximation().137*/138static struct kunit_suite clk_fd_approximation_suite = {139.name = "clk-fd-approximation",140.test_cases = clk_fd_approximation_test_cases,141};142143kunit_test_suites(144&clk_fd_approximation_suite145);146MODULE_DESCRIPTION("Kunit tests for clk fractional divider");147MODULE_LICENSE("GPL");148149150