blob: 349123a657c2f0599ebb2f2f07e0d6761284af4d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse821d182014-02-26 15:59:24 -07002/*
3 * Copyright (C) 2013 Google, Inc
Simon Glasse821d182014-02-26 15:59:24 -07004 */
5
6#include <common.h>
7#include <fdtdec.h>
8#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glassb4463db2014-10-04 11:29:51 -060010#include <dm/root.h>
Simon Glasse821d182014-02-26 15:59:24 -070011#include <dm/test.h>
12#include <dm/util.h>
13#include <asm/gpio.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050014#include <test/ut.h>
Simon Glasse821d182014-02-26 15:59:24 -070015
16/* Test that sandbox GPIOs work correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -050017static int dm_test_gpio(struct unit_test_state *uts)
Simon Glasse821d182014-02-26 15:59:24 -070018{
19 unsigned int offset, gpio;
20 struct dm_gpio_ops *ops;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020021 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070022 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);
Simon Glass96f7d602014-10-04 11:29:48 -060043 ut_assert(ops->get_function);
Simon Glasse821d182014-02-26 15:59:24 -070044
45 /* Cannot get a value until it is reserved */
Simon Glass96f7d602014-10-04 11:29:48 -060046 ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
Simon Glasse821d182014-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 Glass96f7d602014-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 Glasse821d182014-02-26 15:59:24 -070053
54 /* Change it to an output */
55 sandbox_gpio_set_direction(dev, offset, 1);
Simon Glass96f7d602014-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 Glasse821d182014-02-26 15:59:24 -070058
59 sandbox_gpio_set_value(dev, offset, 1);
Simon Glass96f7d602014-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 Glasse821d182014-02-26 15:59:24 -070062
Simon Glass96f7d602014-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 Glasse821d182014-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 Glass96f7d602014-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 Glasse821d182014-02-26 15:59:24 -070073 ut_assertok(ops->set_value(dev, offset, 1));
74 ut_asserteq(1, ops->get_value(dev, offset));
75
mario.six@gdsys.cc90937cc2016-05-25 15:15:23 +020076 /* Make it an open drain output, and reset it */
77 ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
78 ut_assertok(ops->set_open_drain(dev, offset, 1));
79 ut_asserteq(1, sandbox_gpio_get_open_drain(dev, offset));
80 ut_assertok(ops->set_open_drain(dev, offset, 0));
81 ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
82
Simon Glasse821d182014-02-26 15:59:24 -070083 /* Make it an input */
84 ut_assertok(ops->direction_input(dev, offset));
Simon Glass96f7d602014-10-04 11:29:48 -060085 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
86 ut_asserteq_str("b4: input: 1 [x] testing", buf);
Simon Glasse821d182014-02-26 15:59:24 -070087 sandbox_gpio_set_value(dev, offset, 0);
88 ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
Simon Glass96f7d602014-10-04 11:29:48 -060089 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
90 ut_asserteq_str("b4: input: 0 [x] testing", buf);
Simon Glasse821d182014-02-26 15:59:24 -070091
Simon Glass96f7d602014-10-04 11:29:48 -060092 ut_assertok(gpio_free(gpio));
93 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
94 ut_asserteq_str("b4: input: 0 [ ]", buf);
Simon Glasse821d182014-02-26 15:59:24 -070095
96 /* Check the 'a' bank also */
97 ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
98 ut_asserteq_str(dev->name, "base-gpios");
99 ut_asserteq(15, offset);
100 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
101
102 name = gpio_get_bank_info(dev, &offset_count);
103 ut_asserteq_str("a", name);
104 ut_asserteq(20, offset_count);
105
Simon Glass96f7d602014-10-04 11:29:48 -0600106 return 0;
107}
108DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
109
110/* Test that sandbox anonymous GPIOs work correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500111static int dm_test_gpio_anon(struct unit_test_state *uts)
Simon Glass96f7d602014-10-04 11:29:48 -0600112{
113 unsigned int offset, gpio;
114 struct udevice *dev;
115 const char *name;
116 int offset_count;
117
Simon Glasse821d182014-02-26 15:59:24 -0700118 /* And the anonymous bank */
119 ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
120 ut_asserteq_str(dev->name, "gpio_sandbox");
121 ut_asserteq(14, offset);
122 ut_asserteq(14, gpio);
123
124 name = gpio_get_bank_info(dev, &offset_count);
125 ut_asserteq_ptr(NULL, name);
126 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
127
128 return 0;
129}
Simon Glass96f7d602014-10-04 11:29:48 -0600130DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass1b27d602014-10-04 11:29:49 -0600131
132/* Test that gpio_requestf() works as expected */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500133static int dm_test_gpio_requestf(struct unit_test_state *uts)
Simon Glass1b27d602014-10-04 11:29:49 -0600134{
135 unsigned int offset, gpio;
136 struct udevice *dev;
137 char buf[80];
138
139 ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
140 ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
141 sandbox_gpio_set_direction(dev, offset, 1);
142 sandbox_gpio_set_value(dev, offset, 1);
143 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
144 ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
145
146 return 0;
147}
148DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassb4463db2014-10-04 11:29:51 -0600149
150/* Test that gpio_request() copies its string */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500151static int dm_test_gpio_copy(struct unit_test_state *uts)
Simon Glassb4463db2014-10-04 11:29:51 -0600152{
153 unsigned int offset, gpio;
154 struct udevice *dev;
155 char buf[80], name[10];
156
157 ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
158 strcpy(name, "odd_name");
159 ut_assertok(gpio_request(gpio, name));
160 sandbox_gpio_set_direction(dev, offset, 1);
161 sandbox_gpio_set_value(dev, offset, 1);
162 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
163 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
164 strcpy(name, "nothing");
165 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
166 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
167
168 return 0;
169}
170DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
171
172/* Test that we don't leak memory with GPIOs */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500173static int dm_test_gpio_leak(struct unit_test_state *uts)
Simon Glassb4463db2014-10-04 11:29:51 -0600174{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500175 ut_assertok(dm_test_gpio(uts));
176 ut_assertok(dm_test_gpio_anon(uts));
177 ut_assertok(dm_test_gpio_requestf(uts));
178 ut_assertok(dm_leak_check_end(uts));
Simon Glassb4463db2014-10-04 11:29:51 -0600179
180 return 0;
181}
Simon Glassb4463db2014-10-04 11:29:51 -0600182DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass16e10402015-01-05 20:05:29 -0700183
184/* Test that we can find GPIOs using phandles */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500185static int dm_test_gpio_phandles(struct unit_test_state *uts)
Simon Glass16e10402015-01-05 20:05:29 -0700186{
187 struct gpio_desc desc, desc_list[8], desc_list2[8];
188 struct udevice *dev, *gpio_a, *gpio_b;
189
190 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
191 ut_asserteq_str("a-test", dev->name);
192
193 ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
194 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
195 ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
196 ut_asserteq_str("base-gpios", gpio_a->name);
197 ut_asserteq(true, !!device_active(gpio_a));
198 ut_asserteq_ptr(gpio_a, desc.dev);
199 ut_asserteq(4, desc.offset);
200 /* GPIOF_INPUT is the sandbox GPIO driver default */
201 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
202 ut_assertok(dm_gpio_free(dev, &desc));
203
204 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
205 0));
206 ut_asserteq_ptr(NULL, desc.dev);
207 ut_asserteq(desc.offset, 0);
208 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
209 0));
210
211 /* Last GPIO is ignord as it comes after <0> */
212 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
213 ARRAY_SIZE(desc_list), 0));
214 ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
215 desc_list2,
216 ARRAY_SIZE(desc_list2),
217 0));
218 ut_assertok(gpio_free_list(dev, desc_list, 3));
219 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
220 ARRAY_SIZE(desc_list),
221 GPIOD_IS_OUT |
222 GPIOD_IS_OUT_ACTIVE));
223 ut_asserteq_ptr(gpio_a, desc_list[0].dev);
224 ut_asserteq(1, desc_list[0].offset);
225 ut_asserteq_ptr(gpio_a, desc_list[1].dev);
226 ut_asserteq(4, desc_list[1].offset);
227 ut_asserteq_ptr(gpio_b, desc_list[2].dev);
228 ut_asserteq(5, desc_list[2].offset);
229 ut_asserteq(1, dm_gpio_get_value(desc_list));
230 ut_assertok(gpio_free_list(dev, desc_list, 3));
231
232 ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
233 ARRAY_SIZE(desc_list), 0));
234 /* This was set to output previously, so still will be */
235 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 1, NULL));
236
237 /* Active low should invert the input value */
238 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
239 ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
240
241 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
242 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
243 ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
244 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
245 ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
246
247
248 return 0;
249}
250DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);