Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Google, Inc |
Simon Glass | e821d18 | 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 | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 10 | #include <dm/root.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 11 | #include <dm/test.h> |
| 12 | #include <dm/util.h> |
| 13 | #include <asm/gpio.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | #include <test/ut.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 15 | |
| 16 | /* Test that sandbox GPIOs work correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | static int dm_test_gpio(struct unit_test_state *uts) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 18 | { |
| 19 | unsigned int offset, gpio; |
| 20 | struct dm_gpio_ops *ops; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 21 | struct udevice *dev; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 22 | const char *name; |
| 23 | int offset_count; |
| 24 | char buf[80]; |
| 25 | |
| 26 | /* |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 27 | * 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 Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 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 Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 43 | ut_assert(ops->get_function); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 44 | |
| 45 | /* Cannot get a value until it is reserved */ |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 46 | ut_asserteq(-EBUSY, gpio_get_value(gpio + 1)); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 47 | /* |
| 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 Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 51 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 52 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 53 | |
| 54 | /* Change it to an output */ |
| 55 | sandbox_gpio_set_direction(dev, offset, 1); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 56 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 57 | ut_asserteq_str("b4: output: 0 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 58 | |
| 59 | sandbox_gpio_set_value(dev, offset, 1); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 60 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 61 | ut_asserteq_str("b4: output: 1 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 62 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 63 | 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 Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 66 | |
| 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 Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 71 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 72 | ut_asserteq_str("b4: output: 0 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 73 | ut_assertok(ops->set_value(dev, offset, 1)); |
| 74 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 75 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 76 | /* 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 Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 88 | /* Make it an input */ |
| 89 | ut_assertok(ops->direction_input(dev, offset)); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 90 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 91 | ut_asserteq_str("b4: input: 1 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 92 | sandbox_gpio_set_value(dev, offset, 0); |
| 93 | ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 94 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 95 | ut_asserteq_str("b4: input: 0 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 96 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 97 | 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 Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 100 | |
| 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 Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 114 | |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame^] | 115 | /* Test that GPIO open-drain/open-source emulation works correctly */ |
| 116 | static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts) |
| 117 | { |
| 118 | struct gpio_desc desc_list[8]; |
| 119 | struct udevice *dev, *gpio_c; |
| 120 | char buf[80]; |
| 121 | |
| 122 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 123 | ut_asserteq_str("a-test", dev->name); |
| 124 | |
| 125 | ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c)); |
| 126 | ut_asserteq_str("pinmux-gpios", gpio_c->name); |
| 127 | |
| 128 | ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list, |
| 129 | ARRAY_SIZE(desc_list), 0)) |
| 130 | |
| 131 | ut_asserteq(true, !!device_active(gpio_c)); |
| 132 | ut_asserteq_ptr(gpio_c, desc_list[0].dev); |
| 133 | ut_asserteq_ptr(gpio_c, desc_list[1].dev); |
| 134 | ut_asserteq_ptr(gpio_c, desc_list[2].dev); |
| 135 | ut_asserteq_ptr(gpio_c, desc_list[3].dev); |
| 136 | ut_asserteq_ptr(gpio_c, desc_list[4].dev); |
| 137 | ut_asserteq_ptr(gpio_c, desc_list[5].dev); |
| 138 | ut_asserteq_ptr(gpio_c, desc_list[6].dev); |
| 139 | ut_asserteq_ptr(gpio_c, desc_list[7].dev); |
| 140 | |
| 141 | /* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */ |
| 142 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
| 143 | sandbox_gpio_get_dir_flags(gpio_c, 0)); |
| 144 | |
| 145 | /* Set it as output high, should become an input */ |
| 146 | ut_assertok(dm_gpio_set_value(&desc_list[0], 1)); |
| 147 | ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf))); |
| 148 | ut_asserteq_str("c0: input: 0 [x] a-test.test3-gpios0", buf); |
| 149 | |
| 150 | /* Set it as output low, should become output low */ |
| 151 | ut_assertok(dm_gpio_set_value(&desc_list[0], 0)); |
| 152 | ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf))); |
| 153 | ut_asserteq_str("c0: output: 0 [x] a-test.test3-gpios0", buf); |
| 154 | |
| 155 | /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */ |
| 156 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 157 | sandbox_gpio_get_dir_flags(gpio_c, 1)); |
| 158 | |
| 159 | /* Set it as output high, should become output high */ |
| 160 | ut_assertok(dm_gpio_set_value(&desc_list[1], 1)); |
| 161 | ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); |
| 162 | ut_asserteq_str("c1: output: 1 [x] a-test.test3-gpios1", buf); |
| 163 | |
| 164 | /* Set it as output low, should become an input */ |
| 165 | ut_assertok(dm_gpio_set_value(&desc_list[1], 0)); |
| 166 | ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); |
| 167 | ut_asserteq_str("c1: input: 1 [x] a-test.test3-gpios1", buf); |
| 168 | |
| 169 | /* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */ |
| 170 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
| 171 | sandbox_gpio_get_dir_flags(gpio_c, 6)); |
| 172 | |
| 173 | /* Set it as output high, should become output low */ |
| 174 | ut_assertok(dm_gpio_set_value(&desc_list[6], 1)); |
| 175 | ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); |
| 176 | ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf); |
| 177 | |
| 178 | /* Set it as output low, should become an input */ |
| 179 | ut_assertok(dm_gpio_set_value(&desc_list[6], 0)); |
| 180 | ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); |
| 181 | ut_asserteq_str("c6: input: 0 [x] a-test.test3-gpios6", buf); |
| 182 | |
| 183 | /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */ |
| 184 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 185 | sandbox_gpio_get_dir_flags(gpio_c, 7)); |
| 186 | |
| 187 | /* Set it as output high, should become an input */ |
| 188 | ut_assertok(dm_gpio_set_value(&desc_list[7], 1)); |
| 189 | ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); |
| 190 | ut_asserteq_str("c7: input: 0 [x] a-test.test3-gpios7", buf); |
| 191 | |
| 192 | /* Set it as output low, should become output high */ |
| 193 | ut_assertok(dm_gpio_set_value(&desc_list[7], 0)); |
| 194 | ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); |
| 195 | ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf); |
| 196 | |
| 197 | ut_assertok(gpio_free_list(dev, desc_list, 8)); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | DM_TEST(dm_test_gpio_opendrain_opensource, |
| 202 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 203 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 204 | /* Test that sandbox anonymous GPIOs work correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 205 | static int dm_test_gpio_anon(struct unit_test_state *uts) |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 206 | { |
| 207 | unsigned int offset, gpio; |
| 208 | struct udevice *dev; |
| 209 | const char *name; |
| 210 | int offset_count; |
| 211 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 212 | /* And the anonymous bank */ |
| 213 | ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); |
| 214 | ut_asserteq_str(dev->name, "gpio_sandbox"); |
| 215 | ut_asserteq(14, offset); |
| 216 | ut_asserteq(14, gpio); |
| 217 | |
| 218 | name = gpio_get_bank_info(dev, &offset_count); |
| 219 | ut_asserteq_ptr(NULL, name); |
| 220 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); |
| 221 | |
| 222 | return 0; |
| 223 | } |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 224 | DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 225 | |
| 226 | /* Test that gpio_requestf() works as expected */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 227 | static int dm_test_gpio_requestf(struct unit_test_state *uts) |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 228 | { |
| 229 | unsigned int offset, gpio; |
| 230 | struct udevice *dev; |
| 231 | char buf[80]; |
| 232 | |
| 233 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 234 | ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi")); |
| 235 | sandbox_gpio_set_direction(dev, offset, 1); |
| 236 | sandbox_gpio_set_value(dev, offset, 1); |
| 237 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 238 | ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 243 | |
| 244 | /* Test that gpio_request() copies its string */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 245 | static int dm_test_gpio_copy(struct unit_test_state *uts) |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 246 | { |
| 247 | unsigned int offset, gpio; |
| 248 | struct udevice *dev; |
| 249 | char buf[80], name[10]; |
| 250 | |
| 251 | ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio)); |
| 252 | strcpy(name, "odd_name"); |
| 253 | ut_assertok(gpio_request(gpio, name)); |
| 254 | sandbox_gpio_set_direction(dev, offset, 1); |
| 255 | sandbox_gpio_set_value(dev, offset, 1); |
| 256 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 257 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 258 | strcpy(name, "nothing"); |
| 259 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 260 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 265 | |
| 266 | /* Test that we don't leak memory with GPIOs */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 267 | static int dm_test_gpio_leak(struct unit_test_state *uts) |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 268 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 269 | ut_assertok(dm_test_gpio(uts)); |
| 270 | ut_assertok(dm_test_gpio_anon(uts)); |
| 271 | ut_assertok(dm_test_gpio_requestf(uts)); |
| 272 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 276 | DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 277 | |
| 278 | /* Test that we can find GPIOs using phandles */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 279 | static int dm_test_gpio_phandles(struct unit_test_state *uts) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 280 | { |
| 281 | struct gpio_desc desc, desc_list[8], desc_list2[8]; |
| 282 | struct udevice *dev, *gpio_a, *gpio_b; |
| 283 | |
| 284 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 285 | ut_asserteq_str("a-test", dev->name); |
| 286 | |
| 287 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 288 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a)); |
| 289 | ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b)); |
| 290 | ut_asserteq_str("base-gpios", gpio_a->name); |
| 291 | ut_asserteq(true, !!device_active(gpio_a)); |
| 292 | ut_asserteq_ptr(gpio_a, desc.dev); |
| 293 | ut_asserteq(4, desc.offset); |
| 294 | /* GPIOF_INPUT is the sandbox GPIO driver default */ |
| 295 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
| 296 | ut_assertok(dm_gpio_free(dev, &desc)); |
| 297 | |
| 298 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc, |
| 299 | 0)); |
| 300 | ut_asserteq_ptr(NULL, desc.dev); |
| 301 | ut_asserteq(desc.offset, 0); |
| 302 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc, |
| 303 | 0)); |
| 304 | |
| 305 | /* Last GPIO is ignord as it comes after <0> */ |
| 306 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 307 | ARRAY_SIZE(desc_list), 0)); |
| 308 | ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios", |
| 309 | desc_list2, |
| 310 | ARRAY_SIZE(desc_list2), |
| 311 | 0)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 312 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 313 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 314 | ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 315 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 316 | ARRAY_SIZE(desc_list), |
| 317 | GPIOD_IS_OUT | |
| 318 | GPIOD_IS_OUT_ACTIVE)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 319 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 320 | ut_asserteq_ptr(gpio_a, desc_list[0].dev); |
| 321 | ut_asserteq(1, desc_list[0].offset); |
| 322 | ut_asserteq_ptr(gpio_a, desc_list[1].dev); |
| 323 | ut_asserteq(4, desc_list[1].offset); |
| 324 | ut_asserteq_ptr(gpio_b, desc_list[2].dev); |
| 325 | ut_asserteq(5, desc_list[2].offset); |
| 326 | ut_asserteq(1, dm_gpio_get_value(desc_list)); |
| 327 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
| 328 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 329 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
| 330 | sandbox_gpio_get_dir_flags(gpio_a, 1)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 331 | ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list, |
| 332 | ARRAY_SIZE(desc_list), 0)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 333 | |
| 334 | /* This was set to output previously but flags resetted to 0 = INPUT */ |
| 335 | ut_asserteq(0, sandbox_gpio_get_dir_flags(gpio_a, 1)); |
| 336 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 337 | |
| 338 | /* Active low should invert the input value */ |
| 339 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL)); |
| 340 | ut_asserteq(1, dm_gpio_get_value(&desc_list[2])); |
| 341 | |
| 342 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL)); |
| 343 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL)); |
| 344 | ut_asserteq(0, dm_gpio_get_value(&desc_list[4])); |
| 345 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL)); |
| 346 | ut_asserteq(1, dm_gpio_get_value(&desc_list[5])); |
| 347 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 351 | |
| 352 | /* Check the gpio pin configuration get from device tree information */ |
| 353 | static int dm_test_gpio_get_dir_flags(struct unit_test_state *uts) |
| 354 | { |
| 355 | struct gpio_desc desc_list[6]; |
| 356 | struct udevice *dev; |
| 357 | ulong flags; |
| 358 | |
| 359 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 360 | |
| 361 | ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list, |
| 362 | ARRAY_SIZE(desc_list), 0)); |
| 363 | |
| 364 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[0], &flags)); |
| 365 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags); |
| 366 | |
| 367 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[1], &flags)); |
| 368 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags); |
| 369 | |
| 370 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[2], &flags)); |
| 371 | ut_asserteq(GPIOD_IS_OUT, flags); |
| 372 | |
| 373 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[3], &flags)); |
| 374 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags); |
| 375 | |
| 376 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[4], &flags)); |
| 377 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags); |
| 378 | |
| 379 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[5], &flags)); |
| 380 | ut_asserteq(GPIOD_IS_IN, flags); |
| 381 | |
| 382 | ut_assertok(gpio_free_list(dev, desc_list, 6)); |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | DM_TEST(dm_test_gpio_get_dir_flags, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |