mdio-mux-gpio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_value_cansleep(s->gpios->ndescs, s->gpios->desc,
  32. values);
  33. return 0;
  34. }
  35. static int mdio_mux_gpio_probe(struct platform_device *pdev)
  36. {
  37. struct mdio_mux_gpio_state *s;
  38. int r;
  39. s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
  40. if (!s)
  41. return -ENOMEM;
  42. s->gpios = gpiod_get_array(&pdev->dev, NULL, GPIOD_OUT_LOW);
  43. if (IS_ERR(s->gpios))
  44. return PTR_ERR(s->gpios);
  45. r = mdio_mux_init(&pdev->dev,
  46. mdio_mux_gpio_switch_fn, &s->mux_handle, s);
  47. if (r != 0) {
  48. gpiod_put_array(s->gpios);
  49. return r;
  50. }
  51. pdev->dev.platform_data = s;
  52. return 0;
  53. }
  54. static int mdio_mux_gpio_remove(struct platform_device *pdev)
  55. {
  56. struct mdio_mux_gpio_state *s = dev_get_platdata(&pdev->dev);
  57. mdio_mux_uninit(s->mux_handle);
  58. gpiod_put_array(s->gpios);
  59. return 0;
  60. }
  61. static const struct of_device_id mdio_mux_gpio_match[] = {
  62. {
  63. .compatible = "mdio-mux-gpio",
  64. },
  65. {
  66. /* Legacy compatible property. */
  67. .compatible = "cavium,mdio-mux-sn74cbtlv3253",
  68. },
  69. {},
  70. };
  71. MODULE_DEVICE_TABLE(of, mdio_mux_gpio_match);
  72. static struct platform_driver mdio_mux_gpio_driver = {
  73. .driver = {
  74. .name = "mdio-mux-gpio",
  75. .of_match_table = mdio_mux_gpio_match,
  76. },
  77. .probe = mdio_mux_gpio_probe,
  78. .remove = mdio_mux_gpio_remove,
  79. };
  80. module_platform_driver(mdio_mux_gpio_driver);
  81. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  82. MODULE_VERSION(DRV_VERSION);
  83. MODULE_AUTHOR("David Daney");
  84. MODULE_LICENSE("GPL");