core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /**
  2. * core.c - DesignWare USB3 DRD Controller Core file
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/ioport.h>
  46. #include <linux/io.h>
  47. #include <linux/list.h>
  48. #include <linux/delay.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/of.h>
  51. #include <linux/usb/otg.h>
  52. #include <linux/usb/ch9.h>
  53. #include <linux/usb/gadget.h>
  54. #include "core.h"
  55. #include "gadget.h"
  56. #include "io.h"
  57. #include "debug.h"
  58. static char *maximum_speed = "super";
  59. module_param(maximum_speed, charp, 0);
  60. MODULE_PARM_DESC(maximum_speed, "Maximum supported speed.");
  61. /* -------------------------------------------------------------------------- */
  62. #define DWC3_DEVS_POSSIBLE 32
  63. static DECLARE_BITMAP(dwc3_devs, DWC3_DEVS_POSSIBLE);
  64. int dwc3_get_device_id(void)
  65. {
  66. int id;
  67. again:
  68. id = find_first_zero_bit(dwc3_devs, DWC3_DEVS_POSSIBLE);
  69. if (id < DWC3_DEVS_POSSIBLE) {
  70. int old;
  71. old = test_and_set_bit(id, dwc3_devs);
  72. if (old)
  73. goto again;
  74. } else {
  75. pr_err("dwc3: no space for new device\n");
  76. id = -ENOMEM;
  77. }
  78. return id;
  79. }
  80. EXPORT_SYMBOL_GPL(dwc3_get_device_id);
  81. void dwc3_put_device_id(int id)
  82. {
  83. int ret;
  84. if (id < 0)
  85. return;
  86. ret = test_bit(id, dwc3_devs);
  87. WARN(!ret, "dwc3: ID %d not in use\n", id);
  88. smp_mb__before_clear_bit();
  89. clear_bit(id, dwc3_devs);
  90. }
  91. EXPORT_SYMBOL_GPL(dwc3_put_device_id);
  92. void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
  93. {
  94. u32 reg;
  95. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  96. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  97. reg |= DWC3_GCTL_PRTCAPDIR(mode);
  98. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  99. }
  100. /**
  101. * dwc3_core_soft_reset - Issues core soft reset and PHY reset
  102. * @dwc: pointer to our context structure
  103. */
  104. static void dwc3_core_soft_reset(struct dwc3 *dwc)
  105. {
  106. u32 reg;
  107. /* Before Resetting PHY, put Core in Reset */
  108. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  109. reg |= DWC3_GCTL_CORESOFTRESET;
  110. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  111. /* Assert USB3 PHY reset */
  112. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  113. reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
  114. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  115. /* Assert USB2 PHY reset */
  116. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  117. reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
  118. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  119. usb_phy_init(dwc->usb2_phy);
  120. usb_phy_init(dwc->usb3_phy);
  121. mdelay(100);
  122. /* Clear USB3 PHY reset */
  123. reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
  124. reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
  125. dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
  126. /* Clear USB2 PHY reset */
  127. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  128. reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
  129. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  130. mdelay(100);
  131. /* After PHYs are stable we can take Core out of reset state */
  132. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  133. reg &= ~DWC3_GCTL_CORESOFTRESET;
  134. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  135. }
  136. /**
  137. * dwc3_free_one_event_buffer - Frees one event buffer
  138. * @dwc: Pointer to our controller context structure
  139. * @evt: Pointer to event buffer to be freed
  140. */
  141. static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
  142. struct dwc3_event_buffer *evt)
  143. {
  144. dma_free_coherent(dwc->dev, evt->length, evt->buf, evt->dma);
  145. kfree(evt);
  146. }
  147. /**
  148. * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
  149. * @dwc: Pointer to our controller context structure
  150. * @length: size of the event buffer
  151. *
  152. * Returns a pointer to the allocated event buffer structure on success
  153. * otherwise ERR_PTR(errno).
  154. */
  155. static struct dwc3_event_buffer *__devinit
  156. dwc3_alloc_one_event_buffer(struct dwc3 *dwc, unsigned length)
  157. {
  158. struct dwc3_event_buffer *evt;
  159. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  160. if (!evt)
  161. return ERR_PTR(-ENOMEM);
  162. evt->dwc = dwc;
  163. evt->length = length;
  164. evt->buf = dma_alloc_coherent(dwc->dev, length,
  165. &evt->dma, GFP_KERNEL);
  166. if (!evt->buf) {
  167. kfree(evt);
  168. return ERR_PTR(-ENOMEM);
  169. }
  170. return evt;
  171. }
  172. /**
  173. * dwc3_free_event_buffers - frees all allocated event buffers
  174. * @dwc: Pointer to our controller context structure
  175. */
  176. static void dwc3_free_event_buffers(struct dwc3 *dwc)
  177. {
  178. struct dwc3_event_buffer *evt;
  179. int i;
  180. for (i = 0; i < dwc->num_event_buffers; i++) {
  181. evt = dwc->ev_buffs[i];
  182. if (evt)
  183. dwc3_free_one_event_buffer(dwc, evt);
  184. }
  185. kfree(dwc->ev_buffs);
  186. }
  187. /**
  188. * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
  189. * @dwc: pointer to our controller context structure
  190. * @length: size of event buffer
  191. *
  192. * Returns 0 on success otherwise negative errno. In the error case, dwc
  193. * may contain some buffers allocated but not all which were requested.
  194. */
  195. static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
  196. {
  197. int num;
  198. int i;
  199. num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
  200. dwc->num_event_buffers = num;
  201. dwc->ev_buffs = kzalloc(sizeof(*dwc->ev_buffs) * num, GFP_KERNEL);
  202. if (!dwc->ev_buffs) {
  203. dev_err(dwc->dev, "can't allocate event buffers array\n");
  204. return -ENOMEM;
  205. }
  206. for (i = 0; i < num; i++) {
  207. struct dwc3_event_buffer *evt;
  208. evt = dwc3_alloc_one_event_buffer(dwc, length);
  209. if (IS_ERR(evt)) {
  210. dev_err(dwc->dev, "can't allocate event buffer\n");
  211. return PTR_ERR(evt);
  212. }
  213. dwc->ev_buffs[i] = evt;
  214. }
  215. return 0;
  216. }
  217. /**
  218. * dwc3_event_buffers_setup - setup our allocated event buffers
  219. * @dwc: pointer to our controller context structure
  220. *
  221. * Returns 0 on success otherwise negative errno.
  222. */
  223. static int dwc3_event_buffers_setup(struct dwc3 *dwc)
  224. {
  225. struct dwc3_event_buffer *evt;
  226. int n;
  227. for (n = 0; n < dwc->num_event_buffers; n++) {
  228. evt = dwc->ev_buffs[n];
  229. dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
  230. evt->buf, (unsigned long long) evt->dma,
  231. evt->length);
  232. evt->lpos = 0;
  233. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
  234. lower_32_bits(evt->dma));
  235. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
  236. upper_32_bits(evt->dma));
  237. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
  238. evt->length & 0xffff);
  239. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  240. }
  241. return 0;
  242. }
  243. static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
  244. {
  245. struct dwc3_event_buffer *evt;
  246. int n;
  247. for (n = 0; n < dwc->num_event_buffers; n++) {
  248. evt = dwc->ev_buffs[n];
  249. evt->lpos = 0;
  250. dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
  251. dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
  252. dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
  253. dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
  254. }
  255. }
  256. static void __devinit dwc3_cache_hwparams(struct dwc3 *dwc)
  257. {
  258. struct dwc3_hwparams *parms = &dwc->hwparams;
  259. parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
  260. parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
  261. parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
  262. parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
  263. parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
  264. parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
  265. parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  266. parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
  267. parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
  268. }
  269. /**
  270. * dwc3_core_init - Low-level initialization of DWC3 Core
  271. * @dwc: Pointer to our controller context structure
  272. *
  273. * Returns 0 on success otherwise negative errno.
  274. */
  275. static int __devinit dwc3_core_init(struct dwc3 *dwc)
  276. {
  277. unsigned long timeout;
  278. u32 reg;
  279. int ret;
  280. reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
  281. /* This should read as U3 followed by revision number */
  282. if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
  283. dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
  284. ret = -ENODEV;
  285. goto err0;
  286. }
  287. dwc->revision = reg;
  288. /* issue device SoftReset too */
  289. timeout = jiffies + msecs_to_jiffies(500);
  290. dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
  291. do {
  292. reg = dwc3_readl(dwc->regs, DWC3_DCTL);
  293. if (!(reg & DWC3_DCTL_CSFTRST))
  294. break;
  295. if (time_after(jiffies, timeout)) {
  296. dev_err(dwc->dev, "Reset Timed Out\n");
  297. ret = -ETIMEDOUT;
  298. goto err0;
  299. }
  300. cpu_relax();
  301. } while (true);
  302. dwc3_core_soft_reset(dwc);
  303. dwc3_cache_hwparams(dwc);
  304. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  305. reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
  306. reg &= ~DWC3_GCTL_DISSCRAMBLE;
  307. switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
  308. case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
  309. reg &= ~DWC3_GCTL_DSBLCLKGTNG;
  310. break;
  311. default:
  312. dev_dbg(dwc->dev, "No power optimization available\n");
  313. }
  314. /*
  315. * WORKAROUND: DWC3 revisions <1.90a have a bug
  316. * where the device can fail to connect at SuperSpeed
  317. * and falls back to high-speed mode which causes
  318. * the device to enter a Connect/Disconnect loop
  319. */
  320. if (dwc->revision < DWC3_REVISION_190A)
  321. reg |= DWC3_GCTL_U2RSTECN;
  322. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  323. ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
  324. if (ret) {
  325. dev_err(dwc->dev, "failed to allocate event buffers\n");
  326. ret = -ENOMEM;
  327. goto err1;
  328. }
  329. ret = dwc3_event_buffers_setup(dwc);
  330. if (ret) {
  331. dev_err(dwc->dev, "failed to setup event buffers\n");
  332. goto err1;
  333. }
  334. return 0;
  335. err1:
  336. dwc3_free_event_buffers(dwc);
  337. err0:
  338. return ret;
  339. }
  340. static void dwc3_core_exit(struct dwc3 *dwc)
  341. {
  342. dwc3_event_buffers_cleanup(dwc);
  343. dwc3_free_event_buffers(dwc);
  344. usb_phy_shutdown(dwc->usb2_phy);
  345. usb_phy_shutdown(dwc->usb3_phy);
  346. }
  347. #define DWC3_ALIGN_MASK (16 - 1)
  348. static int __devinit dwc3_probe(struct platform_device *pdev)
  349. {
  350. struct device_node *node = pdev->dev.of_node;
  351. struct resource *res;
  352. struct dwc3 *dwc;
  353. struct device *dev = &pdev->dev;
  354. int ret = -ENOMEM;
  355. void __iomem *regs;
  356. void *mem;
  357. u8 mode;
  358. mem = devm_kzalloc(dev, sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
  359. if (!mem) {
  360. dev_err(dev, "not enough memory\n");
  361. return -ENOMEM;
  362. }
  363. dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
  364. dwc->mem = mem;
  365. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  366. if (!res) {
  367. dev_err(dev, "missing IRQ\n");
  368. return -ENODEV;
  369. }
  370. dwc->xhci_resources[1].start = res->start;
  371. dwc->xhci_resources[1].end = res->end;
  372. dwc->xhci_resources[1].flags = res->flags;
  373. dwc->xhci_resources[1].name = res->name;
  374. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  375. if (!res) {
  376. dev_err(dev, "missing memory resource\n");
  377. return -ENODEV;
  378. }
  379. dwc->xhci_resources[0].start = res->start;
  380. dwc->xhci_resources[0].end = dwc->xhci_resources[0].start +
  381. DWC3_XHCI_REGS_END;
  382. dwc->xhci_resources[0].flags = res->flags;
  383. dwc->xhci_resources[0].name = res->name;
  384. /*
  385. * Request memory region but exclude xHCI regs,
  386. * since it will be requested by the xhci-plat driver.
  387. */
  388. res = devm_request_mem_region(dev, res->start + DWC3_GLOBALS_REGS_START,
  389. resource_size(res) - DWC3_GLOBALS_REGS_START,
  390. dev_name(dev));
  391. if (!res) {
  392. dev_err(dev, "can't request mem region\n");
  393. return -ENOMEM;
  394. }
  395. regs = devm_ioremap_nocache(dev, res->start, resource_size(res));
  396. if (!regs) {
  397. dev_err(dev, "ioremap failed\n");
  398. return -ENOMEM;
  399. }
  400. dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  401. if (IS_ERR_OR_NULL(dwc->usb2_phy)) {
  402. dev_err(dev, "no usb2 phy configured\n");
  403. return -EPROBE_DEFER;
  404. }
  405. dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3);
  406. if (IS_ERR_OR_NULL(dwc->usb3_phy)) {
  407. dev_err(dev, "no usb3 phy configured\n");
  408. return -EPROBE_DEFER;
  409. }
  410. spin_lock_init(&dwc->lock);
  411. platform_set_drvdata(pdev, dwc);
  412. dwc->regs = regs;
  413. dwc->regs_size = resource_size(res);
  414. dwc->dev = dev;
  415. if (!strncmp("super", maximum_speed, 5))
  416. dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
  417. else if (!strncmp("high", maximum_speed, 4))
  418. dwc->maximum_speed = DWC3_DCFG_HIGHSPEED;
  419. else if (!strncmp("full", maximum_speed, 4))
  420. dwc->maximum_speed = DWC3_DCFG_FULLSPEED1;
  421. else if (!strncmp("low", maximum_speed, 3))
  422. dwc->maximum_speed = DWC3_DCFG_LOWSPEED;
  423. else
  424. dwc->maximum_speed = DWC3_DCFG_SUPERSPEED;
  425. if (of_get_property(node, "tx-fifo-resize", NULL))
  426. dwc->needs_fifo_resize = true;
  427. pm_runtime_enable(dev);
  428. pm_runtime_get_sync(dev);
  429. pm_runtime_forbid(dev);
  430. ret = dwc3_core_init(dwc);
  431. if (ret) {
  432. dev_err(dev, "failed to initialize core\n");
  433. return ret;
  434. }
  435. mode = DWC3_MODE(dwc->hwparams.hwparams0);
  436. switch (mode) {
  437. case DWC3_MODE_DEVICE:
  438. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
  439. ret = dwc3_gadget_init(dwc);
  440. if (ret) {
  441. dev_err(dev, "failed to initialize gadget\n");
  442. goto err1;
  443. }
  444. break;
  445. case DWC3_MODE_HOST:
  446. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
  447. ret = dwc3_host_init(dwc);
  448. if (ret) {
  449. dev_err(dev, "failed to initialize host\n");
  450. goto err1;
  451. }
  452. break;
  453. case DWC3_MODE_DRD:
  454. dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
  455. ret = dwc3_host_init(dwc);
  456. if (ret) {
  457. dev_err(dev, "failed to initialize host\n");
  458. goto err1;
  459. }
  460. ret = dwc3_gadget_init(dwc);
  461. if (ret) {
  462. dev_err(dev, "failed to initialize gadget\n");
  463. goto err1;
  464. }
  465. break;
  466. default:
  467. dev_err(dev, "Unsupported mode of operation %d\n", mode);
  468. goto err1;
  469. }
  470. dwc->mode = mode;
  471. ret = dwc3_debugfs_init(dwc);
  472. if (ret) {
  473. dev_err(dev, "failed to initialize debugfs\n");
  474. goto err2;
  475. }
  476. pm_runtime_allow(dev);
  477. return 0;
  478. err2:
  479. switch (mode) {
  480. case DWC3_MODE_DEVICE:
  481. dwc3_gadget_exit(dwc);
  482. break;
  483. case DWC3_MODE_HOST:
  484. dwc3_host_exit(dwc);
  485. break;
  486. case DWC3_MODE_DRD:
  487. dwc3_host_exit(dwc);
  488. dwc3_gadget_exit(dwc);
  489. break;
  490. default:
  491. /* do nothing */
  492. break;
  493. }
  494. err1:
  495. dwc3_core_exit(dwc);
  496. return ret;
  497. }
  498. static int __devexit dwc3_remove(struct platform_device *pdev)
  499. {
  500. struct dwc3 *dwc = platform_get_drvdata(pdev);
  501. struct resource *res;
  502. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  503. pm_runtime_put(&pdev->dev);
  504. pm_runtime_disable(&pdev->dev);
  505. dwc3_debugfs_exit(dwc);
  506. switch (dwc->mode) {
  507. case DWC3_MODE_DEVICE:
  508. dwc3_gadget_exit(dwc);
  509. break;
  510. case DWC3_MODE_HOST:
  511. dwc3_host_exit(dwc);
  512. break;
  513. case DWC3_MODE_DRD:
  514. dwc3_host_exit(dwc);
  515. dwc3_gadget_exit(dwc);
  516. break;
  517. default:
  518. /* do nothing */
  519. break;
  520. }
  521. dwc3_core_exit(dwc);
  522. return 0;
  523. }
  524. static struct platform_driver dwc3_driver = {
  525. .probe = dwc3_probe,
  526. .remove = __devexit_p(dwc3_remove),
  527. .driver = {
  528. .name = "dwc3",
  529. },
  530. };
  531. module_platform_driver(dwc3_driver);
  532. MODULE_ALIAS("platform:dwc3");
  533. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  534. MODULE_LICENSE("Dual BSD/GPL");
  535. MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");