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
;
40 * The parent bus' lock is used to order access to the switch_fn.
42 static int mdio_mux_read(struct mii_bus
*bus
, int phy_id
, int regnum
)
44 struct mdio_mux_child_bus
*cb
= bus
->priv
;
45 struct mdio_mux_parent_bus
*pb
= cb
->parent
;
48 mutex_lock_nested(&pb
->mii_bus
->mdio_lock
, MDIO_MUTEX_MUX
);
49 r
= pb
->switch_fn(pb
->current_child
, cb
->bus_number
, pb
->switch_data
);
53 pb
->current_child
= cb
->bus_number
;
55 r
= pb
->mii_bus
->read(pb
->mii_bus
, phy_id
, regnum
);
57 mutex_unlock(&pb
->mii_bus
->mdio_lock
);
63 * The parent bus' lock is used to order access to the switch_fn.
65 static int mdio_mux_write(struct mii_bus
*bus
, int phy_id
,
68 struct mdio_mux_child_bus
*cb
= bus
->priv
;
69 struct mdio_mux_parent_bus
*pb
= cb
->parent
;
73 mutex_lock_nested(&pb
->mii_bus
->mdio_lock
, MDIO_MUTEX_MUX
);
74 r
= pb
->switch_fn(pb
->current_child
, cb
->bus_number
, pb
->switch_data
);
78 pb
->current_child
= cb
->bus_number
;
80 r
= pb
->mii_bus
->write(pb
->mii_bus
, phy_id
, regnum
, val
);
82 mutex_unlock(&pb
->mii_bus
->mdio_lock
);
87 static int parent_count
;
89 int mdio_mux_init(struct device
*dev
,
90 int (*switch_fn
)(int cur
, int desired
, void *data
),
93 struct mii_bus
*mux_bus
)
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
;
106 parent_bus_node
= of_parse_phandle(dev
->of_node
,
107 "mdio-parent-bus", 0);
109 if (!parent_bus_node
)
112 parent_bus
= of_mdio_find_bus(parent_bus_node
);
114 ret_val
= -EPROBE_DEFER
;
118 parent_bus_node
= NULL
;
119 parent_bus
= mux_bus
;
122 pb
= devm_kzalloc(dev
, sizeof(*pb
), GFP_KERNEL
);
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
;
135 for_each_available_child_of_node(dev
->of_node
, child_bus_node
) {
138 v
= of_mdio_parse_addr(dev
, child_bus_node
);
141 "Error: Failed to find reg for child %s\n",
142 of_node_full_name(child_bus_node
));
146 cb
= devm_kzalloc(dev
, sizeof(*cb
), GFP_KERNEL
);
149 "Error: Failed to allocate memory for child %s\n",
150 of_node_full_name(child_bus_node
));
157 cb
->mii_bus
= mdiobus_alloc();
160 "Error: Failed to allocate MDIO bus for child %s\n",
161 of_node_full_name(child_bus_node
));
166 cb
->mii_bus
->priv
= cb
;
168 cb
->mii_bus
->name
= "mdio_mux";
169 snprintf(cb
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%x.%x",
171 cb
->mii_bus
->parent
= dev
;
172 cb
->mii_bus
->read
= mdio_mux_read
;
173 cb
->mii_bus
->write
= mdio_mux_write
;
174 r
= of_mdiobus_register(cb
->mii_bus
, child_bus_node
);
177 "Error: Failed to register MDIO bus for child %s\n",
178 of_node_full_name(child_bus_node
));
179 mdiobus_free(cb
->mii_bus
);
182 cb
->next
= pb
->children
;
188 dev_info(dev
, "Version " DRV_VERSION
"\n");
192 dev_err(dev
, "Error: No acceptable child buses found\n");
195 /* balance the reference of_mdio_find_bus() took */
197 put_device(&parent_bus
->dev
);
199 of_node_put(parent_bus_node
);
202 EXPORT_SYMBOL_GPL(mdio_mux_init
);
204 void mdio_mux_uninit(void *mux_handle
)
206 struct mdio_mux_parent_bus
*pb
= mux_handle
;
207 struct mdio_mux_child_bus
*cb
= pb
->children
;
210 mdiobus_unregister(cb
->mii_bus
);
211 mdiobus_free(cb
->mii_bus
);
215 /* balance the reference of_mdio_find_bus() in mdio_mux_init() took */
216 put_device(&pb
->mii_bus
->dev
);
218 EXPORT_SYMBOL_GPL(mdio_mux_uninit
);
220 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
221 MODULE_VERSION(DRV_VERSION
);
222 MODULE_AUTHOR("David Daney");
223 MODULE_LICENSE("GPL");