183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
26a1c7cefSSimon Glass /*
36a1c7cefSSimon Glass * Copyright (C) 2015 Google, Inc
46a1c7cefSSimon Glass */
56a1c7cefSSimon Glass
66a1c7cefSSimon Glass #include <common.h>
76a1c7cefSSimon Glass #include <dm.h>
8135aa950SStephen Warren #include <asm/clk.h>
96a1c7cefSSimon Glass #include <dm/test.h>
106a1c7cefSSimon Glass #include <linux/err.h>
116a1c7cefSSimon Glass #include <test/ut.h>
126a1c7cefSSimon Glass
dm_test_clk(struct unit_test_state * uts)13135aa950SStephen Warren static int dm_test_clk(struct unit_test_state *uts)
146a1c7cefSSimon Glass {
15*b630d57dSAnup Patel struct udevice *dev_fixed, *dev_fixed_factor, *dev_clk, *dev_test;
166a1c7cefSSimon Glass ulong rate;
176a1c7cefSSimon Glass
18135aa950SStephen Warren ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed",
19135aa950SStephen Warren &dev_fixed));
206a1c7cefSSimon Glass
21*b630d57dSAnup Patel ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed-factor",
22*b630d57dSAnup Patel &dev_fixed_factor));
23*b630d57dSAnup Patel
24135aa950SStephen Warren ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
25135aa950SStephen Warren &dev_clk));
26135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
27135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
28135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
29135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
306a1c7cefSSimon Glass
31135aa950SStephen Warren ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
32135aa950SStephen Warren &dev_test));
33135aa950SStephen Warren ut_assertok(sandbox_clk_test_get(dev_test));
341fe243a1SFabrice Gasnier ut_assertok(sandbox_clk_test_valid(dev_test));
356a1c7cefSSimon Glass
36135aa950SStephen Warren ut_asserteq(1234,
37135aa950SStephen Warren sandbox_clk_test_get_rate(dev_test,
38135aa950SStephen Warren SANDBOX_CLK_TEST_ID_FIXED));
39135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
40135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI));
41135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
42135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C));
436a1c7cefSSimon Glass
44135aa950SStephen Warren rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED,
45135aa950SStephen Warren 12345);
46135aa950SStephen Warren ut_assert(IS_ERR_VALUE(rate));
47135aa950SStephen Warren rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED);
486a1c7cefSSimon Glass ut_asserteq(1234, rate);
496a1c7cefSSimon Glass
50135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
51135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI,
52135aa950SStephen Warren 1000));
53135aa950SStephen Warren ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
54135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C,
55135aa950SStephen Warren 2000));
56135aa950SStephen Warren
57135aa950SStephen Warren ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test,
58135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI));
59135aa950SStephen Warren ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test,
60135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C));
61135aa950SStephen Warren
62135aa950SStephen Warren ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test,
63135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI,
64135aa950SStephen Warren 10000));
65135aa950SStephen Warren ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test,
66135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C,
67135aa950SStephen Warren 20000));
68135aa950SStephen Warren
69135aa950SStephen Warren rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
70135aa950SStephen Warren ut_assert(IS_ERR_VALUE(rate));
71135aa950SStephen Warren rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
72135aa950SStephen Warren ut_assert(IS_ERR_VALUE(rate));
73135aa950SStephen Warren
74135aa950SStephen Warren ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
75135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI));
76135aa950SStephen Warren ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
77135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C));
78135aa950SStephen Warren
79135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
80135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
81135aa950SStephen Warren ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
82135aa950SStephen Warren ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
83135aa950SStephen Warren
84135aa950SStephen Warren ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI));
85135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
86135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
87135aa950SStephen Warren
88135aa950SStephen Warren ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C));
89135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
90135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
91135aa950SStephen Warren
92135aa950SStephen Warren ut_assertok(sandbox_clk_test_disable(dev_test,
93135aa950SStephen Warren SANDBOX_CLK_TEST_ID_SPI));
94135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
95135aa950SStephen Warren ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
96135aa950SStephen Warren
97135aa950SStephen Warren ut_assertok(sandbox_clk_test_disable(dev_test,
98135aa950SStephen Warren SANDBOX_CLK_TEST_ID_I2C));
99135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
100135aa950SStephen Warren ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
101135aa950SStephen Warren
102135aa950SStephen Warren ut_assertok(sandbox_clk_test_free(dev_test));
1036a1c7cefSSimon Glass
1046a1c7cefSSimon Glass return 0;
1056a1c7cefSSimon Glass }
106135aa950SStephen Warren DM_TEST(dm_test_clk, DM_TESTF_SCAN_FDT);
10765388d0dSNeil Armstrong
dm_test_clk_bulk(struct unit_test_state * uts)10865388d0dSNeil Armstrong static int dm_test_clk_bulk(struct unit_test_state *uts)
10965388d0dSNeil Armstrong {
11065388d0dSNeil Armstrong struct udevice *dev_clk, *dev_test;
11165388d0dSNeil Armstrong
11265388d0dSNeil Armstrong ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
11365388d0dSNeil Armstrong &dev_clk));
11465388d0dSNeil Armstrong ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
11565388d0dSNeil Armstrong &dev_test));
11665388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_get_bulk(dev_test));
11765388d0dSNeil Armstrong
11865388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
11965388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
12065388d0dSNeil Armstrong
12165388d0dSNeil Armstrong /* Fixed clock does not support enable, thus should not fail */
12265388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
12365388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
12465388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
12565388d0dSNeil Armstrong
12665388d0dSNeil Armstrong /* Fixed clock does not support disable, thus should not fail */
12765388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_disable_bulk(dev_test));
12865388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
12965388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
13065388d0dSNeil Armstrong
13165388d0dSNeil Armstrong /* Fixed clock does not support enable, thus should not fail */
13265388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
13365388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
13465388d0dSNeil Armstrong ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
13565388d0dSNeil Armstrong
13665388d0dSNeil Armstrong /* Fixed clock does not support disable, thus should not fail */
13765388d0dSNeil Armstrong ut_assertok(sandbox_clk_test_release_bulk(dev_test));
13865388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
13965388d0dSNeil Armstrong ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
14065388d0dSNeil Armstrong
14165388d0dSNeil Armstrong return 0;
14265388d0dSNeil Armstrong }
14365388d0dSNeil Armstrong DM_TEST(dm_test_clk_bulk, DM_TESTF_SCAN_FDT);
144