Skip to contentSkip to conversation

net: phy: aquantia: add hwmon support

PATCH Superseded Heiner Kallweit 3 participants 6 messages Feb 24, 2019 – Feb 25, 2019

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Heiner Kallweit

author maintainer
#0 lore raw

v2 cover net: phy: aquantia: add hwmon support

This series adds HWMON support for the temperature sensor and the related alarms on the 107/108/109 chips.

v2:
- remove struct aqr_priv
- rename header file to aquantia.h

Heiner Kallweit (2):
net: phy: aquantia: rename aquantia.c to aquantia_main.c
net: phy: aquantia: add hwmon support

 drivers/net/phy/Makefile                      |   1 +
 drivers/net/phy/aquantia.h                    |  16 ++
 drivers/net/phy/aquantia_hwmon.c              | 245 ++++++++++++++++++
 .../net/phy/{aquantia.c => aquantia_main.c}   |   4 +
 4 files changed, 266 insertions(+)
 create mode 100644 drivers/net/phy/aquantia.h
 create mode 100644 drivers/net/phy/aquantia_hwmon.c
 rename drivers/net/phy/{aquantia.c => aquantia_main.c} (99%)
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

PATCH v2 1/2 net: phy: aquantia: rename aquantia.c to aquantia_main.c

Rename aquantia.c to aquantia_main.c to be prepared for adding new functionality to separate source code files.

---
 drivers/net/phy/Makefile                        | 1 +
 drivers/net/phy/{aquantia.c => aquantia_main.c} | 0
 2 files changed, 1 insertion(+)
 rename drivers/net/phy/{aquantia.c => aquantia_main.c} (100%)
drivers/net/phy/Makefile +1 -0
index 5805c0b7d..b0845adaf 100644
@@ -45,6 +45,7 @@ sfp-obj-$(CONFIG_SFP)		+= sfp-bus.o
 obj-y				+= $(sfp-obj-y) $(sfp-obj-m)
 
 obj-$(CONFIG_AMD_PHY)		+= amd.o
+aquantia-objs			+= aquantia_main.o
 obj-$(CONFIG_AQUANTIA_PHY)	+= aquantia.o
 obj-$(CONFIG_ASIX_PHY)		+= asix.o
 obj-$(CONFIG_AT803X_PHY)	+= at803x.o
drivers/net/phy/aquantia_main.c renamed from drivers/net/phy/aquantia.c +0 -0
similarity index 100%
rename from drivers/net/phy/aquantia.c
rename to drivers/net/phy/aquantia_main.c
Signature · 1 line
2.20.1
author maintainer
#2 lore raw

PATCH v2 2/2 net: phy: aquantia: add hwmon support

In reply to Heiner Kallweit

This adds HWMON support for the temperature sensor and the related alarms on the 107/108/109 chips. This patch is based on work from Nikita and Andrew. I added:
- support for changing alarm thresholds via sysfs
- move HWMON code to a separate source file to improve maintainability
- smaller changes like using IS_REACHABLE instead of ifdef (avoids problems if PHY driver is built in and HWMON is a module)

v2:
- remove struct aqr_priv
- rename header file to aquantia.h

---
 drivers/net/phy/Makefile         |   2 +-
 drivers/net/phy/aquantia.h       |  16 ++
 drivers/net/phy/aquantia_hwmon.c | 245 +++++++++++++++++++++++++++++++
 drivers/net/phy/aquantia_main.c  |   4 +
 4 files changed, 266 insertions(+), 1 deletion(-)
 create mode 100644 drivers/net/phy/aquantia.h
 create mode 100644 drivers/net/phy/aquantia_hwmon.c
drivers/net/phy/Makefile +1 -1
index b0845adaf..c48596626 100644
@@ -45,7 +45,7 @@ sfp-obj-$(CONFIG_SFP)		+= sfp-bus.o
 obj-y				+= $(sfp-obj-y) $(sfp-obj-m)
 
 obj-$(CONFIG_AMD_PHY)		+= amd.o
-aquantia-objs			+= aquantia_main.o
+aquantia-objs			+= aquantia_main.o aquantia_hwmon.o
 obj-$(CONFIG_AQUANTIA_PHY)	+= aquantia.o
 obj-$(CONFIG_ASIX_PHY)		+= asix.o
 obj-$(CONFIG_AT803X_PHY)	+= at803x.o
drivers/net/phy/aquantia.h new file +16 -0
new file mode 100644
index 000000000..5a16caab7
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0
+ * HWMON driver for Aquantia PHY
+ *
+ * Author: Nikita Yushchenko <[email protected]>
+ * Author: Andrew Lunn <[email protected]>
+ * Author: Heiner Kallweit <[email protected]>
+ */
+
+#include <linux/device.h>
+#include <linux/phy.h>
+
+#if IS_REACHABLE(CONFIG_HWMON)
+int aqr_hwmon_probe(struct phy_device *phydev);
+#else
+static inline int aqr_hwmon_probe(struct phy_device *phydev) { return 0; }
+#endif
drivers/net/phy/aquantia_hwmon.c new file +245 -0

245 lines not on this page · raw message

drivers/net/phy/aquantia_main.c +4 -0
index 0f0eb5682..37218e5d7 100644
@@ -12,6 +12,8 @@
 #include <linux/delay.h>
 #include <linux/phy.h>
 
+#include "aquantia.h"
+
 #define PHY_ID_AQ1202	0x03a1b445
 #define PHY_ID_AQ2104	0x03a1b460
 #define PHY_ID_AQR105	0x03a1b4a2
@@ -231,6 +233,7 @@ static struct phy_driver aqr_driver[] = {
 	.name		= "Aquantia AQR107",
 	.aneg_done	= genphy_c45_aneg_done,
 	.get_features	= genphy_c45_pma_read_abilities,
+	.probe		= aqr_hwmon_probe,
 	.config_aneg    = aqr_config_aneg,
 	.config_intr	= aqr_config_intr,
 	.ack_interrupt	= aqr_ack_interrupt,
@@ -241,6 +244,7 @@ static struct phy_driver aqr_driver[] = {
 	.name		= "Aquantia AQCS109",
 	.aneg_done	= genphy_c45_aneg_done,
 	.get_features	= genphy_c45_pma_read_abilities,
+	.probe		= aqr_hwmon_probe,
 	.config_init	= aqcs109_config_init,
 	.config_aneg    = aqr_config_aneg,
 	.config_intr	= aqr_config_intr,
Signature · 1 line
2.20.1
#3 lore raw

Le 2/24/19 à 1:36 PM, Heiner Kallweit a écrit :

Quoted 8 lines

This adds HWMON support for the temperature sensor and the related alarms on the 107/108/109 chips. This patch is based on work from Nikita and Andrew. I added:
- support for changing alarm thresholds via sysfs
- move HWMON code to a separate source file to improve maintainability
- smaller changes like using IS_REACHABLE instead of ifdef (avoids problems if PHY driver is built in and HWMON is a module)

Quoted 5 lines

v2:
- remove struct aqr_priv
- rename header file to aquantia.h

Should not Nikita be the author for that patch? Some minor nits below:

Quoted 16 lines
---
 drivers/net/phy/Makefile         |   2 +-
 drivers/net/phy/aquantia.h       |  16 ++
 drivers/net/phy/aquantia_hwmon.c | 245 +++++++++++++++++++++++++++++++
 drivers/net/phy/aquantia_main.c  |   4 +
 4 files changed, 266 insertions(+), 1 deletion(-)
 create mode 100644 drivers/net/phy/aquantia.h
 create mode 100644 drivers/net/phy/aquantia_hwmon.c
drivers/net/phy/Makefile +0 -0
index b0845adaf..c48596626 100644

drivers/net/phy/Makefile sfp-obj-$(CONFIG_SFP) += sfp-bus.o · line 48

Quoted 5 lines
drivers/net/phy/Makefile +1 -1
@@ -45,7 +45,7 @@ sfp-obj-$(CONFIG_SFP)		+= sfp-bus.o
 obj-y				+= $(sfp-obj-y) $(sfp-obj-m)
 
 obj-$(CONFIG_AMD_PHY)		+= amd.o
-aquantia-objs			+= aquantia_main.o
+aquantia-objs			+= aquantia_main.o aquantia_hwmon.o

Since you are adding a stub if CONFIG_HWMON is not reachable, does not that mean you would want to do something like:

ifdef CONFIG_HWMON
aquanti-objs += aquantia_hwmon.o
endif

in this makefile?

[snip]

drivers/net/phy/Makefile sfp-obj-$(CONFIG_SFP) += sfp-bus.o

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+static int aqr_hwmon_get(struct phy_device *phydev, int reg, long *value)
+{
+	int temp = phy_read_mmd(phydev, MDIO_MMD_VEND1, reg);
+
+	if (temp < 0)
+		return temp;
+
+	/* register value is 2's complement with LSB = 1/256th degree Celsius */
+	if (temp > 0x8000)
+		temp -= 0x10000;

Would decimal or BIT() notation be easier to use/read here?

Quoted 7 lines
drivers/net/phy/Makefile +7 -0
+
+	*value = temp * 1000 / 256;
+
+	return 0;
+}
+
+static int aqr_hwmon_set(struct phy_device *phydev, int reg, long value)

drivers/net/phy/Makefile

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+{
+	int temp;
+
+	if (value >= 128000 || value < -128000)
+		return -ERANGE;
+
+	temp = value * 256 / 1000;
+
+	if (temp < 0)
+		temp += 0x10000;

Likewise

Quoted 5 lines
drivers/net/phy/Makefile +5 -0
+
+	return phy_write_mmd(phydev, MDIO_MMD_VEND1, reg, temp);
+}
+
+static int aqr_hwmon_status1(struct phy_device *phydev, int bit, long *value)

drivers/net/phy/Makefile

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+{
+	int reg = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_GENERAL_STAT1);
+
+	if (reg < 0)
+		return reg;
+
+	*value = !!(reg & bit);
+
+	return 0;
+}

Can you also make this function take a register offset as parameter such that you can eliminate open coding that in the case hwmon_temp_input below?

Quoted 8 lines
drivers/net/phy/Makefile +8 -0
+
+static int aqr_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+			  u32 attr, int channel, long *value)
+{
+	struct phy_device *phydev = dev_get_drvdata(dev);
+	int reg;
+
+	if (type != hwmon_temp)

drivers/net/phy/Makefile

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+		return -EOPNOTSUPP;
+
+	switch (attr) {
+	case hwmon_temp_input:
+		reg = phy_read_mmd(phydev, MDIO_MMD_VEND1,
+				   VEND1_THERMAL_STAT2);
+		if (reg < 0)
+			return reg;
+		if (!(reg & VEND1_THERMAL_STAT2_VALID))
+			return -EIO;

And use that helper here.

Quoted 3 lines
drivers/net/phy/Makefile +3 -0
+
+		return aqr_hwmon_get(phydev, VEND1_THERMAL_STAT1, value);
+

[snip]

drivers/net/phy/Makefile

Quoted 12 lines
drivers/net/phy/Makefile +12 -0
+	hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
+	if (!hwmon_name)
+		return -ENOMEM;
+
+	for (i = j = 0; hwmon_name[i]; i++) {
+		if (isalnum(hwmon_name[i])) {
+			if (i != j)
+				hwmon_name[j] = hwmon_name[i];
+			j++;
+		}
+	}
+	hwmon_name[j] = '\0';

I am always baffled that drivers need to take care of these details... but that seems to be the way to go.

Signature · 1 line
Florian
maintainer
#4 lore raw
Quoted 9 lines · Florian Fainelli

Le 2/24/19 à 1:36 PM, Heiner Kallweit a écrit :

Quoted 8 lines

This adds HWMON support for the temperature sensor and the related alarms on the 107/108/109 chips. This patch is based on work from Nikita and Andrew. I added:
- support for changing alarm thresholds via sysfs
- move HWMON code to a separate source file to improve maintainability
- smaller changes like using IS_REACHABLE instead of ifdef (avoids problems if PHY driver is built in and HWMON is a module)

Quoted 10 lines
Quoted 5 lines

v2:
- remove struct aqr_priv
- rename header file to aquantia.h

Should not Nikita be the author for that patch? Some minor nits below:

Quoted 2 lines

Hi Florian

The history is convoluted. I did the initial implementation, but at the time i did not have the hardware. So it got passed to Nikita who did. He made it actually work, and passed patches back. I then passed it onto Heiner who has done all the work on the driver and core for C45.

Andrew

author maintainer
#5 lore raw
In reply to Florian Fainelli
Quoted 42 lines · Florian Fainelli

Le 2/24/19 à 1:36 PM, Heiner Kallweit a écrit :

Quoted 8 lines

This adds HWMON support for the temperature sensor and the related alarms on the 107/108/109 chips. This patch is based on work from Nikita and Andrew. I added:
- support for changing alarm thresholds via sysfs
- move HWMON code to a separate source file to improve maintainability
- smaller changes like using IS_REACHABLE instead of ifdef (avoids problems if PHY driver is built in and HWMON is a module)

Quoted 5 lines

v2:
- remove struct aqr_priv
- rename header file to aquantia.h

Should not Nikita be the author for that patch? Some minor nits below:

Quoted 16 lines
---
 drivers/net/phy/Makefile         |   2 +-
 drivers/net/phy/aquantia.h       |  16 ++
 drivers/net/phy/aquantia_hwmon.c | 245 +++++++++++++++++++++++++++++++
 drivers/net/phy/aquantia_main.c  |   4 +
 4 files changed, 266 insertions(+), 1 deletion(-)
 create mode 100644 drivers/net/phy/aquantia.h
 create mode 100644 drivers/net/phy/aquantia_hwmon.c
drivers/net/phy/Makefile +0 -0
index b0845adaf..c48596626 100644

drivers/net/phy/Makefile sfp-obj-$(CONFIG_SFP) += sfp-bus.o · line 48

Quoted 5 lines
drivers/net/phy/Makefile +1 -1
@@ -45,7 +45,7 @@ sfp-obj-$(CONFIG_SFP)		+= sfp-bus.o
 obj-y				+= $(sfp-obj-y) $(sfp-obj-m)
 
 obj-$(CONFIG_AMD_PHY)		+= amd.o
-aquantia-objs			+= aquantia_main.o
+aquantia-objs			+= aquantia_main.o aquantia_hwmon.o

Since you are adding a stub if CONFIG_HWMON is not reachable, does not that mean you would want to do something like:

Quoted 6 lines

ifdef CONFIG_HWMON
aquanti-objs += aquantia_hwmon.o
endif

in this makefile?

Yepp, this should be changed as suggested by you.

Quoted 15 lines

[snip]

drivers/net/phy/Makefile

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+static int aqr_hwmon_get(struct phy_device *phydev, int reg, long *value)
+{
+	int temp = phy_read_mmd(phydev, MDIO_MMD_VEND1, reg);
+
+	if (temp < 0)
+		return temp;
+
+	/* register value is 2's complement with LSB = 1/256th degree Celsius */
+	if (temp > 0x8000)
+		temp -= 0x10000;

Would decimal or BIT() notation be easier to use/read here?

For the comparison I agree, we can write it as: if (temp & BIT(15)) And yes, the offset we could write decimal.

An alternative could be to write: temp = (s16)temp;
I tested it and it works, but I'm not sure whether this is covered by C standard and works with all supported compilers.

Quoted 30 lines
Quoted 7 lines
drivers/net/phy/Makefile +7 -0
+
+	*value = temp * 1000 / 256;
+
+	return 0;
+}
+
+static int aqr_hwmon_set(struct phy_device *phydev, int reg, long value)

drivers/net/phy/Makefile

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+{
+	int temp;
+
+	if (value >= 128000 || value < -128000)
+		return -ERANGE;
+
+	temp = value * 256 / 1000;
+
+	if (temp < 0)
+		temp += 0x10000;

Likewise

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+
+	return phy_write_mmd(phydev, MDIO_MMD_VEND1, reg, temp);
+}
+
+static int aqr_hwmon_status1(struct phy_device *phydev, int bit, long *value)
+{
+	int reg = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_GENERAL_STAT1);
+
+	if (reg < 0)
+		return reg;
Quoted 9 lines

drivers/net/phy/Makefile

Quoted 5 lines
drivers/net/phy/Makefile +5 -0
+
+	*value = !!(reg & bit);
+
+	return 0;
+}

Can you also make this function take a register offset as parameter such that you can eliminate open coding that in the case hwmon_temp_input below?

OK

Quoted 27 lines
Quoted 8 lines
drivers/net/phy/Makefile +8 -0
+
+static int aqr_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+			  u32 attr, int channel, long *value)
+{
+	struct phy_device *phydev = dev_get_drvdata(dev);
+	int reg;
+
+	if (type != hwmon_temp)

drivers/net/phy/Makefile

Quoted 10 lines
drivers/net/phy/Makefile +10 -0
+		return -EOPNOTSUPP;
+
+	switch (attr) {
+	case hwmon_temp_input:
+		reg = phy_read_mmd(phydev, MDIO_MMD_VEND1,
+				   VEND1_THERMAL_STAT2);
+		if (reg < 0)
+			return reg;
+		if (!(reg & VEND1_THERMAL_STAT2_VALID))
+			return -EIO;

And use that helper here.

Quoted 3 lines
drivers/net/phy/Makefile +3 -0
+
+		return aqr_hwmon_get(phydev, VEND1_THERMAL_STAT1, value);
+

[snip]

Quoted 16 lines

drivers/net/phy/Makefile

Quoted 12 lines
drivers/net/phy/Makefile +12 -0
+	hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
+	if (!hwmon_name)
+		return -ENOMEM;
+
+	for (i = j = 0; hwmon_name[i]; i++) {
+		if (isalnum(hwmon_name[i])) {
+			if (i != j)
+				hwmon_name[j] = hwmon_name[i];
+			j++;
+		}
+	}
+	hwmon_name[j] = '\0';

I am always baffled that drivers need to take care of these details... but that seems to be the way to go.

IIRC we still have a similar construction site in phylib. If a PHY driver name includes e.g. a slash then sysfs is broken. There should be a general helper e.g. in lib/string.c, because we have this across subsystems wherever a device name is used in a sysfs path.