mdio-mux-gpio.c 2.9 KB

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