platform.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * platform.c - DesignWare HS OTG Controller platform driver
  3. *
  4. * Copyright (C) Matthijs Kooijman <matthijs@stdin.nl>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/slab.h>
  39. #include <linux/clk.h>
  40. #include <linux/device.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/of_device.h>
  43. #include <linux/mutex.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/phy/phy.h>
  46. #include <linux/platform_data/s3c-hsotg.h>
  47. #include <linux/usb/of.h>
  48. #include "core.h"
  49. #include "hcd.h"
  50. #include "debug.h"
  51. static const char dwc2_driver_name[] = "dwc2";
  52. static const struct dwc2_core_params params_hi6220 = {
  53. .otg_cap = 2, /* No HNP/SRP capable */
  54. .otg_ver = 0, /* 1.3 */
  55. .dma_enable = 1,
  56. .dma_desc_enable = 0,
  57. .dma_desc_fs_enable = 0,
  58. .speed = 0, /* High Speed */
  59. .enable_dynamic_fifo = 1,
  60. .en_multiple_tx_fifo = 1,
  61. .host_rx_fifo_size = 512,
  62. .host_nperio_tx_fifo_size = 512,
  63. .host_perio_tx_fifo_size = 512,
  64. .max_transfer_size = 65535,
  65. .max_packet_count = 511,
  66. .host_channels = 16,
  67. .phy_type = 1, /* UTMI */
  68. .phy_utmi_width = 8,
  69. .phy_ulpi_ddr = 0, /* Single */
  70. .phy_ulpi_ext_vbus = 0,
  71. .i2c_enable = 0,
  72. .ulpi_fs_ls = 0,
  73. .host_support_fs_ls_low_power = 0,
  74. .host_ls_low_power_phy_clk = 0, /* 48 MHz */
  75. .ts_dline = 0,
  76. .reload_ctl = 0,
  77. .ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  78. GAHBCFG_HBSTLEN_SHIFT,
  79. .uframe_sched = 0,
  80. .external_id_pin_ctl = -1,
  81. .hibernation = -1,
  82. };
  83. static const struct dwc2_core_params params_bcm2835 = {
  84. .otg_cap = 0, /* HNP/SRP capable */
  85. .otg_ver = 0, /* 1.3 */
  86. .dma_enable = 1,
  87. .dma_desc_enable = 0,
  88. .dma_desc_fs_enable = 0,
  89. .speed = 0, /* High Speed */
  90. .enable_dynamic_fifo = 1,
  91. .en_multiple_tx_fifo = 1,
  92. .host_rx_fifo_size = 774, /* 774 DWORDs */
  93. .host_nperio_tx_fifo_size = 256, /* 256 DWORDs */
  94. .host_perio_tx_fifo_size = 512, /* 512 DWORDs */
  95. .max_transfer_size = 65535,
  96. .max_packet_count = 511,
  97. .host_channels = 8,
  98. .phy_type = 1, /* UTMI */
  99. .phy_utmi_width = 8, /* 8 bits */
  100. .phy_ulpi_ddr = 0, /* Single */
  101. .phy_ulpi_ext_vbus = 0,
  102. .i2c_enable = 0,
  103. .ulpi_fs_ls = 0,
  104. .host_support_fs_ls_low_power = 0,
  105. .host_ls_low_power_phy_clk = 0, /* 48 MHz */
  106. .ts_dline = 0,
  107. .reload_ctl = 0,
  108. .ahbcfg = 0x10,
  109. .uframe_sched = 0,
  110. .external_id_pin_ctl = -1,
  111. .hibernation = -1,
  112. };
  113. static const struct dwc2_core_params params_rk3066 = {
  114. .otg_cap = 2, /* non-HNP/non-SRP */
  115. .otg_ver = -1,
  116. .dma_enable = -1,
  117. .dma_desc_enable = 0,
  118. .dma_desc_fs_enable = 0,
  119. .speed = -1,
  120. .enable_dynamic_fifo = 1,
  121. .en_multiple_tx_fifo = -1,
  122. .host_rx_fifo_size = 525, /* 525 DWORDs */
  123. .host_nperio_tx_fifo_size = 128, /* 128 DWORDs */
  124. .host_perio_tx_fifo_size = 256, /* 256 DWORDs */
  125. .max_transfer_size = -1,
  126. .max_packet_count = -1,
  127. .host_channels = -1,
  128. .phy_type = -1,
  129. .phy_utmi_width = -1,
  130. .phy_ulpi_ddr = -1,
  131. .phy_ulpi_ext_vbus = -1,
  132. .i2c_enable = -1,
  133. .ulpi_fs_ls = -1,
  134. .host_support_fs_ls_low_power = -1,
  135. .host_ls_low_power_phy_clk = -1,
  136. .ts_dline = -1,
  137. .reload_ctl = -1,
  138. .ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  139. GAHBCFG_HBSTLEN_SHIFT,
  140. .uframe_sched = -1,
  141. .external_id_pin_ctl = -1,
  142. .hibernation = -1,
  143. };
  144. static const struct dwc2_core_params params_ltq = {
  145. .otg_cap = 2, /* non-HNP/non-SRP */
  146. .otg_ver = -1,
  147. .dma_enable = -1,
  148. .dma_desc_enable = -1,
  149. .dma_desc_fs_enable = -1,
  150. .speed = -1,
  151. .enable_dynamic_fifo = -1,
  152. .en_multiple_tx_fifo = -1,
  153. .host_rx_fifo_size = 288, /* 288 DWORDs */
  154. .host_nperio_tx_fifo_size = 128, /* 128 DWORDs */
  155. .host_perio_tx_fifo_size = 96, /* 96 DWORDs */
  156. .max_transfer_size = 65535,
  157. .max_packet_count = 511,
  158. .host_channels = -1,
  159. .phy_type = -1,
  160. .phy_utmi_width = -1,
  161. .phy_ulpi_ddr = -1,
  162. .phy_ulpi_ext_vbus = -1,
  163. .i2c_enable = -1,
  164. .ulpi_fs_ls = -1,
  165. .host_support_fs_ls_low_power = -1,
  166. .host_ls_low_power_phy_clk = -1,
  167. .ts_dline = -1,
  168. .reload_ctl = -1,
  169. .ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
  170. GAHBCFG_HBSTLEN_SHIFT,
  171. .uframe_sched = -1,
  172. .external_id_pin_ctl = -1,
  173. .hibernation = -1,
  174. };
  175. /*
  176. * Check the dr_mode against the module configuration and hardware
  177. * capabilities.
  178. *
  179. * The hardware, module, and dr_mode, can each be set to host, device,
  180. * or otg. Check that all these values are compatible and adjust the
  181. * value of dr_mode if possible.
  182. *
  183. * actual
  184. * HW MOD dr_mode dr_mode
  185. * ------------------------------
  186. * HST HST any : HST
  187. * HST DEV any : ---
  188. * HST OTG any : HST
  189. *
  190. * DEV HST any : ---
  191. * DEV DEV any : DEV
  192. * DEV OTG any : DEV
  193. *
  194. * OTG HST any : HST
  195. * OTG DEV any : DEV
  196. * OTG OTG any : dr_mode
  197. */
  198. static int dwc2_get_dr_mode(struct dwc2_hsotg *hsotg)
  199. {
  200. enum usb_dr_mode mode;
  201. hsotg->dr_mode = usb_get_dr_mode(hsotg->dev);
  202. if (hsotg->dr_mode == USB_DR_MODE_UNKNOWN)
  203. hsotg->dr_mode = USB_DR_MODE_OTG;
  204. mode = hsotg->dr_mode;
  205. if (dwc2_hw_is_device(hsotg)) {
  206. if (IS_ENABLED(CONFIG_USB_DWC2_HOST)) {
  207. dev_err(hsotg->dev,
  208. "Controller does not support host mode.\n");
  209. return -EINVAL;
  210. }
  211. mode = USB_DR_MODE_PERIPHERAL;
  212. } else if (dwc2_hw_is_host(hsotg)) {
  213. if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL)) {
  214. dev_err(hsotg->dev,
  215. "Controller does not support device mode.\n");
  216. return -EINVAL;
  217. }
  218. mode = USB_DR_MODE_HOST;
  219. } else {
  220. if (IS_ENABLED(CONFIG_USB_DWC2_HOST))
  221. mode = USB_DR_MODE_HOST;
  222. else if (IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL))
  223. mode = USB_DR_MODE_PERIPHERAL;
  224. }
  225. if (mode != hsotg->dr_mode) {
  226. dev_warn(hsotg->dev,
  227. "Configuration mismatch. dr_mode forced to %s\n",
  228. mode == USB_DR_MODE_HOST ? "host" : "device");
  229. hsotg->dr_mode = mode;
  230. }
  231. return 0;
  232. }
  233. static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
  234. {
  235. struct platform_device *pdev = to_platform_device(hsotg->dev);
  236. int ret;
  237. ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
  238. hsotg->supplies);
  239. if (ret)
  240. return ret;
  241. if (hsotg->clk) {
  242. ret = clk_prepare_enable(hsotg->clk);
  243. if (ret)
  244. return ret;
  245. }
  246. if (hsotg->uphy)
  247. ret = usb_phy_init(hsotg->uphy);
  248. else if (hsotg->plat && hsotg->plat->phy_init)
  249. ret = hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
  250. else {
  251. ret = phy_power_on(hsotg->phy);
  252. if (ret == 0)
  253. ret = phy_init(hsotg->phy);
  254. }
  255. return ret;
  256. }
  257. /**
  258. * dwc2_lowlevel_hw_enable - enable platform lowlevel hw resources
  259. * @hsotg: The driver state
  260. *
  261. * A wrapper for platform code responsible for controlling
  262. * low-level USB platform resources (phy, clock, regulators)
  263. */
  264. int dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
  265. {
  266. int ret = __dwc2_lowlevel_hw_enable(hsotg);
  267. if (ret == 0)
  268. hsotg->ll_hw_enabled = true;
  269. return ret;
  270. }
  271. static int __dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg)
  272. {
  273. struct platform_device *pdev = to_platform_device(hsotg->dev);
  274. int ret = 0;
  275. if (hsotg->uphy)
  276. usb_phy_shutdown(hsotg->uphy);
  277. else if (hsotg->plat && hsotg->plat->phy_exit)
  278. ret = hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
  279. else {
  280. ret = phy_exit(hsotg->phy);
  281. if (ret == 0)
  282. ret = phy_power_off(hsotg->phy);
  283. }
  284. if (ret)
  285. return ret;
  286. if (hsotg->clk)
  287. clk_disable_unprepare(hsotg->clk);
  288. ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
  289. hsotg->supplies);
  290. return ret;
  291. }
  292. /**
  293. * dwc2_lowlevel_hw_disable - disable platform lowlevel hw resources
  294. * @hsotg: The driver state
  295. *
  296. * A wrapper for platform code responsible for controlling
  297. * low-level USB platform resources (phy, clock, regulators)
  298. */
  299. int dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg)
  300. {
  301. int ret = __dwc2_lowlevel_hw_disable(hsotg);
  302. if (ret == 0)
  303. hsotg->ll_hw_enabled = false;
  304. return ret;
  305. }
  306. static int dwc2_lowlevel_hw_init(struct dwc2_hsotg *hsotg)
  307. {
  308. int i, ret;
  309. /* Set default UTMI width */
  310. hsotg->phyif = GUSBCFG_PHYIF16;
  311. /*
  312. * Attempt to find a generic PHY, then look for an old style
  313. * USB PHY and then fall back to pdata
  314. */
  315. hsotg->phy = devm_phy_get(hsotg->dev, "usb2-phy");
  316. if (IS_ERR(hsotg->phy)) {
  317. ret = PTR_ERR(hsotg->phy);
  318. switch (ret) {
  319. case -ENODEV:
  320. case -ENOSYS:
  321. hsotg->phy = NULL;
  322. break;
  323. case -EPROBE_DEFER:
  324. return ret;
  325. default:
  326. dev_err(hsotg->dev, "error getting phy %d\n", ret);
  327. return ret;
  328. }
  329. }
  330. if (!hsotg->phy) {
  331. hsotg->uphy = devm_usb_get_phy(hsotg->dev, USB_PHY_TYPE_USB2);
  332. if (IS_ERR(hsotg->uphy)) {
  333. ret = PTR_ERR(hsotg->uphy);
  334. switch (ret) {
  335. case -ENODEV:
  336. case -ENXIO:
  337. hsotg->uphy = NULL;
  338. break;
  339. case -EPROBE_DEFER:
  340. return ret;
  341. default:
  342. dev_err(hsotg->dev, "error getting usb phy %d\n",
  343. ret);
  344. return ret;
  345. }
  346. }
  347. }
  348. hsotg->plat = dev_get_platdata(hsotg->dev);
  349. if (hsotg->phy) {
  350. /*
  351. * If using the generic PHY framework, check if the PHY bus
  352. * width is 8-bit and set the phyif appropriately.
  353. */
  354. if (phy_get_bus_width(hsotg->phy) == 8)
  355. hsotg->phyif = GUSBCFG_PHYIF8;
  356. }
  357. /* Clock */
  358. hsotg->clk = devm_clk_get(hsotg->dev, "otg");
  359. if (IS_ERR(hsotg->clk)) {
  360. hsotg->clk = NULL;
  361. dev_dbg(hsotg->dev, "cannot get otg clock\n");
  362. }
  363. /* Regulators */
  364. for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
  365. hsotg->supplies[i].supply = dwc2_hsotg_supply_names[i];
  366. ret = devm_regulator_bulk_get(hsotg->dev, ARRAY_SIZE(hsotg->supplies),
  367. hsotg->supplies);
  368. if (ret) {
  369. dev_err(hsotg->dev, "failed to request supplies: %d\n", ret);
  370. return ret;
  371. }
  372. return 0;
  373. }
  374. /**
  375. * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the
  376. * DWC_otg driver
  377. *
  378. * @dev: Platform device
  379. *
  380. * This routine is called, for example, when the rmmod command is executed. The
  381. * device may or may not be electrically present. If it is present, the driver
  382. * stops device processing. Any resources used on behalf of this device are
  383. * freed.
  384. */
  385. static int dwc2_driver_remove(struct platform_device *dev)
  386. {
  387. struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
  388. dwc2_debugfs_exit(hsotg);
  389. if (hsotg->hcd_enabled)
  390. dwc2_hcd_remove(hsotg);
  391. if (hsotg->gadget_enabled)
  392. dwc2_hsotg_remove(hsotg);
  393. if (hsotg->ll_hw_enabled)
  394. dwc2_lowlevel_hw_disable(hsotg);
  395. return 0;
  396. }
  397. /**
  398. * dwc2_driver_shutdown() - Called on device shutdown
  399. *
  400. * @dev: Platform device
  401. *
  402. * In specific conditions (involving usb hubs) dwc2 devices can create a
  403. * lot of interrupts, even to the point of overwhelming devices running
  404. * at low frequencies. Some devices need to do special clock handling
  405. * at shutdown-time which may bring the system clock below the threshold
  406. * of being able to handle the dwc2 interrupts. Disabling dwc2-irqs
  407. * prevents reboots/poweroffs from getting stuck in such cases.
  408. */
  409. static void dwc2_driver_shutdown(struct platform_device *dev)
  410. {
  411. struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
  412. disable_irq(hsotg->irq);
  413. }
  414. static const struct of_device_id dwc2_of_match_table[] = {
  415. { .compatible = "brcm,bcm2835-usb", .data = &params_bcm2835 },
  416. { .compatible = "hisilicon,hi6220-usb", .data = &params_hi6220 },
  417. { .compatible = "rockchip,rk3066-usb", .data = &params_rk3066 },
  418. { .compatible = "lantiq,arx100-usb", .data = &params_ltq },
  419. { .compatible = "lantiq,xrx200-usb", .data = &params_ltq },
  420. { .compatible = "snps,dwc2", .data = NULL },
  421. { .compatible = "samsung,s3c6400-hsotg", .data = NULL},
  422. {},
  423. };
  424. MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
  425. /**
  426. * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg
  427. * driver
  428. *
  429. * @dev: Platform device
  430. *
  431. * This routine creates the driver components required to control the device
  432. * (core, HCD, and PCD) and initializes the device. The driver components are
  433. * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved
  434. * in the device private data. This allows the driver to access the dwc2_hsotg
  435. * structure on subsequent calls to driver methods for this device.
  436. */
  437. static int dwc2_driver_probe(struct platform_device *dev)
  438. {
  439. const struct of_device_id *match;
  440. const struct dwc2_core_params *params;
  441. struct dwc2_core_params defparams;
  442. struct dwc2_hsotg *hsotg;
  443. struct resource *res;
  444. int retval;
  445. match = of_match_device(dwc2_of_match_table, &dev->dev);
  446. if (match && match->data) {
  447. params = match->data;
  448. } else {
  449. /* Default all params to autodetect */
  450. dwc2_set_all_params(&defparams, -1);
  451. params = &defparams;
  452. /*
  453. * Disable descriptor dma mode by default as the HW can support
  454. * it, but does not support it for SPLIT transactions.
  455. * Disable it for FS devices as well.
  456. */
  457. defparams.dma_desc_enable = 0;
  458. defparams.dma_desc_fs_enable = 0;
  459. }
  460. hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL);
  461. if (!hsotg)
  462. return -ENOMEM;
  463. hsotg->dev = &dev->dev;
  464. /*
  465. * Use reasonable defaults so platforms don't have to provide these.
  466. */
  467. if (!dev->dev.dma_mask)
  468. dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
  469. retval = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
  470. if (retval)
  471. return retval;
  472. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  473. hsotg->regs = devm_ioremap_resource(&dev->dev, res);
  474. if (IS_ERR(hsotg->regs))
  475. return PTR_ERR(hsotg->regs);
  476. dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n",
  477. (unsigned long)res->start, hsotg->regs);
  478. retval = dwc2_lowlevel_hw_init(hsotg);
  479. if (retval)
  480. return retval;
  481. spin_lock_init(&hsotg->lock);
  482. hsotg->core_params = devm_kzalloc(&dev->dev,
  483. sizeof(*hsotg->core_params), GFP_KERNEL);
  484. if (!hsotg->core_params)
  485. return -ENOMEM;
  486. dwc2_set_all_params(hsotg->core_params, -1);
  487. hsotg->irq = platform_get_irq(dev, 0);
  488. if (hsotg->irq < 0) {
  489. dev_err(&dev->dev, "missing IRQ resource\n");
  490. return hsotg->irq;
  491. }
  492. dev_dbg(hsotg->dev, "registering common handler for irq%d\n",
  493. hsotg->irq);
  494. retval = devm_request_irq(hsotg->dev, hsotg->irq,
  495. dwc2_handle_common_intr, IRQF_SHARED,
  496. dev_name(hsotg->dev), hsotg);
  497. if (retval)
  498. return retval;
  499. retval = dwc2_lowlevel_hw_enable(hsotg);
  500. if (retval)
  501. return retval;
  502. retval = dwc2_get_dr_mode(hsotg);
  503. if (retval)
  504. return retval;
  505. /*
  506. * Reset before dwc2_get_hwparams() then it could get power-on real
  507. * reset value form registers.
  508. */
  509. dwc2_core_reset_and_force_dr_mode(hsotg);
  510. /* Detect config values from hardware */
  511. retval = dwc2_get_hwparams(hsotg);
  512. if (retval)
  513. goto error;
  514. /* Validate parameter values */
  515. dwc2_set_parameters(hsotg, params);
  516. dwc2_force_dr_mode(hsotg);
  517. if (hsotg->dr_mode != USB_DR_MODE_HOST) {
  518. retval = dwc2_gadget_init(hsotg, hsotg->irq);
  519. if (retval)
  520. goto error;
  521. hsotg->gadget_enabled = 1;
  522. }
  523. if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) {
  524. retval = dwc2_hcd_init(hsotg, hsotg->irq);
  525. if (retval) {
  526. if (hsotg->gadget_enabled)
  527. dwc2_hsotg_remove(hsotg);
  528. goto error;
  529. }
  530. hsotg->hcd_enabled = 1;
  531. }
  532. platform_set_drvdata(dev, hsotg);
  533. dwc2_debugfs_init(hsotg);
  534. /* Gadget code manages lowlevel hw on its own */
  535. if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
  536. dwc2_lowlevel_hw_disable(hsotg);
  537. return 0;
  538. error:
  539. dwc2_lowlevel_hw_disable(hsotg);
  540. return retval;
  541. }
  542. static int __maybe_unused dwc2_suspend(struct device *dev)
  543. {
  544. struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
  545. int ret = 0;
  546. if (dwc2_is_device_mode(dwc2))
  547. dwc2_hsotg_suspend(dwc2);
  548. if (dwc2->ll_hw_enabled)
  549. ret = __dwc2_lowlevel_hw_disable(dwc2);
  550. return ret;
  551. }
  552. static int __maybe_unused dwc2_resume(struct device *dev)
  553. {
  554. struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
  555. int ret = 0;
  556. if (dwc2->ll_hw_enabled) {
  557. ret = __dwc2_lowlevel_hw_enable(dwc2);
  558. if (ret)
  559. return ret;
  560. }
  561. if (dwc2_is_device_mode(dwc2))
  562. ret = dwc2_hsotg_resume(dwc2);
  563. return ret;
  564. }
  565. static const struct dev_pm_ops dwc2_dev_pm_ops = {
  566. SET_SYSTEM_SLEEP_PM_OPS(dwc2_suspend, dwc2_resume)
  567. };
  568. static struct platform_driver dwc2_platform_driver = {
  569. .driver = {
  570. .name = dwc2_driver_name,
  571. .of_match_table = dwc2_of_match_table,
  572. .pm = &dwc2_dev_pm_ops,
  573. },
  574. .probe = dwc2_driver_probe,
  575. .remove = dwc2_driver_remove,
  576. .shutdown = dwc2_driver_shutdown,
  577. };
  578. module_platform_driver(dwc2_platform_driver);
  579. MODULE_DESCRIPTION("DESIGNWARE HS OTG Platform Glue");
  580. MODULE_AUTHOR("Matthijs Kooijman <matthijs@stdin.nl>");
  581. MODULE_LICENSE("Dual BSD/GPL");