Linux Power Management development
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Navon John Lukose <navonjohnlukose@gmail.com>
Cc: hansg@kernel.org, ilpo.jarvinen@linux.intel.com, sre@kernel.org,
	 platform-driver-x86@vger.kernel.org, linux-pm@vger.kernel.org,
	 linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH v2] platform/x86: hp-wmi: Add charge behaviour support
Date: Tue, 7 Jul 2026 16:06:21 +0300 (EEST)	[thread overview]
Message-ID: <edbe584a-c241-c3e6-9993-2783934885d9@linux.intel.com> (raw)
In-Reply-To: <20260531181326.1000801-1-navonjohnlukose@gmail.com>

On Sun, 31 May 2026, Navon John Lukose wrote:

> Some HP laptops expose battery charge mode control through the existing
> HP BIOS WMI GUID. On HP board 85C6, command types 0x1f and 0x2b map to
> the ACPI GBCC/GBCO and SBCC/SBCO methods through the WMI command
> dispatcher.
> 
> Expose the mode control through the standard power_supply
> charge_behaviour property using a battery hook and power_supply
> extension. The initial quirk is limited to board 85C6 and still
> requires a successful charge mode read before registering the extension.
> 
> The firmware uses different commands for restoring auto mode and setting
> explicit charge modes, so report the last successfully requested policy
> after seeding it from firmware at probe time. The regular battery status
> continues to report the live charging state.
> 
> Compile the battery hook support only when the ACPI battery driver is
> reachable so non-battery hp-wmi configurations keep building.
> 
> Assisted-by: Codex:GPT-5.5
> Signed-off-by: Navon John Lukose <navonjohnlukose@gmail.com>
> ---
> Changes in v2:
> - Compile the battery hook integration only when ACPI battery support is
>   reachable.
> - Keep charge_behaviour as the last successfully requested policy.
> - Use FIELD_PREP() for the write-mode field and document the readback values.
> - Leave the ACPI battery header include unguarded and split compact return
>   handling into explicit branches.
> - Reject unknown GBCO readback values during setup instead of treating them as
>   auto.

Why is this marked as RFC?

>  drivers/platform/x86/hp/hp-wmi.c | 230 +++++++++++++++++++++++++++++++
>  1 file changed, 230 insertions(+)
> 
> diff --git a/drivers/platform/x86/hp/hp-wmi.c b/drivers/platform/x86/hp/hp-wmi.c
> index d1cc6e7d176c..66f409962cd9 100644
> --- a/drivers/platform/x86/hp/hp-wmi.c
> +++ b/drivers/platform/x86/hp/hp-wmi.c
> @@ -14,6 +14,7 @@
>  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
>  
>  #include <linux/acpi.h>
> +#include <linux/bitfield.h>
>  #include <linux/cleanup.h>
>  #include <linux/compiler_attributes.h>
>  #include <linux/dmi.h>
> @@ -36,6 +37,8 @@
>  #include <linux/types.h>
>  #include <linux/workqueue.h>
>  
> +#include <acpi/battery.h>
> +
>  MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
>  MODULE_DESCRIPTION("HP laptop WMI driver");
>  MODULE_LICENSE("GPL");
> @@ -309,7 +312,9 @@ enum hp_wmi_commandtype {
>  	HPWMI_HOTKEY_QUERY		= 0x0c,
>  	HPWMI_FEATURE2_QUERY		= 0x0d,
>  	HPWMI_WIRELESS2_QUERY		= 0x1b,
> +	HPWMI_BATTERY_CHARGE_CONTROL	= 0x1f,
>  	HPWMI_POSTCODEERROR_QUERY	= 0x2a,
> +	HPWMI_BATTERY_CHARGE_MODE	= 0x2b,
>  	HPWMI_SYSTEM_DEVICE_MODE	= 0x40,
>  	HPWMI_THERMAL_PROFILE_QUERY	= 0x4c,
>  };
> @@ -378,6 +383,27 @@ enum hp_wireless2_bits {
>  #define IS_HWBLOCKED(x) ((x & HPWMI_POWER_FW_OR_HW) != HPWMI_POWER_FW_OR_HW)
>  #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
>  
> +#if IS_REACHABLE(CONFIG_ACPI_BATTERY)
> +#define HPWMI_CHARGE_BEHAVIOURS \
> +	(BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) | \
> +	 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE) | \
> +	 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
> +
> +/*
> + * SBCC/SBCO read the requested mode from the high byte of the input value.
> + * GBCO returns literal status values, not a bit mask.
> + */
> +#define HPWMI_CHARGE_MODE_MASK			GENMASK(15, 8)
> +#define HPWMI_CHARGE_MODE_AUTO			0x00
> +#define HPWMI_CHARGE_MODE_FORCE_DISCHARGE	0x02
> +#define HPWMI_CHARGE_MODE_INHIBIT		0x05
> +
> +#define HPWMI_CHARGE_MODE_READ_AUTO		0x00
> +#define HPWMI_CHARGE_MODE_READ_FORCE_DISCHARGE	0x02
> +#define HPWMI_CHARGE_MODE_READ_INHIBIT		0x04
> +#define HPWMI_CHARGE_MODE_READ_INHIBIT_EXT	0x06
> +#endif
> +
>  struct bios_rfkill2_device_state {
>  	u8 radio_type;
>  	u8 bus_type;
> @@ -465,6 +491,18 @@ static const char * const tablet_chassis_types[] = {
>  	"32"  /* Detachable */
>  };
>  
> +#if IS_REACHABLE(CONFIG_ACPI_BATTERY)
> +static const struct dmi_system_id hp_wmi_charge_control_quirks[] __initconst = {
> +	{
> +		.matches = {
> +			DMI_MATCH(DMI_BOARD_VENDOR, "HP"),
> +			DMI_MATCH(DMI_BOARD_NAME, "85C6"),
> +		},
> +	},
> +	{}
> +};
> +#endif
> +
>  #define DEVICE_MODE_TABLET	0x06
>  
>  #define CPU_FAN 0
> @@ -694,6 +732,194 @@ static int hp_wmi_read_int(int query)
>  	return val;
>  }
>  
> +#if IS_REACHABLE(CONFIG_ACPI_BATTERY)
> +static DEFINE_MUTEX(hp_wmi_charge_lock);

Please always add a comment telling what is protected by a lock.

> +static enum power_supply_charge_behaviour hp_wmi_charge_behaviour =
> +	POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
> +
> +static int hp_wmi_charge_mode_to_behaviour(int mode,
> +					   enum power_supply_charge_behaviour *behaviour)
> +{
> +	switch (mode) {
> +	case HPWMI_CHARGE_MODE_READ_AUTO:
> +		*behaviour = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
> +		return 0;
> +	case HPWMI_CHARGE_MODE_READ_FORCE_DISCHARGE:
> +		*behaviour = POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE;
> +		return 0;
> +	case HPWMI_CHARGE_MODE_READ_INHIBIT:
> +	case HPWMI_CHARGE_MODE_READ_INHIBIT_EXT:
> +		*behaviour = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE;
> +		return 0;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int hp_wmi_charge_mode_read(void)
> +{
> +	int val = 0, ret;
> +
> +	ret = hp_wmi_perform_query(HPWMI_BATTERY_CHARGE_MODE, HPWMI_READ,
> +				   &val, zero_if_sup(val), sizeof(val));
> +	if (ret < 0)
> +		return ret;
> +	if (ret > 0)
> +		return -EINVAL;
> +
> +	return val & 0xff;
> +}
> +
> +static int hp_wmi_charge_write(enum hp_wmi_commandtype query, u32 mode)
> +{
> +	int ret;
> +
> +	mode = FIELD_PREP(HPWMI_CHARGE_MODE_MASK, mode);
> +	ret = hp_wmi_perform_query(query, HPWMI_WRITE, &mode, sizeof(mode), 0);
> +	if (ret < 0)
> +		return ret;
> +	if (ret > 0)
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
> +static int hp_wmi_charge_set_behaviour(enum power_supply_charge_behaviour behaviour)
> +{
> +	int ret;
> +
> +	guard(mutex)(&hp_wmi_charge_lock);
> +
> +	/*
> +	 * HP firmware exposes normal charging through the GBCC/SBCC command
> +	 * pair, while explicit charge modes use the GBCO/SBCO command pair.
> +	 */
> +	switch (behaviour) {
> +	case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
> +		ret = hp_wmi_charge_write(HPWMI_BATTERY_CHARGE_CONTROL,
> +					  HPWMI_CHARGE_MODE_AUTO);
> +		break;
> +	case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
> +		ret = hp_wmi_charge_write(HPWMI_BATTERY_CHARGE_MODE,
> +					  HPWMI_CHARGE_MODE_INHIBIT);
> +		break;
> +	case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
> +		ret = hp_wmi_charge_write(HPWMI_BATTERY_CHARGE_MODE,
> +					  HPWMI_CHARGE_MODE_FORCE_DISCHARGE);
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
> +
> +	if (ret)
> +		return ret;
> +
> +	/*
> +	 * charge_behaviour is the requested policy. The live charging state is
> +	 * still reported by the regular battery status property.
> +	 */
> +	hp_wmi_charge_behaviour = behaviour;
> +
> +	return 0;
> +}
> +
> +static int hp_wmi_charge_get_property(struct power_supply *psy,
> +				      const struct power_supply_ext *ext,
> +				      void *data,
> +				      enum power_supply_property psp,
> +				      union power_supply_propval *val)
> +{
> +	if (psp != POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR)
> +		return -EINVAL;
> +
> +	guard(mutex)(&hp_wmi_charge_lock);
> +	val->intval = hp_wmi_charge_behaviour;
> +
> +	return 0;
> +}
> +
> +static int hp_wmi_charge_set_property(struct power_supply *psy,
> +				      const struct power_supply_ext *ext,
> +				      void *data,
> +				      enum power_supply_property psp,
> +				      const union power_supply_propval *val)
> +{
> +	if (psp != POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR)
> +		return -EINVAL;
> +
> +	return hp_wmi_charge_set_behaviour(val->intval);
> +}
> +
> +static int hp_wmi_charge_property_is_writeable(struct power_supply *psy,
> +					       const struct power_supply_ext *ext,
> +					       void *data,
> +					       enum power_supply_property psp)
> +{
> +	return psp == POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR;
> +}
> +
> +static const enum power_supply_property hp_wmi_charge_properties[] = {
> +	POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
> +};
> +
> +static const struct power_supply_ext hp_wmi_charge_extension = {
> +	.name			= "hp-wmi-charge-control",
> +	.properties		= hp_wmi_charge_properties,
> +	.num_properties		= ARRAY_SIZE(hp_wmi_charge_properties),
> +	.charge_behaviours	= HPWMI_CHARGE_BEHAVIOURS,
> +	.get_property		= hp_wmi_charge_get_property,
> +	.set_property		= hp_wmi_charge_set_property,
> +	.property_is_writeable	= hp_wmi_charge_property_is_writeable,
> +};
> +
> +static int hp_wmi_charge_add_battery(struct power_supply *battery,
> +				     struct acpi_battery_hook *hook)
> +{
> +	return power_supply_register_extension(battery, &hp_wmi_charge_extension,
> +					       &hp_wmi_platform_dev->dev, NULL);
> +}
> +
> +static int hp_wmi_charge_remove_battery(struct power_supply *battery,
> +					struct acpi_battery_hook *hook)
> +{
> +	power_supply_unregister_extension(battery, &hp_wmi_charge_extension);
> +	return 0;
> +}
> +
> +static struct acpi_battery_hook hp_wmi_charge_battery_hook = {
> +	.name = "HP WMI charge control",
> +	.add_battery = hp_wmi_charge_add_battery,
> +	.remove_battery = hp_wmi_charge_remove_battery,
> +};
> +
> +static int __init hp_wmi_charge_control_setup(struct device *dev)
> +{
> +	enum power_supply_charge_behaviour behaviour;
> +	int ret;
> +
> +	if (!dmi_check_system(hp_wmi_charge_control_quirks))
> +		return 0;
> +
> +	ret = hp_wmi_charge_mode_read();
> +	if (ret < 0)
> +		return 0;
> +
> +	ret = hp_wmi_charge_mode_to_behaviour(ret, &behaviour);
> +	if (ret)
> +		return 0;
> +
> +	scoped_guard(mutex, &hp_wmi_charge_lock)
> +		hp_wmi_charge_behaviour = behaviour;
> +
> +	return devm_battery_hook_register(dev, &hp_wmi_charge_battery_hook);
> +}
> +#else
> +static int __init hp_wmi_charge_control_setup(struct device *dev)
> +{
> +	return 0;
> +}
> +#endif
> +
>  static int hp_wmi_get_dock_state(void)
>  {
>  	int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
> @@ -2284,6 +2510,10 @@ static int __init hp_wmi_bios_setup(struct platform_device *device)
>  	if (err < 0)
>  		return err;
>  
> +	err = hp_wmi_charge_control_setup(&device->dev);
> +	if (err)
> +		return err;
> +
>  	thermal_profile_setup(device);
>  
>  	return 0;
> 

-- 
 i.


      reply	other threads:[~2026-07-07 13:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260505200222.157144-1-navonjohnlukose@gmail.com>
2026-05-27  6:15 ` [RFC PATCH] platform/x86: hp-wmi: Add charge behaviour support Navon John Lukose
2026-05-31 18:13 ` [RFC PATCH v2] " Navon John Lukose
2026-07-07 13:06   ` Ilpo Järvinen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=edbe584a-c241-c3e6-9993-2783934885d9@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=hansg@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=navonjohnlukose@gmail.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=sre@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox