Subject: [PATCH net-next 1/2] r8169: prepare for extending hwmon support

From: Heiner Kallweit <[email-masked]>
Date: Mon, 6 Jan 2025 19:01:16 +0100

This factors out the temperature value calculation and
prepares for adding further temperature attributes.

Signed-off-by: Heiner Kallweit <[email-masked]>
---
 drivers/net/ethernet/realtek/r8169_main.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c
index 5724f650f..38f915956 100644
--- a/drivers/net/ethernet/realtek/r8169_main.c
+++ b/drivers/net/ethernet/realtek/r8169_main.c
@@ -5338,17 +5338,28 @@ static bool rtl_aspm_is_safe(struct rtl8169_private *tp)
 	return false;
 }
 
+static int r8169_hwmon_get_temp(int raw)
+{
+	if (raw >= 512)
+		raw -= 1024;
+
+	return 1000 * raw / 2;
+}
+
 static int r8169_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
 			    u32 attr, int channel, long *val)
 {
 	struct rtl8169_private *tp = dev_get_drvdata(dev);
-	int val_raw;
-
-	val_raw = phy_read_paged(tp->phydev, 0xbd8, 0x12) & 0x3ff;
-	if (val_raw >= 512)
-		val_raw -= 1024;
+	int raw;
 
-	*val = 1000 * val_raw / 2;
+	switch (attr) {
+	case hwmon_temp_input:
+		raw = phy_read_paged(tp->phydev, 0xbd8, 0x12) & 0x3ff;
+		*val = r8169_hwmon_get_temp(raw);
+		break;
+	default:
+		return -EINVAL;
+	}
 
 	return 0;
 }
-- 
2.47.1