mdio-mux-gpio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/init.h>
  13. #include <linux/phy.h>
  14. #include <linux/mdio-mux.h>
  15. #include <linux/of_gpio.h>
  16. #define DRV_VERSION "1.0"
  17. #define DRV_DESCRIPTION "GPIO controlled MDIO bus multiplexer driver"
  18. #define MDIO_MUX_GPIO_MAX_BITS 8
  19. struct mdio_mux_gpio_state {
  20. int gpio[MDIO_MUX_GPIO_MAX_BITS];
  21. unsigned int num_gpios;
  22. void *mux_handle;
  23. };
  24. static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
  25. void *data)
  26. {
  27. int change;
  28. unsigned int n;
  29. struct mdio_mux_gpio_state *s = data;
  30. if (current_child == desired_child)
  31. return 0;
  32. change = current_child == -1 ? -1 : current_child ^ desired_child;
  33. for (n = 0; n < s->num_gpios; n++) {
  34. if (change & 1)
  35. gpio_set_value_cansleep(s->gpio[n],
  36. (desired_child & 1) != 0);
  37. change >>= 1;
  38. desired_child >>= 1;
  39. }
  40. return 0;
  41. }
  42. static int __devinit mdio_mux_gpio_probe(struct platform_device *pdev)
  43. {
  44. enum of_gpio_flags f;
  45. struct mdio_mux_gpio_state *s;
  46. unsigned int num_gpios;
  47. unsigned int n;
  48. int r;
  49. if (!pdev->dev.of_node)
  50. return -ENODEV;
  51. num_gpios = of_gpio_count(pdev->dev.of_node);
  52. if (num_gpios == 0 || num_gpios > MDIO_MUX_GPIO_MAX_BITS)
  53. return -ENODEV;
  54. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  55. if (!s)
  56. return -ENOMEM;
  57. s->num_gpios = num_gpios;
  58. for (n = 0; n < num_gpios; ) {
  59. int gpio = of_get_gpio_flags(pdev->dev.of_node, n, &f);
  60. if (gpio < 0) {
  61. r = (gpio == -ENODEV) ? -EPROBE_DEFER : gpio;
  62. goto err;
  63. }
  64. s->gpio[n] = gpio;
  65. n++;
  66. r = gpio_request(gpio, "mdio_mux_gpio");
  67. if (r)
  68. goto err;
  69. r = gpio_direction_output(gpio, 0);
  70. if (r)
  71. goto err;
  72. }
  73. r = mdio_mux_init(&pdev->dev,
  74. mdio_mux_gpio_switch_fn, &s->mux_handle, s);
  75. if (r == 0) {
  76. pdev->dev.platform_data = s;
  77. return 0;
  78. }
  79. err:
  80. while (n) {
  81. n--;
  82. gpio_free(s->gpio[n]);
  83. }
  84. devm_kfree(&pdev->dev, s);
  85. return r;
  86. }
  87. static int __devexit mdio_mux_gpio_remove(struct platform_device *pdev)
  88. {
  89. struct mdio_mux_gpio_state *s = pdev->dev.platform_data;
  90. mdio_mux_uninit(s->mux_handle);
  91. return 0;
  92. }
  93. static struct of_device_id mdio_mux_gpio_match[] = {
  94. {
  95. .compatible = "mdio-mux-gpio",
  96. },
  97. {
  98. /* Legacy compatible property. */
  99. .compatible = "cavium,mdio-mux-sn74cbtlv3253",
  100. },
  101. {},
  102. };
  103. MODULE_DEVICE_TABLE(of, mdio_mux_gpio_match);
  104. static struct platform_driver mdio_mux_gpio_driver = {
  105. .driver = {
  106. .name = "mdio-mux-gpio",
  107. .owner = THIS_MODULE,
  108. .of_match_table = mdio_mux_gpio_match,
  109. },
  110. .probe = mdio_mux_gpio_probe,
  111. .remove = __devexit_p(mdio_mux_gpio_remove),
  112. };
  113. module_platform_driver(mdio_mux_gpio_driver);
  114. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  115. MODULE_VERSION(DRV_VERSION);
  116. MODULE_AUTHOR("David Daney");
  117. MODULE_LICENSE("GPL");