Linux-OMAP Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 3/5] power: supply: cpcap-battery: invalidate empty->counter_uah and charge_full when they are grossly wrong
@ 2021-01-17 21:46 Carl Philipp Klemm
  2021-04-05  8:12 ` Sebastian Reichel
  0 siblings, 1 reply; 2+ messages in thread
From: Carl Philipp Klemm @ 2021-01-17 21:46 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: linux-pm, linux-omap, Arthur Demchenkov, Tony Lindgren,
	Merlijn Wajer, Pavel Machek

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 | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
index 86ed41d9627f..8ae0c9c7ebcb 100644
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -667,10 +667,22 @@ 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) {
+			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) {
+			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)
@@ -747,7 +759,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.29.2



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 3/5] power: supply: cpcap-battery: invalidate empty->counter_uah and charge_full when they are grossly wrong
  2021-01-17 21:46 [PATCH 3/5] power: supply: cpcap-battery: invalidate empty->counter_uah and charge_full when they are grossly wrong Carl Philipp Klemm
@ 2021-04-05  8:12 ` Sebastian Reichel
  0 siblings, 0 replies; 2+ messages in thread
From: Sebastian Reichel @ 2021-04-05  8:12 UTC (permalink / raw)
  To: Carl Philipp Klemm
  Cc: linux-pm, linux-omap, Arthur Demchenkov, Tony Lindgren,
	Merlijn Wajer, Pavel Machek


[-- Attachment #1: Type: text/plain, Size: 2399 bytes --]

Hi,

On Sun, Jan 17, 2021 at 10:46:38PM +0100, 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>
> ---
>  drivers/power/supply/cpcap-battery.c | 18 +++++++++++++++---
>  1 file changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
> index 86ed41d9627f..8ae0c9c7ebcb 100644
> --- a/drivers/power/supply/cpcap-battery.c
> +++ b/drivers/power/supply/cpcap-battery.c
> @@ -667,10 +667,22 @@ 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) {
> +			if (ddata->charge_full && abs(val->intval) > ddata->charge_full/5) {

Please add a comment what this check is doing. It's hard to grasp
without context of the commit when reading the driver at a later
point.

/* Assume invalid config if CHARGE_NOW is -20% */

> +				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) {

put closing curly bracket into the same line as the else. Also
please add sth. like

/* 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)
> @@ -747,7 +759,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;

-- Sebastian

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-17 21:46 [PATCH 3/5] power: supply: cpcap-battery: invalidate empty->counter_uah and charge_full when they are grossly wrong Carl Philipp Klemm
2021-04-05  8:12 ` Sebastian Reichel

Linux-OMAP Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-omap/0 linux-omap/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-omap linux-omap/ https://lore.kernel.org/linux-omap \
		linux-omap@vger.kernel.org
	public-inbox-index linux-omap

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-omap


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git