blob: bf632bca54ed3e96b05419fe6a4d6695e6541b44 [file] [log] [blame]
Simon Glass96495d902014-02-26 15:59:24 -07001/*
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 */
16static int dm_test_gpio(struct dm_test_state *dms)
17{
18 unsigned int offset, gpio;
19 struct dm_gpio_ops *ops;
20 struct device *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_state);
43
44 /* Cannot get a value until it is reserved */
45 ut_asserteq(-1, ops->get_value(dev, offset));
46
47 /*
48 * Now some tests that use the 'sandbox' back door. All GPIOs
49 * should default to input, include b4 that we are using here.
50 */
51 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
52 ut_asserteq_str("b4: in: 0 [ ]", buf);
53
54 /* Change it to an output */
55 sandbox_gpio_set_direction(dev, offset, 1);
56 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
57 ut_asserteq_str("b4: out: 0 [ ]", buf);
58
59 sandbox_gpio_set_value(dev, offset, 1);
60 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
61 ut_asserteq_str("b4: out: 1 [ ]", buf);
62
63 ut_assertok(ops->request(dev, offset, "testing"));
64 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
65 ut_asserteq_str("b4: out: 1 [x] testing", buf);
66
67 /* Change the value a bit */
68 ut_asserteq(1, ops->get_value(dev, offset));
69 ut_assertok(ops->set_value(dev, offset, 0));
70 ut_asserteq(0, ops->get_value(dev, offset));
71 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
72 ut_asserteq_str("b4: out: 0 [x] testing", buf);
73 ut_assertok(ops->set_value(dev, offset, 1));
74 ut_asserteq(1, ops->get_value(dev, offset));
75
76 /* Make it an input */
77 ut_assertok(ops->direction_input(dev, offset));
78 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
79 ut_asserteq_str("b4: in: 1 [x] testing", buf);
80 sandbox_gpio_set_value(dev, offset, 0);
81 ut_asserteq(0, sandbox_gpio_get_value(dev, offset));
82 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
83 ut_asserteq_str("b4: in: 0 [x] testing", buf);
84
85 ut_assertok(ops->free(dev, offset));
86 ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf)));
87 ut_asserteq_str("b4: in: 0 [ ]", buf);
88
89 /* Check the 'a' bank also */
90 ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio));
91 ut_asserteq_str(dev->name, "base-gpios");
92 ut_asserteq(15, offset);
93 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio);
94
95 name = gpio_get_bank_info(dev, &offset_count);
96 ut_asserteq_str("a", name);
97 ut_asserteq(20, offset_count);
98
99 /* And the anonymous bank */
100 ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio));
101 ut_asserteq_str(dev->name, "gpio_sandbox");
102 ut_asserteq(14, offset);
103 ut_asserteq(14, gpio);
104
105 name = gpio_get_bank_info(dev, &offset_count);
106 ut_asserteq_ptr(NULL, name);
107 ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count);
108
109 return 0;
110}
111DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);