blob: 85535d9eabc75fac3fe9daf2a1d1675168e38b71 [file] [log] [blame]
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +01001/*
2 * Tests for the driver model ADC API
3 *
4 * Copyright (c) 2015 Samsung Electronics
5 * Przemyslaw Marczak <p.marczak@samsung.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <adc.h>
12#include <dm.h>
13#include <dm/root.h>
14#include <dm/util.h>
15#include <dm/test.h>
16#include <errno.h>
17#include <fdtdec.h>
18#include <power/regulator.h>
19#include <power/sandbox_pmic.h>
20#include <sandbox-adc.h>
21#include <test/ut.h>
22
Przemyslaw Marczak23bc2a72015-10-27 13:08:07 +010023static int dm_test_adc_bind(struct unit_test_state *uts)
24{
25 struct udevice *dev;
26
27 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
28 ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
29
30 return 0;
31}
32DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
33
34static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
35{
36 struct udevice *dev;
37
38 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
39 ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
40
41 return 0;
42}
43DM_TEST(dm_test_adc_wrong_channel_selection, DM_TESTF_SCAN_FDT);
44
45static int dm_test_adc_supply(struct unit_test_state *uts)
46{
47 struct udevice *supply;
48 struct udevice *dev;
49 int uV;
50
51 ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
52
53 /* Test Vss value - predefined 0 uV */
54 ut_assertok(adc_vss_value(dev, &uV));
55 ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
56
57 /* Test Vdd initial value - buck2 */
58 ut_assertok(adc_vdd_value(dev, &uV));
59 ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
60
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));
65
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);
69
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));
76
77 return 0;
78}
79DM_TEST(dm_test_adc_supply, DM_TESTF_SCAN_FDT);
80
81struct 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 },
86};
87
88static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
89{
90 struct adc_channel *tdata = adc_channel_test_data;
91 unsigned int i, data;
92 struct udevice *dev;
93
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);
100 }
101
102 return 0;
103}
104DM_TEST(dm_test_adc_single_channel_conversion, DM_TESTF_SCAN_FDT);
105
106static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
107{
108 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
109 struct udevice *dev;
110 struct adc_channel *tdata = adc_channel_test_data;
111 unsigned int i, channel_mask;
112
113 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
114 ADC_CHANNEL(2) | ADC_CHANNEL(3);
115
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));
120
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);
124
125 return 0;
126}
127DM_TEST(dm_test_adc_multi_channel_conversion, DM_TESTF_SCAN_FDT);
128
129static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
130{
131 struct adc_channel *tdata = adc_channel_test_data;
132 unsigned int i, data;
133
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);
139 }
140
141 return 0;
142}
143DM_TEST(dm_test_adc_single_channel_shot, DM_TESTF_SCAN_FDT);
144
145static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
146{
147 struct adc_channel channels[SANDBOX_ADC_CHANNELS];
148 struct adc_channel *tdata = adc_channel_test_data;
149 unsigned int i, channel_mask;
150
151 channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
152 ADC_CHANNEL(2) | ADC_CHANNEL(3);
153
154 /* Start single call and multi channel conversion */
155 ut_assertok(adc_channels_single_shot("adc", channel_mask, channels));
156
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);
160
161 return 0;
162}
163DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);