Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Kunit test for clk rate management
0004  */
0005 #include <linux/clk.h>
0006 #include <linux/clk-provider.h>
0007 
0008 /* Needed for clk_hw_get_clk() */
0009 #include "clk.h"
0010 
0011 #include <kunit/test.h>
0012 
0013 #define DUMMY_CLOCK_INIT_RATE   (42 * 1000 * 1000)
0014 #define DUMMY_CLOCK_RATE_1  (142 * 1000 * 1000)
0015 #define DUMMY_CLOCK_RATE_2  (242 * 1000 * 1000)
0016 
0017 struct clk_dummy_context {
0018     struct clk_hw hw;
0019     unsigned long rate;
0020 };
0021 
0022 static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw,
0023                        unsigned long parent_rate)
0024 {
0025     struct clk_dummy_context *ctx =
0026         container_of(hw, struct clk_dummy_context, hw);
0027 
0028     return ctx->rate;
0029 }
0030 
0031 static int clk_dummy_determine_rate(struct clk_hw *hw,
0032                     struct clk_rate_request *req)
0033 {
0034     /* Just return the same rate without modifying it */
0035     return 0;
0036 }
0037 
0038 static int clk_dummy_maximize_rate(struct clk_hw *hw,
0039                    struct clk_rate_request *req)
0040 {
0041     /*
0042      * If there's a maximum set, always run the clock at the maximum
0043      * allowed.
0044      */
0045     if (req->max_rate < ULONG_MAX)
0046         req->rate = req->max_rate;
0047 
0048     return 0;
0049 }
0050 
0051 static int clk_dummy_minimize_rate(struct clk_hw *hw,
0052                    struct clk_rate_request *req)
0053 {
0054     /*
0055      * If there's a minimum set, always run the clock at the minimum
0056      * allowed.
0057      */
0058     if (req->min_rate > 0)
0059         req->rate = req->min_rate;
0060 
0061     return 0;
0062 }
0063 
0064 static int clk_dummy_set_rate(struct clk_hw *hw,
0065                   unsigned long rate,
0066                   unsigned long parent_rate)
0067 {
0068     struct clk_dummy_context *ctx =
0069         container_of(hw, struct clk_dummy_context, hw);
0070 
0071     ctx->rate = rate;
0072     return 0;
0073 }
0074 
0075 static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
0076 {
0077     if (index >= clk_hw_get_num_parents(hw))
0078         return -EINVAL;
0079 
0080     return 0;
0081 }
0082 
0083 static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
0084 {
0085     return 0;
0086 }
0087 
0088 static const struct clk_ops clk_dummy_rate_ops = {
0089     .recalc_rate = clk_dummy_recalc_rate,
0090     .determine_rate = clk_dummy_determine_rate,
0091     .set_rate = clk_dummy_set_rate,
0092 };
0093 
0094 static const struct clk_ops clk_dummy_maximize_rate_ops = {
0095     .recalc_rate = clk_dummy_recalc_rate,
0096     .determine_rate = clk_dummy_maximize_rate,
0097     .set_rate = clk_dummy_set_rate,
0098 };
0099 
0100 static const struct clk_ops clk_dummy_minimize_rate_ops = {
0101     .recalc_rate = clk_dummy_recalc_rate,
0102     .determine_rate = clk_dummy_minimize_rate,
0103     .set_rate = clk_dummy_set_rate,
0104 };
0105 
0106 static const struct clk_ops clk_dummy_single_parent_ops = {
0107     .set_parent = clk_dummy_single_set_parent,
0108     .get_parent = clk_dummy_single_get_parent,
0109 };
0110 
0111 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
0112 {
0113     struct clk_dummy_context *ctx;
0114     struct clk_init_data init = { };
0115     int ret;
0116 
0117     ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
0118     if (!ctx)
0119         return -ENOMEM;
0120     ctx->rate = DUMMY_CLOCK_INIT_RATE;
0121     test->priv = ctx;
0122 
0123     init.name = "test_dummy_rate";
0124     init.ops = ops;
0125     ctx->hw.init = &init;
0126 
0127     ret = clk_hw_register(NULL, &ctx->hw);
0128     if (ret)
0129         return ret;
0130 
0131     return 0;
0132 }
0133 
0134 static int clk_test_init(struct kunit *test)
0135 {
0136     return clk_test_init_with_ops(test, &clk_dummy_rate_ops);
0137 }
0138 
0139 static int clk_maximize_test_init(struct kunit *test)
0140 {
0141     return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops);
0142 }
0143 
0144 static int clk_minimize_test_init(struct kunit *test)
0145 {
0146     return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops);
0147 }
0148 
0149 static void clk_test_exit(struct kunit *test)
0150 {
0151     struct clk_dummy_context *ctx = test->priv;
0152 
0153     clk_hw_unregister(&ctx->hw);
0154 }
0155 
0156 /*
0157  * Test that the actual rate matches what is returned by clk_get_rate()
0158  */
0159 static void clk_test_get_rate(struct kunit *test)
0160 {
0161     struct clk_dummy_context *ctx = test->priv;
0162     struct clk_hw *hw = &ctx->hw;
0163     struct clk *clk = hw->clk;
0164     unsigned long rate;
0165 
0166     rate = clk_get_rate(clk);
0167     KUNIT_ASSERT_GT(test, rate, 0);
0168     KUNIT_EXPECT_EQ(test, rate, ctx->rate);
0169 }
0170 
0171 /*
0172  * Test that, after a call to clk_set_rate(), the rate returned by
0173  * clk_get_rate() matches.
0174  *
0175  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
0176  * modify the requested rate, which is our case in clk_dummy_rate_ops.
0177  */
0178 static void clk_test_set_get_rate(struct kunit *test)
0179 {
0180     struct clk_dummy_context *ctx = test->priv;
0181     struct clk_hw *hw = &ctx->hw;
0182     struct clk *clk = hw->clk;
0183     unsigned long rate;
0184 
0185     KUNIT_ASSERT_EQ(test,
0186             clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
0187             0);
0188 
0189     rate = clk_get_rate(clk);
0190     KUNIT_ASSERT_GT(test, rate, 0);
0191     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
0192 }
0193 
0194 /*
0195  * Test that, after several calls to clk_set_rate(), the rate returned
0196  * by clk_get_rate() matches the last one.
0197  *
0198  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
0199  * modify the requested rate, which is our case in clk_dummy_rate_ops.
0200  */
0201 static void clk_test_set_set_get_rate(struct kunit *test)
0202 {
0203     struct clk_dummy_context *ctx = test->priv;
0204     struct clk_hw *hw = &ctx->hw;
0205     struct clk *clk = hw->clk;
0206     unsigned long rate;
0207 
0208     KUNIT_ASSERT_EQ(test,
0209             clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
0210             0);
0211 
0212     KUNIT_ASSERT_EQ(test,
0213             clk_set_rate(clk, DUMMY_CLOCK_RATE_2),
0214             0);
0215 
0216     rate = clk_get_rate(clk);
0217     KUNIT_ASSERT_GT(test, rate, 0);
0218     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
0219 }
0220 
0221 /*
0222  * Test that clk_round_rate and clk_set_rate are consitent and will
0223  * return the same frequency.
0224  */
0225 static void clk_test_round_set_get_rate(struct kunit *test)
0226 {
0227     struct clk_dummy_context *ctx = test->priv;
0228     struct clk_hw *hw = &ctx->hw;
0229     struct clk *clk = hw->clk;
0230     unsigned long rounded_rate, set_rate;
0231 
0232     rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1);
0233     KUNIT_ASSERT_GT(test, rounded_rate, 0);
0234     KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1);
0235 
0236     KUNIT_ASSERT_EQ(test,
0237             clk_set_rate(clk, DUMMY_CLOCK_RATE_1),
0238             0);
0239 
0240     set_rate = clk_get_rate(clk);
0241     KUNIT_ASSERT_GT(test, set_rate, 0);
0242     KUNIT_EXPECT_EQ(test, rounded_rate, set_rate);
0243 }
0244 
0245 static struct kunit_case clk_test_cases[] = {
0246     KUNIT_CASE(clk_test_get_rate),
0247     KUNIT_CASE(clk_test_set_get_rate),
0248     KUNIT_CASE(clk_test_set_set_get_rate),
0249     KUNIT_CASE(clk_test_round_set_get_rate),
0250     {}
0251 };
0252 
0253 static struct kunit_suite clk_test_suite = {
0254     .name = "clk-test",
0255     .init = clk_test_init,
0256     .exit = clk_test_exit,
0257     .test_cases = clk_test_cases,
0258 };
0259 
0260 struct clk_single_parent_ctx {
0261     struct clk_dummy_context parent_ctx;
0262     struct clk_hw hw;
0263 };
0264 
0265 static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
0266 {
0267     struct clk_single_parent_ctx *ctx;
0268     struct clk_init_data init = { };
0269     const char * const parents[] = { "orphan_parent" };
0270     int ret;
0271 
0272     ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
0273     if (!ctx)
0274         return -ENOMEM;
0275     test->priv = ctx;
0276 
0277     init.name = "test_orphan_dummy_parent";
0278     init.ops = &clk_dummy_single_parent_ops;
0279     init.parent_names = parents;
0280     init.num_parents = ARRAY_SIZE(parents);
0281     init.flags = CLK_SET_RATE_PARENT;
0282     ctx->hw.init = &init;
0283 
0284     ret = clk_hw_register(NULL, &ctx->hw);
0285     if (ret)
0286         return ret;
0287 
0288     memset(&init, 0, sizeof(init));
0289     init.name = "orphan_parent";
0290     init.ops = &clk_dummy_rate_ops;
0291     ctx->parent_ctx.hw.init = &init;
0292     ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
0293 
0294     ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
0295     if (ret)
0296         return ret;
0297 
0298     return 0;
0299 }
0300 
0301 static void clk_orphan_transparent_single_parent_mux_test_exit(struct kunit *test)
0302 {
0303     struct clk_single_parent_ctx *ctx = test->priv;
0304 
0305     clk_hw_unregister(&ctx->hw);
0306     clk_hw_unregister(&ctx->parent_ctx.hw);
0307 }
0308 
0309 /*
0310  * Test that a mux-only clock, with an initial rate within a range,
0311  * will still have the same rate after the range has been enforced.
0312  */
0313 static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
0314 {
0315     struct clk_single_parent_ctx *ctx = test->priv;
0316     struct clk_hw *hw = &ctx->hw;
0317     struct clk *clk = hw->clk;
0318     unsigned long rate, new_rate;
0319 
0320     rate = clk_get_rate(clk);
0321     KUNIT_ASSERT_GT(test, rate, 0);
0322 
0323     KUNIT_ASSERT_EQ(test,
0324             clk_set_rate_range(clk,
0325                        ctx->parent_ctx.rate - 1000,
0326                        ctx->parent_ctx.rate + 1000),
0327             0);
0328 
0329     new_rate = clk_get_rate(clk);
0330     KUNIT_ASSERT_GT(test, new_rate, 0);
0331     KUNIT_EXPECT_EQ(test, rate, new_rate);
0332 }
0333 
0334 static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
0335     KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
0336     {}
0337 };
0338 
0339 static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
0340     .name = "clk-orphan-transparent-single-parent-test",
0341     .init = clk_orphan_transparent_single_parent_mux_test_init,
0342     .exit = clk_orphan_transparent_single_parent_mux_test_exit,
0343     .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
0344 };
0345 
0346 /*
0347  * Test that clk_set_rate_range won't return an error for a valid range
0348  * and that it will make sure the rate of the clock is within the
0349  * boundaries.
0350  */
0351 static void clk_range_test_set_range(struct kunit *test)
0352 {
0353     struct clk_dummy_context *ctx = test->priv;
0354     struct clk_hw *hw = &ctx->hw;
0355     struct clk *clk = hw->clk;
0356     unsigned long rate;
0357 
0358     KUNIT_ASSERT_EQ(test,
0359             clk_set_rate_range(clk,
0360                        DUMMY_CLOCK_RATE_1,
0361                        DUMMY_CLOCK_RATE_2),
0362             0);
0363 
0364     rate = clk_get_rate(clk);
0365     KUNIT_ASSERT_GT(test, rate, 0);
0366     KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
0367     KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
0368 }
0369 
0370 /*
0371  * Test that calling clk_set_rate_range with a minimum rate higher than
0372  * the maximum rate returns an error.
0373  */
0374 static void clk_range_test_set_range_invalid(struct kunit *test)
0375 {
0376     struct clk_dummy_context *ctx = test->priv;
0377     struct clk_hw *hw = &ctx->hw;
0378     struct clk *clk = hw->clk;
0379 
0380     KUNIT_EXPECT_LT(test,
0381             clk_set_rate_range(clk,
0382                        DUMMY_CLOCK_RATE_1 + 1000,
0383                        DUMMY_CLOCK_RATE_1),
0384             0);
0385 }
0386 
0387 /*
0388  * Test that users can't set multiple, disjoints, range that would be
0389  * impossible to meet.
0390  */
0391 static void clk_range_test_multiple_disjoints_range(struct kunit *test)
0392 {
0393     struct clk_dummy_context *ctx = test->priv;
0394     struct clk_hw *hw = &ctx->hw;
0395     struct clk *user1, *user2;
0396 
0397     user1 = clk_hw_get_clk(hw, NULL);
0398     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
0399 
0400     user2 = clk_hw_get_clk(hw, NULL);
0401     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
0402 
0403     KUNIT_ASSERT_EQ(test,
0404             clk_set_rate_range(user1, 1000, 2000),
0405             0);
0406 
0407     KUNIT_EXPECT_LT(test,
0408             clk_set_rate_range(user2, 3000, 4000),
0409             0);
0410 
0411     clk_put(user2);
0412     clk_put(user1);
0413 }
0414 
0415 /*
0416  * Test that if our clock has some boundaries and we try to round a rate
0417  * lower than the minimum, the returned rate will be within range.
0418  */
0419 static void clk_range_test_set_range_round_rate_lower(struct kunit *test)
0420 {
0421     struct clk_dummy_context *ctx = test->priv;
0422     struct clk_hw *hw = &ctx->hw;
0423     struct clk *clk = hw->clk;
0424     long rate;
0425 
0426     KUNIT_ASSERT_EQ(test,
0427             clk_set_rate_range(clk,
0428                        DUMMY_CLOCK_RATE_1,
0429                        DUMMY_CLOCK_RATE_2),
0430             0);
0431 
0432     rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
0433     KUNIT_ASSERT_GT(test, rate, 0);
0434     KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
0435     KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
0436 }
0437 
0438 /*
0439  * Test that if our clock has some boundaries and we try to set a rate
0440  * higher than the maximum, the new rate will be within range.
0441  */
0442 static void clk_range_test_set_range_set_rate_lower(struct kunit *test)
0443 {
0444     struct clk_dummy_context *ctx = test->priv;
0445     struct clk_hw *hw = &ctx->hw;
0446     struct clk *clk = hw->clk;
0447     unsigned long rate;
0448 
0449     KUNIT_ASSERT_EQ(test,
0450             clk_set_rate_range(clk,
0451                        DUMMY_CLOCK_RATE_1,
0452                        DUMMY_CLOCK_RATE_2),
0453             0);
0454 
0455     KUNIT_ASSERT_EQ(test,
0456             clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
0457             0);
0458 
0459     rate = clk_get_rate(clk);
0460     KUNIT_ASSERT_GT(test, rate, 0);
0461     KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
0462     KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
0463 }
0464 
0465 /*
0466  * Test that if our clock has some boundaries and we try to round and
0467  * set a rate lower than the minimum, the rate returned by
0468  * clk_round_rate() will be consistent with the new rate set by
0469  * clk_set_rate().
0470  */
0471 static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test)
0472 {
0473     struct clk_dummy_context *ctx = test->priv;
0474     struct clk_hw *hw = &ctx->hw;
0475     struct clk *clk = hw->clk;
0476     long rounded;
0477 
0478     KUNIT_ASSERT_EQ(test,
0479             clk_set_rate_range(clk,
0480                        DUMMY_CLOCK_RATE_1,
0481                        DUMMY_CLOCK_RATE_2),
0482             0);
0483 
0484     rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000);
0485     KUNIT_ASSERT_GT(test, rounded, 0);
0486 
0487     KUNIT_ASSERT_EQ(test,
0488             clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
0489             0);
0490 
0491     KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
0492 }
0493 
0494 /*
0495  * Test that if our clock has some boundaries and we try to round a rate
0496  * higher than the maximum, the returned rate will be within range.
0497  */
0498 static void clk_range_test_set_range_round_rate_higher(struct kunit *test)
0499 {
0500     struct clk_dummy_context *ctx = test->priv;
0501     struct clk_hw *hw = &ctx->hw;
0502     struct clk *clk = hw->clk;
0503     long rate;
0504 
0505     KUNIT_ASSERT_EQ(test,
0506             clk_set_rate_range(clk,
0507                        DUMMY_CLOCK_RATE_1,
0508                        DUMMY_CLOCK_RATE_2),
0509             0);
0510 
0511     rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
0512     KUNIT_ASSERT_GT(test, rate, 0);
0513     KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
0514     KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
0515 }
0516 
0517 /*
0518  * Test that if our clock has some boundaries and we try to set a rate
0519  * higher than the maximum, the new rate will be within range.
0520  */
0521 static void clk_range_test_set_range_set_rate_higher(struct kunit *test)
0522 {
0523     struct clk_dummy_context *ctx = test->priv;
0524     struct clk_hw *hw = &ctx->hw;
0525     struct clk *clk = hw->clk;
0526     unsigned long rate;
0527 
0528     KUNIT_ASSERT_EQ(test,
0529             clk_set_rate_range(clk,
0530                        DUMMY_CLOCK_RATE_1,
0531                        DUMMY_CLOCK_RATE_2),
0532             0);
0533 
0534     KUNIT_ASSERT_EQ(test,
0535             clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
0536             0);
0537 
0538     rate = clk_get_rate(clk);
0539     KUNIT_ASSERT_GT(test, rate, 0);
0540     KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1);
0541     KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2);
0542 }
0543 
0544 /*
0545  * Test that if our clock has some boundaries and we try to round and
0546  * set a rate higher than the maximum, the rate returned by
0547  * clk_round_rate() will be consistent with the new rate set by
0548  * clk_set_rate().
0549  */
0550 static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test)
0551 {
0552     struct clk_dummy_context *ctx = test->priv;
0553     struct clk_hw *hw = &ctx->hw;
0554     struct clk *clk = hw->clk;
0555     long rounded;
0556 
0557     KUNIT_ASSERT_EQ(test,
0558             clk_set_rate_range(clk,
0559                        DUMMY_CLOCK_RATE_1,
0560                        DUMMY_CLOCK_RATE_2),
0561             0);
0562 
0563     rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000);
0564     KUNIT_ASSERT_GT(test, rounded, 0);
0565 
0566     KUNIT_ASSERT_EQ(test,
0567             clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
0568             0);
0569 
0570     KUNIT_EXPECT_EQ(test, rounded, clk_get_rate(clk));
0571 }
0572 
0573 /*
0574  * Test that if our clock has a rate lower than the minimum set by a
0575  * call to clk_set_rate_range(), the rate will be raised to match the
0576  * new minimum.
0577  *
0578  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
0579  * modify the requested rate, which is our case in clk_dummy_rate_ops.
0580  */
0581 static void clk_range_test_set_range_get_rate_raised(struct kunit *test)
0582 {
0583     struct clk_dummy_context *ctx = test->priv;
0584     struct clk_hw *hw = &ctx->hw;
0585     struct clk *clk = hw->clk;
0586     unsigned long rate;
0587 
0588     KUNIT_ASSERT_EQ(test,
0589             clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
0590             0);
0591 
0592     KUNIT_ASSERT_EQ(test,
0593             clk_set_rate_range(clk,
0594                        DUMMY_CLOCK_RATE_1,
0595                        DUMMY_CLOCK_RATE_2),
0596             0);
0597 
0598     rate = clk_get_rate(clk);
0599     KUNIT_ASSERT_GT(test, rate, 0);
0600     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
0601 }
0602 
0603 /*
0604  * Test that if our clock has a rate higher than the maximum set by a
0605  * call to clk_set_rate_range(), the rate will be lowered to match the
0606  * new maximum.
0607  *
0608  * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't
0609  * modify the requested rate, which is our case in clk_dummy_rate_ops.
0610  */
0611 static void clk_range_test_set_range_get_rate_lowered(struct kunit *test)
0612 {
0613     struct clk_dummy_context *ctx = test->priv;
0614     struct clk_hw *hw = &ctx->hw;
0615     struct clk *clk = hw->clk;
0616     unsigned long rate;
0617 
0618     KUNIT_ASSERT_EQ(test,
0619             clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
0620             0);
0621 
0622     KUNIT_ASSERT_EQ(test,
0623             clk_set_rate_range(clk,
0624                        DUMMY_CLOCK_RATE_1,
0625                        DUMMY_CLOCK_RATE_2),
0626             0);
0627 
0628     rate = clk_get_rate(clk);
0629     KUNIT_ASSERT_GT(test, rate, 0);
0630     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
0631 }
0632 
0633 static struct kunit_case clk_range_test_cases[] = {
0634     KUNIT_CASE(clk_range_test_set_range),
0635     KUNIT_CASE(clk_range_test_set_range_invalid),
0636     KUNIT_CASE(clk_range_test_multiple_disjoints_range),
0637     KUNIT_CASE(clk_range_test_set_range_round_rate_lower),
0638     KUNIT_CASE(clk_range_test_set_range_set_rate_lower),
0639     KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower),
0640     KUNIT_CASE(clk_range_test_set_range_round_rate_higher),
0641     KUNIT_CASE(clk_range_test_set_range_set_rate_higher),
0642     KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher),
0643     KUNIT_CASE(clk_range_test_set_range_get_rate_raised),
0644     KUNIT_CASE(clk_range_test_set_range_get_rate_lowered),
0645     {}
0646 };
0647 
0648 static struct kunit_suite clk_range_test_suite = {
0649     .name = "clk-range-test",
0650     .init = clk_test_init,
0651     .exit = clk_test_exit,
0652     .test_cases = clk_range_test_cases,
0653 };
0654 
0655 /*
0656  * Test that if we have several subsequent calls to
0657  * clk_set_rate_range(), the core will reevaluate whether a new rate is
0658  * needed each and every time.
0659  *
0660  * With clk_dummy_maximize_rate_ops, this means that the rate will
0661  * trail along the maximum as it evolves.
0662  */
0663 static void clk_range_test_set_range_rate_maximized(struct kunit *test)
0664 {
0665     struct clk_dummy_context *ctx = test->priv;
0666     struct clk_hw *hw = &ctx->hw;
0667     struct clk *clk = hw->clk;
0668     unsigned long rate;
0669 
0670     KUNIT_ASSERT_EQ(test,
0671             clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
0672             0);
0673 
0674     KUNIT_ASSERT_EQ(test,
0675             clk_set_rate_range(clk,
0676                        DUMMY_CLOCK_RATE_1,
0677                        DUMMY_CLOCK_RATE_2),
0678             0);
0679 
0680     rate = clk_get_rate(clk);
0681     KUNIT_ASSERT_GT(test, rate, 0);
0682     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
0683 
0684     KUNIT_ASSERT_EQ(test,
0685             clk_set_rate_range(clk,
0686                        DUMMY_CLOCK_RATE_1,
0687                        DUMMY_CLOCK_RATE_2 - 1000),
0688             0);
0689 
0690     rate = clk_get_rate(clk);
0691     KUNIT_ASSERT_GT(test, rate, 0);
0692     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000);
0693 
0694     KUNIT_ASSERT_EQ(test,
0695             clk_set_rate_range(clk,
0696                        DUMMY_CLOCK_RATE_1,
0697                        DUMMY_CLOCK_RATE_2),
0698             0);
0699 
0700     rate = clk_get_rate(clk);
0701     KUNIT_ASSERT_GT(test, rate, 0);
0702     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
0703 }
0704 
0705 /*
0706  * Test that if we have several subsequent calls to
0707  * clk_set_rate_range(), across multiple users, the core will reevaluate
0708  * whether a new rate is needed each and every time.
0709  *
0710  * With clk_dummy_maximize_rate_ops, this means that the rate will
0711  * trail along the maximum as it evolves.
0712  */
0713 static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)
0714 {
0715     struct clk_dummy_context *ctx = test->priv;
0716     struct clk_hw *hw = &ctx->hw;
0717     struct clk *clk = hw->clk;
0718     struct clk *user1, *user2;
0719     unsigned long rate;
0720 
0721     user1 = clk_hw_get_clk(hw, NULL);
0722     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
0723 
0724     user2 = clk_hw_get_clk(hw, NULL);
0725     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
0726 
0727     KUNIT_ASSERT_EQ(test,
0728             clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
0729             0);
0730 
0731     KUNIT_ASSERT_EQ(test,
0732             clk_set_rate_range(user1,
0733                        0,
0734                        DUMMY_CLOCK_RATE_2),
0735             0);
0736 
0737     rate = clk_get_rate(clk);
0738     KUNIT_ASSERT_GT(test, rate, 0);
0739     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
0740 
0741     KUNIT_ASSERT_EQ(test,
0742             clk_set_rate_range(user2,
0743                        0,
0744                        DUMMY_CLOCK_RATE_1),
0745             0);
0746 
0747     rate = clk_get_rate(clk);
0748     KUNIT_ASSERT_GT(test, rate, 0);
0749     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
0750 
0751     KUNIT_ASSERT_EQ(test,
0752             clk_drop_range(user2),
0753             0);
0754 
0755     rate = clk_get_rate(clk);
0756     KUNIT_ASSERT_GT(test, rate, 0);
0757     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
0758 
0759     clk_put(user2);
0760     clk_put(user1);
0761 }
0762 
0763 static struct kunit_case clk_range_maximize_test_cases[] = {
0764     KUNIT_CASE(clk_range_test_set_range_rate_maximized),
0765     KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
0766     {}
0767 };
0768 
0769 static struct kunit_suite clk_range_maximize_test_suite = {
0770     .name = "clk-range-maximize-test",
0771     .init = clk_maximize_test_init,
0772     .exit = clk_test_exit,
0773     .test_cases = clk_range_maximize_test_cases,
0774 };
0775 
0776 /*
0777  * Test that if we have several subsequent calls to
0778  * clk_set_rate_range(), the core will reevaluate whether a new rate is
0779  * needed each and every time.
0780  *
0781  * With clk_dummy_minimize_rate_ops, this means that the rate will
0782  * trail along the minimum as it evolves.
0783  */
0784 static void clk_range_test_set_range_rate_minimized(struct kunit *test)
0785 {
0786     struct clk_dummy_context *ctx = test->priv;
0787     struct clk_hw *hw = &ctx->hw;
0788     struct clk *clk = hw->clk;
0789     unsigned long rate;
0790 
0791     KUNIT_ASSERT_EQ(test,
0792             clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000),
0793             0);
0794 
0795     KUNIT_ASSERT_EQ(test,
0796             clk_set_rate_range(clk,
0797                        DUMMY_CLOCK_RATE_1,
0798                        DUMMY_CLOCK_RATE_2),
0799             0);
0800 
0801     rate = clk_get_rate(clk);
0802     KUNIT_ASSERT_GT(test, rate, 0);
0803     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
0804 
0805     KUNIT_ASSERT_EQ(test,
0806             clk_set_rate_range(clk,
0807                        DUMMY_CLOCK_RATE_1 + 1000,
0808                        DUMMY_CLOCK_RATE_2),
0809             0);
0810 
0811     rate = clk_get_rate(clk);
0812     KUNIT_ASSERT_GT(test, rate, 0);
0813     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000);
0814 
0815     KUNIT_ASSERT_EQ(test,
0816             clk_set_rate_range(clk,
0817                        DUMMY_CLOCK_RATE_1,
0818                        DUMMY_CLOCK_RATE_2),
0819             0);
0820 
0821     rate = clk_get_rate(clk);
0822     KUNIT_ASSERT_GT(test, rate, 0);
0823     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
0824 }
0825 
0826 /*
0827  * Test that if we have several subsequent calls to
0828  * clk_set_rate_range(), across multiple users, the core will reevaluate
0829  * whether a new rate is needed each and every time.
0830  *
0831  * With clk_dummy_minimize_rate_ops, this means that the rate will
0832  * trail along the minimum as it evolves.
0833  */
0834 static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)
0835 {
0836     struct clk_dummy_context *ctx = test->priv;
0837     struct clk_hw *hw = &ctx->hw;
0838     struct clk *clk = hw->clk;
0839     struct clk *user1, *user2;
0840     unsigned long rate;
0841 
0842     user1 = clk_hw_get_clk(hw, NULL);
0843     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
0844 
0845     user2 = clk_hw_get_clk(hw, NULL);
0846     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
0847 
0848     KUNIT_ASSERT_EQ(test,
0849             clk_set_rate_range(user1,
0850                        DUMMY_CLOCK_RATE_1,
0851                        ULONG_MAX),
0852             0);
0853 
0854     rate = clk_get_rate(clk);
0855     KUNIT_ASSERT_GT(test, rate, 0);
0856     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
0857 
0858     KUNIT_ASSERT_EQ(test,
0859             clk_set_rate_range(user2,
0860                        DUMMY_CLOCK_RATE_2,
0861                        ULONG_MAX),
0862             0);
0863 
0864     rate = clk_get_rate(clk);
0865     KUNIT_ASSERT_GT(test, rate, 0);
0866     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
0867 
0868     KUNIT_ASSERT_EQ(test,
0869             clk_drop_range(user2),
0870             0);
0871 
0872     rate = clk_get_rate(clk);
0873     KUNIT_ASSERT_GT(test, rate, 0);
0874     KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
0875 
0876     clk_put(user2);
0877     clk_put(user1);
0878 }
0879 
0880 static struct kunit_case clk_range_minimize_test_cases[] = {
0881     KUNIT_CASE(clk_range_test_set_range_rate_minimized),
0882     KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
0883     {}
0884 };
0885 
0886 static struct kunit_suite clk_range_minimize_test_suite = {
0887     .name = "clk-range-minimize-test",
0888     .init = clk_minimize_test_init,
0889     .exit = clk_test_exit,
0890     .test_cases = clk_range_minimize_test_cases,
0891 };
0892 
0893 kunit_test_suites(
0894     &clk_test_suite,
0895     &clk_orphan_transparent_single_parent_test_suite,
0896     &clk_range_test_suite,
0897     &clk_range_maximize_test_suite,
0898     &clk_range_minimize_test_suite
0899 );
0900 MODULE_LICENSE("GPL v2");