Skip to content

Commit 7f93e1f

Browse files
paulkocialkowskisre
authored andcommitted
power: supply: sbs-battery: Correct supply status with current draw
The status reported directly by the battery controller is not always reliable and should be corrected based on the current draw information. This implements such a correction with a dedicated function, called where the supply status is retrieved. Signed-off-by: Paul Kocialkowski <contact@paulk.fr> Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.co.uk>
1 parent 4df2cce commit 7f93e1f

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎drivers/power/supply/sbs-battery.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,31 @@ static int sbs_write_word_data(struct i2c_client *client, u8 address,
295295
return 0;
296296
}
297297

298+
static int sbs_status_correct(struct i2c_client *client, int *intval)
299+
{
300+
int ret;
301+
302+
ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
303+
if (ret < 0)
304+
return ret;
305+
306+
ret = (s16)ret;
307+
308+
/* Not drawing current means full (cannot be not charging) */
309+
if (ret == 0)
310+
*intval = POWER_SUPPLY_STATUS_FULL;
311+
312+
if (*intval == POWER_SUPPLY_STATUS_FULL) {
313+
/* Drawing or providing current when full */
314+
if (ret > 0)
315+
*intval = POWER_SUPPLY_STATUS_CHARGING;
316+
else if (ret < 0)
317+
*intval = POWER_SUPPLY_STATUS_DISCHARGING;
318+
}
319+
320+
return 0;
321+
}
322+
298323
static int sbs_get_battery_presence_and_health(
299324
struct i2c_client *client, enum power_supply_property psp,
300325
union power_supply_propval *val)
@@ -401,6 +426,8 @@ static int sbs_get_battery_property(struct i2c_client *client,
401426
else
402427
val->intval = POWER_SUPPLY_STATUS_CHARGING;
403428

429+
sbs_status_correct(client, &val->intval);
430+
404431
if (chip->poll_time == 0)
405432
chip->last_state = val->intval;
406433
else if (chip->last_state != val->intval) {
@@ -721,6 +748,8 @@ static void sbs_delayed_work(struct work_struct *work)
721748
else
722749
ret = POWER_SUPPLY_STATUS_CHARGING;
723750

751+
sbs_status_correct(chip->client, &ret);
752+
724753
if (chip->last_state != ret) {
725754
chip->poll_time = 0;
726755
power_supply_changed(chip->power_supply);

0 commit comments

Comments
 (0)