diff mbox series

[2/6] power: supply: ab8500: Standardize alert mode charging

Message ID 20220203171633.183828-3-linus.walleij@linaro.org
State New
Headers show
Series AB8500 charging, the final steps | expand

Commit Message

Linus Walleij Feb. 3, 2022, 5:16 p.m. UTC
The AB8500 code is using a special current and voltage setting
when the battery is in "alert mode", i.e. when it is starting
to go outside normal operating conditions so it is too
cold or too hot. This makes sense as a way for the charging
algorithm to deal with hostile environments.

Add the needed members to the struct power_supply_battery_info,
and switch the AB8500 charging code over to using this.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/power/supply/ab8500-bm.h         |  4 ----
 drivers/power/supply/ab8500_bmdata.c     |  9 +++++++--
 drivers/power/supply/ab8500_chargalg.c   |  4 ++--
 drivers/power/supply/power_supply_core.c |  2 ++
 include/linux/power_supply.h             | 10 ++++++++++
 5 files changed, 21 insertions(+), 8 deletions(-)

Comments

Vaittinen, Matti Feb. 4, 2022, 2:49 p.m. UTC | #1
Hi dee Ho Linus,

On 2/3/22 19:16, Linus Walleij wrote:
> The AB8500 code is using a special current and voltage setting
> when the battery is in "alert mode", i.e. when it is starting
> to go outside normal operating conditions so it is too
> cold or too hot. This makes sense as a way for the charging
> algorithm to deal with hostile environments.
> 
> Add the needed members to the struct power_supply_battery_info,
> and switch the AB8500 charging code over to using this.

Thanks for making this generic.

> 
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
> ---
>   drivers/power/supply/ab8500-bm.h         |  4 ----
>   drivers/power/supply/ab8500_bmdata.c     |  9 +++++++--
>   drivers/power/supply/ab8500_chargalg.c   |  4 ++--
>   drivers/power/supply/power_supply_core.c |  2 ++
>   include/linux/power_supply.h             | 10 ++++++++++
>   5 files changed, 21 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/power/supply/ab8500-bm.h b/drivers/power/supply/ab8500-bm.h
> index 4d74d21cf1eb..91ef9d4a5222 100644
> --- a/drivers/power/supply/ab8500-bm.h
> +++ b/drivers/power/supply/ab8500-bm.h
> @@ -331,14 +331,10 @@ struct ab8500_maxim_parameters {
>    * struct ab8500_battery_type - different batteries supported
>    * @resis_high:			battery upper resistance limit
>    * @resis_low:			battery lower resistance limit
> - * @low_high_cur_lvl:		charger current in temp low/high state in mA
> - * @low_high_vol_lvl:		charger voltage in temp low/high state in mV'
>    */
>   struct ab8500_battery_type {
>   	int resis_high;
>   	int resis_low;
> -	int low_high_cur_lvl;
> -	int low_high_vol_lvl;

I am just wondering if we might have cases where the 'mitigation action' 
should be different for low and high temperature alerts? As this is 
going to the DT it'd be nice to be prepared for different cases. I am 
definitely not an expert here but I could imagine that in some case 
increasing charge current at low temperature could warm-up the battery 
allowing charging for few more minutes(?) May be I am not making any 
sense here so please just ignore me if this sounds like nonsense - I 
keep often talking more and faster than thinking.

Best Regards
	-- Matti Vaittinen
Linus Walleij Feb. 13, 2022, 12:01 a.m. UTC | #2
On Fri, Feb 4, 2022 at 3:49 PM Vaittinen, Matti
<Matti.Vaittinen@fi.rohmeurope.com> wrote:

> I am just wondering if we might have cases where the 'mitigation action'
> should be different for low and high temperature alerts? As this is
> going to the DT it'd be nice to be prepared for different cases. I am
> definitely not an expert here but I could imagine that in some case
> increasing charge current at low temperature could warm-up the battery
> allowing charging for few more minutes(?) May be I am not making any
> sense here so please just ignore me if this sounds like nonsense - I
> keep often talking more and faster than thinking.

It makes perfect sense of course.

I augmented the patch to account for this, and also augmented the
AB8500 charging algorithm to support the two cases, sadly I don't have
corresponding battery data from Samsung so the currents and voltages
will be the same in my static data, but it makes a lot of sense
so let's do it like this.

Yours,
Linus Walleij
diff mbox series

Patch

diff --git a/drivers/power/supply/ab8500-bm.h b/drivers/power/supply/ab8500-bm.h
index 4d74d21cf1eb..91ef9d4a5222 100644
--- a/drivers/power/supply/ab8500-bm.h
+++ b/drivers/power/supply/ab8500-bm.h
@@ -331,14 +331,10 @@  struct ab8500_maxim_parameters {
  * struct ab8500_battery_type - different batteries supported
  * @resis_high:			battery upper resistance limit
  * @resis_low:			battery lower resistance limit
- * @low_high_cur_lvl:		charger current in temp low/high state in mA
- * @low_high_vol_lvl:		charger voltage in temp low/high state in mV'
  */
 struct ab8500_battery_type {
 	int resis_high;
 	int resis_low;
-	int low_high_cur_lvl;
-	int low_high_vol_lvl;
 };
 
 /**
diff --git a/drivers/power/supply/ab8500_bmdata.c b/drivers/power/supply/ab8500_bmdata.c
index c104afe83b4b..c878dc6e4197 100644
--- a/drivers/power/supply/ab8500_bmdata.c
+++ b/drivers/power/supply/ab8500_bmdata.c
@@ -77,8 +77,6 @@  struct power_supply_maintenance_charge_table maint_charge_table[] = {
 static struct ab8500_battery_type bat_type_thermistor_unknown = {
 	.resis_high = 0,
 	.resis_low = 0,
-	.low_high_cur_lvl = 300,
-	.low_high_vol_lvl = 4000,
 };
 
 static const struct ab8500_bm_capacity_levels cap_levels = {
@@ -192,6 +190,13 @@  int ab8500_bm_of_probe(struct power_supply *psy,
 		bi->maintenance_charge_size = ARRAY_SIZE(maint_charge_table);
 	}
 
+	if (bi->alert_temp_charge_current_ua < 0 ||
+	    bi->alert_temp_charge_voltage_uv < 0)
+	{
+		bi->alert_temp_charge_current_ua = 300000;
+		bi->alert_temp_charge_voltage_uv = 4000000;
+	}
+
 	/*
 	 * Internal resistance and factory resistance are tightly coupled
 	 * so both MUST be defined or we fall back to defaults.
diff --git a/drivers/power/supply/ab8500_chargalg.c b/drivers/power/supply/ab8500_chargalg.c
index 6054996b6260..01fd2ef6e6b5 100644
--- a/drivers/power/supply/ab8500_chargalg.c
+++ b/drivers/power/supply/ab8500_chargalg.c
@@ -1511,8 +1511,8 @@  static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di)
 
 	case STATE_TEMP_LOWHIGH_INIT:
 		ab8500_chargalg_start_charging(di,
-			di->bm->bat_type->low_high_vol_lvl,
-			di->bm->bat_type->low_high_cur_lvl);
+			bi->alert_temp_charge_voltage_uv,
+			bi->alert_temp_charge_current_ua);
 		ab8500_chargalg_stop_maintenance_timer(di);
 		di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
 		ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index 6568939e4518..01f9898ab548 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -591,6 +591,8 @@  int power_supply_get_battery_info(struct power_supply *psy,
 	info->charge_restart_voltage_uv      = -EINVAL;
 	info->overvoltage_limit_uv           = -EINVAL;
 	info->maintenance_charge             = NULL;
+	info->alert_temp_charge_current_ua   = -EINVAL;
+	info->alert_temp_charge_voltage_uv   = -EINVAL;
 	info->temp_ambient_alert_min         = INT_MIN;
 	info->temp_ambient_alert_max         = INT_MAX;
 	info->temp_alert_min                 = INT_MIN;
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index b998fc4c87ae..5a059c013c12 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -443,6 +443,14 @@  struct power_supply_maintenance_charge_table {
  *   after the main CC/CV charging phase is complete.
  * @maintenance_charge_size: the number of maintenance charging settings in
  *   maintenance_charge.
+ * @alert_temp_charge_current_ua: The charging current to use if the battery
+ *   enters alert temperatures. I.e. if the internal temperature is between
+ *   temp_alert_min and temp_min or temp_alert_max and temp_max. No matter
+ *   the charging phase, this and alert_temp_charge_voltage_uv will be
+ *   applied under alert temperature conditions, usually lowering the charging
+ *   current as an evasive manouver.
+ * @alert_temp_charge_voltage_uv: Same as alert_temp_charge_current_ua, but
+ *   for the charging voltage.
  * @factory_internal_resistance_uohm: the internal resistance of the battery
  *   at fabrication time, expressed in microohms. This resistance will vary
  *   depending on the lifetime and charge of the battery, so this is just a
@@ -594,6 +602,8 @@  struct power_supply_battery_info {
 	int constant_charge_voltage_max_uv;
 	struct power_supply_maintenance_charge_table *maintenance_charge;
 	int maintenance_charge_size;
+	int alert_temp_charge_current_ua;
+	int alert_temp_charge_voltage_uv;
 	int factory_internal_resistance_uohm;
 	int ocv_temp[POWER_SUPPLY_OCV_TEMP_MAX];
 	int temp_ambient_alert_min;