1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Google, Inc
17 /* Test that sandbox GPIOs work correctly */
18 static int dm_test_gpio(struct unit_test_state *uts)
20 unsigned int offset, gpio;
21 struct dm_gpio_ops *ops;
28 * We expect to get 4 banks. One is anonymous (just numbered) and
29 * comes from platdata. The other are named a (20 gpios),
30 * b (10 gpios) and c (10 gpios) and come from the device tree. See
33 ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
34 ut_asserteq_str(dev->name, "extra-gpios");
35 ut_asserteq(4, offset);
36 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio);
38 name = gpio_get_bank_info(dev, &offset_count);
39 ut_asserteq_str("b", name);
40 ut_asserteq(10, offset_count);
42 /* Get the operations for this device */
43 ops = gpio_get_ops(dev);
44 ut_assert(ops->get_function);
46 /* Cannot get a value until it is reserved */
47 ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
49 * Now some tests that use the 'sandbox' back door. All GPIOs
50 * should default to input, include b4 that we are using here.
52 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
53 ut_asserteq_str("b4: input: 0 [ ]", buf);
55 /* Change it to an output */
56 sandbox_gpio_set_direction(dev, offset, 1);
57 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
58 ut_asserteq_str("b4: output: 0 [ ]", buf);
60 sandbox_gpio_set_value(dev, offset, 1);
61 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
62 ut_asserteq_str("b4: output: 1 [ ]", buf);
64 ut_assertok(gpio_request(gpio, "testing"));
65 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
66 ut_asserteq_str("b4: output: 1 [x] testing", buf);
68 /* Change the value a bit */
69 ut_asserteq(1, ops->get_value(dev, offset));
70 ut_assertok(ops->set_value(dev, offset, 0));
71 ut_asserteq(0, ops->get_value(dev, offset));
72 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
73 ut_asserteq_str("b4: output: 0 [x] testing", buf);
74 ut_assertok(ops->set_value(dev, offset, 1));
75 ut_asserteq(1, ops->get_value(dev, offset));
77 /* Make it an open drain output, and reset it */
78 ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
79 sandbox_gpio_get_dir_flags(dev, offset));
80 ut_assertok(ops->set_dir_flags(dev, offset,
81 GPIOD_IS_OUT | GPIOD_OPEN_DRAIN));
82 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
83 sandbox_gpio_get_dir_flags(dev, offset));
84 ut_assertok(ops->set_dir_flags(dev, offset,
85 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE));
86 ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
87 sandbox_gpio_get_dir_flags(dev, offset));
89 /* Make it an input */
90 ut_assertok(ops->direction_input(dev, offset));
91 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
92 ut_asserteq_str("b4: input: 1 [x] testing", buf);
93 sandbox_gpio_set_value(dev, offset, 0);
94 ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
95 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
96 ut_asserteq_str("b4: input: 0 [x] testing", buf);
98 ut_assertok(gpio_free(gpio));
99 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
100 ut_asserteq_str("b4: input: 0 [ ]", buf);
102 /* Check the 'a' bank also */
103 ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
104 ut_asserteq_str(dev->name, "base-gpios");
105 ut_asserteq(15, offset);
106 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
108 name = gpio_get_bank_info(dev, &offset_count);
109 ut_asserteq_str("a", name);
110 ut_asserteq(20, offset_count);
114 DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
116 /* Test that GPIO open-drain/open-source emulation works correctly */
117 static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
119 struct gpio_desc desc_list[8];
120 struct udevice *dev, *gpio_c;
123 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
124 ut_asserteq_str("a-test", dev->name);
126 ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c));
127 ut_asserteq_str("pinmux-gpios", gpio_c->name);
129 ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
130 ARRAY_SIZE(desc_list), 0))
132 ut_asserteq(true, !!device_active(gpio_c));
133 ut_asserteq_ptr(gpio_c, desc_list[0].dev);
134 ut_asserteq_ptr(gpio_c, desc_list[1].dev);
135 ut_asserteq_ptr(gpio_c, desc_list[2].dev);
136 ut_asserteq_ptr(gpio_c, desc_list[3].dev);
137 ut_asserteq_ptr(gpio_c, desc_list[4].dev);
138 ut_asserteq_ptr(gpio_c, desc_list[5].dev);
139 ut_asserteq_ptr(gpio_c, desc_list[6].dev);
140 ut_asserteq_ptr(gpio_c, desc_list[7].dev);
142 /* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */
143 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
144 sandbox_gpio_get_dir_flags(gpio_c, 0));
146 /* Set it as output high, should become an input */
147 ut_assertok(dm_gpio_set_value(&desc_list[0], 1));
148 ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf)));
149 ut_asserteq_str("c0: input: 0 [x] a-test.test3-gpios0", buf);
151 /* Set it as output low, should become output low */
152 ut_assertok(dm_gpio_set_value(&desc_list[0], 0));
153 ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf)));
154 ut_asserteq_str("c0: output: 0 [x] a-test.test3-gpios0", buf);
156 /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */
157 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
158 sandbox_gpio_get_dir_flags(gpio_c, 1));
160 /* Set it as output high, should become output high */
161 ut_assertok(dm_gpio_set_value(&desc_list[1], 1));
162 ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf)));
163 ut_asserteq_str("c1: output: 1 [x] a-test.test3-gpios1", buf);
165 /* Set it as output low, should become an input */
166 ut_assertok(dm_gpio_set_value(&desc_list[1], 0));
167 ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf)));
168 ut_asserteq_str("c1: input: 1 [x] a-test.test3-gpios1", buf);
170 /* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */
171 ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
172 sandbox_gpio_get_dir_flags(gpio_c, 6));
174 /* Set it as output high, should become output low */
175 ut_assertok(dm_gpio_set_value(&desc_list[6], 1));
176 ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf)));
177 ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf);
179 /* Set it as output low, should become an input */
180 ut_assertok(dm_gpio_set_value(&desc_list[6], 0));
181 ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf)));
182 ut_asserteq_str("c6: input: 0 [x] a-test.test3-gpios6", buf);
184 /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */
185 ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE,
186 sandbox_gpio_get_dir_flags(gpio_c, 7));
188 /* Set it as output high, should become an input */
189 ut_assertok(dm_gpio_set_value(&desc_list[7], 1));
190 ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf)));
191 ut_asserteq_str("c7: input: 0 [x] a-test.test3-gpios7", buf);
193 /* Set it as output low, should become output high */
194 ut_assertok(dm_gpio_set_value(&desc_list[7], 0));
195 ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf)));
196 ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf);
198 ut_assertok(gpio_free_list(dev, desc_list, 8));
202 DM_TEST(dm_test_gpio_opendrain_opensource,
203 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
205 /* Test that sandbox anonymous GPIOs work correctly */
206 static int dm_test_gpio_anon(struct unit_test_state *uts)
208 unsigned int offset, gpio;
213 /* And the anonymous bank */
214 ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
215 ut_asserteq_str(dev->name, "gpio_sandbox");
216 ut_asserteq(14, offset);
217 ut_asserteq(14, gpio);
219 name = gpio_get_bank_info(dev, &offset_count);
220 ut_asserteq_ptr(NULL, name);
221 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
225 DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
227 /* Test that gpio_requestf() works as expected */
228 static int dm_test_gpio_requestf(struct unit_test_state *uts)
230 unsigned int offset, gpio;
234 ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
235 ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
236 sandbox_gpio_set_direction(dev, offset, 1);
237 sandbox_gpio_set_value(dev, offset, 1);
238 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
239 ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
243 DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
245 /* Test that gpio_request() copies its string */
246 static int dm_test_gpio_copy(struct unit_test_state *uts)
248 unsigned int offset, gpio;
250 char buf[80], name[10];
252 ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
253 strcpy(name, "odd_name");
254 ut_assertok(gpio_request(gpio, name));
255 sandbox_gpio_set_direction(dev, offset, 1);
256 sandbox_gpio_set_value(dev, offset, 1);
257 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
258 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
259 strcpy(name, "nothing");
260 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
261 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
265 DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
267 /* Test that we don't leak memory with GPIOs */
268 static int dm_test_gpio_leak(struct unit_test_state *uts)
270 ut_assertok(dm_test_gpio(uts));
271 ut_assertok(dm_test_gpio_anon(uts));
272 ut_assertok(dm_test_gpio_requestf(uts));
273 ut_assertok(dm_leak_check_end(uts));
277 DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
279 /* Test that we can find GPIOs using phandles */
280 static int dm_test_gpio_phandles(struct unit_test_state *uts)
282 struct gpio_desc desc, desc_list[8], desc_list2[8];
283 struct udevice *dev, *gpio_a, *gpio_b;
285 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
286 ut_asserteq_str("a-test", dev->name);
288 ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
289 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
290 ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
291 ut_asserteq_str("base-gpios", gpio_a->name);
292 ut_asserteq(true, !!device_active(gpio_a));
293 ut_asserteq_ptr(gpio_a, desc.dev);
294 ut_asserteq(4, desc.offset);
295 /* GPIOF_INPUT is the sandbox GPIO driver default */
296 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
297 ut_assertok(dm_gpio_free(dev, &desc));
299 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
301 ut_asserteq_ptr(NULL, desc.dev);
302 ut_asserteq(desc.offset, 0);
303 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
306 /* Last GPIO is ignord as it comes after <0> */
307 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
308 ARRAY_SIZE(desc_list), 0));
309 ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
311 ARRAY_SIZE(desc_list2),
313 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
314 ut_assertok(gpio_free_list(dev, desc_list, 3));
315 ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL));
316 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
317 ARRAY_SIZE(desc_list),
319 GPIOD_IS_OUT_ACTIVE));
320 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL));
321 ut_asserteq_ptr(gpio_a, desc_list[0].dev);
322 ut_asserteq(1, desc_list[0].offset);
323 ut_asserteq_ptr(gpio_a, desc_list[1].dev);
324 ut_asserteq(4, desc_list[1].offset);
325 ut_asserteq_ptr(gpio_b, desc_list[2].dev);
326 ut_asserteq(5, desc_list[2].offset);
327 ut_asserteq(1, dm_gpio_get_value(desc_list));
328 ut_assertok(gpio_free_list(dev, desc_list, 3));
330 ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
331 sandbox_gpio_get_dir_flags(gpio_a, 1));
332 ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
333 ARRAY_SIZE(desc_list), 0));
335 /* This was set to output previously but flags resetted to 0 = INPUT */
336 ut_asserteq(0, sandbox_gpio_get_dir_flags(gpio_a, 1));
337 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL));
339 /* Active low should invert the input value */
340 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
341 ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
343 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
344 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
345 ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
346 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
347 ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
351 DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
353 /* Check the gpio pin configuration get from device tree information */
354 static int dm_test_gpio_get_dir_flags(struct unit_test_state *uts)
356 struct gpio_desc desc_list[6];
360 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
362 ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
363 ARRAY_SIZE(desc_list), 0));
365 ut_assertok(dm_gpio_get_dir_flags(&desc_list[0], &flags));
366 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags);
368 ut_assertok(dm_gpio_get_dir_flags(&desc_list[1], &flags));
369 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags);
371 ut_assertok(dm_gpio_get_dir_flags(&desc_list[2], &flags));
372 ut_asserteq(GPIOD_IS_OUT, flags);
374 ut_assertok(dm_gpio_get_dir_flags(&desc_list[3], &flags));
375 ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags);
377 ut_assertok(dm_gpio_get_dir_flags(&desc_list[4], &flags));
378 ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags);
380 ut_assertok(dm_gpio_get_dir_flags(&desc_list[5], &flags));
381 ut_asserteq(GPIOD_IS_IN, flags);
383 ut_assertok(gpio_free_list(dev, desc_list, 6));
387 DM_TEST(dm_test_gpio_get_dir_flags, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);