Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <fdtdec.h> |
| 9 | #include <dm.h> |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 10 | #include <dm/root.h> |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 11 | #include <dm/test.h> |
| 12 | #include <dm/util.h> |
| 13 | #include <asm/gpio.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | #include <test/ut.h> |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 15 | |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 18 | /* Test that sandbox GPIOs work correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | static int dm_test_gpio(struct unit_test_state *uts) |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 20 | { |
| 21 | unsigned int offset, gpio; |
| 22 | struct dm_gpio_ops *ops; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 23 | struct udevice *dev; |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 24 | const char *name; |
| 25 | int offset_count; |
| 26 | char buf[80]; |
| 27 | |
| 28 | /* |
| 29 | * We expect to get 3 banks. One is anonymous (just numbered) and |
| 30 | * comes from platdata. The other two are named a (20 gpios) |
| 31 | * and b (10 gpios) and come from the device tree. See |
| 32 | * test/dm/test.dts. |
| 33 | */ |
| 34 | ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio)); |
| 35 | ut_asserteq_str(dev->name, "extra-gpios"); |
| 36 | ut_asserteq(4, offset); |
| 37 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio); |
| 38 | |
| 39 | name = gpio_get_bank_info(dev, &offset_count); |
| 40 | ut_asserteq_str("b", name); |
| 41 | ut_asserteq(10, offset_count); |
| 42 | |
| 43 | /* Get the operations for this device */ |
| 44 | ops = gpio_get_ops(dev); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 45 | ut_assert(ops->get_function); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 46 | |
| 47 | /* Cannot get a value until it is reserved */ |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 48 | ut_asserteq(-EBUSY, gpio_get_value(gpio + 1)); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 49 | /* |
| 50 | * Now some tests that use the 'sandbox' back door. All GPIOs |
| 51 | * should default to input, include b4 that we are using here. |
| 52 | */ |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 53 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 54 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 55 | |
| 56 | /* Change it to an output */ |
| 57 | sandbox_gpio_set_direction(dev, offset, 1); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 58 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 59 | ut_asserteq_str("b4: output: 0 [ ]", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 60 | |
| 61 | sandbox_gpio_set_value(dev, offset, 1); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 62 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 63 | ut_asserteq_str("b4: output: 1 [ ]", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 64 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 65 | ut_assertok(gpio_request(gpio, "testing")); |
| 66 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 67 | ut_asserteq_str("b4: output: 1 [x] testing", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 68 | |
| 69 | /* Change the value a bit */ |
| 70 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 71 | ut_assertok(ops->set_value(dev, offset, 0)); |
| 72 | ut_asserteq(0, ops->get_value(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 73 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 74 | ut_asserteq_str("b4: output: 0 [x] testing", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 75 | ut_assertok(ops->set_value(dev, offset, 1)); |
| 76 | ut_asserteq(1, ops->get_value(dev, offset)); |
| 77 | |
mario.six@gdsys.cc | 743268f | 2016-05-25 15:15:23 +0200 | [diff] [blame] | 78 | /* Make it an open drain output, and reset it */ |
| 79 | ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset)); |
| 80 | ut_assertok(ops->set_open_drain(dev, offset, 1)); |
| 81 | ut_asserteq(1, sandbox_gpio_get_open_drain(dev, offset)); |
| 82 | ut_assertok(ops->set_open_drain(dev, offset, 0)); |
| 83 | ut_asserteq(0, sandbox_gpio_get_open_drain(dev, offset)); |
| 84 | |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 85 | /* Make it an input */ |
| 86 | ut_assertok(ops->direction_input(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 87 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 88 | ut_asserteq_str("b4: input: 1 [x] testing", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 89 | sandbox_gpio_set_value(dev, offset, 0); |
| 90 | ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 91 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 92 | ut_asserteq_str("b4: input: 0 [x] testing", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 93 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 94 | ut_assertok(gpio_free(gpio)); |
| 95 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 96 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 97 | |
| 98 | /* Check the 'a' bank also */ |
| 99 | ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio)); |
| 100 | ut_asserteq_str(dev->name, "base-gpios"); |
| 101 | ut_asserteq(15, offset); |
| 102 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio); |
| 103 | |
| 104 | name = gpio_get_bank_info(dev, &offset_count); |
| 105 | ut_asserteq_str("a", name); |
| 106 | ut_asserteq(20, offset_count); |
| 107 | |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 111 | |
| 112 | /* Test that sandbox anonymous GPIOs work correctly */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 113 | static int dm_test_gpio_anon(struct unit_test_state *uts) |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 114 | { |
| 115 | unsigned int offset, gpio; |
| 116 | struct udevice *dev; |
| 117 | const char *name; |
| 118 | int offset_count; |
| 119 | |
Simon Glass | 96495d90 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 120 | /* And the anonymous bank */ |
| 121 | ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); |
| 122 | ut_asserteq_str(dev->name, "gpio_sandbox"); |
| 123 | ut_asserteq(14, offset); |
| 124 | ut_asserteq(14, gpio); |
| 125 | |
| 126 | name = gpio_get_bank_info(dev, &offset_count); |
| 127 | ut_asserteq_ptr(NULL, name); |
| 128 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); |
| 129 | |
| 130 | return 0; |
| 131 | } |
Simon Glass | 4b8f11c | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 132 | DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 133 | |
| 134 | /* Test that gpio_requestf() works as expected */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 135 | static int dm_test_gpio_requestf(struct unit_test_state *uts) |
Simon Glass | d44f597 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 136 | { |
| 137 | unsigned int offset, gpio; |
| 138 | struct udevice *dev; |
| 139 | char buf[80]; |
| 140 | |
| 141 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 142 | ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi")); |
| 143 | sandbox_gpio_set_direction(dev, offset, 1); |
| 144 | sandbox_gpio_set_value(dev, offset, 1); |
| 145 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 146 | ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 151 | |
| 152 | /* Test that gpio_request() copies its string */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 153 | static int dm_test_gpio_copy(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 154 | { |
| 155 | unsigned int offset, gpio; |
| 156 | struct udevice *dev; |
| 157 | char buf[80], name[10]; |
| 158 | |
| 159 | ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio)); |
| 160 | strcpy(name, "odd_name"); |
| 161 | ut_assertok(gpio_request(gpio, name)); |
| 162 | sandbox_gpio_set_direction(dev, offset, 1); |
| 163 | sandbox_gpio_set_value(dev, offset, 1); |
| 164 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 165 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 166 | strcpy(name, "nothing"); |
| 167 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 168 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 173 | |
| 174 | /* Test that we don't leak memory with GPIOs */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 175 | static int dm_test_gpio_leak(struct unit_test_state *uts) |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 176 | { |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 177 | ut_assertok(dm_test_gpio(uts)); |
| 178 | ut_assertok(dm_test_gpio_anon(uts)); |
| 179 | ut_assertok(dm_test_gpio_requestf(uts)); |
| 180 | ut_assertok(dm_leak_check_end(uts)); |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 181 | |
| 182 | return 0; |
| 183 | } |
Simon Glass | 4324174 | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 184 | DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 185 | |
| 186 | /* Test that we can find GPIOs using phandles */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 187 | static int dm_test_gpio_phandles(struct unit_test_state *uts) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 188 | { |
| 189 | struct gpio_desc desc, desc_list[8], desc_list2[8]; |
| 190 | struct udevice *dev, *gpio_a, *gpio_b; |
| 191 | |
| 192 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 193 | ut_asserteq_str("a-test", dev->name); |
| 194 | |
| 195 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 196 | ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio_a)); |
| 197 | ut_assertok(uclass_get_device(UCLASS_GPIO, 2, &gpio_b)); |
| 198 | ut_asserteq_str("base-gpios", gpio_a->name); |
| 199 | ut_asserteq(true, !!device_active(gpio_a)); |
| 200 | ut_asserteq_ptr(gpio_a, desc.dev); |
| 201 | ut_asserteq(4, desc.offset); |
| 202 | /* GPIOF_INPUT is the sandbox GPIO driver default */ |
| 203 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_a, 4, NULL)); |
| 204 | ut_assertok(dm_gpio_free(dev, &desc)); |
| 205 | |
| 206 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 3, &desc, |
| 207 | 0)); |
| 208 | ut_asserteq_ptr(NULL, desc.dev); |
| 209 | ut_asserteq(desc.offset, 0); |
| 210 | ut_asserteq(-ENOENT, gpio_request_by_name(dev, "test-gpios", 5, &desc, |
| 211 | 0)); |
| 212 | |
| 213 | /* Last GPIO is ignord as it comes after <0> */ |
| 214 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 215 | ARRAY_SIZE(desc_list), 0)); |
| 216 | ut_asserteq(-EBUSY, gpio_request_list_by_name(dev, "test-gpios", |
| 217 | desc_list2, |
| 218 | ARRAY_SIZE(desc_list2), |
| 219 | 0)); |
| 220 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
| 221 | ut_asserteq(3, gpio_request_list_by_name(dev, "test-gpios", desc_list, |
| 222 | ARRAY_SIZE(desc_list), |
| 223 | GPIOD_IS_OUT | |
| 224 | GPIOD_IS_OUT_ACTIVE)); |
| 225 | ut_asserteq_ptr(gpio_a, desc_list[0].dev); |
| 226 | ut_asserteq(1, desc_list[0].offset); |
| 227 | ut_asserteq_ptr(gpio_a, desc_list[1].dev); |
| 228 | ut_asserteq(4, desc_list[1].offset); |
| 229 | ut_asserteq_ptr(gpio_b, desc_list[2].dev); |
| 230 | ut_asserteq(5, desc_list[2].offset); |
| 231 | ut_asserteq(1, dm_gpio_get_value(desc_list)); |
| 232 | ut_assertok(gpio_free_list(dev, desc_list, 3)); |
| 233 | |
| 234 | ut_asserteq(6, gpio_request_list_by_name(dev, "test2-gpios", desc_list, |
| 235 | ARRAY_SIZE(desc_list), 0)); |
| 236 | /* This was set to output previously, so still will be */ |
| 237 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_a, 1, NULL)); |
| 238 | |
| 239 | /* Active low should invert the input value */ |
| 240 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 6, NULL)); |
| 241 | ut_asserteq(1, dm_gpio_get_value(&desc_list[2])); |
| 242 | |
| 243 | ut_asserteq(GPIOF_INPUT, gpio_get_function(gpio_b, 7, NULL)); |
| 244 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 8, NULL)); |
| 245 | ut_asserteq(0, dm_gpio_get_value(&desc_list[4])); |
| 246 | ut_asserteq(GPIOF_OUTPUT, gpio_get_function(gpio_b, 9, NULL)); |
| 247 | ut_asserteq(1, dm_gpio_get_value(&desc_list[5])); |
| 248 | |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | DM_TEST(dm_test_gpio_phandles, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |