hwmon: ina2xx: implement chip reinitialization and add new attributes
Subsystems (2)
v6 cover hwmon: ina2xx: implement chip reinitialization and add new attributes
This series implements a mechanism to detect if the chip is in its POR state and reinitialize it if needed. It also extends the sysfs interface to make the driver configurable at run-time.
The shunt_resistor attribute allows to change the shunt resistance value at run-time in cases where ina2xx used to do the measurement isn't integrated with the shunt.
The avg_rate attribute allows to increase/decrease noise reduction.
v6:
- moved the chip initialization and calibration to a separate function and extended the update_device function to check if the chip hasn't been reset and, if so, reinitialize it,
- added a new variable to struct ina2xx_data which holds current configuration value in order to be able to restore the config after a reset and reinit,
- decided to keep the rshunt value in ina2xx_data in order to be able to restore the shunt value when reinitializing the device,
- other minor fixes and improvements,
v5:
https://www.mail-archive.com/linux-kernel%40vger.kernel.org/msg785734.html
Bartosz Golaszewski (5):
hwmon: ina2xx: reinitialize the chip in case it's been reset
hwmon: ina2xx: remove a stray new line
hwmon: ina2xx: don't accept shunt values greater than the calibration factor
hwmon: ina2xx: make shunt resistance configurable at run-time
hwmon: ina2xx: allow to change the averaging rate at run-time
Documentation/hwmon/ina2xx | 8 +- drivers/hwmon/ina2xx.c | 266 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 243 insertions(+), 31 deletions(-)
Signature · 7 lines
2.1.3 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v6 1/5 hwmon: ina2xx: reinitialize the chip in case it's been reset
Chips from the ina family don't like to be uninitialized. In case the power is cut-off and restored again the calibration register will be reset to 0 and both the power and current registers will remain at 0.
Check the calibration register in ina2xx_update_device() and reinitialize the chip if needed.
--- drivers/hwmon/ina2xx.c | 90 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 25 deletions(-)
drivers/hwmon/ina2xx.c +65 -25
@@ -35,6 +35,7 @@#include <linux/hwmon-sysfs.h> #include <linux/jiffies.h> #include <linux/of.h> +#include <linux/delay.h> #include <linux/platform_data/ina2xx.h>
@@ -64,6 +65,9 @@/* worst case is 68.10 ms (~14.6Hz, ina219) */ #define INA2XX_CONVERSION_RATE 15 +#define INA2XX_MAX_DELAY 69/* worst case delay in ms */ + +#define INA2XX_RSHUNT_DEFAULT 10000enum ina2xx_ids { ina219, ina226 };
@@ -81,6 +85,8 @@ struct ina2xx_data {struct i2c_client *client;const struct ina2xx_config *config; +long rshunt; +struct mutex update_lock;bool valid;unsigned long last_updated;
@@ -110,6 +116,28 @@ static const struct ina2xx_config ina2xx_config[] = { }, }; +/* +* Initialize the configuration and calibration registers. +*/ +static int ina2xx_init (struct ina2xx_data *data) +{ +struct i2c_client *client = data->client; +int ret; + +/* device configuration */ + ret =i2c_smbus_write_word_swapped (client,INA2XX_CONFIG , + data->config->config_default); +if (ret <0 ) +return ret; + +/* +* Set current LSB to 1mA, shunt is in uOhms +* (equation 13 in datasheet). +*/ +return i2c_smbus_write_word_swapped (client,INA2XX_CALIBRATION , + data->config->calibration_factor / data->rshunt); +} +static struct ina2xx_data *ina2xx_update_device (struct device *dev) {struct ina2xx_data *data =dev_get_drvdata (dev);
@@ -121,19 +149,45 @@ static struct ina2xx_data *ina2xx_update_device(struct device *dev)if (time_after (jiffies, data->last_updated +HZ /INA2XX_CONVERSION_RATE ) || !data->valid) { -int i; +int i, rv;dev_dbg (&client->dev,"Starting ina2xx update\n" ); +again:/* Read all registers */ for (i =0 ; i < data->config->registers; i++) { -int rv =i2c_smbus_read_word_swapped (client, i); + rv =i2c_smbus_read_word_swapped (client, i);if (rv <0 ) { ret =ERR_PTR (rv); goto abort; } data->regs[i] = rv; } + +/* +* If the current value in the calibration register is 0, the +* power and current registers will also remain at 0. In case +* the chip has been reset let's check the calibration +* register and reinitialize if needed. +*/ +if (data->regs[INA2XX_CALIBRATION ] =0 ) { +/* Reinitialize the chip */ +dev_warn (dev,"chip not calibrated, reinitializing\n" ); + + rv =ina2xx_init (data); +if (rv <0 ) { + ret =ERR_PTR (rv); + goto abort; + } + +/* +* Let's make sure the power and current registers +* have been updated before trying again. +*/ +mdelay (INA2XX_MAX_DELAY ); + goto again; + } + data->last_updated = jiffies; data->valid =1 ; }
@@ -221,7 +275,6 @@ static int ina2xx_probe(struct i2c_client *client,struct device *dev = &client->dev;struct ina2xx_data *data;struct device *hwmon_dev; -long shunt =10000 ;/* default shunt value 10mOhms */ u32 val;int ret;
@@ -234,41 +287,28 @@ static int ina2xx_probe(struct i2c_client *client,if (dev_get_platdata (dev)) { pdata =dev_get_platdata (dev); - shunt = pdata->shunt_uohms; + data->rshunt = pdata->shunt_uohms; }else if (!of_property_read_u32 (dev->of_node,"shunt-resistor" , &val)) { - shunt = val; + data->rshunt = val; + }else { + data->rshunt =INA2XX_RSHUNT_DEFAULT ; } -if (shunt <=0 ) -return -ENODEV ; -/* set the device type */ data->kind = id->driver_data; data->config = &ina2xx_config[data->kind]; + data->client = client; -/* device configuration */ - ret =i2c_smbus_write_word_swapped (client,INA2XX_CONFIG , - data->config->config_default); -if (ret <0 ) { -dev_err (dev, -"error writing to the config register: %d" , ret); +if (data->rshunt <=0 )return -ENODEV ; - } -/* -* Set current LSB to 1mA, shunt is in uOhms -* (equation 13 in datasheet). -*/ - ret =i2c_smbus_write_word_swapped (client,INA2XX_CALIBRATION , - data->config->calibration_factor / shunt); + ret =ina2xx_init (data);if (ret <0 ) { -dev_err (dev, -"error writing to the calibration register: %d" , ret); +dev_err (dev,"error configuring the device: %d\n" , ret);return -ENODEV ; } - data->client = client;mutex_init (&data->update_lock); hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
@@ -277,7 +317,7 @@ static int ina2xx_probe(struct i2c_client *client,return PTR_ERR (hwmon_dev);dev_info (dev,"power monitor %s (Rshunt = %li uOhm)\n" , - id->name, shunt); + id->name, data->rshunt);return 0 ; }
Signature · 7 lines
2.1.3 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Guenter Roeck
maintainerRe: [PATCH v6 1/5] hwmon: ina2xx: reinitialize the chip in case it's been reset
Quoted 104 lines · Bartosz Golaszewski
Chips from the ina family don't like to be uninitialized. In case the power is cut-off and restored again the calibration register will be reset to 0 and both the power and current registers will remain at 0.
Check the calibration register in
ina2xx_update_device()and reinitialize the chip if needed.Signed-off-by Bartosz Golaszewski <[email protected]>--- drivers/hwmon/ina2xx.c | 90 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 25 deletions(-)drivers/hwmon/ina2xx.c +43
-2@@ -35,6 +35,7 @@#include <linux/hwmon-sysfs.h> #include <linux/jiffies.h> #include <linux/of.h> +#include <linux/delay.h> #include <linux/platform_data/ina2xx.h> @@ -64,6 +65,9 @@/* worst case is 68.10 ms (~14.6Hz, ina219) */ #define INA2XX_CONVERSION_RATE 15 +#define INA2XX_MAX_DELAY 69/* worst case delay in ms */ + +#define INA2XX_RSHUNT_DEFAULT 10000enum ina2xx_ids { ina219, ina226 };@@ -81,6 +85,8 @@ struct ina2xx_data {struct i2c_client *client;const struct ina2xx_config *config; +long rshunt; +struct mutex update_lock;bool valid;unsigned long last_updated;@@ -110,6 +116,28 @@ static const struct ina2xx_config ina2xx_config[] = { }, }; +/* +* Initialize the configuration and calibration registers. +*/ +static int ina2xx_init (struct ina2xx_data *data) +{ +struct i2c_client *client = data->client; +int ret; + +/* device configuration */ + ret =i2c_smbus_write_word_swapped (client,INA2XX_CONFIG , + data->config->config_default); +if (ret <0 ) +return ret; + +/* +* Set current LSB to 1mA, shunt is in uOhms +* (equation 13 in datasheet). +*/ +return i2c_smbus_write_word_swapped (client,INA2XX_CALIBRATION , + data->config->calibration_factor / data->rshunt); +} +static struct ina2xx_data *ina2xx_update_device (struct device *dev) {struct ina2xx_data *data =dev_get_drvdata (dev);@@ -121,19 +149,45 @@ static struct ina2xx_data *ina2xx_update_device(struct device *dev)if (time_after (jiffies, data->last_updated +HZ /INA2XX_CONVERSION_RATE ) || !data->valid) { -int i; +int i, rv;dev_dbg (&client->dev,"Starting ina2xx update\n" ); +again:/* Read all registers */ for (i =0 ; i < data->config->registers; i++) { -int rv =i2c_smbus_read_word_swapped (client, i); + rv =i2c_smbus_read_word_swapped (client, i);if (rv <0 ) { ret =ERR_PTR (rv); goto abort; } data->regs[i] = rv; } + +/* +* If the current value in the calibration register is 0, the +* power and current registers will also remain at 0. In case +* the chip has been reset let's check the calibration +* register and reinitialize if needed. +*/ +if (data->regs[INA2XX_CALIBRATION ] =0 ) { +/* Reinitialize the chip */ +dev_warn (dev,"chip not calibrated, reinitializing\n" ); + + rv =ina2xx_init (data);
drivers/hwmon/ina2xx.c static struct ina2xx_data *ina2xx_update_device(struct device *dev) · line 187
Quoted 10 lines
drivers/hwmon/ina2xx.c +10
-0+if (rv <0 ) { + ret =ERR_PTR (rv); + goto abort; + } + +/* +* Let's make sure the power and current registers +* have been updated before trying again. +*/ +mdelay (INA2XX_MAX_DELAY );
That should be msleep() or usleep_range().
drivers/hwmon/ina2xx.c static struct ina2xx_data *ina2xx_update_device(struct device *dev) · line 188
Quoted 1 line
drivers/hwmon/ina2xx.c +1
-0+ goto again;
That is not a good idea. We'll have to have a retry count to handle this case to avoid an endless loop. Sure, that should be unlikely but would be easy to trigger by instantiating the driver on some different chip which always returns 0 when reading the calibration register.
I would suggest to implement a retry loop. Possibly add a helper function to handle the actual update to avoid deep indentation.
Thanks,
Guenter
Quoted 75 lines
drivers/hwmon/ina2xx.c +11
-23+ } + data->last_updated = jiffies; data->valid =1 ; }@@ -221,7 +275,6 @@ static int ina2xx_probe(struct i2c_client *client,struct device *dev = &client->dev;struct ina2xx_data *data;struct device *hwmon_dev; -long shunt =10000 ;/* default shunt value 10mOhms */ u32 val;int ret;@@ -234,41 +287,28 @@ static int ina2xx_probe(struct i2c_client *client,if (dev_get_platdata (dev)) { pdata =dev_get_platdata (dev); - shunt = pdata->shunt_uohms; + data->rshunt = pdata->shunt_uohms; }else if (!of_property_read_u32 (dev->of_node,"shunt-resistor" , &val)) { - shunt = val; + data->rshunt = val; + }else { + data->rshunt =INA2XX_RSHUNT_DEFAULT ; } -if (shunt <=0 ) -return -ENODEV ; -/* set the device type */ data->kind = id->driver_data; data->config = &ina2xx_config[data->kind]; + data->client = client; -/* device configuration */ - ret =i2c_smbus_write_word_swapped (client,INA2XX_CONFIG , - data->config->config_default); -if (ret <0 ) { -dev_err (dev, -"error writing to the config register: %d" , ret); +if (data->rshunt <=0 )return -ENODEV ; - } -/* -* Set current LSB to 1mA, shunt is in uOhms -* (equation 13 in datasheet). -*/ - ret =i2c_smbus_write_word_swapped (client,INA2XX_CALIBRATION , - data->config->calibration_factor / shunt); + ret =ina2xx_init (data);if (ret <0 ) { -dev_err (dev, -"error writing to the calibration register: %d" , ret); +dev_err (dev,"error configuring the device: %d\n" , ret);return -ENODEV ; } - data->client = client;mutex_init (&data->update_lock); hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,@@ -277,7 +317,7 @@ static int ina2xx_probe(struct i2c_client *client,return PTR_ERR (hwmon_dev);dev_info (dev,"power monitor %s (Rshunt = %li uOhm)\n" , - id->name, shunt); + id->name, data->rshunt);return 0 ; }Signature · 1 line
2.1.3
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v6 2/5 hwmon: ina2xx: remove a stray new line
In reply to Bartosz Golaszewski--- drivers/hwmon/ina2xx.c | 1 - 1 file changed, 1 deletion(-)
drivers/hwmon/ina2xx.c +0 -1
@@ -52,7 +52,6 @@#define INA226_ALERT_LIMIT 0x07#define INA226_DIE_ID 0xFF -/* register count */ #define INA219_REGISTERS 6#define INA226_REGISTERS 8
Signature · 7 lines
2.1.3 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v6 3/5 hwmon: ina2xx: don't accept shunt values greater than the calibration factor
In reply to Bartosz GolaszewskiShunt resistance values greater than the chip's calibration factor make no sense since the actual value written to the register equals:
<calibration factor> / <shunt>
Bail-out from ina2xx_probe() if the configured value is greater than the calibration factor.
--- drivers/hwmon/ina2xx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/ina2xx.c +2 -1
@@ -299,7 +299,8 @@ static int ina2xx_probe(struct i2c_client *client, data->config = &ina2xx_config[data->kind]; data->client = client; -if (data->rshunt <=0 ) +if (data->rshunt <=0 || + data->rshunt > data->config->calibration_factor)return -ENODEV ; ret =ina2xx_init (data);
Signature · 7 lines
2.1.3 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v6 4/5 hwmon: ina2xx: make shunt resistance configurable at run-time
In reply to Bartosz GolaszewskiThe shunt resistance can only be set via platform_data or device tree. This isn't suitable for devices in which the shunt resistance can change/isn't known at boot-time.
Add a sysfs attribute that allows to read and set the shunt resistance.
--- Documentation/hwmon/ina2xx | 5 +++-- drivers/hwmon/ina2xx.c | 48 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-)
Documentation/hwmon/ina2xx +3 -2
@@ -44,6 +44,7 @@ The INA226 monitors both a shunt voltage drop and bus supply voltage. The INA230 is a high or low side current shunt and power monitor with an I2C interface. The INA230 monitors both a shunt voltage drop and bus supply voltage. -The shunt value in micro-ohms can be set via platform data or device tree. -Please refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings +The shunt value in micro-ohms can be set via platform data or device tree at +compile-time or via the shunt_resistor attribute in sysfs at run-time. Please +refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings if the device tree is used.
drivers/hwmon/ina2xx.c +46 -2
84 lines not on this page · raw message
Signature · 7 lines
2.1.3 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
PATCH v6 5/5 hwmon: ina2xx: allow to change the averaging rate at run-time
In reply to Bartosz GolaszewskiThe averaging rate of ina226 is hardcoded to 16 in the driver. Make it modifiable at run-time via a new sysfs attribute.
While we're at it - add an additional variable to ina2xx_data, which holds the current configuration settings - this way we'll be able to restore the configuration in case of an unexpected chip reset.
--- Documentation/hwmon/ina2xx | 3 ++ drivers/hwmon/ina2xx.c | 132 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 131 insertions(+), 4 deletions(-)
Documentation/hwmon/ina2xx +3 -0
@@ -48,3 +48,6 @@ The shunt value in micro-ohms can be set via platform data or device tree at compile-time or via the shunt_resistor attribute in sysfs at run-time. Please refer to the Documentation/devicetree/bindings/i2c/ina2xx.txt for bindings if the device tree is used. + +The averaging rate of INA226 and INA230 can be changed via the avg_rate sysfs +attribute. The available rates are: 1, 4, 16, 64, 128, 256, 512 and 1024.
drivers/hwmon/ina2xx.c +128 -4
199 lines not on this page · raw message
Signature · 7 lines
2.1.3 _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors