Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Google, Inc |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <fdtdec.h> |
| 8 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 11 | #include <dm/root.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 12 | #include <dm/test.h> |
| 13 | #include <dm/util.h> |
| 14 | #include <asm/gpio.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 15 | #include <test/ut.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 16 | |
| 17 | /* Test that sandbox GPIOs work correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 18 | static int dm_test_gpio(struct unit_test_state *uts) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 19 | { |
| 20 | unsigned int offset, gpio; |
| 21 | struct dm_gpio_ops *ops; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 22 | struct udevice *dev; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 23 | const char *name; |
| 24 | int offset_count; |
| 25 | char buf[80]; |
| 26 | |
| 27 | /* |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 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 |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 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); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 44 | ut_assert(ops->get_function); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 45 | |
| 46 | /* Cannot get a value until it is reserved */ |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 47 | ut_asserteq(-EBUSY, gpio_get_value(gpio + 1)); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 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 | */ |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 52 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 53 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 54 | |
| 55 | /* Change it to an output */ |
| 56 | sandbox_gpio_set_direction(dev, offset, 1); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 57 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 58 | ut_asserteq_str("b4: output: 0 [ ]", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 59 | |
| 60 | sandbox_gpio_set_value(dev, offset, 1); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 61 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 62 | ut_asserteq_str("b4: output: 1 [ ]", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 63 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 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); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 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)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 72 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 73 | ut_asserteq_str("b4: output: 0 [x] testing", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 74 | ut_assertok(ops->set_value(dev, offset, 1)); |
| 75 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 76 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 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 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 89 | /* Make it an input */ |
| 90 | ut_assertok(ops->direction_input(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 91 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 92 | ut_asserteq_str("b4: input: 1 [x] testing", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 93 | sandbox_gpio_set_value(dev, offset, 0); |
| 94 | ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 95 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 96 | ut_asserteq_str("b4: input: 0 [x] testing", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 97 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 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); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 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 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 115 | |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 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 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 205 | /* Test that sandbox anonymous GPIOs work correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 206 | static int dm_test_gpio_anon(struct unit_test_state *uts) |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 207 | { |
| 208 | unsigned int offset, gpio; |
| 209 | struct udevice *dev; |
| 210 | const char *name; |
| 211 | int offset_count; |
| 212 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 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 | } |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 225 | DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 226 | |
| 227 | /* Test that gpio_requestf() works as expected */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 228 | static int dm_test_gpio_requestf(struct unit_test_state *uts) |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 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); |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 244 | |
| 245 | /* Test that gpio_request() copies its string */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 246 | static int dm_test_gpio_copy(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 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 */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 268 | static int dm_test_gpio_leak(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 269 | { |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 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)); |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 277 | DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 278 | |
| 279 | /* Test that we can find GPIOs using phandles */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 280 | static int dm_test_gpio_phandles(struct unit_test_state *uts) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 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)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 313 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 314 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 315 | ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 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)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 320 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 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 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 330 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
| 331 | sandbox_gpio_get_dir_flags(gpio_a, 1)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 332 | ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list, |
| 333 | ARRAY_SIZE(desc_list), 0)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 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)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 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 | |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 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); |