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_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;
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;
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;
49 mutex_lock(&pb->mii_bus->mdio_lock);
50 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
54 pb->current_child = cb->bus_number;
56 r = pb->mii_bus->read(pb->mii_bus, phy_id, regnum);
58 mutex_unlock(&pb->mii_bus->mdio_lock);
64 * The parent bus' lock is used to order access to the switch_fn.
66 static int mdio_mux_write(struct mii_bus *bus, int phy_id,
69 struct mdio_mux_child_bus *cb = bus->priv;
70 struct mdio_mux_parent_bus *pb = cb->parent;
74 mutex_lock(&pb->mii_bus->mdio_lock);
75 r = pb->switch_fn(pb->current_child, cb->bus_number, pb->switch_data);
79 pb->current_child = cb->bus_number;
81 r = pb->mii_bus->write(pb->mii_bus, phy_id, regnum, val);
83 mutex_unlock(&pb->mii_bus->mdio_lock);
88 static int parent_count;
90 int mdio_mux_init(struct device *dev,
91 int (*switch_fn)(int cur, int desired, void *data),
95 struct device_node *parent_bus_node;
96 struct device_node *child_bus_node;
98 struct mii_bus *parent_bus;
99 struct mdio_mux_parent_bus *pb;
100 struct mdio_mux_child_bus *cb;
105 parent_bus_node = of_parse_phandle(dev->of_node, "mdio-parent-bus", 0);
107 if (!parent_bus_node)
110 parent_bus = of_mdio_find_bus(parent_bus_node);
111 if (parent_bus == NULL) {
112 ret_val = -EPROBE_DEFER;
116 pb = devm_kzalloc(dev, sizeof(*pb), GFP_KERNEL);
122 pb->switch_data = data;
123 pb->switch_fn = switch_fn;
124 pb->current_child = -1;
125 pb->parent_id = parent_count++;
126 pb->mii_bus = parent_bus;
129 for_each_child_of_node(dev->of_node, child_bus_node) {
132 r = of_property_read_u32(child_bus_node, "reg", &v);
136 cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
139 "Error: Failed to allocate memory for child\n");
145 cb->mii_bus = mdiobus_alloc();
146 cb->mii_bus->priv = cb;
148 cb->mii_bus->irq = cb->phy_irq;
149 cb->mii_bus->name = "mdio_mux";
150 snprintf(cb->mii_bus->id, MII_BUS_ID_SIZE, "%x.%x",
152 cb->mii_bus->parent = dev;
153 cb->mii_bus->read = mdio_mux_read;
154 cb->mii_bus->write = mdio_mux_write;
155 r = of_mdiobus_register(cb->mii_bus, child_bus_node);
157 mdiobus_free(cb->mii_bus);
160 of_node_get(child_bus_node);
161 cb->next = pb->children;
167 dev_info(dev, "Version " DRV_VERSION "\n");
171 of_node_put(parent_bus_node);
174 EXPORT_SYMBOL_GPL(mdio_mux_init);
176 void mdio_mux_uninit(void *mux_handle)
178 struct mdio_mux_parent_bus *pb = mux_handle;
179 struct mdio_mux_child_bus *cb = pb->children;
182 mdiobus_unregister(cb->mii_bus);
183 mdiobus_free(cb->mii_bus);
187 EXPORT_SYMBOL_GPL(mdio_mux_uninit);
189 MODULE_DESCRIPTION(DRV_DESCRIPTION);
190 MODULE_VERSION(DRV_VERSION);
191 MODULE_AUTHOR("David Daney");
192 MODULE_LICENSE("GPL");