|
| 1 | +/***************************************************************************//** |
| 2 | + * @file iio_ltc2378.c |
| 3 | + * @brief Implementation of iio_ltc2378.c. |
| 4 | + * @author Cherrence Sarip (cherrence.sarip@analog.com) |
| 5 | +******************************************************************************** |
| 6 | + * Copyright 2025(c) Analog Devices, Inc. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * 3. Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from this |
| 20 | + * software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR |
| 23 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 24 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 25 | + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 28 | + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 31 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +*******************************************************************************/ |
| 33 | +#include <stdlib.h> |
| 34 | +#include <stdio.h> |
| 35 | +#include <string.h> |
| 36 | +#include "no_os_alloc.h" |
| 37 | +#include "no_os_error.h" |
| 38 | +#include "no_os_units.h" |
| 39 | +#include "no_os_util.h" |
| 40 | +#include "iio.h" |
| 41 | +#include "ltc2378.h" |
| 42 | +#include "iio_ltc2378.h" |
| 43 | + |
| 44 | +enum ltc2378_iio_attr { |
| 45 | + LTC2378_IIO_RAW, |
| 46 | + LTC2378_IIO_SCALE, |
| 47 | + LTC2378_IIO_PROCESSED, |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Read from ADC and provide IIO data |
| 52 | + * @param dev - The iio device structure. |
| 53 | + * @param buf - Buffer to be filled with requested attr val. |
| 54 | + * @param len - Length of the received command buffer in bytes. |
| 55 | + * @param channel - Command channel info. |
| 56 | + * @param priv - Command attribute id. |
| 57 | + * @return 0 in case of success, negative error code otherwise. |
| 58 | + */ |
| 59 | +static int ltc2378_iio_read(void *dev, char *buf, uint32_t len, |
| 60 | + const struct iio_ch_info *channel, intptr_t priv) |
| 61 | +{ |
| 62 | + struct ltc2378_iio_desc *iio_ltc2378 = dev; |
| 63 | + struct ltc2378_dev *ltc2378 = iio_ltc2378->ltc2378_dev; |
| 64 | + int ret; |
| 65 | + int64_t vals[2]; |
| 66 | + uint32_t raw_data; |
| 67 | + int32_t voltage_uv; |
| 68 | + |
| 69 | + if (!dev || !iio_ltc2378->ltc2378_dev) |
| 70 | + return -EINVAL; |
| 71 | + |
| 72 | + switch (priv) { |
| 73 | + case LTC2378_IIO_RAW: |
| 74 | + ret = ltc2378_read_raw(ltc2378, &raw_data); |
| 75 | + if (ret) |
| 76 | + return ret; |
| 77 | + |
| 78 | + vals[0] = raw_data; |
| 79 | + vals[1] = 0; |
| 80 | + return iio_format_value(buf, len, IIO_VAL_INT, 1, vals); |
| 81 | + |
| 82 | + case LTC2378_IIO_SCALE: |
| 83 | + vals[0] = ltc2378->vref_uv; |
| 84 | + if (ltc2378->input_mode == LTC2378_UNIPOLAR) { |
| 85 | + vals[1] = 20; |
| 86 | + } else { |
| 87 | + vals[1] = 19; |
| 88 | + } |
| 89 | + return iio_format_value(buf, len, IIO_VAL_FRACTIONAL_LOG2, 2, vals); |
| 90 | + |
| 91 | + case LTC2378_IIO_PROCESSED: |
| 92 | + ret = ltc2378_read_raw(ltc2378, &raw_data); |
| 93 | + if (ret) { |
| 94 | + vals[0] = -1000000; |
| 95 | + vals[1] = 0; |
| 96 | + return iio_format_value(buf, len, IIO_VAL_INT, 1, vals); |
| 97 | + } |
| 98 | + |
| 99 | + ret = ltc2378_raw_to_uv(ltc2378, raw_data, &voltage_uv); |
| 100 | + if (ret) { |
| 101 | + vals[0] = -2000000; |
| 102 | + vals[1] = 0; |
| 103 | + return iio_format_value(buf, len, IIO_VAL_INT, 1, vals); |
| 104 | + } |
| 105 | + |
| 106 | + vals[0] = voltage_uv; |
| 107 | + vals[1] = 0; |
| 108 | + return iio_format_value(buf, len, IIO_VAL_INT, 1, vals); |
| 109 | + |
| 110 | + default: |
| 111 | + return -EINVAL; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +static struct iio_attribute ltc2378_iio_attrs[] = { |
| 116 | + { |
| 117 | + .name = "raw", |
| 118 | + .show = ltc2378_iio_read, |
| 119 | + .store = NULL, |
| 120 | + .priv = LTC2378_IIO_RAW, |
| 121 | + }, |
| 122 | + { |
| 123 | + .name = "scale", |
| 124 | + .show = ltc2378_iio_read, |
| 125 | + .store = NULL, |
| 126 | + .priv = LTC2378_IIO_SCALE, |
| 127 | + }, |
| 128 | + { |
| 129 | + .name = "processed", |
| 130 | + .show = ltc2378_iio_read, |
| 131 | + .store = NULL, |
| 132 | + .priv = LTC2378_IIO_PROCESSED, |
| 133 | + }, |
| 134 | + END_ATTRIBUTES_ARRAY |
| 135 | +}; |
| 136 | + |
| 137 | +static struct iio_channel ltc2378_iio_channels[] = { |
| 138 | + { |
| 139 | + .name = "voltage0", |
| 140 | + .attributes = ltc2378_iio_attrs, |
| 141 | + .ch_type = IIO_VOLTAGE, |
| 142 | + .indexed = true, |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +static struct iio_device ltc2378_iio_dev = { |
| 147 | + .channels = ltc2378_iio_channels, |
| 148 | + .num_ch = NO_OS_ARRAY_SIZE(ltc2378_iio_channels), |
| 149 | + .debug_reg_read = NULL, |
| 150 | + .debug_reg_write = NULL, |
| 151 | +}; |
| 152 | + |
| 153 | +/** |
| 154 | + * @brief Initialize the IIO LTC2378 driver |
| 155 | + * @param iio_desc - IIO device descriptor. |
| 156 | + * @param init_param - Init parameter structure. |
| 157 | + * @return 0 in case of success, negative error code otherwise. |
| 158 | + */ |
| 159 | +int ltc2378_iio_init(struct ltc2378_iio_desc **iio_desc, |
| 160 | + struct ltc2378_iio_desc_init_param *init_param) |
| 161 | +{ |
| 162 | + struct ltc2378_iio_desc *descriptor; |
| 163 | + int ret; |
| 164 | + |
| 165 | + if (!iio_desc || !init_param || !init_param->ltc2378_init_param) |
| 166 | + return -EINVAL; |
| 167 | + |
| 168 | + descriptor = no_os_calloc(1, sizeof(*descriptor)); |
| 169 | + if (!descriptor) |
| 170 | + return -ENOMEM; |
| 171 | + |
| 172 | + ret = ltc2378_init(&descriptor->ltc2378_dev, init_param->ltc2378_init_param); |
| 173 | + if (ret) |
| 174 | + goto error_free_desc; |
| 175 | + |
| 176 | + descriptor->iio_dev = <c2378_iio_dev; |
| 177 | + |
| 178 | + *iio_desc = descriptor; |
| 179 | + |
| 180 | + return 0; |
| 181 | + |
| 182 | +error_free_desc: |
| 183 | + no_os_free(descriptor); |
| 184 | + return ret; |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * @brief Free the resources allocated by ltc2378_iio_init() |
| 189 | + * @param iio_desc - IIO device descriptor. |
| 190 | + * @return 0 in case of success, negative error code otherwise. |
| 191 | + */ |
| 192 | +int ltc2378_iio_remove(struct ltc2378_iio_desc *iio_desc) |
| 193 | +{ |
| 194 | + if (!iio_desc) |
| 195 | + return -EINVAL; |
| 196 | + |
| 197 | + ltc2378_remove(iio_desc->ltc2378_dev); |
| 198 | + no_os_free(iio_desc); |
| 199 | + |
| 200 | + return 0; |
| 201 | +} |
0 commit comments