dwc3_otg.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /**
  2. * dwc3_otg.c - DesignWare USB3 DRD Controller OTG
  3. *
  4. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/usb.h>
  17. #include <linux/usb/hcd.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. #include "core.h"
  21. #include "dwc3_otg.h"
  22. #include "io.h"
  23. #include "xhci.h"
  24. #define VBUS_REG_CHECK_DELAY (msecs_to_jiffies(1000))
  25. #define MAX_INVALID_CHRGR_RETRY 3
  26. static int max_chgr_retry_count = MAX_INVALID_CHRGR_RETRY;
  27. module_param(max_chgr_retry_count, int, S_IRUGO | S_IWUSR);
  28. MODULE_PARM_DESC(max_chgr_retry_count, "Max invalid charger retry count");
  29. static void dwc3_otg_reset(struct dwc3_otg *dotg);
  30. static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode);
  31. static void dwc3_otg_reset(struct dwc3_otg *dotg);
  32. /**
  33. * dwc3_otg_set_host_regs - reset dwc3 otg registers to host operation.
  34. *
  35. * This function sets the OTG registers to work in A-Device host mode.
  36. * This function should be called just before entering to A-Device mode.
  37. *
  38. * @w: Pointer to the dwc3 otg struct
  39. */
  40. static void dwc3_otg_set_host_regs(struct dwc3_otg *dotg)
  41. {
  42. u32 reg;
  43. struct dwc3 *dwc = dotg->dwc;
  44. struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
  45. if (ext_xceiv && !ext_xceiv->otg_capability) {
  46. /* Set OCTL[6](PeriMode) to 0 (host) */
  47. reg = dwc3_readl(dotg->regs, DWC3_OCTL);
  48. reg &= ~DWC3_OTG_OCTL_PERIMODE;
  49. dwc3_writel(dotg->regs, DWC3_OCTL, reg);
  50. } else {
  51. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  52. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  53. reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_HOST);
  54. /*
  55. * Allow ITP generated off of ref clk based counter instead
  56. * of UTMI/ULPI clk based counter, when superspeed only is
  57. * active so that UTMI/ULPI can be suspened.
  58. */
  59. reg |= DWC3_GCTL_SOFITPSYNC;
  60. /*
  61. * Set this bit so that device attempts three more times at SS,
  62. * even if it failed previously to operate in SS mode.
  63. */
  64. reg |= DWC3_GCTL_U2RSTECN;
  65. reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
  66. reg |= DWC3_GCTL_PWRDNSCALE(2);
  67. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  68. }
  69. }
  70. static int dwc3_otg_set_suspend(struct usb_phy *phy, int suspend)
  71. {
  72. struct usb_otg *otg = phy->otg;
  73. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  74. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  75. dev_info(otg->phy->dev, "%s host_bus_suspend=%d suspend=%d\n",
  76. __func__, dotg->host_bus_suspend, suspend);
  77. #endif
  78. if (dotg->host_bus_suspend == suspend)
  79. return 0;
  80. dotg->host_bus_suspend = suspend;
  81. if (suspend) {
  82. pm_runtime_put_sync(phy->dev);
  83. } else {
  84. pm_runtime_get_noresume(phy->dev);
  85. pm_runtime_resume(phy->dev);
  86. }
  87. return 0;
  88. }
  89. static void dwc3_otg_set_hsphy_auto_suspend(struct dwc3_otg *dotg, bool susp);
  90. static int dwc3_otg_set_autosuspend(struct usb_phy *phy, int enable_autosuspend)
  91. {
  92. struct usb_otg *otg = phy->otg;
  93. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  94. dwc3_otg_set_hsphy_auto_suspend(dotg, enable_autosuspend);
  95. return 0;
  96. }
  97. static void dwc3_otg_set_hsphy_auto_suspend(struct dwc3_otg *dotg, bool susp)
  98. {
  99. struct dwc3 *dwc = dotg->dwc;
  100. u32 reg;
  101. reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
  102. if (susp)
  103. reg |= DWC3_GUSB2PHYCFG_SUSPHY;
  104. else
  105. reg &= ~(DWC3_GUSB2PHYCFG_SUSPHY);
  106. dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
  107. }
  108. /**
  109. * dwc3_otg_set_host_power - Enable port power control for host operation
  110. *
  111. * This function enables the OTG Port Power required to operate in Host mode
  112. * This function should be called only after XHCI driver has set the port
  113. * power in PORTSC register.
  114. *
  115. * @w: Pointer to the dwc3 otg struct
  116. */
  117. void dwc3_otg_set_host_power(struct dwc3_otg *dotg)
  118. {
  119. u32 osts;
  120. osts = dwc3_readl(dotg->regs, DWC3_OSTS);
  121. if (!(osts & 0x8))
  122. dev_err(dotg->dwc->dev, "%s: xHCIPrtPower not set\n", __func__);
  123. dwc3_writel(dotg->regs, DWC3_OCTL, DWC3_OTG_OCTL_PRTPWRCTL);
  124. }
  125. /**
  126. * dwc3_otg_set_peripheral_regs - reset dwc3 otg registers to peripheral operation.
  127. *
  128. * This function sets the OTG registers to work in B-Device peripheral mode.
  129. * This function should be called just before entering to B-Device mode.
  130. *
  131. * @w: Pointer to the dwc3 otg workqueue.
  132. */
  133. static void dwc3_otg_set_peripheral_regs(struct dwc3_otg *dotg)
  134. {
  135. u32 reg;
  136. struct dwc3 *dwc = dotg->dwc;
  137. struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
  138. if (ext_xceiv && !ext_xceiv->otg_capability) {
  139. /* Set OCTL[6](PeriMode) to 1 (peripheral) */
  140. reg = dwc3_readl(dotg->regs, DWC3_OCTL);
  141. reg |= DWC3_OTG_OCTL_PERIMODE;
  142. dwc3_writel(dotg->regs, DWC3_OCTL, reg);
  143. /*
  144. * TODO: add more OTG registers writes for PERIPHERAL mode here,
  145. * see figure 12-19 B-device flow in dwc3 Synopsis spec
  146. */
  147. } else {
  148. reg = dwc3_readl(dwc->regs, DWC3_GCTL);
  149. reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
  150. reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
  151. /*
  152. * Set this bit so that device attempts three more times at SS,
  153. * even if it failed previously to operate in SS mode.
  154. */
  155. reg |= DWC3_GCTL_U2RSTECN;
  156. reg &= ~(DWC3_GCTL_PWRDNSCALEMASK);
  157. reg |= DWC3_GCTL_PWRDNSCALE(2);
  158. reg &= ~(DWC3_GCTL_SOFITPSYNC);
  159. dwc3_writel(dwc->regs, DWC3_GCTL, reg);
  160. }
  161. }
  162. /**
  163. * dwc3_otg_start_host - helper function for starting/stoping the host controller driver.
  164. *
  165. * @otg: Pointer to the otg_transceiver structure.
  166. * @on: start / stop the host controller driver.
  167. *
  168. * Returns 0 on success otherwise negative errno.
  169. */
  170. static int dwc3_otg_start_host(struct usb_otg *otg, int on)
  171. {
  172. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  173. struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
  174. struct dwc3 *dwc = dotg->dwc;
  175. int ret = 0;
  176. if (!dwc->xhci)
  177. return -EINVAL;
  178. #ifdef CONFIG_CHARGER_PM8941
  179. if (!dotg->vbus_otg) {
  180. dotg->vbus_otg = devm_regulator_get(dwc->dev->parent,
  181. "vbus_dwc3");
  182. if (IS_ERR(dotg->vbus_otg)) {
  183. dev_err(dwc->dev, "Failed to get vbus regulator\n");
  184. ret = PTR_ERR(dotg->vbus_otg);
  185. dotg->vbus_otg = 0;
  186. return ret;
  187. }
  188. }
  189. #endif
  190. if (on) {
  191. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  192. dev_info(otg->phy->dev, "%s: turn on host\n", __func__);
  193. #else
  194. dev_dbg(otg->phy->dev, "%s: turn on host\n", __func__);
  195. #endif
  196. dwc3_otg_notify_host_mode(otg, on);
  197. #ifdef CONFIG_CHARGER_PM8941
  198. ret = regulator_enable(dotg->vbus_otg);
  199. if (ret) {
  200. dev_err(otg->phy->dev, "unable to enable vbus_otg\n");
  201. dwc3_otg_notify_host_mode(otg, 0);
  202. return ret;
  203. }
  204. #endif
  205. /*
  206. * This should be revisited for more testing post-silicon.
  207. * In worst case we may need to disconnect the root hub
  208. * before stopping the controller so that it does not
  209. * interfere with runtime pm/system pm.
  210. * We can also consider registering and unregistering xhci
  211. * platform device. It is almost similar to add_hcd and
  212. * remove_hcd, But we may not use standard set_host method
  213. * anymore.
  214. */
  215. dwc3_otg_set_hsphy_auto_suspend(dotg, true);
  216. dwc3_otg_set_host_regs(dotg);
  217. /*
  218. * FIXME If micro A cable is disconnected during system suspend,
  219. * xhci platform device will be removed before runtime pm is
  220. * enabled for xhci device. Due to this, disable_depth becomes
  221. * greater than one and runtimepm is not enabled for next microA
  222. * connect. Fix this by calling pm_runtime_init for xhci device.
  223. */
  224. pm_runtime_init(&dwc->xhci->dev);
  225. ret = platform_device_add(dwc->xhci);
  226. if (ret) {
  227. dev_err(otg->phy->dev,
  228. "%s: failed to add XHCI pdev ret=%d\n",
  229. __func__, ret);
  230. regulator_disable(dotg->vbus_otg);
  231. dwc3_otg_notify_host_mode(otg, 0);
  232. return ret;
  233. }
  234. /* re-init OTG EVTEN register as XHCI reset clears it */
  235. if (ext_xceiv && !ext_xceiv->otg_capability)
  236. dwc3_otg_reset(dotg);
  237. } else {
  238. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  239. dev_info(otg->phy->dev, "%s: turn off host\n", __func__);
  240. #else
  241. dev_dbg(otg->phy->dev, "%s: turn off host\n", __func__);
  242. #endif
  243. #ifdef CONFIG_CHARGER_PM8941
  244. ret = regulator_disable(dotg->vbus_otg);
  245. if (ret) {
  246. dev_err(otg->phy->dev, "unable to disable vbus_otg\n");
  247. return ret;
  248. }
  249. #endif
  250. dwc3_otg_notify_host_mode(otg, on);
  251. platform_device_del(dwc->xhci);
  252. /*
  253. * Perform USB hardware RESET (both core reset and DBM reset)
  254. * when moving from host to peripheral. This is required for
  255. * peripheral mode to work.
  256. */
  257. if (ext_xceiv && ext_xceiv->otg_capability &&
  258. ext_xceiv->ext_block_reset)
  259. ext_xceiv->ext_block_reset(ext_xceiv, true);
  260. dwc3_otg_set_hsphy_auto_suspend(dotg, false);
  261. dwc3_otg_set_peripheral_regs(dotg);
  262. /* re-init core and OTG registers as block reset clears these */
  263. dwc3_post_host_reset_core_init(dwc);
  264. if (ext_xceiv && !ext_xceiv->otg_capability)
  265. dwc3_otg_reset(dotg);
  266. }
  267. return 0;
  268. }
  269. /**
  270. * dwc3_otg_set_host - bind/unbind the host controller driver.
  271. *
  272. * @otg: Pointer to the otg_transceiver structure.
  273. * @host: Pointer to the usb_bus structure.
  274. *
  275. * Returns 0 on success otherwise negative errno.
  276. */
  277. static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  278. {
  279. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  280. if (host) {
  281. dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n",
  282. __func__, host->bus_name);
  283. otg->host = host;
  284. /*
  285. * Though XHCI power would be set by now, but some delay is
  286. * required for XHCI controller before setting OTG Port Power
  287. * TODO: Tune this delay
  288. */
  289. msleep(300);
  290. dwc3_otg_set_host_power(dotg);
  291. } else {
  292. otg->host = NULL;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * dwc3_otg_start_peripheral - bind/unbind the peripheral controller.
  298. *
  299. * @otg: Pointer to the otg_transceiver structure.
  300. * @gadget: pointer to the usb_gadget structure.
  301. *
  302. * Returns 0 on success otherwise negative errno.
  303. */
  304. static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on)
  305. {
  306. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  307. struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
  308. if (!otg->gadget)
  309. return -EINVAL;
  310. if (on) {
  311. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  312. dev_info(otg->phy->dev, "%s: turn on gadget %s\n",
  313. __func__, otg->gadget->name);
  314. #else
  315. dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n",
  316. __func__, otg->gadget->name);
  317. #endif
  318. /* Core reset is not required during start peripheral. Only
  319. * DBM reset is required, hence perform only DBM reset here */
  320. if (ext_xceiv && ext_xceiv->otg_capability &&
  321. ext_xceiv->ext_block_reset)
  322. ext_xceiv->ext_block_reset(ext_xceiv, false);
  323. dwc3_otg_set_hsphy_auto_suspend(dotg, true);
  324. dwc3_otg_set_peripheral_regs(dotg);
  325. usb_gadget_vbus_connect(otg->gadget);
  326. } else {
  327. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  328. dev_info(otg->phy->dev, "%s: turn off gadget %s\n",
  329. __func__, otg->gadget->name);
  330. #else
  331. dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n",
  332. __func__, otg->gadget->name);
  333. #endif
  334. usb_gadget_vbus_disconnect(otg->gadget);
  335. dwc3_otg_set_hsphy_auto_suspend(dotg, false);
  336. }
  337. return 0;
  338. }
  339. /**
  340. * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver.
  341. *
  342. * @otg: Pointer to the otg_transceiver structure.
  343. * @gadget: pointer to the usb_gadget structure.
  344. *
  345. * Returns 0 on success otherwise negative errno.
  346. */
  347. static int dwc3_otg_set_peripheral(struct usb_otg *otg,
  348. struct usb_gadget *gadget)
  349. {
  350. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  351. if (gadget) {
  352. dev_dbg(otg->phy->dev, "%s: set gadget %s\n",
  353. __func__, gadget->name);
  354. otg->gadget = gadget;
  355. queue_delayed_work(system_nrt_wq, &dotg->sm_work, 0);
  356. } else {
  357. if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
  358. dwc3_otg_start_peripheral(otg, 0);
  359. otg->gadget = NULL;
  360. otg->phy->state = OTG_STATE_UNDEFINED;
  361. queue_delayed_work(system_nrt_wq, &dotg->sm_work, 0);
  362. } else {
  363. otg->gadget = NULL;
  364. }
  365. }
  366. return 0;
  367. }
  368. /**
  369. * dwc3_ext_chg_det_done - callback to handle charger detection completion
  370. * @otg: Pointer to the otg transceiver structure
  371. * @charger: Pointer to the external charger structure
  372. *
  373. * Returns 0 on success
  374. */
  375. static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg)
  376. {
  377. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  378. /*
  379. * Ignore chg_detection notification if BSV has gone off by this time.
  380. * STOP chg_det as part of !BSV handling would reset the chg_det flags
  381. */
  382. if (test_bit(B_SESS_VLD, &dotg->inputs))
  383. queue_delayed_work(system_nrt_wq, &dotg->sm_work, 0);
  384. }
  385. /**
  386. * dwc3_set_charger - bind/unbind external charger driver
  387. * @otg: Pointer to the otg transceiver structure
  388. * @charger: Pointer to the external charger structure
  389. *
  390. * Returns 0 on success
  391. */
  392. int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger)
  393. {
  394. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  395. dotg->charger = charger;
  396. if (charger)
  397. charger->notify_detection_complete = dwc3_ext_chg_det_done;
  398. return 0;
  399. }
  400. /**
  401. * dwc3_ext_event_notify - callback to handle events from external transceiver
  402. * @otg: Pointer to the otg transceiver structure
  403. * @event: Event reported by transceiver
  404. *
  405. * Returns 0 on success
  406. */
  407. static void dwc3_ext_event_notify(struct usb_otg *otg,
  408. enum dwc3_ext_events event)
  409. {
  410. static bool init;
  411. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  412. struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
  413. struct usb_phy *phy = dotg->otg.phy;
  414. int ret = 0;
  415. /* Flush processing any pending events before handling new ones */
  416. if (init)
  417. flush_delayed_work(&dotg->sm_work);
  418. if (event == DWC3_EVENT_PHY_RESUME) {
  419. if (!pm_runtime_status_suspended(phy->dev)) {
  420. dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n");
  421. } else {
  422. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  423. dev_info(phy->dev, "ext PHY_RESUME event received\n");
  424. #else
  425. dev_dbg(phy->dev, "ext PHY_RESUME event received\n");
  426. #endif
  427. /* ext_xceiver would have taken h/w out of LPM by now */
  428. ret = pm_runtime_get(phy->dev);
  429. if ((phy->state == OTG_STATE_A_HOST) &&
  430. dotg->host_bus_suspend)
  431. dotg->host_bus_suspend = 0;
  432. if (ret == -EACCES) {
  433. /* pm_runtime_get may fail during system
  434. resume with -EACCES error */
  435. pm_runtime_disable(phy->dev);
  436. pm_runtime_set_active(phy->dev);
  437. pm_runtime_enable(phy->dev);
  438. } else if (ret < 0) {
  439. dev_warn(phy->dev, "pm_runtime_get failed!\n");
  440. }
  441. }
  442. } else if (event == DWC3_EVENT_XCEIV_STATE) {
  443. if (pm_runtime_status_suspended(phy->dev)) {
  444. dev_warn(phy->dev, "PHY_STATE event in LPM!!!!\n");
  445. ret = pm_runtime_get(phy->dev);
  446. if (ret < 0)
  447. dev_warn(phy->dev, "pm_runtime_get failed!!\n");
  448. }
  449. if (ext_xceiv->id == DWC3_ID_FLOAT) {
  450. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  451. dev_info(phy->dev, "XCVR: ID set\n");
  452. #else
  453. dev_dbg(phy->dev, "XCVR: ID set\n");
  454. #endif
  455. set_bit(ID, &dotg->inputs);
  456. } else {
  457. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  458. dev_info(phy->dev, "XCVR: ID clear\n");
  459. #else
  460. dev_dbg(phy->dev, "XCVR: ID clear\n");
  461. #endif
  462. clear_bit(ID, &dotg->inputs);
  463. }
  464. if (ext_xceiv->bsv) {
  465. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  466. dev_info(phy->dev, "XCVR: BSV set\n");
  467. #else
  468. dev_dbg(phy->dev, "XCVR: BSV set\n");
  469. #endif
  470. set_bit(B_SESS_VLD, &dotg->inputs);
  471. } else {
  472. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  473. dev_info(phy->dev, "XCVR: BSV clear\n");
  474. #else
  475. dev_dbg(phy->dev, "XCVR: BSV clear\n");
  476. #endif
  477. clear_bit(B_SESS_VLD, &dotg->inputs);
  478. }
  479. if (!init) {
  480. init = true;
  481. if (!work_busy(&dotg->sm_work.work))
  482. queue_delayed_work(system_nrt_wq,
  483. &dotg->sm_work, 0);
  484. complete(&dotg->dwc3_xcvr_vbus_init);
  485. dev_dbg(phy->dev, "XCVR: BSV init complete\n");
  486. return;
  487. }
  488. queue_delayed_work(system_nrt_wq, &dotg->sm_work, 0);
  489. }
  490. }
  491. /**
  492. * dwc3_set_ext_xceiv - bind/unbind external transceiver driver
  493. * @otg: Pointer to the otg transceiver structure
  494. * @ext_xceiv: Pointer to the external transceiver struccture
  495. *
  496. * Returns 0 on success
  497. */
  498. int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv)
  499. {
  500. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  501. dotg->ext_xceiv = ext_xceiv;
  502. if (ext_xceiv)
  503. ext_xceiv->notify_ext_events = dwc3_ext_event_notify;
  504. return 0;
  505. }
  506. static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode)
  507. {
  508. struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
  509. if (!dotg->psy) {
  510. dev_err(otg->phy->dev, "no usb power supply registered\n");
  511. return;
  512. }
  513. if (host_mode)
  514. power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM);
  515. else
  516. power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE);
  517. }
  518. static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA)
  519. {
  520. static int power_supply_type;
  521. struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg);
  522. #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
  523. return 0;
  524. #endif
  525. if (!dotg->psy || !dotg->charger) {
  526. dev_err(phy->dev, "no usb power supply/charger registered\n");
  527. return 0;
  528. }
  529. if (dotg->charger->charging_disabled)
  530. return 0;
  531. if (dotg->charger->chg_type == DWC3_SDP_CHARGER)
  532. power_supply_type = POWER_SUPPLY_TYPE_USB;
  533. else if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
  534. power_supply_type = POWER_SUPPLY_TYPE_USB_CDP;
  535. else if (dotg->charger->chg_type == DWC3_DCP_CHARGER ||
  536. dotg->charger->chg_type == DWC3_PROPRIETARY_CHARGER)
  537. power_supply_type = POWER_SUPPLY_TYPE_USB_DCP;
  538. else
  539. power_supply_type = POWER_SUPPLY_TYPE_UNKNOWN;
  540. power_supply_set_supply_type(dotg->psy, power_supply_type);
  541. if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
  542. mA = DWC3_IDEV_CHG_MAX;
  543. if (dotg->charger->max_power == mA)
  544. return 0;
  545. dev_info(phy->dev, "Avail curr from USB = %u\n", mA);
  546. if (dotg->charger->max_power <= 2 && mA > 2) {
  547. /* Enable charging */
  548. if (power_supply_set_online(dotg->psy, true))
  549. goto psy_error;
  550. if (power_supply_set_current_limit(dotg->psy, 1000*mA))
  551. goto psy_error;
  552. } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) {
  553. /* Disable charging */
  554. if (power_supply_set_online(dotg->psy, false))
  555. goto psy_error;
  556. /* Set max current limit */
  557. if (power_supply_set_current_limit(dotg->psy, 0))
  558. goto psy_error;
  559. }
  560. power_supply_changed(dotg->psy);
  561. dotg->charger->max_power = mA;
  562. return 0;
  563. psy_error:
  564. dev_dbg(phy->dev, "power supply error when setting property\n");
  565. return -ENXIO;
  566. }
  567. /* IRQs which OTG driver is interested in handling */
  568. #define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
  569. DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
  570. /**
  571. * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
  572. * @_dotg: Pointer to out controller context structure
  573. *
  574. * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
  575. */
  576. static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
  577. {
  578. struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
  579. u32 osts, oevt_reg;
  580. int ret = IRQ_NONE;
  581. int handled_irqs = 0;
  582. struct usb_phy *phy = dotg->otg.phy;
  583. oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
  584. if (!(oevt_reg & DWC3_OEVT_MASK))
  585. return IRQ_NONE;
  586. osts = dwc3_readl(dotg->regs, DWC3_OSTS);
  587. if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
  588. (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
  589. /*
  590. * ID sts has changed, set inputs later, in the workqueue
  591. * function, switch from A to B or from B to A.
  592. */
  593. if (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) {
  594. if (osts & DWC3_OTG_OSTS_CONIDSTS) {
  595. dev_dbg(phy->dev, "ID set\n");
  596. set_bit(ID, &dotg->inputs);
  597. } else {
  598. dev_dbg(phy->dev, "ID clear\n");
  599. clear_bit(ID, &dotg->inputs);
  600. }
  601. handled_irqs |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT;
  602. }
  603. if (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) {
  604. if (osts & DWC3_OTG_OSTS_BSESVALID) {
  605. dev_dbg(phy->dev, "BSV set\n");
  606. set_bit(B_SESS_VLD, &dotg->inputs);
  607. } else {
  608. dev_dbg(phy->dev, "BSV clear\n");
  609. clear_bit(B_SESS_VLD, &dotg->inputs);
  610. }
  611. handled_irqs |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT;
  612. }
  613. queue_delayed_work(system_nrt_wq, &dotg->sm_work, 0);
  614. ret = IRQ_HANDLED;
  615. /* Clear the interrupts we handled */
  616. dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
  617. }
  618. return ret;
  619. }
  620. /**
  621. * dwc3_otg_init_sm - initialize OTG statemachine input
  622. * @dotg: Pointer to the dwc3_otg structure
  623. *
  624. */
  625. void dwc3_otg_init_sm(struct dwc3_otg *dotg)
  626. {
  627. u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
  628. struct usb_phy *phy = dotg->otg.phy;
  629. struct dwc3_ext_xceiv *ext_xceiv;
  630. int ret;
  631. dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
  632. /*
  633. * VBUS initial state is reported after PMIC
  634. * driver initialization. Wait for it.
  635. */
  636. ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5);
  637. if (!ret) {
  638. dev_err(phy->dev, "%s: completion timeout\n", __func__);
  639. /* We can safely assume no cable connected */
  640. set_bit(ID, &dotg->inputs);
  641. }
  642. ext_xceiv = dotg->ext_xceiv;
  643. dwc3_otg_reset(dotg);
  644. if (ext_xceiv && !ext_xceiv->otg_capability) {
  645. if (osts & DWC3_OTG_OSTS_CONIDSTS)
  646. set_bit(ID, &dotg->inputs);
  647. else
  648. clear_bit(ID, &dotg->inputs);
  649. if (osts & DWC3_OTG_OSTS_BSESVALID)
  650. set_bit(B_SESS_VLD, &dotg->inputs);
  651. else
  652. clear_bit(B_SESS_VLD, &dotg->inputs);
  653. }
  654. }
  655. /**
  656. * dwc3_otg_sm_work - workqueue function.
  657. *
  658. * @w: Pointer to the dwc3 otg workqueue
  659. *
  660. * NOTE: After any change in phy->state,
  661. * we must reschdule the state machine.
  662. */
  663. static void dwc3_otg_sm_work(struct work_struct *w)
  664. {
  665. struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work.work);
  666. struct usb_phy *phy = dotg->otg.phy;
  667. struct dwc3_charger *charger = dotg->charger;
  668. bool work = 0;
  669. int ret = 0;
  670. unsigned long delay = 0;
  671. pm_runtime_resume(phy->dev);
  672. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  673. dev_info(phy->dev, "%s state-->\n", otg_state_string(phy->state));
  674. #else
  675. dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
  676. #endif
  677. /* Check OTG state */
  678. switch (phy->state) {
  679. case OTG_STATE_UNDEFINED:
  680. dwc3_otg_init_sm(dotg);
  681. if (!dotg->psy) {
  682. dotg->psy = power_supply_get_by_name("dwc-usb");
  683. if (!dotg->psy)
  684. dev_err(phy->dev,
  685. "couldn't get usb power supply\n");
  686. }
  687. /* Switch to A or B-Device according to ID / BSV */
  688. if (!test_bit(ID, &dotg->inputs)) {
  689. dev_dbg(phy->dev, "!id\n");
  690. phy->state = OTG_STATE_A_IDLE;
  691. work = 1;
  692. } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
  693. dev_dbg(phy->dev, "b_sess_vld\n");
  694. phy->state = OTG_STATE_B_IDLE;
  695. work = 1;
  696. } else {
  697. phy->state = OTG_STATE_B_IDLE;
  698. dev_dbg(phy->dev, "No device, trying to suspend\n");
  699. pm_runtime_put_sync(phy->dev);
  700. }
  701. break;
  702. case OTG_STATE_B_IDLE:
  703. if (!test_bit(ID, &dotg->inputs)) {
  704. dev_dbg(phy->dev, "!id\n");
  705. phy->state = OTG_STATE_A_IDLE;
  706. work = 1;
  707. dotg->charger_retry_count = 0;
  708. if (charger) {
  709. if (charger->chg_type == DWC3_INVALID_CHARGER)
  710. charger->start_detection(dotg->charger,
  711. false);
  712. else
  713. charger->chg_type =
  714. DWC3_INVALID_CHARGER;
  715. }
  716. } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
  717. dev_dbg(phy->dev, "b_sess_vld\n");
  718. if (charger) {
  719. /* Has charger been detected? If no detect it */
  720. switch (charger->chg_type) {
  721. case DWC3_DCP_CHARGER:
  722. case DWC3_PROPRIETARY_CHARGER:
  723. dev_dbg(phy->dev, "lpm, DCP charger\n");
  724. dwc3_otg_set_power(phy,
  725. DWC3_IDEV_CHG_MAX);
  726. pm_runtime_put_sync(phy->dev);
  727. break;
  728. case DWC3_CDP_CHARGER:
  729. dwc3_otg_set_power(phy,
  730. DWC3_IDEV_CHG_MAX);
  731. dwc3_otg_start_peripheral(&dotg->otg,
  732. 1);
  733. phy->state = OTG_STATE_B_PERIPHERAL;
  734. work = 1;
  735. break;
  736. case DWC3_SDP_CHARGER:
  737. dwc3_otg_start_peripheral(&dotg->otg,
  738. 1);
  739. phy->state = OTG_STATE_B_PERIPHERAL;
  740. work = 1;
  741. break;
  742. case DWC3_FLOATED_CHARGER:
  743. if (dotg->charger_retry_count <
  744. max_chgr_retry_count)
  745. dotg->charger_retry_count++;
  746. /*
  747. * In case of floating charger, if
  748. * retry count equal to max retry count
  749. * notify PMIC about floating charger
  750. * and put Hw in low power mode. Else
  751. * perform charger detection again by
  752. * calling start_detection() with false
  753. * and then with true argument.
  754. */
  755. if (dotg->charger_retry_count ==
  756. max_chgr_retry_count) {
  757. dwc3_otg_set_power(phy, 0);
  758. pm_runtime_put_sync(phy->dev);
  759. break;
  760. }
  761. charger->start_detection(dotg->charger,
  762. false);
  763. default:
  764. dev_dbg(phy->dev, "chg_det started\n");
  765. charger->start_detection(charger, true);
  766. break;
  767. }
  768. } else {
  769. /* no charger registered, start peripheral */
  770. if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
  771. /*
  772. * Probably set_peripheral not called
  773. * yet. We will re-try as soon as it
  774. * will be called
  775. */
  776. dev_err(phy->dev, "enter lpm as\n"
  777. "unable to start B-device\n");
  778. phy->state = OTG_STATE_UNDEFINED;
  779. pm_runtime_put_sync(phy->dev);
  780. return;
  781. }
  782. }
  783. } else {
  784. if (charger)
  785. charger->start_detection(dotg->charger, false);
  786. dotg->charger_retry_count = 0;
  787. dwc3_otg_set_power(phy, 0);
  788. dev_dbg(phy->dev, "No device, trying to suspend\n");
  789. pm_runtime_put_sync(phy->dev);
  790. }
  791. break;
  792. case OTG_STATE_B_PERIPHERAL:
  793. if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
  794. !test_bit(ID, &dotg->inputs)) {
  795. dev_dbg(phy->dev, "!id || !bsv\n");
  796. dwc3_otg_start_peripheral(&dotg->otg, 0);
  797. phy->state = OTG_STATE_B_IDLE;
  798. if (charger)
  799. charger->chg_type = DWC3_INVALID_CHARGER;
  800. work = 1;
  801. }
  802. break;
  803. case OTG_STATE_A_IDLE:
  804. /* Switch to A-Device*/
  805. if (test_bit(ID, &dotg->inputs)) {
  806. dev_dbg(phy->dev, "id\n");
  807. phy->state = OTG_STATE_B_IDLE;
  808. dotg->vbus_retry_count = 0;
  809. work = 1;
  810. } else {
  811. pm_runtime_get_noresume(phy->dev);
  812. phy->state = OTG_STATE_A_HOST;
  813. ret = dwc3_otg_start_host(&dotg->otg, 1);
  814. if ((ret == -EPROBE_DEFER) &&
  815. dotg->vbus_retry_count < 3) {
  816. /*
  817. * Get regulator failed as regulator driver is
  818. * not up yet. Will try to start host after 1sec
  819. */
  820. pm_runtime_put_noidle(phy->dev);
  821. phy->state = OTG_STATE_A_IDLE;
  822. dev_dbg(phy->dev, "Unable to get vbus regulator. Retrying...\n");
  823. delay = VBUS_REG_CHECK_DELAY;
  824. work = 1;
  825. dotg->vbus_retry_count++;
  826. } else if (ret) {
  827. /*
  828. * Probably set_host was not called yet.
  829. * We will re-try as soon as it will be called
  830. */
  831. pm_runtime_put_noidle(phy->dev);
  832. dev_dbg(phy->dev, "enter lpm as\n"
  833. "unable to start A-device\n");
  834. phy->state = OTG_STATE_A_IDLE;
  835. pm_runtime_put_sync(phy->dev);
  836. return;
  837. }
  838. }
  839. break;
  840. case OTG_STATE_A_HOST:
  841. if (test_bit(ID, &dotg->inputs)) {
  842. dev_dbg(phy->dev, "id\n");
  843. dwc3_otg_start_host(&dotg->otg, 0);
  844. phy->state = OTG_STATE_B_IDLE;
  845. dotg->vbus_retry_count = 0;
  846. work = 1;
  847. pm_runtime_put_noidle(phy->dev);
  848. }
  849. break;
  850. default:
  851. dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
  852. }
  853. #ifdef CONFIG_USB_DEBUG_DETEAILED_LOG
  854. dev_info(phy->dev, "-->%s state\n", otg_state_string(phy->state));
  855. #endif
  856. if (work)
  857. queue_delayed_work(system_nrt_wq, &dotg->sm_work, delay);
  858. }
  859. /**
  860. * dwc3_otg_reset - reset dwc3 otg registers.
  861. *
  862. * @w: Pointer to the dwc3 otg workqueue
  863. */
  864. static void dwc3_otg_reset(struct dwc3_otg *dotg)
  865. {
  866. static int once;
  867. struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
  868. /*
  869. * OCFG[2] - OTG-Version = 1
  870. * OCFG[1] - HNPCap = 0
  871. * OCFG[0] - SRPCap = 0
  872. */
  873. if (ext_xceiv && !ext_xceiv->otg_capability)
  874. dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
  875. /*
  876. * OCTL[6] - PeriMode = 1
  877. * OCTL[5] - PrtPwrCtl = 0
  878. * OCTL[4] - HNPReq = 0
  879. * OCTL[3] - SesReq = 0
  880. * OCTL[2] - TermSelDLPulse = 0
  881. * OCTL[1] - DevSetHNPEn = 0
  882. * OCTL[0] - HstSetHNPEn = 0
  883. */
  884. if (!once) {
  885. if (ext_xceiv && !ext_xceiv->otg_capability)
  886. dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
  887. once++;
  888. }
  889. /* Clear all otg events (interrupts) indications */
  890. dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
  891. /* Enable ID/BSV StsChngEn event*/
  892. if (ext_xceiv && !ext_xceiv->otg_capability)
  893. dwc3_writel(dotg->regs, DWC3_OEVTEN,
  894. DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
  895. DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
  896. }
  897. /**
  898. * dwc3_otg_init - Initializes otg related registers
  899. * @dwc: Pointer to out controller context structure
  900. *
  901. * Returns 0 on success otherwise negative errno.
  902. */
  903. int dwc3_otg_init(struct dwc3 *dwc)
  904. {
  905. u32 reg;
  906. int ret = 0;
  907. struct dwc3_otg *dotg;
  908. dev_dbg(dwc->dev, "dwc3_otg_init\n");
  909. /*
  910. * GHWPARAMS6[10] bit is SRPSupport.
  911. * This bit also reflects DWC_USB3_EN_OTG
  912. */
  913. reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
  914. if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
  915. /*
  916. * No OTG support in the HW core.
  917. * We return 0 to indicate no error, since this is acceptable
  918. * situation, just continue probe the dwc3 driver without otg.
  919. */
  920. dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
  921. return 0;
  922. }
  923. /* Allocate and init otg instance */
  924. dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
  925. if (!dotg) {
  926. dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
  927. return -ENOMEM;
  928. }
  929. /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
  930. dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
  931. "otg_irq");
  932. if (dotg->irq < 0) {
  933. dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
  934. ret = -ENODEV;
  935. goto err1;
  936. }
  937. dotg->regs = dwc->regs;
  938. dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
  939. dotg->otg.set_host = dwc3_otg_set_host;
  940. /* This reference is used by dwc3 modules for checking otg existance */
  941. dwc->dotg = dotg;
  942. dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
  943. if (!dotg->otg.phy) {
  944. dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
  945. ret = -ENOMEM;
  946. goto err1;
  947. }
  948. dotg->dwc = dwc;
  949. dotg->otg.phy->otg = &dotg->otg;
  950. dotg->otg.phy->dev = dwc->dev;
  951. dotg->otg.phy->set_power = dwc3_otg_set_power;
  952. dotg->otg.phy->set_suspend = dwc3_otg_set_suspend;
  953. dotg->otg.phy->set_phy_autosuspend = dwc3_otg_set_autosuspend;
  954. #ifdef CONFIG_USB_HOST_NOTIFY
  955. dotg->otg.phy->set_suspend = NULL;
  956. #endif
  957. ret = usb_set_transceiver(dotg->otg.phy);
  958. if (ret) {
  959. dev_err(dotg->otg.phy->dev,
  960. "%s: failed to set transceiver, already exists\n",
  961. __func__);
  962. goto err2;
  963. }
  964. dotg->otg.phy->state = OTG_STATE_UNDEFINED;
  965. init_completion(&dotg->dwc3_xcvr_vbus_init);
  966. INIT_DELAYED_WORK(&dotg->sm_work, dwc3_otg_sm_work);
  967. ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
  968. "dwc3_otg", dotg);
  969. if (ret) {
  970. dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
  971. dotg->irq, ret);
  972. goto err3;
  973. }
  974. pm_runtime_get(dwc->dev);
  975. return 0;
  976. err3:
  977. cancel_delayed_work_sync(&dotg->sm_work);
  978. usb_set_transceiver(NULL);
  979. err2:
  980. kfree(dotg->otg.phy);
  981. err1:
  982. dwc->dotg = NULL;
  983. kfree(dotg);
  984. return ret;
  985. }
  986. /**
  987. * dwc3_otg_exit
  988. * @dwc: Pointer to out controller context structure
  989. *
  990. * Returns 0 on success otherwise negative errno.
  991. */
  992. void dwc3_otg_exit(struct dwc3 *dwc)
  993. {
  994. struct dwc3_otg *dotg = dwc->dotg;
  995. /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
  996. if (dotg) {
  997. if (dotg->charger)
  998. dotg->charger->start_detection(dotg->charger, false);
  999. cancel_delayed_work_sync(&dotg->sm_work);
  1000. usb_set_transceiver(NULL);
  1001. pm_runtime_put(dwc->dev);
  1002. free_irq(dotg->irq, dotg);
  1003. kfree(dotg->otg.phy);
  1004. kfree(dotg);
  1005. dwc->dotg = NULL;
  1006. }
  1007. }