2 * Copyright (C) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
15 /* Test that sandbox GPIOs work correctly */
16 static int dm_test_gpio(struct dm_test_state *dms)
18 unsigned int offset, gpio;
19 struct dm_gpio_ops *ops;
26 * We expect to get 3 banks. One is anonymous (just numbered) and
27 * comes from platdata. The other two are named a (20 gpios)
28 * and b (10 gpios) and come from the device tree. See
31 ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
32 ut_asserteq_str(dev->name, "extra-gpios");
33 ut_asserteq(4, offset);
34 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio);
36 name = gpio_get_bank_info(dev, &offset_count);
37 ut_asserteq_str("b", name);
38 ut_asserteq(10, offset_count);
40 /* Get the operations for this device */
41 ops = gpio_get_ops(dev);
42 ut_assert(ops->get_function);
44 /* Cannot get a value until it is reserved */
45 ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
47 * Now some tests that use the 'sandbox' back door. All GPIOs
48 * should default to input, include b4 that we are using here.
50 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
51 ut_asserteq_str("b4: input: 0 [ ]", buf);
53 /* Change it to an output */
54 sandbox_gpio_set_direction(dev, offset, 1);
55 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
56 ut_asserteq_str("b4: output: 0 [ ]", buf);
58 sandbox_gpio_set_value(dev, offset, 1);
59 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
60 ut_asserteq_str("b4: output: 1 [ ]", buf);
62 ut_assertok(gpio_request(gpio, "testing"));
63 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
64 ut_asserteq_str("b4: output: 1 [x] testing", buf);
66 /* Change the value a bit */
67 ut_asserteq(1, ops->get_value(dev, offset));
68 ut_assertok(ops->set_value(dev, offset, 0));
69 ut_asserteq(0, ops->get_value(dev, offset));
70 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
71 ut_asserteq_str("b4: output: 0 [x] testing", buf);
72 ut_assertok(ops->set_value(dev, offset, 1));
73 ut_asserteq(1, ops->get_value(dev, offset));
75 /* Make it an input */
76 ut_assertok(ops->direction_input(dev, offset));
77 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
78 ut_asserteq_str("b4: input: 1 [x] testing", buf);
79 sandbox_gpio_set_value(dev, offset, 0);
80 ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
81 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
82 ut_asserteq_str("b4: input: 0 [x] testing", buf);
84 ut_assertok(gpio_free(gpio));
85 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
86 ut_asserteq_str("b4: input: 0 [ ]", buf);
88 /* Check the 'a' bank also */
89 ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
90 ut_asserteq_str(dev->name, "base-gpios");
91 ut_asserteq(15, offset);
92 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
94 name = gpio_get_bank_info(dev, &offset_count);
95 ut_asserteq_str("a", name);
96 ut_asserteq(20, offset_count);
100 DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
102 /* Test that sandbox anonymous GPIOs work correctly */
103 static int dm_test_gpio_anon(struct dm_test_state *dms)
105 unsigned int offset, gpio;
110 /* And the anonymous bank */
111 ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
112 ut_asserteq_str(dev->name, "gpio_sandbox");
113 ut_asserteq(14, offset);
114 ut_asserteq(14, gpio);
116 name = gpio_get_bank_info(dev, &offset_count);
117 ut_asserteq_ptr(NULL, name);
118 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
122 DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
124 /* Test that gpio_requestf() works as expected */
125 static int dm_test_gpio_requestf(struct dm_test_state *dms)
127 unsigned int offset, gpio;
131 ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
132 ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
133 sandbox_gpio_set_direction(dev, offset, 1);
134 sandbox_gpio_set_value(dev, offset, 1);
135 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
136 ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
140 DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);