1 /*
2 * Copyright (c) 2021 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "test_device.h"
8
9 #include <zephyr/drivers/pinctrl.h>
10 #include <zephyr/fff.h>
11 #include <zephyr/ztest.h>
12
13 DEFINE_FFF_GLOBALS;
14
15 FAKE_VALUE_FUNC(int, pinctrl_configure_pins, const pinctrl_soc_pin_t *, uint8_t, uintptr_t);
16
17 /* test device 0 */
18 #define TEST_DEVICE0 DT_NODELABEL(test_device0)
19 PINCTRL_DT_DEV_CONFIG_DECLARE(TEST_DEVICE0);
20 static struct pinctrl_dev_config *pcfg0 = PINCTRL_DT_DEV_CONFIG_GET(TEST_DEVICE0);
21
22 /* test device 1 */
23 #define TEST_DEVICE1 DT_NODELABEL(test_device1)
24 PINCTRL_DT_DEV_CONFIG_DECLARE(TEST_DEVICE1);
25 static struct pinctrl_dev_config *pcfg1 = PINCTRL_DT_DEV_CONFIG_GET(TEST_DEVICE1);
26
27 /**
28 * @brief Test if configuration for device 0 has been stored as expected.
29 *
30 * Device 0 is also used to verify that certain states are skipped
31 * automatically, e.g. "sleep" if CONFIG_PM_DEVICE is not active.
32 *
33 * This test together with test_config_dev1 is used to verify that the whole
34 * set of macros used to define and initialize pin control config from
35 * Devicetree works as expected.
36 */
ZTEST(pinctrl_api,test_config_dev0)37 ZTEST(pinctrl_api, test_config_dev0)
38 {
39 const struct pinctrl_state *scfg;
40
41 zassert_equal(pcfg0->state_cnt, 1);
42 #ifdef CONFIG_PINCTRL_STORE_REG
43 zassert_equal(pcfg0->reg, 0);
44 #endif
45
46 scfg = &pcfg0->states[0];
47 zassert_equal(scfg->id, PINCTRL_STATE_DEFAULT);
48 zassert_equal(TEST_GET_PIN(scfg->pins[0]), 0);
49 zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_UP);
50 zassert_equal(TEST_GET_PIN(scfg->pins[1]), 1);
51 zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_DOWN);
52 }
53
54 /**
55 * @brief Test if configuration for device 1 has been stored as expected.
56 *
57 * Device 1 is also used to verify that custom states can be defined.
58 *
59 * @see test_config_dev0()
60 */
ZTEST(pinctrl_api,test_config_dev1)61 ZTEST(pinctrl_api, test_config_dev1)
62 {
63 const struct pinctrl_state *scfg;
64
65 zassert_equal(pcfg1->state_cnt, 2);
66 #ifdef CONFIG_PINCTRL_STORE_REG
67 zassert_equal(pcfg1->reg, 1);
68 #endif
69
70 scfg = &pcfg1->states[0];
71 zassert_equal(scfg->id, PINCTRL_STATE_DEFAULT);
72 zassert_equal(scfg->pin_cnt, 3);
73 zassert_equal(TEST_GET_PIN(scfg->pins[0]), 10);
74 zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_DISABLE);
75 zassert_equal(TEST_GET_PIN(scfg->pins[1]), 11);
76 zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_DISABLE);
77 zassert_equal(TEST_GET_PIN(scfg->pins[2]), 12);
78 zassert_equal(TEST_GET_PULL(scfg->pins[2]), TEST_PULL_DISABLE);
79
80 scfg = &pcfg1->states[1];
81 zassert_equal(scfg->id, PINCTRL_STATE_MYSTATE);
82 zassert_equal(scfg->pin_cnt, 3);
83 zassert_equal(TEST_GET_PIN(scfg->pins[0]), 10);
84 zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_DISABLE);
85 zassert_equal(TEST_GET_PIN(scfg->pins[1]), 11);
86 zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_UP);
87 zassert_equal(TEST_GET_PIN(scfg->pins[2]), 12);
88 zassert_equal(TEST_GET_PULL(scfg->pins[2]), TEST_PULL_DOWN);
89 }
90
91 /**
92 * @brief Test that pinctrl_lookup_state() works as expected
93 */
ZTEST(pinctrl_api,test_lookup_state)94 ZTEST(pinctrl_api, test_lookup_state)
95 {
96 int ret;
97 const struct pinctrl_state *scfg;
98
99 ret = pinctrl_lookup_state(pcfg0, PINCTRL_STATE_DEFAULT, &scfg);
100 zassert_equal(ret, 0);
101 zassert_equal_ptr(scfg, &pcfg0->states[0], NULL);
102
103 ret = pinctrl_lookup_state(pcfg0, PINCTRL_STATE_SLEEP, &scfg);
104 zassert_equal(ret, -ENOENT);
105 }
106
107 /**
108 * @brief Test that pinctrl_apply_state() works as expected.
109 */
ZTEST(pinctrl_api,test_apply_state)110 ZTEST(pinctrl_api, test_apply_state)
111 {
112 zassert_ok(pinctrl_apply_state(pcfg0, PINCTRL_STATE_DEFAULT));
113 zassert_equal(1, pinctrl_configure_pins_fake.call_count);
114 zassert_equal(pcfg0->states[0].pins, pinctrl_configure_pins_fake.arg0_val);
115 zassert_equal(pcfg0->states[0].pin_cnt, pinctrl_configure_pins_fake.arg1_val);
116 #ifdef CONFIG_PINCTRL_STORE_REG
117 zassert_equal(0, pinctrl_configure_pins_fake.arg2_val);
118 #else
119 zassert_equal(PINCTRL_REG_NONE, pinctrl_configure_pins_fake.arg2_val);
120 #endif
121 }
122
123 /** Test device 0 alternative pins for default state */
124 PINCTRL_DT_STATE_PINS_DEFINE(DT_PATH(zephyr_user), test_device0_alt_default);
125 /** Test device 0 alternative pins for sleep state */
126 PINCTRL_DT_STATE_PINS_DEFINE(DT_PATH(zephyr_user), test_device0_alt_sleep);
127
128 /** Test device 0 alternative states. */
129 static const struct pinctrl_state test_device0_alt[] = {
130 PINCTRL_DT_STATE_INIT(test_device0_alt_default, PINCTRL_STATE_DEFAULT),
131 };
132
133 /** Test device 0 invalid alternative states. */
134 static const struct pinctrl_state test_device0_alt_invalid[] = {
135 PINCTRL_DT_STATE_INIT(test_device0_alt_default, PINCTRL_STATE_DEFAULT),
136 /* sleep state is skipped (no CONFIG_PM_DEVICE), so it is invalid */
137 PINCTRL_DT_STATE_INIT(test_device0_alt_sleep, PINCTRL_STATE_SLEEP),
138 };
139
140 /**
141 * @brief This test checks if pinctrl_update_states() works as expected.
142 */
ZTEST(pinctrl_api,test_update_states)143 ZTEST(pinctrl_api, test_update_states)
144 {
145 int ret;
146 const struct pinctrl_state *scfg;
147
148 ret = pinctrl_update_states(pcfg0, test_device0_alt, ARRAY_SIZE(test_device0_alt));
149 zassert_equal(ret, 0);
150
151 scfg = &pcfg0->states[0];
152 zassert_equal(TEST_GET_PIN(scfg->pins[0]), 2);
153 zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_DOWN);
154 zassert_equal(TEST_GET_PIN(scfg->pins[1]), 3);
155 zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_UP);
156
157 ret = pinctrl_update_states(pcfg0, test_device0_alt_invalid,
158 ARRAY_SIZE(test_device0_alt_invalid));
159 zassert_equal(ret, -EINVAL);
160 }
161
pinctrl_api_before(void * f)162 static void pinctrl_api_before(void *f)
163 {
164 ARG_UNUSED(f);
165 RESET_FAKE(pinctrl_configure_pins);
166 FFF_RESET_HISTORY();
167 }
168
169 ZTEST_SUITE(pinctrl_api, NULL, NULL, pinctrl_api_before, NULL, NULL);
170