power: supply: max17042: support Maxim MAX77759 fuel gauge
Subsystems (4)
v3 cover power: supply: max17042: support Maxim MAX77759 fuel gauge
Hi,
This series adds support for the fuel gauge integrated into the Maxim MAX77759, which is a companion PMIC intended for use in mobile phones and tablets and is used on Google Pixel 6 and 6 Pro (oriole and raven).
Amongst others, the PMIC contains a fuel gauge employing the Maxim ModelGauge m5 algorithm, that is similar to the ones supported by the max17042 driver and binding.
The Maxim ModelGauge m5 algorithm, as well as previous generations like m3 on max17047/max17050, requires the host to save/restore some register values across power cycles to maintain full accuracy. Extending the driver for such support is out of scope in this initial series.
The series starts with binding updates, followed by driver updates and improvements in preparation for finally adding max77759 support.
A DT update for Pixel 6 has been posted in
https://lore.kernel.org/all/[email protected]/
Note: While there was a previous attempt to add support for this fuel gauge via a new driver [1], development seems to have come to a halt, and extending this driver here seems more appropriate. The patches here are unrelated to that other attempt, other than supporting the same device.
Test results:
$ ./test_power_supply_properties.sh max170xx_battery
TAP version 13
1..33
# Testing device max170xx_battery
ok 1 max170xx_battery.exists
ok 2 max170xx_battery.uevent.NAME
ok 3 max170xx_battery.sysfs.type
ok 4 max170xx_battery.uevent.TYPE
ok 5 max170xx_battery.sysfs.usb_type # SKIP
ok 6 max170xx_battery.sysfs.online # SKIP
# Reported: '1' ()
ok 7 max170xx_battery.sysfs.present
# Reported: 'Unknown'
ok 8 max170xx_battery.sysfs.status
# Reported: '92' % ()
ok 9 max170xx_battery.sysfs.capacity
ok 10 max170xx_battery.sysfs.capacity_level # SKIP
ok 11 max170xx_battery.sysfs.model_name # SKIP
ok 12 max170xx_battery.sysfs.manufacturer # SKIP
ok 13 max170xx_battery.sysfs.serial_number # SKIP
# Reported: 'Li-ion'
ok 14 max170xx_battery.sysfs.technology
# Reported: '36032' ()
ok 15 max170xx_battery.sysfs.cycle_count
# Reported: 'System'
ok 16 max170xx_battery.sysfs.scope
ok 17 max170xx_battery.sysfs.input_current_limit # SKIP
ok 18 max170xx_battery.sysfs.input_voltage_limit # SKIP
# Reported: '4323906' uV (4.32391 V)
ok 19 max170xx_battery.sysfs.voltage_now
# Reported: '3660000' uV (3.66 V)
ok 20 max170xx_battery.sysfs.voltage_min
# Reported: '4320000' uV (4.32 V)
ok 21 max170xx_battery.sysfs.voltage_max
# Reported: '3300000' uV (3.3 V)
ok 22 max170xx_battery.sysfs.voltage_min_design
ok 23 max170xx_battery.sysfs.voltage_max_design # SKIP
# Reported: '289687' uA (289.687 mA)
ok 24 max170xx_battery.sysfs.current_now
ok 25 max170xx_battery.sysfs.current_max # SKIP
# Reported: '3942000' uAh (3.942 Ah)
ok 26 max170xx_battery.sysfs.charge_now
# Reported: '4330000' uAh (4.33 Ah)
ok 27 max170xx_battery.sysfs.charge_full
# Reported: '4524000' uAh (4.524 Ah)
ok 28 max170xx_battery.sysfs.charge_full_design
ok 29 max170xx_battery.sysfs.power_now # SKIP
ok 30 max170xx_battery.sysfs.energy_now # SKIP
ok 31 max170xx_battery.sysfs.energy_full # SKIP
ok 32 max170xx_battery.sysfs.energy_full_design # SKIP
ok 33 max170xx_battery.sysfs.energy_full_design # SKIP
# 15 skipped test(s) detected. Consider enabling relevant config options to improve coverage.
# Totals: pass:18 fail:0 xfail:0 xpass:0 skip:15 error:0Cheers,
Andre'
---
Changes in v3:
- update commit message patch 5 (dev_err_probe)
- drop stray comment patch 6 (overflow)
- add Fixes tag to patch 6 (overflow) (Pete)
- collect tags
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- collect tags
- update commit message subject prefix of patch 10 to avoid duplication
- patch 11 (time to full reporting):
- limit to max17055 & max77759, the datasheet for max17047 and
max17050 describes the register as 'reserved'. I was mislead by the comment and enum ordering in max17042_battery.h
- report as POWER_SUPPLY_PROP_TIME_TO_FULL_NOW (not _AVG). The
max17050 datasheet is a bit clearer than the max77759 one on that.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
André Draszik (11):
dt-bindings: power: supply: max17042: add support for max77759
dt-bindings: power: supply: max17042: support shunt-resistor-micro-ohms
dt-bindings: power: supply: max17042: drop formatting specifier |
power: supply: max17042: fix a comment typo (then -> than)
power: supply: max17042: use dev_err_probe() where appropriate
power: supply: max17042: avoid overflow when determining health
power: supply: max17042: time to empty is meaningless when charging
power: supply: max17042: support standard shunt-resistor-micro-ohms DT property
power: supply: max17042: initial support for Maxim MAX77759
power: supply: max17042: consider task period (max77759)
power: supply: max17042: report time to full (max17055 & max77759)
.../bindings/power/supply/maxim,max17042.yaml | 21 ++-- drivers/power/supply/max17042_battery.c | 130 ++++++++++++++++++--- include/linux/power/max17042_battery.h | 25 +++- 3 files changed, 148 insertions(+), 28 deletions(-)
---
base-commit: 3fa5e5702a82d259897bd7e209469bc06368bf31
change-id: 20260226-max77759-fg-0f4bf0816ced
Best regards,
Signature · 1 line
André Draszik <[email protected]>
PATCH v3 1/11 dt-bindings: power: supply: max17042: add support for max77759
The Maxim MAX77759 is a companion PMIC intended for use in mobile phones and tablets. It is used on Google Pixel 6 and 6 Pro (oriole and raven). Amongst others, it contains a fuel gauge that is similar to the ones supported by this binding.
The fuel gauge can measure battery charge and discharge current, battery voltage, battery temperature, and the Type C connector's temperature.
--- Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml +2 -1
@@ -20,6 +20,7 @@ properties: - maxim,max17050 - maxim,max17055 - maxim,max77705-battery + - maxim,max77759-fg - maxim,max77849-battery reg:
@@ -28,7 +29,7 @@ properties: interrupts: maxItems: 1 description: | - The ALRT pin, an open-drain interrupt. + The ALRT pin (or FG_INTB pin on MAX77759), an open-drain interrupt. maxim,rsns-microohm: $ref: /schemas/types.yaml#/definitions/uint32
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 2/11 dt-bindings: power: supply: max17042: support shunt-resistor-micro-ohms
In reply to André DraszikThis binding supports the vendor-specific property maxim,rsns-microohm to describe the value of a shunt resistor required when measuring currents. shunt-resistor-micro-ohms is a standard property with the same meaning. Standard properties should be used instead of vendor- specific ones of similar intention when possible.
Allow this standard property here, while also deprecating the existing vendor-specific property maxim,rsns-microohm.
--- Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml | 6 ++++++ 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml +6 -0
@@ -31,7 +31,13 @@ properties: description: | The ALRT pin (or FG_INTB pin on MAX77759), an open-drain interrupt. + shunt-resistor-micro-ohms: + description: + Resistance of rsns resistor in micro Ohms (datasheet-recommended value is 10000). + Defining this property enables current-sense functionality. + maxim,rsns-microohm: + deprecated: true $ref: /schemas/types.yaml#/definitions/uint32 description: | Resistance of rsns resistor in micro Ohms (datasheet-recommended value is 10000).
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 3/11 dt-bindings: power: supply: max17042: drop formatting specifier |
In reply to André Draszik| denotes a literal (preformatted) block and is not necessary here.
Drop them from this file.
--- .../devicetree/bindings/power/supply/maxim,max17042.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
Documentation/devicetree/bindings/power/supply/maxim,max17042.yaml +6 -6
@@ -28,7 +28,7 @@ properties: interrupts: maxItems: 1 - description: | + description: The ALRT pin (or FG_INTB pin on MAX77759), an open-drain interrupt. shunt-resistor-micro-ohms:
@@ -39,31 +39,31 @@ properties: maxim,rsns-microohm: deprecated: true $ref: /schemas/types.yaml#/definitions/uint32 - description: | + description: Resistance of rsns resistor in micro Ohms (datasheet-recommended value is 10000). Defining this property enables current-sense functionality. maxim,cold-temp: $ref: /schemas/types.yaml#/definitions/uint32 - description: | + description: Temperature threshold to report battery as cold (in tenths of degree Celsius). Default is not to report cold events. maxim,over-heat-temp: $ref: /schemas/types.yaml#/definitions/uint32 - description: | + description: Temperature threshold to report battery as over heated (in tenths of degree Celsius). Default is not to report over heating events. maxim,dead-volt: $ref: /schemas/types.yaml#/definitions/uint32 - description: | + description: Voltage threshold to report battery as dead (in mV). Default is not to report dead battery events. maxim,over-volt: $ref: /schemas/types.yaml#/definitions/uint32 - description: | + description: Voltage threshold to report battery as over voltage (in mV). Default is not to report over-voltage events.
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 4/11 power: supply: max17042: fix a comment typo (then -> than)
In reply to André DraszikFix this trivial typo where than should be used instead of then.
--- drivers/power/supply/max17042_battery.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/supply/max17042_battery.c +1 -1
@@ -131,7 +131,7 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)* FullCAP to match RepCap when it detects end of charging. * * When this cycle the battery gets charged to a higher (calculated) -* capacity then the previous cycle then FullCAP will get updated +* capacity than the previous cycle then FullCAP will get updated * continuously once end-of-charge detection kicks in, so allow the * 2 to differ a bit. */
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 5/11 power: supply: max17042: use dev_err_probe() where appropriate
In reply to André Draszikdev_err_probe() exists to simplify code, harmonise error messages, and set the deferred probe reason if relevant - there's no reason not to use it here.
While at it, return the actual error from devm_regmap_init_i2c() rather than overwriting with -EINVAL, when relevant.
---
v3:
- update commit message regarding devm_regmap_init_i2c()
--- drivers/power/supply/max17042_battery.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-)
drivers/power/supply/max17042_battery.c +9 -12
@@ -1053,16 +1053,14 @@ static int max17042_probe(struct i2c_client *client, struct device *dev, int irq chip->dev = dev; chip->chip_type = chip_type; chip->regmap =devm_regmap_init_i2c (client, &max17042_regmap_config); -if (IS_ERR (chip->regmap)) { -dev_err (dev,"Failed to initialize regmap\n" ); -return -EINVAL ; - } +if (IS_ERR (chip->regmap)) +return dev_err_probe (dev,PTR_ERR (chip->regmap), +"Failed to initialize regmap\n" ); chip->pdata =max17042_get_pdata (chip); -if (!chip->pdata) { -dev_err (dev,"no platform data provided\n" ); -return -EINVAL ; - } +if (!chip->pdata) +return dev_err_probe (dev, -EINVAL , +"no platform data provided\n" );dev_set_drvdata (dev, chip); psy_cfg.drv_data = chip;
@@ -1090,10 +1088,9 @@ static int max17042_probe(struct i2c_client *client, struct device *dev, int irq chip->battery =devm_power_supply_register (dev, max17042_desc, &psy_cfg); -if (IS_ERR (chip->battery)) { -dev_err (dev,"failed: power supply register\n" ); -return PTR_ERR (chip->battery); - } +if (IS_ERR (chip->battery)) +return dev_err_probe (dev,PTR_ERR (chip->battery), +"failed: power supply register\n" );if (irq) {unsigned int flags =IRQF_ONESHOT |IRQF_SHARED |IRQF_PROBE_SHARED ;
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 6/11 power: supply: max17042: avoid overflow when determining health
In reply to André DraszikIf vmax has the default value of INT_MAX (e.g. because not specified in DT), battery health is reported as over-voltage. This is because adding any value to vmax (the vmax tolerance in this case) causes it to wrap around, making it negative and smaller than the measured battery voltage.
Avoid that by using size_add().
---
v3:
- drop a useless comment introduced in earlier versions.
--- drivers/power/supply/max17042_battery.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/supply/max17042_battery.c +1 -1
@@ -201,7 +201,7 @@ static int max17042_get_battery_health(struct max17042_chip *chip, int *health) goto out; } -if (vbatt > chip->pdata->vmax +MAX17042_VMAX_TOLERANCE ) { +if (vbatt >size_add (chip->pdata->vmax,MAX17042_VMAX_TOLERANCE )) { *health =POWER_SUPPLY_HEALTH_OVERVOLTAGE ; goto out; }
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 7/11 power: supply: max17042: time to empty is meaningless when charging
In reply to André DraszikWhen charging, the fuel gauge reports U16_MAX as time to empty. Ignoring this special case (as this driver currently does), causes the remaining time to be reported as ~102hours, which is incorrect.
Update the code to not return anything in this case.
--- drivers/power/supply/max17042_battery.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/power/supply/max17042_battery.c +4 -0
@@ -430,6 +430,10 @@ static int max17042_get_property(struct power_supply *psy,if (ret <0 )return ret; +/* when charging, the value is not meaningful */ +if (data ==U16_MAX ) +return -ENODATA ; + val->intval = data *5625 /1000 ;break ;default :
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 8/11 power: supply: max17042: support standard shunt-resistor-micro-ohms DT property
In reply to André Draszikshunt-resistor-micro-ohms is a standard property used to describe the value of a shunt resistor required when measuring currents. Standard properties should be used instead of vendor-specific ones of similar intention when possible.
Try to read it from DT, and fall back to the vendor-specific property maxim,rsns-microohm if unsuccessful for compatibility with existing DTs.
--- drivers/power/supply/max17042_battery.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/power/supply/max17042_battery.c +5 -1
@@ -925,8 +925,12 @@ max17042_get_of_pdata(struct max17042_chip *chip)/* * Require current sense resistor value to be specified for * current-sense functionality to be enabled at all. +* maxim,rsns-microohm is the property name used by older DTs and kept +* for compatibility. */ -if (of_property_read_u32 (np,"maxim,rsns-microohm" , &prop) ==0 ) { +if ((of_property_read_u32 (np,"shunt-resistor-micro-ohms" , + &prop) ==0 ) || + (of_property_read_u32 (np,"maxim,rsns-microohm" , &prop) ==0 )) { pdata->r_sns = prop; pdata->enable_current_sense = true; }
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 9/11 power: supply: max17042: initial support for Maxim MAX77759
In reply to André DraszikThe Maxim MAX77759 is a companion PMIC intended for use in mobile phones and tablets. It is used on Google Pixel 6 and 6 Pro (oriole and raven). Amongst others, it contains a fuel gauge that is similar to the ones supported by this driver.
The fuel gauge can measure battery charge and discharge current, battery voltage, battery temperature, and the Type C connector's temperature.
The MAX77759 incorporates the Maxim ModelGauge m5 algorithm. It, as well as previous generations like m3 on max17047/max17050, requires the host to save/restore some register values across power cycles to maintain full accuracy. Extending the driver for such support is out of scope in this initial commit.
---
v2: fix typo MAX77759_T_convert -> MAX77759_Tconvert
--- drivers/power/supply/max17042_battery.c | 59 ++++++++++++++++++++++++++++++--- include/linux/power/max17042_battery.h | 24 ++++++++++++-- 2 files changed, 77 insertions(+), 6 deletions(-)
drivers/power/supply/max17042_battery.c +55 -4
@@ -650,7 +650,8 @@ static void max17042_write_config_regs(struct max17042_chip *chip)regmap_write (map, MAX17042_RelaxCFG, config->relax_cfg);if (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17047 || chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17050 || - chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17055 ) + chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17055 || + chip->chip_type ==MAXIM_DEVICE_TYPE_MAX77759 )regmap_write (map, MAX17047_FullSOCThr, config->full_soc_thresh); }
@@ -787,7 +788,8 @@ static inline void max17042_override_por_values(struct max17042_chip *chip)if ((chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17042 ) || (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17047 ) || - (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17050 )) { + (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17050 ) || + (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX77759 )) {max17042_override_por (map, MAX17042_IAvg_empty, config->iavg_empty);max17042_override_por (map, MAX17042_TempNom, config->temp_nom);max17042_override_por (map, MAX17042_TempLim, config->temp_lim);
@@ -796,7 +798,8 @@ static inline void max17042_override_por_values(struct max17042_chip *chip)if ((chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17047 ) || (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17050 ) || - (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17055 )) { + (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX17055 ) || + (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX77759 )) {max17042_override_por (map, MAX17047_V_empty, config->vempty); } }
@@ -1019,6 +1022,45 @@ static const struct regmap_config max17042_regmap_config = { .val_format_endian = REGMAP_ENDIAN_NATIVE, }; +static const struct regmap_range max77759_fg_registers[] = { +regmap_reg_range (MAX17042_STATUS , MAX77759_MixAtFull), +regmap_reg_range (MAX17042_VFSOC0Enable, MAX17042_VFSOC0Enable), +regmap_reg_range (MAX17042_MLOCKReg1, MAX17042_MLOCKReg2), +regmap_reg_range (MAX17042_MODELChrTbl, MAX17055_TimerH), +regmap_reg_range (MAX77759_IIn, MAX77759_IIn), +regmap_reg_range (MAX17055_AtQResidual, MAX17055_AtAvCap), +regmap_reg_range (MAX17042_OCVInternal, MAX17042_OCVInternal), +regmap_reg_range (MAX17042_VFSOC ,MAX17042_VFSOC ), +}; + +static const struct regmap_range max77759_fg_ro_registers[] = { +regmap_reg_range (MAX17042_FSTAT ,MAX17042_FSTAT ), +regmap_reg_range (MAX17042_OCVInternal, MAX17042_OCVInternal), +regmap_reg_range (MAX17042_VFSOC ,MAX17042_VFSOC ), +}; + +static const struct regmap_access_table max77759_fg_write_table = { + .yes_ranges = max77759_fg_registers, + .n_yes_ranges =ARRAY_SIZE (max77759_fg_registers), + .no_ranges = max77759_fg_ro_registers, + .n_no_ranges =ARRAY_SIZE (max77759_fg_ro_registers), +}; + +static const struct regmap_access_table max77759_fg_rd_table = { + .yes_ranges = max77759_fg_registers, + .n_yes_ranges =ARRAY_SIZE (max77759_fg_registers), +}; + +static const struct regmap_config max77759_fg_regmap_cfg = { + .reg_bits =8 , + .val_bits =16 , + .max_register =0xff , + .wr_table = &max77759_fg_write_table, + .rd_table = &max77759_fg_rd_table, + .val_format_endian =REGMAP_ENDIAN_NATIVE , + .cache_type =REGCACHE_NONE , +}; +static const struct power_supply_desc max17042_psy_desc = { .name ="max170xx_battery" , .type =POWER_SUPPLY_TYPE_BATTERY ,
@@ -1045,6 +1087,7 @@ static int max17042_probe(struct i2c_client *client, struct device *dev, int irq {struct i2c_adapter *adapter = client->adapter;const struct power_supply_desc *max17042_desc = &max17042_psy_desc; +const struct regmap_config *regmap_config;struct power_supply_config psy_cfg = {};struct max17042_chip *chip;int ret;
@@ -1060,7 +1103,12 @@ static int max17042_probe(struct i2c_client *client, struct device *dev, int irq chip->dev = dev; chip->chip_type = chip_type; - chip->regmap =devm_regmap_init_i2c (client, &max17042_regmap_config); + +if (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX77759 ) + regmap_config = &max77759_fg_regmap_cfg; +else + regmap_config = &max17042_regmap_config; + chip->regmap =devm_regmap_init_i2c (client, regmap_config);if (IS_ERR (chip->regmap))return dev_err_probe (dev,PTR_ERR (chip->regmap),"Failed to initialize regmap\n" );
@@ -1241,6 +1289,8 @@ static const struct of_device_id max17042_dt_match[] __used = { .data = (void *) MAXIM_DEVICE_TYPE_MAX17055 }, { .compatible = "maxim,max77705-battery", .data = (void *) MAXIM_DEVICE_TYPE_MAX17047 }, + { .compatible = "maxim,max77759-fg", + .data = (void *) MAXIM_DEVICE_TYPE_MAX77759 }, { .compatible = "maxim,max77849-battery", .data = (void *) MAXIM_DEVICE_TYPE_MAX17047 }, { },
@@ -1253,6 +1303,7 @@ static const struct i2c_device_id max17042_id[] = { { "max17047", MAXIM_DEVICE_TYPE_MAX17047 }, { "max17050", MAXIM_DEVICE_TYPE_MAX17050 }, { "max17055", MAXIM_DEVICE_TYPE_MAX17055 }, + { "max77759-fg", MAXIM_DEVICE_TYPE_MAX77759 }, { "max77849-battery", MAXIM_DEVICE_TYPE_MAX17047 }, { } };
include/linux/power/max17042_battery.h +22 -2
@@ -105,7 +105,7 @@ enum max17042_register {MAX17042_OCV =0xEE , - MAX17042_OCVInternal =0xFB ,/* MAX17055 VFOCV */ + MAX17042_OCVInternal =0xFB ,/* MAX17055/77759 VFOCV */ MAX17042_VFSOC = 0xFF, };
@@ -156,7 +156,7 @@ enum max17055_register { MAX17055_AtAvCap = 0xDF, }; -/* Registers specific to max17047/50/55 */ +/* Registers specific to max17047/50/55/77759 */ enum max17047_register { MAX17047_QRTbl00 =0x12 , MAX17047_FullSOCThr =0x13 ,
@@ -167,12 +167,32 @@ enum max17047_register { MAX17047_QRTbl30 = 0x42, }; +enum max77759_register { + MAX77759_AvgTA0 =0x26 , + MAX77759_AtTTF =0x33 , + MAX77759_Tconvert =0x34 , + MAX77759_AvgCurrent0 =0x3B , +MAX77759_THMHOT =0x40 , + MAX77759_CTESample =0x41 , + MAX77759_ISys =0x43 , + MAX77759_AvgVCell0 =0x44 , + MAX77759_RlxSOC =0x47 , + MAX77759_AvgISys =0x4B , +MAX77759_QH0 =0x4C , + MAX77759_MixAtFull =0x4F , + MAX77759_VSys =0xB1 , + MAX77759_TAlrtTh2 =0xB2 , + MAX77759_VByp =0xB3 , + MAX77759_IIn =0xD0 , +}; +enum max170xx_chip_type {MAXIM_DEVICE_TYPE_UNKNOWN =0 ,MAXIM_DEVICE_TYPE_MAX17042 ,MAXIM_DEVICE_TYPE_MAX17047 ,MAXIM_DEVICE_TYPE_MAX17050 ,MAXIM_DEVICE_TYPE_MAX17055 , +MAXIM_DEVICE_TYPE_MAX77759 ,MAXIM_DEVICE_TYPE_NUM };
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 10/11 power: supply: max17042: consider task period (max77759)
In reply to André DraszikSeveral (register) values reported by the fuel gauge depend on its internal task period and it needs to be taken into account when calculating results. All relevant example formulas in the data sheet assume the default task period (of 5760) and final results need to be adjusted based on the task period in effect.
Update the code as and where necessary.
---
While I do believe this should apply to all devices supported by this driver, given the register description in max17042_battery.h, I've made this change specific to max77759, as I have no way to confirm this works as expected on those. I've found a data sheet for max17047/max17050 online, which does describe the relevant register 0x3c as 'reserved', hence I'm a bit hesitant to enable this for all.
v2:
* update commit message subject prefix
--- drivers/power/supply/max17042_battery.c | 20 ++++++++++++++++++++ include/linux/power/max17042_battery.h | 1 + 2 files changed, 21 insertions(+)
drivers/power/supply/max17042_battery.c +20 -0
@@ -61,6 +61,7 @@ struct max17042_chip {struct work_struct work;int init_complete;int irq; +int task_period; };static enum power_supply_property max17042_battery_props[] = {
@@ -331,6 +332,8 @@ static int max17042_get_property(struct power_supply *psy,return ret; data64 = data *5000000ll ; + data64 *= chip->task_period; +do_div (data64,MAX17042_DEFAULT_TASK_PERIOD );do_div (data64, chip->pdata->r_sns); val->intval = data64;break ;
@@ -340,6 +343,8 @@ static int max17042_get_property(struct power_supply *psy,return ret; data64 = data *5000000ll ; + data64 *= chip->task_period; +do_div (data64,MAX17042_DEFAULT_TASK_PERIOD );do_div (data64, chip->pdata->r_sns); val->intval = data64;break ;
@@ -349,6 +354,8 @@ static int max17042_get_property(struct power_supply *psy,return ret; data64 = data *5000000ll ; + data64 *= chip->task_period; +do_div (data64,MAX17042_DEFAULT_TASK_PERIOD );do_div (data64, chip->pdata->r_sns); val->intval = data64;break ;
@@ -358,6 +365,8 @@ static int max17042_get_property(struct power_supply *psy,return ret; data64 =sign_extend64 (data,15 ) *5000000ll ; + data64 *= chip->task_period; + data64 =div_s64 (data64,MAX17042_DEFAULT_TASK_PERIOD ); val->intval =div_s64 (data64, chip->pdata->r_sns);break ;case POWER_SUPPLY_PROP_TEMP :
@@ -1142,6 +1151,17 @@ static int max17042_probe(struct i2c_client *client, struct device *dev, int irqregmap_write (chip->regmap, MAX17042_LearnCFG,0x0007 ); } + chip->task_period =MAX17042_DEFAULT_TASK_PERIOD ; +if (chip->chip_type ==MAXIM_DEVICE_TYPE_MAX77759 ) { + ret =regmap_read (chip->regmap, MAX17042_TaskPeriod, &val); +if (ret) +return dev_err_probe (dev, ret, +"failed to read task period\n" ); + chip->task_period = val; + } +dev_dbg (dev,"task period: %#.4x (%d)\n" , chip->task_period, + chip->task_period); + chip->battery =devm_power_supply_register (dev, max17042_desc, &psy_cfg);if (IS_ERR (chip->battery))
include/linux/power/max17042_battery.h +1 -0
@@ -17,6 +17,7 @@#define MAX17042_DEFAULT_VMAX (4500)/* LiHV cell max */ #define MAX17042_DEFAULT_TEMP_MIN (0)/* For sys without temp sensor */ #define MAX17042_DEFAULT_TEMP_MAX (700)/* 70 degrees Celcius */ +#define MAX17042_DEFAULT_TASK_PERIOD (5760)/* Consider RepCap which is less then 10 units below FullCAP full */ #define MAX17042_FULL_THRESHOLD 10
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
PATCH v3 11/11 power: supply: max17042: report time to full (max17055 & max77759)
In reply to André DraszikReport the remaining time to full as calculated by the firmware for devices that implement this.
Similar to time to empty, the reported value is only meaningful when charging, i.e. if it is != U16_MAX.
---
v2:
- limit to max17055 & max77759, the datasheet for max17047 and max17050 describes the register as 'reserved'. I was mislead by the comment and enum ordering in max17042_battery.h
- report as POWER_SUPPLY_PROP_TIME_TO_FULL_NOW (not _AVG). The max17055 datasheet is a bit clearer than the max77759 one on that.
--- drivers/power/supply/max17042_battery.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
drivers/power/supply/max17042_battery.c +16 -0
@@ -89,6 +89,7 @@ static enum power_supply_property max17042_battery_props[] = {POWER_SUPPLY_PROP_HEALTH ,POWER_SUPPLY_PROP_SCOPE ,POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW , +POWER_SUPPLY_PROP_TIME_TO_FULL_NOW ,// these two have to be at the end on the list POWER_SUPPLY_PROP_CURRENT_NOW , POWER_SUPPLY_PROP_CURRENT_AVG,
@@ -443,6 +444,21 @@ static int max17042_get_property(struct power_supply *psy,if (data ==U16_MAX )return -ENODATA ; + val->intval = data *5625 /1000 ; +break ; +case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW : +if (chip->chip_type !=MAXIM_DEVICE_TYPE_MAX17055 && + chip->chip_type !=MAXIM_DEVICE_TYPE_MAX77759 ) +return -EINVAL ; + + ret =regmap_read (map,MAX17055_TTF , &data); +if (ret <0 ) +return ret; + +/* when discharging, the value is not meaningful */ +if (data ==U16_MAX ) +return -ENODATA ; + val->intval = data *5625 /1000 ;break ;default :
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Sebastian Reichel
AppliedQuoted 9 lines · André Draszik
This series adds support for the fuel gauge integrated into the Maxim MAX77759, which is a companion PMIC intended for use in mobile phones and tablets and is used on Google Pixel 6 and 6 Pro (oriole and raven).
Amongst others, the PMIC contains a fuel gauge employing the Maxim ModelGauge m5 algorithm, that is similar to the ones supported by the max17042 driver and binding.
[...]
Applied, thanks!
[01/11] dt-bindings: power: supply: max17042: add support for max77759
commit: 7a9ae6e2457d654cb3b2597882d61297ce9652c3
[02/11] dt-bindings: power: supply: max17042: support shunt-resistor-micro-ohms
commit: cb850494d77ab8bf1ebe40a1e6c709724a899e87
[03/11] dt-bindings: power: supply: max17042: drop formatting specifier |
commit: ef67048dca6a94ecd874565a48f023dead25d7b7
[04/11] power: supply: max17042: fix a comment typo (then -> than)
commit: 62b5cb32f0beed13cf6d7cf4605e09eb92f2f64c
[05/11] power: supply: max17042: use dev_err_probe() where appropriate
commit: 092a518f8e8f97aa4e50c413600a849dcd0a8705
[06/11] power: supply: max17042: avoid overflow when determining health
commit: 1c6b480642c8457e477867f348d1347ac2f6bb82
[07/11] power: supply: max17042: time to empty is meaningless when charging
commit: c4ff06590c3149fa5cc3061c7877cd75df4351e9
[08/11] power: supply: max17042: support standard shunt-resistor-micro-ohms DT property
commit: 460f54cd8dee5f048fe33d1858db919566c73d0e
[09/11] power: supply: max17042: initial support for Maxim MAX77759
commit: 4ece9d5d7fcd176821f41a278dfa9efd87544afd
[10/11] power: supply: max17042: consider task period (max77759)
commit: add02276239b8af842f2e761774fcd8a861c2d48
[11/11] power: supply: max17042: report time to full (max17055 & max77759)
commit: bb42637516d058c412d2acabab4cd2a455ecd769Best regards,
Signature · 1 line
Sebastian Reichel <[email protected]>