Unverified Commit 03ae0a0d authored by Werner Sembach's avatar Werner Sembach Committed by Ilpo Järvinen
Browse files

platform/x86: uniwill-laptop: Implement USB-C power priority setting



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.

Reviewed-by: default avatarArmin Wolf <W_Armin@gmx.de>
Signed-off-by: default avatarWerner Sembach <wse@tuxedocomputers.com>
Link: https://patch.msgid.link/20260324203413.454361-3-wse@tuxedocomputers.com


Reviewed-by: default avatarIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: default avatarIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
parent f3660b13
Loading
Loading
Loading
Loading
+138 −7
Original line number Diff line number Diff line
@@ -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_CHARGING = 0,
	USB_C_POWER_PRIORITY_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,104 @@ static int uniwill_nvidia_ctgp_init(struct uniwill_data *data)
	return 0;
}

static const char * const usb_c_power_priority_text[] = {
	[USB_C_POWER_PRIORITY_CHARGING]		= "charging",
	[USB_C_POWER_PRIORITY_PERFORMANCE]	= "performance",
};

static const u8 usb_c_power_priority_value[] = {
	[USB_C_POWER_PRIORITY_CHARGING]		= 0,
	[USB_C_POWER_PRIORITY_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_text, buf);
	if (option < 0)
		return option;

	value = usb_c_power_priority_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;
	int ret;

	ret = regmap_read(data->regmap, EC_ADDR_USB_C_POWER_PRIORITY, &value);
	if (ret < 0)
		return ret;

	value &= USB_C_POWER_PRIORITY;

	if (usb_c_power_priority_value[USB_C_POWER_PRIORITY_PERFORMANCE] == value)
		return sysfs_emit(buf, "%s\n",
				  usb_c_power_priority_text[USB_C_POWER_PRIORITY_PERFORMANCE]);

	return sysfs_emit(buf, "%s\n", usb_c_power_priority_text[USB_C_POWER_PRIORITY_CHARGING]);
}

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_value[data->last_usb_c_power_priority_option];

	guard(mutex)(&data->usb_c_power_priority_lock);

	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;
	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;

	data->last_usb_c_power_priority_option =
		usb_c_power_priority_value[USB_C_POWER_PRIORITY_PERFORMANCE] == value ?
			USB_C_POWER_PRIORITY_PERFORMANCE :
			USB_C_POWER_PRIORITY_CHARGING;

	return 0;
}

static struct attribute *uniwill_attrs[] = {
	/* Keyboard-related */
	&dev_attr_fn_lock.attr,
@@ -893,6 +1002,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 +1037,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 +1532,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,6 +1629,7 @@ 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;
@@ -1552,6 +1667,10 @@ static int uniwill_probe(struct platform_device *pdev)
	if (ret < 0)
		return ret;

	ret = usb_c_power_priority_init(data);
	if (ret < 0)
		return ret;

	return uniwill_input_init(data);
}

@@ -1681,6 +1800,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;

	return usb_c_power_priority_restore(data);
}

static int uniwill_resume(struct device *dev)
{
	struct uniwill_data *data = dev_get_drvdata(dev);
@@ -1704,7 +1831,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);