Files
geminux/patches/kernel/0012-gemini-lid-wake-and-fg-taper-fix.patch

495 lines
16 KiB
Diff

From: Geminux <geminux@localhost>
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 & 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
+++ b/drivers/input/misc/gemini-lid.c
@@ -18,6 +18,7 @@
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/slab.h>
+#include <linux/suspend.h>
#include <linux/vt_kern.h>
#include <linux/workqueue.h>
@@ -33,6 +34,7 @@
static DEFINE_MUTEX(lid_lock);
static struct input_dev *lid_keyboard;
static struct input_dev *lid_touchscreen;
+static struct input_dev *lid_switch;
static struct gpio_device *hall_gdev;
static struct gpio_desc *hall_gpiod;
static bool lid_exiting;
@@ -121,6 +123,60 @@
return hall_gpiod && gpiod_get_raw_value(hall_gpiod) == 0;
}
+static void gemini_lid_recheck_fn(struct work_struct *work);
+static DECLARE_DELAYED_WORK(gemini_lid_recheck_work, gemini_lid_recheck_fn);
+
+static void gemini_lid_recheck_fn(struct work_struct *work)
+{
+ bool raw_closed;
+ bool sw_open;
+
+ if (!hall_gpiod || !READ_ONCE(lid_armed))
+ return;
+
+ raw_closed = gemini_lid_raw_closed();
+ scoped_guard(mutex, &lid_lock) {
+ if (lid_exiting)
+ return;
+ if (READ_ONCE(lid_closed) != raw_closed) {
+ WRITE_ONCE(lid_closed, raw_closed);
+ queue_work(system_freezable_wq, &gemini_lid_work);
+ }
+ sw_open = lid_switch && !test_bit(SW_LID, lid_switch->sw);
+ }
+ if (raw_closed && sw_open)
+ mod_delayed_work(system_freezable_wq, &gemini_lid_recheck_work,
+ msecs_to_jiffies(50));
+}
+
+static int gemini_lid_pm_notify(struct notifier_block *nb, unsigned long action,
+ void *data)
+{
+ bool raw_closed;
+
+ if (action != PM_POST_SUSPEND && action != PM_POST_HIBERNATION)
+ return NOTIFY_DONE;
+ if (!hall_gpiod || !READ_ONCE(lid_armed))
+ return NOTIFY_DONE;
+
+ raw_closed = gemini_lid_raw_closed();
+ scoped_guard(mutex, &lid_lock) {
+ if (lid_exiting)
+ return NOTIFY_DONE;
+ if (READ_ONCE(lid_closed) != raw_closed) {
+ WRITE_ONCE(lid_closed, raw_closed);
+ queue_work(system_freezable_wq, &gemini_lid_work);
+ }
+ }
+ mod_delayed_work(system_freezable_wq, &gemini_lid_recheck_work,
+ msecs_to_jiffies(50));
+ return NOTIFY_OK;
+}
+
+static struct notifier_block gemini_lid_pm_nb = {
+ .notifier_call = gemini_lid_pm_notify,
+};
+
static bool gemini_lid_filter(struct input_handle *handle, unsigned int type,
unsigned int code, int value)
{
@@ -132,6 +188,8 @@
return false;
WRITE_ONCE(lid_closed, true);
queue_work(system_freezable_wq, &gemini_lid_work);
+ mod_delayed_work(system_freezable_wq, &gemini_lid_recheck_work,
+ msecs_to_jiffies(50));
}
/* Allow key releases and EV_SYN through so no held key sticks. */
if (type == EV_KEY)
@@ -146,9 +204,16 @@
if (!value)
WRITE_ONCE(lid_armed, true);
- if (READ_ONCE(lid_armed) && READ_ONCE(lid_closed) != !!value) {
- WRITE_ONCE(lid_closed, !!value);
- queue_work(system_freezable_wq, &gemini_lid_work);
+ if (READ_ONCE(lid_armed)) {
+ bool closed = !!value;
+
+ if (READ_ONCE(lid_closed) != closed) {
+ WRITE_ONCE(lid_closed, closed);
+ queue_work(system_freezable_wq, &gemini_lid_work);
+ }
+ if (closed && hall_gpiod && !gemini_lid_raw_closed())
+ mod_delayed_work(system_freezable_wq, &gemini_lid_recheck_work,
+ msecs_to_jiffies(50));
}
return false;
@@ -200,6 +265,9 @@
return 0;
}
+ scoped_guard(mutex, &lid_lock)
+ lid_switch = dev;
+
if (!test_bit(SW_LID, dev->sw))
WRITE_ONCE(lid_armed, true);
@@ -221,6 +289,11 @@
else if (handle->private == GEMINI_LID_TOUCHSCREEN)
lid_touchscreen = NULL;
}
+ } else {
+ scoped_guard(mutex, &lid_lock) {
+ if (lid_switch == handle->dev)
+ lid_switch = NULL;
+ }
}
input_close_device(handle);
input_unregister_handle(handle);
@@ -273,11 +346,19 @@
pr_warn("gemini-lid: Hall pinctrl GPIO unavailable; using SW_LID only\n");
}
+ err = register_pm_notifier(&gemini_lid_pm_nb);
+ if (err)
+ goto err_put_gdev;
+
err = input_register_handler(&gemini_lid_handler);
- if (err && hall_gdev) {
- gpio_device_put(hall_gdev);
- hall_gdev = NULL;
- hall_gpiod = NULL;
+ if (err) {
+ unregister_pm_notifier(&gemini_lid_pm_nb);
+err_put_gdev:
+ if (hall_gdev) {
+ gpio_device_put(hall_gdev);
+ hall_gdev = NULL;
+ hall_gpiod = NULL;
+ }
}
return err;
}
@@ -291,7 +372,9 @@
if (lid_applied_closed)
gemini_lid_apply(false);
}
+ unregister_pm_notifier(&gemini_lid_pm_nb);
input_unregister_handler(&gemini_lid_handler);
+ cancel_delayed_work_sync(&gemini_lid_recheck_work);
cancel_work_sync(&gemini_lid_work);
if (hall_gdev) {
gpio_device_put(hall_gdev);
--- a/drivers/power/supply/mt6351-fuel-gauge.c
+++ b/drivers/power/supply/mt6351-fuel-gauge.c
@@ -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 +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;
+ if (fg->win_ua > MT6351_FG_IDLE_UA && fg->win_ua <= MT6351_FG_CV_MAX_UA &&
+ reg_ua > MT6351_FG_IDLE_UA && reg_ua <= MT6351_FG_CV_MAX_UA) {
+ if (fg->taper_windows < 16)
+ fg->taper_windows++;
+ } else {
+ fg->taper_windows = 0;
+ }
/*
* The typical load follows the discharge; off battery it returns to the
@@ -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);
+ fg->reg_ua = *current_ua;
/*
* The whole 35-bit counter. The vendor keeps only bits 30:14, with
@@ -791,7 +808,11 @@
goto counted;
}
/* The open-circuit voltage: charging current raises the terminal. */
- uv = mt6351_fg_ocv(fg, uv, ua, mc);
+ if (!fg->anchored && ua > MT6351_FG_IDLE_UA &&
+ !fg->r_updated && fg->r25_uohm <= fg->r_design_uohm)
+ uv -= div_s64((s64)ua * mt6351_fg_r_uohm(fg, mc) * 2, 1000000);
+ else
+ uv = mt6351_fg_ocv(fg, uv, ua, mc);
ret = power_supply_batinfo_ocv2cap(fg->info, uv, mc / 1000);
if (ret < 0)
return ret;
@@ -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 &&
!mt6351_fg_voltage(fg, &uv) &&
- 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_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);
+ int max_sag_uv = init_ua <= 0 ? 180000 : 80000;
+ int max_sag_pct = init_ua <= 0 ? 20 : 10;
- if (abs(uv - live_ocv) > 80000 ||
- (live_pct >= 0 && abs(pct - live_pct) > 10)) {
+ if (live_ocv - uv > 80000 || uv - live_ocv > max_sag_uv ||
+ (live_pct >= 0 && (live_pct - pct > 10 || pct - live_pct > max_sag_pct))) {
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. */