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 |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 33 | * comes from plat. The other are named a (20 gpios), |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 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, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 83 | sandbox_gpio_get_flags(dev, offset)); |
Simon Glass | 13979fc | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 84 | ut_assertok(ops->set_flags(dev, offset, |
| 85 | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 86 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 87 | sandbox_gpio_get_flags(dev, offset)); |
Simon Glass | 13979fc | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 88 | ut_assertok(ops->set_flags(dev, offset, |
| 89 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 90 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 91 | sandbox_gpio_get_flags(dev, offset)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 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, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 179 | sandbox_gpio_get_flags(gpio_c, 0)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 180 | |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 181 | /* Set it as output high */ |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 182 | ut_assertok(dm_gpio_set_value(&desc_list[0], 1)); |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 183 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN | GPIOD_IS_OUT_ACTIVE, |
| 184 | sandbox_gpio_get_flags(gpio_c, 0)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 185 | |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 186 | /* Set it as output low */ |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 187 | ut_assertok(dm_gpio_set_value(&desc_list[0], 0)); |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 188 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
| 189 | sandbox_gpio_get_flags(gpio_c, 0)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 190 | |
| 191 | /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */ |
| 192 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 193 | sandbox_gpio_get_flags(gpio_c, 1)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 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 | |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 200 | /* Set it as output low */ |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 201 | ut_assertok(dm_gpio_set_value(&desc_list[1], 0)); |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 202 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 203 | sandbox_gpio_get_flags(gpio_c, 1)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 204 | |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 205 | ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); |
| 206 | ut_asserteq_str("c1: output: 0 [x] a-test.test3-gpios1", buf); |
| 207 | |
| 208 | /* |
| 209 | * GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN). Looking at it |
| 210 | * directlt from the driver, we get GPIOD_IS_OUT_ACTIVE also, since it |
| 211 | * is active low |
| 212 | */ |
| 213 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN | |
| 214 | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 215 | sandbox_gpio_get_flags(gpio_c, 6)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 216 | |
| 217 | /* Set it as output high, should become output low */ |
| 218 | ut_assertok(dm_gpio_set_value(&desc_list[6], 1)); |
| 219 | ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); |
| 220 | ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf); |
| 221 | |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 222 | /* Set it as output low */ |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 223 | ut_assertok(dm_gpio_set_value(&desc_list[6], 0)); |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 224 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN | |
| 225 | GPIOD_IS_OUT_ACTIVE, |
| 226 | sandbox_gpio_get_flags(gpio_c, 6)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 227 | |
| 228 | /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */ |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 229 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE | |
| 230 | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 231 | sandbox_gpio_get_flags(gpio_c, 7)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 232 | |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 233 | /* Set it as output high */ |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 234 | ut_assertok(dm_gpio_set_value(&desc_list[7], 1)); |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 235 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 236 | sandbox_gpio_get_flags(gpio_c, 7)); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 237 | |
| 238 | /* Set it as output low, should become output high */ |
| 239 | ut_assertok(dm_gpio_set_value(&desc_list[7], 0)); |
| 240 | ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); |
| 241 | ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf); |
| 242 | |
| 243 | ut_assertok(gpio_free_list(dev, desc_list, 8)); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | DM_TEST(dm_test_gpio_opendrain_opensource, |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 248 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Neil Armstrong | 9bf87e2 | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 249 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 250 | /* Test that sandbox anonymous GPIOs work correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 251 | static int dm_test_gpio_anon(struct unit_test_state *uts) |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 252 | { |
| 253 | unsigned int offset, gpio; |
| 254 | struct udevice *dev; |
| 255 | const char *name; |
| 256 | int offset_count; |
| 257 | |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 258 | /* And the anonymous bank */ |
| 259 | ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 260 | ut_asserteq_str(dev->name, "sandbox_gpio"); |
Simon Glass | 96495d9 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 261 | ut_asserteq(14, offset); |
| 262 | ut_asserteq(14, gpio); |
| 263 | |
| 264 | name = gpio_get_bank_info(dev, &offset_count); |
| 265 | ut_asserteq_ptr(NULL, name); |
| 266 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); |
| 267 | |
| 268 | return 0; |
| 269 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 270 | 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] | 271 | |
| 272 | /* Test that gpio_requestf() works as expected */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 273 | static int dm_test_gpio_requestf(struct unit_test_state *uts) |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 274 | { |
| 275 | unsigned int offset, gpio; |
| 276 | struct udevice *dev; |
| 277 | char buf[80]; |
| 278 | |
| 279 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 280 | ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi")); |
| 281 | sandbox_gpio_set_direction(dev, offset, 1); |
| 282 | sandbox_gpio_set_value(dev, offset, 1); |
| 283 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 284 | ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf); |
| 285 | |
| 286 | return 0; |
| 287 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 288 | 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] | 289 | |
| 290 | /* Test that gpio_request() copies its string */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 291 | static int dm_test_gpio_copy(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 292 | { |
| 293 | unsigned int offset, gpio; |
| 294 | struct udevice *dev; |
| 295 | char buf[80], name[10]; |
| 296 | |
| 297 | ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio)); |
| 298 | strcpy(name, "odd_name"); |
| 299 | ut_assertok(gpio_request(gpio, name)); |
| 300 | sandbox_gpio_set_direction(dev, offset, 1); |
| 301 | sandbox_gpio_set_value(dev, offset, 1); |
| 302 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 303 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 304 | strcpy(name, "nothing"); |
| 305 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 306 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 307 | |
| 308 | return 0; |
| 309 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 310 | 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] | 311 | |
| 312 | /* Test that we don't leak memory with GPIOs */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 313 | static int dm_test_gpio_leak(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 314 | { |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 315 | ut_assertok(dm_test_gpio(uts)); |
| 316 | ut_assertok(dm_test_gpio_anon(uts)); |
| 317 | ut_assertok(dm_test_gpio_requestf(uts)); |
| 318 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 322 | 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] | 323 | |
| 324 | /* Test that we can find GPIOs using phandles */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 325 | static int dm_test_gpio_phandles(struct unit_test_state *uts) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 326 | { |
| 327 | struct gpio_desc desc, desc_list[8], desc_list2[8]; |
| 328 | struct udevice *dev, *gpio_a, *gpio_b; |
| 329 | |
| 330 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 331 | ut_asserteq_str("a-test", dev->name); |
| 332 | |
| 333 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 334 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a)); |
| 335 | ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b)); |
| 336 | ut_asserteq_str("base-gpios", gpio_a->name); |
| 337 | ut_asserteq(true, !!device_active(gpio_a)); |
| 338 | ut_asserteq_ptr(gpio_a, desc.dev); |
| 339 | ut_asserteq(4, desc.offset); |
| 340 | /* GPIOF_INPUT is the sandbox GPIO driver default */ |
| 341 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
| 342 | ut_assertok(dm_gpio_free(dev, &desc)); |
| 343 | |
| 344 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc, |
| 345 | 0)); |
| 346 | ut_asserteq_ptr(NULL, desc.dev); |
| 347 | ut_asserteq(desc.offset, 0); |
| 348 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc, |
| 349 | 0)); |
| 350 | |
Yuepeng Xing | 7943ae2 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 351 | /* Last GPIO is ignored as it comes after <0> */ |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 352 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 353 | ARRAY_SIZE(desc_list), 0)); |
| 354 | ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios", |
| 355 | desc_list2, |
| 356 | ARRAY_SIZE(desc_list2), |
| 357 | 0)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 358 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 359 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 360 | ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 361 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 362 | ARRAY_SIZE(desc_list), |
| 363 | GPIOD_IS_OUT | |
| 364 | GPIOD_IS_OUT_ACTIVE)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 365 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 366 | ut_asserteq_ptr(gpio_a, desc_list[0].dev); |
| 367 | ut_asserteq(1, desc_list[0].offset); |
| 368 | ut_asserteq_ptr(gpio_a, desc_list[1].dev); |
| 369 | ut_asserteq(4, desc_list[1].offset); |
| 370 | ut_asserteq_ptr(gpio_b, desc_list[2].dev); |
| 371 | ut_asserteq(5, desc_list[2].offset); |
| 372 | ut_asserteq(1, dm_gpio_get_value(desc_list)); |
| 373 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
| 374 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 375 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 376 | sandbox_gpio_get_flags(gpio_a, 1)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 377 | ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list, |
| 378 | ARRAY_SIZE(desc_list), 0)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 379 | |
Yuepeng Xing | 7943ae2 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 380 | /* This was set to output previously but flags reset to 0 = INPUT */ |
Simon Glass | a03a0aa | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 381 | ut_asserteq(0, sandbox_gpio_get_flags(gpio_a, 1)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 382 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL)); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 383 | |
| 384 | /* Active low should invert the input value */ |
| 385 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL)); |
| 386 | ut_asserteq(1, dm_gpio_get_value(&desc_list[2])); |
| 387 | |
| 388 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL)); |
| 389 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL)); |
| 390 | ut_asserteq(0, dm_gpio_get_value(&desc_list[4])); |
| 391 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL)); |
| 392 | ut_asserteq(1, dm_gpio_get_value(&desc_list[5])); |
| 393 | |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 394 | return 0; |
| 395 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 396 | 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] | 397 | |
| 398 | /* Check the gpio pin configuration get from device tree information */ |
| 399 | static int dm_test_gpio_get_dir_flags(struct unit_test_state *uts) |
| 400 | { |
| 401 | struct gpio_desc desc_list[6]; |
| 402 | struct udevice *dev; |
| 403 | ulong flags; |
| 404 | |
| 405 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 406 | |
| 407 | ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list, |
| 408 | ARRAY_SIZE(desc_list), 0)); |
| 409 | |
Simon Glass | c0c1e62 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 410 | ut_assertok(dm_gpio_get_flags(&desc_list[0], &flags)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 411 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags); |
| 412 | |
Simon Glass | c0c1e62 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 413 | ut_assertok(dm_gpio_get_flags(&desc_list[1], &flags)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 414 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags); |
| 415 | |
Simon Glass | c0c1e62 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 416 | ut_assertok(dm_gpio_get_flags(&desc_list[2], &flags)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 417 | ut_asserteq(GPIOD_IS_OUT, flags); |
| 418 | |
Simon Glass | c0c1e62 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 419 | ut_assertok(dm_gpio_get_flags(&desc_list[3], &flags)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 420 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags); |
| 421 | |
Simon Glass | c0c1e62 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 422 | ut_assertok(dm_gpio_get_flags(&desc_list[4], &flags)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 423 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags); |
| 424 | |
Simon Glass | c0c1e62 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 425 | ut_assertok(dm_gpio_get_flags(&desc_list[5], &flags)); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 426 | ut_asserteq(GPIOD_IS_IN, flags); |
| 427 | |
| 428 | ut_assertok(gpio_free_list(dev, desc_list, 6)); |
| 429 | |
| 430 | return 0; |
| 431 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 432 | 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] | 433 | |
| 434 | /* Test of gpio_get_acpi() */ |
| 435 | static int dm_test_gpio_get_acpi(struct unit_test_state *uts) |
| 436 | { |
| 437 | struct acpi_gpio agpio; |
| 438 | struct udevice *dev; |
| 439 | struct gpio_desc desc; |
| 440 | |
| 441 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 442 | ut_asserteq_str("a-test", dev->name); |
| 443 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 444 | |
| 445 | /* See sb_gpio_get_acpi() */ |
| 446 | ut_assertok(gpio_get_acpi(&desc, &agpio)); |
| 447 | ut_asserteq(1, agpio.pin_count); |
| 448 | ut_asserteq(4, agpio.pins[0]); |
| 449 | ut_asserteq(ACPI_GPIO_TYPE_IO, agpio.type); |
| 450 | ut_asserteq(ACPI_GPIO_PULL_UP, agpio.pull); |
| 451 | ut_asserteq_str("\\_SB.PINC", agpio.resource); |
| 452 | ut_asserteq(0, agpio.interrupt_debounce_timeout); |
| 453 | ut_asserteq(0, agpio.irq.pin); |
| 454 | ut_asserteq(1234, agpio.output_drive_strength); |
| 455 | ut_asserteq(true, agpio.io_shared); |
| 456 | ut_asserteq(ACPI_GPIO_IO_RESTRICT_INPUT, agpio.io_restrict); |
| 457 | ut_asserteq(ACPI_GPIO_ACTIVE_HIGH, agpio.polarity); |
| 458 | |
| 459 | return 0; |
| 460 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 461 | 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] | 462 | |
| 463 | /* Test of gpio_get_acpi() with an interrupt GPIO */ |
| 464 | static int dm_test_gpio_get_acpi_irq(struct unit_test_state *uts) |
| 465 | { |
| 466 | struct acpi_gpio agpio; |
| 467 | struct udevice *dev; |
| 468 | struct gpio_desc desc; |
| 469 | |
| 470 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 471 | ut_asserteq_str("a-test", dev->name); |
| 472 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0)); |
| 473 | |
| 474 | /* See sb_gpio_get_acpi() */ |
| 475 | ut_assertok(gpio_get_acpi(&desc, &agpio)); |
| 476 | ut_asserteq(1, agpio.pin_count); |
| 477 | ut_asserteq(6, agpio.pins[0]); |
| 478 | ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, agpio.type); |
| 479 | ut_asserteq(ACPI_GPIO_PULL_DOWN, agpio.pull); |
| 480 | ut_asserteq_str("\\_SB.PINC", agpio.resource); |
| 481 | ut_asserteq(4321, agpio.interrupt_debounce_timeout); |
| 482 | ut_asserteq(6, agpio.irq.pin); |
| 483 | ut_asserteq(ACPI_IRQ_ACTIVE_BOTH, agpio.irq.polarity); |
| 484 | ut_asserteq(ACPI_IRQ_SHARED, agpio.irq.shared); |
| 485 | ut_asserteq(true, agpio.irq.wake); |
| 486 | ut_asserteq(0, agpio.output_drive_strength); |
| 487 | ut_asserteq(false, agpio.io_shared); |
| 488 | ut_asserteq(0, agpio.io_restrict); |
| 489 | ut_asserteq(ACPI_GPIO_ACTIVE_LOW, agpio.polarity); |
| 490 | |
| 491 | return 0; |
| 492 | } |
Simon Glass | e180c2b | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 493 | 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] | 494 | |
| 495 | /* Test that we can get/release GPIOs using managed API */ |
| 496 | static int dm_test_gpio_devm(struct unit_test_state *uts) |
| 497 | { |
| 498 | static const u32 flags = GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE; |
| 499 | struct gpio_desc *desc1, *desc2, *desc3, *desc_err; |
| 500 | struct udevice *dev; |
| 501 | struct udevice *dev2; |
| 502 | |
| 503 | ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test", |
| 504 | &dev)); |
| 505 | ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "another-test", |
| 506 | &dev2)); |
| 507 | |
| 508 | /* Get 3 GPIOs from 'a-test' dev */ |
| 509 | desc1 = devm_gpiod_get_index(dev, "test4", 0, flags); |
| 510 | ut_assert(!IS_ERR(desc1)); |
| 511 | desc2 = devm_gpiod_get_index(dev, "test4", 1, flags); |
| 512 | ut_assert(!IS_ERR(desc2)); |
| 513 | desc3 = devm_gpiod_get_index_optional(dev, "test5", 0, flags); |
| 514 | ut_assert(!IS_ERR(desc3)); |
| 515 | ut_assert(desc3); |
| 516 | |
| 517 | /* |
| 518 | * Try get the same 3 GPIOs from 'a-test' and 'another-test' devices. |
| 519 | * check that it fails |
| 520 | */ |
| 521 | desc_err = devm_gpiod_get_index(dev, "test4", 0, flags); |
| 522 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 523 | desc_err = devm_gpiod_get_index(dev2, "test4", 0, flags); |
| 524 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 525 | desc_err = devm_gpiod_get_index(dev, "test4", 1, flags); |
| 526 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 527 | desc_err = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 528 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 529 | desc_err = devm_gpiod_get_index_optional(dev, "test5", 0, flags); |
| 530 | ut_asserteq_ptr(NULL, desc_err); |
| 531 | desc_err = devm_gpiod_get_index_optional(dev2, "test5", 0, flags); |
| 532 | ut_asserteq_ptr(NULL, desc_err); |
| 533 | |
| 534 | /* Try get GPIOs outside of the list */ |
| 535 | desc_err = devm_gpiod_get_index(dev, "test4", 2, flags); |
| 536 | ut_assert(IS_ERR(desc_err)); |
| 537 | desc_err = devm_gpiod_get_index_optional(dev, "test5", 1, flags); |
| 538 | ut_asserteq_ptr(NULL, desc_err); |
| 539 | |
| 540 | /* Manipulate the GPIOs */ |
| 541 | ut_assertok(dm_gpio_set_value(desc1, 1)); |
| 542 | ut_asserteq(1, dm_gpio_get_value(desc1)); |
| 543 | ut_assertok(dm_gpio_set_value(desc1, 0)); |
| 544 | ut_asserteq(0, dm_gpio_get_value(desc1)); |
| 545 | |
| 546 | ut_assertok(dm_gpio_set_value(desc2, 1)); |
| 547 | ut_asserteq(1, dm_gpio_get_value(desc2)); |
| 548 | ut_assertok(dm_gpio_set_value(desc2, 0)); |
| 549 | ut_asserteq(0, dm_gpio_get_value(desc2)); |
| 550 | |
| 551 | ut_assertok(dm_gpio_set_value(desc3, 1)); |
| 552 | ut_asserteq(1, dm_gpio_get_value(desc3)); |
| 553 | ut_assertok(dm_gpio_set_value(desc3, 0)); |
| 554 | ut_asserteq(0, dm_gpio_get_value(desc3)); |
| 555 | |
| 556 | /* Check that the GPIO cannot be owned by more than one device */ |
| 557 | desc_err = devm_gpiod_get_index(dev2, "test4", 0, flags); |
| 558 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 559 | desc_err = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 560 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 561 | desc_err = devm_gpiod_get_index_optional(dev2, "test5", 0, flags); |
| 562 | ut_asserteq_ptr(NULL, desc_err); |
| 563 | |
| 564 | /* |
| 565 | * Release one GPIO and check that we can get it back using |
| 566 | * 'another-test' and then 'a-test' |
| 567 | */ |
| 568 | devm_gpiod_put(dev, desc2); |
| 569 | desc2 = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 570 | ut_assert(!IS_ERR(desc2)); |
| 571 | |
| 572 | devm_gpiod_put(dev2, desc2); |
| 573 | desc2 = devm_gpiod_get_index(dev, "test4", 1, flags); |
| 574 | ut_assert(!IS_ERR(desc2)); |
| 575 | |
| 576 | /* Release one GPIO before removing the 'a-test' dev. */ |
| 577 | devm_gpiod_put(dev, desc2); |
| 578 | device_remove(dev, DM_REMOVE_NORMAL); |
| 579 | |
| 580 | /* All the GPIOs must have been freed. We should be able to claim |
| 581 | * them with the 'another-test' device. |
| 582 | */ |
| 583 | desc1 = devm_gpiod_get_index(dev2, "test4", 0, flags); |
| 584 | ut_assert(!IS_ERR(desc1)); |
| 585 | desc2 = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 586 | ut_assert(!IS_ERR(desc2)); |
| 587 | desc3 = devm_gpiod_get_index_optional(dev2, "test5", 0, flags); |
| 588 | ut_assert(!IS_ERR(desc3)); |
| 589 | ut_assert(desc3); |
| 590 | |
| 591 | device_remove(dev2, DM_REMOVE_NORMAL); |
| 592 | return 0; |
| 593 | } |
| 594 | DM_TEST(dm_test_gpio_devm, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 7e0a96d | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 595 | |
| 596 | static int dm_test_clrset_flags(struct unit_test_state *uts) |
| 597 | { |
| 598 | struct gpio_desc desc; |
| 599 | struct udevice *dev; |
| 600 | ulong flags; |
| 601 | |
| 602 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 603 | ut_asserteq_str("a-test", dev->name); |
| 604 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 605 | |
| 606 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_OUT)); |
| 607 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 608 | ut_asserteq(GPIOD_IS_OUT, flags); |
| 609 | ut_asserteq(GPIOD_IS_OUT, desc.flags); |
| 610 | ut_asserteq(0, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 611 | |
| 612 | ut_assertok(dm_gpio_clrset_flags(&desc, 0, GPIOD_IS_OUT_ACTIVE)); |
| 613 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 614 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, flags); |
| 615 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, desc.flags); |
| 616 | ut_asserteq(1, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 617 | ut_asserteq(1, dm_gpio_get_value(&desc)); |
| 618 | |
| 619 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN)); |
| 620 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 621 | ut_asserteq(GPIOD_IS_IN, flags & GPIOD_MASK_DIR); |
| 622 | ut_asserteq(GPIOD_IS_IN, desc.flags & GPIOD_MASK_DIR); |
| 623 | |
| 624 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_MASK_PULL, |
| 625 | GPIOD_PULL_UP)); |
| 626 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 627 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags); |
| 628 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, desc.flags); |
| 629 | |
| 630 | /* Check we cannot set both PULL_UP and PULL_DOWN */ |
| 631 | ut_asserteq(-EINVAL, dm_gpio_clrset_flags(&desc, 0, GPIOD_PULL_DOWN)); |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | DM_TEST(dm_test_clrset_flags, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 636 | |
| 637 | /* Check that an active-low GPIO works as expected */ |
| 638 | static int dm_test_clrset_flags_invert(struct unit_test_state *uts) |
| 639 | { |
| 640 | struct gpio_desc desc; |
| 641 | struct udevice *dev; |
| 642 | ulong flags; |
| 643 | |
| 644 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 645 | ut_asserteq_str("a-test", dev->name); |
| 646 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, |
| 647 | GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)); |
| 648 | |
| 649 | /* |
| 650 | * From this size we see it as 0 (active low), but the sandbox driver |
| 651 | * sees the pin value high |
| 652 | */ |
| 653 | ut_asserteq(0, dm_gpio_get_value(&desc)); |
| 654 | ut_asserteq(1, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 655 | |
| 656 | ut_assertok(dm_gpio_set_value(&desc, 1)); |
| 657 | ut_asserteq(1, dm_gpio_get_value(&desc)); |
| 658 | ut_asserteq(0, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 659 | |
| 660 | /* Do the same with dm_gpio_clrset_flags() */ |
| 661 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_IS_OUT_ACTIVE, 0)); |
| 662 | ut_asserteq(0, dm_gpio_get_value(&desc)); |
| 663 | ut_asserteq(1, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 664 | |
| 665 | ut_assertok(dm_gpio_clrset_flags(&desc, 0, GPIOD_IS_OUT_ACTIVE)); |
| 666 | ut_asserteq(1, dm_gpio_get_value(&desc)); |
| 667 | ut_asserteq(0, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 668 | |
| 669 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 670 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE, |
| 671 | flags); |
| 672 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE, |
| 673 | desc.flags); |
| 674 | |
| 675 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_IS_OUT_ACTIVE, 0)); |
| 676 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 677 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW, flags); |
| 678 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW, desc.flags); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | DM_TEST(dm_test_clrset_flags_invert, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 8a45b22 | 2021-02-04 21:22:09 -0700 | [diff] [blame] | 683 | |
| 684 | static int set_gpios(struct unit_test_state *uts, struct gpio_desc *desc, |
| 685 | int count, uint value) |
| 686 | { |
| 687 | int i; |
| 688 | |
| 689 | for (i = 0; i < count; i++) { |
| 690 | const uint mask = 1 << i; |
| 691 | |
| 692 | ut_assertok(sandbox_gpio_set_value(desc[i].dev, desc[i].offset, |
| 693 | value & mask)); |
| 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* Check that an active-low GPIO works as expected */ |
| 700 | static int dm_test_gpio_get_values_as_int(struct unit_test_state *uts) |
| 701 | { |
| 702 | const int gpio_count = 3; |
| 703 | struct gpio_desc desc[gpio_count]; |
| 704 | struct udevice *dev; |
| 705 | |
| 706 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 707 | ut_asserteq_str("a-test", dev->name); |
| 708 | |
| 709 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc, |
| 710 | gpio_count, GPIOD_IS_IN)); |
| 711 | ut_assertok(set_gpios(uts, desc, gpio_count, 0)); |
| 712 | ut_asserteq(0, dm_gpio_get_values_as_int(desc, gpio_count)); |
| 713 | |
| 714 | ut_assertok(set_gpios(uts, desc, gpio_count, 5)); |
| 715 | ut_asserteq(5, dm_gpio_get_values_as_int(desc, gpio_count)); |
| 716 | |
| 717 | ut_assertok(set_gpios(uts, desc, gpio_count, 7)); |
| 718 | ut_asserteq(7, dm_gpio_get_values_as_int(desc, gpio_count)); |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | DM_TEST(dm_test_gpio_get_values_as_int, |
| 723 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 724 | |
| 725 | /* Check that an active-low GPIO works as expected */ |
| 726 | static int dm_test_gpio_get_values_as_int_base3(struct unit_test_state *uts) |
| 727 | { |
| 728 | const int gpio_count = 3; |
| 729 | struct gpio_desc desc[gpio_count]; |
| 730 | struct udevice *dev; |
| 731 | |
| 732 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 733 | ut_asserteq_str("a-test", dev->name); |
| 734 | |
| 735 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc, |
| 736 | gpio_count, GPIOD_IS_IN)); |
| 737 | |
| 738 | /* |
| 739 | * First test the sandbox GPIO driver works as expected. The external |
| 740 | * pull resistor should be stronger than the internal one. |
| 741 | */ |
| 742 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, |
| 743 | GPIOD_IS_IN | GPIOD_EXT_PULL_UP | GPIOD_PULL_UP); |
| 744 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 745 | |
| 746 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, GPIOD_IS_IN | |
| 747 | GPIOD_EXT_PULL_DOWN | GPIOD_PULL_UP); |
| 748 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 749 | |
| 750 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, |
| 751 | GPIOD_IS_IN | GPIOD_PULL_UP); |
| 752 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 753 | |
| 754 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, GPIOD_PULL_DOWN); |
| 755 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 756 | |
| 757 | /* |
| 758 | * Set up pins: pull-up (1), pull-down (0) and floating (2). This should |
| 759 | * result in digits 2 0 1, i.e. 2 * 9 + 1 * 3 = 19 |
| 760 | */ |
| 761 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, GPIOD_EXT_PULL_UP); |
| 762 | sandbox_gpio_set_flags(desc[1].dev, desc[1].offset, |
| 763 | GPIOD_EXT_PULL_DOWN); |
| 764 | sandbox_gpio_set_flags(desc[2].dev, desc[2].offset, 0); |
| 765 | ut_asserteq(19, dm_gpio_get_values_as_int_base3(desc, gpio_count)); |
| 766 | |
| 767 | /* |
| 768 | * Set up pins: floating (2), pull-up (1) and pull-down (0). This should |
| 769 | * result in digits 0 1 2, i.e. 1 * 3 + 2 = 5 |
| 770 | */ |
| 771 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, 0); |
| 772 | sandbox_gpio_set_flags(desc[1].dev, desc[1].offset, GPIOD_EXT_PULL_UP); |
| 773 | sandbox_gpio_set_flags(desc[2].dev, desc[2].offset, |
| 774 | GPIOD_EXT_PULL_DOWN); |
| 775 | ut_asserteq(5, dm_gpio_get_values_as_int_base3(desc, gpio_count)); |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | DM_TEST(dm_test_gpio_get_values_as_int_base3, |
| 780 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Patrice Chotard | f6f6816 | 2022-08-30 14:09:14 +0200 | [diff] [blame] | 781 | |
| 782 | /* Check that gpio_get_status return the label of a GPIO configured as GPIOD_AF */ |
| 783 | static int dm_test_gpio_function(struct unit_test_state *uts) |
| 784 | { |
| 785 | struct gpio_desc desc; |
| 786 | struct udevice *dev; |
| 787 | ulong flags; |
| 788 | unsigned int offset, gpio; |
| 789 | char buf[80]; |
| 790 | |
| 791 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 792 | ut_asserteq_str("a-test", dev->name); |
| 793 | |
| 794 | /* request gpio_b 5 */ |
| 795 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 2, &desc, 0)); |
| 796 | /* update gpio_b 5 function to GPIO_AF */ |
| 797 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_IS_AF, GPIOD_IS_AF)); |
| 798 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 799 | ut_asserteq(GPIOD_IS_AF, flags); |
| 800 | /* check using gpio_get_status that label is displayed for a pin with GPIO_AF function */ |
| 801 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 802 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 803 | ut_asserteq_str("b5: func a-test.test-gpios2", buf); |
| 804 | |
| 805 | ut_assertok(dm_gpio_free(dev, &desc)); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | DM_TEST(dm_test_gpio_function, |
| 810 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |