gpio: remove the open_drain API and ops
[oweals/u-boot.git] / test / dm / gpio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2013 Google, Inc
4  */
5
6 #include <common.h>
7 #include <fdtdec.h>
8 #include <dm.h>
9 #include <malloc.h>
10 #include <dm/root.h>
11 #include <dm/test.h>
12 #include <dm/util.h>
13 #include <asm/gpio.h>
14 #include <test/ut.h>
15
16 /* Test that sandbox GPIOs work correctly */
17 static int dm_test_gpio(struct unit_test_state *uts)
18 {
19         unsigned int offset, gpio;
20         struct dm_gpio_ops *ops;
21         struct udevice *dev;
22         const char *name;
23         int offset_count;
24         char buf[80];
25
26         /*
27          * We expect to get 3 banks. One is anonymous (just numbered) and
28          * comes from platdata. The other two are named a (20 gpios)
29          * and b (10 gpios) and come from the device tree. See
30          * test/dm/test.dts.
31          */
32         ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio));
33         ut_asserteq_str(dev->name, "extra-gpios");
34         ut_asserteq(4, offset);
35         ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio);
36
37         name = gpio_get_bank_info(dev, &offset_count);
38         ut_asserteq_str("b", name);
39         ut_asserteq(10, offset_count);
40
41         /* Get the operations for this device */
42         ops = gpio_get_ops(dev);
43         ut_assert(ops->get_function);
44
45         /* Cannot get a value until it is reserved */
46         ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
47         /*
48          * Now some tests that use the 'sandbox' back door. All GPIOs
49          * should default to input, include b4 that we are using here.
50          */
51         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
52         ut_asserteq_str("b4: input: 0 [ ]", buf);
53
54         /* Change it to an output */
55         sandbox_gpio_set_direction(dev, offset, 1);
56         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
57         ut_asserteq_str("b4: output: 0 [ ]", buf);
58
59         sandbox_gpio_set_value(dev, offset, 1);
60         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
61         ut_asserteq_str("b4: output: 1 [ ]", buf);
62
63         ut_assertok(gpio_request(gpio, "testing"));
64         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
65         ut_asserteq_str("b4: output: 1 [x] testing", buf);
66
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(gpio_get_status(dev, offset, buf, sizeof(buf)));
72         ut_asserteq_str("b4: output: 0 [x] testing", buf);
73         ut_assertok(ops->set_value(dev, offset, 1));
74         ut_asserteq(1, ops->get_value(dev, offset));
75
76         /* Make it an input */
77         ut_assertok(ops->direction_input(dev, offset));
78         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
79         ut_asserteq_str("b4: input: 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(gpio_get_status(dev, offset, buf, sizeof(buf)));
83         ut_asserteq_str("b4: input: 0 [x] testing", buf);
84
85         ut_assertok(gpio_free(gpio));
86         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
87         ut_asserteq_str("b4: input: 0 [ ]", buf);
88
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);
94
95         name = gpio_get_bank_info(dev, &offset_count);
96         ut_asserteq_str("a", name);
97         ut_asserteq(20, offset_count);
98
99         return 0;
100 }
101 DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
102
103 /* Test that sandbox anonymous GPIOs work correctly */
104 static int dm_test_gpio_anon(struct unit_test_state *uts)
105 {
106         unsigned int offset, gpio;
107         struct udevice *dev;
108         const char *name;
109         int offset_count;
110
111         /* And the anonymous bank */
112         ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
113         ut_asserteq_str(dev->name, "gpio_sandbox");
114         ut_asserteq(14, offset);
115         ut_asserteq(14, gpio);
116
117         name = gpio_get_bank_info(dev, &offset_count);
118         ut_asserteq_ptr(NULL, name);
119         ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
120
121         return 0;
122 }
123 DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
124
125 /* Test that gpio_requestf() works as expected */
126 static int dm_test_gpio_requestf(struct unit_test_state *uts)
127 {
128         unsigned int offset, gpio;
129         struct udevice *dev;
130         char buf[80];
131
132         ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
133         ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
134         sandbox_gpio_set_direction(dev, offset, 1);
135         sandbox_gpio_set_value(dev, offset, 1);
136         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
137         ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
138
139         return 0;
140 }
141 DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
142
143 /* Test that gpio_request() copies its string */
144 static int dm_test_gpio_copy(struct unit_test_state *uts)
145 {
146         unsigned int offset, gpio;
147         struct udevice *dev;
148         char buf[80], name[10];
149
150         ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
151         strcpy(name, "odd_name");
152         ut_assertok(gpio_request(gpio, name));
153         sandbox_gpio_set_direction(dev, offset, 1);
154         sandbox_gpio_set_value(dev, offset, 1);
155         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
156         ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
157         strcpy(name, "nothing");
158         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
159         ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
160
161         return 0;
162 }
163 DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
164
165 /* Test that we don't leak memory with GPIOs */
166 static int dm_test_gpio_leak(struct unit_test_state *uts)
167 {
168         ut_assertok(dm_test_gpio(uts));
169         ut_assertok(dm_test_gpio_anon(uts));
170         ut_assertok(dm_test_gpio_requestf(uts));
171         ut_assertok(dm_leak_check_end(uts));
172
173         return 0;
174 }
175 DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
176
177 /* Test that we can find GPIOs using phandles */
178 static int dm_test_gpio_phandles(struct unit_test_state *uts)
179 {
180         struct gpio_desc desc, desc_list[8], desc_list2[8];
181         struct udevice *dev, *gpio_a, *gpio_b;
182
183         ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
184         ut_asserteq_str("a-test", dev->name);
185
186         ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
187         ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
188         ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
189         ut_asserteq_str("base-gpios", gpio_a->name);
190         ut_asserteq(true, !!device_active(gpio_a));
191         ut_asserteq_ptr(gpio_a, desc.dev);
192         ut_asserteq(4, desc.offset);
193         /* GPIOF_INPUT is the sandbox GPIO driver default */
194         ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
195         ut_assertok(dm_gpio_free(dev, &desc));
196
197         ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
198                                                   0));
199         ut_asserteq_ptr(NULL, desc.dev);
200         ut_asserteq(desc.offset, 0);
201         ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
202                                                   0));
203
204         /* Last GPIO is ignord as it comes after <0> */
205         ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
206                                                  ARRAY_SIZE(desc_list), 0));
207         ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
208                                                       desc_list2,
209                                                       ARRAY_SIZE(desc_list2),
210                                                       0));
211         ut_assertok(gpio_free_list(dev, desc_list, 3));
212         ut_asserteq(3, gpio_request_list_by_name(dev,  "test-gpios", desc_list,
213                                                  ARRAY_SIZE(desc_list),
214                                                  GPIOD_IS_OUT |
215                                                  GPIOD_IS_OUT_ACTIVE));
216         ut_asserteq_ptr(gpio_a, desc_list[0].dev);
217         ut_asserteq(1, desc_list[0].offset);
218         ut_asserteq_ptr(gpio_a, desc_list[1].dev);
219         ut_asserteq(4, desc_list[1].offset);
220         ut_asserteq_ptr(gpio_b, desc_list[2].dev);
221         ut_asserteq(5, desc_list[2].offset);
222         ut_asserteq(1, dm_gpio_get_value(desc_list));
223         ut_assertok(gpio_free_list(dev, desc_list, 3));
224
225         ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
226                                                  ARRAY_SIZE(desc_list), 0));
227         /* This was set to output previously, so still will be */
228         ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 1, NULL));
229
230         /* Active low should invert the input value */
231         ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
232         ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
233
234         ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
235         ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
236         ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
237         ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
238         ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
239
240
241         return 0;
242 }
243 DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);