Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/fmcomms1/AD9122/cf_axi_dds.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file cf_axi_dds.c
3 * @brief Implementation file of AD9122 Driver helper functions.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2012(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 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /*****************************************************************************/
44 /***************************** Include Files *********************************/
45 /*****************************************************************************/
46 #include "spi_interface.h"
47 #include "dac_core.h"
48 #include "cf_axi_dds.h"
49
50 /******************************************************************************/
51 /***************************** Global State Variables *************************/
52 /******************************************************************************/
53 extern struct cf_axi_converter dds_conv;
54 #ifdef CF_AXI_DDS
55 struct cf_axi_dds_state dds_state;
56 #endif
57
58 /******************************************************************************/
59 /***************************** Delay Functions ********************************/
60 /******************************************************************************/
61 extern void delay_us(uint32_t us_count);
62 #define mdelay(x) delay_us(x*1000)
63 #define msleep(x) delay_us(x*1000)
64
65 #ifdef CF_AXI_DDS
66 /******************************************************************************/
67 /***************************** Macros *****************************************/
68 /******************************************************************************/
69 #define test_bit(bit, val) \
70 (((*val) & (1 << bit)) != 0)
71 #define set_bit(bit, val) \
72 (*val |= (1 << bit))
73 #define sign_extend32(value, index) \
74 ((int32_t)(value << (31-index)) >> (31-index))
75 #define ARRAY_SIZE(x) \
76 sizeof(x) / sizeof(x[0])
77
78 /*****************************************************************************/
79 /************************ Private Functions Prototypes ***********************/
80 /*****************************************************************************/
81 void cf_axi_dds_sync_frame();
82
83 /***************************************************************************//**
84 * @brief Computes the mod and integer division of two numbers.
85 *
86 * @return Returns the mod value, stores in the a parameter the integer division
87 *******************************************************************************/
88 uint64_t do_div(void* a, uint32_t b)
89 {
90 uint64_t mod;
91 mod = *(uint64_t *)a % b;
92 *(uint64_t *)a = *(uint64_t *)a / b;
93
94 return mod;
95 }
96
97 /***************************************************************************//**
98 * @brief Writes data to the AXI DAC core.
99 *
100 * @return None.
101 *******************************************************************************/
102 void dds_write(struct cf_axi_dds_state* st, uint32_t reg, uint32_t val)
103 {
104 DAC_Core_Write(reg, val);
105 }
106
107 /***************************************************************************//**
108 * @brief Reads data from the AXI DAC core.
109 *
110 * @return Returns the read value.
111 *******************************************************************************/
112 uint32_t dds_read(struct cf_axi_dds_state* st, uint32_t reg)
113 {
114 uint32_t data;
115 DAC_Core_Read(reg, &data);
116
117 return data;
118 }
119
120 /***************************************************************************//**
121 * @brief Stops the AXI DAC core.
122 *
123 * @return None.
124 *******************************************************************************/
125 void cf_axi_dds_stop(struct cf_axi_dds_state *st)
126 {
127 dds_write(st, ADI_REG_CNTRL_1, 0);
128 }
129
130 /***************************************************************************//**
131 * @brief Computes the frequency divider and phase offset.
132 *
133 * @return Returns the frequency divider and phase offset.
134 *******************************************************************************/
135 static int cf_axi_dds_default_setup(struct cf_axi_dds_state *st, uint32_t chan,
136 uint32_t phase, uint32_t freq, uint32_t scale)
137 {
138 uint64_t val64;
139 uint32_t val;
140
141 val64 = (uint64_t) freq * 0xFFFFULL;
142 do_div(&val64, st->dac_clk);
143 val = ADI_DDS_INCR(val64) | 1;
144
145 val64 = (uint64_t) phase * 0x10000ULL + (360000 / 2);
146 do_div(&val64, 360000);
147 val |= ADI_DDS_INIT(val64);
148
149 dds_write(st, ADI_REG_CHAN_CNTRL_1_IIOCHAN(chan), ADI_DDS_SCALE(scale));
150 dds_write(st, ADI_REG_CHAN_CNTRL_2_IIOCHAN(chan), val);
151
152 return 0;
153 }
154
155 /***************************************************************************//**
156 * @brief Reads parameters from the AD9122.
157 *
158 * @return Returns negative error code or 0 in case of success.
159 *******************************************************************************/
160 int32_t cf_axi_dds_read_raw(uint32_t channel,
161 uint32_t address,
162 int32_t *val,
163 int32_t *val2,
164 int32_t m)
165 {
166 struct cf_axi_dds_state *st = &dds_state;
167 struct cf_axi_converter *conv = &dds_conv;
168 uint64_t val64;
169 uint32_t reg;
170
171 switch (m) {
172 case 0:
173 *val = !!(dds_read(st, ADI_REG_CNTRL_1) & ADI_ENABLE);
174 return 0;
175 case IIO_CHAN_INFO_SCALE:
176 reg = ADI_TO_DDS_SCALE(dds_read(st, ADI_REG_CHAN_CNTRL_1_IIOCHAN(channel)));
177 if (!reg) {
178 *val = 1;
179 *val2 = 0;
180 } else {
181 *val = 0;
182 *val2 = 1000000 >> reg;
183 }
184 return 0;
185 case IIO_CHAN_INFO_FREQUENCY:
186 reg = dds_read(st, ADI_REG_CHAN_CNTRL_2_IIOCHAN(channel));
187 val64 = (uint64_t)ADI_TO_DDS_INCR(reg) * (uint64_t)st->dac_clk;
188 do_div(&val64, 0xFFFF);
189 *val = (int32_t)val64;
190 return 0;
191 case IIO_CHAN_INFO_PHASE:
192 reg = dds_read(st, ADI_REG_CHAN_CNTRL_2_IIOCHAN(channel));
193 val64 = (uint64_t)ADI_TO_DDS_INIT(reg) * 360000ULL + (0x10000 / 2);
194 do_div(&val64, 0xFFFF);
195 *val = (int32_t)val64;
196 return 0;
197 case IIO_CHAN_INFO_SAMP_FREQ:
198 if (!conv->get_data_clk)
199 return -1;
200
201 *val = st->dac_clk = conv->get_data_clk(conv);
202 return 0;
203
204 }
205 return -1;
206 }
207
208 /***************************************************************************//**
209 * @brief Writes parameters to the AD9122.
210 *
211 * @return Returns negative error code or 0 in case of success.
212 *******************************************************************************/
213 int32_t cf_axi_dds_write_raw(uint32_t channel,
214 uint32_t address,
215 int32_t val,
216 int32_t val2,
217 int32_t mask)
218 {
219 struct cf_axi_dds_state *st = &dds_state;
220 struct cf_axi_converter *conv = &dds_conv;
221 uint64_t val64;
222 uint32_t reg, ctrl_reg;
223 int32_t i;
224
225 ctrl_reg = dds_read(st, ADI_REG_CNTRL_1);
226 switch (mask) {
227 case 0:
228 if (val)
229 ctrl_reg |= ADI_ENABLE;
230 else
231 ctrl_reg &= ~ADI_ENABLE;
232 dds_write(st, ADI_REG_CNTRL_1, ctrl_reg);
233 break;
234 case IIO_CHAN_INFO_SCALE:
235 if (val == 1) {
236 i = 0;
237 } else {
238 for (i = 1; i < 16; i++)
239 if (val2 == (1000000 >> i))
240 break;
241 }
242 cf_axi_dds_stop(st);
243 dds_write(st, ADI_REG_CHAN_CNTRL_1_IIOCHAN(channel), ADI_DDS_SCALE(i));
244 dds_write(st, ADI_REG_CNTRL_1, ctrl_reg);
245 break;
246 case IIO_CHAN_INFO_FREQUENCY:
247 if (val > (int32_t)(st->dac_clk / 2))
248 return -1;
249 cf_axi_dds_stop(st);
250 reg = dds_read(st, ADI_REG_CHAN_CNTRL_2_IIOCHAN(channel));
251 reg &= ~ADI_DDS_INCR(~0);
252 val64 = (uint64_t) val * 0xFFFFULL;
253 do_div(&val64, st->dac_clk);
254 reg |= ADI_DDS_INCR(val64) | 1;
255 dds_write(st, ADI_REG_CHAN_CNTRL_2_IIOCHAN(channel), reg);
256 dds_write(st, ADI_REG_CNTRL_1, ctrl_reg);
257 break;
258 case IIO_CHAN_INFO_PHASE:
259 if (val < 0 || val > 360000)
260 return -1;
261 cf_axi_dds_stop(st);
262 reg = dds_read(st, ADI_REG_CHAN_CNTRL_2_IIOCHAN(channel));
263 reg &= ~ADI_DDS_INIT(~0);
264 val64 = (uint64_t) val * 0xFFFFULL;
265 do_div(&val64, 360000);
266 reg |= ADI_DDS_INIT(val64);
267 dds_write(st, ADI_REG_CHAN_CNTRL_2_IIOCHAN(channel), reg);
268 dds_write(st, ADI_REG_CNTRL_1, ctrl_reg);
269 break;
270 case IIO_CHAN_INFO_SAMP_FREQ:
271 if (!conv->write_raw)
272 return -1;
273 cf_axi_dds_stop(st);
274 conv->write_raw(channel, val, val2, mask);
275 st->dac_clk = conv->get_data_clk(conv);
276 dds_write(st, ADI_REG_CNTRL_1, ctrl_reg);
277 cf_axi_dds_sync_frame();
278 break;
279 default:
280 return -1;
281 }
282
283 return 0;
284 }
285 #endif //CF_AXI_DDS
286
287 /***************************************************************************//**
288 * @brief Waits for the AXI DAC core to sync.
289 *
290 * @return None.
291 *******************************************************************************/
292 void cf_axi_dds_sync_frame()
293 {
294 struct cf_axi_converter *conv = &dds_conv;
295 uint32_t stat;
296 static int32_t retry = 0;
297
298 mdelay(10); /* Wait until clocks are stable */
299
300 DAC_Core_Write(ADI_REG_FRAME, 0);
301 DAC_Core_Write(ADI_REG_FRAME, ADI_FRAME);
302
303 /* Check FIFO status */
304 stat = conv->get_fifo_status(conv);
305 if (stat) {
306 if (retry++ > 10) {
307 #ifdef SYNC_FRAME_DEBUG
308 xil_printf("FRAME/FIFO Reset Retry cnt\n");
309 #endif
310 return;
311 }
312
313 cf_axi_dds_sync_frame();
314 }
315
316 retry = 0;
317 }
318
319 /***************************************************************************//**
320 * @brief Configures the DDS with the DAC SED pattern.
321 *
322 * @return None.
323 *******************************************************************************/
324 static void cf_axi_dds_set_sed_pattern(unsigned chan,
325 unsigned pat1,
326 unsigned pat2)
327 {
328 uint32_t ctrl;
329 uint32_t hdl_version;
330
331 DAC_Core_Read(ADI_REG_VERSION, &hdl_version);
332
333 DAC_Core_Write(ADI_REG_CHAN_CNTRL_5(chan),
334 ADI_TO_DDS_PATT_1(pat1) | ADI_DDS_PATT_2(pat2));
335
336 DAC_Core_Write(ADI_REG_CNTRL_1, 0);
337
338 DAC_Core_Read(ADI_REG_CNTRL_2, &ctrl);
339 if (PCORE_VERSION_MAJOR(hdl_version) > 7) {
340 DAC_Core_Write(ADI_REG_CHAN_CNTRL_7(0), ADI_DATA_SEL(DATA_SEL_SED));
341 DAC_Core_Write(ADI_REG_CHAN_CNTRL_7(1), ADI_DATA_SEL(DATA_SEL_SED));
342 } else {
343 ctrl &= ~ADI_DATA_SEL(~0);
344 ctrl |= ADI_DATA_SEL(DATA_SEL_SED);
345 }
346 DAC_Core_Write(ADI_REG_CNTRL_2, ctrl | ADI_DATA_FORMAT);
347
348 DAC_Core_Write(ADI_REG_CNTRL_1, ADI_ENABLE);
349 }
350
351 /***************************************************************************//**
352 * @brief Initializes the DAC AXI core.
353 *
354 * @return Returns negative error code or 0 in case of success.
355 *******************************************************************************/
356 int32_t cf_axi_dds_of_probe()
357 {
358 #ifdef CF_AXI_DDS
359 struct cf_axi_dds_state *st = &dds_state;
360 #endif
361 struct cf_axi_converter *conv = &dds_conv;
362 uint32_t hdl_version;
363 uint32_t drp_status;
364 int32_t timeout = 100;
365
366 conv->pcore_sync = cf_axi_dds_sync_frame;
367 conv->pcore_set_sed_pattern = cf_axi_dds_set_sed_pattern;
368
369 DAC_Core_Read(ADI_REG_VERSION, &hdl_version);
370
371 DAC_Core_Write(ADI_REG_RSTN, 0x0);
372 if (PCORE_VERSION_MAJOR(hdl_version) > 7) {
373 DAC_Core_Write(ADI_REG_RSTN, ADI_MMCM_RSTN);
374 do {
375 DAC_Core_Read(ADI_REG_DRP_STATUS, &drp_status);
376 if (drp_status & ADI_DRP_LOCKED)
377 break;
378 mdelay(1);
379 } while(timeout--);
380 if(timeout == -1) {
381 xil_printf("Error: DRP UNLOCKED\n");
382 return -1;
383 }
384 }
385 DAC_Core_Write(ADI_REG_RSTN, ADI_RSTN | ADI_MMCM_RSTN);
386 DAC_Core_Write(ADI_REG_RATECNTRL, ADI_RATE(1));
387 DAC_Core_Write(ADI_REG_CNTRL_1, 0);
388 #ifdef CF_AXI_DDS
389 DAC_Core_Write(ADI_REG_CNTRL_2, ADI_DATA_SEL(DATA_SEL_DDS) | ADI_DATA_FORMAT);
390 cf_axi_dds_default_setup(st, 0, 90000, 40000000, 2);
391 cf_axi_dds_default_setup(st, 1, 90000, 40000000, 2);
392
393 cf_axi_dds_default_setup(st, 2, 0, 40000000, 2);
394 cf_axi_dds_default_setup(st, 3, 0, 40000000, 2);
395 #endif
396 DAC_Core_Write(ADI_REG_CNTRL_1, ADI_ENABLE);
397
398 cf_axi_dds_sync_frame();
399
400 return 0;
401 }