Skip to content

Commit e978201

Browse files
committed
clk: test: Add KUnit tests for clock-assigned-rates{-u64} DT properties
Add unit tests for the two types of assigned rate properties. Test different combinations of assigned clocks and make sure that rates aren't assigned when the DT properties are malformed or are zero. Cc: Peng Fan <[email protected]> Signed-off-by: Stephen Boyd <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 00977af commit e978201

17 files changed

+569
-0
lines changed

drivers/clk/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ obj-$(CONFIG_HAVE_CLK) += clk-devres.o clk-bulk.o clkdev.o
44
obj-$(CONFIG_COMMON_CLK) += clk.o
55
obj-$(CONFIG_CLK_KUNIT_TEST) += clk-test.o
66
clk-test-y := clk_test.o \
7+
kunit_clk_assigned_rates_u64_one.dtbo.o \
8+
kunit_clk_assigned_rates_u64_one_consumer.dtbo.o \
9+
kunit_clk_assigned_rates_u64_multiple.dtbo.o \
10+
kunit_clk_assigned_rates_u64_multiple_consumer.dtbo.o \
11+
kunit_clk_assigned_rates_multiple.dtbo.o \
12+
kunit_clk_assigned_rates_multiple_consumer.dtbo.o \
13+
kunit_clk_assigned_rates_null.dtbo.o \
14+
kunit_clk_assigned_rates_null_consumer.dtbo.o \
15+
kunit_clk_assigned_rates_one.dtbo.o \
16+
kunit_clk_assigned_rates_one_consumer.dtbo.o \
17+
kunit_clk_assigned_rates_without.dtbo.o \
18+
kunit_clk_assigned_rates_without_consumer.dtbo.o \
19+
kunit_clk_assigned_rates_zero.dtbo.o \
20+
kunit_clk_assigned_rates_zero_consumer.dtbo.o \
721
kunit_clk_parent_data_test.dtbo.o
822
obj-$(CONFIG_COMMON_CLK) += clk-divider.o
923
obj-$(CONFIG_COMMON_CLK) += clk-fixed-factor.o

drivers/clk/clk_test.c

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
#include <linux/clk.h>
66
#include <linux/clk-provider.h>
7+
#include <linux/clk/clk-conf.h>
78
#include <linux/of.h>
89
#include <linux/platform_device.h>
910

@@ -15,6 +16,7 @@
1516
#include <kunit/platform_device.h>
1617
#include <kunit/test.h>
1718

19+
#include "kunit_clk_assigned_rates.h"
1820
#include "clk_parent_data_test.h"
1921

2022
static const struct clk_ops empty_clk_ops = { };
@@ -3108,7 +3110,326 @@ static struct kunit_suite clk_register_clk_parent_data_device_suite = {
31083110
.test_cases = clk_register_clk_parent_data_device_test_cases,
31093111
};
31103112

3113+
struct clk_assigned_rates_context {
3114+
struct clk_dummy_context clk0;
3115+
struct clk_dummy_context clk1;
3116+
};
3117+
3118+
/*
3119+
* struct clk_assigned_rates_test_param - Test parameters for clk_assigned_rates test
3120+
* @desc: Test description
3121+
* @overlay_begin: Pointer to start of DT overlay to apply for test
3122+
* @overlay_end: Pointer to end of DT overlay to apply for test
3123+
* @rate0: Initial rate of first clk
3124+
* @rate1: Initial rate of second clk
3125+
* @consumer_test: true if a consumer is being tested
3126+
*/
3127+
struct clk_assigned_rates_test_param {
3128+
const char *desc;
3129+
u8 *overlay_begin;
3130+
u8 *overlay_end;
3131+
unsigned long rate0;
3132+
unsigned long rate1;
3133+
bool consumer_test;
3134+
};
3135+
3136+
#define TEST_PARAM_OVERLAY(overlay_name) \
3137+
.overlay_begin = of_overlay_begin(overlay_name), \
3138+
.overlay_end = of_overlay_end(overlay_name)
3139+
3140+
static void
3141+
clk_assigned_rates_register_clk(struct kunit *test,
3142+
struct clk_dummy_context *ctx,
3143+
struct device_node *np, const char *name,
3144+
unsigned long rate)
3145+
{
3146+
struct clk_init_data init = { };
3147+
3148+
init.name = name;
3149+
init.ops = &clk_dummy_rate_ops;
3150+
ctx->hw.init = &init;
3151+
ctx->rate = rate;
3152+
3153+
KUNIT_ASSERT_EQ(test, 0, of_clk_hw_register_kunit(test, np, &ctx->hw));
3154+
KUNIT_ASSERT_EQ(test, ctx->rate, rate);
3155+
}
3156+
3157+
/*
3158+
* Does most of the work of the test:
3159+
*
3160+
* 1. Apply the overlay to test
3161+
* 2. Register the clk or clks to test
3162+
* 3. Register the clk provider
3163+
* 4. Apply clk defaults to the consumer device if this is a consumer test
3164+
*
3165+
* The tests will set different test_param values to test different scenarios
3166+
* and validate that in their test functions.
3167+
*/
3168+
static int clk_assigned_rates_test_init(struct kunit *test)
3169+
{
3170+
struct device_node *np, *consumer;
3171+
struct clk_hw_onecell_data *data;
3172+
struct clk_assigned_rates_context *ctx;
3173+
u32 clk_cells;
3174+
const struct clk_assigned_rates_test_param *test_param;
3175+
3176+
test_param = test->param_value;
3177+
3178+
KUNIT_ASSERT_EQ(test, 0, __of_overlay_apply_kunit(test,
3179+
test_param->overlay_begin,
3180+
test_param->overlay_end));
3181+
3182+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3183+
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL));
3184+
test->priv = ctx;
3185+
3186+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3187+
np = of_find_compatible_node(NULL, NULL, "test,clk-assigned-rates"));
3188+
of_node_put_kunit(test, np);
3189+
3190+
KUNIT_ASSERT_EQ(test, 0, of_property_read_u32(np, "#clock-cells", &clk_cells));
3191+
/* Only support #clock-cells = <0> or <1> */
3192+
KUNIT_ASSERT_LT(test, clk_cells, 2);
3193+
3194+
clk_assigned_rates_register_clk(test, &ctx->clk0, np,
3195+
"test_assigned_rate0", test_param->rate0);
3196+
if (clk_cells == 0) {
3197+
KUNIT_ASSERT_EQ(test, 0,
3198+
of_clk_add_hw_provider_kunit(test, np, of_clk_hw_simple_get,
3199+
&ctx->clk0.hw));
3200+
} else if (clk_cells == 1) {
3201+
clk_assigned_rates_register_clk(test, &ctx->clk1, np,
3202+
"test_assigned_rate1", test_param->rate1);
3203+
3204+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3205+
data = kunit_kzalloc(test, struct_size(data, hws, 2), GFP_KERNEL));
3206+
data->num = 2;
3207+
data->hws[0] = &ctx->clk0.hw;
3208+
data->hws[1] = &ctx->clk1.hw;
3209+
3210+
KUNIT_ASSERT_EQ(test, 0,
3211+
of_clk_add_hw_provider_kunit(test, np, of_clk_hw_onecell_get, data));
3212+
}
3213+
3214+
/* Consumers are optional */
3215+
if (test_param->consumer_test) {
3216+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test,
3217+
consumer = of_find_compatible_node(NULL, NULL, "test,clk-consumer"));
3218+
of_node_put_kunit(test, consumer);
3219+
3220+
KUNIT_ASSERT_EQ(test, 0, of_clk_set_defaults(consumer, false));
3221+
}
3222+
3223+
return 0;
3224+
}
3225+
3226+
static void clk_assigned_rates_assigns_one(struct kunit *test)
3227+
{
3228+
struct clk_assigned_rates_context *ctx = test->priv;
3229+
3230+
KUNIT_EXPECT_EQ(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
3231+
}
3232+
3233+
static void clk_assigned_rates_assigns_multiple(struct kunit *test)
3234+
{
3235+
struct clk_assigned_rates_context *ctx = test->priv;
3236+
3237+
KUNIT_EXPECT_EQ(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
3238+
KUNIT_EXPECT_EQ(test, ctx->clk1.rate, ASSIGNED_RATES_1_RATE);
3239+
}
3240+
3241+
static void clk_assigned_rates_skips(struct kunit *test)
3242+
{
3243+
struct clk_assigned_rates_context *ctx = test->priv;
3244+
const struct clk_assigned_rates_test_param *test_param = test->param_value;
3245+
3246+
KUNIT_EXPECT_NE(test, ctx->clk0.rate, ASSIGNED_RATES_0_RATE);
3247+
KUNIT_EXPECT_EQ(test, ctx->clk0.rate, test_param->rate0);
3248+
}
3249+
3250+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_one);
3251+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_one_consumer);
3252+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_one);
3253+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_one_consumer);
3254+
3255+
/* Test cases that assign one rate */
3256+
static const struct clk_assigned_rates_test_param clk_assigned_rates_assigns_one_test_params[] = {
3257+
{
3258+
/*
3259+
* Test that a single cell assigned-clock-rates property
3260+
* assigns the rate when the property is in the provider.
3261+
*/
3262+
.desc = "provider assigns",
3263+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_one),
3264+
},
3265+
{
3266+
/*
3267+
* Test that a single cell assigned-clock-rates property
3268+
* assigns the rate when the property is in the consumer.
3269+
*/
3270+
.desc = "consumer assigns",
3271+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_one_consumer),
3272+
.consumer_test = true,
3273+
},
3274+
{
3275+
/*
3276+
* Test that a single cell assigned-clock-rates-u64 property
3277+
* assigns the rate when the property is in the provider.
3278+
*/
3279+
.desc = "provider assigns u64",
3280+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_one),
3281+
},
3282+
{
3283+
/*
3284+
* Test that a single cell assigned-clock-rates-u64 property
3285+
* assigns the rate when the property is in the consumer.
3286+
*/
3287+
.desc = "consumer assigns u64",
3288+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_one_consumer),
3289+
.consumer_test = true,
3290+
},
3291+
};
3292+
KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_assigns_one,
3293+
clk_assigned_rates_assigns_one_test_params, desc)
3294+
3295+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_multiple);
3296+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_multiple_consumer);
3297+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_multiple);
3298+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_u64_multiple_consumer);
3299+
3300+
/* Test cases that assign multiple rates */
3301+
static const struct clk_assigned_rates_test_param clk_assigned_rates_assigns_multiple_test_params[] = {
3302+
{
3303+
/*
3304+
* Test that a multiple cell assigned-clock-rates property
3305+
* assigns the rates when the property is in the provider.
3306+
*/
3307+
.desc = "provider assigns",
3308+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_multiple),
3309+
},
3310+
{
3311+
/*
3312+
* Test that a multiple cell assigned-clock-rates property
3313+
* assigns the rates when the property is in the consumer.
3314+
*/
3315+
.desc = "consumer assigns",
3316+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_multiple_consumer),
3317+
.consumer_test = true,
3318+
},
3319+
{
3320+
/*
3321+
* Test that a single cell assigned-clock-rates-u64 property
3322+
* assigns the rate when the property is in the provider.
3323+
*/
3324+
.desc = "provider assigns u64",
3325+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_multiple),
3326+
},
3327+
{
3328+
/*
3329+
* Test that a multiple cell assigned-clock-rates-u64 property
3330+
* assigns the rates when the property is in the consumer.
3331+
*/
3332+
.desc = "consumer assigns u64",
3333+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_u64_multiple_consumer),
3334+
.consumer_test = true,
3335+
},
3336+
};
3337+
KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_assigns_multiple,
3338+
clk_assigned_rates_assigns_multiple_test_params,
3339+
desc)
3340+
3341+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_without);
3342+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_without_consumer);
3343+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_zero);
3344+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_zero_consumer);
3345+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_null);
3346+
OF_OVERLAY_DECLARE(kunit_clk_assigned_rates_null_consumer);
3347+
3348+
/* Test cases that skip changing the rate due to malformed DT */
3349+
static const struct clk_assigned_rates_test_param clk_assigned_rates_skips_test_params[] = {
3350+
{
3351+
/*
3352+
* Test that an assigned-clock-rates property without an assigned-clocks
3353+
* property fails when the property is in the provider.
3354+
*/
3355+
.desc = "provider missing assigned-clocks",
3356+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_without),
3357+
.rate0 = 3000,
3358+
},
3359+
{
3360+
/*
3361+
* Test that an assigned-clock-rates property without an assigned-clocks
3362+
* property fails when the property is in the consumer.
3363+
*/
3364+
.desc = "consumer missing assigned-clocks",
3365+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_without_consumer),
3366+
.rate0 = 3000,
3367+
.consumer_test = true,
3368+
},
3369+
{
3370+
/*
3371+
* Test that an assigned-clock-rates property of zero doesn't
3372+
* set a rate when the property is in the provider.
3373+
*/
3374+
.desc = "provider assigned-clock-rates of zero",
3375+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_zero),
3376+
.rate0 = 3000,
3377+
},
3378+
{
3379+
/*
3380+
* Test that an assigned-clock-rates property of zero doesn't
3381+
* set a rate when the property is in the consumer.
3382+
*/
3383+
.desc = "consumer assigned-clock-rates of zero",
3384+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_zero_consumer),
3385+
.rate0 = 3000,
3386+
.consumer_test = true,
3387+
},
3388+
{
3389+
/*
3390+
* Test that an assigned-clocks property with a null phandle
3391+
* doesn't set a rate when the property is in the provider.
3392+
*/
3393+
.desc = "provider assigned-clocks null phandle",
3394+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_null),
3395+
.rate0 = 3000,
3396+
},
3397+
{
3398+
/*
3399+
* Test that an assigned-clocks property with a null phandle
3400+
* doesn't set a rate when the property is in the consumer.
3401+
*/
3402+
.desc = "provider assigned-clocks null phandle",
3403+
TEST_PARAM_OVERLAY(kunit_clk_assigned_rates_null_consumer),
3404+
.rate0 = 3000,
3405+
.consumer_test = true,
3406+
},
3407+
};
3408+
KUNIT_ARRAY_PARAM_DESC(clk_assigned_rates_skips,
3409+
clk_assigned_rates_skips_test_params,
3410+
desc)
3411+
3412+
static struct kunit_case clk_assigned_rates_test_cases[] = {
3413+
KUNIT_CASE_PARAM(clk_assigned_rates_assigns_one,
3414+
clk_assigned_rates_assigns_one_gen_params),
3415+
KUNIT_CASE_PARAM(clk_assigned_rates_assigns_multiple,
3416+
clk_assigned_rates_assigns_multiple_gen_params),
3417+
KUNIT_CASE_PARAM(clk_assigned_rates_skips,
3418+
clk_assigned_rates_skips_gen_params),
3419+
{}
3420+
};
3421+
3422+
/*
3423+
* Test suite for assigned-clock-rates{-u64} DT property.
3424+
*/
3425+
static struct kunit_suite clk_assigned_rates_suite = {
3426+
.name = "clk_assigned_rates",
3427+
.test_cases = clk_assigned_rates_test_cases,
3428+
.init = clk_assigned_rates_test_init,
3429+
};
3430+
31113431
kunit_test_suites(
3432+
&clk_assigned_rates_suite,
31123433
&clk_leaf_mux_set_rate_parent_test_suite,
31133434
&clk_test_suite,
31143435
&clk_multiple_parents_mux_test_suite,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _KUNIT_CLK_ASSIGNED_RATES_H
3+
#define _KUNIT_CLK_ASSIGNED_RATES_H
4+
5+
#define ASSIGNED_RATES_0_RATE 1600000
6+
#define ASSIGNED_RATES_1_RATE 9700000
7+
8+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/dts-v1/;
3+
/plugin/;
4+
5+
#include "kunit_clk_assigned_rates.h"
6+
7+
&{/} {
8+
clk: kunit-clock {
9+
compatible = "test,clk-assigned-rates";
10+
#clock-cells = <1>;
11+
assigned-clocks = <&clk 0>,
12+
<&clk 1>;
13+
assigned-clock-rates = <ASSIGNED_RATES_0_RATE>,
14+
<ASSIGNED_RATES_1_RATE>;
15+
};
16+
};

0 commit comments

Comments
 (0)