Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad9625.c
3 * @brief Implementation of AD9625 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2014(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 "ad9625.h"
48
49 /******************************************************************************/
50 /************************ Variables Definitions *******************************/
51 /******************************************************************************/
52 uint8_t spi_is_initialized = 0;
53
54 /***************************************************************************//**
55 * @brief ad9625_spi_read
56 *******************************************************************************/
57 int32_t ad9625_spi_read(ad9625_dev *dev,
58 uint16_t reg_addr,
59 uint8_t *reg_data)
60 {
61 uint8_t buf[3];
62 int32_t ret;
63
64 buf[0] = 0x80 | (reg_addr >> 8);
65 buf[1] = reg_addr & 0xFF;
66 buf[2] = 0x00;
67
68 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
69 *reg_data = buf[2];
70
71 return ret;
72 }
73
74 /***************************************************************************//**
75 * @brief ad9625_spi_write
76 *******************************************************************************/
77 int32_t ad9625_spi_write(ad9625_dev *dev,
78 uint16_t reg_addr,
79 uint8_t reg_data)
80 {
81 uint8_t buf[3];
82 int32_t ret;
83
84 buf[0] = reg_addr >> 8;
85 buf[1] = reg_addr & 0xFF;
86 buf[2] = reg_data;
87
88 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
89
90 return ret;
91 }
92
93 /***************************************************************************//**
94 * @brief ad9625_setup
95 *******************************************************************************/
96 int32_t ad9625_setup(ad9625_dev **device,
97 ad9625_init_param init_param)
98 {
99 ad9625_dev *dev;
100 uint8_t chip_id;
101 uint8_t pll_stat;
102 int32_t ret;
103
104 if (!spi_is_initialized) {
105 dev = (ad9625_dev *)malloc(sizeof(*dev));
106 if (!dev) {
107 return -1;
108 }
109
110 dev->spi_dev.chip_select = init_param.spi_chip_select;
111 dev->spi_dev.mode = init_param.spi_mode;
112 dev->spi_dev.device_id = init_param.spi_device_id;
113 dev->spi_dev.type = init_param.spi_type;
114 ret = spi_init(&dev->spi_dev);
115 spi_is_initialized = 1;
116 }
117
118 ad9625_spi_write(dev, AD9625_REG_CHIP_PORT_CONF, 0x18);
119 ad9625_spi_write(dev, AD9625_REG_TRANSFER, 0x01);
120 mdelay(10);
121
122 ad9625_spi_write(dev, AD9625_REG_POWER_MODE, 0x00);
123 ad9625_spi_write(dev, AD9625_REG_TRANSFER, 0x01);
124 ad9625_spi_write(dev, AD9625_REG_JESD204B_LINK_CNTRL_1, 0x15);
125 ad9625_spi_write(dev, AD9625_REG_JESD204B_LANE_POWER_MODE, 0x00);
126 ad9625_spi_write(dev, AD9625_REG_DIVCLK_OUT_CNTRL, 0x11);
127 ad9625_spi_write(dev, AD9625_REG_TEST_CNTRL, 0x00);
128 ad9625_spi_write(dev, AD9625_REG_OUTPUT_MODE, 0x00);
129 ad9625_spi_write(dev, AD9625_REG_OUTPUT_ADJUST, 0x10);
130 ad9625_spi_write(dev, AD9625_REG_JESD204B_LINK_CNTRL_1, 0x14);
131 ad9625_spi_write(dev, AD9625_REG_TRANSFER, 0x01);
132 mdelay(10);
133
134 ad9625_spi_read(dev, AD9625_REG_CHIP_ID, &chip_id);
135 if(chip_id != AD9625_CHIP_ID)
136 {
137 xil_printf("Error: Invalid CHIP ID (0x%x).\n", chip_id);
138 return -1;
139 }
140
141 ad9625_spi_read(dev, AD9625_REG_PLL_STATUS, &pll_stat);
142 if((pll_stat & 0x80) != 0x80)
143 {
144 xil_printf("Error: AD9625 PLL is NOT locked (0x%x).\n", chip_id);
145 return -1;
146 }
147
148 *device = dev;
149
150 xil_printf("AD9625 successfully initialized.\n");
151
152 return ret;
153 }