123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Kunit test for clk gate basic type
- */
- #include <linux/clk.h>
- #include <linux/clk-provider.h>
- #include <linux/platform_device.h>
- #include <kunit/test.h>
- static void clk_gate_register_test_dev(struct kunit *test)
- {
- struct clk_hw *ret;
- struct platform_device *pdev;
- pdev = platform_device_register_simple("test_gate_device", -1, NULL, 0);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
- ret = clk_hw_register_gate(&pdev->dev, "test_gate", NULL, 0, NULL,
- 0, 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
- KUNIT_EXPECT_STREQ(test, "test_gate", clk_hw_get_name(ret));
- KUNIT_EXPECT_EQ(test, 0UL, clk_hw_get_flags(ret));
- clk_hw_unregister_gate(ret);
- platform_device_put(pdev);
- }
- static void clk_gate_register_test_parent_names(struct kunit *test)
- {
- struct clk_hw *parent;
- struct clk_hw *ret;
- parent = clk_hw_register_fixed_rate(NULL, "test_parent", NULL, 0,
- 1000000);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
- ret = clk_hw_register_gate(NULL, "test_gate", "test_parent", 0, NULL,
- 0, 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
- KUNIT_EXPECT_PTR_EQ(test, parent, clk_hw_get_parent(ret));
- clk_hw_unregister_gate(ret);
- clk_hw_unregister_fixed_rate(parent);
- }
- static void clk_gate_register_test_parent_data(struct kunit *test)
- {
- struct clk_hw *parent;
- struct clk_hw *ret;
- struct clk_parent_data pdata = { };
- parent = clk_hw_register_fixed_rate(NULL, "test_parent", NULL, 0,
- 1000000);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
- pdata.hw = parent;
- ret = clk_hw_register_gate_parent_data(NULL, "test_gate", &pdata, 0,
- NULL, 0, 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
- KUNIT_EXPECT_PTR_EQ(test, parent, clk_hw_get_parent(ret));
- clk_hw_unregister_gate(ret);
- clk_hw_unregister_fixed_rate(parent);
- }
- static void clk_gate_register_test_parent_data_legacy(struct kunit *test)
- {
- struct clk_hw *parent;
- struct clk_hw *ret;
- struct clk_parent_data pdata = { };
- parent = clk_hw_register_fixed_rate(NULL, "test_parent", NULL, 0,
- 1000000);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
- pdata.name = "test_parent";
- ret = clk_hw_register_gate_parent_data(NULL, "test_gate", &pdata, 0,
- NULL, 0, 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
- KUNIT_EXPECT_PTR_EQ(test, parent, clk_hw_get_parent(ret));
- clk_hw_unregister_gate(ret);
- clk_hw_unregister_fixed_rate(parent);
- }
- static void clk_gate_register_test_parent_hw(struct kunit *test)
- {
- struct clk_hw *parent;
- struct clk_hw *ret;
- parent = clk_hw_register_fixed_rate(NULL, "test_parent", NULL, 0,
- 1000000);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
- ret = clk_hw_register_gate_parent_hw(NULL, "test_gate", parent, 0, NULL,
- 0, 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ret);
- KUNIT_EXPECT_PTR_EQ(test, parent, clk_hw_get_parent(ret));
- clk_hw_unregister_gate(ret);
- clk_hw_unregister_fixed_rate(parent);
- }
- static void clk_gate_register_test_hiword_invalid(struct kunit *test)
- {
- struct clk_hw *ret;
- ret = clk_hw_register_gate(NULL, "test_gate", NULL, 0, NULL,
- 20, CLK_GATE_HIWORD_MASK, NULL);
- KUNIT_EXPECT_TRUE(test, IS_ERR(ret));
- }
- static struct kunit_case clk_gate_register_test_cases[] = {
- KUNIT_CASE(clk_gate_register_test_dev),
- KUNIT_CASE(clk_gate_register_test_parent_names),
- KUNIT_CASE(clk_gate_register_test_parent_data),
- KUNIT_CASE(clk_gate_register_test_parent_data_legacy),
- KUNIT_CASE(clk_gate_register_test_parent_hw),
- KUNIT_CASE(clk_gate_register_test_hiword_invalid),
- {}
- };
- static struct kunit_suite clk_gate_register_test_suite = {
- .name = "clk-gate-register-test",
- .test_cases = clk_gate_register_test_cases,
- };
- struct clk_gate_test_context {
- void __iomem *fake_mem;
- struct clk_hw *hw;
- struct clk_hw *parent;
- u32 fake_reg; /* Keep at end, KASAN can detect out of bounds */
- };
- static struct clk_gate_test_context *clk_gate_test_alloc_ctx(struct kunit *test)
- {
- struct clk_gate_test_context *ctx;
- test->priv = ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
- ctx->fake_mem = (void __force __iomem *)&ctx->fake_reg;
- return ctx;
- }
- static void clk_gate_test_parent_rate(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- struct clk_hw *parent = ctx->parent;
- struct clk_hw *hw = ctx->hw;
- unsigned long prate = clk_hw_get_rate(parent);
- unsigned long rate = clk_hw_get_rate(hw);
- KUNIT_EXPECT_EQ(test, prate, rate);
- }
- static void clk_gate_test_enable(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- struct clk_hw *parent = ctx->parent;
- struct clk_hw *hw = ctx->hw;
- struct clk *clk = hw->clk;
- u32 enable_val = BIT(5);
- KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
- KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
- KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(parent));
- }
- static void clk_gate_test_disable(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- struct clk_hw *parent = ctx->parent;
- struct clk_hw *hw = ctx->hw;
- struct clk *clk = hw->clk;
- u32 enable_val = BIT(5);
- u32 disable_val = 0;
- KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
- KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
- clk_disable_unprepare(clk);
- KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
- KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(parent));
- }
- static struct kunit_case clk_gate_test_cases[] = {
- KUNIT_CASE(clk_gate_test_parent_rate),
- KUNIT_CASE(clk_gate_test_enable),
- KUNIT_CASE(clk_gate_test_disable),
- {}
- };
- static int clk_gate_test_init(struct kunit *test)
- {
- struct clk_hw *parent;
- struct clk_hw *hw;
- struct clk_gate_test_context *ctx;
- ctx = clk_gate_test_alloc_ctx(test);
- parent = clk_hw_register_fixed_rate(NULL, "test_parent", NULL, 0,
- 2000000);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
- hw = clk_hw_register_gate_parent_hw(NULL, "test_gate", parent, 0,
- ctx->fake_mem, 5, 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
- ctx->hw = hw;
- ctx->parent = parent;
- return 0;
- }
- static void clk_gate_test_exit(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- clk_hw_unregister_gate(ctx->hw);
- clk_hw_unregister_fixed_rate(ctx->parent);
- }
- static struct kunit_suite clk_gate_test_suite = {
- .name = "clk-gate-test",
- .init = clk_gate_test_init,
- .exit = clk_gate_test_exit,
- .test_cases = clk_gate_test_cases,
- };
- static void clk_gate_test_invert_enable(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- struct clk_hw *parent = ctx->parent;
- struct clk_hw *hw = ctx->hw;
- struct clk *clk = hw->clk;
- u32 enable_val = 0;
- KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
- KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
- KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(parent));
- }
- static void clk_gate_test_invert_disable(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- struct clk_hw *parent = ctx->parent;
- struct clk_hw *hw = ctx->hw;
- struct clk *clk = hw->clk;
- u32 enable_val = 0;
- u32 disable_val = BIT(15);
- KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
- KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
- clk_disable_unprepare(clk);
- KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
- KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(parent));
- }
- static struct kunit_case clk_gate_test_invert_cases[] = {
- KUNIT_CASE(clk_gate_test_invert_enable),
- KUNIT_CASE(clk_gate_test_invert_disable),
- {}
- };
- static int clk_gate_test_invert_init(struct kunit *test)
- {
- struct clk_hw *parent;
- struct clk_hw *hw;
- struct clk_gate_test_context *ctx;
- ctx = clk_gate_test_alloc_ctx(test);
- parent = clk_hw_register_fixed_rate(NULL, "test_parent", NULL, 0,
- 2000000);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
- ctx->fake_reg = BIT(15); /* Default to off */
- hw = clk_hw_register_gate_parent_hw(NULL, "test_gate", parent, 0,
- ctx->fake_mem, 15,
- CLK_GATE_SET_TO_DISABLE, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
- ctx->hw = hw;
- ctx->parent = parent;
- return 0;
- }
- static struct kunit_suite clk_gate_test_invert_suite = {
- .name = "clk-gate-invert-test",
- .init = clk_gate_test_invert_init,
- .exit = clk_gate_test_exit,
- .test_cases = clk_gate_test_invert_cases,
- };
- static void clk_gate_test_hiword_enable(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- struct clk_hw *parent = ctx->parent;
- struct clk_hw *hw = ctx->hw;
- struct clk *clk = hw->clk;
- u32 enable_val = BIT(9) | BIT(9 + 16);
- KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
- KUNIT_EXPECT_EQ(test, enable_val, ctx->fake_reg);
- KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(hw));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(hw));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_enabled(parent));
- KUNIT_EXPECT_TRUE(test, clk_hw_is_prepared(parent));
- }
- static void clk_gate_test_hiword_disable(struct kunit *test)
- {
- struct clk_gate_test_context *ctx = test->priv;
- struct clk_hw *parent = ctx->parent;
- struct clk_hw *hw = ctx->hw;
- struct clk *clk = hw->clk;
- u32 enable_val = BIT(9) | BIT(9 + 16);
- u32 disable_val = BIT(9 + 16);
- KUNIT_ASSERT_EQ(test, clk_prepare_enable(clk), 0);
- KUNIT_ASSERT_EQ(test, enable_val, ctx->fake_reg);
- clk_disable_unprepare(clk);
- KUNIT_EXPECT_EQ(test, disable_val, ctx->fake_reg);
- KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(hw));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(hw));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_enabled(parent));
- KUNIT_EXPECT_FALSE(test, clk_hw_is_prepared(parent));
- }
- static struct kunit_case clk_gate_test_hiword_cases[] = {
- KUNIT_CASE(clk_gate_test_hiword_enable),
- KUNIT_CASE(clk_gate_test_hiword_disable),
- {}
- };
- static int clk_gate_test_hiword_init(struct kunit *test)
- {
- struct clk_hw *parent;
- struct clk_hw *hw;
- struct clk_gate_test_context *ctx;
- ctx = clk_gate_test_alloc_ctx(test);
- parent = clk_hw_register_fixed_rate(NULL, "test_parent", NULL, 0,
- 2000000);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent);
- hw = clk_hw_register_gate_parent_hw(NULL, "test_gate", parent, 0,
- ctx->fake_mem, 9,
- CLK_GATE_HIWORD_MASK, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
- ctx->hw = hw;
- ctx->parent = parent;
- return 0;
- }
- static struct kunit_suite clk_gate_test_hiword_suite = {
- .name = "clk-gate-hiword-test",
- .init = clk_gate_test_hiword_init,
- .exit = clk_gate_test_exit,
- .test_cases = clk_gate_test_hiword_cases,
- };
- static void clk_gate_test_is_enabled(struct kunit *test)
- {
- struct clk_hw *hw;
- struct clk_gate_test_context *ctx;
- ctx = clk_gate_test_alloc_ctx(test);
- ctx->fake_reg = BIT(7);
- hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 7,
- 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
- KUNIT_ASSERT_TRUE(test, clk_hw_is_enabled(hw));
- clk_hw_unregister_gate(hw);
- }
- static void clk_gate_test_is_disabled(struct kunit *test)
- {
- struct clk_hw *hw;
- struct clk_gate_test_context *ctx;
- ctx = clk_gate_test_alloc_ctx(test);
- ctx->fake_reg = BIT(4);
- hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 7,
- 0, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
- KUNIT_ASSERT_FALSE(test, clk_hw_is_enabled(hw));
- clk_hw_unregister_gate(hw);
- }
- static void clk_gate_test_is_enabled_inverted(struct kunit *test)
- {
- struct clk_hw *hw;
- struct clk_gate_test_context *ctx;
- ctx = clk_gate_test_alloc_ctx(test);
- ctx->fake_reg = BIT(31);
- hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 2,
- CLK_GATE_SET_TO_DISABLE, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
- KUNIT_ASSERT_TRUE(test, clk_hw_is_enabled(hw));
- clk_hw_unregister_gate(hw);
- }
- static void clk_gate_test_is_disabled_inverted(struct kunit *test)
- {
- struct clk_hw *hw;
- struct clk_gate_test_context *ctx;
- ctx = clk_gate_test_alloc_ctx(test);
- ctx->fake_reg = BIT(29);
- hw = clk_hw_register_gate(NULL, "test_gate", NULL, 0, ctx->fake_mem, 29,
- CLK_GATE_SET_TO_DISABLE, NULL);
- KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
- KUNIT_ASSERT_FALSE(test, clk_hw_is_enabled(hw));
- clk_hw_unregister_gate(hw);
- }
- static struct kunit_case clk_gate_test_enabled_cases[] = {
- KUNIT_CASE(clk_gate_test_is_enabled),
- KUNIT_CASE(clk_gate_test_is_disabled),
- KUNIT_CASE(clk_gate_test_is_enabled_inverted),
- KUNIT_CASE(clk_gate_test_is_disabled_inverted),
- {}
- };
- static struct kunit_suite clk_gate_test_enabled_suite = {
- .name = "clk-gate-is_enabled-test",
- .test_cases = clk_gate_test_enabled_cases,
- };
- kunit_test_suites(
- &clk_gate_register_test_suite,
- &clk_gate_test_suite,
- &clk_gate_test_invert_suite,
- &clk_gate_test_hiword_suite,
- &clk_gate_test_enabled_suite
- );
- MODULE_LICENSE("GPL v2");
|