[PATCH V3 3/7] power: supply: rk817-charger: simplify re-initializing coulomb counter
Chris Morgan
macroalpha82 at gmail.com
Mon Nov 9 06:58:29 PST 2026
From: Chris Morgan <macromorgan at hotmail.com>
There are multiple places we re-initialize the coulomb counter. Create
a function to do this to make the code easier to read.
Signed-off-by: Chris Morgan <macromorgan at hotmail.com>
Acked-by: Lee Jones <lee at kernel.org>
---
drivers/power/supply/rk817_charger.c | 44 +++++++++++++++-------------
1 file changed, 23 insertions(+), 21 deletions(-)
diff --git a/drivers/power/supply/rk817_charger.c b/drivers/power/supply/rk817_charger.c
index 39c945622c4d..8843871948b9 100644
--- a/drivers/power/supply/rk817_charger.c
+++ b/drivers/power/supply/rk817_charger.c
@@ -167,6 +167,20 @@ static int rk817_chg_cur_from_reg(u8 reg)
}
}
+static int rk817_initialize_coulumb_counter(struct rk817_charger *charger,
+ u32 charge_ua)
+{
+ int tmp = CHARGE_TO_ADC(charge_ua, charger->res_div);
+ u8 bulk_reg[4];
+ int ret;
+
+ put_unaligned_be32(tmp, bulk_reg);
+ ret = regmap_bulk_write(charger->rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
+ bulk_reg, 4);
+
+ return ret;
+}
+
static void rk817_bat_calib_vol(struct rk817_charger *charger)
{
uint32_t vcalib0 = 0;
@@ -242,6 +256,7 @@ static int rk817_bat_calib_cap(struct rk817_charger *charger)
struct rk808 *rk808 = charger->rk808;
int charge_now, charge_now_adc;
u8 bulk_reg[4];
+ int ret;
/* Don't do anything if there's no battery. */
if (!charger->battery_present)
@@ -281,11 +296,9 @@ static int rk817_bat_calib_cap(struct rk817_charger *charger)
*/
charger->soc = 100000;
- charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
- charger->res_div);
- put_unaligned_be32(charge_now_adc, bulk_reg);
- regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
- bulk_reg, 4);
+ ret = rk817_initialize_coulumb_counter(charger, charger->fcc_mah);
+ if (ret)
+ return ret;
charger->soc_cal = 1;
dev_dbg(charger->dev,
@@ -313,17 +326,9 @@ static int rk817_bat_calib_cap(struct rk817_charger *charger)
dev_dbg(charger->dev,
"Recalibrating coulomb counter to %d uah\n",
charge_now);
- /*
- * Order of operations matters here to ensure we keep
- * enough precision until the last step to keep from
- * making needless updates to coulomb counter.
- */
- charge_now_adc = CHARGE_TO_ADC(charger->fcc_mah,
- charger->res_div);
- put_unaligned_be32(charge_now_adc, bulk_reg);
- regmap_bulk_write(rk808->regmap,
- RK817_GAS_GAUGE_Q_INIT_H3,
- bulk_reg, 4);
+ ret = rk817_initialize_coulumb_counter(charger, charger->fcc_mah);
+ if (ret)
+ return ret;
}
}
@@ -846,11 +851,8 @@ rk817_read_or_set_full_charge_on_boot(struct rk817_charger *charger,
boot_charge_mah = charger->soc * charger->fcc_mah / 100 / 1000;
if (boot_charge_mah > charger->fcc_mah)
boot_charge_mah = charger->fcc_mah;
- tmp = CHARGE_TO_ADC(boot_charge_mah, charger->res_div);
- put_unaligned_be32(tmp, bulk_reg);
- ret = regmap_bulk_write(rk808->regmap, RK817_GAS_GAUGE_Q_INIT_H3,
- bulk_reg, 4);
- if (ret < 0)
+ ret = rk817_initialize_coulumb_counter(charger, boot_charge_mah);
+ if (ret)
return ret;
/* Set QMAX value to max design capacity. */
--
2.53.0
More information about the Linux-rockchip
mailing list