ina2xx.c (232177a37b90d76d3616f28fb47636d81c02fe8b) ina2xx.c (2bb476524c61f43e6e89aeacaee6599ce5dd3505)
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Texas Instruments INA219, INA226 power monitor chips
4 *
5 * INA219:
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
7 * Datasheet: https://www.ti.com/product/ina219
8 *

--- 36 unchanged lines hidden (view full) ---

45#define INA2XX_CURRENT 0x04 /* readonly */
46#define INA2XX_CALIBRATION 0x05
47
48/* INA226 register definitions */
49#define INA226_MASK_ENABLE 0x06
50#define INA226_ALERT_LIMIT 0x07
51#define INA226_DIE_ID 0xFF
52
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Texas Instruments INA219, INA226 power monitor chips
4 *
5 * INA219:
6 * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
7 * Datasheet: https://www.ti.com/product/ina219
8 *

--- 36 unchanged lines hidden (view full) ---

45#define INA2XX_CURRENT 0x04 /* readonly */
46#define INA2XX_CALIBRATION 0x05
47
48/* INA226 register definitions */
49#define INA226_MASK_ENABLE 0x06
50#define INA226_ALERT_LIMIT 0x07
51#define INA226_DIE_ID 0xFF
52
53/* register count */
54#define INA219_REGISTERS 6
55#define INA226_REGISTERS 8
56
57#define INA2XX_MAX_REGISTERS 8
58
59/* settings - depend on use case */
60#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
61#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
62
63/* worst case is 68.10 ms (~14.6Hz, ina219) */
64#define INA2XX_CONVERSION_RATE 15

--- 25 unchanged lines hidden (view full) ---

90#define INA2XX_MAX_ATTRIBUTE_GROUPS 3
91
92/*
93 * Both bus voltage and shunt voltage conversion times for ina226 are set
94 * to 0b0100 on POR, which translates to 2200 microseconds in total.
95 */
96#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
97
53#define INA2XX_MAX_REGISTERS 8
54
55/* settings - depend on use case */
56#define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
57#define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
58
59/* worst case is 68.10 ms (~14.6Hz, ina219) */
60#define INA2XX_CONVERSION_RATE 15

--- 25 unchanged lines hidden (view full) ---

86#define INA2XX_MAX_ATTRIBUTE_GROUPS 3
87
88/*
89 * Both bus voltage and shunt voltage conversion times for ina226 are set
90 * to 0b0100 on POR, which translates to 2200 microseconds in total.
91 */
92#define INA226_TOTAL_CONV_TIME_DEFAULT 2200
93
98static struct regmap_config ina2xx_regmap_config = {
94static const struct regmap_config ina2xx_regmap_config = {
99 .reg_bits = 8,
100 .val_bits = 16,
95 .reg_bits = 8,
96 .val_bits = 16,
97 .max_register = INA2XX_MAX_REGISTERS,
101};
102
103enum ina2xx_ids { ina219, ina226 };
104
105struct ina2xx_config {
106 u16 config_default;
107 int calibration_value;
98};
99
100enum ina2xx_ids { ina219, ina226 };
101
102struct ina2xx_config {
103 u16 config_default;
104 int calibration_value;
108 int registers;
109 int shunt_div;
110 int bus_voltage_shift;
111 int bus_voltage_lsb; /* uV */
112 int power_lsb_factor;
113};
114
115struct ina2xx_data {
116 const struct ina2xx_config *config;

--- 6 unchanged lines hidden (view full) ---

123
124 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
125};
126
127static const struct ina2xx_config ina2xx_config[] = {
128 [ina219] = {
129 .config_default = INA219_CONFIG_DEFAULT,
130 .calibration_value = 4096,
105 int shunt_div;
106 int bus_voltage_shift;
107 int bus_voltage_lsb; /* uV */
108 int power_lsb_factor;
109};
110
111struct ina2xx_data {
112 const struct ina2xx_config *config;

--- 6 unchanged lines hidden (view full) ---

119
120 const struct attribute_group *groups[INA2XX_MAX_ATTRIBUTE_GROUPS];
121};
122
123static const struct ina2xx_config ina2xx_config[] = {
124 [ina219] = {
125 .config_default = INA219_CONFIG_DEFAULT,
126 .calibration_value = 4096,
131 .registers = INA219_REGISTERS,
132 .shunt_div = 100,
133 .bus_voltage_shift = 3,
134 .bus_voltage_lsb = 4000,
135 .power_lsb_factor = 20,
136 },
137 [ina226] = {
138 .config_default = INA226_CONFIG_DEFAULT,
139 .calibration_value = 2048,
127 .shunt_div = 100,
128 .bus_voltage_shift = 3,
129 .bus_voltage_lsb = 4000,
130 .power_lsb_factor = 20,
131 },
132 [ina226] = {
133 .config_default = INA226_CONFIG_DEFAULT,
134 .calibration_value = 2048,
140 .registers = INA226_REGISTERS,
141 .shunt_div = 400,
142 .bus_voltage_shift = 0,
143 .bus_voltage_lsb = 1250,
144 .power_lsb_factor = 25,
145 },
146};
147
148/*

--- 492 unchanged lines hidden (view full) ---

641 data->config = &ina2xx_config[chip];
642 mutex_init(&data->config_lock);
643
644 if (device_property_read_u32(dev, "shunt-resistor", &val) < 0)
645 val = INA2XX_RSHUNT_DEFAULT;
646
647 ina2xx_set_shunt(data, val);
648
135 .shunt_div = 400,
136 .bus_voltage_shift = 0,
137 .bus_voltage_lsb = 1250,
138 .power_lsb_factor = 25,
139 },
140};
141
142/*

--- 492 unchanged lines hidden (view full) ---

635 data->config = &ina2xx_config[chip];
636 mutex_init(&data->config_lock);
637
638 if (device_property_read_u32(dev, "shunt-resistor", &val) < 0)
639 val = INA2XX_RSHUNT_DEFAULT;
640
641 ina2xx_set_shunt(data, val);
642
649 ina2xx_regmap_config.max_register = data->config->registers;
650
651 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
652 if (IS_ERR(data->regmap)) {
653 dev_err(dev, "failed to allocate register map\n");
654 return PTR_ERR(data->regmap);
655 }
656
657 ret = devm_regulator_get_enable(dev, "vs");
658 if (ret)

--- 91 unchanged lines hidden ---
643 data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
644 if (IS_ERR(data->regmap)) {
645 dev_err(dev, "failed to allocate register map\n");
646 return PTR_ERR(data->regmap);
647 }
648
649 ret = devm_regulator_get_enable(dev, "vs");
650 if (ret)

--- 91 unchanged lines hidden ---