1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO bus multiplexer using kernel multiplexer subsystem
7 #include <linux/platform_device.h>
8 #include <linux/mdio-mux.h>
9 #include <linux/module.h>
10 #include <linux/mux/consumer.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
);
75 if (IS_ERR(s
->muxc
)) {
76 ret
= PTR_ERR(s
->muxc
);
77 if (ret
!= -EPROBE_DEFER
)
78 dev_err(&pdev
->dev
, "Failed to get mux: %d\n", ret
);
82 platform_set_drvdata(pdev
, s
);
84 ret
= mdio_mux_init(&pdev
->dev
, pdev
->dev
.of_node
,
85 mdio_mux_multiplexer_switch_fn
, &s
->mux_handle
,
91 static int mdio_mux_multiplexer_remove(struct platform_device
*pdev
)
93 struct mdio_mux_multiplexer_state
*s
= platform_get_drvdata(pdev
);
95 mdio_mux_uninit(s
->mux_handle
);
98 mux_control_deselect(s
->muxc
);
103 static const struct of_device_id mdio_mux_multiplexer_match
[] = {
104 { .compatible
= "mdio-mux-multiplexer", },
107 MODULE_DEVICE_TABLE(of
, mdio_mux_multiplexer_match
);
109 static struct platform_driver mdio_mux_multiplexer_driver
= {
111 .name
= "mdio-mux-multiplexer",
112 .of_match_table
= mdio_mux_multiplexer_match
,
114 .probe
= mdio_mux_multiplexer_probe
,
115 .remove
= mdio_mux_multiplexer_remove
,
118 module_platform_driver(mdio_mux_multiplexer_driver
);
120 MODULE_DESCRIPTION("MDIO bus multiplexer using kernel multiplexer subsystem");
121 MODULE_AUTHOR("Pankaj Bansal <pankaj.bansal@nxp.com>");
122 MODULE_LICENSE("GPL");