Simon Glass | e821d18 | 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 | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 10 | #include <dm/root.h> |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 11 | #include <dm/ut.h> |
| 12 | #include <dm/test.h> |
| 13 | #include <dm/util.h> |
| 14 | #include <asm/gpio.h> |
| 15 | |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 18 | /* Test that sandbox GPIOs work correctly */ |
| 19 | static int dm_test_gpio(struct dm_test_state *dms) |
| 20 | { |
| 21 | unsigned int offset, gpio; |
| 22 | struct dm_gpio_ops *ops; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 23 | struct udevice *dev; |
Simon Glass | e821d18 | 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 | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 45 | ut_assert(ops->get_function); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 46 | |
| 47 | /* Cannot get a value until it is reserved */ |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 48 | ut_asserteq(-EBUSY, gpio_get_value(gpio + 1)); |
Simon Glass | e821d18 | 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 | 96f7d60 | 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 | e821d18 | 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 | 96f7d60 | 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 | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 60 | |
| 61 | sandbox_gpio_set_value(dev, offset, 1); |
Simon Glass | 96f7d60 | 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 | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 64 | |
Simon Glass | 96f7d60 | 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 | e821d18 | 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 | 96f7d60 | 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 | e821d18 | 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 | |
| 78 | /* Make it an input */ |
| 79 | ut_assertok(ops->direction_input(dev, offset)); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 80 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 81 | ut_asserteq_str("b4: input: 1 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 82 | sandbox_gpio_set_value(dev, offset, 0); |
| 83 | ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 84 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 85 | ut_asserteq_str("b4: input: 0 [x] testing", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 86 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 87 | ut_assertok(gpio_free(gpio)); |
| 88 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 89 | ut_asserteq_str("b4: input: 0 [ ]", buf); |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 90 | |
| 91 | /* Check the 'a' bank also */ |
| 92 | ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio)); |
| 93 | ut_asserteq_str(dev->name, "base-gpios"); |
| 94 | ut_asserteq(15, offset); |
| 95 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio); |
| 96 | |
| 97 | name = gpio_get_bank_info(dev, &offset_count); |
| 98 | ut_asserteq_str("a", name); |
| 99 | ut_asserteq(20, offset_count); |
| 100 | |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 104 | |
| 105 | /* Test that sandbox anonymous GPIOs work correctly */ |
| 106 | static int dm_test_gpio_anon(struct dm_test_state *dms) |
| 107 | { |
| 108 | unsigned int offset, gpio; |
| 109 | struct udevice *dev; |
| 110 | const char *name; |
| 111 | int offset_count; |
| 112 | |
Simon Glass | e821d18 | 2014-02-26 15:59:24 -0700 | [diff] [blame] | 113 | /* And the anonymous bank */ |
| 114 | ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); |
| 115 | ut_asserteq_str(dev->name, "gpio_sandbox"); |
| 116 | ut_asserteq(14, offset); |
| 117 | ut_asserteq(14, gpio); |
| 118 | |
| 119 | name = gpio_get_bank_info(dev, &offset_count); |
| 120 | ut_asserteq_ptr(NULL, name); |
| 121 | ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); |
| 122 | |
| 123 | return 0; |
| 124 | } |
Simon Glass | 96f7d60 | 2014-10-04 11:29:48 -0600 | [diff] [blame] | 125 | DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 1b27d60 | 2014-10-04 11:29:49 -0600 | [diff] [blame] | 126 | |
| 127 | /* Test that gpio_requestf() works as expected */ |
| 128 | static int dm_test_gpio_requestf(struct dm_test_state *dms) |
| 129 | { |
| 130 | unsigned int offset, gpio; |
| 131 | struct udevice *dev; |
| 132 | char buf[80]; |
| 133 | |
| 134 | ut_assertok(gpio_lookup_name("b5", &dev, &offset, &gpio)); |
| 135 | ut_assertok(gpio_requestf(gpio, "testing %d %s", 1, "hi")); |
| 136 | sandbox_gpio_set_direction(dev, offset, 1); |
| 137 | sandbox_gpio_set_value(dev, offset, 1); |
| 138 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 139 | ut_asserteq_str("b5: output: 1 [x] testing 1 hi", buf); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | DM_TEST(dm_test_gpio_requestf, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | b4463db | 2014-10-04 11:29:51 -0600 | [diff] [blame] | 144 | |
| 145 | /* Test that gpio_request() copies its string */ |
| 146 | static int dm_test_gpio_copy(struct dm_test_state *dms) |
| 147 | { |
| 148 | unsigned int offset, gpio; |
| 149 | struct udevice *dev; |
| 150 | char buf[80], name[10]; |
| 151 | |
| 152 | ut_assertok(gpio_lookup_name("b6", &dev, &offset, &gpio)); |
| 153 | strcpy(name, "odd_name"); |
| 154 | ut_assertok(gpio_request(gpio, name)); |
| 155 | sandbox_gpio_set_direction(dev, offset, 1); |
| 156 | sandbox_gpio_set_value(dev, offset, 1); |
| 157 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 158 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 159 | strcpy(name, "nothing"); |
| 160 | ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); |
| 161 | ut_asserteq_str("b6: output: 1 [x] odd_name", buf); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | DM_TEST(dm_test_gpio_copy, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 166 | |
| 167 | /* Test that we don't leak memory with GPIOs */ |
| 168 | static int dm_test_gpio_leak(struct dm_test_state *dms) |
| 169 | { |
| 170 | ut_assertok(dm_test_gpio(dms)); |
| 171 | ut_assertok(dm_test_gpio_anon(dms)); |
| 172 | ut_assertok(dm_test_gpio_requestf(dms)); |
| 173 | ut_assertok(dm_leak_check_end(dms)); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | DM_TEST(dm_test_gpio_leak, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |