1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020 Texas Instruments Inc.
4  * Pratyush Yadav <p.yadav@ti.com>
5  */
6 #include <dm.h>
7 #include <mux.h>
8 #include <mux-internal.h>
9 #include <dt-bindings/mux/mux.h>
10 #include <asm/test.h>
11 #include <dm/test.h>
12 #include <test/ut.h>
13 #include <console.h>
14 #include <rand.h>
15 #include <time.h>
16 
17 #define BUF_SIZE		256
18 
19 /* Test 'mux list' */
dm_test_cmd_mux_list(struct unit_test_state * uts)20 static int dm_test_cmd_mux_list(struct unit_test_state *uts)
21 {
22 	char str[BUF_SIZE], *tok;
23 	struct udevice *dev;
24 	struct mux_chip *chip;
25 	struct mux_control *mux;
26 	int i;
27 	unsigned long val;
28 
29 	ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
30 					      &dev));
31 	chip = dev_get_uclass_priv(dev);
32 	ut_assertnonnull(chip);
33 
34 	run_command("mux list", 0);
35 	ut_assert_nextline("a-mux-controller:");
36 
37 	/*
38 	 * Check the table header to make sure we are not out of sync with the
39 	 * code in the command. If we are, catch it early.
40 	 */
41 	console_record_readline(str, BUF_SIZE);
42 	tok = strtok(str, " ");
43 	ut_asserteq_str("ID", tok);
44 
45 	tok = strtok(NULL, " ");
46 	ut_asserteq_str("Selected", tok);
47 
48 	tok = strtok(NULL, " ");
49 	ut_asserteq_str("Current", tok);
50 	tok = strtok(NULL, " ");
51 	ut_asserteq_str("State", tok);
52 
53 	tok = strtok(NULL, " ");
54 	ut_asserteq_str("Idle", tok);
55 	tok = strtok(NULL, " ");
56 	ut_asserteq_str("State", tok);
57 
58 	tok = strtok(NULL, " ");
59 	ut_asserteq_str("Num", tok);
60 	tok = strtok(NULL, " ");
61 	ut_asserteq_str("States", tok);
62 
63 	for (i = 0; i < chip->controllers; i++) {
64 		mux = &chip->mux[i];
65 
66 		console_record_readline(str, BUF_SIZE);
67 
68 		/*
69 		 * Check if the ID printed matches with the ID of the chip we
70 		 * have.
71 		 */
72 		tok = strtok(str, " ");
73 		ut_assertok(strict_strtoul(tok, 10, &val));
74 		ut_asserteq(i, val);
75 
76 		/* Check if mux selection state matches. */
77 		tok = strtok(NULL, " ");
78 		if (mux->in_use) {
79 			ut_asserteq_str("yes", tok);
80 		} else {
81 			ut_asserteq_str("no", tok);
82 		}
83 
84 		/* Check if the current state matches. */
85 		tok = strtok(NULL, " ");
86 		if (mux->cached_state == MUX_IDLE_AS_IS) {
87 			ut_asserteq_str("unknown", tok);
88 		} else {
89 			ut_assertok(strict_strtoul(tok, 16, &val));
90 			ut_asserteq(mux->cached_state, val);
91 		}
92 
93 		/* Check if the idle state matches */
94 		tok = strtok(NULL, " ");
95 		if (mux->idle_state == MUX_IDLE_AS_IS) {
96 			ut_asserteq_str("as-is", tok);
97 		} else {
98 			ut_assertok(strict_strtoul(tok, 16, &val));
99 			ut_asserteq(mux->idle_state, val);
100 		}
101 
102 		/* Check if the number of states matches */
103 		tok = strtok(NULL, " ");
104 		ut_assertok(strict_strtoul(tok, 16, &val));
105 		ut_asserteq(mux->states, val);
106 	}
107 
108 	return 0;
109 }
110 DM_TEST(dm_test_cmd_mux_list, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
111 
dm_test_cmd_mux_select(struct unit_test_state * uts)112 static int dm_test_cmd_mux_select(struct unit_test_state *uts)
113 {
114 	struct udevice *dev;
115 	struct mux_chip *chip;
116 	struct mux_control *mux;
117 	char cmd[BUF_SIZE];
118 	unsigned int i, state;
119 
120 	ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
121 					      &dev));
122 	chip = dev_get_uclass_priv(dev);
123 	ut_assertnonnull(chip);
124 
125 	srand(get_ticks() + rand());
126 	for (i = 0; i < chip->controllers; i++) {
127 		mux = &chip->mux[i];
128 
129 		state = rand() % mux->states;
130 
131 		snprintf(cmd, BUF_SIZE, "mux select a-mux-controller %x %x", i,
132 			 state);
133 		run_command(cmd, 0);
134 		ut_asserteq(!!mux->in_use, true);
135 		ut_asserteq(state, mux->cached_state);
136 
137 		ut_assertok(mux_control_deselect(mux));
138 	}
139 
140 	return 0;
141 }
142 DM_TEST(dm_test_cmd_mux_select, UTF_SCAN_PDATA | UTF_SCAN_FDT);
143 
dm_test_cmd_mux_deselect(struct unit_test_state * uts)144 static int dm_test_cmd_mux_deselect(struct unit_test_state *uts)
145 {
146 	struct udevice *dev;
147 	struct mux_chip *chip;
148 	struct mux_control *mux;
149 	char cmd[BUF_SIZE];
150 	unsigned int i, state;
151 
152 	ut_assertok(uclass_get_device_by_name(UCLASS_MUX, "a-mux-controller",
153 					      &dev));
154 	chip = dev_get_uclass_priv(dev);
155 	ut_assertnonnull(chip);
156 
157 	srand(get_ticks() + rand());
158 	for (i = 0; i < chip->controllers; i++) {
159 		mux = &chip->mux[i];
160 
161 		state = rand() % mux->states;
162 		ut_assertok(mux_control_select(mux, state));
163 
164 		snprintf(cmd, BUF_SIZE, "mux deselect a-mux-controller %d", i);
165 		run_command(cmd, 0);
166 		ut_asserteq(!!mux->in_use, false);
167 	}
168 
169 	return 0;
170 }
171 DM_TEST(dm_test_cmd_mux_deselect, UTF_SCAN_PDATA | UTF_SCAN_FDT);
172