Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

hwmon: (ina238) Improve current dynamic range

The best possible dynamic range for current measurements is achieved
if the shunt register value matches the current register value. Adjust
the calibration register as well as fixed and default shunt resistor
values accordingly to achieve this range.

Reviewed-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
Tested-by: Chris Packham <chris.packham@alliedtelesis.co.nz> # INA780
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

+19 -32
+19 -32
drivers/hwmon/ina238.c
··· 51 51 52 52 #define INA238_REGISTERS 0x20 53 53 54 - #define INA238_RSHUNT_DEFAULT 10000 /* uOhm */ 54 + #define INA238_RSHUNT_DEFAULT 2500 /* uOhm */ 55 55 56 56 /* Default configuration of device on reset. */ 57 57 #define INA238_CONFIG_DEFAULT 0 ··· 67 67 * relative to the shunt resistor value within the driver. This is similar to 68 68 * how the ina2xx driver handles current/power scaling. 69 69 * 70 - * The end result of this is that increasing shunt values (from a fixed 20 mOhm 71 - * shunt) increase the effective current/power accuracy whilst limiting the 72 - * range and decreasing shunt values decrease the effective accuracy but 73 - * increase the range. 70 + * To achieve the best possible dynamic range, the value of the shunt voltage 71 + * register should match the value of the current register. With that, the shunt 72 + * voltage of 0x7fff = 32,767 uV = 163,785 uV matches the maximum current, 73 + * and no accuracy is lost. Experiments with a real chip show that this is 74 + * achieved by setting the SHUNT_CAL register to a value of 0x1000 = 4,096. 75 + * Per datasheet, 76 + * SHUNT_CAL = 819.2 x 10^6 x CURRENT_LSB x Rshunt 77 + * = 819,200,000 x CURRENT_LSB x Rshunt 78 + * With SHUNT_CAL set to 4,096, we get 79 + * CURRENT_LSB = 4,096 / (819,200,000 x Rshunt) 80 + * Assuming an Rshunt value of 5 mOhm, we get 81 + * CURRENT_LSB = 4,096 / (819,200,000 x 0.005) = 1mA 82 + * and thus a dynamic range of 1mA ... 32,767mA, which is sufficient for most 83 + * applications. The actual dynamic range is of course determined by the actual 84 + * shunt resistor value. 74 85 * 75 - * The value of the Current register is calculated given the following: 76 - * Current (A) = (shunt voltage register * 5) * calibration / 81920 77 - * 78 - * The maximum shunt voltage is 163.835 mV (0x7fff, ADC_RANGE = 0, gain = 4). 79 - * With the maximum current value of 0x7fff and a fixed shunt value results in 80 - * a calibration value of 16384 (0x4000). 81 - * 82 - * 0x7fff = (0x7fff * 5) * calibration / 81920 83 - * calibration = 0x4000 84 - * 85 - * Equivalent calibration is applied for the Power register (maximum value for 86 - * bus voltage is 102396.875 mV, 0x7fff), where the maximum power that can 87 - * occur is ~16776192 uW (register value 0x147a8): 88 - * 89 - * This scaling means the resulting values for Current and Power registers need 90 - * to be scaled by the difference between the fixed shunt resistor and the 91 - * actual shunt resistor: 92 - * 93 - * shunt = 0x4000 / (819.2 * 10^6) / 0.001 = 20000 uOhms (with 1mA/lsb) 94 - * 95 - * Current (mA) = register value * 20000 / rshunt / 4 * gain 96 - * Power (mW) = 0.2 * register value * 20000 / rshunt / 4 * gain 97 - * (Specific for SQ52206) 98 - * Power (mW) = 0.24 * register value * 20000 / rshunt / 4 * gain 99 - * Energy (uJ) = 16 * 0.24 * register value * 20000 / rshunt / 4 * gain * 1000 86 + * Power and energy values are scaled accordingly. 100 87 */ 101 - #define INA238_CALIBRATION_VALUE 16384 102 - #define INA238_FIXED_SHUNT 20000 88 + #define INA238_CALIBRATION_VALUE 4096 89 + #define INA238_FIXED_SHUNT 5000 103 90 104 91 #define INA238_SHUNT_VOLTAGE_LSB 5000 /* 5 uV/lsb, in nV */ 105 92 #define INA238_BUS_VOLTAGE_LSB 3125000 /* 3.125 mV/lsb, in nV */