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