Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* MDIO bus multiplexer using kernel multiplexer subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2019 NXP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mdio-mux.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/mux/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) struct mdio_mux_multiplexer_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	struct mux_control *muxc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	bool do_deselect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	void *mux_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * mdio_mux_multiplexer_switch_fn - This function is called by the mdio-mux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *                                  layer when it thinks the mdio bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *                                  multiplexer needs to switch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @current_child:  current value of the mux register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * @desired_child: value of the 'reg' property of the target child MDIO node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * @data: Private data used by this switch_fn passed to mdio_mux_init function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *        via mdio_mux_init(.., .., .., .., data, ..).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * The first time this function is called, current_child == -1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * If current_child == desired_child, then the mux is already set to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * correct bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int mdio_mux_multiplexer_switch_fn(int current_child, int desired_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 					  void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct mdio_mux_multiplexer_state *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	pdev = (struct platform_device *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	s = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (!(current_child ^ desired_child))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (s->do_deselect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		ret = mux_control_deselect(s->muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		dev_err(&pdev->dev, "mux_control_deselect failed in %s: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			__func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	ret =  mux_control_select(s->muxc, desired_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		dev_dbg(&pdev->dev, "%s %d -> %d\n", __func__, current_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			desired_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		s->do_deselect = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		s->do_deselect = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int mdio_mux_multiplexer_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct mdio_mux_multiplexer_state *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	s->muxc = devm_mux_control_get(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (IS_ERR(s->muxc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		ret = PTR_ERR(s->muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			dev_err(&pdev->dev, "Failed to get mux: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	platform_set_drvdata(pdev, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	ret = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			    mdio_mux_multiplexer_switch_fn, &s->mux_handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			    pdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static int mdio_mux_multiplexer_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct mdio_mux_multiplexer_state *s = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	mdio_mux_uninit(s->mux_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (s->do_deselect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		mux_control_deselect(s->muxc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static const struct of_device_id mdio_mux_multiplexer_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	{ .compatible = "mdio-mux-multiplexer", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) MODULE_DEVICE_TABLE(of, mdio_mux_multiplexer_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static struct platform_driver mdio_mux_multiplexer_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		.name		= "mdio-mux-multiplexer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		.of_match_table	= mdio_mux_multiplexer_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	.probe		= mdio_mux_multiplexer_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	.remove		= mdio_mux_multiplexer_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) module_platform_driver(mdio_mux_multiplexer_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) MODULE_DESCRIPTION("MDIO bus multiplexer using kernel multiplexer subsystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MODULE_AUTHOR("Pankaj Bansal <pankaj.bansal@nxp.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) MODULE_LICENSE("GPL");