blob: f5c7aaf3bce0979959a2e68b01fa172489fcdd6b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass96495d92014-02-26 15:59:24 -07002/*
3 * Copyright (C) 2013 Google, Inc
Simon Glass96495d92014-02-26 15:59:24 -07004 */
5
6#include <common.h>
7#include <fdtdec.h>
8#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glass43241742014-10-04 11:29:51 -060010#include <dm/root.h>
Simon Glass96495d92014-02-26 15:59:24 -070011#include <dm/test.h>
12#include <dm/util.h>
13#include <asm/gpio.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050014#include <test/ut.h>
Simon Glass96495d92014-02-26 15:59:24 -070015
16/* Test that sandbox GPIOs work correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -050017static int dm_test_gpio(struct unit_test_state *uts)
Simon Glass96495d92014-02-26 15:59:24 -070018{
19 unsigned int offset, gpio;
20 struct dm_gpio_ops *ops;
Heiko Schocher54c5d082014-05-22 12:43:05 +020021 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070022 const char *name;
23 int offset_count;
24 char buf[80];
25
26 /*
Patrick Delaunayff526652020-01-13 11:35:14 +010027 * We expect to get 4 banks. One is anonymous (just numbered) and
28 * comes from platdata. The other are named a (20 gpios),
29 * b (10 gpios) and c (10 gpios) and come from the device tree. See
Simon Glass96495d92014-02-26 15:59:24 -070030 * 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);
Simon Glass4b8f11c2014-10-04 11:29:48 -060043 ut_assert(ops->get_function);
Simon Glass96495d92014-02-26 15:59:24 -070044
45 /* Cannot get a value until it is reserved */
Simon Glass4b8f11c2014-10-04 11:29:48 -060046 ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
Simon Glass96495d92014-02-26 15:59:24 -070047 /*
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 */
Simon Glass4b8f11c2014-10-04 11:29:48 -060051 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
52 ut_asserteq_str("b4: input: 0 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -070053
54 /* Change it to an output */
55 sandbox_gpio_set_direction(dev, offset, 1);
Simon Glass4b8f11c2014-10-04 11:29:48 -060056 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
57 ut_asserteq_str("b4: output: 0 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -070058
59 sandbox_gpio_set_value(dev, offset, 1);
Simon Glass4b8f11c2014-10-04 11:29:48 -060060 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
61 ut_asserteq_str("b4: output: 1 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -070062
Simon Glass4b8f11c2014-10-04 11:29:48 -060063 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);
Simon Glass96495d92014-02-26 15:59:24 -070066
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));
Simon Glass4b8f11c2014-10-04 11:29:48 -060071 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
72 ut_asserteq_str("b4: output: 0 [x] testing", buf);
Simon Glass96495d92014-02-26 15:59:24 -070073 ut_assertok(ops->set_value(dev, offset, 1));
74 ut_asserteq(1, ops->get_value(dev, offset));
75
Patrick Delaunayff526652020-01-13 11:35:14 +010076 /* Make it an open drain output, and reset it */
77 ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
78 sandbox_gpio_get_dir_flags(dev, offset));
79 ut_assertok(ops->set_dir_flags(dev, offset,
80 GPIOD_IS_OUT | GPIOD_OPEN_DRAIN));
81 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN,
82 sandbox_gpio_get_dir_flags(dev, offset));
83 ut_assertok(ops->set_dir_flags(dev, offset,
84 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE));
85 ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
86 sandbox_gpio_get_dir_flags(dev, offset));
87
Simon Glass96495d92014-02-26 15:59:24 -070088 /* Make it an input */
89 ut_assertok(ops->direction_input(dev, offset));
Simon Glass4b8f11c2014-10-04 11:29:48 -060090 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
91 ut_asserteq_str("b4: input: 1 [x] testing", buf);
Simon Glass96495d92014-02-26 15:59:24 -070092 sandbox_gpio_set_value(dev, offset, 0);
93 ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
Simon Glass4b8f11c2014-10-04 11:29:48 -060094 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
95 ut_asserteq_str("b4: input: 0 [x] testing", buf);
Simon Glass96495d92014-02-26 15:59:24 -070096
Simon Glass4b8f11c2014-10-04 11:29:48 -060097 ut_assertok(gpio_free(gpio));
98 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
99 ut_asserteq_str("b4: input: 0 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -0700100
101 /* Check the 'a' bank also */
102 ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
103 ut_asserteq_str(dev->name, "base-gpios");
104 ut_asserteq(15, offset);
105 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
106
107 name = gpio_get_bank_info(dev, &offset_count);
108 ut_asserteq_str("a", name);
109 ut_asserteq(20, offset_count);
110
Simon Glass4b8f11c2014-10-04 11:29:48 -0600111 return 0;
112}
113DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
114
115/* Test that sandbox anonymous GPIOs work correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500116static int dm_test_gpio_anon(struct unit_test_state *uts)
Simon Glass4b8f11c2014-10-04 11:29:48 -0600117{
118 unsigned int offset, gpio;
119 struct udevice *dev;
120 const char *name;
121 int offset_count;
122
Simon Glass96495d92014-02-26 15:59:24 -0700123 /* And the anonymous bank */
124 ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
125 ut_asserteq_str(dev->name, "gpio_sandbox");
126 ut_asserteq(14, offset);
127 ut_asserteq(14, gpio);
128
129 name = gpio_get_bank_info(dev, &offset_count);
130 ut_asserteq_ptr(NULL, name);
131 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
132
133 return 0;
134}
Simon Glass4b8f11c2014-10-04 11:29:48 -0600135DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassd44f5972014-10-04 11:29:49 -0600136
137/* Test that gpio_requestf() works as expected */
Joe Hershbergere721b882015-05-20 14:27:27 -0500138static int dm_test_gpio_requestf(struct unit_test_state *uts)
Simon Glassd44f5972014-10-04 11:29:49 -0600139{
140 unsigned int offset, gpio;
141 struct udevice *dev;
142 char buf[80];
143
144 ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
145 ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
146 sandbox_gpio_set_direction(dev, offset, 1);
147 sandbox_gpio_set_value(dev, offset, 1);
148 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
149 ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
150
151 return 0;
152}
153DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass43241742014-10-04 11:29:51 -0600154
155/* Test that gpio_request() copies its string */
Joe Hershbergere721b882015-05-20 14:27:27 -0500156static int dm_test_gpio_copy(struct unit_test_state *uts)
Simon Glass43241742014-10-04 11:29:51 -0600157{
158 unsigned int offset, gpio;
159 struct udevice *dev;
160 char buf[80], name[10];
161
162 ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
163 strcpy(name, "odd_name");
164 ut_assertok(gpio_request(gpio, name));
165 sandbox_gpio_set_direction(dev, offset, 1);
166 sandbox_gpio_set_value(dev, offset, 1);
167 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
168 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
169 strcpy(name, "nothing");
170 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
171 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
172
173 return 0;
174}
175DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
176
177/* Test that we don't leak memory with GPIOs */
Joe Hershbergere721b882015-05-20 14:27:27 -0500178static int dm_test_gpio_leak(struct unit_test_state *uts)
Simon Glass43241742014-10-04 11:29:51 -0600179{
Joe Hershbergere721b882015-05-20 14:27:27 -0500180 ut_assertok(dm_test_gpio(uts));
181 ut_assertok(dm_test_gpio_anon(uts));
182 ut_assertok(dm_test_gpio_requestf(uts));
183 ut_assertok(dm_leak_check_end(uts));
Simon Glass43241742014-10-04 11:29:51 -0600184
185 return 0;
186}
Simon Glass43241742014-10-04 11:29:51 -0600187DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass3669e0e2015-01-05 20:05:29 -0700188
189/* Test that we can find GPIOs using phandles */
Joe Hershbergere721b882015-05-20 14:27:27 -0500190static int dm_test_gpio_phandles(struct unit_test_state *uts)
Simon Glass3669e0e2015-01-05 20:05:29 -0700191{
192 struct gpio_desc desc, desc_list[8], desc_list2[8];
193 struct udevice *dev, *gpio_a, *gpio_b;
194
195 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
196 ut_asserteq_str("a-test", dev->name);
197
198 ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
199 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
200 ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
201 ut_asserteq_str("base-gpios", gpio_a->name);
202 ut_asserteq(true, !!device_active(gpio_a));
203 ut_asserteq_ptr(gpio_a, desc.dev);
204 ut_asserteq(4, desc.offset);
205 /* GPIOF_INPUT is the sandbox GPIO driver default */
206 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
207 ut_assertok(dm_gpio_free(dev, &desc));
208
209 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
210 0));
211 ut_asserteq_ptr(NULL, desc.dev);
212 ut_asserteq(desc.offset, 0);
213 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
214 0));
215
216 /* Last GPIO is ignord as it comes after <0> */
217 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
218 ARRAY_SIZE(desc_list), 0));
219 ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
220 desc_list2,
221 ARRAY_SIZE(desc_list2),
222 0));
Patrick Delaunayff526652020-01-13 11:35:14 +0100223 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
Simon Glass3669e0e2015-01-05 20:05:29 -0700224 ut_assertok(gpio_free_list(dev, desc_list, 3));
Patrick Delaunayff526652020-01-13 11:35:14 +0100225 ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL));
Simon Glass3669e0e2015-01-05 20:05:29 -0700226 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
227 ARRAY_SIZE(desc_list),
228 GPIOD_IS_OUT |
229 GPIOD_IS_OUT_ACTIVE));
Patrick Delaunayff526652020-01-13 11:35:14 +0100230 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL));
Simon Glass3669e0e2015-01-05 20:05:29 -0700231 ut_asserteq_ptr(gpio_a, desc_list[0].dev);
232 ut_asserteq(1, desc_list[0].offset);
233 ut_asserteq_ptr(gpio_a, desc_list[1].dev);
234 ut_asserteq(4, desc_list[1].offset);
235 ut_asserteq_ptr(gpio_b, desc_list[2].dev);
236 ut_asserteq(5, desc_list[2].offset);
237 ut_asserteq(1, dm_gpio_get_value(desc_list));
238 ut_assertok(gpio_free_list(dev, desc_list, 3));
239
Patrick Delaunayff526652020-01-13 11:35:14 +0100240 ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE,
241 sandbox_gpio_get_dir_flags(gpio_a, 1));
Simon Glass3669e0e2015-01-05 20:05:29 -0700242 ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
243 ARRAY_SIZE(desc_list), 0));
Patrick Delaunayff526652020-01-13 11:35:14 +0100244
245 /* This was set to output previously but flags resetted to 0 = INPUT */
246 ut_asserteq(0, sandbox_gpio_get_dir_flags(gpio_a, 1));
247 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL));
Simon Glass3669e0e2015-01-05 20:05:29 -0700248
249 /* Active low should invert the input value */
250 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
251 ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
252
253 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
254 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
255 ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
256 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
257 ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
258
Simon Glass3669e0e2015-01-05 20:05:29 -0700259 return 0;
260}
261DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Patrick Delaunayff526652020-01-13 11:35:14 +0100262
263/* Check the gpio pin configuration get from device tree information */
264static int dm_test_gpio_get_dir_flags(struct unit_test_state *uts)
265{
266 struct gpio_desc desc_list[6];
267 struct udevice *dev;
268 ulong flags;
269
270 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
271
272 ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list,
273 ARRAY_SIZE(desc_list), 0));
274
275 ut_assertok(dm_gpio_get_dir_flags(&desc_list[0], &flags));
276 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags);
277
278 ut_assertok(dm_gpio_get_dir_flags(&desc_list[1], &flags));
279 ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags);
280
281 ut_assertok(dm_gpio_get_dir_flags(&desc_list[2], &flags));
282 ut_asserteq(GPIOD_IS_OUT, flags);
283
284 ut_assertok(dm_gpio_get_dir_flags(&desc_list[3], &flags));
285 ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags);
286
287 ut_assertok(dm_gpio_get_dir_flags(&desc_list[4], &flags));
288 ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags);
289
290 ut_assertok(dm_gpio_get_dir_flags(&desc_list[5], &flags));
291 ut_asserteq(GPIOD_IS_IN, flags);
292
293 ut_assertok(gpio_free_list(dev, desc_list, 6));
294
295 return 0;
296}
297DM_TEST(dm_test_gpio_get_dir_flags, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);