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_state);
44
/* Cannot get a value until it is reserved */
45
ut_asserteq(-1, ops->get_value(dev, offset));
48
* Now some tests that use the 'sandbox' back door. All GPIOs
49
* should default to input, include b4 that we are using here.
51
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
52
ut_asserteq_str("b4: in: 0 [ ]", buf);
54
/* Change it to an output */
55
sandbox_gpio_set_direction(dev, offset, 1);
56
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
57
ut_asserteq_str("b4: out: 0 [ ]", buf);
59
sandbox_gpio_set_value(dev, offset, 1);
60
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
61
ut_asserteq_str("b4: out: 1 [ ]", buf);
63
ut_assertok(ops->request(dev, offset, "testing"));
64
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
65
ut_asserteq_str("b4: out: 1 [x] testing", buf);
67
/* Change the value a bit */
68
ut_asserteq(1, ops->get_value(dev, offset));
69
ut_assertok(ops->set_value(dev, offset, 0));
70
ut_asserteq(0, ops->get_value(dev, offset));
71
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
72
ut_asserteq_str("b4: out: 0 [x] testing", buf);
73
ut_assertok(ops->set_value(dev, offset, 1));
74
ut_asserteq(1, ops->get_value(dev, offset));
76
/* Make it an input */
77
ut_assertok(ops->direction_input(dev, offset));
78
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
79
ut_asserteq_str("b4: in: 1 [x] testing", buf);
80
sandbox_gpio_set_value(dev, offset, 0);
81
ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
82
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
83
ut_asserteq_str("b4: in: 0 [x] testing", buf);
85
ut_assertok(ops->free(dev, offset));
86
ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
87
ut_asserteq_str("b4: in: 0 [ ]", buf);
89
/* Check the 'a' bank also */
90
ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
91
ut_asserteq_str(dev->name, "base-gpios");
92
ut_asserteq(15, offset);
93
ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
95
name = gpio_get_bank_info(dev, &offset_count);
96
ut_asserteq_str("a", name);
97
ut_asserteq(20, offset_count);
99
/* And the anonymous bank */
100
ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
101
ut_asserteq_str(dev->name, "gpio_sandbox");
102
ut_asserteq(14, offset);
103
ut_asserteq(14, gpio);
105
name = gpio_get_bank_info(dev, &offset_count);
106
ut_asserteq_ptr(NULL, name);
107
ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
111
DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);