Skip to content

Commit a28caa7

Browse files
Maciej Purskigregkh
authored andcommitted
iio: adc: ina2xx: Make calibration register value fixed
Calibration register is used for calculating current register in hardware according to datasheet: current = shunt_volt * calib_register / 2048 (ina 226) current = shunt_volt * calib_register / 4096 (ina 219) Fix calib_register value to 2048 for ina226 and 4096 for ina 219 in order to avoid truncation error and provide best precision allowed by shunt_voltage measurement. Make current scale value follow changes of shunt_resistor from sysfs as calib_register value is now fixed. Power_lsb value should also follow shunt_resistor changes as stated in datasheet: power_lsb = 25 * current_lsb (ina 226) power_lsb = 20 * current_lsb (ina 219) This is a part of the patchset: https://lkml.org/lkml/2017/11/22/394 Signed-off-by: Maciej Purski <m.purski@samsung.com> Reviewed-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bbdcbd1 commit a28caa7

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

‎drivers/iio/adc/ina2xx-adc.c‎

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ enum ina2xx_ids { ina219, ina226 };
124124

125125
struct ina2xx_config {
126126
u16 config_default;
127-
int calibration_factor;
127+
int calibration_value;
128128
int shunt_voltage_lsb; /* nV */
129129
int bus_voltage_shift; /* position of lsb */
130130
int bus_voltage_lsb; /* uV */
131-
int power_lsb; /* uW */
131+
/* fixed relation between current and power lsb, uW/uA */
132+
int power_lsb_factor;
132133
enum ina2xx_ids chip_id;
133134
};
134135

@@ -149,20 +150,20 @@ struct ina2xx_chip_info {
149150
static const struct ina2xx_config ina2xx_config[] = {
150151
[ina219] = {
151152
.config_default = INA219_CONFIG_DEFAULT,
152-
.calibration_factor = 40960000,
153+
.calibration_value = 4096,
153154
.shunt_voltage_lsb = 10000,
154155
.bus_voltage_shift = INA219_BUS_VOLTAGE_SHIFT,
155156
.bus_voltage_lsb = 4000,
156-
.power_lsb = 20000,
157+
.power_lsb_factor = 20,
157158
.chip_id = ina219,
158159
},
159160
[ina226] = {
160161
.config_default = INA226_CONFIG_DEFAULT,
161-
.calibration_factor = 5120000,
162+
.calibration_value = 2048,
162163
.shunt_voltage_lsb = 2500,
163164
.bus_voltage_shift = 0,
164165
.bus_voltage_lsb = 1250,
165-
.power_lsb = 25000,
166+
.power_lsb_factor = 25,
166167
.chip_id = ina226,
167168
},
168169
};
@@ -227,16 +228,26 @@ static int ina2xx_read_raw(struct iio_dev *indio_dev,
227228
*val2 = 1000;
228229
return IIO_VAL_FRACTIONAL;
229230

230-
case INA2XX_POWER:
231-
/* processed (mW) = raw*lsb (uW) / 1000 */
232-
*val = chip->config->power_lsb;
233-
*val2 = 1000;
231+
case INA2XX_CURRENT:
232+
/*
233+
* processed (mA) = raw * current_lsb (mA)
234+
* current_lsb (mA) = shunt_voltage_lsb (nV) /
235+
* shunt_resistor (uOhm)
236+
*/
237+
*val = chip->config->shunt_voltage_lsb;
238+
*val2 = chip->shunt_resistor_uohm;
234239
return IIO_VAL_FRACTIONAL;
235240

236-
case INA2XX_CURRENT:
237-
/* processed (mA) = raw (mA) */
238-
*val = 1;
239-
return IIO_VAL_INT;
241+
case INA2XX_POWER:
242+
/*
243+
* processed (mW) = raw * power_lsb (mW)
244+
* power_lsb (mW) = power_lsb_factor (mW/mA) *
245+
* current_lsb (mA)
246+
*/
247+
*val = chip->config->power_lsb_factor *
248+
chip->config->shunt_voltage_lsb;
249+
*val2 = chip->shunt_resistor_uohm;
250+
return IIO_VAL_FRACTIONAL;
240251
}
241252

242253
case IIO_CHAN_INFO_HARDWAREGAIN:
@@ -541,25 +552,21 @@ static ssize_t ina2xx_allow_async_readout_store(struct device *dev,
541552
}
542553

543554
/*
544-
* Set current LSB to 1mA, shunt is in uOhms
545-
* (equation 13 in datasheet). We hardcode a Current_LSB
546-
* of 1.0 x10-3. The only remaining parameter is RShunt.
547-
* There is no need to expose the CALIBRATION register
548-
* to the user for now. But we need to reset this register
549-
* if the user updates RShunt after driver init, e.g upon
550-
* reading an EEPROM/Probe-type value.
555+
* Calibration register is set to the best value, which eliminates
556+
* truncation errors on calculating current register in hardware.
557+
* According to datasheet (INA 226: eq. 3, INA219: eq. 4) the best values
558+
* are 2048 for ina226 and 4096 for ina219. They are hardcoded as
559+
* calibration_value.
551560
*/
552561
static int ina2xx_set_calibration(struct ina2xx_chip_info *chip)
553562
{
554-
u16 regval = DIV_ROUND_CLOSEST(chip->config->calibration_factor,
555-
chip->shunt_resistor_uohm);
556-
557-
return regmap_write(chip->regmap, INA2XX_CALIBRATION, regval);
563+
return regmap_write(chip->regmap, INA2XX_CALIBRATION,
564+
chip->config->calibration_value);
558565
}
559566

560567
static int set_shunt_resistor(struct ina2xx_chip_info *chip, unsigned int val)
561568
{
562-
if (val <= 0 || val > chip->config->calibration_factor)
569+
if (val == 0 || val > INT_MAX)
563570
return -EINVAL;
564571

565572
chip->shunt_resistor_uohm = val;
@@ -592,11 +599,6 @@ static ssize_t ina2xx_shunt_resistor_store(struct device *dev,
592599
if (ret)
593600
return ret;
594601

595-
/* Update the Calibration register */
596-
ret = ina2xx_set_calibration(chip);
597-
if (ret)
598-
return ret;
599-
600602
return len;
601603
}
602604

0 commit comments

Comments
 (0)