1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <log.h>
9 #include <syscon.h>
10 #include <regmap.h>
11 #include <asm/test.h>
12 #include <dm/test.h>
13 #include <linux/err.h>
14 #include <test/test.h>
15 #include <test/ut.h>
16 
17 /* Base test of system controllers */
dm_test_syscon_base(struct unit_test_state * uts)18 static int dm_test_syscon_base(struct unit_test_state *uts)
19 {
20 	struct udevice *dev;
21 
22 	ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev));
23 	ut_asserteq(SYSCON0, dev->driver_data);
24 
25 	ut_assertok(uclass_get_device(UCLASS_SYSCON, 1, &dev));
26 	ut_asserteq(SYSCON1, dev->driver_data);
27 
28 	ut_asserteq(-ENODEV, uclass_get_device(UCLASS_SYSCON, 2, &dev));
29 
30 	return 0;
31 }
32 DM_TEST(dm_test_syscon_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
33 
34 /* Test system controller finding */
dm_test_syscon_by_driver_data(struct unit_test_state * uts)35 static int dm_test_syscon_by_driver_data(struct unit_test_state *uts)
36 {
37 	struct udevice *dev;
38 
39 	ut_assertok(syscon_get_by_driver_data(SYSCON0, &dev));
40 	ut_asserteq(SYSCON0, dev->driver_data);
41 
42 	ut_assertok(syscon_get_by_driver_data(SYSCON1, &dev));
43 	ut_asserteq(SYSCON1, dev->driver_data);
44 
45 	ut_asserteq(-ENODEV, syscon_get_by_driver_data(2, &dev));
46 
47 	return 0;
48 }
49 DM_TEST(dm_test_syscon_by_driver_data, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
50 
51 /* Test system controller by phandle */
dm_test_syscon_by_phandle(struct unit_test_state * uts)52 static int dm_test_syscon_by_phandle(struct unit_test_state *uts)
53 {
54 	struct udevice *dev;
55 	struct regmap *map;
56 
57 	ut_assertok(uclass_get_device_by_name(UCLASS_TEST_PROBE, "test4",
58 					      &dev));
59 
60 	ut_assertok_ptr(syscon_regmap_lookup_by_phandle(dev, "first-syscon"));
61 	map = syscon_regmap_lookup_by_phandle(dev, "first-syscon");
62 	ut_assert(map);
63 	ut_assert(!IS_ERR(map));
64 	ut_asserteq(1, map->range_count);
65 
66 	ut_assertok_ptr(syscon_regmap_lookup_by_phandle(dev,
67 							"second-sys-ctrl"));
68 	map = syscon_regmap_lookup_by_phandle(dev, "second-sys-ctrl");
69 	ut_assert(map);
70 	ut_assert(!IS_ERR(map));
71 	ut_asserteq(4, map->range_count);
72 
73 	ut_assertok_ptr(syscon_regmap_lookup_by_phandle(dev,
74 							"third-syscon"));
75 	map = syscon_regmap_lookup_by_phandle(dev, "third-syscon");
76 	ut_assert(map);
77 	ut_assert(!IS_ERR(map));
78 	ut_asserteq(4, map->range_count);
79 
80 	ut_assert(IS_ERR(syscon_regmap_lookup_by_phandle(dev, "not-present")));
81 
82 	return 0;
83 }
84 DM_TEST(dm_test_syscon_by_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
85