Skip to content

AC power state bounces after plugging/unplugging AC power #88

Open

Description

@baodrate

system: FW 13 Pro w/ Ultra X7 358H (Sakura)

When I plug/unplug AC power, the state of /sys/class/power_supply/ACAD/online bounces for ~150ms before it settles back to the correct state.

This is causing issues for me with tuned-ppd which seems to be reading the wrong AC state and never correcting it.

e.g:

  1. AC is unplugged
  2. 0ms: kernel event on /devices/../power_supply/ACAD.
  3. 5ms: /sys/class/power_supply/ACAD/online reads 0
  4. 30ms: /sys/class/power_supply/ACAD/online reads 1
  5. 100ms: tuned (on udev event) reads AC state as online, changes to AC power profile.
  6. 150ms: /sys/class/power_supply/ACAD/online reads 0

It seems like tuned does not poll but only reacts to udev events, so it never changes to the correct power profile.

Activity

  1. baodrate commented on Sep 22, 2026

    @baodrate
    Author

    I'm not confident, but is the issue something like this?

    • charger_task() reads AC state in a loop, and reads the AC-state

    while (1) {
    /* Let's see what's going on... */
    curr.ts = get_time();
    sleep_usec = 0;
    problems_exist = 0;
    battery_critical = 0;
    check_extpower(chgnum);

    • then the charger is connected/disconnected and triggers the extpower_deferred() to be deferred

    static void extpower_deferred(void)
    {
    int extpower_presence = gpio_get_level(GPIO_AC_PRESENT);
    if (extpower_presence == debounced_extpower_presence)
    return;
    debounced_extpower_presence = extpower_presence;
    extpower_handle_update(extpower_presence);
    }
    DECLARE_DEFERRED(extpower_deferred);
    void extpower_interrupt(enum gpio_signal signal)
    {
    /* 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

    1. not very familiar with how zephyr scheduling works. this might be off the mark ↩

  2. pqwy commented on Nov 5, 2026

    @pqwy

    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:

     2.843 online=0
     2.881 online=1
     2.981 online=0
    
     6.556 online=1
     6.612 online=0
     6.695 online=1
    
     9.248 online=0
     9.335 online=1
     9.413 online=0
    
    13.201 online=1
    13.239 online=0
    13.339 online=1
    
    16.313 online=0
    16.372 online=1
    16.461 online=0
    
    20.770 online=1
    20.802 online=0
    20.900 online=1
    
    24.594 online=0
    24.616 online=1
    24.724 online=0
    
    29.377 online=1
    29.398 online=0
    29.500 online=1
    
    33.054 online=0
    33.095 online=1
    33.201 online=0
    
    37.054 online=1
    37.081 online=0
    37.182 online=1
    

    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.

  3. mitar commented on Nov 18, 2026

    @mitar

    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++;

    I have not tested it on the laptop yet.

  4. mitar commented on Nov 22, 2026

    @mitar

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions