mdio-mux-gpio.c 2.4 KB

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