|
| 1 | +/***************************************************************************//** |
| 2 | + * @file max22216.c |
| 3 | + * @brief Current control |
| 4 | + * @author Robert Budai (robert.budai@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 | + |
| 34 | +#include "max22216.h" |
| 35 | + |
| 36 | +int max22216_write_reg(struct max22216_desc *desc, uint8_t reg_addr, |
| 37 | + uint16_t data) |
| 38 | +{ |
| 39 | + int ret = 0; |
| 40 | + |
| 41 | + if (!desc || !desc->spi_desc) |
| 42 | + return -EINVAL; |
| 43 | + no_os_field_get(data, MP_OS_GEN_MASK()) |
| 44 | + uint8_t tx[3] = { |
| 45 | + (uint8_t)(reg_addr | NO_OS_BIT(8)), |
| 46 | + (uint8_t)no_os_field_get(0xFF00, data), |
| 47 | + (uint8_t)no_os_field_get(0x00FF, data) |
| 48 | + }; |
| 49 | + |
| 50 | + return no_os_spi_write_and_read(desc->spi_desc, tx, 3); |
| 51 | +} |
| 52 | + |
| 53 | +int max22216_read_reg(struct max22216_desc *desc, uint8_t reg_addr, |
| 54 | + uint16_t *data) |
| 55 | +{ |
| 56 | + int ret; |
| 57 | + uint8_t tx[3] = { reg_addr, 0x00, 0x00}; |
| 58 | + |
| 59 | + if (!desc || !desc->spi_desc) |
| 60 | + return -EINVAL; |
| 61 | + |
| 62 | + ret = no_os_spi_write_and_read(desc->spi_desc, tx, 3); |
| 63 | + if (ret) |
| 64 | + return ret; |
| 65 | + ret = no_os_spi_write_and_read(desc->spi_desc, tx, 3); |
| 66 | + if (ret) |
| 67 | + return ret; |
| 68 | + |
| 69 | + desc->status_reg = tx[0]; |
| 70 | + |
| 71 | + *data = (tx[1] << 8) | tx[2]; |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +int max22216_write_reg_list(struct max22216_desc *desc, |
| 77 | + max22216_reg_setting_t* list, uint8_t elem_nr) |
| 78 | +{ |
| 79 | + int ret; |
| 80 | + uint8_t i; |
| 81 | + |
| 82 | + if (!desc || !desc->spi_desc || !list) |
| 83 | + return -1; |
| 84 | + |
| 85 | + for (i = 0; i < elem_nr; i++) { |
| 86 | + ret = max22216_write_reg(desc, list[i].reg_addr, list[i].data); |
| 87 | + if (ret) |
| 88 | + return ret; |
| 89 | + } |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +int erase_fault_reg(struct max22216_desc *desc) |
| 94 | +{ |
| 95 | + int ret; |
| 96 | + uint16_t data = 0; |
| 97 | + |
| 98 | + ret = max22216_write_reg(desc, MAX22216_FAULT1, 0xFFFF); |
| 99 | + if (ret) |
| 100 | + return ret; |
| 101 | + ret = max22216_read_reg(desc, MAX22216_FAULT0, &data); |
| 102 | + if (ret) |
| 103 | + return ret; |
| 104 | + return max22216_read_reg(desc, MAX22216_FAULT1, &data); |
| 105 | +} |
| 106 | + |
| 107 | +int max22216_set_enable_pin(struct max22216_desc *desc, bool value) |
| 108 | +{ |
| 109 | + int ret; |
| 110 | + |
| 111 | + if (!desc || !desc->drv_en_gpio) |
| 112 | + return -EINVAL; |
| 113 | + |
| 114 | + return no_os_gpio_set_value(desc->drv_en_gpio, |
| 115 | + value ? NO_OS_GPIO_HIGH : NO_OS_GPIO_LOW); |
| 116 | +} |
| 117 | + |
| 118 | +int max22216_check_fault_pin(struct max22216_desc *desc, bool *fault_status) |
| 119 | +{ |
| 120 | + int ret; |
| 121 | + uint8_t value; |
| 122 | + |
| 123 | + if (!desc || !desc->fault_gpio || !fault_status) |
| 124 | + return -EINVAL; |
| 125 | + |
| 126 | + ret = no_os_gpio_get_value(desc->fault_gpio, &value); |
| 127 | + if (ret) |
| 128 | + return ret; |
| 129 | + |
| 130 | + *fault_status = (value == NO_OS_GPIO_LOW); // Assuming LOW indicates a fault |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | +int max22216_current_reg_control(struct max22216_desc *desc, uint8_t channel_nr, |
| 135 | + uint16_t value) |
| 136 | +{ |
| 137 | + int ret; |
| 138 | + uint16_t test_value; |
| 139 | + max22216_reg_setting_t reg_setting; |
| 140 | + // Prepare register settings for current control |
| 141 | + uint8_t reg_addr = MAX22216_CFG_DC_H_0 + (channel_nr * |
| 142 | + MAX22216_CHANNEL_CONFIG_REG_SHIFT); |
| 143 | + |
| 144 | + if (!desc || !desc->spi_desc) |
| 145 | + return -EINVAL; |
| 146 | + |
| 147 | + |
| 148 | + if (channel_nr >= MAX22216_NR_OF_CHANNELS) |
| 149 | + return -EINVAL; |
| 150 | + |
| 151 | + // Write the register settings |
| 152 | + ret = max22216_write_reg(desc, reg_addr, value); |
| 153 | + if (ret) |
| 154 | + return ret; |
| 155 | + |
| 156 | + ret = max22216_read_reg(desc, reg_addr, &test_value); |
| 157 | + if (ret) |
| 158 | + return ret; |
| 159 | + |
| 160 | + if (test_value != value) |
| 161 | + return -EINVAL; |
| 162 | + |
| 163 | + return 0; |
| 164 | +} |
| 165 | + |
| 166 | +int max22216_turn_on(struct max22216_desc *desc, uint8_t channel_nr) |
| 167 | +{ |
| 168 | + int ret; |
| 169 | + uint16_t data; |
| 170 | + |
| 171 | + if (!desc || !desc->spi_desc) |
| 172 | + return -EINVAL; |
| 173 | + |
| 174 | + if (channel_nr >= MAX22216_NR_OF_CHANNELS) |
| 175 | + return -EINVAL; |
| 176 | + |
| 177 | + ret = max22216_read_reg(desc, |
| 178 | + MAX22216_CFG_IND_0_0 + (channel_nr * MAX22216_CHANNEL_CONFIG_REG_SHIFT), &data); |
| 179 | + if (ret) |
| 180 | + return ret; |
| 181 | + ret = max22216_write_reg(desc, |
| 182 | + MAX22216_CFG_IND_0_0 + (channel_nr * MAX22216_CHANNEL_CONFIG_REG_SHIFT), |
| 183 | + data | NO_OS_BIT(MAX22216_CFG_IND_DITHERING_BIT)); |
| 184 | + if (ret) |
| 185 | + return ret; |
| 186 | + ret = max22216_read_reg(desc, |
| 187 | + MAX22216_CFG_IND_0_0 + (channel_nr * MAX22216_CHANNEL_CONFIG_REG_SHIFT), &data); |
| 188 | + if (ret) |
| 189 | + return ret; |
| 190 | + if (!(data | NO_OS_BIT(MAX22216_CFG_IND_DITHERING_BIT))) |
| 191 | + return -1; |
| 192 | + |
| 193 | + ret = max22216_read_reg(desc, MAX22216_GLOBAL_CTRL, &data); |
| 194 | + if (ret) |
| 195 | + return ret; |
| 196 | + ret = max22216_write_reg(desc, MAX22216_GLOBAL_CTRL, |
| 197 | + data | NO_OS_BIT(channel_nr)); |
| 198 | + if (ret) |
| 199 | + return ret; |
| 200 | + ret = max22216_read_reg(desc, MAX22216_GLOBAL_CTRL, &data); |
| 201 | + if (ret) |
| 202 | + return ret; |
| 203 | + if (!(data | NO_OS_BIT(channel_nr))) |
| 204 | + return -EINVAL; |
| 205 | + |
| 206 | + return 0; |
| 207 | + |
| 208 | +} |
| 209 | + |
| 210 | +int max22216_turn_off(struct max22216_desc *desc, uint8_t channel_nr) |
| 211 | +{ |
| 212 | + int ret; |
| 213 | + uint16_t data; |
| 214 | + |
| 215 | + if (!desc || !desc->spi_desc) |
| 216 | + return -EINVAL; |
| 217 | + |
| 218 | + if (channel_nr >= MAX22216_NR_OF_CHANNELS) |
| 219 | + return -EINVAL; |
| 220 | + ret = max22216_read_reg(desc, MAX22216_GLOBAL_CTRL, &data); |
| 221 | + if (ret) |
| 222 | + return ret; |
| 223 | + ret = max22216_write_reg(desc, MAX22216_GLOBAL_CTRL, |
| 224 | + data & ~(NO_OS_BIT(channel_nr))); |
| 225 | + if (ret) |
| 226 | + return ret; |
| 227 | + ret = max22216_read_reg(desc, MAX22216_GLOBAL_CTRL, &data); |
| 228 | + if (ret) |
| 229 | + return ret; |
| 230 | + if (data & (NO_OS_BIT(channel_nr))) |
| 231 | + // Indicates that the relevant bits for the channel were not cleared as expected |
| 232 | + return -EINVAL; |
| 233 | + ret = max22216_read_reg(desc, |
| 234 | + MAX22216_CFG_IND_0_0 + (channel_nr * MAX22216_CHANNEL_CONFIG_REG_SHIFT), &data); |
| 235 | + if (ret) |
| 236 | + return ret; |
| 237 | + ret = max22216_write_reg(desc, |
| 238 | + MAX22216_CFG_IND_0_0 + (channel_nr * MAX22216_CHANNEL_CONFIG_REG_SHIFT), |
| 239 | + data & ~NO_OS_BIT(MAX22216_CFG_IND_DITHERING_BIT)); |
| 240 | + if (ret) |
| 241 | + return ret; |
| 242 | + ret = max22216_read_reg(desc, |
| 243 | + MAX22216_CFG_IND_0_0 + (channel_nr * MAX22216_CHANNEL_CONFIG_REG_SHIFT), &data); |
| 244 | + if (ret) |
| 245 | + return ret; |
| 246 | + if (data & NO_OS_BIT(MAX22216_CFG_IND_DITHERING_BIT)) |
| 247 | + return -EINVAL; |
| 248 | + |
| 249 | + return 0; |
| 250 | +} |
| 251 | + |
| 252 | +int max22216_set_current_ma(struct max22216_desc *desc, uint8_t channel_nr, |
| 253 | + uint16_t current_ma) |
| 254 | +{ |
| 255 | + int ret; |
| 256 | + uint16_t reg_value = (uint16_t)(current_ma / (MAX22216_K_CDR * MAX22216_GAIN * |
| 257 | + MAX22216_SNSF)); |
| 258 | + |
| 259 | + if (!desc) |
| 260 | + return -EINVAL; |
| 261 | + |
| 262 | + ret = max22216_current_reg_control(desc, channel_nr, reg_value); |
| 263 | + if (ret) |
| 264 | + return ret; |
| 265 | + |
| 266 | + return 0; |
| 267 | +} |
| 268 | + |
| 269 | +int max22216_init(struct max22216_desc **desc, |
| 270 | + struct max22216_init_param *param) |
| 271 | +{ |
| 272 | + int ret; |
| 273 | + |
| 274 | + if (!desc || !param || !param->spi_ip) |
| 275 | + return -EINVAL; |
| 276 | + |
| 277 | + struct max22216_desc *dev = calloc(1, sizeof(*dev)); |
| 278 | + |
| 279 | + if (!dev) |
| 280 | + return -ENOMEM; |
| 281 | + |
| 282 | + struct no_os_gpio_desc *max22216_drv_en_desc = calloc(1, |
| 283 | + sizeof(*max22216_drv_en_desc)); |
| 284 | + if (!max22216_drv_en_desc) |
| 285 | + goto error1; |
| 286 | + struct no_os_gpio_desc *max22216_fault_desc = calloc(1, |
| 287 | + sizeof(*max22216_fault_desc)); |
| 288 | + |
| 289 | + if (!max22216_fault_desc) |
| 290 | + goto error2; |
| 291 | + |
| 292 | + ret = no_os_spi_init(&dev->spi_desc, param->spi_ip); |
| 293 | + if (ret) |
| 294 | + goto error1; |
| 295 | + |
| 296 | + // Setup for enable pin |
| 297 | + ret = no_os_gpio_get(&max22216_drv_en_desc, param->drv_en_gpio_ip); |
| 298 | + if (ret) |
| 299 | + goto error1; |
| 300 | + |
| 301 | + dev->drv_en_gpio = max22216_drv_en_desc; |
| 302 | + ret = no_os_gpio_direction_output(max22216_drv_en_desc, NO_OS_GPIO_HIGH); |
| 303 | + if (ret) |
| 304 | + goto error1; |
| 305 | + |
| 306 | + ret = no_os_gpio_set_value(max22216_drv_en_desc, NO_OS_GPIO_HIGH); |
| 307 | + if (ret) |
| 308 | + goto error1; |
| 309 | + |
| 310 | + // Setup for fault pin polling |
| 311 | + ret = no_os_gpio_get(&max22216_fault_desc, param->fault_gpio_ip); |
| 312 | + if (ret) |
| 313 | + goto error1; |
| 314 | + |
| 315 | + dev->fault_gpio = max22216_fault_desc; |
| 316 | + |
| 317 | + ret = no_os_gpio_direction_input(max22216_fault_desc); |
| 318 | + if (ret) |
| 319 | + goto error1; |
| 320 | + |
| 321 | + dev->status_reg = 0; |
| 322 | + *desc = dev; |
| 323 | + return 0; |
| 324 | + |
| 325 | +error1: |
| 326 | + free(max22216_fault_desc); |
| 327 | +error2: |
| 328 | + free(max22216_drv_en_desc); |
| 329 | +error3: |
| 330 | + free(dev); |
| 331 | + return ret; |
| 332 | +} |
| 333 | + |
| 334 | +int max22216_remove(struct max22216_desc *desc) |
| 335 | +{ |
| 336 | + if (!desc) |
| 337 | + return -1; |
| 338 | + if (desc->spi_desc) |
| 339 | + no_os_spi_remove(desc->spi_desc); |
| 340 | + if (desc->drv_en_gpio) |
| 341 | + no_os_gpio_remove(desc->drv_en_gpio); |
| 342 | + if (desc->fault_gpio) |
| 343 | + no_os_gpio_remove(desc->fault_gpio); |
| 344 | + free(desc); |
| 345 | + return 0; |
| 346 | +} |
0 commit comments