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 iio_ad7606.c
pmallick 0:3afcd581558d 3 * @brief Implementation of AD7606 IIO application interfaces
pmallick 0:3afcd581558d 4 * @details This module acts as an interface for AD7606 IIO application
pmallick 0:3afcd581558d 5 ********************************************************************************
pmallick 0:3afcd581558d 6 * Copyright (c) 2020 Analog Devices, Inc.
pmallick 0:3afcd581558d 7 *
pmallick 0:3afcd581558d 8 * This software is proprietary to Analog Devices, Inc. and its licensors.
pmallick 0:3afcd581558d 9 * By using this software you agree to the terms of the associated
pmallick 0:3afcd581558d 10 * Analog Devices Software License Agreement.
pmallick 0:3afcd581558d 11 *******************************************************************************/
pmallick 0:3afcd581558d 12
pmallick 0:3afcd581558d 13 /******************************************************************************/
pmallick 0:3afcd581558d 14 /***************************** Include Files **********************************/
pmallick 0:3afcd581558d 15 /******************************************************************************/
pmallick 0:3afcd581558d 16
pmallick 0:3afcd581558d 17 #include <inttypes.h>
pmallick 0:3afcd581558d 18 #include <string.h>
pmallick 0:3afcd581558d 19 #include <errno.h>
pmallick 0:3afcd581558d 20
pmallick 0:3afcd581558d 21 #include "app_config.h"
pmallick 0:3afcd581558d 22
pmallick 0:3afcd581558d 23 #include "tinyiiod.h"
pmallick 0:3afcd581558d 24 #include "iio_ad7606.h"
pmallick 0:3afcd581558d 25 #include "iio_ad7606_attr.h"
pmallick 0:3afcd581558d 26 #include "iio_app.h"
pmallick 0:3afcd581558d 27 #include "iio_transport.h"
pmallick 0:3afcd581558d 28
pmallick 0:3afcd581558d 29 #include "platform_support.h"
pmallick 0:3afcd581558d 30 #include "spi_extra.h"
pmallick 0:3afcd581558d 31 #include "gpio_extra.h"
pmallick 0:3afcd581558d 32 #include "uart_extra.h"
pmallick 0:3afcd581558d 33 #include "irq_extra.h"
pmallick 0:3afcd581558d 34
pmallick 0:3afcd581558d 35 #include "ad7606.h"
pmallick 0:3afcd581558d 36 #include "ad7606_data_capture.h"
pmallick 0:3afcd581558d 37 #include "ad7606_support.h"
pmallick 0:3afcd581558d 38 #include "ad7606_user_config.h"
pmallick 0:3afcd581558d 39
pmallick 0:3afcd581558d 40 /******************************************************************************/
pmallick 0:3afcd581558d 41 /************************ Macros/Constants ************************************/
pmallick 0:3afcd581558d 42 /******************************************************************************/
pmallick 0:3afcd581558d 43
pmallick 0:3afcd581558d 44 /* ADC data to Voltage conversion scale factor for IIO client */
pmallick 0:3afcd581558d 45 #define DEFAULT_SCALE ((DEFAULT_CHN_RANGE / ADC_MAX_COUNT_BIPOLAR) * 1000)
pmallick 0:3afcd581558d 46
pmallick 0:3afcd581558d 47 /* Select open wire detection mode (disable to select auto mode) */
pmallick 0:3afcd581558d 48 #define MANUAL_OPEN_DETECT
pmallick 0:3afcd581558d 49
pmallick 0:3afcd581558d 50 /* Open detect threshold @50K Rpd as per datasheet */
pmallick 0:3afcd581558d 51 #define OPEN_DETECT_THRESHOLD_RPD50K 15
pmallick 0:3afcd581558d 52
pmallick 0:3afcd581558d 53 /* Number of conversions to be performed before detecting open wire */
pmallick 0:3afcd581558d 54 #define OPEN_DETECT_AUTO_MODE_CNTS 10
pmallick 0:3afcd581558d 55
pmallick 0:3afcd581558d 56 /******************************************************************************/
pmallick 0:3afcd581558d 57 /*************************** Types Declarations *******************************/
pmallick 0:3afcd581558d 58 /******************************************************************************/
pmallick 0:3afcd581558d 59
pmallick 0:3afcd581558d 60 /**
pmallick 0:3afcd581558d 61 * IIO application instance descriptor.
pmallick 0:3afcd581558d 62 */
pmallick 0:3afcd581558d 63 static struct iio_app_desc *iio_app_desc;
pmallick 0:3afcd581558d 64
pmallick 0:3afcd581558d 65 /**
pmallick 0:3afcd581558d 66 * Device name.
pmallick 0:3afcd581558d 67 */
pmallick 0:3afcd581558d 68 static const char dev_name[] = ACTIVE_DEVICE_NAME;
pmallick 0:3afcd581558d 69
pmallick 0:3afcd581558d 70 /**
pmallick 0:3afcd581558d 71 * Pointer to the struct representing the AD7606 IIO device
pmallick 0:3afcd581558d 72 */
pmallick 0:3afcd581558d 73 static struct ad7606_dev *p_ad7606_dev = NULL;
pmallick 0:3afcd581558d 74
pmallick 0:3afcd581558d 75
pmallick 0:3afcd581558d 76 /* Device attributes with default values */
pmallick 0:3afcd581558d 77
pmallick 0:3afcd581558d 78 /* Power down mode values string representation (possible values specified in datasheet) */
pmallick 0:3afcd581558d 79 static char *operating_mode_str[] = {
pmallick 0:3afcd581558d 80 "0 (Normal Mode)",
pmallick 0:3afcd581558d 81 "1 (Standby Mode)",
pmallick 0:3afcd581558d 82 "2 (Auto Standby Mode)",
pmallick 0:3afcd581558d 83 "3 (Shutdown Mode)"
pmallick 0:3afcd581558d 84 };
pmallick 0:3afcd581558d 85
pmallick 0:3afcd581558d 86 /* Bandwidth values string */
pmallick 0:3afcd581558d 87 static char *bandwidth_str[] = {
pmallick 0:3afcd581558d 88 "0 (Low)",
pmallick 0:3afcd581558d 89 "1 (High)"
pmallick 0:3afcd581558d 90 };
pmallick 0:3afcd581558d 91
pmallick 0:3afcd581558d 92 /* Channel range values string representation (possible values specified in datasheet) */
pmallick 0:3afcd581558d 93 static char *chn_range_str[] = {
pmallick 0:3afcd581558d 94 #if defined(DEV_AD7606B)
pmallick 0:3afcd581558d 95 "0 (+/-2.5V SE)", "1 (+/-5.0V SE)", "2 (+/-10.0V SE)", "3 (+/-10.0V SE)",
pmallick 0:3afcd581558d 96 "4 (+/-10.0V SE)", "5 (+/-10.0V SE)", "6 (+/-10.0V SE)", "7 (+/-10.0V SE)",
pmallick 0:3afcd581558d 97 "8 (+/-10.0V SE)", "9 (+/-10.0V SE)", "10 (+/-10.0V SE)", "11 (+/-10.0V SE)",
pmallick 0:3afcd581558d 98 #elif defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
pmallick 0:3afcd581558d 99 "0 (+/-2.5V SE)", "1 (+/-5.0V SE)", "2 (+/-6.25V SE)", "3 (+/-10.0V SE)",
pmallick 0:3afcd581558d 100 "4 (+/-12.5V SE)", "5 (+5.0V SE)", "6 (+10.0V SE)", "7 (+12.5V SE)",
pmallick 0:3afcd581558d 101 "8 (+/-5.0V DE)", "9 (+/-10.0V DE)", "10 (+/-12.5V DE)", "11 (+/-20.0V DE)"
pmallick 0:3afcd581558d 102 #elif defined(DEV_AD7609)
pmallick 0:3afcd581558d 103 "0 (+/-10.0V SE)", "1 (+/-20.0V SE)"
pmallick 0:3afcd581558d 104 #else
pmallick 0:3afcd581558d 105 "0 (+/-5.0V SE)", "1 (+/-10.0V SE)"
pmallick 0:3afcd581558d 106 #endif
pmallick 0:3afcd581558d 107 };
pmallick 0:3afcd581558d 108
pmallick 0:3afcd581558d 109 /* Oversampling values string representation (possible values specified in datasheet) */
pmallick 0:3afcd581558d 110 static char *oversampling_val_str[] = {
pmallick 0:3afcd581558d 111 "0 (no oversampling)", "1 (oversampling by 2)", "2 (oversampling by 4)",
pmallick 0:3afcd581558d 112 "3 (oversampling by 8)", "4 (oversampling by 16)", "5 (oversampling by 32)",
pmallick 0:3afcd581558d 113 "6 (oversampling by 64)", "7 (oversampling by 128)", "8 (oversampling by 256)"
pmallick 0:3afcd581558d 114 };
pmallick 0:3afcd581558d 115
pmallick 0:3afcd581558d 116
pmallick 0:3afcd581558d 117 /* Channel range values string representation (possible values specified in datasheet) */
pmallick 0:3afcd581558d 118 static float chn_range_val[] = {
pmallick 0:3afcd581558d 119 #if defined(DEV_AD7606B)
pmallick 0:3afcd581558d 120 2.5, 5.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0
pmallick 0:3afcd581558d 121 #elif defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
pmallick 0:3afcd581558d 122 2.5, 5.0, 6.25, 10.0, 12.5, 5.0, 10.0, 12.5, 5.0, 10.0, 12.5, 20.0
pmallick 0:3afcd581558d 123 #elif defined(DEV_AD7609)
pmallick 0:3afcd581558d 124 10.0, 20.0
pmallick 0:3afcd581558d 125 #else
pmallick 0:3afcd581558d 126 5.0, 10.0
pmallick 0:3afcd581558d 127 #endif
pmallick 0:3afcd581558d 128 };
pmallick 0:3afcd581558d 129
pmallick 0:3afcd581558d 130 /* Range value per channel */
pmallick 0:3afcd581558d 131 static float attr_chn_range[NO_OF_CHANNELS] = {
pmallick 0:3afcd581558d 132 DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE,
pmallick 0:3afcd581558d 133 DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE, DEFAULT_CHN_RANGE
pmallick 0:3afcd581558d 134 };
pmallick 0:3afcd581558d 135
pmallick 0:3afcd581558d 136 /* Scale value per channel */
pmallick 0:3afcd581558d 137 static float attr_scale_val[NO_OF_CHANNELS] = {
pmallick 0:3afcd581558d 138 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE,
pmallick 0:3afcd581558d 139 DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE, DEFAULT_SCALE
pmallick 0:3afcd581558d 140 };
pmallick 0:3afcd581558d 141
pmallick 0:3afcd581558d 142 /* Scale value per channel */
pmallick 0:3afcd581558d 143 static polarity_e attr_polarity_val[NO_OF_CHANNELS] = {
pmallick 0:3afcd581558d 144 BIPOLAR, BIPOLAR, BIPOLAR, BIPOLAR,
pmallick 0:3afcd581558d 145 BIPOLAR, BIPOLAR, BIPOLAR, BIPOLAR
pmallick 0:3afcd581558d 146 };
pmallick 0:3afcd581558d 147
pmallick 0:3afcd581558d 148 /* Channel range */
pmallick 0:3afcd581558d 149 typedef enum {
pmallick 0:3afcd581558d 150 LOW,
pmallick 0:3afcd581558d 151 HIGH
pmallick 0:3afcd581558d 152 } range_e;
pmallick 0:3afcd581558d 153
pmallick 0:3afcd581558d 154 /* Device register value */
pmallick 0:3afcd581558d 155 static uint8_t device_reg_val;
pmallick 0:3afcd581558d 156
pmallick 0:3afcd581558d 157 /* GPIO LED descriptor and init structure */
pmallick 0:3afcd581558d 158 static struct gpio_desc *gpio_led;
pmallick 0:3afcd581558d 159
pmallick 0:3afcd581558d 160 /******************************************************************************/
pmallick 0:3afcd581558d 161 /************************ Functions Prototypes ********************************/
pmallick 0:3afcd581558d 162 /******************************************************************************/
pmallick 0:3afcd581558d 163
pmallick 0:3afcd581558d 164 static float get_vltg_conv_scale_factor(float chn_range, polarity_e polarity);
pmallick 0:3afcd581558d 165
pmallick 0:3afcd581558d 166 /******************************************************************************/
pmallick 0:3afcd581558d 167 /************************ Functions Definitions *******************************/
pmallick 0:3afcd581558d 168 /******************************************************************************/
pmallick 0:3afcd581558d 169
pmallick 0:3afcd581558d 170 /*!
pmallick 0:3afcd581558d 171 * @brief Getter/Setter for the scale attribute value
pmallick 0:3afcd581558d 172 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 173 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 174 * @param len- length of buffer string data
pmallick 0:3afcd581558d 175 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 176 * @return Number of characters read/written
pmallick 0:3afcd581558d 177 */
pmallick 0:3afcd581558d 178 ssize_t get_chn_scale(void *device,
pmallick 0:3afcd581558d 179 char *buf,
pmallick 0:3afcd581558d 180 size_t len,
pmallick 0:3afcd581558d 181 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 182 {
pmallick 0:3afcd581558d 183 return (ssize_t) sprintf(buf, "%f", attr_scale_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 184 }
pmallick 0:3afcd581558d 185
pmallick 0:3afcd581558d 186 ssize_t set_chn_scale(void *device,
pmallick 0:3afcd581558d 187 char *buf,
pmallick 0:3afcd581558d 188 size_t len,
pmallick 0:3afcd581558d 189 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 190 {
pmallick 0:3afcd581558d 191 float scale;
pmallick 0:3afcd581558d 192
pmallick 0:3afcd581558d 193 (void)sscanf(buf, "%f", &scale);
pmallick 0:3afcd581558d 194
pmallick 0:3afcd581558d 195 if (scale > 0.0) {
pmallick 0:3afcd581558d 196 attr_scale_val[channel->ch_num - 1] = scale;
pmallick 0:3afcd581558d 197 return len;
pmallick 0:3afcd581558d 198 }
pmallick 0:3afcd581558d 199
pmallick 0:3afcd581558d 200 return -EINVAL;
pmallick 0:3afcd581558d 201 }
pmallick 0:3afcd581558d 202
pmallick 0:3afcd581558d 203
pmallick 0:3afcd581558d 204 /*!
pmallick 0:3afcd581558d 205 * @brief Getter/Setter for the raw attribute value
pmallick 0:3afcd581558d 206 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 207 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 208 * @param len- length of buffer string data
pmallick 0:3afcd581558d 209 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 210 * @return Number of characters read/written
pmallick 0:3afcd581558d 211 */
pmallick 0:3afcd581558d 212 ssize_t get_chn_raw(void *device,
pmallick 0:3afcd581558d 213 char *buf,
pmallick 0:3afcd581558d 214 size_t len,
pmallick 0:3afcd581558d 215 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 216 {
pmallick 0:3afcd581558d 217 int32_t adc_data_raw;
pmallick 0:3afcd581558d 218
pmallick 0:3afcd581558d 219 /* Capture the raw adc data */
pmallick 0:3afcd581558d 220 adc_data_raw = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 221 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 222
pmallick 0:3afcd581558d 223 return (ssize_t) sprintf(buf, "%d", adc_data_raw);
pmallick 0:3afcd581558d 224 }
pmallick 0:3afcd581558d 225
pmallick 0:3afcd581558d 226 ssize_t set_chn_raw(void *device,
pmallick 0:3afcd581558d 227 char *buf,
pmallick 0:3afcd581558d 228 size_t len,
pmallick 0:3afcd581558d 229 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 230 {
pmallick 0:3afcd581558d 231 /* NA- Can't set raw value */
pmallick 0:3afcd581558d 232 return len;
pmallick 0:3afcd581558d 233 }
pmallick 0:3afcd581558d 234
pmallick 0:3afcd581558d 235
pmallick 0:3afcd581558d 236 /*!
pmallick 0:3afcd581558d 237 * @brief Getter/Setter for the operating mode attribute value
pmallick 0:3afcd581558d 238 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 239 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 240 * @param len- length of buffer string data
pmallick 0:3afcd581558d 241 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 242 * @return Number of characters read/written
pmallick 0:3afcd581558d 243 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 244 */
pmallick 0:3afcd581558d 245 ssize_t get_operating_mode(void *device,
pmallick 0:3afcd581558d 246 char *buf,
pmallick 0:3afcd581558d 247 size_t len,
pmallick 0:3afcd581558d 248 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 249 {
pmallick 0:3afcd581558d 250 uint8_t read_val;
pmallick 0:3afcd581558d 251 uint8_t operating_mode_value;
pmallick 0:3afcd581558d 252
pmallick 0:3afcd581558d 253 if (ad7606_spi_reg_read(device, AD7606_REG_CONFIG, &read_val) == SUCCESS) {
pmallick 0:3afcd581558d 254 operating_mode_value = (read_val & AD7606_CONFIG_OPERATION_MODE_MSK);
pmallick 0:3afcd581558d 255
pmallick 0:3afcd581558d 256 if (operating_mode_value < sizeof(operating_mode_str) / sizeof(
pmallick 0:3afcd581558d 257 operating_mode_str[0])) {
pmallick 0:3afcd581558d 258 return (ssize_t)sprintf(buf, "%s", operating_mode_str[operating_mode_value]);
pmallick 0:3afcd581558d 259 }
pmallick 0:3afcd581558d 260 }
pmallick 0:3afcd581558d 261
pmallick 0:3afcd581558d 262 return -EINVAL;
pmallick 0:3afcd581558d 263 }
pmallick 0:3afcd581558d 264
pmallick 0:3afcd581558d 265 ssize_t set_operating_mode(void *device,
pmallick 0:3afcd581558d 266 char *buf,
pmallick 0:3afcd581558d 267 size_t len,
pmallick 0:3afcd581558d 268 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 269 {
pmallick 0:3afcd581558d 270 uint8_t operating_mode_value;
pmallick 0:3afcd581558d 271
pmallick 0:3afcd581558d 272 (void)sscanf(buf, "%d", &operating_mode_value);
pmallick 0:3afcd581558d 273
pmallick 0:3afcd581558d 274 if (operating_mode_value < sizeof(operating_mode_str) / sizeof(
pmallick 0:3afcd581558d 275 operating_mode_str[0])) {
pmallick 0:3afcd581558d 276 if (ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 277 AD7606_REG_CONFIG,
pmallick 0:3afcd581558d 278 AD7606_CONFIG_OPERATION_MODE_MSK,
pmallick 0:3afcd581558d 279 operating_mode_value) == SUCCESS) {
pmallick 0:3afcd581558d 280 return len;
pmallick 0:3afcd581558d 281 }
pmallick 0:3afcd581558d 282 }
pmallick 0:3afcd581558d 283
pmallick 0:3afcd581558d 284 return -EINVAL;
pmallick 0:3afcd581558d 285 }
pmallick 0:3afcd581558d 286
pmallick 0:3afcd581558d 287
pmallick 0:3afcd581558d 288 /*!
pmallick 0:3afcd581558d 289 * @brief Getter/Setter for the power down mode attribute value
pmallick 0:3afcd581558d 290 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 291 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 292 * @param len- length of buffer string data
pmallick 0:3afcd581558d 293 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 294 * @return Number of characters read/written
pmallick 0:3afcd581558d 295 * @note Available for all devices except AD7606B and AD7606C
pmallick 0:3afcd581558d 296 */
pmallick 0:3afcd581558d 297 ssize_t get_power_down_mode(void *device,
pmallick 0:3afcd581558d 298 char *buf,
pmallick 0:3afcd581558d 299 size_t len,
pmallick 0:3afcd581558d 300 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 301 {
pmallick 0:3afcd581558d 302 uint8_t gpio_stby_val;
pmallick 0:3afcd581558d 303 uint8_t gpio_range_val;
pmallick 0:3afcd581558d 304
pmallick 0:3afcd581558d 305 if (gpio_get_value(((struct ad7606_dev *)device)->gpio_stby_n,
pmallick 0:3afcd581558d 306 &gpio_stby_val) == SUCCESS) {
pmallick 0:3afcd581558d 307 if (gpio_get_value(((struct ad7606_dev *)device)->gpio_range,
pmallick 0:3afcd581558d 308 &gpio_range_val) == SUCCESS) {
pmallick 0:3afcd581558d 309
pmallick 0:3afcd581558d 310 if (gpio_stby_val) {
pmallick 0:3afcd581558d 311 return sprintf(buf, "%s", operating_mode_str[AD7606_NORMAL]);
pmallick 0:3afcd581558d 312 } else {
pmallick 0:3afcd581558d 313 if (gpio_range_val) {
pmallick 0:3afcd581558d 314 return sprintf(buf, "%s", operating_mode_str[AD7606_STANDBY]);
pmallick 0:3afcd581558d 315 } else {
pmallick 0:3afcd581558d 316 return sprintf(buf, "%s", operating_mode_str[AD7606_SHUTDOWN]);
pmallick 0:3afcd581558d 317 }
pmallick 0:3afcd581558d 318 }
pmallick 0:3afcd581558d 319 }
pmallick 0:3afcd581558d 320 }
pmallick 0:3afcd581558d 321
pmallick 0:3afcd581558d 322 return -EINVAL;
pmallick 0:3afcd581558d 323 }
pmallick 0:3afcd581558d 324
pmallick 0:3afcd581558d 325 ssize_t set_power_down_mode(void *device,
pmallick 0:3afcd581558d 326 char *buf,
pmallick 0:3afcd581558d 327 size_t len,
pmallick 0:3afcd581558d 328 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 329 {
pmallick 0:3afcd581558d 330 uint8_t power_down_mode_value;
pmallick 0:3afcd581558d 331 static enum ad7606_op_mode prev_power_down_mode = AD7606_NORMAL;
pmallick 0:3afcd581558d 332 struct ad7606_config dev_config;
pmallick 0:3afcd581558d 333
pmallick 0:3afcd581558d 334 sscanf(buf, "%d", &power_down_mode_value);
pmallick 0:3afcd581558d 335
pmallick 0:3afcd581558d 336 if (power_down_mode_value < (sizeof(operating_mode_str) / sizeof(
pmallick 0:3afcd581558d 337 operating_mode_str[0]))) {
pmallick 0:3afcd581558d 338
pmallick 0:3afcd581558d 339 dev_config.op_mode = power_down_mode_value;
pmallick 0:3afcd581558d 340
pmallick 0:3afcd581558d 341 switch (power_down_mode_value) {
pmallick 0:3afcd581558d 342 case AD7606_NORMAL:
pmallick 0:3afcd581558d 343 if (ad7606_set_config(device, dev_config) == SUCCESS) {
pmallick 0:3afcd581558d 344 /* Reset the device if previous power down mode was either standby
pmallick 0:3afcd581558d 345 * or shutdown */
pmallick 0:3afcd581558d 346 if (prev_power_down_mode != AD7606_NORMAL) {
pmallick 0:3afcd581558d 347
pmallick 0:3afcd581558d 348 /* Power-up wait time */
pmallick 0:3afcd581558d 349 mdelay(1);
pmallick 0:3afcd581558d 350
pmallick 0:3afcd581558d 351 /* Toggle reset pin */
pmallick 0:3afcd581558d 352 if (gpio_set_value(((struct ad7606_dev *)device)->gpio_reset,
pmallick 0:3afcd581558d 353 GPIO_HIGH) == SUCCESS) {
pmallick 0:3afcd581558d 354 mdelay(1);
pmallick 0:3afcd581558d 355
pmallick 0:3afcd581558d 356 if (gpio_set_value(((struct ad7606_dev *)device)->gpio_reset,
pmallick 0:3afcd581558d 357 GPIO_LOW) == SUCCESS) {
pmallick 0:3afcd581558d 358 prev_power_down_mode = AD7606_NORMAL;
pmallick 0:3afcd581558d 359 return len;
pmallick 0:3afcd581558d 360 }
pmallick 0:3afcd581558d 361 }
pmallick 0:3afcd581558d 362 }
pmallick 0:3afcd581558d 363 }
pmallick 0:3afcd581558d 364 break;
pmallick 0:3afcd581558d 365
pmallick 0:3afcd581558d 366 case AD7606_STANDBY:
pmallick 0:3afcd581558d 367 if (ad7606_set_config(device, dev_config) == SUCCESS) {
pmallick 0:3afcd581558d 368 prev_power_down_mode = AD7606_STANDBY;
pmallick 0:3afcd581558d 369 return len;
pmallick 0:3afcd581558d 370 }
pmallick 0:3afcd581558d 371 break;
pmallick 0:3afcd581558d 372
pmallick 0:3afcd581558d 373 case AD7606_SHUTDOWN:
pmallick 0:3afcd581558d 374 if (ad7606_set_config(device, dev_config) == SUCCESS) {
pmallick 0:3afcd581558d 375 prev_power_down_mode = AD7606_SHUTDOWN;
pmallick 0:3afcd581558d 376 return len;
pmallick 0:3afcd581558d 377 }
pmallick 0:3afcd581558d 378 break;
pmallick 0:3afcd581558d 379
pmallick 0:3afcd581558d 380 default:
pmallick 0:3afcd581558d 381 break;
pmallick 0:3afcd581558d 382 }
pmallick 0:3afcd581558d 383 }
pmallick 0:3afcd581558d 384
pmallick 0:3afcd581558d 385 return -EINVAL;
pmallick 0:3afcd581558d 386 }
pmallick 0:3afcd581558d 387
pmallick 0:3afcd581558d 388
pmallick 0:3afcd581558d 389 /*!
pmallick 0:3afcd581558d 390 * @brief Getter/Setter for the range attribute value
pmallick 0:3afcd581558d 391 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 392 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 393 * @param len- length of buffer string data
pmallick 0:3afcd581558d 394 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 395 * @return Number of characters read/written
pmallick 0:3afcd581558d 396 * @note Available for all devices except AD7606B and AD7606C
pmallick 0:3afcd581558d 397 */
pmallick 0:3afcd581558d 398 ssize_t get_range(void *device,
pmallick 0:3afcd581558d 399 char *buf,
pmallick 0:3afcd581558d 400 size_t len,
pmallick 0:3afcd581558d 401 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 402 {
pmallick 0:3afcd581558d 403 uint8_t gpio_range_val;
pmallick 0:3afcd581558d 404 struct ad7606_dev *dev = device;
pmallick 0:3afcd581558d 405
pmallick 0:3afcd581558d 406 if (gpio_get_value(dev->gpio_range, &gpio_range_val) == SUCCESS) {
pmallick 0:3afcd581558d 407 if (gpio_range_val) {
pmallick 0:3afcd581558d 408 return sprintf(buf, "%s", chn_range_str[HIGH]);
pmallick 0:3afcd581558d 409 } else {
pmallick 0:3afcd581558d 410 return sprintf(buf, "%s", chn_range_str[LOW]);
pmallick 0:3afcd581558d 411 }
pmallick 0:3afcd581558d 412 }
pmallick 0:3afcd581558d 413
pmallick 0:3afcd581558d 414 return -EINVAL;
pmallick 0:3afcd581558d 415 }
pmallick 0:3afcd581558d 416
pmallick 0:3afcd581558d 417 ssize_t set_range(void *device,
pmallick 0:3afcd581558d 418 char *buf,
pmallick 0:3afcd581558d 419 size_t len,
pmallick 0:3afcd581558d 420 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 421 {
pmallick 0:3afcd581558d 422 uint8_t range_value;
pmallick 0:3afcd581558d 423 struct ad7606_dev *dev = device;
pmallick 0:3afcd581558d 424
pmallick 0:3afcd581558d 425 (void)sscanf(buf, "%d", &range_value);
pmallick 0:3afcd581558d 426
pmallick 0:3afcd581558d 427 if (range_value < (sizeof(chn_range_str) / sizeof(chn_range_str[0]))) {
pmallick 0:3afcd581558d 428 if (range_value == LOW) {
pmallick 0:3afcd581558d 429 if (gpio_set_value(dev->gpio_range, GPIO_LOW) == SUCCESS) {
pmallick 0:3afcd581558d 430 return len;
pmallick 0:3afcd581558d 431 }
pmallick 0:3afcd581558d 432 } else {
pmallick 0:3afcd581558d 433 if (gpio_set_value(dev->gpio_range, GPIO_HIGH) == SUCCESS) {
pmallick 0:3afcd581558d 434 return len;
pmallick 0:3afcd581558d 435 }
pmallick 0:3afcd581558d 436 }
pmallick 0:3afcd581558d 437 }
pmallick 0:3afcd581558d 438
pmallick 0:3afcd581558d 439 return -EINVAL;
pmallick 0:3afcd581558d 440 }
pmallick 0:3afcd581558d 441
pmallick 0:3afcd581558d 442
pmallick 0:3afcd581558d 443 /*!
pmallick 0:3afcd581558d 444 * @brief Getter/Setter for the oversampling attribute value
pmallick 0:3afcd581558d 445 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 446 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 447 * @param len- length of buffer string data
pmallick 0:3afcd581558d 448 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 449 * @return Number of characters read/written
pmallick 0:3afcd581558d 450 * @note Available for all devices except AD7606B and AD7606C
pmallick 0:3afcd581558d 451 */
pmallick 0:3afcd581558d 452 ssize_t get_oversampling(void *device,
pmallick 0:3afcd581558d 453 char *buf,
pmallick 0:3afcd581558d 454 size_t len,
pmallick 0:3afcd581558d 455 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 456 {
pmallick 0:3afcd581558d 457 uint8_t oversampling_value;
pmallick 0:3afcd581558d 458 uint8_t read_val;
pmallick 0:3afcd581558d 459 uint8_t gpio_osr0_val;
pmallick 0:3afcd581558d 460 uint8_t gpio_osr1_val;
pmallick 0:3afcd581558d 461 uint8_t gpio_osr2_val;
pmallick 0:3afcd581558d 462 struct ad7606_dev *dev = device;
pmallick 0:3afcd581558d 463
pmallick 0:3afcd581558d 464 #if defined(DEV_AD7606B) || defined(DEV_AD7606C_18) || defined(DEV_AD7606C_16)
pmallick 0:3afcd581558d 465 if (ad7606_spi_reg_read(device,
pmallick 0:3afcd581558d 466 AD7606_REG_OVERSAMPLING,
pmallick 0:3afcd581558d 467 &read_val) == SUCCESS) {
pmallick 0:3afcd581558d 468 oversampling_value = (read_val & AD7606_OVERSAMPLING_MSK);
pmallick 0:3afcd581558d 469
pmallick 0:3afcd581558d 470 if (oversampling_value < sizeof(oversampling_val_str) / sizeof(
pmallick 0:3afcd581558d 471 oversampling_val_str[0])) {
pmallick 0:3afcd581558d 472 return (ssize_t)sprintf(buf, "%s", oversampling_val_str[oversampling_value]);
pmallick 0:3afcd581558d 473 }
pmallick 0:3afcd581558d 474 }
pmallick 0:3afcd581558d 475 #else
pmallick 0:3afcd581558d 476 if (gpio_get_value(dev->gpio_os0, &gpio_osr0_val) == SUCCESS) {
pmallick 0:3afcd581558d 477 if (gpio_get_value(dev->gpio_os1, &gpio_osr1_val) == SUCCESS) {
pmallick 0:3afcd581558d 478 if (gpio_get_value(dev->gpio_os2, &gpio_osr2_val) == SUCCESS) {
pmallick 0:3afcd581558d 479 oversampling_value = (gpio_osr2_val << 2) | (gpio_osr1_val << 1) |
pmallick 0:3afcd581558d 480 gpio_osr0_val;
pmallick 0:3afcd581558d 481
pmallick 0:3afcd581558d 482 if (oversampling_value < (sizeof(oversampling_val_str) / sizeof(
pmallick 0:3afcd581558d 483 oversampling_val_str[0]))) {
pmallick 0:3afcd581558d 484 return sprintf(buf, "%s", oversampling_val_str[oversampling_value]);
pmallick 0:3afcd581558d 485 }
pmallick 0:3afcd581558d 486 }
pmallick 0:3afcd581558d 487 }
pmallick 0:3afcd581558d 488 }
pmallick 0:3afcd581558d 489 #endif
pmallick 0:3afcd581558d 490
pmallick 0:3afcd581558d 491 return -EINVAL;
pmallick 0:3afcd581558d 492 }
pmallick 0:3afcd581558d 493
pmallick 0:3afcd581558d 494 ssize_t set_oversampling(void *device,
pmallick 0:3afcd581558d 495 char *buf,
pmallick 0:3afcd581558d 496 size_t len,
pmallick 0:3afcd581558d 497 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 498 {
pmallick 0:3afcd581558d 499 uint8_t oversampling_value;
pmallick 0:3afcd581558d 500 struct ad7606_oversampling oversampling_cfg;
pmallick 0:3afcd581558d 501
pmallick 0:3afcd581558d 502 (void)sscanf(buf, "%d", &oversampling_value);
pmallick 0:3afcd581558d 503
pmallick 0:3afcd581558d 504 if (oversampling_value < (sizeof(oversampling_val_str) / sizeof(
pmallick 0:3afcd581558d 505 oversampling_val_str[0]))) {
pmallick 0:3afcd581558d 506
pmallick 0:3afcd581558d 507 oversampling_cfg.os_pad = 0;
pmallick 0:3afcd581558d 508 oversampling_cfg.os_ratio = oversampling_value;
pmallick 0:3afcd581558d 509
pmallick 0:3afcd581558d 510 ad7606_set_oversampling(device, oversampling_cfg);
pmallick 0:3afcd581558d 511
pmallick 0:3afcd581558d 512 return len;
pmallick 0:3afcd581558d 513 }
pmallick 0:3afcd581558d 514
pmallick 0:3afcd581558d 515 return -EINVAL;
pmallick 0:3afcd581558d 516 }
pmallick 0:3afcd581558d 517
pmallick 0:3afcd581558d 518
pmallick 0:3afcd581558d 519 /*!
pmallick 0:3afcd581558d 520 * @brief Getter/Setter for the bandwidth attribute value
pmallick 0:3afcd581558d 521 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 522 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 523 * @param len- length of buffer string data
pmallick 0:3afcd581558d 524 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 525 * @return Number of characters read/written
pmallick 0:3afcd581558d 526 * @note Available for only AD7606C
pmallick 0:3afcd581558d 527 */
pmallick 0:3afcd581558d 528 ssize_t get_bandwidth(void *device,
pmallick 0:3afcd581558d 529 char *buf,
pmallick 0:3afcd581558d 530 size_t len,
pmallick 0:3afcd581558d 531 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 532 {
pmallick 0:3afcd581558d 533 uint8_t bw_value;
pmallick 0:3afcd581558d 534 uint8_t read_val;
pmallick 0:3afcd581558d 535
pmallick 0:3afcd581558d 536 if (ad7606_spi_reg_read(device,
pmallick 0:3afcd581558d 537 AD7606_REG_BANDWIDTH,
pmallick 0:3afcd581558d 538 &read_val) == SUCCESS) {
pmallick 0:3afcd581558d 539 bw_value = (read_val >> (channel->ch_num - 1)) & 0x1;
pmallick 0:3afcd581558d 540
pmallick 0:3afcd581558d 541 if (bw_value < sizeof(bandwidth_str) / sizeof(
pmallick 0:3afcd581558d 542 bandwidth_str[0])) {
pmallick 0:3afcd581558d 543 return (ssize_t)sprintf(buf, "%s", bandwidth_str[bw_value]);
pmallick 0:3afcd581558d 544 }
pmallick 0:3afcd581558d 545 }
pmallick 0:3afcd581558d 546
pmallick 0:3afcd581558d 547 return -EINVAL;
pmallick 0:3afcd581558d 548 }
pmallick 0:3afcd581558d 549
pmallick 0:3afcd581558d 550 ssize_t set_bandwidth(void *device,
pmallick 0:3afcd581558d 551 char *buf,
pmallick 0:3afcd581558d 552 size_t len,
pmallick 0:3afcd581558d 553 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 554 {
pmallick 0:3afcd581558d 555 uint8_t bw_value;
pmallick 0:3afcd581558d 556 uint8_t read_val;
pmallick 0:3afcd581558d 557
pmallick 0:3afcd581558d 558 (void)sscanf(buf, "%d", &bw_value);
pmallick 0:3afcd581558d 559
pmallick 0:3afcd581558d 560 if (bw_value < sizeof(bandwidth_str) / sizeof(
pmallick 0:3afcd581558d 561 bandwidth_str[0])) {
pmallick 0:3afcd581558d 562 if (ad7606_spi_reg_read(device,
pmallick 0:3afcd581558d 563 AD7606_REG_BANDWIDTH,
pmallick 0:3afcd581558d 564 &read_val) == SUCCESS) {
pmallick 0:3afcd581558d 565 if (bw_value) {
pmallick 0:3afcd581558d 566 read_val |= (1 << (channel->ch_num - 1));
pmallick 0:3afcd581558d 567 } else {
pmallick 0:3afcd581558d 568 read_val &= (~(1 << (channel->ch_num - 1)));
pmallick 0:3afcd581558d 569 }
pmallick 0:3afcd581558d 570
pmallick 0:3afcd581558d 571 if (ad7606_spi_reg_write(device,
pmallick 0:3afcd581558d 572 AD7606_REG_BANDWIDTH,
pmallick 0:3afcd581558d 573 read_val) == SUCCESS) {
pmallick 0:3afcd581558d 574 return len;
pmallick 0:3afcd581558d 575 }
pmallick 0:3afcd581558d 576 }
pmallick 0:3afcd581558d 577 }
pmallick 0:3afcd581558d 578
pmallick 0:3afcd581558d 579 return -EINVAL;
pmallick 0:3afcd581558d 580 }
pmallick 0:3afcd581558d 581
pmallick 0:3afcd581558d 582
pmallick 0:3afcd581558d 583 /*!
pmallick 0:3afcd581558d 584 * @brief Getter/Setter for the channel range attribute value
pmallick 0:3afcd581558d 585 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 586 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 587 * @param len- length of buffer string data
pmallick 0:3afcd581558d 588 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 589 * @return Number of characters read/written
pmallick 0:3afcd581558d 590 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 591 */
pmallick 0:3afcd581558d 592 ssize_t get_chn_range(void *device,
pmallick 0:3afcd581558d 593 char *buf,
pmallick 0:3afcd581558d 594 size_t len,
pmallick 0:3afcd581558d 595 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 596 {
pmallick 0:3afcd581558d 597 uint8_t read_val;
pmallick 0:3afcd581558d 598 uint8_t chn_range;
pmallick 0:3afcd581558d 599
pmallick 0:3afcd581558d 600 if (ad7606_spi_reg_read(device, AD7606_REG_RANGE_CH_ADDR(channel->ch_num-1),
pmallick 0:3afcd581558d 601 &read_val) == SUCCESS) {
pmallick 0:3afcd581558d 602 if (((channel->ch_num - 1) % 2) != 0) {
pmallick 0:3afcd581558d 603 read_val >>= CHANNEL_RANGE_MSK_OFFSET;
pmallick 0:3afcd581558d 604 chn_range = read_val;
pmallick 0:3afcd581558d 605 } else {
pmallick 0:3afcd581558d 606 chn_range = (read_val & AD7606_RANGE_CH_MSK(channel->ch_num - 1));
pmallick 0:3afcd581558d 607 }
pmallick 0:3afcd581558d 608
pmallick 0:3afcd581558d 609 if (chn_range < sizeof(chn_range_str) / sizeof(chn_range_str[0])) {
pmallick 0:3afcd581558d 610 return (ssize_t)sprintf(buf, "%s", chn_range_str[chn_range]);
pmallick 0:3afcd581558d 611 }
pmallick 0:3afcd581558d 612 }
pmallick 0:3afcd581558d 613
pmallick 0:3afcd581558d 614 return -EINVAL;
pmallick 0:3afcd581558d 615 }
pmallick 0:3afcd581558d 616
pmallick 0:3afcd581558d 617 ssize_t set_chn_range(void *device,
pmallick 0:3afcd581558d 618 char *buf,
pmallick 0:3afcd581558d 619 size_t len,
pmallick 0:3afcd581558d 620 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 621 {
pmallick 0:3afcd581558d 622 uint8_t chn_range;
pmallick 0:3afcd581558d 623
pmallick 0:3afcd581558d 624 (void)sscanf(buf, "%d", &chn_range);
pmallick 0:3afcd581558d 625
pmallick 0:3afcd581558d 626 if (chn_range < sizeof(chn_range_val) / sizeof(chn_range_val[0])) {
pmallick 0:3afcd581558d 627
pmallick 0:3afcd581558d 628 /* Get the polarity of channel */
pmallick 0:3afcd581558d 629 attr_polarity_val[channel->ch_num - 1] = ad7606_get_input_polarity(chn_range);
pmallick 0:3afcd581558d 630
pmallick 0:3afcd581558d 631 attr_chn_range[channel->ch_num - 1] = chn_range_val[chn_range];
pmallick 0:3afcd581558d 632 attr_scale_val[channel->ch_num - 1] = get_vltg_conv_scale_factor(
pmallick 0:3afcd581558d 633 chn_range_val[chn_range],
pmallick 0:3afcd581558d 634 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 635
pmallick 0:3afcd581558d 636 if (((channel->ch_num - 1) % 2) != 0) {
pmallick 0:3afcd581558d 637 chn_range <<= CHANNEL_RANGE_MSK_OFFSET;
pmallick 0:3afcd581558d 638 }
pmallick 0:3afcd581558d 639
pmallick 0:3afcd581558d 640 if (ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 641 AD7606_REG_RANGE_CH_ADDR(channel->ch_num-1),
pmallick 0:3afcd581558d 642 AD7606_RANGE_CH_MSK(channel->ch_num-1),
pmallick 0:3afcd581558d 643 chn_range) == SUCCESS) {
pmallick 0:3afcd581558d 644 return len;
pmallick 0:3afcd581558d 645 }
pmallick 0:3afcd581558d 646 }
pmallick 0:3afcd581558d 647
pmallick 0:3afcd581558d 648 return -EINVAL;
pmallick 0:3afcd581558d 649 }
pmallick 0:3afcd581558d 650
pmallick 0:3afcd581558d 651
pmallick 0:3afcd581558d 652 /*!
pmallick 0:3afcd581558d 653 * @brief Getter/Setter for the channel gain attribute value
pmallick 0:3afcd581558d 654 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 655 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 656 * @param len- length of buffer string data
pmallick 0:3afcd581558d 657 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 658 * @return Number of characters read/written
pmallick 0:3afcd581558d 659 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 660 */
pmallick 0:3afcd581558d 661 ssize_t get_chn_gain(void *device,
pmallick 0:3afcd581558d 662 char *buf,
pmallick 0:3afcd581558d 663 size_t len,
pmallick 0:3afcd581558d 664 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 665 {
pmallick 0:3afcd581558d 666 uint8_t read_val;
pmallick 0:3afcd581558d 667 uint8_t chn_gain_value;
pmallick 0:3afcd581558d 668
pmallick 0:3afcd581558d 669 if (ad7606_spi_reg_read(device,
pmallick 0:3afcd581558d 670 AD7606_REG_GAIN_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 671 &read_val) == SUCCESS) {
pmallick 0:3afcd581558d 672 chn_gain_value = (read_val & AD7606_GAIN_MSK);
pmallick 0:3afcd581558d 673 return (ssize_t)sprintf(buf, "%d", chn_gain_value);
pmallick 0:3afcd581558d 674 }
pmallick 0:3afcd581558d 675
pmallick 0:3afcd581558d 676 return -EINVAL;
pmallick 0:3afcd581558d 677 }
pmallick 0:3afcd581558d 678
pmallick 0:3afcd581558d 679 ssize_t set_chn_gain(void *device,
pmallick 0:3afcd581558d 680 char *buf,
pmallick 0:3afcd581558d 681 size_t len,
pmallick 0:3afcd581558d 682 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 683 {
pmallick 0:3afcd581558d 684 uint8_t chn_gain_value;
pmallick 0:3afcd581558d 685
pmallick 0:3afcd581558d 686 (void)sscanf(buf, "%d", &chn_gain_value);
pmallick 0:3afcd581558d 687
pmallick 0:3afcd581558d 688 if (ad7606_set_ch_gain(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 689 chn_gain_value) == SUCCESS) {
pmallick 0:3afcd581558d 690 return len;
pmallick 0:3afcd581558d 691 }
pmallick 0:3afcd581558d 692
pmallick 0:3afcd581558d 693 return -EINVAL;
pmallick 0:3afcd581558d 694 }
pmallick 0:3afcd581558d 695
pmallick 0:3afcd581558d 696
pmallick 0:3afcd581558d 697 /*!
pmallick 0:3afcd581558d 698 * @brief Getter/Setter for the channel offset attribute value
pmallick 0:3afcd581558d 699 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 700 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 701 * @param len- length of buffer string data
pmallick 0:3afcd581558d 702 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 703 * @return Number of characters read/written
pmallick 0:3afcd581558d 704 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 705 */
pmallick 0:3afcd581558d 706 ssize_t get_chn_offset(void *device,
pmallick 0:3afcd581558d 707 char *buf,
pmallick 0:3afcd581558d 708 size_t len,
pmallick 0:3afcd581558d 709 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 710 {
pmallick 0:3afcd581558d 711 uint8_t chn_offset_value;
pmallick 0:3afcd581558d 712
pmallick 0:3afcd581558d 713 if (ad7606_spi_reg_read(device, AD7606_REG_OFFSET_CH(channel->ch_num-1),
pmallick 0:3afcd581558d 714 &chn_offset_value) == SUCCESS) {
pmallick 0:3afcd581558d 715 return (ssize_t)sprintf(buf, "%d", chn_offset_value);
pmallick 0:3afcd581558d 716 }
pmallick 0:3afcd581558d 717
pmallick 0:3afcd581558d 718 return -EINVAL;
pmallick 0:3afcd581558d 719 }
pmallick 0:3afcd581558d 720
pmallick 0:3afcd581558d 721 ssize_t set_chn_offset(void *device,
pmallick 0:3afcd581558d 722 char *buf,
pmallick 0:3afcd581558d 723 size_t len,
pmallick 0:3afcd581558d 724 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 725 {
pmallick 0:3afcd581558d 726 uint8_t chn_offset_value = 0;
pmallick 0:3afcd581558d 727
pmallick 0:3afcd581558d 728 (void)sscanf(buf, "%d", &chn_offset_value);
pmallick 0:3afcd581558d 729
pmallick 0:3afcd581558d 730 if (ad7606_set_ch_offset(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 731 chn_offset_value) == SUCCESS) {
pmallick 0:3afcd581558d 732 return len;
pmallick 0:3afcd581558d 733 }
pmallick 0:3afcd581558d 734
pmallick 0:3afcd581558d 735 return -EINVAL;
pmallick 0:3afcd581558d 736 }
pmallick 0:3afcd581558d 737
pmallick 0:3afcd581558d 738
pmallick 0:3afcd581558d 739 /*!
pmallick 0:3afcd581558d 740 * @brief Getter/Setter for the channel pahse offset attribute value
pmallick 0:3afcd581558d 741 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 742 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 743 * @param len- length of buffer string data
pmallick 0:3afcd581558d 744 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 745 * @return Number of characters read/written
pmallick 0:3afcd581558d 746 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 747 */
pmallick 0:3afcd581558d 748 ssize_t get_chn_phase_offset(void *device,
pmallick 0:3afcd581558d 749 char *buf,
pmallick 0:3afcd581558d 750 size_t len,
pmallick 0:3afcd581558d 751 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 752 {
pmallick 0:3afcd581558d 753 uint8_t chn_phase_offset_value;
pmallick 0:3afcd581558d 754
pmallick 0:3afcd581558d 755 if (ad7606_spi_reg_read(device,
pmallick 0:3afcd581558d 756 AD7606_REG_PHASE_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 757 &chn_phase_offset_value) == SUCCESS) {
pmallick 0:3afcd581558d 758 return (ssize_t)sprintf(buf, "%d", chn_phase_offset_value);
pmallick 0:3afcd581558d 759 }
pmallick 0:3afcd581558d 760
pmallick 0:3afcd581558d 761 return -EINVAL;
pmallick 0:3afcd581558d 762 }
pmallick 0:3afcd581558d 763
pmallick 0:3afcd581558d 764 ssize_t set_chn_phase_offset(void *device,
pmallick 0:3afcd581558d 765 char *buf,
pmallick 0:3afcd581558d 766 size_t len,
pmallick 0:3afcd581558d 767 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 768 {
pmallick 0:3afcd581558d 769 uint8_t chn_phase_offset_value = 0;
pmallick 0:3afcd581558d 770
pmallick 0:3afcd581558d 771 (void)sscanf(buf, "%d", &chn_phase_offset_value);
pmallick 0:3afcd581558d 772
pmallick 0:3afcd581558d 773 if (ad7606_set_ch_phase(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 774 chn_phase_offset_value) == SUCCESS) {
pmallick 0:3afcd581558d 775 return len;
pmallick 0:3afcd581558d 776 }
pmallick 0:3afcd581558d 777
pmallick 0:3afcd581558d 778 return -EINVAL;
pmallick 0:3afcd581558d 779 }
pmallick 0:3afcd581558d 780
pmallick 0:3afcd581558d 781
pmallick 0:3afcd581558d 782 /*!
pmallick 0:3afcd581558d 783 * @brief Getter/Setter for the channel temperature attribute value
pmallick 0:3afcd581558d 784 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 785 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 786 * @param len- length of buffer string data
pmallick 0:3afcd581558d 787 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 788 * @return Number of characters read/written
pmallick 0:3afcd581558d 789 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 790 */
pmallick 0:3afcd581558d 791 ssize_t get_chn_temperature(void *device,
pmallick 0:3afcd581558d 792 char *buf,
pmallick 0:3afcd581558d 793 size_t len,
pmallick 0:3afcd581558d 794 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 795 {
pmallick 0:3afcd581558d 796 int32_t adc_chn_data = 0;
pmallick 0:3afcd581558d 797 float temperature;
pmallick 0:3afcd581558d 798 float voltage;
pmallick 0:3afcd581558d 799
pmallick 0:3afcd581558d 800 /* Configure the channel multiplexer to select temperature read */
pmallick 0:3afcd581558d 801 if (ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 802 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 803 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 804 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 805 TEMPERATURE_MUX)) == SUCCESS) {
pmallick 0:3afcd581558d 806
pmallick 0:3afcd581558d 807 /* Allow to settle Mux channel */
pmallick 0:3afcd581558d 808 udelay(100);
pmallick 0:3afcd581558d 809
pmallick 0:3afcd581558d 810 /* Sample the channel and read conversion result */
pmallick 0:3afcd581558d 811 adc_chn_data = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 812 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 813
pmallick 0:3afcd581558d 814 /* Convert ADC data into equivalent voltage */
pmallick 0:3afcd581558d 815 voltage = convert_adc_raw_to_voltage(adc_chn_data,
pmallick 0:3afcd581558d 816 attr_scale_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 817
pmallick 0:3afcd581558d 818 /* Obtain the temperature using equation specified in device datasheet */
pmallick 0:3afcd581558d 819 temperature = ((voltage - 0.69068) / 0.019328) + 25.0;
pmallick 0:3afcd581558d 820
pmallick 0:3afcd581558d 821 /* Change channel mux back to analog input */
pmallick 0:3afcd581558d 822 (void)ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 823 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 824 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 825 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 826 ANALOG_INPUT_MUX));
pmallick 0:3afcd581558d 827
pmallick 0:3afcd581558d 828 return (ssize_t)sprintf(buf, "%f", temperature);
pmallick 0:3afcd581558d 829 }
pmallick 0:3afcd581558d 830
pmallick 0:3afcd581558d 831 return -EINVAL;
pmallick 0:3afcd581558d 832 }
pmallick 0:3afcd581558d 833
pmallick 0:3afcd581558d 834 ssize_t set_chn_temperature(void *device,
pmallick 0:3afcd581558d 835 char *buf,
pmallick 0:3afcd581558d 836 size_t len,
pmallick 0:3afcd581558d 837 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 838 {
pmallick 0:3afcd581558d 839 // NA- Can't set temperature
pmallick 0:3afcd581558d 840 return -EINVAL;
pmallick 0:3afcd581558d 841 }
pmallick 0:3afcd581558d 842
pmallick 0:3afcd581558d 843
pmallick 0:3afcd581558d 844 /*!
pmallick 0:3afcd581558d 845 * @brief Getter/Setter for the channel Vref attribute value
pmallick 0:3afcd581558d 846 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 847 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 848 * @param len- length of buffer string data
pmallick 0:3afcd581558d 849 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 850 * @return Number of characters read/written
pmallick 0:3afcd581558d 851 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 852 */
pmallick 0:3afcd581558d 853 ssize_t get_chn_vref(void *device,
pmallick 0:3afcd581558d 854 char *buf,
pmallick 0:3afcd581558d 855 size_t len,
pmallick 0:3afcd581558d 856 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 857 {
pmallick 0:3afcd581558d 858 float vref_voltge;
pmallick 0:3afcd581558d 859 int32_t adc_chn_data;
pmallick 0:3afcd581558d 860
pmallick 0:3afcd581558d 861 /* Configure the channel multiplexer to select Vref read */
pmallick 0:3afcd581558d 862 if (ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 863 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 864 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 865 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 866 VREF_MUX)) == SUCCESS) {
pmallick 0:3afcd581558d 867
pmallick 0:3afcd581558d 868 /* Allow to settle Mux channel */
pmallick 0:3afcd581558d 869 udelay(100);
pmallick 0:3afcd581558d 870
pmallick 0:3afcd581558d 871 /* Sample the channel and read conversion result */
pmallick 0:3afcd581558d 872 adc_chn_data = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 873 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 874
pmallick 0:3afcd581558d 875 /* Convert ADC data into equivalent voltage */
pmallick 0:3afcd581558d 876 vref_voltge = convert_adc_raw_to_voltage(adc_chn_data,
pmallick 0:3afcd581558d 877 attr_scale_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 878
pmallick 0:3afcd581558d 879 /* Divide by 4 since Vref Mux has 4x multiplier on it */
pmallick 0:3afcd581558d 880 vref_voltge /= VREF_MUX_MULTIPLIER;
pmallick 0:3afcd581558d 881
pmallick 0:3afcd581558d 882 /* Change channel mux back to analog input */
pmallick 0:3afcd581558d 883 (void)ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 884 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 885 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 886 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 887 ANALOG_INPUT_MUX));
pmallick 0:3afcd581558d 888
pmallick 0:3afcd581558d 889 return (ssize_t)sprintf(buf, "%f", vref_voltge);
pmallick 0:3afcd581558d 890 }
pmallick 0:3afcd581558d 891
pmallick 0:3afcd581558d 892 return -EINVAL;
pmallick 0:3afcd581558d 893 }
pmallick 0:3afcd581558d 894
pmallick 0:3afcd581558d 895 ssize_t set_chn_vref(void *device,
pmallick 0:3afcd581558d 896 char *buf,
pmallick 0:3afcd581558d 897 size_t len,
pmallick 0:3afcd581558d 898 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 899 {
pmallick 0:3afcd581558d 900 // NA- Can't set Vref
pmallick 0:3afcd581558d 901 return - EINVAL;
pmallick 0:3afcd581558d 902 }
pmallick 0:3afcd581558d 903
pmallick 0:3afcd581558d 904
pmallick 0:3afcd581558d 905 /*!
pmallick 0:3afcd581558d 906 * @brief Getter/Setter for the channel Vdrive attribute value
pmallick 0:3afcd581558d 907 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 908 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 909 * @param len- length of buffer string data
pmallick 0:3afcd581558d 910 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 911 * @return Number of characters read/written
pmallick 0:3afcd581558d 912 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 913 */
pmallick 0:3afcd581558d 914 ssize_t get_chn_vdrive(void *device,
pmallick 0:3afcd581558d 915 char *buf,
pmallick 0:3afcd581558d 916 size_t len,
pmallick 0:3afcd581558d 917 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 918 {
pmallick 0:3afcd581558d 919 float vdrive_voltge;
pmallick 0:3afcd581558d 920 int32_t adc_chn_data;
pmallick 0:3afcd581558d 921
pmallick 0:3afcd581558d 922 /* Configure the channel multiplexer to select Vdrive read */
pmallick 0:3afcd581558d 923 if (ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 924 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 925 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 926 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 927 VDRIVE_MUX)) == SUCCESS) {
pmallick 0:3afcd581558d 928
pmallick 0:3afcd581558d 929 /* Allow to settle Mux channel */
pmallick 0:3afcd581558d 930 udelay(100);
pmallick 0:3afcd581558d 931
pmallick 0:3afcd581558d 932 /* Sample the channel and read conversion result */
pmallick 0:3afcd581558d 933 adc_chn_data = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 934 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 935
pmallick 0:3afcd581558d 936 /* Convert ADC data into equivalent voltage */
pmallick 0:3afcd581558d 937 vdrive_voltge = convert_adc_raw_to_voltage(adc_chn_data,
pmallick 0:3afcd581558d 938 attr_scale_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 939
pmallick 0:3afcd581558d 940 /* Change channel mux back to analog input */
pmallick 0:3afcd581558d 941 (void)ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 942 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 943 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 944 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 945 ANALOG_INPUT_MUX));
pmallick 0:3afcd581558d 946
pmallick 0:3afcd581558d 947 return (ssize_t)sprintf(buf, "%f", vdrive_voltge);
pmallick 0:3afcd581558d 948 }
pmallick 0:3afcd581558d 949
pmallick 0:3afcd581558d 950 return -EINVAL;
pmallick 0:3afcd581558d 951 }
pmallick 0:3afcd581558d 952
pmallick 0:3afcd581558d 953 ssize_t set_chn_vdrive(void *device,
pmallick 0:3afcd581558d 954 char *buf,
pmallick 0:3afcd581558d 955 size_t len,
pmallick 0:3afcd581558d 956 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 957 {
pmallick 0:3afcd581558d 958 // NA- Can't set Vdrive
pmallick 0:3afcd581558d 959 return - EINVAL;
pmallick 0:3afcd581558d 960 }
pmallick 0:3afcd581558d 961
pmallick 0:3afcd581558d 962
pmallick 0:3afcd581558d 963 /*!
pmallick 0:3afcd581558d 964 * @brief Getter/Setter for the channel ALDO attribute value
pmallick 0:3afcd581558d 965 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 966 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 967 * @param len- length of buffer string data
pmallick 0:3afcd581558d 968 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 969 * @return Number of characters read/written
pmallick 0:3afcd581558d 970 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 971 */
pmallick 0:3afcd581558d 972 ssize_t get_chn_aldo(void *device,
pmallick 0:3afcd581558d 973 char *buf,
pmallick 0:3afcd581558d 974 size_t len,
pmallick 0:3afcd581558d 975 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 976 {
pmallick 0:3afcd581558d 977 float aldo_voltge;
pmallick 0:3afcd581558d 978 int32_t adc_chn_data;
pmallick 0:3afcd581558d 979
pmallick 0:3afcd581558d 980 /* Configure the channel multiplexer to select ALDO read */
pmallick 0:3afcd581558d 981 if (ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 982 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 983 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 984 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 985 ALDO_MUX)) == SUCCESS) {
pmallick 0:3afcd581558d 986
pmallick 0:3afcd581558d 987 /* Allow to settle Mux channel */
pmallick 0:3afcd581558d 988 udelay(100);
pmallick 0:3afcd581558d 989
pmallick 0:3afcd581558d 990 /* Sample the channel and read conversion result */
pmallick 0:3afcd581558d 991 adc_chn_data = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 992 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 993
pmallick 0:3afcd581558d 994 /* Convert ADC data into equivalent voltage */
pmallick 0:3afcd581558d 995 aldo_voltge = convert_adc_raw_to_voltage(adc_chn_data,
pmallick 0:3afcd581558d 996 attr_scale_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 997
pmallick 0:3afcd581558d 998 /* Divide by 4 since ALDO Mux has 4x multiplier on it */
pmallick 0:3afcd581558d 999 aldo_voltge /= VREF_MUX_MULTIPLIER;
pmallick 0:3afcd581558d 1000
pmallick 0:3afcd581558d 1001 /* Change channel mux back to analog input */
pmallick 0:3afcd581558d 1002 (void)ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 1003 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 1004 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 1005 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 1006 ANALOG_INPUT_MUX));
pmallick 0:3afcd581558d 1007
pmallick 0:3afcd581558d 1008 return (ssize_t)sprintf(buf, "%f", aldo_voltge);
pmallick 0:3afcd581558d 1009 }
pmallick 0:3afcd581558d 1010
pmallick 0:3afcd581558d 1011 return -EINVAL;
pmallick 0:3afcd581558d 1012 }
pmallick 0:3afcd581558d 1013
pmallick 0:3afcd581558d 1014 ssize_t set_chn_aldo(void *device,
pmallick 0:3afcd581558d 1015 char *buf,
pmallick 0:3afcd581558d 1016 size_t len,
pmallick 0:3afcd581558d 1017 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1018 {
pmallick 0:3afcd581558d 1019 // NA- Can't set ALDO
pmallick 0:3afcd581558d 1020 return - EINVAL;
pmallick 0:3afcd581558d 1021 }
pmallick 0:3afcd581558d 1022
pmallick 0:3afcd581558d 1023
pmallick 0:3afcd581558d 1024 /*!
pmallick 0:3afcd581558d 1025 * @brief Getter/Setter for the channel DLDO attribute value
pmallick 0:3afcd581558d 1026 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 1027 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 1028 * @param len- length of buffer string data
pmallick 0:3afcd581558d 1029 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 1030 * @return Number of characters read/written
pmallick 0:3afcd581558d 1031 * @note Available only for AD7606B and AD7606C
pmallick 0:3afcd581558d 1032 */
pmallick 0:3afcd581558d 1033 ssize_t get_chn_dldo(void *device,
pmallick 0:3afcd581558d 1034 char *buf,
pmallick 0:3afcd581558d 1035 size_t len,
pmallick 0:3afcd581558d 1036 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1037 {
pmallick 0:3afcd581558d 1038 float dldo_voltge;
pmallick 0:3afcd581558d 1039 int32_t adc_chn_data;
pmallick 0:3afcd581558d 1040
pmallick 0:3afcd581558d 1041 /* Configure the channel multiplexer to select DLDO read */
pmallick 0:3afcd581558d 1042 if (ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 1043 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 1044 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 1045 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 1046 DLDO_MUX)) == SUCCESS) {
pmallick 0:3afcd581558d 1047
pmallick 0:3afcd581558d 1048 /* Allow to settle Mux channel */
pmallick 0:3afcd581558d 1049 udelay(100);
pmallick 0:3afcd581558d 1050
pmallick 0:3afcd581558d 1051 /* Sample the channel and read conversion result */
pmallick 0:3afcd581558d 1052 adc_chn_data = single_data_read(device,
pmallick 0:3afcd581558d 1053 channel->ch_num - 1,
pmallick 0:3afcd581558d 1054 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 1055
pmallick 0:3afcd581558d 1056 /* Convert ADC data into equivalent voltage */
pmallick 0:3afcd581558d 1057 dldo_voltge = convert_adc_raw_to_voltage(adc_chn_data,
pmallick 0:3afcd581558d 1058 attr_scale_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 1059
pmallick 0:3afcd581558d 1060 /* Divide by 4 since ALDO Mux has 4x multiplier on it */
pmallick 0:3afcd581558d 1061 dldo_voltge /= VREF_MUX_MULTIPLIER;
pmallick 0:3afcd581558d 1062
pmallick 0:3afcd581558d 1063 /* Change channel mux back to analog input */
pmallick 0:3afcd581558d 1064 (void)ad7606_spi_write_mask(device,
pmallick 0:3afcd581558d 1065 AD7606_REG_DIAGNOSTIC_MUX_CH(channel->ch_num - 1),
pmallick 0:3afcd581558d 1066 AD7606_DIAGN_MUX_CH_MSK(channel->ch_num - 1),
pmallick 0:3afcd581558d 1067 AD7606_DIAGN_MUX_CH_VAL((channel->ch_num - 1),
pmallick 0:3afcd581558d 1068 ANALOG_INPUT_MUX));
pmallick 0:3afcd581558d 1069
pmallick 0:3afcd581558d 1070 return (ssize_t)sprintf(buf, "%f", dldo_voltge);
pmallick 0:3afcd581558d 1071 }
pmallick 0:3afcd581558d 1072
pmallick 0:3afcd581558d 1073 return -EINVAL;
pmallick 0:3afcd581558d 1074 }
pmallick 0:3afcd581558d 1075
pmallick 0:3afcd581558d 1076 ssize_t set_chn_dldo(void *device,
pmallick 0:3afcd581558d 1077 char *buf,
pmallick 0:3afcd581558d 1078 size_t len,
pmallick 0:3afcd581558d 1079 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1080 {
pmallick 0:3afcd581558d 1081 // NA- Can't set DLDO
pmallick 0:3afcd581558d 1082 return - EINVAL;
pmallick 0:3afcd581558d 1083 }
pmallick 0:3afcd581558d 1084
pmallick 0:3afcd581558d 1085
pmallick 0:3afcd581558d 1086 /*!
pmallick 0:3afcd581558d 1087 * @brief Getter/Setter for the channel open wire detect attribute value
pmallick 0:3afcd581558d 1088 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 1089 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 1090 * @param len- length of buffer string data
pmallick 0:3afcd581558d 1091 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 1092 * @return Number of characters read/written
pmallick 0:3afcd581558d 1093 */
pmallick 0:3afcd581558d 1094 ssize_t get_chn_open_wire_detect(void *device,
pmallick 0:3afcd581558d 1095 char *buf,
pmallick 0:3afcd581558d 1096 size_t len,
pmallick 0:3afcd581558d 1097 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1098 {
pmallick 0:3afcd581558d 1099 int32_t data[2];
pmallick 0:3afcd581558d 1100 uint8_t open_detect_flag = false;
pmallick 0:3afcd581558d 1101 int32_t rw_status = FAILURE;
pmallick 0:3afcd581558d 1102
pmallick 0:3afcd581558d 1103 #if defined MANUAL_OPEN_DETECT
pmallick 0:3afcd581558d 1104 /* Read the ADC output voltage on channel */
pmallick 0:3afcd581558d 1105 data[0] = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 1106 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 1107
pmallick 0:3afcd581558d 1108 /* Enter into manual open detect mode */
pmallick 0:3afcd581558d 1109 if (ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_QUEUE, 1) == SUCCESS) {
pmallick 0:3afcd581558d 1110 /* Enable open wire detection on selected channel */
pmallick 0:3afcd581558d 1111 if (ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_ENABLE,
pmallick 0:3afcd581558d 1112 (1 << ((channel->ch_num) - 1))) == SUCCESS) {
pmallick 0:3afcd581558d 1113 /* Read the ADC output after open wire detection */
pmallick 0:3afcd581558d 1114 data[1] = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 1115 attr_polarity_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 1116
pmallick 0:3afcd581558d 1117 /* Check for shift in common mode output voltage */
pmallick 0:3afcd581558d 1118 if ((data[1] - data[0]) > OPEN_DETECT_THRESHOLD_RPD50K) {
pmallick 0:3afcd581558d 1119 open_detect_flag = true;
pmallick 0:3afcd581558d 1120 }
pmallick 0:3afcd581558d 1121
pmallick 0:3afcd581558d 1122 rw_status = SUCCESS;
pmallick 0:3afcd581558d 1123 }
pmallick 0:3afcd581558d 1124 }
pmallick 0:3afcd581558d 1125 #else
pmallick 0:3afcd581558d 1126 /* Enter into auto open detect mode */
pmallick 0:3afcd581558d 1127 if (ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_QUEUE,
pmallick 0:3afcd581558d 1128 OPEN_DETECT_AUTO_MODE_CNTS) == SUCCESS) {
pmallick 0:3afcd581558d 1129 /* Enable open wire detection on selected channel */
pmallick 0:3afcd581558d 1130 if (ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_ENABLE,
pmallick 0:3afcd581558d 1131 (1 << ((channel->ch_num) - 1))) == SUCCESS) {
pmallick 0:3afcd581558d 1132 mdelay(1);
pmallick 0:3afcd581558d 1133
pmallick 0:3afcd581558d 1134 if (ad7606_spi_reg_read(device, AD7606_REG_OPEN_DETECTED,
pmallick 0:3afcd581558d 1135 &open_detect_flag) == SUCCESS) {
pmallick 0:3afcd581558d 1136 rw_status = SUCCESS;
pmallick 0:3afcd581558d 1137
pmallick 0:3afcd581558d 1138 open_detect_flag >>= (channel->ch_num -1);
pmallick 0:3afcd581558d 1139 open_detect_flag &= 0x1;
pmallick 0:3afcd581558d 1140 }
pmallick 0:3afcd581558d 1141 }
pmallick 0:3afcd581558d 1142 }
pmallick 0:3afcd581558d 1143 #endif
pmallick 0:3afcd581558d 1144
pmallick 0:3afcd581558d 1145 /* Disable open detect mode */
pmallick 0:3afcd581558d 1146 (void)ad7606_spi_reg_write(device, AD7606_REG_OPEN_DETECT_QUEUE, 0);
pmallick 0:3afcd581558d 1147
pmallick 0:3afcd581558d 1148 if (rw_status == SUCCESS) {
pmallick 0:3afcd581558d 1149 if (open_detect_flag) {
pmallick 0:3afcd581558d 1150 strcpy(buf, "Open Wire Detected");
pmallick 0:3afcd581558d 1151 } else {
pmallick 0:3afcd581558d 1152 strcpy(buf, "Open Wire Not Detected");
pmallick 0:3afcd581558d 1153 }
pmallick 0:3afcd581558d 1154
pmallick 0:3afcd581558d 1155 return len;
pmallick 0:3afcd581558d 1156 }
pmallick 0:3afcd581558d 1157
pmallick 0:3afcd581558d 1158 return -EINVAL;
pmallick 0:3afcd581558d 1159 }
pmallick 0:3afcd581558d 1160
pmallick 0:3afcd581558d 1161 ssize_t set_chn_open_wire_detect(void *device,
pmallick 0:3afcd581558d 1162 char *buf,
pmallick 0:3afcd581558d 1163 size_t len,
pmallick 0:3afcd581558d 1164 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1165 {
pmallick 0:3afcd581558d 1166 // NA- Can't set open wire detect
pmallick 0:3afcd581558d 1167 return - EINVAL;
pmallick 0:3afcd581558d 1168 }
pmallick 0:3afcd581558d 1169
pmallick 0:3afcd581558d 1170
pmallick 0:3afcd581558d 1171 /*!
pmallick 0:3afcd581558d 1172 * @brief Getter/Setter for the adc calibration
pmallick 0:3afcd581558d 1173 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 1174 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 1175 * @param len- length of buffer string data
pmallick 0:3afcd581558d 1176 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 1177 * @return Number of characters read/written
pmallick 0:3afcd581558d 1178 */
pmallick 0:3afcd581558d 1179 ssize_t get_chn_calibrate_adc(void *device,
pmallick 0:3afcd581558d 1180 char *buf,
pmallick 0:3afcd581558d 1181 size_t len,
pmallick 0:3afcd581558d 1182 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1183 {
pmallick 0:3afcd581558d 1184 float lsb_voltage;
pmallick 0:3afcd581558d 1185 float adc_voltage;
pmallick 0:3afcd581558d 1186 polarity_e polarity = attr_polarity_val[channel->ch_num - 1];
pmallick 0:3afcd581558d 1187 int32_t adc_data;
pmallick 0:3afcd581558d 1188 int8_t chn_offset;
pmallick 0:3afcd581558d 1189
pmallick 0:3afcd581558d 1190 /* Perform the system offset calibration */
pmallick 0:3afcd581558d 1191
pmallick 0:3afcd581558d 1192 if (polarity == UNIPOLAR) {
pmallick 0:3afcd581558d 1193 lsb_voltage = attr_chn_range[channel->ch_num - 1] / ADC_MAX_COUNT_UNIPOLAR;
pmallick 0:3afcd581558d 1194 } else {
pmallick 0:3afcd581558d 1195 lsb_voltage = attr_chn_range[channel->ch_num - 1] / ADC_MAX_COUNT_BIPOLAR;
pmallick 0:3afcd581558d 1196 }
pmallick 0:3afcd581558d 1197
pmallick 0:3afcd581558d 1198 /* Sample and read the ADC channel */
pmallick 0:3afcd581558d 1199 adc_data = single_data_read(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 1200 polarity);
pmallick 0:3afcd581558d 1201
pmallick 0:3afcd581558d 1202 /* Get an equivalent ADC voltage */
pmallick 0:3afcd581558d 1203 adc_voltage = convert_adc_raw_to_voltage(adc_data,
pmallick 0:3afcd581558d 1204 attr_scale_val[channel->ch_num - 1]);
pmallick 0:3afcd581558d 1205
pmallick 0:3afcd581558d 1206 /* Calculate the channel offset and write it to offset register (0x80-chn_offset) */
pmallick 0:3afcd581558d 1207 chn_offset = (adc_voltage / lsb_voltage);
pmallick 0:3afcd581558d 1208
pmallick 0:3afcd581558d 1209 if (ad7606_set_ch_offset(device, channel->ch_num - 1,
pmallick 0:3afcd581558d 1210 chn_offset) == SUCCESS) {
pmallick 0:3afcd581558d 1211 return sprintf(buf, "%s", "ADC Calibration Done");
pmallick 0:3afcd581558d 1212 }
pmallick 0:3afcd581558d 1213
pmallick 0:3afcd581558d 1214 return -EINVAL;
pmallick 0:3afcd581558d 1215 }
pmallick 0:3afcd581558d 1216
pmallick 0:3afcd581558d 1217 ssize_t set_chn_calibrate_adc(void *device,
pmallick 0:3afcd581558d 1218 char *buf,
pmallick 0:3afcd581558d 1219 size_t len,
pmallick 0:3afcd581558d 1220 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1221 {
pmallick 0:3afcd581558d 1222 // NA- Can't set open wire detect
pmallick 0:3afcd581558d 1223 return - EINVAL;
pmallick 0:3afcd581558d 1224 }
pmallick 0:3afcd581558d 1225
pmallick 0:3afcd581558d 1226
pmallick 0:3afcd581558d 1227 /*!
pmallick 0:3afcd581558d 1228 * @brief Getter/Setter for the direct register access attribute value
pmallick 0:3afcd581558d 1229 * @param device- pointer to IIO device structure
pmallick 0:3afcd581558d 1230 * @param buf- pointer to buffer holding attribute value
pmallick 0:3afcd581558d 1231 * @param len- length of buffer string data
pmallick 0:3afcd581558d 1232 * @param channel- pointer to IIO channel structure
pmallick 0:3afcd581558d 1233 * @return Number of characters read/written
pmallick 0:3afcd581558d 1234 */
pmallick 0:3afcd581558d 1235 ssize_t get_direct_reg_access(void *device,
pmallick 0:3afcd581558d 1236 char *buf,
pmallick 0:3afcd581558d 1237 size_t len,
pmallick 0:3afcd581558d 1238 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1239 {
pmallick 0:3afcd581558d 1240 return (ssize_t)sprintf(buf, "%d", device_reg_val);
pmallick 0:3afcd581558d 1241 }
pmallick 0:3afcd581558d 1242
pmallick 0:3afcd581558d 1243 ssize_t set_direct_reg_access(void *device,
pmallick 0:3afcd581558d 1244 char *buf,
pmallick 0:3afcd581558d 1245 size_t len,
pmallick 0:3afcd581558d 1246 const struct iio_ch_info *channel)
pmallick 0:3afcd581558d 1247 {
pmallick 0:3afcd581558d 1248 uint8_t reg_address;
pmallick 0:3afcd581558d 1249 char *token;
pmallick 0:3afcd581558d 1250 uint8_t offset = strlen("0x");
pmallick 0:3afcd581558d 1251 char str[10] = "";
pmallick 0:3afcd581558d 1252 uint8_t i=0;
pmallick 0:3afcd581558d 1253 uint8_t reg_data;
pmallick 0:3afcd581558d 1254
pmallick 0:3afcd581558d 1255 if (buf[1] == 'x') {
pmallick 0:3afcd581558d 1256 /* Write a data to device */
pmallick 0:3afcd581558d 1257
pmallick 0:3afcd581558d 1258 /* Extract the register address from received string */
pmallick 0:3afcd581558d 1259 strcpy(str, buf);
pmallick 0:3afcd581558d 1260 token = strtok(str, " ");
pmallick 0:3afcd581558d 1261 (void)sscanf(token + offset, "%x", &reg_address);
pmallick 0:3afcd581558d 1262
pmallick 0:3afcd581558d 1263 /* Extract the register data from received string */
pmallick 0:3afcd581558d 1264 i = (strlen(str) + 1) + offset;
pmallick 0:3afcd581558d 1265 (void)sscanf(str + i, "%x", &reg_data);
pmallick 0:3afcd581558d 1266
pmallick 0:3afcd581558d 1267 if (reg_address <= NUM_OF_REGISTERS) {
pmallick 0:3afcd581558d 1268 if ((ad7606_spi_reg_write(device, reg_address, reg_data) == SUCCESS)) {
pmallick 0:3afcd581558d 1269 return len;
pmallick 0:3afcd581558d 1270 }
pmallick 0:3afcd581558d 1271 }
pmallick 0:3afcd581558d 1272 } else {
pmallick 0:3afcd581558d 1273 /* Read the data from device */
pmallick 0:3afcd581558d 1274 (void)sscanf(buf, "%d", &reg_address);
pmallick 0:3afcd581558d 1275 if ((ad7606_spi_reg_read(device, reg_address, &device_reg_val) == SUCCESS)) {
pmallick 0:3afcd581558d 1276 return len;
pmallick 0:3afcd581558d 1277 }
pmallick 0:3afcd581558d 1278 }
pmallick 0:3afcd581558d 1279
pmallick 0:3afcd581558d 1280 return -EINVAL;
pmallick 0:3afcd581558d 1281 }
pmallick 0:3afcd581558d 1282
pmallick 0:3afcd581558d 1283
pmallick 0:3afcd581558d 1284 /**
pmallick 0:3afcd581558d 1285 * @brief Get xml corresponding to an AD7606 device.
pmallick 0:3afcd581558d 1286 * @param xml - Xml containing description of a device.
pmallick 0:3afcd581558d 1287 * @param iio_dev - Structure describing a device, channels and attributes.
pmallick 0:3afcd581558d 1288 * @return SUCCESS in case of success or negative value otherwise.
pmallick 0:3afcd581558d 1289 */
pmallick 0:3afcd581558d 1290 static ssize_t iio_ad7606_get_xml(char **xml, struct iio_device *iio_dev)
pmallick 0:3afcd581558d 1291 {
pmallick 0:3afcd581558d 1292 *xml = (char*)calloc(1, strlen(ad7606_phy_xml) + 1);
pmallick 0:3afcd581558d 1293 if (!(*xml)) {
pmallick 0:3afcd581558d 1294 return -ENOMEM;
pmallick 0:3afcd581558d 1295 }
pmallick 0:3afcd581558d 1296
pmallick 0:3afcd581558d 1297 memcpy(*xml, ad7606_phy_xml, strlen(ad7606_phy_xml));
pmallick 0:3afcd581558d 1298
pmallick 0:3afcd581558d 1299 return SUCCESS;
pmallick 0:3afcd581558d 1300 }
pmallick 0:3afcd581558d 1301
pmallick 0:3afcd581558d 1302
pmallick 0:3afcd581558d 1303 /**
pmallick 0:3afcd581558d 1304 * @brief Read buffer data corresponding to AD7606 IIO device
pmallick 0:3afcd581558d 1305 * @param dev_instance[in] - IIO device instance
pmallick 0:3afcd581558d 1306 * @param pbuf[out] - Pointer to output data buffer
pmallick 0:3afcd581558d 1307 * @return SUCCESS in case of success or negative value otherwise
pmallick 0:3afcd581558d 1308 */
pmallick 0:3afcd581558d 1309 static ssize_t iio_ad7606_read_data(void *dev_instance,
pmallick 0:3afcd581558d 1310 char *pbuf,
pmallick 0:3afcd581558d 1311 size_t offset,
pmallick 0:3afcd581558d 1312 size_t bytes_count,
pmallick 0:3afcd581558d 1313 uint32_t ch_mask)
pmallick 0:3afcd581558d 1314 {
pmallick 0:3afcd581558d 1315 /* Read the buffered data */
pmallick 0:3afcd581558d 1316 buffered_data_read(pbuf, bytes_count, offset, ch_mask);
pmallick 0:3afcd581558d 1317
pmallick 0:3afcd581558d 1318 return bytes_count;
pmallick 0:3afcd581558d 1319 }
pmallick 0:3afcd581558d 1320
pmallick 0:3afcd581558d 1321
pmallick 0:3afcd581558d 1322 /**
pmallick 0:3afcd581558d 1323 * @brief Create structure describing a device, channels and attributes
pmallick 0:3afcd581558d 1324 * @param device_name[in] - Device name
pmallick 0:3afcd581558d 1325 * @return iio_device or NULL, in case of failure
pmallick 0:3afcd581558d 1326 */
pmallick 0:3afcd581558d 1327 static struct iio_device *iio_ad7606_create_device(const char *device_name)
pmallick 0:3afcd581558d 1328 {
pmallick 0:3afcd581558d 1329 struct iio_device *iio_ad7606_device;
pmallick 0:3afcd581558d 1330
pmallick 0:3afcd581558d 1331 iio_ad7606_device = calloc(1, sizeof(struct iio_device));
pmallick 0:3afcd581558d 1332 if (!iio_ad7606_device) {
pmallick 0:3afcd581558d 1333 return NULL;
pmallick 0:3afcd581558d 1334 }
pmallick 0:3afcd581558d 1335
pmallick 0:3afcd581558d 1336 iio_ad7606_device->name = device_name;
pmallick 0:3afcd581558d 1337 iio_ad7606_device->num_ch = sizeof(iio_ad7606_channels) / sizeof(
pmallick 0:3afcd581558d 1338 iio_ad7606_channels[0]);
pmallick 0:3afcd581558d 1339 iio_ad7606_device->channels = iio_ad7606_channels;
pmallick 0:3afcd581558d 1340 iio_ad7606_device->attributes = global_attributes;
pmallick 0:3afcd581558d 1341
pmallick 0:3afcd581558d 1342 return iio_ad7606_device;
pmallick 0:3afcd581558d 1343 }
pmallick 0:3afcd581558d 1344
pmallick 0:3afcd581558d 1345
pmallick 0:3afcd581558d 1346 /**
pmallick 0:3afcd581558d 1347 * @brief Delete IIO device.
pmallick 0:3afcd581558d 1348 * @param iio_device - Structure describing a device, channels and attributes
pmallick 0:3afcd581558d 1349 * @return SUCCESS in case of success or negative value otherwise
pmallick 0:3afcd581558d 1350 */
pmallick 0:3afcd581558d 1351 static ssize_t iio_ad7606_delete_device(struct iio_device *iio_device)
pmallick 0:3afcd581558d 1352 {
pmallick 0:3afcd581558d 1353 if (!iio_device) {
pmallick 0:3afcd581558d 1354 return FAILURE;
pmallick 0:3afcd581558d 1355 }
pmallick 0:3afcd581558d 1356
pmallick 0:3afcd581558d 1357 free(iio_device);
pmallick 0:3afcd581558d 1358
pmallick 0:3afcd581558d 1359 return SUCCESS;
pmallick 0:3afcd581558d 1360 }
pmallick 0:3afcd581558d 1361
pmallick 0:3afcd581558d 1362
pmallick 0:3afcd581558d 1363 /**
pmallick 0:3afcd581558d 1364 * @brief Init for reading/writing and parameterization of a
pmallick 0:3afcd581558d 1365 * ad7606 IIO device
pmallick 0:3afcd581558d 1366 * @param desc[in,out] - IIO device descriptor
pmallick 0:3afcd581558d 1367 * @param init[in] - Configuration structure
pmallick 0:3afcd581558d 1368 * @return SUCCESS in case of success, FAILURE otherwise
pmallick 0:3afcd581558d 1369 */
pmallick 0:3afcd581558d 1370 int32_t iio_ad7606_init(struct iio_ad7606_desc **desc,
pmallick 0:3afcd581558d 1371 struct iio_ad7606_init_param *init)
pmallick 0:3afcd581558d 1372 {
pmallick 0:3afcd581558d 1373 int32_t status;
pmallick 0:3afcd581558d 1374 struct iio_interface *iio_interface;
pmallick 0:3afcd581558d 1375
pmallick 0:3afcd581558d 1376 iio_interface = (struct iio_interface *)calloc(1, sizeof(struct iio_interface));
pmallick 0:3afcd581558d 1377 if (!iio_interface) {
pmallick 0:3afcd581558d 1378 return -ENOMEM;
pmallick 0:3afcd581558d 1379 }
pmallick 0:3afcd581558d 1380
pmallick 0:3afcd581558d 1381 *iio_interface = (struct iio_interface) {
pmallick 0:3afcd581558d 1382 .name = dev_name,
pmallick 0:3afcd581558d 1383 .dev_instance = init->ad7606_phy,
pmallick 0:3afcd581558d 1384 .iio = iio_ad7606_create_device(dev_name),
pmallick 0:3afcd581558d 1385 .get_xml = iio_ad7606_get_xml,
pmallick 0:3afcd581558d 1386 .transfer_dev_to_mem = NULL,
pmallick 0:3afcd581558d 1387 .transfer_mem_to_dev = NULL,
pmallick 0:3afcd581558d 1388 .read_data = iio_ad7606_read_data,
pmallick 0:3afcd581558d 1389 .write_data = NULL,
pmallick 0:3afcd581558d 1390 };
pmallick 0:3afcd581558d 1391
pmallick 0:3afcd581558d 1392 status = iio_register(iio_interface);
pmallick 0:3afcd581558d 1393 if (status < 0) {
pmallick 0:3afcd581558d 1394 free(iio_interface);
pmallick 0:3afcd581558d 1395 return FAILURE;
pmallick 0:3afcd581558d 1396 }
pmallick 0:3afcd581558d 1397
pmallick 0:3afcd581558d 1398 *desc = calloc(1, sizeof(struct iio_ad7606_desc));
pmallick 0:3afcd581558d 1399 if (!(*desc)) {
pmallick 0:3afcd581558d 1400 iio_unregister(iio_interface);
pmallick 0:3afcd581558d 1401 free(iio_interface);
pmallick 0:3afcd581558d 1402 return FAILURE;
pmallick 0:3afcd581558d 1403 }
pmallick 0:3afcd581558d 1404
pmallick 0:3afcd581558d 1405 (*desc)->iio_interface = iio_interface;
pmallick 0:3afcd581558d 1406
pmallick 0:3afcd581558d 1407 return SUCCESS;
pmallick 0:3afcd581558d 1408 }
pmallick 0:3afcd581558d 1409
pmallick 0:3afcd581558d 1410
pmallick 0:3afcd581558d 1411 /**
pmallick 0:3afcd581558d 1412 * @brief Release resources allocated for IIO device
pmallick 0:3afcd581558d 1413 * @param desc[in] - IIO device descriptor
pmallick 0:3afcd581558d 1414 * @return SUCCESS in case of success, FAILURE otherwise
pmallick 0:3afcd581558d 1415 */
pmallick 0:3afcd581558d 1416 int32_t iio_ad7606_remove(struct iio_ad7606_desc *desc)
pmallick 0:3afcd581558d 1417 {
pmallick 0:3afcd581558d 1418 int32_t status;
pmallick 0:3afcd581558d 1419
pmallick 0:3afcd581558d 1420 if (!desc) {
pmallick 0:3afcd581558d 1421 return FAILURE;
pmallick 0:3afcd581558d 1422 }
pmallick 0:3afcd581558d 1423
pmallick 0:3afcd581558d 1424 status = iio_unregister(desc->iio_interface);
pmallick 0:3afcd581558d 1425 if (status < 0) {
pmallick 0:3afcd581558d 1426 return FAILURE;
pmallick 0:3afcd581558d 1427 }
pmallick 0:3afcd581558d 1428
pmallick 0:3afcd581558d 1429 status = iio_ad7606_delete_device(desc->iio_interface->iio);
pmallick 0:3afcd581558d 1430 if (status < 0) {
pmallick 0:3afcd581558d 1431 return FAILURE;
pmallick 0:3afcd581558d 1432 }
pmallick 0:3afcd581558d 1433
pmallick 0:3afcd581558d 1434 free(desc->iio_interface);
pmallick 0:3afcd581558d 1435 free(desc);
pmallick 0:3afcd581558d 1436
pmallick 0:3afcd581558d 1437 return SUCCESS;
pmallick 0:3afcd581558d 1438 }
pmallick 0:3afcd581558d 1439
pmallick 0:3afcd581558d 1440
pmallick 0:3afcd581558d 1441 /*!
pmallick 0:3afcd581558d 1442 * @brief Get scale factor for adc data to voltage conversion for IIO client
pmallick 0:3afcd581558d 1443 * @param chn_range[in] - Current channel voltage range
pmallick 0:3afcd581558d 1444 * @param chn_range_bits[in] - Channel range register bits
pmallick 0:3afcd581558d 1445 * @return scale
pmallick 0:3afcd581558d 1446 * @details This function samples and capture the new data when previous data
pmallick 0:3afcd581558d 1447 * is transmitted to IIO client
pmallick 0:3afcd581558d 1448 */
pmallick 0:3afcd581558d 1449 static float get_vltg_conv_scale_factor(float chn_range, polarity_e polarity)
pmallick 0:3afcd581558d 1450 {
pmallick 0:3afcd581558d 1451 float scale;
pmallick 0:3afcd581558d 1452
pmallick 0:3afcd581558d 1453 /* Get the scale factor for voltage conversion from range */
pmallick 0:3afcd581558d 1454 if (polarity == UNIPOLAR) {
pmallick 0:3afcd581558d 1455 scale = (chn_range / ADC_MAX_COUNT_UNIPOLAR) * 1000;
pmallick 0:3afcd581558d 1456 } else {
pmallick 0:3afcd581558d 1457 scale = (chn_range / ADC_MAX_COUNT_BIPOLAR) * 1000;
pmallick 0:3afcd581558d 1458 }
pmallick 0:3afcd581558d 1459
pmallick 0:3afcd581558d 1460 return scale;
pmallick 0:3afcd581558d 1461 }
pmallick 0:3afcd581558d 1462
pmallick 0:3afcd581558d 1463
pmallick 0:3afcd581558d 1464 /**
pmallick 0:3afcd581558d 1465 * @brief Initialize the IRQ contoller
pmallick 0:3afcd581558d 1466 * @param uart_desc[in] - UART descriptor for UART Rx IRQ event init
pmallick 0:3afcd581558d 1467 * @return none
pmallick 0:3afcd581558d 1468 * @details This function initialize the interrupt controller
pmallick 0:3afcd581558d 1469 */
pmallick 0:3afcd581558d 1470 static int32_t iio_interrupt_handler_init(mbed_uart_desc *uart_desc)
pmallick 0:3afcd581558d 1471 {
pmallick 0:3afcd581558d 1472 /* Pin to generate external interrupt */
pmallick 0:3afcd581558d 1473 PinName ext_int_pin = BUSY_PIN;
pmallick 0:3afcd581558d 1474
pmallick 0:3afcd581558d 1475 /* External interrupt descriptor */
pmallick 0:3afcd581558d 1476 struct irq_ctrl_desc *external_int_desc;
pmallick 0:3afcd581558d 1477
pmallick 0:3afcd581558d 1478 /* Define external interrupt platform specific parameters structure */
pmallick 0:3afcd581558d 1479 mbed_irq_init_param mbed_ext_int_init_param = {
pmallick 0:3afcd581558d 1480 .int_mode = EXT_IRQ_FALL,
pmallick 0:3afcd581558d 1481 .int_obj_type = &ext_int_pin
pmallick 0:3afcd581558d 1482 };
pmallick 0:3afcd581558d 1483
pmallick 0:3afcd581558d 1484 /* External interrupt init parameters */
pmallick 0:3afcd581558d 1485 struct irq_init_param external_int_init_param = {
pmallick 0:3afcd581558d 1486 .irq_ctrl_id = EXTERNAL_INT_ID,
pmallick 0:3afcd581558d 1487 .extra = &mbed_ext_int_init_param
pmallick 0:3afcd581558d 1488 };
pmallick 0:3afcd581558d 1489
pmallick 0:3afcd581558d 1490 /* External interrupt callback descriptor */
pmallick 0:3afcd581558d 1491 struct callback_desc external_int_callback_desc = {
pmallick 0:3afcd581558d 1492 &do_conversion_callback,
pmallick 0:3afcd581558d 1493 NULL,
pmallick 0:3afcd581558d 1494 NULL
pmallick 0:3afcd581558d 1495 };
pmallick 0:3afcd581558d 1496
pmallick 0:3afcd581558d 1497 /* UART Rx interrupt descriptor */
pmallick 0:3afcd581558d 1498 struct irq_ctrl_desc *uart_rx_int_desc;
pmallick 0:3afcd581558d 1499
pmallick 0:3afcd581558d 1500 /* Define external interrupt platform specific parameters structure */
pmallick 0:3afcd581558d 1501 mbed_irq_init_param mbed_uart_rx_int_init_param = {
pmallick 0:3afcd581558d 1502 .int_mode = 0,
pmallick 0:3afcd581558d 1503 .int_obj_type = uart_desc->uart_port
pmallick 0:3afcd581558d 1504 };
pmallick 0:3afcd581558d 1505
pmallick 0:3afcd581558d 1506 /* UART Rx interrupt init parameters */
pmallick 0:3afcd581558d 1507 struct irq_init_param uart_rx_int_init_param = {
pmallick 0:3afcd581558d 1508 .irq_ctrl_id = UART_RX_INT_ID,
pmallick 0:3afcd581558d 1509 .extra = &mbed_uart_rx_int_init_param
pmallick 0:3afcd581558d 1510 };
pmallick 0:3afcd581558d 1511
pmallick 0:3afcd581558d 1512 /* UART Rx interrupt callback descriptor */
pmallick 0:3afcd581558d 1513 struct callback_desc uart_rx_int_callback_desc = {
pmallick 0:3afcd581558d 1514 &iio_uart_rx_callback,
pmallick 0:3afcd581558d 1515 NULL,
pmallick 0:3afcd581558d 1516 NULL
pmallick 0:3afcd581558d 1517 };
pmallick 0:3afcd581558d 1518
pmallick 0:3afcd581558d 1519 /* Init interrupt controller for external interrupt */
pmallick 0:3afcd581558d 1520 if (irq_ctrl_init(&external_int_desc, &external_int_init_param) == FAILURE) {
pmallick 0:3afcd581558d 1521 return FAILURE;
pmallick 0:3afcd581558d 1522 }
pmallick 0:3afcd581558d 1523
pmallick 0:3afcd581558d 1524 /* Init interrupt controller for UART Rx interrupt */
pmallick 0:3afcd581558d 1525 if (irq_ctrl_init(&uart_rx_int_desc, &uart_rx_int_init_param) == FAILURE) {
pmallick 0:3afcd581558d 1526 return FAILURE;
pmallick 0:3afcd581558d 1527 }
pmallick 0:3afcd581558d 1528
pmallick 0:3afcd581558d 1529 /* Register a callback function for external interrupt */
pmallick 0:3afcd581558d 1530 if (irq_register_callback(external_int_desc, EXTERNAL_INT_ID,
pmallick 0:3afcd581558d 1531 &external_int_callback_desc) == FAILURE) {
pmallick 0:3afcd581558d 1532 return FAILURE;
pmallick 0:3afcd581558d 1533 }
pmallick 0:3afcd581558d 1534
pmallick 0:3afcd581558d 1535 /* Register a callback function for UART Rx interrupt */
pmallick 0:3afcd581558d 1536 if (irq_register_callback(uart_rx_int_desc, UART_RX_INT_ID,
pmallick 0:3afcd581558d 1537 &uart_rx_int_callback_desc) == FAILURE) {
pmallick 0:3afcd581558d 1538 return FAILURE;
pmallick 0:3afcd581558d 1539 }
pmallick 0:3afcd581558d 1540
pmallick 0:3afcd581558d 1541 return SUCCESS;
pmallick 0:3afcd581558d 1542 }
pmallick 0:3afcd581558d 1543
pmallick 0:3afcd581558d 1544
pmallick 0:3afcd581558d 1545 /**
pmallick 0:3afcd581558d 1546 * @brief Initialize the IIO interface for AD7606 IIO device
pmallick 0:3afcd581558d 1547 * @return none
pmallick 0:3afcd581558d 1548 * @return SUCCESS in case of success, FAILURE otherwise
pmallick 0:3afcd581558d 1549 */
pmallick 0:3afcd581558d 1550 int32_t ad7606_iio_initialize(void)
pmallick 0:3afcd581558d 1551 {
pmallick 0:3afcd581558d 1552 int32_t init_status;
pmallick 0:3afcd581558d 1553
pmallick 0:3afcd581558d 1554 /**
pmallick 0:3afcd581558d 1555 * AD7606 IIO instance descriptor
pmallick 0:3afcd581558d 1556 */
pmallick 0:3afcd581558d 1557 struct iio_ad7606_desc *piio_ad7606_desc;
pmallick 0:3afcd581558d 1558
pmallick 0:3afcd581558d 1559 /**
pmallick 0:3afcd581558d 1560 * AD7606 IIO init parameters
pmallick 0:3afcd581558d 1561 */
pmallick 0:3afcd581558d 1562 struct iio_ad7606_init_param iio_ad7606_init_param;
pmallick 0:3afcd581558d 1563
pmallick 0:3afcd581558d 1564 /**
pmallick 0:3afcd581558d 1565 * IIO application init parameters
pmallick 0:3afcd581558d 1566 */
pmallick 0:3afcd581558d 1567 struct iio_app_init_param iio_app_init_par;
pmallick 0:3afcd581558d 1568
pmallick 0:3afcd581558d 1569 /**
pmallick 0:3afcd581558d 1570 * UART serial interface read/write callback
pmallick 0:3afcd581558d 1571 */
pmallick 0:3afcd581558d 1572 struct iio_server_ops uart_iio_server_ops;
pmallick 0:3afcd581558d 1573
pmallick 0:3afcd581558d 1574 /*
pmallick 0:3afcd581558d 1575 * UART Mbed init extra parameters structure
pmallick 0:3afcd581558d 1576 */
pmallick 0:3afcd581558d 1577 mbed_uart_init_param uart_extra_init_param;
pmallick 0:3afcd581558d 1578
pmallick 0:3afcd581558d 1579 /*
pmallick 0:3afcd581558d 1580 * UART init parameters structure
pmallick 0:3afcd581558d 1581 */
pmallick 0:3afcd581558d 1582 struct uart_init_param uart_init_param;
pmallick 0:3afcd581558d 1583
pmallick 0:3afcd581558d 1584 /**
pmallick 0:3afcd581558d 1585 * IIO application UART descriptor
pmallick 0:3afcd581558d 1586 */
pmallick 0:3afcd581558d 1587 struct uart_desc *uart_desc = NULL;
pmallick 0:3afcd581558d 1588
pmallick 0:3afcd581558d 1589 /*
pmallick 0:3afcd581558d 1590 * GPIO LED Init parameters structure
pmallick 0:3afcd581558d 1591 */
pmallick 0:3afcd581558d 1592 struct gpio_init_param gpio_init_led;
pmallick 0:3afcd581558d 1593
pmallick 0:3afcd581558d 1594 iio_ad7606_init_param = (struct iio_ad7606_init_param) {
pmallick 0:3afcd581558d 1595 .ad7606_phy = NULL,
pmallick 0:3afcd581558d 1596 };
pmallick 0:3afcd581558d 1597
pmallick 0:3afcd581558d 1598 uart_extra_init_param = (mbed_uart_init_param) {
pmallick 0:3afcd581558d 1599 .uart_tx_pin = UART_TX,
pmallick 0:3afcd581558d 1600 .uart_rx_pin = UART_RX
pmallick 0:3afcd581558d 1601 };
pmallick 0:3afcd581558d 1602
pmallick 0:3afcd581558d 1603 uart_init_param = (struct uart_init_param ) {
pmallick 0:3afcd581558d 1604 .device_id = NULL,
pmallick 0:3afcd581558d 1605 .baud_rate = IIO_UART_BAUD_RATE,
pmallick 0:3afcd581558d 1606 .extra = &uart_extra_init_param
pmallick 0:3afcd581558d 1607 };
pmallick 0:3afcd581558d 1608
pmallick 0:3afcd581558d 1609 uart_iio_server_ops = (struct iio_server_ops) {
pmallick 0:3afcd581558d 1610 .read = iio_uart_read,
pmallick 0:3afcd581558d 1611 .write = iio_uart_write,
pmallick 0:3afcd581558d 1612 };
pmallick 0:3afcd581558d 1613
pmallick 0:3afcd581558d 1614 gpio_init_led = (struct gpio_init_param) {
pmallick 0:3afcd581558d 1615 .number = LED_GREEN,
pmallick 0:3afcd581558d 1616 .extra = NULL
pmallick 0:3afcd581558d 1617 };
pmallick 0:3afcd581558d 1618
pmallick 0:3afcd581558d 1619 iio_app_init_par = (struct iio_app_init_param) {
pmallick 0:3afcd581558d 1620 .iio_server_ops = &uart_iio_server_ops,
pmallick 0:3afcd581558d 1621 };
pmallick 0:3afcd581558d 1622
pmallick 0:3afcd581558d 1623 /* Initialize AD7606 device and peripheral interface */
pmallick 0:3afcd581558d 1624 init_status = ad7606_init(&p_ad7606_dev, &ad7606_init_str);
pmallick 0:3afcd581558d 1625 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1626 return init_status;
pmallick 0:3afcd581558d 1627 }
pmallick 0:3afcd581558d 1628
pmallick 0:3afcd581558d 1629 /* Initialize the LED GPIO descriptor */
pmallick 0:3afcd581558d 1630 init_status = gpio_get_optional(&gpio_led, &gpio_init_led);
pmallick 0:3afcd581558d 1631 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1632 return init_status;
pmallick 0:3afcd581558d 1633 } else {
pmallick 0:3afcd581558d 1634 init_status = gpio_direction_output(gpio_led, GPIO_HIGH);
pmallick 0:3afcd581558d 1635 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1636 return init_status;
pmallick 0:3afcd581558d 1637 }
pmallick 0:3afcd581558d 1638 }
pmallick 0:3afcd581558d 1639
pmallick 0:3afcd581558d 1640 /* Get the AD7606 IIO device instance */
pmallick 0:3afcd581558d 1641 iio_ad7606_init_param.ad7606_phy = p_ad7606_dev;
pmallick 0:3afcd581558d 1642
pmallick 0:3afcd581558d 1643 /* Initialize the UART interface for IIO application */
pmallick 0:3afcd581558d 1644 init_status = iio_uart_init(&uart_desc, &uart_init_param);
pmallick 0:3afcd581558d 1645 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1646 return init_status;
pmallick 0:3afcd581558d 1647 }
pmallick 0:3afcd581558d 1648
pmallick 0:3afcd581558d 1649 /* Initialize the IIO application interface */
pmallick 0:3afcd581558d 1650 init_status = iio_app_init(&iio_app_desc, &iio_app_init_par);
pmallick 0:3afcd581558d 1651 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1652 return init_status;
pmallick 0:3afcd581558d 1653 }
pmallick 0:3afcd581558d 1654
pmallick 0:3afcd581558d 1655 /* Register and initialize the AD7606 device into IIO interface */
pmallick 0:3afcd581558d 1656 init_status = iio_ad7606_init(&piio_ad7606_desc, &iio_ad7606_init_param);
pmallick 0:3afcd581558d 1657 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1658 return init_status;
pmallick 0:3afcd581558d 1659 }
pmallick 0:3afcd581558d 1660
pmallick 0:3afcd581558d 1661 /* Init the data capture for AD7606 IIO app */
pmallick 0:3afcd581558d 1662 init_status = iio_data_capture_init(p_ad7606_dev);
pmallick 0:3afcd581558d 1663 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1664 return init_status;
pmallick 0:3afcd581558d 1665 }
pmallick 0:3afcd581558d 1666
pmallick 0:3afcd581558d 1667 /* Init the interrupt event handler for AD7606 IIO app */
pmallick 0:3afcd581558d 1668 init_status = iio_interrupt_handler_init(uart_desc->extra);
pmallick 0:3afcd581558d 1669 if (init_status != SUCCESS) {
pmallick 0:3afcd581558d 1670 return init_status;
pmallick 0:3afcd581558d 1671 }
pmallick 0:3afcd581558d 1672
pmallick 0:3afcd581558d 1673 return init_status;
pmallick 0:3afcd581558d 1674 }
pmallick 0:3afcd581558d 1675
pmallick 0:3afcd581558d 1676
pmallick 0:3afcd581558d 1677 /**
pmallick 0:3afcd581558d 1678 * @brief Run the AD7606 IIO event handler
pmallick 0:3afcd581558d 1679 * @return none
pmallick 0:3afcd581558d 1680 * @details This function monitors the new IIO client event
pmallick 0:3afcd581558d 1681 */
pmallick 0:3afcd581558d 1682 void ad7606_iio_event_handler(void)
pmallick 0:3afcd581558d 1683 {
pmallick 0:3afcd581558d 1684 static bool background_conversion_started = false;
pmallick 0:3afcd581558d 1685
pmallick 0:3afcd581558d 1686 /* Handle new IIO command */
pmallick 0:3afcd581558d 1687 if (is_new_iio_command_detected()) {
pmallick 0:3afcd581558d 1688 /* Start/Stop the bakground conversion based on buffer read request */
pmallick 0:3afcd581558d 1689 if (!check_iio_cmd("READBUF", 7)) {
pmallick 0:3afcd581558d 1690 stop_background_data_conversion();
pmallick 0:3afcd581558d 1691 background_conversion_started = false;
pmallick 0:3afcd581558d 1692 } else {
pmallick 0:3afcd581558d 1693 if (background_conversion_started == false) {
pmallick 0:3afcd581558d 1694 start_background_data_conversion();
pmallick 0:3afcd581558d 1695 background_conversion_started = true;
pmallick 0:3afcd581558d 1696 }
pmallick 0:3afcd581558d 1697 }
pmallick 0:3afcd581558d 1698
pmallick 0:3afcd581558d 1699 /* Run the IIO interface when new command is detected */
pmallick 0:3afcd581558d 1700 (void)iio_app(iio_app_desc);
pmallick 0:3afcd581558d 1701 }
pmallick 0:3afcd581558d 1702 }