phy-am335x.c 3.7 KB

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