blob: bb4b20cea938d57463627e697f8bd20cf09db4cd [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 Glass43241742014-10-04 11:29:51 -06009#include <dm/root.h>
Simon Glass96495d92014-02-26 15:59:24 -070010#include <dm/test.h>
11#include <dm/util.h>
12#include <asm/gpio.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050013#include <test/ut.h>
Simon Glass96495d92014-02-26 15:59:24 -070014
15/* Test that sandbox GPIOs work correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -050016static int dm_test_gpio(struct unit_test_state *uts)
Simon Glass96495d92014-02-26 15:59:24 -070017{
18 unsigned int offset, gpio;
19 struct dm_gpio_ops *ops;
Heiko Schocher54c5d082014-05-22 12:43:05 +020020 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070021 const char *name;
22 int offset_count;
23 char buf[80];
24
25 /*
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
29 * test/dm/test.dts.
30 */
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);
35
36 name = gpio_get_bank_info(dev, &offset_count);
37 ut_asserteq_str("b", name);
38 ut_asserteq(10, offset_count);
39
40 /* Get the operations for this device */
41 ops = gpio_get_ops(dev);
Simon Glass4b8f11c2014-10-04 11:29:48 -060042 ut_assert(ops->get_function);
Simon Glass96495d92014-02-26 15:59:24 -070043
44 /* Cannot get a value until it is reserved */
Simon Glass4b8f11c2014-10-04 11:29:48 -060045 ut_asserteq(-EBUSY, gpio_get_value(gpio + 1));
Simon Glass96495d92014-02-26 15:59:24 -070046 /*
47 * Now some tests that use the 'sandbox' back door. All GPIOs
48 * should default to input, include b4 that we are using here.
49 */
Simon Glass4b8f11c2014-10-04 11:29:48 -060050 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
51 ut_asserteq_str("b4: input: 0 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -070052
53 /* Change it to an output */
54 sandbox_gpio_set_direction(dev, offset, 1);
Simon Glass4b8f11c2014-10-04 11:29:48 -060055 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
56 ut_asserteq_str("b4: output: 0 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -070057
58 sandbox_gpio_set_value(dev, offset, 1);
Simon Glass4b8f11c2014-10-04 11:29:48 -060059 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
60 ut_asserteq_str("b4: output: 1 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -070061
Simon Glass4b8f11c2014-10-04 11:29:48 -060062 ut_assertok(gpio_request(gpio, "testing"));
63 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
64 ut_asserteq_str("b4: output: 1 [x] testing", buf);
Simon Glass96495d92014-02-26 15:59:24 -070065
66 /* Change the value a bit */
67 ut_asserteq(1, ops->get_value(dev, offset));
68 ut_assertok(ops->set_value(dev, offset, 0));
69 ut_asserteq(0, ops->get_value(dev, offset));
Simon Glass4b8f11c2014-10-04 11:29:48 -060070 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
71 ut_asserteq_str("b4: output: 0 [x] testing", buf);
Simon Glass96495d92014-02-26 15:59:24 -070072 ut_assertok(ops->set_value(dev, offset, 1));
73 ut_asserteq(1, ops->get_value(dev, offset));
74
mario.six@gdsys.cc743268f2016-05-25 15:15:23 +020075 /* Make it an open drain output, and reset it */
76 ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
77 ut_assertok(ops->set_open_drain(dev, offset, 1));
78 ut_asserteq(1, sandbox_gpio_get_open_drain(dev, offset));
79 ut_assertok(ops->set_open_drain(dev, offset, 0));
80 ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset));
81
Simon Glass96495d92014-02-26 15:59:24 -070082 /* Make it an input */
83 ut_assertok(ops->direction_input(dev, offset));
Simon Glass4b8f11c2014-10-04 11:29:48 -060084 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
85 ut_asserteq_str("b4: input: 1 [x] testing", buf);
Simon Glass96495d92014-02-26 15:59:24 -070086 sandbox_gpio_set_value(dev, offset, 0);
87 ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
Simon Glass4b8f11c2014-10-04 11:29:48 -060088 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
89 ut_asserteq_str("b4: input: 0 [x] testing", buf);
Simon Glass96495d92014-02-26 15:59:24 -070090
Simon Glass4b8f11c2014-10-04 11:29:48 -060091 ut_assertok(gpio_free(gpio));
92 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
93 ut_asserteq_str("b4: input: 0 [ ]", buf);
Simon Glass96495d92014-02-26 15:59:24 -070094
95 /* Check the 'a' bank also */
96 ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
97 ut_asserteq_str(dev->name, "base-gpios");
98 ut_asserteq(15, offset);
99 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
100
101 name = gpio_get_bank_info(dev, &offset_count);
102 ut_asserteq_str("a", name);
103 ut_asserteq(20, offset_count);
104
Simon Glass4b8f11c2014-10-04 11:29:48 -0600105 return 0;
106}
107DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
108
109/* Test that sandbox anonymous GPIOs work correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500110static int dm_test_gpio_anon(struct unit_test_state *uts)
Simon Glass4b8f11c2014-10-04 11:29:48 -0600111{
112 unsigned int offset, gpio;
113 struct udevice *dev;
114 const char *name;
115 int offset_count;
116
Simon Glass96495d92014-02-26 15:59:24 -0700117 /* And the anonymous bank */
118 ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
119 ut_asserteq_str(dev->name, "gpio_sandbox");
120 ut_asserteq(14, offset);
121 ut_asserteq(14, gpio);
122
123 name = gpio_get_bank_info(dev, &offset_count);
124 ut_asserteq_ptr(NULL, name);
125 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
126
127 return 0;
128}
Simon Glass4b8f11c2014-10-04 11:29:48 -0600129DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassd44f5972014-10-04 11:29:49 -0600130
131/* Test that gpio_requestf() works as expected */
Joe Hershbergere721b882015-05-20 14:27:27 -0500132static int dm_test_gpio_requestf(struct unit_test_state *uts)
Simon Glassd44f5972014-10-04 11:29:49 -0600133{
134 unsigned int offset, gpio;
135 struct udevice *dev;
136 char buf[80];
137
138 ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio));
139 ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi"));
140 sandbox_gpio_set_direction(dev, offset, 1);
141 sandbox_gpio_set_value(dev, offset, 1);
142 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
143 ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf);
144
145 return 0;
146}
147DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass43241742014-10-04 11:29:51 -0600148
149/* Test that gpio_request() copies its string */
Joe Hershbergere721b882015-05-20 14:27:27 -0500150static int dm_test_gpio_copy(struct unit_test_state *uts)
Simon Glass43241742014-10-04 11:29:51 -0600151{
152 unsigned int offset, gpio;
153 struct udevice *dev;
154 char buf[80], name[10];
155
156 ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio));
157 strcpy(name, "odd_name");
158 ut_assertok(gpio_request(gpio, name));
159 sandbox_gpio_set_direction(dev, offset, 1);
160 sandbox_gpio_set_value(dev, offset, 1);
161 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
162 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
163 strcpy(name, "nothing");
164 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf)));
165 ut_asserteq_str("b6: output: 1 [x] odd_name", buf);
166
167 return 0;
168}
169DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
170
171/* Test that we don't leak memory with GPIOs */
Joe Hershbergere721b882015-05-20 14:27:27 -0500172static int dm_test_gpio_leak(struct unit_test_state *uts)
Simon Glass43241742014-10-04 11:29:51 -0600173{
Joe Hershbergere721b882015-05-20 14:27:27 -0500174 ut_assertok(dm_test_gpio(uts));
175 ut_assertok(dm_test_gpio_anon(uts));
176 ut_assertok(dm_test_gpio_requestf(uts));
177 ut_assertok(dm_leak_check_end(uts));
Simon Glass43241742014-10-04 11:29:51 -0600178
179 return 0;
180}
Simon Glass43241742014-10-04 11:29:51 -0600181DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass3669e0e2015-01-05 20:05:29 -0700182
183/* Test that we can find GPIOs using phandles */
Joe Hershbergere721b882015-05-20 14:27:27 -0500184static int dm_test_gpio_phandles(struct unit_test_state *uts)
Simon Glass3669e0e2015-01-05 20:05:29 -0700185{
186 struct gpio_desc desc, desc_list[8], desc_list2[8];
187 struct udevice *dev, *gpio_a, *gpio_b;
188
189 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
190 ut_asserteq_str("a-test", dev->name);
191
192 ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
193 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a));
194 ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b));
195 ut_asserteq_str("base-gpios", gpio_a->name);
196 ut_asserteq(true, !!device_active(gpio_a));
197 ut_asserteq_ptr(gpio_a, desc.dev);
198 ut_asserteq(4, desc.offset);
199 /* GPIOF_INPUT is the sandbox GPIO driver default */
200 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL));
201 ut_assertok(dm_gpio_free(dev, &desc));
202
203 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc,
204 0));
205 ut_asserteq_ptr(NULL, desc.dev);
206 ut_asserteq(desc.offset, 0);
207 ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc,
208 0));
209
210 /* Last GPIO is ignord as it comes after <0> */
211 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
212 ARRAY_SIZE(desc_list), 0));
213 ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios",
214 desc_list2,
215 ARRAY_SIZE(desc_list2),
216 0));
217 ut_assertok(gpio_free_list(dev, desc_list, 3));
218 ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list,
219 ARRAY_SIZE(desc_list),
220 GPIOD_IS_OUT |
221 GPIOD_IS_OUT_ACTIVE));
222 ut_asserteq_ptr(gpio_a, desc_list[0].dev);
223 ut_asserteq(1, desc_list[0].offset);
224 ut_asserteq_ptr(gpio_a, desc_list[1].dev);
225 ut_asserteq(4, desc_list[1].offset);
226 ut_asserteq_ptr(gpio_b, desc_list[2].dev);
227 ut_asserteq(5, desc_list[2].offset);
228 ut_asserteq(1, dm_gpio_get_value(desc_list));
229 ut_assertok(gpio_free_list(dev, desc_list, 3));
230
231 ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list,
232 ARRAY_SIZE(desc_list), 0));
233 /* This was set to output previously, so still will be */
234 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 1, NULL));
235
236 /* Active low should invert the input value */
237 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL));
238 ut_asserteq(1, dm_gpio_get_value(&desc_list[2]));
239
240 ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL));
241 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL));
242 ut_asserteq(0, dm_gpio_get_value(&desc_list[4]));
243 ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL));
244 ut_asserteq(1, dm_gpio_get_value(&desc_list[5]));
245
246
247 return 0;
248}
249DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);