blob: 9157ec928783d84de929a6884e35189c89d2c7c6 [file] [log] [blame]
Philippe Reynes0785e202020-07-24 18:19:52 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com>
4 *
5 * Based on led.c
6 */
7
Philippe Reynes0785e202020-07-24 18:19:52 +02008#include <dm.h>
Marek Szyprowskiad398592021-02-18 11:33:18 +01009#include <adc.h>
Philippe Reynes0785e202020-07-24 18:19:52 +020010#include <button.h>
Marek Szyprowskiad398592021-02-18 11:33:18 +010011#include <power/regulator.h>
12#include <power/sandbox_pmic.h>
Philippe Reynes0785e202020-07-24 18:19:52 +020013#include <asm/gpio.h>
14#include <dm/test.h>
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030015#include <dt-bindings/input/input.h>
Philippe Reynes0785e202020-07-24 18:19:52 +020016#include <test/ut.h>
17
18/* Base test of the button uclass */
19static int dm_test_button_base(struct unit_test_state *uts)
20{
21 struct udevice *dev;
22
Marek Szyprowskiad398592021-02-18 11:33:18 +010023 /* Get the top-level gpio buttons device */
Philippe Reynes0785e202020-07-24 18:19:52 +020024 ut_assertok(uclass_get_device(UCLASS_BUTTON, 0, &dev));
Marek Szyprowskiad398592021-02-18 11:33:18 +010025 /* Get the 2 gpio buttons */
Philippe Reynes0785e202020-07-24 18:19:52 +020026 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
27 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &dev));
Marek Szyprowskiad398592021-02-18 11:33:18 +010028
29 /* Get the top-level adc buttons device */
30 ut_assertok(uclass_get_device(UCLASS_BUTTON, 3, &dev));
31 /* Get the 3 adc buttons */
32 ut_assertok(uclass_get_device(UCLASS_BUTTON, 4, &dev));
33 ut_assertok(uclass_get_device(UCLASS_BUTTON, 5, &dev));
34 ut_assertok(uclass_get_device(UCLASS_BUTTON, 6, &dev));
35
36 ut_asserteq(-ENODEV, uclass_get_device(UCLASS_BUTTON, 7, &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020037
38 return 0;
39}
Simon Glass974dccd2020-07-28 19:41:12 -060040DM_TEST(dm_test_button_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes0785e202020-07-24 18:19:52 +020041
42/* Test of the button uclass using the button_gpio driver */
43static int dm_test_button_gpio(struct unit_test_state *uts)
44{
45 const int offset = 3;
46 struct udevice *dev, *gpio;
47
48 /*
Yuepeng Xing71cc0212022-12-02 14:23:07 +080049 * Check that we can manipulate a BUTTON. BUTTON 1 is connected to GPIO
Philippe Reynes0785e202020-07-24 18:19:52 +020050 * bank gpio_a, offset 3.
51 */
52 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
53 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
54
55 ut_asserteq(0, sandbox_gpio_set_value(gpio, offset, 0));
56 ut_asserteq(0, sandbox_gpio_get_value(gpio, offset));
57 ut_asserteq(BUTTON_OFF, button_get_state(dev));
58
59 ut_asserteq(0, sandbox_gpio_set_value(gpio, offset, 1));
60 ut_asserteq(1, sandbox_gpio_get_value(gpio, offset));
61 ut_asserteq(BUTTON_ON, button_get_state(dev));
62
63 return 0;
64}
Simon Glass974dccd2020-07-28 19:41:12 -060065DM_TEST(dm_test_button_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Philippe Reynes0785e202020-07-24 18:19:52 +020066
Yuepeng Xing71cc0212022-12-02 14:23:07 +080067/* Test obtaining a BUTTON by label */
Philippe Reynes0785e202020-07-24 18:19:52 +020068static int dm_test_button_label(struct unit_test_state *uts)
69{
70 struct udevice *dev, *cmp;
71
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020072 ut_assertok(button_get_by_label("button1", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020073 ut_asserteq(1, device_active(dev));
74 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &cmp));
75 ut_asserteq_ptr(dev, cmp);
76
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020077 ut_assertok(button_get_by_label("button2", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020078 ut_asserteq(1, device_active(dev));
79 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &cmp));
80 ut_asserteq_ptr(dev, cmp);
81
Heinrich Schuchardt57c2fc62020-09-14 12:50:54 +020082 ut_asserteq(-ENODEV, button_get_by_label("nobutton", &dev));
Philippe Reynes0785e202020-07-24 18:19:52 +020083
84 return 0;
85}
Simon Glass974dccd2020-07-28 19:41:12 -060086DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Marek Szyprowskiad398592021-02-18 11:33:18 +010087
Dzmitry Sankouski157f2c52023-01-22 18:21:24 +030088/* Test button has linux,code */
89static int dm_test_button_linux_code(struct unit_test_state *uts)
90{
91 struct udevice *dev;
92
93 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
94 ut_asserteq(BTN_1, button_get_code(dev));
95
96 return 0;
97}
98DM_TEST(dm_test_button_linux_code, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
99
Marek Szyprowskiad398592021-02-18 11:33:18 +0100100/* Test adc-keys driver */
101static int dm_test_button_keys_adc(struct unit_test_state *uts)
102{
103 struct udevice *supply;
104 struct udevice *dev;
105 int uV;
106
107 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
108
109 ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
110 ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
111 ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
112 /* Update ADC plat and get new Vdd value */
113 ut_assertok(adc_vdd_value(dev, &uV));
114 ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
115
116 /*
117 * sandbox-adc returns constant value on channel 3, is used by adc-keys:
118 * SANDBOX_ADC_CHANNEL3_DATA * SANDBOX_BUCK2_SET_UV / SANDBOX_ADC_DATA_MASK =
119 * 0x3000 * 3300000 / 0xffff = 618759uV
120 * This means that button3 and button4 are released and button5
121 * is pressed.
122 */
123 ut_assertok(button_get_by_label("button3", &dev));
124 ut_asserteq(BUTTON_OFF, button_get_state(dev));
125 ut_assertok(button_get_by_label("button4", &dev));
126 ut_asserteq(BUTTON_OFF, button_get_state(dev));
127 ut_assertok(button_get_by_label("button5", &dev));
128 ut_asserteq(BUTTON_ON, button_get_state(dev));
129
130 return 0;
131}
132DM_TEST(dm_test_button_keys_adc, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Caleb Connollyb10091b2024-03-19 13:24:42 +0000133
134/* Test of the button uclass using the button_gpio driver */
135static int dm_test_button_cmd(struct unit_test_state *uts)
136{
137 struct udevice *btn1_dev, *btn2_dev, *gpio;
138 const char *envstr;
139
140#define BTN1_GPIO 3
141#define BTN2_GPIO 4
142#define BTN1_PASS_VAR "test_button_cmds_0"
143#define BTN2_PASS_VAR "test_button_cmds_1"
144
145 /*
146 * Buttons 1 and 2 are connected to gpio_a gpios 3 and 4 respectively.
147 * set the GPIOs to known values and then check that the appropriate
148 * commands are run when invoking process_button_cmds().
149 */
150 ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &btn1_dev));
151 ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &btn2_dev));
152 ut_assertok(uclass_get_device(UCLASS_GPIO, 1, &gpio));
153
154 /*
155 * Map a command to button 1 and check that it process_button_cmds()
156 * runs it if called with button 1 pressed.
157 */
158 ut_assertok(env_set("button_cmd_0_name", "button1"));
159 ut_assertok(env_set("button_cmd_0", "env set " BTN1_PASS_VAR " PASS"));
160 ut_assertok(sandbox_gpio_set_value(gpio, BTN1_GPIO, 1));
161 /* Sanity check that the button is actually pressed */
162 ut_asserteq(BUTTON_ON, button_get_state(btn1_dev));
163 process_button_cmds();
164 ut_assertnonnull((envstr = env_get(BTN1_PASS_VAR)));
165 ut_asserteq_str(envstr, "PASS");
166
167 /* Clear result */
168 ut_assertok(env_set(BTN1_PASS_VAR, NULL));
169
170 /*
171 * Map a command for button 2, press it, check that only the command
172 * for button 1 runs because it comes first and is also pressed.
173 */
174 ut_assertok(env_set("button_cmd_1_name", "button2"));
175 ut_assertok(env_set("button_cmd_1", "env set " BTN2_PASS_VAR " PASS"));
176 ut_assertok(sandbox_gpio_set_value(gpio, BTN2_GPIO, 1));
177 ut_asserteq(BUTTON_ON, button_get_state(btn2_dev));
178 process_button_cmds();
179 /* Check that button 1 triggered again */
180 ut_assertnonnull((envstr = env_get(BTN1_PASS_VAR)));
181 ut_asserteq_str(envstr, "PASS");
182 /* And button 2 didn't */
183 ut_assertnull(env_get(BTN2_PASS_VAR));
184
185 /* Clear result */
186 ut_assertok(env_set(BTN1_PASS_VAR, NULL));
187
188 /*
189 * Release button 1 and check that the command for button 2 is run
190 */
191 ut_assertok(sandbox_gpio_set_value(gpio, BTN1_GPIO, 0));
192 process_button_cmds();
193 ut_assertnull(env_get(BTN1_PASS_VAR));
194 /* Check that the command for button 2 ran */
195 ut_assertnonnull((envstr = env_get(BTN2_PASS_VAR)));
196 ut_asserteq_str(envstr, "PASS");
197
198 /* Clear result */
199 ut_assertok(env_set(BTN2_PASS_VAR, NULL));
200
201 /*
202 * Unset "button_cmd_0_name" and check that no commands run even
203 * with both buttons pressed.
204 */
205 ut_assertok(env_set("button_cmd_0_name", NULL));
206 /* Press button 1 (button 2 is already pressed )*/
207 ut_assertok(sandbox_gpio_set_value(gpio, BTN1_GPIO, 1));
208 ut_asserteq(BUTTON_ON, button_get_state(btn1_dev));
209 process_button_cmds();
210 ut_assertnull(env_get(BTN1_PASS_VAR));
211 ut_assertnull(env_get(BTN2_PASS_VAR));
212
213 /*
214 * Check that no command is run if the button name is wrong.
215 */
216 ut_assertok(env_set("button_cmd_0_name", "invalid_button"));
217 process_button_cmds();
218 ut_assertnull(env_get(BTN1_PASS_VAR));
219 ut_assertnull(env_get(BTN2_PASS_VAR));
220
221#undef BTN1_PASS_VAR
222#undef BTN2_PASS_VAR
223#undef BTN1_GPIO
224#undef BTN2_GPIO
225
226 return 0;
227}
228DM_TEST(dm_test_button_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);