Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file adf4350.c
3 * @brief Implementation of ADF4350 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2012-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 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include <malloc.h>
45 #include <stdio.h>
46 #include "platform_drivers.h"
47 #include "adf4350.h"
48
49 /***************************************************************************//**
50 * @brief Writes 4 bytes of data to ADF4350.
51 *
52 * @param data - Data value to write.
53 *
54 * @return Returns 0 in case of success or negative error code..
55 *******************************************************************************/
56 int32_t adf4350_write(adf4350_dev *dev,
57 uint32_t data)
58 {
59 uint8_t txData[4];
60
61 txData[0] = (data & 0xFF000000) >> 24;
62 txData[1] = (data & 0x00FF0000) >> 16;
63 txData[2] = (data & 0x0000FF00) >> 8;
64 txData[3] = (data & 0x000000FF) >> 0;
65
66 return spi_write_and_read(&dev->spi_dev, txData, 4);
67 }
68
69 /***************************************************************************//**
70 * @brief Updates the registers values.
71 *
72 * @param st - The selected structure.
73 *
74 * @return Returns 0 in case of success or negative error code.
75 *******************************************************************************/
76 int32_t adf4350_sync_config(adf4350_dev *dev)
77 {
78 int32_t ret, i, doublebuf = 0;
79
80 for (i = ADF4350_REG5; i >= ADF4350_REG0; i--)
81 {
82 if ((dev->regs_hw[i] != dev->regs[i]) ||
83 ((i == ADF4350_REG0) && doublebuf))
84 {
85 switch (i)
86 {
87 case ADF4350_REG1:
88 case ADF4350_REG4:
89 doublebuf = 1;
90 break;
91 }
92
93 dev->val = (dev->regs[i] | i);
94 ret = adf4350_write(dev, dev->val);
95 if (ret < 0)
96 return ret;
97 dev->regs_hw[i] = dev->regs[i];
98 }
99 }
100
101 return 0;
102 }
103
104 /***************************************************************************//**
105 * @brief Increases the R counter value until the ADF4350_MAX_FREQ_PFD is
106 * greater than PFD frequency.
107 *
108 * @param st - The selected structure.
109 * @param r_cnt - Initial r_cnt value.
110 *
111 * @return Returns 0 in case of success or negative error code.
112 *******************************************************************************/
113 int32_t adf4350_tune_r_cnt(adf4350_dev *dev,
114 uint16_t r_cnt)
115 {
116 do
117 {
118 r_cnt++;
119 dev->fpfd = (dev->clkin * (dev->pdata->ref_doubler_en ? 2 : 1)) /
120 (r_cnt * (dev->pdata->ref_div2_en ? 2 : 1));
121 } while (dev->fpfd > ADF4350_MAX_FREQ_PFD);
122
123 return r_cnt;
124 }
125
126 /***************************************************************************//**
127 * @brief Computes the greatest common divider of two numbers
128 *
129 * @return Returns the gcd.
130 *******************************************************************************/
131 uint32_t gcd(uint32_t x, uint32_t y)
132 {
133 int32_t tmp;
134
135 tmp = y > x ? x : y;
136
137
138 while((x % tmp) || (y % tmp))
139 {
140 tmp--;
141 }
142
143 return tmp;
144 }
145
146 /***************************************************************************//**
147 * @brief Sets the ADF4350 frequency.
148 *
149 * @param st - The selected structure.
150 * @param freq - The desired frequency value.
151 *
152 * @return calculatedFrequency - The actual frequency value that was set.
153 *******************************************************************************/
154 int64_t adf4350_set_freq(adf4350_dev *dev,
155 uint64_t freq)
156 {
157 uint64_t tmp;
158 uint32_t div_gcd, prescaler, chspc;
159 uint16_t mdiv, r_cnt = 0;
160 uint8_t band_sel_div;
161 int32_t ret;
162
163 if ((freq > ADF4350_MAX_OUT_FREQ) || (freq < ADF4350_MIN_OUT_FREQ))
164 return -1;
165
166 if (freq > ADF4350_MAX_FREQ_45_PRESC) {
167 prescaler = ADF4350_REG1_PRESCALER;
168 mdiv = 75;
169 }
170 else
171 {
172 prescaler = 0;
173 mdiv = 23;
174 }
175
176 dev->r4_rf_div_sel = 0;
177
178 while (freq < ADF4350_MIN_VCO_FREQ)
179 {
180 freq <<= 1;
181 dev->r4_rf_div_sel++;
182 }
183
184 /*
185 * Allow a predefined reference division factor
186 * if not set, compute our own
187 */
188 if (dev->pdata->ref_div_factor)
189 r_cnt = dev->pdata->ref_div_factor - 1;
190
191 chspc = dev->chspc;
192
193 do
194 {
195 do
196 {
197 do
198 {
199 r_cnt = adf4350_tune_r_cnt(dev, r_cnt);
200 dev->r1_mod = dev->fpfd / chspc;
201 if (r_cnt > ADF4350_MAX_R_CNT)
202 {
203 /* try higher spacing values */
204 chspc++;
205 r_cnt = 0;
206 }
207 } while ((dev->r1_mod > ADF4350_MAX_MODULUS) && r_cnt);
208 } while (r_cnt == 0);
209
210
211 tmp = freq * (uint64_t)dev->r1_mod + (dev->fpfd > 1);
212
213 tmp = (tmp / dev->fpfd); /* Div round closest (n + d/2)/d */
214
215 dev->r0_fract = tmp % dev->r1_mod;
216 tmp = tmp / dev->r1_mod;
217
218 dev->r0_int = tmp;
219 } while (mdiv > dev->r0_int);
220
221 band_sel_div = dev->fpfd % ADF4350_MAX_BANDSEL_CLK >
222 ADF4350_MAX_BANDSEL_CLK / 2 ?
223 dev->fpfd / ADF4350_MAX_BANDSEL_CLK + 1 :
224 dev->fpfd / ADF4350_MAX_BANDSEL_CLK;
225
226 if (dev->r0_fract && dev->r1_mod) {
227 div_gcd = gcd(dev->r1_mod, dev->r0_fract);
228 dev->r1_mod /= div_gcd;
229 dev->r0_fract /= div_gcd;
230 }
231 else
232 {
233 dev->r0_fract = 0;
234 dev->r1_mod = 1;
235 }
236
237 dev->regs[ADF4350_REG0] = ADF4350_REG0_INT(dev->r0_int) |
238 ADF4350_REG0_FRACT(dev->r0_fract);
239
240 dev->regs[ADF4350_REG1] = ADF4350_REG1_PHASE(1) |
241 ADF4350_REG1_MOD(dev->r1_mod) |
242 prescaler;
243
244 dev->regs[ADF4350_REG2] =
245 ADF4350_REG2_10BIT_R_CNT(r_cnt) |
246 ADF4350_REG2_DOUBLE_BUFF_EN |
247 (dev->pdata->ref_doubler_en ? ADF4350_REG2_RMULT2_EN : 0) |
248 (dev->pdata->ref_div2_en ? ADF4350_REG2_RDIV2_EN : 0) |
249 (dev->pdata->r2_user_settings & (ADF4350_REG2_PD_POLARITY_POS |
250 ADF4350_REG2_LDP_6ns | ADF4350_REG2_LDF_INT_N |
251 ADF4350_REG2_CHARGE_PUMP_CURR_uA(5000) |
252 ADF4350_REG2_MUXOUT(0x7) | ADF4350_REG2_NOISE_MODE(0x9)));
253
254 dev->regs[ADF4350_REG3] = dev->pdata->r3_user_settings &
255 (ADF4350_REG3_12BIT_CLKDIV(0xFFF) |
256 ADF4350_REG3_12BIT_CLKDIV_MODE(0x3) |
257 ADF4350_REG3_12BIT_CSR_EN);
258
259 dev->regs[ADF4350_REG4] =
260 ADF4350_REG4_FEEDBACK_FUND |
261 ADF4350_REG4_RF_DIV_SEL(dev->r4_rf_div_sel) |
262 ADF4350_REG4_8BIT_BAND_SEL_CLKDIV(band_sel_div) |
263 ADF4350_REG4_RF_OUT_EN |
264 (dev->pdata->r4_user_settings &
265 (ADF4350_REG4_OUTPUT_PWR(0x3) |
266 ADF4350_REG4_AUX_OUTPUT_PWR(0x3) |
267 ADF4350_REG4_AUX_OUTPUT_EN |
268 ADF4350_REG4_AUX_OUTPUT_FUND |
269 ADF4350_REG4_MUTE_TILL_LOCK_EN));
270
271 dev->regs[ADF4350_REG5] = ADF4350_REG5_LD_PIN_MODE_DIGITAL | 0x00180000;
272
273 ret = adf4350_sync_config(dev);
274 if(ret < 0)
275 {
276 return ret;
277 }
278
279 tmp = (uint64_t)((dev->r0_int * dev->r1_mod) +
280 dev->r0_fract) * (uint64_t)dev->fpfd;
281 tmp = tmp / ((uint64_t)dev->r1_mod * ((uint64_t)1 << dev->r4_rf_div_sel));
282
283 return tmp;
284 }
285
286 /***************************************************************************//**
287 * @brief Initializes the ADF4350.
288 *
289 * @param spiBaseAddr - SPI peripheral AXI base address.
290 * @param ssNo - Slave select line on which the slave is connected.
291 *
292 * @return Returns 0 in case of success or negative error code.
293 *******************************************************************************/
294 int32_t adf4350_setup(adf4350_dev **device,
295 adf4350_init_param init_param)
296 {
297 adf4350_dev *dev;
298 int32_t ret;
299
300 dev = (adf4350_dev *)malloc(sizeof(*dev));
301 if (!dev) {
302 return -1;
303 }
304
305 dev->spi_dev.chip_select = init_param.spi_chip_select;
306 dev->spi_dev.mode = init_param.spi_mode;
307 dev->spi_dev.device_id = init_param.spi_device_id;
308 dev->spi_dev.type = init_param.spi_type;
309 ret = spi_init(&dev->spi_dev);
310
311 dev->pdata = (struct adf4350_platform_data *)malloc(sizeof(*dev->pdata));
312 if (!dev->pdata)
313 return -1;
314
315 dev->pdata->clkin = init_param.clkin;
316
317 dev->pdata->channel_spacing = init_param.channel_spacing;
318
319 dev->pdata->power_up_frequency = init_param.power_up_frequency;
320 dev->pdata->ref_div_factor = init_param.reference_div_factor;
321 dev->pdata->ref_doubler_en = init_param.reference_doubler_enable;
322 dev->pdata->ref_div2_en = init_param.reference_div2_enable;
323
324 /* r2_user_settings */
325
326 dev->pdata->r2_user_settings =
327 init_param.phase_detector_polarity_positive_enable ?
328 ADF4350_REG2_PD_POLARITY_POS : 0;
329 dev->pdata->r2_user_settings |=
330 init_param.lock_detect_precision_6ns_enable ?
331 ADF4350_REG2_LDP_6ns : 0;
332 dev->pdata->r2_user_settings |=
333 init_param.lock_detect_function_integer_n_enable ?
334 ADF4350_REG2_LDF_INT_N : 0;
335 dev->pdata->r2_user_settings |=
336 ADF4350_REG2_CHARGE_PUMP_CURR_uA(init_param.charge_pump_current);
337 dev->pdata->r2_user_settings |=
338 ADF4350_REG2_MUXOUT(init_param.muxout_select);
339 dev->pdata->r2_user_settings |=
340 init_param.low_spur_mode_enable ? ADF4350_REG2_NOISE_MODE(0x3) : 0;
341
342 /* r3_user_settings */
343
344 dev->pdata->r3_user_settings = init_param.cycle_slip_reduction_enable ?
345 ADF4350_REG3_12BIT_CSR_EN : 0;
346 dev->pdata->r3_user_settings |= init_param.charge_cancellation_enable ?
347 ADF4351_REG3_CHARGE_CANCELLATION_EN : 0;
348 dev->pdata->r3_user_settings |= init_param.anti_backlash_3ns_enable ?
349 ADF4351_REG3_ANTI_BACKLASH_3ns_EN : 0;
350 dev->pdata->r3_user_settings |=
351 init_param.band_select_clock_mode_high_enable ?
352 ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH : 0;
353 dev->pdata->r3_user_settings |=
354 ADF4350_REG3_12BIT_CLKDIV(init_param.clk_divider_12bit);
355 dev->pdata->r3_user_settings |=
356 ADF4350_REG3_12BIT_CLKDIV_MODE(init_param.clk_divider_mode);
357
358 /* r4_user_settings */
359
360 dev->pdata->r4_user_settings = init_param.aux_output_enable ?
361 ADF4350_REG4_AUX_OUTPUT_EN : 0;
362 dev->pdata->r4_user_settings |= init_param.aux_output_fundamental_enable ?
363 ADF4350_REG4_AUX_OUTPUT_FUND : 0;
364 dev->pdata->r4_user_settings |= init_param.mute_till_lock_enable ?
365 ADF4350_REG4_MUTE_TILL_LOCK_EN : 0;
366 dev->pdata->r4_user_settings |=
367 ADF4350_REG4_OUTPUT_PWR(init_param.output_power);
368 dev->pdata->r4_user_settings |=
369 ADF4350_REG4_AUX_OUTPUT_PWR(init_param.aux_output_power);
370
371 adf4350_out_altvoltage0_refin_frequency(dev, dev->pdata->clkin);
372 adf4350_out_altvoltage0_frequency_resolution(dev,
373 dev->pdata->channel_spacing);
374 adf4350_out_altvoltage0_frequency(dev, dev->pdata->power_up_frequency);
375
376 *device = dev;
377
378 printf("ADF4350 successfully initialized.\n");
379
380 return ret;
381 }
382
383 /***************************************************************************//**
384 * @brief Stores PLL 0 frequency in Hz.
385 *
386 * @param Hz - The selected frequency.
387 *
388 * @return Returns the selected frequency.
389 *******************************************************************************/
390 int64_t adf4350_out_altvoltage0_frequency(adf4350_dev *dev,
391 int64_t Hz)
392 {
393 return adf4350_set_freq(dev, Hz);
394 }
395
396 /***************************************************************************//**
397 * @brief Stores PLL 0 frequency resolution/channel spacing in Hz.
398 *
399 * @param Hz - The selected frequency.
400 *
401 * @return Returns the selected frequency.
402 *******************************************************************************/
403 int32_t adf4350_out_altvoltage0_frequency_resolution(adf4350_dev *dev,
404 int32_t Hz)
405 {
406 if(Hz != INT32_MAX)
407 {
408 dev->chspc = Hz;
409 }
410
411 return dev->chspc;
412 }
413
414 /***************************************************************************//**
415 * @brief Sets PLL 0 REFin frequency in Hz.
416 *
417 * @param Hz - The selected frequency.
418 *
419 * @return Returns the selected frequency.
420 *******************************************************************************/
421 int64_t adf4350_out_altvoltage0_refin_frequency(adf4350_dev *dev,
422 int64_t Hz)
423 {
424 if(Hz != INT32_MAX)
425 {
426 dev->clkin = Hz;
427 }
428
429 return dev->clkin;
430 }
431
432 /***************************************************************************//**
433 * @brief Powers down the PLL.
434 *
435 * @param pwd - Power option.
436 * Example: 0 - Power up the PLL.
437 * 1 - Power down the PLL.
438 *
439 * @return Returns the PLL's power status.
440 *******************************************************************************/
441 int32_t adf4350_out_altvoltage0_powerdown(adf4350_dev *dev,
442 int32_t pwd)
443 {
444 if(pwd == 1)
445 {
446 dev->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN;
447 adf4350_sync_config(dev);
448 }
449 if(pwd == 0)
450 {
451 dev->regs[ADF4350_REG2] &= ~ADF4350_REG2_POWER_DOWN_EN;
452 adf4350_sync_config(dev);
453 }
454
455
456 return (dev->regs[ADF4350_REG2] & ADF4350_REG2_POWER_DOWN_EN);
457 }