[v4,2/3] phy: handle optional regulator for PHY

Message ID 20221115073603.3425396-3-clabbe@baylibre.com
State New
Headers
Series arm64: add ethernet to orange pi 3 |

Commit Message

Corentin LABBE Nov. 15, 2022, 7:36 a.m. UTC
  Add handling of optional regulators for PHY.
Regulators need to be enabled before PHY scanning, so MDIO bus
initiate this task.

Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
---
 drivers/net/mdio/fwnode_mdio.c | 31 ++++++++++++++++++++++++++++++-
 drivers/net/phy/phy_device.c   | 10 ++++++++++
 include/linux/phy.h            |  3 +++
 3 files changed, 43 insertions(+), 1 deletion(-)
  

Comments

Andrew Lunn Nov. 16, 2022, 1:51 a.m. UTC | #1
On Tue, Nov 15, 2022 at 07:36:02AM +0000, Corentin Labbe wrote:
> Add handling of optional regulators for PHY.
> Regulators need to be enabled before PHY scanning, so MDIO bus
> initiate this task.
> 
> Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
> ---
>  drivers/net/mdio/fwnode_mdio.c | 31 ++++++++++++++++++++++++++++++-
>  drivers/net/phy/phy_device.c   | 10 ++++++++++
>  include/linux/phy.h            |  3 +++
>  3 files changed, 43 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
> index 689e728345ce..19a16072d4ca 100644
> --- a/drivers/net/mdio/fwnode_mdio.c
> +++ b/drivers/net/mdio/fwnode_mdio.c
> @@ -10,6 +10,7 @@
>  #include <linux/fwnode_mdio.h>
>  #include <linux/of.h>
>  #include <linux/phy.h>
> +#include <linux/regulator/consumer.h>
>  #include <linux/pse-pd/pse.h>

These headers are sorted, so please add regulator after pse.

>  
>  MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
> @@ -116,7 +117,9 @@ int fwnode_mdiobus_register_phy(struct mii_bus *bus,
>  	struct phy_device *phy;
>  	bool is_c45 = false;
>  	u32 phy_id;
> -	int rc;
> +	int rc, reg_cnt = 0;
> +	struct regulator_bulk_data *consumers = NULL;
> +	struct device_node __maybe_unused *nchild = NULL;

Reverse Christmas tree.

>  
>  	psec = fwnode_find_pse_control(child);
>  	if (IS_ERR(psec))
> @@ -133,6 +136,26 @@ int fwnode_mdiobus_register_phy(struct mii_bus *bus,
>  	if (rc >= 0)
>  		is_c45 = true;
>  
> +#ifdef CONFIG_OF

Do you need this #ifdef ? Generally, all of_* functions should have
stubs if CONFIG_OF is not enabled. It would be nice to remove this, so
we get compile testing. And the __maybe_unused above is then probably
not needed.

> +	for_each_child_of_node(bus->dev.of_node, nchild) {
> +		u32 reg;
> +
> +		of_property_read_u32(nchild, "reg", &reg);
> +		if (reg != addr)
> +			continue;
> +		reg_cnt = of_regulator_bulk_get_all(&bus->dev, nchild, &consumers);
> +		if (reg_cnt > 0) {
> +			rc = regulator_bulk_enable(reg_cnt, consumers);
> +			if (rc)
> +				return rc;
> +		}
> +		if (reg_cnt < 0) {
> +			dev_err(&bus->dev, "Fail to regulator_bulk_get_all err=%d\n", reg_cnt);
> +			return reg_cnt;
> +		}
> +	}
> +#endif
> +

	Andrew
  
Andrew Lunn Nov. 16, 2022, 1:58 a.m. UTC | #2
> +		reg_cnt = of_regulator_bulk_get_all(&bus->dev, nchild, &consumers);

This allocates memory for consumers?

I don't see it being freed. I think you need to add to
phy_remove. Plus the error patch should also free it.

	    Andrew
  

Patch

diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
index 689e728345ce..19a16072d4ca 100644
--- a/drivers/net/mdio/fwnode_mdio.c
+++ b/drivers/net/mdio/fwnode_mdio.c
@@ -10,6 +10,7 @@ 
 #include <linux/fwnode_mdio.h>
 #include <linux/of.h>
 #include <linux/phy.h>
+#include <linux/regulator/consumer.h>
 #include <linux/pse-pd/pse.h>
 
 MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
@@ -116,7 +117,9 @@  int fwnode_mdiobus_register_phy(struct mii_bus *bus,
 	struct phy_device *phy;
 	bool is_c45 = false;
 	u32 phy_id;
-	int rc;
+	int rc, reg_cnt = 0;
+	struct regulator_bulk_data *consumers = NULL;
+	struct device_node __maybe_unused *nchild = NULL;
 
 	psec = fwnode_find_pse_control(child);
 	if (IS_ERR(psec))
@@ -133,6 +136,26 @@  int fwnode_mdiobus_register_phy(struct mii_bus *bus,
 	if (rc >= 0)
 		is_c45 = true;
 
+#ifdef CONFIG_OF
+	for_each_child_of_node(bus->dev.of_node, nchild) {
+		u32 reg;
+
+		of_property_read_u32(nchild, "reg", &reg);
+		if (reg != addr)
+			continue;
+		reg_cnt = of_regulator_bulk_get_all(&bus->dev, nchild, &consumers);
+		if (reg_cnt > 0) {
+			rc = regulator_bulk_enable(reg_cnt, consumers);
+			if (rc)
+				return rc;
+		}
+		if (reg_cnt < 0) {
+			dev_err(&bus->dev, "Fail to regulator_bulk_get_all err=%d\n", reg_cnt);
+			return reg_cnt;
+		}
+	}
+#endif
+
 	if (is_c45 || fwnode_get_phy_id(child, &phy_id))
 		phy = get_phy_device(bus, addr, is_c45);
 	else
@@ -142,6 +165,9 @@  int fwnode_mdiobus_register_phy(struct mii_bus *bus,
 		goto clean_mii_ts;
 	}
 
+	phy->regulator_cnt = reg_cnt;
+	phy->consumers = consumers;
+
 	if (is_acpi_node(child)) {
 		phy->irq = bus->irq[addr];
 
@@ -180,6 +206,9 @@  int fwnode_mdiobus_register_phy(struct mii_bus *bus,
 clean_pse:
 	pse_control_put(psec);
 
+	if (reg_cnt > 0)
+		regulator_bulk_disable(reg_cnt, consumers);
+
 	return rc;
 }
 EXPORT_SYMBOL(fwnode_mdiobus_register_phy);
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 57849ac0384e..957e27c75eb2 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -28,6 +28,7 @@ 
 #include <linux/phy_led_triggers.h>
 #include <linux/pse-pd/pse.h>
 #include <linux/property.h>
+#include <linux/regulator/consumer.h>
 #include <linux/sfp.h>
 #include <linux/skbuff.h>
 #include <linux/slab.h>
@@ -1818,6 +1819,9 @@  int phy_suspend(struct phy_device *phydev)
 	if (!ret)
 		phydev->suspended = true;
 
+	if (phydev->regulator_cnt > 0)
+		regulator_bulk_disable(phydev->regulator_cnt, phydev->consumers);
+
 	return ret;
 }
 EXPORT_SYMBOL(phy_suspend);
@@ -1844,6 +1848,12 @@  int phy_resume(struct phy_device *phydev)
 {
 	int ret;
 
+	if (phydev->regulator_cnt > 0) {
+		ret = regulator_bulk_enable(phydev->regulator_cnt, phydev->consumers);
+		if (ret)
+			return ret;
+	}
+
 	mutex_lock(&phydev->lock);
 	ret = __phy_resume(phydev);
 	mutex_unlock(&phydev->lock);
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 9a3752c0c444..5d1311b35cc3 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -729,6 +729,9 @@  struct phy_device {
 	void (*phy_link_change)(struct phy_device *phydev, bool up);
 	void (*adjust_link)(struct net_device *dev);
 
+	int regulator_cnt;
+	struct regulator_bulk_data *consumers;
+
 #if IS_ENABLED(CONFIG_MACSEC)
 	/* MACsec management functions */
 	const struct macsec_ops *macsec_ops;