power: supply: max17040 support compatible devices
Iskren Chernev
reviewerv4 cover power: supply: max17040 support compatible devices
From: Iskren Chernev <[email protected]>
The max17040 fuel gauge is part of a family of 8 chips that have very similar mode of operations and registers.
This patch set adds:
- compatible strings for all supported devices and handles the minor differences between them;
- handling for devices reporting double capacity via maxim,double-soc;
- handling for setting rcomp, a compensation value for more accurate reading, affected by battery chemistry and operating temps;
- suppot for SOC alerts (capacity changes by +/- 1%), to prevent polling every second;
- improved max17040 driver with regmap and devm_
The datasheets of the supported devices are linked [0] [1] [2] [3].
[0] https://datasheets.maximintegrated.com/en/ds/MAX17040-MAX17041.pdf
[1] https://datasheets.maximintegrated.com/en/ds/MAX17043-MAX17044.pdf
[2] https://datasheets.maximintegrated.com/en/ds/MAX17048-MAX17049.pdf
[3] https://datasheets.maximintegrated.com/en/ds/MAX17058-MAX17059.pdf
v3: https://lkml.org/lkml/2020/6/24/874
v2: https://lkml.org/lkml/2020/6/18/260
v1: https://lkml.org/lkml/2020/6/8/682
Changes in v4:
- split refactor patch into devm_ and regmap patches (as suggested by Sebastian Reichel)
- correct handling of soc alert for double-soc devices
- rebased on 5.9-rc, incorporating upstream work
Iskren Chernev (7):
power: supply: max17040: Use devm_ to automate remove
power: supply: max17040: Use regmap i2c
dt-bindings: power: supply: Extend max17040 compatibility
power: supply: max17040: Support compatible devices
dt-bindings: power: supply: max17040: Add maxim,rcomp
power: supply: max17040: Support setting rcomp
power: supply: max17040: Support soc alert
.../power/supply/max17040_battery.txt | 21 +- drivers/power/supply/Kconfig | 11 +- drivers/power/supply/max17040_battery.c | 488 ++++++++++++------ 3 files changed, 366 insertions(+), 154 deletions(-)
base-commit: b36c969764ab12faebb74711c942fa3e6eaf1e96 --
2.28.0
Iskren Chernev
reviewerPATCH v4 1/7 power: supply: max17040: Use devm_ to automate remove
Two actions were performed during remove - power supply dereg and delayed work cleanup. Power supply dereg can be handled by using the devm_ version of the registration function. Work cleanup can be added as a devm_action.
If probe fails after psy registration it will now be cleaned up properly.
--- drivers/power/supply/max17040_battery.c | 39 +++++++++++++------------ 1 file changed, 21 insertions(+), 18 deletions(-)
drivers/power/supply/max17040_battery.c +21 -18
@@ -207,6 +207,19 @@ static void max17040_check_changes(struct i2c_client *client)max17040_get_status (client); } +static void max17040_queue_work (struct max17040_chip *chip) +{ +queue_delayed_work (system_power_efficient_wq, &chip->work, +MAX17040_DELAY ); +} + +static void max17040_stop_work (void *data) +{ +struct max17040_chip *chip = data; + +cancel_delayed_work_sync (&chip->work); +} +static void max17040_work (struct work_struct *work) {struct max17040_chip *chip;
@@ -223,8 +236,7 @@ static void max17040_work(struct work_struct *work)if (last_soc != chip->soc || last_status != chip->status)power_supply_changed (chip->battery); -queue_delayed_work (system_power_efficient_wq, &chip->work, -MAX17040_DELAY ); +max17040_queue_work (chip); }static irqreturn_t max17040_thread_handler (int id,void *dev)
@@ -339,7 +351,7 @@ static int max17040_probe(struct i2c_client *client,i2c_set_clientdata (client, chip); psy_cfg.drv_data = chip; - chip->battery =power_supply_register (&client->dev, + chip->battery =devm_power_supply_register (&client->dev, &max17040_battery_desc, &psy_cfg);if (IS_ERR (chip->battery)) {dev_err (&client->dev,"failed: power supply register\n" );
@@ -368,18 +380,11 @@ static int max17040_probe(struct i2c_client *client, }INIT_DEFERRABLE_WORK (&chip->work, max17040_work); -queue_delayed_work (system_power_efficient_wq, &chip->work, -MAX17040_DELAY ); - -return 0 ; -} - -static int max17040_remove (struct i2c_client *client) -{ -struct max17040_chip *chip =i2c_get_clientdata (client); + ret =devm_add_action (&client->dev, max17040_stop_work, chip); +if (ret) +return ret; +max17040_queue_work (chip); -power_supply_unregister (chip->battery); -cancel_delayed_work (&chip->work);return 0 ; }
@@ -403,12 +408,11 @@ static int max17040_resume(struct device *dev)struct i2c_client *client =to_i2c_client (dev);struct max17040_chip *chip =i2c_get_clientdata (client); -queue_delayed_work (system_power_efficient_wq, &chip->work, -MAX17040_DELAY ); -if (client->irq &&device_may_wakeup (dev))disable_irq_wake (client->irq); +max17040_queue_work (chip); +return 0 ; }
@@ -442,7 +446,6 @@ static struct i2c_driver max17040_i2c_driver = { .pm = MAX17040_PM_OPS, }, .probe = max17040_probe, - .remove = max17040_remove, .id_table = max17040_id, };module_i2c_driver (max17040_i2c_driver);
Signature · 1 line
2.28.0
Iskren Chernev
reviewerPATCH v4 2/7 power: supply: max17040: Use regmap i2c
In reply to Iskren ChernevFrom: Iskren Chernev <[email protected]>
Rewrite i2c operations from i2c client read/write to regmap i2c. As a result, most private functions now accept the private driver data instead of an i2c client pointer.
--- drivers/power/supply/Kconfig | 1 + drivers/power/supply/max17040_battery.c | 219 ++++++++++-------------- 2 files changed, 93 insertions(+), 127 deletions(-)
drivers/power/supply/Kconfig +1 -0
@@ -367,6 +367,7 @@ config AXP288_FUEL_GAUGE config BATTERY_MAX17040 tristate "Maxim MAX17040 Fuel Gauge" depends on I2C + select REGMAP_I2C help MAX17040 is fuel-gauge systems for lithium-ion (Li+) batteries in handheld and portable equipment. The MAX17040 is configured
drivers/power/supply/max17040_battery.c +92 -127
343 lines not on this page · raw message
Signature · 1 line
2.28.0
Iskren Chernev
reviewerPATCH v4 3/7 dt-bindings: power: supply: Extend max17040 compatibility
In reply to Iskren ChernevMaxim max17040 is a fuel gauge from a larger family utilising the Model Gauge technology. Document all different compatible strings that the max17040 driver recognizes.
Some devices in the wild report double the capacity. The maxim,double-soc (from State-Of-Charge) property fixes that.
Examples: https://lore.kernel.org/patchwork/patch/1263411/#1468420
--- .../bindings/power/supply/max17040_battery.txt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/power/supply/max17040_battery.txt +14 -1
@@ -2,7 +2,9 @@ max17040_battery ~~~~~~~~~~~~~~~~ Required properties : - - compatible : "maxim,max17040" or "maxim,max77836-battery" + - compatible : "maxim,max17040", "maxim,max17041", "maxim,max17043", + "maxim,max17044", "maxim,max17048", "maxim,max17049", + "maxim,max17058", "maxim,max17059" or "maxim,max77836-battery" - reg: i2c slave address Optional properties :
@@ -11,6 +13,10 @@ Optional properties : generated. Can be configured from 1 up to 32 (%). If skipped the power up default value of 4 (%) will be used. +- maxim,double-soc : Certain devices return double the capacity. + Specify this boolean property to divide the + reported value in 2 and thus normalize it. + SOC == State of Charge == Capacity. - interrupts : Interrupt line see Documentation/devicetree/ bindings/interrupt-controller/interrupts.txt - wakeup-source : This device has wakeup capabilities. Use this
@@ -31,3 +37,10 @@ Example: interrupts = <2 IRQ_TYPE_EDGE_FALLING>; wakeup-source; }; + + battery-fuel-gauge@36 { + compatible = "maxim,max17048"; + reg = <0x36>; + maxim,alert-low-soc-level = <10>; + maxim,double-soc; + };
Signature · 1 line
2.28.0
Iskren Chernev
reviewerPATCH v4 4/7 power: supply: max17040: Support compatible devices
In reply to Iskren ChernevThe max17040 fuel gauge is part of a family of 8 chips that have very similar mode of operations and registers.
This change adds:
- compatible strings for all supported devices and handling for the minor differences between them;
- handling for devices reporting double capacity via maxim,double-soc;
The datasheets of the supported devices are linked [0] [1] [2] [3].
[0] https://datasheets.maximintegrated.com/en/ds/MAX17040-MAX17041.pdf
[1] https://datasheets.maximintegrated.com/en/ds/MAX17043-MAX17044.pdf
[2] https://datasheets.maximintegrated.com/en/ds/MAX17048-MAX17049.pdf
[3] https://datasheets.maximintegrated.com/en/ds/MAX17058-MAX17059.pdf
--- drivers/power/supply/Kconfig | 10 +- drivers/power/supply/max17040_battery.c | 154 +++++++++++++++++++++--- 2 files changed, 142 insertions(+), 22 deletions(-)
drivers/power/supply/Kconfig +7 -3
@@ -369,9 +369,13 @@ config BATTERY_MAX17040 depends on I2C select REGMAP_I2C help - MAX17040 is fuel-gauge systems for lithium-ion (Li+) batteries - in handheld and portable equipment. The MAX17040 is configured - to operate with a single lithium cell + Maxim models with ModelGauge are fuel-gauge systems for lithium-ion + (Li+) batteries in handheld and portable equipment, including + max17040, max17041, max17043, max17044, max17048, max17049, max17058, + max17059. It is also included in some batteries like max77836. + + Driver supports reporting SOC (State of Charge, i.e capacity), + voltage and configurable low-SOC wakeup interrupt. config BATTERY_MAX17042 tristate "Maxim MAX17042/17047/17050/8997/8966 Fuel Gauge"
drivers/power/supply/max17040_battery.c +135 -19
249 lines not on this page · raw message
Signature · 1 line
2.28.0
kernel test robot
build botRe: [PATCH v4 4/7] power: supply: max17040: Support compatible devices
Thank you for the patch! Perhaps something to improve:
Hi Iskren,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on b36c969764ab12faebb74711c942fa3e6eaf1e96]
url: https://github.com/0day-ci/linux/commits/Iskren-Chernev/power-supply-max17040-support-compatible-devices/20200907-112145 base: b36c969764ab12faebb74711c942fa3e6eaf1e96 config: x86_64-randconfig-r026-20200907 (attached as .config) compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project ab68517e6b7e51b84c4b0e813a30258ec1ce5da5)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> drivers/power/supply/max17040_battery.c:410:13: warning: cast to smaller integer type 'enum chip_id' from 'const void *' [-Wvoid-pointer-to-enum-cast]
chip_id = (enum chip_id) of_device_get_match_data(&client->dev);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1 warning generated.
# https://github.com/0day-ci/linux/commit/20c853834c05ddde6df590b46b94ef66477df1d0
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Iskren-Chernev/power-supply-max17040-support-compatible-devices/20200907-112145 git checkout 20c853834c05ddde6df590b46b94ef66477df1d0
vim +410 drivers/power/supply/max17040_battery.c
385
386 static int max17040_probe(struct i2c_client *client,
387 const struct i2c_device_id *id)
388 {
389 struct i2c_adapter *adapter = client->adapter;
390 struct power_supply_config psy_cfg = {};
391 struct max17040_chip *chip;
392 enum chip_id chip_id;
393 int ret;
394
395 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
396 return -EIO;
397
398 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
399 if (!chip)
400 return -ENOMEM;
401
402 chip->client = client;
403 chip->regmap = devm_regmap_init_i2c(client, &max17040_regmap);
404 chip->pdata = client->dev.platform_data;
405 chip_id = (enum chip_id) id->driver_data;
406 if (client->dev.of_node) {
407 ret = max17040_get_of_data(chip);
408 if (ret)
409 return ret;
> 410 chip_id = (enum chip_id) of_device_get_match_data(&client->dev);
411 }
412 chip->data = max17040_family[chip_id];
413
414 i2c_set_clientdata(client, chip);
415 psy_cfg.drv_data = chip;
416
417 chip->battery = devm_power_supply_register(&client->dev,
418 &max17040_battery_desc, &psy_cfg);
419 if (IS_ERR(chip->battery)) {
420 dev_err(&client->dev, "failed: power supply register\n");
421 return PTR_ERR(chip->battery);
422 }
423
424 ret = max17040_get_version(chip);
425 if (ret < 0)
426 return ret;
427 dev_dbg(&chip->client->dev, "MAX17040 Fuel-Gauge Ver 0x%x\n", ret);
428
429 if (chip_id == ID_MAX17040 || chip_id == ID_MAX17041)
430 max17040_reset(chip);
431
432 /* check interrupt */
433 if (client->irq && chip->data.has_low_soc_alert) {
434 ret = max17040_set_low_soc_alert(chip, chip->low_soc_alert);
435 if (ret) {
436 dev_err(&client->dev,
437 "Failed to set low SOC alert: err %d\n", ret);
438 return ret;
439 }
440
441 ret = max17040_enable_alert_irq(chip);
442 if (ret) {
443 client->irq = 0;
444 dev_warn(&client->dev,
445 "Failed to get IRQ err %d\n", ret);
446 }
447 }
448
449 INIT_DEFERRABLE_WORK(&chip->work, max17040_work);
450 ret = devm_add_action(&client->dev, max17040_stop_work, chip);
451 if (ret)
452 return ret;
453 max17040_queue_work(chip);
454
455 return 0;
456 }
457
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Iskren Chernev
reviewerPATCH v4 5/7 dt-bindings: power: supply: max17040: Add maxim,rcomp
In reply to Iskren ChernevTo compensate for the battery chemistry and operating conditions the chips support a compensation value. Specify one or two byte compensation via the maxim,rcomp byte array.
--- .../devicetree/bindings/power/supply/max17040_battery.txt | 6 ++++++ 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/power/supply/max17040_battery.txt +6 -0
@@ -17,6 +17,11 @@ Optional properties : Specify this boolean property to divide the reported value in 2 and thus normalize it. SOC == State of Charge == Capacity. +- maxim,rcomp : A value to compensate readings for various + battery chemistries and operating temperatures. + max17040,41 have 2 byte rcomp, default to + 0x97 0x00. All other devices have one byte + rcomp, default to 0x97. - interrupts : Interrupt line see Documentation/devicetree/ bindings/interrupt-controller/interrupts.txt - wakeup-source : This device has wakeup capabilities. Use this
@@ -41,6 +46,7 @@ Example: battery-fuel-gauge@36 { compatible = "maxim,max17048"; reg = <0x36>; + maxim,rcomp = /bits/ 8 <0x56>; maxim,alert-low-soc-level = <10>; maxim,double-soc; };
Signature · 1 line
2.28.0
Iskren Chernev
reviewerPATCH v4 6/7 power: supply: max17040: Support setting rcomp
In reply to Iskren ChernevThe Maxim ModelGauge family supports fine-tuning by setting a compensation value (named rcomp in the docs). The value is affected by battery chemistry and ambient temperature.
Add support for reading maxim,rcomp from DT and configuring the device with the supplied value. Temperature adjustment is not implemented at the moment, because there is no provision for receiving the ambient temperature at the moment.
--- drivers/power/supply/max17040_battery.c | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
drivers/power/supply/max17040_battery.c +40 -0
@@ -30,9 +30,11 @@#define MAX17040_DELAY 1000#define MAX17040_BATTERY_FULL 95 +#define MAX17040_RCOMP_DEFAULT 0x9700#define MAX17040_ATHD_MASK 0x3f#define MAX17040_ATHD_DEFAULT_POWER_UP 4 +#define MAX17040_CFG_RCOMP_MASK 0xff00enum chip_id {ID_MAX17040 ,
@@ -52,6 +54,7 @@ struct chip_data {u16 vcell_mul;u16 vcell_div;u8 has_low_soc_alert; +u8 rcomp_bytes; };static struct chip_data max17040_family[] = {
@@ -61,6 +64,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 1250, .vcell_div = 1, .has_low_soc_alert = 0, + .rcomp_bytes = 2, }, [ID_MAX17041] = { .reset_val = 0x0054,
@@ -68,6 +72,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 2500, .vcell_div = 1, .has_low_soc_alert = 0, + .rcomp_bytes = 2, }, [ID_MAX17043] = { .reset_val = 0x0054,
@@ -75,6 +80,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 1250, .vcell_div = 1, .has_low_soc_alert = 1, + .rcomp_bytes = 1, }, [ID_MAX17044] = { .reset_val = 0x0054,
@@ -82,6 +88,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 2500, .vcell_div = 1, .has_low_soc_alert = 1, + .rcomp_bytes = 1, }, [ID_MAX17048] = { .reset_val = 0x5400,
@@ -89,6 +96,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 625, .vcell_div = 8, .has_low_soc_alert = 1, + .rcomp_bytes = 1, }, [ID_MAX17049] = { .reset_val = 0x5400,
@@ -96,6 +104,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 625, .vcell_div = 4, .has_low_soc_alert = 1, + .rcomp_bytes = 1, }, [ID_MAX17058] = { .reset_val = 0x5400,
@@ -103,6 +112,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 625, .vcell_div = 8, .has_low_soc_alert = 1, + .rcomp_bytes = 1, }, [ID_MAX17059] = { .reset_val = 0x5400,
@@ -110,6 +120,7 @@ static struct chip_data max17040_family[] = { .vcell_mul = 625, .vcell_div = 4, .has_low_soc_alert = 1, + .rcomp_bytes = 1, }, };
@@ -129,6 +140,8 @@ struct max17040_chip {u32 low_soc_alert;/* some devices return twice the capacity */ bool quirk_double_soc; +/* higher 8 bits for 17043+, 16 bits for 17040,41 */ +u16 rcomp; };static int max17040_reset (struct max17040_chip *chip)
@@ -143,6 +156,14 @@ static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level) MAX17040_ATHD_MASK, level); } +static int max17040_set_rcomp (struct max17040_chip *chip,u16 rcomp) +{ +u16 mask = chip->data.rcomp_bytes ==2 ? +0xffff :MAX17040_CFG_RCOMP_MASK ; + +return regmap_update_bits (chip->regmap,MAX17040_CONFIG , mask, rcomp); +} +static int max17040_raw_vcell_to_uvolts (struct max17040_chip *chip,u16 vcell) {struct chip_data *d = &chip->data;
@@ -206,6 +227,10 @@ static int max17040_get_status(struct max17040_chip *chip)static int max17040_get_of_data (struct max17040_chip *chip) {struct device *dev = &chip->client->dev; +struct chip_data *data = &max17040_family[ + (enum chip_id )of_device_get_match_data (dev)]; +int rcomp_len; +u8 rcomp[2 ]; chip->quirk_double_soc =device_property_read_bool (dev,"maxim,double-soc" );
@@ -221,6 +246,19 @@ static int max17040_get_of_data(struct max17040_chip *chip)return -EINVAL ; } + rcomp_len =device_property_count_u8 (dev,"maxim,rcomp" ); + chip->rcomp =MAX17040_RCOMP_DEFAULT ; +if (rcomp_len == data->rcomp_bytes) { +device_property_read_u8_array (dev,"maxim,rcomp" , + rcomp, rcomp_len); + chip->rcomp = rcomp_len ==2 ? + rcomp[0 ] <<8 | rcomp[1 ] : + rcomp[0 ] <<8 ; + }else if (rcomp_len >0 ) { +dev_err (dev,"maxim,rcomp has incorrect length\n" ); +return -EINVAL ; + } +return 0 ; }
@@ -429,6 +467,8 @@ static int max17040_probe(struct i2c_client *client,if (chip_id ==ID_MAX17040 || chip_id ==ID_MAX17041 )max17040_reset (chip); +max17040_set_rcomp (chip, chip->rcomp); +/* check interrupt */ if (client->irq && chip->data.has_low_soc_alert) { ret =max17040_set_low_soc_alert (chip, chip->low_soc_alert);
Signature · 1 line
2.28.0
Iskren Chernev
reviewerPATCH v4 7/7 power: supply: max17040: Support soc alert
In reply to Iskren Chernevmax17048 and max17049 support SOC alerts (interrupts when battery capacity changes by +/- 1%). At the moment the driver polls for changes every second. Using the alerts removes the need for polling.
--- drivers/power/supply/max17040_battery.c | 82 ++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 9 deletions(-)
drivers/power/supply/max17040_battery.c +73 -9
@@ -25,6 +25,7 @@#define MAX17040_MODE 0x06#define MAX17040_VER 0x08#define MAX17040_CONFIG 0x0C +#define MAX17040_STATUS 0x1A#define MAX17040_CMD 0xFE
@@ -33,7 +34,10 @@#define MAX17040_RCOMP_DEFAULT 0x9700#define MAX17040_ATHD_MASK 0x3f +#define MAX17040_ALSC_MASK 0x40#define MAX17040_ATHD_DEFAULT_POWER_UP 4 +#define MAX17040_STATUS_HD_MASK 0x1000 +#define MAX17040_STATUS_SC_MASK 0x2000#define MAX17040_CFG_RCOMP_MASK 0xff00enum chip_id {
@@ -55,6 +59,7 @@ struct chip_data {u16 vcell_div;u8 has_low_soc_alert;u8 rcomp_bytes; +u8 has_soc_alert; };static struct chip_data max17040_family[] = {
@@ -65,6 +70,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 0, .rcomp_bytes = 2, + .has_soc_alert = 0, }, [ID_MAX17041] = { .reset_val = 0x0054,
@@ -73,6 +79,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 0, .rcomp_bytes = 2, + .has_soc_alert = 0, }, [ID_MAX17043] = { .reset_val = 0x0054,
@@ -81,6 +88,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, [ID_MAX17044] = { .reset_val = 0x0054,
@@ -89,6 +97,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 1, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, [ID_MAX17048] = { .reset_val = 0x5400,
@@ -97,6 +106,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 8, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 1, }, [ID_MAX17049] = { .reset_val = 0x5400,
@@ -105,6 +115,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 4, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 1, }, [ID_MAX17058] = { .reset_val = 0x5400,
@@ -113,6 +124,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 8, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, [ID_MAX17059] = { .reset_val = 0x5400,
@@ -121,6 +133,7 @@ static struct chip_data max17040_family[] = { .vcell_div = 4, .has_low_soc_alert = 1, .rcomp_bytes = 1, + .has_soc_alert = 0, }, };
@@ -156,6 +169,12 @@ static int max17040_set_low_soc_alert(struct max17040_chip *chip, u32 level) MAX17040_ATHD_MASK, level); } +static int max17040_set_soc_alert (struct max17040_chip *chip,bool enable) +{ +return regmap_update_bits (chip->regmap,MAX17040_CONFIG , +MAX17040_ALSC_MASK , enable ?MAX17040_ALSC_MASK :0 ); +} +static int max17040_set_rcomp (struct max17040_chip *chip,u16 rcomp) { u16 mask = chip->data.rcomp_bytes == 2 ?
@@ -300,11 +319,33 @@ static void max17040_work(struct work_struct *work)max17040_queue_work (chip); } +/* Returns true if alert cause was SOC change, not low SOC */ +static bool max17040_handle_soc_alert (struct max17040_chip *chip) +{ +bool ret = true; +u32 data; + +regmap_read (chip->regmap,MAX17040_STATUS , &data); + +if (data &MAX17040_STATUS_HD_MASK ) { +// this alert was caused by low soc + ret = false; + } +if (data &MAX17040_STATUS_SC_MASK ) { +// soc change bit -- deassert to mark as handled +regmap_write (chip->regmap,MAX17040_STATUS , + data & ~MAX17040_STATUS_SC_MASK ); + } + +return ret; +} +static irqreturn_t max17040_thread_handler (int id,void *dev) {struct max17040_chip *chip = dev; -dev_warn (&chip->client->dev,"IRQ: Alert battery low level" ); +if (!(chip->data.has_soc_alert &&max17040_handle_soc_alert (chip))) +dev_warn (&chip->client->dev,"IRQ: Alert battery low level\n" );/* read registers */ max17040_check_changes (chip);
@@ -428,6 +469,7 @@ static int max17040_probe(struct i2c_client *client,struct power_supply_config psy_cfg = {};struct max17040_chip *chip;enum chip_id chip_id; +bool enable_irq = false;int ret;if (!i2c_check_functionality (adapter,I2C_FUNC_SMBUS_BYTE ))
@@ -478,6 +520,27 @@ static int max17040_probe(struct i2c_client *client,return ret; } + enable_irq = true; + } + +if (client->irq && chip->data.has_soc_alert) { + ret =max17040_set_soc_alert (chip,1 ); +if (ret) { +dev_err (&client->dev, +"Failed to set SOC alert: err %d\n" , ret); +return ret; + } + enable_irq = true; + }else { +/* soc alerts negate the need for polling */ +INIT_DEFERRABLE_WORK (&chip->work, max17040_work); + ret =devm_add_action (&client->dev, max17040_stop_work, chip); +if (ret) +return ret; +max17040_queue_work (chip); + } + +if (enable_irq) { ret =max17040_enable_alert_irq (chip);if (ret) { client->irq =0 ;
@@ -486,12 +549,6 @@ static int max17040_probe(struct i2c_client *client, } } -INIT_DEFERRABLE_WORK (&chip->work, max17040_work); - ret =devm_add_action (&client->dev, max17040_stop_work, chip); -if (ret) -return ret; -max17040_queue_work (chip); -return 0 ; }
@@ -502,7 +559,11 @@ static int max17040_suspend(struct device *dev)struct i2c_client *client =to_i2c_client (dev);struct max17040_chip *chip =i2c_get_clientdata (client); -cancel_delayed_work (&chip->work); +if (client->irq && chip->data.has_soc_alert) +// disable soc alert to prevent wakeup +max17040_set_soc_alert (chip,0 ); +else +cancel_delayed_work (&chip->work);if (client->irq &&device_may_wakeup (dev))enable_irq_wake (client->irq);
@@ -518,7 +579,10 @@ static int max17040_resume(struct device *dev)if (client->irq &&device_may_wakeup (dev))disable_irq_wake (client->irq); -max17040_queue_work (chip); +if (client->irq && chip->data.has_soc_alert) +max17040_set_soc_alert (chip,1 ); +else +max17040_queue_work (chip);return 0 ; }
Signature · 1 line
2.28.0