1 // 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 
8 #include <common.h>
9 #include <dm.h>
10 #include <adc.h>
11 #include <button.h>
12 #include <power/regulator.h>
13 #include <power/sandbox_pmic.h>
14 #include <asm/gpio.h>
15 #include <dm/test.h>
16 #include <test/ut.h>
17 
18 /* Base test of the button uclass */
dm_test_button_base(struct unit_test_state * uts)19 static int dm_test_button_base(struct unit_test_state *uts)
20 {
21 	struct udevice *dev;
22 
23 	/* Get the top-level gpio buttons device */
24 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 0, &dev));
25 	/* Get the 2 gpio buttons */
26 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
27 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &dev));
28 
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));
37 
38 	return 0;
39 }
40 DM_TEST(dm_test_button_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
41 
42 /* Test of the button uclass using the button_gpio driver */
dm_test_button_gpio(struct unit_test_state * uts)43 static int dm_test_button_gpio(struct unit_test_state *uts)
44 {
45 	const int offset = 3;
46 	struct udevice *dev, *gpio;
47 
48 	/*
49 	 * Check that we can manipulate an BUTTON. BUTTON 1 is connected to GPIO
50 	 * 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 }
65 DM_TEST(dm_test_button_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
66 
67 /* Test obtaining an BUTTON by label */
dm_test_button_label(struct unit_test_state * uts)68 static int dm_test_button_label(struct unit_test_state *uts)
69 {
70 	struct udevice *dev, *cmp;
71 
72 	ut_assertok(button_get_by_label("button1", &dev));
73 	ut_asserteq(1, device_active(dev));
74 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &cmp));
75 	ut_asserteq_ptr(dev, cmp);
76 
77 	ut_assertok(button_get_by_label("button2", &dev));
78 	ut_asserteq(1, device_active(dev));
79 	ut_assertok(uclass_get_device(UCLASS_BUTTON, 2, &cmp));
80 	ut_asserteq_ptr(dev, cmp);
81 
82 	ut_asserteq(-ENODEV, button_get_by_label("nobutton", &dev));
83 
84 	return 0;
85 }
86 DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
87 
88 /* Test adc-keys driver */
dm_test_button_keys_adc(struct unit_test_state * uts)89 static int dm_test_button_keys_adc(struct unit_test_state *uts)
90 {
91 	struct udevice *supply;
92 	struct udevice *dev;
93 	int uV;
94 
95 	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
96 
97 	ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
98 	ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
99 	ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
100 	/* Update ADC plat and get new Vdd value */
101 	ut_assertok(adc_vdd_value(dev, &uV));
102 	ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
103 
104 	/*
105 	 * sandbox-adc returns constant value on channel 3, is used by adc-keys:
106 	 * SANDBOX_ADC_CHANNEL3_DATA * SANDBOX_BUCK2_SET_UV / SANDBOX_ADC_DATA_MASK =
107 	 * 0x3000 * 3300000 / 0xffff = 618759uV
108 	 * This means that button3 and button4 are released and button5
109 	 * is pressed.
110 	 */
111 	ut_assertok(button_get_by_label("button3", &dev));
112 	ut_asserteq(BUTTON_OFF, button_get_state(dev));
113 	ut_assertok(button_get_by_label("button4", &dev));
114 	ut_asserteq(BUTTON_OFF, button_get_state(dev));
115 	ut_assertok(button_get_by_label("button5", &dev));
116 	ut_asserteq(BUTTON_ON, button_get_state(dev));
117 
118 	return 0;
119 }
120 DM_TEST(dm_test_button_keys_adc, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
121