Skip to contentSkip to conversation

hwmon: (applesmc) Expose the SMC battery charge limit

PATCH In review Michal Szpakowski 2 participants 3 messages Sep 23 – Sep 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (applesmc) Expose the SMC battery charge limit

Intel Macs keep a battery charge limit inside the SMC, in the key BCLM (one byte, percent). The SMC enforces it on its own: charging stops at the limit and the value survives reboots and operating systems, so the only thing an OS has to do is write it. On macOS the tool "bclm" does that; on Linux there has been no way, because applesmc exposes keys read-only through key_at_index.

Expose it as the standard charge_control_end_threshold property on the battery through a power supply extension, so UPower and the desktops that read that property pick it up without knowing anything about Apple. The battery of an Intel Mac is an ACPI Smart Battery handled by the sbs driver, which the ACPI battery hooks do not cover, so the supply is found by walking the registered supplies; should it register after applesmc, its first property-change notification attaches the extension. Nothing happens on machines whose SMC lacks the key. Writes are read back, because the SMC silently drops values it does not accept; 100 disables the cap.

Tested on a MacBookPro13,1 (2016, 13", no Touch Bar): with the limit at 80 and the charger attached, charging ran at a steady 1.55 A and stopped at 79% of charge_full with status "Full" and current 0, and the limit read back 80 after a reboot.

---
 drivers/hwmon/Kconfig    |  4 +++-
 drivers/hwmon/applesmc.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 137 insertions(+), 1 deletion(-)
drivers/hwmon/Kconfig +3 -1
@@ -368,13 +368,15 @@
 config SENSORS_APPLESMC
 	tristate "Apple SMC (Motion sensor, light sensor, keyboard backlight)"
 	depends on INPUT && X86
+	depends on POWER_SUPPLY
 	select NEW_LEDS
 	select LEDS_CLASS
 	help
 	  This driver provides support for the Apple System Management
 	  Controller, which provides an accelerometer (Apple Sudden Motion
 	  Sensor), light sensors, temperature sensors, keyboard backlight
-	  control and fan control.
+	  control, fan control and, on machines whose SMC has the BCLM key,
+	  a battery charge limit exposed as charge_control_end_threshold.
 
 	  Only Intel-based Apple's computers are supported (MacBook Pro,
 	  MacBook, MacMini).
drivers/hwmon/applesmc.c +159 -0

201 lines not on this page · raw message

#1 lore raw

[+cc Jordan]

Quoted 18 lines · Michal Szpakowski

Intel Macs keep a battery charge limit inside the SMC, in the key BCLM (one byte, percent). The SMC enforces it on its own: charging stops at the limit and the value survives reboots and operating systems, so the only thing an OS has to do is write it. On macOS the tool "bclm" does that; on Linux there has been no way, because applesmc exposes keys read-only through key_at_index.

Expose it as the standard charge_control_end_threshold property on the battery through a power supply extension, so UPower and the desktops that read that property pick it up without knowing anything about Apple. The battery of an Intel Mac is an ACPI Smart Battery handled by the sbs driver, which the ACPI battery hooks do not cover, so the supply is found by walking the registered supplies; should it register after applesmc, its first property-change notification attaches the extension. Nothing happens on machines whose SMC lacks the key. Writes are read back, because the SMC silently drops values it does not accept; 100 disables the cap.

Quoted 4 lines

Tested on a MacBookPro13,1 (2016, 13", no Touch Bar): with the limit at 80 and the charger attached, charging ran at a steady 1.55 A and stopped at 79% of charge_full with status "Full" and current 0, and the limit read back 80 after a reboot.

Jordan Brough is working on similar patches, perhaps you two can work together to make sure there's no breakage on either of your laptops an no conflict between your patches:

https://lore.kernel.org/r/[email protected]

Thanks,

Lukas

#2 lore raw
Quoted 5 lines · Lukas Wunner

Jordan Brough is working on similar patches, perhaps you two can work together to make sure there's no breakage on either of your laptops an no conflict between your patches:

https://lore.kernel.org/r/[email protected]

Thanks, I had missed that series. Jordan's approach is the better one:
extending the battery hooks to SBS batteries is the proper fix for the problem I worked around by walking the supplies, and the BFCL/LED handling covers hardware I do not have. Please consider my patch withdrawn in favour of his; I am happy to test v3 and add a Tested-by.

Two things from a MacBookPro13,1 (2016, 13", USB-C, no MagSafe) that may be useful for it, Jordan:

1. This SMC has no BFCL key at all. A dump of all 798 keys has BCLM but no BFCL (the BF* keys here are BFCT, BFLO and BFWC), and asking the driver directly, through a debug attribute that calls applesmc_get_entry_by_key():

BFCL (no such key)
BCLM type=ui8 len=1 flags=0xd0 R W

With v2's set_property, the unconditional applesmc_write_key(BFCL) after the BCLM write therefore fails with -EINVAL, so userspace gets an error although the limit was in fact applied. Gating the LED write on applesmc_has_key(BFCL) at init, like the BCLM check, would fix that.

2. The SMC silently drops writes it does not like: applesmc_write_key() returns 0 and the key keeps its old value (seen on this machine with the CLWK key; BCLM accepted everything I tried between 50 and 100). Reading BCLM back after the write and returning -EINVAL on a mismatch costs one read and makes the failure visible.

For what it is worth, BCLM=80 on this machine stops charging at 79% of charge_full with status "Full", the value survives reboots, and UPower 1.91 picks the property up as charge-end-threshold once it re-reads the battery.

Michal