[2/5] power: supply: cpcap-battery: Simplify coulomb counter calculation with div_s64
diff mbox series

Message ID 20191009210621.10522-3-tony@atomide.com
State New
Headers show
Series
  • cpcap battery simplification and calibrate support
Related show

Commit Message

Tony Lindgren Oct. 9, 2019, 9:06 p.m. UTC
We can simplify cpcap_battery_cc_raw_div() with div_s64.

Cc: Merlijn Wajer <merlijn@wizzup.org>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
 drivers/power/supply/cpcap-battery.c | 23 +++++------------------
 1 file changed, 5 insertions(+), 18 deletions(-)

Comments

Pavel Machek Oct. 13, 2019, 11:01 a.m. UTC | #1
On Wed 2019-10-09 14:06:18, Tony Lindgren wrote:
> We can simplify cpcap_battery_cc_raw_div() with div_s64.
> 
> Cc: Merlijn Wajer <merlijn@wizzup.org>
> Cc: Pavel Machek <pavel@ucw.cz>

Acked-by: Pavel Machek <pavel@ucw.cz>

									Pavel
Pavel Machek Oct. 13, 2019, 11:36 a.m. UTC | #2
On Wed 2019-10-09 14:06:18, Tony Lindgren wrote:
> We can simplify cpcap_battery_cc_raw_div() with div_s64.
> 
> Cc: Merlijn Wajer <merlijn@wizzup.org>
> Cc: Pavel Machek <pavel@ucw.cz>

Acked-by: Pavel Machek <pavel@ucw.cz>

									Pavel

Patch
diff mbox series

diff --git a/drivers/power/supply/cpcap-battery.c b/drivers/power/supply/cpcap-battery.c
--- a/drivers/power/supply/cpcap-battery.c
+++ b/drivers/power/supply/cpcap-battery.c
@@ -33,8 +33,6 @@ 
 #include <linux/iio/types.h>
 #include <linux/mfd/motorola-cpcap.h>
 
-#include <asm/div64.h>
-
 /*
  * Register bit defines for CPCAP_REG_BPEOL. Some of these seem to
  * map to MC13783UG.pdf "Table 5-19. Register 13, Power Control 0"
@@ -219,28 +217,17 @@  static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
 				    s16 offset, u32 divider)
 {
 	s64 acc;
-	u64 tmp;
-	int avg_current;
 
 	if (!divider)
 		return 0;
 
 	acc = accumulator;
-	acc = acc - ((s64)sample * offset);
-
-	if (acc >=  0)
-		tmp = acc;
-	else
-		tmp = acc * -1;
+	acc -= (s64)sample * offset;
+	acc *= ddata->cc_lsb;
+	acc *= -1;
+	acc = div_s64(acc, divider);
 
-	tmp = tmp * ddata->cc_lsb;
-	do_div(tmp, divider);
-	avg_current = tmp;
-
-	if (acc >= 0)
-		return -avg_current;
-	else
-		return avg_current;
+	return acc;
 }
 
 /* 3600000μAms = 1μAh */