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

Commit 29684f14 authored by Luca Stefani's avatar Luca Stefani Committed by Razziell
Browse files

power: qpnp-fg: Implement mi_last_soc

* In case the battery profile doesn't load we now load the soc directly from the battery

Change-Id: Idbda39362d7a81da455844419b6d37b575284cbf
parent 84e4409f
Loading
Loading
Loading
Loading
+80 −0
Original line number Original line Diff line number Diff line
@@ -503,6 +503,7 @@ struct fg_chip {
	struct delayed_work	update_sram_data;
	struct delayed_work	update_sram_data;
	struct delayed_work	update_temp_work;
	struct delayed_work	update_temp_work;
	struct delayed_work	check_empty_work;
	struct delayed_work	check_empty_work;
	struct delayed_work	soc_work;
	char			*batt_profile;
	char			*batt_profile;
	u8			thermal_coefficients[THERMAL_COEFF_N_BYTES];
	u8			thermal_coefficients[THERMAL_COEFF_N_BYTES];
	u32			cc_cv_threshold_mv;
	u32			cc_cv_threshold_mv;
@@ -540,6 +541,7 @@ struct fg_chip {
	u16			*offset;
	u16			*offset;
	bool			ima_supported;
	bool			ima_supported;
	bool			init_done;
	bool			init_done;
	int			mi_last_soc;
	/* jeita hysteresis */
	/* jeita hysteresis */
	bool			jeita_hysteresis_support;
	bool			jeita_hysteresis_support;
	bool			batt_hot;
	bool			batt_hot;
@@ -1862,6 +1864,11 @@ static int get_prop_capacity(struct fg_chip *chip)


	if (chip->battery_missing)
	if (chip->battery_missing)
		return MISSING_CAPACITY;
		return MISSING_CAPACITY;
	if (!chip->profile_loaded && !chip->use_otp_profile) {
		pr_info("loading batt profile, return last soc %d\n", chip->mi_last_soc);
		return chip->mi_last_soc;
	}

	if (chip->charge_full)
	if (chip->charge_full)
		return FULL_CAPACITY;
		return FULL_CAPACITY;
	if (chip->soc_empty) {
	if (chip->soc_empty) {
@@ -1879,6 +1886,37 @@ static int get_prop_capacity(struct fg_chip *chip)
			FULL_SOC_RAW - 2) + 1;
			FULL_SOC_RAW - 2) + 1;
}
}


static int get_last_soc(struct fg_chip *chip)
{
	u8 cap[2];
	int rc, capacity = 0, tries = 0;

	while (tries < MAX_TRIES_SOC) {
		rc = fg_read(chip, cap,
				chip->soc_base + SOC_MONOTONIC_SOC, 2);
		if (rc) {
			pr_err("spmi read failed: addr=%03x, rc=%d\n",
				chip->soc_base + SOC_MONOTONIC_SOC, rc);
			return rc;
		}

		if (cap[0] == cap[1])
			break;

		tries++;
	}

	if (tries == MAX_TRIES_SOC) {
		pr_err("shadow registers do not match\n");
		return DEFAULT_CAPACITY;
	}

	if (cap[0] > 0)
		capacity = (cap[0] * 100 / FULL_PERCENT);

	return capacity;
}

#define HIGH_BIAS	3
#define HIGH_BIAS	3
#define MED_BIAS	BIT(1)
#define MED_BIAS	BIT(1)
#define LOW_BIAS	BIT(0)
#define LOW_BIAS	BIT(0)
@@ -2215,6 +2253,36 @@ resched:
	fg_relax(&chip->update_sram_wakeup_source);
	fg_relax(&chip->update_sram_wakeup_source);
}
}


#define SOC_WORK_MS	20000
static void soc_work_fn(struct work_struct *work)
{
	struct fg_chip *chip = container_of(work,
				struct fg_chip,
				soc_work.work);
	int soc;
	static int prev_soc = -EINVAL;

	soc = get_prop_capacity(chip);
	pr_info("adjust_soc: s %d r %d i %d v %d t %d\n",
			soc,
			get_sram_prop_now(chip, FG_DATA_BATT_ESR),
			get_sram_prop_now(chip, FG_DATA_CURRENT),
			get_sram_prop_now(chip, FG_DATA_VOLTAGE),
			get_sram_prop_now(chip, FG_DATA_BATT_TEMP));

	/* if soc changes, report power supply change uevent */
	if (soc != prev_soc) {
		if (chip->power_supply_registered)
			power_supply_changed(&chip->bms_psy);
		prev_soc = soc;
	}

	schedule_delayed_work(
		&chip->soc_work,
		msecs_to_jiffies(SOC_WORK_MS));

}

#define SRAM_TIMEOUT_MS			3000
#define SRAM_TIMEOUT_MS			3000
static void update_sram_data_work(struct work_struct *work)
static void update_sram_data_work(struct work_struct *work)
{
{
@@ -6485,6 +6553,7 @@ static int fg_init_irqs(struct fg_chip *chip)


static void fg_cleanup(struct fg_chip *chip)
static void fg_cleanup(struct fg_chip *chip)
{
{
	cancel_delayed_work_sync(&chip->soc_work);
	cancel_delayed_work_sync(&chip->update_sram_data);
	cancel_delayed_work_sync(&chip->update_sram_data);
	cancel_delayed_work_sync(&chip->update_temp_work);
	cancel_delayed_work_sync(&chip->update_temp_work);
	cancel_delayed_work_sync(&chip->update_jeita_setting);
	cancel_delayed_work_sync(&chip->update_jeita_setting);
@@ -7439,6 +7508,9 @@ static void delayed_init_work(struct work_struct *work)
	if (chip->last_temp_update_time == 0)
	if (chip->last_temp_update_time == 0)
		update_temp_data(&chip->update_temp_work.work);
		update_temp_data(&chip->update_temp_work.work);


       schedule_delayed_work(&chip->soc_work,
               msecs_to_jiffies(SOC_WORK_MS));

	if (!chip->use_otp_profile)
	if (!chip->use_otp_profile)
		schedule_delayed_work(&chip->batt_profile_init, 0);
		schedule_delayed_work(&chip->batt_profile_init, 0);


@@ -7557,6 +7629,7 @@ static int fg_probe(struct spmi_device *spmi)
	INIT_DELAYED_WORK(&chip->update_sram_data, update_sram_data_work);
	INIT_DELAYED_WORK(&chip->update_sram_data, update_sram_data_work);
	INIT_DELAYED_WORK(&chip->update_temp_work, update_temp_data);
	INIT_DELAYED_WORK(&chip->update_temp_work, update_temp_data);
	INIT_DELAYED_WORK(&chip->check_empty_work, check_empty_work);
	INIT_DELAYED_WORK(&chip->check_empty_work, check_empty_work);
	INIT_DELAYED_WORK(&chip->soc_work, soc_work_fn);
	INIT_DELAYED_WORK(&chip->batt_profile_init, batt_profile_init);
	INIT_DELAYED_WORK(&chip->batt_profile_init, batt_profile_init);
	INIT_WORK(&chip->rslow_comp_work, rslow_comp_work);
	INIT_WORK(&chip->rslow_comp_work, rslow_comp_work);
	INIT_WORK(&chip->fg_cap_learning_work, fg_cap_learning_work);
	INIT_WORK(&chip->fg_cap_learning_work, fg_cap_learning_work);
@@ -7713,6 +7786,9 @@ static int fg_probe(struct spmi_device *spmi)
		}
		}
	}
	}


	chip->mi_last_soc = get_last_soc(chip);
	pr_info("last soc %d\n", chip->mi_last_soc);

	/* Initialize batt_info variables */
	/* Initialize batt_info variables */
	chip->batt_range_ocv = &fg_batt_valid_ocv;
	chip->batt_range_ocv = &fg_batt_valid_ocv;
	chip->batt_range_pct = &fg_batt_range_pct;
	chip->batt_range_pct = &fg_batt_range_pct;
@@ -7796,6 +7872,9 @@ static void check_and_update_sram_data(struct fg_chip *chip)


	schedule_delayed_work(
	schedule_delayed_work(
		&chip->update_sram_data, msecs_to_jiffies(time_left * 1000));
		&chip->update_sram_data, msecs_to_jiffies(time_left * 1000));

	schedule_delayed_work(
		&chip->soc_work, msecs_to_jiffies(SOC_WORK_MS));
}
}


static int fg_suspend(struct device *dev)
static int fg_suspend(struct device *dev)
@@ -7805,6 +7884,7 @@ static int fg_suspend(struct device *dev)
	if (!chip->sw_rbias_ctrl)
	if (!chip->sw_rbias_ctrl)
		return 0;
		return 0;


	cancel_delayed_work(&chip->soc_work);
	cancel_delayed_work(&chip->update_temp_work);
	cancel_delayed_work(&chip->update_temp_work);
	cancel_delayed_work(&chip->update_sram_data);
	cancel_delayed_work(&chip->update_sram_data);