Skip to content

Commit 118859d

Browse files
committed
Merge: net: phy: realtek: add hwmon support for temp sensor on RTL822x
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-10/-/merge_requests/1263 net: phy: realtek: add hwmon support for temp sensor on RTL822x Depends: !1056 Depends: !1244 JIRA: https://issues.redhat.com/browse/RHEL-106145 Signed-off-by: Izabela Bakollari <ibakolla@redhat.com> Approved-by: Michal Schmidt <mschmidt@redhat.com> Approved-by: Murphy Zhou <xzhou@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Scott Weaver <scweaver@redhat.com>
2 parents 13edca9 + 6757363 commit 118859d

File tree

6 files changed

+117
-3
lines changed

6 files changed

+117
-3
lines changed

drivers/net/phy/realtek/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ config REALTEK_PHY
33
tristate "Realtek PHYs"
44
help
55
Currently supports RTL821x/RTL822x and fast ethernet PHYs
6+
7+
if REALTEK_PHY
8+
9+
config REALTEK_PHY_HWMON
10+
bool "HWMON support for Realtek PHYs"
11+
depends on HWMON && !(REALTEK_PHY=y && HWMON=m)
12+
help
13+
Optional hwmon support for the temperature sensor
14+
15+
endif # REALTEK_PHY

drivers/net/phy/realtek/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# SPDX-License-Identifier: GPL-2.0
22
realtek-y += realtek_main.o
3+
realtek-$(CONFIG_REALTEK_PHY_HWMON) += realtek_hwmon.o
34
obj-$(CONFIG_REALTEK_PHY) += realtek.o

drivers/net/phy/realtek/realtek.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#ifndef REALTEK_H
4+
#define REALTEK_H
5+
6+
#include <linux/phy.h>
7+
8+
int rtl822x_hwmon_init(struct phy_device *phydev);
9+
10+
#endif /* REALTEK_H */
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* HWMON support for Realtek PHY's
4+
*
5+
* Author: Heiner Kallweit <hkallweit1@gmail.com>
6+
*/
7+
8+
#include <linux/hwmon.h>
9+
#include <linux/phy.h>
10+
11+
#include "realtek.h"
12+
13+
#define RTL822X_VND2_TSALRM 0xa662
14+
#define RTL822X_VND2_TSRR 0xbd84
15+
#define RTL822X_VND2_TSSR 0xb54c
16+
17+
static int rtl822x_hwmon_get_temp(int raw)
18+
{
19+
if (raw >= 512)
20+
raw -= 1024;
21+
22+
return 1000 * raw / 2;
23+
}
24+
25+
static int rtl822x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
26+
u32 attr, int channel, long *val)
27+
{
28+
struct phy_device *phydev = dev_get_drvdata(dev);
29+
int raw;
30+
31+
switch (attr) {
32+
case hwmon_temp_input:
33+
raw = phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSRR) & 0x3ff;
34+
*val = rtl822x_hwmon_get_temp(raw);
35+
break;
36+
case hwmon_temp_max:
37+
/* Chip reduces speed to 1G if threshold is exceeded */
38+
raw = phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSSR) >> 6;
39+
*val = rtl822x_hwmon_get_temp(raw);
40+
break;
41+
default:
42+
return -EINVAL;
43+
}
44+
45+
return 0;
46+
}
47+
48+
static const struct hwmon_ops rtl822x_hwmon_ops = {
49+
.visible = 0444,
50+
.read = rtl822x_hwmon_read,
51+
};
52+
53+
static const struct hwmon_channel_info * const rtl822x_hwmon_info[] = {
54+
HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX),
55+
NULL
56+
};
57+
58+
static const struct hwmon_chip_info rtl822x_hwmon_chip_info = {
59+
.ops = &rtl822x_hwmon_ops,
60+
.info = rtl822x_hwmon_info,
61+
};
62+
63+
int rtl822x_hwmon_init(struct phy_device *phydev)
64+
{
65+
struct device *hwdev, *dev = &phydev->mdio.dev;
66+
const char *name;
67+
68+
/* Ensure over-temp alarm is reset. */
69+
phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSALRM, 3);
70+
71+
name = devm_hwmon_sanitize_name(dev, dev_name(dev));
72+
if (IS_ERR(name))
73+
return PTR_ERR(name);
74+
75+
hwdev = devm_hwmon_device_register_with_info(dev, name, phydev,
76+
&rtl822x_hwmon_chip_info,
77+
NULL);
78+
return PTR_ERR_OR_ZERO(hwdev);
79+
}

drivers/net/phy/realtek/realtek_main.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
#include <linux/clk.h>
1717
#include <linux/string_choices.h>
1818

19-
#define RTL821x_PHYSR 0x11
20-
#define RTL821x_PHYSR_DUPLEX BIT(13)
21-
#define RTL821x_PHYSR_SPEED GENMASK(15, 14)
19+
#include "realtek.h"
2220

2321
#define RTL8201F_IER 0x13
2422

@@ -1025,6 +1023,15 @@ static int rtl822x_write_mmd(struct phy_device *phydev, int devnum, u16 regnum,
10251023
return ret;
10261024
}
10271025

1026+
static int rtl822x_probe(struct phy_device *phydev)
1027+
{
1028+
if (IS_ENABLED(CONFIG_REALTEK_PHY_HWMON) &&
1029+
phydev->phy_id != RTL_GENERIC_PHYID)
1030+
return rtl822x_hwmon_init(phydev);
1031+
1032+
return 0;
1033+
}
1034+
10281035
static int rtl822xb_config_init(struct phy_device *phydev)
10291036
{
10301037
bool has_2500, has_sgmii;
@@ -1684,6 +1691,7 @@ static struct phy_driver realtek_drvs[] = {
16841691
}, {
16851692
.match_phy_device = rtl8221b_vb_cg_c22_match_phy_device,
16861693
.name = "RTL8221B-VB-CG 2.5Gbps PHY (C22)",
1694+
.probe = rtl822x_probe,
16871695
.get_features = rtl822x_get_features,
16881696
.config_aneg = rtl822x_config_aneg,
16891697
.config_init = rtl822xb_config_init,
@@ -1696,6 +1704,7 @@ static struct phy_driver realtek_drvs[] = {
16961704
}, {
16971705
.match_phy_device = rtl8221b_vb_cg_c45_match_phy_device,
16981706
.name = "RTL8221B-VB-CG 2.5Gbps PHY (C45)",
1707+
.probe = rtl822x_probe,
16991708
.config_init = rtl822xb_config_init,
17001709
.get_rate_matching = rtl822xb_get_rate_matching,
17011710
.get_features = rtl822x_c45_get_features,
@@ -1706,6 +1715,7 @@ static struct phy_driver realtek_drvs[] = {
17061715
}, {
17071716
.match_phy_device = rtl8221b_vn_cg_c22_match_phy_device,
17081717
.name = "RTL8221B-VM-CG 2.5Gbps PHY (C22)",
1718+
.probe = rtl822x_probe,
17091719
.get_features = rtl822x_get_features,
17101720
.config_aneg = rtl822x_config_aneg,
17111721
.config_init = rtl822xb_config_init,
@@ -1718,6 +1728,7 @@ static struct phy_driver realtek_drvs[] = {
17181728
}, {
17191729
.match_phy_device = rtl8221b_vn_cg_c45_match_phy_device,
17201730
.name = "RTL8221B-VN-CG 2.5Gbps PHY (C45)",
1731+
.probe = rtl822x_probe,
17211732
.config_init = rtl822xb_config_init,
17221733
.get_rate_matching = rtl822xb_get_rate_matching,
17231734
.get_features = rtl822x_c45_get_features,
@@ -1728,6 +1739,7 @@ static struct phy_driver realtek_drvs[] = {
17281739
}, {
17291740
.match_phy_device = rtl8251b_c45_match_phy_device,
17301741
.name = "RTL8251B 5Gbps PHY",
1742+
.probe = rtl822x_probe,
17311743
.get_features = rtl822x_get_features,
17321744
.config_aneg = rtl822x_config_aneg,
17331745
.read_status = rtl822x_read_status,
@@ -1739,6 +1751,7 @@ static struct phy_driver realtek_drvs[] = {
17391751
.match_phy_device = rtl_internal_nbaset_match_phy_device,
17401752
.name = "Realtek Internal NBASE-T PHY",
17411753
.flags = PHY_IS_INTERNAL,
1754+
.probe = rtl822x_probe,
17421755
.get_features = rtl822x_get_features,
17431756
.config_aneg = rtl822x_config_aneg,
17441757
.read_status = rtl822x_read_status,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_REALTEK_PHY_HWMON=y

0 commit comments

Comments
 (0)