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>
20 #include <test/test.h>
23 static int dm_test_adc_bind(struct unit_test_state *uts)
26 unsigned int channel_mask;
28 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
29 ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
31 ut_assertok(adc_channel_mask(dev, &channel_mask));
32 ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask);
36 DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
38 static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
42 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
43 ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
47 DM_TEST(dm_test_adc_wrong_channel_selection, DM_TESTF_SCAN_FDT);
49 static int dm_test_adc_supply(struct unit_test_state *uts)
51 struct udevice *supply;
55 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev));
57 /* Test Vss value - predefined 0 uV */
58 ut_assertok(adc_vss_value(dev, &uV));
59 ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
61 /* Test Vdd initial value - buck2 */
62 ut_assertok(adc_vdd_value(dev, &uV));
63 ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
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));
70 /* Update ADC platdata and get new Vdd value */
71 ut_assertok(adc_vdd_value(dev, &uV));
72 ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
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));
83 DM_TEST(dm_test_adc_supply, DM_TESTF_SCAN_FDT);
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 },
92 static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
94 struct adc_channel *tdata = adc_channel_test_data;
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);
108 DM_TEST(dm_test_adc_single_channel_conversion, DM_TESTF_SCAN_FDT);
110 static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
112 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
114 struct adc_channel *tdata = adc_channel_test_data;
115 unsigned int i, channel_mask;
117 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
118 ADC_CHANNEL(2) | ADC_CHANNEL(3);
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));
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);
131 DM_TEST(dm_test_adc_multi_channel_conversion, DM_TESTF_SCAN_FDT);
133 static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
135 struct adc_channel *tdata = adc_channel_test_data;
136 unsigned int i, data;
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);
147 DM_TEST(dm_test_adc_single_channel_shot, DM_TESTF_SCAN_FDT);
149 static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
151 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
152 struct adc_channel *tdata = adc_channel_test_data;
153 unsigned int i, channel_mask;
155 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
156 ADC_CHANNEL(2) | ADC_CHANNEL(3);
158 /* Start single call and multi channel conversion */
159 ut_assertok(adc_channels_single_shot("adc@0", channel_mask, channels));
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);
167 DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);
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,
180 static int dm_test_adc_raw_to_uV(struct unit_test_state *uts)
182 struct adc_channel *tdata = adc_channel_test_data;
183 unsigned int i, data;
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);
198 DM_TEST(dm_test_adc_raw_to_uV, DM_TESTF_SCAN_FDT);