[PATCH net-next 12/16] net: pcs: xpcs: Add xpcs_create_bynode() method

Serge Semin
2023-12-05T10:37:10Z

It's now possible to have the DW XPCS device defined as a standard
platform device for instance in the platform DT-file. Although it's
pointless unless there is a way to have the device found by the client
drivers (STMMAC/DW *MAC, NXP SJA1105 Eth Switch, etc). Provide such
ability by means of the xpcs_create_bynode() method. It needs to be
supplied with the device fwnode which is equipped with the "pcs-handle"
property pointing to the DW XPCS fw-node (in this regards it looks similar
to the phylink interface). That node will be then used to find the
MDIO-device instance in order to create the DW XPCS descriptor.

Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
---
drivers/net/pcs/pcs-xpcs.c | 26 ++++++++++++++++++++++++++
include/linux/pcs/pcs-xpcs.h | 3 +++
2 files changed, 29 insertions(+)

diff --git a/drivers/net/pcs/pcs-xpcs.c b/drivers/net/pcs/pcs-xpcs.c
index e376e255f1d3..c3336895a124 100644
--- a/drivers/net/pcs/pcs-xpcs.c
+++ b/drivers/net/pcs/pcs-xpcs.c
@@ -9,9 +9,11 @@
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
+#include <linux/fwnode.h>
#include <linux/mdio.h>
#include <linux/module.h>
#include <linux/pcs/pcs-xpcs.h>
+#include <linux/phy.h>
#include <linux/phylink.h>
#include <linux/property.h>

@@ -1511,6 +1513,30 @@ static struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev,
return ERR_PTR(ret);
}

+struct dw_xpcs *xpcs_create_bynode(const struct fwnode_handle *fwnode,
+ phy_interface_t interface)
+{
+ struct fwnode_handle *pcs_node;
+ struct mdio_device *mdiodev;
+ struct dw_xpcs *xpcs;
+
+ pcs_node = fwnode_find_reference(fwnode, "pcs-handle", 0);
+ if (IS_ERR(pcs_node))
+ return ERR_CAST(pcs_node);
+
+ mdiodev = fwnode_mdio_find_device(pcs_node);
+ fwnode_handle_put(pcs_node);
+ if (!mdiodev)
+ return ERR_PTR(-ENODEV);
+
+ xpcs = xpcs_create(mdiodev, interface);
+ if (IS_ERR(xpcs))
+ mdio_device_put(mdiodev);
+
+ return xpcs;
+}
+EXPORT_SYMBOL_GPL(xpcs_create_bynode);
+
struct dw_xpcs *xpcs_create_byaddr(struct mii_bus *bus, int addr,
phy_interface_t interface)
{
diff --git a/include/linux/pcs/pcs-xpcs.h b/include/linux/pcs/pcs-xpcs.h
index b11bbb5e820a..2981dcdf65d4 100644
--- a/include/linux/pcs/pcs-xpcs.h
+++ b/include/linux/pcs/pcs-xpcs.h
@@ -8,6 +8,7 @@
#define __LINUX_PCS_XPCS_H

#include <linux/clk.h>
+#include <linux/fwnode.h>
#include <linux/mdio.h>
#include <linux/phy.h>
#include <linux/phylink.h>
@@ -73,6 +74,8 @@ int xpcs_do_config(struct dw_xpcs *xpcs, phy_interface_t interface,
void xpcs_get_interfaces(struct dw_xpcs *xpcs, unsigned long *interfaces);
int xpcs_config_eee(struct dw_xpcs *xpcs, int mult_fact_100ns,
int enable);
+struct dw_xpcs *xpcs_create_bynode(const struct fwnode_handle *fwnode,
+ phy_interface_t interface);
struct dw_xpcs *xpcs_create_byaddr(struct mii_bus *bus, int addr,
phy_interface_t interface);
void xpcs_destroy(struct dw_xpcs *xpcs);
--
2.42.1