Debian Codegraph @

thirdparty/analogdevicesinc/no-OS_local/platform_bladerf2/adc_core.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file adc_core.c
3 * @brief Implementation of ADC Core Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43
44 #include <stdint.h>
45
46 #include "adc_core.h"
47 #include "platform.h"
48 #include "util.h"
49
50 /***************************************************************************//**
51 * @brief adc_read
52 *******************************************************************************/
53 int adc_read(struct ad9361_rf_phy *phy, uint32_t regAddr, uint32_t *data)
54 {
55 return axiadc_read(phy->adc_state, regAddr, data);
56 }
57
58 /***************************************************************************//**
59 * @brief adc_write
60 *******************************************************************************/
61 int adc_write(struct ad9361_rf_phy *phy, uint32_t regAddr, uint32_t data)
62 {
63 return axiadc_write(phy->adc_state, regAddr, data);
64 }
65
66 /***************************************************************************//**
67 * @brief adc_init
68 *******************************************************************************/
69 int adc_init(struct ad9361_rf_phy *phy)
70 {
71 int ret;
72
73 ret = adc_write(phy, ADC_REG_RSTN, 0);
74 if (ret < 0) {
75 return ret;
76 }
77
78 ret = adc_write(phy, ADC_REG_RSTN, ADC_RSTN);
79 if (ret < 0) {
80 return ret;
81 }
82
83 ret = adc_write(phy, ADC_REG_CHAN_CNTRL(0),
84 ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE | ADC_ENABLE);
85 if (ret < 0) {
86 return ret;
87 }
88
89 ret = adc_write(phy, ADC_REG_CHAN_CNTRL(1),
90 ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE | ADC_ENABLE);
91 if (ret < 0) {
92 return ret;
93 }
94
95 if (phy->pdata->rx2tx2)
96 {
97 ret = adc_write(phy, ADC_REG_CHAN_CNTRL(2),
98 ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE | ADC_ENABLE);
99 if (ret < 0) {
100 return ret;
101 }
102
103 ret = adc_write(phy, ADC_REG_CHAN_CNTRL(3),
104 ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE | ADC_ENABLE);
105 if (ret < 0) {
106 return ret;
107 }
108 }
109
110 return 0;
111 }
112
113 /***************************************************************************//**
114 * @brief adc_set_calib_scale_phase
115 *******************************************************************************/
116 int32_t adc_set_calib_scale_phase(struct ad9361_rf_phy *phy,
117 uint32_t phase,
118 uint32_t chan,
119 int32_t val,
120 int32_t val2)
121 {
122 int ret;
123 uint32_t fract;
124 uint64_t llval;
125 uint32_t tmp;
126
127 switch (val) {
128 case 1:
129 fract = 0x4000;
130 break;
131 case -1:
132 fract = 0xC000;
133 break;
134 case 0:
135 fract = 0;
136 if (val2 < 0) {
137 fract = 0x8000;
138 val2 *= -1;
139 }
140 break;
141 default:
142 return -1;
143 }
144
145 llval = (uint64_t)val2 * 0x4000UL + (1000000UL / 2);
146 do_div(&llval, 1000000UL);
147 fract |= llval;
148
149 ret = adc_read(phy, ADC_REG_CHAN_CNTRL_2(chan), &tmp);
150 if (ret < 0) {
151 return ret;
152 }
153
154 if (!((chan + phase) % 2)) {
155 tmp &= ~ADC_IQCOR_COEFF_1(~0);
156 tmp |= ADC_IQCOR_COEFF_1(fract);
157 } else {
158 tmp &= ~ADC_IQCOR_COEFF_2(~0);
159 tmp |= ADC_IQCOR_COEFF_2(fract);
160 }
161
162 ret = adc_write(phy, ADC_REG_CHAN_CNTRL_2(chan), tmp);
163 if (ret < 0) {
164 return ret;
165 }
166
167 return 0;
168 }
169
170 /***************************************************************************//**
171 * @brief adc_get_calib_scale_phase
172 *******************************************************************************/
173 int32_t adc_get_calib_scale_phase(struct ad9361_rf_phy *phy,
174 uint32_t phase,
175 uint32_t chan,
176 int32_t *val,
177 int32_t *val2)
178 {
179 int ret;
180 uint32_t tmp;
181 int32_t sign;
182 uint64_t llval;
183
184 ret = adc_read(phy, ADC_REG_CHAN_CNTRL_2(chan), &tmp);
185 if (ret < 0) {
186 return ret;
187 }
188
189 /* format is 1.1.14 (sign, integer and fractional bits) */
190
191 if (!((phase + chan) % 2)) {
192 tmp = ADC_TO_IQCOR_COEFF_1(tmp);
193 } else {
194 tmp = ADC_TO_IQCOR_COEFF_2(tmp);
195 }
196
197 if (tmp & 0x8000)
198 sign = -1;
199 else
200 sign = 1;
201
202 if (tmp & 0x4000)
203 *val = 1 * sign;
204 else
205 *val = 0;
206
207 tmp &= ~0xC000;
208
209 llval = tmp * 1000000ULL + (0x4000 / 2);
210 do_div(&llval, 0x4000);
211 if (*val == 0)
212 *val2 = (int32_t)llval * sign;
213 else
214 *val2 = (int32_t)llval;
215
216 return 0;
217 }
218
219 /***************************************************************************//**
220 * @brief adc_set_calib_scale
221 *******************************************************************************/
222 int32_t adc_set_calib_scale(struct ad9361_rf_phy *phy,
223 uint32_t chan,
224 int32_t val,
225 int32_t val2)
226 {
227 return adc_set_calib_scale_phase(phy, 0, chan, val, val2);
228 }
229
230 /***************************************************************************//**
231 * @brief adc_get_calib_scale
232 *******************************************************************************/
233 int32_t adc_get_calib_scale(struct ad9361_rf_phy *phy,
234 uint32_t chan,
235 int32_t *val,
236 int32_t *val2)
237 {
238 return adc_get_calib_scale_phase(phy, 0, chan, val, val2);
239 }
240
241 /***************************************************************************//**
242 * @brief adc_set_calib_phase
243 *******************************************************************************/
244 int32_t adc_set_calib_phase(struct ad9361_rf_phy *phy,
245 uint32_t chan,
246 int32_t val,
247 int32_t val2)
248 {
249 return adc_set_calib_scale_phase(phy, 1, chan, val, val2);
250 }
251
252 /***************************************************************************//**
253 * @brief adc_get_calib_phase
254 *******************************************************************************/
255 int32_t adc_get_calib_phase(struct ad9361_rf_phy *phy,
256 uint32_t chan,
257 int32_t *val,
258 int32_t *val2)
259 {
260 return adc_get_calib_scale_phase(phy, 1, chan, val, val2);
261 }