Skip to contentSkip to conversation

platform/x86: thinkpad_acpi: Fix fan speed reporting on X9 Gen 1

PATCH In review Vishnu Sankar 3 participants 3 messages Nov 26 – Nov 27

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: thinkpad_acpi: Fix fan speed reporting on X9 Gen 1

On the ThinkPad X9-14 Gen 1 and X9-15 Gen 1, the EC reports fan1 RPM in registers 0x84/0x85 and fan2 RPM in 0x82/0x83, and does not use bit 0 of 0x31 as a fan select. As a result fan2 reports fan1's speed, and every fan read overwrites an EC-owned bit.

The EC also does not latch either register pair, so a read can combine the LSB of a new value with the MSB of an old one.

Add two quirks: TPACPI_FAN_2REG reads fan2 from 0x82/0x83 and never touches 0x31, and TPACPI_FAN_UNLATCH re-reads an RPM register pair until the MSB is stable. Enable both for the X9 Gen 1 models.

A single fan level write controls both fans on these models, so second fan control is disabled when TPACPI_FAN_2REG is set.

Tested on X9-14 Gen 1 (BIOS N4DET42W, EC N4DHT39W).

---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 79 ++++++++++++++++++---
 1 file changed, 68 insertions(+), 11 deletions(-)
drivers/platform/x86/lenovo/thinkpad_acpi.c +68 -11
index 4f81855ad721..266f774b8ac3 100644
@@ -7964,6 +7964,8 @@ enum {					/* Fan control constants */
 	fan_select_offset = 0x31,	/* EC register 0x31 (Firmware 7M)
 					   bit 0 selects which fan is active */
 
+	fan2_rpm_offset = 0x82,		/* EC register 0x82: LSB, 0x83 MSB (fan2 RPM) */
+
 	fan_status_offset_ns = 0x93,	/* Special status/control offset for non-standard EC Fan1 */
 	fan2_status_offset_ns = 0x96,	/* Special status/control offset for non-standard EC Fan2 */
 	fan_rpm_status_ns = 0x95,	/* Special offset for Fan1 RPM status for non-standard EC */
@@ -8012,6 +8014,8 @@ static int fan_watchdog_maxinterval;
 static bool fan_with_ns_addr;
 static bool ecfw_with_fan_dec_rpm;
 static bool fan_speed_in_tpr;
+static bool fan2_with_own_rpm_reg;
+static bool fan_rpm_unlatched;
 
 static struct mutex fan_mutex;
 
@@ -8073,6 +8077,9 @@ static void fan_quirk1_handle(u8 *fan_status)
 /* Select main fan on X60/X61, NOOP on others */
 static bool fan_select_fan1(void)
 {
+	if (fan2_with_own_rpm_reg)
+		return true;
+
 	if (tp_features.second_fan) {
 		u8 val;
 
@@ -8093,6 +8100,9 @@ static bool fan_select_fan2(void)
 	if (!tp_features.second_fan)
 		return false;
 
+	if (fan2_with_own_rpm_reg)
+		return true;
+
 	if (ec_read(fan_select_offset, &val) < 0)
 		return false;
 	val |= 0x01U;
@@ -8202,21 +8212,48 @@ static int fan_get_status_safe(u8 *status)
 	return 0;
 }
 
+/* Read a 16-bit RPM register pair (LSB at offset, MSB at offset + 1) */
+static bool fan_read_rpm(u8 offset, unsigned int *rpm)
+{
+	u8 hi, lo, hi2;
+	int retries = 3;
+
+	if (fan_rpm_unlatched) {
+		/* No latch: re-read until the MSB is stable */
+		do {
+			if (!acpi_ec_read(offset + 1, &hi) ||
+			    !acpi_ec_read(offset, &lo) ||
+			    !acpi_ec_read(offset + 1, &hi2))
+				return false;
+		} while (hi != hi2 && --retries);
+
+		if (hi != hi2)
+			return false;
+	} else {
+		if (!acpi_ec_read(offset, &lo) || !acpi_ec_read(offset + 1, &hi))
+			return false;
+	}
+
+	*rpm = (hi << 8) | lo;
+	return true;
+}
+
 static int fan_get_speed(unsigned int *speed)
 {
-	u8 hi, lo;
+	unsigned int rpm;
+	u8 lo;
 
 	switch (fan_status_access_mode) {
 	case TPACPI_FAN_RD_TPEC:
 		/* all except 570, 600e/x, 770e, 770x */
 		if (unlikely(!fan_select_fan1()))
 			return -EIO;
-		if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
-			     !acpi_ec_read(fan_rpm_offset + 1, &hi)))
+
+		if (unlikely(!fan_read_rpm(fan_rpm_offset, &rpm)))
 			return -EIO;
 
 		if (likely(speed)) {
-			*speed = (hi << 8) | lo;
+			*speed = rpm;
 			if (fan_speed_in_tpr && *speed != 0)
 				*speed = FAN_CLOCK_TPM / *speed;
 		}
@@ -8238,7 +8275,8 @@ static int fan_get_speed(unsigned int *speed)
 
 static int fan2_get_speed(unsigned int *speed)
 {
-	u8 hi, lo, status;
+	unsigned int rpm;
+	u8 lo, status;
 	bool rc;
 
 	switch (fan_status_access_mode) {
@@ -8246,14 +8284,16 @@ static int fan2_get_speed(unsigned int *speed)
 		/* all except 570, 600e/x, 770e, 770x */
 		if (unlikely(!fan_select_fan2()))
 			return -EIO;
-		rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
-			     !acpi_ec_read(fan_rpm_offset + 1, &hi);
+
+		rc = !fan_read_rpm(fan2_with_own_rpm_reg ? fan2_rpm_offset : fan_rpm_offset,
+				   &rpm);
+
 		fan_select_fan1(); /* play it safe */
 		if (rc)
 			return -EIO;
 
 		if (likely(speed)) {
-			*speed = (hi << 8) | lo;
+			*speed = rpm;
 			if (fan_speed_in_tpr && *speed != 0)
 				*speed = FAN_CLOCK_TPM / *speed;
 		}
@@ -8873,6 +8913,8 @@ static const struct attribute_group fan_driver_attr_group = {
 #define TPACPI_FAN_DECRPM	0x0020		/* For ECFW's with RPM in register as decimal */
 #define TPACPI_FAN_TPR		0x0040		/* Fan speed is in Ticks Per Revolution */
 #define TPACPI_FAN_NOACPI	0x0080		/* Don't use ACPI methods even if detected */
+#define TPACPI_FAN_2REG		0x0100		/* Fan2 RPM in EC regs 0x82/0x83, no fan select */
+#define TPACPI_FAN_UNLATCH	0x0200		/* RPM registers are not latched */
 
 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
 	TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
@@ -8908,6 +8950,8 @@ static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
 	TPACPI_Q_LNV3('R', '0', '0', TPACPI_FAN_NOACPI),/* E560 */
 	TPACPI_Q_LNV3('R', '1', '2', TPACPI_FAN_NOACPI),/* T495 */
 	TPACPI_Q_LNV3('R', '1', '3', TPACPI_FAN_NOACPI),/* T495s */
+	TPACPI_Q_LNV3('N', '4', 'D', TPACPI_FAN_2REG | TPACPI_FAN_UNLATCH),	/* X9-14 Gen 1 */
+	TPACPI_Q_LNV3('N', '4', 'C', TPACPI_FAN_2REG | TPACPI_FAN_UNLATCH),	/* X9-15 Gen 1 */
 };
 
 static int __init fan_init(struct ibm_init_struct *iibm)
@@ -8959,6 +9003,12 @@ static int __init fan_init(struct ibm_init_struct *iibm)
 		tp_features.fan_ctrl_status_undef = 1;
 	}
 
+	if (quirks & TPACPI_FAN_2REG)
+		fan2_with_own_rpm_reg = true;
+
+	if (quirks & TPACPI_FAN_UNLATCH)
+		fan_rpm_unlatched = true;
+
 	if (quirks & TPACPI_FAN_NOACPI) {
 		/* E560, T495, T495s */
 		pr_info("Ignoring buggy ACPI fan access method\n");
@@ -8993,9 +9043,16 @@ static int __init fan_init(struct ibm_init_struct *iibm)
 			if (res >= 0 && speed != FAN_NOT_PRESENT) {
 				/* It responded - so let's assume it's there */
 				tp_features.second_fan = 1;
-				/* fan control not currently available for ns ECFW */
-				tp_features.second_fan_ctl = !fan_with_ns_addr;
-				pr_info("secondary fan control detected & enabled\n");
+				/*
+				 * fan control not currently available for ns ECFW,
+				 * and not needed with 2REG: one write drives both fans
+				 */
+				tp_features.second_fan_ctl = !fan_with_ns_addr &&
+							     !fan2_with_own_rpm_reg;
+				if (tp_features.second_fan_ctl)
+					pr_info("secondary fan control detected & enabled\n");
+				else
+					pr_info("secondary fan detected\n");
 			} else {
 				/* Fan not auto-detected */
 				tp_features.second_fan = 0;
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/1
#1 lore raw

Thanks Vishnu,

Quoted 213 lines · Vishnu Sankar

On the ThinkPad X9-14 Gen 1 and X9-15 Gen 1, the EC reports fan1 RPM in registers 0x84/0x85 and fan2 RPM in 0x82/0x83, and does not use bit 0 of 0x31 as a fan select. As a result fan2 reports fan1's speed, and every fan read overwrites an EC-owned bit.

The EC also does not latch either register pair, so a read can combine the LSB of a new value with the MSB of an old one.

Add two quirks: TPACPI_FAN_2REG reads fan2 from 0x82/0x83 and never touches 0x31, and TPACPI_FAN_UNLATCH re-reads an RPM register pair until the MSB is stable. Enable both for the X9 Gen 1 models.

A single fan level write controls both fans on these models, so second fan control is disabled when TPACPI_FAN_2REG is set.

Tested on X9-14 Gen 1 (BIOS N4DET42W, EC N4DHT39W).

---
 drivers/platform/x86/lenovo/thinkpad_acpi.c | 79 ++++++++++++++++++---
 1 file changed, 68 insertions(+), 11 deletions(-)
drivers/platform/x86/lenovo/thinkpad_acpi.c +1 -0
index 4f81855ad721..266f774b8ac3 100644
@@ -7964,6 +7964,8 @@ enum {					/* Fan control constants */
 	fan_select_offset = 0x31,	/* EC register 0x31 (Firmware 7M)
 					   bit 0 selects which fan is active */
 
+	fan2_rpm_offset = 0x82,		/* EC register 0x82: LSB, 0x83 MSB (fan2 

RPM) */

drivers/platform/x86/lenovo/thinkpad_acpi.c +1 -0
+
 	fan_status_offset_ns = 0x93,	/* Special status/control offset for 

non-standard EC Fan1 */
fan2_status_offset_ns = 0x96, /* Special status/control offset for
non-standard EC Fan2 */
fan_rpm_status_ns = 0x95, /* Special offset for Fan1 RPM status for
non-standard EC */

drivers/platform/x86/lenovo/thinkpad_acpi.c +46 -8
@@ -8012,6 +8014,8 @@ static int fan_watchdog_maxinterval;
 static bool fan_with_ns_addr;
 static bool ecfw_with_fan_dec_rpm;
 static bool fan_speed_in_tpr;
+static bool fan2_with_own_rpm_reg;
+static bool fan_rpm_unlatched;
 
 static struct mutex fan_mutex;
 
@@ -8073,6 +8077,9 @@ static void fan_quirk1_handle(u8 *fan_status)
 /* Select main fan on X60/X61, NOOP on others */
 static bool fan_select_fan1(void)
 {
+	if (fan2_with_own_rpm_reg)
+		return true;
+
 	if (tp_features.second_fan) {
 		u8 val;
 
@@ -8093,6 +8100,9 @@ static bool fan_select_fan2(void)
 	if (!tp_features.second_fan)
 		return false;
 
+	if (fan2_with_own_rpm_reg)
+		return true;
+
 	if (ec_read(fan_select_offset, &val) < 0)
 		return false;
 	val |= 0x01U;
@@ -8202,21 +8212,48 @@ static int fan_get_status_safe(u8 *status)
 	return 0;
 }
 
+/* Read a 16-bit RPM register pair (LSB at offset, MSB at offset + 1) */
+static bool fan_read_rpm(u8 offset, unsigned int *rpm)
+{
+	u8 hi, lo, hi2;
+	int retries = 3;
+
+	if (fan_rpm_unlatched) {
+		/* No latch: re-read until the MSB is stable */
+		do {
+			if (!acpi_ec_read(offset + 1, &hi) ||
+			    !acpi_ec_read(offset, &lo) ||
+			    !acpi_ec_read(offset + 1, &hi2))
+				return false;
+		} while (hi != hi2 && --retries);
+
+		if (hi != hi2)
+			return false;
+	} else {
+		if (!acpi_ec_read(offset, &lo) || !acpi_ec_read(offset + 1, &hi))
+			return false;
+	}
+
+	*rpm = (hi << 8) | lo;
+	return true;
+}
+
 static int fan_get_speed(unsigned int *speed)
 {
-	u8 hi, lo;
+	unsigned int rpm;
+	u8 lo;
 
 	switch (fan_status_access_mode) {
 	case TPACPI_FAN_RD_TPEC:
 		/* all except 570, 600e/x, 770e, 770x */
 		if (unlikely(!fan_select_fan1()))
 			return -EIO;
-		if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
-			     !acpi_ec_read(fan_rpm_offset + 1, &hi)))
+
+		if (unlikely(!fan_read_rpm(fan_rpm_offset, &rpm)))
 			return -EIO;
 
 		if (likely(speed)) {
-			*speed = (hi << 8) | lo;
+			*speed = rpm;
 			if (fan_speed_in_tpr && *speed != 0)
 				*speed = FAN_CLOCK_TPM / *speed;
 		}
@@ -8238,7 +8275,8 @@ static int fan_get_speed(unsigned int *speed)
 
 static int fan2_get_speed(unsigned int *speed)
 {
-	u8 hi, lo, status;
+	unsigned int rpm;
+	u8 lo, status;
 	bool rc;
 
 	switch (fan_status_access_mode) {
@@ -8246,14 +8284,16 @@ static int fan2_get_speed(unsigned int *speed)
 		/* all except 570, 600e/x, 770e, 770x */
 		if (unlikely(!fan_select_fan2()))
 			return -EIO;
-		rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
-			     !acpi_ec_read(fan_rpm_offset + 1, &hi);
+
+		rc = !fan_read_rpm(fan2_with_own_rpm_reg ? fan2_rpm_offset : fan_rpm_offset,
+				   &rpm);
+
 		fan_select_fan1(); /* play it safe */
 		if (rc)
 			return -EIO;
 
 		if (likely(speed)) {
-			*speed = (hi << 8) | lo;
+			*speed = rpm;
 			if (fan_speed_in_tpr && *speed != 0)
 				*speed = FAN_CLOCK_TPM / *speed;
 		}
@@ -8873,6 +8913,8 @@ static const struct attribute_group 
 fan_driver_attr_group = {
 #define TPACPI_FAN_DECRPM	0x0020		/* For ECFW's with RPM in register 

as decimal */
#define TPACPI_FAN_TPR 0x0040 /* Fan speed is in Ticks Per
Revolution */
#define TPACPI_FAN_NOACPI 0x0080 /* Don't use ACPI methods even if
detected */
+#define TPACPI_FAN_2REG 0x0100 /* Fan2 RPM in EC regs 0x82/0x83, no
fan select */

drivers/platform/x86/lenovo/thinkpad_acpi.c +2 -0
+#define TPACPI_FAN_UNLATCH	0x0200		/* RPM registers are not latched */
 
 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
 	TPACPI_QEC_IBM('1', 'Y', TPACPI_FAN_Q1),
@@ -8908,6 +8950,8 @@ static const struct tpacpi_quirk 
 fan_quirk_table[] __initconst = {
 	TPACPI_Q_LNV3('R', '0', '0', TPACPI_FAN_NOACPI),/* E560 */
 	TPACPI_Q_LNV3('R', '1', '2', TPACPI_FAN_NOACPI),/* T495 */
 	TPACPI_Q_LNV3('R', '1', '3', TPACPI_FAN_NOACPI),/* T495s */
+	TPACPI_Q_LNV3('N', '4', 'D', TPACPI_FAN_2REG | 

TPACPI_FAN_UNLATCH), /* X9-14 Gen 1 */

drivers/platform/x86/lenovo/thinkpad_acpi.c +1 -0
+	TPACPI_Q_LNV3('N', '4', 'C', TPACPI_FAN_2REG | 

TPACPI_FAN_UNLATCH), /* X9-15 Gen 1 */
};

static int __init fan_init(struct ibm_init_struct *iibm)

drivers/platform/x86/lenovo/thinkpad_acpi.c +16 -3
@@ -8959,6 +9003,12 @@ static int __init fan_init(struct ibm_init_struct *iibm)
 		tp_features.fan_ctrl_status_undef = 1;
 	}
 
+	if (quirks & TPACPI_FAN_2REG)
+		fan2_with_own_rpm_reg = true;
+
+	if (quirks & TPACPI_FAN_UNLATCH)
+		fan_rpm_unlatched = true;
+
 	if (quirks & TPACPI_FAN_NOACPI) {
 		/* E560, T495, T495s */
 		pr_info("Ignoring buggy ACPI fan access method\n");
@@ -8993,9 +9043,16 @@ static int __init fan_init(struct ibm_init_struct *iibm)
 			if (res >= 0 && speed != FAN_NOT_PRESENT) {
 				/* It responded - so let's assume it's there */
 				tp_features.second_fan = 1;
-				/* fan control not currently available for ns ECFW */
-				tp_features.second_fan_ctl = !fan_with_ns_addr;
-				pr_info("secondary fan control detected & enabled\n");
+				/*
+				 * fan control not currently available for ns ECFW,
+				 * and not needed with 2REG: one write drives both fans
+				 */
+				tp_features.second_fan_ctl = !fan_with_ns_addr &&
+							     !fan2_with_own_rpm_reg;
+				if (tp_features.second_fan_ctl)
+					pr_info("secondary fan control detected & enabled\n");
+				else
+					pr_info("secondary fan detected\n");
 			} else {
 				/* Fan not auto-detected */
 				tp_features.second_fan = 0;
Signature · 1 line
2.53.0

Looks good to me.

Mark

Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH] platform/x86: thinkpad acpi: Fix fan speed reporting on X9 Gen 1

In reply to Vishnu Sankar
Quoted 4 lines · Vishnu Sankar

On the ThinkPad X9-14 Gen 1 and X9-15 Gen 1, the EC reports fan1 RPM in registers 0x84/0x85 and fan2 RPM in 0x82/0x83, and does not use bit 0 of 0x31 as a fan select. As a result fan2 reports fan1's speed, and every fan read overwrites an EC-owned bit.

For context from the other side of this history: we ran into the X9-14 Gen 1 EC layout back in August from the thermal side (bug 221228), tried to reuse the non-standard quirk-table offsets at the time, and reverted after the EC data showed the X9 has its own register layout - so it's good to see the fan side done right this time, based on what the EC actually implements.

Verified against the current source:

- The new quirk bits (TPACPI_FAN_2REG 0x0100, TPACPI_FAN_UNLATCH 0x0200) don't collide with the existing fan quirk flags (0x0002/0x0004).
- With 2REG, fan_select_fan1()/fan_select_fan2() become no-ops, which removes both problems in one go: fan2 no longer aliases fan1's registers, and the 0x31 writes that clobbered an EC-owned bit are gone.
- The UNLATCH re-read (MSB, LSB, MSB again until stable) is the right guard for a non-latching register pair.

One thing worth a line in the commit message, maybe: the unlatched path reads the MSB first, while the latched path keeps the "read 0x84 before 0x85" ordering that older EC firmware requires. Your testing shows the X9 EC doesn't have that old quirk, but a note that the read order is intentionally different would save the next person some archaeology.

Either way, that's a nit, not an objection: