treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / phy / mdio-mux-multiplexer.c
blobd6564381aa3e4f619d4c7bac8820a464f7bc31fa
1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO bus multiplexer using kernel multiplexer subsystem
4 * Copyright 2019 NXP
5 */
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;
14 bool do_deselect;
15 void *mux_handle;
18 /**
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
29 * correct bus.
31 static int mdio_mux_multiplexer_switch_fn(int current_child, int desired_child,
32 void *data)
34 struct platform_device *pdev;
35 struct mdio_mux_multiplexer_state *s;
36 int ret = 0;
38 pdev = (struct platform_device *)data;
39 s = platform_get_drvdata(pdev);
41 if (!(current_child ^ desired_child))
42 return 0;
44 if (s->do_deselect)
45 ret = mux_control_deselect(s->muxc);
46 if (ret) {
47 dev_err(&pdev->dev, "mux_control_deselect failed in %s: %d\n",
48 __func__, ret);
49 return ret;
52 ret = mux_control_select(s->muxc, desired_child);
53 if (!ret) {
54 dev_dbg(&pdev->dev, "%s %d -> %d\n", __func__, current_child,
55 desired_child);
56 s->do_deselect = true;
57 } else {
58 s->do_deselect = false;
61 return ret;
64 static int mdio_mux_multiplexer_probe(struct platform_device *pdev)
66 struct device *dev = &pdev->dev;
67 struct mdio_mux_multiplexer_state *s;
68 int ret = 0;
70 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
71 if (!s)
72 return -ENOMEM;
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);
79 return 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,
86 pdev, NULL);
88 return ret;
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);
97 if (s->do_deselect)
98 mux_control_deselect(s->muxc);
100 return 0;
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 = {
110 .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");