Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad5766.c
3 * @brief Implementation of AD5766 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2016(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 "platform_drivers.h"
47 #include "ad5766.h"
48
49 /******************************************************************************/
50 /************************** Functions Implementation **************************/
51 /******************************************************************************/
52 /**
53 * SPI command write to device.
54 * @param dev - The device structure.
55 * @param cmd - The command.
56 * @param data - The data.
57 * @return SUCCESS in case of success, negative error code otherwise.
58 */
59 int32_t ad5766_spi_cmd_write(ad5766_dev *dev,
60 uint8_t cmd,
61 uint16_t data)
62 {
63 uint8_t buf[3];
64 int32_t ret;
65
66 buf[0] = cmd;
67 buf[1] = (data & 0xFF00) >> 8;
68 buf[2] = (data & 0x00FF) >> 0;
69
70 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
71
72 return ret;
73 }
74
75 /**
76 * SPI readback register from device.
77 * @param dev - The device structure.
78 * @param dac - The dac address.
79 * @param data - The register data.
80 * @return SUCCESS in case of success, negative error code otherwise.
81 */
82 int32_t ad5766_spi_readback_reg(ad5766_dev *dev,
83 ad5766_dac dac,
84 uint32_t *data)
85 {
86 uint8_t buf[3] = {0, 0, 0};
87 int32_t ret;
88
89 if (dev->daisy_chain_en == AD5766_ENABLE) {
90 printf("%s: This feature is not available in Daisy-Chain mode.\n",
91 __func__);
92 return FAILURE;
93 }
94
95 ad5766_spi_cmd_write(dev, AD5766_CMD_READBACK_REG(dac), 0x0000);
96
97 ret = spi_write_and_read(&dev->spi_dev, buf, 3);
98
99 *data = (buf[0] << 16) | ((buf[1] << 8)) | (buf[2] << 0);
100
101 return ret;
102 }
103
104 /**
105 * Set software LDAC for the selected channels.
106 * @param dev - The device structure.
107 * @param setting - The setting.
108 * Accepted values: AD5766_LDAC(x) | AD5766_LDAC(y) | ...
109 * @return SUCCESS in case of success, negative error code otherwise.
110 */
111 int32_t ad5766_set_sw_ldac(ad5766_dev *dev,
112 uint16_t setting)
113 {
114 return ad5766_spi_cmd_write(dev, AD5766_CMD_SW_LDAC,
115 setting);
116 }
117
118 /**
119 * Set clear code and span settings.
120 * @param dev - The device structure.
121 * Accepted values:
122 * @param clr - The clear code setting.
123 * Accepted values: AD5766_ZERO
124 * AD5766_MID
125 * AD5766_FULL
126 * @param span - The span setting.
127 * Accepted values: AD5766_M_20V_TO_0V
128 * AD5766_M_16V_TO_0V
129 * AD5766_M_10V_TO_0V
130 * AD5766_M_12V_TO_P_14V
131 * AD5766_M_16V_TO_P_10V
132 * AD5766_M_5V_TO_P_6V
133 * AD5766_M_10V_TO_P_10V
134 * @return SUCCESS in case of success, negative error code otherwise.
135 */
136 int32_t ad5766_set_clr_span(ad5766_dev *dev,
137 ad5766_clr clr,
138 ad5766_span span)
139 {
140 return ad5766_spi_cmd_write(dev, AD5766_CMD_SPAN_REG,
141 AD5766_CFG_CLR(clr) | AD5766_SPAN(span));
142 }
143
144 /**
145 * Power down the selected channels.
146 * @param dev - The device structure.
147 * @param setting - The setting.
148 * Accepted values: AD5766_PWDN(x) | AD5766_PWDN(y) | ...
149 * @return SUCCESS in case of success, negative error code otherwise.
150 */
151 int32_t ad5766_set_pwr_dac(ad5766_dev *dev,
152 uint16_t setting)
153 {
154 return ad5766_spi_cmd_write(dev, AD5766_CMD_WR_PWR_DAC,
155 setting);
156 }
157
158 /**
159 * Power down the dither block for the selected channels.
160 * @param dev - The device structure.
161 * @param setting - The setting.
162 * Accepted values: AD5766_PWDN(x) | AD5766_PWDN(y) | ...
163 * @return SUCCESS in case of success, negative error code otherwise.
164 */
165 int32_t ad5766_set_pwr_dither(ad5766_dev *dev,
166 uint16_t setting)
167 {
168 return ad5766_spi_cmd_write(dev, AD5766_CMD_WR_PWR_DITHER,
169 setting);
170 }
171
172 /**
173 * Enable the dither signal for the selected channels.
174 * @param dev - The device structure.
175 * @param setting - The setting.
176 * Accepted values: AD5766_N0(x) | AD5766_N1(y) | ...
177 * @return SUCCESS in case of success, negative error code otherwise.
178 */
179 int32_t ad5766_set_dither_signal(ad5766_dev *dev,
180 uint32_t setting)
181 {
182 int32_t ret;
183
184 ret = ad5766_spi_cmd_write(dev, AD5766_CMD_DITHER_SIG_1,
185 (setting & 0xFFFF) >> 0);
186 ret |= ad5766_spi_cmd_write(dev, AD5766_CMD_DITHER_SIG_2,
187 (setting & 0xFFFF0000) >> 16);
188
189 return ret;
190 }
191
192 /**
193 * Invert the dither signal for the selected channels.
194 * @param dev - The device structure.
195 * @param setting - The setting.
196 * Accepted values: AD5766_INV_D(x) | AD5766_INV_D(y) | ...
197 * @return SUCCESS in case of success, negative error code otherwise.
198 */
199 int32_t ad5766_set_inv_dither(ad5766_dev *dev,
200 uint16_t setting)
201 {
202 return ad5766_spi_cmd_write(dev, AD5766_CMD_INV_DITHER,
203 setting);
204 }
205
206 /**
207 * Enable the dither scaling for the selected channels.
208 * @param dev - The device structure.
209 * @param setting - The setting.
210 * Accepted values: AD5766_75(x) | AD5766_50(y) |
211 * AD5766_25(z) | ...
212 * @return SUCCESS in case of success, negative error code otherwise.
213 */
214 int32_t ad5766_set_dither_scale(ad5766_dev *dev,
215 uint32_t setting)
216 {
217 int32_t ret;
218
219 ret = ad5766_spi_cmd_write(dev, AD5766_CMD_DITHER_SCALE_1,
220 (setting & 0xFFFF) >> 0);
221 ret |= ad5766_spi_cmd_write(dev, AD5766_CMD_DITHER_SCALE_2,
222 (setting & 0xFFFF0000) >> 16);
223
224 return ret;
225 }
226
227 /**
228 * Do a software reset.
229 * @param dev - The device structure.
230 * @return SUCCESS in case of success, negative error code otherwise.
231 */
232 int32_t ad5766_do_soft_reset(ad5766_dev *dev)
233 {
234 return ad5766_spi_cmd_write(dev, AD5766_CMD_SW_FULL_RESET,
235 AD5766_RESET);
236 }
237
238 /**
239 * Set the input register for the selected channel.
240 * @param dev - The device structure.
241 * @param dac - The selected channel.
242 * @param data - The register data.
243 * @return SUCCESS in case of success, negative error code otherwise.
244 */
245 int32_t ad5766_set_in_reg(ad5766_dev *dev,
246 ad5766_dac dac,
247 uint16_t data)
248 {
249 return ad5766_spi_cmd_write(dev, AD5766_CMD_WR_IN_REG(dac),
250 data);
251 }
252
253 /**
254 * Set the DAC register for the selected channel.
255 * @param dev - The device structure.
256 * @param dac - The selected channel.
257 * @param data - The register data.
258 * @return SUCCESS in case of success, negative error code otherwise.
259 */
260 int32_t ad5766_set_dac_reg(ad5766_dev *dev,
261 ad5766_dac dac,
262 uint16_t data)
263 {
264 return ad5766_spi_cmd_write(dev, AD5766_CMD_WR_DAC_REG(dac),
265 data);
266 }
267
268 /**
269 * Set the DAC register for all channels.
270 * @param dev - The device structure.
271 * @param data - The register data.
272 * @return SUCCESS in case of success, negative error code otherwise.
273 */
274 int32_t ad5766_set_dac_reg_all(ad5766_dev *dev,
275 ad5766_dac dac,
276 uint16_t data)
277 {
278 return ad5766_spi_cmd_write(dev, AD5766_CMD_WR_DAC_REG_ALL,
279 data);
280 }
281
282 /**
283 * Initialize the device.
284 * @param device - The device structure.
285 * @param init_param - The structure that contains the device initial
286 * parameters.
287 * @return SUCCESS in case of success, negative error code otherwise.
288 */
289 int32_t ad5766_setup(ad5766_dev **device,
290 ad5766_init_param init_param)
291 {
292 ad5766_dev *dev;
293 int32_t ret;
294
295 dev = (ad5766_dev *)malloc(sizeof(*dev));
296 if (!dev) {
297 return -1;
298 }
299
300 /* SPI */
301 dev->spi_dev.chip_select = init_param.spi_chip_select;
302 dev->spi_dev.mode = init_param.spi_mode;
303 dev->spi_dev.device_id = init_param.spi_device_id;
304 dev->spi_dev.type = init_param.spi_type;
305 ret = spi_init(&dev->spi_dev);
306
307 /* GPIO */
308 dev->gpio_dev.device_id = init_param.gpio_device_id;
309 dev->gpio_dev.type = init_param.gpio_type;
310 ret |= gpio_init(&dev->gpio_dev);
311
312 dev->gpio_reset = init_param.gpio_reset;
313 ret |= gpio_set_direction(&dev->gpio_dev, dev->gpio_reset, GPIO_OUT);
314 ret |= gpio_set_value(&dev->gpio_dev, dev->gpio_reset, GPIO_LOW);
315 mdelay(10);
316 ret |= gpio_set_value(&dev->gpio_dev, dev->gpio_reset, GPIO_HIGH);
317 mdelay(10);
318
319 /* Device Settings */
320 dev->daisy_chain_en = init_param.daisy_chain_en;
321 ret |= ad5766_spi_cmd_write(dev, AD5766_CMD_SDO_CNTRL,
322 dev->daisy_chain_en ? AD5766_SDO_EN : 0);
323
324 ret |= ad5766_set_clr_span(dev, init_param.clr, init_param.span);
325 ret |= ad5766_set_pwr_dac(dev, init_param.pwr_dac_setting);
326 ret |= ad5766_set_pwr_dither(dev, init_param.pwr_dither_setting);
327 ret |= ad5766_set_dither_signal(dev, init_param.dither_signal_setting);
328 ret |= ad5766_set_inv_dither(dev, init_param.inv_dither_setting);
329 ret |= ad5766_set_dither_scale(dev, init_param.dither_scale_setting);
330
331 *device = dev;
332
333 if (!ret)
334 printf("AD5766 successfully initialized\n");
335 else
336 printf("AD5766 initialization error (%d)\n", ret);
337
338 return ret;
339 }