Skip to content

Commit

Permalink
power: qpnp-fg-gen3: Limit how frequently fg data can be queried
Browse files Browse the repository at this point in the history
Querying the fuelgauge for data is expensive, so doing it frequently can
result in a significant increase in power consumption.

Limit how frequently data queries can be sent to 5 seconds in order to
prevent fuelgauge queries from consuming too much power. Only do this while
there is no USB cable connected to the device, since the charger drivers
need fresh statistics when there's a USB cable connected.

Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com>
  • Loading branch information
kerneltoast committed Mar 11, 2019
1 parent 11063c2 commit bd95828
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions drivers/power/supply/qcom/fg-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ static const struct fg_pt fg_tsmc_osc_table[] = {
{ 90, 444992 },
};

struct fg_saved_data {
union power_supply_propval val;
unsigned long last_req_expires;
};

struct fg_chip {
struct thermal_zone_device *tz_dev;
struct device *dev;
Expand Down Expand Up @@ -496,6 +501,7 @@ struct fg_chip {
struct work_struct esr_filter_work;
struct alarm esr_filter_alarm;
ktime_t last_delta_temp_time;
struct fg_saved_data saved_data[POWER_SUPPLY_PROP_MAX];
};

/* Debugfs data structures are below */
Expand Down
33 changes: 33 additions & 0 deletions drivers/power/supply/qcom/qpnp-fg-gen3.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ module_param_named(
static int fg_restart;
static bool fg_sram_dump;

#define FG_RATE_LIM_MS (5 * MSEC_PER_SEC)

/* All getters HERE */

#define VOLTAGE_15BIT_MASK GENMASK(14, 0)
Expand Down Expand Up @@ -3874,8 +3876,36 @@ static int fg_psy_get_property(struct power_supply *psy,
union power_supply_propval *pval)
{
struct fg_chip *chip = power_supply_get_drvdata(psy);
struct fg_saved_data *sd = chip->saved_data + psp;
union power_supply_propval typec_sts = { .intval = -1 };
int rc = 0;

switch (psp) {
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
case POWER_SUPPLY_PROP_RESISTANCE_ID:
case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
case POWER_SUPPLY_PROP_CYCLE_COUNT_ID:
case POWER_SUPPLY_PROP_CHARGE_NOW:
case POWER_SUPPLY_PROP_CHARGE_FULL:
case POWER_SUPPLY_PROP_SOC_REPORTING_READY:
/* These props don't require a fg query; don't ratelimit them */
break;
default:
if (!sd->last_req_expires)
break;

if (usb_psy_initialized(chip))
power_supply_get_property(chip->usb_psy,
POWER_SUPPLY_PROP_TYPEC_MODE, &typec_sts);

if (typec_sts.intval == POWER_SUPPLY_TYPEC_NONE &&
time_before(jiffies, sd->last_req_expires)) {
*pval = sd->val;
return 0;
}
break;
}

switch (psp) {
case POWER_SUPPLY_PROP_CAPACITY:
rc = fg_get_prop_capacity(chip, &pval->intval);
Expand Down Expand Up @@ -4006,6 +4036,9 @@ static int fg_psy_get_property(struct power_supply *psy,
if (rc < 0)
return -ENODATA;

sd->val = *pval;
sd->last_req_expires = jiffies + msecs_to_jiffies(FG_RATE_LIM_MS);

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions include/linux/power_supply.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ enum power_supply_property {
POWER_SUPPLY_PROP_SERIAL_NUMBER,
POWER_SUPPLY_PROP_BATTERY_TYPE,
POWER_SUPPLY_PROP_CYCLE_COUNTS,
POWER_SUPPLY_PROP_MAX
};

enum power_supply_type {
Expand Down

0 comments on commit bd95828

Please sign in to comment.