1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for clk fixed rate basic type
6 #include <linux/clk-provider.h>
7 #include <linux/completion.h>
9 #include <linux/platform_device.h>
11 #include <kunit/clk.h>
13 #include <kunit/platform_device.h>
14 #include <kunit/resource.h>
15 #include <kunit/test.h>
17 #include "clk-fixed-rate_test.h"
20 * struct clk_hw_fixed_rate_kunit_params - Parameters to pass to __clk_hw_register_fixed_rate()
21 * @dev: device registering clk
22 * @np: device_node of device registering clk
24 * @parent_name: parent name of clk
25 * @parent_hw: clk_hw pointer to parent of clk
26 * @parent_data: parent_data describing parent of clk
27 * @flags: clk framework flags
28 * @fixed_rate: frequency of clk
29 * @fixed_accuracy: accuracy of clk
30 * @clk_fixed_flags: fixed rate specific clk flags
32 struct clk_hw_fixed_rate_kunit_params {
34 struct device_node *np;
36 const char *parent_name;
37 const struct clk_hw *parent_hw;
38 const struct clk_parent_data *parent_data;
40 unsigned long fixed_rate;
41 unsigned long fixed_accuracy;
42 unsigned long clk_fixed_flags;
46 clk_hw_register_fixed_rate_kunit_init(struct kunit_resource *res, void *context)
48 struct clk_hw_fixed_rate_kunit_params *params = context;
51 hw = __clk_hw_register_fixed_rate(params->dev, params->np,
58 params->fixed_accuracy,
59 params->clk_fixed_flags,
69 static void clk_hw_register_fixed_rate_kunit_exit(struct kunit_resource *res)
71 struct clk_hw *hw = res->data;
73 clk_hw_unregister_fixed_rate(hw);
77 * clk_hw_register_fixed_rate_kunit() - Test managed __clk_hw_register_fixed_rate()
78 * @test: The test context
79 * @params: Arguments to __clk_hw_register_fixed_rate()
81 * Return: Registered fixed rate clk_hw or ERR_PTR on failure
83 static struct clk_hw *
84 clk_hw_register_fixed_rate_kunit(struct kunit *test,
85 struct clk_hw_fixed_rate_kunit_params *params)
89 hw = kunit_alloc_resource(test,
90 clk_hw_register_fixed_rate_kunit_init,
91 clk_hw_register_fixed_rate_kunit_exit,
94 return ERR_PTR(-EINVAL);
100 * clk_hw_unregister_fixed_rate_kunit() - Test managed clk_hw_unregister_fixed_rate()
101 * @test: The test context
102 * @hw: fixed rate clk to unregister upon test completion
104 * Automatically unregister @hw when @test is complete via
105 * clk_hw_unregister_fixed_rate().
107 * Return: 0 on success or negative errno on failure
109 static int clk_hw_unregister_fixed_rate_kunit(struct kunit *test, struct clk_hw *hw)
111 if (!kunit_alloc_resource(test, NULL,
112 clk_hw_register_fixed_rate_kunit_exit,
120 * Test that clk_get_rate() on a fixed rate clk registered with
121 * clk_hw_register_fixed_rate() gets the proper frequency.
123 static void clk_fixed_rate_rate_test(struct kunit *test)
127 const unsigned long fixed_rate = 230000;
129 hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", NULL, 0, fixed_rate);
130 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
131 KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
133 clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__);
134 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
136 KUNIT_EXPECT_EQ(test, fixed_rate, clk_get_rate(clk));
140 * Test that clk_get_accuracy() on a fixed rate clk registered via
141 * clk_hw_register_fixed_rate_with_accuracy() gets the proper accuracy.
143 static void clk_fixed_rate_accuracy_test(struct kunit *test)
147 const unsigned long fixed_accuracy = 5000;
149 hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate",
152 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
153 KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
155 clk = clk_hw_get_clk_kunit(test, hw, __func__);
156 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
158 KUNIT_EXPECT_EQ(test, fixed_accuracy, clk_get_accuracy(clk));
161 /* Test suite for a fixed rate clk without any parent */
162 static struct kunit_case clk_fixed_rate_test_cases[] = {
163 KUNIT_CASE(clk_fixed_rate_rate_test),
164 KUNIT_CASE(clk_fixed_rate_accuracy_test),
168 static struct kunit_suite clk_fixed_rate_suite = {
169 .name = "clk_fixed_rate",
170 .test_cases = clk_fixed_rate_test_cases,
174 * Test that clk_get_parent() on a fixed rate clk gets the proper parent.
176 static void clk_fixed_rate_parent_test(struct kunit *test)
178 struct clk_hw *hw, *parent_hw;
179 struct clk *expected_parent, *actual_parent;
181 const char *parent_name = "test-fixed-rate-parent";
182 struct clk_hw_fixed_rate_kunit_params parent_params = {
186 parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
187 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
188 KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
190 expected_parent = clk_hw_get_clk_kunit(test, parent_hw, __func__);
191 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, expected_parent);
193 hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0, 0);
194 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
195 KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
197 clk = clk_hw_get_clk_kunit(test, hw, __func__);
198 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
200 actual_parent = clk_get_parent(clk);
201 KUNIT_EXPECT_TRUE(test, clk_is_match(expected_parent, actual_parent));
205 * Test that clk_get_rate() on a fixed rate clk ignores the parent rate.
207 static void clk_fixed_rate_parent_rate_test(struct kunit *test)
209 struct clk_hw *hw, *parent_hw;
211 const unsigned long expected_rate = 1405;
212 const unsigned long parent_rate = 90402;
213 const char *parent_name = "test-fixed-rate-parent";
214 struct clk_hw_fixed_rate_kunit_params parent_params = {
216 .fixed_rate = parent_rate,
219 parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
220 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
221 KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
223 hw = clk_hw_register_fixed_rate(NULL, "test-fixed-rate", parent_name, 0,
225 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
226 KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
228 clk = clk_hw_get_clk_prepared_enabled_kunit(test, hw, __func__);
229 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
231 KUNIT_EXPECT_EQ(test, expected_rate, clk_get_rate(clk));
235 * Test that clk_get_accuracy() on a fixed rate clk ignores the parent accuracy.
237 static void clk_fixed_rate_parent_accuracy_test(struct kunit *test)
239 struct clk_hw *hw, *parent_hw;
241 const unsigned long expected_accuracy = 900;
242 const unsigned long parent_accuracy = 24000;
243 const char *parent_name = "test-fixed-rate-parent";
244 struct clk_hw_fixed_rate_kunit_params parent_params = {
246 .fixed_accuracy = parent_accuracy,
249 parent_hw = clk_hw_register_fixed_rate_kunit(test, &parent_params);
250 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent_hw);
251 KUNIT_ASSERT_STREQ(test, parent_name, clk_hw_get_name(parent_hw));
253 hw = clk_hw_register_fixed_rate_with_accuracy(NULL, "test-fixed-rate",
256 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, hw);
257 KUNIT_ASSERT_EQ(test, 0, clk_hw_unregister_fixed_rate_kunit(test, hw));
259 clk = clk_hw_get_clk_kunit(test, hw, __func__);
260 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
262 KUNIT_EXPECT_EQ(test, expected_accuracy, clk_get_accuracy(clk));
265 /* Test suite for a fixed rate clk with a parent */
266 static struct kunit_case clk_fixed_rate_parent_test_cases[] = {
267 KUNIT_CASE(clk_fixed_rate_parent_test),
268 KUNIT_CASE(clk_fixed_rate_parent_rate_test),
269 KUNIT_CASE(clk_fixed_rate_parent_accuracy_test),
273 static struct kunit_suite clk_fixed_rate_parent_suite = {
274 .name = "clk_fixed_rate_parent",
275 .test_cases = clk_fixed_rate_parent_test_cases,
278 struct clk_fixed_rate_of_test_context {
280 struct platform_driver pdrv;
281 struct completion probed;
284 static inline struct clk_fixed_rate_of_test_context *
285 pdev_to_clk_fixed_rate_of_test_context(struct platform_device *pdev)
287 return container_of(to_platform_driver(pdev->dev.driver),
288 struct clk_fixed_rate_of_test_context,
293 * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper
296 static void clk_fixed_rate_of_probe_test(struct kunit *test)
298 struct clk_fixed_rate_of_test_context *ctx = test->priv;
299 struct device *dev = ctx->dev;
302 clk = clk_get_kunit(test, dev, NULL);
303 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
305 KUNIT_ASSERT_EQ(test, 0, clk_prepare_enable_kunit(test, clk));
306 KUNIT_EXPECT_EQ(test, TEST_FIXED_FREQUENCY, clk_get_rate(clk));
310 * Test that of_fixed_clk_setup() registers a fixed rate clk with the proper
313 static void clk_fixed_rate_of_accuracy_test(struct kunit *test)
315 struct clk_fixed_rate_of_test_context *ctx = test->priv;
316 struct device *dev = ctx->dev;
319 clk = clk_get_kunit(test, dev, NULL);
320 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, clk);
322 KUNIT_EXPECT_EQ(test, TEST_FIXED_ACCURACY, clk_get_accuracy(clk));
325 static struct kunit_case clk_fixed_rate_of_cases[] = {
326 KUNIT_CASE(clk_fixed_rate_of_probe_test),
327 KUNIT_CASE(clk_fixed_rate_of_accuracy_test),
331 static int clk_fixed_rate_of_test_probe(struct platform_device *pdev)
333 struct clk_fixed_rate_of_test_context *ctx;
335 ctx = pdev_to_clk_fixed_rate_of_test_context(pdev);
336 ctx->dev = &pdev->dev;
337 complete(&ctx->probed);
342 static int clk_fixed_rate_of_init(struct kunit *test)
344 struct clk_fixed_rate_of_test_context *ctx;
345 static const struct of_device_id match_table[] = {
346 { .compatible = "test,single-clk-consumer" },
350 KUNIT_ASSERT_EQ(test, 0, of_overlay_apply_kunit(test, kunit_clk_fixed_rate_test));
352 ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
353 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
356 ctx->pdrv.probe = clk_fixed_rate_of_test_probe;
357 ctx->pdrv.driver.of_match_table = match_table;
358 ctx->pdrv.driver.name = __func__;
359 ctx->pdrv.driver.owner = THIS_MODULE;
360 init_completion(&ctx->probed);
362 KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
363 KUNIT_ASSERT_NE(test, 0, wait_for_completion_timeout(&ctx->probed, HZ));
368 static struct kunit_suite clk_fixed_rate_of_suite = {
369 .name = "clk_fixed_rate_of",
370 .init = clk_fixed_rate_of_init,
371 .test_cases = clk_fixed_rate_of_cases,
375 &clk_fixed_rate_suite,
376 &clk_fixed_rate_of_suite,
377 &clk_fixed_rate_parent_suite,
379 MODULE_LICENSE("GPL");
380 MODULE_DESCRIPTION("KUnit test for clk fixed rate basic type");