Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Google, Inc |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 6 | #include <fdtdec.h> |
| 7 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 10 | #include <acpi/acpi_device.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 11 | #include <asm/gpio.h> |
Jean-Jacques Hiblot | 7387340 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 13 | #include <dm/root.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 14 | #include <dm/test.h> |
| 15 | #include <dm/util.h> |
Simon Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 16 | #include <test/test.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 17 | #include <test/ut.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 18 | |
| 19 | /* Test that sandbox GPIOs work correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | static int dm_test_gpio(struct unit_test_state *uts) |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 21 | { |
| 22 | unsigned int offset, gpio; |
| 23 | struct dm_gpio_ops *ops; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 24 | struct udevice *dev; |
Heiko Schocher | 4508abf | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 25 | struct gpio_desc *desc; |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 26 | const char *name; |
| 27 | int offset_count; |
| 28 | char buf[80]; |
| 29 | |
| 30 | /* |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 31 | * We expect to get 4 banks. One is anonymous (just numbered) and |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 32 | * comes from plat. The other are named a (20 gpios), |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 33 | * b (10 gpios) and c (10 gpios) and come from the device tree. See |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 34 | * test/dm/test.dts. |
| 35 | */ |
| 36 | ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio)); |
| 37 | ut_asserteq_str(dev->name, "extra-gpios"); |
| 38 | ut_asserteq(4, offset); |
| 39 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio); |
| 40 | |
| 41 | name = gpio_get_bank_info(dev, &offset_count); |
| 42 | ut_asserteq_str("b", name); |
| 43 | ut_asserteq(10, offset_count); |
| 44 | |
| 45 | /* Get the operations for this device */ |
| 46 | ops = gpio_get_ops(dev); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 47 | ut_assert(ops->get_function); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 48 | |
| 49 | /* Cannot get a value until it is reserved */ |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 50 | ut_asserteq(-EBUSY, gpio_get_value(gpio + 1)); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Now some tests that use the 'sandbox' back door. All GPIOs |
| 53 | * should default to input, include b4 that we are using here. |
| 54 | */ |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 55 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 56 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 57 | |
| 58 | /* Change it to an output */ |
| 59 | sandbox_gpio_set_direction(dev, offset, 1); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 60 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 61 | ut_asserteq_str("b4: output: 0 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 62 | |
| 63 | sandbox_gpio_set_value(dev, offset, 1); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 64 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 65 | ut_asserteq_str("b4: output: 1 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 66 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 67 | ut_assertok(gpio_request(gpio, "testing")); |
| 68 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 69 | ut_asserteq_str("b4: output: 1 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 70 | |
| 71 | /* Change the value a bit */ |
| 72 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 73 | ut_assertok(ops->set_value(dev, offset, 0)); |
| 74 | ut_asserteq(0, ops->get_value(dev, offset)); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 75 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 76 | ut_asserteq_str("b4: output: 0 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 77 | ut_assertok(ops->set_value(dev, offset, 1)); |
| 78 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 79 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 80 | /* Make it an open drain output, and reset it */ |
| 81 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 82 | sandbox_gpio_get_flags(dev, offset)); |
Simon Glass | 54befdd | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 83 | ut_assertok(ops->set_flags(dev, offset, |
| 84 | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 85 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 86 | sandbox_gpio_get_flags(dev, offset)); |
Simon Glass | 54befdd | 2021-02-04 21:21:55 -0700 | [diff] [blame] | 87 | ut_assertok(ops->set_flags(dev, offset, |
| 88 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 89 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 90 | sandbox_gpio_get_flags(dev, offset)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 91 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 92 | /* Make it an input */ |
| 93 | ut_assertok(ops->direction_input(dev, offset)); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 94 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 95 | ut_asserteq_str("b4: input: 1 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 96 | sandbox_gpio_set_value(dev, offset, 0); |
| 97 | ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 98 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 99 | ut_asserteq_str("b4: input: 0 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 100 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 101 | ut_assertok(gpio_free(gpio)); |
| 102 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 103 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 104 | |
| 105 | /* Check the 'a' bank also */ |
| 106 | ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio)); |
| 107 | ut_asserteq_str(dev->name, "base-gpios"); |
| 108 | ut_asserteq(15, offset); |
| 109 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio); |
| 110 | |
| 111 | name = gpio_get_bank_info(dev, &offset_count); |
| 112 | ut_asserteq_str("a", name); |
| 113 | ut_asserteq(20, offset_count); |
| 114 | |
Heiko Schocher | 4508abf | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 115 | /* add gpio hog tests */ |
| 116 | ut_assertok(gpio_hog_lookup_name("hog_input_active_low", &desc)); |
| 117 | ut_asserteq(GPIOD_IS_IN | GPIOD_ACTIVE_LOW, desc->flags); |
Philippe Reynes | b25a5b3 | 2020-07-24 15:51:53 +0200 | [diff] [blame] | 118 | ut_asserteq(10, desc->offset); |
Heiko Schocher | 4508abf | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 119 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 120 | ut_assertok(gpio_hog_lookup_name("hog_input_active_high", &desc)); |
| 121 | ut_asserteq(GPIOD_IS_IN, desc->flags); |
Philippe Reynes | b25a5b3 | 2020-07-24 15:51:53 +0200 | [diff] [blame] | 122 | ut_asserteq(11, desc->offset); |
Heiko Schocher | 4508abf | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 123 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 124 | ut_assertok(gpio_hog_lookup_name("hog_output_low", &desc)); |
| 125 | ut_asserteq(GPIOD_IS_OUT, desc->flags); |
Philippe Reynes | b25a5b3 | 2020-07-24 15:51:53 +0200 | [diff] [blame] | 126 | ut_asserteq(12, desc->offset); |
Heiko Schocher | 4508abf | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 127 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 128 | ut_assertok(dm_gpio_set_value(desc, 1)); |
| 129 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 130 | ut_assertok(gpio_hog_lookup_name("hog_output_high", &desc)); |
| 131 | ut_asserteq(GPIOD_IS_OUT, desc->flags); |
Philippe Reynes | b25a5b3 | 2020-07-24 15:51:53 +0200 | [diff] [blame] | 132 | ut_asserteq(13, desc->offset); |
Heiko Schocher | 4508abf | 2020-05-22 11:08:58 +0200 | [diff] [blame] | 133 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 134 | ut_assertok(dm_gpio_set_value(desc, 0)); |
| 135 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 136 | |
Heiko Schocher | a3e793c | 2020-05-22 11:08:59 +0200 | [diff] [blame] | 137 | /* Check if lookup for labels work */ |
| 138 | ut_assertok(gpio_lookup_name("hog_input_active_low", &dev, &offset, |
| 139 | &gpio)); |
| 140 | ut_asserteq_str(dev->name, "base-gpios"); |
Philippe Reynes | b25a5b3 | 2020-07-24 15:51:53 +0200 | [diff] [blame] | 141 | ut_asserteq(10, offset); |
| 142 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 10, gpio); |
Heiko Schocher | a3e793c | 2020-05-22 11:08:59 +0200 | [diff] [blame] | 143 | ut_assert(gpio_lookup_name("hog_not_exist", &dev, &offset, |
| 144 | &gpio)); |
| 145 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 146 | return 0; |
| 147 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 148 | DM_TEST(dm_test_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 149 | |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 150 | /* Test that GPIO open-drain/open-source emulation works correctly */ |
| 151 | static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts) |
| 152 | { |
| 153 | struct gpio_desc desc_list[8]; |
| 154 | struct udevice *dev, *gpio_c; |
| 155 | char buf[80]; |
| 156 | |
| 157 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 158 | ut_asserteq_str("a-test", dev->name); |
| 159 | |
| 160 | ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c)); |
| 161 | ut_asserteq_str("pinmux-gpios", gpio_c->name); |
| 162 | |
| 163 | ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list, |
| 164 | ARRAY_SIZE(desc_list), 0)) |
| 165 | |
| 166 | ut_asserteq(true, !!device_active(gpio_c)); |
| 167 | ut_asserteq_ptr(gpio_c, desc_list[0].dev); |
| 168 | ut_asserteq_ptr(gpio_c, desc_list[1].dev); |
| 169 | ut_asserteq_ptr(gpio_c, desc_list[2].dev); |
| 170 | ut_asserteq_ptr(gpio_c, desc_list[3].dev); |
| 171 | ut_asserteq_ptr(gpio_c, desc_list[4].dev); |
| 172 | ut_asserteq_ptr(gpio_c, desc_list[5].dev); |
| 173 | ut_asserteq_ptr(gpio_c, desc_list[6].dev); |
| 174 | ut_asserteq_ptr(gpio_c, desc_list[7].dev); |
| 175 | |
| 176 | /* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */ |
| 177 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 178 | sandbox_gpio_get_flags(gpio_c, 0)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 179 | |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 180 | /* Set it as output high */ |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 181 | ut_assertok(dm_gpio_set_value(&desc_list[0], 1)); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 182 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN | GPIOD_IS_OUT_ACTIVE, |
| 183 | sandbox_gpio_get_flags(gpio_c, 0)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 184 | |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 185 | /* Set it as output low */ |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 186 | ut_assertok(dm_gpio_set_value(&desc_list[0], 0)); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 187 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, |
| 188 | sandbox_gpio_get_flags(gpio_c, 0)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 189 | |
| 190 | /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */ |
| 191 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 192 | sandbox_gpio_get_flags(gpio_c, 1)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 193 | |
| 194 | /* Set it as output high, should become output high */ |
| 195 | ut_assertok(dm_gpio_set_value(&desc_list[1], 1)); |
| 196 | ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); |
| 197 | ut_asserteq_str("c1: output: 1 [x] a-test.test3-gpios1", buf); |
| 198 | |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 199 | /* Set it as output low */ |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 200 | ut_assertok(dm_gpio_set_value(&desc_list[1], 0)); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 201 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 202 | sandbox_gpio_get_flags(gpio_c, 1)); |
| 203 | |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 204 | ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 205 | ut_asserteq_str("c1: output: 0 [x] a-test.test3-gpios1", buf); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 206 | |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 207 | /* |
| 208 | * GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN). Looking at it |
| 209 | * directlt from the driver, we get GPIOD_IS_OUT_ACTIVE also, since it |
| 210 | * is active low |
| 211 | */ |
| 212 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN | |
| 213 | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 214 | sandbox_gpio_get_flags(gpio_c, 6)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 215 | |
| 216 | /* Set it as output high, should become output low */ |
| 217 | ut_assertok(dm_gpio_set_value(&desc_list[6], 1)); |
| 218 | ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); |
| 219 | ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf); |
| 220 | |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 221 | /* Set it as output low */ |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 222 | ut_assertok(dm_gpio_set_value(&desc_list[6], 0)); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 223 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN | |
| 224 | GPIOD_IS_OUT_ACTIVE, |
| 225 | sandbox_gpio_get_flags(gpio_c, 6)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 226 | |
| 227 | /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */ |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 228 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE | |
| 229 | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 230 | sandbox_gpio_get_flags(gpio_c, 7)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 231 | |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 232 | /* Set it as output high */ |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 233 | ut_assertok(dm_gpio_set_value(&desc_list[7], 1)); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 234 | ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, |
| 235 | sandbox_gpio_get_flags(gpio_c, 7)); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 236 | |
| 237 | /* Set it as output low, should become output high */ |
| 238 | ut_assertok(dm_gpio_set_value(&desc_list[7], 0)); |
| 239 | ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); |
| 240 | ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf); |
| 241 | |
| 242 | ut_assertok(gpio_free_list(dev, desc_list, 8)); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | DM_TEST(dm_test_gpio_opendrain_opensource, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 247 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Neil Armstrong | 643778b | 2020-05-05 10:43:18 +0200 | [diff] [blame] | 248 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 249 | /* Test that sandbox anonymous GPIOs work correctly */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 250 | static int dm_test_gpio_anon(struct unit_test_state *uts) |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 251 | { |
| 252 | unsigned int offset, gpio; |
| 253 | struct udevice *dev; |
| 254 | const char *name; |
| 255 | int offset_count; |
| 256 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 257 | /* And the anonymous bank */ |
| 258 | ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 259 | ut_asserteq_str(dev->name, "sandbox_gpio"); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 260 | ut_asserteq(14, offset); |
| 261 | ut_asserteq(14, gpio); |
| 262 | |
| 263 | name = gpio_get_bank_info(dev, &offset_count); |
| 264 | ut_asserteq_ptr(NULL, name); |
| 265 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); |
| 266 | |
| 267 | return 0; |
| 268 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 269 | DM_TEST(dm_test_gpio_anon, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 270 | |
| 271 | /* Test that gpio_requestf() works as expected */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 272 | static int dm_test_gpio_requestf(struct unit_test_state *uts) |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 273 | { |
| 274 | unsigned int offset, gpio; |
| 275 | struct udevice *dev; |
| 276 | char buf[80]; |
| 277 | |
| 278 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 279 | ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi")); |
| 280 | sandbox_gpio_set_direction(dev, offset, 1); |
| 281 | sandbox_gpio_set_value(dev, offset, 1); |
| 282 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 283 | ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf); |
| 284 | |
| 285 | return 0; |
| 286 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 287 | DM_TEST(dm_test_gpio_requestf, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 288 | |
| 289 | /* Test that gpio_request() copies its string */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 290 | static int dm_test_gpio_copy(struct unit_test_state *uts) |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 291 | { |
| 292 | unsigned int offset, gpio; |
| 293 | struct udevice *dev; |
| 294 | char buf[80], name[10]; |
| 295 | |
| 296 | ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio)); |
| 297 | strcpy(name, "odd_name"); |
| 298 | ut_assertok(gpio_request(gpio, name)); |
| 299 | sandbox_gpio_set_direction(dev, offset, 1); |
| 300 | sandbox_gpio_set_value(dev, offset, 1); |
| 301 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 302 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 303 | strcpy(name, "nothing"); |
| 304 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 305 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 306 | |
| 307 | return 0; |
| 308 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 309 | DM_TEST(dm_test_gpio_copy, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 310 | |
| 311 | /* Test that we don't leak memory with GPIOs */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 312 | static int dm_test_gpio_leak(struct unit_test_state *uts) |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 313 | { |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 314 | ut_assertok(dm_test_gpio(uts)); |
| 315 | ut_assertok(dm_test_gpio_anon(uts)); |
| 316 | ut_assertok(dm_test_gpio_requestf(uts)); |
| 317 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 318 | |
| 319 | return 0; |
| 320 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 321 | DM_TEST(dm_test_gpio_leak, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 322 | |
| 323 | /* Test that we can find GPIOs using phandles */ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 324 | static int dm_test_gpio_phandles(struct unit_test_state *uts) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 325 | { |
| 326 | struct gpio_desc desc, desc_list[8], desc_list2[8]; |
| 327 | struct udevice *dev, *gpio_a, *gpio_b; |
| 328 | |
| 329 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 330 | ut_asserteq_str("a-test", dev->name); |
| 331 | |
| 332 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 333 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a)); |
| 334 | ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b)); |
| 335 | ut_asserteq_str("base-gpios", gpio_a->name); |
| 336 | ut_asserteq(true, !!device_active(gpio_a)); |
| 337 | ut_asserteq_ptr(gpio_a, desc.dev); |
| 338 | ut_asserteq(4, desc.offset); |
| 339 | /* GPIOF_INPUT is the sandbox GPIO driver default */ |
| 340 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
| 341 | ut_assertok(dm_gpio_free(dev, &desc)); |
| 342 | |
| 343 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc, |
| 344 | 0)); |
| 345 | ut_asserteq_ptr(NULL, desc.dev); |
| 346 | ut_asserteq(desc.offset, 0); |
| 347 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc, |
| 348 | 0)); |
| 349 | |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 350 | /* Last GPIO is ignored as it comes after <0> */ |
Simon Glass | 16e1040 | 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), 0)); |
| 353 | ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios", |
| 354 | desc_list2, |
| 355 | ARRAY_SIZE(desc_list2), |
| 356 | 0)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 357 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 358 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 359 | ut_asserteq(GPIOF_UNUSED, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 360 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 361 | ARRAY_SIZE(desc_list), |
| 362 | GPIOD_IS_OUT | |
| 363 | GPIOD_IS_OUT_ACTIVE)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 364 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 4, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 365 | ut_asserteq_ptr(gpio_a, desc_list[0].dev); |
| 366 | ut_asserteq(1, desc_list[0].offset); |
| 367 | ut_asserteq_ptr(gpio_a, desc_list[1].dev); |
| 368 | ut_asserteq(4, desc_list[1].offset); |
| 369 | ut_asserteq_ptr(gpio_b, desc_list[2].dev); |
| 370 | ut_asserteq(5, desc_list[2].offset); |
| 371 | ut_asserteq(1, dm_gpio_get_value(desc_list)); |
| 372 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
| 373 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 374 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 375 | sandbox_gpio_get_flags(gpio_a, 1)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 376 | ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list, |
| 377 | ARRAY_SIZE(desc_list), 0)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 378 | |
Yuepeng Xing | 71cc021 | 2022-12-02 14:23:07 +0800 | [diff] [blame] | 379 | /* This was set to output previously but flags reset to 0 = INPUT */ |
Simon Glass | 3f2d575 | 2021-02-04 21:21:59 -0700 | [diff] [blame] | 380 | ut_asserteq(0, sandbox_gpio_get_flags(gpio_a, 1)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 381 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 1, NULL)); |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 382 | |
| 383 | /* Active low should invert the input value */ |
| 384 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL)); |
| 385 | ut_asserteq(1, dm_gpio_get_value(&desc_list[2])); |
| 386 | |
| 387 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL)); |
| 388 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL)); |
| 389 | ut_asserteq(0, dm_gpio_get_value(&desc_list[4])); |
| 390 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL)); |
| 391 | ut_asserteq(1, dm_gpio_get_value(&desc_list[5])); |
| 392 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 393 | return 0; |
| 394 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 395 | DM_TEST(dm_test_gpio_phandles, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 396 | |
| 397 | /* Check the gpio pin configuration get from device tree information */ |
| 398 | static int dm_test_gpio_get_dir_flags(struct unit_test_state *uts) |
| 399 | { |
| 400 | struct gpio_desc desc_list[6]; |
| 401 | struct udevice *dev; |
| 402 | ulong flags; |
| 403 | |
| 404 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 405 | |
| 406 | ut_asserteq(6, gpio_request_list_by_name(dev, "test3-gpios", desc_list, |
| 407 | ARRAY_SIZE(desc_list), 0)); |
| 408 | |
Simon Glass | 909bee3 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 409 | ut_assertok(dm_gpio_get_flags(&desc_list[0], &flags)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 410 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, flags); |
| 411 | |
Simon Glass | 909bee3 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 412 | ut_assertok(dm_gpio_get_flags(&desc_list[1], &flags)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 413 | ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, flags); |
| 414 | |
Simon Glass | 909bee3 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 415 | ut_assertok(dm_gpio_get_flags(&desc_list[2], &flags)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 416 | ut_asserteq(GPIOD_IS_OUT, flags); |
| 417 | |
Simon Glass | 909bee3 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 418 | ut_assertok(dm_gpio_get_flags(&desc_list[3], &flags)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 419 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags); |
| 420 | |
Simon Glass | 909bee3 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 421 | ut_assertok(dm_gpio_get_flags(&desc_list[4], &flags)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 422 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_DOWN, flags); |
| 423 | |
Simon Glass | 909bee3 | 2021-02-04 21:21:57 -0700 | [diff] [blame] | 424 | ut_assertok(dm_gpio_get_flags(&desc_list[5], &flags)); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 425 | ut_asserteq(GPIOD_IS_IN, flags); |
| 426 | |
| 427 | ut_assertok(gpio_free_list(dev, desc_list, 6)); |
| 428 | |
| 429 | return 0; |
| 430 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 431 | DM_TEST(dm_test_gpio_get_dir_flags, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 432 | |
| 433 | /* Test of gpio_get_acpi() */ |
| 434 | static int dm_test_gpio_get_acpi(struct unit_test_state *uts) |
| 435 | { |
| 436 | struct acpi_gpio agpio; |
| 437 | struct udevice *dev; |
| 438 | struct gpio_desc desc; |
| 439 | |
| 440 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 441 | ut_asserteq_str("a-test", dev->name); |
| 442 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 443 | |
| 444 | /* See sb_gpio_get_acpi() */ |
| 445 | ut_assertok(gpio_get_acpi(&desc, &agpio)); |
| 446 | ut_asserteq(1, agpio.pin_count); |
| 447 | ut_asserteq(4, agpio.pins[0]); |
| 448 | ut_asserteq(ACPI_GPIO_TYPE_IO, agpio.type); |
| 449 | ut_asserteq(ACPI_GPIO_PULL_UP, agpio.pull); |
| 450 | ut_asserteq_str("\\_SB.PINC", agpio.resource); |
| 451 | ut_asserteq(0, agpio.interrupt_debounce_timeout); |
| 452 | ut_asserteq(0, agpio.irq.pin); |
| 453 | ut_asserteq(1234, agpio.output_drive_strength); |
| 454 | ut_asserteq(true, agpio.io_shared); |
| 455 | ut_asserteq(ACPI_GPIO_IO_RESTRICT_INPUT, agpio.io_restrict); |
| 456 | ut_asserteq(ACPI_GPIO_ACTIVE_HIGH, agpio.polarity); |
| 457 | |
| 458 | return 0; |
| 459 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 460 | DM_TEST(dm_test_gpio_get_acpi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 461 | |
| 462 | /* Test of gpio_get_acpi() with an interrupt GPIO */ |
| 463 | static int dm_test_gpio_get_acpi_irq(struct unit_test_state *uts) |
| 464 | { |
| 465 | struct acpi_gpio agpio; |
| 466 | struct udevice *dev; |
| 467 | struct gpio_desc desc; |
| 468 | |
| 469 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 470 | ut_asserteq_str("a-test", dev->name); |
| 471 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0)); |
| 472 | |
| 473 | /* See sb_gpio_get_acpi() */ |
| 474 | ut_assertok(gpio_get_acpi(&desc, &agpio)); |
| 475 | ut_asserteq(1, agpio.pin_count); |
| 476 | ut_asserteq(6, agpio.pins[0]); |
| 477 | ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, agpio.type); |
| 478 | ut_asserteq(ACPI_GPIO_PULL_DOWN, agpio.pull); |
| 479 | ut_asserteq_str("\\_SB.PINC", agpio.resource); |
| 480 | ut_asserteq(4321, agpio.interrupt_debounce_timeout); |
| 481 | ut_asserteq(6, agpio.irq.pin); |
| 482 | ut_asserteq(ACPI_IRQ_ACTIVE_BOTH, agpio.irq.polarity); |
| 483 | ut_asserteq(ACPI_IRQ_SHARED, agpio.irq.shared); |
| 484 | ut_asserteq(true, agpio.irq.wake); |
| 485 | ut_asserteq(0, agpio.output_drive_strength); |
| 486 | ut_asserteq(false, agpio.io_shared); |
| 487 | ut_asserteq(0, agpio.io_restrict); |
| 488 | ut_asserteq(ACPI_GPIO_ACTIVE_LOW, agpio.polarity); |
| 489 | |
| 490 | return 0; |
| 491 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 492 | DM_TEST(dm_test_gpio_get_acpi_irq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Jean-Jacques Hiblot | 7387340 | 2020-09-11 13:43:35 +0530 | [diff] [blame] | 493 | |
| 494 | /* Test that we can get/release GPIOs using managed API */ |
| 495 | static int dm_test_gpio_devm(struct unit_test_state *uts) |
| 496 | { |
| 497 | static const u32 flags = GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE; |
| 498 | struct gpio_desc *desc1, *desc2, *desc3, *desc_err; |
| 499 | struct udevice *dev; |
| 500 | struct udevice *dev2; |
| 501 | |
| 502 | ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test", |
| 503 | &dev)); |
| 504 | ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "another-test", |
| 505 | &dev2)); |
| 506 | |
| 507 | /* Get 3 GPIOs from 'a-test' dev */ |
| 508 | desc1 = devm_gpiod_get_index(dev, "test4", 0, flags); |
| 509 | ut_assert(!IS_ERR(desc1)); |
| 510 | desc2 = devm_gpiod_get_index(dev, "test4", 1, flags); |
| 511 | ut_assert(!IS_ERR(desc2)); |
| 512 | desc3 = devm_gpiod_get_index_optional(dev, "test5", 0, flags); |
| 513 | ut_assert(!IS_ERR(desc3)); |
| 514 | ut_assert(desc3); |
| 515 | |
| 516 | /* |
| 517 | * Try get the same 3 GPIOs from 'a-test' and 'another-test' devices. |
| 518 | * check that it fails |
| 519 | */ |
| 520 | desc_err = devm_gpiod_get_index(dev, "test4", 0, flags); |
| 521 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 522 | desc_err = devm_gpiod_get_index(dev2, "test4", 0, flags); |
| 523 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 524 | desc_err = devm_gpiod_get_index(dev, "test4", 1, flags); |
| 525 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 526 | desc_err = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 527 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 528 | desc_err = devm_gpiod_get_index_optional(dev, "test5", 0, flags); |
| 529 | ut_asserteq_ptr(NULL, desc_err); |
| 530 | desc_err = devm_gpiod_get_index_optional(dev2, "test5", 0, flags); |
| 531 | ut_asserteq_ptr(NULL, desc_err); |
| 532 | |
| 533 | /* Try get GPIOs outside of the list */ |
| 534 | desc_err = devm_gpiod_get_index(dev, "test4", 2, flags); |
| 535 | ut_assert(IS_ERR(desc_err)); |
| 536 | desc_err = devm_gpiod_get_index_optional(dev, "test5", 1, flags); |
| 537 | ut_asserteq_ptr(NULL, desc_err); |
| 538 | |
| 539 | /* Manipulate the GPIOs */ |
| 540 | ut_assertok(dm_gpio_set_value(desc1, 1)); |
| 541 | ut_asserteq(1, dm_gpio_get_value(desc1)); |
| 542 | ut_assertok(dm_gpio_set_value(desc1, 0)); |
| 543 | ut_asserteq(0, dm_gpio_get_value(desc1)); |
| 544 | |
| 545 | ut_assertok(dm_gpio_set_value(desc2, 1)); |
| 546 | ut_asserteq(1, dm_gpio_get_value(desc2)); |
| 547 | ut_assertok(dm_gpio_set_value(desc2, 0)); |
| 548 | ut_asserteq(0, dm_gpio_get_value(desc2)); |
| 549 | |
| 550 | ut_assertok(dm_gpio_set_value(desc3, 1)); |
| 551 | ut_asserteq(1, dm_gpio_get_value(desc3)); |
| 552 | ut_assertok(dm_gpio_set_value(desc3, 0)); |
| 553 | ut_asserteq(0, dm_gpio_get_value(desc3)); |
| 554 | |
| 555 | /* Check that the GPIO cannot be owned by more than one device */ |
| 556 | desc_err = devm_gpiod_get_index(dev2, "test4", 0, flags); |
| 557 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 558 | desc_err = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 559 | ut_asserteq(-EBUSY, PTR_ERR(desc_err)); |
| 560 | desc_err = devm_gpiod_get_index_optional(dev2, "test5", 0, flags); |
| 561 | ut_asserteq_ptr(NULL, desc_err); |
| 562 | |
| 563 | /* |
| 564 | * Release one GPIO and check that we can get it back using |
| 565 | * 'another-test' and then 'a-test' |
| 566 | */ |
| 567 | devm_gpiod_put(dev, desc2); |
| 568 | desc2 = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 569 | ut_assert(!IS_ERR(desc2)); |
| 570 | |
| 571 | devm_gpiod_put(dev2, desc2); |
| 572 | desc2 = devm_gpiod_get_index(dev, "test4", 1, flags); |
| 573 | ut_assert(!IS_ERR(desc2)); |
| 574 | |
| 575 | /* Release one GPIO before removing the 'a-test' dev. */ |
| 576 | devm_gpiod_put(dev, desc2); |
| 577 | device_remove(dev, DM_REMOVE_NORMAL); |
| 578 | |
| 579 | /* All the GPIOs must have been freed. We should be able to claim |
| 580 | * them with the 'another-test' device. |
| 581 | */ |
| 582 | desc1 = devm_gpiod_get_index(dev2, "test4", 0, flags); |
| 583 | ut_assert(!IS_ERR(desc1)); |
| 584 | desc2 = devm_gpiod_get_index(dev2, "test4", 1, flags); |
| 585 | ut_assert(!IS_ERR(desc2)); |
| 586 | desc3 = devm_gpiod_get_index_optional(dev2, "test5", 0, flags); |
| 587 | ut_assert(!IS_ERR(desc3)); |
| 588 | ut_assert(desc3); |
| 589 | |
| 590 | device_remove(dev2, DM_REMOVE_NORMAL); |
| 591 | return 0; |
| 592 | } |
| 593 | DM_TEST(dm_test_gpio_devm, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 7b893f9 | 2021-02-04 21:22:03 -0700 | [diff] [blame] | 594 | |
| 595 | static int dm_test_clrset_flags(struct unit_test_state *uts) |
| 596 | { |
| 597 | struct gpio_desc desc; |
| 598 | struct udevice *dev; |
| 599 | ulong flags; |
| 600 | |
| 601 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 602 | ut_asserteq_str("a-test", dev->name); |
| 603 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 604 | |
| 605 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_OUT)); |
| 606 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 607 | ut_asserteq(GPIOD_IS_OUT, flags); |
| 608 | ut_asserteq(GPIOD_IS_OUT, desc.flags); |
| 609 | ut_asserteq(0, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 610 | |
| 611 | ut_assertok(dm_gpio_clrset_flags(&desc, 0, GPIOD_IS_OUT_ACTIVE)); |
| 612 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 613 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, flags); |
| 614 | ut_asserteq(GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE, desc.flags); |
| 615 | ut_asserteq(1, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 616 | ut_asserteq(1, dm_gpio_get_value(&desc)); |
| 617 | |
| 618 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN)); |
| 619 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 620 | ut_asserteq(GPIOD_IS_IN, flags & GPIOD_MASK_DIR); |
| 621 | ut_asserteq(GPIOD_IS_IN, desc.flags & GPIOD_MASK_DIR); |
| 622 | |
| 623 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_MASK_PULL, |
| 624 | GPIOD_PULL_UP)); |
| 625 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 626 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, flags); |
| 627 | ut_asserteq(GPIOD_IS_IN | GPIOD_PULL_UP, desc.flags); |
| 628 | |
| 629 | /* Check we cannot set both PULL_UP and PULL_DOWN */ |
| 630 | ut_asserteq(-EINVAL, dm_gpio_clrset_flags(&desc, 0, GPIOD_PULL_DOWN)); |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | DM_TEST(dm_test_clrset_flags, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 635 | |
| 636 | /* Check that an active-low GPIO works as expected */ |
| 637 | static int dm_test_clrset_flags_invert(struct unit_test_state *uts) |
| 638 | { |
| 639 | struct gpio_desc desc; |
| 640 | struct udevice *dev; |
| 641 | ulong flags; |
| 642 | |
| 643 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 644 | ut_asserteq_str("a-test", dev->name); |
| 645 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, |
| 646 | GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)); |
| 647 | |
| 648 | /* |
| 649 | * From this size we see it as 0 (active low), but the sandbox driver |
| 650 | * sees the pin value high |
| 651 | */ |
| 652 | ut_asserteq(0, dm_gpio_get_value(&desc)); |
| 653 | ut_asserteq(1, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 654 | |
| 655 | ut_assertok(dm_gpio_set_value(&desc, 1)); |
| 656 | ut_asserteq(1, dm_gpio_get_value(&desc)); |
| 657 | ut_asserteq(0, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 658 | |
| 659 | /* Do the same with dm_gpio_clrset_flags() */ |
| 660 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_IS_OUT_ACTIVE, 0)); |
| 661 | ut_asserteq(0, dm_gpio_get_value(&desc)); |
| 662 | ut_asserteq(1, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 663 | |
| 664 | ut_assertok(dm_gpio_clrset_flags(&desc, 0, GPIOD_IS_OUT_ACTIVE)); |
| 665 | ut_asserteq(1, dm_gpio_get_value(&desc)); |
| 666 | ut_asserteq(0, sandbox_gpio_get_value(desc.dev, desc.offset)); |
| 667 | |
| 668 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 669 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE, |
| 670 | flags); |
| 671 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE, |
| 672 | desc.flags); |
| 673 | |
| 674 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_IS_OUT_ACTIVE, 0)); |
| 675 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 676 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW, flags); |
| 677 | ut_asserteq(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW, desc.flags); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | DM_TEST(dm_test_clrset_flags_invert, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 247ccf2 | 2021-02-04 21:22:09 -0700 | [diff] [blame] | 682 | |
| 683 | static int set_gpios(struct unit_test_state *uts, struct gpio_desc *desc, |
| 684 | int count, uint value) |
| 685 | { |
| 686 | int i; |
| 687 | |
| 688 | for (i = 0; i < count; i++) { |
| 689 | const uint mask = 1 << i; |
| 690 | |
| 691 | ut_assertok(sandbox_gpio_set_value(desc[i].dev, desc[i].offset, |
| 692 | value & mask)); |
| 693 | } |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* Check that an active-low GPIO works as expected */ |
| 699 | static int dm_test_gpio_get_values_as_int(struct unit_test_state *uts) |
| 700 | { |
| 701 | const int gpio_count = 3; |
| 702 | struct gpio_desc desc[gpio_count]; |
| 703 | struct udevice *dev; |
| 704 | |
| 705 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 706 | ut_asserteq_str("a-test", dev->name); |
| 707 | |
| 708 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc, |
| 709 | gpio_count, GPIOD_IS_IN)); |
| 710 | ut_assertok(set_gpios(uts, desc, gpio_count, 0)); |
| 711 | ut_asserteq(0, dm_gpio_get_values_as_int(desc, gpio_count)); |
| 712 | |
| 713 | ut_assertok(set_gpios(uts, desc, gpio_count, 5)); |
| 714 | ut_asserteq(5, dm_gpio_get_values_as_int(desc, gpio_count)); |
| 715 | |
| 716 | ut_assertok(set_gpios(uts, desc, gpio_count, 7)); |
| 717 | ut_asserteq(7, dm_gpio_get_values_as_int(desc, gpio_count)); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | DM_TEST(dm_test_gpio_get_values_as_int, |
| 722 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
| 723 | |
| 724 | /* Check that an active-low GPIO works as expected */ |
| 725 | static int dm_test_gpio_get_values_as_int_base3(struct unit_test_state *uts) |
| 726 | { |
| 727 | const int gpio_count = 3; |
| 728 | struct gpio_desc desc[gpio_count]; |
| 729 | struct udevice *dev; |
| 730 | |
| 731 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 732 | ut_asserteq_str("a-test", dev->name); |
| 733 | |
| 734 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc, |
| 735 | gpio_count, GPIOD_IS_IN)); |
| 736 | |
| 737 | /* |
| 738 | * First test the sandbox GPIO driver works as expected. The external |
| 739 | * pull resistor should be stronger than the internal one. |
| 740 | */ |
| 741 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, |
| 742 | GPIOD_IS_IN | GPIOD_EXT_PULL_UP | GPIOD_PULL_UP); |
| 743 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 744 | |
| 745 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, GPIOD_IS_IN | |
| 746 | GPIOD_EXT_PULL_DOWN | GPIOD_PULL_UP); |
| 747 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 748 | |
| 749 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, |
| 750 | GPIOD_IS_IN | GPIOD_PULL_UP); |
| 751 | ut_asserteq(1, dm_gpio_get_value(desc)); |
| 752 | |
| 753 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, GPIOD_PULL_DOWN); |
| 754 | ut_asserteq(0, dm_gpio_get_value(desc)); |
| 755 | |
| 756 | /* |
| 757 | * Set up pins: pull-up (1), pull-down (0) and floating (2). This should |
| 758 | * result in digits 2 0 1, i.e. 2 * 9 + 1 * 3 = 19 |
| 759 | */ |
| 760 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, GPIOD_EXT_PULL_UP); |
| 761 | sandbox_gpio_set_flags(desc[1].dev, desc[1].offset, |
| 762 | GPIOD_EXT_PULL_DOWN); |
| 763 | sandbox_gpio_set_flags(desc[2].dev, desc[2].offset, 0); |
| 764 | ut_asserteq(19, dm_gpio_get_values_as_int_base3(desc, gpio_count)); |
| 765 | |
| 766 | /* |
| 767 | * Set up pins: floating (2), pull-up (1) and pull-down (0). This should |
| 768 | * result in digits 0 1 2, i.e. 1 * 3 + 2 = 5 |
| 769 | */ |
| 770 | sandbox_gpio_set_flags(desc[0].dev, desc[0].offset, 0); |
| 771 | sandbox_gpio_set_flags(desc[1].dev, desc[1].offset, GPIOD_EXT_PULL_UP); |
| 772 | sandbox_gpio_set_flags(desc[2].dev, desc[2].offset, |
| 773 | GPIOD_EXT_PULL_DOWN); |
| 774 | ut_asserteq(5, dm_gpio_get_values_as_int_base3(desc, gpio_count)); |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | DM_TEST(dm_test_gpio_get_values_as_int_base3, |
| 779 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Patrice Chotard | f69c39d | 2022-08-30 14:09:14 +0200 | [diff] [blame] | 780 | |
| 781 | /* Check that gpio_get_status return the label of a GPIO configured as GPIOD_AF */ |
| 782 | static int dm_test_gpio_function(struct unit_test_state *uts) |
| 783 | { |
| 784 | struct gpio_desc desc; |
| 785 | struct udevice *dev; |
| 786 | ulong flags; |
| 787 | unsigned int offset, gpio; |
| 788 | char buf[80]; |
| 789 | |
| 790 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 791 | ut_asserteq_str("a-test", dev->name); |
| 792 | |
| 793 | /* request gpio_b 5 */ |
| 794 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 2, &desc, 0)); |
| 795 | /* update gpio_b 5 function to GPIO_AF */ |
| 796 | ut_assertok(dm_gpio_clrset_flags(&desc, GPIOD_IS_AF, GPIOD_IS_AF)); |
| 797 | ut_assertok(dm_gpio_get_flags(&desc, &flags)); |
| 798 | ut_asserteq(GPIOD_IS_AF, flags); |
| 799 | /* check using gpio_get_status that label is displayed for a pin with GPIO_AF function */ |
| 800 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 801 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 802 | ut_asserteq_str("b5: func a-test.test-gpios2", buf); |
| 803 | |
| 804 | ut_assertok(dm_gpio_free(dev, &desc)); |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | DM_TEST(dm_test_gpio_function, |
| 809 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |