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 | 2912686 | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 11 | #include <acpi/acpi_device.h> |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 12 | #include <dm/root.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 13 | #include <dm/test.h> |
| 14 | #include <dm/util.h> |
| 15 | #include <asm/gpio.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | #include <test/ut.h> |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 17 | |
| 18 | /* Test that sandbox GPIOs work correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | static int dm_test_gpio(struct unit_test_state *uts) |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 20 | { |
| 21 | unsigned int offset, gpio; |
| 22 | struct dm_gpio_ops *ops; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 23 | struct udevice *dev; |
Heiko Schocher | 9ba8432 | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 24 | struct gpio_desc *desc; |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 25 | const char *name; |
| 26 | int offset_count; |
| 27 | char buf[80]; |
| 28 | |
| 29 | /* |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 30 | * We expect to get 4 banks. One is anonymous (just numbered) and |
| 31 | * comes from platdata. The other are named a (20 gpios), |
| 32 | * 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] | 33 | * test/dm/test.dts. |
| 34 | */ |
| 35 | ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio)); |
| 36 | ut_asserteq_str(dev->name, "extra-gpios"); |
| 37 | ut_asserteq(4, offset); |
| 38 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio); |
| 39 | |
| 40 | name = gpio_get_bank_info(dev, &offset_count); |
| 41 | ut_asserteq_str("b", name); |
| 42 | ut_asserteq(10, offset_count); |
| 43 | |
| 44 | /* Get the operations for this device */ |
| 45 | ops = gpio_get_ops(dev); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 46 | ut_assert(ops->get_function); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 47 | |
| 48 | /* Cannot get a value until it is reserved */ |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 49 | ut_asserteq(-EBUSY, gpio_get_value(gpio + 1)); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Now some tests that use the 'sandbox' back door. All GPIOs |
| 52 | * should default to input, include b4 that we are using here. |
| 53 | */ |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 54 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 55 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 56 | |
| 57 | /* Change it to an output */ |
| 58 | sandbox_gpio_set_direction(dev, offset, 1); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 59 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 60 | ut_asserteq_str("b4: output: 0 [ ]", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 61 | |
| 62 | sandbox_gpio_set_value(dev, offset, 1); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 63 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 64 | ut_asserteq_str("b4: output: 1 [ ]", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 65 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 66 | ut_assertok(gpio_request(gpio, "testing")); |
| 67 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 68 | ut_asserteq_str("b4: output: 1 [x] testing", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 69 | |
| 70 | /* Change the value a bit */ |
| 71 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 72 | ut_assertok(ops->set_value(dev, offset, 0)); |
| 73 | ut_asserteq(0, ops->get_value(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 74 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 75 | ut_asserteq_str("b4: output: 0 [x] testing", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 76 | ut_assertok(ops->set_value(dev, offset, 1)); |
| 77 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 78 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 79 | /* Make it an open drain output, and reset it */ |
| 80 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
| 81 | sandbox_gpio_get_dir_flags(dev, offset)); |
| 82 | ut_assertok(ops->set_dir_flags(dev, offset, |
| 83 | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN)); |
| 84 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
| 85 | sandbox_gpio_get_dir_flags(dev, offset)); |
| 86 | ut_assertok(ops->set_dir_flags(dev, offset, |
| 87 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE)); |
| 88 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
| 89 | sandbox_gpio_get_dir_flags(dev, offset)); |
| 90 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 91 | /* Make it an input */ |
| 92 | ut_assertok(ops->direction_input(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 93 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 94 | ut_asserteq_str("b4: input: 1 [x] testing", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 95 | sandbox_gpio_set_value(dev, offset, 0); |
| 96 | ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 97 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 98 | ut_asserteq_str("b4: input: 0 [x] testing", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 99 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 100 | ut_assertok(gpio_free(gpio)); |
| 101 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 102 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 103 | |
| 104 | /* Check the 'a' bank also */ |
| 105 | ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio)); |
| 106 | ut_asserteq_str(dev->name, "base-gpios"); |
| 107 | ut_asserteq(15, offset); |
| 108 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio); |
| 109 | |
| 110 | name = gpio_get_bank_info(dev, &offset_count); |
| 111 | ut_asserteq_str("a", name); |
| 112 | ut_asserteq(20, offset_count); |
| 113 | |
Heiko Schocher | 9ba8432 | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 114 | /* add gpio hog tests */ |
| 115 | ut_assertok(gpio_hog_lookup_name("hog_input_active_low", &desc)); |
| 116 | ut_asserteq(GPIOD_IS_IN | GPIOD_ACTIVE_LOW, desc->flags); |
| 117 | ut_asserteq(0, desc->offset); |
| 118 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 119 | ut_assertok(gpio_hog_lookup_name("hog_input_active_high", &desc)); |
| 120 | ut_asserteq(GPIOD_IS_IN, desc->flags); |
| 121 | ut_asserteq(1, desc->offset); |
| 122 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 123 | ut_assertok(gpio_hog_lookup_name("hog_output_low", &desc)); |
| 124 | ut_asserteq(GPIOD_IS_OUT, desc->flags); |
| 125 | ut_asserteq(2, desc->offset); |
| 126 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 127 | ut_assertok(dm_gpio_set_value(desc, 1)); |
| 128 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 129 | ut_assertok(gpio_hog_lookup_name("hog_output_high", &desc)); |
| 130 | ut_asserteq(GPIOD_IS_OUT, desc->flags); |
| 131 | ut_asserteq(3, desc->offset); |
| 132 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 133 | ut_assertok(dm_gpio_set_value(desc, 0)); |
| 134 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 135 | |
Heiko Schocher | 2bd261d | 2020-05-22 11:08:59 +0200 | [diff] [blame] | 136 | /* Check if lookup for labels work */ |
| 137 | ut_assertok(gpio_lookup_name("hog_input_active_low", &dev, &offset, |
| 138 | &gpio)); |
| 139 | ut_asserteq_str(dev->name, "base-gpios"); |
| 140 | ut_asserteq(0, offset); |
| 141 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 0, gpio); |
| 142 | ut_assert(gpio_lookup_name("hog_not_exist", &dev, &offset, |
| 143 | &gpio)); |
| 144 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 148 | |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 149 | /* Test that GPIO open-drain/open-source emulation works correctly */ |
| 150 | static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts) |
| 151 | { |
| 152 | struct gpio_desc desc_list[8]; |
| 153 | struct udevice *dev, *gpio_c; |
| 154 | char buf[80]; |
| 155 | |
| 156 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 157 | ut_asserteq_str("a-test", dev->name); |
| 158 | |
| 159 | ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c)); |
| 160 | ut_asserteq_str("pinmux-gpios", gpio_c->name); |
| 161 | |
| 162 | ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list, |
| 163 | ARRAY_SIZE(desc_list), 0)) |
| 164 | |
| 165 | ut_asserteq(true, !!device_active(gpio_c)); |
| 166 | ut_asserteq_ptr(gpio_c, desc_list[0].dev); |
| 167 | ut_asserteq_ptr(gpio_c, desc_list[1].dev); |
| 168 | ut_asserteq_ptr(gpio_c, desc_list[2].dev); |
| 169 | ut_asserteq_ptr(gpio_c, desc_list[3].dev); |
| 170 | ut_asserteq_ptr(gpio_c, desc_list[4].dev); |
| 171 | ut_asserteq_ptr(gpio_c, desc_list[5].dev); |
| 172 | ut_asserteq_ptr(gpio_c, desc_list[6].dev); |
| 173 | ut_asserteq_ptr(gpio_c, desc_list[7].dev); |
| 174 | |
| 175 | /* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */ |
| 176 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
| 177 | sandbox_gpio_get_dir_flags(gpio_c, 0)); |
| 178 | |
| 179 | /* Set it as output high, should become an input */ |
| 180 | ut_assertok(dm_gpio_set_value(&desc_list[0], 1)); |
| 181 | ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf))); |
| 182 | ut_asserteq_str("c0: input: 0 [x] a-test.test3-gpios0", buf); |
| 183 | |
| 184 | /* Set it as output low, should become output low */ |
| 185 | ut_assertok(dm_gpio_set_value(&desc_list[0], 0)); |
| 186 | ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf))); |
| 187 | ut_asserteq_str("c0: output: 0 [x] a-test.test3-gpios0", buf); |
| 188 | |
| 189 | /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */ |
| 190 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 191 | sandbox_gpio_get_dir_flags(gpio_c, 1)); |
| 192 | |
| 193 | /* Set it as output high, should become output high */ |
| 194 | ut_assertok(dm_gpio_set_value(&desc_list[1], 1)); |
| 195 | ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); |
| 196 | ut_asserteq_str("c1: output: 1 [x] a-test.test3-gpios1", buf); |
| 197 | |
| 198 | /* Set it as output low, should become an input */ |
| 199 | ut_assertok(dm_gpio_set_value(&desc_list[1], 0)); |
| 200 | ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); |
| 201 | ut_asserteq_str("c1: input: 1 [x] a-test.test3-gpios1", buf); |
| 202 | |
| 203 | /* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */ |
| 204 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
| 205 | sandbox_gpio_get_dir_flags(gpio_c, 6)); |
| 206 | |
| 207 | /* Set it as output high, should become output low */ |
| 208 | ut_assertok(dm_gpio_set_value(&desc_list[6], 1)); |
| 209 | ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); |
| 210 | ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf); |
| 211 | |
| 212 | /* Set it as output low, should become an input */ |
| 213 | ut_assertok(dm_gpio_set_value(&desc_list[6], 0)); |
| 214 | ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); |
| 215 | ut_asserteq_str("c6: input: 0 [x] a-test.test3-gpios6", buf); |
| 216 | |
| 217 | /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */ |
| 218 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 219 | sandbox_gpio_get_dir_flags(gpio_c, 7)); |
| 220 | |
| 221 | /* Set it as output high, should become an input */ |
| 222 | ut_assertok(dm_gpio_set_value(&desc_list[7], 1)); |
| 223 | ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); |
| 224 | ut_asserteq_str("c7: input: 0 [x] a-test.test3-gpios7", buf); |
| 225 | |
| 226 | /* Set it as output low, should become output high */ |
| 227 | ut_assertok(dm_gpio_set_value(&desc_list[7], 0)); |
| 228 | ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); |
| 229 | ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf); |
| 230 | |
| 231 | ut_assertok(gpio_free_list(dev, desc_list, 8)); |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | DM_TEST(dm_test_gpio_opendrain_opensource, |
| 236 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 237 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 238 | /* Test that sandbox anonymous GPIOs work correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 239 | static int dm_test_gpio_anon(struct unit_test_state *uts) |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 240 | { |
| 241 | unsigned int offset, gpio; |
| 242 | struct udevice *dev; |
| 243 | const char *name; |
| 244 | int offset_count; |
| 245 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 246 | /* And the anonymous bank */ |
| 247 | ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 248 | ut_asserteq_str(dev->name, "sandbox_gpio"); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 249 | ut_asserteq(14, offset); |
| 250 | ut_asserteq(14, gpio); |
| 251 | |
| 252 | name = gpio_get_bank_info(dev, &offset_count); |
| 253 | ut_asserteq_ptr(NULL, name); |
| 254 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); |
| 255 | |
| 256 | return 0; |
| 257 | } |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 258 | 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] | 259 | |
| 260 | /* Test that gpio_requestf() works as expected */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 261 | static int dm_test_gpio_requestf(struct unit_test_state *uts) |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 262 | { |
| 263 | unsigned int offset, gpio; |
| 264 | struct udevice *dev; |
| 265 | char buf[80]; |
| 266 | |
| 267 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 268 | ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi")); |
| 269 | sandbox_gpio_set_direction(dev, offset, 1); |
| 270 | sandbox_gpio_set_value(dev, offset, 1); |
| 271 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 272 | ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | 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] | 277 | |
| 278 | /* Test that gpio_request() copies its string */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 279 | static int dm_test_gpio_copy(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 280 | { |
| 281 | unsigned int offset, gpio; |
| 282 | struct udevice *dev; |
| 283 | char buf[80], name[10]; |
| 284 | |
| 285 | ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio)); |
| 286 | strcpy(name, "odd_name"); |
| 287 | ut_assertok(gpio_request(gpio, name)); |
| 288 | sandbox_gpio_set_direction(dev, offset, 1); |
| 289 | sandbox_gpio_set_value(dev, offset, 1); |
| 290 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 291 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 292 | strcpy(name, "nothing"); |
| 293 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 294 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 299 | |
| 300 | /* Test that we don't leak memory with GPIOs */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 301 | static int dm_test_gpio_leak(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 302 | { |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 303 | ut_assertok(dm_test_gpio(uts)); |
| 304 | ut_assertok(dm_test_gpio_anon(uts)); |
| 305 | ut_assertok(dm_test_gpio_requestf(uts)); |
| 306 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 310 | 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] | 311 | |
| 312 | /* Test that we can find GPIOs using phandles */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 313 | static int dm_test_gpio_phandles(struct unit_test_state *uts) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 314 | { |
| 315 | struct gpio_desc desc, desc_list[8], desc_list2[8]; |
| 316 | struct udevice *dev, *gpio_a, *gpio_b; |
| 317 | |
| 318 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 319 | ut_asserteq_str("a-test", dev->name); |
| 320 | |
| 321 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 322 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a)); |
| 323 | ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b)); |
| 324 | ut_asserteq_str("base-gpios", gpio_a->name); |
| 325 | ut_asserteq(true, !!device_active(gpio_a)); |
| 326 | ut_asserteq_ptr(gpio_a, desc.dev); |
| 327 | ut_asserteq(4, desc.offset); |
| 328 | /* GPIOF_INPUT is the sandbox GPIO driver default */ |
| 329 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
| 330 | ut_assertok(dm_gpio_free(dev, &desc)); |
| 331 | |
| 332 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc, |
| 333 | 0)); |
| 334 | ut_asserteq_ptr(NULL, desc.dev); |
| 335 | ut_asserteq(desc.offset, 0); |
| 336 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc, |
| 337 | 0)); |
| 338 | |
| 339 | /* Last GPIO is ignord as it comes after <0> */ |
| 340 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 341 | ARRAY_SIZE(desc_list), 0)); |
| 342 | ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios", |
| 343 | desc_list2, |
| 344 | ARRAY_SIZE(desc_list2), |
| 345 | 0)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 346 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 347 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 348 | ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 349 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 350 | ARRAY_SIZE(desc_list), |
| 351 | GPIOD_IS_OUT | |
| 352 | GPIOD_IS_OUT_ACTIVE)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 353 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 354 | ut_asserteq_ptr(gpio_a, desc_list[0].dev); |
| 355 | ut_asserteq(1, desc_list[0].offset); |
| 356 | ut_asserteq_ptr(gpio_a, desc_list[1].dev); |
| 357 | ut_asserteq(4, desc_list[1].offset); |
| 358 | ut_asserteq_ptr(gpio_b, desc_list[2].dev); |
| 359 | ut_asserteq(5, desc_list[2].offset); |
| 360 | ut_asserteq(1, dm_gpio_get_value(desc_list)); |
| 361 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
| 362 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 363 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
| 364 | sandbox_gpio_get_dir_flags(gpio_a, 1)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 365 | ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list, |
| 366 | ARRAY_SIZE(desc_list), 0)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 367 | |
| 368 | /* This was set to output previously but flags resetted to 0 = INPUT */ |
| 369 | ut_asserteq(0, sandbox_gpio_get_dir_flags(gpio_a, 1)); |
| 370 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 371 | |
| 372 | /* Active low should invert the input value */ |
| 373 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL)); |
| 374 | ut_asserteq(1, dm_gpio_get_value(&desc_list[2])); |
| 375 | |
| 376 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL)); |
| 377 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL)); |
| 378 | ut_asserteq(0, dm_gpio_get_value(&desc_list[4])); |
| 379 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL)); |
| 380 | ut_asserteq(1, dm_gpio_get_value(&desc_list[5])); |
| 381 | |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | 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] | 385 | |
| 386 | /* Check the gpio pin configuration get from device tree information */ |
| 387 | static int dm_test_gpio_get_dir_flags(struct unit_test_state *uts) |
| 388 | { |
| 389 | struct gpio_desc desc_list[6]; |
| 390 | struct udevice *dev; |
| 391 | ulong flags; |
| 392 | |
| 393 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 394 | |
| 395 | ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list, |
| 396 | ARRAY_SIZE(desc_list), 0)); |
| 397 | |
| 398 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[0], &flags)); |
| 399 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags); |
| 400 | |
| 401 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[1], &flags)); |
| 402 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags); |
| 403 | |
| 404 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[2], &flags)); |
| 405 | ut_asserteq(GPIOD_IS_OUT, flags); |
| 406 | |
| 407 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[3], &flags)); |
| 408 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags); |
| 409 | |
| 410 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[4], &flags)); |
| 411 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags); |
| 412 | |
| 413 | ut_assertok(dm_gpio_get_dir_flags(&desc_list[5], &flags)); |
| 414 | ut_asserteq(GPIOD_IS_IN, flags); |
| 415 | |
| 416 | ut_assertok(gpio_free_list(dev, desc_list, 6)); |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | DM_TEST(dm_test_gpio_get_dir_flags, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 2912686 | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 421 | |
| 422 | /* Test of gpio_get_acpi() */ |
| 423 | static int dm_test_gpio_get_acpi(struct unit_test_state *uts) |
| 424 | { |
| 425 | struct acpi_gpio agpio; |
| 426 | struct udevice *dev; |
| 427 | struct gpio_desc desc; |
| 428 | |
| 429 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 430 | ut_asserteq_str("a-test", dev->name); |
| 431 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 432 | |
| 433 | /* See sb_gpio_get_acpi() */ |
| 434 | ut_assertok(gpio_get_acpi(&desc, &agpio)); |
| 435 | ut_asserteq(1, agpio.pin_count); |
| 436 | ut_asserteq(4, agpio.pins[0]); |
| 437 | ut_asserteq(ACPI_GPIO_TYPE_IO, agpio.type); |
| 438 | ut_asserteq(ACPI_GPIO_PULL_UP, agpio.pull); |
| 439 | ut_asserteq_str("\\_SB.PINC", agpio.resource); |
| 440 | ut_asserteq(0, agpio.interrupt_debounce_timeout); |
| 441 | ut_asserteq(0, agpio.irq.pin); |
| 442 | ut_asserteq(1234, agpio.output_drive_strength); |
| 443 | ut_asserteq(true, agpio.io_shared); |
| 444 | ut_asserteq(ACPI_GPIO_IO_RESTRICT_INPUT, agpio.io_restrict); |
| 445 | ut_asserteq(ACPI_GPIO_ACTIVE_HIGH, agpio.polarity); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | DM_TEST(dm_test_gpio_get_acpi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 450 | |
| 451 | /* Test of gpio_get_acpi() with an interrupt GPIO */ |
| 452 | static int dm_test_gpio_get_acpi_irq(struct unit_test_state *uts) |
| 453 | { |
| 454 | struct acpi_gpio agpio; |
| 455 | struct udevice *dev; |
| 456 | struct gpio_desc desc; |
| 457 | |
| 458 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 459 | ut_asserteq_str("a-test", dev->name); |
| 460 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0)); |
| 461 | |
| 462 | /* See sb_gpio_get_acpi() */ |
| 463 | ut_assertok(gpio_get_acpi(&desc, &agpio)); |
| 464 | ut_asserteq(1, agpio.pin_count); |
| 465 | ut_asserteq(6, agpio.pins[0]); |
| 466 | ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, agpio.type); |
| 467 | ut_asserteq(ACPI_GPIO_PULL_DOWN, agpio.pull); |
| 468 | ut_asserteq_str("\\_SB.PINC", agpio.resource); |
| 469 | ut_asserteq(4321, agpio.interrupt_debounce_timeout); |
| 470 | ut_asserteq(6, agpio.irq.pin); |
| 471 | ut_asserteq(ACPI_IRQ_ACTIVE_BOTH, agpio.irq.polarity); |
| 472 | ut_asserteq(ACPI_IRQ_SHARED, agpio.irq.shared); |
| 473 | ut_asserteq(true, agpio.irq.wake); |
| 474 | ut_asserteq(0, agpio.output_drive_strength); |
| 475 | ut_asserteq(false, agpio.io_shared); |
| 476 | ut_asserteq(0, agpio.io_restrict); |
| 477 | ut_asserteq(ACPI_GPIO_ACTIVE_LOW, agpio.polarity); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | DM_TEST(dm_test_gpio_get_acpi_irq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |