|
Takashi Iwai |
36cf33 |
From 5d389b125186cf254ad5b8015763ac07c151aea4 Mon Sep 17 00:00:00 2001
|
|
Takashi Iwai |
36cf33 |
From: Maciej Purski <m.purski@samsung.com>
|
|
Takashi Iwai |
36cf33 |
Date: Wed, 22 Nov 2017 16:32:15 +0100
|
|
Takashi Iwai |
36cf33 |
Subject: [PATCH] hwmon: (ina2xx) Make calibration register value fixed
|
|
Takashi Iwai |
36cf33 |
Git-commit: 5d389b125186cf254ad5b8015763ac07c151aea4
|
|
Takashi Iwai |
36cf33 |
Patch-mainline: v4.16-rc1
|
|
Takashi Iwai |
36cf33 |
References: bsc#1051510
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
Calibration register is used for calculating current register in
|
|
Takashi Iwai |
36cf33 |
hardware according to datasheet:
|
|
Takashi Iwai |
36cf33 |
current = shunt_volt * calib_register / 2048 (ina 226)
|
|
Takashi Iwai |
36cf33 |
current = shunt_volt * calib_register / 4096 (ina 219)
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
Fix calib_register value to 2048 for ina226 and 4096 for ina 219 in
|
|
Takashi Iwai |
36cf33 |
order to avoid truncation error and provide best precision allowed
|
|
Takashi Iwai |
36cf33 |
by shunt_voltage measurement. Make current scale value follow changes
|
|
Takashi Iwai |
36cf33 |
of shunt_resistor from sysfs as calib_register value is now fixed.
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
Power_lsb value should also follow shunt_resistor changes as stated in
|
|
Takashi Iwai |
36cf33 |
Datasheet:
|
|
Takashi Iwai |
36cf33 |
power_lsb = 25 * current_lsb (ina 226)
|
|
Takashi Iwai |
36cf33 |
power_lsb = 20 * current_lsb (ina 219)
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
Signed-off-by: Maciej Purski <m.purski@samsung.com>
|
|
Takashi Iwai |
36cf33 |
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
|
|
Takashi Iwai |
36cf33 |
Acked-by: Takashi Iwai <tiwai@suse.de>
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
---
|
|
Takashi Iwai |
36cf33 |
drivers/hwmon/ina2xx.c | 87 ++++++++++++++++++++++++++++---------------------
|
|
Takashi Iwai |
36cf33 |
1 file changed, 50 insertions(+), 37 deletions(-)
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
--- a/drivers/hwmon/ina2xx.c
|
|
Takashi Iwai |
36cf33 |
+++ b/drivers/hwmon/ina2xx.c
|
|
Takashi Iwai |
36cf33 |
@@ -95,18 +95,20 @@ enum ina2xx_ids { ina219, ina226 };
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
struct ina2xx_config {
|
|
Takashi Iwai |
36cf33 |
u16 config_default;
|
|
Takashi Iwai |
36cf33 |
- int calibration_factor;
|
|
Takashi Iwai |
36cf33 |
+ int calibration_value;
|
|
Takashi Iwai |
36cf33 |
int registers;
|
|
Takashi Iwai |
36cf33 |
int shunt_div;
|
|
Takashi Iwai |
36cf33 |
int bus_voltage_shift;
|
|
Takashi Iwai |
36cf33 |
int bus_voltage_lsb; /* uV */
|
|
Takashi Iwai |
36cf33 |
- int power_lsb; /* uW */
|
|
Takashi Iwai |
36cf33 |
+ int power_lsb_factor;
|
|
Takashi Iwai |
36cf33 |
};
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
struct ina2xx_data {
|
|
Takashi Iwai |
36cf33 |
const struct ina2xx_config *config;
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
long rshunt;
|
|
Takashi Iwai |
36cf33 |
+ long current_lsb_uA;
|
|
Takashi Iwai |
36cf33 |
+ long power_lsb_uW;
|
|
Takashi Iwai |
36cf33 |
struct mutex config_lock;
|
|
Takashi Iwai |
36cf33 |
struct regmap *regmap;
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
@@ -116,21 +118,21 @@ struct ina2xx_data {
|
|
Takashi Iwai |
36cf33 |
static const struct ina2xx_config ina2xx_config[] = {
|
|
Takashi Iwai |
36cf33 |
[ina219] = {
|
|
Takashi Iwai |
36cf33 |
.config_default = INA219_CONFIG_DEFAULT,
|
|
Takashi Iwai |
36cf33 |
- .calibration_factor = 40960000,
|
|
Takashi Iwai |
36cf33 |
+ .calibration_value = 4096,
|
|
Takashi Iwai |
36cf33 |
.registers = INA219_REGISTERS,
|
|
Takashi Iwai |
36cf33 |
.shunt_div = 100,
|
|
Takashi Iwai |
36cf33 |
.bus_voltage_shift = 3,
|
|
Takashi Iwai |
36cf33 |
.bus_voltage_lsb = 4000,
|
|
Takashi Iwai |
36cf33 |
- .power_lsb = 20000,
|
|
Takashi Iwai |
36cf33 |
+ .power_lsb_factor = 20,
|
|
Takashi Iwai |
36cf33 |
},
|
|
Takashi Iwai |
36cf33 |
[ina226] = {
|
|
Takashi Iwai |
36cf33 |
.config_default = INA226_CONFIG_DEFAULT,
|
|
Takashi Iwai |
36cf33 |
- .calibration_factor = 5120000,
|
|
Takashi Iwai |
36cf33 |
+ .calibration_value = 2048,
|
|
Takashi Iwai |
36cf33 |
.registers = INA226_REGISTERS,
|
|
Takashi Iwai |
36cf33 |
.shunt_div = 400,
|
|
Takashi Iwai |
36cf33 |
.bus_voltage_shift = 0,
|
|
Takashi Iwai |
36cf33 |
.bus_voltage_lsb = 1250,
|
|
Takashi Iwai |
36cf33 |
- .power_lsb = 25000,
|
|
Takashi Iwai |
36cf33 |
+ .power_lsb_factor = 25,
|
|
Takashi Iwai |
36cf33 |
},
|
|
Takashi Iwai |
36cf33 |
};
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
@@ -169,12 +171,16 @@ static u16 ina226_interval_to_reg(int in
|
|
Takashi Iwai |
36cf33 |
return INA226_SHIFT_AVG(avg_bits);
|
|
Takashi Iwai |
36cf33 |
}
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
+/*
|
|
Takashi Iwai |
36cf33 |
+ * Calibration register is set to the best value, which eliminates
|
|
Takashi Iwai |
36cf33 |
+ * truncation errors on calculating current register in hardware.
|
|
Takashi Iwai |
36cf33 |
+ * According to datasheet (eq. 3) the best values are 2048 for
|
|
Takashi Iwai |
36cf33 |
+ * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
|
|
Takashi Iwai |
36cf33 |
+ */
|
|
Takashi Iwai |
36cf33 |
static int ina2xx_calibrate(struct ina2xx_data *data)
|
|
Takashi Iwai |
36cf33 |
{
|
|
Takashi Iwai |
36cf33 |
- u16 val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
|
|
Takashi Iwai |
36cf33 |
- data->rshunt);
|
|
Takashi Iwai |
36cf33 |
-
|
|
Takashi Iwai |
36cf33 |
- return regmap_write(data->regmap, INA2XX_CALIBRATION, val);
|
|
Takashi Iwai |
36cf33 |
+ return regmap_write(data->regmap, INA2XX_CALIBRATION,
|
|
Takashi Iwai |
36cf33 |
+ data->config->calibration_value);
|
|
Takashi Iwai |
36cf33 |
}
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
/*
|
|
Takashi Iwai |
36cf33 |
@@ -187,10 +193,6 @@ static int ina2xx_init(struct ina2xx_dat
|
|
Takashi Iwai |
36cf33 |
if (ret < 0)
|
|
Takashi Iwai |
36cf33 |
return ret;
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
- /*
|
|
Takashi Iwai |
36cf33 |
- * Set current LSB to 1mA, shunt is in uOhms
|
|
Takashi Iwai |
36cf33 |
- * (equation 13 in datasheet).
|
|
Takashi Iwai |
36cf33 |
- */
|
|
Takashi Iwai |
36cf33 |
return ina2xx_calibrate(data);
|
|
Takashi Iwai |
36cf33 |
}
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
@@ -268,15 +270,15 @@ static int ina2xx_get_value(struct ina2x
|
|
Takashi Iwai |
36cf33 |
val = DIV_ROUND_CLOSEST(val, 1000);
|
|
Takashi Iwai |
36cf33 |
break;
|
|
Takashi Iwai |
36cf33 |
case INA2XX_POWER:
|
|
Takashi Iwai |
36cf33 |
- val = regval * data->config->power_lsb;
|
|
Takashi Iwai |
36cf33 |
+ val = regval * data->power_lsb_uW;
|
|
Takashi Iwai |
36cf33 |
break;
|
|
Takashi Iwai |
36cf33 |
case INA2XX_CURRENT:
|
|
Takashi Iwai |
36cf33 |
- /* signed register, LSB=1mA (selected), in mA */
|
|
Takashi Iwai |
36cf33 |
- val = (s16)regval;
|
|
Takashi Iwai |
36cf33 |
+ /* signed register, result in mA */
|
|
Takashi Iwai |
36cf33 |
+ val = regval * data->current_lsb_uA;
|
|
Takashi Iwai |
36cf33 |
+ val = DIV_ROUND_CLOSEST(val, 1000);
|
|
Takashi Iwai |
36cf33 |
break;
|
|
Takashi Iwai |
36cf33 |
case INA2XX_CALIBRATION:
|
|
Takashi Iwai |
36cf33 |
- val = DIV_ROUND_CLOSEST(data->config->calibration_factor,
|
|
Takashi Iwai |
36cf33 |
- regval);
|
|
Takashi Iwai |
36cf33 |
+ val = regval;
|
|
Takashi Iwai |
36cf33 |
break;
|
|
Takashi Iwai |
36cf33 |
default:
|
|
Takashi Iwai |
36cf33 |
/* programmer goofed */
|
|
Takashi Iwai |
36cf33 |
@@ -304,9 +306,32 @@ static ssize_t ina2xx_show_value(struct
|
|
Takashi Iwai |
36cf33 |
ina2xx_get_value(data, attr->index, regval));
|
|
Takashi Iwai |
36cf33 |
}
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
-static ssize_t ina2xx_set_shunt(struct device *dev,
|
|
Takashi Iwai |
36cf33 |
- struct device_attribute *da,
|
|
Takashi Iwai |
36cf33 |
- const char *buf, size_t count)
|
|
Takashi Iwai |
36cf33 |
+/*
|
|
Takashi Iwai |
36cf33 |
+ * In order to keep calibration register value fixed, the product
|
|
Takashi Iwai |
36cf33 |
+ * of current_lsb and shunt_resistor should also be fixed and equal
|
|
Takashi Iwai |
36cf33 |
+ * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
|
|
Takashi Iwai |
36cf33 |
+ * to keep the scale.
|
|
Takashi Iwai |
36cf33 |
+ */
|
|
Takashi Iwai |
36cf33 |
+static int ina2xx_set_shunt(struct ina2xx_data *data, long val)
|
|
Takashi Iwai |
36cf33 |
+{
|
|
Takashi Iwai |
36cf33 |
+ unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
|
|
Takashi Iwai |
36cf33 |
+ data->config->shunt_div);
|
|
Takashi Iwai |
36cf33 |
+ if (val <= 0 || val > dividend)
|
|
Takashi Iwai |
36cf33 |
+ return -EINVAL;
|
|
Takashi Iwai |
36cf33 |
+
|
|
Takashi Iwai |
36cf33 |
+ mutex_lock(&data->config_lock);
|
|
Takashi Iwai |
36cf33 |
+ data->rshunt = val;
|
|
Takashi Iwai |
36cf33 |
+ data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
|
|
Takashi Iwai |
36cf33 |
+ data->power_lsb_uW = data->config->power_lsb_factor *
|
|
Takashi Iwai |
36cf33 |
+ data->current_lsb_uA;
|
|
Takashi Iwai |
36cf33 |
+ mutex_unlock(&data->config_lock);
|
|
Takashi Iwai |
36cf33 |
+
|
|
Takashi Iwai |
36cf33 |
+ return 0;
|
|
Takashi Iwai |
36cf33 |
+}
|
|
Takashi Iwai |
36cf33 |
+
|
|
Takashi Iwai |
36cf33 |
+static ssize_t ina2xx_store_shunt(struct device *dev,
|
|
Takashi Iwai |
36cf33 |
+ struct device_attribute *da,
|
|
Takashi Iwai |
36cf33 |
+ const char *buf, size_t count)
|
|
Takashi Iwai |
36cf33 |
{
|
|
Takashi Iwai |
36cf33 |
unsigned long val;
|
|
Takashi Iwai |
36cf33 |
int status;
|
|
Takashi Iwai |
36cf33 |
@@ -316,18 +341,9 @@ static ssize_t ina2xx_set_shunt(struct d
|
|
Takashi Iwai |
36cf33 |
if (status < 0)
|
|
Takashi Iwai |
36cf33 |
return status;
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
- if (val == 0 ||
|
|
Takashi Iwai |
36cf33 |
- /* Values greater than the calibration factor make no sense. */
|
|
Takashi Iwai |
36cf33 |
- val > data->config->calibration_factor)
|
|
Takashi Iwai |
36cf33 |
- return -EINVAL;
|
|
Takashi Iwai |
36cf33 |
-
|
|
Takashi Iwai |
36cf33 |
- mutex_lock(&data->config_lock);
|
|
Takashi Iwai |
36cf33 |
- data->rshunt = val;
|
|
Takashi Iwai |
36cf33 |
- status = ina2xx_calibrate(data);
|
|
Takashi Iwai |
36cf33 |
- mutex_unlock(&data->config_lock);
|
|
Takashi Iwai |
36cf33 |
+ status = ina2xx_set_shunt(data, val);
|
|
Takashi Iwai |
36cf33 |
if (status < 0)
|
|
Takashi Iwai |
36cf33 |
return status;
|
|
Takashi Iwai |
36cf33 |
-
|
|
Takashi Iwai |
36cf33 |
return count;
|
|
Takashi Iwai |
36cf33 |
}
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
@@ -387,7 +403,7 @@ static SENSOR_DEVICE_ATTR(power1_input,
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
/* shunt resistance */
|
|
Takashi Iwai |
36cf33 |
static SENSOR_DEVICE_ATTR(shunt_resistor, S_IRUGO | S_IWUSR,
|
|
Takashi Iwai |
36cf33 |
- ina2xx_show_value, ina2xx_set_shunt,
|
|
Takashi Iwai |
36cf33 |
+ ina2xx_show_value, ina2xx_store_shunt,
|
|
Takashi Iwai |
36cf33 |
INA2XX_CALIBRATION);
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
/* update interval (ina226 only) */
|
|
Takashi Iwai |
36cf33 |
@@ -448,10 +464,7 @@ static int ina2xx_probe(struct i2c_clien
|
|
Takashi Iwai |
36cf33 |
val = INA2XX_RSHUNT_DEFAULT;
|
|
Takashi Iwai |
36cf33 |
}
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
- if (val <= 0 || val > data->config->calibration_factor)
|
|
Takashi Iwai |
36cf33 |
- return -ENODEV;
|
|
Takashi Iwai |
36cf33 |
-
|
|
Takashi Iwai |
36cf33 |
- data->rshunt = val;
|
|
Takashi Iwai |
36cf33 |
+ ina2xx_set_shunt(data, val);
|
|
Takashi Iwai |
36cf33 |
|
|
Takashi Iwai |
36cf33 |
ina2xx_regmap_config.max_register = data->config->registers;
|
|
Takashi Iwai |
36cf33 |
|