drm/omap: Fix error handling path in 'omap_dmm_probe()'
[linux/fpc-iii.git] / drivers / net / phy / mdio-mux.c
blob7f8e7662e28cbcb97891cb8af27d37c9fbb915c0
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
6 * Copyright (C) 2011, 2012 Cavium, Inc.
7 */
9 #include <linux/platform_device.h>
10 #include <linux/mdio-mux.h>
11 #include <linux/of_mdio.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/phy.h>
16 #define DRV_VERSION "1.0"
17 #define DRV_DESCRIPTION "MDIO bus multiplexer driver"
19 struct mdio_mux_child_bus;
21 struct mdio_mux_parent_bus {
22 struct mii_bus *mii_bus;
23 int current_child;
24 int parent_id;
25 void *switch_data;
26 int (*switch_fn)(int current_child, int desired_child, void *data);
28 /* List of our children linked through their next fields. */
29 struct mdio_mux_child_bus *children;
32 struct mdio_mux_child_bus {
33 struct mii_bus *mii_bus;
34 struct mdio_mux_parent_bus *parent;
35 struct mdio_mux_child_bus *next;
36 int bus_number;
37 int phy_irq[PHY_MAX_ADDR];
41 * The parent bus' lock is used to order access to the switch_fn.
43 static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
45 struct mdio_mux_child_bus *cb = bus->priv;
46 struct mdio_mux_parent_bus *pb = cb->parent;
47 int r;
49 /* In theory multiple mdio_mux could be stacked, thus creating
50 * more than a single level of nesting. But in practice,
51 * SINGLE_DEPTH_NESTING will cover the vast majority of use
52 * cases. We use it, instead of trying to handle the general
53 * case.
55 mutex_lock_nested(&pb->mii_bus->mdio_lock, SINGLE_DEPTH_NESTING);
56 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
57 if (r)
58 goto out;
60 pb->current_child = cb->bus_number;
62 r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
63 out:
64 mutex_unlock(&pb->mii_bus->mdio_lock);
66 return r;
70 * The parent bus' lock is used to order access to the switch_fn.
72 static int mdio_mux_write(struct mii_bus *bus, int phy_id,
73 int regnum, u16 val)
75 struct mdio_mux_child_bus *cb = bus->priv;
76 struct mdio_mux_parent_bus *pb = cb->parent;
78 int r;
80 mutex_lock_nested(&pb->mii_bus->mdio_lock, SINGLE_DEPTH_NESTING);
81 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
82 if (r)
83 goto out;
85 pb->current_child = cb->bus_number;
87 r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
88 out:
89 mutex_unlock(&pb->mii_bus->mdio_lock);
91 return r;
94 static int parent_count;
96 int mdio_mux_init(struct device *dev,
97 int (*switch_fn)(int cur, int desired, void *data),
98 void **mux_handle,
99 void *data)
101 struct device_node *parent_bus_node;
102 struct device_node *child_bus_node;
103 int r, ret_val;
104 struct mii_bus *parent_bus;
105 struct mdio_mux_parent_bus *pb;
106 struct mdio_mux_child_bus *cb;
108 if (!dev->of_node)
109 return -ENODEV;
111 parent_bus_node = of_parse_phandle(dev->of_node, "mdio-parent-bus", 0);
113 if (!parent_bus_node)
114 return -ENODEV;
116 pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
117 if (pb == NULL) {
118 ret_val = -ENOMEM;
119 goto err_parent_bus;
122 parent_bus = of_mdio_find_bus(parent_bus_node);
123 if (parent_bus == NULL) {
124 ret_val = -EPROBE_DEFER;
125 goto err_parent_bus;
128 pb->switch_data = data;
129 pb->switch_fn = switch_fn;
130 pb->current_child = -1;
131 pb->parent_id = parent_count++;
132 pb->mii_bus = parent_bus;
134 ret_val = -ENODEV;
135 for_each_available_child_of_node(dev->of_node, child_bus_node) {
136 u32 v;
138 r = of_property_read_u32(child_bus_node, "reg", &v);
139 if (r)
140 continue;
142 cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
143 if (cb == NULL) {
144 dev_err(dev,
145 "Error: Failed to allocate memory for child\n");
146 ret_val = -ENOMEM;
147 of_node_put(child_bus_node);
148 break;
150 cb->bus_number = v;
151 cb->parent = pb;
153 cb->mii_bus = mdiobus_alloc();
154 if (!cb->mii_bus) {
155 ret_val = -ENOMEM;
156 of_node_put(child_bus_node);
157 break;
159 cb->mii_bus->priv = cb;
160 cb->mii_bus->irq = cb->phy_irq;
161 cb->mii_bus->name = "mdio_mux";
162 snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
163 pb->parent_id, v);
164 cb->mii_bus->parent = dev;
165 cb->mii_bus->read = mdio_mux_read;
166 cb->mii_bus->write = mdio_mux_write;
167 r = of_mdiobus_register(cb->mii_bus, child_bus_node);
168 if (r) {
169 mdiobus_free(cb->mii_bus);
170 devm_kfree(dev, cb);
171 } else {
172 of_node_get(child_bus_node);
173 cb->next = pb->children;
174 pb->children = cb;
177 if (pb->children) {
178 *mux_handle = pb;
179 dev_info(dev, "Version " DRV_VERSION "\n");
180 return 0;
183 /* balance the reference of_mdio_find_bus() took */
184 put_device(&pb->mii_bus->dev);
186 err_parent_bus:
187 of_node_put(parent_bus_node);
188 return ret_val;
190 EXPORT_SYMBOL_GPL(mdio_mux_init);
192 void mdio_mux_uninit(void *mux_handle)
194 struct mdio_mux_parent_bus *pb = mux_handle;
195 struct mdio_mux_child_bus *cb = pb->children;
197 while (cb) {
198 mdiobus_unregister(cb->mii_bus);
199 mdiobus_free(cb->mii_bus);
200 cb = cb->next;
203 /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */
204 put_device(&pb->mii_bus->dev);
206 EXPORT_SYMBOL_GPL(mdio_mux_uninit);
208 MODULE_DESCRIPTION(DRV_DESCRIPTION);
209 MODULE_VERSION(DRV_VERSION);
210 MODULE_AUTHOR("David Daney");
211 MODULE_LICENSE("GPL");