phy-am335x.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <linux/module.h>
  2. #include <linux/platform_device.h>
  3. #include <linux/dma-mapping.h>
  4. #include <linux/usb/otg.h>
  5. #include <linux/usb/usb_phy_generic.h>
  6. #include <linux/slab.h>
  7. #include <linux/clk.h>
  8. #include <linux/of.h>
  9. #include <linux/of_address.h>
  10. #include <linux/usb/of.h>
  11. #include "phy-am335x-control.h"
  12. #include "phy-generic.h"
  13. struct am335x_phy {
  14. struct usb_phy_generic usb_phy_gen;
  15. struct phy_control *phy_ctrl;
  16. int id;
  17. enum usb_dr_mode dr_mode;
  18. };
  19. static int am335x_init(struct usb_phy *phy)
  20. {
  21. struct am335x_phy *am_phy = dev_get_drvdata(phy->dev);
  22. phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true);
  23. return 0;
  24. }
  25. static void am335x_shutdown(struct usb_phy *phy)
  26. {
  27. struct am335x_phy *am_phy = dev_get_drvdata(phy->dev);
  28. phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
  29. }
  30. static int am335x_phy_probe(struct platform_device *pdev)
  31. {
  32. struct am335x_phy *am_phy;
  33. struct device *dev = &pdev->dev;
  34. int ret;
  35. am_phy = devm_kzalloc(dev, sizeof(*am_phy), GFP_KERNEL);
  36. if (!am_phy)
  37. return -ENOMEM;
  38. am_phy->phy_ctrl = am335x_get_phy_control(dev);
  39. if (!am_phy->phy_ctrl)
  40. return -EPROBE_DEFER;
  41. am_phy->id = of_alias_get_id(pdev->dev.of_node, "phy");
  42. if (am_phy->id < 0) {
  43. dev_err(&pdev->dev, "Missing PHY id: %d\n", am_phy->id);
  44. return am_phy->id;
  45. }
  46. am_phy->dr_mode = of_usb_get_dr_mode_by_phy(pdev->dev.of_node, -1);
  47. ret = usb_phy_gen_create_phy(dev, &am_phy->usb_phy_gen, NULL);
  48. if (ret)
  49. return ret;
  50. ret = usb_add_phy_dev(&am_phy->usb_phy_gen.phy);
  51. if (ret)
  52. return ret;
  53. am_phy->usb_phy_gen.phy.init = am335x_init;
  54. am_phy->usb_phy_gen.phy.shutdown = am335x_shutdown;
  55. platform_set_drvdata(pdev, am_phy);
  56. device_init_wakeup(dev, true);
  57. /*
  58. * If we leave PHY wakeup enabled then AM33XX wakes up
  59. * immediately from DS0. To avoid this we mark dev->power.can_wakeup
  60. * to false. The same is checked in suspend routine to decide
  61. * on whether to enable PHY wakeup or not.
  62. * PHY wakeup works fine in standby mode, there by allowing us to
  63. * handle remote wakeup, wakeup on disconnect and connect.
  64. */
  65. device_set_wakeup_enable(dev, false);
  66. phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
  67. return 0;
  68. }
  69. static int am335x_phy_remove(struct platform_device *pdev)
  70. {
  71. struct am335x_phy *am_phy = platform_get_drvdata(pdev);
  72. usb_remove_phy(&am_phy->usb_phy_gen.phy);
  73. return 0;
  74. }
  75. #ifdef CONFIG_PM_SLEEP
  76. static int am335x_phy_suspend(struct device *dev)
  77. {
  78. struct platform_device *pdev = to_platform_device(dev);
  79. struct am335x_phy *am_phy = platform_get_drvdata(pdev);
  80. /*
  81. * Enable phy wakeup only if dev->power.can_wakeup is true.
  82. * Make sure to enable wakeup to support remote wakeup in
  83. * standby mode ( same is not supported in OFF(DS0) mode).
  84. * Enable it by doing
  85. * echo enabled > /sys/bus/platform/devices/<usb-phy-id>/power/wakeup
  86. */
  87. if (device_may_wakeup(dev))
  88. phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, true);
  89. phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, false);
  90. return 0;
  91. }
  92. static int am335x_phy_resume(struct device *dev)
  93. {
  94. struct platform_device *pdev = to_platform_device(dev);
  95. struct am335x_phy *am_phy = platform_get_drvdata(pdev);
  96. phy_ctrl_power(am_phy->phy_ctrl, am_phy->id, am_phy->dr_mode, true);
  97. if (device_may_wakeup(dev))
  98. phy_ctrl_wkup(am_phy->phy_ctrl, am_phy->id, false);
  99. return 0;
  100. }
  101. #endif
  102. static SIMPLE_DEV_PM_OPS(am335x_pm_ops, am335x_phy_suspend, am335x_phy_resume);
  103. static const struct of_device_id am335x_phy_ids[] = {
  104. { .compatible = "ti,am335x-usb-phy" },
  105. { }
  106. };
  107. MODULE_DEVICE_TABLE(of, am335x_phy_ids);
  108. static struct platform_driver am335x_phy_driver = {
  109. .probe = am335x_phy_probe,
  110. .remove = am335x_phy_remove,
  111. .driver = {
  112. .name = "am335x-phy-driver",
  113. .pm = &am335x_pm_ops,
  114. .of_match_table = am335x_phy_ids,
  115. },
  116. };
  117. module_platform_driver(am335x_phy_driver);
  118. MODULE_LICENSE("GPL v2");