mdio-mux-gpio.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011, 2012 Cavium, Inc.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/device.h>
  10. #include <linux/of_mdio.h>
  11. #include <linux/module.h>
  12. #include <linux/phy.h>
  13. #include <linux/mdio-mux.h>
  14. #include <linux/of_gpio.h>
  15. #define DRV_VERSION "1.1"
  16. #define DRV_DESCRIPTION "GPIO controlled MDIO bus multiplexer driver"
  17. #define MDIO_MUX_GPIO_MAX_BITS 8
  18. struct mdio_mux_gpio_state {
  19. struct gpio_desc *gpio[MDIO_MUX_GPIO_MAX_BITS];
  20. unsigned int num_gpios;
  21. void *mux_handle;
  22. };
  23. static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
  24. void *data)
  25. {
  26. int values[MDIO_MUX_GPIO_MAX_BITS];
  27. unsigned int n;
  28. struct mdio_mux_gpio_state *s = data;
  29. if (current_child == desired_child)
  30. return 0;
  31. for (n = 0; n < s->num_gpios; n++) {
  32. values[n] = (desired_child >> n) & 1;
  33. }
  34. gpiod_set_array_cansleep(s->num_gpios, s->gpio, values);
  35. return 0;
  36. }
  37. static int mdio_mux_gpio_probe(struct platform_device *pdev)
  38. {
  39. struct mdio_mux_gpio_state *s;
  40. int num_gpios;
  41. unsigned int n;
  42. int r;
  43. if (!pdev->dev.of_node)
  44. return -ENODEV;
  45. num_gpios = of_gpio_count(pdev->dev.of_node);
  46. if (num_gpios <= 0 || num_gpios > MDIO_MUX_GPIO_MAX_BITS)
  47. return -ENODEV;
  48. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  49. if (!s)
  50. return -ENOMEM;
  51. s->num_gpios = num_gpios;
  52. for (n = 0; n < num_gpios; ) {
  53. struct gpio_desc *gpio = gpiod_get_index(&pdev->dev, NULL, n,
  54. GPIOD_OUT_LOW);
  55. if (IS_ERR(gpio)) {
  56. r = PTR_ERR(gpio);
  57. goto err;
  58. }
  59. s->gpio[n] = gpio;
  60. n++;
  61. }
  62. r = mdio_mux_init(&pdev->dev,
  63. mdio_mux_gpio_switch_fn, &s->mux_handle, s);
  64. if (r == 0) {
  65. pdev->dev.platform_data = s;
  66. return 0;
  67. }
  68. err:
  69. while (n) {
  70. n--;
  71. gpiod_put(s->gpio[n]);
  72. }
  73. return r;
  74. }
  75. static int mdio_mux_gpio_remove(struct platform_device *pdev)
  76. {
  77. unsigned int n;
  78. struct mdio_mux_gpio_state *s = dev_get_platdata(&pdev->dev);
  79. mdio_mux_uninit(s->mux_handle);
  80. for (n = 0; n < s->num_gpios; n++)
  81. gpiod_put(s->gpio[n]);
  82. return 0;
  83. }
  84. static const struct of_device_id mdio_mux_gpio_match[] = {
  85. {
  86. .compatible = "mdio-mux-gpio",
  87. },
  88. {
  89. /* Legacy compatible property. */
  90. .compatible = "cavium,mdio-mux-sn74cbtlv3253",
  91. },
  92. {},
  93. };
  94. MODULE_DEVICE_TABLE(of, mdio_mux_gpio_match);
  95. static struct platform_driver mdio_mux_gpio_driver = {
  96. .driver = {
  97. .name = "mdio-mux-gpio",
  98. .of_match_table = mdio_mux_gpio_match,
  99. },
  100. .probe = mdio_mux_gpio_probe,
  101. .remove = mdio_mux_gpio_remove,
  102. };
  103. module_platform_driver(mdio_mux_gpio_driver);
  104. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  105. MODULE_VERSION(DRV_VERSION);
  106. MODULE_AUTHOR("David Daney");
  107. MODULE_LICENSE("GPL");