Skip to contentSkip to conversation

platform/x86/tuxedo: Implement TUXEDO TUXI ACPI TFAN via hwmon

PATCH In review Werner Sembach 3 participants 7 messages Mar 13, 2025 – Mar 24, 2025

Subsystems (3)
lore
Share & tools
lore

Werner Sembach

author maintainer
#0 lore raw

v3 cover platform/x86/tuxedo: Implement TUXEDO TUXI ACPI TFAN via hwmon

The static assert Ilpo suggested is still missing, but it is fully functional and tested by me.

v2: Coding style fixes
Different safeguard values
v3: Coding style fixes
Add cover letter

Werner Sembach (1):
platform/x86/tuxedo: Implement TUXEDO TUXI ACPI TFAN via hwmon

 MAINTAINERS                                  |   6 +
 drivers/platform/x86/Kconfig                 |   2 +
 drivers/platform/x86/Makefile                |   3 +
 drivers/platform/x86/tuxedo/Kbuild           |   8 +
 drivers/platform/x86/tuxedo/Kconfig          |   8 +
 drivers/platform/x86/tuxedo/nbxx/Kbuild      |   9 +
 drivers/platform/x86/tuxedo/nbxx/Kconfig     |  15 +
 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c | 591 +++++++++++++++++++
 8 files changed, 642 insertions(+)
 create mode 100644 drivers/platform/x86/tuxedo/Kbuild
 create mode 100644 drivers/platform/x86/tuxedo/Kconfig
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kbuild
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kconfig
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v3 1/1 platform/x86/tuxedo: Implement TUXEDO TUXI ACPI TFAN via hwmon

The TUXEDO Sirius 16 Gen1 & Gen2 have the custom TUXEDO Interface (TUXI) ACPI interface which currently consists of the TFAN device. This has ACPI functions to control the built in fans and monitor fan speeds and CPU and GPU temperature.

This driver implements this TFAN device via the hwmon subsystem with an added temperature check that ensure a minimum fanspeed at certain temperatures. This allows userspace controlled, but hardware safe, custom fan curves.

---
 MAINTAINERS                                  |   6 +
 drivers/platform/x86/Kconfig                 |   2 +
 drivers/platform/x86/Makefile                |   3 +
 drivers/platform/x86/tuxedo/Kbuild           |   8 +
 drivers/platform/x86/tuxedo/Kconfig          |   8 +
 drivers/platform/x86/tuxedo/nbxx/Kbuild      |   9 +
 drivers/platform/x86/tuxedo/nbxx/Kconfig     |  15 +
 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c | 591 +++++++++++++++++++
 8 files changed, 642 insertions(+)
 create mode 100644 drivers/platform/x86/tuxedo/Kbuild
 create mode 100644 drivers/platform/x86/tuxedo/Kconfig
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kbuild
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kconfig
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c
MAINTAINERS +6 -0
index 8e0736dc2ee0e..7139c32e96dc7 100644
@@ -24190,6 +24190,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
 F:	tools/power/x86/turbostat/
 F:	tools/testing/selftests/turbostat/
 
+TUXEDO DRIVERS
+M:	Werner Sembach <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/platform/x86/tuxedo/
+
 TW5864 VIDEO4LINUX DRIVER
 M:	Bluecherry Maintainers <[email protected]>
 M:	Andrey Utkin <[email protected]>
drivers/platform/x86/Kconfig +2 -0
index 0258dd879d64b..58b258cde4fdb 100644
@@ -1186,6 +1186,8 @@ config SEL3350_PLATFORM
 	  To compile this driver as a module, choose M here: the module
 	  will be called sel3350-platform.
 
+source "drivers/platform/x86/tuxedo/Kconfig"
+
 endif # X86_PLATFORM_DEVICES
 
 config P2SB
drivers/platform/x86/Makefile +3 -0
index e1b1429470674..1562dcd7ad9a5 100644
@@ -153,3 +153,6 @@ obj-$(CONFIG_WINMATE_FM07_KEYS)		+= winmate-fm07-keys.o
 
 # SEL
 obj-$(CONFIG_SEL3350_PLATFORM)		+= sel3350-platform.o
+
+# TUXEDO
+obj-y					+= tuxedo/
drivers/platform/x86/tuxedo/Kbuild new file +8 -0
new file mode 100644
index 0000000000000..dc55b403f201d
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+obj-y	+= nbxx/
drivers/platform/x86/tuxedo/Kconfig new file +8 -0
new file mode 100644
index 0000000000000..1b22a0b29460a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+source "drivers/platform/x86/tuxedo/nbxx/Kconfig"
drivers/platform/x86/tuxedo/nbxx/Kbuild new file +9 -0
new file mode 100644
index 0000000000000..256b03921c732
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+tuxedo_nbxx_acpi_tuxi-y			:= acpi_tuxi.o
+obj-$(CONFIG_TUXEDO_NBXX_ACPI_TUXI)	+= tuxedo_nbxx_acpi_tuxi.o
drivers/platform/x86/tuxedo/nbxx/Kconfig new file +15 -0
new file mode 100644
index 0000000000000..0d011c0c715a5
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+config TUXEDO_NBXX_ACPI_TUXI
+	tristate "TUXEDO NBxx ACPI TUXI Platform Driver"
+	help
+	  This driver implements the ACPI TUXI device found on some TUXEDO
+	  Notebooks. Currently this consists only of the TFAN subdevice which is
+	  implemented via hwmon.
+
+	  When compiled as a module it will be called tuxedo_nbxx_acpi_tuxi
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +591 -0

591 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Hi Werner,

drivers/platform/x86/tuxedo/Kbuild

Quoted 69 lines · Werner Sembach

The TUXEDO Sirius 16 Gen1 & Gen2 have the custom TUXEDO Interface (TUXI) ACPI interface which currently consists of the TFAN device. This has ACPI functions to control the built in fans and monitor fan speeds and CPU and GPU temperature.

This driver implements this TFAN device via the hwmon subsystem with an added temperature check that ensure a minimum fanspeed at certain temperatures. This allows userspace controlled, but hardware safe, custom fan curves.

---
 MAINTAINERS                                  |   6 +
 drivers/platform/x86/Kconfig                 |   2 +
 drivers/platform/x86/Makefile                |   3 +
 drivers/platform/x86/tuxedo/Kbuild           |   8 +
 drivers/platform/x86/tuxedo/Kconfig          |   8 +
 drivers/platform/x86/tuxedo/nbxx/Kbuild      |   9 +
 drivers/platform/x86/tuxedo/nbxx/Kconfig     |  15 +
 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c | 591 +++++++++++++++++++
 8 files changed, 642 insertions(+)
 create mode 100644 drivers/platform/x86/tuxedo/Kbuild
 create mode 100644 drivers/platform/x86/tuxedo/Kconfig
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kbuild
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kconfig
 create mode 100644 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c
MAINTAINERS +6 -0
index 8e0736dc2ee0e..7139c32e96dc7 100644
@@ -24190,6 +24190,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
 F:	tools/power/x86/turbostat/
 F:	tools/testing/selftests/turbostat/
 
+TUXEDO DRIVERS
+M:	Werner Sembach <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/platform/x86/tuxedo/
+
 TW5864 VIDEO4LINUX DRIVER
 M:	Bluecherry Maintainers <[email protected]>
 M:	Andrey Utkin <[email protected]>
drivers/platform/x86/Kconfig +2 -0
index 0258dd879d64b..58b258cde4fdb 100644
@@ -1186,6 +1186,8 @@ config SEL3350_PLATFORM
 	  To compile this driver as a module, choose M here: the module
 	  will be called sel3350-platform.
 
+source "drivers/platform/x86/tuxedo/Kconfig"
+
 endif # X86_PLATFORM_DEVICES
 
 config P2SB
drivers/platform/x86/Makefile +3 -0
index e1b1429470674..1562dcd7ad9a5 100644
@@ -153,3 +153,6 @@ obj-$(CONFIG_WINMATE_FM07_KEYS)		+= winmate-fm07-keys.o
 
 # SEL
 obj-$(CONFIG_SEL3350_PLATFORM)		+= sel3350-platform.o
+
+# TUXEDO
+obj-y					+= tuxedo/
drivers/platform/x86/tuxedo/Kbuild +0 -0

I think this should be named Makefile instead of Kbuild.

drivers/platform/x86/tuxedo/nbxx/Kbuild

Quoted 28 lines

new file mode 100644
index 0000000000000..dc55b403f201d

drivers/platform/x86/tuxedo/Kbuild new file +8 -0
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+obj-y	+= nbxx/
drivers/platform/x86/tuxedo/Kconfig new file +8 -0
new file mode 100644
index 0000000000000..1b22a0b29460a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+source "drivers/platform/x86/tuxedo/nbxx/Kconfig"
drivers/platform/x86/tuxedo/nbxx/Kbuild +0 -0

Same as above.

Quoted 55 lines

new file mode 100644
index 0000000000000..256b03921c732

drivers/platform/x86/tuxedo/nbxx/Kbuild new file +9 -0
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+tuxedo_nbxx_acpi_tuxi-y			:= acpi_tuxi.o
+obj-$(CONFIG_TUXEDO_NBXX_ACPI_TUXI)	+= tuxedo_nbxx_acpi_tuxi.o
drivers/platform/x86/tuxedo/nbxx/Kconfig new file +15 -0
new file mode 100644
index 0000000000000..0d011c0c715a5
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+config TUXEDO_NBXX_ACPI_TUXI
+	tristate "TUXEDO NBxx ACPI TUXI Platform Driver"
+	help
+	  This driver implements the ACPI TUXI device found on some TUXEDO
+	  Notebooks. Currently this consists only of the TFAN subdevice which is
+	  implemented via hwmon.
+
+	  When compiled as a module it will be called tuxedo_nbxx_acpi_tuxi
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +14 -0
new file mode 100644
index 0000000000000..bb452cc33568a
@@ -0,0 +1,591 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024-2025 Werner Sembach [email protected]
+ */
+
+#include <linux/acpi.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/units.h>

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 24

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+#include <linux/workqueue.h>
+
+#define TUXI_SAFEGUARD_PERIOD 1000      // 1s
+#define TUXI_PWM_FAN_ON_MIN_SPEED 0x40  // ~25%
+#define TUXI_TEMP_LEVEL_HYSTERESIS 1500 // 1.5°C
+#define TUXI_FW_TEMP_OFFSET 2730        // Kelvin to Celsius
+#define TUXI_MAX_FAN_COUNT 16           /*
+					 * If this is increased, new lines must
+					 * be added to hwmcinfo below.
+					 */

The style of these macros could be better.

I suggest using an enum instead. Something like:

	enum TUXI_SAFEGUARD_LIMITS {
		...
	}

with it's values aligned and the comment on top.

Quoted 135 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +135 -0
+
+static const struct acpi_device_id acpi_device_ids[] = {
+	{"TUXI0000", 0},
+	{"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, acpi_device_ids);
+
+struct tuxi_driver_data_t {
+	acpi_handle tfan_handle;
+	struct device *hwmdev;
+};
+
+struct tuxi_hwmon_driver_data_t {
+	struct delayed_work work;
+	struct device *hwmdev;
+	u8 fan_count;
+	const char *fan_types[TUXI_MAX_FAN_COUNT];
+	u8 temp_level[TUXI_MAX_FAN_COUNT];
+	u8 curr_speed[TUXI_MAX_FAN_COUNT];
+	u8 want_speed[TUXI_MAX_FAN_COUNT];
+	u8 pwm_enabled;
+};
+
+struct tuxi_temp_high_config_t {
+	long temp;
+	u8 min_speed;
+};
+
+/*
+ * Speed values in this table must be >= TUXI_PWM_FAN_ON_MIN_SPEED to avoid
+ * undefined behaviour.
+ */
+static const struct tuxi_temp_high_config_t temp_levels[] = {
+	{  80000, 0x4d }, // ~30%
+	{  90000, 0x66 }, // ~40%
+	{ 100000, 0xff }, // 100%
+	{ }
+};
+
+/*
+ * Set fan speed target
+ *
+ * Set a specific fan speed (needs manual mode)
+ *
+ * Arg0: Fan index
+ * Arg1: Fan speed as a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_SPEED		"SSPD"
+
+/*
+ * Get fan speed target
+ *
+ * Arg0: Fan index
+ * Returns: Current fan speed target a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_SPEED		"GSPD"
+
+/*
+ * Get fans count
+ *
+ * Returns: Number of individually controllable fans
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_COUNT		"GCNT"
+
+/*
+ * Set fans mode
+ *
+ * Arg0: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_MODE		"SMOD"
+
+/*
+ * Get fans mode
+ *
+ * Returns: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_MODE		"GMOD"
+
+#define TUXI_TFAN_FAN_MODE_AUTO 0
+#define TUXI_TFAN_FAN_MODE_MANUAL 1
+
+/*
+ * Get fan type/what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: 0 = general, 1 = CPU, 2 = GPU
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TYPE		"GTYP"
+
+static const char * const tuxi_fan_type_labels[] = {
+	"general",
+	"cpu",
+	"gpu"
+};
+
+/*
+ * Get fan temperature/temperature of what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: Temperature sensor value in 10ths of degrees kelvin
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE	"GTMP"
+
+/*
+ * Get actual fan speed in RPM
+ *
+ * Arg0: Fan index
+ * Returns: Speed sensor value in revolutions per minute
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_RPM		"GRPM"
+
+static int tuxi_tfan_method(struct acpi_device *device, acpi_string method,
+			    unsigned long long *params, u32 pcount,
+			    unsigned long long *retval)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+	acpi_handle handle = driver_data->tfan_handle;
+	union acpi_object *obj __free(kfree) = NULL;
+	struct acpi_object_list arguments;
+	unsigned long long data;
+	acpi_status status;
+	unsigned int i;
+
+	if (pcount > 0) {
+		obj = kcalloc(pcount, sizeof(*arguments.pointer), GFP_KERNEL);
+
+		arguments.count = pcount;
+		arguments.pointer = obj;
+		for (i = 0; i < pcount; ++i) {
+			arguments.pointer[i].type = ACPI_TYPE_INTEGER;
+			arguments.pointer[i].integer.value = params[i];
+		}
+	}
+	status = acpi_evaluate_integer(handle, method,
+				       pcount ? &arguments : NULL, &data);

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 169

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
+	if (retval)
+		*retval = data;
+
+	return 0;
+}
+
+static umode_t hwm_is_visible(const void *data, enum hwmon_sensor_types type,

All of these callbacks should be prefixed with "tuxi_".

Quoted 25 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +25 -0
+			      u32 __always_unused attr, int channel)
+{
+	struct tuxi_hwmon_driver_data_t const *driver_data = data;
+
+	if (channel >= driver_data->fan_count)
+		return 0;
+
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	case hwmon_temp:
+		return 0444;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
+		    int channel, long *val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 204

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	int ret;
+
+	switch (type) {
+	case hwmon_fan:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_RPM,
+				       params, 1, &retval);
+		*val = retval > S32_MAX ? S32_MAX : retval;

Use clamp_val().

Also, is an RPM >S32_MAX a bogus value? Should it be treated as an error instead?

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 209

Quoted 5 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +5 -0
+		return ret;
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			if (driver_data->pwm_enabled) {

I think this needs locking.

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 217

Quoted 8 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+				*val = driver_data->curr_speed[channel];
+				return 0;
+			}
+			params[0] = channel;
+			ret = tuxi_tfan_method(pdev,
+					       TUXI_TFAN_METHOD_GET_FAN_SPEED,
+					       params, 1, &retval);
+			*val = retval > S32_MAX ? S32_MAX : retval;

HWMON ABI specifies that the pwm attribute range is 0-255. Are values above this, bogus values?

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 221

Quoted 4 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +4 -0
+			return ret;
+		case hwmon_pwm_enable:
+			*val = driver_data->pwm_enabled;
+			return ret;

ret is uninitialized when used here!

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 230

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +9 -0
+		}
+		break;
+	case hwmon_temp:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		*val = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;

Same comment as the clamped values above.

Additionally, please use deci_kelvin_to_millicelsius() from <linux/units.h> and IMO this should be an standard if-else block.

Quoted 63 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +63 -0
+		return ret;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read_string(struct device *dev,
+			   enum hwmon_sensor_types __always_unused type,
+			   u32 __always_unused attr, int channel,
+			   const char **str)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+
+	*str = driver_data->fan_types[channel];
+
+	return 0;
+}
+
+static int write_speed(struct device *dev, int channel, u8 val, bool force)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long new_speed, params[2];
+	u8 temp_level;
+	int ret;
+
+	params[0] = channel;
+
+	/*
+	 * The heatpipe across the VRMs is shared between both fans and the VRMs
+	 * are the most likely to go up in smoke. So it's better to apply the
+	 * minimum fan speed to all fans if either CPU or GPU is working hard.
+	 */
+	temp_level = max_array(driver_data->temp_level, driver_data->fan_count);
+	if (temp_level)
+		new_speed = max(val, temp_levels[temp_level - 1].min_speed);
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED / 2)
+		new_speed = 0;
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED)
+		new_speed = TUXI_PWM_FAN_ON_MIN_SPEED;
+	else
+		new_speed = val;
+
+	if (force || new_speed != driver_data->curr_speed[channel]) {
+		params[0] = channel;
+		params[1] = new_speed;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_SPEED,
+				       params, 2, NULL);
+		if (ret)
+			return ret;
+	}
+
+	driver_data->curr_speed[channel] = new_speed;
+
+	return 0;
+}
+
+static int hwm_write(struct device *dev,
+		     enum hwmon_sensor_types __always_unused type, u32 attr,
+		     int channel, long val)
+{

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 303

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2];
+	unsigned int i;
+	int ret;
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		if (val > U8_MAX || val < 0)
+			return -EINVAL;

Please, clamp_val() instead.

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 305

Quoted 2 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +2 -0
+
+		if (driver_data->pwm_enabled) {

I believe this needs locking to ensure pwm_enabled hasn't changed.

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 313

Quoted 8 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+			driver_data->want_speed[channel] = val;
+			return write_speed(dev, channel, val, false);
+		}
+
+		return 0;
+	case hwmon_pwm_enable:
+		params[0] = val ? TUXI_TFAN_FAN_MODE_MANUAL :
+				  TUXI_TFAN_FAN_MODE_AUTO;

Per ABI specification, a value of 0 corresponds to fans at full speed, not "auto". A value of 2+ corresponds to auto.

Please, refer to:

https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-hwmon-hwmonx-pwmy-enable

for more details.

Quoted 16 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +16 -0
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE,
+				       params, 1, NULL);
+		if (ret)
+			return ret;
+
+		driver_data->pwm_enabled = val;
+
+		/*
+		 * Activating PWM sets speed to 0. Alternative design decision
+		 * could be to keep the current value. This would require proper
+		 * setting of driver_data->curr_speed for example.
+		 */
+		if (val)
+			for (i = 0; i < driver_data->fan_count; ++i) {
+				ret = write_speed(dev, i, 0, true);
+				if (ret)

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 339

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+					return ret;
+			}
+
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_ops hwmops = {

Prefix this struct with "tuxi_"

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 346

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	.is_visible = hwm_is_visible,
+	.read = hwm_read,
+	.read_string = hwm_read_string,
+	.write = hwm_write,
+};
+
+static const struct hwmon_channel_info * const hwmcinfo[] = {

Same as above.

Quoted 45 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +45 -0
+	HWMON_CHANNEL_INFO(fan,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(pwm,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 401

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+static const struct hwmon_chip_info hwminfo = {

Same as above.

Quoted 19 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +19 -0
+	.ops = &hwmops,
+	.info = hwmcinfo
+};
+
+static u8 tuxi_get_temp_level(struct tuxi_hwmon_driver_data_t *driver_data,
+			      u8 fan_id, long temp)
+{
+	long temp_low, temp_high;
+	unsigned int i;
+	int ret;
+
+	ret = driver_data->temp_level[fan_id];
+
+	for (i = 0; temp_levels[i].temp; ++i) {
+		temp_low = i == 0 ? S32_MIN : temp_levels[i - 1].temp;
+		temp_high = temp_levels[i].temp;
+		if (ret > i)
+			temp_high -= TUXI_TEMP_LEVEL_HYSTERESIS;
+

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 430

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+		if (temp >= temp_low && temp < temp_high)
+			return i;
+	}
+	if (temp >= temp_high)
+		ret = i;
+
+	return ret;
+}
+
+static void tuxi_periodic_hw_safeguard(struct work_struct *work)

I wonder if all of this convoluted logic is really necessary. Why can't you just force TUXI_TFAN_FAN_MODE_AUTO if a certain limit is surpassed?

That would mean fewer CPU instructions that are constantly running.

Quoted 138 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +138 -0
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = container_of(work,
+								    struct tuxi_hwmon_driver_data_t,
+								    work.work);
+	struct device *dev = driver_data->hwmdev;
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	unsigned int i;
+	long temp;
+	int ret;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		/*
+		 * If reading the temperature fails, default to a high value to
+		 * be on the safe side in the worst case.
+		 */
+		if (ret)
+			retval = TUXI_FW_TEMP_OFFSET + 1200;
+
+		temp = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;
+
+		driver_data->temp_level[i] = tuxi_get_temp_level(driver_data, i,
+								 temp);
+	}
+
+	// Reapply want_speeds to respect eventual new temp_levels
+	for (i = 0; i < driver_data->fan_count; ++i)
+		write_speed(dev, i, driver_data->want_speed[i], false);
+
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+}
+
+static int tuxi_hwmon_add_devices(struct acpi_device *pdev, struct device **hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data;
+	unsigned long long params[2], retval;
+	unsigned int i;
+	int ret;
+
+	/*
+	 * The first version of TUXI TFAN didn't have the Get Fan Temperature
+	 * method which is integral to this driver. So probe for existence and
+	 * abort otherwise.
+	 *
+	 * The Get Fan Speed method is also missing in that version, but was
+	 * added in the same version so it doesn't have to be probe separately.
+	 */
+	params[0] = 0;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+			       params, 1, &retval);
+	if (ret)
+		return ret;
+
+	driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	/*
+	 * Loading this module sets the fan mode to auto. Alternative design
+	 * decision could be to keep the current value. This would require
+	 * proper initialization of driver_data->curr_speed for example.
+	 */
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1,
+			       NULL);
+	if (ret)
+		return ret;
+
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_COUNT, NULL, 0,
+			       &retval);
+	if (ret)
+		return ret;
+	if (retval > TUXI_MAX_FAN_COUNT)
+		return -EINVAL;
+	driver_data->fan_count = retval;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TYPE,
+				       params, 1, &retval);
+		if (ret)
+			return ret;
+		if (retval >= ARRAY_SIZE(tuxi_fan_type_labels))
+			return -EOPNOTSUPP;
+		driver_data->fan_types[i] = tuxi_fan_type_labels[retval];
+	}
+
+	*hwmdev = devm_hwmon_device_register_with_info(&pdev->dev,
+						       "tuxedo_nbxx_acpi_tuxi",
+						       driver_data, &hwminfo,
+						       NULL);
+	if (IS_ERR(*hwmdev))
+		return PTR_ERR(*hwmdev);
+
+	driver_data->hwmdev = *hwmdev;
+
+	INIT_DELAYED_WORK(&driver_data->work, tuxi_periodic_hw_safeguard);
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+
+	return 0;
+}
+
+static void tuxi_hwmon_remove_devices(struct device *hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(hwmdev);
+	struct acpi_device *pdev = to_acpi_device(hwmdev->parent);
+	unsigned long long params[2];
+
+	cancel_delayed_work_sync(&driver_data->work);
+
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1, NULL);
+}
+
+static int tuxi_add(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data;
+	acpi_status status;
+
+	driver_data = devm_kzalloc(&device->dev, sizeof(*driver_data),
+				   GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	// Find subdevices
+	status = acpi_get_handle(device->handle, "TFAN",
+				 &driver_data->tfan_handle);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
+	dev_set_drvdata(&device->dev, driver_data);
+
+	return tuxi_hwmon_add_devices(device, &driver_data->hwmdev);

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 578

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+}
+
+static void tuxi_remove(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+
+	tuxi_hwmon_remove_devices(driver_data->hwmdev);
+}
+
+static struct acpi_driver acpi_driver = {

AFAIK platform drivers are *strongly* preferred over acpi drivers. You should use that instead.

For an example check the samsung-galaxybook driver in the for-next branch.

Signature · 1 line
 ~ Kurt
Quoted 13 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +13 -0
+	.name = "tuxedo_nbxx_acpi_tuxi",
+	.ids = acpi_device_ids,
+	.ops = {
+		.add = tuxi_add,
+		.remove = tuxi_remove,
+	},
+};
+
+module_acpi_driver(acpi_driver);
+
+MODULE_DESCRIPTION("Fan control for TUXEDO devices using the TUXI ACPI device");
+MODULE_AUTHOR("Werner Sembach <[email protected]>");
+MODULE_LICENSE("GPL");
author maintainer
#3 lore raw

Hi Kurt,

Am 18.03.25 um 06:32 schrieb Kurt Borja:

Quoted 66 lines

Hi Werner,

Quoted 63 lines · Werner Sembach

The TUXEDO Sirius 16 Gen1 & Gen2 have the custom TUXEDO Interface (TUXI) ACPI interface which currently consists of the TFAN device. This has ACPI functions to control the built in fans and monitor fan speeds and CPU and GPU temperature.

This driver implements this TFAN device via the hwmon subsystem with an added temperature check that ensure a minimum fanspeed at certain temperatures. This allows userspace controlled, but hardware safe, custom fan curves.

---
  MAINTAINERS                                  |   6 +
  drivers/platform/x86/Kconfig                 |   2 +
  drivers/platform/x86/Makefile                |   3 +
  drivers/platform/x86/tuxedo/Kbuild           |   8 +
  drivers/platform/x86/tuxedo/Kconfig          |   8 +
  drivers/platform/x86/tuxedo/nbxx/Kbuild      |   9 +
  drivers/platform/x86/tuxedo/nbxx/Kconfig     |  15 +
  drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c | 591 +++++++++++++++++++
  8 files changed, 642 insertions(+)

create mode 100644 drivers/platform/x86/tuxedo/Kbuild create mode 100644 drivers/platform/x86/tuxedo/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kbuild create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c

MAINTAINERS +6 -0
index 8e0736dc2ee0e..7139c32e96dc7 100644
@@ -24190,6 +24190,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
  F:	tools/power/x86/turbostat/
  F:	tools/testing/selftests/turbostat/
 
+TUXEDO DRIVERS
+M:	Werner Sembach <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/platform/x86/tuxedo/
+
  TW5864 VIDEO4LINUX DRIVER
  M:	Bluecherry Maintainers <[email protected]>
  M:	Andrey Utkin <[email protected]>
drivers/platform/x86/Kconfig +2 -0
index 0258dd879d64b..58b258cde4fdb 100644
@@ -1186,6 +1186,8 @@ config SEL3350_PLATFORM
  	  To compile this driver as a module, choose M here: the module
  	  will be called sel3350-platform.
 
+source "drivers/platform/x86/tuxedo/Kconfig"
+
  endif # X86_PLATFORM_DEVICES
 
  config P2SB
drivers/platform/x86/Makefile +0 -0
index e1b1429470674..1562dcd7ad9a5 100644
@@ -153,3 +153,6 @@ obj-$(CONFIG_WINMATE_FM07_KEYS)		+= winmate-fm07-keys.o
 
Quoted 7 lines

drivers/platform/x86/tuxedo/Kbuild

Quoted 6 lines
drivers/platform/x86/Makefile +3 -0
  # SEL
  obj-$(CONFIG_SEL3350_PLATFORM)		+= sel3350-platform.o
+
+# TUXEDO
+obj-y					+= tuxedo/
drivers/platform/x86/tuxedo/Kbuild +0 -0

I think this should be named Makefile instead of Kbuild.

Ack

I thought since I did not use anything that is makefile syntax I should use Kbuild, but I looked it up and found this: https://www.kernel.org/doc/html/v6.14-rc7/kbuild/makefiles.html#the-kbuild-files

Quoted 22 lines
Quoted 22 lines

new file mode 100644
index 0000000000000..dc55b403f201d

drivers/platform/x86/tuxedo/Kbuild new file +8 -0
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+obj-y	+= nbxx/
drivers/platform/x86/tuxedo/Kconfig new file +2 -0
new file mode 100644
index 0000000000000..1b22a0b29460a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/Kbuild

Quoted 7 lines
drivers/platform/x86/tuxedo/Kconfig new file +6 -0
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+source "drivers/platform/x86/tuxedo/nbxx/Kconfig"
drivers/platform/x86/tuxedo/nbxx/Kbuild +0 -0

Same as above.

Ack

Quoted 68 lines
Quoted 56 lines

new file mode 100644
index 0000000000000..256b03921c732

drivers/platform/x86/tuxedo/nbxx/Kbuild new file +9 -0
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+tuxedo_nbxx_acpi_tuxi-y			:= acpi_tuxi.o
+obj-$(CONFIG_TUXEDO_NBXX_ACPI_TUXI)	+= tuxedo_nbxx_acpi_tuxi.o
drivers/platform/x86/tuxedo/nbxx/Kconfig new file +15 -0
new file mode 100644
index 0000000000000..0d011c0c715a5
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+config TUXEDO_NBXX_ACPI_TUXI
+	tristate "TUXEDO NBxx ACPI TUXI Platform Driver"
+	help
+	  This driver implements the ACPI TUXI device found on some TUXEDO
+	  Notebooks. Currently this consists only of the TFAN subdevice which is
+	  implemented via hwmon.
+
+	  When compiled as a module it will be called tuxedo_nbxx_acpi_tuxi
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +14 -0
new file mode 100644
index 0000000000000..bb452cc33568a
@@ -0,0 +1,591 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024-2025 Werner Sembach [email protected]
+ */
+
+#include <linux/acpi.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/units.h>

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 24

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+#include <linux/workqueue.h>
+
+#define TUXI_SAFEGUARD_PERIOD 1000      // 1s
+#define TUXI_PWM_FAN_ON_MIN_SPEED 0x40  // ~25%
+#define TUXI_TEMP_LEVEL_HYSTERESIS 1500 // 1.5°C
+#define TUXI_FW_TEMP_OFFSET 2730        // Kelvin to Celsius
+#define TUXI_MAX_FAN_COUNT 16           /*
+					 * If this is increased, new lines must
+					 * be added to hwmcinfo below.
+					 */

The style of these macros could be better.

Quoted 5 lines

I suggest using an enum instead. Something like:

	enum TUXI_SAFEGUARD_LIMITS {
		...
	}

Since these values are all on very different scales an enum feels somehow wrong for me here.

Quoted 2 lines

with it's values aligned and the comment on top.

Ack

Quoted 139 lines
Quoted 139 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +138 -0
+
+static const struct acpi_device_id acpi_device_ids[] = {
+	{"TUXI0000", 0},
+	{"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, acpi_device_ids);
+
+struct tuxi_driver_data_t {
+	acpi_handle tfan_handle;
+	struct device *hwmdev;
+};
+
+struct tuxi_hwmon_driver_data_t {
+	struct delayed_work work;
+	struct device *hwmdev;
+	u8 fan_count;
+	const char *fan_types[TUXI_MAX_FAN_COUNT];
+	u8 temp_level[TUXI_MAX_FAN_COUNT];
+	u8 curr_speed[TUXI_MAX_FAN_COUNT];
+	u8 want_speed[TUXI_MAX_FAN_COUNT];
+	u8 pwm_enabled;
+};
+
+struct tuxi_temp_high_config_t {
+	long temp;
+	u8 min_speed;
+};
+
+/*
+ * Speed values in this table must be >= TUXI_PWM_FAN_ON_MIN_SPEED to avoid
+ * undefined behaviour.
+ */
+static const struct tuxi_temp_high_config_t temp_levels[] = {
+	{  80000, 0x4d }, // ~30%
+	{  90000, 0x66 }, // ~40%
+	{ 100000, 0xff }, // 100%
+	{ }
+};
+
+/*
+ * Set fan speed target
+ *
+ * Set a specific fan speed (needs manual mode)
+ *
+ * Arg0: Fan index
+ * Arg1: Fan speed as a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_SPEED		"SSPD"
+
+/*
+ * Get fan speed target
+ *
+ * Arg0: Fan index
+ * Returns: Current fan speed target a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_SPEED		"GSPD"
+
+/*
+ * Get fans count
+ *
+ * Returns: Number of individually controllable fans
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_COUNT		"GCNT"
+
+/*
+ * Set fans mode
+ *
+ * Arg0: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_MODE		"SMOD"
+
+/*
+ * Get fans mode
+ *
+ * Returns: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_MODE		"GMOD"
+
+#define TUXI_TFAN_FAN_MODE_AUTO 0
+#define TUXI_TFAN_FAN_MODE_MANUAL 1
+
+/*
+ * Get fan type/what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: 0 = general, 1 = CPU, 2 = GPU
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TYPE		"GTYP"
+
+static const char * const tuxi_fan_type_labels[] = {
+	"general",
+	"cpu",
+	"gpu"
+};
+
+/*
+ * Get fan temperature/temperature of what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: Temperature sensor value in 10ths of degrees kelvin
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE	"GTMP"
+
+/*
+ * Get actual fan speed in RPM
+ *
+ * Arg0: Fan index
+ * Returns: Speed sensor value in revolutions per minute
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_RPM		"GRPM"
+
+static int tuxi_tfan_method(struct acpi_device *device, acpi_string method,
+			    unsigned long long *params, u32 pcount,
+			    unsigned long long *retval)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+	acpi_handle handle = driver_data->tfan_handle;
+	union acpi_object *obj __free(kfree) = NULL;
+	struct acpi_object_list arguments;
+	unsigned long long data;
+	acpi_status status;
+	unsigned int i;
+
+	if (pcount > 0) {
+		obj = kcalloc(pcount, sizeof(*arguments.pointer), GFP_KERNEL);
+
+		arguments.count = pcount;
+		arguments.pointer = obj;
+		for (i = 0; i < pcount; ++i) {
+			arguments.pointer[i].type = ACPI_TYPE_INTEGER;
+			arguments.pointer[i].integer.value = params[i];
+		}
+	}
+	status = acpi_evaluate_integer(handle, method,
+				       pcount ? &arguments : NULL, &data);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 169

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	if (retval)
+		*retval = data;
+
+	return 0;
+}
+
+static umode_t hwm_is_visible(const void *data, enum hwmon_sensor_types type,

All of these callbacks should be prefixed with "tuxi_".

Ack.

Quoted 29 lines
Quoted 29 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +28 -0
+			      u32 __always_unused attr, int channel)
+{
+	struct tuxi_hwmon_driver_data_t const *driver_data = data;
+
+	if (channel >= driver_data->fan_count)
+		return 0;
+
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	case hwmon_temp:
+		return 0444;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
+		    int channel, long *val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	int ret;
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 204

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+
+	switch (type) {
+	case hwmon_fan:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_RPM,
+				       params, 1, &retval);
+		*val = retval > S32_MAX ? S32_MAX : retval;

Use clamp_val().

Thanks for the tip

Quoted 3 lines

Also, is an RPM >S32_MAX a bogus value? Should it be treated as an error instead?

Yeah sounds good, will do.

Quoted 7 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 209

Quoted 6 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +5 -0
+		return ret;
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			if (driver_data->pwm_enabled) {

I think this needs locking.

Since this was only meant as a small performance optimization if wonder if I shouldn't just skip the if-case and just ask the wmi every time instead?

Quoted 11 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 217

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+				*val = driver_data->curr_speed[channel];
+				return 0;
+			}
+			params[0] = channel;
+			ret = tuxi_tfan_method(pdev,
+					       TUXI_TFAN_METHOD_GET_FAN_SPEED,
+					       params, 1, &retval);
+			*val = retval > S32_MAX ? S32_MAX : retval;

HWMON ABI specifies that the pwm attribute range is 0-255. Are values above this, bogus values?

Didn't know, but ofc I can change the check here.

Quoted 6 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 221

Quoted 5 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +4 -0
+			return ret;
+		case hwmon_pwm_enable:
+			*val = driver_data->pwm_enabled;
+			return ret;

ret is uninitialized when used here!

Sorry, will fix.

Quoted 12 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 230

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +9 -0
+		}
+		break;
+	case hwmon_temp:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		*val = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;

Same comment as the clamped values above.

Quoted 2 lines

Additionally, please use deci_kelvin_to_millicelsius() from <linux/units.h> and IMO this should be an standard if-else block.

Because of firmware weirdness I can't use deci_kelvin_to_millicelsius: While the firmware claims to output 10th kelvin it actually outputs only full degrees with the last number always being 0, that's why I defined the custom TUXI_FW_TEMP_OFFSET as 2730 instead of 2731.

I could use deci_kelvin_to_celsius()*MILLIDEGREE_PER_DEGREE, but this would introduce an unnecessary rounding step.

Quoted 67 lines
Quoted 67 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +66 -0
+		return ret;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read_string(struct device *dev,
+			   enum hwmon_sensor_types __always_unused type,
+			   u32 __always_unused attr, int channel,
+			   const char **str)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+
+	*str = driver_data->fan_types[channel];
+
+	return 0;
+}
+
+static int write_speed(struct device *dev, int channel, u8 val, bool force)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long new_speed, params[2];
+	u8 temp_level;
+	int ret;
+
+	params[0] = channel;
+
+	/*
+	 * The heatpipe across the VRMs is shared between both fans and the VRMs
+	 * are the most likely to go up in smoke. So it's better to apply the
+	 * minimum fan speed to all fans if either CPU or GPU is working hard.
+	 */
+	temp_level = max_array(driver_data->temp_level, driver_data->fan_count);
+	if (temp_level)
+		new_speed = max(val, temp_levels[temp_level - 1].min_speed);
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED / 2)
+		new_speed = 0;
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED)
+		new_speed = TUXI_PWM_FAN_ON_MIN_SPEED;
+	else
+		new_speed = val;
+
+	if (force || new_speed != driver_data->curr_speed[channel]) {
+		params[0] = channel;
+		params[1] = new_speed;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_SPEED,
+				       params, 2, NULL);
+		if (ret)
+			return ret;
+	}
+
+	driver_data->curr_speed[channel] = new_speed;
+
+	return 0;
+}
+
+static int hwm_write(struct device *dev,
+		     enum hwmon_sensor_types __always_unused type, u32 attr,
+		     int channel, long val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2];
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 303

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	unsigned int i;
+	int ret;
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		if (val > U8_MAX || val < 0)
+			return -EINVAL;

Please, clamp_val() instead.

ack

Quoted 4 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 305

Quoted 3 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +2 -0
+
+		if (driver_data->pwm_enabled) {

I believe this needs locking to ensure pwm_enabled hasn't changed.

ok

Quoted 12 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 313

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+			driver_data->want_speed[channel] = val;
+			return write_speed(dev, channel, val, false);
+		}
+
+		return 0;
+	case hwmon_pwm_enable:
+		params[0] = val ? TUXI_TFAN_FAN_MODE_MANUAL :
+				  TUXI_TFAN_FAN_MODE_AUTO;

Per ABI specification, a value of 0 corresponds to fans at full speed, not "auto". A value of 2+ corresponds to auto.

Quoted 5 lines

Please, refer to:

https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-hwmon-hwmonx-pwmy-enable

for more details.

Thanks for the link, will fix

Quoted 20 lines
Quoted 20 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +19 -0
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE,
+				       params, 1, NULL);
+		if (ret)
+			return ret;
+
+		driver_data->pwm_enabled = val;
+
+		/*
+		 * Activating PWM sets speed to 0. Alternative design decision
+		 * could be to keep the current value. This would require proper
+		 * setting of driver_data->curr_speed for example.
+		 */
+		if (val)
+			for (i = 0; i < driver_data->fan_count; ++i) {
+				ret = write_speed(dev, i, 0, true);
+				if (ret)
+					return ret;
+			}
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 339

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_ops hwmops = {

Prefix this struct with "tuxi_"

ok

Quoted 9 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 346

Quoted 8 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	.is_visible = hwm_is_visible,
+	.read = hwm_read,
+	.read_string = hwm_read_string,
+	.write = hwm_write,
+};
+
+static const struct hwmon_channel_info * const hwmcinfo[] = {

Same as above.

ok

Quoted 49 lines
Quoted 49 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +48 -0
+	HWMON_CHANNEL_INFO(fan,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(pwm,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 401

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+static const struct hwmon_chip_info hwminfo = {

Same as above.

ok

Quoted 24 lines
Quoted 24 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +23 -0
+	.ops = &hwmops,
+	.info = hwmcinfo
+};
+
+static u8 tuxi_get_temp_level(struct tuxi_hwmon_driver_data_t *driver_data,
+			      u8 fan_id, long temp)
+{
+	long temp_low, temp_high;
+	unsigned int i;
+	int ret;
+
+	ret = driver_data->temp_level[fan_id];
+
+	for (i = 0; temp_levels[i].temp; ++i) {
+		temp_low = i == 0 ? S32_MIN : temp_levels[i - 1].temp;
+		temp_high = temp_levels[i].temp;
+		if (ret > i)
+			temp_high -= TUXI_TEMP_LEVEL_HYSTERESIS;
+
+		if (temp >= temp_low && temp < temp_high)
+			return i;
+	}
+	if (temp >= temp_high)
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 430

Quoted 6 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +6 -0
+		ret = i;
+
+	return ret;
+}
+
+static void tuxi_periodic_hw_safeguard(struct work_struct *work)

I wonder if all of this convoluted logic is really necessary. Why can't you just force TUXI_TFAN_FAN_MODE_AUTO if a certain limit is surpassed?

The auto mode, aka the built in fan curve is way more aggressive than this, and reaching 80°C is very easy on this device.

Quoted 145 lines

That would mean fewer CPU instructions that are constantly running.

Quoted 142 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +142 -0
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = container_of(work,
+								    struct tuxi_hwmon_driver_data_t,
+								    work.work);
+	struct device *dev = driver_data->hwmdev;
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	unsigned int i;
+	long temp;
+	int ret;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		/*
+		 * If reading the temperature fails, default to a high value to
+		 * be on the safe side in the worst case.
+		 */
+		if (ret)
+			retval = TUXI_FW_TEMP_OFFSET + 1200;
+
+		temp = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;
+
+		driver_data->temp_level[i] = tuxi_get_temp_level(driver_data, i,
+								 temp);
+	}
+
+	// Reapply want_speeds to respect eventual new temp_levels
+	for (i = 0; i < driver_data->fan_count; ++i)
+		write_speed(dev, i, driver_data->want_speed[i], false);
+
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+}
+
+static int tuxi_hwmon_add_devices(struct acpi_device *pdev, struct device **hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data;
+	unsigned long long params[2], retval;
+	unsigned int i;
+	int ret;
+
+	/*
+	 * The first version of TUXI TFAN didn't have the Get Fan Temperature
+	 * method which is integral to this driver. So probe for existence and
+	 * abort otherwise.
+	 *
+	 * The Get Fan Speed method is also missing in that version, but was
+	 * added in the same version so it doesn't have to be probe separately.
+	 */
+	params[0] = 0;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+			       params, 1, &retval);
+	if (ret)
+		return ret;
+
+	driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	/*
+	 * Loading this module sets the fan mode to auto. Alternative design
+	 * decision could be to keep the current value. This would require
+	 * proper initialization of driver_data->curr_speed for example.
+	 */
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1,
+			       NULL);
+	if (ret)
+		return ret;
+
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_COUNT, NULL, 0,
+			       &retval);
+	if (ret)
+		return ret;
+	if (retval > TUXI_MAX_FAN_COUNT)
+		return -EINVAL;
+	driver_data->fan_count = retval;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TYPE,
+				       params, 1, &retval);
+		if (ret)
+			return ret;
+		if (retval >= ARRAY_SIZE(tuxi_fan_type_labels))
+			return -EOPNOTSUPP;
+		driver_data->fan_types[i] = tuxi_fan_type_labels[retval];
+	}
+
+	*hwmdev = devm_hwmon_device_register_with_info(&pdev->dev,
+						       "tuxedo_nbxx_acpi_tuxi",
+						       driver_data, &hwminfo,
+						       NULL);
+	if (IS_ERR(*hwmdev))
+		return PTR_ERR(*hwmdev);
+
+	driver_data->hwmdev = *hwmdev;
+
+	INIT_DELAYED_WORK(&driver_data->work, tuxi_periodic_hw_safeguard);
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+
+	return 0;
+}
+
+static void tuxi_hwmon_remove_devices(struct device *hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(hwmdev);
+	struct acpi_device *pdev = to_acpi_device(hwmdev->parent);
+	unsigned long long params[2];
+
+	cancel_delayed_work_sync(&driver_data->work);
+
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1, NULL);
+}
+
+static int tuxi_add(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data;
+	acpi_status status;
+
+	driver_data = devm_kzalloc(&device->dev, sizeof(*driver_data),
+				   GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	// Find subdevices
+	status = acpi_get_handle(device->handle, "TFAN",
+				 &driver_data->tfan_handle);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
+	dev_set_drvdata(&device->dev, driver_data);
+
+	return tuxi_hwmon_add_devices(device, &driver_data->hwmdev);
+}
+
+static void tuxi_remove(struct acpi_device *device)
+{
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 578

Quoted 6 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +6 -0
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+
+	tuxi_hwmon_remove_devices(driver_data->hwmdev);
+}
+
+static struct acpi_driver acpi_driver = {

AFAIK platform drivers are *strongly* preferred over acpi drivers. You should use that instead.

Sorry, first time reading this. Can change it ofc, but is there a specific reason?

Best regards,

Werner

Quoted 4 lines

For an example check the samsung-galaxybook driver in the for-next branch.

#4 lore raw
Quoted 182 lines · Werner Sembach

Hi Kurt,

Am 18.03.25 um 06:32 schrieb Kurt Borja:

Quoted 66 lines

Hi Werner,

Quoted 63 lines · Werner Sembach

The TUXEDO Sirius 16 Gen1 & Gen2 have the custom TUXEDO Interface (TUXI) ACPI interface which currently consists of the TFAN device. This has ACPI functions to control the built in fans and monitor fan speeds and CPU and GPU temperature.

This driver implements this TFAN device via the hwmon subsystem with an added temperature check that ensure a minimum fanspeed at certain temperatures. This allows userspace controlled, but hardware safe, custom fan curves.

---
  MAINTAINERS                                  |   6 +
  drivers/platform/x86/Kconfig                 |   2 +
  drivers/platform/x86/Makefile                |   3 +
  drivers/platform/x86/tuxedo/Kbuild           |   8 +
  drivers/platform/x86/tuxedo/Kconfig          |   8 +
  drivers/platform/x86/tuxedo/nbxx/Kbuild      |   9 +
  drivers/platform/x86/tuxedo/nbxx/Kconfig     |  15 +
  drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c | 591 +++++++++++++++++++
  8 files changed, 642 insertions(+)

create mode 100644 drivers/platform/x86/tuxedo/Kbuild create mode 100644 drivers/platform/x86/tuxedo/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kbuild create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c

MAINTAINERS +6 -0
index 8e0736dc2ee0e..7139c32e96dc7 100644
@@ -24190,6 +24190,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
  F:	tools/power/x86/turbostat/
  F:	tools/testing/selftests/turbostat/
 
+TUXEDO DRIVERS
+M:	Werner Sembach <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/platform/x86/tuxedo/
+
  TW5864 VIDEO4LINUX DRIVER
  M:	Bluecherry Maintainers <[email protected]>
  M:	Andrey Utkin <[email protected]>
drivers/platform/x86/Kconfig +2 -0
index 0258dd879d64b..58b258cde4fdb 100644
@@ -1186,6 +1186,8 @@ config SEL3350_PLATFORM
  	  To compile this driver as a module, choose M here: the module
  	  will be called sel3350-platform.
 
+source "drivers/platform/x86/tuxedo/Kconfig"
+
  endif # X86_PLATFORM_DEVICES
 
  config P2SB
drivers/platform/x86/Makefile +0 -0
index e1b1429470674..1562dcd7ad9a5 100644
@@ -153,3 +153,6 @@ obj-$(CONFIG_WINMATE_FM07_KEYS)		+= winmate-fm07-keys.o
 
Quoted 7 lines

drivers/platform/x86/tuxedo/Kbuild

Quoted 6 lines
drivers/platform/x86/Makefile +3 -0
  # SEL
  obj-$(CONFIG_SEL3350_PLATFORM)		+= sel3350-platform.o
+
+# TUXEDO
+obj-y					+= tuxedo/
drivers/platform/x86/tuxedo/Kbuild +0 -0

I think this should be named Makefile instead of Kbuild.

Ack

I thought since I did not use anything that is makefile syntax I should use Kbuild, but I looked it up and found this:
https://www.kernel.org/doc/html/v6.14-rc7/kbuild/makefiles.html#the-kbuild-files

Quoted 22 lines
Quoted 22 lines

new file mode 100644
index 0000000000000..dc55b403f201d

drivers/platform/x86/tuxedo/Kbuild new file +8 -0
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+obj-y	+= nbxx/
drivers/platform/x86/tuxedo/Kconfig new file +2 -0
new file mode 100644
index 0000000000000..1b22a0b29460a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/Kbuild

Quoted 7 lines
drivers/platform/x86/tuxedo/Kconfig new file +6 -0
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+source "drivers/platform/x86/tuxedo/nbxx/Kconfig"
drivers/platform/x86/tuxedo/nbxx/Kbuild +0 -0

Same as above.

Ack

Quoted 68 lines
Quoted 56 lines

new file mode 100644
index 0000000000000..256b03921c732

drivers/platform/x86/tuxedo/nbxx/Kbuild new file +9 -0
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+tuxedo_nbxx_acpi_tuxi-y			:= acpi_tuxi.o
+obj-$(CONFIG_TUXEDO_NBXX_ACPI_TUXI)	+= tuxedo_nbxx_acpi_tuxi.o
drivers/platform/x86/tuxedo/nbxx/Kconfig new file +15 -0
new file mode 100644
index 0000000000000..0d011c0c715a5
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+config TUXEDO_NBXX_ACPI_TUXI
+	tristate "TUXEDO NBxx ACPI TUXI Platform Driver"
+	help
+	  This driver implements the ACPI TUXI device found on some TUXEDO
+	  Notebooks. Currently this consists only of the TFAN subdevice which is
+	  implemented via hwmon.
+
+	  When compiled as a module it will be called tuxedo_nbxx_acpi_tuxi
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +14 -0
new file mode 100644
index 0000000000000..bb452cc33568a
@@ -0,0 +1,591 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024-2025 Werner Sembach [email protected]
+ */
+
+#include <linux/acpi.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/units.h>

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 24

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+#include <linux/workqueue.h>
+
+#define TUXI_SAFEGUARD_PERIOD 1000      // 1s
+#define TUXI_PWM_FAN_ON_MIN_SPEED 0x40  // ~25%
+#define TUXI_TEMP_LEVEL_HYSTERESIS 1500 // 1.5°C
+#define TUXI_FW_TEMP_OFFSET 2730        // Kelvin to Celsius
+#define TUXI_MAX_FAN_COUNT 16           /*
+					 * If this is increased, new lines must
+					 * be added to hwmcinfo below.
+					 */

The style of these macros could be better.

Quoted 7 lines
Quoted 5 lines

I suggest using an enum instead. Something like:

	enum TUXI_SAFEGUARD_LIMITS {
		...
	}

Since these values are all on very different scales an enum feels somehow wrong for me here.

I agree.

Quoted 194 lines
Quoted 2 lines

with it's values aligned and the comment on top.

Ack

Quoted 139 lines
Quoted 139 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +138 -0
+
+static const struct acpi_device_id acpi_device_ids[] = {
+	{"TUXI0000", 0},
+	{"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, acpi_device_ids);
+
+struct tuxi_driver_data_t {
+	acpi_handle tfan_handle;
+	struct device *hwmdev;
+};
+
+struct tuxi_hwmon_driver_data_t {
+	struct delayed_work work;
+	struct device *hwmdev;
+	u8 fan_count;
+	const char *fan_types[TUXI_MAX_FAN_COUNT];
+	u8 temp_level[TUXI_MAX_FAN_COUNT];
+	u8 curr_speed[TUXI_MAX_FAN_COUNT];
+	u8 want_speed[TUXI_MAX_FAN_COUNT];
+	u8 pwm_enabled;
+};
+
+struct tuxi_temp_high_config_t {
+	long temp;
+	u8 min_speed;
+};
+
+/*
+ * Speed values in this table must be >= TUXI_PWM_FAN_ON_MIN_SPEED to avoid
+ * undefined behaviour.
+ */
+static const struct tuxi_temp_high_config_t temp_levels[] = {
+	{  80000, 0x4d }, // ~30%
+	{  90000, 0x66 }, // ~40%
+	{ 100000, 0xff }, // 100%
+	{ }
+};
+
+/*
+ * Set fan speed target
+ *
+ * Set a specific fan speed (needs manual mode)
+ *
+ * Arg0: Fan index
+ * Arg1: Fan speed as a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_SPEED		"SSPD"
+
+/*
+ * Get fan speed target
+ *
+ * Arg0: Fan index
+ * Returns: Current fan speed target a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_SPEED		"GSPD"
+
+/*
+ * Get fans count
+ *
+ * Returns: Number of individually controllable fans
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_COUNT		"GCNT"
+
+/*
+ * Set fans mode
+ *
+ * Arg0: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_MODE		"SMOD"
+
+/*
+ * Get fans mode
+ *
+ * Returns: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_MODE		"GMOD"
+
+#define TUXI_TFAN_FAN_MODE_AUTO 0
+#define TUXI_TFAN_FAN_MODE_MANUAL 1
+
+/*
+ * Get fan type/what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: 0 = general, 1 = CPU, 2 = GPU
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TYPE		"GTYP"
+
+static const char * const tuxi_fan_type_labels[] = {
+	"general",
+	"cpu",
+	"gpu"
+};
+
+/*
+ * Get fan temperature/temperature of what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: Temperature sensor value in 10ths of degrees kelvin
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE	"GTMP"
+
+/*
+ * Get actual fan speed in RPM
+ *
+ * Arg0: Fan index
+ * Returns: Speed sensor value in revolutions per minute
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_RPM		"GRPM"
+
+static int tuxi_tfan_method(struct acpi_device *device, acpi_string method,
+			    unsigned long long *params, u32 pcount,
+			    unsigned long long *retval)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+	acpi_handle handle = driver_data->tfan_handle;
+	union acpi_object *obj __free(kfree) = NULL;
+	struct acpi_object_list arguments;
+	unsigned long long data;
+	acpi_status status;
+	unsigned int i;
+
+	if (pcount > 0) {
+		obj = kcalloc(pcount, sizeof(*arguments.pointer), GFP_KERNEL);
+
+		arguments.count = pcount;
+		arguments.pointer = obj;
+		for (i = 0; i < pcount; ++i) {
+			arguments.pointer[i].type = ACPI_TYPE_INTEGER;
+			arguments.pointer[i].integer.value = params[i];
+		}
+	}
+	status = acpi_evaluate_integer(handle, method,
+				       pcount ? &arguments : NULL, &data);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 169

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	if (retval)
+		*retval = data;
+
+	return 0;
+}
+
+static umode_t hwm_is_visible(const void *data, enum hwmon_sensor_types type,

All of these callbacks should be prefixed with "tuxi_".

Ack.

Quoted 29 lines
Quoted 29 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +28 -0
+			      u32 __always_unused attr, int channel)
+{
+	struct tuxi_hwmon_driver_data_t const *driver_data = data;
+
+	if (channel >= driver_data->fan_count)
+		return 0;
+
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	case hwmon_temp:
+		return 0444;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
+		    int channel, long *val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	int ret;
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 204

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+
+	switch (type) {
+	case hwmon_fan:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_RPM,
+				       params, 1, &retval);
+		*val = retval > S32_MAX ? S32_MAX : retval;

Use clamp_val().

Thanks for the tip

Quoted 3 lines

Also, is an RPM >S32_MAX a bogus value? Should it be treated as an error instead?

Yeah sounds good, will do.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 209

Quoted 5 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +5 -0
+		return ret;
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			if (driver_data->pwm_enabled) {

I think this needs locking.

Since this was only meant as a small performance optimization if wonder if I shouldn't just skip the if-case and just ask the wmi every time instead?

IMO, yes. I don't think it's necessary to cache the value either, as you could just retrieve it every time. That way you avoid having to provide locking.

Quoted 31 lines
Quoted 11 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 217

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+				*val = driver_data->curr_speed[channel];
+				return 0;
+			}
+			params[0] = channel;
+			ret = tuxi_tfan_method(pdev,
+					       TUXI_TFAN_METHOD_GET_FAN_SPEED,
+					       params, 1, &retval);
+			*val = retval > S32_MAX ? S32_MAX : retval;

HWMON ABI specifies that the pwm attribute range is 0-255. Are values above this, bogus values?

Didn't know, but ofc I can change the check here.

Quoted 6 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 221

Quoted 5 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +4 -0
+			return ret;
+		case hwmon_pwm_enable:
+			*val = driver_data->pwm_enabled;
+			return ret;

ret is uninitialized when used here!

Sorry, will fix.

Quoted 12 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 230

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +9 -0
+		}
+		break;
+	case hwmon_temp:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		*val = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;

Same comment as the clamped values above.

Quoted 7 lines
Quoted 2 lines

Additionally, please use deci_kelvin_to_millicelsius() from <linux/units.h> and IMO this should be an standard if-else block.

Because of firmware weirdness I can't use deci_kelvin_to_millicelsius: While the firmware claims to output 10th kelvin it actually outputs only full degrees with the last number always being 0, that's why I defined the custom TUXI_FW_TEMP_OFFSET as 2730 instead of 2731.

Then you can use deci_kelvin_to_millicelsius_with_offset() and provide your own offset.

Quoted 2 lines

I could use deci_kelvin_to_celsius()*MILLIDEGREE_PER_DEGREE, but this would

deci_kelvin_to_millicelsius() avoids rounding and makes that multiplication unnecessary.

Quoted 230 lines

introduce an unnecessary rounding step.

Quoted 67 lines
Quoted 67 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +66 -0
+		return ret;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read_string(struct device *dev,
+			   enum hwmon_sensor_types __always_unused type,
+			   u32 __always_unused attr, int channel,
+			   const char **str)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+
+	*str = driver_data->fan_types[channel];
+
+	return 0;
+}
+
+static int write_speed(struct device *dev, int channel, u8 val, bool force)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long new_speed, params[2];
+	u8 temp_level;
+	int ret;
+
+	params[0] = channel;
+
+	/*
+	 * The heatpipe across the VRMs is shared between both fans and the VRMs
+	 * are the most likely to go up in smoke. So it's better to apply the
+	 * minimum fan speed to all fans if either CPU or GPU is working hard.
+	 */
+	temp_level = max_array(driver_data->temp_level, driver_data->fan_count);
+	if (temp_level)
+		new_speed = max(val, temp_levels[temp_level - 1].min_speed);
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED / 2)
+		new_speed = 0;
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED)
+		new_speed = TUXI_PWM_FAN_ON_MIN_SPEED;
+	else
+		new_speed = val;
+
+	if (force || new_speed != driver_data->curr_speed[channel]) {
+		params[0] = channel;
+		params[1] = new_speed;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_SPEED,
+				       params, 2, NULL);
+		if (ret)
+			return ret;
+	}
+
+	driver_data->curr_speed[channel] = new_speed;
+
+	return 0;
+}
+
+static int hwm_write(struct device *dev,
+		     enum hwmon_sensor_types __always_unused type, u32 attr,
+		     int channel, long val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2];
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 303

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	unsigned int i;
+	int ret;
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		if (val > U8_MAX || val < 0)
+			return -EINVAL;

Please, clamp_val() instead.

ack

Quoted 4 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 305

Quoted 3 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +2 -0
+
+		if (driver_data->pwm_enabled) {

I believe this needs locking to ensure pwm_enabled hasn't changed.

ok

Quoted 12 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 313

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+			driver_data->want_speed[channel] = val;
+			return write_speed(dev, channel, val, false);
+		}
+
+		return 0;
+	case hwmon_pwm_enable:
+		params[0] = val ? TUXI_TFAN_FAN_MODE_MANUAL :
+				  TUXI_TFAN_FAN_MODE_AUTO;

Per ABI specification, a value of 0 corresponds to fans at full speed, not "auto". A value of 2+ corresponds to auto.

Quoted 5 lines

Please, refer to:

https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-hwmon-hwmonx-pwmy-enable

for more details.

Thanks for the link, will fix

Quoted 20 lines
Quoted 20 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +19 -0
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE,
+				       params, 1, NULL);
+		if (ret)
+			return ret;
+
+		driver_data->pwm_enabled = val;
+
+		/*
+		 * Activating PWM sets speed to 0. Alternative design decision
+		 * could be to keep the current value. This would require proper
+		 * setting of driver_data->curr_speed for example.
+		 */
+		if (val)
+			for (i = 0; i < driver_data->fan_count; ++i) {
+				ret = write_speed(dev, i, 0, true);
+				if (ret)
+					return ret;
+			}
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 339

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_ops hwmops = {

Prefix this struct with "tuxi_"

ok

Quoted 9 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 346

Quoted 8 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	.is_visible = hwm_is_visible,
+	.read = hwm_read,
+	.read_string = hwm_read_string,
+	.write = hwm_write,
+};
+
+static const struct hwmon_channel_info * const hwmcinfo[] = {

Same as above.

ok

Quoted 49 lines
Quoted 49 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +48 -0
+	HWMON_CHANNEL_INFO(fan,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(pwm,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 401

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+static const struct hwmon_chip_info hwminfo = {

Same as above.

ok

Quoted 24 lines
Quoted 24 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +23 -0
+	.ops = &hwmops,
+	.info = hwmcinfo
+};
+
+static u8 tuxi_get_temp_level(struct tuxi_hwmon_driver_data_t *driver_data,
+			      u8 fan_id, long temp)
+{
+	long temp_low, temp_high;
+	unsigned int i;
+	int ret;
+
+	ret = driver_data->temp_level[fan_id];
+
+	for (i = 0; temp_levels[i].temp; ++i) {
+		temp_low = i == 0 ? S32_MIN : temp_levels[i - 1].temp;
+		temp_high = temp_levels[i].temp;
+		if (ret > i)
+			temp_high -= TUXI_TEMP_LEVEL_HYSTERESIS;
+
+		if (temp >= temp_low && temp < temp_high)
+			return i;
+	}
+	if (temp >= temp_high)
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 430

Quoted 6 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +6 -0
+		ret = i;
+
+	return ret;
+}
+
+static void tuxi_periodic_hw_safeguard(struct work_struct *work)

I wonder if all of this convoluted logic is really necessary. Why can't you just force TUXI_TFAN_FAN_MODE_AUTO if a certain limit is surpassed?

Quoted 2 lines

The auto mode, aka the built in fan curve is way more aggressive than this, and reaching 80°C is very easy on this device.

If it's more aggresive, isn't it safer? IMO we should trust FW instead of hardcoded values.

Quoted 154 lines
Quoted 145 lines

That would mean fewer CPU instructions that are constantly running.

Quoted 142 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +142 -0
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = container_of(work,
+								    struct tuxi_hwmon_driver_data_t,
+								    work.work);
+	struct device *dev = driver_data->hwmdev;
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	unsigned int i;
+	long temp;
+	int ret;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		/*
+		 * If reading the temperature fails, default to a high value to
+		 * be on the safe side in the worst case.
+		 */
+		if (ret)
+			retval = TUXI_FW_TEMP_OFFSET + 1200;
+
+		temp = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;
+
+		driver_data->temp_level[i] = tuxi_get_temp_level(driver_data, i,
+								 temp);
+	}
+
+	// Reapply want_speeds to respect eventual new temp_levels
+	for (i = 0; i < driver_data->fan_count; ++i)
+		write_speed(dev, i, driver_data->want_speed[i], false);
+
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+}
+
+static int tuxi_hwmon_add_devices(struct acpi_device *pdev, struct device **hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data;
+	unsigned long long params[2], retval;
+	unsigned int i;
+	int ret;
+
+	/*
+	 * The first version of TUXI TFAN didn't have the Get Fan Temperature
+	 * method which is integral to this driver. So probe for existence and
+	 * abort otherwise.
+	 *
+	 * The Get Fan Speed method is also missing in that version, but was
+	 * added in the same version so it doesn't have to be probe separately.
+	 */
+	params[0] = 0;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+			       params, 1, &retval);
+	if (ret)
+		return ret;
+
+	driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	/*
+	 * Loading this module sets the fan mode to auto. Alternative design
+	 * decision could be to keep the current value. This would require
+	 * proper initialization of driver_data->curr_speed for example.
+	 */
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1,
+			       NULL);
+	if (ret)
+		return ret;
+
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_COUNT, NULL, 0,
+			       &retval);
+	if (ret)
+		return ret;
+	if (retval > TUXI_MAX_FAN_COUNT)
+		return -EINVAL;
+	driver_data->fan_count = retval;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TYPE,
+				       params, 1, &retval);
+		if (ret)
+			return ret;
+		if (retval >= ARRAY_SIZE(tuxi_fan_type_labels))
+			return -EOPNOTSUPP;
+		driver_data->fan_types[i] = tuxi_fan_type_labels[retval];
+	}
+
+	*hwmdev = devm_hwmon_device_register_with_info(&pdev->dev,
+						       "tuxedo_nbxx_acpi_tuxi",
+						       driver_data, &hwminfo,
+						       NULL);
+	if (IS_ERR(*hwmdev))
+		return PTR_ERR(*hwmdev);
+
+	driver_data->hwmdev = *hwmdev;
+
+	INIT_DELAYED_WORK(&driver_data->work, tuxi_periodic_hw_safeguard);
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+
+	return 0;
+}
+
+static void tuxi_hwmon_remove_devices(struct device *hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(hwmdev);
+	struct acpi_device *pdev = to_acpi_device(hwmdev->parent);
+	unsigned long long params[2];
+
+	cancel_delayed_work_sync(&driver_data->work);
+
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1, NULL);
+}
+
+static int tuxi_add(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data;
+	acpi_status status;
+
+	driver_data = devm_kzalloc(&device->dev, sizeof(*driver_data),
+				   GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	// Find subdevices
+	status = acpi_get_handle(device->handle, "TFAN",
+				 &driver_data->tfan_handle);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
+	dev_set_drvdata(&device->dev, driver_data);
+
+	return tuxi_hwmon_add_devices(device, &driver_data->hwmdev);
+}
+
+static void tuxi_remove(struct acpi_device *device)
+{
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 578

Quoted 6 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +6 -0
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+
+	tuxi_hwmon_remove_devices(driver_data->hwmdev);
+}
+
+static struct acpi_driver acpi_driver = {

AFAIK platform drivers are *strongly* preferred over acpi drivers. You should use that instead.

Quoted 1 line

Sorry, first time reading this. Can change it ofc, but is there a specific reason?

I'm not really sure. But while samsung-galaxybook was in review, I believe this was requested by Hans and Armin.

Hopefully they can elaborate on this.

Signature · 1 line
 ~ Kurt
Quoted 9 lines

Best regards,

Werner

Quoted 4 lines

For an example check the samsung-galaxybook driver in the for-next branch.

#5 lore raw
Quoted 838 lines · Kurt Borja
Quoted 182 lines · Werner Sembach

Hi Kurt,

Am 18.03.25 um 06:32 schrieb Kurt Borja:

Quoted 66 lines

Hi Werner,

Quoted 63 lines · Werner Sembach

The TUXEDO Sirius 16 Gen1 & Gen2 have the custom TUXEDO Interface (TUXI) ACPI interface which currently consists of the TFAN device. This has ACPI functions to control the built in fans and monitor fan speeds and CPU and GPU temperature.

This driver implements this TFAN device via the hwmon subsystem with an added temperature check that ensure a minimum fanspeed at certain temperatures. This allows userspace controlled, but hardware safe, custom fan curves.

---
  MAINTAINERS                                  |   6 +
  drivers/platform/x86/Kconfig                 |   2 +
  drivers/platform/x86/Makefile                |   3 +
  drivers/platform/x86/tuxedo/Kbuild           |   8 +
  drivers/platform/x86/tuxedo/Kconfig          |   8 +
  drivers/platform/x86/tuxedo/nbxx/Kbuild      |   9 +
  drivers/platform/x86/tuxedo/nbxx/Kconfig     |  15 +
  drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c | 591 +++++++++++++++++++
  8 files changed, 642 insertions(+)

create mode 100644 drivers/platform/x86/tuxedo/Kbuild create mode 100644 drivers/platform/x86/tuxedo/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kbuild create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c

MAINTAINERS +6 -0
index 8e0736dc2ee0e..7139c32e96dc7 100644
@@ -24190,6 +24190,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
  F:	tools/power/x86/turbostat/
  F:	tools/testing/selftests/turbostat/
 
+TUXEDO DRIVERS
+M:	Werner Sembach <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/platform/x86/tuxedo/
+
  TW5864 VIDEO4LINUX DRIVER
  M:	Bluecherry Maintainers <[email protected]>
  M:	Andrey Utkin <[email protected]>
drivers/platform/x86/Kconfig +2 -0
index 0258dd879d64b..58b258cde4fdb 100644
@@ -1186,6 +1186,8 @@ config SEL3350_PLATFORM
  	  To compile this driver as a module, choose M here: the module
  	  will be called sel3350-platform.
 
+source "drivers/platform/x86/tuxedo/Kconfig"
+
  endif # X86_PLATFORM_DEVICES
 
  config P2SB
drivers/platform/x86/Makefile +0 -0
index e1b1429470674..1562dcd7ad9a5 100644
@@ -153,3 +153,6 @@ obj-$(CONFIG_WINMATE_FM07_KEYS)		+= winmate-fm07-keys.o
 
Quoted 7 lines

drivers/platform/x86/tuxedo/Kbuild

Quoted 6 lines
drivers/platform/x86/Makefile +3 -0
  # SEL
  obj-$(CONFIG_SEL3350_PLATFORM)		+= sel3350-platform.o
+
+# TUXEDO
+obj-y					+= tuxedo/
drivers/platform/x86/tuxedo/Kbuild +0 -0

I think this should be named Makefile instead of Kbuild.

Ack

I thought since I did not use anything that is makefile syntax I should use Kbuild, but I looked it up and found this:
https://www.kernel.org/doc/html/v6.14-rc7/kbuild/makefiles.html#the-kbuild-files

Quoted 22 lines
Quoted 22 lines

new file mode 100644
index 0000000000000..dc55b403f201d

drivers/platform/x86/tuxedo/Kbuild new file +8 -0
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+obj-y	+= nbxx/
drivers/platform/x86/tuxedo/Kconfig new file +2 -0
new file mode 100644
index 0000000000000..1b22a0b29460a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/Kbuild

Quoted 7 lines
drivers/platform/x86/tuxedo/Kconfig new file +6 -0
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+source "drivers/platform/x86/tuxedo/nbxx/Kconfig"
drivers/platform/x86/tuxedo/nbxx/Kbuild +0 -0

Same as above.

Ack

Quoted 68 lines
Quoted 56 lines

new file mode 100644
index 0000000000000..256b03921c732

drivers/platform/x86/tuxedo/nbxx/Kbuild new file +9 -0
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+tuxedo_nbxx_acpi_tuxi-y			:= acpi_tuxi.o
+obj-$(CONFIG_TUXEDO_NBXX_ACPI_TUXI)	+= tuxedo_nbxx_acpi_tuxi.o
drivers/platform/x86/tuxedo/nbxx/Kconfig new file +15 -0
new file mode 100644
index 0000000000000..0d011c0c715a5
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+config TUXEDO_NBXX_ACPI_TUXI
+	tristate "TUXEDO NBxx ACPI TUXI Platform Driver"
+	help
+	  This driver implements the ACPI TUXI device found on some TUXEDO
+	  Notebooks. Currently this consists only of the TFAN subdevice which is
+	  implemented via hwmon.
+
+	  When compiled as a module it will be called tuxedo_nbxx_acpi_tuxi
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +14 -0
new file mode 100644
index 0000000000000..bb452cc33568a
@@ -0,0 +1,591 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024-2025 Werner Sembach [email protected]
+ */
+
+#include <linux/acpi.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/units.h>

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 24

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+#include <linux/workqueue.h>
+
+#define TUXI_SAFEGUARD_PERIOD 1000      // 1s
+#define TUXI_PWM_FAN_ON_MIN_SPEED 0x40  // ~25%
+#define TUXI_TEMP_LEVEL_HYSTERESIS 1500 // 1.5°C
+#define TUXI_FW_TEMP_OFFSET 2730        // Kelvin to Celsius
+#define TUXI_MAX_FAN_COUNT 16           /*
+					 * If this is increased, new lines must
+					 * be added to hwmcinfo below.
+					 */

The style of these macros could be better.

Quoted 7 lines
Quoted 5 lines

I suggest using an enum instead. Something like:

	enum TUXI_SAFEGUARD_LIMITS {
		...
	}

Since these values are all on very different scales an enum feels somehow wrong for me here.

I agree.

Quoted 194 lines
Quoted 2 lines

with it's values aligned and the comment on top.

Ack

Quoted 139 lines
Quoted 139 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +138 -0
+
+static const struct acpi_device_id acpi_device_ids[] = {
+	{"TUXI0000", 0},
+	{"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, acpi_device_ids);
+
+struct tuxi_driver_data_t {
+	acpi_handle tfan_handle;
+	struct device *hwmdev;
+};
+
+struct tuxi_hwmon_driver_data_t {
+	struct delayed_work work;
+	struct device *hwmdev;
+	u8 fan_count;
+	const char *fan_types[TUXI_MAX_FAN_COUNT];
+	u8 temp_level[TUXI_MAX_FAN_COUNT];
+	u8 curr_speed[TUXI_MAX_FAN_COUNT];
+	u8 want_speed[TUXI_MAX_FAN_COUNT];
+	u8 pwm_enabled;
+};
+
+struct tuxi_temp_high_config_t {
+	long temp;
+	u8 min_speed;
+};
+
+/*
+ * Speed values in this table must be >= TUXI_PWM_FAN_ON_MIN_SPEED to avoid
+ * undefined behaviour.
+ */
+static const struct tuxi_temp_high_config_t temp_levels[] = {
+	{  80000, 0x4d }, // ~30%
+	{  90000, 0x66 }, // ~40%
+	{ 100000, 0xff }, // 100%
+	{ }
+};
+
+/*
+ * Set fan speed target
+ *
+ * Set a specific fan speed (needs manual mode)
+ *
+ * Arg0: Fan index
+ * Arg1: Fan speed as a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_SPEED		"SSPD"
+
+/*
+ * Get fan speed target
+ *
+ * Arg0: Fan index
+ * Returns: Current fan speed target a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_SPEED		"GSPD"
+
+/*
+ * Get fans count
+ *
+ * Returns: Number of individually controllable fans
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_COUNT		"GCNT"
+
+/*
+ * Set fans mode
+ *
+ * Arg0: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_MODE		"SMOD"
+
+/*
+ * Get fans mode
+ *
+ * Returns: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_MODE		"GMOD"
+
+#define TUXI_TFAN_FAN_MODE_AUTO 0
+#define TUXI_TFAN_FAN_MODE_MANUAL 1
+
+/*
+ * Get fan type/what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: 0 = general, 1 = CPU, 2 = GPU
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TYPE		"GTYP"
+
+static const char * const tuxi_fan_type_labels[] = {
+	"general",
+	"cpu",
+	"gpu"
+};
+
+/*
+ * Get fan temperature/temperature of what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: Temperature sensor value in 10ths of degrees kelvin
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE	"GTMP"
+
+/*
+ * Get actual fan speed in RPM
+ *
+ * Arg0: Fan index
+ * Returns: Speed sensor value in revolutions per minute
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_RPM		"GRPM"
+
+static int tuxi_tfan_method(struct acpi_device *device, acpi_string method,
+			    unsigned long long *params, u32 pcount,
+			    unsigned long long *retval)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+	acpi_handle handle = driver_data->tfan_handle;
+	union acpi_object *obj __free(kfree) = NULL;
+	struct acpi_object_list arguments;
+	unsigned long long data;
+	acpi_status status;
+	unsigned int i;
+
+	if (pcount > 0) {
+		obj = kcalloc(pcount, sizeof(*arguments.pointer), GFP_KERNEL);
+
+		arguments.count = pcount;
+		arguments.pointer = obj;
+		for (i = 0; i < pcount; ++i) {
+			arguments.pointer[i].type = ACPI_TYPE_INTEGER;
+			arguments.pointer[i].integer.value = params[i];
+		}
+	}
+	status = acpi_evaluate_integer(handle, method,
+				       pcount ? &arguments : NULL, &data);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 169

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	if (retval)
+		*retval = data;
+
+	return 0;
+}
+
+static umode_t hwm_is_visible(const void *data, enum hwmon_sensor_types type,

All of these callbacks should be prefixed with "tuxi_".

Ack.

Quoted 29 lines
Quoted 29 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +28 -0
+			      u32 __always_unused attr, int channel)
+{
+	struct tuxi_hwmon_driver_data_t const *driver_data = data;
+
+	if (channel >= driver_data->fan_count)
+		return 0;
+
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	case hwmon_temp:
+		return 0444;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
+		    int channel, long *val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	int ret;
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 204

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+
+	switch (type) {
+	case hwmon_fan:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_RPM,
+				       params, 1, &retval);
+		*val = retval > S32_MAX ? S32_MAX : retval;

Use clamp_val().

Thanks for the tip

Quoted 3 lines

Also, is an RPM >S32_MAX a bogus value? Should it be treated as an error instead?

Yeah sounds good, will do.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 209

Quoted 5 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +5 -0
+		return ret;
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			if (driver_data->pwm_enabled) {

I think this needs locking.

Since this was only meant as a small performance optimization if wonder if I shouldn't just skip the if-case and just ask the wmi every time instead?

IMO, yes. I don't think it's necessary to cache the value either, as you could just retrieve it every time. That way you avoid having to provide locking.

Quoted 31 lines
Quoted 11 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 217

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+				*val = driver_data->curr_speed[channel];
+				return 0;
+			}
+			params[0] = channel;
+			ret = tuxi_tfan_method(pdev,
+					       TUXI_TFAN_METHOD_GET_FAN_SPEED,
+					       params, 1, &retval);
+			*val = retval > S32_MAX ? S32_MAX : retval;

HWMON ABI specifies that the pwm attribute range is 0-255. Are values above this, bogus values?

Didn't know, but ofc I can change the check here.

Quoted 6 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 221

Quoted 5 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +4 -0
+			return ret;
+		case hwmon_pwm_enable:
+			*val = driver_data->pwm_enabled;
+			return ret;

ret is uninitialized when used here!

Sorry, will fix.

Quoted 12 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 230

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +9 -0
+		}
+		break;
+	case hwmon_temp:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		*val = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;

Same comment as the clamped values above.

Quoted 7 lines
Quoted 2 lines

Additionally, please use deci_kelvin_to_millicelsius() from <linux/units.h> and IMO this should be an standard if-else block.

Because of firmware weirdness I can't use deci_kelvin_to_millicelsius: While the firmware claims to output 10th kelvin it actually outputs only full degrees with the last number always being 0, that's why I defined the custom TUXI_FW_TEMP_OFFSET as 2730 instead of 2731.

Then you can use deci_kelvin_to_millicelsius_with_offset() and provide your own offset.

Quoted 2 lines

I could use deci_kelvin_to_celsius()*MILLIDEGREE_PER_DEGREE, but this would

deci_kelvin_to_millicelsius() avoids rounding and makes that multiplication unnecessary.

Quoted 230 lines

introduce an unnecessary rounding step.

Quoted 67 lines
Quoted 67 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +66 -0
+		return ret;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read_string(struct device *dev,
+			   enum hwmon_sensor_types __always_unused type,
+			   u32 __always_unused attr, int channel,
+			   const char **str)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+
+	*str = driver_data->fan_types[channel];
+
+	return 0;
+}
+
+static int write_speed(struct device *dev, int channel, u8 val, bool force)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long new_speed, params[2];
+	u8 temp_level;
+	int ret;
+
+	params[0] = channel;
+
+	/*
+	 * The heatpipe across the VRMs is shared between both fans and the VRMs
+	 * are the most likely to go up in smoke. So it's better to apply the
+	 * minimum fan speed to all fans if either CPU or GPU is working hard.
+	 */
+	temp_level = max_array(driver_data->temp_level, driver_data->fan_count);
+	if (temp_level)
+		new_speed = max(val, temp_levels[temp_level - 1].min_speed);
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED / 2)
+		new_speed = 0;
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED)
+		new_speed = TUXI_PWM_FAN_ON_MIN_SPEED;
+	else
+		new_speed = val;
+
+	if (force || new_speed != driver_data->curr_speed[channel]) {
+		params[0] = channel;
+		params[1] = new_speed;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_SPEED,
+				       params, 2, NULL);
+		if (ret)
+			return ret;
+	}
+
+	driver_data->curr_speed[channel] = new_speed;
+
+	return 0;
+}
+
+static int hwm_write(struct device *dev,
+		     enum hwmon_sensor_types __always_unused type, u32 attr,
+		     int channel, long val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2];
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 303

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	unsigned int i;
+	int ret;
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		if (val > U8_MAX || val < 0)
+			return -EINVAL;

Please, clamp_val() instead.

ack

Quoted 4 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 305

Quoted 3 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +2 -0
+
+		if (driver_data->pwm_enabled) {

I believe this needs locking to ensure pwm_enabled hasn't changed.

ok

Quoted 12 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 313

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+			driver_data->want_speed[channel] = val;
+			return write_speed(dev, channel, val, false);
+		}
+
+		return 0;
+	case hwmon_pwm_enable:
+		params[0] = val ? TUXI_TFAN_FAN_MODE_MANUAL :
+				  TUXI_TFAN_FAN_MODE_AUTO;

Per ABI specification, a value of 0 corresponds to fans at full speed, not "auto". A value of 2+ corresponds to auto.

Quoted 5 lines

Please, refer to:

https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-hwmon-hwmonx-pwmy-enable

for more details.

Thanks for the link, will fix

Quoted 20 lines
Quoted 20 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +19 -0
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE,
+				       params, 1, NULL);
+		if (ret)
+			return ret;
+
+		driver_data->pwm_enabled = val;
+
+		/*
+		 * Activating PWM sets speed to 0. Alternative design decision
+		 * could be to keep the current value. This would require proper
+		 * setting of driver_data->curr_speed for example.
+		 */
+		if (val)
+			for (i = 0; i < driver_data->fan_count; ++i) {
+				ret = write_speed(dev, i, 0, true);
+				if (ret)
+					return ret;
+			}
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 339

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_ops hwmops = {

Prefix this struct with "tuxi_"

ok

Quoted 9 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 346

Quoted 8 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	.is_visible = hwm_is_visible,
+	.read = hwm_read,
+	.read_string = hwm_read_string,
+	.write = hwm_write,
+};
+
+static const struct hwmon_channel_info * const hwmcinfo[] = {

Same as above.

ok

Quoted 49 lines
Quoted 49 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +48 -0
+	HWMON_CHANNEL_INFO(fan,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(pwm,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 401

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+static const struct hwmon_chip_info hwminfo = {

Same as above.

ok

Quoted 24 lines
Quoted 24 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +23 -0
+	.ops = &hwmops,
+	.info = hwmcinfo
+};
+
+static u8 tuxi_get_temp_level(struct tuxi_hwmon_driver_data_t *driver_data,
+			      u8 fan_id, long temp)
+{
+	long temp_low, temp_high;
+	unsigned int i;
+	int ret;
+
+	ret = driver_data->temp_level[fan_id];
+
+	for (i = 0; temp_levels[i].temp; ++i) {
+		temp_low = i == 0 ? S32_MIN : temp_levels[i - 1].temp;
+		temp_high = temp_levels[i].temp;
+		if (ret > i)
+			temp_high -= TUXI_TEMP_LEVEL_HYSTERESIS;
+
+		if (temp >= temp_low && temp < temp_high)
+			return i;
+	}
+	if (temp >= temp_high)
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 430

Quoted 6 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +6 -0
+		ret = i;
+
+	return ret;
+}
+
+static void tuxi_periodic_hw_safeguard(struct work_struct *work)

I wonder if all of this convoluted logic is really necessary. Why can't you just force TUXI_TFAN_FAN_MODE_AUTO if a certain limit is surpassed?

Quoted 2 lines

The auto mode, aka the built in fan curve is way more aggressive than this, and reaching 80°C is very easy on this device.

If it's more aggresive, isn't it safer? IMO we should trust FW instead of hardcoded values.

Quoted 154 lines
Quoted 153 lines

That would mean fewer CPU instructions that are constantly running.

Quoted 138 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +138 -0
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = container_of(work,
+								    struct tuxi_hwmon_driver_data_t,
+								    work.work);
+	struct device *dev = driver_data->hwmdev;
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	unsigned int i;
+	long temp;
+	int ret;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		/*
+		 * If reading the temperature fails, default to a high value to
+		 * be on the safe side in the worst case.
+		 */
+		if (ret)
+			retval = TUXI_FW_TEMP_OFFSET + 1200;
+
+		temp = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;
+
+		driver_data->temp_level[i] = tuxi_get_temp_level(driver_data, i,
+								 temp);
+	}
+
+	// Reapply want_speeds to respect eventual new temp_levels
+	for (i = 0; i < driver_data->fan_count; ++i)
+		write_speed(dev, i, driver_data->want_speed[i], false);
+
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+}
+
+static int tuxi_hwmon_add_devices(struct acpi_device *pdev, struct device **hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data;
+	unsigned long long params[2], retval;
+	unsigned int i;
+	int ret;
+
+	/*
+	 * The first version of TUXI TFAN didn't have the Get Fan Temperature
+	 * method which is integral to this driver. So probe for existence and
+	 * abort otherwise.
+	 *
+	 * The Get Fan Speed method is also missing in that version, but was
+	 * added in the same version so it doesn't have to be probe separately.
+	 */
+	params[0] = 0;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+			       params, 1, &retval);
+	if (ret)
+		return ret;
+
+	driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	/*
+	 * Loading this module sets the fan mode to auto. Alternative design
+	 * decision could be to keep the current value. This would require
+	 * proper initialization of driver_data->curr_speed for example.
+	 */
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1,
+			       NULL);
+	if (ret)
+		return ret;
+
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_COUNT, NULL, 0,
+			       &retval);
+	if (ret)
+		return ret;
+	if (retval > TUXI_MAX_FAN_COUNT)
+		return -EINVAL;
+	driver_data->fan_count = retval;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TYPE,
+				       params, 1, &retval);
+		if (ret)
+			return ret;
+		if (retval >= ARRAY_SIZE(tuxi_fan_type_labels))
+			return -EOPNOTSUPP;
+		driver_data->fan_types[i] = tuxi_fan_type_labels[retval];
+	}
+
+	*hwmdev = devm_hwmon_device_register_with_info(&pdev->dev,
+						       "tuxedo_nbxx_acpi_tuxi",
+						       driver_data, &hwminfo,
+						       NULL);
+	if (IS_ERR(*hwmdev))
+		return PTR_ERR(*hwmdev);
+
+	driver_data->hwmdev = *hwmdev;
+
+	INIT_DELAYED_WORK(&driver_data->work, tuxi_periodic_hw_safeguard);
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+
+	return 0;
+}
+
+static void tuxi_hwmon_remove_devices(struct device *hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(hwmdev);
+	struct acpi_device *pdev = to_acpi_device(hwmdev->parent);
+	unsigned long long params[2];
+
+	cancel_delayed_work_sync(&driver_data->work);
+
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1, NULL);
+}
+
+static int tuxi_add(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data;
+	acpi_status status;
+
+	driver_data = devm_kzalloc(&device->dev, sizeof(*driver_data),
+				   GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	// Find subdevices
+	status = acpi_get_handle(device->handle, "TFAN",
+				 &driver_data->tfan_handle);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
+	dev_set_drvdata(&device->dev, driver_data);
+
+	return tuxi_hwmon_add_devices(device, &driver_data->hwmdev);

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 578

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+}
+
+static void tuxi_remove(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+
+	tuxi_hwmon_remove_devices(driver_data->hwmdev);
+}
+
+static struct acpi_driver acpi_driver = {

AFAIK platform drivers are *strongly* preferred over acpi drivers. You should use that instead.

Quoted 7 lines
Quoted 1 line

Sorry, first time reading this. Can change it ofc, but is there a specific reason?

I'm not really sure. But while samsung-galaxybook was in review, I believe this was requested by Hans and Armin.

Hopefully they can elaborate on this.

Hi! In case it helps I think most of the rationale behind this can be found in the docs:

https://docs.kernel.org/firmware-guide/acpi/enumeration.html#acpi-device-objects

In the case of samsung-galaxybook the issue was that I was originally creating new devices in the driver which were not necessary; both the ACPI device and its corresponding platform device had already been created by the ACPI Core as per the documentation referenced above, it is just a matter of matching and fetching them in the right way.

I do think in general if the idea is that this Tuxedo platform driver would eventually include more "platform stuff" that it feels more natural to hang everything from a platform device instead of a specific ACPI device (on top of/ presumably some of what the rationale in the documentation is based on).

If it is currently and only will ever be a hwmon device then I suppose one could even argue that this driver belongs under drivers/hwmon instead :) But it seems as though the intention is that there will or could be other things added?

Just out of curiosity what is the significance of "NBxx"? Is it something that will add value (and should remain in the path as well as various descriptions) or would it make sense that anything falling under drivers/platform/x86/tuxedo will always be "NBxx" without needing to state it again (e.g. will there be other Tuxedo x86 Platform Drivers that are not "NBxx")?

Hope this helps!

Best regards,
Joshua

author maintainer
#6 lore raw

Am 22.03.25 um 12:46 schrieb Joshua Grisham:

Quoted 829 lines · Kurt Borja
Quoted 811 lines · Werner Sembach
Quoted 179 lines

Hi Kurt,

Am 18.03.25 um 06:32 schrieb Kurt Borja:

Quoted 66 lines

Hi Werner,

Quoted 63 lines · Werner Sembach

The TUXEDO Sirius 16 Gen1 & Gen2 have the custom TUXEDO Interface (TUXI) ACPI interface which currently consists of the TFAN device. This has ACPI functions to control the built in fans and monitor fan speeds and CPU and GPU temperature.

This driver implements this TFAN device via the hwmon subsystem with an added temperature check that ensure a minimum fanspeed at certain temperatures. This allows userspace controlled, but hardware safe, custom fan curves.

---
   MAINTAINERS                                  |   6 +
   drivers/platform/x86/Kconfig                 |   2 +
   drivers/platform/x86/Makefile                |   3 +
   drivers/platform/x86/tuxedo/Kbuild           |   8 +
   drivers/platform/x86/tuxedo/Kconfig          |   8 +
   drivers/platform/x86/tuxedo/nbxx/Kbuild      |   9 +
   drivers/platform/x86/tuxedo/nbxx/Kconfig     |  15 +
   drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c | 591 +++++++++++++++++++
   8 files changed, 642 insertions(+)

create mode 100644 drivers/platform/x86/tuxedo/Kbuild create mode 100644 drivers/platform/x86/tuxedo/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kbuild create mode 100644 drivers/platform/x86/tuxedo/nbxx/Kconfig create mode 100644 drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c

MAINTAINERS +6 -0
index 8e0736dc2ee0e..7139c32e96dc7 100644
@@ -24190,6 +24190,12 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux.git turbostat
   F:	tools/power/x86/turbostat/
   F:	tools/testing/selftests/turbostat/
 
+TUXEDO DRIVERS
+M:	Werner Sembach <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/platform/x86/tuxedo/
+
   TW5864 VIDEO4LINUX DRIVER
   M:	Bluecherry Maintainers <[email protected]>
   M:	Andrey Utkin <[email protected]>
drivers/platform/x86/Kconfig +2 -0
index 0258dd879d64b..58b258cde4fdb 100644
@@ -1186,6 +1186,8 @@ config SEL3350_PLATFORM
   	  To compile this driver as a module, choose M here: the module
   	  will be called sel3350-platform.
 
+source "drivers/platform/x86/tuxedo/Kconfig"
+
   endif # X86_PLATFORM_DEVICES
 
   config P2SB
drivers/platform/x86/Makefile +0 -0
index e1b1429470674..1562dcd7ad9a5 100644
@@ -153,3 +153,6 @@ obj-$(CONFIG_WINMATE_FM07_KEYS)		+= winmate-fm07-keys.o
 
Quoted 7 lines

drivers/platform/x86/tuxedo/Kbuild

Quoted 6 lines
drivers/platform/x86/Makefile +3 -0
   # SEL
   obj-$(CONFIG_SEL3350_PLATFORM)		+= sel3350-platform.o
+
+# TUXEDO
+obj-y					+= tuxedo/
drivers/platform/x86/tuxedo/Kbuild +0 -0

I think this should be named Makefile instead of Kbuild.

Ack

I thought since I did not use anything that is makefile syntax I should use Kbuild, but I looked it up and found this:
https://www.kernel.org/doc/html/v6.14-rc7/kbuild/makefiles.html#the-kbuild-files

Quoted 21 lines
Quoted 21 lines

new file mode 100644
index 0000000000000..dc55b403f201d

drivers/platform/x86/tuxedo/Kbuild new file +8 -0
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+obj-y	+= nbxx/
drivers/platform/x86/tuxedo/Kconfig new file +2 -0
new file mode 100644
index 0000000000000..1b22a0b29460a
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/Kbuild

Quoted 7 lines
drivers/platform/x86/tuxedo/Kconfig new file +6 -0
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+source "drivers/platform/x86/tuxedo/nbxx/Kconfig"
drivers/platform/x86/tuxedo/nbxx/Kbuild +0 -0

Same as above.

Ack

Quoted 67 lines
Quoted 55 lines

new file mode 100644
index 0000000000000..256b03921c732

drivers/platform/x86/tuxedo/nbxx/Kbuild new file +9 -0
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+tuxedo_nbxx_acpi_tuxi-y			:= acpi_tuxi.o
+obj-$(CONFIG_TUXEDO_NBXX_ACPI_TUXI)	+= tuxedo_nbxx_acpi_tuxi.o
drivers/platform/x86/tuxedo/nbxx/Kconfig new file +15 -0
new file mode 100644
index 0000000000000..0d011c0c715a5
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# Copyright (C) 2024-2025 Werner Sembach [email protected]
+#
+# TUXEDO X86 Platform Specific Drivers
+#
+
+config TUXEDO_NBXX_ACPI_TUXI
+	tristate "TUXEDO NBxx ACPI TUXI Platform Driver"
+	help
+	  This driver implements the ACPI TUXI device found on some TUXEDO
+	  Notebooks. Currently this consists only of the TFAN subdevice which is
+	  implemented via hwmon.
+
+	  When compiled as a module it will be called tuxedo_nbxx_acpi_tuxi
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +14 -0
new file mode 100644
index 0000000000000..bb452cc33568a
@@ -0,0 +1,591 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024-2025 Werner Sembach [email protected]
+ */
+
+#include <linux/acpi.h>
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/hwmon.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/units.h>

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 24

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+#include <linux/workqueue.h>
+
+#define TUXI_SAFEGUARD_PERIOD 1000      // 1s
+#define TUXI_PWM_FAN_ON_MIN_SPEED 0x40  // ~25%
+#define TUXI_TEMP_LEVEL_HYSTERESIS 1500 // 1.5°C
+#define TUXI_FW_TEMP_OFFSET 2730        // Kelvin to Celsius
+#define TUXI_MAX_FAN_COUNT 16           /*
+					 * If this is increased, new lines must
+					 * be added to hwmcinfo below.
+					 */

The style of these macros could be better.

Quoted 7 lines
Quoted 5 lines

I suggest using an enum instead. Something like:

	enum TUXI_SAFEGUARD_LIMITS {
		...
	}

Since these values are all on very different scales an enum feels somehow wrong for me here.

I agree.

Quoted 189 lines
Quoted 1 line

with it's values aligned and the comment on top.

Ack

Quoted 138 lines
Quoted 138 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +138 -0
+
+static const struct acpi_device_id acpi_device_ids[] = {
+	{"TUXI0000", 0},
+	{"", 0}
+};
+MODULE_DEVICE_TABLE(acpi, acpi_device_ids);
+
+struct tuxi_driver_data_t {
+	acpi_handle tfan_handle;
+	struct device *hwmdev;
+};
+
+struct tuxi_hwmon_driver_data_t {
+	struct delayed_work work;
+	struct device *hwmdev;
+	u8 fan_count;
+	const char *fan_types[TUXI_MAX_FAN_COUNT];
+	u8 temp_level[TUXI_MAX_FAN_COUNT];
+	u8 curr_speed[TUXI_MAX_FAN_COUNT];
+	u8 want_speed[TUXI_MAX_FAN_COUNT];
+	u8 pwm_enabled;
+};
+
+struct tuxi_temp_high_config_t {
+	long temp;
+	u8 min_speed;
+};
+
+/*
+ * Speed values in this table must be >= TUXI_PWM_FAN_ON_MIN_SPEED to avoid
+ * undefined behaviour.
+ */
+static const struct tuxi_temp_high_config_t temp_levels[] = {
+	{  80000, 0x4d }, // ~30%
+	{  90000, 0x66 }, // ~40%
+	{ 100000, 0xff }, // 100%
+	{ }
+};
+
+/*
+ * Set fan speed target
+ *
+ * Set a specific fan speed (needs manual mode)
+ *
+ * Arg0: Fan index
+ * Arg1: Fan speed as a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_SPEED		"SSPD"
+
+/*
+ * Get fan speed target
+ *
+ * Arg0: Fan index
+ * Returns: Current fan speed target a fraction of maximum speed (0-255)
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_SPEED		"GSPD"
+
+/*
+ * Get fans count
+ *
+ * Returns: Number of individually controllable fans
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_COUNT		"GCNT"
+
+/*
+ * Set fans mode
+ *
+ * Arg0: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_SET_FAN_MODE		"SMOD"
+
+/*
+ * Get fans mode
+ *
+ * Returns: 0 = auto, 1 = manual
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_MODE		"GMOD"
+
+#define TUXI_TFAN_FAN_MODE_AUTO 0
+#define TUXI_TFAN_FAN_MODE_MANUAL 1
+
+/*
+ * Get fan type/what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: 0 = general, 1 = CPU, 2 = GPU
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TYPE		"GTYP"
+
+static const char * const tuxi_fan_type_labels[] = {
+	"general",
+	"cpu",
+	"gpu"
+};
+
+/*
+ * Get fan temperature/temperature of what the fan is pointed at
+ *
+ * Arg0: Fan index
+ * Returns: Temperature sensor value in 10ths of degrees kelvin
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE	"GTMP"
+
+/*
+ * Get actual fan speed in RPM
+ *
+ * Arg0: Fan index
+ * Returns: Speed sensor value in revolutions per minute
+ */
+#define TUXI_TFAN_METHOD_GET_FAN_RPM		"GRPM"
+
+static int tuxi_tfan_method(struct acpi_device *device, acpi_string method,
+			    unsigned long long *params, u32 pcount,
+			    unsigned long long *retval)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+	acpi_handle handle = driver_data->tfan_handle;
+	union acpi_object *obj __free(kfree) = NULL;
+	struct acpi_object_list arguments;
+	unsigned long long data;
+	acpi_status status;
+	unsigned int i;
+
+	if (pcount > 0) {
+		obj = kcalloc(pcount, sizeof(*arguments.pointer), GFP_KERNEL);
+
+		arguments.count = pcount;
+		arguments.pointer = obj;
+		for (i = 0; i < pcount; ++i) {
+			arguments.pointer[i].type = ACPI_TYPE_INTEGER;
+			arguments.pointer[i].integer.value = params[i];
+		}
+	}
+	status = acpi_evaluate_integer(handle, method,
+				       pcount ? &arguments : NULL, &data);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 169

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	if (retval)
+		*retval = data;
+
+	return 0;
+}
+
+static umode_t hwm_is_visible(const void *data, enum hwmon_sensor_types type,

All of these callbacks should be prefixed with "tuxi_".

Ack.

Quoted 28 lines
Quoted 28 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +28 -0
+			      u32 __always_unused attr, int channel)
+{
+	struct tuxi_hwmon_driver_data_t const *driver_data = data;
+
+	if (channel >= driver_data->fan_count)
+		return 0;
+
+	switch (type) {
+	case hwmon_fan:
+		return 0444;
+	case hwmon_pwm:
+		return 0644;
+	case hwmon_temp:
+		return 0444;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
+		    int channel, long *val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	int ret;
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 204

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+
+	switch (type) {
+	case hwmon_fan:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_RPM,
+				       params, 1, &retval);
+		*val = retval > S32_MAX ? S32_MAX : retval;

Use clamp_val().

Thanks for the tip

Quoted 2 lines

Also, is an RPM >S32_MAX a bogus value? Should it be treated as an error instead?

Yeah sounds good, will do.

Quoted 8 lines
Quoted 6 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 209

Quoted 5 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +5 -0
+		return ret;
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			if (driver_data->pwm_enabled) {

I think this needs locking.

Since this was only meant as a small performance optimization if wonder if I shouldn't just skip the if-case and just ask the wmi every time instead?

IMO, yes. I don't think it's necessary to cache the value either, as you could just retrieve it every time. That way you avoid having to provide locking.

Quoted 28 lines
Quoted 10 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 217

Quoted 8 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+				*val = driver_data->curr_speed[channel];
+				return 0;
+			}
+			params[0] = channel;
+			ret = tuxi_tfan_method(pdev,
+					       TUXI_TFAN_METHOD_GET_FAN_SPEED,
+					       params, 1, &retval);
+			*val = retval > S32_MAX ? S32_MAX : retval;

HWMON ABI specifies that the pwm attribute range is 0-255. Are values above this, bogus values?

Didn't know, but ofc I can change the check here.

Quoted 5 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 221

Quoted 4 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +4 -0
+			return ret;
+		case hwmon_pwm_enable:
+			*val = driver_data->pwm_enabled;
+			return ret;

ret is uninitialized when used here!

Sorry, will fix.

Quoted 11 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 230

Quoted 9 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +9 -0
+		}
+		break;
+	case hwmon_temp:
+		params[0] = channel;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		*val = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;

Same comment as the clamped values above.

Quoted 6 lines
Quoted 2 lines

Additionally, please use deci_kelvin_to_millicelsius() from <linux/units.h> and IMO this should be an standard if-else block.

Because of firmware weirdness I can't use deci_kelvin_to_millicelsius: While the firmware claims to output 10th kelvin it actually outputs only full degrees with the last number always being 0, that's why I defined the custom TUXI_FW_TEMP_OFFSET as 2730 instead of 2731.

Then you can use deci_kelvin_to_millicelsius_with_offset() and provide your own offset.

Quoted 1 line

I could use deci_kelvin_to_celsius()*MILLIDEGREE_PER_DEGREE, but this would

deci_kelvin_to_millicelsius() avoids rounding and makes that multiplication unnecessary.

Quoted 223 lines

introduce an unnecessary rounding step.

Quoted 66 lines
Quoted 66 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +66 -0
+		return ret;
+	default:
+		break;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static int hwm_read_string(struct device *dev,
+			   enum hwmon_sensor_types __always_unused type,
+			   u32 __always_unused attr, int channel,
+			   const char **str)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+
+	*str = driver_data->fan_types[channel];
+
+	return 0;
+}
+
+static int write_speed(struct device *dev, int channel, u8 val, bool force)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long new_speed, params[2];
+	u8 temp_level;
+	int ret;
+
+	params[0] = channel;
+
+	/*
+	 * The heatpipe across the VRMs is shared between both fans and the VRMs
+	 * are the most likely to go up in smoke. So it's better to apply the
+	 * minimum fan speed to all fans if either CPU or GPU is working hard.
+	 */
+	temp_level = max_array(driver_data->temp_level, driver_data->fan_count);
+	if (temp_level)
+		new_speed = max(val, temp_levels[temp_level - 1].min_speed);
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED / 2)
+		new_speed = 0;
+	else if (val < TUXI_PWM_FAN_ON_MIN_SPEED)
+		new_speed = TUXI_PWM_FAN_ON_MIN_SPEED;
+	else
+		new_speed = val;
+
+	if (force || new_speed != driver_data->curr_speed[channel]) {
+		params[0] = channel;
+		params[1] = new_speed;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_SPEED,
+				       params, 2, NULL);
+		if (ret)
+			return ret;
+	}
+
+	driver_data->curr_speed[channel] = new_speed;
+
+	return 0;
+}
+
+static int hwm_write(struct device *dev,
+		     enum hwmon_sensor_types __always_unused type, u32 attr,
+		     int channel, long val)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(dev);
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2];
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 303

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	unsigned int i;
+	int ret;
+
+	switch (attr) {
+	case hwmon_pwm_input:
+		if (val > U8_MAX || val < 0)
+			return -EINVAL;

Please, clamp_val() instead.

ack

Quoted 3 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 305

Quoted 2 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +2 -0
+
+		if (driver_data->pwm_enabled) {

I believe this needs locking to ensure pwm_enabled hasn't changed.

ok

Quoted 11 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 313

Quoted 8 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +8 -0
+			driver_data->want_speed[channel] = val;
+			return write_speed(dev, channel, val, false);
+		}
+
+		return 0;
+	case hwmon_pwm_enable:
+		params[0] = val ? TUXI_TFAN_FAN_MODE_MANUAL :
+				  TUXI_TFAN_FAN_MODE_AUTO;

Per ABI specification, a value of 0 corresponds to fans at full speed, not "auto". A value of 2+ corresponds to auto.

Quoted 5 lines

Please, refer to:

https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-hwmon-hwmonx-pwmy-enable

for more details.

Thanks for the link, will fix

Quoted 19 lines
Quoted 19 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +19 -0
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE,
+				       params, 1, NULL);
+		if (ret)
+			return ret;
+
+		driver_data->pwm_enabled = val;
+
+		/*
+		 * Activating PWM sets speed to 0. Alternative design decision
+		 * could be to keep the current value. This would require proper
+		 * setting of driver_data->curr_speed for example.
+		 */
+		if (val)
+			for (i = 0; i < driver_data->fan_count; ++i) {
+				ret = write_speed(dev, i, 0, true);
+				if (ret)
+					return ret;
+			}
+
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 339

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+		return 0;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+static const struct hwmon_ops hwmops = {

Prefix this struct with "tuxi_"

ok

Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 346

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+	.is_visible = hwm_is_visible,
+	.read = hwm_read,
+	.read_string = hwm_read_string,
+	.write = hwm_write,
+};
+
+static const struct hwmon_channel_info * const hwmcinfo[] = {

Same as above.

ok

Quoted 48 lines
Quoted 48 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +48 -0
+	HWMON_CHANNEL_INFO(fan,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL,
+			   HWMON_F_INPUT | HWMON_F_LABEL),
+	HWMON_CHANNEL_INFO(pwm,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
+			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
+	HWMON_CHANNEL_INFO(temp,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 401

Quoted 7 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +7 -0
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL,
+			   HWMON_T_INPUT | HWMON_T_LABEL),
+	NULL
+};
+
+static const struct hwmon_chip_info hwminfo = {

Same as above.

ok

Quoted 23 lines
Quoted 23 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +23 -0
+	.ops = &hwmops,
+	.info = hwmcinfo
+};
+
+static u8 tuxi_get_temp_level(struct tuxi_hwmon_driver_data_t *driver_data,
+			      u8 fan_id, long temp)
+{
+	long temp_low, temp_high;
+	unsigned int i;
+	int ret;
+
+	ret = driver_data->temp_level[fan_id];
+
+	for (i = 0; temp_levels[i].temp; ++i) {
+		temp_low = i == 0 ? S32_MIN : temp_levels[i - 1].temp;
+		temp_high = temp_levels[i].temp;
+		if (ret > i)
+			temp_high -= TUXI_TEMP_LEVEL_HYSTERESIS;
+
+		if (temp >= temp_low && temp < temp_high)
+			return i;
+	}
+	if (temp >= temp_high)
Quoted 8 lines

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 430

Quoted 6 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +6 -0
+		ret = i;
+
+	return ret;
+}
+
+static void tuxi_periodic_hw_safeguard(struct work_struct *work)

I wonder if all of this convoluted logic is really necessary. Why can't you just force TUXI_TFAN_FAN_MODE_AUTO if a certain limit is surpassed?

Quoted 2 lines

The auto mode, aka the built in fan curve is way more aggressive than this, and reaching 80°C is very easy on this device.

If it's more aggresive, isn't it safer? IMO we should trust FW instead of hardcoded values.

Quoted 152 lines
Quoted 152 lines

That would mean fewer CPU instructions that are constantly running.

Quoted 138 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +138 -0
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = container_of(work,
+								    struct tuxi_hwmon_driver_data_t,
+								    work.work);
+	struct device *dev = driver_data->hwmdev;
+	struct acpi_device *pdev = to_acpi_device(dev->parent);
+	unsigned long long params[2], retval;
+	unsigned int i;
+	long temp;
+	int ret;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev,
+				       TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+				       params, 1, &retval);
+		/*
+		 * If reading the temperature fails, default to a high value to
+		 * be on the safe side in the worst case.
+		 */
+		if (ret)
+			retval = TUXI_FW_TEMP_OFFSET + 1200;
+
+		temp = retval > S32_MAX / 100 ?
+			S32_MAX : (retval - TUXI_FW_TEMP_OFFSET) * 100;
+
+		driver_data->temp_level[i] = tuxi_get_temp_level(driver_data, i,
+								 temp);
+	}
+
+	// Reapply want_speeds to respect eventual new temp_levels
+	for (i = 0; i < driver_data->fan_count; ++i)
+		write_speed(dev, i, driver_data->want_speed[i], false);
+
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+}
+
+static int tuxi_hwmon_add_devices(struct acpi_device *pdev, struct device **hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data;
+	unsigned long long params[2], retval;
+	unsigned int i;
+	int ret;
+
+	/*
+	 * The first version of TUXI TFAN didn't have the Get Fan Temperature
+	 * method which is integral to this driver. So probe for existence and
+	 * abort otherwise.
+	 *
+	 * The Get Fan Speed method is also missing in that version, but was
+	 * added in the same version so it doesn't have to be probe separately.
+	 */
+	params[0] = 0;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TEMPERATURE,
+			       params, 1, &retval);
+	if (ret)
+		return ret;
+
+	driver_data = devm_kzalloc(&pdev->dev, sizeof(*driver_data), GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	/*
+	 * Loading this module sets the fan mode to auto. Alternative design
+	 * decision could be to keep the current value. This would require
+	 * proper initialization of driver_data->curr_speed for example.
+	 */
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1,
+			       NULL);
+	if (ret)
+		return ret;
+
+	ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_COUNT, NULL, 0,
+			       &retval);
+	if (ret)
+		return ret;
+	if (retval > TUXI_MAX_FAN_COUNT)
+		return -EINVAL;
+	driver_data->fan_count = retval;
+
+	for (i = 0; i < driver_data->fan_count; ++i) {
+		params[0] = i;
+		ret = tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_GET_FAN_TYPE,
+				       params, 1, &retval);
+		if (ret)
+			return ret;
+		if (retval >= ARRAY_SIZE(tuxi_fan_type_labels))
+			return -EOPNOTSUPP;
+		driver_data->fan_types[i] = tuxi_fan_type_labels[retval];
+	}
+
+	*hwmdev = devm_hwmon_device_register_with_info(&pdev->dev,
+						       "tuxedo_nbxx_acpi_tuxi",
+						       driver_data, &hwminfo,
+						       NULL);
+	if (IS_ERR(*hwmdev))
+		return PTR_ERR(*hwmdev);
+
+	driver_data->hwmdev = *hwmdev;
+
+	INIT_DELAYED_WORK(&driver_data->work, tuxi_periodic_hw_safeguard);
+	schedule_delayed_work(&driver_data->work, TUXI_SAFEGUARD_PERIOD);
+
+	return 0;
+}
+
+static void tuxi_hwmon_remove_devices(struct device *hwmdev)
+{
+	struct tuxi_hwmon_driver_data_t *driver_data = dev_get_drvdata(hwmdev);
+	struct acpi_device *pdev = to_acpi_device(hwmdev->parent);
+	unsigned long long params[2];
+
+	cancel_delayed_work_sync(&driver_data->work);
+
+	params[0] = TUXI_TFAN_FAN_MODE_AUTO;
+	tuxi_tfan_method(pdev, TUXI_TFAN_METHOD_SET_FAN_MODE, params, 1, NULL);
+}
+
+static int tuxi_add(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data;
+	acpi_status status;
+
+	driver_data = devm_kzalloc(&device->dev, sizeof(*driver_data),
+				   GFP_KERNEL);
+	if (!driver_data)
+		return -ENOMEM;
+
+	// Find subdevices
+	status = acpi_get_handle(device->handle, "TFAN",
+				 &driver_data->tfan_handle);
+	if (ACPI_FAILURE(status))
+		return_ACPI_STATUS(status);
+
+	dev_set_drvdata(&device->dev, driver_data);
+
+	return tuxi_hwmon_add_devices(device, &driver_data->hwmdev);

drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c line 578

Quoted 10 lines
drivers/platform/x86/tuxedo/nbxx/acpi_tuxi.c new file +10 -0
+}
+
+static void tuxi_remove(struct acpi_device *device)
+{
+	struct tuxi_driver_data_t *driver_data = dev_get_drvdata(&device->dev);
+
+	tuxi_hwmon_remove_devices(driver_data->hwmdev);
+}
+
+static struct acpi_driver acpi_driver = {

AFAIK platform drivers are *strongly* preferred over acpi drivers. You should use that instead.

Quoted 6 lines
Quoted 1 line

Sorry, first time reading this. Can change it ofc, but is there a specific reason?

I'm not really sure. But while samsung-galaxybook was in review, I believe this was requested by Hans and Armin.

Hopefully they can elaborate on this.

Hi! In case it helps I think most of the rationale behind this can be found in the docs:

https://docs.kernel.org/firmware-guide/acpi/enumeration.html#acpi-device-objects

In the case of samsung-galaxybook the issue was that I was originally creating new devices in the driver which were not necessary; both the ACPI device and its corresponding platform device had already been created by the ACPI Core as per the documentation referenced above, it is just a matter of matching and fetching them in the right way.

Quoted 8 lines

I do think in general if the idea is that this Tuxedo platform driver would eventually include more "platform stuff" that it feels more natural to hang everything from a platform device instead of a specific ACPI device (on top of/ presumably some of what the rationale in the documentation is based on).

If it is currently and only will ever be a hwmon device then I suppose one could even argue that this driver belongs under drivers/hwmon instead :) But it seems as though the intention is that there will or could be other things added?

I made a unified branch to showcase what the current goal is https://gitlab.com/tuxedocomputers/development/packages/linux/-/tree/tuxedo-drivers_upstream_wip

We have several, theoretical independent, WMI and ACPI devices on the NB04 platform enabling control of different firmware features:

- Keyboard backlight (wmi_ab device)

- Hotkeys (wmi notify events)

- Fan control (acpi_tuxi device)

- Power profiles (wmi_bs device, atm only a skeleton driver that does nothing)

TBH I don't know if all that should be grouped under one platform driver or should stay separated and in which folder it should go. I will follow whatever the maintainers prefer.

Quoted 6 lines

Just out of curiosity what is the significance of "NBxx"? Is it something that will add value (and should remain in the path as well as various descriptions) or would it make sense that anything falling under drivers/platform/x86/tuxedo will always be "NBxx" without needing to state it again (e.g. will there be other Tuxedo x86 Platform Drivers that are not "NBxx")?

We distinguish different ODMs for our boards in the board_vendor DMI string with NB01, NB02, NB04, and NB05 (NB03 doesn't exist for historical reasons) which all have different firmwares and therefore different drivers in our out-of-tree driver package tuxedo-drivers.

The TUXI interface is actually written by us and theoretical not bound to a singular board_vendor. However, at least currently, it only exists on NB04 devices. But that's why I named the folder nbxx and as you can see in the gitlab link above it is next to the nb04 folder and in the future there also will be nb01, nb02, and nb05.

Best regards,

Werner

Quoted 6 lines

Hope this helps!

Best regards,
Joshua