cdns3-plat.c 7.6 KB

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