diff mbox series

[net-next,v2,04/14] net: phy: Introduce fwnode_get_phy_id()

Message ID 20201215164315.3666-5-calvin.johnson@oss.nxp.com
State New
Headers show
Series ACPI support for dpaa2 driver | expand

Commit Message

Calvin Johnson Dec. 15, 2020, 4:43 p.m. UTC
Extract phy_id from compatible string. This will be used by
fwnode_mdiobus_register_phy() to create phy device using the
phy_id.

Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com>
---

Changes in v2: None

 drivers/net/phy/phy_device.c | 21 +++++++++++++++++++++
 include/linux/phy.h          |  5 +++++
 2 files changed, 26 insertions(+)

Comments

Calvin Johnson Dec. 17, 2020, 8:28 a.m. UTC | #1
On Tue, Dec 15, 2020 at 07:28:10PM +0200, Andy Shevchenko wrote:
> On Tue, Dec 15, 2020 at 6:44 PM Calvin Johnson
> <calvin.johnson@oss.nxp.com> wrote:
> >
> > Extract phy_id from compatible string. This will be used by
> > fwnode_mdiobus_register_phy() to create phy device using the
> > phy_id.
> 
> ...
> 
> > +       if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
> > +               *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
> > +               return 0;
> > +       }
> > +       return -EINVAL;
> 
> Perhaps traditional pattern, i.e.
>        if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2)
>                return -EINVAL;
> 
>        *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
>        return 0;
> 
> And perhaps GENMASK() ?

Sure. Will rewrite accordingly.

Thanks
Calvin
Andy Shevchenko Dec. 17, 2020, 9:44 a.m. UTC | #2
On Thu, Dec 17, 2020 at 10:28 AM Calvin Johnson
<calvin.johnson@oss.nxp.com> wrote:
> On Tue, Dec 15, 2020 at 07:28:10PM +0200, Andy Shevchenko wrote:

> > On Tue, Dec 15, 2020 at 6:44 PM Calvin Johnson

> > <calvin.johnson@oss.nxp.com> wrote:


...

> > > +       if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {


> >        *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);


> > And perhaps GENMASK() ?

>

> Sure. Will rewrite accordingly.


Reading this again I'm now not sure these masks are needed at all.

-- 
With Best Regards,
Andy Shevchenko
diff mbox series

Patch

diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index c153273606c1..6fad89c02c5a 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -846,6 +846,27 @@  static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
 	return 0;
 }
 
+/* Extract the phy ID from the compatible string of the form
+ * ethernet-phy-idAAAA.BBBB.
+ */
+int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
+{
+	unsigned int upper, lower;
+	const char *cp;
+	int ret;
+
+	ret = fwnode_property_read_string(fwnode, "compatible", &cp);
+	if (ret)
+		return ret;
+
+	if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
+		*phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
+		return 0;
+	}
+	return -EINVAL;
+}
+EXPORT_SYMBOL(fwnode_get_phy_id);
+
 /**
  * get_phy_device - reads the specified PHY device and returns its @phy_device
  *		    struct
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 7790a9a56d0f..10a66b65a008 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -1341,6 +1341,7 @@  struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
 				     bool is_c45,
 				     struct phy_c45_device_ids *c45_ids);
 #if IS_ENABLED(CONFIG_PHYLIB)
+int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id);
 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode);
 struct phy_device *device_phy_find_device(struct device *dev);
 struct fwnode_handle *fwnode_get_phy_node(struct fwnode_handle *fwnode);
@@ -1348,6 +1349,10 @@  struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
 int phy_device_register(struct phy_device *phy);
 void phy_device_free(struct phy_device *phydev);
 #else
+static inline int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
+{
+	return 0;
+}
 static inline
 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
 {