1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO bus multiplexer using kernel multiplexer subsystem
7 #include <linux/mdio-mux.h>
8 #include <linux/module.h>
9 #include <linux/mux/consumer.h>
10 #include <linux/platform_device.h>
12 struct mdio_mux_multiplexer_state {
13 struct mux_control *muxc;
19 * mdio_mux_multiplexer_switch_fn - This function is called by the mdio-mux
20 * layer when it thinks the mdio bus
21 * multiplexer needs to switch.
22 * @current_child: current value of the mux register.
23 * @desired_child: value of the 'reg' property of the target child MDIO node.
24 * @data: Private data used by this switch_fn passed to mdio_mux_init function
25 * via mdio_mux_init(.., .., .., .., data, ..).
27 * The first time this function is called, current_child == -1.
28 * If current_child == desired_child, then the mux is already set to the
31 static int mdio_mux_multiplexer_switch_fn(int current_child, int desired_child,
34 struct platform_device *pdev;
35 struct mdio_mux_multiplexer_state *s;
38 pdev = (struct platform_device *)data;
39 s = platform_get_drvdata(pdev);
41 if (!(current_child ^ desired_child))
45 ret = mux_control_deselect(s->muxc);
47 dev_err(&pdev->dev, "mux_control_deselect failed in %s: %d\n",
52 ret = mux_control_select(s->muxc, desired_child);
54 dev_dbg(&pdev->dev, "%s %d -> %d\n", __func__, current_child,
56 s->do_deselect = true;
58 s->do_deselect = false;
64 static int mdio_mux_multiplexer_probe(struct platform_device *pdev)
66 struct device *dev = &pdev->dev;
67 struct mdio_mux_multiplexer_state *s;
70 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
74 s->muxc = devm_mux_control_get(dev, NULL);
76 return dev_err_probe(&pdev->dev, PTR_ERR(s->muxc),
77 "Failed to get mux\n");
79 platform_set_drvdata(pdev, s);
81 ret = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
82 mdio_mux_multiplexer_switch_fn, &s->mux_handle,
88 static int mdio_mux_multiplexer_remove(struct platform_device *pdev)
90 struct mdio_mux_multiplexer_state *s = platform_get_drvdata(pdev);
92 mdio_mux_uninit(s->mux_handle);
95 mux_control_deselect(s->muxc);
100 static const struct of_device_id mdio_mux_multiplexer_match[] = {
101 { .compatible = "mdio-mux-multiplexer", },
104 MODULE_DEVICE_TABLE(of, mdio_mux_multiplexer_match);
106 static struct platform_driver mdio_mux_multiplexer_driver = {
108 .name = "mdio-mux-multiplexer",
109 .of_match_table = mdio_mux_multiplexer_match,
111 .probe = mdio_mux_multiplexer_probe,
112 .remove = mdio_mux_multiplexer_remove,
115 module_platform_driver(mdio_mux_multiplexer_driver);
117 MODULE_DESCRIPTION("MDIO bus multiplexer using kernel multiplexer subsystem");
119 MODULE_LICENSE("GPL");