From: Geminux Subject: [PATCH 12/12] Input: gemini-lid / power: mt6351-fuel-gauge: prevent stuck lid lockout and USB attach CV taper jump 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. --- --- a/drivers/input/misc/gemini-lid.c +++ b/drivers/input/misc/gemini-lid.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -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 @@ -236,6 +236,8 @@ bool win_first_done; int win_ua; bool win_ua_valid; + int reg_ua; + unsigned int taper_windows; /* The latest counter value, and since when the battery has rested. */ s64 car; ktime_t rest_start; @@ -367,6 +369,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 +543,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 +801,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; @@ -826,7 +840,9 @@ } /* In the charger's constant-voltage taper, the voltage gives the capacity. */ - if (fg->anchored && ua > 0 && ua <= MT6351_FG_CV_MAX_UA && + 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); - 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 @@ 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);