From f1272295ba971027353eded2c0f9e7f91429c8d4 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 26 Sep 2026 02:50:02 +1000 Subject: [PATCH 1/3] power: supply: macsmc: Only register optional properties whose keys exist VOLTAGE_MIN_DESIGN, CHARGE_TERM_CURRENT, CONSTANT_CHARGE_CURRENT_MAX and CONSTANT_CHARGE_VOLTAGE are read from the BITV, B0RC, B0RI and B0RV keys, and the driver registers them on every machine. When such a read fails with an SMC error (-EIO), the power supply core logs "driver failed to report" (rate-limited) for each read of the property, and power_supply_uevent() gives up at the first failing property, so no change uevent is sent for the battery at all. Register these four properties only if their key exists at probe time. This touches every machine the driver supports; firmware that has the keys keeps the same properties. On J700, reads of all four keys fail and the VOLTAGE_MIN_DESIGN errors flood the kernel log. It has not been confirmed there that the keys are missing from the key index, which is what apple_smc_key_exists() checks, rather than present but unreadable. macsmc_power_critical_work() forces an emergency shutdown when B0AV drops below BITV. Without a readable BITV that check never fires, as before this change, and only the BCF0 empty flag starts the orderly shutdown. This applies to J700, where no key that could replace BITV is known. Compile-tested only; not run on hardware. Assisted-by: LLM checkpatch sparse Signed-off-by: Ryan Murray --- drivers/power/supply/macsmc-power.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/power/supply/macsmc-power.c b/drivers/power/supply/macsmc-power.c index e1135e089ac90..7496a901165c2 100644 --- a/drivers/power/supply/macsmc-power.c +++ b/drivers/power/supply/macsmc-power.c @@ -888,13 +888,17 @@ static int macsmc_power_probe(struct platform_device *pdev) /* Extended properties usually present */ props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW; props[nprops++] = POWER_SUPPLY_PROP_TIME_TO_FULL_NOW; - props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN; + if (apple_smc_key_exists(smc, SMC_KEY(BITV))) + props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN; props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN; props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MIN; props[nprops++] = POWER_SUPPLY_PROP_VOLTAGE_MAX; - props[nprops++] = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT; - props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX; - props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE; + if (apple_smc_key_exists(smc, SMC_KEY(B0RC))) + props[nprops++] = POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT; + if (apple_smc_key_exists(smc, SMC_KEY(B0RI))) + props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX; + if (apple_smc_key_exists(smc, SMC_KEY(B0RV))) + props[nprops++] = POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE; props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN; props[nprops++] = POWER_SUPPLY_PROP_CHARGE_FULL; props[nprops++] = POWER_SUPPLY_PROP_CHARGE_NOW; From 3d5b6367b7f804fd16c4ee1120438b2a4463624a Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 26 Sep 2026 02:50:45 +1000 Subject: [PATCH 2/3] power: supply: macsmc: Read B0RM in native byte order on J700 Commit 57694663d658 ("power: supply: macsmc: Support macOS 27 SMC firmware") infers two firmware changes from one fact: when the BCF0 key shrinks from four bytes to one, B0RM is taken to switch from the gas gauge's big-endian pass-through to native byte order. J700's SMC firmware reports a four-byte BCF0 but a native-endian B0RM. Swapping it made CHARGE_NOW and ENERGY_NOW move in 256 mAh steps as the low byte changed, and gave values above CHARGE_FULL or below zero. Track the B0RM byte order in its own flag, b0rm_native, set at probe with the one-byte BCF0 layout as before and additionally on J700. fw_ge_27 now means only that BCF0 is one byte. The J700 case is keyed on the machine compatible because nothing in the key information the driver reads distinguishes it: BCF0 has the older size, and the B0RM key type has not been compared with other firmware. The helper now returns u16 instead of s16 on every machine. B0RM is an unsigned mAh count, so a remaining charge below 32768 mAh reads as before; a larger value, such as a wrongly swapped one, no longer turns negative. Compile-tested only; not run on hardware. Assisted-by: LLM checkpatch sparse Co-authored-by: Claude Opus 5.5 Signed-off-by: Ryan Murray --- drivers/power/supply/macsmc-power.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/drivers/power/supply/macsmc-power.c b/drivers/power/supply/macsmc-power.c index 7496a901165c2..5e82f2668c8b5 100644 --- a/drivers/power/supply/macsmc-power.c +++ b/drivers/power/supply/macsmc-power.c @@ -86,11 +86,14 @@ struct macsmc_power { bool has_ch0i; /* Force discharge (Older firmware) */ bool has_ch0c; /* Inhibit charge (Older firmware) */ bool has_chte; /* Inhibit charge (Modern firmware) */ + /* Battery critical key (BCF0) is 1 byte (Modern firmware) */ + bool fw_ge_27; /* - * Battery critical key is 1 byte and charge key is little endian - * (Modern firmware) + * Remaining charge key (B0RM) is in native byte order rather than the + * gas gauge's big-endian pass-through. This comes with the 1-byte BCF0 + * firmware, but J700 firmware has a native B0RM and a 4-byte BCF0. */ - bool fw_ge_27; + bool b0rm_native; u8 num_cells; int nominal_voltage_mv; @@ -421,10 +424,9 @@ static int macsmc_battery_get_capacity_level(struct macsmc_power *power) return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; } -static s16 macsmc_swap_b0rm(struct macsmc_power *power, s16 b0rm) +static u16 macsmc_b0rm_to_mah(struct macsmc_power *power, u16 b0rm) { - /* B0RM was Big Endian, likely pass through from TI gas gauge */ - return power->fw_ge_27 ? b0rm : (s16)swab16(b0rm); + return power->b0rm_native ? b0rm : swab16(b0rm); } static int macsmc_battery_get_property(struct power_supply *psy, @@ -521,7 +523,7 @@ static int macsmc_battery_get_property(struct power_supply *psy, break; case POWER_SUPPLY_PROP_CHARGE_NOW: ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RM), &vu16); - val->intval = macsmc_swap_b0rm(power, vu16) * 1000; + val->intval = macsmc_b0rm_to_mah(power, vu16) * 1000; break; case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: ret = apple_smc_read_u16(power->smc, SMC_KEY(B0DC), &vu16); @@ -533,7 +535,7 @@ static int macsmc_battery_get_property(struct power_supply *psy, break; case POWER_SUPPLY_PROP_ENERGY_NOW: ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RM), &vu16); - val->intval = macsmc_swap_b0rm(power, vu16) * power->nominal_voltage_mv; + val->intval = macsmc_b0rm_to_mah(power, vu16) * power->nominal_voltage_mv; break; case POWER_SUPPLY_PROP_TEMP: ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AT), &vu16); @@ -929,6 +931,9 @@ static int macsmc_power_probe(struct platform_device *pdev) return -EIO; } + power->b0rm_native = power->fw_ge_27 || + of_machine_is_compatible("apple,j700"); + /* Reset "Optimised Battery Charging" flags to default state */ if (power->has_chte) apple_smc_write_u32(smc, SMC_KEY(CHTE), 0); From e9f549349ad5b54cf3b9f7c891c80d951cb1e5cc Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 26 Sep 2026 02:51:48 +1000 Subject: [PATCH 3/3] power: supply: macsmc: Report net discharge on J700 as discharging A J700 on a low-power USB-C source can have a charge-capable adapter online, with CHCE and CHCC set, while the system draws more than the adapter supplies. The battery current B0AC is then negative and the remaining charge falls. CHCE and CHCC describe the adapter, not the battery, so where the other SMC flags then lead the driver to report CHARGING, the status contradicts the battery. On J700, report DISCHARGING instead of CHARGING when B0AC is negative. Only the three CHARGING results change: CHNC clear, CHNC holding only the BMS-busy bit below the charge limit, and the CHSC fallback when CHNC cannot be read. FULL, NOT_CHARGING for a charge limit or an inhibit, and the existing DISCHARGING cases are decided first and stay as they are, so a battery held at its charge limit keeps reporting NOT_CHARGING. Whether CHNC reports an inhibit in the low-power state has not been recorded; if it does, the status there stays NOT_CHARGING. The quirk is decided once at probe, and other machines keep the current status logic. B0AC is compared against zero. No noise floor around 0 mA has been measured, so a net current that hovers around zero can make the status alternate between CHARGING and DISCHARGING. Compile-tested only; not run on hardware. Assisted-by: LLM checkpatch sparse Co-authored-by: Claude Opus 5.5 Signed-off-by: Ryan Murray --- drivers/power/supply/macsmc-power.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/power/supply/macsmc-power.c b/drivers/power/supply/macsmc-power.c index 5e82f2668c8b5..3cac04b129ddd 100644 --- a/drivers/power/supply/macsmc-power.c +++ b/drivers/power/supply/macsmc-power.c @@ -94,6 +94,8 @@ struct macsmc_power { * firmware, but J700 firmware has a native B0RM and a 4-byte BCF0. */ bool b0rm_native; + /* Charging with a negative battery current is discharging (J700) */ + bool status_from_current; u8 num_cells; int nominal_voltage_mv; @@ -203,6 +205,23 @@ static void macsmc_do_dbg(struct macsmc_power *power) #undef FD3 } +/* + * The status to report where the SMC flags say the battery is charging. On + * J700 a charge-capable adapter can be online while the system draws more + * than it supplies, and the battery then drains. + */ +static int macsmc_battery_charging_status(struct macsmc_power *power) +{ + s16 current_ma; + + if (power->status_from_current && + !apple_smc_read_s16(power->smc, SMC_KEY(B0AC), ¤t_ma) && + current_ma < 0) + return POWER_SUPPLY_STATUS_DISCHARGING; + + return POWER_SUPPLY_STATUS_CHARGING; +} + static int macsmc_battery_get_status(struct macsmc_power *power) { u64 nocharge_flags; @@ -280,11 +299,11 @@ static int macsmc_battery_get_status(struct macsmc_power *power) return POWER_SUPPLY_STATUS_FULL; /* BMS busy shows up as inhibit, but we treat it as charging */ else if (nocharge_flags == CHNC_BMS_BUSY && !limited) - return POWER_SUPPLY_STATUS_CHARGING; + return macsmc_battery_charging_status(power); else if (nocharge_flags) return POWER_SUPPLY_STATUS_NOT_CHARGING; else - return POWER_SUPPLY_STATUS_CHARGING; + return macsmc_battery_charging_status(power); } /* Fallback: System charging flag */ @@ -294,7 +313,7 @@ static int macsmc_battery_get_status(struct macsmc_power *power) if (!flag) return POWER_SUPPLY_STATUS_NOT_CHARGING; - return POWER_SUPPLY_STATUS_CHARGING; + return macsmc_battery_charging_status(power); } static int macsmc_battery_get_charge_behaviour(struct macsmc_power *power) @@ -933,6 +952,7 @@ static int macsmc_power_probe(struct platform_device *pdev) power->b0rm_native = power->fw_ge_27 || of_machine_is_compatible("apple,j700"); + power->status_from_current = of_machine_is_compatible("apple,j700"); /* Reset "Optimised Battery Charging" flags to default state */ if (power->has_chte)