uboot/test/dm/adc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Tests for the driver model ADC API
   4 *
   5 * Copyright (c) 2015 Samsung Electronics
   6 * Przemyslaw Marczak <p.marczak@samsung.com>
   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>
  20#include <test/ut.h>
  21
  22static int dm_test_adc_bind(struct unit_test_state *uts)
  23{
  24        struct udevice *dev;
  25        unsigned int channel_mask;
  26
  27        ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
  28        ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
  29
  30        ut_assertok(adc_channel_mask(dev, &channel_mask));
  31        ut_asserteq((1 << SANDBOX_ADC_CHANNELS) - 1, channel_mask);
  32
  33        return 0;
  34}
  35DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
  36
  37static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
  38{
  39        struct udevice *dev;
  40
  41        ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
  42        ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
  43
  44        return 0;
  45}
  46DM_TEST(dm_test_adc_wrong_channel_selection, DM_TESTF_SCAN_FDT);
  47
  48static int dm_test_adc_supply(struct unit_test_state *uts)
  49{
  50        struct udevice *supply;
  51        struct udevice *dev;
  52        int uV;
  53
  54        ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
  55
  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
  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);
  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}
  82DM_TEST(dm_test_adc_supply, DM_TESTF_SCAN_FDT);
  83
  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
  97        ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &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);
 103        }
 104
 105        return 0;
 106}
 107DM_TEST(dm_test_adc_single_channel_conversion, DM_TESTF_SCAN_FDT);
 108
 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 */
 120        ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
 121        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}
 130DM_TEST(dm_test_adc_multi_channel_conversion, DM_TESTF_SCAN_FDT);
 131
 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 */
 139                ut_assertok(adc_channel_single_shot("adc", tdata->id, &data));
 140                /* Compare the expected and returned conversion data. */
 141                ut_asserteq(tdata->data, data);
 142        }
 143
 144        return 0;
 145}
 146DM_TEST(dm_test_adc_single_channel_shot, DM_TESTF_SCAN_FDT);
 147
 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 */
 158        ut_assertok(adc_channels_single_shot("adc", channel_mask, channels));
 159
 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}
 166DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);
 167
 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
 186        ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &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);
 193        }
 194
 195        return 0;
 196}
 197DM_TEST(dm_test_adc_raw_to_uV, DM_TESTF_SCAN_FDT);
 198