cdns3-plat.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence USBSS DRD Driver.
  4. *
  5. * Copyright (C) 2018-2020 Cadence.
  6. * Copyright (C) 2017-2018 NXP
  7. * Copyright (C) 2019 Texas Instruments
  8. *
  9. *
  10. * Author: Peter Chen <[email protected]>
  11. * Pawel Laszczak <[email protected]>
  12. * Roger Quadros <[email protected]>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include "core.h"
  20. #include "gadget-export.h"
  21. #include "drd.h"
  22. static int set_phy_power_on(struct cdns *cdns)
  23. {
  24. int ret;
  25. ret = phy_power_on(cdns->usb2_phy);
  26. if (ret)
  27. return ret;
  28. ret = phy_power_on(cdns->usb3_phy);
  29. if (ret)
  30. phy_power_off(cdns->usb2_phy);
  31. return ret;
  32. }
  33. static void set_phy_power_off(struct cdns *cdns)
  34. {
  35. phy_power_off(cdns->usb3_phy);
  36. phy_power_off(cdns->usb2_phy);
  37. }
  38. /**
  39. * cdns3_plat_probe - probe for cdns3 core device
  40. * @pdev: Pointer to cdns3 core platform device
  41. *
  42. * Returns 0 on success otherwise negative errno
  43. */
  44. static int cdns3_plat_probe(struct platform_device *pdev)
  45. {
  46. struct device *dev = &pdev->dev;
  47. struct resource *res;
  48. struct cdns *cdns;
  49. void __iomem *regs;
  50. int ret;
  51. cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
  52. if (!cdns)
  53. return -ENOMEM;
  54. cdns->dev = dev;
  55. cdns->pdata = dev_get_platdata(dev);
  56. platform_set_drvdata(pdev, cdns);
  57. ret = platform_get_irq_byname(pdev, "host");
  58. if (ret < 0)
  59. return ret;
  60. cdns->xhci_res[0].start = ret;
  61. cdns->xhci_res[0].end = ret;
  62. cdns->xhci_res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(ret);
  63. cdns->xhci_res[0].name = "host";
  64. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
  65. if (!res) {
  66. dev_err(dev, "couldn't get xhci resource\n");
  67. return -ENXIO;
  68. }
  69. cdns->xhci_res[1] = *res;
  70. cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
  71. if (cdns->dev_irq < 0)
  72. return cdns->dev_irq;
  73. regs = devm_platform_ioremap_resource_byname(pdev, "dev");
  74. if (IS_ERR(regs))
  75. return PTR_ERR(regs);
  76. cdns->dev_regs = regs;
  77. cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
  78. if (cdns->otg_irq < 0)
  79. return cdns->otg_irq;
  80. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
  81. if (!res) {
  82. dev_err(dev, "couldn't get otg resource\n");
  83. return -ENXIO;
  84. }
  85. cdns->phyrst_a_enable = device_property_read_bool(dev, "cdns,phyrst-a-enable");
  86. cdns->otg_res = *res;
  87. cdns->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
  88. if (cdns->wakeup_irq == -EPROBE_DEFER)
  89. return cdns->wakeup_irq;
  90. if (cdns->wakeup_irq < 0) {
  91. dev_dbg(dev, "couldn't get wakeup irq\n");
  92. cdns->wakeup_irq = 0x0;
  93. }
  94. cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
  95. if (IS_ERR(cdns->usb2_phy))
  96. return PTR_ERR(cdns->usb2_phy);
  97. ret = phy_init(cdns->usb2_phy);
  98. if (ret)
  99. return ret;
  100. cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
  101. if (IS_ERR(cdns->usb3_phy))
  102. return PTR_ERR(cdns->usb3_phy);
  103. ret = phy_init(cdns->usb3_phy);
  104. if (ret)
  105. goto err_phy3_init;
  106. ret = set_phy_power_on(cdns);
  107. if (ret)
  108. goto err_phy_power_on;
  109. cdns->gadget_init = cdns3_gadget_init;
  110. ret = cdns_init(cdns);
  111. if (ret)
  112. goto err_cdns_init;
  113. device_set_wakeup_capable(dev, true);
  114. pm_runtime_set_active(dev);
  115. pm_runtime_enable(dev);
  116. if (!(cdns->pdata && (cdns->pdata->quirks & CDNS3_DEFAULT_PM_RUNTIME_ALLOW)))
  117. pm_runtime_forbid(dev);
  118. /*
  119. * The controller needs less time between bus and controller suspend,
  120. * and we also needs a small delay to avoid frequently entering low
  121. * power mode.
  122. */
  123. pm_runtime_set_autosuspend_delay(dev, 20);
  124. pm_runtime_mark_last_busy(dev);
  125. pm_runtime_use_autosuspend(dev);
  126. return 0;
  127. err_cdns_init:
  128. set_phy_power_off(cdns);
  129. err_phy_power_on:
  130. phy_exit(cdns->usb3_phy);
  131. err_phy3_init:
  132. phy_exit(cdns->usb2_phy);
  133. return ret;
  134. }
  135. /**
  136. * cdns3_plat_remove() - unbind drd driver and clean up
  137. * @pdev: Pointer to Linux platform device
  138. *
  139. * Returns 0 on success otherwise negative errno
  140. */
  141. static int cdns3_plat_remove(struct platform_device *pdev)
  142. {
  143. struct cdns *cdns = platform_get_drvdata(pdev);
  144. struct device *dev = cdns->dev;
  145. pm_runtime_get_sync(dev);
  146. pm_runtime_disable(dev);
  147. pm_runtime_put_noidle(dev);
  148. cdns_remove(cdns);
  149. set_phy_power_off(cdns);
  150. phy_exit(cdns->usb2_phy);
  151. phy_exit(cdns->usb3_phy);
  152. return 0;
  153. }
  154. #ifdef CONFIG_PM
  155. static int cdns3_set_platform_suspend(struct device *dev,
  156. bool suspend, bool wakeup)
  157. {
  158. struct cdns *cdns = dev_get_drvdata(dev);
  159. int ret = 0;
  160. if (cdns->pdata && cdns->pdata->platform_suspend)
  161. ret = cdns->pdata->platform_suspend(dev, suspend, wakeup);
  162. return ret;
  163. }
  164. static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
  165. {
  166. struct cdns *cdns = dev_get_drvdata(dev);
  167. bool wakeup;
  168. unsigned long flags;
  169. if (cdns->in_lpm)
  170. return 0;
  171. if (PMSG_IS_AUTO(msg))
  172. wakeup = true;
  173. else
  174. wakeup = device_may_wakeup(dev);
  175. cdns3_set_platform_suspend(cdns->dev, true, wakeup);
  176. set_phy_power_off(cdns);
  177. spin_lock_irqsave(&cdns->lock, flags);
  178. cdns->in_lpm = true;
  179. spin_unlock_irqrestore(&cdns->lock, flags);
  180. dev_dbg(cdns->dev, "%s ends\n", __func__);
  181. return 0;
  182. }
  183. static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
  184. {
  185. struct cdns *cdns = dev_get_drvdata(dev);
  186. int ret;
  187. unsigned long flags;
  188. if (!cdns->in_lpm)
  189. return 0;
  190. if (cdns_power_is_lost(cdns)) {
  191. phy_exit(cdns->usb2_phy);
  192. ret = phy_init(cdns->usb2_phy);
  193. if (ret)
  194. return ret;
  195. phy_exit(cdns->usb3_phy);
  196. ret = phy_init(cdns->usb3_phy);
  197. if (ret)
  198. return ret;
  199. }
  200. ret = set_phy_power_on(cdns);
  201. if (ret)
  202. return ret;
  203. cdns3_set_platform_suspend(cdns->dev, false, false);
  204. spin_lock_irqsave(&cdns->lock, flags);
  205. cdns_resume(cdns);
  206. cdns->in_lpm = false;
  207. spin_unlock_irqrestore(&cdns->lock, flags);
  208. cdns_set_active(cdns, !PMSG_IS_AUTO(msg));
  209. if (cdns->wakeup_pending) {
  210. cdns->wakeup_pending = false;
  211. enable_irq(cdns->wakeup_irq);
  212. }
  213. dev_dbg(cdns->dev, "%s ends\n", __func__);
  214. return ret;
  215. }
  216. static int cdns3_plat_runtime_suspend(struct device *dev)
  217. {
  218. return cdns3_controller_suspend(dev, PMSG_AUTO_SUSPEND);
  219. }
  220. static int cdns3_plat_runtime_resume(struct device *dev)
  221. {
  222. return cdns3_controller_resume(dev, PMSG_AUTO_RESUME);
  223. }
  224. #ifdef CONFIG_PM_SLEEP
  225. static int cdns3_plat_suspend(struct device *dev)
  226. {
  227. struct cdns *cdns = dev_get_drvdata(dev);
  228. int ret;
  229. cdns_suspend(cdns);
  230. ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
  231. if (ret)
  232. return ret;
  233. if (device_may_wakeup(dev) && cdns->wakeup_irq)
  234. enable_irq_wake(cdns->wakeup_irq);
  235. return ret;
  236. }
  237. static int cdns3_plat_resume(struct device *dev)
  238. {
  239. return cdns3_controller_resume(dev, PMSG_RESUME);
  240. }
  241. #endif /* CONFIG_PM_SLEEP */
  242. #endif /* CONFIG_PM */
  243. static const struct dev_pm_ops cdns3_pm_ops = {
  244. SET_SYSTEM_SLEEP_PM_OPS(cdns3_plat_suspend, cdns3_plat_resume)
  245. SET_RUNTIME_PM_OPS(cdns3_plat_runtime_suspend,
  246. cdns3_plat_runtime_resume, NULL)
  247. };
  248. #ifdef CONFIG_OF
  249. static const struct of_device_id of_cdns3_match[] = {
  250. { .compatible = "cdns,usb3" },
  251. { },
  252. };
  253. MODULE_DEVICE_TABLE(of, of_cdns3_match);
  254. #endif
  255. static struct platform_driver cdns3_driver = {
  256. .probe = cdns3_plat_probe,
  257. .remove = cdns3_plat_remove,
  258. .driver = {
  259. .name = "cdns-usb3",
  260. .of_match_table = of_match_ptr(of_cdns3_match),
  261. .pm = &cdns3_pm_ops,
  262. },
  263. };
  264. module_platform_driver(cdns3_driver);
  265. MODULE_ALIAS("platform:cdns3");
  266. MODULE_AUTHOR("Pawel Laszczak <[email protected]>");
  267. MODULE_LICENSE("GPL v2");
  268. MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");