mvebu: Add sub-profiles
[openwrt.git] / target / linux / mvebu / patches-3.14 / 019-add_fixed_phy_register.patch
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()
5
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
8 PHYs:
9
10  * The address of the PHY on the fake bus needs to be passed, while a
11    dynamic allocation is desired.
12
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.
17
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
21 OF node.
22
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>
28
29 --- a/drivers/net/phy/fixed.c
30 +++ b/drivers/net/phy/fixed.c
31 @@ -21,6 +21,7 @@
32  #include <linux/phy_fixed.h>
33  #include <linux/err.h>
34  #include <linux/slab.h>
35 +#include <linux/of.h>
36  
37  #define MII_REGS_NUM 29
38  
39 @@ -203,6 +204,66 @@ err_regs:
40  }
41  EXPORT_SYMBOL_GPL(fixed_phy_add);
42  
43 +void fixed_phy_del(int phy_addr)
44 +{
45 +       struct fixed_mdio_bus *fmb = &platform_fmb;
46 +       struct fixed_phy *fp, *tmp;
47 +
48 +       list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
49 +               if (fp->addr == phy_addr) {
50 +                       list_del(&fp->node);
51 +                       kfree(fp);
52 +                       return;
53 +               }
54 +       }
55 +}
56 +EXPORT_SYMBOL_GPL(fixed_phy_del);
57 +
58 +static int phy_fixed_addr;
59 +static DEFINE_SPINLOCK(phy_fixed_addr_lock);
60 +
61 +int fixed_phy_register(unsigned int irq,
62 +                      struct fixed_phy_status *status,
63 +                      struct device_node *np)
64 +{
65 +       struct fixed_mdio_bus *fmb = &platform_fmb;
66 +       struct phy_device *phy;
67 +       int phy_addr;
68 +       int ret;
69 +
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);
74 +               return -ENOSPC;
75 +       }
76 +       phy_addr = phy_fixed_addr++;
77 +       spin_unlock(&phy_fixed_addr_lock);
78 +
79 +       ret = fixed_phy_add(PHY_POLL, phy_addr, status);
80 +       if (ret < 0)
81 +               return ret;
82 +
83 +       phy = get_phy_device(fmb->mii_bus, phy_addr, false);
84 +       if (!phy || IS_ERR(phy)) {
85 +               fixed_phy_del(phy_addr);
86 +               return -EINVAL;
87 +       }
88 +
89 +       of_node_get(np);
90 +       phy->dev.of_node = np;
91 +
92 +       ret = phy_device_register(phy);
93 +       if (ret) {
94 +               phy_device_free(phy);
95 +               of_node_put(np);
96 +               fixed_phy_del(phy_addr);
97 +               return ret;
98 +       }
99 +
100 +       return 0;
101 +}
102 +
103  static int __init fixed_mdio_bus_init(void)
104  {
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 {
109         int asym_pause;
110  };
111  
112 +struct device_node;
113 +
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);
120  #else
121  static inline int fixed_phy_add(unsigned int irq, int phy_id,
122                                 struct fixed_phy_status *status)
123  {
124         return -ENODEV;
125  }
126 +static inline int fixed_phy_register(unsigned int irq,
127 +                                    struct fixed_phy_status *status,
128 +                                    struct device_node *np)
129 +{
130 +       return -ENODEV;
131 +}
132  #endif /* CONFIG_FIXED_PHY */
133  
134  /*