Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/fmcomms1/AD9523/AD9523.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD9523.c
3 * @brief Implementation of AD9523 Driver.
4 * @author ACozma([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 <math.h>
47 #include "spi_interface.h"
48 #include "AD9523.h"
49 #include "AD9523_cfg.h"
50
51 /******************************************************************************/
52 /************************ Local variables and types ***************************/
53 /******************************************************************************/
54 struct ad9523_state
55 {
56 struct ad9523_platform_data *pdata;
57 uint32_t vcxo_freq;
58 uint32_t vco_freq;
59 uint32_t vco_out_freq[3];
60 uint8_t vco_out_map[14];
61 }ad9523_st;
62
63 enum ad9523_raw_masks
64 {
65 CHAN_INFO_RAW,
66 CHAN_INFO_FREQUENCY,
67 CHAN_INFO_PHASE
68 };
69
70 enum
71 {
72 AD9523_STAT_PLL1_LD,
73 AD9523_STAT_PLL2_LD,
74 AD9523_STAT_REFA,
75 AD9523_STAT_REFB,
76 AD9523_STAT_REF_TEST,
77 AD9523_STAT_VCXO,
78 AD9523_STAT_PLL2_FB_CLK,
79 AD9523_STAT_PLL2_REF_CLK,
80 AD9523_SYNC,
81 AD9523_EEPROM,
82 };
83
84 enum
85 {
86 AD9523_VCO1,
87 AD9523_VCO2,
88 AD9523_VCXO,
89 AD9523_NUM_CLK_SRC,
90 };
91
92 /* Platform dependent sleep function */
93 extern void delay_us(uint32_t us_count);
94
95 /* Helpers to avoid excess line breaks */
96 #define AD_IFE(_pde, _a, _b) ((pdata->_pde) ? _a : _b)
97 #define AD_IF(_pde, _a) AD_IFE(_pde, _a, 0)
98 #define abs(x) ((x)< 0 ? -(x) : (x))
99
100 /*
101 * Divide positive or negative dividend by positive divisor and round
102 * to closest integer. Result is undefined for negative divisors.
103 */
104 #define DIV_ROUND_CLOSEST(x, divisor) \
105 (((x) > 0) ? \
106 (((x) + ((divisor) / 2)) / (divisor)) : \
107 (((x) - ((divisor) / 2)) / (divisor)));
108
109
110
111 /***************************************************************************//**
112 * @brief Reads the value of the selected register.
113 *
114 * @param registerAddress - The address of the register to read.
115 *
116 * @return registerValue - The register's value or negative error code.
117 *******************************************************************************/
118 int32_t ad9523_read(uint32_t registerAddress)
119 {
120 uint16_t regAddr = 0;
121 uint32_t registerValue = 0;
122 uint32_t registerValue1 = 0;
123 int32_t ret;
124 uint32_t i = 0;
125
126 regAddr = 0x8000 + (registerAddress & 0x1FFF);
127 for(i = 0; i < AD9523_TRANSF_LEN(registerAddress); i++)
128 {
129 ret = SPI_Read(SPI_SEL_AD9523, regAddr, &registerValue1);
130 if(ret < 0)
131 return ret;
132 regAddr--;
133 registerValue <<= 8;
134 registerValue |= registerValue1;
135 }
136 return (int32_t)registerValue;
137 }
138
139 /***************************************************************************//**
140 * @brief Writes a value to the selected register.
141 *
142 * @param registerAddress - The address of the register to write to.
143 * @param registerValue - The value to write to the register.
144 *
145 * @return Returns 0 in case of success or negative error code.
146 *******************************************************************************/
147 int32_t ad9523_write(uint32_t registerAddress,
148 uint32_t registerValue)
149 {
150 uint32_t i = 0;
151 int32_t ret = -1;
152 uint16_t regAddr = 0;
153
154 regAddr = (registerAddress & 0x1FFF);
155 for(i = 0; i < AD9523_TRANSF_LEN(registerAddress); i++)
156 {
157 ret = SPI_Write(SPI_SEL_AD9523, regAddr, (registerValue >> ((AD9523_TRANSF_LEN(registerAddress) - i - 1) * 8)) & 0xFF);
158 if(ret < 0)
159 return ret;
160 regAddr--;
161 }
162
163 return ret;
164 }
165
166 /***************************************************************************//**
167 * @brief Updates the AD9523 configuration
168 *
169 * @return Returns 0 in case of success or negative error code.
170 *******************************************************************************/
171 int32_t ad9523_io_update()
172 {
173 return ad9523_write(AD9523_IO_UPDATE, AD9523_IO_UPDATE_EN);
174 }
175
176 /***************************************************************************//**
177 * @brief Sets the clock provider for selected channel.
178 *
179 * @param ch - Selected channel.
180 * @param out - Selected clock provider.
181 *
182 * @return Returns 0 in case of success or negative error code.
183 *******************************************************************************/
184 int32_t ad9523_vco_out_map(uint32_t ch, uint32_t out)
185 {
186 struct ad9523_state *st = &ad9523_st;
187 int32_t ret;
188 uint32_t mask;
189
190 switch (ch) {
191 case 0:
192 case 1:
193 case 2:
194 case 3:
195 ret = ad9523_read(AD9523_PLL1_OUTPUT_CHANNEL_CTRL);
196 if (ret < 0)
197 break;
198 mask = AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH0 << ch;
199 if (out) {
200 ret |= mask;
201 out = AD9523_VCXO;
202 } else {
203 ret &= ~mask;
204 }
205 ret = ad9523_write(AD9523_PLL1_OUTPUT_CHANNEL_CTRL, ret);
206 break;
207 case 4:
208 case 5:
209 case 6:
210 ret = ad9523_read(AD9523_PLL1_OUTPUT_CTRL);
211 if (ret < 0)
212 break;
213 mask = AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH4_M2 << (ch - 4);
214 if (out)
215 ret |= mask;
216 else
217 ret &= ~mask;
218 ret = ad9523_write(AD9523_PLL1_OUTPUT_CTRL, ret);
219 break;
220 case 7:
221 case 8:
222 case 9:
223 ret = ad9523_read(AD9523_PLL1_OUTPUT_CHANNEL_CTRL);
224 if (ret < 0)
225 break;
226 mask = AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH7_M2 << (ch - 7);
227 if (out)
228 ret |= mask;
229 else
230 ret &= ~mask;
231 ret = ad9523_write(AD9523_PLL1_OUTPUT_CHANNEL_CTRL, ret);
232 break;
233 default:
234 return 0;
235 }
236
237 st->vco_out_map[ch] = out;
238
239 return ret;
240 }
241
242 /***************************************************************************//**
243 * @brief Sets the clock provider for selected channel.
244 *
245 * @param ch - Selected channel.
246 * @param freq - The output frequency of the selected channel. This parameter is
247 * required for selecting the VCXO or the VCO clock for channels
248 * 0 - 3, or the the best VCO Divider (M1 or M2) for channels
249 * 4 - 9.
250 *
251 * @return Returns 0 in case of success or negative error code.
252 *******************************************************************************/
253 int32_t ad9523_set_clock_provider(int32_t ch, uint32_t freq)
254 {
255 struct ad9523_state *st = &ad9523_st;
256 int32_t tmp1, tmp2;
257 uint8_t use_alt_clk_src;
258
259 switch (ch) {
260 case 0:
261 case 1:
262 case 2:
263 case 3:
264 use_alt_clk_src = (freq == st->vco_out_freq[AD9523_VCXO]);
265 break;
266 case 4:
267 case 5:
268 case 6:
269 case 7:
270 case 8:
271 case 9:
272 tmp1 = st->vco_out_freq[AD9523_VCO1] / freq;
273 tmp2 = st->vco_out_freq[AD9523_VCO2] / freq;
274 tmp1 *= freq;
275 tmp2 *= freq;
276 use_alt_clk_src = ((freq - tmp1) > (freq - tmp2));
277 break;
278 default:
279 /* Ch 10..14: No action required, return success */
280 return 0;
281 }
282
283 return ad9523_vco_out_map(ch, use_alt_clk_src);
284 }
285
286 /***************************************************************************//**
287 * @brief Reads the frequency, the phase or the power status of selected
288 * channel.
289 *
290 * @param val - pointer to store the first 32 bits of data
291 * @param val2 - pointer to store the last 32 bits of data
292 * @param mask - The parameter selected to be read.
293 * Example: CHAN_INFO_RAW
294 * CHAN_INFO_FREQUENCY
295 * CHAN_INFO_PHASE
296 *
297 * @return Returns 0 in case of success or negative error code.
298 *******************************************************************************/
299 int32_t ad9523_read_raw(int32_t channel,
300 int32_t *val,
301 int32_t *val2,
302 int32_t mask)
303 {
304 struct ad9523_state *st = &ad9523_st;
305 uint32_t code;
306 int32_t ret;
307
308 ret = ad9523_read(AD9523_CHANNEL_CLOCK_DIST(channel));
309
310 if (ret < 0)
311 return ret;
312
313 switch (mask) {
314 case CHAN_INFO_RAW:
315 *val = !(ret & AD9523_CLK_DIST_PWR_DOWN_EN);
316 return 0;
317 case CHAN_INFO_FREQUENCY:
318 *val = st->vco_out_freq[st->vco_out_map[channel]] /
319 AD9523_CLK_DIST_DIV_REV(ret);
320 return 0;
321 case CHAN_INFO_PHASE:
322 code = (AD9523_CLK_DIST_DIV_PHASE_REV(ret) * 3141592) /
323 AD9523_CLK_DIST_DIV_REV(ret);
324 *val = code / 1000000;
325 *val2 = (code % 1000000) * 10;
326 return 0;
327 default:
328 return -1;
329 }
330 };
331
332 /***************************************************************************//**
333 * @brief Writes the frequency, the phase or the power status for selected
334 * channel.
335 *
336 * @param val - the first 32 bits of data to write
337 * @param val2 - the last 32 bits of data to write
338 * @param mask - The parameter selected to be written.
339 * Example: CHAN_INFO_RAW
340 * CHAN_INFO_FREQUENCY
341 * CHAN_INFO_PHASE
342 *
343 * @return Returns 0 in case of success or negative error code.
344 * When setting the frequency the returned value is the set frequency
345 * or negative error code.
346 *******************************************************************************/
347 int32_t ad9523_write_raw(int32_t channel,
348 int32_t val,
349 int32_t val2,
350 int32_t mask)
351 {
352 struct ad9523_state *st = &ad9523_st;
353 uint32_t reg;
354 int32_t ret, tmp, code;
355 int32_t ret_val = 0;
356
357 ret = ad9523_read(AD9523_CHANNEL_CLOCK_DIST(channel));
358 if (ret < 0)
359 goto out;
360
361 reg = ret;
362
363 switch (mask) {
364 case CHAN_INFO_RAW:
365 if (val)
366 reg &= ~AD9523_CLK_DIST_PWR_DOWN_EN;
367 else
368 reg |= AD9523_CLK_DIST_PWR_DOWN_EN;
369 break;
370 case CHAN_INFO_FREQUENCY:
371 if (val <= 0) {
372 ret = -1;
373 goto out;
374 }
375 ret = ad9523_set_clock_provider(channel, val);
376 if (ret < 0)
377 goto out;
378 tmp = DIV_ROUND_CLOSEST(st->vco_out_freq[st->vco_out_map[channel]], val);
379 tmp = tmp < 1 ? 1 : tmp > 1024 ? 1024 : tmp;
380 ret_val = st->vco_out_freq[st->vco_out_map[channel]] / tmp;
381 reg &= ~(0x3FF << 8);
382 reg |= AD9523_CLK_DIST_DIV(tmp);
383 break;
384 case CHAN_INFO_PHASE:
385 code = val * 1000000 + val2 % 1000000;
386 tmp = (code * AD9523_CLK_DIST_DIV_REV(ret)) / 3141592;
387 tmp = tmp < 0 ? 0 : tmp > 63 ? 63 : tmp;
388 reg &= ~AD9523_CLK_DIST_DIV_PHASE(~0);
389 reg |= AD9523_CLK_DIST_DIV_PHASE(tmp);
390 break;
391 default:
392 ret = -1;
393 goto out;
394 }
395
396 ret = ad9523_write(AD9523_CHANNEL_CLOCK_DIST(channel), reg);
397 if (ret < 0)
398 goto out;
399
400 ad9523_io_update();
401 out:
402
403 return ret < 0 ? ret : ret_val;
404 }
405
406 /***************************************************************************//**
407 * @brief Stores data into the EEPROM
408 *
409 * @return Returns 0 in case of success or negative error code.
410 *******************************************************************************/
411 int32_t ad9523_store_eeprom()
412 {
413 int32_t ret, tmp;
414
415 ret = ad9523_write(AD9523_EEPROM_CTRL1,
416 AD9523_EEPROM_CTRL1_EEPROM_WRITE_PROT_DIS);
417 if (ret < 0)
418 return ret;
419 ret = ad9523_write(AD9523_EEPROM_CTRL2,
420 AD9523_EEPROM_CTRL2_REG2EEPROM);
421 if (ret < 0)
422 return ret;
423
424 tmp = 4;
425 do {
426 delay_us(16000);
427 ret = ad9523_read(AD9523_EEPROM_DATA_XFER_STATUS);
428 if (ret < 0)
429 return ret;
430 } while ((ret & AD9523_EEPROM_DATA_XFER_IN_PROGRESS) && tmp--);
431
432 ret = ad9523_write(AD9523_EEPROM_CTRL1, 0);
433 if (ret < 0)
434 return ret;
435
436 ret = ad9523_read(AD9523_EEPROM_ERROR_READBACK);
437 if (ret < 0)
438 return ret;
439
440 if (ret & AD9523_EEPROM_ERROR_READBACK_FAIL) {
441 //Verify EEPROM failed
442 ret = -1;
443 }
444
445 return ret;
446 }
447
448 /***************************************************************************//**
449 * @brief Updates the AD9523 configuration.
450 *
451 * @return Returns 0 in case of success or negative error code.
452 *******************************************************************************/
453 int32_t ad9523_sync()
454 {
455 int32_t ret, tmp;
456
457 ret = ad9523_read(AD9523_STATUS_SIGNALS);
458 if (ret < 0)
459 return ret;
460
461 tmp = ret;
462 tmp |= AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL;
463
464 ret = ad9523_write(AD9523_STATUS_SIGNALS, tmp);
465 if (ret < 0)
466 return ret;
467
468 ad9523_io_update();
469 tmp &= ~AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL;
470
471 ret = ad9523_write(AD9523_STATUS_SIGNALS, tmp);
472 if (ret < 0)
473 return ret;
474
475 return ad9523_io_update();
476 }
477
478 /***************************************************************************//**
479 * @brief Sets the output frequency for selected channel.
480 *
481 * @param channel - Selected channel number.
482 * @param Hz - The output frequency for the selected channel. If the value is
483 * INT64_MAX then the function just returns the already set value.
484 *
485 * @return Returns negative error code, or the actual frequency in Hz.
486 *******************************************************************************/
487 int64_t ad9523_out_frequency(int32_t channel, int64_t Hz)
488 {
489 int32_t ret;
490 int32_t ret_freq;
491
492 if(Hz != INT64_MAX)
493 {
494 ret = ad9523_write_raw(channel, (int32_t)Hz, 0, CHAN_INFO_FREQUENCY);
495 if(ret < 0)
496 return ret;
497
498 ret_freq = (int32_t)ret;
499 }
500 else
501 {
502 ret = ad9523_read_raw(channel, &ret_freq, 0, CHAN_INFO_FREQUENCY);
503 if(ret < 0)
504 return ret;
505 }
506
507 return (int64_t)ret_freq;
508 }
509
510 /***************************************************************************//**
511 * @brief Sets the phase for selected channel.
512 *
513 * @param channel - Selected channel number.
514 * @param rad - The phase for the selected channel. If the value is INT64_MAX
515 * then the function just returns the already set value.
516 *
517 * @return Returns negative error code, or the actual phase in rad.
518 *******************************************************************************/
519 int64_t ad9523_out_phase(int32_t channel, int64_t rad)
520 {
521 int32_t ret;
522 int32_t ret_phase;
523
524 if(rad != INT64_MAX)
525 {
526 ret = ad9523_write_raw(channel, (int32_t)rad, 0, CHAN_INFO_PHASE);
527 if(ret < 0)
528 return ret;
529
530 ret_phase = (int32_t)rad;
531 }
532 else
533 {
534 ret = ad9523_read_raw(channel, &ret_phase, 0, CHAN_INFO_PHASE);
535 if(ret < 0)
536 return ret;
537 }
538
539 return (int64_t)ret_phase;
540 }
541
542 /***************************************************************************//**
543 * @brief Sets the phase for selected channel.
544 *
545 * @param channel - Selected channel number.
546 * @param data - '0' powers down channel 0, while any value > 0 enables the
547 * channel. If the value is INT64_MAX then the function just
548 * returns the already set value.
549 *
550 * @return Returns negative error code, or the actual phase in rad.
551 *******************************************************************************/
552 int64_t ad9523_out_raw(int32_t channel, int64_t raw_data)
553 {
554 int32_t ret;
555 int32_t ret_raw;
556
557 if(raw_data != INT64_MAX)
558 {
559 ret = ad9523_write_raw(channel, (int32_t)raw_data, 0, CHAN_INFO_RAW);
560 if(ret < 0)
561 return ret;
562
563 ret_raw = (int32_t)raw_data;
564 }
565 else
566 {
567 ret = ad9523_read_raw(channel, &ret_raw, 0, CHAN_INFO_RAW);
568 if(ret < 0)
569 return ret;
570 }
571
572 return (int64_t)ret_raw;
573 }
574
575 /***************************************************************************//**
576 * @brief Helper function to get the status of the AD9523
577 *
578 * @param status_bit - The status bit to check
579 *
580 * @return Returns the state of the checked status bit.
581 *******************************************************************************/
582 int32_t ad9523_status(int32_t status_bit)
583 {
584 int32_t ret;
585
586 ret = ad9523_read(AD9523_READBACK_0);
587 if (ret < 0)
588 return ret;
589
590 return ((ret & status_bit) != 0);
591 }
592
593 /***************************************************************************//**
594 * @brief Determines the achievable output frequency
595 * given a desired output frequency.
596 *
597 * @param ch - Selected channel number.
598 * @param rate - Desired output frequency.
599 *
600 * @return Returns the achievable output frequency.
601 *******************************************************************************/
602 uint32_t ad9523_clk_round_rate(int32_t ch, uint32_t rate)
603 {
604 struct ad9523_state *st = &ad9523_st;
605 unsigned long clk, tmp1, tmp2;
606
607 if (!rate)
608 return 0;
609
610 switch (ch)
611 {
612 case 0:
613 case 1:
614 case 2:
615 case 3:
616 if (rate == st->vco_out_freq[AD9523_VCXO])
617 clk = st->vco_out_freq[AD9523_VCXO];
618 else
619 clk = st->vco_out_freq[AD9523_VCO1];
620 break;
621 case 4:
622 case 5:
623 case 6:
624 case 7:
625 case 8:
626 case 9:
627 tmp1 = st->vco_out_freq[AD9523_VCO1] / rate;
628 tmp2 = st->vco_out_freq[AD9523_VCO2] / rate;
629 tmp1 *= rate;
630 tmp2 *= rate;
631 if (abs((int32_t)tmp1 - (int32_t)rate) > abs((int32_t)tmp2 - (int32_t)rate))
632 clk = st->vco_out_freq[AD9523_VCO2];
633 else
634 clk = st->vco_out_freq[AD9523_VCO1];
635 break;
636 default:
637 clk = st->vco_out_freq[AD9523_VCO1];
638 /* Ch 10..14: No action required, return success */
639 }
640
641 tmp1 = DIV_ROUND_CLOSEST(clk, rate);
642 tmp1 = tmp1 < 1UL ? 1UL : tmp1 > 1024UL ? 1024UL : tmp1;
643
644 return clk / tmp1;
645 }
646
647 /***************************************************************************//**
648 * @brief Determines the achievable output frequency for the DAC CLK channel
649 *
650 * @param rate - Desired output frequency.
651 *
652 * @return Returns the achievable output frequency.
653 *******************************************************************************/
654 uint32_t ad9523_clk_round_rate_DAC_CLK(uint32_t rate)
655 {
656 return ad9523_clk_round_rate(12, rate);
657 }
658
659 /***************************************************************************//**
660 * @brief Determines the achievable output frequency for the DAC DCO CLK channel
661 *
662 * @param rate - Desired output frequency.
663 *
664 * @return Returns the achievable output frequency.
665 *******************************************************************************/
666 uint32_t ad9523_clk_round_rate_DAC_DCO_CLK(uint32_t rate)
667 {
668 return ad9523_clk_round_rate(6, rate);
669 }
670
671 /***************************************************************************//**
672 * @brief Determines the achievable output frequency for the DAC REF CLK channel
673 *
674 * @param rate - Desired output frequency.
675 *
676 * @return Returns the achievable output frequency.
677 *******************************************************************************/
678 uint32_t ad9523_clk_round_rate_DAC_REF_CLK(uint32_t rate)
679 {
680 return ad9523_clk_round_rate(10, rate);
681 }
682
683 /***************************************************************************//**
684 * @brief Sets the output frequency for channel 0.
685 *
686 * @param Hz - The output frequency for the selected channel.
687 *
688 * @return Returns negative error code, or the actual frequency in Hz.
689 *******************************************************************************/
690 int64_t AD9523_out_altvoltage_ZD_OUTPUT_frequency(int64_t Hz)
691 {
692 return ad9523_out_frequency(0, Hz);
693 }
694
695 /***************************************************************************//**
696 * @brief Sets the phase for channel 0.
697 *
698 * @param rad - The phase for the selected channel.
699 *
700 * @return Returns negative error code, or the actual phase in rad.
701 *******************************************************************************/
702 int64_t ad9523_out_altvoltage_ZD_OUTPUT_phase(int64_t rad)
703 {
704 return ad9523_out_phase(0, rad);
705 }
706
707 /***************************************************************************//**
708 * @brief Powers down / enables channel 0.
709 *
710 * @param data - '0' powers down channel 0, while any value > 0 enables the
711 * channel.
712 *
713 * @return Returns the channel power status.
714 *******************************************************************************/
715 int64_t ad9523_out_altvoltage_ZD_OUTPUT_raw(int64_t raw_data)
716 {
717 return ad9523_out_raw(0, raw_data);
718 }
719
720 /***************************************************************************//**
721 * @brief Sets the output frequency for channel 12.
722 *
723 * @param Hz - The output frequency for the selected channel.
724 *
725 * @return Returns negative error code, or the actual frequency in Hz.
726 *******************************************************************************/
727 int64_t ad9523_out_altvoltage_DAC_CLK_frequency(int64_t Hz)
728 {
729 return ad9523_out_frequency(12, Hz);
730 }
731
732 /***************************************************************************//**
733 * @brief Sets the phase for channel 12.
734 *
735 * @param rad - The phase for the selected channel.
736 *
737 * @return Returns negative error code, or the actual phase in rad.
738 *******************************************************************************/
739 int64_t ad9523_out_altvoltage_DAC_CLK_phase(int64_t rad)
740 {
741 return ad9523_out_phase(12, rad);
742 }
743
744 /***************************************************************************//**
745 * @brief Powers down / enables channel 12.
746 *
747 * @param data - '0' powers down channel 0, while any value > 0 enables the
748 * channel.
749 *
750 * @return Returns the channel power status.
751 *******************************************************************************/
752 int64_t ad9523_out_altvoltage_DAC_CLK_raw(int64_t raw_data)
753 {
754 return ad9523_out_raw(12, raw_data);
755 }
756
757 /***************************************************************************//**
758 * @brief Sets the output frequency for channel 2.
759 *
760 * @param Hz - The output frequency for the selected channel.
761 *
762 * @return Returns negative error code, or the actual frequency in Hz.
763 *******************************************************************************/
764 int64_t ad9523_out_altvoltage_ADC_CLK_frequency(int64_t Hz)
765 {
766 return ad9523_out_frequency(2, Hz);
767 }
768
769 /***************************************************************************//**
770 * @brief Sets the phase for channel 2.
771 *
772 * @param rad - The phase for the selected channel.
773 *
774 * @return Returns negative error code, or the actual phase in rad.
775 *******************************************************************************/
776 int64_t ad9523_out_altvoltage_ADC_CLK_phase(int64_t rad)
777 {
778 return ad9523_out_phase(2, rad);
779 }
780
781 /***************************************************************************//**
782 * @brief Powers down / enables channel 2.
783 *
784 * @param data - '0' powers down channel 0, while any value > 0 enables the
785 * channel.
786 *
787 * @return Returns the channel power status.
788 *******************************************************************************/
789 int64_t ad9523_out_altvoltage_ADC_CLK_raw(int64_t raw_data)
790 {
791 return ad9523_out_raw(2, raw_data);
792 }
793
794 /***************************************************************************//**
795 * @brief Sets the output frequency for channel 10.
796 *
797 * @param Hz - The output frequency for the selected channel.
798 *
799 * @return Returns negative error code, or the actual frequency in Hz.
800 *******************************************************************************/
801 int64_t ad9523_out_altvoltage_DAC_REF_CLK_frequency(int64_t Hz)
802 {
803 return ad9523_out_frequency(10, Hz);
804 }
805
806 /***************************************************************************//**
807 * @brief Sets the phase for channel 10.
808 *
809 * @param rad - The phase for the selected channel.
810 *
811 * @return Returns negative error code, or the actual phase in rad.
812 *******************************************************************************/
813 int64_t ad9523_out_altvoltage_DAC_REF_CLK_phase(int64_t rad)
814 {
815 return ad9523_out_phase(10, rad);
816 }
817
818 /***************************************************************************//**
819 * @brief Powers down / enables channel 10.
820 *
821 * @param data - '0' powers down channel 0, while any value > 0 enables the
822 * channel.
823 *
824 * @return Returns the channel power status.
825 *******************************************************************************/
826 int64_t ad9523_out_altvoltage_DAC_REF_CLK_raw(int64_t raw_data)
827 {
828 return ad9523_out_raw(10, raw_data);
829 }
830
831 /***************************************************************************//**
832 * @brief Sets the output frequency for channel 5.
833 *
834 * @param Hz - The output frequency for the selected channel.
835 *
836 * @return Returns negative error code, or the actual frequency in Hz.
837 *******************************************************************************/
838 int64_t ad9523_out_altvoltage_TX_LO_REF_CLK_frequency(int64_t Hz)
839 {
840 return ad9523_out_frequency(5, Hz);
841 }
842
843 /***************************************************************************//**
844 * @brief Sets the phase for channel 5.
845 *
846 * @param rad - The phase for the selected channel.
847 *
848 * @return Returns negative error code, or the actual phase in rad.
849 *******************************************************************************/
850 int64_t ad9523_out_altvoltage_TX_LO_REF_CLK_phase(int64_t rad)
851 {
852 return ad9523_out_phase(5, rad);
853 }
854
855 /***************************************************************************//**
856 * @brief Powers down / enables channel 5.
857 *
858 * @param data - '0' powers down channel 0, while any value > 0 enables the
859 * channel.
860 *
861 * @return Returns the channel power status.
862 *******************************************************************************/
863 int64_t ad9523_out_altvoltage_TX_LO_REF_CLK_raw(int64_t raw_data)
864 {
865 return ad9523_out_raw(5, raw_data);
866 }
867
868 /***************************************************************************//**
869 * @brief Sets the output frequency for channel 6.
870 *
871 * @param Hz - The output frequency for the selected channel.
872 *
873 * @return Returns negative error code, or the actual frequency in Hz.
874 *******************************************************************************/
875 int64_t ad9523_out_altvoltage_DAC_DCO_CLK_frequency(int64_t Hz)
876 {
877 return ad9523_out_frequency(6, Hz);
878 }
879
880 /***************************************************************************//**
881 * @brief Sets the phase for channel 6.
882 *
883 * @param rad - The phase for the selected channel.
884 *
885 * @return Returns negative error code, or the actual phase in rad.
886 *******************************************************************************/
887 int64_t ad9523_out_altvoltage_DAC_DCO_CLK_phase(int64_t rad)
888 {
889 return ad9523_out_phase(6, rad);
890 }
891
892 /***************************************************************************//**
893 * @brief Powers down / enables channel 6.
894 *
895 * @param data - '0' powers down channel 0, while any value > 0 enables the
896 * channel.
897 *
898 * @return Returns the channel power status.
899 *******************************************************************************/
900 int64_t ad9523_out_altvoltage_DAC_DCO_CLK_raw(int64_t raw_data)
901 {
902 return ad9523_out_raw(6, raw_data);
903 }
904
905 /***************************************************************************//**
906 * @brief Sets the output frequency for channel 7.
907 *
908 * @param Hz - The output frequency for the selected channel.
909 *
910 * @return Returns negative error code, or the actual frequency in Hz.
911 *******************************************************************************/
912 int64_t ad9523_out_altvoltage_ADC_SYNC_CLK_frequency(int64_t Hz)
913 {
914 return ad9523_out_frequency(7, Hz);
915 }
916
917 /***************************************************************************//**
918 * @brief Sets the phase for channel 8.
919 *
920 * @param rad - The phase for the selected channel.
921 *
922 * @return Returns negative error code, or the actual phase in rad.
923 *******************************************************************************/
924 int64_t ad9523_out_altvoltage_ADC_SYNC_CLK_phase(int64_t rad)
925 {
926 return ad9523_out_phase(7, rad);
927 }
928
929 /***************************************************************************//**
930 * @brief Powers down / enables channel 8.
931 *
932 * @param data - '0' powers down channel 0, while any value > 0 enables the
933 * channel.
934 *
935 * @return Returns the channel power status.
936 *******************************************************************************/
937 int64_t ad9523_out_altvoltage_ADC_SYNC_CLK_raw(int64_t raw_data)
938 {
939 return ad9523_out_raw(7, raw_data);
940 }
941
942 /***************************************************************************//**
943 * @brief Sets the output frequency for channel 9.
944 *
945 * @param Hz - The output frequency for the selected channel.
946 *
947 * @return Returns negative error code, or the actual frequency in Hz.
948 *******************************************************************************/
949 int64_t ad9523_out_altvoltage_RX_LO_REF_CLK_frequency(int64_t Hz)
950 {
951 return ad9523_out_frequency(9, Hz);
952 }
953
954 /***************************************************************************//**
955 * @brief Sets the phase for channel 9.
956 *
957 * @param rad - The phase for the selected channel.
958 *
959 * @return Returns negative error code, or the actual phase in rad.
960 *******************************************************************************/
961 int64_t ad9523_out_altvoltage_RX_LO_REF_CLK_phase(int64_t rad)
962 {
963 return ad9523_out_phase(9, rad);
964 }
965
966 /***************************************************************************//**
967 * @brief Powers down / enables channel 9.
968 *
969 * @param data - '0' powers down channel 0, while any value > 0 enables the
970 * channel.
971 *
972 * @return Returns the channel power status.
973 *******************************************************************************/
974 int64_t ad9523_out_altvoltage_RX_LO_REF_CLK_raw(int64_t raw_data)
975 {
976 return ad9523_out_raw(9, raw_data);
977 }
978
979 /***************************************************************************//**
980 * @brief Returns the state of the VCXO clock.
981 *******************************************************************************/
982 int32_t ad9523_vcxo_clk_present()
983 {
984 return ad9523_status(AD9523_READBACK_0_STAT_VCXO);
985 }
986
987 /***************************************************************************//**
988 * @brief Resets the device.
989 *
990 * @return Returns negative error code or 0 in case of success.
991 *******************************************************************************/
992 int32_t ad9523_reset(void)
993 {
994 int32_t ret;
995
996 ret = ad9523_write(AD9523_SERIAL_PORT_CONFIG,
997 AD9523_SER_CONF_SOFT_RESET |
998 (AD9523_SPI_3_WIRE ? 0 :
999 AD9523_SER_CONF_SDO_ACTIVE));
1000
1001 return ret;
1002 }
1003
1004 /***************************************************************************//**
1005 * @brief Initializes the AD9523.
1006 *
1007 * @return Returns 0 in case of success or negative error code.
1008 *******************************************************************************/
1009 int32_t ad9523_setup()
1010 {
1011 struct ad9523_state *st = &ad9523_st;
1012 struct ad9523_platform_data *pdata = &ad9523_pdata_lpc;
1013 struct ad9523_channel_spec *chan;
1014 uint32_t active_mask = 0;
1015 int32_t ret, i;
1016
1017 ret = ad9523_reset();
1018
1019 if (ret < 0)
1020 return ret;
1021
1022 ret = ad9523_write(AD9523_SERIAL_PORT_CONFIG, 0x00);
1023 if (ret < 0)
1024 return ret;
1025
1026 ret = ad9523_write(AD9523_READBACK_CTRL,
1027 AD9523_READBACK_CTRL_READ_BUFFERED);
1028 if (ret < 0)
1029 return ret;
1030
1031 ret = ad9523_io_update();
1032 if (ret < 0)
1033 return ret;
1034
1035 /*
1036 * PLL1 Setup
1037 */
1038 ret = ad9523_write(AD9523_PLL1_REF_A_DIVIDER,
1039 pdata->refa_r_div);
1040 if (ret < 0)
1041 return ret;
1042
1043 ret = ad9523_write(AD9523_PLL1_REF_B_DIVIDER,
1044 pdata->refb_r_div);
1045 if (ret < 0)
1046 return ret;
1047
1048 ret = ad9523_write(AD9523_PLL1_FEEDBACK_DIVIDER,
1049 pdata->pll1_feedback_div);
1050 if (ret < 0)
1051 return ret;
1052
1053 ret = ad9523_write(AD9523_PLL1_CHARGE_PUMP_CTRL,
1054 AD9523_PLL1_CHARGE_PUMP_CURRENT_nA(pdata->pll1_charge_pump_current_nA) |
1055 AD9523_PLL1_CHARGE_PUMP_MODE_NORMAL |
1056 AD9523_PLL1_BACKLASH_PW_MIN);
1057 if (ret < 0)
1058 return ret;
1059
1060 ret = ad9523_write(AD9523_PLL1_INPUT_RECEIVERS_CTRL,
1061 AD_IF(refa_diff_rcv_en, AD9523_PLL1_REFA_RCV_EN) |
1062 AD_IF(refb_diff_rcv_en, AD9523_PLL1_REFB_RCV_EN) |
1063 AD_IF(osc_in_diff_en, AD9523_PLL1_OSC_IN_DIFF_EN) |
1064 AD_IF(osc_in_cmos_neg_inp_en,
1065 AD9523_PLL1_OSC_IN_CMOS_NEG_INP_EN) |
1066 AD_IF(refa_diff_rcv_en, AD9523_PLL1_REFA_DIFF_RCV_EN) |
1067 AD_IF(refb_diff_rcv_en, AD9523_PLL1_REFB_DIFF_RCV_EN));
1068 if (ret < 0)
1069 return ret;
1070
1071 ret = ad9523_write(AD9523_PLL1_REF_CTRL,
1072 AD_IF(zd_in_diff_en, AD9523_PLL1_ZD_IN_DIFF_EN) |
1073 AD_IF(zd_in_cmos_neg_inp_en,
1074 AD9523_PLL1_ZD_IN_CMOS_NEG_INP_EN) |
1075 AD_IF(zero_delay_mode_internal_en,
1076 AD9523_PLL1_ZERO_DELAY_MODE_INT) |
1077 AD_IF(osc_in_feedback_en, AD9523_PLL1_OSC_IN_PLL_FEEDBACK_EN) |
1078 AD_IF(refa_cmos_neg_inp_en, AD9523_PLL1_REFA_CMOS_NEG_INP_EN) |
1079 AD_IF(refb_cmos_neg_inp_en, AD9523_PLL1_REFB_CMOS_NEG_INP_EN));
1080 if (ret < 0)
1081 return ret;
1082
1083 ret = ad9523_write(AD9523_PLL1_MISC_CTRL,
1084 AD9523_PLL1_REFB_INDEP_DIV_CTRL_EN |
1085 AD9523_PLL1_REF_MODE(pdata->ref_mode));
1086 if (ret < 0)
1087 return ret;
1088
1089 ret = ad9523_write(AD9523_PLL1_LOOP_FILTER_CTRL,
1090 AD9523_PLL1_LOOP_FILTER_RZERO(pdata->pll1_loop_filter_rzero));
1091 if (ret < 0)
1092 return ret;
1093 /*
1094 * PLL2 Setup
1095 */
1096
1097 ret = ad9523_write(AD9523_PLL2_CHARGE_PUMP,
1098 AD9523_PLL2_CHARGE_PUMP_CURRENT_nA(pdata->
1099 pll2_charge_pump_current_nA));
1100
1101 ret = ad9523_read(AD9523_PLL2_CHARGE_PUMP);
1102
1103 if (ret < 0)
1104 return ret;
1105
1106 ret = ad9523_write(AD9523_PLL2_FEEDBACK_DIVIDER_AB,
1107 AD9523_PLL2_FB_NDIV_A_CNT(pdata->pll2_ndiv_a_cnt) |
1108 AD9523_PLL2_FB_NDIV_B_CNT(pdata->pll2_ndiv_b_cnt));
1109 if (ret < 0)
1110 return ret;
1111
1112 ret = ad9523_write(AD9523_PLL2_CTRL,
1113 AD9523_PLL2_CHARGE_PUMP_MODE_NORMAL |
1114 AD9523_PLL2_BACKLASH_CTRL_EN |
1115 AD_IF(pll2_freq_doubler_en, AD9523_PLL2_FREQ_DOUBLER_EN));
1116 if (ret < 0)
1117 return ret;
1118
1119 st->vco_freq = (pdata->vcxo_freq * (pdata->pll2_freq_doubler_en ? 2 : 1)
1120 / pdata->pll2_r2_div) * AD9523_PLL2_FB_NDIV(pdata->
1121 pll2_ndiv_a_cnt, pdata->pll2_ndiv_b_cnt);
1122
1123 ret = ad9523_write(AD9523_PLL2_VCO_DIVIDER,
1124 AD9523_PLL2_VCO_DIV_M1(pdata->pll2_vco_diff_m1) |
1125 AD9523_PLL2_VCO_DIV_M2(pdata->pll2_vco_diff_m2) |
1126 AD_IFE(pll2_vco_diff_m1, 0,
1127 AD9523_PLL2_VCO_DIV_M1_PWR_DOWN_EN) |
1128 AD_IFE(pll2_vco_diff_m2, 0,
1129 AD9523_PLL2_VCO_DIV_M2_PWR_DOWN_EN));
1130 if (ret < 0)
1131 return ret;
1132
1133 if (pdata->pll2_vco_diff_m1)
1134 st->vco_out_freq[AD9523_VCO1] =
1135 st->vco_freq / pdata->pll2_vco_diff_m1;
1136
1137 if (pdata->pll2_vco_diff_m2)
1138 st->vco_out_freq[AD9523_VCO2] =
1139 st->vco_freq / pdata->pll2_vco_diff_m2;
1140
1141 st->vco_out_freq[AD9523_VCXO] = pdata->vcxo_freq;
1142
1143 ret = ad9523_write(AD9523_PLL2_R2_DIVIDER,
1144 AD9523_PLL2_R2_DIVIDER_VAL(pdata->pll2_r2_div));
1145 if (ret < 0)
1146 return ret;
1147
1148 ret = ad9523_write(AD9523_PLL2_LOOP_FILTER_CTRL,
1149 AD9523_PLL2_LOOP_FILTER_CPOLE1(pdata->cpole1) |
1150 AD9523_PLL2_LOOP_FILTER_RZERO(pdata->rzero) |
1151 AD9523_PLL2_LOOP_FILTER_RPOLE2(pdata->rpole2) |
1152 AD_IF(rzero_bypass_en,
1153 AD9523_PLL2_LOOP_FILTER_RZERO_BYPASS_EN));
1154 if (ret < 0)
1155 return ret;
1156
1157 for (i = 0; i < pdata->num_channels; i++) {
1158 chan = &pdata->channels[i];
1159 if (chan->channel_num < AD9523_NUM_CHAN) {
1160 active_mask |= (1 << chan->channel_num);
1161 ret = ad9523_write(AD9523_CHANNEL_CLOCK_DIST(chan->channel_num),
1162 AD9523_CLK_DIST_DRIVER_MODE(chan->driver_mode) |
1163 AD9523_CLK_DIST_DIV(chan->channel_divider) |
1164 AD9523_CLK_DIST_DIV_PHASE(chan->divider_phase) |
1165 (chan->sync_ignore_en ?
1166 AD9523_CLK_DIST_IGNORE_SYNC_EN : 0) |
1167 (chan->divider_output_invert_en ?
1168 AD9523_CLK_DIST_INV_DIV_OUTPUT_EN : 0) |
1169 (chan->low_power_mode_en ?
1170 AD9523_CLK_DIST_LOW_PWR_MODE_EN : 0) |
1171 (chan->output_dis ?
1172 AD9523_CLK_DIST_PWR_DOWN_EN : 0));
1173 if (ret < 0)
1174 return ret;
1175
1176 ret = ad9523_vco_out_map(chan->channel_num,
1177 chan->use_alt_clock_src);
1178 if (ret < 0)
1179 return ret;
1180 }
1181 }
1182
1183 for(i = 0; i < AD9523_NUM_CHAN; i++)
1184 {
1185 if(!(active_mask & (1 << i)))
1186 {
1187 ad9523_write(AD9523_CHANNEL_CLOCK_DIST(i),
1188 AD9523_CLK_DIST_DRIVER_MODE(TRISTATE) |
1189 AD9523_CLK_DIST_PWR_DOWN_EN);
1190 }
1191 }
1192
1193 ret = ad9523_write(AD9523_POWER_DOWN_CTRL, 0);
1194 if (ret < 0)
1195 return ret;
1196
1197 ret = ad9523_write(AD9523_STATUS_SIGNALS,
1198 AD9523_STATUS_MONITOR_01_PLL12_LOCKED);
1199 if (ret < 0)
1200 return ret;
1201
1202 ret = ad9523_io_update();
1203 if (ret < 0)
1204 return ret;
1205
1206 ret = ad9523_write(AD9523_PLL2_VCO_CTRL,
1207 AD9523_PLL2_VCO_CALIBRATE);
1208 if (ret < 0)
1209 return ret;
1210
1211 ret = ad9523_io_update();
1212 if (ret < 0)
1213 return ret;
1214
1215 ret = ad9523_write(AD9523_PLL2_VCO_CTRL, 0x00);
1216 if (ret < 0)
1217 return ret;
1218
1219 ret = ad9523_io_update();
1220 if (ret < 0)
1221 return ret;
1222
1223 return 0;
1224 }