Merge git://git.denx.de/u-boot-sh
[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 <log.h>
10 #include <malloc.h>
11 #include <dm/root.h>
12 #include <dm/test.h>
13 #include <dm/util.h>
14 #include <asm/gpio.h>
15 #include <test/ut.h>
16
17 /* Test that sandbox GPIOs work correctly */
18 static int dm_test_gpio(struct unit_test_state *uts)
19 {
20         unsigned int offset, gpio;
21         struct dm_gpio_ops *ops;
22         struct udevice *dev;
23         const char *name;
24         int offset_count;
25         char buf[80];
26
27         /*
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
31          * test/dm/test.dts.
32          */
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);
37
38         name = gpio_get_bank_info(dev, &offset_count);
39         ut_asserteq_str("b", name);
40         ut_asserteq(10, offset_count);
41
42         /* Get the operations for this device */
43         ops = gpio_get_ops(dev);
44         ut_assert(ops->get_function);
45
46         /* Cannot get a value until it is reserved */
47         ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
48         /*
49          * Now some tests that use the 'sandbox' back door. All GPIOs
50          * should default to input, include b4 that we are using here.
51          */
52         ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
53         ut_asserteq_str("b4: input: 0 [ ]", buf);
54
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);
59
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);
63
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);
67
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));
76
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));
88
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);
97
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);
101
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);
107
108         name = gpio_get_bank_info(dev, &offset_count);
109         ut_asserteq_str("a", name);
110         ut_asserteq(20, offset_count);
111
112         return 0;
113 }
114 DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
115
116 /* Test that GPIO open-drain/open-source emulation works correctly */
117 static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts)
118 {
119         struct gpio_desc desc_list[8];
120         struct udevice *dev, *gpio_c;
121         char buf[80];
122
123         ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
124         ut_asserteq_str("a-test", dev->name);
125
126         ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c));
127         ut_asserteq_str("pinmux-gpios", gpio_c->name);
128
129         ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
130                                                  ARRAY_SIZE(desc_list), 0))
131
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);
141
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));
145
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);
150
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);
155
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));
159
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);
164
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);
169
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));
173
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);
178
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);
183
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));
187
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);
192
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);
197
198         ut_assertok(gpio_free_list(dev, desc_list, 8));
199
200         return 0;
201 }
202 DM_TEST(dm_test_gpio_opendrain_opensource,
203         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
204
205 /* Test that sandbox anonymous GPIOs work correctly */
206 static int dm_test_gpio_anon(struct unit_test_state *uts)
207 {
208         unsigned int offset, gpio;
209         struct udevice *dev;
210         const char *name;
211         int offset_count;
212
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);
218
219         name = gpio_get_bank_info(dev, &offset_count);
220         ut_asserteq_ptr(NULL, name);
221         ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
222
223         return 0;
224 }
225 DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
226
227 /* Test that gpio_requestf() works as expected */
228 static int dm_test_gpio_requestf(struct unit_test_state *uts)
229 {
230         unsigned int offset, gpio;
231         struct udevice *dev;
232         char buf[80];
233
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);
240
241         return 0;
242 }
243 DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
244
245 /* Test that gpio_request() copies its string */
246 static int dm_test_gpio_copy(struct unit_test_state *uts)
247 {
248         unsigned int offset, gpio;
249         struct udevice *dev;
250         char buf[80], name[10];
251
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);
262
263         return 0;
264 }
265 DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
266
267 /* Test that we don't leak memory with GPIOs */
268 static int dm_test_gpio_leak(struct unit_test_state *uts)
269 {
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));
274
275         return 0;
276 }
277 DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
278
279 /* Test that we can find GPIOs using phandles */
280 static int dm_test_gpio_phandles(struct unit_test_state *uts)
281 {
282         struct gpio_desc desc, desc_list[8], desc_list2[8];
283         struct udevice *dev, *gpio_a, *gpio_b;
284
285         ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
286         ut_asserteq_str("a-test", dev->name);
287
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));
298
299         ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
300                                                   0));
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,
304                                                   0));
305
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",
310                                                       desc_list2,
311                                                       ARRAY_SIZE(desc_list2),
312                                                       0));
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),
318                                                  GPIOD_IS_OUT |
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));
329
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));
334
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));
338
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]));
342
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]));
348
349         return 0;
350 }
351 DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
352
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)
355 {
356         struct gpio_desc desc_list[6];
357         struct udevice *dev;
358         ulong flags;
359
360         ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
361
362         ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
363                                                  ARRAY_SIZE(desc_list), 0));
364
365         ut_assertok(dm_gpio_get_dir_flags(&desc_list[0], &flags));
366         ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags);
367
368         ut_assertok(dm_gpio_get_dir_flags(&desc_list[1], &flags));
369         ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags);
370
371         ut_assertok(dm_gpio_get_dir_flags(&desc_list[2], &flags));
372         ut_asserteq(GPIOD_IS_OUT, flags);
373
374         ut_assertok(dm_gpio_get_dir_flags(&desc_list[3], &flags));
375         ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags);
376
377         ut_assertok(dm_gpio_get_dir_flags(&desc_list[4], &flags));
378         ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags);
379
380         ut_assertok(dm_gpio_get_dir_flags(&desc_list[5], &flags));
381         ut_asserteq(GPIOD_IS_IN, flags);
382
383         ut_assertok(gpio_free_list(dev, desc_list, 6));
384
385         return 0;
386 }
387 DM_TEST(dm_test_gpio_get_dir_flags, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);