blob: e27302b449e815b4bd7df23c9d7820c75cfde943 [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
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +01009#include <adc.h>
10#include <dm.h>
11#include <dm/root.h>
12#include <dm/util.h>
13#include <dm/test.h>
14#include <errno.h>
15#include <fdtdec.h>
16#include <power/regulator.h>
17#include <power/sandbox_pmic.h>
18#include <sandbox-adc.h>
Simon Glass75c4d412020-07-19 10:15:37 -060019#include <test/test.h>
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010020#include <test/ut.h>
21
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010022static int dm_test_adc_bind(struct unit_test_state *uts)
23{
24 struct udevice *dev;
Fabrice Gasnierb341a432018-11-12 14:03:58 +010025 unsigned int channel_mask;
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010026
Peng Fan2c1f2962019-05-22 07:08:09 +000027 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010028 ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
29
Fabrice Gasnierb341a432018-11-12 14:03:58 +010030 ut_assertok(adc_channel_mask(dev, &channel_mask));
31 ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask);
32
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010033 return 0;
34}
Simon Glass1a92f832024-08-22 07:57:48 -060035DM_TEST(dm_test_adc_bind, UTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010036
37static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
38{
39 struct udevice *dev;
40
Peng Fan2c1f2962019-05-22 07:08:09 +000041 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010042 ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
43
44 return 0;
45}
Simon Glass1a92f832024-08-22 07:57:48 -060046DM_TEST(dm_test_adc_wrong_channel_selection, UTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010047
48static int dm_test_adc_supply(struct unit_test_state *uts)
49{
50 struct udevice *supply;
51 struct udevice *dev;
52 int uV;
53
Peng Fan2c1f2962019-05-22 07:08:09 +000054 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010055
56 /* Test Vss value - predefined 0 uV */
57 ut_assertok(adc_vss_value(dev, &uV));
58 ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
59
60 /* Test Vdd initial value - buck2 */
61 ut_assertok(adc_vdd_value(dev, &uV));
62 ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
63
64 /* Change Vdd value - buck2 manual preset */
65 ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
66 ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
67 ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
68
Simon Glass71fa5b42020-12-03 16:55:18 -070069 /* Update ADC plat and get new Vdd value */
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010070 ut_assertok(adc_vdd_value(dev, &uV));
71 ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
72
73 /* Disable buck2 and test ADC supply enable function */
74 ut_assertok(regulator_set_enable(supply, false));
75 ut_asserteq(false, regulator_get_enable(supply));
76 /* adc_start_channel() should enable the supply regulator */
77 ut_assertok(adc_start_channel(dev, 0));
78 ut_asserteq(true, regulator_get_enable(supply));
79
80 return 0;
81}
Simon Glass1a92f832024-08-22 07:57:48 -060082DM_TEST(dm_test_adc_supply, UTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010083
84struct adc_channel adc_channel_test_data[] = {
85 { 0, SANDBOX_ADC_CHANNEL0_DATA },
86 { 1, SANDBOX_ADC_CHANNEL1_DATA },
87 { 2, SANDBOX_ADC_CHANNEL2_DATA },
88 { 3, SANDBOX_ADC_CHANNEL3_DATA },
89};
90
91static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
92{
93 struct adc_channel *tdata = adc_channel_test_data;
94 unsigned int i, data;
95 struct udevice *dev;
96
Peng Fan2c1f2962019-05-22 07:08:09 +000097 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010098 /* Test each ADC channel's value */
99 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
100 ut_assertok(adc_start_channel(dev, tdata->id));
101 ut_assertok(adc_channel_data(dev, tdata->id, &data));
102 ut_asserteq(tdata->data, data);
103 }
104
105 return 0;
106}
Simon Glass1a92f832024-08-22 07:57:48 -0600107DM_TEST(dm_test_adc_single_channel_conversion, UTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100108
109static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
110{
111 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
112 struct udevice *dev;
113 struct adc_channel *tdata = adc_channel_test_data;
114 unsigned int i, channel_mask;
115
116 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
117 ADC_CHANNEL(2) | ADC_CHANNEL(3);
118
119 /* Start multi channel conversion */
Peng Fan2c1f2962019-05-22 07:08:09 +0000120 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100121 ut_assertok(adc_start_channels(dev, channel_mask));
122 ut_assertok(adc_channels_data(dev, channel_mask, channels));
123
124 /* Compare the expected and returned conversion data. */
125 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
126 ut_asserteq(tdata->data, channels[i].data);
127
128 return 0;
129}
Simon Glass1a92f832024-08-22 07:57:48 -0600130DM_TEST(dm_test_adc_multi_channel_conversion, UTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100131
132static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
133{
134 struct adc_channel *tdata = adc_channel_test_data;
135 unsigned int i, data;
136
137 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
138 /* Start single channel conversion */
Peng Fan2c1f2962019-05-22 07:08:09 +0000139 ut_assertok(adc_channel_single_shot("adc@0", tdata->id, &data));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100140 /* Compare the expected and returned conversion data. */
141 ut_asserteq(tdata->data, data);
142 }
143
144 return 0;
145}
Simon Glass1a92f832024-08-22 07:57:48 -0600146DM_TEST(dm_test_adc_single_channel_shot, UTF_SCAN_FDT);
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100147
148static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
149{
150 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
151 struct adc_channel *tdata = adc_channel_test_data;
152 unsigned int i, channel_mask;
153
154 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
155 ADC_CHANNEL(2) | ADC_CHANNEL(3);
156
157 /* Start single call and multi channel conversion */
Peng Fan2c1f2962019-05-22 07:08:09 +0000158 ut_assertok(adc_channels_single_shot("adc@0", channel_mask, channels));
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +0100159
160 /* Compare the expected and returned conversion data. */
161 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
162 ut_asserteq(tdata->data, channels[i].data);
163
164 return 0;
165}
Simon Glass1a92f832024-08-22 07:57:48 -0600166DM_TEST(dm_test_adc_multi_channel_shot, UTF_SCAN_FDT);
Fabrice Gasnierb341a432018-11-12 14:03:58 +0100167
168static const int dm_test_adc_uV_data[SANDBOX_ADC_CHANNELS] = {
169 ((u64)SANDBOX_ADC_CHANNEL0_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
170 SANDBOX_ADC_DATA_MASK,
171 ((u64)SANDBOX_ADC_CHANNEL1_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
172 SANDBOX_ADC_DATA_MASK,
173 ((u64)SANDBOX_ADC_CHANNEL2_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
174 SANDBOX_ADC_DATA_MASK,
175 ((u64)SANDBOX_ADC_CHANNEL3_DATA * SANDBOX_BUCK2_INITIAL_EXPECTED_UV) /
176 SANDBOX_ADC_DATA_MASK,
177};
178
179static int dm_test_adc_raw_to_uV(struct unit_test_state *uts)
180{
181 struct adc_channel *tdata = adc_channel_test_data;
182 unsigned int i, data;
183 struct udevice *dev;
184 int uV;
185
Peng Fan2c1f2962019-05-22 07:08:09 +0000186 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
Fabrice Gasnierb341a432018-11-12 14:03:58 +0100187 /* Test each ADC channel's value in microvolts */
188 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
189 ut_assertok(adc_start_channel(dev, tdata->id));
190 ut_assertok(adc_channel_data(dev, tdata->id, &data));
191 ut_assertok(adc_raw_to_uV(dev, data, &uV));
192 ut_asserteq(dm_test_adc_uV_data[i], uV);
193 }
194
195 return 0;
196}
Simon Glass1a92f832024-08-22 07:57:48 -0600197DM_TEST(dm_test_adc_raw_to_uV, UTF_SCAN_FDT);