mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Armin Wolf <W_Armin@gmx.de>
To: "Werner Sembach" <wse@tuxedocomputers.com>,
	"Hans de Goede" <hansg@kernel.org>,
	"Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Cc: platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/4] platform/x86/uniwill: Implement USB-C power priority setting
Date: Sun, 1 Mar 2026 14:05:46 +0100	[thread overview]
Message-ID: <1f498e55-c365-4fb5-b06f-e0413774a453@gmx.de> (raw)
In-Reply-To: <20260227200906.514928-3-wse@tuxedocomputers.com>

Am 27.02.26 um 21:07 schrieb Werner Sembach:

> On some devices Uniwill offers the option to set the USB-C port to
> prioritise charging or performance. This patch exposes this setting to the
> userspace via sysfs for all TUXEDO devices supporting it.
>
> Signed-off-by: Werner Sembach <wse@tuxedocomputers.com>
> ---
>   drivers/platform/x86/uniwill/uniwill-acpi.c | 147 +++++++++++++++++++-
>   1 file changed, 140 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/platform/x86/uniwill/uniwill-acpi.c b/drivers/platform/x86/uniwill/uniwill-acpi.c
> index 048b265bff374..f92c24d64160b 100644
> --- a/drivers/platform/x86/uniwill/uniwill-acpi.c
> +++ b/drivers/platform/x86/uniwill/uniwill-acpi.c
> @@ -266,8 +266,8 @@
>   #define BATTERY_CHARGE_FULL_OVER_24H	BIT(3)
>   #define BATTERY_ERM_STATUS_REACHED	BIT(4)
>   
> -#define EC_ADDR_CHARGE_PRIO		0x07CC
> -#define CHARGING_PERFORMANCE		BIT(7)
> +#define EC_ADDR_USB_C_POWER_PRIORITY	0x07CC
> +#define USB_C_POWER_PRIORITY		BIT(7)
>   
>   /* Same bits as EC_ADDR_LIGHTBAR_AC_CTRL except LIGHTBAR_S3_OFF */
>   #define EC_ADDR_LIGHTBAR_BAT_CTRL	0x07E2
> @@ -324,6 +324,12 @@
>   #define UNIWILL_FEATURE_PRIMARY_FAN		BIT(7)
>   #define UNIWILL_FEATURE_SECONDARY_FAN		BIT(8)
>   #define UNIWILL_FEATURE_NVIDIA_CTGP_CONTROL	BIT(9)
> +#define UNIWILL_FEATURE_USB_C_POWER_PRIORITY	BIT(10)
> +
> +enum usb_c_power_priority_options {
> +	USB_C_POWER_PRIORITY_OPTIONS_CHARGING = 0,
> +	USB_C_POWER_PRIORITY_OPTIONS_PERFORMANCE,
> +};
>   
>   struct uniwill_data {
>   	struct device *dev;
> @@ -343,6 +349,8 @@ struct uniwill_data {
>   	struct mutex input_lock;	/* Protects input sequence during notify */
>   	struct input_dev *input_device;
>   	struct notifier_block nb;
> +	struct mutex usb_c_power_priority_lock; /* Protects dependent bit write and state safe */
> +	enum usb_c_power_priority_options last_usb_c_power_priority_option;
>   };
>   
>   struct uniwill_battery_entry {
> @@ -527,6 +535,7 @@ static bool uniwill_writeable_reg(struct device *dev, unsigned int reg)
>   	case EC_ADDR_CTGP_DB_CTGP_OFFSET:
>   	case EC_ADDR_CTGP_DB_TPP_OFFSET:
>   	case EC_ADDR_CTGP_DB_DB_OFFSET:
> +	case EC_ADDR_USB_C_POWER_PRIORITY:
>   		return true;
>   	default:
>   		return false;
> @@ -565,6 +574,7 @@ static bool uniwill_readable_reg(struct device *dev, unsigned int reg)
>   	case EC_ADDR_CTGP_DB_CTGP_OFFSET:
>   	case EC_ADDR_CTGP_DB_TPP_OFFSET:
>   	case EC_ADDR_CTGP_DB_DB_OFFSET:
> +	case EC_ADDR_USB_C_POWER_PRIORITY:
>   		return true;
>   	default:
>   		return false;
> @@ -587,6 +597,7 @@ static bool uniwill_volatile_reg(struct device *dev, unsigned int reg)
>   	case EC_ADDR_TRIGGER:
>   	case EC_ADDR_SWITCH_STATUS:
>   	case EC_ADDR_CHARGE_CTRL:
> +	case EC_ADDR_USB_C_POWER_PRIORITY:
>   		return true;
>   	default:
>   		return false;
> @@ -883,6 +894,106 @@ static int uniwill_nvidia_ctgp_init(struct uniwill_data *data)
>   	return 0;
>   }
>   
> +static const char * const USB_C_POWER_PRIORITY_OPTIONS_TEXT[] = {
> +	[USB_C_POWER_PRIORITY_OPTIONS_CHARGING]		= "charging",
> +	[USB_C_POWER_PRIORITY_OPTIONS_PERFORMANCE]	= "performance",
> +};
> +
> +static const u8 USB_C_POWER_PRIORITY_OPTIONS_VALUE[] = {
> +	[USB_C_POWER_PRIORITY_OPTIONS_CHARGING]		= 0,
> +	[USB_C_POWER_PRIORITY_OPTIONS_PERFORMANCE]	= USB_C_POWER_PRIORITY,
> +};
> +
> +static ssize_t usb_c_power_priority_store(struct device *dev,
> +					  struct device_attribute *attr,
> +					  const char *buf, size_t count)
> +{
> +	struct uniwill_data *data = dev_get_drvdata(dev);
> +	enum usb_c_power_priority_options option;
> +	unsigned int value;
> +	int ret;
> +
> +	option = sysfs_match_string(USB_C_POWER_PRIORITY_OPTIONS_TEXT, buf);
> +	if (option < 0)
> +		return option;
> +
> +	value = USB_C_POWER_PRIORITY_OPTIONS_VALUE[option];
> +
> +	guard(mutex)(&data->usb_c_power_priority_lock);
> +
> +	ret = regmap_update_bits(data->regmap, EC_ADDR_USB_C_POWER_PRIORITY,
> +				 USB_C_POWER_PRIORITY, value);
> +	if (ret < 0)
> +		return ret;
> +
> +	data->last_usb_c_power_priority_option = option;
> +
> +	return count;
> +}
> +
> +static ssize_t usb_c_power_priority_show(struct device *dev,
> +					 struct device_attribute *attr,
> +					 char *buf)
> +{
> +	struct uniwill_data *data = dev_get_drvdata(dev);
> +	unsigned int value, i;
> +	int ret;
> +
> +	ret = regmap_read(data->regmap, EC_ADDR_USB_C_POWER_PRIORITY, &value);
> +	if (ret < 0)
> +		return ret;
> +
> +	value &= USB_C_POWER_PRIORITY;
> +
> +	for (i = 0; i < ARRAY_SIZE(USB_C_POWER_PRIORITY_OPTIONS_VALUE); ++i)
> +		if (USB_C_POWER_PRIORITY_OPTIONS_VALUE[i] == value)
> +			return sysfs_emit(buf, "%s\n",
> +					  USB_C_POWER_PRIORITY_OPTIONS_TEXT[i]);
> +

Please drop this for-loop and use a if-statement instead:

	if (value & USB_C_POWER_PRIORITY)
		return sysfs_emit(buf, "%s\n", USB_C_POWER_PRIORITY_OPTIONS_TEXT[USB_C_POWER_PRIORITY_OPTIONS_PERFORMANCE]);

	return sysfs_emit(buf, "%s\n", USB_C_POWER_PRIORITY_OPTIONS_TEXT[USB_C_POWER_PRIORITY_OPTIONS_CHARGING]);

Also, i think USB_C_POWER_PRIORITY_OPTIONS_VALUE[] is unnecessary. Using an if-statement would yield more readable
code than using for-loops in this case.

> +	/* Since all possible values are valid, this should be unreachable. */
> +	return -EINVAL;
> +}
> +
> +static DEVICE_ATTR_RW(usb_c_power_priority);
> +
> +static int usb_c_power_priority_restore(struct uniwill_data *data)
> +{
> +	unsigned int value;
> +
> +	value = USB_C_POWER_PRIORITY_OPTIONS_VALUE[data->last_usb_c_power_priority_option];

You need to take the appropriate mutex here.

> +
> +	return regmap_update_bits(data->regmap, EC_ADDR_USB_C_POWER_PRIORITY,
> +				  USB_C_POWER_PRIORITY, value);
> +}
> +
> +static int usb_c_power_priority_init(struct uniwill_data *data)
> +{
> +	unsigned int value, i;
> +	int ret;
> +
> +	if (!uniwill_device_supports(data, UNIWILL_FEATURE_USB_C_POWER_PRIORITY))
> +		return 0;
> +
> +	ret = devm_mutex_init(data->dev, &data->usb_c_power_priority_lock);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = regmap_read(data->regmap, EC_ADDR_USB_C_POWER_PRIORITY, &value);
> +	if (ret < 0)
> +		return ret;
> +
> +	value &= USB_C_POWER_PRIORITY;
> +
> +	for (i = 0; i < ARRAY_SIZE(USB_C_POWER_PRIORITY_OPTIONS_VALUE); ++i)
> +		if (USB_C_POWER_PRIORITY_OPTIONS_VALUE[i] == value) {
> +			data->last_usb_c_power_priority_option = i;
> +			return 0;
> +		}

Please drop this for-loop and use an if-statement instead.

> +
> +	/* Since all possible values are valid, this should be unreachable. */
> +	return -EINVAL;
> +}
> +
>   static struct attribute *uniwill_attrs[] = {
>   	/* Keyboard-related */
>   	&dev_attr_fn_lock.attr,
> @@ -893,6 +1004,7 @@ static struct attribute *uniwill_attrs[] = {
>   	&dev_attr_breathing_in_suspend.attr,
>   	/* Power-management-related */
>   	&dev_attr_ctgp_offset.attr,
> +	&dev_attr_usb_c_power_priority.attr,
>   	NULL
>   };
>   
> @@ -927,6 +1039,11 @@ static umode_t uniwill_attr_is_visible(struct kobject *kobj, struct attribute *a
>   			return attr->mode;
>   	}
>   
> +	if (attr == &dev_attr_usb_c_power_priority.attr) {
> +		if (uniwill_device_supports(data, UNIWILL_FEATURE_USB_C_POWER_PRIORITY))
> +			return attr->mode;
> +	}
> +
>   	return 0;
>   }
>   
> @@ -1417,11 +1534,10 @@ static int uniwill_notifier_call(struct notifier_block *nb, unsigned long action
>   
>   		return NOTIFY_OK;
>   	case UNIWILL_OSD_DC_ADAPTER_CHANGED:
> -		/* noop for the time being, will change once charging priority
> -		 * gets implemented.
> -		 */
> +		if (!uniwill_device_supports(data, UNIWILL_FEATURE_USB_C_POWER_PRIORITY))
> +			return NOTIFY_DONE;
>   
> -		return NOTIFY_OK;
> +		return notifier_from_errno(usb_c_power_priority_restore(data));
>   	case UNIWILL_OSD_FN_LOCK:
>   		if (!uniwill_device_supports(data, UNIWILL_FEATURE_FN_LOCK))
>   			return NOTIFY_DONE;
> @@ -1515,10 +1631,15 @@ static int uniwill_probe(struct platform_device *pdev)
>   		return PTR_ERR(regmap);
>   
>   	data->regmap = regmap;
> +
>   	ret = devm_mutex_init(&pdev->dev, &data->super_key_lock);
>   	if (ret < 0)
>   		return ret;
>   
> +	ret = usb_c_power_priority_init(data);
> +	if (ret < 0)
> +		return ret;
> +
>   	ret = uniwill_ec_init(data);
>   	if (ret < 0)
>   		return ret;
> @@ -1681,6 +1802,14 @@ static int uniwill_resume_nvidia_ctgp(struct uniwill_data *data)
>   			       CTGP_DB_DB_ENABLE | CTGP_DB_CTGP_ENABLE);
>   }
>   
> +static int uniwill_resume_usb_c_power_priority(struct uniwill_data *data)
> +{
> +	if (!uniwill_device_supports(data, UNIWILL_FEATURE_USB_C_POWER_PRIORITY))
> +		return 0;

I think it would make sense to move the feature check into usb_c_power_priority_restore().
This way usb_c_power_priority_restore() can be called by both the notifier and the resume
code directly.

It would also be very nice if you can include a short changelog with your next patch series.

Thanks,
Armin Wolf

> +
> +	return usb_c_power_priority_restore(data);
> +}
> +
>   static int uniwill_resume(struct device *dev)
>   {
>   	struct uniwill_data *data = dev_get_drvdata(dev);
> @@ -1704,7 +1833,11 @@ static int uniwill_resume(struct device *dev)
>   	if (ret < 0)
>   		return ret;
>   
> -	return uniwill_resume_nvidia_ctgp(data);
> +	ret = uniwill_resume_nvidia_ctgp(data);
> +	if (ret < 0)
> +		return ret;
> +
> +	return uniwill_resume_usb_c_power_priority(data);
>   }
>   
>   static DEFINE_SIMPLE_DEV_PM_OPS(uniwill_pm_ops, uniwill_suspend, uniwill_resume);

  reply	other threads:[~2026-03-01 13:05 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260227200906.514928-1-wse@tuxedocomputers.com>
2026-02-27 20:07 ` [PATCH v3 1/4] platform/x86/uniwill: uniwill-laptop: Rework hwmon feature defines Werner Sembach
2026-02-27 20:07 ` [PATCH v3 2/4] platform/x86/uniwill: Implement USB-C power priority setting Werner Sembach
2026-03-01 13:05   ` Armin Wolf [this message]
2026-03-02 18:22     ` Werner Sembach
2026-03-03 20:40       ` Armin Wolf
2026-03-03 22:52     ` Werner Sembach
2026-02-27 20:07 ` [PATCH v3 3/4] platform/x86/uniwill: Apply features across all TUXEDO devices Werner Sembach
2026-02-27 20:07 ` [PATCH v3 4/4] Documentation: laptops: Update documentation for uniwill laptops Werner Sembach
2026-03-01 13:15   ` Armin Wolf

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=1f498e55-c365-4fb5-b06f-e0413774a453@gmx.de \
    --to=w_armin@gmx.de \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=wse@tuxedocomputers.com \
    /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

all inboxes | Powered by JetHome®