Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/ad6676/ad6676.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad6676.c
3 * @brief Implementation of AD6676 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2015(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 #include <stdint.h>
44 #include <stdlib.h>
45 #include <xil_printf.h>
46 #include "platform_drivers.h"
47 #include "ad6676.h"
48
49 /***************************************************************************//**
50 * @brief ad6676_spi_read
51 *******************************************************************************/
52 int32_t ad6676_spi_read(ad6676_dev *dev,
53 uint16_t reg_addr,
54 uint8_t *reg_data)
55 {
56 uint8_t buf[3];
57 int32_t ret;
58
59 buf[0] = 0x80 | (reg_addr >> 8);
60 buf[1] = reg_addr & 0xFF;
61 buf[2] = 0x00;
62
63 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
64 *reg_data = buf[2];
65
66 return ret;
67 }
68
69 /***************************************************************************//**
70 * @brief ad6676_spi_write
71 *******************************************************************************/
72 int32_t ad6676_spi_write(ad6676_dev *dev,
73 uint16_t reg_addr,
74 uint8_t reg_data)
75 {
76 uint8_t buf[3];
77 int32_t ret;
78
79 buf[0] = reg_addr >> 8;
80 buf[1] = reg_addr & 0xFF;
81 buf[2] = reg_data;
82
83 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
84
85 return ret;
86 }
87
88 /***************************************************************************//**
89 * @brief ad6676_set_splitreg
90 *******************************************************************************/
91 static int32_t ad6676_set_splitreg(ad6676_dev *dev,
92 uint32_t reg,
93 uint32_t val)
94 {
95 int32_t ret;
96
97 ret = ad6676_spi_write(dev, reg, val & 0xFF);
98 ret |= ad6676_spi_write(dev, reg + 1, val >> 8);
99
100 return ret;
101 }
102
103 /***************************************************************************//**
104 * @brief ad6676_get_splitreg
105 *******************************************************************************/
106 static inline int32_t ad6676_get_splitreg(ad6676_dev *dev,
107 uint32_t reg,
108 uint32_t *val)
109 {
110 int32_t ret;
111 uint8_t reg_data;
112
113 ret = ad6676_spi_read(dev, reg, &reg_data);
114 if (ret < 0)
115 return ret;
116
117 *val = reg_data;
118
119 ret = ad6676_spi_read(dev, reg + 1, &reg_data);
120 if (ret < 0)
121 return ret;
122
123 *val |= ((uint32_t)reg_data << 8);
124
125 return ret;
126 }
127
128 /***************************************************************************//**
129 * @brief ad6676_set_fadc
130 *******************************************************************************/
131 static int32_t ad6676_set_fadc(ad6676_dev *dev,
132 uint32_t val)
133 {
134 return ad6676_set_splitreg(dev, AD6676_FADC_0,
135 clamp_t(uint32_t, val, MIN_FADC, MAX_FADC) / MHz);
136 }
137
138 /***************************************************************************//**
139 * @brief ad6676_get_fadc
140 *******************************************************************************/
141 static inline uint32_t ad6676_get_fadc(ad6676_dev *dev)
142 {
143 uint32_t val;
144 int32_t ret = ad6676_get_splitreg(dev, AD6676_FADC_0, &val);
145 if (ret < 0)
146 return 0;
147
148 return val * MHz;
149 }
150
151 /***************************************************************************//**
152 * @brief ad6676_set_fif
153 *******************************************************************************/
154 static int32_t ad6676_set_fif(ad6676_dev *dev,
155 uint32_t val)
156 {
157 struct ad6676_platform_data *pdata = dev->phy->pdata;
158
159 return ad6676_set_splitreg(dev, AD6676_FIF_0,
160 clamp_t(uint32_t, val, pdata->base.f_if_min_hz,
161 pdata->base.f_if_max_hz) / MHz);
162 }
163
164 /***************************************************************************//**
165 * @brief ad6676_get_fif
166 *******************************************************************************/
167 static uint32_t ad6676_get_fif(ad6676_dev *dev)
168 {
169 struct ad6676_platform_data *pdata = dev->phy->pdata;
170 uint64_t mix1 = 0, mix2 = 0;
171
172 ad6676_spi_read(dev, AD6676_MIX1_TUNING, (uint8_t *)&mix1);
173 ad6676_spi_read(dev, AD6676_MIX2_TUNING, (uint8_t *)&mix2);
174
175 mix1 = mix1 * pdata->base.f_adc_hz;
176 mix2 = mix2 * pdata->base.f_adc_hz;
177
178 do_div(&mix1, 64);
179 do_div(&mix2, dev->phy->m);
180
181 return mix1 + mix2;
182 }
183
184 /***************************************************************************//**
185 * @brief ad6676_set_bw
186 *******************************************************************************/
187 static int32_t ad6676_set_bw(ad6676_dev *dev,
188 uint32_t val)
189 {
190 return ad6676_set_splitreg(dev, AD6676_BW_0,
191 clamp_t(uint32_t, val, MIN_BW, MAX_BW) / MHz);
192 }
193
194 /***************************************************************************//**
195 * @brief ad6676_get_bw
196 *******************************************************************************/
197 static inline uint32_t ad6676_get_bw(ad6676_dev *dev)
198 {
199 uint32_t val;
200 int32_t ret = ad6676_get_splitreg(dev, AD6676_BW_0, &val);
201 if (ret < 0)
202 return 0;
203
204 return val * MHz;
205 }
206
207 /***************************************************************************//**
208 * @brief ad6676_set_decimation
209 *******************************************************************************/
210 static int32_t ad6676_set_decimation(ad6676_dev *dev,
211 uint32_t val)
212 {
213 switch (val) {
214 case 32:
215 val = DEC_32;
216 dev->phy->m = 4096;
217 break;
218 case 24:
219 val = DEC_24;
220 dev->phy->m = 3072;
221 break;
222 case 16:
223 val = DEC_16;
224 dev->phy->m = 4096;
225 break;
226 case 12:
227 val = DEC_12;
228 dev->phy->m = 3072;
229 break;
230 default:
231 return -1;
232 }
233
234 return ad6676_spi_write(dev, AD6676_DEC_MODE, val);
235 }
236
237 /***************************************************************************//**
238 * @brief ad6676_set_clk_synth
239 *******************************************************************************/
240 static int32_t ad6676_set_clk_synth(ad6676_dev *dev,
241 uint32_t refin_Hz,
242 uint32_t freq)
243 {
244 uint32_t f_pfd, reg_val, tout, div_val;
245 uint64_t val64;
246 int32_t ret;
247
248 if (refin_Hz < 40000000UL) {
249 f_pfd = 2 * refin_Hz;
250 div_val = R_DIV(3);
251 } else if (refin_Hz < 80000000UL) {
252 f_pfd = refin_Hz;
253 div_val = R_DIV(0);
254 } else if (refin_Hz < 160000000UL) {
255 f_pfd = refin_Hz / 2;
256 div_val = R_DIV(1);
257 } else if (refin_Hz < 320000000UL) {
258 f_pfd = refin_Hz / 4;
259 div_val = R_DIV(2);
260 } else
261 return -1;
262
263 /* Compute N val */
264
265 freq = clamp_t(uint32_t, freq, MIN_FADC_INT_SYNTH, MAX_FADC);
266
267 reg_val = freq / (f_pfd / 2);
268 ret = ad6676_set_splitreg(dev, AD6676_CLKSYN_INT_N_LSB, reg_val); /* 2A0 */
269 if (ret < 0)
270 return ret;
271
272 ret = ad6676_spi_write(dev, AD6676_CLKSYN_LOGEN, RESET_CAL);
273 if (ret < 0)
274 return ret;
275
276 /* Compute I_CP val */
277
278 reg_val = (f_pfd / MHz) * (freq / MHz) * (freq / MHz);
279 val64 = 13300000000ULL + reg_val / 2;
280 do_div(&val64, reg_val);
281 reg_val = min_t(uint64_t, 64U, val64 - 1);
282
283 ret = ad6676_spi_write(dev, AD6676_CLKSYN_I_CP, reg_val); /* I_CP 2AC */
284 if (ret < 0)
285 return ret;
286
287 /* VCO Configuration Settings */
288 if (freq <= 2950000000UL)
289 reg_val = 0xF0;
290 else if (freq < 3100000000UL)
291 reg_val = 0xE0;
292 else
293 reg_val = 0xD0;
294
295 ret = ad6676_spi_write(dev, AD6676_CLKSYN_VCO_BIAS, 0x37); /* 2AA */
296 if (ret < 0)
297 return ret;
298
299 ret = ad6676_spi_write(dev, AD6676_CLKSYN_VCO_VAR, reg_val); /* 2B7 */
300 if (ret < 0)
301 return ret;
302
303 ret = ad6676_spi_write(dev, AD6676_CLKSYN_R_DIV,
304 div_val | CLKSYN_R_DIV_RESERVED); /* Reference Div 2BB */
305 if (ret < 0)
306 return ret;
307
308
309 /* Enable CLKSYN and ADC clock */
310 ret = ad6676_spi_write(dev, AD6676_CLKSYN_ENABLE, /* 2A0 */
311 EN_OVER_IDE | EN_VCO | EN_VCO_ALC |
312 EN_VCO_PTAT | EN_ADC_CK | EN_SYNTH);
313 if (ret < 0)
314 return ret;
315
316 /* Start VCO calibration */
317 ret = ad6676_spi_write(dev, AD6676_CLKSYN_VCO_CAL, /* 2AB */
318 INIT_ALC_VALUE(0xC) | 0x5);
319 if (ret < 0)
320 return ret;
321
322 tout = 4;
323 do {
324 mdelay(1);
325 ad6676_spi_read(dev, AD6676_CLKSYN_STATUS, (uint8_t *)&reg_val);
326 } while (--tout && (reg_val & SYN_STAT_VCO_CAL_BUSY));
327
328 if (!tout)
329 xil_printf("VCO CAL failed (0x%X)\n", reg_val);
330
331
332 /* Start CP calibration */
333 ret = ad6676_spi_write(dev, AD6676_CLKSYN_CP_CAL, CP_CAL_EN); /* 2AD */
334 if (ret < 0)
335 return ret;
336
337 tout = 4;
338 do {
339 mdelay(1);
340 ad6676_spi_read(dev, AD6676_CLKSYN_STATUS, (uint8_t *)&reg_val);
341 reg_val &= SYN_STAT_PLL_LCK | SYN_STAT_VCO_CAL_BUSY | SYN_STAT_CP_CAL_DONE;
342 } while (--tout && (reg_val != (SYN_STAT_PLL_LCK | SYN_STAT_CP_CAL_DONE)));
343
344 if (!tout) {
345 xil_printf("AD6676 Synthesizer PLL unlocked (0x%X)\n", reg_val);
346 return -1;
347 }
348
349 return 0;
350 }
351
352 /***************************************************************************//**
353 * @brief ad6676_jesd_setup
354 *******************************************************************************/
355 static int32_t ad6676_set_extclk_cntl(ad6676_dev *dev,
356 uint32_t freq)
357 {
358 int ret;
359
360 xil_printf("%s: frequency %u\n", __func__, freq);
361
362 ret = ad6676_spi_write(dev, AD6676_CLKSYN_LOGEN, 0x5);
363 if (ret < 0)
364 return ret;
365
366 /* Enable EXT CLK and ADC clock */
367 ret = ad6676_spi_write(dev, AD6676_CLKSYN_ENABLE, /* 2A0 */
368 EN_EXT_CK | EN_ADC_CK);
369 if (ret < 0)
370 return ret;
371
372 return 0;
373 }
374
375 /***************************************************************************//**
376 * @brief ad6676_jesd_setup
377 *******************************************************************************/
378 static int32_t ad6676_jesd_setup(ad6676_dev *dev,
379 struct ad6676_jesd_conf *conf)
380 {
381 int32_t ret;
382
383 ret = ad6676_spi_write(dev, AD6676_SYNCB_CTRL,
384 (conf->sysref_pd ? PD_SYSREF_RX : 0) |
385 (conf->lvds_syncb ? LVDS_SYNCB : 0)); // lvds sync_n
386 ret |= ad6676_spi_write(dev, AD6676_DID, 0x01); // device id
387 ret |= ad6676_spi_write(dev, AD6676_BID, 0x05); // bank id
388 ret |= ad6676_spi_write(dev, AD6676_L, (conf->l - 1) |
389 (conf->scrambling_en ? SCR : 0)); // scrambling, 2 lanes
390 ret |= ad6676_spi_write(dev, AD6676_F, 0x01); // 2 bytes/frame
391 ret |= ad6676_spi_write(dev, AD6676_K, conf->f - 1); // 16 frames/multi-frame
392 ret |= ad6676_spi_write(dev, AD6676_M, 0x01); // 2 converters
393 ret |= ad6676_spi_write(dev, AD6676_S, 0x00); // 1 samples per frame
394 ret |= ad6676_spi_write(dev, AD6676_SER2, 0xBD);
395
396 return ret;
397
398 }
399
400 /***************************************************************************//**
401 * @brief ad6676_shuffle_setup
402 *******************************************************************************/
403 int32_t ad6676_shuffle_setup(ad6676_dev *dev,
404 struct ad6676_shuffler_conf *conf)
405 {
406 uint32_t reg_val, val, thresh;
407 int32_t i;
408
409 thresh = clamp_t(uint8_t, conf->shuffle_thresh, 0, 8U);
410
411 for (i = 0; i < 4; i++) {
412 if ((i + 1) == conf->shuffle_ctrl)
413 val = thresh;
414 else
415 val = 0xF;
416
417 reg_val |= (val << (i * 4));
418 }
419
420 return ad6676_set_splitreg(dev, AD6676_SHUFFLE_THREG0, reg_val);
421 }
422
423 /***************************************************************************//**
424 * @brief ad6676_calibrate
425 *******************************************************************************/
426 static int32_t ad6676_calibrate(ad6676_dev *dev,
427 uint32_t cal)
428 {
429 int32_t tout_i, tout_o = 2;
430 uint32_t done;
431
432 do {
433 ad6676_spi_write(dev, AD6676_CAL_CMD, cal);
434 tout_i = 2;
435
436 do {
437 mdelay(250);
438 ad6676_spi_read(dev, AD6676_CAL_DONE, (uint8_t *)&done);
439 done &= CAL_DONE;
440 } while (tout_i-- && !done);
441
442 if (!done) {
443 xil_printf("AD6676 CAL timeout (0x%X)\n", cal);
444 ad6676_spi_write(dev, AD6676_FORCE_END_CAL, FORCE_END_CAL);
445 ad6676_spi_write(dev, AD6676_FORCE_END_CAL, 0);
446 } else {
447 return 0;
448 }
449
450 } while (tout_o--);
451
452 xil_printf("AD6676 CAL failed (0x%X)\n", cal);
453
454 return -1;
455 }
456
457 /***************************************************************************//**
458 * @brief ad6676_reset
459 *******************************************************************************/
460 static int32_t ad6676_reset(ad6676_dev *dev,
461 uint8_t spi3wire)
462 {
463 int32_t ret;
464
465 ret = ad6676_spi_write(dev, AD6676_SPI_CONFIG,
466 SPI_CONF_SW_RESET |
467 (spi3wire ? 0 : SPI_CONF_SDIO_DIR));
468 mdelay(2);
469
470 return ret;
471 }
472
473 /***************************************************************************//**
474 * @brief ad6676_init
475 *******************************************************************************/
476 static int32_t ad6676_init(ad6676_dev *dev)
477 {
478 int32_t ret;
479 uint32_t reg_val;
480
481 ret = ad6676_reset(dev, dev->phy->pdata->spi3wire);
482 if (ret < 0)
483 return ret;
484
485 if (!dev->phy->pdata->base.use_extclk)
486 ad6676_set_clk_synth(dev, dev->phy->ref_clk, dev->phy->pdata->base.f_adc_hz);
487 else
488 ad6676_set_extclk_cntl(dev, dev->phy->pdata->base.f_adc_hz);
489
490 ad6676_jesd_setup(dev, &dev->phy->pdata->jesd);
491
492 ad6676_set_fadc(dev, dev->phy->pdata->base.f_adc_hz);
493 ad6676_set_fif(dev, dev->phy->pdata->base.f_if_hz);
494 ad6676_set_bw(dev, dev->phy->pdata->base.bw_hz);
495
496 ret |= ad6676_spi_write(dev, AD6676_LEXT, dev->phy->pdata->base.ext_l);
497 ret |= ad6676_spi_write(dev, AD6676_MRGN_L, dev->phy->pdata->base.bw_margin_low_mhz);
498 ret |= ad6676_spi_write(dev, AD6676_MRGN_U, dev->phy->pdata->base.bw_margin_high_mhz);
499 ret |= ad6676_spi_write(dev, AD6676_MRGN_IF, dev->phy->pdata->base.bw_margin_if_mhz);
500 ret |= ad6676_spi_write(dev, AD6676_XSCALE_1, dev->phy->pdata->base.scale);
501
502 ad6676_set_decimation(dev, 32);
503
504 ret = ad6676_calibrate(dev, RESON1_CAL | INIT_ADC);
505
506 ad6676_set_decimation(dev, dev->phy->pdata->base.decimation);
507
508 ret = ad6676_calibrate(dev, XCMD0 | XCMD1 | INIT_ADC | TUNE_ADC | FLASH_CAL);
509
510 ad6676_spi_read(dev, AD6676_JESDSYN_STATUS, (uint8_t *)&reg_val);
511 reg_val &= SYN_STAT_PLL_LCK;
512
513 if (reg_val != SYN_STAT_PLL_LCK) {
514 xil_printf("AD6676 JESD PLL unlocked (0x%X)\n", reg_val);
515 return -1;
516 }
517
518 return 0;
519 }
520
521 /***************************************************************************//**
522 * @brief ad6676_update
523 *******************************************************************************/
524 int32_t ad6676_update(ad6676_dev *dev,
525 struct ad6676_platform_data *pdata)
526 {
527 int32_t ret;
528
529 pdata->base.bw_hz = clamp_t(uint32_t, pdata->base.bw_hz,
530 pdata->base.f_adc_hz / 200,
531 pdata->base.f_adc_hz / 20);
532
533 pdata->base.f_if_hz = clamp_t(uint32_t, pdata->base.f_if_hz,
534 pdata->base.f_if_min_hz,
535 pdata->base.f_if_max_hz);
536
537 ret = ad6676_set_fif(dev, pdata->base.f_if_hz);
538 ret |= ad6676_set_bw(dev, pdata->base.bw_hz);
539
540 ret |= ad6676_spi_write(dev, AD6676_MRGN_L, pdata->base.bw_margin_low_mhz);
541 ret |= ad6676_spi_write(dev, AD6676_MRGN_U, pdata->base.bw_margin_high_mhz);
542 ret |= ad6676_spi_write(dev, AD6676_MRGN_IF, pdata->base.bw_margin_if_mhz);
543 ret |= ad6676_spi_write(dev, AD6676_XSCALE_1, pdata->base.scale);
544
545 ret = ad6676_calibrate(dev, RESON1_CAL | INIT_ADC);
546 ret = ad6676_calibrate(dev, XCMD0 | XCMD1 | INIT_ADC | TUNE_ADC | FLASH_CAL);
547
548 pdata->base.f_if_hz = ad6676_get_fif(dev);
549
550 return ret;
551 }
552
553 /***************************************************************************//**
554 * @brief ad6676_outputmode_set
555 *******************************************************************************/
556 static int32_t ad6676_outputmode_set(ad6676_dev *dev,
557 uint32_t mode)
558 {
559 int32_t ret;
560
561 ret = ad6676_spi_write(dev, AD6676_DP_CTRL, mode);
562 if (ret < 0)
563 return ret;
564
565 return ad6676_spi_write(dev, AD6676_TEST_GEN, TESTGENMODE_OFF);
566 }
567
568 /***************************************************************************//**
569 * @brief ad6676_set_if_frequency
570 *******************************************************************************/
571 int32_t ad6676_set_if_frequency(ad6676_dev *dev,
572 uint32_t if_freq_hz)
573 {
574 dev->phy->pdata->base.f_if_hz = if_freq_hz;
575 ad6676_update(dev, dev->phy->pdata);
576
577 return 0;
578 }
579
580 /***************************************************************************//**
581 * @brief ad6676_get_if_freq
582 *******************************************************************************/
583 int32_t ad6676_get_if_frequency(ad6676_dev *dev,
584 uint32_t *if_freq_hz)
585 {
586 *if_freq_hz = dev->phy->pdata->base.f_if_hz;
587
588 return 0;
589 }
590
591 /***************************************************************************//**
592 * @brief ad6676_set_if_bandwidth
593 *******************************************************************************/
594 int32_t ad6676_set_if_bandwidth(ad6676_dev *dev,
595 uint32_t if_bw_hz)
596 {
597 dev->phy->pdata->base.bw_hz = if_bw_hz;
598 ad6676_update(dev, dev->phy->pdata);
599
600 return 0;
601 }
602
603 /***************************************************************************//**
604 * @brief ad6676_get_if_bandwidth
605 *******************************************************************************/
606 int32_t ad6676_get_if_bandwidth(ad6676_dev *dev,
607 uint32_t *if_bw_hz)
608 {
609 *if_bw_hz = dev->phy->pdata->base.bw_hz;
610
611 return 0;
612 }
613
614 /***************************************************************************//**
615 * @brief ad6676_set_scale
616 *******************************************************************************/
617 int32_t ad6676_set_scale(ad6676_dev *dev,
618 uint8_t scale)
619 {
620 scale = clamp(scale, 0, 64);
621 dev->phy->pdata->base.scale = scale;
622 ad6676_update(dev, dev->phy->pdata);
623
624 return 0;
625 }
626
627 /***************************************************************************//**
628 * @brief ad6676_get_scale
629 *******************************************************************************/
630 int32_t ad6676_get_scale(ad6676_dev *dev,
631 uint8_t *scale)
632 {
633 *scale = dev->phy->pdata->base.scale;
634
635 return 0;
636 }
637
638 /***************************************************************************//**
639 * @brief ad6676_set_attenuation
640 *******************************************************************************/
641 int32_t ad6676_set_attenuation(ad6676_dev *dev,
642 uint8_t attenuation)
643 {
644 dev->phy->pdata->base.attenuation = clamp(attenuation, 0, 27);
645 ad6676_spi_write(dev, AD6676_ATTEN_VALUE_PIN0,
646 dev->phy->pdata->base.attenuation);
647 ad6676_spi_write(dev, AD6676_ATTEN_VALUE_PIN1,
648 dev->phy->pdata->base.attenuation);
649
650 return 0;
651 }
652
653 /***************************************************************************//**
654 * @brief ad6676_get_attenuation
655 *******************************************************************************/
656 int32_t ad6676_get_attenuation(ad6676_dev *dev,
657 uint8_t *attenuation)
658 {
659 *attenuation = dev->phy->pdata->base.attenuation;
660
661 return 0;
662 }
663
664 /***************************************************************************//**
665 * @brief ad6676_gpio_config
666 *******************************************************************************/
667 int32_t ad6676_gpio_config(ad6676_dev *dev)
668 {
669 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_oen, GPIO_OUT);
670 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_sela, GPIO_OUT);
671 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_selb, GPIO_OUT);
672 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s0, GPIO_OUT);
673 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s1, GPIO_OUT);
674 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_resetb, GPIO_OUT);
675 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_agc1, GPIO_OUT);
676 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_agc2, GPIO_OUT);
677 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_agc3, GPIO_IN);
678 gpio_set_direction(&dev->gpio_dev, dev->phy->pdata->gpio.adc_agc4, GPIO_IN);
679
680 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_oen, GPIO_LOW);
681
682 switch (dev->phy->pdata->base.decimation) {
683 case 12:
684 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s0, GPIO_HIGH);
685 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s1, GPIO_HIGH);
686 break;
687 case 16:
688 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s0, GPIO_LOW);
689 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s1, GPIO_HIGH);
690 break;
691 case 24:
692 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s0, GPIO_LOW);
693 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s1, GPIO_HIGH);
694 break;
695 case 32:
696 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s0, GPIO_LOW);
697 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_s1, GPIO_LOW);
698 break;
699 default:
700 return -1;
701 }
702
703 if (dev->phy->pdata->base.use_extclk) {
704 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_sela, GPIO_HIGH);
705 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_selb, GPIO_LOW);
706 } else {
707 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_sela, GPIO_LOW);
708 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_selb, GPIO_HIGH);
709 }
710
711 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_resetb, GPIO_HIGH);
712 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_agc1, GPIO_LOW);
713 gpio_set_value(&dev->gpio_dev, dev->phy->pdata->gpio.adc_agc2, GPIO_LOW);
714
715 return 0;
716 }
717
718 /***************************************************************************//**
719 * @brief ad6676_setup
720 *******************************************************************************/
721 int32_t ad6676_setup(ad6676_dev **device,
722 ad6676_init_param init_param)
723 {
724 ad6676_dev *dev;
725 uint8_t reg_id;
726 uint8_t status;
727 int32_t ret;
728
729 dev = (ad6676_dev *)malloc(sizeof(*dev));
730 if (!dev) {
731 return -1;
732 }
733
734 dev->gpio_dev.device_id = init_param.gpio_device_id;
735 dev->gpio_dev.type = init_param.gpio_type;
736 ret = gpio_init(&dev->gpio_dev);
737
738 dev->spi_dev.chip_select = init_param.spi_chip_select;
739 dev->spi_dev.mode = init_param.spi_mode;
740 dev->spi_dev.device_id = init_param.spi_device_id;
741 dev->spi_dev.type = init_param.spi_type;
742 ret |= spi_init(&dev->spi_dev);
743
744 dev->phy = (struct ad6676_phy *)malloc(sizeof(*dev->phy));
745 if (!dev->phy) {
746 return -1;
747 }
748
749 dev->phy->pdata = (struct ad6676_platform_data *)malloc(sizeof(*dev->phy->pdata));
750 if (!dev->phy->pdata) {
751 return -1;
752 }
753
754 dev->phy->ref_clk = init_param.reference_clk_rate;
755
756 dev->phy->pdata->spi3wire = init_param.spi_3wire_enable;
757
758 /* Base Configuration */
759 dev->phy->pdata->base.f_adc_hz = init_param.adc_frequency_hz;
760 dev->phy->pdata->base.f_if_hz = init_param.intermediate_frequency_hz;
761 dev->phy->pdata->base.f_if_min_hz = init_param.intermediate_frequency_min_hz;
762 dev->phy->pdata->base.f_if_max_hz = init_param.intermediate_frequency_max_hz;
763 dev->phy->pdata->base.bw_hz = init_param.bandwidth_hz;
764 dev->phy->pdata->base.bw_margin_low_mhz = init_param.bandwidth_margin_low_mhz;
765 dev->phy->pdata->base.bw_margin_high_mhz = init_param.bandwidth_margin_high_mhz;
766 dev->phy->pdata->base.bw_margin_if_mhz = init_param.bandwidth_margin_if_mhz;
767 dev->phy->pdata->base.decimation = init_param.decimation;
768 dev->phy->pdata->base.ext_l = init_param.external_inductance_l_nh;
769 dev->phy->pdata->base.scale = init_param.idac1_fullscale_adjust;
770 dev->phy->pdata->base.use_extclk = init_param.use_external_clk_enable;
771 dev->phy->pdata->base.fadc_fixed = init_param.adc_frequency_fixed_enable;
772
773 /* JESD Configuration */
774 dev->phy->pdata->jesd.scrambling_en = init_param.jesd_scrambling_enable;
775 dev->phy->pdata->jesd.lvds_syncb = init_param.jesd_use_lvds_syncb_enable;
776 dev->phy->pdata->jesd.sysref_pd = init_param.jesd_powerdown_sysref_enable;
777 dev->phy->pdata->jesd.l = init_param.jesd_l_lanes;
778 dev->phy->pdata->jesd.f = init_param.jesd_f_frames_per_multiframe;
779
780 /* Shuffler Configuration */
781 dev->phy->pdata->shuffler.shuffle_ctrl = init_param.shuffler_control;
782 dev->phy->pdata->shuffler.shuffle_thresh = init_param.shuffler_thresh;
783
784 /* GPIO */
785 dev->phy->pdata->gpio.adc_oen = init_param.gpio_adc_oen;
786 dev->phy->pdata->gpio.adc_sela = init_param.gpio_adc_sela;
787 dev->phy->pdata->gpio.adc_selb = init_param.gpio_adc_selb;
788 dev->phy->pdata->gpio.adc_s0 = init_param.gpio_adc_s0;
789 dev->phy->pdata->gpio.adc_s1 = init_param.gpio_adc_s1;
790 dev->phy->pdata->gpio.adc_resetb = init_param.gpio_adc_resetb;
791 dev->phy->pdata->gpio.adc_agc1 = init_param.gpio_adc_agc1;
792 dev->phy->pdata->gpio.adc_agc2 = init_param.gpio_adc_agc2;
793 dev->phy->pdata->gpio.adc_agc3 = init_param.gpio_adc_agc3;
794 dev->phy->pdata->gpio.adc_agc4 = init_param.gpio_adc_agc4;
795
796 dev->phy->pdata->base.attenuation = 12;
797
798 ad6676_gpio_config(dev);
799
800 ad6676_spi_read(dev, AD6676_CHIP_ID0, &reg_id);
801 if (reg_id != CHIPID0_AD6676) {
802 xil_printf("Unrecognized CHIP_ID 0x%X\n", reg_id);
803 goto error;
804 }
805
806 ad6676_init(dev);
807
808 ad6676_outputmode_set(dev, DP_CTRL_TWOS_COMPLEMENT);
809
810 ad6676_spi_read(dev, AD6676_CLKSYN_STATUS, &status);
811 if ((status & 0xb) != (SYN_STAT_PLL_LCK | SYN_STAT_CP_CAL_DONE)) {
812 xil_printf("AD6676 PLL not locked!!\n\r");
813 }
814
815 ad6676_spi_read(dev, AD6676_JESDSYN_STATUS, &status);
816 if ((status & 0xb) != (SYN_STAT_PLL_LCK | SYN_STAT_CP_CAL_DONE)) {
817 xil_printf("AD6676 JESD PLL not locked!!\n\r");
818 }
819
820 *device = dev;
821
822 xil_printf("AD6676 successfully initialized.\n");
823
824 return ret;
825
826 error:
827 free(dev->phy->pdata);
828 free(dev->phy);
829
830 return -1;
831 }