iio: adc: ina2xx: add INA236 support
PATCH v2 1/1 iio: adc: ina2xx: add INA236 support
The calibration divisor is not directly specified in the datasheet, but can be calculated:
I = Current_LSB * Current
Current = ShuntVoltage * CAL / calibration_divisor CAL = 0.00512 / (Current_LSB * Rshunt) ShuntVoltage = Vshunt / ShuntVoltage_LSB
=> I = (0.00512 / (calibration_divisor*ShuntVoltage_LSB)) * (Vshunt / Rshunt)
Ohm's law, I = Vshunt / Rshunt
=> 0.00512 / (calibration_divisor*ShuntVoltage_LSB) = 1
ShuntVoltage_LSB = 2.5 uV = 0.0000025 V
=> calibration_divisor = 2048
--- drivers/iio/adc/ina2xx-adc.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/iio/adc/ina2xx-adc.c +20 -5
@@ -121,7 +121,7 @@ static const struct regmap_config ina2xx_regmap_config = { .volatile_reg = ina2xx_is_volatile_reg, }; -enum ina2xx_ids { ina219, ina226 }; +enum ina2xx_ids { ina219, ina226, ina236 };struct ina2xx_config {const char *name;
@@ -175,6 +175,16 @@ static const struct ina2xx_config ina2xx_config[] = { .power_lsb_factor = 25, .chip_id = ina226, }, + [ina236] = { + .name = "ina236", + .config_default = INA226_CONFIG_DEFAULT, + .calibration_value = 2048, + .shunt_voltage_lsb = 2500, + .bus_voltage_shift = 0, + .bus_voltage_lsb = 1600, + .power_lsb_factor = 32, + .chip_id = ina236, + }, }; static int ina2xx_read_raw(struct iio_dev *indio_dev,
@@ -499,7 +509,7 @@ static int ina2xx_write_raw(struct iio_dev *indio_dev,break ;case IIO_CHAN_INFO_INT_TIME : -if (chip->config->chip_id == ina226) { +if (chip->config->chip_id != ina219) {if (chan->address ==INA2XX_SHUNT_VOLTAGE ) ret =ina226_set_int_time_vshunt (chip, val2, &tmp);
@@ -727,7 +737,7 @@ static int ina2xx_conversion_ready(struct iio_dev *indio_dev)* For now, we do an extra read of the MASK_ENABLE register (INA226) * resp. the BUS_VOLTAGE register (INA219). */ -if (chip->config->chip_id == ina226) { +if (chip->config->chip_id != ina219) { ret =regmap_read (chip->regmap,INA226_MASK_ENABLE , &alert); alert &=INA226_CVRF ;
@@ -998,7 +1008,7 @@ static int ina2xx_probe(struct i2c_client *client)/* Patch the current config register with default. */ val = chip->config->config_default; -if (type == ina226) { +if (type != ina219) {ina226_set_average (chip,INA226_DEFAULT_AVG , &val);ina226_set_int_time_vbus (chip,INA226_DEFAULT_IT , &val);ina226_set_int_time_vshunt (chip,INA226_DEFAULT_IT , &val);
@@ -1017,7 +1027,7 @@ static int ina2xx_probe(struct i2c_client *client) } indio_dev->modes =INDIO_DIRECT_MODE ; -if (type == ina226) { +if (type != ina219) { indio_dev->channels = ina226_channels; indio_dev->num_channels =ARRAY_SIZE (ina226_channels); indio_dev->info = &ina226_info;
@@ -1057,6 +1067,7 @@ static const struct i2c_device_id ina2xx_id[] = { { "ina226", ina226 }, { "ina230", ina226 }, { "ina231", ina226 }, + { "ina236", ina236 }, { } };MODULE_DEVICE_TABLE (i2c, ina2xx_id);
@@ -1082,6 +1093,10 @@ static const struct of_device_id ina2xx_of_match[] = { .compatible = "ti,ina231", .data = (void *)ina226 }, + { + .compatible = "ti,ina236", + .data = (void *)ina236 + }, { } };MODULE_DEVICE_TABLE (of, ina2xx_of_match);
Signature · 1 line
2.51.2
Jonathan Cameron
maintainerQuoted 9 lines · On Tue, 10 Mar 2026 03:37:42 +0800 Chuang Zhu <[email protected]> wrote:
The calibration divisor is not directly specified in the datasheet, but can be calculated:
I =
Current_LSB* Current
Current = ShuntVoltage * CAL /calibration_divisorCAL = 0.00512 / (Current_LSB* Rshunt) ShuntVoltage = Vshunt /ShuntVoltage_LSB=> I = (0.00512 / (calibration_divisor*ShuntVoltage_LSB)) * (Vshunt / Rshunt)
Quoted 7 lines
Ohm's law, I = Vshunt / Rshunt
=> 0.00512 / (calibration_divisor*ShuntVoltage_LSB) = 1
ShuntVoltage_LSB= 2.5 uV = 0.0000025 V
=>calibration_divisor= 2048Signed-off-by Chuang Zhu <[email protected]>
Hi.
Ideally I'd like this driver to move away from using an enum for device identification and towards encapsulating all the differences between devices in a chip specific structure instance. However, that's obviously a more significant change and I don't feel it's fair to land that on you! If you want to do it though it would be appreciated. Just look for other drivers that use separate structures rather than indexing into an array.
Having said that, if we are going to stick to the enum for now I would prefer a few changes to how we do it. See inline.
Quoted 35 lines
--- drivers/iio/adc/ina2xx-adc.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)drivers/iio/adc/ina2xx-adc.c +11
-1@@ -121,7 +121,7 @@ static const struct regmap_config ina2xx_regmap_config = { .volatile_reg = ina2xx_is_volatile_reg, }; -enum ina2xx_ids { ina219, ina226 }; +enum ina2xx_ids { ina219, ina226, ina236 };struct ina2xx_config {const char *name;@@ -175,6 +175,16 @@ static const struct ina2xx_config ina2xx_config[] = { .power_lsb_factor = 25, .chip_id = ina226, }, + [ina236] = { + .name = "ina236", + .config_default = INA226_CONFIG_DEFAULT, + .calibration_value = 2048, + .shunt_voltage_lsb = 2500, + .bus_voltage_shift = 0, + .bus_voltage_lsb = 1600, + .power_lsb_factor = 32, + .chip_id = ina236, + }, }; static int ina2xx_read_raw(struct iio_dev *indio_dev,
drivers/iio/adc/ina2xx-adc.c static int ina2xx_write_raw(struct iio_dev *indio_dev, · line 512
Quoted 5 lines
drivers/iio/adc/ina2xx-adc.c +1
-1@@ -499,7 +509,7 @@ static int ina2xx_write_raw(struct iio_dev *indio_dev,break ;case IIO_CHAN_INFO_INT_TIME : -if (chip->config->chip_id == ina226) { +if (chip->config->chip_id != ina219) {
I would prefer explicit matching. Probably best done via
a switch on the chip_id. Same applies in all the other similar changes.
Quoted 7 lines
drivers/iio/adc/ina2xx-adc.c +0
-0if (chan->address ==INA2XX_SHUNT_VOLTAGE ) ret =ina226_set_int_time_vshunt (chip, val2, &tmp);@@ -727,7 +737,7 @@ static int ina2xx_conversion_ready(struct iio_dev *indio_dev)* For now, we do an extra read of the MASK_ENABLE register (INA226) * resp. the BUS_VOLTAGE register (INA219). */