You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AC power state bounces after plugging/unplugging AC power #88
if (extpower_presence==debounced_extpower_presence)
return;
debounced_extpower_presence=extpower_presence;
extpower_handle_update(extpower_presence);
}
DECLARE_DEFERRED(extpower_deferred);
voidextpower_interrupt(enumgpio_signalsignal)
{
/* Trigger deferred notification of external power change */
hook_call_deferred(&extpower_deferred_data,
CONFIG_EXTPOWER_DEBOUNCE_MS*MSEC);
}
charger_task() continues to charger_get_params() which reads from i2c and blocks/yields(?)1
extpower_deferred() runs, calling extpower_handle_update() which updates the mmap and notifies the system
charger_task() continues, hitting process_charge_state(), which updates the battery flags mmap with the now-outdated AC-state that was read at the beginning of the iteration
on the next iteration the updated state is read and is reverted
Footnotes
not very familiar with how zephyr scheduling works. this might be off the mark ↩
To corroborate, I'm seeing the same. Same machine, FW 13 Pro w/ Ultra X7 358H. Firmware 03.02
If I read /sys/class/power_supply/ACAD/online every 5ms:
#!/usr/bin/python3
import time
t0 = time.time()
last = None
while True:
with open('/sys/class/power_supply/ACAD/online') as f:
v = f.read().strip()
if v != last:
print(f'%.03f online={v}' % (time.time() - t0))
last = v
time.sleep(0.005)
and yank the charging cable in/out, changes always come in threes:
Upower waits for udev notification, and then reads sysfs, racing with the flip-flop. It hits the window quite often, about 1/5 of times. Since it only reacts to events, and never polls, this causes it to get stuck in "on AC" and cascades into things like power-profiles-daemon not changing EPP, or Gnome never using its "on battery" settings. This can be resolved with another plug/unplug, but the problem is that it mostly goes unnoticed.
I was playing with correlating times from these events with what framework_tool --console recent produces, and I can provide that, but I presume upstream can easily recreate such traces. It's really not an edge case — but it still probably shouldn't be happening.
The same happens on a Laptop 13 Pro (AMD Ryzen AI 300 Series), EC lilac-4.0.2-fdbf549. Lilac and the fwk-sakura-20260429 branch both use CONFIG_PLATFORM_EC_EXTPOWER_GPIO and the same common/extpower_common.c, common/charge_state.c and common/battery_v2.c code for this.
Measurement: a script read /sys/class/power_supply/ACAD/online every 5 ms (one read lasts about 0.1 ms), followed the ACAD uevents with udevadm monitor --kernel --udev --property --subsystem-match=power_supply, and 3 s after the last change compared UPower's line power Online with sysfs. The EC console (framework_tool --console follow) ran in parallel. A 100 W USB-C charger was plugged and unplugged directly on one port, about 5 s apart.
Result: 5 of 13 transitions bounced, 4 of 7 plugs and 1 of 6 unplugs:
Transition
sysfs values (ms after the first change)
Kernel ACAD uevent
plug
1 at 0, 0 at 22, 1 at 135
online=1 at 12
plug
1 at 0, 0 at 43, 1 at 151
online=1 at 13
unplug
0 at 0, 1 at 44, 0 at 128
online=0 at 8
plug
1 at 0, 0 at 54, 1 at 143
online=1 at 10
plug
1 at 0, 0 at 43, 1 at 142
online=1 at 11
Every transition had a single kernel ACAD uevent, which carried the correct value, and a single AC on or AC off on the EC console, so the AC signal itself did not toggle. The stale value appeared 10-44 ms after the uevent and lasted 84-113 ms. UPower ended correct in all 13 transitions here, presumably because it read sysfs before the stale value appeared, so on this machine the race is narrow but real: a busier system which delays UPower's read by a few tens of ms would presumably leave it in the wrong state, and after an unplug that means "on AC" while on battery.
Mechanism: this matches the analysis in the first comment below the issue description. extpower_handle_update() sets EC_BATT_FLAG_AC_PRESENT in the memory map and sends the AC host event. The charger task read curr.ac at the start of its loop, before its I2C transfers to the charger and the battery, and update_dynamic_battery_info() then copies that stale value into the battery flags, which battery_memmap_refresh() writes over the flag. The next loop, which the HOOK_AC_CHANGE wakeup starts, writes the correct value again, and no host event announces either change. The EC console timing is consistent with this: aligned on the AC host event, the return to the correct value came 1-13 ms before the next charger loop's Battery ...% output in all five bounces. Plugs probably bounce more often because the new PD contract wakes the charger task (CL: ... lines) just before AC on, so a loop with the old value is often still running when the flag changes. On unplug, AC off comes about 700 ms after PORT_DISCONNECT, when that loop has finished.
Upstream ChromiumOS EC main (checked at d8b8cf83d8) still has the same path: charge_state.c passes curr.ac to battery_set_dynamic_info() in common/battery_info.c (the renamed battery_v2.c).
Fix: the flag should follow extpower_is_present(), read after the last battery transfer of update_dynamic_battery_info() (with CONFIG_CUSTOMIZED_DESIGN it reads battery_get_avg_current()). Deferred functions run in the system work queue, which has a lower priority than every EC task, so extpower_handle_update() cannot run between that read and battery_memmap_refresh(). On boards with an EC-EC battery client, curr.ac is deliberately 0 while the base powers the system (base_check_extpower()), so those keep curr.ac:
--- a/common/battery_v2.c+++ b/common/battery_v2.c@@ -321,8 +321,6 @@ void update_dynamic_battery_info(void)
curr = charge_get_status();
tmp = 0;
- if (curr->ac)- tmp |= EC_BATT_FLAG_AC_PRESENT;
if (curr->batt.is_present == BP_YES) {
tmp |= EC_BATT_FLAG_BATT_PRESENT;
@@ -500,6 +498,26 @@ void update_dynamic_battery_info(void)
if (battery_is_cut_off())
tmp |= EC_BATT_FLAG_CUT_OFF;
+ /*+ * The charger task reads curr->ac at the start of its loop, before its+ * transfers to the charger and the battery. When AC changes during+ * them, extpower_handle_update() sets the AC flag of the memory map+ * and sends the AC host event, and curr->ac would set the flag back+ * until the next loop, without an event. So the flag follows+ * extpower_is_present(), read after the last transfer of this+ * function: extpower_handle_update() runs in the system work queue,+ * which has a lower priority than the charger task, so it cannot run+ * between this read and battery_memmap_refresh() below. A battery+ * client keeps curr->ac, which is 0 while the base powers the system+ * although extpower_is_present() reports AC then too.+ */+ if (IS_ENABLED(CONFIG_EC_EC_COMM_BATTERY_CLIENT)) {+ if (curr->ac)+ tmp |= EC_BATT_FLAG_AC_PRESENT;+ } else if (extpower_is_present()) {+ tmp |= EC_BATT_FLAG_AC_PRESENT;+ }+
/* Tell the AP to re-read battery status if charge state changes */
if (bd->flags != tmp)
send_batt_status_event++;
Follow-up to my comment above: the change is now a commit on my branch, with a new charge_state_ac suite in zephyr/test/drivers/common_charger which fails without it. It sets the AC bit of the battery flags at the end of update_dynamic_battery_info() from extpower_is_present(), instead of the value the charger task read at the start of its loop.
On the same laptop with this firmware, measured the same way: 15 transitions (8 unplugs, 7 plugs), each a single change of ACAD/online, none bounced, where Framework's firmware bounced in 5 of 13.
Activity
baodrate commentedon Sep 22, 2026
I'm not confident, but is the issue something like this?
charger_task()reads AC state in a loop, and reads the AC-stateEmbeddedController/common/charge_state.c
Lines 1566 to 1573 in 8cbd986
extpower_deferred()to be deferredEmbeddedController/common/extpower_gpio.c
Lines 25 to 42 in 8cbd986
charger_task()continues tocharger_get_params()which reads from i2c and blocks/yields(?)1extpower_deferred()runs, callingextpower_handle_update()which updates the mmap and notifies the systemcharger_task()continues, hittingprocess_charge_state(), which updates the battery flags mmap with the now-outdated AC-state that was read at the beginning of the iterationFootnotes
not very familiar with how zephyr scheduling works. this might be off the mark ↩
pqwy commentedon Nov 5, 2026
To corroborate, I'm seeing the same. Same machine, FW 13 Pro w/ Ultra X7 358H. Firmware 03.02
If I read
/sys/class/power_supply/ACAD/onlineevery 5ms:and yank the charging cable in/out, changes always come in threes:
Upower waits for udev notification, and then reads sysfs, racing with the flip-flop. It hits the window quite often, about 1/5 of times. Since it only reacts to events, and never polls, this causes it to get stuck in "on AC" and cascades into things like power-profiles-daemon not changing EPP, or Gnome never using its "on battery" settings. This can be resolved with another plug/unplug, but the problem is that it mostly goes unnoticed.
I was playing with correlating times from these events with what
framework_tool --console recentproduces, and I can provide that, but I presume upstream can easily recreate such traces. It's really not an edge case — but it still probably shouldn't be happening.mitar commentedon Nov 18, 2026
The same happens on a Laptop 13 Pro (AMD Ryzen AI 300 Series), EC
lilac-4.0.2-fdbf549. Lilac and thefwk-sakura-20260429branch both useCONFIG_PLATFORM_EC_EXTPOWER_GPIOand the samecommon/extpower_common.c,common/charge_state.candcommon/battery_v2.ccode for this.Measurement: a script read
/sys/class/power_supply/ACAD/onlineevery 5 ms (one read lasts about 0.1 ms), followed the ACAD uevents withudevadm monitor --kernel --udev --property --subsystem-match=power_supply, and 3 s after the last change compared UPower's line powerOnlinewith sysfs. The EC console (framework_tool --console follow) ran in parallel. A 100 W USB-C charger was plugged and unplugged directly on one port, about 5 s apart.Result: 5 of 13 transitions bounced, 4 of 7 plugs and 1 of 6 unplugs:
Every transition had a single kernel ACAD uevent, which carried the correct value, and a single
AC onorAC offon the EC console, so the AC signal itself did not toggle. The stale value appeared 10-44 ms after the uevent and lasted 84-113 ms. UPower ended correct in all 13 transitions here, presumably because it read sysfs before the stale value appeared, so on this machine the race is narrow but real: a busier system which delays UPower's read by a few tens of ms would presumably leave it in the wrong state, and after an unplug that means "on AC" while on battery.Mechanism: this matches the analysis in the first comment below the issue description.
extpower_handle_update()setsEC_BATT_FLAG_AC_PRESENTin the memory map and sends the AC host event. The charger task readcurr.acat the start of its loop, before its I2C transfers to the charger and the battery, andupdate_dynamic_battery_info()then copies that stale value into the battery flags, whichbattery_memmap_refresh()writes over the flag. The next loop, which theHOOK_AC_CHANGEwakeup starts, writes the correct value again, and no host event announces either change. The EC console timing is consistent with this: aligned on the AC host event, the return to the correct value came 1-13 ms before the next charger loop'sBattery ...%output in all five bounces. Plugs probably bounce more often because the new PD contract wakes the charger task (CL: ...lines) just beforeAC on, so a loop with the old value is often still running when the flag changes. On unplug,AC offcomes about 700 ms afterPORT_DISCONNECT, when that loop has finished.Upstream ChromiumOS EC main (checked at
d8b8cf83d8) still has the same path:charge_state.cpassescurr.actobattery_set_dynamic_info()incommon/battery_info.c(the renamedbattery_v2.c).Fix: the flag should follow
extpower_is_present(), read after the last battery transfer ofupdate_dynamic_battery_info()(withCONFIG_CUSTOMIZED_DESIGNit readsbattery_get_avg_current()). Deferred functions run in the system work queue, which has a lower priority than every EC task, soextpower_handle_update()cannot run between that read andbattery_memmap_refresh(). On boards with an EC-EC battery client,curr.acis deliberately 0 while the base powers the system (base_check_extpower()), so those keepcurr.ac:I have not tested it on the laptop yet.
battery: report the current AC presence in the battery flags
battery: report the current AC presence in the battery flags
mitar commentedon Nov 22, 2026
Follow-up to my comment above: the change is now a commit on my branch, with a new
charge_state_acsuite inzephyr/test/drivers/common_chargerwhich fails without it. It sets the AC bit of the battery flags at the end ofupdate_dynamic_battery_info()fromextpower_is_present(), instead of the value the charger task read at the start of its loop.On the same laptop with this firmware, measured the same way: 15 transitions (8 unplugs, 7 plugs), each a single change of
ACAD/online, none bounced, where Framework's firmware bounced in 5 of 13.