1 From a75951217472c522c324adb0a4de3ba69d656ef5 Mon Sep 17 00:00:00 2001
2 From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
3 Date: Fri, 16 May 2014 16:14:04 +0200
4 Subject: net: phy: extend fixed driver with fixed_phy_register()
6 The existing fixed_phy_add() function has several drawbacks that
7 prevents it from being used as is for OF-based declaration of fixed
10 * The address of the PHY on the fake bus needs to be passed, while a
11 dynamic allocation is desired.
13 * Since the phy_device instantiation is post-poned until the next
14 mdiobus scan, there is no way to associate the fixed PHY with its
15 OF node, which later prevents of_phy_connect() from finding this
16 fixed PHY from a given OF node.
18 To solve this, this commit introduces fixed_phy_register(), which will
19 allocate an available PHY address, add the PHY using fixed_phy_add()
20 and instantiate the phy_device structure associated with the provided
23 Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
24 Acked-by: Florian Fainelli <f.fainelli@gmail.com>
25 Acked-by: Grant Likely <grant.likely@linaro.org>
26 Tested-by: Florian Fainelli <f.fainelli@gmail.com>
27 Signed-off-by: David S. Miller <davem@davemloft.net>
29 --- a/drivers/net/phy/fixed.c
30 +++ b/drivers/net/phy/fixed.c
32 #include <linux/phy_fixed.h>
33 #include <linux/err.h>
34 #include <linux/slab.h>
35 +#include <linux/of.h>
37 #define MII_REGS_NUM 29
39 @@ -203,6 +204,66 @@ err_regs:
41 EXPORT_SYMBOL_GPL(fixed_phy_add);
43 +void fixed_phy_del(int phy_addr)
45 + struct fixed_mdio_bus *fmb = &platform_fmb;
46 + struct fixed_phy *fp, *tmp;
48 + list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
49 + if (fp->addr == phy_addr) {
50 + list_del(&fp->node);
56 +EXPORT_SYMBOL_GPL(fixed_phy_del);
58 +static int phy_fixed_addr;
59 +static DEFINE_SPINLOCK(phy_fixed_addr_lock);
61 +int fixed_phy_register(unsigned int irq,
62 + struct fixed_phy_status *status,
63 + struct device_node *np)
65 + struct fixed_mdio_bus *fmb = &platform_fmb;
66 + struct phy_device *phy;
70 + /* Get the next available PHY address, up to PHY_MAX_ADDR */
71 + spin_lock(&phy_fixed_addr_lock);
72 + if (phy_fixed_addr == PHY_MAX_ADDR) {
73 + spin_unlock(&phy_fixed_addr_lock);
76 + phy_addr = phy_fixed_addr++;
77 + spin_unlock(&phy_fixed_addr_lock);
79 + ret = fixed_phy_add(PHY_POLL, phy_addr, status);
83 + phy = get_phy_device(fmb->mii_bus, phy_addr, false);
84 + if (!phy || IS_ERR(phy)) {
85 + fixed_phy_del(phy_addr);
90 + phy->dev.of_node = np;
92 + ret = phy_device_register(phy);
94 + phy_device_free(phy);
96 + fixed_phy_del(phy_addr);
103 static int __init fixed_mdio_bus_init(void)
105 struct fixed_mdio_bus *fmb = &platform_fmb;
106 --- a/include/linux/phy_fixed.h
107 +++ b/include/linux/phy_fixed.h
108 @@ -9,15 +9,26 @@ struct fixed_phy_status {
114 #ifdef CONFIG_FIXED_PHY
115 extern int fixed_phy_add(unsigned int irq, int phy_id,
116 struct fixed_phy_status *status);
117 +extern int fixed_phy_register(unsigned int irq,
118 + struct fixed_phy_status *status,
119 + struct device_node *np);
121 static inline int fixed_phy_add(unsigned int irq, int phy_id,
122 struct fixed_phy_status *status)
126 +static inline int fixed_phy_register(unsigned int irq,
127 + struct fixed_phy_status *status,
128 + struct device_node *np)
132 #endif /* CONFIG_FIXED_PHY */