diff mbox series

[v2] power: supply: cpcap-battery: invalidate empty->counter_uah and charge_full when charge_now indicates that they are grossly wrong

Message ID 20210423145831.a8f0ba37baa211353cdf43b8@uvos.xyz
State New
Headers show
Series [v2] power: supply: cpcap-battery: invalidate empty->counter_uah and charge_full when charge_now indicates that they are grossly wrong | expand

Commit Message

Carl Philipp Klemm April 23, 2021, 12:58 p.m. UTC
This invalidates empty->counter_uah and charge_full when charge_now indicates
that they are grossly wrong and adds some tolerance to
POWER_SUPPLY_PROP_CHARGE_FULL to allow for inaccuracies in the charge counter
and manufacturing tolerances in the battery.

Signed-off-by: Carl Philipp Klemm <philipp@uvos.xyz>
---
 drivers/power/supply/cpcap-battery.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

Comments

Sebastian Reichel June 4, 2021, 12:37 p.m. UTC | #1
Hi,

On Fri, Apr 23, 2021 at 02:58:31PM +0200, Carl Philipp Klemm wrote:
> This invalidates empty->counter_uah and charge_full when charge_now indicates
> that they are grossly wrong and adds some tolerance to
> POWER_SUPPLY_PROP_CHARGE_FULL to allow for inaccuracies in the charge counter
> and manufacturing tolerances in the battery.
> 
> Signed-off-by: Carl Philipp Klemm <philipp@uvos.xyz>
> ---

Thanks, queued.

-- Sebastian

>  drivers/power/supply/cpcap-battery.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
> index 386d269e699f..9d9453231ea7 100644
> --- a/drivers/power/supply/cpcap-battery.c
> +++ b/drivers/power/supply/cpcap-battery.c
> @@ -762,10 +762,23 @@ static int cpcap_battery_get_property(struct power_supply *psy,
>  		if (!empty->voltage)
>  			return -ENODATA;
>  		val->intval = empty->counter_uah - latest->counter_uah;
> -		if (val->intval < 0)
> +		if (val->intval < 0) {
> +			/* Assume invalid config if CHARGE_NOW is -20% */
> +			if (ddata->charge_full && abs(val->intval) > ddata->charge_full/5) {
> +				empty->voltage = 0;
> +				ddata->charge_full = 0;
> +				return -ENODATA;
> +			}
>  			val->intval = 0;
> -		else if (ddata->charge_full && ddata->charge_full < val->intval)
> +		} else if (ddata->charge_full && ddata->charge_full < val->intval) {
> +			/* Assume invalid config if CHARGE_NOW exceeds CHARGE_FULL by 20% */
> +			if (val->intval > (6*ddata->charge_full)/5) {
> +				empty->voltage = 0;
> +				ddata->charge_full = 0;
> +				return -ENODATA;
> +			}
>  			val->intval = ddata->charge_full;
> +		}
>  		break;
>  	case POWER_SUPPLY_PROP_CHARGE_FULL:
>  		if (!ddata->charge_full)
> @@ -842,7 +855,7 @@ static int cpcap_battery_set_property(struct power_supply *psy,
>  	case POWER_SUPPLY_PROP_CHARGE_FULL:
>  		if (val->intval < 0)
>  			return -EINVAL;
> -		if (val->intval > ddata->config.info.charge_full_design)
> +		if (val->intval > (6*ddata->config.info.charge_full_design)/5)
>  			return -EINVAL;
>  
>  		ddata->charge_full = val->intval;
> -- 
> 2.31.0
> 
>
diff mbox series

Patch

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
index 386d269e699f..9d9453231ea7 100644
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -762,10 +762,23 @@  static int cpcap_battery_get_property(struct power_supply *psy,
 		if (!empty->voltage)
 			return -ENODATA;
 		val->intval = empty->counter_uah - latest->counter_uah;
-		if (val->intval < 0)
+		if (val->intval < 0) {
+			/* Assume invalid config if CHARGE_NOW is -20% */
+			if (ddata->charge_full && abs(val->intval) > ddata->charge_full/5) {
+				empty->voltage = 0;
+				ddata->charge_full = 0;
+				return -ENODATA;
+			}
 			val->intval = 0;
-		else if (ddata->charge_full && ddata->charge_full < val->intval)
+		} else if (ddata->charge_full && ddata->charge_full < val->intval) {
+			/* Assume invalid config if CHARGE_NOW exceeds CHARGE_FULL by 20% */
+			if (val->intval > (6*ddata->charge_full)/5) {
+				empty->voltage = 0;
+				ddata->charge_full = 0;
+				return -ENODATA;
+			}
 			val->intval = ddata->charge_full;
+		}
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_FULL:
 		if (!ddata->charge_full)
@@ -842,7 +855,7 @@  static int cpcap_battery_set_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_CHARGE_FULL:
 		if (val->intval < 0)
 			return -EINVAL;
-		if (val->intval > ddata->config.info.charge_full_design)
+		if (val->intval > (6*ddata->config.info.charge_full_design)/5)
 			return -EINVAL;
 
 		ddata->charge_full = val->intval;