diff --git a/patches/kernel/0012-gemini-lid-wake-and-fg-taper-fix.patch b/patches/kernel/0012-gemini-lid-wake-and-fg-taper-fix.patch new file mode 100644 index 0000000..6cb949f --- /dev/null +++ b/patches/kernel/0012-gemini-lid-wake-and-fg-taper-fix.patch @@ -0,0 +1,270 @@ +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); diff --git a/patches/kernel/series b/patches/kernel/series index a3b7e43..5bed576 100644 --- a/patches/kernel/series +++ b/patches/kernel/series @@ -9,3 +9,4 @@ 0009-mt6797-wlan-drop-free-stability.patch 0010-mt6797-ccci-md1-boot-hs-v2-and-rpc.patch 0011-gemini-lid-sync-filter-and-debounce.patch +0012-gemini-lid-wake-and-fg-taper-fix.patch diff --git a/prebuilt/geminux/usr/sbin/geminux-powerd b/prebuilt/geminux/usr/sbin/geminux-powerd index 15fe1ae..3f17098 100755 Binary files a/prebuilt/geminux/usr/sbin/geminux-powerd and b/prebuilt/geminux/usr/sbin/geminux-powerd differ diff --git a/prebuilt/kernel/boot/Image.gz b/prebuilt/kernel/boot/Image.gz index e7300a4..5dfa4c2 100644 Binary files a/prebuilt/kernel/boot/Image.gz and b/prebuilt/kernel/boot/Image.gz differ diff --git a/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/input/misc/gemini-lid.ko.zst b/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/input/misc/gemini-lid.ko.zst index 5aeafbe..6923ca7 100644 Binary files a/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/input/misc/gemini-lid.ko.zst and b/prebuilt/kernel/lib/modules/7.3.5-geminux/kernel/drivers/input/misc/gemini-lid.ko.zst differ diff --git a/scripts/test-power-policy.py b/scripts/test-power-policy.py index 8f9b8f9..ba4a13e 100755 --- a/scripts/test-power-policy.py +++ b/scripts/test-power-policy.py @@ -142,10 +142,14 @@ int main(void) { geminux_policy_init(&pol); assert(pol.profile == POWER_PROFILE_BALANCED); assert(pol.lid_suspend_enabled); + assert(pol.inhibit_ac_lid); assert(!geminux_should_suspend_on_lid(&pol)); - /* Closing lid on battery should trigger S3 suspend */ + /* Closing lid on battery should trigger S3 suspend, but AC inhibits lid suspend */ pol.lid_closed = true; + pol.on_ac_power = true; + assert(!geminux_should_suspend_on_lid(&pol)); + pol.on_ac_power = false; assert(geminux_should_suspend_on_lid(&pol)); /* Active voice call inhibitor blocks lid suspend */ @@ -678,6 +682,7 @@ def main(): ), os.path.join(BASE_DIR, "patches", "kernel", "series"): ( "0009-mt6797-wlan-drop-free-stability.patch", + "0012-gemini-lid-wake-and-fg-taper-fix.patch", ), os.path.join(overlay_dir, "etc", "profile.d", "10-geminux-env.sh"): ( "export NO_AT_BRIDGE=1", @@ -714,6 +719,8 @@ def main(): raise AssertionError("geminux-powerd.c must unload mt6797_consys and reload mt6797-consys") if 'if (ctx->airplane_mode)' not in powerd_src or '"airplane toggle"' not in powerd_src: raise AssertionError("geminux-powerd.c must guard CONSYS resume on airplane_mode and support airplane toggle") + if '"/sys/power/state", "freeze"' in trig_src: + raise AssertionError("trigger_suspend must not fall back to s2idle freeze") with open(os.path.join(APPS_DIR, "geminux-control.c"), "r", encoding="utf-8") as handle: ctrl_src = handle.read() diff --git a/src/geminux-daemons/geminux-power-policy.c b/src/geminux-daemons/geminux-power-policy.c index 5318b18..ddfb06d 100644 --- a/src/geminux-daemons/geminux-power-policy.c +++ b/src/geminux-daemons/geminux-power-policy.c @@ -8,6 +8,7 @@ void geminux_policy_init(geminux_power_policy_t *p) { memset(p, 0, sizeof(*p)); p->profile = POWER_PROFILE_BALANCED; p->lid_suspend_enabled = true; + p->inhibit_ac_lid = true; } bool geminux_policy_set_profile_name(geminux_power_policy_t *p, const char *name) { diff --git a/src/geminux-daemons/geminux-powerd.c b/src/geminux-daemons/geminux-powerd.c index 8768c53..19831de 100644 --- a/src/geminux-daemons/geminux-powerd.c +++ b/src/geminux-daemons/geminux-powerd.c @@ -46,6 +46,7 @@ typedef struct { int srv_fd; int tmr_fd; int ev_fds[MAX_EVDEVS]; + bool ev_is_pmic_pwr[MAX_EVDEVS]; int num_ev; int saved_brightness; uint64_t last_input_ms; @@ -66,6 +67,7 @@ typedef struct { pid_t radio_worker_pid; bool consys_suspended; bool suspend_ebusy_on_ac; + bool low_power_standby; bool airplane_mode; bool air_prev_wifi; bool air_prev_bt_loaded; @@ -241,6 +243,8 @@ static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) { static void lock_x11_session(void); static void trigger_suspend(powerd_ctx_t *ctx); +static void restore_cpus_after_resume(powerd_ctx_t *ctx); +static void exit_low_power_standby(powerd_ctx_t *ctx); static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force); static void apply_cpu_policy(powerd_ctx_t *ctx, bool force); @@ -350,6 +354,7 @@ static void evaluate_idle(powerd_ctx_t *ctx) { } static void note_user_activity(powerd_ctx_t *ctx) { + exit_low_power_standby(ctx); if (ctx->consys_suspended && !ctx->policy.lid_closed) { restore_consys_after_resume(ctx); } @@ -363,6 +368,7 @@ static void note_user_activity(powerd_ctx_t *ctx) { } static void apply_cpu_sysfs(powerd_ctx_t *ctx, bool force) { + if (ctx->low_power_standby) return; bool a72_ready = (access("/sys/module/mt6797_a72", F_OK) == 0); bool cpufreq_present = (access("/sys/devices/system/cpu/cpufreq/policy0", F_OK) == 0); bool interactive_boost = (ctx->policy.profile == POWER_PROFILE_BALANCED && @@ -1080,25 +1086,71 @@ static void restore_cpus_after_resume(powerd_ctx_t *ctx) { ctx->policy_applied = false; } +static bool can_enter_deep_mem_sleep(const powerd_ctx_t *ctx) { + if (ctx->policy.on_ac_power) return false; + char st[16] = {0}; + if (read_sysfs_line("/sys/class/mt6797_ccci/md1/stage", st, sizeof(st)) == 0 && atoi(st) > 0) { + return false; + } + return true; +} + +static void exit_low_power_standby(powerd_ctx_t *ctx) { + if (!ctx->low_power_standby) return; + ctx->low_power_standby = false; + restore_cpus_after_resume(ctx); +} + +static bool fd_is_pmic_pwr(const powerd_ctx_t *ctx, int fd) { + for (int i = 0; i < ctx->num_ev; i++) { + if (ctx->ev_fds[i] == fd) return ctx->ev_is_pmic_pwr[i]; + } + return false; +} + static void trigger_suspend(powerd_ctx_t *ctx) { arm_timer_ms(ctx->tmr_fd, 0); + refresh_ac_and_hdmi(ctx); lock_x11_session(); toggle_backlight(ctx, false); save_fuel_gauge_state(ctx); + if (ctx->policy.on_ac_power) { + ctx->suspend_ebusy_on_ac = true; + ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK; + apply_cpu_sysfs(ctx, false); + return; + } + exit_low_power_standby(ctx); prepare_consys_for_suspend(ctx); prepare_cpus_for_suspend(); - write_sysfs_str("/sys/power/mem_sleep", "deep"); - bool suspended = (write_sysfs_str("/sys/power/state", "mem") == 0 || - write_sysfs_str("/sys/power/state", "freeze") == 0); + bool can_deep = can_enter_deep_mem_sleep(ctx); + bool suspended = false; + if (can_deep) { + write_sysfs_str("/sys/power/mem_sleep", "deep"); + suspended = (write_sysfs_str("/sys/power/state", "mem") == 0); + } + if (!suspended) { + refresh_lid_state(ctx); + refresh_ac_and_hdmi(ctx); + if (!ctx->policy.on_ac_power && !ctx->policy.hdmi_connected && + ctx->policy.inhibit_count == 0 && + (ctx->policy.lid_closed ? geminux_should_suspend_on_lid(&ctx->policy) : true)) { + ctx->low_power_standby = true; + ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK; + arm_timer_ms(ctx->tmr_fd, 0); + return; + } + } + ctx->low_power_standby = false; restore_cpus_after_resume(ctx); - bool saw_wake_input = wait_post_resume_lid_events(ctx, 90); + bool saw_wake_input = suspended ? wait_post_resume_lid_events(ctx, 90) : !ctx->policy.lid_closed; refresh_ac_and_hdmi(ctx); ctx->suspend_ebusy_on_ac = (!suspended && ctx->policy.on_ac_power); if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy) || ctx->suspend_ebusy_on_ac) { restore_consys_after_resume(ctx); } - if ((suspended || saw_wake_input) && !ctx->policy.lid_closed) { + if (!ctx->policy.lid_closed && (suspended || saw_wake_input)) { ctx->suspend_ebusy_on_ac = false; ctx->idle_stage = IDLE_STAGE_ACTIVE; ctx->last_input_ms = monotonic_ms(); @@ -1108,7 +1160,7 @@ static void trigger_suspend(powerd_ctx_t *ctx) { } else { toggle_backlight(ctx, false); ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK; - if (!ctx->suspend_ebusy_on_ac && + if (suspended && !ctx->suspend_ebusy_on_ac && (ctx->policy.lid_closed ? geminux_should_suspend_on_lid(&ctx->policy) : (!ctx->policy.on_ac_power && !saw_wake_input))) { arm_timer_ms(ctx->tmr_fd, 2000); @@ -1125,6 +1177,7 @@ static void scan_input_devices(powerd_ctx_t *ctx) { close(ctx->ev_fds[i]); } ctx->num_ev = 0; + memset(ctx->ev_is_pmic_pwr, 0, sizeof(ctx->ev_is_pmic_pwr)); ctx->policy.lid_closed = false; for (int i = 0; i < 32 && ctx->num_ev < MAX_EVDEVS; i++) { @@ -1145,6 +1198,9 @@ static void scan_input_devices(powerd_ctx_t *ctx) { } struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd }; if (epoll_ctl(ctx->epfd, EPOLL_CTL_ADD, fd, &ev) == 0) { + char name[128] = {0}; + if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) name[0] = '\0'; + ctx->ev_is_pmic_pwr[ctx->num_ev] = (strstr(name, "pmic-keys") != NULL); ctx->ev_fds[ctx->num_ev++] = fd; continue; } @@ -1172,6 +1228,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) { } else if (strncmp(buf, "inhibit add ", 12) == 0) { geminux_policy_add_inhibit(&ctx->policy, buf + 12); if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy)) { + exit_low_power_standby(ctx); restore_consys_after_resume(ctx); } apply_cpu_policy(ctx, false); @@ -1185,6 +1242,7 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) { dprintf(cfd, "OK lid_suspend=on\n"); } else if (strcmp(buf, "lid-suspend off") == 0) { ctx->policy.lid_suspend_enabled = false; + exit_low_power_standby(ctx); restore_consys_after_resume(ctx); dprintf(cfd, "OK lid_suspend=off\n"); } else if (strcmp(buf, "wifi-restore") == 0) { @@ -1257,6 +1315,31 @@ static int setup_uevent_socket(void) { return fd; } +static void handle_lid_closed(powerd_ctx_t *ctx) { + lock_x11_session(); + evaluate_idle(ctx); + if (geminux_should_suspend_on_lid(&ctx->policy)) { + trigger_suspend(ctx); + } else { + exit_low_power_standby(ctx); + restore_consys_after_resume(ctx); + toggle_backlight(ctx, false); + ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK; + apply_cpu_sysfs(ctx, false); + } +} + +static void handle_lid_open(powerd_ctx_t *ctx) { + ctx->suspend_ebusy_on_ac = false; + exit_low_power_standby(ctx); + restore_consys_after_resume(ctx); + ctx->idle_stage = IDLE_STAGE_ACTIVE; + ctx->last_input_ms = monotonic_ms(); + toggle_backlight(ctx, true); + refresh_ac_and_hdmi(ctx); + apply_cpu_policy(ctx, true); +} + int main(void) { signal(SIGPIPE, SIG_IGN); powerd_ctx_t ctx; @@ -1309,6 +1392,7 @@ int main(void) { if (errno == EINTR) continue; break; } + bool ev_fds_rescan = false; for (int i = 0; i < n; i++) { int fd = events[i].data.fd; if (fd == ctx.sig_fd) { @@ -1337,6 +1421,7 @@ int main(void) { if (ctx.policy.lid_closed && geminux_should_suspend_on_lid(&ctx.policy)) { trigger_suspend(&ctx); } else { + exit_low_power_standby(&ctx); restore_consys_after_resume(&ctx); evaluate_idle(&ctx); } @@ -1357,7 +1442,16 @@ int main(void) { need_ps_or_drm = true; } } - if (need_ev_rescan) scan_input_devices(&ctx); + if (need_ev_rescan) { + bool prev_lid = ctx.policy.lid_closed; + scan_input_devices(&ctx); + ev_fds_rescan = true; + if (prev_lid && !ctx.policy.lid_closed) { + handle_lid_open(&ctx); + } else if (!prev_lid && ctx.policy.lid_closed) { + handle_lid_closed(&ctx); + } + } if (need_ps) { if (!ctx.fg_restored) { restore_fuel_gauge_state(&ctx); @@ -1370,6 +1464,7 @@ int main(void) { bool prev_hdmi = ctx.policy.hdmi_connected; refresh_ac_and_hdmi(&ctx); if (!ctx.policy.lid_closed || !geminux_should_suspend_on_lid(&ctx.policy)) { + exit_low_power_standby(&ctx); restore_consys_after_resume(&ctx); } if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) { @@ -1388,6 +1483,8 @@ int main(void) { } } } else { + if (ev_fds_rescan) continue; + bool is_pmic_pwr = fd_is_pmic_pwr(&ctx, fd); struct input_event iev; bool saw_activity = false; bool power_pressed = false; @@ -1395,29 +1492,21 @@ int main(void) { if (iev.type == EV_SW && iev.code == SW_LID) { ctx.policy.lid_closed = (iev.value != 0); if (ctx.policy.lid_closed) { - lock_x11_session(); - evaluate_idle(&ctx); - if (geminux_should_suspend_on_lid(&ctx.policy)) { - trigger_suspend(&ctx); - } else { - restore_consys_after_resume(&ctx); - toggle_backlight(&ctx, false); - } + handle_lid_closed(&ctx); } else { - ctx.suspend_ebusy_on_ac = false; - restore_consys_after_resume(&ctx); - ctx.idle_stage = IDLE_STAGE_ACTIVE; - ctx.last_input_ms = monotonic_ms(); - toggle_backlight(&ctx, true); - refresh_ac_and_hdmi(&ctx); - apply_cpu_policy(&ctx, true); - evaluate_idle(&ctx); + handle_lid_open(&ctx); } - } else if (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) { - if (ctx.idle_stage == IDLE_STAGE_LOCKED_BLANK) { - note_user_activity(&ctx); - } else { + } else if (iev.type == EV_KEY && iev.value == 1 && + (iev.code == KEY_POWER || (is_pmic_pwr && iev.code == KEY_ESC))) { + refresh_lid_state(&ctx); + if (ctx.idle_stage != IDLE_STAGE_ACTIVE || ctx.low_power_standby) { + if (!ctx.policy.lid_closed) { + note_user_activity(&ctx); + } + } else if (iev.code == KEY_POWER) { power_pressed = true; + } else { + saw_activity = true; } } else if (iev.type == EV_KEY || iev.type == EV_REL || iev.type == EV_ABS) { saw_activity = true;