Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: Introduce Kunit Tests for the framework

Let's test various parts of the rate-related clock API with the kunit
testing framework.

Cc: kunit-dev@googlegroups.com
Tested-by: Daniel Latypov <dlatypov@google.com>
Suggested-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
Link: https://lore.kernel.org/r/20220225143534.405820-3-maxime@cerno.tech
Signed-off-by: Stephen Boyd <sboyd@kernel.org>

authored by

Maxime Ripard and committed by
Stephen Boyd
723d0530 0c1b56df

+796
+1
drivers/clk/.kunitconfig
··· 1 1 CONFIG_KUNIT=y 2 2 CONFIG_COMMON_CLK=y 3 + CONFIG_CLK_KUNIT_TEST=y 3 4 CONFIG_CLK_GATE_KUNIT_TEST=y
+7
drivers/clk/Kconfig
··· 429 429 source "drivers/clk/zynqmp/Kconfig" 430 430 431 431 # Kunit test cases 432 + config CLK_KUNIT_TEST 433 + tristate "Basic Clock Framework Kunit Tests" if !KUNIT_ALL_TESTS 434 + depends on KUNIT 435 + default KUNIT_ALL_TESTS 436 + help 437 + Kunit tests for the common clock framework. 438 + 432 439 config CLK_GATE_KUNIT_TEST 433 440 tristate "Basic gate type Kunit test" if !KUNIT_ALL_TESTS 434 441 depends on KUNIT
+1
drivers/clk/Makefile
··· 2 2 # common clock types 3 3 obj-$(CONFIG_HAVE_CLK) += clk-devres.o clk-bulk.o clkdev.o 4 4 obj-$(CONFIG_COMMON_CLK) += clk.o 5 + obj-$(CONFIG_CLK_KUNIT_TEST) += clk_test.o 5 6 obj-$(CONFIG_COMMON_CLK) += clk-divider.o 6 7 obj-$(CONFIG_COMMON_CLK) += clk-fixed-factor.o 7 8 obj-$(CONFIG_COMMON_CLK) += clk-fixed-rate.o
+787
drivers/clk/clk_test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Kunit test for clk rate management 4 + */ 5 + #include <linux/clk.h> 6 + #include <linux/clk-provider.h> 7 + 8 + /* Needed for clk_hw_get_clk() */ 9 + #include "clk.h" 10 + 11 + #include <kunit/test.h> 12 + 13 + #define DUMMY_CLOCK_INIT_RATE (42 * 1000 * 1000) 14 + #define DUMMY_CLOCK_RATE_1 (142 * 1000 * 1000) 15 + #define DUMMY_CLOCK_RATE_2 (242 * 1000 * 1000) 16 + 17 + struct clk_dummy_context { 18 + struct clk_hw hw; 19 + unsigned long rate; 20 + }; 21 + 22 + static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw, 23 + unsigned long parent_rate) 24 + { 25 + struct clk_dummy_context *ctx = 26 + container_of(hw, struct clk_dummy_context, hw); 27 + 28 + return ctx->rate; 29 + } 30 + 31 + static int clk_dummy_determine_rate(struct clk_hw *hw, 32 + struct clk_rate_request *req) 33 + { 34 + /* Just return the same rate without modifying it */ 35 + return 0; 36 + } 37 + 38 + static int clk_dummy_maximize_rate(struct clk_hw *hw, 39 + struct clk_rate_request *req) 40 + { 41 + /* 42 + * If there's a maximum set, always run the clock at the maximum 43 + * allowed. 44 + */ 45 + if (req->max_rate < ULONG_MAX) 46 + req->rate = req->max_rate; 47 + 48 + return 0; 49 + } 50 + 51 + static int clk_dummy_minimize_rate(struct clk_hw *hw, 52 + struct clk_rate_request *req) 53 + { 54 + /* 55 + * If there's a minimum set, always run the clock at the minimum 56 + * allowed. 57 + */ 58 + if (req->min_rate > 0) 59 + req->rate = req->min_rate; 60 + 61 + return 0; 62 + } 63 + 64 + static int clk_dummy_set_rate(struct clk_hw *hw, 65 + unsigned long rate, 66 + unsigned long parent_rate) 67 + { 68 + struct clk_dummy_context *ctx = 69 + container_of(hw, struct clk_dummy_context, hw); 70 + 71 + ctx->rate = rate; 72 + return 0; 73 + } 74 + 75 + static const struct clk_ops clk_dummy_rate_ops = { 76 + .recalc_rate = clk_dummy_recalc_rate, 77 + .determine_rate = clk_dummy_determine_rate, 78 + .set_rate = clk_dummy_set_rate, 79 + }; 80 + 81 + static const struct clk_ops clk_dummy_maximize_rate_ops = { 82 + .recalc_rate = clk_dummy_recalc_rate, 83 + .determine_rate = clk_dummy_maximize_rate, 84 + .set_rate = clk_dummy_set_rate, 85 + }; 86 + 87 + static const struct clk_ops clk_dummy_minimize_rate_ops = { 88 + .recalc_rate = clk_dummy_recalc_rate, 89 + .determine_rate = clk_dummy_minimize_rate, 90 + .set_rate = clk_dummy_set_rate, 91 + }; 92 + 93 + static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops) 94 + { 95 + struct clk_dummy_context *ctx; 96 + struct clk_init_data init = { }; 97 + int ret; 98 + 99 + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); 100 + if (!ctx) 101 + return -ENOMEM; 102 + ctx->rate = DUMMY_CLOCK_INIT_RATE; 103 + test->priv = ctx; 104 + 105 + init.name = "test_dummy_rate"; 106 + init.ops = ops; 107 + ctx->hw.init = &init; 108 + 109 + ret = clk_hw_register(NULL, &ctx->hw); 110 + if (ret) 111 + return ret; 112 + 113 + return 0; 114 + } 115 + 116 + static int clk_test_init(struct kunit *test) 117 + { 118 + return clk_test_init_with_ops(test, &clk_dummy_rate_ops); 119 + } 120 + 121 + static int clk_maximize_test_init(struct kunit *test) 122 + { 123 + return clk_test_init_with_ops(test, &clk_dummy_maximize_rate_ops); 124 + } 125 + 126 + static int clk_minimize_test_init(struct kunit *test) 127 + { 128 + return clk_test_init_with_ops(test, &clk_dummy_minimize_rate_ops); 129 + } 130 + 131 + static void clk_test_exit(struct kunit *test) 132 + { 133 + struct clk_dummy_context *ctx = test->priv; 134 + 135 + clk_hw_unregister(&ctx->hw); 136 + } 137 + 138 + /* 139 + * Test that the actual rate matches what is returned by clk_get_rate() 140 + */ 141 + static void clk_test_get_rate(struct kunit *test) 142 + { 143 + struct clk_dummy_context *ctx = test->priv; 144 + struct clk_hw *hw = &ctx->hw; 145 + struct clk *clk = hw->clk; 146 + unsigned long rate; 147 + 148 + rate = clk_get_rate(clk); 149 + KUNIT_ASSERT_GT(test, rate, 0); 150 + KUNIT_EXPECT_EQ(test, rate, ctx->rate); 151 + } 152 + 153 + /* 154 + * Test that, after a call to clk_set_rate(), the rate returned by 155 + * clk_get_rate() matches. 156 + * 157 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't 158 + * modify the requested rate, which is our case in clk_dummy_rate_ops. 159 + */ 160 + static void clk_test_set_get_rate(struct kunit *test) 161 + { 162 + struct clk_dummy_context *ctx = test->priv; 163 + struct clk_hw *hw = &ctx->hw; 164 + struct clk *clk = hw->clk; 165 + unsigned long rate; 166 + 167 + KUNIT_ASSERT_EQ(test, 168 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1), 169 + 0); 170 + 171 + rate = clk_get_rate(clk); 172 + KUNIT_ASSERT_GT(test, rate, 0); 173 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1); 174 + } 175 + 176 + /* 177 + * Test that, after several calls to clk_set_rate(), the rate returned 178 + * by clk_get_rate() matches the last one. 179 + * 180 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't 181 + * modify the requested rate, which is our case in clk_dummy_rate_ops. 182 + */ 183 + static void clk_test_set_set_get_rate(struct kunit *test) 184 + { 185 + struct clk_dummy_context *ctx = test->priv; 186 + struct clk_hw *hw = &ctx->hw; 187 + struct clk *clk = hw->clk; 188 + unsigned long rate; 189 + 190 + KUNIT_ASSERT_EQ(test, 191 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1), 192 + 0); 193 + 194 + KUNIT_ASSERT_EQ(test, 195 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2), 196 + 0); 197 + 198 + rate = clk_get_rate(clk); 199 + KUNIT_ASSERT_GT(test, rate, 0); 200 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2); 201 + } 202 + 203 + /* 204 + * Test that clk_round_rate and clk_set_rate are consitent and will 205 + * return the same frequency. 206 + */ 207 + static void clk_test_round_set_get_rate(struct kunit *test) 208 + { 209 + struct clk_dummy_context *ctx = test->priv; 210 + struct clk_hw *hw = &ctx->hw; 211 + struct clk *clk = hw->clk; 212 + unsigned long rounded_rate, set_rate; 213 + 214 + rounded_rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1); 215 + KUNIT_ASSERT_GT(test, rounded_rate, 0); 216 + KUNIT_EXPECT_EQ(test, rounded_rate, DUMMY_CLOCK_RATE_1); 217 + 218 + KUNIT_ASSERT_EQ(test, 219 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1), 220 + 0); 221 + 222 + set_rate = clk_get_rate(clk); 223 + KUNIT_ASSERT_GT(test, set_rate, 0); 224 + KUNIT_EXPECT_EQ(test, rounded_rate, set_rate); 225 + } 226 + 227 + static struct kunit_case clk_test_cases[] = { 228 + KUNIT_CASE(clk_test_get_rate), 229 + KUNIT_CASE(clk_test_set_get_rate), 230 + KUNIT_CASE(clk_test_set_set_get_rate), 231 + KUNIT_CASE(clk_test_round_set_get_rate), 232 + {} 233 + }; 234 + 235 + static struct kunit_suite clk_test_suite = { 236 + .name = "clk-test", 237 + .init = clk_test_init, 238 + .exit = clk_test_exit, 239 + .test_cases = clk_test_cases, 240 + }; 241 + 242 + /* 243 + * Test that clk_set_rate_range won't return an error for a valid range 244 + * and that it will make sure the rate of the clock is within the 245 + * boundaries. 246 + */ 247 + static void clk_range_test_set_range(struct kunit *test) 248 + { 249 + struct clk_dummy_context *ctx = test->priv; 250 + struct clk_hw *hw = &ctx->hw; 251 + struct clk *clk = hw->clk; 252 + unsigned long rate; 253 + 254 + KUNIT_ASSERT_EQ(test, 255 + clk_set_rate_range(clk, 256 + DUMMY_CLOCK_RATE_1, 257 + DUMMY_CLOCK_RATE_2), 258 + 0); 259 + 260 + rate = clk_get_rate(clk); 261 + KUNIT_ASSERT_GT(test, rate, 0); 262 + KUNIT_EXPECT_GE(test, rate, DUMMY_CLOCK_RATE_1); 263 + KUNIT_EXPECT_LE(test, rate, DUMMY_CLOCK_RATE_2); 264 + } 265 + 266 + /* 267 + * Test that calling clk_set_rate_range with a minimum rate higher than 268 + * the maximum rate returns an error. 269 + */ 270 + static void clk_range_test_set_range_invalid(struct kunit *test) 271 + { 272 + struct clk_dummy_context *ctx = test->priv; 273 + struct clk_hw *hw = &ctx->hw; 274 + struct clk *clk = hw->clk; 275 + 276 + KUNIT_EXPECT_LT(test, 277 + clk_set_rate_range(clk, 278 + DUMMY_CLOCK_RATE_1 + 1000, 279 + DUMMY_CLOCK_RATE_1), 280 + 0); 281 + } 282 + 283 + /* 284 + * Test that users can't set multiple, disjoints, range that would be 285 + * impossible to meet. 286 + */ 287 + static void clk_range_test_multiple_disjoints_range(struct kunit *test) 288 + { 289 + struct clk_dummy_context *ctx = test->priv; 290 + struct clk_hw *hw = &ctx->hw; 291 + struct clk *user1, *user2; 292 + 293 + user1 = clk_hw_get_clk(hw, NULL); 294 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1); 295 + 296 + user2 = clk_hw_get_clk(hw, NULL); 297 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2); 298 + 299 + KUNIT_ASSERT_EQ(test, 300 + clk_set_rate_range(user1, 1000, 2000), 301 + 0); 302 + 303 + KUNIT_EXPECT_LT(test, 304 + clk_set_rate_range(user2, 3000, 4000), 305 + 0); 306 + 307 + clk_put(user2); 308 + clk_put(user1); 309 + } 310 + 311 + /* 312 + * Test that if our clock has some boundaries and we try to round a rate 313 + * lower than the minimum, the returned rate won't be affected by the 314 + * boundaries. 315 + */ 316 + static void clk_range_test_set_range_round_rate_lower(struct kunit *test) 317 + { 318 + struct clk_dummy_context *ctx = test->priv; 319 + struct clk_hw *hw = &ctx->hw; 320 + struct clk *clk = hw->clk; 321 + long rate; 322 + 323 + KUNIT_ASSERT_EQ(test, 324 + clk_set_rate_range(clk, 325 + DUMMY_CLOCK_RATE_1, 326 + DUMMY_CLOCK_RATE_2), 327 + 0); 328 + 329 + rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000); 330 + KUNIT_ASSERT_GT(test, rate, 0); 331 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 - 1000); 332 + } 333 + 334 + /* 335 + * Test that if our clock has some boundaries and we try to set a rate 336 + * lower than the minimum, we'll get an error. 337 + */ 338 + static void clk_range_test_set_range_set_rate_lower(struct kunit *test) 339 + { 340 + struct clk_dummy_context *ctx = test->priv; 341 + struct clk_hw *hw = &ctx->hw; 342 + struct clk *clk = hw->clk; 343 + 344 + KUNIT_ASSERT_EQ(test, 345 + clk_set_rate_range(clk, 346 + DUMMY_CLOCK_RATE_1, 347 + DUMMY_CLOCK_RATE_2), 348 + 0); 349 + 350 + KUNIT_ASSERT_LT(test, 351 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000), 352 + 0); 353 + } 354 + 355 + /* 356 + * Test that if our clock has some boundaries and we try to round and 357 + * set a rate lower than the minimum, the values won't be consistent 358 + * between clk_round_rate() and clk_set_rate(). 359 + */ 360 + static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test) 361 + { 362 + struct clk_dummy_context *ctx = test->priv; 363 + struct clk_hw *hw = &ctx->hw; 364 + struct clk *clk = hw->clk; 365 + long rounded; 366 + 367 + KUNIT_ASSERT_EQ(test, 368 + clk_set_rate_range(clk, 369 + DUMMY_CLOCK_RATE_1, 370 + DUMMY_CLOCK_RATE_2), 371 + 0); 372 + 373 + rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_1 - 1000); 374 + KUNIT_ASSERT_GT(test, rounded, 0); 375 + 376 + KUNIT_EXPECT_LT(test, 377 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000), 378 + 0); 379 + 380 + KUNIT_EXPECT_NE(test, rounded, clk_get_rate(clk)); 381 + } 382 + 383 + /* 384 + * Test that if our clock has some boundaries and we try to round a rate 385 + * higher than the maximum, the returned rate won't be affected by the 386 + * boundaries. 387 + */ 388 + static void clk_range_test_set_range_round_rate_higher(struct kunit *test) 389 + { 390 + struct clk_dummy_context *ctx = test->priv; 391 + struct clk_hw *hw = &ctx->hw; 392 + struct clk *clk = hw->clk; 393 + long rate; 394 + 395 + KUNIT_ASSERT_EQ(test, 396 + clk_set_rate_range(clk, 397 + DUMMY_CLOCK_RATE_1, 398 + DUMMY_CLOCK_RATE_2), 399 + 0); 400 + 401 + rate = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000); 402 + KUNIT_ASSERT_GT(test, rate, 0); 403 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 + 1000); 404 + } 405 + 406 + /* 407 + * Test that if our clock has some boundaries and we try to set a rate 408 + * lower than the maximum, we'll get an error. 409 + */ 410 + static void clk_range_test_set_range_set_rate_higher(struct kunit *test) 411 + { 412 + struct clk_dummy_context *ctx = test->priv; 413 + struct clk_hw *hw = &ctx->hw; 414 + struct clk *clk = hw->clk; 415 + 416 + KUNIT_ASSERT_EQ(test, 417 + clk_set_rate_range(clk, 418 + DUMMY_CLOCK_RATE_1, 419 + DUMMY_CLOCK_RATE_2), 420 + 0); 421 + 422 + KUNIT_ASSERT_LT(test, 423 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000), 424 + 0); 425 + } 426 + 427 + /* 428 + * Test that if our clock has some boundaries and we try to round and 429 + * set a rate higher than the maximum, the values won't be consistent 430 + * between clk_round_rate() and clk_set_rate(). 431 + */ 432 + static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test) 433 + { 434 + struct clk_dummy_context *ctx = test->priv; 435 + struct clk_hw *hw = &ctx->hw; 436 + struct clk *clk = hw->clk; 437 + long rounded; 438 + 439 + KUNIT_ASSERT_EQ(test, 440 + clk_set_rate_range(clk, 441 + DUMMY_CLOCK_RATE_1, 442 + DUMMY_CLOCK_RATE_2), 443 + 0); 444 + 445 + rounded = clk_round_rate(clk, DUMMY_CLOCK_RATE_2 + 1000); 446 + KUNIT_ASSERT_GT(test, rounded, 0); 447 + 448 + KUNIT_EXPECT_LT(test, 449 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000), 450 + 0); 451 + 452 + KUNIT_EXPECT_NE(test, rounded, clk_get_rate(clk)); 453 + } 454 + 455 + /* 456 + * Test that if our clock has a rate lower than the minimum set by a 457 + * call to clk_set_rate_range(), the rate will be raised to match the 458 + * new minimum. 459 + * 460 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't 461 + * modify the requested rate, which is our case in clk_dummy_rate_ops. 462 + */ 463 + static void clk_range_test_set_range_get_rate_raised(struct kunit *test) 464 + { 465 + struct clk_dummy_context *ctx = test->priv; 466 + struct clk_hw *hw = &ctx->hw; 467 + struct clk *clk = hw->clk; 468 + unsigned long rate; 469 + 470 + KUNIT_ASSERT_EQ(test, 471 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000), 472 + 0); 473 + 474 + KUNIT_ASSERT_EQ(test, 475 + clk_set_rate_range(clk, 476 + DUMMY_CLOCK_RATE_1, 477 + DUMMY_CLOCK_RATE_2), 478 + 0); 479 + 480 + rate = clk_get_rate(clk); 481 + KUNIT_ASSERT_GT(test, rate, 0); 482 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1); 483 + } 484 + 485 + /* 486 + * Test that if our clock has a rate higher than the maximum set by a 487 + * call to clk_set_rate_range(), the rate will be lowered to match the 488 + * new maximum. 489 + * 490 + * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't 491 + * modify the requested rate, which is our case in clk_dummy_rate_ops. 492 + */ 493 + static void clk_range_test_set_range_get_rate_lowered(struct kunit *test) 494 + { 495 + struct clk_dummy_context *ctx = test->priv; 496 + struct clk_hw *hw = &ctx->hw; 497 + struct clk *clk = hw->clk; 498 + unsigned long rate; 499 + 500 + KUNIT_ASSERT_EQ(test, 501 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000), 502 + 0); 503 + 504 + KUNIT_ASSERT_EQ(test, 505 + clk_set_rate_range(clk, 506 + DUMMY_CLOCK_RATE_1, 507 + DUMMY_CLOCK_RATE_2), 508 + 0); 509 + 510 + rate = clk_get_rate(clk); 511 + KUNIT_ASSERT_GT(test, rate, 0); 512 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2); 513 + } 514 + 515 + static struct kunit_case clk_range_test_cases[] = { 516 + KUNIT_CASE(clk_range_test_set_range), 517 + KUNIT_CASE(clk_range_test_set_range_invalid), 518 + KUNIT_CASE(clk_range_test_multiple_disjoints_range), 519 + KUNIT_CASE(clk_range_test_set_range_round_rate_lower), 520 + KUNIT_CASE(clk_range_test_set_range_set_rate_lower), 521 + KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_lower), 522 + KUNIT_CASE(clk_range_test_set_range_round_rate_higher), 523 + KUNIT_CASE(clk_range_test_set_range_set_rate_higher), 524 + KUNIT_CASE(clk_range_test_set_range_set_round_rate_consistent_higher), 525 + KUNIT_CASE(clk_range_test_set_range_get_rate_raised), 526 + KUNIT_CASE(clk_range_test_set_range_get_rate_lowered), 527 + {} 528 + }; 529 + 530 + static struct kunit_suite clk_range_test_suite = { 531 + .name = "clk-range-test", 532 + .init = clk_test_init, 533 + .exit = clk_test_exit, 534 + .test_cases = clk_range_test_cases, 535 + }; 536 + 537 + /* 538 + * Test that if: 539 + * - we have several subsequent calls to clk_set_rate_range(); 540 + * - and we have a round_rate ops that always return the maximum 541 + * frequency allowed; 542 + * 543 + * The clock will run at the minimum of all maximum boundaries 544 + * requested, even if those boundaries aren't there anymore. 545 + */ 546 + static void clk_range_test_set_range_rate_maximized(struct kunit *test) 547 + { 548 + struct clk_dummy_context *ctx = test->priv; 549 + struct clk_hw *hw = &ctx->hw; 550 + struct clk *clk = hw->clk; 551 + unsigned long rate; 552 + 553 + KUNIT_ASSERT_EQ(test, 554 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000), 555 + 0); 556 + 557 + KUNIT_ASSERT_EQ(test, 558 + clk_set_rate_range(clk, 559 + DUMMY_CLOCK_RATE_1, 560 + DUMMY_CLOCK_RATE_2), 561 + 0); 562 + 563 + rate = clk_get_rate(clk); 564 + KUNIT_ASSERT_GT(test, rate, 0); 565 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2); 566 + 567 + KUNIT_ASSERT_EQ(test, 568 + clk_set_rate_range(clk, 569 + DUMMY_CLOCK_RATE_1, 570 + DUMMY_CLOCK_RATE_2 - 1000), 571 + 0); 572 + 573 + rate = clk_get_rate(clk); 574 + KUNIT_ASSERT_GT(test, rate, 0); 575 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000); 576 + 577 + KUNIT_ASSERT_EQ(test, 578 + clk_set_rate_range(clk, 579 + DUMMY_CLOCK_RATE_1, 580 + DUMMY_CLOCK_RATE_2), 581 + 0); 582 + 583 + rate = clk_get_rate(clk); 584 + KUNIT_ASSERT_GT(test, rate, 0); 585 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2 - 1000); 586 + } 587 + 588 + /* 589 + * Test that if: 590 + * - we have several subsequent calls to clk_set_rate_range(), across 591 + * multiple users; 592 + * - and we have a round_rate ops that always return the maximum 593 + * frequency allowed; 594 + * 595 + * The clock will run at the minimum of all maximum boundaries 596 + * requested, even if those boundaries aren't there anymore. 597 + */ 598 + static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test) 599 + { 600 + struct clk_dummy_context *ctx = test->priv; 601 + struct clk_hw *hw = &ctx->hw; 602 + struct clk *clk = hw->clk; 603 + struct clk *user1, *user2; 604 + unsigned long rate; 605 + 606 + user1 = clk_hw_get_clk(hw, NULL); 607 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1); 608 + 609 + user2 = clk_hw_get_clk(hw, NULL); 610 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2); 611 + 612 + KUNIT_ASSERT_EQ(test, 613 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000), 614 + 0); 615 + 616 + KUNIT_ASSERT_EQ(test, 617 + clk_set_rate_range(user1, 618 + 0, 619 + DUMMY_CLOCK_RATE_2), 620 + 0); 621 + 622 + rate = clk_get_rate(clk); 623 + KUNIT_ASSERT_GT(test, rate, 0); 624 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2); 625 + 626 + KUNIT_ASSERT_EQ(test, 627 + clk_set_rate_range(user2, 628 + 0, 629 + DUMMY_CLOCK_RATE_1), 630 + 0); 631 + 632 + rate = clk_get_rate(clk); 633 + KUNIT_ASSERT_GT(test, rate, 0); 634 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1); 635 + 636 + KUNIT_ASSERT_EQ(test, 637 + clk_set_rate_range(user2, 0, ULONG_MAX), 638 + 0); 639 + 640 + rate = clk_get_rate(clk); 641 + KUNIT_ASSERT_GT(test, rate, 0); 642 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1); 643 + 644 + clk_put(user2); 645 + clk_put(user1); 646 + } 647 + 648 + static struct kunit_case clk_range_maximize_test_cases[] = { 649 + KUNIT_CASE(clk_range_test_set_range_rate_maximized), 650 + KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized), 651 + {} 652 + }; 653 + 654 + static struct kunit_suite clk_range_maximize_test_suite = { 655 + .name = "clk-range-maximize-test", 656 + .init = clk_maximize_test_init, 657 + .exit = clk_test_exit, 658 + .test_cases = clk_range_maximize_test_cases, 659 + }; 660 + 661 + /* 662 + * Test that if: 663 + * - we have several subsequent calls to clk_set_rate_range() 664 + * - and we have a round_rate ops that always return the minimum 665 + * frequency allowed; 666 + * 667 + * The clock will run at the maximum of all minimum boundaries 668 + * requested, even if those boundaries aren't there anymore. 669 + */ 670 + static void clk_range_test_set_range_rate_minimized(struct kunit *test) 671 + { 672 + struct clk_dummy_context *ctx = test->priv; 673 + struct clk_hw *hw = &ctx->hw; 674 + struct clk *clk = hw->clk; 675 + unsigned long rate; 676 + 677 + KUNIT_ASSERT_EQ(test, 678 + clk_set_rate(clk, DUMMY_CLOCK_RATE_1 - 1000), 679 + 0); 680 + 681 + KUNIT_ASSERT_EQ(test, 682 + clk_set_rate_range(clk, 683 + DUMMY_CLOCK_RATE_1, 684 + DUMMY_CLOCK_RATE_2), 685 + 0); 686 + 687 + rate = clk_get_rate(clk); 688 + KUNIT_ASSERT_GT(test, rate, 0); 689 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1); 690 + 691 + KUNIT_ASSERT_EQ(test, 692 + clk_set_rate_range(clk, 693 + DUMMY_CLOCK_RATE_1 + 1000, 694 + DUMMY_CLOCK_RATE_2), 695 + 0); 696 + 697 + rate = clk_get_rate(clk); 698 + KUNIT_ASSERT_GT(test, rate, 0); 699 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000); 700 + 701 + KUNIT_ASSERT_EQ(test, 702 + clk_set_rate_range(clk, 703 + DUMMY_CLOCK_RATE_1, 704 + DUMMY_CLOCK_RATE_2), 705 + 0); 706 + 707 + rate = clk_get_rate(clk); 708 + KUNIT_ASSERT_GT(test, rate, 0); 709 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1 + 1000); 710 + } 711 + 712 + /* 713 + * Test that if: 714 + * - we have several subsequent calls to clk_set_rate_range(), across 715 + * multiple users; 716 + * - and we have a round_rate ops that always return the minimum 717 + * frequency allowed; 718 + * 719 + * The clock will run at the maximum of all minimum boundaries 720 + * requested, even if those boundaries aren't there anymore. 721 + */ 722 + static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test) 723 + { 724 + struct clk_dummy_context *ctx = test->priv; 725 + struct clk_hw *hw = &ctx->hw; 726 + struct clk *clk = hw->clk; 727 + struct clk *user1, *user2; 728 + unsigned long rate; 729 + 730 + user1 = clk_hw_get_clk(hw, NULL); 731 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1); 732 + 733 + user2 = clk_hw_get_clk(hw, NULL); 734 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2); 735 + 736 + KUNIT_ASSERT_EQ(test, 737 + clk_set_rate_range(user1, 738 + DUMMY_CLOCK_RATE_1, 739 + ULONG_MAX), 740 + 0); 741 + 742 + rate = clk_get_rate(clk); 743 + KUNIT_ASSERT_GT(test, rate, 0); 744 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1); 745 + 746 + KUNIT_ASSERT_EQ(test, 747 + clk_set_rate_range(user2, 748 + DUMMY_CLOCK_RATE_2, 749 + ULONG_MAX), 750 + 0); 751 + 752 + rate = clk_get_rate(clk); 753 + KUNIT_ASSERT_GT(test, rate, 0); 754 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2); 755 + 756 + KUNIT_ASSERT_EQ(test, 757 + clk_set_rate_range(user2, 0, ULONG_MAX), 758 + 0); 759 + 760 + rate = clk_get_rate(clk); 761 + KUNIT_ASSERT_GT(test, rate, 0); 762 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2); 763 + 764 + clk_put(user2); 765 + clk_put(user1); 766 + } 767 + 768 + static struct kunit_case clk_range_minimize_test_cases[] = { 769 + KUNIT_CASE(clk_range_test_set_range_rate_minimized), 770 + KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized), 771 + {} 772 + }; 773 + 774 + static struct kunit_suite clk_range_minimize_test_suite = { 775 + .name = "clk-range-minimize-test", 776 + .init = clk_minimize_test_init, 777 + .exit = clk_test_exit, 778 + .test_cases = clk_range_minimize_test_cases, 779 + }; 780 + 781 + kunit_test_suites( 782 + &clk_test_suite, 783 + &clk_range_test_suite, 784 + &clk_range_maximize_test_suite, 785 + &clk_range_minimize_test_suite 786 + ); 787 + MODULE_LICENSE("GPL v2");