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