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> 10 #include <dm/ut.h> 11 #include <dm/test.h> 12 #include <dm/util.h> 13 #include <asm/gpio.h> 14 15 /* Test that sandbox GPIOs work correctly */ 16 static int dm_test_gpio(struct dm_test_state *dms) 17 { 18 unsigned int offset, gpio; 19 struct dm_gpio_ops *ops; 20 struct udevice *dev; 21 const char *name; 22 int offset_count; 23 char buf[80]; 24 25 /* 26 * We expect to get 3 banks. One is anonymous (just numbered) and 27 * comes from platdata. The other two are named a (20 gpios) 28 * and b (10 gpios) and come from the device tree. See 29 * test/dm/test.dts. 30 */ 31 ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio)); 32 ut_asserteq_str(dev->name, "extra-gpios"); 33 ut_asserteq(4, offset); 34 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio); 35 36 name = gpio_get_bank_info(dev, &offset_count); 37 ut_asserteq_str("b", name); 38 ut_asserteq(10, offset_count); 39 40 /* Get the operations for this device */ 41 ops = gpio_get_ops(dev); 42 ut_assert(ops->get_function); 43 44 /* Cannot get a value until it is reserved */ 45 ut_asserteq(-EBUSY, gpio_get_value(gpio + 1)); 46 /* 47 * Now some tests that use the 'sandbox' back door. All GPIOs 48 * should default to input, include b4 that we are using here. 49 */ 50 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 51 ut_asserteq_str("b4: input: 0 [ ]", buf); 52 53 /* Change it to an output */ 54 sandbox_gpio_set_direction(dev, offset, 1); 55 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 56 ut_asserteq_str("b4: output: 0 [ ]", buf); 57 58 sandbox_gpio_set_value(dev, offset, 1); 59 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 60 ut_asserteq_str("b4: output: 1 [ ]", buf); 61 62 ut_assertok(gpio_request(gpio, "testing")); 63 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 64 ut_asserteq_str("b4: output: 1 [x] testing", buf); 65 66 /* Change the value a bit */ 67 ut_asserteq(1, ops->get_value(dev, offset)); 68 ut_assertok(ops->set_value(dev, offset, 0)); 69 ut_asserteq(0, ops->get_value(dev, offset)); 70 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 71 ut_asserteq_str("b4: output: 0 [x] testing", buf); 72 ut_assertok(ops->set_value(dev, offset, 1)); 73 ut_asserteq(1, ops->get_value(dev, offset)); 74 75 /* Make it an input */ 76 ut_assertok(ops->direction_input(dev, offset)); 77 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 78 ut_asserteq_str("b4: input: 1 [x] testing", buf); 79 sandbox_gpio_set_value(dev, offset, 0); 80 ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); 81 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 82 ut_asserteq_str("b4: input: 0 [x] testing", buf); 83 84 ut_assertok(gpio_free(gpio)); 85 ut_assertok(gpio_get_status(dev, offset, buf, sizeof(buf))); 86 ut_asserteq_str("b4: input: 0 [ ]", buf); 87 88 /* Check the 'a' bank also */ 89 ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio)); 90 ut_asserteq_str(dev->name, "base-gpios"); 91 ut_asserteq(15, offset); 92 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio); 93 94 name = gpio_get_bank_info(dev, &offset_count); 95 ut_asserteq_str("a", name); 96 ut_asserteq(20, offset_count); 97 98 return 0; 99 } 100 DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); 101 102 /* Test that sandbox anonymous GPIOs work correctly */ 103 static int dm_test_gpio_anon(struct dm_test_state *dms) 104 { 105 unsigned int offset, gpio; 106 struct udevice *dev; 107 const char *name; 108 int offset_count; 109 110 /* And the anonymous bank */ 111 ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); 112 ut_asserteq_str(dev->name, "gpio_sandbox"); 113 ut_asserteq(14, offset); 114 ut_asserteq(14, gpio); 115 116 name = gpio_get_bank_info(dev, &offset_count); 117 ut_asserteq_ptr(NULL, name); 118 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); 119 120 return 0; 121 } 122 DM_TEST(dm_test_gpio_anon, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); 123