power: supply: bq27xxx/bq25890 status update fixes
v2 cover power: supply: bq27xxx/bq25890 status update fixes
Hi all,
Here is v2 if my bq* status update fixes series
Changes in v2:
- Do not use bq->charger in bq25890_external_power_changed(), because it may not be set yet (avoid NULL ptr deref on race)
- Add "power: supply: bq24190: Call power_supply_changed() after updating input current" to the series
The main goal of this series is to fix userspace sometimes not getting notified in a timely manner of charging <-> discharging status changes, causing e.g. battery status icons to lag and the lagging of updates also effects power_supply_leds.c .
While working on this I noticed a number of races surrounding bq27xxx_battery_update(), so patches 1-4 fix those first.
Patches 5-10 take care of the status update lagging issues.
Regards,
Hans
Hans de Goede (10):
power: supply: bq27xxx: Fix bq27xxx_battery_update() race condition
power: supply: bq27xxx: Fix I2C IRQ race on remove
power: supply: bq27xxx: Fix poll_interval handling and races on remove
power: supply: bq27xxx: Add cache parameter to
bq27xxx_battery_current_and_status()
power: supply: bq27xxx: Move bq27xxx_battery_update() down
power: supply: bq27xxx: Ensure power_supply_changed() is called on
current sign changes
power: supply: bq27xxx: After charger plug in/out wait 0.5s for things
to stabilize
power: supply: bq27xxx: Use mod_delayed_work() instead of cancel() +
schedule()
power: supply: bq25890: Call power_supply_changed() after updating
input current or voltage
power: supply: bq24190: Call power_supply_changed() after updating
input current
drivers/power/supply/bq24190_charger.c | 1 + drivers/power/supply/bq25890_charger.c | 3 + drivers/power/supply/bq27xxx_battery.c | 181 +++++++++++---------- drivers/power/supply/bq27xxx_battery_i2c.c | 3 +- include/linux/power/bq27xxx_battery.h | 4 + 5 files changed, 108 insertions(+), 84 deletions(-)
Signature · 1 line
2.39.1
PATCH v2 1/10 power: supply: bq27xxx: Fix bq27xxx_battery_update() race condition
bq27xxx_battery_update() assumes / requires that it is only run once, not multiple times at the same time. But there are 3 possible callers:
1. bq27xxx_battery_poll() delayed_work item handler
2. bq27xxx_battery_irq_handler_thread() I2C IRQ handler
3. bq27xxx_battery_setup()
And there is no protection against these racing with each other, fix this race condition by making all callers take di->lock:
- Rename bq27xxx_battery_update() to bq27xxx_battery_update_unlocked()
- Add new bq27xxx_battery_update() which takes di->lock and then calls bq27xxx_battery_update_unlocked()
- Make stale cache check code in bq27xxx_battery_get_property(), which already takes di->lock directly to check the jiffies, call bq27xxx_battery_update_unlocked() instead of messing with the delayed_work item
- Make bq27xxx_battery_update_unlocked() mod the delayed-work item so that the next poll is delayed to poll_interval milliseconds after the last update independent of the source of the update
--- drivers/power/supply/bq27xxx_battery.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/power/supply/bq27xxx_battery.c +13 -8
@@ -1761,7 +1761,7 @@ static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)return POWER_SUPPLY_HEALTH_GOOD ; } -void bq27xxx_battery_update (struct bq27xxx_device_info *di) +static void bq27xxx_battery_update_unlocked (struct bq27xxx_device_info *di) {struct bq27xxx_reg_cache cache = {0 , };bool has_singe_flag = di->opts &BQ27XXX_O_ZERO ;
@@ -1800,6 +1800,16 @@ void bq27xxx_battery_update(struct bq27xxx_device_info *di) di->cache = cache; di->last_update = jiffies; + +if (poll_interval >0 ) +mod_delayed_work (system_wq, &di->work, poll_interval *HZ ); +} + +void bq27xxx_battery_update (struct bq27xxx_device_info *di) +{ +mutex_lock (&di->lock); +bq27xxx_battery_update_unlocked (di); +mutex_unlock (&di->lock); }EXPORT_SYMBOL_GPL (bq27xxx_battery_update);
@@ -1810,9 +1820,6 @@ static void bq27xxx_battery_poll(struct work_struct *work) work.work);bq27xxx_battery_update (di); - -if (poll_interval >0 ) -schedule_delayed_work (&di->work, poll_interval *HZ ); }static bool bq27xxx_battery_is_full (struct bq27xxx_device_info *di,int flags)
@@ -1985,10 +1992,8 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,struct bq27xxx_device_info *di =power_supply_get_drvdata (psy);mutex_lock (&di->lock); -if (time_is_before_jiffies (di->last_update +5 *HZ )) { -cancel_delayed_work_sync (&di->work); -bq27xxx_battery_poll (&di->work.work); - } +if (time_is_before_jiffies (di->last_update +5 *HZ )) +bq27xxx_battery_update_unlocked (di);mutex_unlock (&di->lock);if (psp !=POWER_SUPPLY_PROP_PRESENT && di->cache.flags <0 )
Signature · 1 line
2.39.1
PATCH v2 2/10 power: supply: bq27xxx: Fix I2C IRQ race on remove
In reply to Hans de Goededevm_request_threaded_irq() requested IRQs are only free-ed after the driver's remove function has ran. So the IRQ could trigger and call bq27xxx_battery_update() after bq27xxx_battery_teardown() has already run.
Switch to explicitly free-ing the IRQ in bq27xxx_battery_i2c_remove() to fix this.
--- drivers/power/supply/bq27xxx_battery_i2c.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/supply/bq27xxx_battery_i2c.c +2 -1
@@ -179,7 +179,7 @@ static int bq27xxx_battery_i2c_probe(struct i2c_client *client)i2c_set_clientdata (client, di);if (client->irq) { - ret =devm_request_threaded_irq (&client->dev, client->irq, + ret =request_threaded_irq (client->irq,NULL , bq27xxx_battery_irq_handler_thread,IRQF_ONESHOT , di->name, di);
@@ -209,6 +209,7 @@ static void bq27xxx_battery_i2c_remove(struct i2c_client *client) {struct bq27xxx_device_info *di =i2c_get_clientdata (client); +free_irq (client->irq, di);bq27xxx_battery_teardown (di);mutex_lock (&battery_mutex);
Signature · 1 line
2.39.1
PATCH v2 3/10 power: supply: bq27xxx: Fix poll_interval handling and races on remove
In reply to Hans de GoedeBefore this patch bq27xxx_battery_teardown() was setting poll_interval = 0 to avoid bq27xxx_battery_update() requeuing the delayed_work item.
There are 2 problems with this:
1. If the driver is unbound through sysfs, rather then the module being rmmod-ed, this changes poll_interval unexpectedly
2. This is racy, after it being set poll_interval could be changed before bq27xxx_battery_update() checks it through /sys/module/bq27xxx_battery/parameters/poll_interval
Fix this by added a removed attribute to struct bq27xxx_device_info and using that instead of setting poll_interval to 0.
There also is another poll_interval related race on remove(), writing /sys/module/bq27xxx_battery/parameters/poll_interval will requeue the delayed_work item for all devices on the bq27xxx_battery_devices list and the device being removed was only removed from that list after cancelling the delayed_work item.
Fix this by moving the removal from the bq27xxx_battery_devices list to before cancelling the delayed_work item.
--- drivers/power/supply/bq27xxx_battery.c | 22 +++++++++------------- include/linux/power/bq27xxx_battery.h | 1 + 2 files changed, 10 insertions(+), 13 deletions(-)
drivers/power/supply/bq27xxx_battery.c +9 -13
@@ -1801,7 +1801,7 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di) di->last_update = jiffies; -if (poll_interval >0 ) +if (!di->removed && poll_interval >0 )mod_delayed_work (system_wq, &di->work, poll_interval *HZ ); }
@@ -2132,22 +2132,18 @@ EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);void bq27xxx_battery_teardown (struct bq27xxx_device_info *di) { -/* -* power_supply_unregister call bq27xxx_battery_get_property which -* call bq27xxx_battery_poll. -* Make sure that bq27xxx_battery_poll will not call -* schedule_delayed_work again after unregister (which cause OOPS). -*/ - poll_interval =0 ; - -cancel_delayed_work_sync (&di->work); - -power_supply_unregister (di->bat); -mutex_lock (&bq27xxx_list_lock);list_del (&di->list);mutex_unlock (&bq27xxx_list_lock); +/* Set removed to avoid bq27xxx_battery_update() re-queuing the work */ +mutex_lock (&di->lock); + di->removed = true; +mutex_unlock (&di->lock); + +cancel_delayed_work_sync (&di->work); + +power_supply_unregister (di->bat);mutex_destroy (&di->lock); }EXPORT_SYMBOL_GPL (bq27xxx_battery_teardown);
include/linux/power/bq27xxx_battery.h +1 -0
@@ -68,6 +68,7 @@ struct bq27xxx_device_info {struct bq27xxx_access_methods bus;struct bq27xxx_reg_cache cache;int charge_design_full; +bool removed;unsigned long last_update;struct delayed_work work;struct power_supply *bat;
Signature · 1 line
2.39.1
PATCH v2 4/10 power: supply: bq27xxx: Add cache parameter to bq27xxx_battery_current_and_status()
In reply to Hans de GoedeAdd a cache parameter to bq27xxx_battery_current_and_status() so that it can optionally use cached flags instead of re-reading them itself.
This is a preparation patch for making bq27xxx_battery_update() check the status and have it call power_supply_changed() on status changes.
--- drivers/power/supply/bq27xxx_battery.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
drivers/power/supply/bq27xxx_battery.c +12 -7
@@ -1840,7 +1840,8 @@ static bool bq27xxx_battery_is_full(struct bq27xxx_device_info *di, int flags)static int bq27xxx_battery_current_and_status (struct bq27xxx_device_info *di,union power_supply_propval *val_curr, -union power_supply_propval *val_status) +union power_supply_propval *val_status, +struct bq27xxx_reg_cache *cache) {bool single_flags = (di->opts &BQ27XXX_O_ZERO );int curr;
@@ -1852,10 +1853,14 @@ static int bq27xxx_battery_current_and_status(return curr; } - flags =bq27xxx_read (di,BQ27XXX_REG_FLAGS , single_flags); -if (flags <0 ) { -dev_err (di->dev,"error reading flags\n" ); -return flags; +if (cache) { + flags = cache->flags; + }else { + flags =bq27xxx_read (di,BQ27XXX_REG_FLAGS , single_flags); +if (flags <0 ) { +dev_err (di->dev,"error reading flags\n" ); +return flags; + } }if (di->opts &BQ27XXX_O_ZERO ) {
@@ -2001,7 +2006,7 @@ static int bq27xxx_battery_get_property(struct power_supply *psy,switch (psp) {case POWER_SUPPLY_PROP_STATUS : - ret =bq27xxx_battery_current_and_status (di,NULL , val); + ret =bq27xxx_battery_current_and_status (di,NULL , val,NULL );break ;case POWER_SUPPLY_PROP_VOLTAGE_NOW : ret =bq27xxx_battery_voltage (di, val);
@@ -2010,7 +2015,7 @@ static int bq27xxx_battery_get_property(struct power_supply *psy, val->intval = di->cache.flags <0 ?0 :1 ;break ;case POWER_SUPPLY_PROP_CURRENT_NOW : - ret =bq27xxx_battery_current_and_status (di, val,NULL ); + ret =bq27xxx_battery_current_and_status (di, val,NULL ,NULL );break ;case POWER_SUPPLY_PROP_CAPACITY : ret =bq27xxx_simple_value (di->cache.capacity, val);
Signature · 1 line
2.39.1
PATCH v2 5/10 power: supply: bq27xxx: Move bq27xxx_battery_update() down
In reply to Hans de GoedeMove the bq27xxx_battery_update() functions to below the bq27xxx_battery_current_and_status() function.
This is just moving a block of text, no functional changes.
This is a preparation patch for making bq27xxx_battery_update() check the status and have it call power_supply_changed() on status changes.
--- drivers/power/supply/bq27xxx_battery.c | 122 ++++++++++++------------- 1 file changed, 61 insertions(+), 61 deletions(-)
drivers/power/supply/bq27xxx_battery.c +61 -61
@@ -1761,67 +1761,6 @@ static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)return POWER_SUPPLY_HEALTH_GOOD ; } -static void bq27xxx_battery_update_unlocked (struct bq27xxx_device_info *di) -{ -struct bq27xxx_reg_cache cache = {0 , }; -bool has_singe_flag = di->opts &BQ27XXX_O_ZERO ; - - cache.flags =bq27xxx_read (di,BQ27XXX_REG_FLAGS , has_singe_flag); -if ((cache.flags &0xff ) ==0xff ) - cache.flags =-1 ;/* read error */ -if (cache.flags >=0 ) { - cache.temperature =bq27xxx_battery_read_temperature (di); -if (di->regs[BQ27XXX_REG_TTE ] !=INVALID_REG_ADDR ) - cache.time_to_empty =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTE ); -if (di->regs[BQ27XXX_REG_TTECP ] !=INVALID_REG_ADDR ) - cache.time_to_empty_avg =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTECP ); -if (di->regs[BQ27XXX_REG_TTF ] !=INVALID_REG_ADDR ) - cache.time_to_full =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTF ); - - cache.charge_full =bq27xxx_battery_read_fcc (di); - cache.capacity =bq27xxx_battery_read_soc (di); -if (di->regs[BQ27XXX_REG_AE ] !=INVALID_REG_ADDR ) - cache.energy =bq27xxx_battery_read_energy (di); - di->cache.flags = cache.flags; - cache.health =bq27xxx_battery_read_health (di); -if (di->regs[BQ27XXX_REG_CYCT ] !=INVALID_REG_ADDR ) - cache.cycle_count =bq27xxx_battery_read_cyct (di); - -/* We only have to read charge design full once */ -if (di->charge_design_full <=0 ) - di->charge_design_full =bq27xxx_battery_read_dcap (di); - } - -if ((di->cache.capacity != cache.capacity) || - (di->cache.flags != cache.flags)) -power_supply_changed (di->bat); - -if (memcmp (&di->cache, &cache,sizeof (cache)) !=0 ) - di->cache = cache; - - di->last_update = jiffies; - -if (!di->removed && poll_interval >0 ) -mod_delayed_work (system_wq, &di->work, poll_interval *HZ ); -} - -void bq27xxx_battery_update (struct bq27xxx_device_info *di) -{ -mutex_lock (&di->lock); -bq27xxx_battery_update_unlocked (di); -mutex_unlock (&di->lock); -} -EXPORT_SYMBOL_GPL (bq27xxx_battery_update); - -static void bq27xxx_battery_poll (struct work_struct *work) -{ -struct bq27xxx_device_info *di = - container_of(work, struct bq27xxx_device_info, - work.work); - -bq27xxx_battery_update (di); -} -static bool bq27xxx_battery_is_full (struct bq27xxx_device_info *di,int flags) {if (di->opts &BQ27XXX_O_ZERO )
@@ -1895,6 +1834,67 @@ static int bq27xxx_battery_current_and_status(return 0 ; } +static void bq27xxx_battery_update_unlocked (struct bq27xxx_device_info *di) +{ +struct bq27xxx_reg_cache cache = {0 , }; +bool has_singe_flag = di->opts &BQ27XXX_O_ZERO ; + + cache.flags =bq27xxx_read (di,BQ27XXX_REG_FLAGS , has_singe_flag); +if ((cache.flags &0xff ) ==0xff ) + cache.flags =-1 ;/* read error */ +if (cache.flags >=0 ) { + cache.temperature =bq27xxx_battery_read_temperature (di); +if (di->regs[BQ27XXX_REG_TTE ] !=INVALID_REG_ADDR ) + cache.time_to_empty =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTE ); +if (di->regs[BQ27XXX_REG_TTECP ] !=INVALID_REG_ADDR ) + cache.time_to_empty_avg =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTECP ); +if (di->regs[BQ27XXX_REG_TTF ] !=INVALID_REG_ADDR ) + cache.time_to_full =bq27xxx_battery_read_time (di,BQ27XXX_REG_TTF ); + + cache.charge_full =bq27xxx_battery_read_fcc (di); + cache.capacity =bq27xxx_battery_read_soc (di); +if (di->regs[BQ27XXX_REG_AE ] !=INVALID_REG_ADDR ) + cache.energy =bq27xxx_battery_read_energy (di); + di->cache.flags = cache.flags; + cache.health =bq27xxx_battery_read_health (di); +if (di->regs[BQ27XXX_REG_CYCT ] !=INVALID_REG_ADDR ) + cache.cycle_count =bq27xxx_battery_read_cyct (di); + +/* We only have to read charge design full once */ +if (di->charge_design_full <=0 ) + di->charge_design_full =bq27xxx_battery_read_dcap (di); + } + +if ((di->cache.capacity != cache.capacity) || + (di->cache.flags != cache.flags)) +power_supply_changed (di->bat); + +if (memcmp (&di->cache, &cache,sizeof (cache)) !=0 ) + di->cache = cache; + + di->last_update = jiffies; + +if (!di->removed && poll_interval >0 ) +mod_delayed_work (system_wq, &di->work, poll_interval *HZ ); +} + +void bq27xxx_battery_update (struct bq27xxx_device_info *di) +{ +mutex_lock (&di->lock); +bq27xxx_battery_update_unlocked (di); +mutex_unlock (&di->lock); +} +EXPORT_SYMBOL_GPL (bq27xxx_battery_update); + +static void bq27xxx_battery_poll (struct work_struct *work) +{ +struct bq27xxx_device_info *di = + container_of(work, struct bq27xxx_device_info, + work.work); + +bq27xxx_battery_update (di); +} +/* * Get the average power in µW * Return < 0 if something fails.
Signature · 1 line
2.39.1
PATCH v2 6/10 power: supply: bq27xxx: Ensure power_supply_changed() is called on current sign changes
In reply to Hans de GoedeOn gauges where the current register is signed, there is no charging flag in the flags register. So only checking flags will not result in power_supply_changed() getting called when e.g. a charger is plugged in and the current sign changes from negative (discharging) to positive (charging).
This causes userspace's notion of the status to lag until userspace does a poll.
And when a power_supply_leds.c LED trigger is used to indicate charging status with a LED, this LED will lag until the capacity percentage changes, which may take many minutes (because the LED trigger only is updated on power_supply_changed() calls).
Fix this by calling bq27xxx_battery_current_and_status() on gauges with a signed current register and checking if the status has changed.
--- drivers/power/supply/bq27xxx_battery.c | 13 ++++++++++++- include/linux/power/bq27xxx_battery.h | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-)
drivers/power/supply/bq27xxx_battery.c +12 -1
@@ -1836,6 +1836,7 @@ static int bq27xxx_battery_current_and_status(static void bq27xxx_battery_update_unlocked (struct bq27xxx_device_info *di) { +union power_supply_propval status = di->last_status;struct bq27xxx_reg_cache cache = {0 , };bool has_singe_flag = di->opts &BQ27XXX_O_ZERO ;
@@ -1860,14 +1861,24 @@ static void bq27xxx_battery_update_unlocked(struct bq27xxx_device_info *di)if (di->regs[BQ27XXX_REG_CYCT ] !=INVALID_REG_ADDR ) cache.cycle_count =bq27xxx_battery_read_cyct (di); +/* +* On gauges with signed current reporting the current must be +* checked to detect charging <-> discharging status changes. +*/ +if (!(di->opts &BQ27XXX_O_ZERO )) +bq27xxx_battery_current_and_status (di,NULL , &status, &cache); +/* We only have to read charge design full once */ if (di->charge_design_full <=0 ) di->charge_design_full =bq27xxx_battery_read_dcap (di); }if ((di->cache.capacity != cache.capacity) || - (di->cache.flags != cache.flags)) + (di->cache.flags != cache.flags) || + (di->last_status.intval != status.intval)) { + di->last_status.intval = status.intval;power_supply_changed (di->bat); + }if (memcmp (&di->cache, &cache,sizeof (cache)) !=0 ) di->cache = cache;
include/linux/power/bq27xxx_battery.h +3 -0
@@ -2,6 +2,8 @@#ifndef __LINUX_BQ27X00_BATTERY_H__#define __LINUX_BQ27X00_BATTERY_H__ +#include <linux/power_supply.h> +enum bq27xxx_chip {BQ27000 =1 ,/* bq27000, bq27200 */ BQ27010, /* bq27010, bq27210 */
@@ -70,6 +72,7 @@ struct bq27xxx_device_info {int charge_design_full;bool removed;unsigned long last_update; +union power_supply_propval last_status;struct delayed_work work;struct power_supply *bat;struct list_head list;
Signature · 1 line
2.39.1
PATCH v2 7/10 power: supply: bq27xxx: After charger plug in/out wait 0.5s for things to stabilize
In reply to Hans de Goedebq27xxx_external_power_changed() gets called when the charger is plugged in or out. Rather then immediately scheduling an update wait 0.5 seconds for things to stabilize, so that e.g. the (dis)charge current is stable when bq27xxx_battery_update() runs.
--- drivers/power/supply/bq27xxx_battery.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/power/supply/bq27xxx_battery.c +2 -2
@@ -2099,8 +2099,8 @@ static void bq27xxx_external_power_changed(struct power_supply *psy) {struct bq27xxx_device_info *di =power_supply_get_drvdata (psy); -cancel_delayed_work_sync (&di->work); -schedule_delayed_work (&di->work,0 ); +/* After charger plug in/out wait 0.5s for things to stabilize */ +mod_delayed_work (system_wq, &di->work,HZ /2 ); }int bq27xxx_battery_setup (struct bq27xxx_device_info *di)
Signature · 1 line
2.39.1
PATCH v2 8/10 power: supply: bq27xxx: Use mod_delayed_work() instead of cancel() + schedule()
In reply to Hans de GoedeUse mod_delayed_work() instead of separate cancel_delayed_work_sync() + schedule_delayed_work() calls.
--- drivers/power/supply/bq27xxx_battery.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/power/supply/bq27xxx_battery.c +2 -4
@@ -1083,10 +1083,8 @@ static int poll_interval_param_set(const char *val, const struct kernel_param *kreturn ret;mutex_lock (&bq27xxx_list_lock); -list_for_each_entry (di, &bq27xxx_battery_devices, list) { -cancel_delayed_work_sync (&di->work); -schedule_delayed_work (&di->work,0 ); - } +list_for_each_entry (di, &bq27xxx_battery_devices, list) +mod_delayed_work (system_wq, &di->work,0 );mutex_unlock (&bq27xxx_list_lock);return ret;
Signature · 1 line
2.39.1
PATCH v2 9/10 power: supply: bq25890: Call power_supply_changed() after updating input current or voltage
In reply to Hans de GoedeThe bq25892 model relies on external charger-type detection and once that is done the bq25890_charger code will update the input current and if pumpexpress is used also the input voltage.
In this case, when the initial power_supply_changed() call is made from the interrupt handler, the input settings are 5V/0.5A which on many devices is not enough power to charge (while the device is on).
On many devices the fuel-gauge relies in its external_power_changed callback to timely signal userspace about charging <-> discharging status changes. Add a power_supply_changed() call after updating the input current or voltage. This allows the fuel-gauge driver to timely recheck if the battery is charging after the new input settings have been applied and then it can immediately notify userspace about this.
--- drivers/power/supply/bq25890_charger.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/power/supply/bq25890_charger.c +3 -0
@@ -775,6 +775,7 @@ static void bq25890_charger_external_power_changed(struct power_supply *psy) }bq25890_field_write (bq,F_IINLIM , input_current_limit); +power_supply_changed (psy); } static int bq25890_get_chip_state(struct bq25890_device *bq,
@@ -1106,6 +1107,8 @@ static void bq25890_pump_express_work(struct work_struct *data)dev_info (bq->dev,"Hi-voltage charging requested, input voltage is %d mV\n" , voltage); +power_supply_changed (bq->charger); +return ; error_print:bq25890_field_write (bq,F_PUMPX_EN ,0 );
Signature · 1 line
2.39.1
PATCH v2 10/10 power: supply: bq24190: Call power_supply_changed() after updating input current
In reply to Hans de GoedeThe bq24192 model relies on external charger-type detection and once that is done the bq24190_charger code will update the input current.
In this case, when the initial power_supply_changed() call is made from the interrupt handler, the input settings are 5V/0.5A which on many devices is not enough power to charge (while the device is on).
On many devices the fuel-gauge relies in its external_power_changed callback to timely signal userspace about charging <-> discharging status changes. Add a power_supply_changed() call after updating the input current. This allows the fuel-gauge driver to timely recheck if the battery is charging after the new input current has been applied and then it can immediately notify userspace about this.
--- drivers/power/supply/bq24190_charger.c | 1 + 1 file changed, 1 insertion(+)
drivers/power/supply/bq24190_charger.c +1 -0
@@ -1262,6 +1262,7 @@ static void bq24190_input_current_limit_work(struct work_struct *work)bq24190_charger_set_property (bdi->charger,POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT , &val); +power_supply_changed (bdi->charger); }/* Sync the input-current-limit with our parent supply (if we have one) */
Signature · 1 line
2.39.1
Hi,
Quoted 15 lines · Hans de Goede
Hi all,
Here is v2 if my bq* status update fixes series
Changes in v2:
- Do not usebq->chargerinbq25890_external_power_changed(), because it may not be set yet (avoid NULL ptr deref on race)
- Add "power: supply: bq24190: Callpower_supply_changed()after updating input current" to the seriesThe main goal of this series is to fix userspace sometimes not getting notified in a timely manner of charging <-> discharging status changes, causing e.g. battery status icons to lag and the lagging of updates also effects
power_supply_leds.c.
Quoted 8 lines
While working on this I noticed a number of races surrounding
bq27xxx_battery_update(), so patches 1-4 fix those first.Patches 5-10 take care of the status update lagging issues.
Regards,
Hans
Thanks, I queued to the full series to my fixes branch.
-- Sebastian
Quoted 30 lines
Hans de Goede (10):
power: supply: bq27xxx: Fixbq27xxx_battery_update()race condition
power: supply: bq27xxx: Fix I2C IRQ race on remove
power: supply: bq27xxx: Fixpoll_intervalhandling and races on remove
power: supply: bq27xxx: Add cache parameter to
bq27xxx_battery_current_and_status()
power: supply: bq27xxx: Movebq27xxx_battery_update()down
power: supply: bq27xxx: Ensurepower_supply_changed()is called on
current sign changes
power: supply: bq27xxx: After charger plug in/out wait 0.5s for things
to stabilize
power: supply: bq27xxx: Usemod_delayed_work()instead ofcancel()+
schedule()
power: supply: bq25890: Callpower_supply_changed()after updating
input current or voltage
power: supply: bq24190: Callpower_supply_changed()after updating
input currentdrivers/power/supply/bq24190_charger.c | 1 + drivers/power/supply/bq25890_charger.c | 3 + drivers/power/supply/bq27xxx_battery.c | 181 +++++++++++---------- drivers/power/supply/bq27xxx_battery_i2c.c | 3 +- include/linux/power/bq27xxx_battery.h | 4 + 5 files changed, 108 insertions(+), 84 deletions(-)Signature · 1 line
2.39.1