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
6 * Copyright (C) 2011, 2012 Cavium, Inc.
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_DESCRIPTION "MDIO bus multiplexer driver"
18 struct mdio_mux_child_bus;
20 struct mdio_mux_parent_bus {
21 struct mii_bus *mii_bus;
25 int (*switch_fn)(int current_child, int desired_child, void *data);
27 /* List of our children linked through their next fields. */
28 struct mdio_mux_child_bus *children;
31 struct mdio_mux_child_bus {
32 struct mii_bus *mii_bus;
33 struct mdio_mux_parent_bus *parent;
34 struct mdio_mux_child_bus *next;
39 * The parent bus' lock is used to order access to the switch_fn.
41 static int mdio_mux_read(struct mii_bus *bus, int phy_id, int regnum)
43 struct mdio_mux_child_bus *cb = bus->priv;
44 struct mdio_mux_parent_bus *pb = cb->parent;
47 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
48 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
52 pb->current_child = cb->bus_number;
54 r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
56 mutex_unlock(&pb->mii_bus->mdio_lock);
62 * The parent bus' lock is used to order access to the switch_fn.
64 static int mdio_mux_write(struct mii_bus *bus, int phy_id,
67 struct mdio_mux_child_bus *cb = bus->priv;
68 struct mdio_mux_parent_bus *pb = cb->parent;
72 mutex_lock_nested(&pb->mii_bus->mdio_lock, MDIO_MUTEX_MUX);
73 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
77 pb->current_child = cb->bus_number;
79 r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
81 mutex_unlock(&pb->mii_bus->mdio_lock);
86 static int parent_count;
88 static void mdio_mux_uninit_children(struct mdio_mux_parent_bus *pb)
90 struct mdio_mux_child_bus *cb = pb->children;
93 mdiobus_unregister(cb->mii_bus);
94 mdiobus_free(cb->mii_bus);
99 int mdio_mux_init(struct device *dev,
100 struct device_node *mux_node,
101 int (*switch_fn)(int cur, int desired, void *data),
104 struct mii_bus *mux_bus)
106 struct device_node *parent_bus_node;
107 struct device_node *child_bus_node;
109 struct mii_bus *parent_bus;
110 struct mdio_mux_parent_bus *pb;
111 struct mdio_mux_child_bus *cb;
117 parent_bus_node = of_parse_phandle(mux_node,
118 "mdio-parent-bus", 0);
120 if (!parent_bus_node)
123 parent_bus = of_mdio_find_bus(parent_bus_node);
125 ret_val = -EPROBE_DEFER;
129 parent_bus_node = NULL;
130 parent_bus = mux_bus;
131 get_device(&parent_bus->dev);
134 pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
140 pb->switch_data = data;
141 pb->switch_fn = switch_fn;
142 pb->current_child = -1;
143 pb->parent_id = parent_count++;
144 pb->mii_bus = parent_bus;
147 for_each_available_child_of_node(mux_node, child_bus_node) {
150 r = of_property_read_u32(child_bus_node, "reg", &v);
153 "Error: Failed to find reg for child %pOF\n",
158 cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
166 cb->mii_bus = mdiobus_alloc();
171 cb->mii_bus->priv = cb;
173 cb->mii_bus->name = "mdio_mux";
174 snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
176 cb->mii_bus->parent = dev;
177 cb->mii_bus->read = mdio_mux_read;
178 cb->mii_bus->write = mdio_mux_write;
179 r = of_mdiobus_register(cb->mii_bus, child_bus_node);
181 mdiobus_free(cb->mii_bus);
182 if (r == -EPROBE_DEFER) {
188 "Error: Failed to register MDIO bus for child %pOF\n",
191 cb->next = pb->children;
200 dev_err(dev, "Error: No acceptable child buses found\n");
204 mdio_mux_uninit_children(pb);
205 of_node_put(child_bus_node);
207 put_device(&parent_bus->dev);
209 of_node_put(parent_bus_node);
212 EXPORT_SYMBOL_GPL(mdio_mux_init);
214 void mdio_mux_uninit(void *mux_handle)
216 struct mdio_mux_parent_bus *pb = mux_handle;
218 mdio_mux_uninit_children(pb);
219 put_device(&pb->mii_bus->dev);
221 EXPORT_SYMBOL_GPL(mdio_mux_uninit);
223 MODULE_DESCRIPTION(DRV_DESCRIPTION);
224 MODULE_AUTHOR("David Daney");
225 MODULE_LICENSE("GPL");