2 * Tests for the driver model ADC API
4 * Copyright (c) 2015 Samsung Electronics
5 * Przemyslaw Marczak <p.marczak@samsung.com>
7 * SPDX-License-Identifier: GPL-2.0+
18 #include <power/regulator.h>
19 #include <power/sandbox_pmic.h>
20 #include <sandbox-adc.h>
23 static int dm_test_adc_bind(struct unit_test_state *uts)
27 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
28 ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
32 DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
34 static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
38 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
39 ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
43 DM_TEST(dm_test_adc_wrong_channel_selection, DM_TESTF_SCAN_FDT);
45 static int dm_test_adc_supply(struct unit_test_state *uts)
47 struct udevice *supply;
51 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
53 /* Test Vss value - predefined 0 uV */
54 ut_assertok(adc_vss_value(dev, &uV));
55 ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
57 /* Test Vdd initial value - buck2 */
58 ut_assertok(adc_vdd_value(dev, &uV));
59 ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
61 /* Change Vdd value - buck2 manual preset */
62 ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
63 ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
64 ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
66 /* Update ADC platdata and get new Vdd value */
67 ut_assertok(adc_vdd_value(dev, &uV));
68 ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
70 /* Disable buck2 and test ADC supply enable function */
71 ut_assertok(regulator_set_enable(supply, false));
72 ut_asserteq(false, regulator_get_enable(supply));
73 /* adc_start_channel() should enable the supply regulator */
74 ut_assertok(adc_start_channel(dev, 0));
75 ut_asserteq(true, regulator_get_enable(supply));
79 DM_TEST(dm_test_adc_supply, DM_TESTF_SCAN_FDT);
81 struct adc_channel adc_channel_test_data[] = {
82 { 0, SANDBOX_ADC_CHANNEL0_DATA },
83 { 1, SANDBOX_ADC_CHANNEL1_DATA },
84 { 2, SANDBOX_ADC_CHANNEL2_DATA },
85 { 3, SANDBOX_ADC_CHANNEL3_DATA },
88 static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
90 struct adc_channel *tdata = adc_channel_test_data;
94 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
95 /* Test each ADC channel's value */
96 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
97 ut_assertok(adc_start_channel(dev, tdata->id));
98 ut_assertok(adc_channel_data(dev, tdata->id, &data));
99 ut_asserteq(tdata->data, data);
104 DM_TEST(dm_test_adc_single_channel_conversion, DM_TESTF_SCAN_FDT);
106 static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
108 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
110 struct adc_channel *tdata = adc_channel_test_data;
111 unsigned int i, channel_mask;
113 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
114 ADC_CHANNEL(2) | ADC_CHANNEL(3);
116 /* Start multi channel conversion */
117 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
118 ut_assertok(adc_start_channels(dev, channel_mask));
119 ut_assertok(adc_channels_data(dev, channel_mask, channels));
121 /* Compare the expected and returned conversion data. */
122 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
123 ut_asserteq(tdata->data, channels[i].data);
127 DM_TEST(dm_test_adc_multi_channel_conversion, DM_TESTF_SCAN_FDT);
129 static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
131 struct adc_channel *tdata = adc_channel_test_data;
132 unsigned int i, data;
134 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
135 /* Start single channel conversion */
136 ut_assertok(adc_channel_single_shot("adc", tdata->id, &data));
137 /* Compare the expected and returned conversion data. */
138 ut_asserteq(tdata->data, data);
143 DM_TEST(dm_test_adc_single_channel_shot, DM_TESTF_SCAN_FDT);
145 static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
147 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
148 struct adc_channel *tdata = adc_channel_test_data;
149 unsigned int i, channel_mask;
151 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
152 ADC_CHANNEL(2) | ADC_CHANNEL(3);
154 /* Start single call and multi channel conversion */
155 ut_assertok(adc_channels_single_shot("adc", channel_mask, channels));
157 /* Compare the expected and returned conversion data. */
158 for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
159 ut_asserteq(tdata->data, channels[i].data);
163 DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);