Fix post-reflash charger-boot OCV calibration and CV taper in fuel gauge

This commit is contained in:
Luxferre committed 2026-11-26 10:20:09 +02:00
1 parent e2981455ed
commit f65dbb28a2
6 files changed
+261 -36

No files matched your search

@@ -1,16 +1,19 @@
From: Geminux <geminux@localhost>
Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge: prevent stuck lid lockout and USB attach CV taper jump
Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge / bq25890: prevent stuck lid lockout and USB/boot OCV jumps
1. gemini-lid: Track SW_LID switch state, reconcile lid_closed with raw Hall
GPIO66 on PM_POST_SUSPEND/PM_POST_HIBERNATION, and schedule a finite 50 ms
recheck whenever raw-GPIO filtering latches lid_closed while SW_LID is still
open or when SW_LID=1 conflicts with raw GPIO66, preventing permanent
BL_CORE_SUSPENDED blackout and input inhibit lockout.
2. mt6351-fuel-gauge: Track instantaneous reg_ua alongside integrated win_ua
and require 3 consecutive CV-taper windows on both before re-anchoring, cap
upward CV-taper steps to +2% of full per 5-minute interval, use conservative
2x R during unlearned charging first-OCV readings, and permit up to 180 mV
(20%) discharge sag at boot before discarding PMIC start-up ADC21.
2. mt6351-fuel-gauge & bq25890: Detect USB VBUS charger presence at boot
(MT6351_RGS_CHRDET / charging boot current) and briefly pause charging via
POWER_SUPPLY_PROP_CALIBRATE (F_CHG_CFG=0, up to 15-25 s while VSYS runs from
VBUS) so initial SoC anchors at true relaxed zero-current OCV rather than
ADC21 or the charger-clamped 4.336 V float after USB reflashes; require 3
consecutive CV-taper windows on both win_ua and reg_ua, cap untrusted CV
adjustments to +1%/5m and <=99% until rest after termination, and tolerate
180 mV (20%) boot discharge sag when validating start-up ADC21.
---
--- a/drivers/input/misc/gemini-lid.c
@@ -179,16 +182,30 @@ Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge: prevent stu
gpio_device_put(hall_gdev);
--- a/drivers/power/supply/mt6351-fuel-gauge.c
+++ b/drivers/power/supply/mt6351-fuel-gauge.c
@@ -236,6 +236,8 @@
@@ -38,6 +38,11 @@
#define MT6351_PWRON_CODE GENMASK(14, 0)
#define MT6351_PWRON_MIN_UV 3000000
#define MT6351_PWRON_MAX_UV 4450000
+#define MT6351_CHR_CON0 0x0f78
+#define MT6351_RGS_CHRDET BIT(5)
+#define MT6351_FG_BOOT_CAL_SETTLE_MS (15 * MSEC_PER_SEC)
+#define MT6351_FG_BOOT_CAL_MAX_MS (25 * MSEC_PER_SEC)
+#define MT6351_FG_BOOT_CAL_MIN_UV 3650000
/* The software read controls in CON0's upper byte; the lower byte is left. */
#define MT6351_FG_CTRL_MASK GENMASK(15, 8)
#define MT6351_FG_SW_READ_PRE BIT(9)
@@ -236,6 +241,10 @@
bool win_first_done;
int win_ua;
bool win_ua_valid;
+ int reg_ua;
+ unsigned int taper_windows;
+ ktime_t probe_time;
+ bool boot_cal;
/* The latest counter value, and since when the battery has rested. */
s64 car;
ktime_t rest_start;
@@ -367,6 +369,13 @@
@@ -367,6 +376,13 @@
dq_nah = mt6351_fg_car_nah(fg, car - fg->win_car);
fg->win_ua = div_s64(dq_nah * 3600, ms);
fg->win_ua_valid = true;
@@ -202,7 +219,7 @@ Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge: prevent stu
/*
* The typical load follows the discharge; off battery it returns to the
@@ -534,6 +543,7 @@
@@ -534,6 +550,7 @@
/* A 16-bit sign-magnitude-like code: above 32767 is discharge. */
count = cur > 32767 ? -(s64)(65535 - cur) : (s64)cur;
*current_ua = mt6351_fg_scale(fg, count, MT6351_FG_CURRENT_NA);
@@ -210,7 +227,7 @@ Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge: prevent stu
/*
* The whole 35-bit counter. The vendor keeps only bits 30:14, with
@@ -791,7 +801,11 @@
@@ -791,7 +808,11 @@
goto counted;
}
/* The open-circuit voltage: charging current raises the terminal. */
@@ -223,38 +240,148 @@ Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge: prevent stu
ret = power_supply_batinfo_ocv2cap(fg->info, uv, mc / 1000);
if (ret < 0)
return ret;
@@ -826,7 +840,9 @@
@@ -800,6 +821,17 @@
mt6351_fg_report(fg, div_s64(full * ret, 100), capacity, charge_uah);
return 0;
}
+ if (!fg->anchored && fg->boot_cal) {
+ s64 boot_ms = ktime_ms_delta(now, fg->probe_time);
+
+ if (boot_ms < MT6351_FG_BOOT_CAL_MAX_MS &&
+ (abs(ua) > MT6351_FG_IDLE_UA ||
+ boot_ms < MT6351_FG_BOOT_CAL_SETTLE_MS)) {
+ mt6351_fg_report(fg, div_s64(full * ret, 100),
+ capacity, charge_uah);
+ return 0;
+ }
+ }
dev_dbg(fg->dev, "%s OCV reading: %d uV at %d uA, %d mC: %d %%\n",
fg->anchored ? "rest" : "first", uv, ua, mc, ret);
if (settled && fg->taper_seen) {
@@ -815,7 +847,15 @@
}
fg->anchored = true;
fg->anchor_time = now;
- fg->anchor_trusted = settled;
+ if (fg->boot_cal) {
+ fg->boot_cal = false;
+ fg->anchor_trusted = settled || abs(ua) <= MT6351_FG_IDLE_UA;
+ *full_changed = true;
+ dev_info(fg->dev, "boot OCV %d uV at %d uA (%lld ms): %d %%\n",
+ uv, ua, ktime_ms_delta(now, fg->probe_time), ret);
+ } else {
+ fg->anchor_trusted = settled;
+ }
if (rest_ms >= MT6351_FG_LEARN_REST_MS && !fg->rest_learned) {
fg->rest_learned = true;
@@ -826,24 +866,38 @@
}
/* In the charger's constant-voltage taper, the voltage gives the capacity. */
- if (fg->anchored && ua > 0 && ua <= MT6351_FG_CV_MAX_UA &&
- (!fg->cv_valid || ktime_ms_delta(now, fg->cv_time) >= MT6351_FG_CV_MS) &&
+ if (fg->anchored && fg->taper_windows >= 3 &&
+ ua > MT6351_FG_IDLE_UA && ua <= MT6351_FG_CV_MAX_UA &&
+ fg->reg_ua > MT6351_FG_IDLE_UA && fg->reg_ua <= MT6351_FG_CV_MAX_UA &&
(!fg->cv_valid || ktime_ms_delta(now, fg->cv_time) >= MT6351_FG_CV_MS) &&
!mt6351_fg_voltage(fg, &uv) &&
uv >= fg->info->voltage_max_design_uv - MT6351_FG_CV_MARGIN_UV &&
@@ -834,10 +850,18 @@
uv = mt6351_fg_ocv(fg, uv, ua, mc);
ret = power_supply_batinfo_ocv2cap(fg->info, uv, mc / 1000);
if (ret >= 0) {
+ s64 counted_uah = fg->anchor_uah +
+ div_s64(mt6351_fg_car_nah(fg, fg->car - fg->anchor_car), 1000);
+ s64 target_uah, max_up;
+
ret = clamp(ret, 0, 100);
+ target_uah = div_s64(full * ret, 100);
+ max_up = counted_uah + div_s64(full * 2, 100);
+ if (target_uah > max_up)
+ target_uah = max_up;
dev_dbg(fg->dev, "taper OCV reading: %d uV at %d uA, %d mC: %d %%\n",
uv, ua, mc, ret);
- uv >= fg->info->voltage_max_design_uv - MT6351_FG_CV_MARGIN_UV &&
- !thermal_zone_get_temp(fg->tz, &mc)) {
- uv = mt6351_fg_ocv(fg, uv, ua, mc);
- ret = power_supply_batinfo_ocv2cap(fg->info, uv, mc / 1000);
- if (ret >= 0) {
- ret = clamp(ret, 0, 100);
- dev_dbg(fg->dev, "taper OCV reading: %d uV at %d uA, %d mC: %d %%\n",
- uv, ua, mc, ret);
- fg->anchor_uah = div_s64(full * ret, 100);
+ fg->anchor_uah = target_uah;
fg->anchor_car = fg->car;
fg->anchor_time = now;
fg->anchor_trusted = false;
@@ -1023,9 +1047,11 @@
- fg->anchor_car = fg->car;
- fg->anchor_time = now;
- fg->anchor_trusted = false;
- fg->cv_time = now;
- fg->cv_valid = true;
- fg->taper_seen = true;
+ uv >= fg->info->voltage_max_design_uv - MT6351_FG_CV_MARGIN_UV) {
+ fg->taper_seen = true;
+ if (!fg->anchor_trusted && ua <= 220000 &&
+ (!fg->cv_valid || ktime_ms_delta(now, fg->cv_time) >= MT6351_FG_CV_MS) &&
+ !thermal_zone_get_temp(fg->tz, &mc)) {
+ uv = mt6351_fg_ocv(fg, uv, ua, mc);
+ ret = power_supply_batinfo_ocv2cap(fg->info, uv, mc / 1000);
+ if (ret >= 0) {
+ s64 counted_uah = fg->anchor_uah +
+ div_s64(mt6351_fg_car_nah(fg, fg->car - fg->anchor_car), 1000);
+ s64 target_uah, max_up, max_cv;
+
+ ret = clamp(ret, 0, 100);
+ target_uah = div_s64(full * ret, 100);
+ max_up = counted_uah + div_s64(full, 100);
+ max_cv = div_s64(mt6351_fg_usable(full) * 99, 100);
+ if (target_uah > max_up)
+ target_uah = max_up;
+ if (target_uah > max_cv)
+ target_uah = max_cv;
+ dev_dbg(fg->dev, "taper OCV reading: %d uV at %d uA, %d mC: %d %%\n",
+ uv, ua, mc, ret);
+ fg->anchor_uah = target_uah;
+ fg->anchor_car = fg->car;
+ fg->anchor_time = now;
+ fg->cv_time = now;
+ fg->cv_valid = true;
+ }
}
}
@@ -917,6 +971,16 @@
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
val->intval = fg->info->voltage_min_design_uv;
return 0;
+ case POWER_SUPPLY_PROP_CALIBRATE:
+ scoped_guard(mutex, &fg->lock) {
+ if (fg->boot_cal &&
+ (fg->anchored ||
+ ktime_ms_delta(ktime_get_boottime(), fg->probe_time) >=
+ MT6351_FG_BOOT_CAL_MAX_MS))
+ fg->boot_cal = false;
+ val->intval = fg->boot_cal ? 1 : 0;
+ }
+ return 0;
case POWER_SUPPLY_PROP_SCOPE:
/* Asked of every supply when the core looks for one online. */
return -EINVAL;
@@ -990,6 +1054,7 @@
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+ POWER_SUPPLY_PROP_CALIBRATE,
};
/*
@@ -1001,8 +1066,23 @@
*/
static void mt6351_fg_pwron_anchor(struct mt6351_fg *fg, int init_ua)
{
- unsigned int val;
+ unsigned int val, chr = 0;
int uv, mc, pct, live_uv;
+ bool chrdet;
+
+ chrdet = !regmap_read(fg->regmap, MT6351_CHR_CON0, &chr) &&
+ (chr & MT6351_RGS_CHRDET);
+ if (chrdet || init_ua > MT6351_FG_IDLE_UA) {
+ if (!mt6351_fg_voltage(fg, &live_uv) &&
+ live_uv >= MT6351_FG_BOOT_CAL_MIN_UV) {
+ scoped_guard(mutex, &fg->lock)
+ fg->boot_cal = true;
+ dev_info(fg->dev,
+ "charger present at boot (%d uV); pausing charge for zero-current OCV anchor\n",
+ live_uv);
+ }
+ return;
+ }
if (regmap_read(fg->regmap, MT6351_AUXADC_ADC21, &val) ||
!(val & MT6351_PWRON_RDY) || thermal_zone_get_temp(fg->tz, &mc))
@@ -1023,9 +1103,11 @@
if (!mt6351_fg_voltage(fg, &live_uv)) {
int live_ocv = mt6351_fg_ocv(fg, live_uv, init_ua, mc);
int live_pct = power_supply_batinfo_ocv2cap(fg->info, live_ocv, mc / 1000);
@@ -268,3 +395,100 @@ Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge: prevent stu
dev_info(fg->dev,
"ignoring stale start-up OCV %d uV (%d %%) vs live %d uV (%d %%)\n",
uv, pct, live_ocv, live_pct);
@@ -1121,6 +1203,8 @@
fg->full_uah = info->charge_full_design_uah;
WRITE_ONCE(fg->info, info);
+ fg->probe_time = ktime_get_boottime();
+
/* Open the first current window now, so the current is known 5 s on. */
{
int init_ua = 0, init_uah = 0;
@@ -1177,10 +1261,15 @@
return -ENODATA;
scoped_guard(mutex, &fg->lock) {
+ bool changed = (fg->r25_uohm != r);
+
fg->r25_uohm = r;
- fg->empty_valid = false;
- if (!fg->anchor_trusted)
- fg->anchored = false;
+ if (changed) {
+ fg->empty_valid = false;
+ if (!fg->anchor_trusted &&
+ (!fg->win_ua_valid || fg->win_ua <= MT6351_FG_IDLE_UA))
+ fg->anchored = false;
+ }
}
power_supply_changed(fg->psy);
--- a/drivers/power/supply/bq25890_charger.c
+++ b/drivers/power/supply/bq25890_charger.c
@@ -193,6 +193,7 @@
bool guard_vreg_high;
bool guard_temp;
bool guard_full;
+ bool guard_cal;
unsigned int guard_unread;
struct mutex lock; /* protect state data */
@@ -1276,6 +1277,15 @@
return bq->guard_full;
}
+static bool bq25890_guard_calibrating(struct power_supply *batt)
+{
+ union power_supply_propval val;
+
+ if (!batt || power_supply_get_property(batt, POWER_SUPPLY_PROP_CALIBRATE, &val))
+ return false;
+ return val.intval != 0;
+}
+
static const enum power_supply_property bq25890_guard_ext_props[] = {
POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD,
POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD,
@@ -1519,6 +1529,7 @@
int end = READ_ONCE(bq->guard_end);
int start = READ_ONCE(bq->guard_start);
bool was_temp = bq->guard_temp, was_full = bq->guard_full;
+ bool was_cal = bq->guard_cal;
bool was_vreg_high = bq->guard_vreg_high;
bool was_raised = bq->guard_wall && bq->guard_board < 2;
int was_board = bq->guard_board, board_mc;
@@ -1529,7 +1540,8 @@
bq->guard_temp = bq25890_guard_temp(bq, batt, &temp);
bq->guard_full = bq25890_guard_full(bq, batt, start, end, &capacity);
- stop = bq->guard_temp || bq->guard_full;
+ bq->guard_cal = bq25890_guard_calibrating(batt);
+ stop = bq->guard_temp || bq->guard_full || bq->guard_cal;
batt = bq25890_guard_offer(bq, batt);
if (batt)
power_supply_put(batt);
@@ -1568,10 +1580,12 @@
if (ret)
dev_crit_ratelimited(bq->dev, "guard cannot reach the charger: %d\n", ret);
- else if (bq->guard_temp != was_temp || bq->guard_full != was_full || chg == stop)
- dev_info(bq->dev, "guard: charging %s: temp=%d capacity=%d limit=%d%s%s\n",
+ else if (bq->guard_temp != was_temp || bq->guard_full != was_full ||
+ bq->guard_cal != was_cal || chg == stop)
+ dev_info(bq->dev, "guard: charging %s: temp=%d capacity=%d limit=%d%s%s%s\n",
stop ? "stopped" : "allowed", temp, capacity, end,
- bq->guard_temp ? " temperature" : "", bq->guard_full ? " full" : "");
+ bq->guard_temp ? " temperature" : "", bq->guard_full ? " full" : "",
+ bq->guard_cal ? " calibrating" : "");
raised = bq->guard_wall && bq->guard_board < 2;
if (!ret && raised && (!was_raised || bq->guard_board != was_board)) {
bq25890_guard_raised(bq->guard_board, &iinlim_ua, &ichg_ua);
@@ -1587,7 +1601,7 @@
TBL_VREG) / 1000, temp);
queue_delayed_work(system_power_efficient_wq, &bq->guard_work,
- BQ25890_GUARD_PERIOD);
+ bq->guard_cal ? msecs_to_jiffies(2000) : BQ25890_GUARD_PERIOD);
}
/* Before the power supply goes: the work and its sysfs attribute both use it. */