diff mbox series

[net-next,v2,3/3] net: phy: realtek: add hwmon support for temp sensor on RTL822x

Message ID ad6bfe9f-6375-4a00-84b4-bfb38a21bd71@gmail.com (mailing list archive)
State Accepted
Commit 33700ca45b7d2e1655d4cad95e25671e8a94e2f0
Delegated to: Netdev Maintainers
Headers show
Series net: phy: realtek: add hwmon support | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1 this patch: 1
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 3 maintainers not CCed: jdelvare@suse.com linux-hwmon@vger.kernel.org linux@roeck-us.net
netdev/build_clang fail Errors and warnings before: 34 this patch: 35
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 5 this patch: 5
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 83 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Heiner Kallweit Jan. 11, 2025, 8:51 p.m. UTC
This adds hwmon support for the temperature sensor on RTL822x.
It's available on the standalone versions of the PHY's, and on
the integrated PHY's in RTL8125B/RTL8125D/RTL8126.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/net/phy/realtek/Kconfig         |  6 ++
 drivers/net/phy/realtek/Makefile        |  1 +
 drivers/net/phy/realtek/realtek.h       | 10 ++++
 drivers/net/phy/realtek/realtek_hwmon.c | 79 +++++++++++++++++++++++++
 drivers/net/phy/realtek/realtek_main.c  | 12 ++++
 5 files changed, 108 insertions(+)
 create mode 100644 drivers/net/phy/realtek/realtek.h
 create mode 100644 drivers/net/phy/realtek/realtek_hwmon.c

Comments

Andrew Lunn Jan. 11, 2025, 9:52 p.m. UTC | #1
> +config REALTEK_PHY_HWMON
> +	def_bool REALTEK_PHY && HWMON
> +	depends on !(REALTEK_PHY=y && HWMON=m)
> +	help
> +	  Optional hwmon support for the temperature sensor

We frequently end up with build problems with HWMON. All the other
PHYs use:

        depends on HWMON || HWMON=n

We have not yet seen 0-day report issues with your earlier patchsets
versions, but maybe we should keep it the same as all other PHYs? But
maybe it is actually the same, if you apply De Morgan's Law?

	Andrew
Heiner Kallweit Jan. 11, 2025, 10:30 p.m. UTC | #2
On 11.01.2025 22:52, Andrew Lunn wrote:
>> +config REALTEK_PHY_HWMON
>> +	def_bool REALTEK_PHY && HWMON
>> +	depends on !(REALTEK_PHY=y && HWMON=m)
>> +	help
>> +	  Optional hwmon support for the temperature sensor
> 
> We frequently end up with build problems with HWMON. All the other
> PHYs use:
> 
>         depends on HWMON || HWMON=n
> 

The situation is different here. In the other cases HWMON is used from
the main source file. If HWMON=n, then the main source file can be
built as module or be built-in, and the stubs of the HWMON functions
are used.

In my case, if HWMON=n, I want REALTEK_PHY_HWMON to be n, because
then I omit building realtek_hwmon.c (see Makefile).

> We have not yet seen 0-day report issues with your earlier patchsets
> versions, but maybe we should keep it the same as all other PHYs? But
> maybe it is actually the same, if you apply De Morgan's Law?
> 
> 	Andrew

Heiner
Andrew Lunn Jan. 12, 2025, 4:39 p.m. UTC | #3
> In my case, if HWMON=n, I want REALTEK_PHY_HWMON to be n, because
> then I omit building realtek_hwmon.c (see Makefile).

Thanks for the explanation.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew
Geert Uytterhoeven Jan. 21, 2025, 4:10 p.m. UTC | #4
Hi Heiner,

CC hwmon

On Sat, 11 Jan 2025, Heiner Kallweit wrote:
> This adds hwmon support for the temperature sensor on RTL822x.
> It's available on the standalone versions of the PHY's, and on
> the integrated PHY's in RTL8125B/RTL8125D/RTL8126.
>
> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>

Thanks for your patch, which is now commit 33700ca45b7d2e16
("net: phy: realtek: add hwmon support for temp sensor on
RTL822x") in net-next.

> --- a/drivers/net/phy/realtek/Kconfig
> +++ b/drivers/net/phy/realtek/Kconfig
> @@ -3,3 +3,9 @@ config REALTEK_PHY
> 	tristate "Realtek PHYs"
> 	help
> 	  Currently supports RTL821x/RTL822x and fast ethernet PHYs
> +
> +config REALTEK_PHY_HWMON
> +	def_bool REALTEK_PHY && HWMON
> +	depends on !(REALTEK_PHY=y && HWMON=m)
> +	help
> +	  Optional hwmon support for the temperature sensor

So this is optional, but as the symbol is invisible, it cannot be
disabled by the user. Is that intentional?

Gr{oetje,eeting}s,

 						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
 							    -- Linus Torvalds
Heiner Kallweit Jan. 23, 2025, 8:12 p.m. UTC | #5
On 21.01.2025 17:10, Geert Uytterhoeven wrote:
>     Hi Heiner,
> 
> CC hwmon
> 
> On Sat, 11 Jan 2025, Heiner Kallweit wrote:
>> This adds hwmon support for the temperature sensor on RTL822x.
>> It's available on the standalone versions of the PHY's, and on
>> the integrated PHY's in RTL8125B/RTL8125D/RTL8126.
>>
>> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
> 
> Thanks for your patch, which is now commit 33700ca45b7d2e16
> ("net: phy: realtek: add hwmon support for temp sensor on
> RTL822x") in net-next.
> 
>> --- a/drivers/net/phy/realtek/Kconfig
>> +++ b/drivers/net/phy/realtek/Kconfig
>> @@ -3,3 +3,9 @@ config REALTEK_PHY
>>     tristate "Realtek PHYs"
>>     help
>>       Currently supports RTL821x/RTL822x and fast ethernet PHYs
>> +
>> +config REALTEK_PHY_HWMON
>> +    def_bool REALTEK_PHY && HWMON
>> +    depends on !(REALTEK_PHY=y && HWMON=m)
>> +    help
>> +      Optional hwmon support for the temperature sensor
> 
> So this is optional, but as the symbol is invisible, it cannot be
> disabled by the user. Is that intentional?
> 
Well, it isn't intentional in either direction.
Thanks for the hint, I should make it user-visible.

> Gr{oetje,eeting}s,
> 
>                         Geert
> 
> -- 
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                 -- Linus Torvalds
>
diff mbox series

Patch

diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig
index 5b9e6e6db..31935f147 100644
--- a/drivers/net/phy/realtek/Kconfig
+++ b/drivers/net/phy/realtek/Kconfig
@@ -3,3 +3,9 @@  config REALTEK_PHY
 	tristate "Realtek PHYs"
 	help
 	  Currently supports RTL821x/RTL822x and fast ethernet PHYs
+
+config REALTEK_PHY_HWMON
+	def_bool REALTEK_PHY && HWMON
+	depends on !(REALTEK_PHY=y && HWMON=m)
+	help
+	  Optional hwmon support for the temperature sensor
diff --git a/drivers/net/phy/realtek/Makefile b/drivers/net/phy/realtek/Makefile
index 996a80642..dd21cf87f 100644
--- a/drivers/net/phy/realtek/Makefile
+++ b/drivers/net/phy/realtek/Makefile
@@ -1,3 +1,4 @@ 
 # SPDX-License-Identifier: GPL-2.0
 realtek-y			+= realtek_main.o
+realtek-$(CONFIG_REALTEK_PHY_HWMON) += realtek_hwmon.o
 obj-$(CONFIG_REALTEK_PHY)	+= realtek.o
diff --git a/drivers/net/phy/realtek/realtek.h b/drivers/net/phy/realtek/realtek.h
new file mode 100644
index 000000000..a39b44fa1
--- /dev/null
+++ b/drivers/net/phy/realtek/realtek.h
@@ -0,0 +1,10 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef REALTEK_H
+#define REALTEK_H
+
+#include <linux/phy.h>
+
+int rtl822x_hwmon_init(struct phy_device *phydev);
+
+#endif /* REALTEK_H */
diff --git a/drivers/net/phy/realtek/realtek_hwmon.c b/drivers/net/phy/realtek/realtek_hwmon.c
new file mode 100644
index 000000000..1ecb410bb
--- /dev/null
+++ b/drivers/net/phy/realtek/realtek_hwmon.c
@@ -0,0 +1,79 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * HWMON support for Realtek PHY's
+ *
+ * Author: Heiner Kallweit <hkallweit1@gmail.com>
+ */
+
+#include <linux/hwmon.h>
+#include <linux/phy.h>
+
+#include "realtek.h"
+
+#define RTL822X_VND2_TSALRM				0xa662
+#define RTL822X_VND2_TSRR				0xbd84
+#define RTL822X_VND2_TSSR				0xb54c
+
+static int rtl822x_hwmon_get_temp(int raw)
+{
+	if (raw >= 512)
+		raw -= 1024;
+
+	return 1000 * raw / 2;
+}
+
+static int rtl822x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+			      u32 attr, int channel, long *val)
+{
+	struct phy_device *phydev = dev_get_drvdata(dev);
+	int raw;
+
+	switch (attr) {
+	case hwmon_temp_input:
+		raw = phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSRR) & 0x3ff;
+		*val = rtl822x_hwmon_get_temp(raw);
+		break;
+	case hwmon_temp_max:
+		/* Chip reduces speed to 1G if threshold is exceeded */
+		raw = phy_read_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSSR) >> 6;
+		*val = rtl822x_hwmon_get_temp(raw);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static const struct hwmon_ops rtl822x_hwmon_ops = {
+	.visible = 0444,
+	.read = rtl822x_hwmon_read,
+};
+
+static const struct hwmon_channel_info * const rtl822x_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX),
+	NULL
+};
+
+static const struct hwmon_chip_info rtl822x_hwmon_chip_info = {
+	.ops = &rtl822x_hwmon_ops,
+	.info = rtl822x_hwmon_info,
+};
+
+int rtl822x_hwmon_init(struct phy_device *phydev)
+{
+	struct device *hwdev, *dev = &phydev->mdio.dev;
+	const char *name;
+
+	/* Ensure over-temp alarm is reset. */
+	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, RTL822X_VND2_TSALRM, 3);
+
+	name = devm_hwmon_sanitize_name(dev, dev_name(dev));
+	if (IS_ERR(name))
+		return PTR_ERR(name);
+
+	hwdev = devm_hwmon_device_register_with_info(dev, name, phydev,
+						     &rtl822x_hwmon_chip_info,
+						     NULL);
+	return PTR_ERR_OR_ZERO(hwdev);
+}
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index af9874143..38149958d 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -14,6 +14,8 @@ 
 #include <linux/delay.h>
 #include <linux/clk.h>
 
+#include "realtek.h"
+
 #define RTL821x_PHYSR				0x11
 #define RTL821x_PHYSR_DUPLEX			BIT(13)
 #define RTL821x_PHYSR_SPEED			GENMASK(15, 14)
@@ -820,6 +822,15 @@  static int rtl822x_write_mmd(struct phy_device *phydev, int devnum, u16 regnum,
 	return ret;
 }
 
+static int rtl822x_probe(struct phy_device *phydev)
+{
+	if (IS_ENABLED(CONFIG_REALTEK_PHY_HWMON) &&
+	    phydev->phy_id != RTL_GENERIC_PHYID)
+		return rtl822x_hwmon_init(phydev);
+
+	return 0;
+}
+
 static int rtl822xb_config_init(struct phy_device *phydev)
 {
 	bool has_2500, has_sgmii;
@@ -1519,6 +1530,7 @@  static struct phy_driver realtek_drvs[] = {
 		.match_phy_device = rtl_internal_nbaset_match_phy_device,
 		.name           = "Realtek Internal NBASE-T PHY",
 		.flags		= PHY_IS_INTERNAL,
+		.probe		= rtl822x_probe,
 		.get_features   = rtl822x_get_features,
 		.config_aneg    = rtl822x_config_aneg,
 		.read_status    = rtl822x_read_status,