Re: [RFC PATCH v3 0/5] platform/x86: huawei-wmi: expose native firmware power profiles
From: Eren Özen
Date: Wed Nov 25 2026 - 03:43:23 EST
Hi Fuwei,
On Wed, Nov 25, 2026 at 03:40:44PM +0800, Fuwei Xie wrote:
> Thanks for trying this out and for catching the silent fallback to
> \SBTT. I'd appreciate a copy of the diff you tested.
Sure, it's below. Since my last mail I've also run it on the CREM-WXX9
(BIOS 2.32): built out of tree against the Fedora 7.3.4-300 kernel and
loaded in place of the stock module, on top of your five v3 patches:
- the thresholds read back correctly in charge modes 1 and 2, and as
0/100 in modes 0 and 4 (mode 3 not re-run),
- the original bug case works: writing 75/80 while the EC is in
smart-charge mode 4 puts it back in mode 1 with 75/80,
- writing 0/100 and the power_supply start/end attributes work,
- reloading the module in mode 1 and in mode 4 reports the right
values, and the kernel log shows nothing beyond the usual probe
messages (no lockdep on this kernel, though).
It also builds in-tree on for-next 61c0dac952b9 with W=1 and sparse. I
didn't re-measure the charging current this time; the EC side is the
same as with the original patch, and I checked the \GBCM and \GBTT
state after every step instead.
The diff is against for-next 61c0dac952b9 with your five v3 patches
applied. Compared with my v1 patch, only the charge-mode calls changed:
- huawei_wmi_charge_mode_get() takes the huawei pointer, returns
negative errors from huawei_wmi_cmd(), requires at least two data
bytes, and reads the mode and the parameter from data[0] and data[1].
- huawei_wmi_charge_mode_set() holds wmi_lock across the \GBCM read
and the \SBCM write.
- huawei_wmi_battery_get() takes wmi_lock around its own \GBCM read.
- the probe check calls huawei_wmi_charge_mode_get() under wmi_lock.
As before, I used Claude for the rebase, the test script and to draft
this mail; I've reviewed the diff and checked the results against the
test log.
Thanks,
Eren
diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c
index ff80050..f8b449d 100644
--- a/drivers/platform/x86/huawei-wmi.c
+++ b/drivers/platform/x86/huawei-wmi.c
@@ -38,6 +38,8 @@
enum {
BATTERY_THRESH_GET = 0x00001103, /* \GBTT */
BATTERY_THRESH_SET = 0x00001003, /* \SBTT */
+ BATTERY_CHARGE_MODE_GET = 0x00001603, /* \GBCM */
+ BATTERY_CHARGE_MODE_SET = 0x00001503, /* \SBCM */
FN_LOCK_GET = 0x00000604, /* \GFRS */
FN_LOCK_SET = 0x00000704, /* \SFRS */
MICMUTE_LED_SET = 0x00000b04, /* \SMLS */
@@ -68,6 +70,7 @@ struct huawei_wmi {
struct wmi_device *wdev;
struct notifier_block profile_notifier;
bool battery_available;
+ bool charge_mode_available;
bool fn_lock_available;
struct huawei_wmi_debug debug;
@@ -413,11 +416,93 @@ static void huawei_wmi_leds_setup(struct device *dev)
/* Battery protection */
+/*
+ * Newer firmware also implements \GBCM and \SBCM ("battery charge mode").
+ * \GBCM returns the charge mode in byte 1, a parameter of unknown meaning
+ * in byte 2 and a pair of thresholds in bytes 3 and 4, and \SBCM takes the
+ * same four values in bytes 2 to 5 of its argument. Thresholds set through
+ * \SBCM are also returned by \GBTT, but \SBTT does not update the ones
+ * returned by \GBCM. huawei_wmi_cmd() strips the status byte, so the mode
+ * and the parameter end up in data[0] and data[1].
+ *
+ * In charge modes 1 to 3 the EC does not charge above the end threshold
+ * returned by \GBTT, and a 0/0 pair means that charging is not limited.
+ * In charge modes 0 and 4 it charges past the end threshold. \SBTT,
+ * which turns a 0/100 request into 0/0, switches the EC to charge mode 4
+ * ("smart charge") in that case, and a later \SBTT write does not
+ * necessarily switch it back. So write the thresholds through \SBCM in
+ * charge mode 1 instead.
+ */
+#define HWMI_CHARGE_MODE_THRESHOLDS 1
+#define HWMI_CHARGE_MODE_PRESET_LAST 3
+
+static int huawei_wmi_charge_mode_get(struct huawei_wmi *huawei, u8 *mode, u8 *param)
+{
+ u8 data[0xff];
+ int len;
+
+ len = huawei_wmi_cmd(huawei, BATTERY_CHARGE_MODE_GET, data, sizeof(data));
+ if (len < 0)
+ return len;
+ if (len < 2)
+ return -ENODATA;
+
+ if (mode)
+ *mode = data[0];
+ if (param)
+ *param = data[1];
+
+ return 0;
+}
+
+static int huawei_wmi_charge_mode_set(struct huawei_wmi *huawei, int start, int end)
+{
+ union hwmi_arg arg;
+ u8 param;
+ int err;
+
+ guard(mutex)(&huawei->wmi_lock);
+ err = huawei_wmi_charge_mode_get(huawei, NULL, ¶m);
+ if (err)
+ return err;
+
+ if (start == 0 && end == 100)
+ end = 0;
+
+ arg.cmd = BATTERY_CHARGE_MODE_SET;
+ arg.args[2] = HWMI_CHARGE_MODE_THRESHOLDS;
+ arg.args[3] = param;
+ arg.args[4] = start;
+ arg.args[5] = end;
+
+ return huawei_wmi_cmd(huawei, arg.cmd, NULL, 0);
+}
+
static int huawei_wmi_battery_get(struct huawei_wmi *huawei, int *start, int *end)
{
u8 data[0xff];
int len;
+ if (huawei->charge_mode_available) {
+ u8 mode;
+ int err;
+
+ scoped_guard(mutex, &huawei->wmi_lock)
+ err = huawei_wmi_charge_mode_get(huawei, &mode, NULL);
+ if (err)
+ return err;
+
+ /* The thresholds are not used in the other charge modes. */
+ if (mode < HWMI_CHARGE_MODE_THRESHOLDS ||
+ mode > HWMI_CHARGE_MODE_PRESET_LAST) {
+ if (start)
+ *start = 0;
+ if (end)
+ *end = 100;
+ return 0;
+ }
+ }
+
scoped_guard(mutex, &huawei->wmi_lock)
len = huawei_wmi_cmd(huawei, BATTERY_THRESH_GET, data, sizeof(data));
if (len < 0)
@@ -433,6 +518,10 @@ static int huawei_wmi_battery_get(struct huawei_wmi *huawei, int *start, int *en
if (end)
*end = data[len - 1];
+ /* A 0/0 pair means that charging is not limited. */
+ if (huawei->charge_mode_available && end && !*end)
+ *end = 100;
+
return 0;
}
@@ -444,6 +533,9 @@ static int huawei_wmi_battery_set(struct huawei_wmi *huawei, int start, int end)
if (start < 0 || end < 0 || start > 100 || end > 100)
return -EINVAL;
+ if (huawei->charge_mode_available)
+ return huawei_wmi_charge_mode_set(huawei, start, end);
+
arg.cmd = BATTERY_THRESH_SET;
arg.args[2] = start;
arg.args[3] = end;
@@ -607,6 +699,17 @@ static void huawei_wmi_battery_setup(struct device *dev)
{
struct huawei_wmi *huawei = dev_get_drvdata(dev);
+ /*
+ * Only trust the \GBCM status if the firmware actually implements the
+ * methods, in case the HWMI dispatcher of some model reports success
+ * for unknown functions.
+ */
+ if (acpi_has_method(NULL, "\\GBCM") && acpi_has_method(NULL, "\\SBCM")) {
+ scoped_guard(mutex, &huawei->wmi_lock)
+ huawei->charge_mode_available =
+ !huawei_wmi_charge_mode_get(huawei, NULL, NULL);
+ }
+
huawei->battery_available = true;
if (huawei_wmi_battery_get(huawei, NULL, NULL)) {
huawei->battery_available = false;