1 /* 2 * Copyright (c) 2023 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include <zephyr/types.h> 8 #include <zephyr/drivers/gpio.h> 9 10 #define INA3221_CONFIG 0x00 11 #define INA3221_SHUNT_V1 0x01 12 #define INA3221_BUS_V1 0x02 13 #define INA3221_SHUNT_V2 0x03 14 #define INA3221_BUS_V2 0x04 15 #define INA3221_SHUNT_V3 0x05 16 #define INA3221_BUS_V3 0x06 17 18 #define INA3221_MASK_ENABLE 0x0f 19 #define INA3221_MANUF_ID 0xfe 20 #define INA3221_MANUF_ID_VALUE 0x5449 21 #define INA3221_CHIP_ID 0xff 22 #define INA3221_CHIP_ID_VALUE 0x3220 23 24 #define INA3221_MASK_ENABLE_CONVERSION_READY BIT(0) 25 #define INA3221_CONFIG_RST BIT(15) 26 #define INA3221_CONFIG_CH1 BIT(14) 27 #define INA3221_CONFIG_CH2 BIT(13) 28 #define INA3221_CONFIG_CH3 BIT(12) 29 #define INA3221_CONFIG_AVG_Msk GENMASK(11, 9) 30 #define INA3221_CONFIG_CT_VBUS_Msk GENMASK(8, 6) 31 #define INA3221_CONFIG_CT_VSH_Msk GENMASK(5, 3) 32 #define INA3221_CONFIG_CONTINUOUS BIT(2) 33 #define INA3221_CONFIG_BUS BIT(1) 34 #define INA3221_CONFIG_SHUNT BIT(0) 35 36 #define INA3221_BUS_VOLTAGE_LSB 0.008f 37 #define INA3221_SHUNT_VOLTAGE_LSB 0.00004f 38 39 #define SENSOR_ATTR_INA3221_SELECTED_CHANNEL (SENSOR_ATTR_PRIV_START+1) 40 41 enum ina3221_avg_mode { 42 INA3221_AVG_MODE_1 = 0, 43 INA3221_AVG_MODE_4, 44 INA3221_AVG_MODE_16, 45 INA3221_AVG_MODE_64, 46 INA3221_AVG_MODE_128, 47 INA3221_AVG_MODE_256, 48 INA3221_AVG_MODE_512, 49 INA3221_AVG_MODE_1024, 50 }; 51 52 enum ina3221_conv_time { 53 INA3221_CONV_TIME_0_140ms = 0, 54 INA3221_CONV_TIME_0_204ms, 55 INA3221_CONV_TIME_0_332ms, 56 INA3221_CONV_TIME_0_588ms, 57 INA3221_CONV_TIME_1_100ms, 58 INA3221_CONV_TIME_2_116ms, 59 INA3221_CONV_TIME_4_156ms, 60 INA3221_CONV_TIME_8_244ms, 61 }; 62 63 static const int32_t avg_mode_samples[8] = {1, 4, 16, 64, 128, 256, 512, 1024}; 64 static const int32_t conv_time_us[8] = {140, 204, 332, 588, 1100, 2116, 4156, 8244}; 65 66 struct ina3221_config { 67 struct i2c_dt_spec bus; 68 enum ina3221_avg_mode avg_mode; 69 enum ina3221_conv_time conv_time_bus; 70 enum ina3221_conv_time conv_time_shunt; 71 bool enable_channel[3]; 72 uint16_t shunt_r[3]; 73 }; 74 75 struct ina3221_data { 76 size_t selected_channel; 77 uint16_t config; 78 int16_t v_bus[3]; 79 int16_t v_shunt[3]; 80 }; 81