Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 27c60c16 authored by Subbaraman Narayanamurthy's avatar Subbaraman Narayanamurthy
Browse files

power: qpnp-fg-gen4: Add support for CYCLE_COUNT property



Currently, CYCLE_COUNTS property shows the bucket cycle
counters as a string. Add CYCLE_COUNT property in FG that can
provide an average of all the bucket counters.

Move the helper functions that gets the cycle_counts and
cycle_count property to fg-alg.c so that they can be re-used.

Change-Id: I17291cb6a3c15fe66700f6ee1bbb3dd7327ce6f2
Signed-off-by: default avatarSubbaraman Narayanamurthy <subbaram@codeaurora.org>
parent ef22add4
Loading
Loading
Loading
Loading
+66 −2
Original line number Diff line number Diff line
@@ -164,13 +164,13 @@ void cycle_count_update(struct cycle_counter *counter, int batt_soc,
}

/**
 * get_cycle_count -
 * get_bucket_cycle_count -
 * @counter: Cycle counter object
 *
 * Returns the cycle counter for a SOC bucket.
 *
 */
int get_cycle_count(struct cycle_counter *counter)
static int get_bucket_cycle_count(struct cycle_counter *counter)
{
	int count;

@@ -186,6 +186,70 @@ int get_cycle_count(struct cycle_counter *counter)
	return count;
}

/**
 * get_cycle_count -
 * @counter: Cycle counter object
 * @count: Average cycle count returned to the caller
 *
 * Get average cycle count for all buckets
 *
 */
int get_cycle_count(struct cycle_counter *counter, int *count)
{
	int i, rc, temp = 0;

	for (i = 1; i <= BUCKET_COUNT; i++) {
		counter->id = i;
		rc = get_bucket_cycle_count(counter);
		if (rc < 0) {
			pr_err("Couldn't get cycle count rc=%d\n", rc);
			return rc;
		}
		temp += rc;
	}

	/*
	 * Normalize the counter across each bucket so that we can get
	 * the overall charge cycle count.
	 */

	*count = temp / BUCKET_COUNT;
	return 0;
}

/**
 * get_cycle_counts -
 * @counter: Cycle counter object
 * @buf: Bucket cycle counts formatted in a string returned to the caller
 *
 * Get cycle count for all buckets in a string format
 *
 */
int get_cycle_counts(struct cycle_counter *counter, const char **buf)
{
	int i, rc, len = 0;

	for (i = 1; i <= BUCKET_COUNT; i++) {
		counter->id = i;
		rc = get_bucket_cycle_count(counter);
		if (rc < 0) {
			pr_err("Couldn't get cycle count rc=%d\n", rc);
			return rc;
		}

		if (sizeof(counter->str_buf) - len < 8) {
			pr_err("Invalid length %d\n", len);
			return -EINVAL;
		}

		len += snprintf(counter->str_buf + len, 8, "%d ", rc);
	}

	counter->str_buf[len] = '\0';
	*buf = counter->str_buf;
	return 0;
}

/**
 * cycle_count_init -
 * @counter: Cycle counter object
+3 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

struct cycle_counter {
	void		*data;
	char		str_buf[BUCKET_COUNT * 8];
	bool		started[BUCKET_COUNT];
	u16		count[BUCKET_COUNT];
	u8		last_soc[BUCKET_COUNT];
@@ -60,7 +61,8 @@ int restore_cycle_count(struct cycle_counter *counter);
void clear_cycle_count(struct cycle_counter *counter);
void cycle_count_update(struct cycle_counter *counter, int batt_soc,
		int charge_status, bool charge_done, bool input_present);
int get_cycle_count(struct cycle_counter *counter);
int get_cycle_count(struct cycle_counter *counter, int *count);
int get_cycle_counts(struct cycle_counter *counter, const char **buf);
int cycle_count_init(struct cycle_counter *counter);
void cap_learning_abort(struct cap_learning *cl);
void cap_learning_update(struct cap_learning *cl, int batt_temp,
+6 −27
Original line number Diff line number Diff line
@@ -148,7 +148,6 @@ struct fg_gen4_chip {
	struct ttf		ttf;
	struct delayed_work	ttf_work;
	char			batt_profile[PROFILE_LEN];
	char			counter_buf[BUCKET_COUNT * 8];
	bool			ki_coeff_dischg_en;
	bool			slope_limit_en;
};
@@ -2037,31 +2036,6 @@ static int fg_get_time_to_empty(struct fg_dev *fg, int *val)
	return 0;
}

static const char *fg_gen4_get_cycle_counts(struct fg_gen4_chip *chip)
{
	int i, rc, len = 0;
	char *buf;

	buf = chip->counter_buf;
	for (i = 1; i <= BUCKET_COUNT; i++) {
		chip->counter->id = i;
		rc = get_cycle_count(chip->counter);
		if (rc < 0) {
			pr_err("Couldn't get cycle count rc=%d\n", rc);
			return NULL;
		}

		if (sizeof(chip->counter_buf) - len < 8) {
			pr_err("Invalid length %d\n", len);
			return NULL;
		}

		len += snprintf(buf+len, 8, "%d ", rc);
	}

	buf[len] = '\0';
	return buf;
}

static void sram_dump_work(struct work_struct *work)
{
@@ -2240,8 +2214,13 @@ static int fg_psy_get_property(struct power_supply *psy,
	case POWER_SUPPLY_PROP_CHARGE_COUNTER_SHADOW:
		rc = fg_gen4_get_charge_counter_shadow(chip, &pval->intval);
		break;
	case POWER_SUPPLY_PROP_CYCLE_COUNT:
		rc = get_cycle_count(chip->counter, &pval->intval);
		break;
	case POWER_SUPPLY_PROP_CYCLE_COUNTS:
		pval->strval = fg_gen4_get_cycle_counts(chip);
		rc = get_cycle_counts(chip->counter, &pval->strval);
		if (rc < 0)
			pval->strval = NULL;
		break;
	case POWER_SUPPLY_PROP_SOC_REPORTING_READY:
		pval->intval = fg->soc_reporting_ready;