blob: 740167e16b8edf4c335e95f8d94c69d16d01ebc6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +01002/*
3 * Tests for the driver model ADC API
4 *
5 * Copyright (c) 2015 Samsung Electronics
6 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +01007 */
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>
Simon Glass75c4d412020-07-19 10:15:37 -060020#include <test/test.h>
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010021#include <test/ut.h>
22
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010023static int dm_test_adc_bind(struct unit_test_state *uts)
24{
25 struct udevice *dev;
Fabrice Gasnierb341a432018-11-12 14:03:58 +010026 unsigned int channel_mask;
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010027
Peng Fan2c1f2962019-05-22 07:08:09 +000028 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010029 ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
30
Fabrice Gasnierb341a432018-11-12 14:03:58 +010031 ut_assertok(adc_channel_mask(dev, &channel_mask));
32 ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask);
33
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010034 return 0;
35}
Simon Glass974dccd2020-07-28 19:41:12 -060036DM_TEST(dm_test_adc_bind, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010037
38static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
39{
40 struct udevice *dev;
41
Peng Fan2c1f2962019-05-22 07:08:09 +000042 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010043 ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
44
45 return 0;
46}
Simon Glass974dccd2020-07-28 19:41:12 -060047DM_TEST(dm_test_adc_wrong_channel_selection, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010048
49static int dm_test_adc_supply(struct unit_test_state *uts)
50{
51 struct udevice *supply;
52 struct udevice *dev;
53 int uV;
54
Peng Fan2c1f2962019-05-22 07:08:09 +000055 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010056
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
Simon Glass71fa5b42020-12-03 16:55:18 -070070 /* Update ADC plat and get new Vdd value */
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010071 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}
Simon Glass974dccd2020-07-28 19:41:12 -060083DM_TEST(dm_test_adc_supply, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010084
85struct 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
92static 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
Peng Fan2c1f2962019-05-22 07:08:09 +000098 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010099 /* 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600108DM_TEST(dm_test_adc_single_channel_conversion, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100109
110static 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 */
Peng Fan2c1f2962019-05-22 07:08:09 +0000121 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100122 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600131DM_TEST(dm_test_adc_multi_channel_conversion, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100132
133static 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 */
Peng Fan2c1f2962019-05-22 07:08:09 +0000140 ut_assertok(adc_channel_single_shot("adc@0", tdata->id, &data));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100141 /* Compare the expected and returned conversion data. */
142 ut_asserteq(tdata->data, data);
143 }
144
145 return 0;
146}
Simon Glass974dccd2020-07-28 19:41:12 -0600147DM_TEST(dm_test_adc_single_channel_shot, UT_TESTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100148
149static 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 */
Peng Fan2c1f2962019-05-22 07:08:09 +0000159 ut_assertok(adc_channels_single_shot("adc@0", channel_mask, channels));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100160
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}
Simon Glass974dccd2020-07-28 19:41:12 -0600167DM_TEST(dm_test_adc_multi_channel_shot, UT_TESTF_SCAN_FDT);
Fabrice Gasnierb341a432018-11-12 14:03:58 +0100168
169static 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
180static 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
Peng Fan2c1f2962019-05-22 07:08:09 +0000187 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Fabrice Gasnierb341a432018-11-12 14:03:58 +0100188 /* 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600198DM_TEST(dm_test_adc_raw_to_uV, UT_TESTF_SCAN_FDT);