1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
// SPDX-License-Identifier: GPL-2.0
/*
* KUnit test for clk fixed rate basic type
*/
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/completion.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <kunit/clk.h>
#include <kunit/of.h>
#include <kunit/platform_device.h>
#include <kunit/resource.h>
#include <kunit/test.h>
#include "clk-fixed-rate_test.h"
/**
* struct clk_hw_fixed_rate_kunit_params - Parameters to pass to __clk_hw_register_fixed_rate()
* @dev: device registering clk
* @np: device_node of device registering clk
* @name: name of clk
* @parent_name: parent name of clk
* @parent_hw: clk_hw pointer to parent of clk
* @parent_data: parent_data describing parent of clk
* @flags: clk framework flags
* @fixed_rate: frequency of clk
* @fixed_accuracy: accuracy of clk
* @clk_fixed_flags: fixed rate specific clk flags
*/
struct clk_hw_fixed_rate_kunit_params {
struct device *dev;
struct device_node *np;
const char *name;
const char *parent_name;
const struct clk_hw *parent_hw;
const struct clk_parent_data *parent_data;
unsigned long flags;
unsigned long fixed_rate;
unsigned long fixed_accuracy;
unsigned long clk_fixed_flags;
};
static int
clk_hw_register_fixed_rate_kunit_init(struct kunit_resource *res, void *context)
{
struct clk_hw_fixed_rate_kunit_params *params = context;
struct clk_hw *hw;
hw = __clk_hw_register_fixed_rate(params->dev, params->np,
params->name,
params->parent_name,
params->parent_hw,
params->parent_data,
params->flags,
params->fixed_rate,
params->fixed_accuracy,
params->clk_fixed_flags,
false);
if (IS_ERR(hw))
return PTR_ERR(hw);
res->data = hw;
return 0;
}
static void clk_hw_register_fixed_rate_kunit_exit(struct kunit_resource *res)
{
struct clk_hw *hw = res->data;
clk_hw_unregister_fixed_rate(hw);
}
/**
* clk_hw_register_fixed_rate_kunit() - Test managed __clk_hw_register_fixed_rate()
* @test: The test context
* @params: Arguments to __clk_hw_register_fixed_rate()
*
* Return: Registered fixed rate clk_hw or ERR_PTR on failure
*/
static struct clk_hw *
clk_hw_register_fixed_rate_kunit(struct kunit *test,
struct clk_hw_fixed_rate_kunit_params *params)
{
struct clk_hw *hw;
hw = kunit_alloc_resource(test,
clk_hw_register_fixed_rate_kunit_init,
clk_hw_register_fixed_rate_kunit_exit,
GFP_KERNEL, params);
if (!hw)
return ERR_PTR(-EINVAL);
return hw;
}
/**
* clk_hw_unregister_fixed_rate_kunit() - Test managed clk_hw_unregister_fixed_rate()
* @test: The test context
* @hw: fixed rate clk to unregister upon test completion
*
* Automatically unregister @hw when @test is complete via
* clk_hw_unregister_fixed_rate().
*
* Return: 0 on success or negative errno on failure
*/
static int clk_hw_unregister_fixed_rate_kunit(struct kunit *test, struct clk_hw *hw)
{
if (!kunit_alloc_resource(test, NULL,
clk_hw_register_fixed_rate_kunit_exit,
GFP_KERNEL, hw))
return -ENOMEM;
return 0;
}
/*
* Test that clk_get_rate() on a fixed rate clk registered with
* clk_hw_register_fixed_rate() gets the proper frequency.
*/
static void clk_fixed_rate_rate_test(struct kunit *test)
{
struct clk_hw *hw;
struct clk *clk;
const unsigned long fixed_rate = 230000;
hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", NULL, 0, fixed_rate);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
KUNIT_EXPECT_EQ(test, fixed_rate, clk_get_rate(clk));
}
/*
* Test that clk_get_accuracy() on a fixed rate clk registered via
* clk_hw_register_fixed_rate_with_accuracy() gets the proper accuracy.
*/
static void clk_fixed_rate_accuracy_test(struct kunit *test)
{
struct clk_hw *hw;
struct clk *clk;
const unsigned long fixed_accuracy = 5000;
hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate",
NULL, 0, 0,
fixed_accuracy);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
clk = clk_hw_get_clk_kunit(test, hw, __func__);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
KUNIT_EXPECT_EQ(test, fixed_accuracy, clk_get_accuracy(clk));
}
/* Test suite for a fixed rate clk without any parent */
static struct kunit_case clk_fixed_rate_test_cases[] = {
KUNIT_CASE(clk_fixed_rate_rate_test),
KUNIT_CASE(clk_fixed_rate_accuracy_test),
{}
};
static struct kunit_suite clk_fixed_rate_suite = {
.name = "clk_fixed_rate",
.test_cases = clk_fixed_rate_test_cases,
};
/*
* Test that clk_get_parent() on a fixed rate clk gets the proper parent.
*/
static void clk_fixed_rate_parent_test(struct kunit *test)
{
struct clk_hw *hw, *parent_hw;
struct clk *expected_parent, *actual_parent;
struct clk *clk;
const char *parent_name = "test-fixed-rate-parent";
struct clk_hw_fixed_rate_kunit_params parent_params = {
.name = parent_name,
};
parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
expected_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0, 0);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
clk = clk_hw_get_clk_kunit(test, hw, __func__);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
actual_parent = clk_get_parent(clk);
KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
}
/*
* Test that clk_get_rate() on a fixed rate clk ignores the parent rate.
*/
static void clk_fixed_rate_parent_rate_test(struct kunit *test)
{
struct clk_hw *hw, *parent_hw;
struct clk *clk;
const unsigned long expected_rate = 1405;
const unsigned long parent_rate = 90402;
const char *parent_name = "test-fixed-rate-parent";
struct clk_hw_fixed_rate_kunit_params parent_params = {
.name = parent_name,
.fixed_rate = parent_rate,
};
parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0,
expected_rate);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
KUNIT_EXPECT_EQ(test, expected_rate, clk_get_rate(clk));
}
/*
* Test that clk_get_accuracy() on a fixed rate clk ignores the parent accuracy.
*/
static void clk_fixed_rate_parent_accuracy_test(struct kunit *test)
{
struct clk_hw *hw, *parent_hw;
struct clk *clk;
const unsigned long expected_accuracy = 900;
const unsigned long parent_accuracy = 24000;
const char *parent_name = "test-fixed-rate-parent";
struct clk_hw_fixed_rate_kunit_params parent_params = {
.name = parent_name,
.fixed_accuracy = parent_accuracy,
};
parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate",
parent_name, 0, 0,
expected_accuracy);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
clk = clk_hw_get_clk_kunit(test, hw, __func__);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
KUNIT_EXPECT_EQ(test, expected_accuracy, clk_get_accuracy(clk));
}
/* Test suite for a fixed rate clk with a parent */
static struct kunit_case clk_fixed_rate_parent_test_cases[] = {
KUNIT_CASE(clk_fixed_rate_parent_test),
KUNIT_CASE(clk_fixed_rate_parent_rate_test),
KUNIT_CASE(clk_fixed_rate_parent_accuracy_test),
{}
};
static struct kunit_suite clk_fixed_rate_parent_suite = {
.name = "clk_fixed_rate_parent",
.test_cases = clk_fixed_rate_parent_test_cases,
};
struct clk_fixed_rate_of_test_context {
struct device *dev;
struct platform_driver pdrv;
struct completion probed;
};
static inline struct clk_fixed_rate_of_test_context *
pdev_to_clk_fixed_rate_of_test_context(struct platform_device *pdev)
{
return container_of(to_platform_driver(pdev->dev.driver),
struct clk_fixed_rate_of_test_context,
pdrv);
}
/*
* Test that of_fixed_clk_setup() registers a fixed rate clk with the proper
* rate.
*/
static void clk_fixed_rate_of_probe_test(struct kunit *test)
{
struct clk_fixed_rate_of_test_context *ctx = test->priv;
struct device *dev = ctx->dev;
struct clk *clk;
clk = clk_get_kunit(test, dev, NULL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
KUNIT_ASSERT_EQ(test, 0, clk_prepare_enable_kunit(test, clk));
KUNIT_EXPECT_EQ(test, TEST_FIXED_FREQUENCY, clk_get_rate(clk));
}
/*
* Test that of_fixed_clk_setup() registers a fixed rate clk with the proper
* accuracy.
*/
static void clk_fixed_rate_of_accuracy_test(struct kunit *test)
{
struct clk_fixed_rate_of_test_context *ctx = test->priv;
struct device *dev = ctx->dev;
struct clk *clk;
clk = clk_get_kunit(test, dev, NULL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
KUNIT_EXPECT_EQ(test, TEST_FIXED_ACCURACY, clk_get_accuracy(clk));
}
static struct kunit_case clk_fixed_rate_of_cases[] = {
KUNIT_CASE(clk_fixed_rate_of_probe_test),
KUNIT_CASE(clk_fixed_rate_of_accuracy_test),
{}
};
static int clk_fixed_rate_of_test_probe(struct platform_device *pdev)
{
struct clk_fixed_rate_of_test_context *ctx;
ctx = pdev_to_clk_fixed_rate_of_test_context(pdev);
ctx->dev = &pdev->dev;
complete(&ctx->probed);
return 0;
}
static int clk_fixed_rate_of_init(struct kunit *test)
{
struct clk_fixed_rate_of_test_context *ctx;
static const struct of_device_id match_table[] = {
{ .compatible = "test,single-clk-consumer" },
{ }
};
KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, kunit_clk_fixed_rate_test));
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
test->priv = ctx;
ctx->pdrv.probe = clk_fixed_rate_of_test_probe;
ctx->pdrv.driver.of_match_table = match_table;
ctx->pdrv.driver.name = __func__;
ctx->pdrv.driver.owner = THIS_MODULE;
init_completion(&ctx->probed);
KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
KUNIT_ASSERT_NE(test, 0, wait_for_completion_timeout(&ctx->probed, HZ));
return 0;
}
static struct kunit_suite clk_fixed_rate_of_suite = {
.name = "clk_fixed_rate_of",
.init = clk_fixed_rate_of_init,
.test_cases = clk_fixed_rate_of_cases,
};
kunit_test_suites(
&clk_fixed_rate_suite,
&clk_fixed_rate_of_suite,
&clk_fixed_rate_parent_suite,
);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("KUnit test for clk fixed rate basic type");
|