mdio-mux-gpio.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011, 2012 Cavium, Inc.
  4. */
  5. #include <linux/platform_device.h>
  6. #include <linux/device.h>
  7. #include <linux/of_mdio.h>
  8. #include <linux/module.h>
  9. #include <linux/phy.h>
  10. #include <linux/mdio-mux.h>
  11. #include <linux/gpio/consumer.h>
  12. #define DRV_VERSION "1.1"
  13. #define DRV_DESCRIPTION "GPIO controlled MDIO bus multiplexer driver"
  14. struct mdio_mux_gpio_state {
  15. struct gpio_descs *gpios;
  16. void *mux_handle;
  17. };
  18. static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
  19. void *data)
  20. {
  21. struct mdio_mux_gpio_state *s = data;
  22. DECLARE_BITMAP(values, BITS_PER_TYPE(desired_child));
  23. if (current_child == desired_child)
  24. return 0;
  25. values[0] = desired_child;
  26. gpiod_set_array_value_cansleep(s->gpios->ndescs, s->gpios->desc,
  27. s->gpios->info, values);
  28. return 0;
  29. }
  30. static int mdio_mux_gpio_probe(struct platform_device *pdev)
  31. {
  32. struct mdio_mux_gpio_state *s;
  33. struct gpio_descs *gpios;
  34. int r;
  35. gpios = gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
  36. if (IS_ERR(gpios))
  37. return PTR_ERR(gpios);
  38. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  39. if (!s) {
  40. gpiod_put_array(gpios);
  41. return -ENOMEM;
  42. }
  43. s->gpios = gpios;
  44. r = mdio_mux_init(&pdev->dev, pdev->dev.of_node,
  45. mdio_mux_gpio_switch_fn, &s->mux_handle, s, NULL);
  46. if (r != 0) {
  47. gpiod_put_array(s->gpios);
  48. return r;
  49. }
  50. pdev->dev.platform_data = s;
  51. return 0;
  52. }
  53. static int mdio_mux_gpio_remove(struct platform_device *pdev)
  54. {
  55. struct mdio_mux_gpio_state *s = dev_get_platdata(&pdev->dev);
  56. mdio_mux_uninit(s->mux_handle);
  57. gpiod_put_array(s->gpios);
  58. return 0;
  59. }
  60. static const struct of_device_id mdio_mux_gpio_match[] = {
  61. {
  62. .compatible = "mdio-mux-gpio",
  63. },
  64. {
  65. /* Legacy compatible property. */
  66. .compatible = "cavium,mdio-mux-sn74cbtlv3253",
  67. },
  68. {},
  69. };
  70. MODULE_DEVICE_TABLE(of, mdio_mux_gpio_match);
  71. static struct platform_driver mdio_mux_gpio_driver = {
  72. .driver = {
  73. .name = "mdio-mux-gpio",
  74. .of_match_table = mdio_mux_gpio_match,
  75. },
  76. .probe = mdio_mux_gpio_probe,
  77. .remove = mdio_mux_gpio_remove,
  78. };
  79. module_platform_driver(mdio_mux_gpio_driver);
  80. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  81. MODULE_VERSION(DRV_VERSION);
  82. MODULE_AUTHOR("David Daney");
  83. MODULE_LICENSE("GPL v2");