Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad9265.c
3 * @brief Implementation of AD9265 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 <stdio.h>
45 #include <stdlib.h>
46 #include "adc_core.h"
47 #include "platform_drivers.h"
48 #include "ad9265.h"
49
50 #define DCO_DEBUG
51
52 /***************************************************************************//**
53 * @brief ad9265_spi_read
54 *******************************************************************************/
55 int32_t ad9265_spi_read(ad9265_dev *dev,
56 uint16_t reg_addr,
57 uint8_t *reg_data)
58 {
59 uint8_t buf[3];
60 int32_t ret;
61
62 buf[0] = 0x80 | (reg_addr >> 8);
63 buf[1] = reg_addr & 0xFF;
64 buf[2] = 0x00;
65
66 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
67 *reg_data = buf[2];
68
69 return ret;
70 }
71
72 /***************************************************************************//**
73 * @brief ad9265_spi_write
74 *******************************************************************************/
75 int32_t ad9265_spi_write(ad9265_dev *dev,
76 uint16_t reg_addr,
77 uint8_t reg_data)
78 {
79 uint8_t buf[3];
80 int32_t ret;
81
82 buf[0] = reg_addr >> 8;
83 buf[1] = reg_addr & 0xFF;
84 buf[2] = reg_data;
85
86 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
87
88 return ret;
89 }
90
91 /***************************************************************************//**
92 * @brief ad9265_setup
93 *******************************************************************************/
94 int32_t ad9265_outputmode_set(ad9265_dev *dev,
95 uint8_t mode)
96 {
97 int32_t ret;
98
99 ret = ad9265_spi_write(dev, AD9265_REG_OUTPUT_MODE, mode);
100 if (ret < 0)
101 return ret;
102 ret = ad9265_spi_write(dev, AD9265_REG_TEST_IO, TESTMODE_OFF);
103 if (ret < 0)
104 return ret;
105
106 return ad9265_spi_write(dev, AD9265_REG_TRANSFER, TRANSFER_SYNC);
107 }
108
109 /***************************************************************************//**
110 * @brief ad9265_setup
111 *******************************************************************************/
112 int32_t ad9265_testmode_set(ad9265_dev *dev,
113 uint8_t chan,
114 uint8_t mode)
115 {
116 ad9265_spi_write(dev, AD9265_REG_CHAN_INDEX, 1 << chan);
117 ad9265_spi_write(dev, AD9265_REG_TEST_IO, mode);
118 ad9265_spi_write(dev, AD9265_REG_CHAN_INDEX, 0x3);
119 ad9265_spi_write(dev, AD9265_REG_TRANSFER, TRANSFER_SYNC);
120
121 return 0;
122 }
123
124 /***************************************************************************//**
125 * @brief ad9265_calibrate
126 *******************************************************************************/
127 int32_t ad9265_calibrate(ad9265_dev *dev,
128 uint8_t dco,
129 uint8_t dco_en,
130 uint8_t nb_lanes,
131 adc_core core)
132 {
133 int32_t ret, val, cnt, start, max_start, max_cnt;
134 uint32_t stat, inv_range = 0, do_inv, lane,
135 chan_ctrl0, max_val = dco ? 32 : 31;
136 unsigned char err_field[66];
137 uint32_t reg_cntrl;
138
139 ret = ad9265_outputmode_set(dev, dev->output_mode & ~OUTPUT_MODE_TWOS_COMPLEMENT);
140 if (ret < 0)
141 return ret;
142
143 adc_read(core, ADC_REG_CHAN_CNTRL(0), &chan_ctrl0);
144
145 do {
146 if (!dco) {
147 adc_read(core, ADC_REG_CNTRL, &reg_cntrl);
148
149 if (inv_range)
150 reg_cntrl |= ADC_DDR_EDGESEL;
151 else
152 reg_cntrl &= ~ADC_DDR_EDGESEL;
153 adc_write(core, ADC_REG_CNTRL, reg_cntrl);
154 }
155
156 ad9265_testmode_set(dev, 0, TESTMODE_PN9_SEQ);
157 adc_write(core, ADC_REG_CHAN_CNTRL(0), ADC_ENABLE);
158 adc_set_pnsel(core, 0, ADC_PN9);
159 adc_write(core, ADC_REG_CHAN_STATUS(0), ~0);
160
161 for (val = 0; val <= max_val; val++) {
162 if (dco) {
163 ad9265_spi_write(dev, AD9265_REG_OUTPUT_DELAY,
164 val > 0 ? ((val - 1) | dco_en) : 0);
165 ad9265_spi_write(dev, AD9265_REG_TRANSFER,
166 TRANSFER_SYNC);
167 } else {
168 for (lane = 0; lane < nb_lanes; lane++) {
169 adc_write(core, ADC_REG_DELAY_CNTRL, 0);
170
171 adc_write(core, ADC_REG_DELAY_CNTRL,
172 ADC_DELAY_ADDRESS(lane)
173 | ADC_DELAY_WDATA(val)
174 | ADC_DELAY_SEL);
175 }
176 }
177
178 adc_write(core, ADC_REG_CHAN_STATUS(0), ~0);
179
180 mdelay(1);
181
182 adc_read(core, ADC_REG_CHAN_STATUS(0), &stat);
183
184 err_field[val + (inv_range * (max_val + 1))] =
185 ! !(stat & (ADC_PN_ERR | ADC_PN_OOS));
186 }
187
188 for (val = 0, cnt = 0, max_cnt = 0, start = -1, max_start = 0;
189 val <= (max_val + (inv_range * (max_val + 1))); val++) {
190 if (err_field[val] == 0) {
191 if (start == -1)
192 start = val;
193 cnt++;
194 } else {
195 if (cnt > max_cnt) {
196 max_cnt = cnt;
197 max_start = start;
198 }
199 start = -1;
200 cnt = 0;
201 }
202 }
203
204 if (cnt > max_cnt) {
205 max_cnt = cnt;
206 max_start = start;
207 }
208
209 if ((inv_range == 0) && ((max_cnt < 3) || (err_field[max_val] == 0))) {
210 do_inv = 1;
211 inv_range = 1;
212 } else {
213 do_inv = 0;
214 }
215
216 } while (do_inv);
217
218 val = max_start + (max_cnt / 2);
219
220 #ifdef DCO_DEBUG
221 for (cnt = 0; cnt <= (max_val + (inv_range * (max_val + 1))); cnt++) {
222 if (cnt == val)
223 printf("|");
224 else
225 printf("%c", err_field[cnt] ? '-' : 'o');
226 if (cnt == max_val)
227 printf("\n");
228 }
229 #endif
230 if (val > max_val) {
231 val -= max_val + 1;
232 if (!dco) {
233 adc_read(core, ADC_REG_CNTRL, &reg_cntrl);
234 reg_cntrl |= ADC_DDR_EDGESEL;
235 adc_write(core, ADC_REG_CNTRL, reg_cntrl);
236 }
237 cnt = 1;
238 } else {
239 if (dco) {
240 ad9265_spi_write(dev, AD9265_REG_OUTPUT_PHASE,
241 OUTPUT_EVEN_ODD_MODE_EN);
242 } else {
243 adc_read(core, ADC_REG_CNTRL, &reg_cntrl);
244 reg_cntrl &= ~ADC_DDR_EDGESEL;
245 adc_write(core, ADC_REG_CNTRL, reg_cntrl);
246 }
247 cnt = 0;
248 }
249
250 #ifdef DCO_DEBUG
251 if (dco)
252 printf(" %s DCO 0x%X\n", cnt ? "INVERT" : "",
253 val > 0 ? (uint16_t)((val - 1) | dco_en) : 0);
254 else
255 printf(" %s IDELAY 0x%x\n", cnt ? "INVERT" : "", (uint16_t)val);
256 #endif
257
258 ad9265_testmode_set(dev, 0, TESTMODE_OFF);
259 ad9265_testmode_set(dev, 1, TESTMODE_OFF);
260 if (dco) {
261 ad9265_spi_write(dev, AD9265_REG_OUTPUT_DELAY,
262 val > 0 ? ((val - 1) | dco_en) : 0);
263 ad9265_spi_write(dev, AD9265_REG_TRANSFER, TRANSFER_SYNC);
264 } else {
265 for (lane = 0; lane < nb_lanes; lane++) {
266 adc_write(core, ADC_REG_DELAY_CNTRL, 0);
267
268 adc_write(core, ADC_REG_DELAY_CNTRL,
269 ADC_DELAY_ADDRESS(lane)
270 | ADC_DELAY_WDATA(val)
271 | ADC_DELAY_SEL);
272 }
273 }
274
275 adc_write(core, ADC_REG_CHAN_CNTRL(0), chan_ctrl0);
276
277 ret = ad9265_outputmode_set(dev, dev->output_mode);
278 if (ret < 0)
279 return ret;
280
281 return 0;
282 }
283
284 /***************************************************************************//**
285 * @brief ad9265_setup
286 *******************************************************************************/
287 int32_t ad9265_setup(ad9265_dev **device,
288 ad9265_init_param init_param,
289 adc_core core)
290 {
291 ad9265_dev *dev;
292 uint8_t chip_id;
293 int32_t ret;
294
295 dev = (ad9265_dev *)malloc(sizeof(*dev));
296 if (!dev) {
297 return -1;
298 }
299
300 dev->spi_dev.chip_select = init_param.spi_chip_select;
301 dev->spi_dev.mode = init_param.spi_mode;
302 dev->spi_dev.device_id = init_param.spi_device_id;
303 dev->spi_dev.type = init_param.spi_type;
304 ret = spi_init(&dev->spi_dev);
305
306 ad9265_spi_read(dev, AD9265_REG_CHIP_ID, &chip_id);
307 if(chip_id != AD9265_CHIP_ID)
308 {
309 printf("Error: Invalid CHIP ID (0x%x).\n", chip_id);
310 return -1;
311 }
312
313 dev->output_mode = AD9265_DEF_OUTPUT_MODE | OUTPUT_MODE_TWOS_COMPLEMENT;
314 ad9265_outputmode_set(dev, dev->output_mode);
315
316 ad9265_calibrate(dev, 1, 0, 0, core);
317
318 *device = dev;
319
320 printf("AD9265 successfully initialized.\n");
321
322 return ret;
323 }