2403da5d55519fb9aa3a7f89615ec8ed998f314e
[openwrt/staging/blocktrron.git] /
1 From 1ffcc8d41306fd2e5f140b276820714a26a11cc4 Mon Sep 17 00:00:00 2001
2 From: Heiner Kallweit <hkallweit1@gmail.com>
3 Date: Mon, 7 Oct 2024 20:34:12 +0200
4 Subject: [PATCH] r8169: add support for the temperature sensor being available
5 from RTL8125B
6
7 This adds support for the temperature sensor being available from
8 RTL8125B. Register information was taken from r8125 vendor driver.
9
10 Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
11 Reviewed-by: Simon Horman <horms@kernel.org>
12 Signed-off-by: David S. Miller <davem@davemloft.net>
13 ---
14 drivers/net/ethernet/realtek/r8169_main.c | 44 +++++++++++++++++++++++
15 1 file changed, 44 insertions(+)
16
17 --- a/drivers/net/ethernet/realtek/r8169_main.c
18 +++ b/drivers/net/ethernet/realtek/r8169_main.c
19 @@ -16,6 +16,7 @@
20 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/ethtool.h>
23 +#include <linux/hwmon.h>
24 #include <linux/phy.h>
25 #include <linux/if_vlan.h>
26 #include <linux/in.h>
27 @@ -5373,6 +5374,43 @@ static bool rtl_aspm_is_safe(struct rtl8
28 return false;
29 }
30
31 +static umode_t r8169_hwmon_is_visible(const void *drvdata,
32 + enum hwmon_sensor_types type,
33 + u32 attr, int channel)
34 +{
35 + return 0444;
36 +}
37 +
38 +static int r8169_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
39 + u32 attr, int channel, long *val)
40 +{
41 + struct rtl8169_private *tp = dev_get_drvdata(dev);
42 + int val_raw;
43 +
44 + val_raw = phy_read_paged(tp->phydev, 0xbd8, 0x12) & 0x3ff;
45 + if (val_raw >= 512)
46 + val_raw -= 1024;
47 +
48 + *val = 1000 * val_raw / 2;
49 +
50 + return 0;
51 +}
52 +
53 +static const struct hwmon_ops r8169_hwmon_ops = {
54 + .is_visible = r8169_hwmon_is_visible,
55 + .read = r8169_hwmon_read,
56 +};
57 +
58 +static const struct hwmon_channel_info * const r8169_hwmon_info[] = {
59 + HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
60 + NULL
61 +};
62 +
63 +static const struct hwmon_chip_info r8169_hwmon_chip_info = {
64 + .ops = &r8169_hwmon_ops,
65 + .info = r8169_hwmon_info,
66 +};
67 +
68 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
69 {
70 struct rtl8169_private *tp;
71 @@ -5547,6 +5585,12 @@ static int rtl_init_one(struct pci_dev *
72 if (rc)
73 return rc;
74
75 + /* The temperature sensor is available from RTl8125B */
76 + if (IS_REACHABLE(CONFIG_HWMON) && tp->mac_version >= RTL_GIGA_MAC_VER_63)
77 + /* ignore errors */
78 + devm_hwmon_device_register_with_info(&pdev->dev, "nic_temp", tp,
79 + &r8169_hwmon_chip_info,
80 + NULL);
81 rc = register_netdev(dev);
82 if (rc)
83 return rc;