1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the driver model ADC API
5 * Copyright (c) 2015 Samsung Electronics
6 * Przemyslaw Marczak <p.marczak@samsung.com>
17 #include <power/regulator.h>
18 #include <power/sandbox_pmic.h>
19 #include <sandbox-adc.h>
22 static int dm_test_adc_bind(struct unit_test_state *uts)
25 unsigned int channel_mask;
27 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
28 ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
30 ut_assertok(adc_channel_mask(dev, &channel_mask));
31 ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask);
35 DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
37 static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
41 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
42 ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
46 DM_TEST(dm_test_adc_wrong_channel_selection, DM_TESTF_SCAN_FDT);
48 static int dm_test_adc_supply(struct unit_test_state *uts)
50 struct udevice *supply;
54 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
56 /* Test Vss value - predefined 0 uV */
57 ut_assertok(adc_vss_value(dev, &uV));
58 ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
60 /* Test Vdd initial value - buck2 */
61 ut_assertok(adc_vdd_value(dev, &uV));
62 ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
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));
69 /* Update ADC platdata and get new Vdd value */
70 ut_assertok(adc_vdd_value(dev, &uV));
71 ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
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));
82 DM_TEST(dm_test_adc_supply, DM_TESTF_SCAN_FDT);
84 struct 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 },
91 static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
93 struct adc_channel *tdata = adc_channel_test_data;
97 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
98 /* 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);
107 DM_TEST(dm_test_adc_single_channel_conversion, DM_TESTF_SCAN_FDT);
109 static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
111 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
113 struct adc_channel *tdata = adc_channel_test_data;
114 unsigned int i, channel_mask;
116 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
117 ADC_CHANNEL(2) | ADC_CHANNEL(3);
119 /* Start multi channel conversion */
120 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
121 ut_assertok(adc_start_channels(dev, channel_mask));
122 ut_assertok(adc_channels_data(dev, channel_mask, channels));
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);
130 DM_TEST(dm_test_adc_multi_channel_conversion, DM_TESTF_SCAN_FDT);
132 static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
134 struct adc_channel *tdata = adc_channel_test_data;
135 unsigned int i, data;
137 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
138 /* Start single channel conversion */
139 ut_assertok(adc_channel_single_shot("adc@0", tdata->id, &data));
140 /* Compare the expected and returned conversion data. */
141 ut_asserteq(tdata->data, data);
146 DM_TEST(dm_test_adc_single_channel_shot, DM_TESTF_SCAN_FDT);
148 static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
150 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
151 struct adc_channel *tdata = adc_channel_test_data;
152 unsigned int i, channel_mask;
154 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
155 ADC_CHANNEL(2) | ADC_CHANNEL(3);
157 /* Start single call and multi channel conversion */
158 ut_assertok(adc_channels_single_shot("adc@0", channel_mask, channels));
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);
166 DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);
168 static 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,
179 static int dm_test_adc_raw_to_uV(struct unit_test_state *uts)
181 struct adc_channel *tdata = adc_channel_test_data;
182 unsigned int i, data;
186 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
187 /* 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);
197 DM_TEST(dm_test_adc_raw_to_uV, DM_TESTF_SCAN_FDT);