xref: /openbmc/u-boot/test/dm/adc.c (revision 382bee57)
1 /*
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 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 static int dm_test_adc_bind(struct unit_test_state *uts)
26 {
27 	struct udevice *dev;
28 
29 	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
30 	ut_asserteq_str(SANDBOX_ADC_DEVNAME, dev->name);
31 
32 	return 0;
33 }
34 DM_TEST(dm_test_adc_bind, DM_TESTF_SCAN_FDT);
35 
36 static int dm_test_adc_wrong_channel_selection(struct unit_test_state *uts)
37 {
38 	struct udevice *dev;
39 
40 	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
41 	ut_asserteq(-EINVAL, adc_start_channel(dev, SANDBOX_ADC_CHANNELS));
42 
43 	return 0;
44 }
45 DM_TEST(dm_test_adc_wrong_channel_selection, DM_TESTF_SCAN_FDT);
46 
47 static int dm_test_adc_supply(struct unit_test_state *uts)
48 {
49 	struct udevice *supply;
50 	struct udevice *dev;
51 	int uV;
52 
53 	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
54 
55 	/* Test Vss value - predefined 0 uV */
56 	ut_assertok(adc_vss_value(dev, &uV));
57 	ut_asserteq(SANDBOX_ADC_VSS_VALUE, uV);
58 
59 	/* Test Vdd initial value - buck2 */
60 	ut_assertok(adc_vdd_value(dev, &uV));
61 	ut_asserteq(SANDBOX_BUCK2_INITIAL_EXPECTED_UV, uV);
62 
63 	/* Change Vdd value - buck2 manual preset */
64 	ut_assertok(regulator_get_by_devname(SANDBOX_BUCK2_DEVNAME, &supply));
65 	ut_assertok(regulator_set_value(supply, SANDBOX_BUCK2_SET_UV));
66 	ut_asserteq(SANDBOX_BUCK2_SET_UV, regulator_get_value(supply));
67 
68 	/* Update ADC platdata and get new Vdd value */
69 	ut_assertok(adc_vdd_value(dev, &uV));
70 	ut_asserteq(SANDBOX_BUCK2_SET_UV, uV);
71 
72 	/* Disable buck2 and test ADC supply enable function */
73 	ut_assertok(regulator_set_enable(supply, false));
74 	ut_asserteq(false, regulator_get_enable(supply));
75 	/* adc_start_channel() should enable the supply regulator */
76 	ut_assertok(adc_start_channel(dev, 0));
77 	ut_asserteq(true, regulator_get_enable(supply));
78 
79 	return 0;
80 }
81 DM_TEST(dm_test_adc_supply, DM_TESTF_SCAN_FDT);
82 
83 struct adc_channel adc_channel_test_data[] = {
84 	{ 0, SANDBOX_ADC_CHANNEL0_DATA },
85 	{ 1, SANDBOX_ADC_CHANNEL1_DATA },
86 	{ 2, SANDBOX_ADC_CHANNEL2_DATA },
87 	{ 3, SANDBOX_ADC_CHANNEL3_DATA },
88 };
89 
90 static int dm_test_adc_single_channel_conversion(struct unit_test_state *uts)
91 {
92 	struct adc_channel *tdata = adc_channel_test_data;
93 	unsigned int i, data;
94 	struct udevice *dev;
95 
96 	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
97 	/* Test each ADC channel's value */
98 	for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
99 		ut_assertok(adc_start_channel(dev, tdata->id));
100 		ut_assertok(adc_channel_data(dev, tdata->id, &data));
101 		ut_asserteq(tdata->data, data);
102 	}
103 
104 	return 0;
105 }
106 DM_TEST(dm_test_adc_single_channel_conversion, DM_TESTF_SCAN_FDT);
107 
108 static int dm_test_adc_multi_channel_conversion(struct unit_test_state *uts)
109 {
110 	struct adc_channel channels[SANDBOX_ADC_CHANNELS];
111 	struct udevice *dev;
112 	struct adc_channel *tdata = adc_channel_test_data;
113 	unsigned int i, channel_mask;
114 
115 	channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
116 		       ADC_CHANNEL(2) | ADC_CHANNEL(3);
117 
118 	/* Start multi channel conversion */
119 	ut_assertok(uclass_get_device_by_name(UCLASS_ADC, "adc", &dev));
120 	ut_assertok(adc_start_channels(dev, channel_mask));
121 	ut_assertok(adc_channels_data(dev, channel_mask, channels));
122 
123 	/* Compare the expected and returned conversion data. */
124 	for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
125 		ut_asserteq(tdata->data, channels[i].data);
126 
127 	return 0;
128 }
129 DM_TEST(dm_test_adc_multi_channel_conversion, DM_TESTF_SCAN_FDT);
130 
131 static int dm_test_adc_single_channel_shot(struct unit_test_state *uts)
132 {
133 	struct adc_channel *tdata = adc_channel_test_data;
134 	unsigned int i, data;
135 
136 	for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++) {
137 		/* Start single channel conversion */
138 		ut_assertok(adc_channel_single_shot("adc", tdata->id, &data));
139 		/* Compare the expected and returned conversion data. */
140 		ut_asserteq(tdata->data, data);
141 	}
142 
143 	return 0;
144 }
145 DM_TEST(dm_test_adc_single_channel_shot, DM_TESTF_SCAN_FDT);
146 
147 static int dm_test_adc_multi_channel_shot(struct unit_test_state *uts)
148 {
149 	struct adc_channel channels[SANDBOX_ADC_CHANNELS];
150 	struct adc_channel *tdata = adc_channel_test_data;
151 	unsigned int i, channel_mask;
152 
153 	channel_mask = ADC_CHANNEL(0) | ADC_CHANNEL(1) |
154 		       ADC_CHANNEL(2) | ADC_CHANNEL(3);
155 
156 	/* Start single call and multi channel conversion */
157 	ut_assertok(adc_channels_single_shot("adc", channel_mask, channels));
158 
159 	/* Compare the expected and returned conversion data. */
160 	for (i = 0; i < SANDBOX_ADC_CHANNELS; i++, tdata++)
161 		ut_asserteq(tdata->data, channels[i].data);
162 
163 	return 0;
164 }
165 DM_TEST(dm_test_adc_multi_channel_shot, DM_TESTF_SCAN_FDT);
166