xref: /openbmc/u-boot/test/dm/clk.c (revision f91281b1)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <asm/test.h>
11 #include <dm/test.h>
12 #include <linux/err.h>
13 #include <test/ut.h>
14 
15 /* Test that we can find and adjust clocks */
16 static int dm_test_clk_base(struct unit_test_state *uts)
17 {
18 	struct udevice *clk;
19 	ulong rate;
20 
21 	ut_assertok(uclass_get_device(UCLASS_CLK, 0, &clk));
22 	rate = clk_get_rate(clk);
23 	ut_asserteq(SANDBOX_CLK_RATE, rate);
24 	ut_asserteq(-EINVAL, clk_set_rate(clk, 0));
25 	ut_assertok(clk_set_rate(clk, rate * 2));
26 	ut_asserteq(SANDBOX_CLK_RATE * 2, clk_get_rate(clk));
27 
28 	return 0;
29 }
30 DM_TEST(dm_test_clk_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
31 
32 /* Test that peripheral clocks work as expected */
33 static int dm_test_clk_periph(struct unit_test_state *uts)
34 {
35 	struct udevice *clk;
36 	ulong rate;
37 
38 	ut_assertok(uclass_get_device(UCLASS_CLK, 0, &clk));
39 	rate = clk_set_periph_rate(clk, PERIPH_ID_COUNT, 123);
40 	ut_asserteq(-EINVAL, rate);
41 	ut_asserteq(1, IS_ERR_VALUE(rate));
42 
43 	rate = clk_set_periph_rate(clk, PERIPH_ID_SPI, 123);
44 	ut_asserteq(0, rate);
45 	ut_asserteq(123, clk_get_periph_rate(clk, PERIPH_ID_SPI));
46 
47 	rate = clk_set_periph_rate(clk, PERIPH_ID_SPI, 1234);
48 	ut_asserteq(123, rate);
49 
50 	rate = clk_set_periph_rate(clk, PERIPH_ID_I2C, 567);
51 
52 	rate = clk_set_periph_rate(clk, PERIPH_ID_SPI, 1234);
53 	ut_asserteq(1234, rate);
54 
55 	ut_asserteq(567, clk_get_periph_rate(clk, PERIPH_ID_I2C));
56 
57 	return 0;
58 }
59 DM_TEST(dm_test_clk_periph, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
60