2022-05-23 06:49:30

by Corentin LABBE

[permalink] [raw]
Subject: [PATCH v3 2/3] phy: handle optional regulator for PHY

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 <[email protected]>
Signed-off-by: Corentin Labbe <[email protected]>
---
drivers/net/mdio/Kconfig | 1 +
drivers/net/mdio/fwnode_mdio.c | 36 ++++++++++++++++++++++++++++++----
drivers/net/phy/phy_device.c | 10 ++++++++++
include/linux/phy.h | 3 +++
4 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/drivers/net/mdio/Kconfig b/drivers/net/mdio/Kconfig
index bfa16826a6e1..3f8098fac74b 100644
--- a/drivers/net/mdio/Kconfig
+++ b/drivers/net/mdio/Kconfig
@@ -22,6 +22,7 @@ config MDIO_BUS
config FWNODE_MDIO
def_tristate PHYLIB
depends on (ACPI || OF) || COMPILE_TEST
+ depends on REGULATOR
select FIXED_PHY
help
FWNODE MDIO bus (Ethernet PHY) accessors
diff --git a/drivers/net/mdio/fwnode_mdio.c b/drivers/net/mdio/fwnode_mdio.c
index 1c1584fca632..7f0d3bc81c52 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>

MODULE_AUTHOR("Calvin Johnson <[email protected]>");
MODULE_LICENSE("GPL");
@@ -94,7 +95,10 @@ 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;
+ struct device_node *nchild = NULL;
+ u32 reg;

mii_ts = fwnode_find_mii_timestamper(child);
if (IS_ERR(mii_ts))
@@ -105,15 +109,35 @@ int fwnode_mdiobus_register_phy(struct mii_bus *bus,
if (rc >= 0)
is_c45 = true;

+ for_each_child_of_node(bus->dev.of_node, nchild) {
+ of_property_read_u32(nchild, "reg", &reg);
+ if (reg != addr)
+ continue;
+ reg_cnt = 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;
+ }
+ }
+
if (is_c45 || fwnode_get_phy_id(child, &phy_id))
phy = get_phy_device(bus, addr, is_c45);
else
phy = phy_device_create(bus, addr, phy_id, 0, NULL);
if (IS_ERR(phy)) {
unregister_mii_timestamper(mii_ts);
- return PTR_ERR(phy);
+ rc = PTR_ERR(phy);
+ goto error;
}

+ phy->regulator_cnt = reg_cnt;
+ phy->consumers = consumers;
+
if (is_acpi_node(child)) {
phy->irq = bus->irq[addr];

@@ -127,14 +151,14 @@ int fwnode_mdiobus_register_phy(struct mii_bus *bus,
if (rc) {
phy_device_free(phy);
fwnode_handle_put(phy->mdio.dev.fwnode);
- return rc;
+ goto error;
}
} else if (is_of_node(child)) {
rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
if (rc) {
unregister_mii_timestamper(mii_ts);
phy_device_free(phy);
- return rc;
+ goto error;
}
}

@@ -145,5 +169,9 @@ int fwnode_mdiobus_register_phy(struct mii_bus *bus,
if (mii_ts)
phy->mii_ts = mii_ts;
return 0;
+error:
+ 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 431a8719c635..711919e40ef7 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -27,6 +27,7 @@
#include <linux/phy.h>
#include <linux/phy_led_triggers.h>
#include <linux/property.h>
+#include <linux/regulator/consumer.h>
#include <linux/sfp.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
@@ -1785,6 +1786,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);
@@ -1811,6 +1815,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 508f1149665b..ef4e0ce67194 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -704,6 +704,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;
--
2.35.1



2022-05-23 09:15:16

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH v3 2/3] phy: handle optional regulator for PHY

Hi Corentin,

I love your patch! Perhaps something to improve:

[auto build test WARNING on broonie-regulator/for-next]
[also build test WARNING on sunxi/sunxi/for-next linus/master v5.18 next-20220520]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/intel-lab-lkp/linux/commits/Corentin-Labbe/arm64-add-ethernet-to-orange-pi-3/20220523-133344
base: https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-next
config: mips-cu1830-neo_defconfig (https://download.01.org/0day-ci/archive/20220523/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project 768a1ca5eccb678947f4155e38a5f5744dcefb56)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mips-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/7ae2ab7d1efe8091f6b7ea048a7ac495afba9e46
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Corentin-Labbe/arm64-add-ethernet-to-orange-pi-3/20220523-133344
git checkout 7ae2ab7d1efe8091f6b7ea048a7ac495afba9e46
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash drivers/net/mdio/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

In file included from drivers/net/mdio/of_mdio.c:13:
>> include/linux/fwnode_mdio.h:20:5: warning: no previous prototype for function 'fwnode_mdiobus_phy_device_register' [-Wmissing-prototypes]
int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
^
include/linux/fwnode_mdio.h:20:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
^
static
1 warning generated.


vim +/fwnode_mdiobus_phy_device_register +20 include/linux/fwnode_mdio.h

bc1bee3b87ee48b Calvin Johnson 2021-06-11 10
bc1bee3b87ee48b Calvin Johnson 2021-06-11 11 #if IS_ENABLED(CONFIG_FWNODE_MDIO)
bc1bee3b87ee48b Calvin Johnson 2021-06-11 12 int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
bc1bee3b87ee48b Calvin Johnson 2021-06-11 13 struct phy_device *phy,
bc1bee3b87ee48b Calvin Johnson 2021-06-11 14 struct fwnode_handle *child, u32 addr);
bc1bee3b87ee48b Calvin Johnson 2021-06-11 15
bc1bee3b87ee48b Calvin Johnson 2021-06-11 16 int fwnode_mdiobus_register_phy(struct mii_bus *bus,
bc1bee3b87ee48b Calvin Johnson 2021-06-11 17 struct fwnode_handle *child, u32 addr);
bc1bee3b87ee48b Calvin Johnson 2021-06-11 18
bc1bee3b87ee48b Calvin Johnson 2021-06-11 19 #else /* CONFIG_FWNODE_MDIO */
bc1bee3b87ee48b Calvin Johnson 2021-06-11 @20 int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
bc1bee3b87ee48b Calvin Johnson 2021-06-11 21 struct phy_device *phy,
bc1bee3b87ee48b Calvin Johnson 2021-06-11 22 struct fwnode_handle *child, u32 addr)
bc1bee3b87ee48b Calvin Johnson 2021-06-11 23 {
bc1bee3b87ee48b Calvin Johnson 2021-06-11 24 return -EINVAL;
bc1bee3b87ee48b Calvin Johnson 2021-06-11 25 }
bc1bee3b87ee48b Calvin Johnson 2021-06-11 26

--
0-DAY CI Kernel Test Service
https://01.org/lkp