this is testing

Committer:
pmallick
Date:
Thu Jan 14 18:54:16 2021 +0530
Revision:
0:3afcd581558d
this is testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmallick 0:3afcd581558d 1 /***************************************************************************//**
pmallick 0:3afcd581558d 2 * @file ad713x.c
pmallick 0:3afcd581558d 3 * @brief Implementation of ad713x Driver.
pmallick 0:3afcd581558d 4 * @author SPopa (stefan.popa@analog.com)
pmallick 0:3afcd581558d 5 * @author Andrei Drimbarean (andrei.drimbarean@analog.com)
pmallick 0:3afcd581558d 6 ********************************************************************************
pmallick 0:3afcd581558d 7 * Copyright 2020(c) Analog Devices, Inc.
pmallick 0:3afcd581558d 8 *
pmallick 0:3afcd581558d 9 * All rights reserved.
pmallick 0:3afcd581558d 10 *
pmallick 0:3afcd581558d 11 * Redistribution and use in source and binary forms, with or without
pmallick 0:3afcd581558d 12 * modification, are permitted provided that the following conditions are met:
pmallick 0:3afcd581558d 13 * - Redistributions of source code must retain the above copyright
pmallick 0:3afcd581558d 14 * notice, this list of conditions and the following disclaimer.
pmallick 0:3afcd581558d 15 * - Redistributions in binary form must reproduce the above copyright
pmallick 0:3afcd581558d 16 * notice, this list of conditions and the following disclaimer in
pmallick 0:3afcd581558d 17 * the documentation and/or other materials provided with the
pmallick 0:3afcd581558d 18 * distribution.
pmallick 0:3afcd581558d 19 * - Neither the name of Analog Devices, Inc. nor the names of its
pmallick 0:3afcd581558d 20 * contributors may be used to endorse or promote products derived
pmallick 0:3afcd581558d 21 * from this software without specific prior written permission.
pmallick 0:3afcd581558d 22 * - The use of this software may or may not infringe the patent rights
pmallick 0:3afcd581558d 23 * of one or more patent holders. This license does not release you
pmallick 0:3afcd581558d 24 * from the requirement that you obtain separate licenses from these
pmallick 0:3afcd581558d 25 * patent holders to use this software.
pmallick 0:3afcd581558d 26 * - Use of the software either in source or binary form, must be run
pmallick 0:3afcd581558d 27 * on or directly connected to an Analog Devices Inc. component.
pmallick 0:3afcd581558d 28 *
pmallick 0:3afcd581558d 29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
pmallick 0:3afcd581558d 30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
pmallick 0:3afcd581558d 31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
pmallick 0:3afcd581558d 32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
pmallick 0:3afcd581558d 33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
pmallick 0:3afcd581558d 34 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
pmallick 0:3afcd581558d 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
pmallick 0:3afcd581558d 36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
pmallick 0:3afcd581558d 37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
pmallick 0:3afcd581558d 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pmallick 0:3afcd581558d 39 *******************************************************************************/
pmallick 0:3afcd581558d 40
pmallick 0:3afcd581558d 41 /******************************************************************************/
pmallick 0:3afcd581558d 42 /***************************** Include Files **********************************/
pmallick 0:3afcd581558d 43 /******************************************************************************/
pmallick 0:3afcd581558d 44
pmallick 0:3afcd581558d 45 #include <stdlib.h>
pmallick 0:3afcd581558d 46 #include "ad713x.h"
pmallick 0:3afcd581558d 47
pmallick 0:3afcd581558d 48 /******************************************************************************/
pmallick 0:3afcd581558d 49 /***************************** Variable definition ****************************/
pmallick 0:3afcd581558d 50 /******************************************************************************/
pmallick 0:3afcd581558d 51
pmallick 0:3afcd581558d 52 static const int ad713x_output_data_frame[3][9][2] = {
pmallick 0:3afcd581558d 53 {
pmallick 0:3afcd581558d 54 {ADC_16_BIT_DATA, CRC_6},
pmallick 0:3afcd581558d 55 {ADC_24_BIT_DATA, CRC_6},
pmallick 0:3afcd581558d 56 {ADC_32_BIT_DATA, NO_CRC},
pmallick 0:3afcd581558d 57 {ADC_32_BIT_DATA, CRC_6},
pmallick 0:3afcd581558d 58 {ADC_16_BIT_DATA, NO_CRC},
pmallick 0:3afcd581558d 59 {ADC_24_BIT_DATA, NO_CRC},
pmallick 0:3afcd581558d 60 {ADC_24_BIT_DATA, CRC_8},
pmallick 0:3afcd581558d 61 {ADC_32_BIT_DATA, CRC_8},
pmallick 0:3afcd581558d 62 {INVALID}
pmallick 0:3afcd581558d 63 },
pmallick 0:3afcd581558d 64 {
pmallick 0:3afcd581558d 65 {ADC_16_BIT_DATA, NO_CRC},
pmallick 0:3afcd581558d 66 {ADC_16_BIT_DATA, CRC_6},
pmallick 0:3afcd581558d 67 {ADC_24_BIT_DATA, NO_CRC},
pmallick 0:3afcd581558d 68 {ADC_24_BIT_DATA, CRC_6},
pmallick 0:3afcd581558d 69 {ADC_16_BIT_DATA, CRC_8},
pmallick 0:3afcd581558d 70 {ADC_24_BIT_DATA, CRC_8},
pmallick 0:3afcd581558d 71 {INVALID}
pmallick 0:3afcd581558d 72 },
pmallick 0:3afcd581558d 73 {
pmallick 0:3afcd581558d 74 {ADC_16_BIT_DATA, NO_CRC},
pmallick 0:3afcd581558d 75 {ADC_16_BIT_DATA, CRC_6},
pmallick 0:3afcd581558d 76 {ADC_16_BIT_DATA, CRC_8},
pmallick 0:3afcd581558d 77 {INVALID}
pmallick 0:3afcd581558d 78 },
pmallick 0:3afcd581558d 79 };
pmallick 0:3afcd581558d 80
pmallick 0:3afcd581558d 81 /******************************************************************************/
pmallick 0:3afcd581558d 82 /************************** Functions Implementation **************************/
pmallick 0:3afcd581558d 83 /******************************************************************************/
pmallick 0:3afcd581558d 84
pmallick 0:3afcd581558d 85 /**
pmallick 0:3afcd581558d 86 * @brief Read from device.
pmallick 0:3afcd581558d 87 * @param dev - The device structure.
pmallick 0:3afcd581558d 88 * @param reg_addr - The register address.
pmallick 0:3afcd581558d 89 * @param reg_data - The register data.
pmallick 0:3afcd581558d 90 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 91 */
pmallick 0:3afcd581558d 92 int32_t ad713x_spi_reg_read(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 93 uint8_t reg_addr,
pmallick 0:3afcd581558d 94 uint8_t *reg_data)
pmallick 0:3afcd581558d 95 {
pmallick 0:3afcd581558d 96 int32_t ret;
pmallick 0:3afcd581558d 97 uint8_t buf[2];
pmallick 0:3afcd581558d 98
pmallick 0:3afcd581558d 99 buf[0] = AD713X_REG_READ(reg_addr);
pmallick 0:3afcd581558d 100 buf[1] = 0x00;
pmallick 0:3afcd581558d 101
pmallick 0:3afcd581558d 102 ret = spi_write_and_read(dev->spi_desc, buf, 2);
pmallick 0:3afcd581558d 103 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 104 return FAILURE;
pmallick 0:3afcd581558d 105 *reg_data = buf[1];
pmallick 0:3afcd581558d 106
pmallick 0:3afcd581558d 107 return SUCCESS;
pmallick 0:3afcd581558d 108 }
pmallick 0:3afcd581558d 109
pmallick 0:3afcd581558d 110 /**
pmallick 0:3afcd581558d 111 * @brief Write to device.
pmallick 0:3afcd581558d 112 * @param dev - The device structure.
pmallick 0:3afcd581558d 113 * @param reg_addr - The register address.
pmallick 0:3afcd581558d 114 * @param reg_data - The register data.
pmallick 0:3afcd581558d 115 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 116 */
pmallick 0:3afcd581558d 117 int32_t ad713x_spi_reg_write(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 118 uint8_t reg_addr,
pmallick 0:3afcd581558d 119 uint8_t reg_data)
pmallick 0:3afcd581558d 120 {
pmallick 0:3afcd581558d 121 uint8_t buf[2];
pmallick 0:3afcd581558d 122
pmallick 0:3afcd581558d 123 buf[0] = reg_addr;
pmallick 0:3afcd581558d 124 buf[1] = reg_data;
pmallick 0:3afcd581558d 125
pmallick 0:3afcd581558d 126 return spi_write_and_read(dev->spi_desc, buf, 2);
pmallick 0:3afcd581558d 127 }
pmallick 0:3afcd581558d 128
pmallick 0:3afcd581558d 129 /**
pmallick 0:3afcd581558d 130 * @brief SPI write to device using a mask.
pmallick 0:3afcd581558d 131 * @param dev - The device structure.
pmallick 0:3afcd581558d 132 * @param reg_addr - The register address.
pmallick 0:3afcd581558d 133 * @param mask - The mask.
pmallick 0:3afcd581558d 134 * @param data - The register data.
pmallick 0:3afcd581558d 135 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 136 */
pmallick 0:3afcd581558d 137 int32_t ad713x_spi_write_mask(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 138 uint8_t reg_addr,
pmallick 0:3afcd581558d 139 uint32_t mask,
pmallick 0:3afcd581558d 140 uint8_t data)
pmallick 0:3afcd581558d 141 {
pmallick 0:3afcd581558d 142 uint8_t reg_data;
pmallick 0:3afcd581558d 143 int32_t ret;
pmallick 0:3afcd581558d 144
pmallick 0:3afcd581558d 145 ret = ad713x_spi_reg_read(dev, reg_addr, &reg_data);
pmallick 0:3afcd581558d 146 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 147 return FAILURE;
pmallick 0:3afcd581558d 148 reg_data &= ~mask;
pmallick 0:3afcd581558d 149 reg_data |= data;
pmallick 0:3afcd581558d 150
pmallick 0:3afcd581558d 151 return ad713x_spi_reg_write(dev, reg_addr, reg_data);
pmallick 0:3afcd581558d 152 }
pmallick 0:3afcd581558d 153
pmallick 0:3afcd581558d 154 /**
pmallick 0:3afcd581558d 155 * @brief Device power mode control.
pmallick 0:3afcd581558d 156 * @param dev - The device structure.
pmallick 0:3afcd581558d 157 * @param mode - Type of power mode
pmallick 0:3afcd581558d 158 * Accepted values: LOW_POWER
pmallick 0:3afcd581558d 159 * HIGH_POWER
pmallick 0:3afcd581558d 160 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 161 */
pmallick 0:3afcd581558d 162 int32_t ad713x_set_power_mode(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 163 enum ad713x_power_mode mode)
pmallick 0:3afcd581558d 164 {
pmallick 0:3afcd581558d 165 if (mode == LOW_POWER)
pmallick 0:3afcd581558d 166 return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG,
pmallick 0:3afcd581558d 167 AD713X_DEV_CONFIG_PWR_MODE_MSK, 0);
pmallick 0:3afcd581558d 168 else if (mode == HIGH_POWER)
pmallick 0:3afcd581558d 169 return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG,
pmallick 0:3afcd581558d 170 AD713X_DEV_CONFIG_PWR_MODE_MSK,
pmallick 0:3afcd581558d 171 1);
pmallick 0:3afcd581558d 172
pmallick 0:3afcd581558d 173 return FAILURE;
pmallick 0:3afcd581558d 174 }
pmallick 0:3afcd581558d 175
pmallick 0:3afcd581558d 176 /**
pmallick 0:3afcd581558d 177 * @brief ADC conversion data output frame control.
pmallick 0:3afcd581558d 178 * @param dev - The device structure.
pmallick 0:3afcd581558d 179 * @param adc_data_len - Data conversion length
pmallick 0:3afcd581558d 180 * Accepted values: ADC_16_BIT_DATA
pmallick 0:3afcd581558d 181 * ADC_24_BIT_DATA
pmallick 0:3afcd581558d 182 * ADC_32_BIT_DATA
pmallick 0:3afcd581558d 183 * @param crc_header - CRC header
pmallick 0:3afcd581558d 184 * Accepted values: NO_CRC
pmallick 0:3afcd581558d 185 * CRC_6
pmallick 0:3afcd581558d 186 * CRC_8
pmallick 0:3afcd581558d 187 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 188 */
pmallick 0:3afcd581558d 189 int32_t ad713x_set_out_data_frame(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 190 enum ad713x_adc_data_len adc_data_len,
pmallick 0:3afcd581558d 191 enum ad713x_crc_header crc_header)
pmallick 0:3afcd581558d 192 {
pmallick 0:3afcd581558d 193 uint8_t id;
pmallick 0:3afcd581558d 194 uint8_t i = 0;
pmallick 0:3afcd581558d 195
pmallick 0:3afcd581558d 196 id = dev->dev_id;
pmallick 0:3afcd581558d 197
pmallick 0:3afcd581558d 198 while (ad713x_output_data_frame[id][i][0] != INVALID) {
pmallick 0:3afcd581558d 199 if((adc_data_len == ad713x_output_data_frame[id][i][0]) &&
pmallick 0:3afcd581558d 200 (crc_header == ad713x_output_data_frame[id][i][1])) {
pmallick 0:3afcd581558d 201 return ad713x_spi_write_mask(dev,
pmallick 0:3afcd581558d 202 AD713X_REG_DATA_PACKET_CONFIG,
pmallick 0:3afcd581558d 203 AD713X_DATA_PACKET_CONFIG_FRAME_MSK,
pmallick 0:3afcd581558d 204 AD713X_DATA_PACKET_CONFIG_FRAME_MODE(i));
pmallick 0:3afcd581558d 205 }
pmallick 0:3afcd581558d 206 i++;
pmallick 0:3afcd581558d 207 }
pmallick 0:3afcd581558d 208
pmallick 0:3afcd581558d 209 return FAILURE;
pmallick 0:3afcd581558d 210 }
pmallick 0:3afcd581558d 211
pmallick 0:3afcd581558d 212 /**
pmallick 0:3afcd581558d 213 * @brief DOUTx output format configuration.
pmallick 0:3afcd581558d 214 * @param dev - The device structure.
pmallick 0:3afcd581558d 215 * @param format - Single channel daisy chain mode. Dual channel daisy chain mode.
pmallick 0:3afcd581558d 216 * Quad channel parallel output mode. Channel data averaging mode.
pmallick 0:3afcd581558d 217 * Accepted values: SINGLE_CH_DC
pmallick 0:3afcd581558d 218 * DUAL_CH_DC
pmallick 0:3afcd581558d 219 * QUAD_CH_PO
pmallick 0:3afcd581558d 220 * CH_AVG_MODE
pmallick 0:3afcd581558d 221 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 222 */
pmallick 0:3afcd581558d 223 int32_t ad713x_dout_format_config(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 224 enum ad713x_doutx_format format)
pmallick 0:3afcd581558d 225 {
pmallick 0:3afcd581558d 226 return ad713x_spi_write_mask(dev, AD713X_REG_DIGITAL_INTERFACE_CONFIG,
pmallick 0:3afcd581558d 227 AD713X_DIG_INT_CONFIG_FORMAT_MSK,
pmallick 0:3afcd581558d 228 AD713X_DIG_INT_CONFIG_FORMAT_MODE(format));
pmallick 0:3afcd581558d 229 }
pmallick 0:3afcd581558d 230
pmallick 0:3afcd581558d 231 /**
pmallick 0:3afcd581558d 232 * @brief Magnitude and phase matching calibration clock delay enable for all
pmallick 0:3afcd581558d 233 * channels at 2 clock delay.
pmallick 0:3afcd581558d 234 * This function is kept for backwards compatibility with the current
pmallick 0:3afcd581558d 235 * application source, but it is deprecated. Use
pmallick 0:3afcd581558d 236 * ad713x_mag_phase_clk_delay_chan().
pmallick 0:3afcd581558d 237 * @param dev - The device structure.
pmallick 0:3afcd581558d 238 * @param clk_delay_en - Enable or disable Mag/Phase clock delay.
pmallick 0:3afcd581558d 239 * Accepted values: true
pmallick 0:3afcd581558d 240 * false
pmallick 0:3afcd581558d 241 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 242 */
pmallick 0:3afcd581558d 243 int32_t ad713x_mag_phase_clk_delay(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 244 bool clk_delay_en)
pmallick 0:3afcd581558d 245 {
pmallick 0:3afcd581558d 246 int32_t ret;
pmallick 0:3afcd581558d 247 int8_t i;
pmallick 0:3afcd581558d 248 int8_t temp_clk_delay;
pmallick 0:3afcd581558d 249
pmallick 0:3afcd581558d 250 if (clk_delay_en)
pmallick 0:3afcd581558d 251 temp_clk_delay = DELAY_2_CLOCKS;
pmallick 0:3afcd581558d 252 else
pmallick 0:3afcd581558d 253 temp_clk_delay = DELAY_NONE;
pmallick 0:3afcd581558d 254
pmallick 0:3afcd581558d 255 for (i = CH3; i >= 0; i--) {
pmallick 0:3afcd581558d 256 ret = ad713x_spi_write_mask(dev, AD713X_REG_MPC_CONFIG,
pmallick 0:3afcd581558d 257 AD713X_MPC_CLKDEL_EN_CH_MSK(i),
pmallick 0:3afcd581558d 258 AD713X_MPC_CLKDEL_EN_CH_MODE(temp_clk_delay, i));
pmallick 0:3afcd581558d 259 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 260 return FAILURE;
pmallick 0:3afcd581558d 261 }
pmallick 0:3afcd581558d 262
pmallick 0:3afcd581558d 263 return SUCCESS;
pmallick 0:3afcd581558d 264 }
pmallick 0:3afcd581558d 265
pmallick 0:3afcd581558d 266 /**
pmallick 0:3afcd581558d 267 * @brief Change magnitude and phase calibration clock delay mode for a specific
pmallick 0:3afcd581558d 268 * channel.
pmallick 0:3afcd581558d 269 * @param dev - The device structure.
pmallick 0:3afcd581558d 270 * @param chan - ID of the channel to be changed.
pmallick 0:3afcd581558d 271 * Accepted values: CH0, CH1, CH2, CH3
pmallick 0:3afcd581558d 272 * @param mode - Delay in clock periods.
pmallick 0:3afcd581558d 273 * Accepted values: DELAY_NONE,
pmallick 0:3afcd581558d 274 * DELAY_1_CLOCKS,
pmallick 0:3afcd581558d 275 * DELAY_2_CLOCKS
pmallick 0:3afcd581558d 276 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 277 */
pmallick 0:3afcd581558d 278 int32_t ad713x_mag_phase_clk_delay_chan(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 279 enum ad713x_channels chan,
pmallick 0:3afcd581558d 280 enum ad717x_mpc_clkdel mode)
pmallick 0:3afcd581558d 281 {
pmallick 0:3afcd581558d 282 return ad713x_spi_write_mask(dev, AD713X_REG_MPC_CONFIG,
pmallick 0:3afcd581558d 283 AD713X_MPC_CLKDEL_EN_CH_MSK(chan),
pmallick 0:3afcd581558d 284 AD713X_MPC_CLKDEL_EN_CH_MODE(mode, chan));
pmallick 0:3afcd581558d 285 }
pmallick 0:3afcd581558d 286
pmallick 0:3afcd581558d 287 /**
pmallick 0:3afcd581558d 288 * @brief Digital filter type selection for each channel
pmallick 0:3afcd581558d 289 * @param dev - The device structure.
pmallick 0:3afcd581558d 290 * @param filter - Type of filter: Wideband, Sinc6, Sinc3,
pmallick 0:3afcd581558d 291 * Sinc3 filter with simultaneous 50Hz and 60Hz rejection.
pmallick 0:3afcd581558d 292 * Accepted values: FIR
pmallick 0:3afcd581558d 293 * SINC6
pmallick 0:3afcd581558d 294 * SINC3
pmallick 0:3afcd581558d 295 * SINC3_50_60_REJ
pmallick 0:3afcd581558d 296 * @param ch - Channel to apply the filter to
pmallick 0:3afcd581558d 297 * Accepted values: CH0
pmallick 0:3afcd581558d 298 * CH1
pmallick 0:3afcd581558d 299 * CH2
pmallick 0:3afcd581558d 300 * CH3
pmallick 0:3afcd581558d 301 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 302 */
pmallick 0:3afcd581558d 303 int32_t ad713x_dig_filter_sel_ch(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 304 enum ad713x_dig_filter_sel filter,
pmallick 0:3afcd581558d 305 enum ad713x_channels ch)
pmallick 0:3afcd581558d 306 {
pmallick 0:3afcd581558d 307 return ad713x_spi_write_mask(dev, AD713X_REG_CHAN_DIG_FILTER_SEL,
pmallick 0:3afcd581558d 308 AD713X_DIGFILTER_SEL_CH_MSK(ch),
pmallick 0:3afcd581558d 309 AD713X_DIGFILTER_SEL_CH_MODE(filter, ch));
pmallick 0:3afcd581558d 310 }
pmallick 0:3afcd581558d 311
pmallick 0:3afcd581558d 312 /**
pmallick 0:3afcd581558d 313 * @brief Enable/Disable CLKOUT output.
pmallick 0:3afcd581558d 314 * @param [in] dev - The device structure.
pmallick 0:3afcd581558d 315 * @param [in] enable - true to enable the clkout output;
pmallick 0:3afcd581558d 316 * false to disable the clkout output.
pmallick 0:3afcd581558d 317 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 318 */
pmallick 0:3afcd581558d 319 int32_t ad713x_clkout_output_en(struct ad713x_dev *dev, bool enable)
pmallick 0:3afcd581558d 320 {
pmallick 0:3afcd581558d 321 return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG1,
pmallick 0:3afcd581558d 322 AD713X_DEV_CONFIG1_CLKOUT_EN_MSK,
pmallick 0:3afcd581558d 323 enable ? AD713X_DEV_CONFIG1_CLKOUT_EN_MSK : 0);
pmallick 0:3afcd581558d 324 }
pmallick 0:3afcd581558d 325
pmallick 0:3afcd581558d 326 /**
pmallick 0:3afcd581558d 327 * @brief Enable/Disable reference gain correction.
pmallick 0:3afcd581558d 328 * @param [in] dev - The device structure.
pmallick 0:3afcd581558d 329 * @param [in] enable - true to enable the reference gain correction;
pmallick 0:3afcd581558d 330 * false to disable the reference gain correction.
pmallick 0:3afcd581558d 331 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 332 */
pmallick 0:3afcd581558d 333 int32_t ad713x_ref_gain_correction_en(struct ad713x_dev *dev, bool enable)
pmallick 0:3afcd581558d 334 {
pmallick 0:3afcd581558d 335 return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG1,
pmallick 0:3afcd581558d 336 AD713X_DEV_CONFIG1_REF_GAIN_CORR_EN_MSK,
pmallick 0:3afcd581558d 337 enable ? AD713X_DEV_CONFIG1_REF_GAIN_CORR_EN_MSK : 0);
pmallick 0:3afcd581558d 338 }
pmallick 0:3afcd581558d 339
pmallick 0:3afcd581558d 340 /**
pmallick 0:3afcd581558d 341 * @brief Select the wideband filter bandwidth for a channel.
pmallick 0:3afcd581558d 342 * The option is relative to ODR, so it's a fraction of it.
pmallick 0:3afcd581558d 343 * @param [in] dev - The device structure.
pmallick 0:3afcd581558d 344 * @param [in] ch - Number of the channel to which to set the wideband filter
pmallick 0:3afcd581558d 345 * option.
pmallick 0:3afcd581558d 346 * @param [in] wb_opt - Option to set the wideband filter:
pmallick 0:3afcd581558d 347 * Values are:
pmallick 0:3afcd581558d 348 * 0 - bandwidth of 0.443 * ODR;
pmallick 0:3afcd581558d 349 * 1 - bandwidth of 0.10825 * ODR.
pmallick 0:3afcd581558d 350 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 351 */
pmallick 0:3afcd581558d 352 int32_t ad713x_wideband_bw_sel(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 353 enum ad713x_channels ch, uint8_t wb_opt)
pmallick 0:3afcd581558d 354 {
pmallick 0:3afcd581558d 355 return ad713x_spi_write_mask(dev, AD713X_REG_FIR_BW_SEL,
pmallick 0:3afcd581558d 356 AD713X_FIR_BW_SEL_CH_MSK(ch),
pmallick 0:3afcd581558d 357 wb_opt ? AD713X_FIR_BW_SEL_CH_MSK(ch) : 0);
pmallick 0:3afcd581558d 358 }
pmallick 0:3afcd581558d 359
pmallick 0:3afcd581558d 360 /**
pmallick 0:3afcd581558d 361 * @brief Initialize GPIO driver handlers for the GPIOs in the system.
pmallick 0:3afcd581558d 362 * ad713x_init() helper function.
pmallick 0:3afcd581558d 363 * @param [out] dev - AD713X device handler.
pmallick 0:3afcd581558d 364 * @param [in] init_param - Pointer to the initialization structure.
pmallick 0:3afcd581558d 365 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 366 */
pmallick 0:3afcd581558d 367 static int32_t ad713x_init_gpio(struct ad713x_dev *dev,
pmallick 0:3afcd581558d 368 struct ad713x_init_param *init_param)
pmallick 0:3afcd581558d 369 {
pmallick 0:3afcd581558d 370
pmallick 0:3afcd581558d 371 int32_t ret;
pmallick 0:3afcd581558d 372
pmallick 0:3afcd581558d 373 ret = gpio_get_optional(&dev->gpio_mode, init_param->gpio_mode);
pmallick 0:3afcd581558d 374 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 375 return FAILURE;
pmallick 0:3afcd581558d 376
pmallick 0:3afcd581558d 377 ret = gpio_get_optional(&dev->gpio_dclkmode, init_param->gpio_dclkmode);
pmallick 0:3afcd581558d 378 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 379 return FAILURE;
pmallick 0:3afcd581558d 380
pmallick 0:3afcd581558d 381 ret = gpio_get_optional(&dev->gpio_dclkio, init_param->gpio_dclkio);
pmallick 0:3afcd581558d 382 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 383 return FAILURE;
pmallick 0:3afcd581558d 384
pmallick 0:3afcd581558d 385 ret = gpio_get_optional(&dev->gpio_resetn, init_param->gpio_resetn);
pmallick 0:3afcd581558d 386 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 387 return FAILURE;
pmallick 0:3afcd581558d 388
pmallick 0:3afcd581558d 389 ret = gpio_get_optional(&dev->gpio_pnd, init_param->gpio_pnd);
pmallick 0:3afcd581558d 390 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 391 return FAILURE;
pmallick 0:3afcd581558d 392
pmallick 0:3afcd581558d 393 /** Tie this pin to IOVDD for master mode operation, tie this pin to
pmallick 0:3afcd581558d 394 * IOGND for slave mode operation. */
pmallick 0:3afcd581558d 395 if (init_param->gpio_mode) {
pmallick 0:3afcd581558d 396 ret = gpio_direction_output(dev->gpio_mode,
pmallick 0:3afcd581558d 397 init_param->mode_master_nslave);
pmallick 0:3afcd581558d 398 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 399 return FAILURE;
pmallick 0:3afcd581558d 400 }
pmallick 0:3afcd581558d 401
pmallick 0:3afcd581558d 402 /* Tie this pin low to ground to make DLCK operating in gated mode */
pmallick 0:3afcd581558d 403 if (init_param->gpio_dclkmode) {
pmallick 0:3afcd581558d 404 ret = gpio_direction_output(dev->gpio_dclkmode,
pmallick 0:3afcd581558d 405 init_param->dclkmode_free_ngated);
pmallick 0:3afcd581558d 406 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 407 return FAILURE;
pmallick 0:3afcd581558d 408 }
pmallick 0:3afcd581558d 409
pmallick 0:3afcd581558d 410 /** Tie this pin high to make DCLK an output, tie this pin low to make
pmallick 0:3afcd581558d 411 * DLCK an input. */
pmallick 0:3afcd581558d 412 if (init_param->gpio_dclkio) {
pmallick 0:3afcd581558d 413 ret = gpio_direction_output(dev->gpio_dclkio,
pmallick 0:3afcd581558d 414 init_param->dclkio_out_nin);
pmallick 0:3afcd581558d 415 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 416 return FAILURE;
pmallick 0:3afcd581558d 417 }
pmallick 0:3afcd581558d 418
pmallick 0:3afcd581558d 419 /** Get the ADCs out of power down state */
pmallick 0:3afcd581558d 420 if (init_param->gpio_pnd) {
pmallick 0:3afcd581558d 421 ret = gpio_direction_output(dev->gpio_pnd, init_param->pnd);
pmallick 0:3afcd581558d 422 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 423 return FAILURE;
pmallick 0:3afcd581558d 424 }
pmallick 0:3afcd581558d 425
pmallick 0:3afcd581558d 426 /** Reset to configure pins */
pmallick 0:3afcd581558d 427 if (init_param->gpio_resetn) {
pmallick 0:3afcd581558d 428 ret = gpio_direction_output(dev->gpio_resetn, false);
pmallick 0:3afcd581558d 429 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 430 return FAILURE;
pmallick 0:3afcd581558d 431 mdelay(100);
pmallick 0:3afcd581558d 432 ret = gpio_set_value(dev->gpio_resetn, true);
pmallick 0:3afcd581558d 433 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 434 return FAILURE;
pmallick 0:3afcd581558d 435 mdelay(100);
pmallick 0:3afcd581558d 436 }
pmallick 0:3afcd581558d 437
pmallick 0:3afcd581558d 438 return SUCCESS;
pmallick 0:3afcd581558d 439 }
pmallick 0:3afcd581558d 440
pmallick 0:3afcd581558d 441 /**
pmallick 0:3afcd581558d 442 * @brief Free the resources allocated by ad713x_init_gpio().
pmallick 0:3afcd581558d 443 * @param dev - The device structure.
pmallick 0:3afcd581558d 444 * @return SUCCESS in case of success, negative error code otherwise.
pmallick 0:3afcd581558d 445 */
pmallick 0:3afcd581558d 446 static int32_t ad713x_remove_gpio(struct ad713x_dev *dev)
pmallick 0:3afcd581558d 447 {
pmallick 0:3afcd581558d 448 int32_t ret;
pmallick 0:3afcd581558d 449
pmallick 0:3afcd581558d 450 if (dev->gpio_dclkio) {
pmallick 0:3afcd581558d 451 ret = gpio_remove(dev->gpio_dclkio);
pmallick 0:3afcd581558d 452 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 453 return FAILURE;
pmallick 0:3afcd581558d 454 }
pmallick 0:3afcd581558d 455 if (dev->gpio_dclkio) {
pmallick 0:3afcd581558d 456 ret = gpio_remove(dev->gpio_dclkmode);
pmallick 0:3afcd581558d 457 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 458 return FAILURE;
pmallick 0:3afcd581558d 459 }
pmallick 0:3afcd581558d 460 if (dev->gpio_mode) {
pmallick 0:3afcd581558d 461 ret = gpio_remove(dev->gpio_mode);
pmallick 0:3afcd581558d 462 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 463 return FAILURE;
pmallick 0:3afcd581558d 464 }
pmallick 0:3afcd581558d 465 if (dev->gpio_pnd) {
pmallick 0:3afcd581558d 466 ret = gpio_remove(dev->gpio_pnd);
pmallick 0:3afcd581558d 467 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 468 return FAILURE;
pmallick 0:3afcd581558d 469 }
pmallick 0:3afcd581558d 470 if (dev->gpio_resetn) {
pmallick 0:3afcd581558d 471 ret = gpio_remove(dev->gpio_resetn);
pmallick 0:3afcd581558d 472 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 473 return FAILURE;
pmallick 0:3afcd581558d 474 }
pmallick 0:3afcd581558d 475
pmallick 0:3afcd581558d 476 return SUCCESS;
pmallick 0:3afcd581558d 477 }
pmallick 0:3afcd581558d 478
pmallick 0:3afcd581558d 479 /**
pmallick 0:3afcd581558d 480 * @brief Initialize the wideband filter bandwidth for every channel.
pmallick 0:3afcd581558d 481 * ad713x_init() helper function.
pmallick 0:3afcd581558d 482 * @param [in] dev - AD713X device handler.
pmallick 0:3afcd581558d 483 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 484 */
pmallick 0:3afcd581558d 485 static int32_t ad713x_init_chan_bw(struct ad713x_dev *dev)
pmallick 0:3afcd581558d 486 {
pmallick 0:3afcd581558d 487 int8_t i;
pmallick 0:3afcd581558d 488 int32_t ret;
pmallick 0:3afcd581558d 489
pmallick 0:3afcd581558d 490 for (i = CH3; i >= 0; i--) {
pmallick 0:3afcd581558d 491 ret = ad713x_wideband_bw_sel(dev, i, 0);
pmallick 0:3afcd581558d 492 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 493 return FAILURE;
pmallick 0:3afcd581558d 494 }
pmallick 0:3afcd581558d 495
pmallick 0:3afcd581558d 496 return SUCCESS;
pmallick 0:3afcd581558d 497 }
pmallick 0:3afcd581558d 498
pmallick 0:3afcd581558d 499 /**
pmallick 0:3afcd581558d 500 * @brief Initialize the device.
pmallick 0:3afcd581558d 501 * @param device - The device structure.
pmallick 0:3afcd581558d 502 * @param init_param - The structure that contains the device initial
pmallick 0:3afcd581558d 503 * parameters.
pmallick 0:3afcd581558d 504 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
pmallick 0:3afcd581558d 505 */
pmallick 0:3afcd581558d 506 int32_t ad713x_init(struct ad713x_dev **device,
pmallick 0:3afcd581558d 507 struct ad713x_init_param *init_param)
pmallick 0:3afcd581558d 508 {
pmallick 0:3afcd581558d 509 struct ad713x_dev *dev;
pmallick 0:3afcd581558d 510 int32_t ret;
pmallick 0:3afcd581558d 511 uint8_t data;
pmallick 0:3afcd581558d 512
pmallick 0:3afcd581558d 513 dev = (struct ad713x_dev *)calloc(1, sizeof(*dev));
pmallick 0:3afcd581558d 514 if (!dev)
pmallick 0:3afcd581558d 515 return FAILURE;
pmallick 0:3afcd581558d 516
pmallick 0:3afcd581558d 517 if (!init_param->spi_common_dev) {
pmallick 0:3afcd581558d 518 ret = spi_init(&dev->spi_desc, &init_param->spi_init_prm);
pmallick 0:3afcd581558d 519 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 520 goto error_dev;
pmallick 0:3afcd581558d 521 } else {
pmallick 0:3afcd581558d 522 dev->spi_desc = calloc(1, sizeof *dev->spi_desc);
pmallick 0:3afcd581558d 523 dev->spi_desc->chip_select = init_param->spi_init_prm.chip_select;
pmallick 0:3afcd581558d 524 dev->spi_desc->extra = init_param->spi_common_dev->extra;
pmallick 0:3afcd581558d 525 dev->spi_desc->max_speed_hz = init_param->spi_init_prm.max_speed_hz;
pmallick 0:3afcd581558d 526 dev->spi_desc->mode = init_param->spi_init_prm.mode;
pmallick 0:3afcd581558d 527 }
pmallick 0:3afcd581558d 528
pmallick 0:3afcd581558d 529 ret = ad713x_init_gpio(dev, init_param);
pmallick 0:3afcd581558d 530 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 531 goto error_gpio;
pmallick 0:3afcd581558d 532
pmallick 0:3afcd581558d 533 dev->dev_id = init_param->dev_id;
pmallick 0:3afcd581558d 534
pmallick 0:3afcd581558d 535 ret = ad713x_spi_reg_read(dev, AD713X_REG_DEVICE_CONFIG, &data);
pmallick 0:3afcd581558d 536 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 537 goto error_gpio;
pmallick 0:3afcd581558d 538 data |= AD713X_DEV_CONFIG_PWR_MODE_MSK;
pmallick 0:3afcd581558d 539 ret = ad713x_spi_reg_write(dev, AD713X_REG_DEVICE_CONFIG, data);
pmallick 0:3afcd581558d 540 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 541 goto error_gpio;
pmallick 0:3afcd581558d 542
pmallick 0:3afcd581558d 543 ret = ad713x_clkout_output_en(dev, true);
pmallick 0:3afcd581558d 544 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 545 goto error_gpio;
pmallick 0:3afcd581558d 546
pmallick 0:3afcd581558d 547 ret = ad713x_ref_gain_correction_en(dev, true);
pmallick 0:3afcd581558d 548 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 549 goto error_gpio;
pmallick 0:3afcd581558d 550
pmallick 0:3afcd581558d 551 ret = ad713x_set_out_data_frame(dev, init_param->adc_data_len,
pmallick 0:3afcd581558d 552 init_param->crc_header);
pmallick 0:3afcd581558d 553 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 554 goto error_gpio;
pmallick 0:3afcd581558d 555
pmallick 0:3afcd581558d 556 ret = ad713x_dout_format_config(dev, init_param->format);
pmallick 0:3afcd581558d 557 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 558 goto error_gpio;
pmallick 0:3afcd581558d 559
pmallick 0:3afcd581558d 560 ret = ad713x_mag_phase_clk_delay(dev, init_param->clk_delay_en);
pmallick 0:3afcd581558d 561 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 562 goto error_gpio;
pmallick 0:3afcd581558d 563
pmallick 0:3afcd581558d 564 ret = ad713x_init_chan_bw(dev);
pmallick 0:3afcd581558d 565 if (IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 566 goto error_gpio;
pmallick 0:3afcd581558d 567
pmallick 0:3afcd581558d 568 *device = dev;
pmallick 0:3afcd581558d 569
pmallick 0:3afcd581558d 570 return SUCCESS;
pmallick 0:3afcd581558d 571
pmallick 0:3afcd581558d 572 error_gpio:
pmallick 0:3afcd581558d 573 ad713x_remove_gpio(dev);
pmallick 0:3afcd581558d 574 error_dev:
pmallick 0:3afcd581558d 575 ad713x_remove(dev);
pmallick 0:3afcd581558d 576
pmallick 0:3afcd581558d 577 return FAILURE;
pmallick 0:3afcd581558d 578 }
pmallick 0:3afcd581558d 579
pmallick 0:3afcd581558d 580 /**
pmallick 0:3afcd581558d 581 * @brief Free the resources allocated by ad713x_init().
pmallick 0:3afcd581558d 582 * @param dev - The device structure.
pmallick 0:3afcd581558d 583 * @return SUCCESS in case of success, negative error code otherwise.
pmallick 0:3afcd581558d 584 */
pmallick 0:3afcd581558d 585 int32_t ad713x_remove(struct ad713x_dev *dev)
pmallick 0:3afcd581558d 586 {
pmallick 0:3afcd581558d 587 int32_t ret;
pmallick 0:3afcd581558d 588
pmallick 0:3afcd581558d 589 if(!dev)
pmallick 0:3afcd581558d 590 return FAILURE;
pmallick 0:3afcd581558d 591
pmallick 0:3afcd581558d 592 ret = spi_remove(dev->spi_desc);
pmallick 0:3afcd581558d 593 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 594 return FAILURE;
pmallick 0:3afcd581558d 595
pmallick 0:3afcd581558d 596 ret = ad713x_remove_gpio(dev);
pmallick 0:3afcd581558d 597 if(IS_ERR_VALUE(ret))
pmallick 0:3afcd581558d 598 return FAILURE;
pmallick 0:3afcd581558d 599
pmallick 0:3afcd581558d 600 free(dev);
pmallick 0:3afcd581558d 601
pmallick 0:3afcd581558d 602 return SUCCESS;
pmallick 0:3afcd581558d 603 }