Skip to content

Commit 471e8fd

Browse files
Ansueldavem330
authored andcommitted
net: phy: add devm/of_phy_package_join helper
Add devm/of_phy_package_join helper to join PHYs in a PHY package. These are variant of the manual phy_package_join with the difference that these will use DT nodes to derive the base_addr instead of manually passing an hardcoded value. An additional value is added in phy_package_shared, "np" to reference the PHY package node pointer in specific PHY driver probe_once and config_init_once functions to make use of additional specific properties defined in the PHY package node in DT. The np value is filled only with of_phy_package_join if a valid PHY package node is found. A valid PHY package node must have the node name set to "ethernet-phy-package". Signed-off-by: Christian Marangi <[email protected]> Reviewed-by: Andrew Lunn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 385ef48 commit 471e8fd

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

drivers/net/phy/phy_device.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,7 @@ int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
17121712
shared->priv_size = priv_size;
17131713
}
17141714
shared->base_addr = base_addr;
1715+
shared->np = NULL;
17151716
refcount_set(&shared->refcnt, 1);
17161717
bus->shared[base_addr] = shared;
17171718
} else {
@@ -1734,6 +1735,63 @@ int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
17341735
}
17351736
EXPORT_SYMBOL_GPL(phy_package_join);
17361737

1738+
/**
1739+
* of_phy_package_join - join a common PHY group in PHY package
1740+
* @phydev: target phy_device struct
1741+
* @priv_size: if non-zero allocate this amount of bytes for private data
1742+
*
1743+
* This is a variant of phy_package_join for PHY package defined in DT.
1744+
*
1745+
* The parent node of the @phydev is checked as a valid PHY package node
1746+
* structure (by matching the node name "ethernet-phy-package") and the
1747+
* base_addr for the PHY package is passed to phy_package_join.
1748+
*
1749+
* With this configuration the shared struct will also have the np value
1750+
* filled to use additional DT defined properties in PHY specific
1751+
* probe_once and config_init_once PHY package OPs.
1752+
*
1753+
* Returns < 0 on error, 0 on success. Esp. calling phy_package_join()
1754+
* with the same cookie but a different priv_size is an error. Or a parent
1755+
* node is not detected or is not valid or doesn't match the expected node
1756+
* name for PHY package.
1757+
*/
1758+
int of_phy_package_join(struct phy_device *phydev, size_t priv_size)
1759+
{
1760+
struct device_node *node = phydev->mdio.dev.of_node;
1761+
struct device_node *package_node;
1762+
u32 base_addr;
1763+
int ret;
1764+
1765+
if (!node)
1766+
return -EINVAL;
1767+
1768+
package_node = of_get_parent(node);
1769+
if (!package_node)
1770+
return -EINVAL;
1771+
1772+
if (!of_node_name_eq(package_node, "ethernet-phy-package")) {
1773+
ret = -EINVAL;
1774+
goto exit;
1775+
}
1776+
1777+
if (of_property_read_u32(package_node, "reg", &base_addr)) {
1778+
ret = -EINVAL;
1779+
goto exit;
1780+
}
1781+
1782+
ret = phy_package_join(phydev, base_addr, priv_size);
1783+
if (ret)
1784+
goto exit;
1785+
1786+
phydev->shared->np = package_node;
1787+
1788+
return 0;
1789+
exit:
1790+
of_node_put(package_node);
1791+
return ret;
1792+
}
1793+
EXPORT_SYMBOL_GPL(of_phy_package_join);
1794+
17371795
/**
17381796
* phy_package_leave - leave a common PHY group
17391797
* @phydev: target phy_device struct
@@ -1750,6 +1808,10 @@ void phy_package_leave(struct phy_device *phydev)
17501808
if (!shared)
17511809
return;
17521810

1811+
/* Decrease the node refcount on leave if present */
1812+
if (shared->np)
1813+
of_node_put(shared->np);
1814+
17531815
if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
17541816
bus->shared[shared->base_addr] = NULL;
17551817
mutex_unlock(&bus->shared_lock);
@@ -1802,6 +1864,40 @@ int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
18021864
}
18031865
EXPORT_SYMBOL_GPL(devm_phy_package_join);
18041866

1867+
/**
1868+
* devm_of_phy_package_join - resource managed of_phy_package_join()
1869+
* @dev: device that is registering this PHY package
1870+
* @phydev: target phy_device struct
1871+
* @priv_size: if non-zero allocate this amount of bytes for private data
1872+
*
1873+
* Managed of_phy_package_join(). Shared storage fetched by this function,
1874+
* phy_package_leave() is automatically called on driver detach. See
1875+
* of_phy_package_join() for more information.
1876+
*/
1877+
int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
1878+
size_t priv_size)
1879+
{
1880+
struct phy_device **ptr;
1881+
int ret;
1882+
1883+
ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
1884+
GFP_KERNEL);
1885+
if (!ptr)
1886+
return -ENOMEM;
1887+
1888+
ret = of_phy_package_join(phydev, priv_size);
1889+
1890+
if (!ret) {
1891+
*ptr = phydev;
1892+
devres_add(dev, ptr);
1893+
} else {
1894+
devres_free(ptr);
1895+
}
1896+
1897+
return ret;
1898+
}
1899+
EXPORT_SYMBOL_GPL(devm_of_phy_package_join);
1900+
18051901
/**
18061902
* phy_detach - detach a PHY device from its network device
18071903
* @phydev: target phy_device struct

include/linux/phy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ struct mdio_bus_stats {
329329
* struct phy_package_shared - Shared information in PHY packages
330330
* @base_addr: Base PHY address of PHY package used to combine PHYs
331331
* in one package and for offset calculation of phy_package_read/write
332+
* @np: Pointer to the Device Node if PHY package defined in DT
332333
* @refcnt: Number of PHYs connected to this shared data
333334
* @flags: Initialization of PHY package
334335
* @priv_size: Size of the shared private data @priv
@@ -340,6 +341,8 @@ struct mdio_bus_stats {
340341
*/
341342
struct phy_package_shared {
342343
u8 base_addr;
344+
/* With PHY package defined in DT this points to the PHY package node */
345+
struct device_node *np;
343346
refcount_t refcnt;
344347
unsigned long flags;
345348
size_t priv_size;
@@ -2000,9 +2003,12 @@ int phy_ethtool_set_link_ksettings(struct net_device *ndev,
20002003
const struct ethtool_link_ksettings *cmd);
20012004
int phy_ethtool_nway_reset(struct net_device *ndev);
20022005
int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size);
2006+
int of_phy_package_join(struct phy_device *phydev, size_t priv_size);
20032007
void phy_package_leave(struct phy_device *phydev);
20042008
int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
20052009
int base_addr, size_t priv_size);
2010+
int devm_of_phy_package_join(struct device *dev, struct phy_device *phydev,
2011+
size_t priv_size);
20062012

20072013
int __init mdio_bus_init(void);
20082014
void mdio_bus_exit(void);

0 commit comments

Comments
 (0)