mdio-mux-gpio.c 2.3 KB

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