mdio-mux-multiplexer.c 3.1 KB

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