hwmon: ina238 backports
Subsystems (2)
cover hwmon: ina238 backports
From: Peter Yin <[email protected]>
hwmon: (ina238) Correctly clamp temperature
hwmon: (ina238) Correctly clamp shunt voltage limit
hwmon: (ina238) Correctly clamp power limits
hwmon: (ina238) Report energy in microjoules
hwmon: (ina238) Fix inconsistent whitespace
hwmon: (ina238) Add support for INA228
hwmon: (ina238) Add ina238_config to save configurations for different
hwmon: (ina238) Add support for SQ52206
hwmon: (ina238) Modify the calculation formula to adapt to different
from 024e5cf24327184bb363b6e330550394982b93f8
to c2623573178bab32990695fb729e9b69710ed66d
Chris Packham (1):
hwmon: (ina238) Correctly clamp temperature
Guenter Roeck (2):
hwmon: (ina238) Correctly clamp shunt voltage limit
hwmon: (ina238) Correctly clamp power limits
Jonas Rebmann (3):
hwmon: (ina238) Report energy in microjoules
hwmon: (ina238) Fix inconsistent whitespace
hwmon: (ina238) Add support for INA228
Wenliang Yan (3):
hwmon: (ina238) Add ina238_config to save configurations for different
chips
hwmon: (ina238) Add support for SQ52206
hwmon: (ina238) Modify the calculation formula to adapt to different
chips
Documentation/hwmon/ina238.rst | 15 ++ drivers/hwmon/ina238.c | 323 +++++++++++++++++++++++++++++---- 2 files changed, 298 insertions(+), 40 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/9 hwmon: (ina238) Add ina238_config to save configurations for different chips
From: Wenliang Yan <[email protected]>
Add structure ina238_config to store proprietary properties for different chips to meet different chip adaptations
--- drivers/hwmon/ina238.c | 57 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 5 deletions(-)
drivers/hwmon/ina238.c +52 -5
@@ -102,7 +102,20 @@ static const struct regmap_config ina238_regmap_config = { .val_bits = 16, }; +enum ina238_ids { ina238, ina237 }; + +struct ina238_config { +bool has_power_highest;/* chip detection power peak */ +bool has_energy;/* chip detection energy */ +u8 temp_shift;/* fixed parameters for temp calculate */ +u32 power_calculate_factor;/* fixed parameters for power calculate */ +u16 config_default;/* Power-on default state */ +int bus_voltage_lsb;/* use for temperature calculate, uV/lsb */ +int temp_lsb;/* use for temperature calculate */ +}; +struct ina238_data { +const struct ina238_config *config;struct i2c_client *client;struct mutex config_lock;struct regmap *regmap;
@@ -110,6 +123,27 @@ struct ina238_data {int gain; }; +static const struct ina238_config ina238_config[] = { + [ina238] = { + .has_energy = false, + .has_power_highest = false, + .temp_shift =4 , + .power_calculate_factor =20 , + .config_default =INA238_CONFIG_DEFAULT , + .bus_voltage_lsb =INA238_BUS_VOLTAGE_LSB , + .temp_lsb =INA238_DIE_TEMP_LSB , + }, + [ina237] = { + .has_energy = false, + .has_power_highest = false, + .temp_shift =4 , + .power_calculate_factor =20 , + .config_default =INA238_CONFIG_DEFAULT , + .bus_voltage_lsb =INA238_BUS_VOLTAGE_LSB , + .temp_lsb =INA238_DIE_TEMP_LSB , + }, +}; +static int ina238_read_reg24 (const struct i2c_client *client,u8 reg,u32 *val) {u8 data[3 ];
@@ -536,14 +570,20 @@ static int ina238_probe(struct i2c_client *client)struct device *dev = &client->dev;struct device *hwmon_dev;struct ina238_data *data; +enum ina238_ids chip;int config;int ret; + chip = (uintptr_t )i2c_get_match_data (client); + data =devm_kzalloc (dev,sizeof (*data),GFP_KERNEL );if (!data)return -ENOMEM ; data->client = client; +/* set the device type */ + data->config = &ina238_config[chip]; +mutex_init (&data->config_lock); data->regmap =devm_regmap_init_i2c (client, &ina238_regmap_config);
@@ -570,7 +610,7 @@ static int ina238_probe(struct i2c_client *client) }/* Setup CONFIG register */ - config =INA238_CONFIG_DEFAULT ; + config = data->config->config_default;if (data->gain ==1 ) config |=INA238_CONFIG_ADCRANGE ;/* ADCRANGE = 1 is /1 */ ret =regmap_write (data->regmap,INA238_CONFIG , config);
@@ -616,15 +656,22 @@ static int ina238_probe(struct i2c_client *client) }static const struct i2c_device_id ina238_id[] = { - {"ina238" }, + {"ina237" , ina237 }, + {"ina238" , ina238 }, { } };MODULE_DEVICE_TABLE (i2c, ina238_id);static const struct of_device_id __maybe_unused ina238_of_match[] = { - { .compatible ="ti,ina237" }, - { .compatible ="ti,ina238" }, - { }, + { + .compatible ="ti,ina237" , + .data = (void *)ina237 + }, + { + .compatible ="ti,ina238" , + .data = (void *)ina238 + }, + { } };MODULE_DEVICE_TABLE (of, ina238_of_match);
Signature · 1 line
2.43.0
PATCH 2/9 hwmon: (ina238) Add support for SQ52206
In reply to Peter YinFrom: Wenliang Yan <[email protected]>
Add support for SQ52206 to the Ina238 driver. Add registers, increase compatibility, add compatibility programs for multiple chips.
--- Documentation/hwmon/ina238.rst | 15 +++++ drivers/hwmon/ina238.c | 116 ++++++++++++++++++++++++++++++--- 2 files changed, 122 insertions(+), 9 deletions(-)
Documentation/hwmon/ina238.rst +15 -0
@@ -14,6 +14,12 @@ Supported chips: Datasheet: https://www.ti.com/lit/gpn/ina238 + * Silergy SQ52206 + + Prefix: 'SQ52206' + + Addresses: I2C 0x40 - 0x4f + Author: Nathan Rossi <[email protected]> Description
@@ -54,3 +60,12 @@ temp1_input Die temperature measurement (mC) temp1_max Maximum die temperature threshold (mC) temp1_max_alarm Maximum die temperature alarm ======================= ======================================================= + +Additional sysfs entries for sq52206 +------------------------------------ + +======================= ======================================================= +energy1_input Energy measurement (mJ) + +power1_input_highest Peak Power (uW) +======================= =======================================================
drivers/hwmon/ina238.c +107 -9
250 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 3/9 hwmon: (ina238) Modify the calculation formula to adapt to different chips
In reply to Peter YinFrom: Wenliang Yan <[email protected]>
Modify the calculation formula to adapt to different chips.
--- drivers/hwmon/ina238.c | 47 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-)
drivers/hwmon/ina238.c +24 -23
@@ -270,10 +270,10 @@ static int ina238_read_in(struct device *dev, u32 attr, int channel, regval = (s16 )regval;if (channel ==0 )/* gain of 1 -> LSB / 4 */ - *val = (regval *INA238_SHUNT_VOLTAGE_LSB ) / - (1000 * (4 - data->gain +1 )); + *val = (regval *INA238_SHUNT_VOLTAGE_LSB ) * + data->gain / (1000 *4 );else - *val = (regval *INA238_BUS_VOLTAGE_LSB ) /1000 ; + *val = (regval * data->config->bus_voltage_lsb) /1000 ;break ;case hwmon_in_max_alarm:case hwmon_in_min_alarm:
@@ -298,8 +298,8 @@ static int ina238_write_in(struct device *dev, u32 attr, int channel,case 0 :/* signed value, clamp to max range +/-163 mV */ regval =clamp_val (val,-163 ,163 ); - regval = (regval *1000 * (4 - data->gain +1 )) / -INA238_SHUNT_VOLTAGE_LSB ; + regval = (regval *1000 *4 ) / + (INA238_SHUNT_VOLTAGE_LSB * data->gain); regval =clamp_val (regval,S16_MIN ,S16_MAX );switch (attr) {
@@ -315,7 +315,7 @@ static int ina238_write_in(struct device *dev, u32 attr, int channel,case 1 :/* signed value, positive values only. Clamp to max 102.396 V */ regval =clamp_val (val,0 ,102396 ); - regval = (regval *1000 ) /INA238_BUS_VOLTAGE_LSB ; + regval = (regval *1000 ) / data->config->bus_voltage_lsb; regval =clamp_val (regval,0 ,S16_MAX );switch (attr) {
@@ -370,8 +370,8 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)return err;/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */ - power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * - data->gain,20 * data->rshunt); + power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * data->gain * + data->config->power_calculate_factor,4 *100 * data->rshunt);/* Clamp value to maximum value of long */ *val =clamp_val (power,0 ,LONG_MAX );break ;
@@ -381,8 +381,8 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)return err;/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */ - power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * - data->gain,20 * data->rshunt); + power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * data->gain * + data->config->power_calculate_factor,4 *100 * data->rshunt);/* Clamp value to maximum value of long */ *val =clamp_val (power,0 ,LONG_MAX );break ;
@@ -395,8 +395,8 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)* Truncated 24-bit compare register, lower 8-bits are * truncated. Same conversion to/from uW as POWER register. */ - power =div_u64 ((regval <<8 ) *1000ULL *INA238_FIXED_SHUNT * - data->gain,20 * data->rshunt); + power =div_u64 ((regval <<8 ) *1000ULL *INA238_FIXED_SHUNT * data->gain * + data->config->power_calculate_factor,4 *100 * data->rshunt);/* Clamp value to maximum value of long */ *val =clamp_val (power,0 ,LONG_MAX );break ;
@@ -428,8 +428,8 @@ static int ina238_write_power(struct device *dev, u32 attr, long val)* register. */ regval =clamp_val (val,0 ,LONG_MAX ); - regval =div_u64 (val *20ULL * data->rshunt, -1000ULL *INA238_FIXED_SHUNT * data->gain); + regval =div_u64 (val *4 *100 * data->rshunt, data->config->power_calculate_factor * +1000ULL *INA238_FIXED_SHUNT * data->gain); regval =clamp_val (regval >>8 ,0 ,U16_MAX );return regmap_write (data->regmap,INA238_POWER_LIMIT , regval);
@@ -446,17 +446,17 @@ static int ina238_read_temp(struct device *dev, u32 attr, long *val) err =regmap_read (data->regmap,INA238_DIE_TEMP , ®val);if (err)return err; - -/* Signed, bits 15-4 of register, result in mC */ - *val = ((s16 )regval >>4 ) *INA238_DIE_TEMP_LSB ; +/* Signed, result in mC */ + *val =div_s64 (((s64)((s16 )regval) >> data->config->temp_shift) * + (s64 )data->config->temp_lsb,10000 );break ;case hwmon_temp_max: err =regmap_read (data->regmap,INA238_TEMP_LIMIT , ®val);if (err)return err; - -/* Signed, bits 15-4 of register, result in mC */ - *val = ((s16 )regval >>4 ) *INA238_DIE_TEMP_LSB ; +/* Signed, result in mC */ + *val =div_s64 (((s64)((s16 )regval) >> data->config->temp_shift) * + (s64 )data->config->temp_lsb,10000 );break ;case hwmon_temp_max_alarm: err =regmap_read (data->regmap,INA238_DIAG_ALERT , ®val);
@@ -480,9 +480,10 @@ static int ina238_write_temp(struct device *dev, u32 attr, long val)if (attr != hwmon_temp_max)return -EOPNOTSUPP ; -/* Signed, bits 15-4 of register */ - regval = (val /INA238_DIE_TEMP_LSB ) <<4 ; - regval =clamp_val (regval,S16_MIN ,S16_MAX ) &0xfff0 ; +/* Signed */ + regval =clamp_val (val,-40000 ,125000 ); + regval =div_s64 (val *10000 , data->config->temp_lsb) << data->config->temp_shift; + regval =clamp_val (regval,S16_MIN ,S16_MAX ) & (0xffff << data->config->temp_shift);return regmap_write (data->regmap,INA238_TEMP_LIMIT , regval); }
Signature · 1 line
2.43.0
PATCH 4/9 hwmon: (ina238) Report energy in microjoules
In reply to Peter YinFrom: Jonas Rebmann <[email protected]>
The hwmon sysfs interface specifies that energy values should be reported in microjoules. This is also what tools such as lmsensors expect, reporting wrong values otherwise.
Adjust the driver to scale the output accordingly and adjust ina238 driver documentation.
--- Documentation/hwmon/ina238.rst | 2 +- drivers/hwmon/ina238.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-)
Documentation/hwmon/ina238.rst +1 -1
@@ -65,7 +65,7 @@ Additional sysfs entries for sq52206 ------------------------------------ ======================= ======================================================= -energy1_input Energy measurement (mJ) +energy1_input Energy measurement (uJ) power1_input_highest Peak Power (uW) ======================= =======================================================
drivers/hwmon/ina238.c +4 -4
@@ -97,7 +97,7 @@* Power (mW) = 0.2 * register value * 20000 / rshunt / 4 * gain * (Specific for SQ52206) * Power (mW) = 0.24 * register value * 20000 / rshunt / 4 * gain -* Energy (mJ) = 16 * 0.24 * register value * 20000 / rshunt / 4 * gain +* Energy (uJ) = 16 * 0.24 * register value * 20000 / rshunt / 4 * gain * 1000 */ #define INA238_CALIBRATION_VALUE 16384#define INA238_FIXED_SHUNT 20000
@@ -500,9 +500,9 @@ static ssize_t energy1_input_show(struct device *dev,if (ret)return ret; -/* result in mJ */ - energy =div_u64 (regval *INA238_FIXED_SHUNT * data->gain *16 * - data->config->power_calculate_factor,4 *100 * data->rshunt); +/* result in uJ */ + energy =div_u64 (regval *INA238_FIXED_SHUNT * data->gain *16 *10 * + data->config->power_calculate_factor,4 * data->rshunt);return sysfs_emit (buf,"%llu\n" , energy); }
Signature · 1 line
2.43.0
PATCH 5/9 hwmon: (ina238) Fix inconsistent whitespace
In reply to Peter YinFrom: Jonas Rebmann <[email protected]>
Some purely cosmetic changes in ina238.c:
- When aligning definitions, do so consistently with tab stop of 8.
- Use spaces instead of tabs around operators.
- Align wrapped lines.
--- drivers/hwmon/ina238.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-)
drivers/hwmon/ina238.c +14 -14
@@ -41,7 +41,7 @@#define INA238_CONFIG_ADCRANGE BIT(4)#define SQ52206_CONFIG_ADCRANGE_HIGH BIT(4) -#define SQ52206_CONFIG_ADCRANGE_LOW BIT(3) +#define SQ52206_CONFIG_ADCRANGE_LOW BIT(3)#define INA238_DIAG_ALERT_TMPOL BIT(7)#define INA238_DIAG_ALERT_SHNTOL BIT(6)
@@ -104,7 +104,7 @@#define INA238_SHUNT_VOLTAGE_LSB 5/* 5 uV/lsb */ #define INA238_BUS_VOLTAGE_LSB 3125/* 3.125 mV/lsb */ -#define INA238_DIE_TEMP_LSB 1250000/* 125.0000 mC/lsb */ +#define INA238_DIE_TEMP_LSB 1250000/* 125.0000 mC/lsb */ #define SQ52206_BUS_VOLTAGE_LSB 3750/* 3.75 mV/lsb */ #define SQ52206_DIE_TEMP_LSB 78125/* 7.8125 mC/lsb */
@@ -118,12 +118,12 @@ enum ina238_ids { ina238, ina237, sq52206 };struct ina238_config {bool has_power_highest;/* chip detection power peak */ -bool has_energy;/* chip detection energy */ -u8 temp_shift;/* fixed parameters for temp calculate */ +bool has_energy;/* chip detection energy */ +u8 temp_shift;/* fixed parameters for temp calculate */ u32 power_calculate_factor;/* fixed parameters for power calculate */ -u16 config_default;/* Power-on default state */ +u16 config_default;/* Power-on default state */ int bus_voltage_lsb;/* use for temperature calculate, uV/lsb */ -int temp_lsb;/* use for temperature calculate */ +int temp_lsb;/* use for temperature calculate */ };struct ina238_data {
@@ -271,7 +271,7 @@ static int ina238_read_in(struct device *dev, u32 attr, int channel,if (channel ==0 )/* gain of 1 -> LSB / 4 */ *val = (regval *INA238_SHUNT_VOLTAGE_LSB ) * - data->gain / (1000 *4 ); + data->gain / (1000 *4 );else *val = (regval * data->config->bus_voltage_lsb) /1000 ;break ;
@@ -370,7 +370,7 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)return err;/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */ - power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * data->gain * + power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * data->gain * data->config->power_calculate_factor,4 *100 * data->rshunt);/* Clamp value to maximum value of long */ *val =clamp_val (power,0 ,LONG_MAX );
@@ -381,7 +381,7 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)return err;/* Fixed 1mA lsb, scaled by 1000000 to have result in uW */ - power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * data->gain * + power =div_u64 (regval *1000ULL *INA238_FIXED_SHUNT * data->gain * data->config->power_calculate_factor,4 *100 * data->rshunt);/* Clamp value to maximum value of long */ *val =clamp_val (power,0 ,LONG_MAX );
@@ -395,7 +395,7 @@ static int ina238_read_power(struct device *dev, u32 attr, long *val)* Truncated 24-bit compare register, lower 8-bits are * truncated. Same conversion to/from uW as POWER register. */ - power =div_u64 ((regval <<8 ) *1000ULL *INA238_FIXED_SHUNT * data->gain * + power =div_u64 ((regval <<8 ) *1000ULL *INA238_FIXED_SHUNT * data->gain * data->config->power_calculate_factor,4 *100 * data->rshunt);/* Clamp value to maximum value of long */ *val =clamp_val (power,0 ,LONG_MAX );
@@ -448,7 +448,7 @@ static int ina238_read_temp(struct device *dev, u32 attr, long *val)return err;/* Signed, result in mC */ *val =div_s64 (((s64)((s16 )regval) >> data->config->temp_shift) * - (s64 )data->config->temp_lsb,10000 ); + (s64 )data->config->temp_lsb,10000 );break ;case hwmon_temp_max: err =regmap_read (data->regmap,INA238_TEMP_LIMIT , ®val);
@@ -456,7 +456,7 @@ static int ina238_read_temp(struct device *dev, u32 attr, long *val)return err;/* Signed, result in mC */ *val =div_s64 (((s64)((s16 )regval) >> data->config->temp_shift) * - (s64 )data->config->temp_lsb,10000 ); + (s64 )data->config->temp_lsb,10000 );break ;case hwmon_temp_max_alarm: err =regmap_read (data->regmap,INA238_DIAG_ALERT , ®val);
@@ -501,8 +501,8 @@ static ssize_t energy1_input_show(struct device *dev,return ret;/* result in uJ */ - energy =div_u64 (regval *INA238_FIXED_SHUNT * data->gain *16 *10 * - data->config->power_calculate_factor,4 * data->rshunt); + energy =div_u64 (regval *INA238_FIXED_SHUNT * data->gain *16 *10 * + data->config->power_calculate_factor,4 * data->rshunt);return sysfs_emit (buf,"%llu\n" , energy); }
Signature · 1 line
2.43.0
PATCH 6/9 hwmon: (ina238) Add support for INA228
In reply to Peter YinFrom: Jonas Rebmann <[email protected]>
Add support for the Texas Instruments INA228 Ultra-Precise Power/Energy/Charge Monitor.
The INA228 is very similar to the INA238 but offers four bits of extra precision in the temperature, voltage and current measurement fields. It also supports energy and charge monitoring, the latter of which is not supported through this patch.
While it seems in the datasheet that some constants such as LSB values differ between the 228 and the 238, they differ only for those registers where four bits of precision have been added and they differ by a factor of 16 (VBUS, VSHUNT, DIETEMP, CURRENT).
Therefore, the INA238 constants are still applicable with regard to the bit of the same significance.
--- drivers/hwmon/ina238.c | 106 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 5 deletions(-)
drivers/hwmon/ina238.c +101 -5
@@ -6,6 +6,7 @@* Copyright (C) 2021 Nathan Rossi <[email protected]> */ +#include <linux/bitops.h> #include <linux/err.h> #include <linux/hwmon.h> #include <linux/i2c.h>
@@ -107,6 +108,7 @@#define INA238_DIE_TEMP_LSB 1250000/* 125.0000 mC/lsb */ #define SQ52206_BUS_VOLTAGE_LSB 3750/* 3.75 mV/lsb */ #define SQ52206_DIE_TEMP_LSB 78125/* 7.8125 mC/lsb */ +#define INA228_DIE_TEMP_LSB 78125/* 7.8125 mC/lsb */ static const struct regmap_config ina238_regmap_config = { .max_register =INA238_REGISTERS ,
@@ -114,9 +116,10 @@ static const struct regmap_config ina238_regmap_config = { .val_bits = 16, }; -enum ina238_ids { ina238, ina237, sq52206 }; +enum ina238_ids { ina238, ina237, sq52206, ina228 };struct ina238_config { +bool has_20bit_voltage_current;/* vshunt, vbus and current are 20-bit fields */ bool has_power_highest;/* chip detection power peak */ bool has_energy;/* chip detection energy */ u8 temp_shift;/* fixed parameters for temp calculate */
@@ -137,6 +140,7 @@ struct ina238_data {static const struct ina238_config ina238_config[] = { [ina238] = { + .has_20bit_voltage_current = false, .has_energy = false, .has_power_highest = false, .temp_shift =4 ,
@@ -146,6 +150,7 @@ static const struct ina238_config ina238_config[] = { .temp_lsb = INA238_DIE_TEMP_LSB, }, [ina237] = { + .has_20bit_voltage_current = false, .has_energy = false, .has_power_highest = false, .temp_shift = 4,
@@ -155,6 +160,7 @@ static const struct ina238_config ina238_config[] = { .temp_lsb = INA238_DIE_TEMP_LSB, }, [sq52206] = { + .has_20bit_voltage_current = false, .has_energy = true, .has_power_highest = true, .temp_shift = 0,
@@ -163,6 +169,16 @@ static const struct ina238_config ina238_config[] = { .bus_voltage_lsb = SQ52206_BUS_VOLTAGE_LSB, .temp_lsb = SQ52206_DIE_TEMP_LSB, }, + [ina228] = { + .has_20bit_voltage_current = true, + .has_energy = true, + .has_power_highest = false, + .temp_shift = 0, + .power_calculate_factor = 20, + .config_default = INA238_CONFIG_DEFAULT, + .bus_voltage_lsb = INA238_BUS_VOLTAGE_LSB, + .temp_lsb = INA228_DIE_TEMP_LSB, + }, };static int ina238_read_reg24 (const struct i2c_client *client,u8 reg,u32 *val)
@@ -199,6 +215,65 @@ static int ina238_read_reg40(const struct i2c_client *client, u8 reg, u64 *val)return 0 ; } +static int ina238_read_field_s20 (const struct i2c_client *client,u8 reg,s32 *val) +{ +u32 regval; +int err; + + err =ina238_read_reg24 (client, reg, ®val); +if (err) +return err; + +/* bits 3-0 Reserved, always zero */ + regval >>=4 ; + + *val =sign_extend32 (regval,19 ); + +return 0 ; +} + +static int ina228_read_shunt_voltage (struct device *dev,u32 attr,int channel, +long *val) +{ +struct ina238_data *data =dev_get_drvdata (dev); +int regval; +int err; + + err =ina238_read_field_s20 (data->client,INA238_SHUNT_VOLTAGE , ®val); +if (err) +return err; + +/* +* gain of 1 -> LSB / 4 +* This field has 16 bit on ina238. ina228 adds another 4 bits of +* precision. ina238 conversion factors can still be applied when +* dividing by 16. +*/ + *val = (regval *INA238_SHUNT_VOLTAGE_LSB ) * data->gain / (1000 *4 ) /16 ; +return 0 ; +} + +static int ina228_read_bus_voltage (struct device *dev,u32 attr,int channel, +long *val) +{ +struct ina238_data *data =dev_get_drvdata (dev); +int regval; +int err; + + err =ina238_read_field_s20 (data->client,INA238_BUS_VOLTAGE , ®val); +if (err) +return err; + +/* +* gain of 1 -> LSB / 4 +* This field has 16 bit on ina238. ina228 adds another 4 bits of +* precision. ina238 conversion factors can still be applied when +* dividing by 16. +*/ + *val = (regval * data->config->bus_voltage_lsb) /1000 /16 ; +return 0 ; +} +static int ina238_read_in (struct device *dev,u32 attr,int channel,long *val) {
@@ -211,6 +286,8 @@ static int ina238_read_in(struct device *dev, u32 attr, int channel,case 0 :switch (attr) {case hwmon_in_input: +if (data->config->has_20bit_voltage_current) +return ina228_read_shunt_voltage (dev, attr, channel, val); reg =INA238_SHUNT_VOLTAGE ;break ;case hwmon_in_max:
@@ -234,6 +311,8 @@ static int ina238_read_in(struct device *dev, u32 attr, int channel,case 1 :switch (attr) {case hwmon_in_input: +if (data->config->has_20bit_voltage_current) +return ina228_read_bus_voltage (dev, attr, channel, val); reg =INA238_BUS_VOLTAGE ;break ;case hwmon_in_max:
@@ -341,13 +420,25 @@ static int ina238_read_current(struct device *dev, u32 attr, long *val)switch (attr) {case hwmon_curr_input: - err =regmap_read (data->regmap,INA238_CURRENT , ®val); -if (err <0 ) -return err; +if (data->config->has_20bit_voltage_current) { + err =ina238_read_field_s20 (data->client,INA238_CURRENT , ®val); +if (err) +return err; + }else { + err =regmap_read (data->regmap,INA238_CURRENT , ®val); +if (err <0 ) +return err; +/* sign-extend */ + regval = (s16 )regval; + }/* Signed register, fixed 1mA current lsb. result in mA */ - *val =div_s64 ((s16 )regval *INA238_FIXED_SHUNT * data->gain, + *val =div_s64 ((s64 )regval *INA238_FIXED_SHUNT * data->gain, data->rshunt *4 ); + +/* Account for 4 bit offset */ +if (data->config->has_20bit_voltage_current) + *val /=16 ;break ;default :return -EOPNOTSUPP ;
@@ -750,6 +841,7 @@ static int ina238_probe(struct i2c_client *client) }static const struct i2c_device_id ina238_id[] = { + {"ina228" , ina228 }, {"ina237" , ina237 }, {"ina238" , ina238 }, {"sq52206" , sq52206 },
@@ -758,6 +850,10 @@ static const struct i2c_device_id ina238_id[] = {MODULE_DEVICE_TABLE (i2c, ina238_id);static const struct of_device_id __maybe_unused ina238_of_match[] = { + { + .compatible ="ti,ina228" , + .data = (void *)ina228 + }, { .compatible ="ti,ina237" , .data = (void *)ina237
Signature · 1 line
2.43.0
PATCH 7/9 hwmon: (ina238) Correctly clamp temperature
In reply to Peter YinFrom: Chris Packham <[email protected]>
ina238_write_temp() was attempting to clamp the user input but was throwing away the result. Ensure that we clamp the value to the appropriate range before it is converted into a register value.
--- drivers/hwmon/ina238.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/ina238.c +1 -1
@@ -572,7 +572,7 @@ static int ina238_write_temp(struct device *dev, u32 attr, long val)return -EOPNOTSUPP ;/* Signed */ - regval =clamp_val (val,-40000 ,125000 ); + val =clamp_val (val,-40000 ,125000 ); regval =div_s64 (val *10000 , data->config->temp_lsb) << data->config->temp_shift; regval =clamp_val (regval,S16_MIN ,S16_MAX ) & (0xffff << data->config->temp_shift);
Signature · 1 line
2.43.0
PATCH 8/9 hwmon: (ina238) Correctly clamp shunt voltage limit
In reply to Peter YinFrom: Guenter Roeck <[email protected]>
When clamping a register value, the result needs to be masked against the register size. This was missing, resulting in errors when trying to write negative limits. Fix by masking the clamping result against the register size.
--- drivers/hwmon/ina238.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/ina238.c +1 -1
@@ -379,7 +379,7 @@ static int ina238_write_in(struct device *dev, u32 attr, int channel, regval =clamp_val (val,-163 ,163 ); regval = (regval *1000 *4 ) / (INA238_SHUNT_VOLTAGE_LSB * data->gain); - regval =clamp_val (regval,S16_MIN ,S16_MAX ); + regval =clamp_val (regval,S16_MIN ,S16_MAX ) &0xffff ;switch (attr) {case hwmon_in_max:
Signature · 1 line
2.43.0
PATCH 9/9 hwmon: (ina238) Correctly clamp power limits
In reply to Peter YinFrom: Guenter Roeck <[email protected]>
ina238_write_power() was attempting to clamp the user input but was throwing away the result. Ensure that we clamp the value to the appropriate range before it is converted into a register value.
--- drivers/hwmon/ina238.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hwmon/ina238.c +3 -2
@@ -517,9 +517,10 @@ static int ina238_write_power(struct device *dev, u32 attr, long val)* Unsigned postive values. Compared against the 24-bit power register, * lower 8-bits are truncated. Same conversion to/from uW as POWER * register. +* The first clamp_val() is to establish a baseline to avoid overflows. */ - regval =clamp_val (val,0 ,LONG_MAX ); - regval =div_u64 (val *4 *100 * data->rshunt, data->config->power_calculate_factor * + regval =clamp_val (val,0 ,LONG_MAX /2 ); + regval =div_u64 (regval *4 *100 * data->rshunt, data->config->power_calculate_factor *1000ULL *INA238_FIXED_SHUNT * data->gain); regval =clamp_val (regval >>8 ,0 ,U16_MAX );
Signature · 1 line
2.43.0