Merge tag 'u-boot-amlogic-20220120' of https://source.denx.de/u-boot/custodians/u...
[platform/kernel/u-boot.git] / test / dm / adc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Tests for the driver model ADC API
4  *
5  * Copyright (c) 2015 Samsung Electronics
6  * Przemyslaw Marczak <p.marczak@samsung.com>
7  */
8
9 #include <common.h>
10 #include <adc.h>
11 #include <dm.h>
12 #include <dm/root.h>
13 #include <dm/util.h>
14 #include <dm/test.h>
15 #include <errno.h>
16 #include <fdtdec.h>
17 #include <power/regulator.h>
18 #include <power/sandbox_pmic.h>
19 #include <sandbox-adc.h>
20 #include <test/test.h>
21 #include <test/ut.h>
22
23 static int dm_test_adc_bind(struct unit_test_state *uts)
24 {
25         struct udevice *dev;
26         unsigned int channel_mask;
27
28         ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
29         ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
30
31         ut_assertok(adc_channel_mask(dev, &channel_mask));
32         ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask);
33
34         return 0;
35 }
36 DM_TEST(dm_test_adc_bind, UT_TESTF_SCAN_FDT);
37
38 static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
39 {
40         struct udevice *dev;
41
42         ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
43         ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
44
45         return 0;
46 }
47 DM_TEST(dm_test_adc_wrong_channel_selection, UT_TESTF_SCAN_FDT);
48
49 static int dm_test_adc_supply(struct unit_test_state *uts)
50 {
51         struct udevice *supply;
52         struct udevice *dev;
53         int uV;
54
55         ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
56
57         /* Test Vss value - predefined 0 uV */
58         ut_assertok(adc_vss_value(dev, &uV));
59         ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
60
61         /* Test Vdd initial value - buck2 */
62         ut_assertok(adc_vdd_value(dev, &uV));
63         ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
64
65         /* Change Vdd value - buck2 manual preset */
66         ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
67         ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
68         ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
69
70         /* Update ADC plat and get new Vdd value */
71         ut_assertok(adc_vdd_value(dev, &uV));
72         ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
73
74         /* Disable buck2 and test ADC supply enable function */
75         ut_assertok(regulator_set_enable(supply, false));
76         ut_asserteq(false, regulator_get_enable(supply));
77         /* adc_start_channel() should enable the supply regulator */
78         ut_assertok(adc_start_channel(dev, 0));
79         ut_asserteq(true, regulator_get_enable(supply));
80
81         return 0;
82 }
83 DM_TEST(dm_test_adc_supply, UT_TESTF_SCAN_FDT);
84
85 struct adc_channel adc_channel_test_data[] = {
86         { 0, SANDBOX_ADC_CHANNEL0_DATA },
87         { 1, SANDBOX_ADC_CHANNEL1_DATA },
88         { 2, SANDBOX_ADC_CHANNEL2_DATA },
89         { 3, SANDBOX_ADC_CHANNEL3_DATA },
90 };
91
92 static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
93 {
94         struct adc_channel *tdata = adc_channel_test_data;
95         unsigned int i, data;
96         struct udevice *dev;
97
98         ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
99         /* Test each ADC channel's value */
100         for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
101                 ut_assertok(adc_start_channel(dev, tdata->id));
102                 ut_assertok(adc_channel_data(dev, tdata->id, &data));
103                 ut_asserteq(tdata->data, data);
104         }
105
106         return 0;
107 }
108 DM_TEST(dm_test_adc_single_channel_conversion, UT_TESTF_SCAN_FDT);
109
110 static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
111 {
112         struct adc_channel channels[SANDBOX_ADC_CHANNELS];
113         struct udevice *dev;
114         struct adc_channel *tdata = adc_channel_test_data;
115         unsigned int i, channel_mask;
116
117         channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
118                        ADC_CHANNEL(2) | ADC_CHANNEL(3);
119
120         /* Start multi channel conversion */
121         ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
122         ut_assertok(adc_start_channels(dev, channel_mask));
123         ut_assertok(adc_channels_data(dev, channel_mask, channels));
124
125         /* Compare the expected and returned conversion data. */
126         for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
127                 ut_asserteq(tdata->data, channels[i].data);
128
129         return 0;
130 }
131 DM_TEST(dm_test_adc_multi_channel_conversion, UT_TESTF_SCAN_FDT);
132
133 static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
134 {
135         struct adc_channel *tdata = adc_channel_test_data;
136         unsigned int i, data;
137
138         for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
139                 /* Start single channel conversion */
140                 ut_assertok(adc_channel_single_shot("adc@0", tdata->id, &data));
141                 /* Compare the expected and returned conversion data. */
142                 ut_asserteq(tdata->data, data);
143         }
144
145         return 0;
146 }
147 DM_TEST(dm_test_adc_single_channel_shot, UT_TESTF_SCAN_FDT);
148
149 static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
150 {
151         struct adc_channel channels[SANDBOX_ADC_CHANNELS];
152         struct adc_channel *tdata = adc_channel_test_data;
153         unsigned int i, channel_mask;
154
155         channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
156                        ADC_CHANNEL(2) | ADC_CHANNEL(3);
157
158         /* Start single call and multi channel conversion */
159         ut_assertok(adc_channels_single_shot("adc@0", channel_mask, channels));
160
161         /* Compare the expected and returned conversion data. */
162         for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
163                 ut_asserteq(tdata->data, channels[i].data);
164
165         return 0;
166 }
167 DM_TEST(dm_test_adc_multi_channel_shot, UT_TESTF_SCAN_FDT);
168
169 static const int dm_test_adc_uV_data[SANDBOX_ADC_CHANNELS] = {
170         ((u64)SANDBOX_ADC_CHANNEL0_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
171                 SANDBOX_ADC_DATA_MASK,
172         ((u64)SANDBOX_ADC_CHANNEL1_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
173                 SANDBOX_ADC_DATA_MASK,
174         ((u64)SANDBOX_ADC_CHANNEL2_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
175                 SANDBOX_ADC_DATA_MASK,
176         ((u64)SANDBOX_ADC_CHANNEL3_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
177                 SANDBOX_ADC_DATA_MASK,
178 };
179
180 static int dm_test_adc_raw_to_uV(struct unit_test_state *uts)
181 {
182         struct adc_channel *tdata = adc_channel_test_data;
183         unsigned int i, data;
184         struct udevice *dev;
185         int uV;
186
187         ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
188         /* Test each ADC channel's value in microvolts */
189         for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
190                 ut_assertok(adc_start_channel(dev, tdata->id));
191                 ut_assertok(adc_channel_data(dev, tdata->id, &data));
192                 ut_assertok(adc_raw_to_uV(dev, data, &uV));
193                 ut_asserteq(dm_test_adc_uV_data[i], uV);
194         }
195
196         return 0;
197 }
198 DM_TEST(dm_test_adc_raw_to_uV, UT_TESTF_SCAN_FDT);