mdio-mux-gpio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. int values[s->gpios->ndescs];
  26. unsigned int n;
  27. if (current_child == desired_child)
  28. return 0;
  29. for (n = 0; n < s->gpios->ndescs; n++)
  30. values[n] = (desired_child >> n) & 1;
  31. gpiod_set_array_cansleep(s->gpios->ndescs, s->gpios->desc, values);
  32. return 0;
  33. }
  34. static int mdio_mux_gpio_probe(struct platform_device *pdev)
  35. {
  36. struct mdio_mux_gpio_state *s;
  37. int r;
  38. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  39. if (!s)
  40. return -ENOMEM;
  41. s->gpios = gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
  42. if (IS_ERR(s->gpios))
  43. return PTR_ERR(s->gpios);
  44. r = mdio_mux_init(&pdev->dev,
  45. mdio_mux_gpio_switch_fn, &s->mux_handle, s);
  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");