Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Tests for the driver model ADC API |
| 4 | * |
| 5 | * Copyright (c) 2015 Samsung Electronics |
| 6 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 7 | */ |
| 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 Glass | 75c4d41 | 2020-07-19 10:15:37 -0600 | [diff] [blame] | 20 | #include <test/test.h> |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 21 | #include <test/ut.h> |
| 22 | |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 23 | static int dm_test_adc_bind(struct unit_test_state *uts) |
| 24 | { |
| 25 | struct udevice *dev; |
Fabrice Gasnier | b341a43 | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 26 | unsigned int channel_mask; |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 27 | |
Peng Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 28 | ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev)); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 29 | ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name); |
| 30 | |
Fabrice Gasnier | b341a43 | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 31 | ut_assertok(adc_channel_mask(dev, &channel_mask)); |
| 32 | ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask); |
| 33 | |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 34 | return 0; |
| 35 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 36 | DM_TEST(dm_test_adc_bind, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 37 | |
| 38 | static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts) |
| 39 | { |
| 40 | struct udevice *dev; |
| 41 | |
Peng Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 42 | ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev)); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 43 | ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS)); |
| 44 | |
| 45 | return 0; |
| 46 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 47 | DM_TEST(dm_test_adc_wrong_channel_selection, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 48 | |
| 49 | static int dm_test_adc_supply(struct unit_test_state *uts) |
| 50 | { |
| 51 | struct udevice *supply; |
| 52 | struct udevice *dev; |
| 53 | int uV; |
| 54 | |
Peng Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 55 | ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev)); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 56 | |
| 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 | |
| 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); |
| 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 Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 83 | DM_TEST(dm_test_adc_supply, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 84 | |
| 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 }, |
| 90 | }; |
| 91 | |
| 92 | static 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 Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 98 | ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev)); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 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); |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 108 | DM_TEST(dm_test_adc_single_channel_conversion, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 109 | |
| 110 | static 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 Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 121 | ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev)); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 122 | 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 Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 131 | DM_TEST(dm_test_adc_multi_channel_conversion, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 132 | |
| 133 | static 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 Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 140 | ut_assertok(adc_channel_single_shot("adc@0", tdata->id, &data)); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 141 | /* Compare the expected and returned conversion data. */ |
| 142 | ut_asserteq(tdata->data, data); |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 147 | DM_TEST(dm_test_adc_single_channel_shot, UT_TESTF_SCAN_FDT); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 148 | |
| 149 | static 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 Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 159 | ut_assertok(adc_channels_single_shot("adc@0", channel_mask, channels)); |
Przemyslaw Marczak | 23bc2a7 | 2015-10-27 13:08:07 +0100 | [diff] [blame] | 160 | |
| 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 Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 167 | DM_TEST(dm_test_adc_multi_channel_shot, UT_TESTF_SCAN_FDT); |
Fabrice Gasnier | b341a43 | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 168 | |
| 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, |
| 178 | }; |
| 179 | |
| 180 | static 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 Fan | 2c1f296 | 2019-05-22 07:08:09 +0000 | [diff] [blame] | 187 | ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc@0", &dev)); |
Fabrice Gasnier | b341a43 | 2018-11-12 14:03:58 +0100 | [diff] [blame] | 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); |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 198 | DM_TEST(dm_test_adc_raw_to_uV, UT_TESTF_SCAN_FDT); |