1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2021 Sean Anderson <sean.anderson@seco.com>
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <log.h>
9 #include <sysinfo.h>
10 #include <asm/gpio.h>
11 #include <dm/test.h>
12 #include <test/test.h>
13 #include <test/ut.h>
14 
dm_test_sysinfo_gpio(struct unit_test_state * uts)15 static int dm_test_sysinfo_gpio(struct unit_test_state *uts)
16 {
17 	char buf[64];
18 	int val;
19 	struct udevice *sysinfo, *gpio;
20 
21 	ut_assertok(uclass_get_device_by_name(UCLASS_SYSINFO, "sysinfo-gpio",
22 					      &sysinfo));
23 	ut_assertok(uclass_get_device_by_name(UCLASS_GPIO, "base-gpios", &gpio));
24 
25 	/*
26 	 * Set up pins: pull-up (1), pull-down (0) and floating (2). This should
27 	 * result in digits 2 0 1, i.e. 2 * 9 + 1 * 3 = 19
28 	 */
29 	sandbox_gpio_set_flags(gpio, 15, GPIOD_EXT_PULL_UP);
30 	sandbox_gpio_set_flags(gpio, 16, GPIOD_EXT_PULL_DOWN);
31 	sandbox_gpio_set_flags(gpio, 17, 0);
32 	ut_assertok(sysinfo_detect(sysinfo));
33 	ut_assertok(sysinfo_get_int(sysinfo, SYSINFO_ID_BOARD_MODEL, &val));
34 	ut_asserteq(19, val);
35 	ut_assertok(sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
36 				    buf));
37 	ut_asserteq_str("rev_a", buf);
38 
39 	/*
40 	 * Set up pins: floating (2), pull-up (1) and pull-down (0). This should
41 	 * result in digits 0 1 2, i.e. 1 * 3 + 2 = 5
42 	 */
43 	sandbox_gpio_set_flags(gpio, 15, 0);
44 	sandbox_gpio_set_flags(gpio, 16, GPIOD_EXT_PULL_UP);
45 	sandbox_gpio_set_flags(gpio, 17, GPIOD_EXT_PULL_DOWN);
46 	ut_assertok(sysinfo_detect(sysinfo));
47 	ut_assertok(sysinfo_get_int(sysinfo, SYSINFO_ID_BOARD_MODEL, &val));
48 	ut_asserteq(5, val);
49 	ut_assertok(sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
50 				    buf));
51 	ut_asserteq_str("foo", buf);
52 
53 	/*
54 	 * Set up pins: floating (2), pull-up (1) and pull-down (0). This should
55 	 * result in digits 1 2 0, i.e. 1 * 9 + 2 * 3 = 15
56 	 */
57 	sandbox_gpio_set_flags(gpio, 15, GPIOD_EXT_PULL_DOWN);
58 	sandbox_gpio_set_flags(gpio, 16, 0);
59 	sandbox_gpio_set_flags(gpio, 17, GPIOD_EXT_PULL_UP);
60 	ut_assertok(sysinfo_detect(sysinfo));
61 	ut_assertok(sysinfo_get_int(sysinfo, SYSINFO_ID_BOARD_MODEL, &val));
62 	ut_asserteq(15, val);
63 	ut_assertok(sysinfo_get_str(sysinfo, SYSINFO_ID_BOARD_MODEL, sizeof(buf),
64 				    buf));
65 	ut_asserteq_str("unknown", buf);
66 
67 	return 0;
68 }
69 DM_TEST(dm_test_sysinfo_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
70